結果

問題 No.2869 yuusaan's Knapsacks
ユーザー cho435cho435
提出日時 2024-09-07 00:32:10
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,817 ms / 4,500 ms
コード長 1,575 bytes
コンパイル時間 4,798 ms
コンパイル使用メモリ 272,184 KB
実行使用メモリ 179,696 KB
最終ジャッジ日時 2024-09-07 00:32:37
合計ジャッジ時間 26,660 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#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<<__builtin_popcount(b)<<" ";
		rep(j,0,m) if((b>>j)&1) cout<<j+1<<" ";
		cout<<endl;
	}
}
0