#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 = 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])
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1628 KB |
Output is correct |
2 |
Correct |
2 ms |
1628 KB |
Output is correct |
3 |
Correct |
2 ms |
1624 KB |
Output is correct |
4 |
Correct |
5 ms |
1628 KB |
Output is correct |
5 |
Runtime error |
375 ms |
524288 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1628 KB |
Output is correct |
2 |
Correct |
2 ms |
1628 KB |
Output is correct |
3 |
Correct |
2 ms |
1624 KB |
Output is correct |
4 |
Correct |
5 ms |
1628 KB |
Output is correct |
5 |
Runtime error |
375 ms |
524288 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1624 KB |
Output is correct |
2 |
Correct |
28 ms |
1864 KB |
Output is correct |
3 |
Correct |
9 ms |
1624 KB |
Output is correct |
4 |
Correct |
31 ms |
1868 KB |
Output is correct |
5 |
Correct |
32 ms |
1628 KB |
Output is correct |
6 |
Correct |
24 ms |
1624 KB |
Output is correct |
7 |
Correct |
5 ms |
1628 KB |
Output is correct |
8 |
Correct |
5 ms |
1640 KB |
Output is correct |
9 |
Correct |
14 ms |
1868 KB |
Output is correct |
10 |
Correct |
21 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1624 KB |
Output is correct |
2 |
Correct |
28 ms |
1864 KB |
Output is correct |
3 |
Correct |
9 ms |
1624 KB |
Output is correct |
4 |
Correct |
31 ms |
1868 KB |
Output is correct |
5 |
Correct |
32 ms |
1628 KB |
Output is correct |
6 |
Correct |
24 ms |
1624 KB |
Output is correct |
7 |
Correct |
5 ms |
1628 KB |
Output is correct |
8 |
Correct |
5 ms |
1640 KB |
Output is correct |
9 |
Correct |
14 ms |
1868 KB |
Output is correct |
10 |
Correct |
21 ms |
1628 KB |
Output is correct |
11 |
Correct |
3 ms |
1856 KB |
Output is correct |
12 |
Correct |
2 ms |
1628 KB |
Output is correct |
13 |
Correct |
2 ms |
1628 KB |
Output is correct |
14 |
Correct |
6 ms |
1628 KB |
Output is correct |
15 |
Correct |
27 ms |
2120 KB |
Output is correct |
16 |
Correct |
10 ms |
1628 KB |
Output is correct |
17 |
Correct |
29 ms |
1628 KB |
Output is correct |
18 |
Correct |
33 ms |
1628 KB |
Output is correct |
19 |
Correct |
25 ms |
1628 KB |
Output is correct |
20 |
Correct |
5 ms |
1628 KB |
Output is correct |
21 |
Correct |
4 ms |
1628 KB |
Output is correct |
22 |
Correct |
13 ms |
1868 KB |
Output is correct |
23 |
Correct |
20 ms |
1624 KB |
Output is correct |
24 |
Correct |
27 ms |
1624 KB |
Output is correct |
25 |
Correct |
11 ms |
1628 KB |
Output is correct |
26 |
Correct |
33 ms |
1628 KB |
Output is correct |
27 |
Correct |
25 ms |
1628 KB |
Output is correct |
28 |
Correct |
27 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1624 KB |
Output is correct |
2 |
Correct |
28 ms |
1864 KB |
Output is correct |
3 |
Correct |
9 ms |
1624 KB |
Output is correct |
4 |
Correct |
31 ms |
1868 KB |
Output is correct |
5 |
Correct |
32 ms |
1628 KB |
Output is correct |
6 |
Correct |
24 ms |
1624 KB |
Output is correct |
7 |
Correct |
5 ms |
1628 KB |
Output is correct |
8 |
Correct |
5 ms |
1640 KB |
Output is correct |
9 |
Correct |
14 ms |
1868 KB |
Output is correct |
10 |
Correct |
21 ms |
1628 KB |
Output is correct |
11 |
Correct |
5 ms |
1624 KB |
Output is correct |
12 |
Correct |
28 ms |
1628 KB |
Output is correct |
13 |
Correct |
9 ms |
1628 KB |
Output is correct |
14 |
Correct |
29 ms |
1628 KB |
Output is correct |
15 |
Correct |
33 ms |
1628 KB |
Output is correct |
16 |
Correct |
24 ms |
1628 KB |
Output is correct |
17 |
Correct |
5 ms |
1628 KB |
Output is correct |
18 |
Correct |
6 ms |
1884 KB |
Output is correct |
19 |
Correct |
14 ms |
1864 KB |
Output is correct |
20 |
Correct |
20 ms |
1864 KB |
Output is correct |
21 |
Correct |
3 ms |
1628 KB |
Output is correct |
22 |
Correct |
5 ms |
1628 KB |
Output is correct |
23 |
Correct |
52 ms |
1840 KB |
Output is correct |
24 |
Correct |
23 ms |
1624 KB |
Output is correct |
25 |
Correct |
46 ms |
1628 KB |
Output is correct |
26 |
Correct |
62 ms |
1628 KB |
Output is correct |
27 |
Correct |
51 ms |
1840 KB |
Output is correct |
28 |
Correct |
39 ms |
1624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1628 KB |
Output is correct |
2 |
Correct |
2 ms |
1628 KB |
Output is correct |
3 |
Correct |
2 ms |
1624 KB |
Output is correct |
4 |
Correct |
5 ms |
1628 KB |
Output is correct |
5 |
Runtime error |
375 ms |
524288 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1624 KB |
Output is correct |
2 |
Correct |
28 ms |
1864 KB |
Output is correct |
3 |
Correct |
9 ms |
1624 KB |
Output is correct |
4 |
Correct |
31 ms |
1868 KB |
Output is correct |
5 |
Correct |
32 ms |
1628 KB |
Output is correct |
6 |
Correct |
24 ms |
1624 KB |
Output is correct |
7 |
Correct |
5 ms |
1628 KB |
Output is correct |
8 |
Correct |
5 ms |
1640 KB |
Output is correct |
9 |
Correct |
14 ms |
1868 KB |
Output is correct |
10 |
Correct |
21 ms |
1628 KB |
Output is correct |
11 |
Correct |
5 ms |
1624 KB |
Output is correct |
12 |
Correct |
28 ms |
1628 KB |
Output is correct |
13 |
Correct |
9 ms |
1628 KB |
Output is correct |
14 |
Correct |
29 ms |
1628 KB |
Output is correct |
15 |
Correct |
33 ms |
1628 KB |
Output is correct |
16 |
Correct |
24 ms |
1628 KB |
Output is correct |
17 |
Correct |
5 ms |
1628 KB |
Output is correct |
18 |
Correct |
6 ms |
1884 KB |
Output is correct |
19 |
Correct |
14 ms |
1864 KB |
Output is correct |
20 |
Correct |
20 ms |
1864 KB |
Output is correct |
21 |
Correct |
3 ms |
1628 KB |
Output is correct |
22 |
Correct |
5 ms |
1628 KB |
Output is correct |
23 |
Correct |
52 ms |
1840 KB |
Output is correct |
24 |
Correct |
23 ms |
1624 KB |
Output is correct |
25 |
Correct |
46 ms |
1628 KB |
Output is correct |
26 |
Correct |
62 ms |
1628 KB |
Output is correct |
27 |
Correct |
51 ms |
1840 KB |
Output is correct |
28 |
Correct |
39 ms |
1624 KB |
Output is correct |
29 |
Correct |
5 ms |
1628 KB |
Output is correct |
30 |
Correct |
28 ms |
1864 KB |
Output is correct |
31 |
Correct |
9 ms |
1868 KB |
Output is correct |
32 |
Correct |
32 ms |
1628 KB |
Output is correct |
33 |
Correct |
32 ms |
1628 KB |
Output is correct |
34 |
Correct |
25 ms |
1628 KB |
Output is correct |
35 |
Correct |
5 ms |
1624 KB |
Output is correct |
36 |
Correct |
4 ms |
1628 KB |
Output is correct |
37 |
Correct |
13 ms |
1868 KB |
Output is correct |
38 |
Correct |
20 ms |
1864 KB |
Output is correct |
39 |
Correct |
3 ms |
1624 KB |
Output is correct |
40 |
Correct |
4 ms |
1628 KB |
Output is correct |
41 |
Correct |
47 ms |
1628 KB |
Output is correct |
42 |
Correct |
22 ms |
1860 KB |
Output is correct |
43 |
Correct |
48 ms |
1840 KB |
Output is correct |
44 |
Correct |
58 ms |
1624 KB |
Output is correct |
45 |
Correct |
50 ms |
1628 KB |
Output is correct |
46 |
Correct |
39 ms |
1624 KB |
Output is correct |
47 |
Correct |
4 ms |
1624 KB |
Output is correct |
48 |
Correct |
5 ms |
1624 KB |
Output is correct |
49 |
Correct |
104 ms |
1844 KB |
Output is correct |
50 |
Correct |
46 ms |
1624 KB |
Output is correct |
51 |
Correct |
114 ms |
1848 KB |
Output is correct |
52 |
Correct |
120 ms |
1624 KB |
Output is correct |
53 |
Correct |
108 ms |
1628 KB |
Output is correct |
54 |
Correct |
97 ms |
1848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1628 KB |
Output is correct |
2 |
Correct |
2 ms |
1628 KB |
Output is correct |
3 |
Correct |
2 ms |
1624 KB |
Output is correct |
4 |
Correct |
5 ms |
1628 KB |
Output is correct |
5 |
Runtime error |
375 ms |
524288 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1624 KB |
Output is correct |
2 |
Correct |
28 ms |
1864 KB |
Output is correct |
3 |
Correct |
9 ms |
1624 KB |
Output is correct |
4 |
Correct |
31 ms |
1868 KB |
Output is correct |
5 |
Correct |
32 ms |
1628 KB |
Output is correct |
6 |
Correct |
24 ms |
1624 KB |
Output is correct |
7 |
Correct |
5 ms |
1628 KB |
Output is correct |
8 |
Correct |
5 ms |
1640 KB |
Output is correct |
9 |
Correct |
14 ms |
1868 KB |
Output is correct |
10 |
Correct |
21 ms |
1628 KB |
Output is correct |
11 |
Correct |
5 ms |
1624 KB |
Output is correct |
12 |
Correct |
28 ms |
1628 KB |
Output is correct |
13 |
Correct |
9 ms |
1628 KB |
Output is correct |
14 |
Correct |
29 ms |
1628 KB |
Output is correct |
15 |
Correct |
33 ms |
1628 KB |
Output is correct |
16 |
Correct |
24 ms |
1628 KB |
Output is correct |
17 |
Correct |
5 ms |
1628 KB |
Output is correct |
18 |
Correct |
6 ms |
1884 KB |
Output is correct |
19 |
Correct |
14 ms |
1864 KB |
Output is correct |
20 |
Correct |
20 ms |
1864 KB |
Output is correct |
21 |
Correct |
3 ms |
1628 KB |
Output is correct |
22 |
Correct |
5 ms |
1628 KB |
Output is correct |
23 |
Correct |
52 ms |
1840 KB |
Output is correct |
24 |
Correct |
23 ms |
1624 KB |
Output is correct |
25 |
Correct |
46 ms |
1628 KB |
Output is correct |
26 |
Correct |
62 ms |
1628 KB |
Output is correct |
27 |
Correct |
51 ms |
1840 KB |
Output is correct |
28 |
Correct |
39 ms |
1624 KB |
Output is correct |
29 |
Correct |
5 ms |
1628 KB |
Output is correct |
30 |
Correct |
28 ms |
1864 KB |
Output is correct |
31 |
Correct |
9 ms |
1868 KB |
Output is correct |
32 |
Correct |
32 ms |
1628 KB |
Output is correct |
33 |
Correct |
32 ms |
1628 KB |
Output is correct |
34 |
Correct |
25 ms |
1628 KB |
Output is correct |
35 |
Correct |
5 ms |
1624 KB |
Output is correct |
36 |
Correct |
4 ms |
1628 KB |
Output is correct |
37 |
Correct |
13 ms |
1868 KB |
Output is correct |
38 |
Correct |
20 ms |
1864 KB |
Output is correct |
39 |
Correct |
3 ms |
1624 KB |
Output is correct |
40 |
Correct |
4 ms |
1628 KB |
Output is correct |
41 |
Correct |
47 ms |
1628 KB |
Output is correct |
42 |
Correct |
22 ms |
1860 KB |
Output is correct |
43 |
Correct |
48 ms |
1840 KB |
Output is correct |
44 |
Correct |
58 ms |
1624 KB |
Output is correct |
45 |
Correct |
50 ms |
1628 KB |
Output is correct |
46 |
Correct |
39 ms |
1624 KB |
Output is correct |
47 |
Correct |
4 ms |
1624 KB |
Output is correct |
48 |
Correct |
5 ms |
1624 KB |
Output is correct |
49 |
Correct |
104 ms |
1844 KB |
Output is correct |
50 |
Correct |
46 ms |
1624 KB |
Output is correct |
51 |
Correct |
114 ms |
1848 KB |
Output is correct |
52 |
Correct |
120 ms |
1624 KB |
Output is correct |
53 |
Correct |
108 ms |
1628 KB |
Output is correct |
54 |
Correct |
97 ms |
1848 KB |
Output is correct |
55 |
Correct |
5 ms |
1628 KB |
Output is correct |
56 |
Correct |
28 ms |
1860 KB |
Output is correct |
57 |
Correct |
9 ms |
1628 KB |
Output is correct |
58 |
Correct |
30 ms |
1872 KB |
Output is correct |
59 |
Correct |
34 ms |
1868 KB |
Output is correct |
60 |
Correct |
24 ms |
1624 KB |
Output is correct |
61 |
Correct |
5 ms |
1628 KB |
Output is correct |
62 |
Correct |
4 ms |
1628 KB |
Output is correct |
63 |
Correct |
14 ms |
1860 KB |
Output is correct |
64 |
Correct |
20 ms |
1628 KB |
Output is correct |
65 |
Correct |
3 ms |
1624 KB |
Output is correct |
66 |
Correct |
4 ms |
1624 KB |
Output is correct |
67 |
Correct |
50 ms |
1628 KB |
Output is correct |
68 |
Correct |
23 ms |
1628 KB |
Output is correct |
69 |
Correct |
47 ms |
1864 KB |
Output is correct |
70 |
Correct |
58 ms |
1624 KB |
Output is correct |
71 |
Correct |
49 ms |
1840 KB |
Output is correct |
72 |
Correct |
40 ms |
1628 KB |
Output is correct |
73 |
Correct |
4 ms |
1628 KB |
Output is correct |
74 |
Correct |
5 ms |
1868 KB |
Output is correct |
75 |
Correct |
107 ms |
1844 KB |
Output is correct |
76 |
Correct |
47 ms |
1624 KB |
Output is correct |
77 |
Correct |
120 ms |
1876 KB |
Output is correct |
78 |
Correct |
118 ms |
1848 KB |
Output is correct |
79 |
Correct |
107 ms |
1848 KB |
Output is correct |
80 |
Correct |
90 ms |
1628 KB |
Output is correct |
81 |
Correct |
430 ms |
1876 KB |
Output is correct |
82 |
Correct |
183 ms |
1624 KB |
Output is correct |
83 |
Correct |
430 ms |
1852 KB |
Output is correct |
84 |
Correct |
454 ms |
1848 KB |
Output is correct |
85 |
Correct |
6 ms |
1628 KB |
Output is correct |
86 |
Correct |
6 ms |
1628 KB |
Output is correct |
87 |
Correct |
370 ms |
1624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1628 KB |
Output is correct |
2 |
Correct |
2 ms |
1628 KB |
Output is correct |
3 |
Correct |
2 ms |
1624 KB |
Output is correct |
4 |
Correct |
5 ms |
1628 KB |
Output is correct |
5 |
Runtime error |
375 ms |
524288 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |