#include <bits/stdc++.h>
using namespace std;
#define dbg(x) //x
#define prt(x) dbg(cerr << x)
#define pv(x) dbg(cerr << #x << " = " << x << '\n')
#define pv2(x) dbg(cerr << #x << " = " << x.first << ',' << x.second << '\n')
#define parr(x) dbg(prt(#x << " = { "); for (auto y : x) prt(y << ' '); prt("}\n");)
#define parr2(x) dbg(prt(#x << " = { "); for (auto [y, z] : x) prt(y << ',' << z << " "); prt("}\n");)
#define parr2d(x) dbg(prt(#x << ":\n"); for (auto arr : x) {parr(arr);} prt('\n'));
#define parr2d2(x) dbg(prt(#x << ":\n"); for (auto arr : x) {parr2(arr);} prt('\n'));
/*
(n elems) * (n copies of each elem) * (o(n) value per element) = o(n^3) states
maybe we can do optimized knapsack with o(log(n)) bit shifts per value
so there are 1e5-1e6 states
and you have to do smth exactly (n)(2n+1)(logn) updates with o(n^3) states
for (2n + 1 numbers)
for (log(n) updates per number)
for (n^3 states)
that makes n^5 without the optimization, n^4logn with
*/
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int m; long long l;
cin >> m >> l;
vector<int> a(2 * m + 1);
int psum = 0, nsum = 0;
for (int i = 0; i < 2 * m + 1; i++) {
cin >> a[i];
if (i < m) nsum += a[i] * (i - m);
else if (i > m) psum += a[i] * (i - m);
}
pv(nsum); pv(psum);
if (l < nsum || l > psum) {
cout << "impossible\n";
} else {
vector<int> dp(psum - nsum + 1, -1);
dp[-nsum] = 0;
for (int i = 0; i <= m; i++) {
pv(i);
int cnt = a[i + m], mxbit = 0;
if (cnt > 0) {
mxbit = 31 - __builtin_clz(cnt);
pv(cnt);
pv(mxbit);
for (int bit = 0; bit < mxbit; bit++) {
pv(bit);
int shift = (i << bit);
pv(shift);
for (int j = psum; j >= nsum; j--) {
if (dp[j - nsum] == -1 || j + shift > psum) continue;
pv(j);
dp[j - nsum + shift] = max(dp[j - nsum + shift], dp[j - nsum] + (1 << bit));
}
}
cnt -= (1 << mxbit) - 1; pv(cnt);
for (int bit = 0; (1 << bit) <= cnt; bit++) {
if ((cnt & (1 << bit))) {
pv(bit);
int shift = (i << bit);
for (int j = psum; j >= nsum; j--) {
if (dp[j - nsum] == -1 || j + shift > psum) continue;
dp[j - nsum + shift] = max(dp[j - nsum + shift], dp[j - nsum] + (1 << bit));
}
}
}
}
if (i == 0) continue;
cnt = a[-i + m], mxbit = 0;
if (cnt > 0) {
mxbit = 31 - __builtin_clz(cnt);
pv(cnt); pv(mxbit);
for (int bit = 0; bit < mxbit; bit++) {
pv(bit);
int shift = -(i << bit);
for (int j = nsum; j <= psum; j++) {
if (dp[j - nsum] == -1 || j + shift < nsum) continue;
dp[j - nsum + shift] = max(dp[j - nsum + shift], dp[j - nsum] + (1 << bit));
}
}
cnt -= (1 << mxbit) - 1; pv(cnt);
for (int bit = 0; (1 << bit) <= cnt; bit++) {
if ((cnt & (1 << bit))) {
pv(bit);
int shift = -(i << bit);
for (int j = nsum; j <= psum; j++) {
if (dp[j - nsum] == -1 || j + shift < nsum) continue;
dp[j - nsum + shift] = max(dp[j - nsum + shift], dp[j - nsum] + (1 << bit));
}
}
}
}
}
parr(dp);
if (dp[l - nsum] == -1) {
cout << "impossible\n";
} else {
cout << dp[l - nsum] << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
38 ms |
604 KB |
Output is correct |
7 |
Correct |
8 ms |
348 KB |
Output is correct |
8 |
Correct |
38 ms |
600 KB |
Output is correct |
9 |
Correct |
84 ms |
856 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
38 ms |
604 KB |
Output is correct |
7 |
Correct |
8 ms |
348 KB |
Output is correct |
8 |
Correct |
38 ms |
600 KB |
Output is correct |
9 |
Correct |
84 ms |
856 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
17 |
Correct |
38 ms |
604 KB |
Output is correct |
18 |
Correct |
8 ms |
348 KB |
Output is correct |
19 |
Correct |
36 ms |
604 KB |
Output is correct |
20 |
Correct |
85 ms |
1016 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
671 ms |
2396 KB |
Output is correct |
25 |
Correct |
114 ms |
1116 KB |
Output is correct |
26 |
Correct |
1459 ms |
4188 KB |
Output is correct |
27 |
Correct |
1517 ms |
4436 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
38 ms |
604 KB |
Output is correct |
7 |
Correct |
8 ms |
348 KB |
Output is correct |
8 |
Correct |
38 ms |
600 KB |
Output is correct |
9 |
Correct |
84 ms |
856 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
38 ms |
604 KB |
Output is correct |
7 |
Correct |
8 ms |
348 KB |
Output is correct |
8 |
Correct |
38 ms |
600 KB |
Output is correct |
9 |
Correct |
84 ms |
856 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
17 |
Correct |
38 ms |
604 KB |
Output is correct |
18 |
Correct |
8 ms |
348 KB |
Output is correct |
19 |
Correct |
36 ms |
604 KB |
Output is correct |
20 |
Correct |
85 ms |
1016 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
671 ms |
2396 KB |
Output is correct |
25 |
Correct |
114 ms |
1116 KB |
Output is correct |
26 |
Correct |
1459 ms |
4188 KB |
Output is correct |
27 |
Correct |
1517 ms |
4436 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
38 ms |
604 KB |
Output is correct |
7 |
Correct |
8 ms |
348 KB |
Output is correct |
8 |
Correct |
38 ms |
600 KB |
Output is correct |
9 |
Correct |
84 ms |
856 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
17 |
Correct |
38 ms |
604 KB |
Output is correct |
18 |
Correct |
8 ms |
348 KB |
Output is correct |
19 |
Correct |
36 ms |
604 KB |
Output is correct |
20 |
Correct |
85 ms |
1016 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
671 ms |
2396 KB |
Output is correct |
25 |
Correct |
114 ms |
1116 KB |
Output is correct |
26 |
Correct |
1459 ms |
4188 KB |
Output is correct |
27 |
Correct |
1517 ms |
4436 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |