Submission #1148654

#TimeUsernameProblemLanguageResultExecution timeMemory
1148654AmirAli_H1Cookies (JOI23_cookies)C++20
13 / 100
765 ms55792 KiB
// In the name of Allah

#include <bits/stdc++.h>
using namespace std;

typedef		long long int			ll;
typedef		long double				ld;
typedef		pair<int, int>			pii;
typedef		pair<ll, ll>			pll;
typedef		complex<ld>				cld;

#define		all(x)					(x).begin(),(x).end()
#define		len(x)					((ll) (x).size())
#define		F						first
#define		S						second
#define		pb						push_back
#define		sep						' '
#define		endl					'\n'
#define		Mp						make_pair
#define		kill(x)					cout << x << '\n', exit(0)
#define		set_dec(x)				cout << fixed << setprecision(x);
#define		file_io(x,y)			freopen(x, "r", stdin); freopen(y, "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 15000 + 4;

int n, m, sm;
int A[maxn], B[maxn];
bitset<maxn> dp[maxn], Mx[maxn], M;
int valx[maxn], par[maxn]; bool ok[maxn];
vector<int> res; vector<pii> lsx;
priority_queue<pii> qu;

void solve() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> A[i];
		qu.push(Mp(A[i], i));
	}
	cin >> m;
	for (int i = 0; i < m; i++) {
		cin >> B[i]; ok[B[i]] = 1;
	}

	for (int i = 0; i < n; i++) {
		valx[1]++; valx[A[i] + 1]--;
		sm += A[i];
	}
	for (int i = 1; i <= sm; i++) valx[i] += valx[i - 1];
	for (int i = 1; i <= sm; i++) valx[i] += valx[i - 1];
	for (int i = 0; i <= sm; i++) {
		for (int j = 0; j <= valx[i]; j++) Mx[i][j] = 1;
	}

	dp[0][0] = 1;
	for (int i = sm; i >= 1; i--) {
		if (!ok[i]) continue;
		for (int j = 1; j <= (sm / i); j++) {
			dp[j] |= (dp[j - 1] << i);
			dp[j] &= Mx[j];
		}
	}

	for (int i = 0; i <= sm; i++) {
		for (int j = 0; j <= valx[i]; j++) {
			Mx[i][j] = (dp[i][j] && !M[j]);
		}
		M |= dp[i];
	}
	if (!M[sm]) {
		cout << -1 << endl;
		return ;
	}
	
	for (int i = 0; i <= sm; i++) dp[i].reset();
	dp[0][0] = 1;
	for (int i = sm; i >= 1; i--) {
		if (!ok[i]) continue;
		for (int j = 1; j <= (sm / i); j++) {
			M = dp[j]; dp[j] |= (dp[j - 1] << i);
			dp[j] &= Mx[j]; M ^= dp[j];
			if (M.count() > 0) {
				for (int R = 0; R <= sm; R++) {
					if (M[R]) par[R] = i;
				}
			}
		}
	}

	int x = sm;
	while (x > 0) {
		if (par[x] == 0) exit(23);
		res.pb(par[x]); x -= par[x];
	}
	reverse(all(res));
	cout << len(res) << endl;
	for (int x : res) {
		cout << x << sep; lsx.clear();
		for (int T = 0; T < x; T++) {
			auto f = qu.top(); qu.pop();
			cout << f.S + 1 << sep; f.F--; lsx.pb(f);
		}
		for (auto f : lsx) qu.push(f);
		cout << endl;
	}
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	
	int T = 1;
	while (T--) {
		solve();
	}
	
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...