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];
}
sort(b.begin(), b.end());
int sa = accumulate(a.begin(), a.end(), 0);
const int inf = (int) 1e9;
vector<int> dp(sa + 1, inf);
dp[0] = 0;
vector<int> to(sa + 1);
for (int r = 0; r < m; r++) {
for (int i = 0; i < sa; i++) {
if (dp[i] == inf) {
continue;
}
int left = 0;
{
int cur = i;
vector<int> f;
while (cur > 0) {
f.push_back(cur - to[cur]);
cur = to[cur];
}
reverse(f.begin(), f.end());
set<pair<int, int>> all;
for (int j = 0; j < n; j++) {
all.emplace(a[j], j);
}
for (int x : f) {
vector<pair<int, int>> to_add;
for (int iter = 0; iter < x; iter++) {
assert(!all.empty());
auto it = prev(all.end());
int idx = it->second;
int val = it->first;
all.erase(it);
if (val > 1) {
to_add.emplace_back(val - 1, idx);
}
}
for (auto& p : to_add) {
all.insert(p);
}
}
left = (int) all.size();
}
if (left >= b[r] && i + b[r] <= sa && dp[i + b[r]] >= dp[i] + 1) {
dp[i + b[r]] = dp[i] + 1;
to[i + b[r]] = i;
}
}
}
if (dp[sa] == inf) {
cout << -1 << '\n';
return 0;
}
vector<int> f;
int p = sa;
while (p > 0) {
f.push_back(p - to[p]);
p = to[p];
}
reverse(f.begin(), f.end());
vector<vector<int>> res;
multiset<pair<int, int>> all;
for (int i = 0; i < n; i++) {
all.emplace(a[i], i);
}
for (int x : f) {
res.push_back({});
vector<int> ids;
for (int i = 0; i < x; i++) {
assert(!all.empty());
auto it = prev(all.end());
res.back().push_back(it->second);
ids.push_back(it->second);
all.erase(it);
}
for (int i : ids) {
a[i] -= 1;
if (a[i] > 0) {
all.emplace(a[i], i);
}
}
}
cout << (int) res.size() << '\n';
for (auto& p : res) {
cout << (int) p.size() << " ";
for (int i = 0; i < (int) p.size(); i++) {
cout << p[i] + 1 << " ";
}
cout << '\n';
}
cout << '\n';
return 0;
}
# | 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... |