結果
問題 | No.2686 商品券の使い道 |
ユーザー |
![]() |
提出日時 | 2024-03-20 22:49:04 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 126 ms / 3,000 ms |
コード長 | 1,704 bytes |
コンパイル時間 | 1,972 ms |
コンパイル使用メモリ | 197,876 KB |
最終ジャッジ日時 | 2025-02-20 09:50:24 |
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 48 |
ソースコード
#include <bits/stdc++.h> using ll = long long; using std::cin; using std::cout; using std::endl; std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count()); template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } constexpr int inf = (int)1e9 + 7; constexpr long long INF = 1LL << 60; void solve() { ll n, M, Q; cin >> n >> M >> Q; std::vector<ll> w(n), v(n); for (int i = 0; i < n; i++) { cin >> w[i] >> v[i]; } std::vector<ll> dp1(1 << n), dp2(1 << n); for (int S = 0; S < (1 << n); S++) { ll cost = 0; ll val = 0; for (int i = 0; i < n; i++) { if (S >> i & 1) { cost += w[i]; val += v[i]; } } if (cost <= M) { dp1[S] = val; } if (cost <= Q) { dp2[S] = val; } } for (int i = 0; i < n; i++) { for (int S = 0; S < (1 << n); S++) { if (S >> i & 1) { continue; } chmax(dp1[S | 1 << i], dp1[S]); chmax(dp2[S | 1 << i], dp2[S]); } } ll res = 0; for (int S = 0; S < (1 << n); S++) { chmax(res, dp1[S] + dp2[((1 << n) - 1) ^ S]); } cout << res << "\n"; } int main() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); int kkt = 1; // cin >> kkt; while (kkt--) { solve(); } }