您的位置:首页 > 其它

题目1107:搬水果

2014-01-07 21:19 267 查看
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.Scanner;
import java.util.PriorityQueue;

class Main
{
public static final boolean DEBUG = false;

public static void main(String[] args) throws IOException
{
Scanner cin;
int n;

if (DEBUG) {
cin = new Scanner(new BufferedReader(new FileReader("d:\\OJ\\uva_in.txt")));
} else {
cin = new Scanner(System.in);
}

while (cin.hasNext()) {
n = cin.nextInt();
if (n == 0) break;

PriorityQueue<Integer> q = new PriorityQueue<Integer>();

for (int i = 0; i < n; i++) {
q.add(new Integer(cin.nextInt()));
}

int sum = 0;
for (int i = 0; i < n - 1; i++) {
int a = q.poll().intValue();
int b= q.poll().intValue();

sum += a + b;

q.add(new Integer(a + b));
}

while (!q.isEmpty()) {
q.poll();
}

System.out.println(sum);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: