// https://oj.uz/problem/view/BOI19_kitchen
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1000 * 1000 * 1000;
const ll LINF = (ll)INF * INF;
#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define msub(a, b) ((mod + ((a) - (b)) % mod) % mod)
#define mdiv(a, b) ((a) * poww((b), mod - 2) % mod)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'
#define MAXN 320
bitset<MAXN * MAXN> sdp[MAXN];
bool fdp[MAXN * MAXN];
int fsum[MAXN * MAXN];
int a[MAXN], b[MAXN];
int n, m, k;
int asum, bsum;
int32_t main(void)
{
fast_io;
cin >> n >> m >> k;
FOR(i, 0, n)
{
cin >> a[i];
if (a[i] < k)
{
cout << "Impossible\n";
return 0;
}
asum += a[i];
}
FOR(i, 0, m)
{
cin >> b[i];
bsum += b[i];
}
sort(b, b + m);
int p;
for (p = 0; p < m; p++)
{
if (b[p] > n)
{
break;
}
}
// fdp
fdp[0] = 1;
FOR(i, 0, p)
{
for (int j = bsum; j >= b[i]; j--)
{
fdp[j] |= fdp[j - b[i]];
}
}
// fsum
int last = INF;
for (int i = MAXN * MAXN - 1; i >= 0; i--)
{
if (fdp[i])
{
last = i;
}
fsum[i] = last;
}
// sdp
sdp[0][0] = 1;
FOR(i, p, m)
{
for (int x = i - p + 1; x > 0; x--)
{
sdp[x] |= (sdp[x - 1] << b[i]);
}
}
// find ans
int ans = INF;
FOR(x, 0, m - p + 1)
{
FOR(j, 0, bsum + 1)
{
if (!sdp[x][j])
continue;
if (max(n * (k - x), asum - j) < 0)
{
ans = min(ans, j);
}
else
{
ans = min(ans, j + fsum[max(n * (k - x), asum - j)]);
}
}
}
if (ans >= INF)
{
cout << "Impossible\n";
return 0;
}
cout << ans - asum << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
1 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
748 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
1 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
748 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
748 KB |
Output is correct |
9 |
Correct |
1 ms |
1004 KB |
Output is correct |
10 |
Correct |
1 ms |
1004 KB |
Output is correct |
11 |
Correct |
1 ms |
876 KB |
Output is correct |
12 |
Correct |
1 ms |
748 KB |
Output is correct |
13 |
Correct |
1 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
876 KB |
Output is correct |
2 |
Correct |
10 ms |
748 KB |
Output is correct |
3 |
Correct |
17 ms |
876 KB |
Output is correct |
4 |
Correct |
38 ms |
2156 KB |
Output is correct |
5 |
Correct |
18 ms |
876 KB |
Output is correct |
6 |
Correct |
8 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1260 KB |
Output is correct |
2 |
Correct |
2 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
1 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
1 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
748 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
748 KB |
Output is correct |
9 |
Correct |
1 ms |
1004 KB |
Output is correct |
10 |
Correct |
1 ms |
1004 KB |
Output is correct |
11 |
Correct |
1 ms |
876 KB |
Output is correct |
12 |
Correct |
1 ms |
748 KB |
Output is correct |
13 |
Correct |
1 ms |
748 KB |
Output is correct |
14 |
Correct |
13 ms |
876 KB |
Output is correct |
15 |
Correct |
10 ms |
748 KB |
Output is correct |
16 |
Correct |
17 ms |
876 KB |
Output is correct |
17 |
Correct |
38 ms |
2156 KB |
Output is correct |
18 |
Correct |
18 ms |
876 KB |
Output is correct |
19 |
Correct |
8 ms |
748 KB |
Output is correct |
20 |
Correct |
5 ms |
1260 KB |
Output is correct |
21 |
Correct |
2 ms |
1004 KB |
Output is correct |
22 |
Correct |
1 ms |
748 KB |
Output is correct |
23 |
Correct |
1 ms |
748 KB |
Output is correct |
24 |
Correct |
1 ms |
396 KB |
Output is correct |
25 |
Correct |
89 ms |
3308 KB |
Output is correct |
26 |
Correct |
67 ms |
3052 KB |
Output is correct |
27 |
Correct |
20 ms |
1900 KB |
Output is correct |
28 |
Correct |
43 ms |
2412 KB |
Output is correct |
29 |
Correct |
126 ms |
3948 KB |
Output is correct |
30 |
Correct |
55 ms |
2668 KB |
Output is correct |