結果
| 問題 | 
                            No.54 Happy Hallowe'en
                             | 
                    
| コンテスト | |
| ユーザー | 
                             tenten
                         | 
                    
| 提出日時 | 2021-03-03 20:07:45 | 
| 言語 | Java  (openjdk 23)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 4,502 ms / 5,000 ms | 
| コード長 | 1,539 bytes | 
| コンパイル時間 | 2,088 ms | 
| コンパイル使用メモリ | 77,836 KB | 
| 実行使用メモリ | 510,692 KB | 
| 最終ジャッジ日時 | 2024-10-03 08:59:27 | 
| 合計ジャッジ時間 | 23,918 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 19 | 
ソースコード
import java.util.*;
import java.io.*;
public class Main {
    static House[] houses;
    static int[][] dp;
    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);
        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;
        }
    }
}
            
            
            
        
            
tenten