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;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second
void solve(){
int n; cin>>n;
vi a(n+1); FOR(i,1,n+1) cin>>a[i];
int m; cin>>m;
vi b(m); FOR(i,0,m) cin>>b[i];
int S=0; FOR(i,1,n+1) S += a[i];
if (S==n){
//subtask 1
vi dp(n+1,-1); dp[0]=0;
vi pre(n+1,-1);
FOR(i,0,m){
FOR(j,b[i],n+1){
if (dp[j-b[i]]==-1) continue;
if (dp[j]==-1 || dp[j]>dp[j-b[i]]+1){
dp[j]=dp[j-b[i]]+1; pre[j]=b[i];
}
}
}
cout<<dp[n]<<"\n";
if (dp[n]==-1) return;
int cur=n;
while (cur>0){
int sz = pre[cur];
cout<<sz;
FOR(i,cur-sz+1,cur+1) cout<<" "<<i;
cout<<"\n";
cur-=sz;
}
return;
}
if (m==1){
if (S%b[0] != 0){cout<<-1<<"\n"; return;}
priority_queue<ii> pq;
vector<vi> ans;
FOR(i,1,n+1) pq.push({a[i],i});
while (pq.size()>=b[0]){
vi val;
FOR(i,0,b[0]){
ii A = pq.top(); pq.pop();
int j = A.se;
val.pb(j); a[j]--;
}
ans.pb(val);
FOR(i,0,b[0]){
int j=val[i];
if (a[j]>0) pq.push({a[j],j});
}
}
if (!pq.empty()) cout<<-1<<"\n";
else{
cout<<S/b[0]<<"\n";
FOR(i,0,S/b[0]){
cout<<b[0];
FOR(j,0,b[0]) cout<<" "<<ans[i][j];
cout<<"\n";
}
}
return;
}
}
int main(){
//MOD=MOD1;
ios::sync_with_stdio(false);
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
int TC = 1;
//cin >> TC;
FOR(i, 1, TC+1){
//cout << "Case #" << i << ": ";
solve();
}
return 0;
}
Compilation message (stderr)
cookies.cpp: In function 'void solve()':
cookies.cpp:51:21: warning: comparison of integer expressions of different signedness: 'std::priority_queue<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} [-Wsign-compare]
51 | while (pq.size()>=b[0]){
cookies.cpp: In function 'int main()':
cookies.cpp:81:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
cookies.cpp:82:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |