結果

問題 No.798 コレクション
ユーザー 37zigen
提出日時 2018-08-05 20:01:07
言語 Java
(openjdk 23)
結果
RE  
実行時間 -
コード長 1,345 bytes
コンパイル時間 2,623 ms
コンパイル使用メモリ 79,872 KB
実行使用メモリ 42,924 KB
最終ジャッジ日時 2024-06-28 16:53:46
合計ジャッジ時間 5,973 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 11 RE * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		if (!(1 <= N && N <= 17))
			throw new AssertionError();
		long[][] A = new long[N][2];
		for (int i = 0; i < N; ++i) {
			A[i][0] = sc.nextLong();
			A[i][1] = sc.nextLong();
			if (!(1 <= A[i][0] && A[i][0] <= 1e5 && 1 <= A[i][1] && A[i][1] <= 1e5))
				throw new AssertionError();
		}
		Arrays.sort(A, new Comparator<long[]>() {
			@Override
			public int compare(long[] o1, long[] o2) {
				return -Long.compare(o1[1], o2[1]);
			}
		});
		long[][] dp = new long[N + 1][N + 1];
		for (int i = 0; i < dp.length; ++i)
			for (int j = 0; j < dp[i].length; ++j)
				dp[i][j] = Long.MAX_VALUE / 3;
		dp[0][0] = 0;
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j <= N; ++j) {
				if (j + 1 <= N)
					dp[i + 1][j + 1] = Math.min(dp[i + 1][j + 1], dp[i][j] + A[i][0] + A[i][1] * j);
				dp[i + 1][j] = Math.min(dp[i + 1][j], dp[i][j]);
			}
		}
		int sub = 0;
		for (int i = 0, res = N; res > 0; ++i) {
			--res;
			if (i % 2 == 1 && res > 0) {
				--res;
				++sub;
			}
		}
		System.out.println(dp[N][N - sub]);
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0