結果

問題 No.54 Happy Hallowe'en
ユーザー tentententen
提出日時 2021-03-03 20:14:02
言語 Java19
(openjdk 21)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,744 bytes
コンパイル時間 2,516 ms
コンパイル使用メモリ 74,960 KB
実行使用メモリ 512,176 KB
最終ジャッジ日時 2023-07-26 22:40:52
合計ジャッジ時間 23,862 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
49,564 KB
testcase_01 AC 41 ms
49,412 KB
testcase_02 AC 41 ms
49,388 KB
testcase_03 AC 42 ms
49,668 KB
testcase_04 AC 477 ms
183,268 KB
testcase_05 AC 832 ms
220,692 KB
testcase_06 AC 1,359 ms
337,928 KB
testcase_07 AC 2,087 ms
404,048 KB
testcase_08 AC 2,786 ms
407,876 KB
testcase_09 AC 3,860 ms
500,016 KB
testcase_10 AC 43 ms
49,548 KB
testcase_11 AC 41 ms
49,596 KB
testcase_12 MLE -
testcase_13 AC 4,332 ms
500,908 KB
testcase_14 AC 43 ms
49,432 KB
testcase_15 AC 41 ms
49,400 KB
testcase_16 AC 47 ms
49,380 KB
testcase_17 AC 47 ms
49,592 KB
testcase_18 AC 45 ms
49,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static House[] houses;
    static int[][] dp;
    static int[] maxes;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        houses = new House[n];
        for (int i = 0; i < n; i++) {
            String[] line = br.readLine().split(" ", 2);
            houses[i] = new House(Integer.parseInt(line[0]), Integer.parseInt(line[1]));
        }
        Arrays.sort(houses);
        maxes = new int[n];
        maxes[0] = houses[0].limit;
        for (int i = 1; i < n; i++) {
            maxes[i] = Math.max(maxes[i - 1], houses[i].limit);
        }
        dp = new int[n][10001];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        System.out.println(dfw(n - 1, 0));
    }
    
    static int dfw(int idx, int value) {
        if (idx < 0) {
            return 0;
        }
        if (value > maxes[idx]) {
            return 0;
        }
        if (dp[idx][value] < 0) {
            dp[idx][value] = dfw(idx - 1, value);
            if (houses[idx].limit > value) {
                dp[idx][value] = Math.max(dp[idx][value], dfw(idx - 1, value + houses[idx].gain) + houses[idx].gain);
            }
        }
        return dp[idx][value];
    }
    
    static class House implements Comparable<House> {
        int gain;
        int limit;
        
        public House(int gain, int limit) {
            this.gain = gain;
            this.limit = limit;
        }
        
        public int compareTo(House another) {
            return another.limit + another.gain - limit - gain;
        }
    }
}
0