#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<bitset<N * 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(i, N) REP(j, N * N) {
dp2[i][j] = 0;
}
REP(i, bsum + 1) {
if(dp1[i]) {
int x = i / n;
x = min(x, k);
dp2[x][i - x * n] = 1;
}
}
for(int i = mid + 1; i <= m; i++) {
for(int j = 0; j <= k; j++) {
for(int s = 0; s <= bsum; s++) {
dp2n[j][s] = dp2[j][s];
if(j > 0 && s - (b[i] - n) >= 0) {
dp2n[j][s] = dp2n[j][s] || dp2[j - 1][s - (b[i] - n)];
}
if(s - b[i] >= 0) {
dp2n[j][s] = dp2n[j][s] || dp2[j][s - b[i]];
}
}
}
swap(dp2, dp2n);
}
sum -= n * k;
for(int s = sum; s <= bsum; s++) {
if(dp2[k][s]) {
cout << s - sum << "\n";
return;
}
}
cout << "Impossible\n";
}
signed main() {
fastio();
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
7252 KB |
Output is correct |
2 |
Correct |
47 ms |
7296 KB |
Output is correct |
3 |
Correct |
48 ms |
7260 KB |
Output is correct |
4 |
Correct |
47 ms |
7380 KB |
Output is correct |
5 |
Correct |
48 ms |
5092 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
47 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
7252 KB |
Output is correct |
2 |
Correct |
47 ms |
7296 KB |
Output is correct |
3 |
Correct |
48 ms |
7260 KB |
Output is correct |
4 |
Correct |
47 ms |
7380 KB |
Output is correct |
5 |
Correct |
48 ms |
5092 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
47 ms |
7380 KB |
Output is correct |
9 |
Correct |
52 ms |
7260 KB |
Output is correct |
10 |
Correct |
50 ms |
7248 KB |
Output is correct |
11 |
Correct |
48 ms |
7252 KB |
Output is correct |
12 |
Correct |
45 ms |
5120 KB |
Output is correct |
13 |
Correct |
46 ms |
5080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
5080 KB |
Output is correct |
2 |
Correct |
95 ms |
5084 KB |
Output is correct |
3 |
Correct |
105 ms |
7252 KB |
Output is correct |
4 |
Correct |
191 ms |
7412 KB |
Output is correct |
5 |
Correct |
139 ms |
4948 KB |
Output is correct |
6 |
Correct |
84 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
7388 KB |
Output is correct |
2 |
Correct |
52 ms |
7404 KB |
Output is correct |
3 |
Correct |
46 ms |
4948 KB |
Output is correct |
4 |
Correct |
45 ms |
5104 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
7252 KB |
Output is correct |
2 |
Correct |
47 ms |
7296 KB |
Output is correct |
3 |
Correct |
48 ms |
7260 KB |
Output is correct |
4 |
Correct |
47 ms |
7380 KB |
Output is correct |
5 |
Correct |
48 ms |
5092 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
47 ms |
7380 KB |
Output is correct |
9 |
Correct |
52 ms |
7260 KB |
Output is correct |
10 |
Correct |
50 ms |
7248 KB |
Output is correct |
11 |
Correct |
48 ms |
7252 KB |
Output is correct |
12 |
Correct |
45 ms |
5120 KB |
Output is correct |
13 |
Correct |
46 ms |
5080 KB |
Output is correct |
14 |
Correct |
111 ms |
5080 KB |
Output is correct |
15 |
Correct |
95 ms |
5084 KB |
Output is correct |
16 |
Correct |
105 ms |
7252 KB |
Output is correct |
17 |
Correct |
191 ms |
7412 KB |
Output is correct |
18 |
Correct |
139 ms |
4948 KB |
Output is correct |
19 |
Correct |
84 ms |
4948 KB |
Output is correct |
20 |
Correct |
59 ms |
7388 KB |
Output is correct |
21 |
Correct |
52 ms |
7404 KB |
Output is correct |
22 |
Correct |
46 ms |
4948 KB |
Output is correct |
23 |
Correct |
45 ms |
5104 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Execution timed out |
1030 ms |
7400 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |