#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <set>
#include <map>
using namespace std;
typedef long long int ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rev_rep(i, a, b) for (ll i = a; i >= b; i--)
#define sz(a) (ll) a.size()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define trav(itr, a, t) for (t::iterator itr = a.begin(); itr != a.end(); itr++)
const ll MAXM = 300LL;
const ll MAXB = 2 * MAXM * (MAXM + 1LL);
const ll INF = 1'000'000'000'000'000'000LL;
ll M, L;
vll A;
vll in_greedy;
vll reduce(ll x, ll multiplier = 1) {
vll res;
while (x) {
//cout << "reduce " << x << " " << multiplier << endl;
res.pb(multiplier);
x--;
if (x & 1) {
res.pb(multiplier);
x--;
}
x >>= 1;
multiplier *= 2;
}
return res;
}
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> M >> L;
A.resize(2 * M + 1);
rep(i, 0, 2 * M + 1) cin >> A[i];
ll s = 0LL;
rep(i, -M, M + 1) {
s += A[i + M] * i;
}
if (s < L) { // reverse the problem
//cout << "reverse " << s << endl;
rep(i, 1, M + 1) swap(A[M - i], A[M + i]);
L *= -1;
}
// greedy
in_greedy.resize(M + 1, 0);
s = 0LL;
ll n = 0LL;
rep(i, -M, 1) {
s += A[i + M] * i;
n += A[i + M];
}
//cout << L << " " << s << " " << n << endl;
rep(i, 1, M + 1) {
in_greedy[i] = min<ll>((L - s) / i, A[i + M]);
//cout << i << " -> " << in_greedy[i] << endl;
s += in_greedy[i] * i;
n += in_greedy[i];
}
//cout << "n=" << n << endl;
// dp
ll dp[2 * MAXB + 1];
rep(i, 0, 2 * MAXB + 1) dp[i] = -INF;
dp[MAXB + (L - s)] = n; // base case
/* rep(i, -10, 11) {
cout << dp[i + MAXB] << " ";
}
cout << endl; */
rep(w, 1, M + 1) {
// add w
vll deltas = reduce(min<ll>(2 * M, A[w + M] - in_greedy[w]));
/* cout << min<ll>(2 * M, A[w + M] - in_greedy[w]) << " -> {";
trav(delta, deltas, vll) cout << (*delta) << " ";
cout << "}" << endl; */
trav(delta, deltas, vll) {
rep(i, - MAXB, MAXB + 1 - (*delta) * w) {
dp[i + MAXB] = max<ll>(dp[i + MAXB], dp[i + MAXB + (*delta) * w] + (*delta));
}
}
// remove w
deltas = reduce(min<ll>(2 * M, in_greedy[w]), -1);
/* cout << min<ll>(2 * M, in_greedy[w]) << " -> {";
trav(delta, deltas, vll) cout << (*delta) << " ";
cout << "}" << endl; */
trav(delta, deltas, vll) {
rev_rep(i, MAXB, - MAXB - (*delta) * w) {
//cout << (*delta) << " " << i + MAXB - (*delta) << " " << 2 * MAXB << endl;
dp[i + MAXB] = max<ll>(dp[i + MAXB], dp[i + MAXB + (*delta) * w] + (*delta));
}
}
/* rep(i, -10, 11) {
cout << dp[i + MAXB] << " ";
}
cout << endl; */
}
if (dp[MAXB] < 0) {
cout << "impossible" << endl;
} else {
cout << dp[MAXB] << endl;
}
return 0;
}
// the dp covers the range of uplifts [L - M - 2M^2, L + M + 2M^2]
// in each step, we consider adding up to min<ll>(2 * M, A[w + M] - in_greedy[w]) of weight w, or removng up to min<ll>(2 * M, in_greedy[w])
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3164 KB |
Output is correct |
2 |
Correct |
3 ms |
3164 KB |
Output is correct |
3 |
Correct |
3 ms |
3160 KB |
Output is correct |
4 |
Correct |
10 ms |
3164 KB |
Output is correct |
5 |
Runtime error |
336 ms |
524288 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3164 KB |
Output is correct |
2 |
Correct |
3 ms |
3164 KB |
Output is correct |
3 |
Correct |
3 ms |
3160 KB |
Output is correct |
4 |
Correct |
10 ms |
3164 KB |
Output is correct |
5 |
Runtime error |
336 ms |
524288 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
3164 KB |
Output is correct |
2 |
Correct |
53 ms |
3164 KB |
Output is correct |
3 |
Correct |
18 ms |
3164 KB |
Output is correct |
4 |
Correct |
59 ms |
3252 KB |
Output is correct |
5 |
Correct |
64 ms |
3160 KB |
Output is correct |
6 |
Correct |
52 ms |
3160 KB |
Output is correct |
7 |
Correct |
9 ms |
3164 KB |
Output is correct |
8 |
Correct |
8 ms |
3272 KB |
Output is correct |
9 |
Correct |
27 ms |
3164 KB |
Output is correct |
10 |
Correct |
49 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
3164 KB |
Output is correct |
2 |
Correct |
53 ms |
3164 KB |
Output is correct |
3 |
Correct |
18 ms |
3164 KB |
Output is correct |
4 |
Correct |
59 ms |
3252 KB |
Output is correct |
5 |
Correct |
64 ms |
3160 KB |
Output is correct |
6 |
Correct |
52 ms |
3160 KB |
Output is correct |
7 |
Correct |
9 ms |
3164 KB |
Output is correct |
8 |
Correct |
8 ms |
3272 KB |
Output is correct |
9 |
Correct |
27 ms |
3164 KB |
Output is correct |
10 |
Correct |
49 ms |
3164 KB |
Output is correct |
11 |
Correct |
4 ms |
3164 KB |
Output is correct |
12 |
Correct |
3 ms |
3060 KB |
Output is correct |
13 |
Correct |
3 ms |
3268 KB |
Output is correct |
14 |
Correct |
10 ms |
3276 KB |
Output is correct |
15 |
Correct |
54 ms |
3160 KB |
Output is correct |
16 |
Correct |
18 ms |
3160 KB |
Output is correct |
17 |
Correct |
60 ms |
3252 KB |
Output is correct |
18 |
Correct |
64 ms |
3256 KB |
Output is correct |
19 |
Correct |
53 ms |
3164 KB |
Output is correct |
20 |
Correct |
8 ms |
3164 KB |
Output is correct |
21 |
Correct |
9 ms |
3164 KB |
Output is correct |
22 |
Correct |
26 ms |
3164 KB |
Output is correct |
23 |
Correct |
39 ms |
3160 KB |
Output is correct |
24 |
Correct |
55 ms |
3160 KB |
Output is correct |
25 |
Correct |
21 ms |
3160 KB |
Output is correct |
26 |
Correct |
65 ms |
3164 KB |
Output is correct |
27 |
Correct |
51 ms |
3160 KB |
Output is correct |
28 |
Correct |
52 ms |
3256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
3164 KB |
Output is correct |
2 |
Correct |
53 ms |
3164 KB |
Output is correct |
3 |
Correct |
18 ms |
3164 KB |
Output is correct |
4 |
Correct |
59 ms |
3252 KB |
Output is correct |
5 |
Correct |
64 ms |
3160 KB |
Output is correct |
6 |
Correct |
52 ms |
3160 KB |
Output is correct |
7 |
Correct |
9 ms |
3164 KB |
Output is correct |
8 |
Correct |
8 ms |
3272 KB |
Output is correct |
9 |
Correct |
27 ms |
3164 KB |
Output is correct |
10 |
Correct |
49 ms |
3164 KB |
Output is correct |
11 |
Correct |
10 ms |
3268 KB |
Output is correct |
12 |
Correct |
72 ms |
3164 KB |
Output is correct |
13 |
Correct |
18 ms |
3164 KB |
Output is correct |
14 |
Correct |
59 ms |
3264 KB |
Output is correct |
15 |
Correct |
64 ms |
3164 KB |
Output is correct |
16 |
Correct |
49 ms |
3160 KB |
Output is correct |
17 |
Correct |
9 ms |
3164 KB |
Output is correct |
18 |
Correct |
8 ms |
3160 KB |
Output is correct |
19 |
Correct |
26 ms |
3164 KB |
Output is correct |
20 |
Correct |
46 ms |
3272 KB |
Output is correct |
21 |
Correct |
6 ms |
3320 KB |
Output is correct |
22 |
Correct |
9 ms |
3164 KB |
Output is correct |
23 |
Correct |
100 ms |
3248 KB |
Output is correct |
24 |
Correct |
45 ms |
3256 KB |
Output is correct |
25 |
Correct |
93 ms |
3160 KB |
Output is correct |
26 |
Correct |
121 ms |
3360 KB |
Output is correct |
27 |
Correct |
114 ms |
3160 KB |
Output is correct |
28 |
Correct |
86 ms |
3248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3164 KB |
Output is correct |
2 |
Correct |
3 ms |
3164 KB |
Output is correct |
3 |
Correct |
3 ms |
3160 KB |
Output is correct |
4 |
Correct |
10 ms |
3164 KB |
Output is correct |
5 |
Runtime error |
336 ms |
524288 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
3164 KB |
Output is correct |
2 |
Correct |
53 ms |
3164 KB |
Output is correct |
3 |
Correct |
18 ms |
3164 KB |
Output is correct |
4 |
Correct |
59 ms |
3252 KB |
Output is correct |
5 |
Correct |
64 ms |
3160 KB |
Output is correct |
6 |
Correct |
52 ms |
3160 KB |
Output is correct |
7 |
Correct |
9 ms |
3164 KB |
Output is correct |
8 |
Correct |
8 ms |
3272 KB |
Output is correct |
9 |
Correct |
27 ms |
3164 KB |
Output is correct |
10 |
Correct |
49 ms |
3164 KB |
Output is correct |
11 |
Correct |
10 ms |
3268 KB |
Output is correct |
12 |
Correct |
72 ms |
3164 KB |
Output is correct |
13 |
Correct |
18 ms |
3164 KB |
Output is correct |
14 |
Correct |
59 ms |
3264 KB |
Output is correct |
15 |
Correct |
64 ms |
3164 KB |
Output is correct |
16 |
Correct |
49 ms |
3160 KB |
Output is correct |
17 |
Correct |
9 ms |
3164 KB |
Output is correct |
18 |
Correct |
8 ms |
3160 KB |
Output is correct |
19 |
Correct |
26 ms |
3164 KB |
Output is correct |
20 |
Correct |
46 ms |
3272 KB |
Output is correct |
21 |
Correct |
6 ms |
3320 KB |
Output is correct |
22 |
Correct |
9 ms |
3164 KB |
Output is correct |
23 |
Correct |
100 ms |
3248 KB |
Output is correct |
24 |
Correct |
45 ms |
3256 KB |
Output is correct |
25 |
Correct |
93 ms |
3160 KB |
Output is correct |
26 |
Correct |
121 ms |
3360 KB |
Output is correct |
27 |
Correct |
114 ms |
3160 KB |
Output is correct |
28 |
Correct |
86 ms |
3248 KB |
Output is correct |
29 |
Correct |
10 ms |
3160 KB |
Output is correct |
30 |
Correct |
67 ms |
3360 KB |
Output is correct |
31 |
Correct |
18 ms |
3160 KB |
Output is correct |
32 |
Correct |
73 ms |
3164 KB |
Output is correct |
33 |
Correct |
68 ms |
3164 KB |
Output is correct |
34 |
Correct |
48 ms |
3164 KB |
Output is correct |
35 |
Correct |
9 ms |
3164 KB |
Output is correct |
36 |
Correct |
8 ms |
3164 KB |
Output is correct |
37 |
Correct |
26 ms |
3164 KB |
Output is correct |
38 |
Correct |
40 ms |
3252 KB |
Output is correct |
39 |
Correct |
7 ms |
3164 KB |
Output is correct |
40 |
Correct |
9 ms |
3164 KB |
Output is correct |
41 |
Correct |
92 ms |
3164 KB |
Output is correct |
42 |
Correct |
48 ms |
3160 KB |
Output is correct |
43 |
Correct |
100 ms |
3256 KB |
Output is correct |
44 |
Correct |
114 ms |
3164 KB |
Output is correct |
45 |
Correct |
99 ms |
3256 KB |
Output is correct |
46 |
Correct |
78 ms |
3248 KB |
Output is correct |
47 |
Correct |
8 ms |
3160 KB |
Output is correct |
48 |
Correct |
10 ms |
3164 KB |
Output is correct |
49 |
Correct |
212 ms |
3256 KB |
Output is correct |
50 |
Correct |
92 ms |
3160 KB |
Output is correct |
51 |
Correct |
274 ms |
3252 KB |
Output is correct |
52 |
Correct |
263 ms |
3164 KB |
Output is correct |
53 |
Correct |
214 ms |
3164 KB |
Output is correct |
54 |
Correct |
177 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3164 KB |
Output is correct |
2 |
Correct |
3 ms |
3164 KB |
Output is correct |
3 |
Correct |
3 ms |
3160 KB |
Output is correct |
4 |
Correct |
10 ms |
3164 KB |
Output is correct |
5 |
Runtime error |
336 ms |
524288 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
3164 KB |
Output is correct |
2 |
Correct |
53 ms |
3164 KB |
Output is correct |
3 |
Correct |
18 ms |
3164 KB |
Output is correct |
4 |
Correct |
59 ms |
3252 KB |
Output is correct |
5 |
Correct |
64 ms |
3160 KB |
Output is correct |
6 |
Correct |
52 ms |
3160 KB |
Output is correct |
7 |
Correct |
9 ms |
3164 KB |
Output is correct |
8 |
Correct |
8 ms |
3272 KB |
Output is correct |
9 |
Correct |
27 ms |
3164 KB |
Output is correct |
10 |
Correct |
49 ms |
3164 KB |
Output is correct |
11 |
Correct |
10 ms |
3268 KB |
Output is correct |
12 |
Correct |
72 ms |
3164 KB |
Output is correct |
13 |
Correct |
18 ms |
3164 KB |
Output is correct |
14 |
Correct |
59 ms |
3264 KB |
Output is correct |
15 |
Correct |
64 ms |
3164 KB |
Output is correct |
16 |
Correct |
49 ms |
3160 KB |
Output is correct |
17 |
Correct |
9 ms |
3164 KB |
Output is correct |
18 |
Correct |
8 ms |
3160 KB |
Output is correct |
19 |
Correct |
26 ms |
3164 KB |
Output is correct |
20 |
Correct |
46 ms |
3272 KB |
Output is correct |
21 |
Correct |
6 ms |
3320 KB |
Output is correct |
22 |
Correct |
9 ms |
3164 KB |
Output is correct |
23 |
Correct |
100 ms |
3248 KB |
Output is correct |
24 |
Correct |
45 ms |
3256 KB |
Output is correct |
25 |
Correct |
93 ms |
3160 KB |
Output is correct |
26 |
Correct |
121 ms |
3360 KB |
Output is correct |
27 |
Correct |
114 ms |
3160 KB |
Output is correct |
28 |
Correct |
86 ms |
3248 KB |
Output is correct |
29 |
Correct |
10 ms |
3160 KB |
Output is correct |
30 |
Correct |
67 ms |
3360 KB |
Output is correct |
31 |
Correct |
18 ms |
3160 KB |
Output is correct |
32 |
Correct |
73 ms |
3164 KB |
Output is correct |
33 |
Correct |
68 ms |
3164 KB |
Output is correct |
34 |
Correct |
48 ms |
3164 KB |
Output is correct |
35 |
Correct |
9 ms |
3164 KB |
Output is correct |
36 |
Correct |
8 ms |
3164 KB |
Output is correct |
37 |
Correct |
26 ms |
3164 KB |
Output is correct |
38 |
Correct |
40 ms |
3252 KB |
Output is correct |
39 |
Correct |
7 ms |
3164 KB |
Output is correct |
40 |
Correct |
9 ms |
3164 KB |
Output is correct |
41 |
Correct |
92 ms |
3164 KB |
Output is correct |
42 |
Correct |
48 ms |
3160 KB |
Output is correct |
43 |
Correct |
100 ms |
3256 KB |
Output is correct |
44 |
Correct |
114 ms |
3164 KB |
Output is correct |
45 |
Correct |
99 ms |
3256 KB |
Output is correct |
46 |
Correct |
78 ms |
3248 KB |
Output is correct |
47 |
Correct |
8 ms |
3160 KB |
Output is correct |
48 |
Correct |
10 ms |
3164 KB |
Output is correct |
49 |
Correct |
212 ms |
3256 KB |
Output is correct |
50 |
Correct |
92 ms |
3160 KB |
Output is correct |
51 |
Correct |
274 ms |
3252 KB |
Output is correct |
52 |
Correct |
263 ms |
3164 KB |
Output is correct |
53 |
Correct |
214 ms |
3164 KB |
Output is correct |
54 |
Correct |
177 ms |
3164 KB |
Output is correct |
55 |
Correct |
10 ms |
3164 KB |
Output is correct |
56 |
Correct |
57 ms |
3164 KB |
Output is correct |
57 |
Correct |
18 ms |
3164 KB |
Output is correct |
58 |
Correct |
67 ms |
3248 KB |
Output is correct |
59 |
Correct |
65 ms |
3164 KB |
Output is correct |
60 |
Correct |
51 ms |
3160 KB |
Output is correct |
61 |
Correct |
13 ms |
3316 KB |
Output is correct |
62 |
Correct |
9 ms |
3160 KB |
Output is correct |
63 |
Correct |
26 ms |
3164 KB |
Output is correct |
64 |
Correct |
40 ms |
3164 KB |
Output is correct |
65 |
Correct |
6 ms |
3164 KB |
Output is correct |
66 |
Correct |
9 ms |
3276 KB |
Output is correct |
67 |
Correct |
103 ms |
3164 KB |
Output is correct |
68 |
Correct |
44 ms |
3252 KB |
Output is correct |
69 |
Correct |
113 ms |
3164 KB |
Output is correct |
70 |
Correct |
113 ms |
3164 KB |
Output is correct |
71 |
Correct |
97 ms |
3164 KB |
Output is correct |
72 |
Correct |
80 ms |
3256 KB |
Output is correct |
73 |
Correct |
7 ms |
3164 KB |
Output is correct |
74 |
Correct |
10 ms |
3276 KB |
Output is correct |
75 |
Correct |
209 ms |
3164 KB |
Output is correct |
76 |
Correct |
93 ms |
3164 KB |
Output is correct |
77 |
Correct |
230 ms |
3160 KB |
Output is correct |
78 |
Correct |
242 ms |
3164 KB |
Output is correct |
79 |
Correct |
218 ms |
3252 KB |
Output is correct |
80 |
Correct |
179 ms |
3164 KB |
Output is correct |
81 |
Correct |
937 ms |
3256 KB |
Output is correct |
82 |
Correct |
367 ms |
3160 KB |
Output is correct |
83 |
Correct |
831 ms |
3264 KB |
Output is correct |
84 |
Correct |
1033 ms |
3260 KB |
Output is correct |
85 |
Correct |
12 ms |
3164 KB |
Output is correct |
86 |
Correct |
14 ms |
3160 KB |
Output is correct |
87 |
Correct |
746 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3164 KB |
Output is correct |
2 |
Correct |
3 ms |
3164 KB |
Output is correct |
3 |
Correct |
3 ms |
3160 KB |
Output is correct |
4 |
Correct |
10 ms |
3164 KB |
Output is correct |
5 |
Runtime error |
336 ms |
524288 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |