//Challenge: Accepted
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 1000005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const int inf = 1<<30;
int dp[maxn];
int main() {
io
int m;
ll L;
cin >> m >> L;
vector<ll> a(2*m+1), ex(2*m+1), ex2(2*m+1);
ll mins = -maxn / 2, ans = 0;
ll sum = 0;
for (int i = 0;i < 2*m+1;i++) {
cin >> a[i];
if (i == m) ans += a[i], a[i] = 0;
sum += a[i] * (i - m);
}
if (sum < L) {
for (int i = 0;i < m;i++) swap(a[i], a[2*m - i]);
L = -L;
}
for (int i = 0;i < m;i++) {
L -= a[i] * (i - m);
ans += a[i];
ex2[2 * m - i] = min(a[i],(ll)2*m);
a[i] = 0;
}
for (int i = m+1;i < 2*m+1;i++) {
ll p = min(a[i], L / (i-m));
ans += p;
L -= p * (i-m);
a[i] -= p;
ex[i] = min(a[i], (ll)2*m);
ex[2 * m - i] = min(p,(ll)2*m);
}
for (int i = 0;i < maxn;i++) dp[i] = -inf;
assert(0 <= L && L <= m);
dp[-mins] = 0;
for (int i = 0;i < 2*m+1;i++) {
ll cnt = 1;
vector<int> num;
while (ex[i] >= cnt) {
num.push_back(cnt);
ex[i] -= cnt;
cnt <<= 1;
}
if (ex[i] > 0) num.push_back(ex[i]);
for (int p:num) {
if (i - m > 0) {
for (int j = maxn - 1;j >= 0;j--) {
int val = j + p * (i-m);
if (val >= maxn) continue;
int del = i < m ? -p : p;
dp[val] = max(dp[val], dp[j] + del);
}
} else {
for (int j = 0;j < maxn;j++) {
int val = j + p * (i-m);
if (val < 0) continue;
int del = i < m ? -p : p;
dp[val] = max(dp[val], dp[j] + del);
}
}
}
}
ex = ex2;
for (int i = m+1;i < 2*m+1;i++) {
ll cnt = 1;
vector<int> num;
while (ex[i] >= cnt) {
num.push_back(cnt);
ex[i] -= cnt;
cnt <<= 1;
}
if (ex[i] > 0) num.push_back(ex[i]);
for (int p:num) {
for (int j = maxn - 1;j >= 0;j--) {
int val = j + p * (i-m);
if (val >= maxn) continue;
dp[val] = max(dp[val], dp[j] - p);
}
}
}
if (L - mins >= maxn || L - mins < 0 || dp[L - mins] < -maxn) cout << "impossible\n";
else cout << dp[L - mins]+ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
4180 KB |
Output is correct |
2 |
Correct |
7 ms |
4180 KB |
Output is correct |
3 |
Correct |
5 ms |
4180 KB |
Output is correct |
4 |
Correct |
35 ms |
4180 KB |
Output is correct |
5 |
Incorrect |
433 ms |
4216 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
4180 KB |
Output is correct |
2 |
Correct |
7 ms |
4180 KB |
Output is correct |
3 |
Correct |
5 ms |
4180 KB |
Output is correct |
4 |
Correct |
35 ms |
4180 KB |
Output is correct |
5 |
Incorrect |
433 ms |
4216 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
4180 KB |
Output is correct |
2 |
Correct |
143 ms |
4228 KB |
Output is correct |
3 |
Correct |
51 ms |
4180 KB |
Output is correct |
4 |
Correct |
149 ms |
4180 KB |
Output is correct |
5 |
Correct |
150 ms |
4180 KB |
Output is correct |
6 |
Correct |
129 ms |
4208 KB |
Output is correct |
7 |
Correct |
18 ms |
4180 KB |
Output is correct |
8 |
Correct |
19 ms |
4308 KB |
Output is correct |
9 |
Correct |
71 ms |
4180 KB |
Output is correct |
10 |
Correct |
98 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
4180 KB |
Output is correct |
2 |
Correct |
143 ms |
4228 KB |
Output is correct |
3 |
Correct |
51 ms |
4180 KB |
Output is correct |
4 |
Correct |
149 ms |
4180 KB |
Output is correct |
5 |
Correct |
150 ms |
4180 KB |
Output is correct |
6 |
Correct |
129 ms |
4208 KB |
Output is correct |
7 |
Correct |
18 ms |
4180 KB |
Output is correct |
8 |
Correct |
19 ms |
4308 KB |
Output is correct |
9 |
Correct |
71 ms |
4180 KB |
Output is correct |
10 |
Correct |
98 ms |
4180 KB |
Output is correct |
11 |
Correct |
10 ms |
4180 KB |
Output is correct |
12 |
Correct |
6 ms |
4180 KB |
Output is correct |
13 |
Correct |
5 ms |
4228 KB |
Output is correct |
14 |
Correct |
28 ms |
4180 KB |
Output is correct |
15 |
Correct |
137 ms |
4212 KB |
Output is correct |
16 |
Correct |
47 ms |
4208 KB |
Output is correct |
17 |
Correct |
150 ms |
4212 KB |
Output is correct |
18 |
Correct |
151 ms |
4208 KB |
Output is correct |
19 |
Correct |
129 ms |
4180 KB |
Output is correct |
20 |
Correct |
17 ms |
4232 KB |
Output is correct |
21 |
Correct |
23 ms |
4260 KB |
Output is correct |
22 |
Correct |
70 ms |
4212 KB |
Output is correct |
23 |
Correct |
102 ms |
4180 KB |
Output is correct |
24 |
Correct |
261 ms |
4180 KB |
Output is correct |
25 |
Correct |
114 ms |
4300 KB |
Output is correct |
26 |
Correct |
292 ms |
4212 KB |
Output is correct |
27 |
Correct |
258 ms |
4180 KB |
Output is correct |
28 |
Correct |
273 ms |
4208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
4180 KB |
Output is correct |
2 |
Correct |
143 ms |
4228 KB |
Output is correct |
3 |
Correct |
51 ms |
4180 KB |
Output is correct |
4 |
Correct |
149 ms |
4180 KB |
Output is correct |
5 |
Correct |
150 ms |
4180 KB |
Output is correct |
6 |
Correct |
129 ms |
4208 KB |
Output is correct |
7 |
Correct |
18 ms |
4180 KB |
Output is correct |
8 |
Correct |
19 ms |
4308 KB |
Output is correct |
9 |
Correct |
71 ms |
4180 KB |
Output is correct |
10 |
Correct |
98 ms |
4180 KB |
Output is correct |
11 |
Correct |
28 ms |
4180 KB |
Output is correct |
12 |
Correct |
138 ms |
4180 KB |
Output is correct |
13 |
Correct |
42 ms |
4180 KB |
Output is correct |
14 |
Correct |
145 ms |
4212 KB |
Output is correct |
15 |
Correct |
159 ms |
4216 KB |
Output is correct |
16 |
Correct |
126 ms |
4180 KB |
Output is correct |
17 |
Correct |
17 ms |
4180 KB |
Output is correct |
18 |
Correct |
19 ms |
4180 KB |
Output is correct |
19 |
Correct |
70 ms |
4212 KB |
Output is correct |
20 |
Correct |
97 ms |
4180 KB |
Output is correct |
21 |
Correct |
15 ms |
4240 KB |
Output is correct |
22 |
Correct |
24 ms |
4236 KB |
Output is correct |
23 |
Correct |
253 ms |
4180 KB |
Output is correct |
24 |
Correct |
118 ms |
4216 KB |
Output is correct |
25 |
Correct |
250 ms |
4212 KB |
Output is correct |
26 |
Correct |
295 ms |
4180 KB |
Output is correct |
27 |
Correct |
262 ms |
4216 KB |
Output is correct |
28 |
Correct |
213 ms |
4224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
4180 KB |
Output is correct |
2 |
Correct |
7 ms |
4180 KB |
Output is correct |
3 |
Correct |
5 ms |
4180 KB |
Output is correct |
4 |
Correct |
35 ms |
4180 KB |
Output is correct |
5 |
Incorrect |
433 ms |
4216 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
4180 KB |
Output is correct |
2 |
Correct |
143 ms |
4228 KB |
Output is correct |
3 |
Correct |
51 ms |
4180 KB |
Output is correct |
4 |
Correct |
149 ms |
4180 KB |
Output is correct |
5 |
Correct |
150 ms |
4180 KB |
Output is correct |
6 |
Correct |
129 ms |
4208 KB |
Output is correct |
7 |
Correct |
18 ms |
4180 KB |
Output is correct |
8 |
Correct |
19 ms |
4308 KB |
Output is correct |
9 |
Correct |
71 ms |
4180 KB |
Output is correct |
10 |
Correct |
98 ms |
4180 KB |
Output is correct |
11 |
Correct |
28 ms |
4180 KB |
Output is correct |
12 |
Correct |
138 ms |
4180 KB |
Output is correct |
13 |
Correct |
42 ms |
4180 KB |
Output is correct |
14 |
Correct |
145 ms |
4212 KB |
Output is correct |
15 |
Correct |
159 ms |
4216 KB |
Output is correct |
16 |
Correct |
126 ms |
4180 KB |
Output is correct |
17 |
Correct |
17 ms |
4180 KB |
Output is correct |
18 |
Correct |
19 ms |
4180 KB |
Output is correct |
19 |
Correct |
70 ms |
4212 KB |
Output is correct |
20 |
Correct |
97 ms |
4180 KB |
Output is correct |
21 |
Correct |
15 ms |
4240 KB |
Output is correct |
22 |
Correct |
24 ms |
4236 KB |
Output is correct |
23 |
Correct |
253 ms |
4180 KB |
Output is correct |
24 |
Correct |
118 ms |
4216 KB |
Output is correct |
25 |
Correct |
250 ms |
4212 KB |
Output is correct |
26 |
Correct |
295 ms |
4180 KB |
Output is correct |
27 |
Correct |
262 ms |
4216 KB |
Output is correct |
28 |
Correct |
213 ms |
4224 KB |
Output is correct |
29 |
Correct |
27 ms |
4228 KB |
Output is correct |
30 |
Correct |
138 ms |
4216 KB |
Output is correct |
31 |
Correct |
41 ms |
4180 KB |
Output is correct |
32 |
Correct |
146 ms |
4220 KB |
Output is correct |
33 |
Correct |
167 ms |
4208 KB |
Output is correct |
34 |
Correct |
127 ms |
4208 KB |
Output is correct |
35 |
Correct |
17 ms |
4236 KB |
Output is correct |
36 |
Correct |
19 ms |
4180 KB |
Output is correct |
37 |
Correct |
73 ms |
4216 KB |
Output is correct |
38 |
Correct |
101 ms |
4180 KB |
Output is correct |
39 |
Correct |
15 ms |
4180 KB |
Output is correct |
40 |
Correct |
23 ms |
4180 KB |
Output is correct |
41 |
Correct |
260 ms |
4212 KB |
Output is correct |
42 |
Correct |
121 ms |
4212 KB |
Output is correct |
43 |
Correct |
248 ms |
4180 KB |
Output is correct |
44 |
Correct |
302 ms |
4300 KB |
Output is correct |
45 |
Correct |
267 ms |
4216 KB |
Output is correct |
46 |
Correct |
213 ms |
4216 KB |
Output is correct |
47 |
Correct |
17 ms |
4308 KB |
Output is correct |
48 |
Correct |
25 ms |
4228 KB |
Output is correct |
49 |
Correct |
573 ms |
4212 KB |
Output is correct |
50 |
Correct |
262 ms |
4300 KB |
Output is correct |
51 |
Correct |
603 ms |
4180 KB |
Output is correct |
52 |
Correct |
723 ms |
4180 KB |
Output is correct |
53 |
Correct |
585 ms |
4224 KB |
Output is correct |
54 |
Correct |
486 ms |
4216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
4180 KB |
Output is correct |
2 |
Correct |
7 ms |
4180 KB |
Output is correct |
3 |
Correct |
5 ms |
4180 KB |
Output is correct |
4 |
Correct |
35 ms |
4180 KB |
Output is correct |
5 |
Incorrect |
433 ms |
4216 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
4180 KB |
Output is correct |
2 |
Correct |
143 ms |
4228 KB |
Output is correct |
3 |
Correct |
51 ms |
4180 KB |
Output is correct |
4 |
Correct |
149 ms |
4180 KB |
Output is correct |
5 |
Correct |
150 ms |
4180 KB |
Output is correct |
6 |
Correct |
129 ms |
4208 KB |
Output is correct |
7 |
Correct |
18 ms |
4180 KB |
Output is correct |
8 |
Correct |
19 ms |
4308 KB |
Output is correct |
9 |
Correct |
71 ms |
4180 KB |
Output is correct |
10 |
Correct |
98 ms |
4180 KB |
Output is correct |
11 |
Correct |
28 ms |
4180 KB |
Output is correct |
12 |
Correct |
138 ms |
4180 KB |
Output is correct |
13 |
Correct |
42 ms |
4180 KB |
Output is correct |
14 |
Correct |
145 ms |
4212 KB |
Output is correct |
15 |
Correct |
159 ms |
4216 KB |
Output is correct |
16 |
Correct |
126 ms |
4180 KB |
Output is correct |
17 |
Correct |
17 ms |
4180 KB |
Output is correct |
18 |
Correct |
19 ms |
4180 KB |
Output is correct |
19 |
Correct |
70 ms |
4212 KB |
Output is correct |
20 |
Correct |
97 ms |
4180 KB |
Output is correct |
21 |
Correct |
15 ms |
4240 KB |
Output is correct |
22 |
Correct |
24 ms |
4236 KB |
Output is correct |
23 |
Correct |
253 ms |
4180 KB |
Output is correct |
24 |
Correct |
118 ms |
4216 KB |
Output is correct |
25 |
Correct |
250 ms |
4212 KB |
Output is correct |
26 |
Correct |
295 ms |
4180 KB |
Output is correct |
27 |
Correct |
262 ms |
4216 KB |
Output is correct |
28 |
Correct |
213 ms |
4224 KB |
Output is correct |
29 |
Correct |
27 ms |
4228 KB |
Output is correct |
30 |
Correct |
138 ms |
4216 KB |
Output is correct |
31 |
Correct |
41 ms |
4180 KB |
Output is correct |
32 |
Correct |
146 ms |
4220 KB |
Output is correct |
33 |
Correct |
167 ms |
4208 KB |
Output is correct |
34 |
Correct |
127 ms |
4208 KB |
Output is correct |
35 |
Correct |
17 ms |
4236 KB |
Output is correct |
36 |
Correct |
19 ms |
4180 KB |
Output is correct |
37 |
Correct |
73 ms |
4216 KB |
Output is correct |
38 |
Correct |
101 ms |
4180 KB |
Output is correct |
39 |
Correct |
15 ms |
4180 KB |
Output is correct |
40 |
Correct |
23 ms |
4180 KB |
Output is correct |
41 |
Correct |
260 ms |
4212 KB |
Output is correct |
42 |
Correct |
121 ms |
4212 KB |
Output is correct |
43 |
Correct |
248 ms |
4180 KB |
Output is correct |
44 |
Correct |
302 ms |
4300 KB |
Output is correct |
45 |
Correct |
267 ms |
4216 KB |
Output is correct |
46 |
Correct |
213 ms |
4216 KB |
Output is correct |
47 |
Correct |
17 ms |
4308 KB |
Output is correct |
48 |
Correct |
25 ms |
4228 KB |
Output is correct |
49 |
Correct |
573 ms |
4212 KB |
Output is correct |
50 |
Correct |
262 ms |
4300 KB |
Output is correct |
51 |
Correct |
603 ms |
4180 KB |
Output is correct |
52 |
Correct |
723 ms |
4180 KB |
Output is correct |
53 |
Correct |
585 ms |
4224 KB |
Output is correct |
54 |
Correct |
486 ms |
4216 KB |
Output is correct |
55 |
Correct |
27 ms |
4180 KB |
Output is correct |
56 |
Correct |
135 ms |
4180 KB |
Output is correct |
57 |
Correct |
41 ms |
4208 KB |
Output is correct |
58 |
Correct |
145 ms |
4208 KB |
Output is correct |
59 |
Correct |
163 ms |
4212 KB |
Output is correct |
60 |
Correct |
128 ms |
4216 KB |
Output is correct |
61 |
Correct |
17 ms |
4180 KB |
Output is correct |
62 |
Correct |
21 ms |
4180 KB |
Output is correct |
63 |
Correct |
74 ms |
4212 KB |
Output is correct |
64 |
Correct |
101 ms |
4208 KB |
Output is correct |
65 |
Correct |
14 ms |
4232 KB |
Output is correct |
66 |
Correct |
21 ms |
4232 KB |
Output is correct |
67 |
Correct |
254 ms |
4216 KB |
Output is correct |
68 |
Correct |
116 ms |
4180 KB |
Output is correct |
69 |
Correct |
252 ms |
4232 KB |
Output is correct |
70 |
Correct |
303 ms |
4180 KB |
Output is correct |
71 |
Correct |
263 ms |
4216 KB |
Output is correct |
72 |
Correct |
216 ms |
4300 KB |
Output is correct |
73 |
Correct |
17 ms |
4180 KB |
Output is correct |
74 |
Correct |
23 ms |
4180 KB |
Output is correct |
75 |
Correct |
559 ms |
4180 KB |
Output is correct |
76 |
Correct |
256 ms |
4180 KB |
Output is correct |
77 |
Correct |
619 ms |
4216 KB |
Output is correct |
78 |
Correct |
675 ms |
4260 KB |
Output is correct |
79 |
Correct |
584 ms |
4224 KB |
Output is correct |
80 |
Correct |
512 ms |
4216 KB |
Output is correct |
81 |
Correct |
2242 ms |
4232 KB |
Output is correct |
82 |
Correct |
967 ms |
4232 KB |
Output is correct |
83 |
Correct |
2203 ms |
4228 KB |
Output is correct |
84 |
Correct |
2513 ms |
4232 KB |
Output is correct |
85 |
Correct |
30 ms |
4236 KB |
Output is correct |
86 |
Correct |
29 ms |
4248 KB |
Output is correct |
87 |
Correct |
1947 ms |
4232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
4180 KB |
Output is correct |
2 |
Correct |
7 ms |
4180 KB |
Output is correct |
3 |
Correct |
5 ms |
4180 KB |
Output is correct |
4 |
Correct |
35 ms |
4180 KB |
Output is correct |
5 |
Incorrect |
433 ms |
4216 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |