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;
vector<vector<vector<int>>> dp(m, vector<vector<int>>(s + 1));
for (int i = 0; i < m; i++) {
for (int j = 0; j <= s; j++) {
int t = (s / b[i]) + 1;
dp[i][j] = vector<int>(t, inf);
}
}
dp[0][0][0] = 0;
for (int i = 0; i < m; i++) {
if (i > 0) {
for (int j = 0; j <= s; j++) {
for (int k = 0; k < (int) dp[i - 1][j].size(); k++) {
dp[i][j][k] = dp[i - 1][j][k];
}
}
}
for (int j = 0; j + b[i] <= s; j++) {
for (int k = 0; k + 1 < (int) dp[i][j].size(); k++) {
if (limit[k + 1] < j + b[i]) {
continue;
}
if (dp[i][j + b[i]][k + 1] == inf && dp[i][j][k] != inf) {
dp[i][j + b[i]][k + 1] = dp[i][j][k] + 1;
}
}
}
}
int p = 0;
for (int i = 0; i < (int) dp[m - 1][s].size(); i++) {
if (dp[m - 1][s][i] < dp[m - 1][s][p]) {
p = i;
}
}
int ans = dp[m - 1][s][p];
if (ans == inf) {
cout << -1 << '\n';
return 0;
}
cout << ans << '\n';
vector<int> v;
int i = m - 1, j = s, k = p;
while (i != -1) {
if ((i == 0 && j == 0) || (i > 0 && k < (int) dp[i - 1][j].size() && dp[i][j][k] == dp[i - 1][j][k])) {
i -= 1;
continue;
}
v.push_back(b[i]);
j -= b[i];
k -= 1;
}
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);
}
}
for (int i = 0; i < sz; i++) {
cout << (int) ids[i].size() << " ";
for (int j : ids[i]) {
cout << j + 1 << " ";
}
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... |