結果
問題 | No.798 コレクション |
ユーザー |
|
提出日時 | 2018-08-05 19:38:16 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 395 ms / 2,000 ms |
コード長 | 1,178 bytes |
コンパイル時間 | 2,212 ms |
コンパイル使用メモリ | 79,296 KB |
実行使用メモリ | 80,064 KB |
最終ジャッジ日時 | 2024-06-28 16:53:56 |
合計ジャッジ時間 | 8,853 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 23 |
ソースコード
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(); long[][] A = new long[N][2]; for (int i = 0; i < N; ++i) { A[i][0] = sc.nextLong(); A[i][1] = sc.nextLong(); } 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)); } }