結果

問題 No.37 遊園地のアトラクション
ユーザー tenten
提出日時 2021-11-10 09:56:13
言語 Java
(openjdk 23)
結果
AC  
実行時間 77 ms / 5,000 ms
コード長 1,996 bytes
コンパイル時間 2,231 ms
コンパイル使用メモリ 78,028 KB
実行使用メモリ 51,184 KB
最終ジャッジ日時 2024-11-20 18:35:47
合計ジャッジ時間 5,340 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int[][] dp;
    static int[] costs;
    static int[] values;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int t = sc.nextInt();
        int n = sc.nextInt();
        costs = new int[n];
        for (int i = 0; i < n; i++) {
            costs[i] = sc.nextInt();
        }
        values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        dp = new int[n][t + 1];
        System.out.println(dfw(n - 1, t));
    }
    
    static int dfw(int idx, int t) {
        if (t < 0) {
            return Integer.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][t] == 0) {
            int time = costs[idx];
            int atract = values[idx];
            int sum = atract;
            dp[idx][t] = dfw(idx - 1, t);
            while (time <= t && atract > 0) {
                dp[idx][t] = Math.max(dp[idx][t], dfw(idx - 1, t - time) + sum);
                time += costs[idx];
                atract /= 2;
                sum += atract;
            }
        }
        return dp[idx][t];
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String nextLine() throws Exception {
        return br.readLine();
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0