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;
const int N=15e3+10;
bool f[N][N];
int n, m, a[N], b[N];
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i=1; i<=n; ++i) cin >> a[i];
cin >> m;
for (int i=1; i<=m; ++i) cin >> b[i];
int sum=accumulate(a+1, a+n+1, 0);
f[0][0]=1;
for (int i=1; i<=m; ++i){
memcpy(f[i], f[i-1], sizeof f[i]);
for (int j=b[i]; j<=sum; ++j) f[i][j]|=f[i][j-b[i]];
}
if (!f[m][sum]){
cout << -1 << '\n';
return 0;
}
priority_queue<pair<int, int>> pq;
vector<vector<int>> ans;
for (int i=1; i<=n; ++i) pq.emplace(a[i], i);
for (int i=m; i>=1; --i){
auto _pq=pq;
int j;
vector<int> vv;
for (j=0; (int)pq.size()>=b[i]; ++j){
vector<int> v;
for (int k=0; k<b[i]; ++k){
v.push_back(pq.top().second);
vv.push_back(pq.top().second);
pq.pop();
}
for (int k:v){
--a[k];
if (a[k]) pq.emplace(a[k], k);
}
}
pq=_pq;
for (int k:vv) ++a[k];
for (; j>=0; --j){
if (f[i-1][sum-j*b[i]]){
while (j--){
ans.emplace_back();
for (int k=0; k<b[i]; ++k){
ans.back().push_back(pq.top().second);
pq.pop();
}
for (int k:ans.back()){
--a[k];
if (a[k]) pq.emplace(a[k], k);
}
sum-=b[i];
}
}
}
}
if (sum){
cout << -1 << '\n';
return 0;
}
cout << ans.size() << '\n';
for (auto &i:ans){
cout << i.size();
for (int j:i) cout << ' ' << j;
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... |