結果
問題 | No.2686 商品券の使い道 |
ユーザー |
![]() |
提出日時 | 2024-03-20 23:25:21 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 342 ms / 3,000 ms |
コード長 | 1,056 bytes |
コンパイル時間 | 2,072 ms |
コンパイル使用メモリ | 77,124 KB |
実行使用メモリ | 66,384 KB |
最終ジャッジ日時 | 2024-09-30 09:28:45 |
合計ジャッジ時間 | 15,824 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 48 |
ソースコード
import java.util.Scanner;public class Main {public static void main(String[] args) throws Exception {Scanner sc = new Scanner(System.in);int n = sc.nextInt();int m = sc.nextInt();int q = sc.nextInt();int[] a = new int[n];int[] b = new int[n];for (int i = 0; i < n; i++) {a[i] = sc.nextInt();b[i] = sc.nextInt();}sc.close();int n2 = 1 << n;long[] c = new long[n2];long[] dp1 = new long[n2];long[] dp2 = new long[n2];for (int i = 0; i < n2; i++) {for (int j = 0; j < n; j++) {if ((i >> j & 1) == 0) {int ni = i | (1 << j);c[ni] = c[i] + a[j];if (c[ni] <= m) {dp1[ni] = dp1[i] + b[j];} else {dp1[ni] = Math.max(dp1[ni], dp1[i]);}if (c[ni] <= q) {dp2[ni] = dp2[i] + b[j];} else {dp2[ni] = Math.max(dp2[ni], dp2[i]);}}}}long ans = 0;int all = n2 - 1;for (int i = 0; i < n2; i++) {int i2 = all - i;long val = dp1[i] + dp2[i2];ans = Math.max(ans, val);}System.out.println(ans);}}