結果
問題 | No.1858 Gorgeous Knapsack |
ユーザー | tenten |
提出日時 | 2023-01-17 13:13:56 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,312 ms / 2,000 ms |
コード長 | 2,896 bytes |
コンパイル時間 | 2,805 ms |
コンパイル使用メモリ | 92,556 KB |
実行使用メモリ | 445,148 KB |
最終ジャッジ日時 | 2024-06-10 17:36:31 |
合計ジャッジ時間 | 15,347 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 48 ms
50,348 KB |
testcase_01 | AC | 49 ms
50,152 KB |
testcase_02 | AC | 48 ms
50,332 KB |
testcase_03 | AC | 52 ms
52,404 KB |
testcase_04 | AC | 318 ms
132,428 KB |
testcase_05 | AC | 160 ms
67,232 KB |
testcase_06 | AC | 364 ms
144,184 KB |
testcase_07 | AC | 289 ms
111,016 KB |
testcase_08 | AC | 492 ms
186,808 KB |
testcase_09 | AC | 895 ms
343,432 KB |
testcase_10 | AC | 403 ms
129,836 KB |
testcase_11 | AC | 439 ms
180,684 KB |
testcase_12 | AC | 721 ms
232,528 KB |
testcase_13 | AC | 183 ms
63,952 KB |
testcase_14 | AC | 51 ms
50,412 KB |
testcase_15 | AC | 51 ms
50,532 KB |
testcase_16 | AC | 51 ms
50,588 KB |
testcase_17 | AC | 51 ms
50,560 KB |
testcase_18 | AC | 52 ms
50,488 KB |
testcase_19 | AC | 52 ms
50,568 KB |
testcase_20 | AC | 53 ms
50,024 KB |
testcase_21 | AC | 49 ms
50,508 KB |
testcase_22 | AC | 49 ms
50,508 KB |
testcase_23 | AC | 50 ms
50,496 KB |
testcase_24 | AC | 193 ms
87,420 KB |
testcase_25 | AC | 559 ms
227,116 KB |
testcase_26 | AC | 468 ms
187,632 KB |
testcase_27 | AC | 437 ms
187,908 KB |
testcase_28 | AC | 386 ms
137,720 KB |
testcase_29 | AC | 123 ms
52,712 KB |
testcase_30 | AC | 123 ms
52,836 KB |
testcase_31 | AC | 118 ms
53,060 KB |
testcase_32 | AC | 108 ms
52,492 KB |
testcase_33 | AC | 129 ms
55,984 KB |
testcase_34 | AC | 1,312 ms
445,088 KB |
testcase_35 | AC | 616 ms
444,904 KB |
testcase_36 | AC | 1,256 ms
445,148 KB |
testcase_37 | AC | 51 ms
50,328 KB |
testcase_38 | AC | 120 ms
53,092 KB |
testcase_39 | AC | 49 ms
50,464 KB |
testcase_40 | AC | 565 ms
444,840 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.stream.*; public class Main { static long[][][] dp; static Juel[] juels; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int m = sc.nextInt(); juels = new Juel[n]; for (int i = 0; i < n; i++) { juels[i] = new Juel(sc.nextInt(), sc.nextInt()); } Arrays.sort(juels); dp = new long[2][n][m + 1]; for (long[][] arr1 : dp) { for (long[] arr2 : arr1) { Arrays.fill(arr2, -1); } } System.out.println(dfw(0, n - 1, m)); } static long dfw(int type, int idx, int w) { if (w < 0) { return Integer.MIN_VALUE; } if (idx < 0) { return 0; } if (dp[type][idx][w] < 0) { dp[type][idx][w] = dfw(type, idx - 1, w); if (type == 0) { dp[type][idx][w] = Math.max(dp[type][idx][w], (dfw(1, idx - 1, w - juels[idx].weight) + juels[idx].value) * juels[idx].value); } else { dp[type][idx][w] = Math.max(dp[type][idx][w], (dfw(1, idx - 1, w - juels[idx].weight) + juels[idx].value)); } } return dp[type][idx][w]; } static class Juel implements Comparable<Juel> { int weight; int value; public Juel(int value, int weight) { this.weight = weight; this.value = value; } public int compareTo(Juel another) { return another.value - value; } } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }