baru003のブログ

baruの雑記兼備忘録

AOJ 0112

・問題リンク A Milk Shop

・コメント
ポイントはArrayListとCollectionsの連携ですかね。それと、先程int型でやっていてWAになっていたことにずっと気づかずlong型にしたところacceptでした(笑)
たぶん、待ち時間の合計がint型での領域を超えてしまっていたのかもしれませんね。詳しいことはわかりませんが以下のソースでacceptできました。

・ソース

import java.util.Scanner;
import java.util.Collections;
import java.util.ArrayList;

public class P0112 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (true) {
			int n = sc.nextInt();
			if (n == 0)
				break;
			ArrayList<Integer> list = new ArrayList<Integer>();
			for (int i = 0; i < n; i++) {
				list.add(sc.nextInt());
			}
			Collections.sort(list);
			long[] a = new long[n];
			long ans = 0;
			long v = list.get(0);
			a[0] = 0;
			for (int i = 1; i < n; i++) {
				a[i] = v;
				v += list.get(i);
				ans += a[i];
			}
			System.out.println(ans);
		}
	}
}