Submission #1148664

#TimeUsernameProblemLanguageResultExecution timeMemory
1148664AmirAli_H1Cookies (JOI23_cookies)C++20
100 / 100
558 ms326812 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];
vector<bitset<maxn>> dp[maxn];
bitset<maxn> Mx[maxn];
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;
	}

	for (int i = 1; i <= sm + 1; i++) dp[i].resize((sm / i) + 1);
	dp[sm + 1][0][0] = 1;
	for (int i = sm; i >= 1; i--) {
		dp[i][0] = dp[i + 1][0];
		for (int j = 1; j <= (sm / i); j++) {
			if (j < len(dp[i + 1])) dp[i][j] = dp[i + 1][j];
			else dp[i][j].reset();
			if (ok[i]) {
				dp[i][j] |= (dp[i][j - 1] << i); dp[i][j] &= Mx[j];
			}
		}
	}

	int i = -1;
	for (int j = 1; j <= sm; j++) {
		if (dp[1][j][sm]) {
			i = j;
			break;
		}
	}
	if (i == -1) {
		cout << -1 << endl;
		return ;
	}
	
	int j = i; i = 1;
	int x = sm;
	while (j > 0) {
		if (j < len(dp[i + 1]) && dp[i + 1][j][x]) i++;
		else {
			res.pb(i);
			x -= i; j--;
		}
	}
	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...