結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー tentententen
提出日時 2020-12-17 12:39:21
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,724 bytes
コンパイル時間 6,738 ms
コンパイル使用メモリ 79,844 KB
実行使用メモリ 104,660 KB
最終ジャッジ日時 2023-10-20 11:25:26
合計ジャッジ時間 11,290 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
57,248 KB
testcase_01 AC 135 ms
55,708 KB
testcase_02 AC 170 ms
59,844 KB
testcase_03 AC 134 ms
57,236 KB
testcase_04 AC 134 ms
57,792 KB
testcase_05 AC 127 ms
57,232 KB
testcase_06 AC 170 ms
57,692 KB
testcase_07 AC 143 ms
57,792 KB
testcase_08 AC 147 ms
57,168 KB
testcase_09 AC 686 ms
104,660 KB
testcase_10 AC 385 ms
84,960 KB
testcase_11 AC 373 ms
72,020 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
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 ArrayList<HashMap<Integer, Integer>> dp1 = new ArrayList<>();
    static ArrayList<HashMap<Integer, Integer>> dp2 = new ArrayList<>();
    static Pizza[] pizzas;
	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());
		    dp1.add(new HashMap<>());
		    dp2.add(new HashMap<>());
		}
		Arrays.sort(pizzas);
		System.out.println(dfw(n - 1, k, 0));
	}
	
	static int dfw(int idx, int cost, int isFree) {
	    if (cost < 0) {
	        return Integer.MIN_VALUE;
	    }
	    if (idx < 0) {
	        return 0;
	    }
	    if (isFree == 0) {
	        if (dp1.get(idx).containsKey(cost)) {
	            return dp1.get(idx).get(cost);
	        } else {
	            int ans = Math.max(dfw(idx - 1, cost, isFree), dfw(idx - 1, cost - pizzas[idx].price, 1) + pizzas[idx].delicious);
	            dp1.get(idx).put(cost, ans);
	            return ans;
	        }
	    } else {
	        if (dp2.get(idx).containsKey(cost)) {
	            return dp2.get(idx).get(cost);
	        } else {
	            int ans = Math.max(dfw(idx - 1, cost, isFree), dfw(idx - 1, cost, 0) + pizzas[idx].delicious);
	            dp2.get(idx).put(cost, ans);
	            return ans;
	        }
	    }
	}
	
	static class Pizza implements Comparable<Pizza> {
	    int price;
	    int delicious;
	    
	    public Pizza(int price, int delicious) {
	        this.price = price;
	        this.delicious = delicious;
	    }
	    
	    public int compareTo(Pizza another) {
	        return price - another.price;
	    }
	}
}
0