結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー tenten
提出日時 2020-12-17 12:39:21
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 1,724 bytes
コンパイル時間 4,273 ms
コンパイル使用メモリ 83,376 KB
実行使用メモリ 88,624 KB
最終ジャッジ日時 2024-09-20 06:58:24
合計ジャッジ時間 10,087 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9 TLE * 1 -- * 42
権限があれば一括ダウンロードができます

ソースコード

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