#include <bits/stdc++.h>
#define lli long long int
#define ld long double
#define pb push_back
#define MP make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.begin(), x.end()
#define REP(i, n) for(int i = 0; (i) < (n); (i)++)
using namespace std;
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int N = 305;
const int INF = 1e9 + 500;
const int MOD = 1e9 + 7;
bitset<N * N> dp1, dp1n;
array<int, N * N> dp2, dp2n;
int n, m, k;
int sum = 0;
vector<int> a, b;
void solve() {
cin >> n >> m >> k;
a.resize(n + 1, 0);
b.assign(m + 1, 0);
bool f = 0;
for(int i = 1; i <= n; i++) {
cin >> a[i];
if(a[i] < k) f = 1;
sum += a[i];
}
int mid = 0;
int bsum = 0;
for(int i = 1; i <= m; i++) {
cin >> b[i];
bsum += b[i];
if(b[i] <= n) mid++;
}
if(f) {
cout << "Impossible\n";
return;
}
sort(all(b));
REP(i, N * N) {
dp1[i] = 0;
}
dp1[0] = 1;
for(int i = 1; i <= mid; i++) {
for(int j = 0; j <= bsum; j++) {
dp1n[j] = dp1[j];
if(j - b[i] >= 0) {
dp1n[j] = dp1n[j] | dp1[j - b[i]];
}
}
swap(dp1, dp1n);
}
REP(j, N * N) {
dp2[j] = -1;
}
REP(i, bsum + 1) {
if(dp1[i]) {
int x = i / n;
dp2[i] = max(x, dp2[i]);
}
}
for(int i = mid + 1; i <= m; i++) {
for(int s = 0; s <= bsum; s++) {
dp2n[s] = dp2[s];
if(s - b[i] >= 0 && dp2[s - b[i]] != -1) {
dp2n[s] = max(dp2n[s], dp2[s - b[i]] + 1);
}
}
swap(dp2, dp2n);
}
for(int s = sum; s <= bsum; s++) {
if(dp2[s] >= k) {
cout << s - sum << "\n";
return;
}
}
cout << "Impossible\n";
}
signed main() {
fastio();
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
2 ms |
1116 KB |
Output is correct |
10 |
Correct |
2 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1212 KB |
Output is correct |
12 |
Correct |
1 ms |
856 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
728 KB |
Output is correct |
2 |
Correct |
52 ms |
748 KB |
Output is correct |
3 |
Correct |
57 ms |
1004 KB |
Output is correct |
4 |
Correct |
77 ms |
988 KB |
Output is correct |
5 |
Correct |
93 ms |
732 KB |
Output is correct |
6 |
Correct |
40 ms |
736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1116 KB |
Output is correct |
2 |
Correct |
3 ms |
1116 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
624 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
2 ms |
1116 KB |
Output is correct |
10 |
Correct |
2 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1212 KB |
Output is correct |
12 |
Correct |
1 ms |
856 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
65 ms |
728 KB |
Output is correct |
15 |
Correct |
52 ms |
748 KB |
Output is correct |
16 |
Correct |
57 ms |
1004 KB |
Output is correct |
17 |
Correct |
77 ms |
988 KB |
Output is correct |
18 |
Correct |
93 ms |
732 KB |
Output is correct |
19 |
Correct |
40 ms |
736 KB |
Output is correct |
20 |
Correct |
3 ms |
1116 KB |
Output is correct |
21 |
Correct |
3 ms |
1116 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
624 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
27 ms |
1116 KB |
Output is correct |
26 |
Correct |
41 ms |
1116 KB |
Output is correct |
27 |
Correct |
17 ms |
1116 KB |
Output is correct |
28 |
Correct |
38 ms |
1012 KB |
Output is correct |
29 |
Correct |
35 ms |
1400 KB |
Output is correct |
30 |
Correct |
78 ms |
1108 KB |
Output is correct |