#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
int M;
ll L, S, arr[1010], rem[1010], ans;
ll dp[660][101010];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> M >> L;
for (int i = 0; i <= 2*M; i++) cin >> arr[i];
for (int i = 0; i <= 2*M; i++) S += (i-M)*arr[i];
if (S < L) {
L = -L;
for (int i = 0; i <= M; i++) swap(arr[i], arr[2*M-i]);
}
S = 0;
for (int i = 0; i <= M; i++) {
S += (i-M)*arr[i];
ans += arr[i];
rem[2*M-i] = arr[i];
arr[i] = 0;
}
if (S > L) {
cout << "impossible\n";
return 0;
}
for (int i = M+1; i <= 2*M; i++) {
ll t = (L-S)/(i-M);
if (t >= arr[i]) {
ans += arr[i];
rem[2*M-i] = arr[i];
S += arr[i]*(i-M);
arr[i] = 0;
}
else {
ans += t;
rem[2*M-i] = t;
S += t*(i-M);
arr[i] -= t;
break;
}
}
for (int j = 0; j <= 2*M*M; j++) dp[0][j] = -1e18;
dp[0][M*M] = 0;
for (int i = 1; i <= M; i++) {
for (int j = 2*M*M; j >= 0; j--) {
dp[i][j] = -1e18;
for (int k = 0; k <= arr[M+i]; k++) {
if (j-i*k < 0) break;
dp[i][j] = max(dp[i][j], dp[i-1][j-i*k]+k);
}
for (int k = 0; k <= rem[M+i]; k++) {
if (j-i*k < 0) break;
dp[i][j] = max(dp[i][j], dp[i-1][j-i*k]-k);
}
}
//cout << i << " " << dp[i][L-S+M*M] << "\n";
}
for (int i = M+1; i <= 2*M; i++) {
for (int j = 0; j <= 2*M*M; j++) {
dp[i][j] = -1e18;
for (int k = 0; k <= arr[2*M-i]; k++) {
if (j+i*k < 0) break;
dp[i][j] = max(dp[i][j], dp[i-1][j+(i-M)*k]+k);
}
for (int k = 0; k <= rem[2*M-i]; k++) {
if (j+i*k < 0) break;
dp[i][j] = max(dp[i][j], dp[i-1][j+(i-M)*k]-k);
}
}
//cout << i << " " << dp[i][L-S+M*M] << "\n";
}
if (dp[2*M][L-S+M*M] <= -1e17) cout << "impossible\n";
else cout << ans + dp[2*M][L-S+M*M] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
2 ms |
16728 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
21 ms |
80532 KB |
Output is correct |
7 |
Correct |
14 ms |
80476 KB |
Output is correct |
8 |
Correct |
20 ms |
80476 KB |
Output is correct |
9 |
Correct |
30 ms |
80472 KB |
Output is correct |
10 |
Correct |
11 ms |
80476 KB |
Output is correct |
11 |
Correct |
10 ms |
80476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
2 ms |
16728 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
21 ms |
80532 KB |
Output is correct |
7 |
Correct |
14 ms |
80476 KB |
Output is correct |
8 |
Correct |
20 ms |
80476 KB |
Output is correct |
9 |
Correct |
30 ms |
80472 KB |
Output is correct |
10 |
Correct |
11 ms |
80476 KB |
Output is correct |
11 |
Correct |
10 ms |
80476 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
2648 KB |
Output is correct |
15 |
Correct |
2 ms |
16732 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
22 ms |
80496 KB |
Output is correct |
18 |
Correct |
14 ms |
80476 KB |
Output is correct |
19 |
Correct |
23 ms |
80336 KB |
Output is correct |
20 |
Correct |
34 ms |
80376 KB |
Output is correct |
21 |
Correct |
11 ms |
80476 KB |
Output is correct |
22 |
Correct |
10 ms |
80336 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
230 ms |
160416 KB |
Output is correct |
25 |
Correct |
87 ms |
160640 KB |
Output is correct |
26 |
Correct |
398 ms |
160544 KB |
Output is correct |
27 |
Correct |
386 ms |
160592 KB |
Output is correct |
28 |
Correct |
35 ms |
160592 KB |
Output is correct |
29 |
Correct |
28 ms |
160604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16728 KB |
Output is correct |
2 |
Runtime error |
57 ms |
55496 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16728 KB |
Output is correct |
2 |
Runtime error |
57 ms |
55496 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16728 KB |
Output is correct |
2 |
Runtime error |
57 ms |
55496 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
2 ms |
16728 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
21 ms |
80532 KB |
Output is correct |
7 |
Correct |
14 ms |
80476 KB |
Output is correct |
8 |
Correct |
20 ms |
80476 KB |
Output is correct |
9 |
Correct |
30 ms |
80472 KB |
Output is correct |
10 |
Correct |
11 ms |
80476 KB |
Output is correct |
11 |
Correct |
10 ms |
80476 KB |
Output is correct |
12 |
Correct |
2 ms |
16728 KB |
Output is correct |
13 |
Runtime error |
57 ms |
55496 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16728 KB |
Output is correct |
2 |
Runtime error |
57 ms |
55496 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
2 ms |
16728 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
21 ms |
80532 KB |
Output is correct |
7 |
Correct |
14 ms |
80476 KB |
Output is correct |
8 |
Correct |
20 ms |
80476 KB |
Output is correct |
9 |
Correct |
30 ms |
80472 KB |
Output is correct |
10 |
Correct |
11 ms |
80476 KB |
Output is correct |
11 |
Correct |
10 ms |
80476 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
2648 KB |
Output is correct |
15 |
Correct |
2 ms |
16732 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
22 ms |
80496 KB |
Output is correct |
18 |
Correct |
14 ms |
80476 KB |
Output is correct |
19 |
Correct |
23 ms |
80336 KB |
Output is correct |
20 |
Correct |
34 ms |
80376 KB |
Output is correct |
21 |
Correct |
11 ms |
80476 KB |
Output is correct |
22 |
Correct |
10 ms |
80336 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
230 ms |
160416 KB |
Output is correct |
25 |
Correct |
87 ms |
160640 KB |
Output is correct |
26 |
Correct |
398 ms |
160544 KB |
Output is correct |
27 |
Correct |
386 ms |
160592 KB |
Output is correct |
28 |
Correct |
35 ms |
160592 KB |
Output is correct |
29 |
Correct |
28 ms |
160604 KB |
Output is correct |
30 |
Correct |
2 ms |
16728 KB |
Output is correct |
31 |
Runtime error |
57 ms |
55496 KB |
Execution killed with signal 11 |
32 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
16728 KB |
Output is correct |
2 |
Runtime error |
57 ms |
55496 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
2 ms |
16728 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
21 ms |
80532 KB |
Output is correct |
7 |
Correct |
14 ms |
80476 KB |
Output is correct |
8 |
Correct |
20 ms |
80476 KB |
Output is correct |
9 |
Correct |
30 ms |
80472 KB |
Output is correct |
10 |
Correct |
11 ms |
80476 KB |
Output is correct |
11 |
Correct |
10 ms |
80476 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
2648 KB |
Output is correct |
15 |
Correct |
2 ms |
16732 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
22 ms |
80496 KB |
Output is correct |
18 |
Correct |
14 ms |
80476 KB |
Output is correct |
19 |
Correct |
23 ms |
80336 KB |
Output is correct |
20 |
Correct |
34 ms |
80376 KB |
Output is correct |
21 |
Correct |
11 ms |
80476 KB |
Output is correct |
22 |
Correct |
10 ms |
80336 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
230 ms |
160416 KB |
Output is correct |
25 |
Correct |
87 ms |
160640 KB |
Output is correct |
26 |
Correct |
398 ms |
160544 KB |
Output is correct |
27 |
Correct |
386 ms |
160592 KB |
Output is correct |
28 |
Correct |
35 ms |
160592 KB |
Output is correct |
29 |
Correct |
28 ms |
160604 KB |
Output is correct |
30 |
Correct |
2 ms |
16728 KB |
Output is correct |
31 |
Runtime error |
57 ms |
55496 KB |
Execution killed with signal 11 |
32 |
Halted |
0 ms |
0 KB |
- |