/*input
1 5
0 0 6
*/
#pragma GCC optimize ("O3")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
mt19937_64 rng(0);
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll M;
ll L;
cin >> M >> L;
ll A_[2 * M + 1];
ll* A = A_ + M;
for (ll i = -M; i <= M; i++)
cin >> A[i];
L = -L;
ll cnt = 0;
for (ll l = -M; l <= M; l++)
{
L += l * A[l];
cnt += A[l];
}
A[0] = 0;
ld L_ = L;
ld C_[2 * M + 1];
ld* C = C_ + M;
C[0] = 0;
ll sum[M + 2];
for (ll x = M; x >= 1; x--)
{
for (ll v : {x, -x})
{
C[v] = L_ / v;
C[v] = max(ld(0), C[v]);
C[v] = min(ld(A[v]), C[v]);
L_ -= C[v] * v;
}
sum[x] = (ll)roundl(L_);
}
sum[0] = 0;
sum[M + 1] = L;
const ll mxdel = 40000;
ll infi = 2e18;
ll ej[M + 2][2 * mxdel + 1];
for (ll a = 0; a <= M + 1; a++)
for (ll b = 0; b <= 2 * mxdel; b++)
ej[a][b] = infi;
ej[M + 1][0 + mxdel] = 0;
for (ll x = M; x >= 0; x--)
{
for (ll a = -mxdel; a <= mxdel; a++)
{
ll val = sum[x + 1] + a - sum[x];
ll val1 = sum[x + 1] + a - sum[x] + mxdel;
ll e = ej[x + 1][a + mxdel];
if (e == infi)
continue;
//val1 - k * v=0
// - k * v=val1
//val1 - k * v=mxdel*2
ll v1 = (x == 0) ? 0 : -(val1 / x);
ll v2 = (x == 0) ? 0 : ((mxdel * 2 - val1) / x);
for (ll v : {x, -x})
{
ll opt = 0;
if (x != 0)
opt = val / v;
opt = max(opt, 0ll);
opt = min(opt, A[v]);
ll lo = 0;
ll hi = A[v];
if (v > 0)
{
lo = max(lo, v1);
hi = min(hi, v2);
}
else
{
lo = max(lo, -v2);
hi = min(hi, -v1);
}
for (ll k = max(lo, opt - x - 5); k <= min(hi, opt + x + 5); k++)
{
ll val_ = val1 - k * v;
ll e_ = e + k;
if (0 <= val_ && val_ <= mxdel * 2)
{
if (e_ < ej[x][val_])
{
ej[x][val_] = e_;
}
}
}
}
}
}
ll best = ej[0][mxdel];
if (best != infi)
cout << cnt - best << "\n";
else
cout << "impossible\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
1 ms |
2132 KB |
Output is correct |
4 |
Correct |
4 ms |
7840 KB |
Output is correct |
5 |
Correct |
130 ms |
32860 KB |
Output is correct |
6 |
Correct |
172 ms |
32864 KB |
Output is correct |
7 |
Correct |
60 ms |
32860 KB |
Output is correct |
8 |
Correct |
142 ms |
32832 KB |
Output is correct |
9 |
Correct |
179 ms |
32836 KB |
Output is correct |
10 |
Correct |
22 ms |
32852 KB |
Output is correct |
11 |
Correct |
26 ms |
32852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
1 ms |
2132 KB |
Output is correct |
4 |
Correct |
4 ms |
7840 KB |
Output is correct |
5 |
Correct |
130 ms |
32860 KB |
Output is correct |
6 |
Correct |
172 ms |
32864 KB |
Output is correct |
7 |
Correct |
60 ms |
32860 KB |
Output is correct |
8 |
Correct |
142 ms |
32832 KB |
Output is correct |
9 |
Correct |
179 ms |
32836 KB |
Output is correct |
10 |
Correct |
22 ms |
32852 KB |
Output is correct |
11 |
Correct |
26 ms |
32852 KB |
Output is correct |
12 |
Correct |
3 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
1 ms |
2132 KB |
Output is correct |
15 |
Correct |
5 ms |
7764 KB |
Output is correct |
16 |
Correct |
130 ms |
32860 KB |
Output is correct |
17 |
Correct |
160 ms |
32860 KB |
Output is correct |
18 |
Correct |
71 ms |
32820 KB |
Output is correct |
19 |
Correct |
147 ms |
32876 KB |
Output is correct |
20 |
Correct |
184 ms |
32852 KB |
Output is correct |
21 |
Correct |
22 ms |
32860 KB |
Output is correct |
22 |
Correct |
25 ms |
32820 KB |
Output is correct |
23 |
Correct |
441 ms |
64176 KB |
Output is correct |
24 |
Correct |
856 ms |
64176 KB |
Output is correct |
25 |
Correct |
264 ms |
64176 KB |
Output is correct |
26 |
Correct |
922 ms |
64176 KB |
Output is correct |
27 |
Correct |
633 ms |
64180 KB |
Output is correct |
28 |
Correct |
70 ms |
64176 KB |
Output is correct |
29 |
Correct |
68 ms |
64176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7764 KB |
Output is correct |
2 |
Correct |
23 ms |
20352 KB |
Output is correct |
3 |
Correct |
15 ms |
20352 KB |
Output is correct |
4 |
Incorrect |
13 ms |
20316 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7764 KB |
Output is correct |
2 |
Correct |
23 ms |
20352 KB |
Output is correct |
3 |
Correct |
15 ms |
20352 KB |
Output is correct |
4 |
Incorrect |
13 ms |
20316 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7764 KB |
Output is correct |
2 |
Correct |
23 ms |
20352 KB |
Output is correct |
3 |
Correct |
15 ms |
20352 KB |
Output is correct |
4 |
Incorrect |
13 ms |
20316 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
1 ms |
2132 KB |
Output is correct |
4 |
Correct |
4 ms |
7840 KB |
Output is correct |
5 |
Correct |
130 ms |
32860 KB |
Output is correct |
6 |
Correct |
172 ms |
32864 KB |
Output is correct |
7 |
Correct |
60 ms |
32860 KB |
Output is correct |
8 |
Correct |
142 ms |
32832 KB |
Output is correct |
9 |
Correct |
179 ms |
32836 KB |
Output is correct |
10 |
Correct |
22 ms |
32852 KB |
Output is correct |
11 |
Correct |
26 ms |
32852 KB |
Output is correct |
12 |
Correct |
5 ms |
7764 KB |
Output is correct |
13 |
Correct |
23 ms |
20352 KB |
Output is correct |
14 |
Correct |
15 ms |
20352 KB |
Output is correct |
15 |
Incorrect |
13 ms |
20316 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7764 KB |
Output is correct |
2 |
Correct |
23 ms |
20352 KB |
Output is correct |
3 |
Correct |
15 ms |
20352 KB |
Output is correct |
4 |
Incorrect |
13 ms |
20316 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
1 ms |
2132 KB |
Output is correct |
4 |
Correct |
4 ms |
7840 KB |
Output is correct |
5 |
Correct |
130 ms |
32860 KB |
Output is correct |
6 |
Correct |
172 ms |
32864 KB |
Output is correct |
7 |
Correct |
60 ms |
32860 KB |
Output is correct |
8 |
Correct |
142 ms |
32832 KB |
Output is correct |
9 |
Correct |
179 ms |
32836 KB |
Output is correct |
10 |
Correct |
22 ms |
32852 KB |
Output is correct |
11 |
Correct |
26 ms |
32852 KB |
Output is correct |
12 |
Correct |
3 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
1 ms |
2132 KB |
Output is correct |
15 |
Correct |
5 ms |
7764 KB |
Output is correct |
16 |
Correct |
130 ms |
32860 KB |
Output is correct |
17 |
Correct |
160 ms |
32860 KB |
Output is correct |
18 |
Correct |
71 ms |
32820 KB |
Output is correct |
19 |
Correct |
147 ms |
32876 KB |
Output is correct |
20 |
Correct |
184 ms |
32852 KB |
Output is correct |
21 |
Correct |
22 ms |
32860 KB |
Output is correct |
22 |
Correct |
25 ms |
32820 KB |
Output is correct |
23 |
Correct |
441 ms |
64176 KB |
Output is correct |
24 |
Correct |
856 ms |
64176 KB |
Output is correct |
25 |
Correct |
264 ms |
64176 KB |
Output is correct |
26 |
Correct |
922 ms |
64176 KB |
Output is correct |
27 |
Correct |
633 ms |
64180 KB |
Output is correct |
28 |
Correct |
70 ms |
64176 KB |
Output is correct |
29 |
Correct |
68 ms |
64176 KB |
Output is correct |
30 |
Correct |
5 ms |
7764 KB |
Output is correct |
31 |
Correct |
23 ms |
20352 KB |
Output is correct |
32 |
Correct |
15 ms |
20352 KB |
Output is correct |
33 |
Incorrect |
13 ms |
20316 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7764 KB |
Output is correct |
2 |
Correct |
23 ms |
20352 KB |
Output is correct |
3 |
Correct |
15 ms |
20352 KB |
Output is correct |
4 |
Incorrect |
13 ms |
20316 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
1 ms |
2132 KB |
Output is correct |
4 |
Correct |
4 ms |
7840 KB |
Output is correct |
5 |
Correct |
130 ms |
32860 KB |
Output is correct |
6 |
Correct |
172 ms |
32864 KB |
Output is correct |
7 |
Correct |
60 ms |
32860 KB |
Output is correct |
8 |
Correct |
142 ms |
32832 KB |
Output is correct |
9 |
Correct |
179 ms |
32836 KB |
Output is correct |
10 |
Correct |
22 ms |
32852 KB |
Output is correct |
11 |
Correct |
26 ms |
32852 KB |
Output is correct |
12 |
Correct |
3 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
1 ms |
2132 KB |
Output is correct |
15 |
Correct |
5 ms |
7764 KB |
Output is correct |
16 |
Correct |
130 ms |
32860 KB |
Output is correct |
17 |
Correct |
160 ms |
32860 KB |
Output is correct |
18 |
Correct |
71 ms |
32820 KB |
Output is correct |
19 |
Correct |
147 ms |
32876 KB |
Output is correct |
20 |
Correct |
184 ms |
32852 KB |
Output is correct |
21 |
Correct |
22 ms |
32860 KB |
Output is correct |
22 |
Correct |
25 ms |
32820 KB |
Output is correct |
23 |
Correct |
441 ms |
64176 KB |
Output is correct |
24 |
Correct |
856 ms |
64176 KB |
Output is correct |
25 |
Correct |
264 ms |
64176 KB |
Output is correct |
26 |
Correct |
922 ms |
64176 KB |
Output is correct |
27 |
Correct |
633 ms |
64180 KB |
Output is correct |
28 |
Correct |
70 ms |
64176 KB |
Output is correct |
29 |
Correct |
68 ms |
64176 KB |
Output is correct |
30 |
Correct |
5 ms |
7764 KB |
Output is correct |
31 |
Correct |
23 ms |
20352 KB |
Output is correct |
32 |
Correct |
15 ms |
20352 KB |
Output is correct |
33 |
Incorrect |
13 ms |
20316 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |