結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー tentententen
提出日時 2020-08-26 16:28:34
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,420 bytes
コンパイル時間 4,875 ms
コンパイル使用メモリ 77,280 KB
実行使用メモリ 246,872 KB
最終ジャッジ日時 2024-04-25 03:26:11
合計ジャッジ時間 13,740 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
45,592 KB
testcase_01 AC 120 ms
40,532 KB
testcase_02 AC 150 ms
41,128 KB
testcase_03 AC 134 ms
41,308 KB
testcase_04 AC 134 ms
41,040 KB
testcase_05 AC 135 ms
41,428 KB
testcase_06 AC 156 ms
41,536 KB
testcase_07 AC 138 ms
41,180 KB
testcase_08 AC 141 ms
41,292 KB
testcase_09 AC 240 ms
48,156 KB
testcase_10 AC 195 ms
44,696 KB
testcase_11 AC 214 ms
47,560 KB
testcase_12 AC 497 ms
92,852 KB
testcase_13 AC 1,779 ms
225,080 KB
testcase_14 AC 362 ms
65,664 KB
testcase_15 AC 361 ms
72,236 KB
testcase_16 AC 433 ms
79,880 KB
testcase_17 AC 751 ms
105,208 KB
testcase_18 AC 135 ms
41,060 KB
testcase_19 AC 131 ms
41,388 KB
testcase_20 AC 133 ms
41,600 KB
testcase_21 AC 169 ms
44,268 KB
testcase_22 AC 182 ms
42,960 KB
testcase_23 AC 180 ms
43,260 KB
testcase_24 TLE -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static Pizza[] pizzas;
    static int[][][] dp;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        pizzas = new Pizza[n];
        for (int i = 0; i < n; i++) {
            pizzas[i] = new Pizza(sc.nextInt(), sc.nextInt());
        }
        Arrays.sort(pizzas);
        dp = new int[2][n][k + 1];
        System.out.println(dfw(0, n - 1, k));
    }
    
    static int dfw(int type, int idx, int cost) {
        if (cost < 0) {
            return Integer.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp[type][idx][cost] == 0) {
             if (type == 0) {
                dp[type][idx][cost] = Math.max(dfw(0, idx - 1, cost), dfw(1, idx - 1, cost - pizzas[idx].cost) + pizzas[idx].value);
            } else {
                dp[type][idx][cost] = Math.max(dfw(1, idx - 1, cost), dfw(0, idx - 1, cost) + pizzas[idx].value);
            }
        }
        return dp[type][idx][cost];
    }
    
    static class Pizza implements Comparable<Pizza> {
        int cost;
        int value;
        
        public Pizza(int cost, int value) {
            this.cost = cost;
            this.value = value;
        }
        
        public int compareTo(Pizza another) {
            return cost - another.cost;
        }
    }
}
0