結果

問題 No.2869 yuusaan's Knapsacks
ユーザー cho435cho435
提出日時 2024-09-07 00:32:10
言語 C++17
(gcc 12.3.0 + boost 1.83.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
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 485 ms
32,668 KB
testcase_04 AC 483 ms
32,536 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1,404 ms
179,696 KB
testcase_07 AC 536 ms
32,920 KB
testcase_08 AC 128 ms
10,484 KB
testcase_09 AC 93 ms
10,484 KB
testcase_10 AC 731 ms
60,596 KB
testcase_11 AC 333 ms
19,588 KB
testcase_12 AC 1,260 ms
127,448 KB
testcase_13 AC 324 ms
19,592 KB
testcase_14 AC 716 ms
41,252 KB
testcase_15 AC 536 ms
41,256 KB
testcase_16 AC 681 ms
49,576 KB
testcase_17 AC 1,817 ms
179,320 KB
testcase_18 AC 1,255 ms
111,956 KB
testcase_19 AC 925 ms
71,996 KB
testcase_20 AC 98 ms
10,484 KB
testcase_21 AC 1,030 ms
84,420 KB
testcase_22 AC 1,225 ms
82,504 KB
testcase_23 AC 345 ms
25,492 KB
testcase_24 AC 1,471 ms
127,320 KB
testcase_25 AC 210 ms
14,596 KB
testcase_26 AC 1,572 ms
84,288 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 1,324 ms
126,192 KB
testcase_29 AC 1,400 ms
179,696 KB
権限があれば一括ダウンロードができます

ソースコード

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