結果

問題 No.2686 商品券の使い道
ユーザー tentententen
提出日時 2024-03-25 11:54:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 367 ms / 3,000 ms
コード長 2,439 bytes
コンパイル時間 2,253 ms
コンパイル使用メモリ 78,648 KB
実行使用メモリ 103,060 KB
最終ジャッジ日時 2024-03-25 11:54:33
合計ジャッジ時間 14,903 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
53,992 KB
testcase_01 AC 47 ms
54,004 KB
testcase_02 AC 144 ms
68,304 KB
testcase_03 AC 149 ms
68,192 KB
testcase_04 AC 155 ms
68,200 KB
testcase_05 AC 141 ms
68,264 KB
testcase_06 AC 137 ms
68,128 KB
testcase_07 AC 137 ms
67,908 KB
testcase_08 AC 154 ms
67,724 KB
testcase_09 AC 141 ms
68,148 KB
testcase_10 AC 157 ms
68,224 KB
testcase_11 AC 136 ms
68,172 KB
testcase_12 AC 144 ms
68,148 KB
testcase_13 AC 185 ms
68,424 KB
testcase_14 AC 147 ms
68,092 KB
testcase_15 AC 161 ms
68,416 KB
testcase_16 AC 164 ms
68,392 KB
testcase_17 AC 151 ms
68,276 KB
testcase_18 AC 157 ms
68,336 KB
testcase_19 AC 164 ms
68,332 KB
testcase_20 AC 140 ms
68,048 KB
testcase_21 AC 175 ms
68,244 KB
testcase_22 AC 156 ms
68,280 KB
testcase_23 AC 137 ms
67,968 KB
testcase_24 AC 143 ms
68,148 KB
testcase_25 AC 146 ms
68,152 KB
testcase_26 AC 150 ms
68,300 KB
testcase_27 AC 133 ms
68,076 KB
testcase_28 AC 162 ms
68,136 KB
testcase_29 AC 299 ms
102,940 KB
testcase_30 AC 290 ms
102,816 KB
testcase_31 AC 311 ms
102,924 KB
testcase_32 AC 337 ms
103,016 KB
testcase_33 AC 299 ms
102,564 KB
testcase_34 AC 321 ms
102,908 KB
testcase_35 AC 289 ms
102,876 KB
testcase_36 AC 311 ms
103,048 KB
testcase_37 AC 367 ms
103,048 KB
testcase_38 AC 339 ms
103,060 KB
testcase_39 AC 321 ms
102,772 KB
testcase_40 AC 333 ms
102,840 KB
testcase_41 AC 308 ms
102,928 KB
testcase_42 AC 349 ms
102,916 KB
testcase_43 AC 326 ms
102,804 KB
testcase_44 AC 353 ms
102,820 KB
testcase_45 AC 317 ms
102,924 KB
testcase_46 AC 351 ms
103,048 KB
evil_random20_1.txt AC 358 ms
102,924 KB
evil_random20_2.txt AC 329 ms
102,956 KB
evil_random20_3.txt AC 285 ms
102,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int a = sc.nextInt();
        int b = sc.nextInt();
        int[] prices = new int[n];
        int[] values = new int[n];
        for (int i = 0; i < n; i++) {
            prices[i] = sc.nextInt();
            values[i] = sc.nextInt();
        }
        long[] weights = new long[1 << n];
        long[][] dp = new long[1 << n][2];
        for (int i = 0; i < (1 << n); i++) {
            for (int j = 0; j < n; j++) {
                if ((i & (1 << j)) == 0) {
                    continue;
                }
                weights[i] = weights[i ^ (1 << j)] + prices[j];
                if (weights[i] <= a) {
                    dp[i][0] = dp[i ^ (1 << j)][0] + values[j];
                }
                if (weights[i] <= b) {
                    dp[i][1] = dp[i ^ (1 << j)][1] + values[j];
                }
                break;
            }
            for (int j = 0; j < n; j++) {
                if ((i & (1 << j)) == 0) {
                    continue;
                }
                dp[i][0] = Math.max(dp[i][0], dp[i ^ (1 << j)][0]);
                dp[i][1] = Math.max(dp[i][1], dp[i ^ (1 << j)][1]);
            }
        }
        long ans = 0;
        for (int i = 0; i < (1 << n); i++) {
            ans = Math.max(ans, dp[i][0] + dp[((1 << n) - 1) ^ i][1]);
        }
        System.out.println(ans);
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0