結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー htensai
提出日時 2020-05-13 18:28:07
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 1,755 bytes
コンパイル時間 2,506 ms
コンパイル使用メモリ 82,144 KB
実行使用メモリ 90,984 KB
最終ジャッジ日時 2024-09-14 15:36:52
合計ジャッジ時間 10,211 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9 TLE * 1 -- * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static ArrayList<HashMap<Integer, Integer>> dpNone = new ArrayList<>();
    static ArrayList<HashMap<Integer, Integer>> dpBonus = 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());
		    dpNone.add(new HashMap<>());
		    dpBonus.add(new HashMap<>());
		}
		Arrays.sort(pizzas);
		System.out.println(dfw(n - 1, k, false));
	}
	
	static int dfw(int idx, int money, boolean hasBonus) {
	    if (money < 0) {
	        return Integer.MIN_VALUE / 10;
	    }
	    if (idx < 0) {
	        return 0;
	    }
	    ArrayList<HashMap<Integer, Integer>> dp;
	    if (hasBonus) {
	        dp = dpBonus;
	    } else {
	        dp = dpNone;
	    }
	    if (dp.get(idx).containsKey(money)) {
	        return dp.get(idx).get(money);
	    }
	    int max = 0;
	    max = Math.max(max, dfw(idx - 1, money, hasBonus));
	    if (hasBonus) {
	        max = Math.max(max, dfw(idx - 1, money, false) + pizzas[idx].value);
	    } else {
	        max = Math.max(max, dfw(idx - 1, money - pizzas[idx].cost, true) + pizzas[idx].value);
	    }
	    dp.get(idx).put(money, max);
	    return max;
	}
	
	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) {
	        if (cost == another.cost) {
	            return value - another.value;
	        } else {
	            return cost - another.cost;
	        }
	    }
	}
}
0