結果

問題 No.1947 質より種類数
ユーザー tentententen
提出日時 2023-04-19 13:32:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,911 ms / 2,000 ms
コード長 2,540 bytes
コンパイル時間 2,785 ms
コンパイル使用メモリ 92,492 KB
実行使用メモリ 241,552 KB
最終ジャッジ日時 2024-04-22 14:25:00
合計ジャッジ時間 18,400 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
36,960 KB
testcase_01 AC 50 ms
36,748 KB
testcase_02 AC 47 ms
36,940 KB
testcase_03 AC 45 ms
37,124 KB
testcase_04 AC 78 ms
39,016 KB
testcase_05 AC 76 ms
38,568 KB
testcase_06 AC 74 ms
38,464 KB
testcase_07 AC 75 ms
38,752 KB
testcase_08 AC 77 ms
38,932 KB
testcase_09 AC 109 ms
46,932 KB
testcase_10 AC 87 ms
42,280 KB
testcase_11 AC 138 ms
51,152 KB
testcase_12 AC 121 ms
46,708 KB
testcase_13 AC 82 ms
41,540 KB
testcase_14 AC 407 ms
100,880 KB
testcase_15 AC 250 ms
75,340 KB
testcase_16 AC 541 ms
120,336 KB
testcase_17 AC 722 ms
176,328 KB
testcase_18 AC 1,350 ms
230,524 KB
testcase_19 AC 1,120 ms
217,260 KB
testcase_20 AC 815 ms
174,668 KB
testcase_21 AC 990 ms
218,416 KB
testcase_22 AC 127 ms
50,992 KB
testcase_23 AC 460 ms
121,668 KB
testcase_24 AC 84 ms
40,028 KB
testcase_25 AC 79 ms
39,020 KB
testcase_26 AC 60 ms
37,304 KB
testcase_27 AC 74 ms
38,784 KB
testcase_28 AC 77 ms
39,252 KB
testcase_29 AC 1,112 ms
238,056 KB
testcase_30 AC 1,196 ms
241,552 KB
testcase_31 AC 49 ms
37,092 KB
testcase_32 AC 46 ms
37,100 KB
testcase_33 AC 49 ms
37,000 KB
testcase_34 AC 1,911 ms
238,184 KB
testcase_35 AC 400 ms
238,548 KB
testcase_36 AC 1,199 ms
238,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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