結果

問題 No.54 Happy Hallowe'en
ユーザー tentententen
提出日時 2021-03-03 19:59:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 4,630 ms / 5,000 ms
コード長 1,365 bytes
コンパイル時間 2,357 ms
コンパイル使用メモリ 77,596 KB
実行使用メモリ 451,968 KB
最終ジャッジ日時 2024-04-14 11:41:32
合計ジャッジ時間 28,872 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
54,168 KB
testcase_01 AC 135 ms
54,248 KB
testcase_02 AC 136 ms
53,956 KB
testcase_03 AC 133 ms
53,984 KB
testcase_04 AC 874 ms
185,404 KB
testcase_05 AC 1,459 ms
253,620 KB
testcase_06 AC 2,042 ms
340,160 KB
testcase_07 AC 2,845 ms
366,452 KB
testcase_08 AC 3,690 ms
416,396 KB
testcase_09 AC 4,514 ms
451,900 KB
testcase_10 AC 137 ms
54,328 KB
testcase_11 AC 133 ms
53,872 KB
testcase_12 AC 3,369 ms
450,024 KB
testcase_13 AC 4,630 ms
451,968 KB
testcase_14 AC 136 ms
54,092 KB
testcase_15 AC 135 ms
54,132 KB
testcase_16 AC 163 ms
54,152 KB
testcase_17 AC 163 ms
54,040 KB
testcase_18 AC 150 ms
54,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static House[] houses;
    static int[][] dp;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        houses = new House[n];
        for (int i = 0; i < n; i++) {
            houses[i] = new House(sc.nextInt(), sc.nextInt());
        }
        Arrays.sort(houses);
        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 > 10000) {
            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