This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int m;
cin >> m;
vector<int> b(m);
for (int i = 0; i < m; i++) {
cin >> b[i];
}
reverse(b.begin(), b.end());
int s = accumulate(a.begin(), a.end(), 0);
vector<long long> limit(s + 1);
for (int i = 0; i < n; i++) {
for (int j = 1; j <= s; j++) {
limit[j] += min(j, a[i]);
}
}
const int inf = (int) 1e9;
const int MAX = 15015;
vector<vector<bitset<MAX>>> dp(m);
for (int i = 0; i < m; i++) {
int t = (s / b[i]) + 1;
dp[i] = vector<bitset<MAX>>(t);
}
dp[0][0][0] = 1;
for (int i = 0; i < m; i++) {
if (i > 0) {
for (int j = 0; j < (int) dp[i - 1].size(); j++) {
dp[i][j] = dp[i - 1][j];
}
}
for (int j = 0; j + 1 < (int) dp[i].size(); j++) {
dp[i][j + 1] = (dp[i][j + 1] | ((dp[i][j] << (MAX - limit[j + 1] - 1 + b[i])) >> (MAX - limit[j + 1] - 1)));
// dp[i][j + 1] = (dp[i][j + 1] | (dp[i][j] << b[i]));
}
}
int p = 0;
for (int i = 0; i < (int) dp[m - 1].size(); i++) {
if (dp[m - 1][i][s]) {
p = i;
break;
}
}
if (!dp[m - 1][p][s]) {
cout << -1 << '\n';
return 0;
}
vector<int> v;
int i = m - 1, j = p, k = s;
while (i != -1) {
if ((i == 0 && j == 0) || (i > 0 && j < (int) dp[i - 1].size() && dp[i - 1][j][k] == 1)) {
i -= 1;
continue;
}
v.push_back(b[i]);
j -= 1;
k -= b[i];
}
sort(v.rbegin(), v.rend());
int sz = (int) v.size();
vector<vector<int>> ids(sz);
set<pair<int, int>> st;
for (int i = 0; i < n; i++) {
st.emplace(a[i], i);
}
for (int i = 0; i < sz; i++) {
vector<int> p;
for (int foo = 0; foo < v[i]; foo++) {
auto it = prev(st.end());
int id = it->second;
st.erase(it);
ids[i].emplace_back(id);
a[id] -= 1;
if (a[id] > 0) {
p.push_back(id);
}
}
for (int j : p) {
st.emplace(a[j], j);
}
}
cout << sz << '\n';
for (int i = 0; i < sz; i++) {
cout << (int) ids[i].size() << " ";
for (int j : ids[i]) {
cout << j + 1 << " ";
}
cout << '\n';
}
return 0;
}
Compilation message (stderr)
cookies.cpp: In function 'int main()':
cookies.cpp:28:13: warning: unused variable 'inf' [-Wunused-variable]
28 | const int inf = (int) 1e9;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |