結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー tentententen
提出日時 2021-03-11 09:50:20
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,735 bytes
コンパイル時間 2,728 ms
コンパイル使用メモリ 83,588 KB
実行使用メモリ 492,896 KB
最終ジャッジ日時 2024-04-21 01:28:47
合計ジャッジ時間 9,828 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
46,708 KB
testcase_01 AC 114 ms
40,336 KB
testcase_02 AC 132 ms
41,652 KB
testcase_03 AC 132 ms
41,480 KB
testcase_04 AC 122 ms
40,244 KB
testcase_05 AC 126 ms
41,512 KB
testcase_06 AC 162 ms
42,204 KB
testcase_07 AC 139 ms
41,300 KB
testcase_08 AC 127 ms
40,964 KB
testcase_09 AC 592 ms
92,888 KB
testcase_10 AC 361 ms
66,524 KB
testcase_11 AC 358 ms
59,308 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>> paid = new ArrayList<>();
    static ArrayList<HashMap<Integer, Integer>> free = 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());
		    paid.add(new HashMap<>());
		    free.add(new HashMap<>());
		}
		Arrays.sort(pizzas);
		System.out.println(dfw(n - 1, k, true));
   }
   
   static class Pizza implements Comparable<Pizza> {
       int price;
       int value;
       
       public Pizza(int price, int value) {
           this.price = price;
           this.value = value;
       }
       
       public int compareTo(Pizza another) {
           return price - another.price;
       }
   }
   
   static int dfw(int idx, int cost, boolean isPaid) {
       if (cost < 0) {
           return Integer.MIN_VALUE;
       }
       if (idx < 0) {
           return 0;
       }
       if (isPaid) {
           if (paid.get(idx).containsKey(cost)) {
               return paid.get(idx).get(cost);
           }
           int ans = Math.max(dfw(idx - 1, cost, true), dfw(idx - 1, cost - pizzas[idx].price, false) + pizzas[idx].value);
           paid.get(idx).put(cost, ans);
           return ans;
       } else {
           if (free.get(idx).containsKey(cost)) {
               return free.get(idx).get(cost);
           }
           int ans = Math.max(dfw(idx - 1, cost, true) + pizzas[idx].value, dfw(idx - 1, cost, false));
           free.get(idx).put(cost, ans);
           return ans;
       }
   }
}

0