結果
問題 |
No.2364 Knapsack Problem
|
ユーザー |
|
提出日時 | 2023-06-30 22:03:32 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,190 bytes |
コンパイル時間 | 2,013 ms |
コンパイル使用メモリ | 200,576 KB |
最終ジャッジ日時 | 2025-02-15 03:55:38 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 WA * 2 |
ソースコード
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, w; cin >> n >> m >> w; vector<pair<int, long long>> merch(n); for (int i=0;i<n;++i) cin >> merch[i].first; for (int i=0;i<n;++i) cin >> merch[i].second; vector<pair<int, long long>> magic(m); for (int i=0;i<m;++i) cin >> magic[i].first; for (int i=0;i<m;++i) cin >> magic[i].second; long long ans {}; for (int i=1;i<(1<<(n+m));++i) { pair<int, long long> ans_tmp {}; vector<int> weights; for (int j=0;j<n;++j) { if (i & (1<<j)) { weights.push_back(merch[j].first); ans_tmp.second += merch[j].second; } } for (int j=n;j<n+m;++j) { if (i & (1<<j)) { weights.push_back(-magic[j-n].first); ans_tmp.second -= magic[j-n].second; } } sort(weights.begin(), weights.end()); if (weights[0] < -w) continue; if (w < weights[weights.size()-1]) continue; ans_tmp.first = accumulate(weights.begin(), weights.end(), 0ll); //cout << ans_tmp.first << ' ' << ans_tmp.second << '\n'; if (ans_tmp.first <= w) ans = max(ans, ans_tmp.second); } cout << ans << '\n'; }