結果

問題 No.1947 質より種類数
ユーザー tentententen
提出日時 2023-02-24 17:15:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,317 ms / 2,000 ms
コード長 2,550 bytes
コンパイル時間 2,591 ms
コンパイル使用メモリ 87,688 KB
実行使用メモリ 251,884 KB
最終ジャッジ日時 2023-10-11 02:33:20
合計ジャッジ時間 18,025 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,400 KB
testcase_01 AC 42 ms
49,368 KB
testcase_02 AC 41 ms
49,404 KB
testcase_03 AC 41 ms
49,688 KB
testcase_04 AC 78 ms
52,748 KB
testcase_05 AC 58 ms
50,464 KB
testcase_06 AC 60 ms
52,516 KB
testcase_07 AC 71 ms
52,556 KB
testcase_08 AC 80 ms
53,160 KB
testcase_09 AC 109 ms
57,636 KB
testcase_10 AC 88 ms
55,232 KB
testcase_11 AC 134 ms
59,960 KB
testcase_12 AC 119 ms
58,156 KB
testcase_13 AC 89 ms
55,220 KB
testcase_14 AC 417 ms
113,028 KB
testcase_15 AC 241 ms
85,688 KB
testcase_16 AC 583 ms
133,240 KB
testcase_17 AC 743 ms
187,204 KB
testcase_18 AC 1,317 ms
239,620 KB
testcase_19 AC 1,214 ms
227,948 KB
testcase_20 AC 845 ms
187,172 KB
testcase_21 AC 1,016 ms
227,376 KB
testcase_22 AC 128 ms
62,508 KB
testcase_23 AC 479 ms
131,000 KB
testcase_24 AC 85 ms
52,552 KB
testcase_25 AC 77 ms
52,556 KB
testcase_26 AC 56 ms
52,304 KB
testcase_27 AC 62 ms
52,028 KB
testcase_28 AC 77 ms
52,484 KB
testcase_29 AC 1,232 ms
250,788 KB
testcase_30 AC 1,138 ms
251,884 KB
testcase_31 AC 42 ms
49,396 KB
testcase_32 AC 42 ms
49,564 KB
testcase_33 AC 45 ms
49,988 KB
testcase_34 AC 765 ms
250,800 KB
testcase_35 AC 304 ms
250,188 KB
testcase_36 AC 1,123 ms
251,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int c;
    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 v = sc.nextInt();
        c = sc.nextInt();
        prices = new int[n];
        values = new int[n];
        for (int i = 0; i < n; i++) {
            prices[i] = sc.nextInt();
            values[i] = sc.nextInt();
        }
        dp = new int[n * 2][v + 1];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        System.out.println(dfw(n * 2 - 1, v));
    }
    
    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 == 1) {
                dp[idx][v] = dfw(idx - 2, v);
                dp[idx][v] = Math.max(dp[idx][v], dfw(idx - 1, v - prices[idx / 2]) + c + values[idx / 2]);
            } else {
                dp[idx][v] = dfw(idx - 1, v);
                dp[idx][v] = Math.max(dp[idx][v], dfw(idx, v - prices[idx / 2]) + values[idx / 2]);
            }
        }
        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