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); FOR(i,0,n) cin>>a[i];
int m; cin>>m;
vi b(m); FOR(i,0,m) cin>>b[i];
int S=0; FOR(i,0,n) 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;
}
}
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 'int main()':
cookies.cpp:52:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
cookies.cpp:53:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | 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... |