結果
| 問題 |
No.2869 yuusaan's Knapsacks
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-09-07 00:31:06 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2,095 ms / 4,500 ms |
| コード長 | 1,575 bytes |
| コンパイル時間 | 17,098 ms |
| コンパイル使用メモリ | 327,812 KB |
| 最終ジャッジ日時 | 2025-02-24 05:03:21 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 |
ソースコード
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
template<typename T>
bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }
template<typename T>
bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }
struct io_setup {
io_setup() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cout << fixed << setprecision(15);
}
} io_setup;
int main(){
int n,m;
cin>>n>>m;
vector<int> e(n);
rep(i,0,n) cin>>e.at(i);
vector<pair<int,int>> vw(m);
for(auto&[v,w]:vw) cin>>v>>w;
vector<ll> smv(1<<m),smw(1<<m);
rep(bit,0,1<<m){
ll tpv=0,tpw=0;
rep(i,0,m) if((bit>>i)&1){
tpv+=vw.at(i).first;
tpw+=vw.at(i).second;
}
smv.at(bit)=tpv;
smw.at(bit)=tpw;
}
vector<vector<ll>> dp(n+1,vector<ll>(1<<m,-1e18));
vector<vector<vector<ll>>> mm(n+1,vector<vector<ll>>(1<<m));
dp.at(0).at(0)=0;
rep(i,0,n){
rep(bit,0,1<<m){
for(int sub=bit;sub>0;sub=(sub-1)&bit){
if(smw.at(sub)>e.at(i)) continue;
if(chmax(dp.at(i+1).at(bit),dp.at(i).at(bit^sub)+smv.at(sub))){
mm.at(i+1).at(bit)=mm.at(i).at(bit^sub);
mm.at(i+1).at(bit).push_back(sub);
}
}
if(chmax(dp.at(i+1).at(bit),dp.at(i).at(bit))){
mm.at(i+1).at(bit)=mm.at(i).at(bit);
mm.at(i+1).at(bit).push_back(0);
}
}
}
ll mx=-1;
int ct=-1;
rep(i,0,1<<m) if(chmax(mx,dp.at(n).at(i))) ct=i;
cout<<mx<<endl;
rep(i,0,n){
auto b=mm.at(n).at(ct).at(i);
cout<<popcount((unsigned)b)<<" ";
rep(j,0,m) if((b>>j)&1) cout<<j+1<<" ";
cout<<endl;
}
}