結果

問題 No.1947 質より種類数
ユーザー tentententen
提出日時 2022-05-25 09:04:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,887 ms / 2,000 ms
コード長 1,941 bytes
コンパイル時間 2,860 ms
コンパイル使用メモリ 77,612 KB
実行使用メモリ 259,268 KB
最終ジャッジ日時 2023-10-20 19:06:11
合計ジャッジ時間 19,968 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
53,464 KB
testcase_01 AC 51 ms
53,472 KB
testcase_02 AC 52 ms
53,468 KB
testcase_03 AC 53 ms
53,468 KB
testcase_04 AC 84 ms
55,032 KB
testcase_05 AC 78 ms
54,200 KB
testcase_06 AC 76 ms
54,848 KB
testcase_07 AC 81 ms
54,688 KB
testcase_08 AC 87 ms
54,688 KB
testcase_09 AC 122 ms
60,620 KB
testcase_10 AC 100 ms
58,160 KB
testcase_11 AC 142 ms
64,868 KB
testcase_12 AC 125 ms
60,616 KB
testcase_13 AC 93 ms
58,392 KB
testcase_14 AC 419 ms
116,544 KB
testcase_15 AC 251 ms
89,820 KB
testcase_16 AC 621 ms
137,060 KB
testcase_17 AC 746 ms
196,916 KB
testcase_18 AC 1,375 ms
246,260 KB
testcase_19 AC 1,251 ms
246,584 KB
testcase_20 AC 877 ms
196,456 KB
testcase_21 AC 1,029 ms
243,008 KB
testcase_22 AC 142 ms
65,448 KB
testcase_23 AC 492 ms
136,812 KB
testcase_24 AC 92 ms
55,596 KB
testcase_25 AC 91 ms
55,264 KB
testcase_26 AC 65 ms
53,672 KB
testcase_27 AC 82 ms
54,480 KB
testcase_28 AC 80 ms
55,192 KB
testcase_29 AC 1,141 ms
259,268 KB
testcase_30 AC 1,119 ms
259,228 KB
testcase_31 AC 53 ms
51,436 KB
testcase_32 AC 52 ms
53,472 KB
testcase_33 AC 54 ms
52,076 KB
testcase_34 AC 1,887 ms
256,896 KB
testcase_35 AC 415 ms
256,628 KB
testcase_36 AC 1,130 ms
259,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int bonus;
    static int[] values;
    static int[] prices;
    static int[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int v = sc.nextInt();
        bonus = sc.nextInt();
        dp = new int[n * 2][v + 1];
        values = new int[n];
        prices = new int[n];
        for (int i = 0; i < n; i++) {
            prices[i] = sc.nextInt();
            values[i] = sc.nextInt();
        }
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        System.out.println(dfw(n * 2 - 1, v));
    }
    
    static int dfw(int idx, int p) {
        if (p < 0) {
            return Integer.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][p] < 0) {
            int x = idx / 2;
            dp[idx][p] = dfw(idx - 1, p);
            if (idx % 2 == 0) {
                dp[idx][p] = Math.max(dp[idx][p], dfw(idx, p - prices[x]) + values[x]);
            } else {
                dp[idx][p] = Math.max(dp[idx][p], dfw(idx - 1, p - prices[x]) + values[x] + bonus);
            }
        }
        return dp[idx][p];
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0