結果
問題 |
No.2869 yuusaan's Knapsacks
|
ユーザー |
![]() |
提出日時 | 2024-09-12 11:36:11 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,343 bytes |
コンパイル時間 | 3,031 ms |
コンパイル使用メモリ | 246,872 KB |
実行使用メモリ | 20,608 KB |
最終ジャッジ日時 | 2024-09-12 11:36:38 |
合計ジャッジ時間 | 21,585 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 WA * 1 |
other | AC * 22 WA * 5 |
ソースコード
#include<bits/stdc++.h> using namespace std; using LL = long long; LL w2[(1 << 17)][20], ans; int w[20], n, m, e[20], v[20], id; vector<int>ev; LL dp[20][(1 << 17)], pre[20][(1 << 17)]; void dfs(int x, int s, LL sw, LL sum){ if(x == m){ for(int j = 1; j <= n; ++j){ if(e[j] >= sw){ w2[s][j] = sum; } } return; } dfs(x + 1, s, sw, sum); dfs(x + 1, (s | (1 << x)), sw + w[x], sum + v[x]); } void Dfs(int x, int s){ if(!x)return; Dfs(x - 1, (s ^ pre[x][s])); ev.clear(); for(int j = 0; j < m; ++j){ if(pre[x][s] & (1 << j)){ ev.push_back(j + 1); } } cout << ev.size() << ' '; for(auto v : ev){ cout << v << ' '; } cout << '\n'; } int main(){ cin >> n >> m; for(int i = 1; i <= n; ++i){ cin >> e[i]; } for(int j = 0; j < m; ++j){ cin >> v[j] >> w[j]; } dfs(0, 0, 0, 0); for(int i = 1; i <= n; ++i){ for(int j = 0; j < (1 << m); j++){ for(int s = (((1 << m) - 1) ^ j); s; s = ((s - 1) & (((1 << m) - 1) ^ j))){ if(dp[i - 1][j] + w2[s][i] > dp[i][j | s]){ dp[i][j | s] = dp[i - 1][j] + w2[s][i]; pre[i][j | s] = s; } } } } for(int i = 0; i < (1 << m); i++){ if(dp[n][i] > ans){ ans = dp[n][i]; id = i; } } cout << dp[n][id] << '\n'; Dfs(n, id); return 0; }