#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 (sum){
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
30552 KB |
Output is correct |
2 |
Correct |
5 ms |
30556 KB |
Output is correct |
3 |
Correct |
5 ms |
30640 KB |
Output is correct |
4 |
Correct |
5 ms |
30552 KB |
Output is correct |
5 |
Correct |
5 ms |
30440 KB |
Output is correct |
6 |
Execution timed out |
1000 ms |
1048576 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
30560 KB |
Output is correct |
2 |
Correct |
5 ms |
30432 KB |
Output is correct |
3 |
Correct |
4 ms |
30560 KB |
Output is correct |
4 |
Correct |
5 ms |
30556 KB |
Output is correct |
5 |
Correct |
5 ms |
30556 KB |
Output is correct |
6 |
Correct |
5 ms |
30564 KB |
Output is correct |
7 |
Correct |
5 ms |
30556 KB |
Output is correct |
8 |
Correct |
5 ms |
30564 KB |
Output is correct |
9 |
Correct |
5 ms |
30820 KB |
Output is correct |
10 |
Correct |
7 ms |
31620 KB |
Output is correct |
11 |
Correct |
5 ms |
30564 KB |
Output is correct |
12 |
Correct |
5 ms |
30424 KB |
Output is correct |
13 |
Correct |
6 ms |
30564 KB |
Output is correct |
14 |
Correct |
4 ms |
30564 KB |
Output is correct |
15 |
Correct |
4 ms |
30816 KB |
Output is correct |
16 |
Correct |
5 ms |
30552 KB |
Output is correct |
17 |
Correct |
5 ms |
30560 KB |
Output is correct |
18 |
Correct |
7 ms |
30912 KB |
Output is correct |
19 |
Correct |
8 ms |
31076 KB |
Output is correct |
20 |
Correct |
7 ms |
30680 KB |
Output is correct |
21 |
Correct |
6 ms |
30556 KB |
Output is correct |
22 |
Correct |
6 ms |
30556 KB |
Output is correct |
23 |
Correct |
5 ms |
30620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
30552 KB |
Output is correct |
2 |
Correct |
5 ms |
30556 KB |
Output is correct |
3 |
Correct |
5 ms |
30424 KB |
Output is correct |
4 |
Correct |
5 ms |
30556 KB |
Output is correct |
5 |
Correct |
5 ms |
30424 KB |
Output is correct |
6 |
Correct |
5 ms |
30440 KB |
Output is correct |
7 |
Runtime error |
792 ms |
1048576 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
30552 KB |
Output is correct |
2 |
Correct |
5 ms |
30556 KB |
Output is correct |
3 |
Correct |
5 ms |
30640 KB |
Output is correct |
4 |
Correct |
5 ms |
30552 KB |
Output is correct |
5 |
Correct |
5 ms |
30440 KB |
Output is correct |
6 |
Execution timed out |
1000 ms |
1048576 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
30552 KB |
Output is correct |
2 |
Correct |
5 ms |
30556 KB |
Output is correct |
3 |
Correct |
5 ms |
30640 KB |
Output is correct |
4 |
Correct |
5 ms |
30552 KB |
Output is correct |
5 |
Correct |
5 ms |
30440 KB |
Output is correct |
6 |
Execution timed out |
1000 ms |
1048576 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
30552 KB |
Output is correct |
2 |
Correct |
5 ms |
30556 KB |
Output is correct |
3 |
Correct |
5 ms |
30640 KB |
Output is correct |
4 |
Correct |
5 ms |
30552 KB |
Output is correct |
5 |
Correct |
5 ms |
30440 KB |
Output is correct |
6 |
Execution timed out |
1000 ms |
1048576 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |