結果

問題 No.2869 yuusaan's Knapsacks
ユーザー kwm_tkwm_t
提出日時 2024-08-31 00:02:17
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,306 ms / 4,500 ms
コード長 2,153 bytes
コンパイル時間 3,340 ms
コンパイル使用メモリ 258,672 KB
実行使用メモリ 22,320 KB
最終ジャッジ日時 2024-08-31 00:02:38
合計ジャッジ時間 20,068 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 398 ms
10,872 KB
testcase_04 AC 367 ms
11,004 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1,026 ms
22,312 KB
testcase_07 AC 409 ms
11,004 KB
testcase_08 AC 101 ms
6,940 KB
testcase_09 AC 74 ms
6,944 KB
testcase_10 AC 567 ms
13,964 KB
testcase_11 AC 258 ms
8,948 KB
testcase_12 AC 939 ms
19,236 KB
testcase_13 AC 249 ms
8,948 KB
testcase_14 AC 508 ms
12,040 KB
testcase_15 AC 437 ms
11,912 KB
testcase_16 AC 504 ms
13,192 KB
testcase_17 AC 1,306 ms
22,316 KB
testcase_18 AC 935 ms
18,204 KB
testcase_19 AC 698 ms
14,992 KB
testcase_20 AC 80 ms
6,944 KB
testcase_21 AC 754 ms
16,276 KB
testcase_22 AC 894 ms
16,148 KB
testcase_23 AC 274 ms
9,976 KB
testcase_24 AC 1,077 ms
19,100 KB
testcase_25 AC 162 ms
7,916 KB
testcase_26 AC 1,076 ms
16,144 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 1,002 ms
22,316 KB
testcase_29 AC 1,027 ms
22,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r) - 1;i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, m; cin >> n >> m;

	vector<long long>e(n);
	rep(i, n)cin >> e[i];

	vector<long long>v(m), w(m);
	rep(i, m)cin >> v[i] >> w[i];

	vector<pair<long long, long long>>sum(1 << m);
	rep(i, 1 << m) {
		long long sv = 0;
		long long sw = 0;
		rep(j, m) {
			if (1 & (i >> j)) {
				sv += v[j];
				sw += w[j];
			}
		}
		sum[i] = { sv,sw };
	}
	long long mx = 0;
	vector dp(n + 1, vector<long long>((1 << m), -LINF));
	vector pre(n + 1, vector<long long>(1 << m));
	dp[0][0] = 0;
	int idx = 0;
	rep(i, n) {
		rep(j, 1 << m) {
			for (int k = (j - 1)&j; k > 0; k = (k - 1)&j) {
				int l = j ^ k;
				if (sum[l].second <= e[i] && chmax(dp[i + 1][j], dp[i][k] + sum[l].first)) {
					pre[i + 1][j] = k;
				}
			}
			if (sum[j].second <= e[i] && chmax(dp[i + 1][j], dp[i][0] + sum[j].first)) {
				pre[i + 1][j] = 0;
			}
			if (chmax(dp[i + 1][j], dp[i][j])) {
				pre[i + 1][j] = j;
			}
			if (chmax(mx, dp[i + 1][j]))idx = j;
		}
	}
	cout << mx << endl;
	vector<int>ans;
	rrep2(i, 1, n + 1) {
		ans.push_back(idx);
		idx = pre[i][idx];
		ans.back() ^= idx;
	}
	reverse(all(ans));
	rep(i, ans.size()) {
		vector<int>v;
		rep(j, m) if (1 & (ans[i] >> j)) v.push_back(j);
		cout << v.size() << " ";
		for (auto e : v)cout << e + 1 << " ";
		cout << endl;
	}
	return 0;
}
0