結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー tentententen
提出日時 2023-01-31 19:52:59
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 3,009 bytes
コンパイル時間 2,929 ms
コンパイル使用メモリ 84,692 KB
実行使用メモリ 250,480 KB
最終ジャッジ日時 2024-06-30 21:24:53
合計ジャッジ時間 43,368 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 50 TLE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static int[][] dp1;
    static int[][] dp2;
    static Pizza[] pizzas;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        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);
        dp1 = new int[n][k + 1];
        for (int[] arr : dp1) {
            Arrays.fill(arr, -1);
        }
        dp2 = new int[n][k + 1];
        for (int[] arr : dp2) {
            Arrays.fill(arr, -1);
        }
        System.out.println(dfw2(n - 1, k));
    }
    
    static int dfw2(int idx, int v) {
        if (v < 0) {
            return Integer.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp2[idx][v] < 0) {
            dp2[idx][v] = Math.max(dfw2(idx - 1, v), dfw1(idx - 1, v - pizzas[idx].price) + pizzas[idx].value);
        }
        return dp2[idx][v];
    }
    
    static int dfw1(int idx, int v) {
        if (v < 0) {
            return Integer.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp1[idx][v] < 0) {
            dp1[idx][v] = Math.max(dfw1(idx - 1, v), dfw2(idx - 1, v) + pizzas[idx].value);
        }
        return dp1[idx][v];
    }
    
    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;
        }
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0