#pragma GCC optimize("O3", "unroll-loops") // "Ofast"
#pragma GCC target("avx2", "bmi", "bmi2", "lzcnt", "popcnt")
#include <bits/stdc++.h>
#define int long long
#define vt vector
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define f first
#define s second
#define dbg(x) cerr << #x << " = " << x << '\n'
#define bit(x, i) ((x) >> (i) & 1)
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
const int N = 1e6 + 5, mod = 1e9 + 7;
const ll inf = 1e18 + 7;
const ld eps = 1e-6;
ll add (ll a, ll b) {
a += b;
if (a < 0) a += mod;
if (a >= mod) a -= mod;
return a;
}
ll mul (ll a, ll b) {
a *= b;
if (a >= mod) a %= mod;
return a;
}
int n, m, k, a[N], b[N], c[N], dp[N];
void solve () {
cin >> n >> m >> k;
int sum = 0;
for (int i = 1; i <= n; i++) cin >> a[i], sum += a[i];
for (int i = 1; i <= m; i++) cin >> b[i];
if (k == 1) {
dp[0] = 1;
for (int i = 1; i <= m; i++) {
for (int w = 90000 - b[i]; w >= 0; w--) dp[w + b[i]] |= dp[w];
}
for (int i = sum; i <= 90000; i++) {
if (dp[i]) cout << i - sum << '\n', exit(0);
}
cout << "Impossible\n";
return;
}
int ans = inf;
for (int mask = 0; mask < (1 << m); mask++) {
priority_queue<int> q;
int res = 0;
for (int i = 0; i < m; i++) {
if (bit(mask, i)) q.push(b[i + 1]);
}
if (sz(q) < k) continue;
for (int i = 1; i <= n; i++) c[i] = a[i];
bool ok = 1;
for (int i = 1; i <= n && ok; i++) {
if (c[i] < k) {
ok = 0;
break;
}
c[i] -= k;
int kol = 0;
while (!q.empty()) {
int x = q.top();
q.pop();
if (x == 0) {
ok = 0;
break;
}
x--, kol++;
q.push(x);
if (kol == k) break;
}
if (kol != k) ok = 0;
}
for (int i = 1; i <= n; i++) res -= c[i];
while (!q.empty()) res += q.top(), q.pop();
if (ok && res >= 0) ans = min(ans, res);
}
if (ans == inf) cout << "Impossible\n", exit(0);
cout << ans;
cout << '\n';
}
bool testcases = 0;
signed main() {
#ifdef AMIR
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
cin.tie(0) -> sync_with_stdio(0);
int test = 1;
if (testcases) cin >> test;
for (int cs = 1; cs <= test; cs++) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
1 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
5212 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6488 KB |
Output is correct |
8 |
Correct |
1 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
1 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
5212 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6488 KB |
Output is correct |
8 |
Correct |
1 ms |
5212 KB |
Output is correct |
9 |
Correct |
12 ms |
6492 KB |
Output is correct |
10 |
Correct |
15 ms |
6492 KB |
Output is correct |
11 |
Incorrect |
7 ms |
6492 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
5208 KB |
Output is correct |
2 |
Correct |
15 ms |
5212 KB |
Output is correct |
3 |
Correct |
20 ms |
5208 KB |
Output is correct |
4 |
Correct |
20 ms |
5208 KB |
Output is correct |
5 |
Correct |
19 ms |
5376 KB |
Output is correct |
6 |
Correct |
14 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
1 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
5212 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6488 KB |
Output is correct |
8 |
Correct |
1 ms |
5212 KB |
Output is correct |
9 |
Correct |
12 ms |
6492 KB |
Output is correct |
10 |
Correct |
15 ms |
6492 KB |
Output is correct |
11 |
Incorrect |
7 ms |
6492 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |