/**
* author: hashman
* created:
**/
// Thanks Dominater069 for the idea of solution!
#include <bits/stdc++.h>
using namespace std;
string to_string(string s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() {
cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
using Int = long long;
const Int inf = 1e18;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
auto solve = [&](auto&& self, int l, int r) -> vector<vector<Int>> {
int sz = r - l + 1, till = (sz + 1) / 2;
vector<vector<Int>> dp(till + 1, vector<Int>(4, -inf));
if (l == r) {
dp[0][0] = 0;
dp[1][3] = a[l];
return dp;
}
int m = (l + r) / 2;
vector<vector<Int>> dpl = self(self, l, m), dpr = self(self, m + 1, r);
for (int i = 0; i <= till; i++) {
for (int ll = 0; ll < 2; ll++) {
for (int lr = 0; lr < 2; lr++) {
for (int rl = 0; rl < 2; rl++) {
for (int rr = 0; rr < 2; rr++) {
if (rl && lr) {
continue;
}
int curo = ll + 2 * rr, lfo = ll + 2 * lr, rgo = rl + 2 * rr;
int low = 0, high = i;
while (high - low > 2) {
int mid_1 = (2 * low + high) / 3, mid_2 = (low + 2 * high) / 3;
if (mid_2 >= int(dpl.size())) {
high = mid_2;
continue;
}
if (i - mid_1 >= int(dpr.size())) {
low = mid_1;
continue;
}
Int candl = dpl[mid_1][lfo] + dpr[i - mid_1][rgo], candh = dpl[mid_2][lfo] + dpr[i - mid_2][rgo];
if (candl > candh) {
high = mid_2;
} else {
low = mid_1;
}
}
for (int op = low; op <= high; op++) {
int a = op, b = i - op;
if (a >= int(dpl.size()) || b >= int(dpr.size())) {
continue;
}
dp[i][curo] = max(dp[i][curo], dpl[a][lfo] + dpr[b][rgo]);
}
}
}
}
}
}
return dp;
};
vector<vector<Int>> dp = solve(solve, 0, n - 1);
for (int i = 1; i <= (n + 1) / 2; i++) {
cout << max({dp[i][0], dp[i][1], dp[i][2], dp[i][3]}) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
604 KB |
Output is correct |
2 |
Correct |
9 ms |
468 KB |
Output is correct |
3 |
Correct |
9 ms |
700 KB |
Output is correct |
4 |
Correct |
9 ms |
604 KB |
Output is correct |
5 |
Correct |
9 ms |
600 KB |
Output is correct |
6 |
Correct |
8 ms |
856 KB |
Output is correct |
7 |
Correct |
8 ms |
604 KB |
Output is correct |
8 |
Correct |
8 ms |
604 KB |
Output is correct |
9 |
Correct |
9 ms |
696 KB |
Output is correct |
10 |
Correct |
9 ms |
604 KB |
Output is correct |
11 |
Correct |
8 ms |
600 KB |
Output is correct |
12 |
Correct |
10 ms |
660 KB |
Output is correct |
13 |
Correct |
9 ms |
696 KB |
Output is correct |
14 |
Correct |
9 ms |
604 KB |
Output is correct |
15 |
Correct |
9 ms |
604 KB |
Output is correct |
16 |
Correct |
10 ms |
664 KB |
Output is correct |
17 |
Correct |
8 ms |
600 KB |
Output is correct |
18 |
Correct |
9 ms |
696 KB |
Output is correct |
19 |
Correct |
9 ms |
600 KB |
Output is correct |
20 |
Correct |
9 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
604 KB |
Output is correct |
2 |
Correct |
9 ms |
468 KB |
Output is correct |
3 |
Correct |
9 ms |
700 KB |
Output is correct |
4 |
Correct |
9 ms |
604 KB |
Output is correct |
5 |
Correct |
9 ms |
600 KB |
Output is correct |
6 |
Correct |
8 ms |
856 KB |
Output is correct |
7 |
Correct |
8 ms |
604 KB |
Output is correct |
8 |
Correct |
8 ms |
604 KB |
Output is correct |
9 |
Correct |
9 ms |
696 KB |
Output is correct |
10 |
Correct |
9 ms |
604 KB |
Output is correct |
11 |
Correct |
8 ms |
600 KB |
Output is correct |
12 |
Correct |
10 ms |
660 KB |
Output is correct |
13 |
Correct |
9 ms |
696 KB |
Output is correct |
14 |
Correct |
9 ms |
604 KB |
Output is correct |
15 |
Correct |
9 ms |
604 KB |
Output is correct |
16 |
Correct |
10 ms |
664 KB |
Output is correct |
17 |
Correct |
8 ms |
600 KB |
Output is correct |
18 |
Correct |
9 ms |
696 KB |
Output is correct |
19 |
Correct |
9 ms |
600 KB |
Output is correct |
20 |
Correct |
9 ms |
600 KB |
Output is correct |
21 |
Correct |
2323 ms |
24412 KB |
Output is correct |
22 |
Correct |
2412 ms |
24532 KB |
Output is correct |
23 |
Correct |
2336 ms |
24412 KB |
Output is correct |
24 |
Correct |
2410 ms |
24232 KB |
Output is correct |
25 |
Correct |
2506 ms |
24328 KB |
Output is correct |
26 |
Correct |
2444 ms |
24256 KB |
Output is correct |
27 |
Correct |
2432 ms |
24436 KB |
Output is correct |
28 |
Correct |
2457 ms |
24624 KB |
Output is correct |
29 |
Correct |
2464 ms |
24444 KB |
Output is correct |
30 |
Correct |
2480 ms |
24456 KB |
Output is correct |
31 |
Correct |
2475 ms |
24444 KB |
Output is correct |
32 |
Correct |
2475 ms |
24444 KB |
Output is correct |
33 |
Correct |
2462 ms |
24200 KB |
Output is correct |
34 |
Correct |
2509 ms |
24204 KB |
Output is correct |
35 |
Correct |
2494 ms |
24240 KB |
Output is correct |
36 |
Correct |
2436 ms |
24416 KB |
Output is correct |
37 |
Correct |
2537 ms |
24592 KB |
Output is correct |
38 |
Correct |
2482 ms |
24416 KB |
Output is correct |
39 |
Correct |
2474 ms |
24260 KB |
Output is correct |
40 |
Correct |
2522 ms |
24272 KB |
Output is correct |
41 |
Correct |
2537 ms |
24252 KB |
Output is correct |
42 |
Correct |
2430 ms |
24464 KB |
Output is correct |
43 |
Correct |
2456 ms |
24440 KB |
Output is correct |
44 |
Correct |
2442 ms |
24496 KB |
Output is correct |
45 |
Correct |
2463 ms |
24456 KB |
Output is correct |
46 |
Correct |
2491 ms |
24440 KB |
Output is correct |
47 |
Correct |
2477 ms |
24464 KB |
Output is correct |
48 |
Correct |
2416 ms |
24200 KB |
Output is correct |
49 |
Correct |
2433 ms |
24264 KB |
Output is correct |
50 |
Correct |
2511 ms |
24196 KB |
Output is correct |