結果

問題 No.1858 Gorgeous Knapsack
ユーザー tentententen
提出日時 2022-02-28 11:09:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 967 ms / 2,000 ms
コード長 2,555 bytes
コンパイル時間 2,093 ms
コンパイル使用メモリ 74,360 KB
実行使用メモリ 253,644 KB
最終ジャッジ日時 2023-09-20 18:41:12
合計ジャッジ時間 12,864 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,464 KB
testcase_01 AC 44 ms
49,576 KB
testcase_02 AC 44 ms
49,800 KB
testcase_03 AC 47 ms
49,464 KB
testcase_04 AC 235 ms
89,852 KB
testcase_05 AC 105 ms
61,536 KB
testcase_06 AC 283 ms
112,712 KB
testcase_07 AC 227 ms
84,156 KB
testcase_08 AC 394 ms
129,664 KB
testcase_09 AC 588 ms
183,644 KB
testcase_10 AC 258 ms
83,460 KB
testcase_11 AC 334 ms
109,664 KB
testcase_12 AC 460 ms
140,920 KB
testcase_13 AC 169 ms
58,336 KB
testcase_14 AC 47 ms
49,504 KB
testcase_15 AC 46 ms
49,476 KB
testcase_16 AC 46 ms
47,552 KB
testcase_17 AC 46 ms
49,480 KB
testcase_18 AC 46 ms
49,596 KB
testcase_19 AC 45 ms
49,576 KB
testcase_20 AC 43 ms
49,652 KB
testcase_21 AC 44 ms
49,488 KB
testcase_22 AC 44 ms
49,584 KB
testcase_23 AC 44 ms
49,732 KB
testcase_24 AC 132 ms
64,996 KB
testcase_25 AC 436 ms
135,016 KB
testcase_26 AC 363 ms
129,864 KB
testcase_27 AC 349 ms
129,524 KB
testcase_28 AC 293 ms
110,204 KB
testcase_29 AC 116 ms
52,472 KB
testcase_30 AC 119 ms
52,728 KB
testcase_31 AC 117 ms
52,644 KB
testcase_32 AC 113 ms
53,184 KB
testcase_33 AC 126 ms
53,804 KB
testcase_34 AC 893 ms
249,848 KB
testcase_35 AC 312 ms
250,368 KB
testcase_36 AC 967 ms
253,644 KB
testcase_37 AC 44 ms
49,664 KB
testcase_38 AC 114 ms
52,280 KB
testcase_39 AC 44 ms
49,580 KB
testcase_40 AC 314 ms
250,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static Jewel[] jewels;
    static long[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        jewels = new Jewel[n];
        for (int i = 0; i < n; i++) {
            jewels[i] = new Jewel(sc.nextInt(), sc.nextInt());
        }
        Arrays.sort(jewels);
        dp = new long[n][m + 1];
        for (long[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        long ans = 0;
        for (int i = 0; i < n; i++) {
            ans = Math.max(ans, (dfw(i - 1, m - jewels[i].weight) + jewels[i].value) * jewels[i].value);
        }
        System.out.println(ans);
    }
    
    static long dfw(int idx, int w) {
        if (w < 0) {
            return Integer.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][w] < 0) {
            dp[idx][w] = Math.max(dfw(idx - 1, w), dfw(idx - 1, w - jewels[idx].weight) + jewels[idx].value);
        }
        return dp[idx][w];
    }
    
    static boolean getEnable(int x, int y) {
        if (x == y) {
            return true;
        }
        if (x > y) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(y); i++) {
            if (y % i > 0) {
                continue;
            }
            if (getEnable(x, i + y / i)) {
                return true;
            }
        }
        return false;
    }
    
    static class Jewel implements Comparable<Jewel> {
        int value;
        int weight;
        
        public Jewel(int value, int weight) {
            this.value = value;
            this.weight = weight;
        }
        
        public int compareTo(Jewel another) {
            return another.value - value;
        }
    }
    
}


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 next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0