#include <bits/stdc++.h>
using namespace std;
#define int long long
main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n, m, k; cin >> n >> m >> k;
int buckets[n], balls[m];
for (int x = 0; x < n; x++) cin >> buckets[x];
for (int x = 0; x < m; x++) cin >> balls[x];
#define IMP cout << "Impossible"; return 0;
bool dead = false;
for (int x = 0; x < n; x++){
if (buckets[x] < k){
dead = true;
}
}
if (dead) {IMP;}
int bucketsum = 0;
for (int x = 0; x < n; x++){
bucketsum += buckets[x];
}
#define BSMAX 100000
deque<bitset<BSMAX>> hmm;
bitset<BSMAX> init;
hmm.push_back(init);
int firstbucket = n*k, lastbucket = n*k;
hmm[n*k - firstbucket][0] = 1;
sort(balls, balls+m, greater<int>());
int sum = 0, ssum[m];
for (int x = m-1; x > -1; x--){
sum += min(n, balls[x]);
ssum[x] = sum;
}
sum = 0; int psum[m];
for (int x = 0; x < m; x++){
sum += balls[x];
psum[x] = sum;
}
for (int z = n*k; z <= n*k; z++){
while (firstbucket > max(0LL, z - min(balls[0], n)) ){
hmm.push_front(init);
firstbucket--;
}
while (lastbucket > min(n*k, ssum[0])){
hmm.pop_back();
lastbucket--;
}
hmm[max(0LL, z - min(balls[0], n)) - firstbucket] |= (hmm[z - firstbucket] << balls[0]);
}
for (int x = 1; x < m; x++){
int kk = max(0LL, max(0LL, n*k - psum[x-1]) - min(balls[x], n));
int zz = min(n*k, ssum[x]);
while (firstbucket > kk){
hmm.push_front(init);
firstbucket--;
}
while (lastbucket > zz){
hmm.pop_back();
lastbucket--;
}
for (int z = max(0LL, n*k - psum[x-1]); z <= min(n*k, ssum[x]); z++){
assert(max(0LL, z - min(balls[x], n)) >= firstbucket);
assert(z <= lastbucket);
hmm[max(0LL, z - min(balls[x], n)) - firstbucket] |= (hmm[z - firstbucket] << balls[x]);
}
}
int ans = -1;
for (int x = bucketsum; x < BSMAX; x++){
if (hmm[0][x]){
ans = x-bucketsum;
break;
}
}
if (ans == -1) cout << "Impossible";
else cout << ans;
}
Compilation message
kitchen.cpp:6:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
6 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
3 ms |
3932 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
3 ms |
3932 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
1372 KB |
Output is correct |
13 |
Correct |
66 ms |
26236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
190 ms |
3932 KB |
Output is correct |
2 |
Correct |
166 ms |
3932 KB |
Output is correct |
3 |
Correct |
211 ms |
3932 KB |
Output is correct |
4 |
Correct |
214 ms |
3932 KB |
Output is correct |
5 |
Correct |
215 ms |
4188 KB |
Output is correct |
6 |
Correct |
146 ms |
3932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
6236 KB |
Output is correct |
2 |
Correct |
13 ms |
3416 KB |
Output is correct |
3 |
Correct |
39 ms |
7512 KB |
Output is correct |
4 |
Correct |
4 ms |
1724 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
3 ms |
3932 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
1372 KB |
Output is correct |
13 |
Correct |
66 ms |
26236 KB |
Output is correct |
14 |
Correct |
190 ms |
3932 KB |
Output is correct |
15 |
Correct |
166 ms |
3932 KB |
Output is correct |
16 |
Correct |
211 ms |
3932 KB |
Output is correct |
17 |
Correct |
214 ms |
3932 KB |
Output is correct |
18 |
Correct |
215 ms |
4188 KB |
Output is correct |
19 |
Correct |
146 ms |
3932 KB |
Output is correct |
20 |
Correct |
26 ms |
6236 KB |
Output is correct |
21 |
Correct |
13 ms |
3416 KB |
Output is correct |
22 |
Correct |
39 ms |
7512 KB |
Output is correct |
23 |
Correct |
4 ms |
1724 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Execution timed out |
1082 ms |
95844 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |