結果
問題 | No.2686 商品券の使い道 |
ユーザー |
![]() |
提出日時 | 2024-03-13 02:03:36 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 179 ms / 3,000 ms |
コード長 | 1,237 bytes |
コンパイル時間 | 2,045 ms |
コンパイル使用メモリ | 196,896 KB |
最終ジャッジ日時 | 2025-02-20 04:05:52 |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 48 |
ソースコード
#include<bits/stdc++.h> using namespace std; #ifdef LOCAL #include <debug_print.hpp> #define OUT(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else #define OUT(...) (static_cast<void>(0)) #endif int main(){ int N, M, Q; cin >> N >> M >> Q; assert(1 <= N && N <= 20); assert(1 <= M && M <= 1e9); assert(1 <= Q && Q <= 1e9); using ll = long long; vector<ll> A(N), B(N); for(int i = 0; i < N; i++){ cin >> A[i] >> B[i]; assert(1 <= A[i] && A[i] <= 1e9); assert(1 <= B[i] && B[i] <= 1e9); } vector<ll> dp(1 << N); for(int i = 0; i < 1 << N; i++){ long long price = 0, value = 0; for(int j = 0; j < N; j++){ if(i >> j & 1){ price += A[j]; value += B[j]; } } if(price <= Q){ dp[i] = value; } } for(int i = 0; i < 1 << N; i++){ for(int j = 0; j < N; j++){ dp[i | 1 << j] = max(dp[i | 1 << j], dp[i]); } } long long ret = 0; for(int i = 0; i < 1 << N; i++){ long long price = 0, value = 0; for(int j = 0; j < N; j++){ if(i >> j & 1){ price += A[j]; value += B[j]; } } if(price <= M){ ret = max(ret, value + dp[i ^ ((1 << N) - 1)]); } } cout << ret << endl; }