結果
問題 | No.1858 Gorgeous Knapsack |
ユーザー | tenten |
提出日時 | 2023-01-17 13:13:56 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,364 ms / 2,000 ms |
コード長 | 2,896 bytes |
コンパイル時間 | 2,916 ms |
コンパイル使用メモリ | 81,480 KB |
実行使用メモリ | 448,780 KB |
最終ジャッジ日時 | 2023-08-30 18:00:28 |
合計ジャッジ時間 | 17,097 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
49,544 KB |
testcase_01 | AC | 41 ms
49,876 KB |
testcase_02 | AC | 43 ms
49,792 KB |
testcase_03 | AC | 44 ms
49,528 KB |
testcase_04 | AC | 288 ms
131,252 KB |
testcase_05 | AC | 125 ms
67,276 KB |
testcase_06 | AC | 339 ms
147,588 KB |
testcase_07 | AC | 271 ms
113,376 KB |
testcase_08 | AC | 464 ms
189,740 KB |
testcase_09 | AC | 891 ms
343,968 KB |
testcase_10 | AC | 319 ms
124,316 KB |
testcase_11 | AC | 435 ms
180,412 KB |
testcase_12 | AC | 624 ms
230,864 KB |
testcase_13 | AC | 182 ms
64,204 KB |
testcase_14 | AC | 43 ms
49,648 KB |
testcase_15 | AC | 45 ms
49,584 KB |
testcase_16 | AC | 44 ms
49,580 KB |
testcase_17 | AC | 45 ms
49,764 KB |
testcase_18 | AC | 44 ms
49,748 KB |
testcase_19 | AC | 42 ms
49,824 KB |
testcase_20 | AC | 42 ms
49,852 KB |
testcase_21 | AC | 43 ms
49,736 KB |
testcase_22 | AC | 42 ms
49,572 KB |
testcase_23 | AC | 43 ms
49,800 KB |
testcase_24 | AC | 168 ms
87,176 KB |
testcase_25 | AC | 565 ms
225,032 KB |
testcase_26 | AC | 457 ms
189,216 KB |
testcase_27 | AC | 417 ms
190,672 KB |
testcase_28 | AC | 413 ms
140,300 KB |
testcase_29 | AC | 114 ms
54,216 KB |
testcase_30 | AC | 116 ms
53,100 KB |
testcase_31 | AC | 115 ms
53,796 KB |
testcase_32 | AC | 115 ms
53,124 KB |
testcase_33 | AC | 130 ms
56,220 KB |
testcase_34 | AC | 1,364 ms
446,388 KB |
testcase_35 | AC | 568 ms
443,540 KB |
testcase_36 | AC | 1,280 ms
448,780 KB |
testcase_37 | AC | 42 ms
49,648 KB |
testcase_38 | AC | 113 ms
54,524 KB |
testcase_39 | AC | 42 ms
49,560 KB |
testcase_40 | AC | 515 ms
445,464 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(); } }