結果

問題 No.2232 Miser's Gift
ユーザー tentententen
提出日時 2023-08-08 18:59:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 408 ms / 2,000 ms
コード長 2,502 bytes
コンパイル時間 2,668 ms
コンパイル使用メモリ 84,536 KB
実行使用メモリ 97,048 KB
最終ジャッジ日時 2024-04-26 11:27:22
合計ジャッジ時間 18,273 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
36,824 KB
testcase_01 AC 48 ms
37,056 KB
testcase_02 AC 48 ms
37,140 KB
testcase_03 AC 317 ms
80,216 KB
testcase_04 AC 321 ms
96,128 KB
testcase_05 AC 128 ms
46,528 KB
testcase_06 AC 46 ms
36,888 KB
testcase_07 AC 139 ms
44,280 KB
testcase_08 AC 327 ms
95,880 KB
testcase_09 AC 336 ms
96,232 KB
testcase_10 AC 338 ms
96,180 KB
testcase_11 AC 340 ms
95,944 KB
testcase_12 AC 354 ms
96,592 KB
testcase_13 AC 312 ms
80,332 KB
testcase_14 AC 312 ms
80,204 KB
testcase_15 AC 307 ms
80,340 KB
testcase_16 AC 315 ms
80,432 KB
testcase_17 AC 304 ms
80,116 KB
testcase_18 AC 300 ms
80,436 KB
testcase_19 AC 320 ms
80,104 KB
testcase_20 AC 309 ms
80,064 KB
testcase_21 AC 305 ms
80,512 KB
testcase_22 AC 318 ms
80,016 KB
testcase_23 AC 408 ms
97,048 KB
testcase_24 AC 367 ms
96,636 KB
testcase_25 AC 375 ms
95,768 KB
testcase_26 AC 367 ms
96,356 KB
testcase_27 AC 354 ms
96,688 KB
testcase_28 AC 356 ms
96,304 KB
testcase_29 AC 346 ms
95,788 KB
testcase_30 AC 359 ms
96,436 KB
testcase_31 AC 351 ms
96,088 KB
testcase_32 AC 350 ms
96,172 KB
testcase_33 AC 326 ms
80,724 KB
testcase_34 AC 316 ms
80,896 KB
testcase_35 AC 318 ms
80,600 KB
testcase_36 AC 339 ms
95,848 KB
testcase_37 AC 331 ms
96,044 KB
testcase_38 AC 77 ms
38,304 KB
testcase_39 AC 66 ms
38,656 KB
testcase_40 AC 72 ms
38,156 KB
testcase_41 AC 77 ms
38,640 KB
testcase_42 AC 65 ms
38,024 KB
testcase_43 AC 77 ms
38,716 KB
testcase_44 AC 76 ms
38,160 KB
testcase_45 AC 76 ms
38,632 KB
testcase_46 AC 77 ms
38,516 KB
testcase_47 AC 77 ms
38,744 KB
testcase_48 AC 50 ms
37,352 KB
testcase_49 AC 51 ms
37,108 KB
testcase_50 AC 50 ms
37,068 KB
testcase_51 AC 50 ms
37,248 KB
testcase_52 AC 51 ms
37,380 KB
testcase_53 AC 52 ms
36,968 KB
testcase_54 AC 49 ms
37,240 KB
testcase_55 AC 51 ms
37,176 KB
testcase_56 AC 52 ms
37,304 KB
testcase_57 AC 49 ms
36,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static int[] weights;
    static int[] values;
    static int[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int w = sc.nextInt();
        weights = new int[n];
        values = new int[n];
        for (int i = 0; i < n; i++) {
            weights[i] = sc.nextInt();
            values[i] = sc.nextInt();
        }
        dp = new int[n][w + 1];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        int[] results = new int[w + 1];
        for (int i = 1; i <= w; i++) {
            results[i] = dfw(n - 1, i);
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= w; i++) {
            sb.append(results[w] - results[w - i] + 1).append("\n");
        }
        System.out.print(sb);
    }
    
    static int dfw(int idx, int v) {
        if (v < 0) {
            return Integer.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][v] < 0) {
            dp[idx][v] = Math.max(dfw(idx - 1, v), dfw(idx - 1, v - weights[idx]) + values[idx]);
        }
        return dp[idx][v];
    }
} 
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();
    }
    
}
0