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;
#ifdef sus
const int N=100, M=20;
#else
const int N=15e3+10, M=510;
#endif
int f[N][M];
int n, m, a[N], b[N], pf[N], _a[N], d[N];
int nxt[N], prv[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];
for (int i=1; i<=n; ++i) a[i]=_a[i];
int sum=accumulate(a+1, a+n+1, 0);
sort(a+1, a+n+1, greater<int>());
int mx=b[m];
if (mx<n){
for (int i=mx+1; i<=n; ++i) a[mx]+=a[i];
}
for (int i=1; i<=mx; ++i) pf[i]=pf[i-1]+a[mx-i+1];
for (int i=1; i<=m; ++i) d[i]=b[i]-b[i-1];
reverse(d+1, d+m+1);
int cur=0;
for (int i=1; i<=m; ++i) nxt[cur]=cur+d[i], prv[cur+d[i]]=cur, cur+=d[i];
memset(f, 0x3f, sizeof f);
f[0][0]=0;
for (int i=0; i<mx; i=nxt[i]){
for (int j=0; j<=pf[i]; ++j){
int l=0, r=sum;
while (l<=r){
int mid=(l+r)>>1;
bool check=1;
for (int k=i; k<=nxt[i]; ++k) check&=j+mid*(k-i)<=pf[k];
if (check) l=mid+1;
else r=mid-1;
}
for (int val=f[i][j]; val<=r; ++val){
f[nxt[i]][j+val*(nxt[i]-i)]=min(f[nxt[i]][j+val*(nxt[i]-i)], val);
}
}
}
if (f[mx][sum]>1e9){
cout << -1 << '\n';
return 0;
}
vector<int> tr;
vector<int> sz;
while (mx){
int val=f[mx][sum];
tr.push_back(val);
sum-=val*(mx-prv[mx]);
mx=prv[mx];
}
reverse(tr.begin(), tr.end());
for (int i=0; i<m; ++i){
int cnt=tr[i]-(i?tr[i-1]:0);
while (cnt--) sz.push_back(b[m-i]);
}
priority_queue<pair<int, int>> pq;
vector<vector<int>> ans;
for (int i=1; i<=n; ++i) a[i]=_a[i];
for (int i=1; i<=n; ++i) pq.emplace(a[i], i);
for (int i:sz){
ans.emplace_back();
for (int j=0; j<i; ++j){
if (pq.empty()){
cout << -1 << '\n';
return 0;
}
ans.back().push_back(pq.top().second);
pq.pop();
}
for (int j:ans.back()){
--a[j];
if (a[j]) pq.emplace(a[j], j);
}
}
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... |