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>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#define MAXN 3007
using namespace std;
int n,m,pt,total;
int a[MAXN],b[MAXN],ind[MAXN];
vector< pair<int,int> > v;
vector< vector<int> > w;
int maxs[MAXN],br;
vector<short> dp[MAXN][MAXN];
void calc_dp(){
for(int sum=total;sum>=0;sum--){
for(int box=1;box<=m;box++){
dp[sum][box].resize(sum/b[box]+2);
for(int pos=sum/b[box]+1;pos>=1;pos--){
if(sum==total){
dp[sum][box][pos]=0;
continue;
}
dp[sum][box][pos]=3005;
if(box>1)dp[sum][box][pos]=dp[sum][box-1][pos];
if(sum+b[box]<=maxs[pos] and sum+b[box]<=total and dp[sum][box][pos]>dp[sum+b[box]][box][pos+1]+1){
dp[sum][box][pos]=dp[sum+b[box]][box][pos+1]+1;
}
}
}
}
}
void solve(int pos,int box,int sum){
if(sum==total)return;
if(box>1 and dp[sum][box][pos]==dp[sum][box-1][pos]){
solve(pos,box-1,sum); return;
}
br++; v.push_back({b[box],br});
solve(pos+1,box,sum+b[box]);
}
bool cmp(int x,int y){
return a[x]>a[y];
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
total+=a[i];
ind[i]=i;
}
for(int i=1;i<=total;i++){
for(int f=1;f<=n;f++){
maxs[i]+=min(a[f],i);
}
}
cin>>m;
for(int i=1;i<=m;i++){
cin>>b[i];
}
calc_dp();
if(dp[0][m][1]>total){
cout<<"-1\n";
return 0;
}
solve(1,m,0);
w.resize(int(v.size()));
sort(ind+1,ind+n+1,cmp);
for(int i=1;i<=n;i++){
sort(v.begin(),v.end());
reverse(v.begin(),v.end());
for(int f=0;f<a[ind[i]];f++){
v[f].first--;
w[v[f].second-1].push_back(ind[i]);
}
}
cout<<w.size()<<"\n";
for(int i=0;i<w.size();i++){
cout<<w[i].size()<<" ";
for(int f:w[i])cout<<f<<" ";
cout<<"\n";
}
return 0;
}
Compilation message (stderr)
cookies.cpp: In function 'int main()':
cookies.cpp:102:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i=0;i<w.size();i++){
| ~^~~~~~~~~
# | 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... |