#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<pii> vpii;
typedef vector<ll> vi;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
vi calcShifts(ll a)
{
vi ans;
ll i = 0;
while ((1LL << i) <= a)
{
a -= (1LL << i);
ans.push_back(i++);
}
while (i > 0)
if ((1LL << --i) <= a)
ans.push_back(i), a -= (1 << i);
return ans;
}
vi dp;
ll center;
void calcDP(vi &A, ll M)
{
ll an = accumulate(A.begin(), A.begin() + M, 0);
ll ap = accumulate(A.begin() + M + 1, A.begin() + 2 * M + 1, 0);
ll MAXSIZE = 2 * M * max(an, ap) + 2;
center = MAXSIZE / 2;
dp.assign(MAXSIZE, -1);
dp[center] = A[M];
for (int foo = 0; foo < 2; ++foo)
{
reverse(all(A)), reverse(all(dp));
for (ll m = 1; m <= M; ++m)
{
ll maxNum = A[m + M];
vi shifts = calcShifts(maxNum);
for (int i : shifts)
{
ll amount = (1 << i);
ll d = amount * m;
for (int j = sz(dp) - 1; j >= d; --j)
dp[j] = max(dp[j], (dp[j - d] != -1) ? dp[j - d] + amount : -1);
}
}
}
}
pii calcPref(vi &A, int M, ll L, ll &pref, ll &tmpans) // find first Prefix position { i, idx } with pref >= L, or { sz(A)-1 = 2 * M, A[2 * M] }
{
int i = 0;
tmpans = 0, pref = 0;
for (; i < M; ++i)
tmpans += A[i], pref += A[i] * (i - M);
if (pref >= L)
return {i, 0};
while (i < sz(A) && pref + A[i] * (i - M) < L)
{
pref += A[i] * (i - M);
tmpans += A[i];
++i;
}
if (i == sz(A))
return {i - 1, A[i - 1]};
ll delta = L - pref;
assert(delta >= 0); // The expectation after the while loop is, that one is able to forfill this shit with the A[i] * (i - M) stuff
ll l = 0, r = A[i], m;
while (l < r)
{
m = (l + r) / 2;
ll v = (i - M) * m;
if (v < delta)
l = m + 1;
else
r = m;
}
pref += l * (i - M);
tmpans += l;
return {i, l};
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
ll M, L;
cin >> M >> L;
vi A(2 * M + 1);
for (int i = 0; i < sz(A); ++i)
cin >> A[i];
// cout << calcDP(A, L, M) << endl;
vi B(sz(A), 0);
ll dpMax = 0, dpMin = 0; // Obere Grenze und untere Grenze für das davon induzierte DP
for (int i = 0; i < sz(B); ++i)
{
if (i == M)
{
B[i] = A[i];
A[i] = 0;
continue;
}
B[i] = min(M - 1, A[i]);
A[i] -= B[i];
dpMax = max(dpMax, dpMax + B[i] * (i - M));
dpMin = min(dpMin, dpMin + B[i] * (i - M));
}
// Calc dp with normal stuff
calcDP(B, M);
ll li, lidx, pref, tmpans;
tie(li, lidx) = calcPref(A, M, L - dpMax, pref, tmpans);
ll ans = -1;
int i = li;
int idx = lidx;
while (i < sz(A) && L - pref + center >= 0)
{
if (dp[L - pref + center] != -1)
ans = max(ans, tmpans + dp[L - pref + center]);
if (idx < A[i])
{
++idx;
++tmpans;
pref += i - M;
}
else
{
++i;
idx = 0;
}
}
if (ans == -1)
cout << "impossible\n";
else
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
97 ms |
1364 KB |
Output is correct |
6 |
Correct |
102 ms |
1404 KB |
Output is correct |
7 |
Correct |
35 ms |
724 KB |
Output is correct |
8 |
Correct |
81 ms |
1236 KB |
Output is correct |
9 |
Correct |
222 ms |
2104 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
97 ms |
1364 KB |
Output is correct |
6 |
Correct |
102 ms |
1404 KB |
Output is correct |
7 |
Correct |
35 ms |
724 KB |
Output is correct |
8 |
Correct |
81 ms |
1236 KB |
Output is correct |
9 |
Correct |
222 ms |
2104 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
84 ms |
1236 KB |
Output is correct |
17 |
Correct |
97 ms |
1364 KB |
Output is correct |
18 |
Correct |
22 ms |
828 KB |
Output is correct |
19 |
Correct |
101 ms |
1236 KB |
Output is correct |
20 |
Correct |
206 ms |
2132 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Runtime error |
1955 ms |
17436 KB |
Execution killed with signal 11 |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
9 ms |
616 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
9 ms |
616 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
9 ms |
616 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
97 ms |
1364 KB |
Output is correct |
6 |
Correct |
102 ms |
1404 KB |
Output is correct |
7 |
Correct |
35 ms |
724 KB |
Output is correct |
8 |
Correct |
81 ms |
1236 KB |
Output is correct |
9 |
Correct |
222 ms |
2104 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Incorrect |
9 ms |
616 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
9 ms |
616 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
97 ms |
1364 KB |
Output is correct |
6 |
Correct |
102 ms |
1404 KB |
Output is correct |
7 |
Correct |
35 ms |
724 KB |
Output is correct |
8 |
Correct |
81 ms |
1236 KB |
Output is correct |
9 |
Correct |
222 ms |
2104 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
84 ms |
1236 KB |
Output is correct |
17 |
Correct |
97 ms |
1364 KB |
Output is correct |
18 |
Correct |
22 ms |
828 KB |
Output is correct |
19 |
Correct |
101 ms |
1236 KB |
Output is correct |
20 |
Correct |
206 ms |
2132 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Runtime error |
1955 ms |
17436 KB |
Execution killed with signal 11 |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
9 ms |
616 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
97 ms |
1364 KB |
Output is correct |
6 |
Correct |
102 ms |
1404 KB |
Output is correct |
7 |
Correct |
35 ms |
724 KB |
Output is correct |
8 |
Correct |
81 ms |
1236 KB |
Output is correct |
9 |
Correct |
222 ms |
2104 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
84 ms |
1236 KB |
Output is correct |
17 |
Correct |
97 ms |
1364 KB |
Output is correct |
18 |
Correct |
22 ms |
828 KB |
Output is correct |
19 |
Correct |
101 ms |
1236 KB |
Output is correct |
20 |
Correct |
206 ms |
2132 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Runtime error |
1955 ms |
17436 KB |
Execution killed with signal 11 |
24 |
Halted |
0 ms |
0 KB |
- |