/**
* author: hashman
* created:
**/
// Thanks Dominater069 for the idea of solution!
#pragma GCC target("avx2")
#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';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
604 KB |
Output is correct |
2 |
Correct |
8 ms |
604 KB |
Output is correct |
3 |
Correct |
10 ms |
672 KB |
Output is correct |
4 |
Correct |
10 ms |
1092 KB |
Output is correct |
5 |
Correct |
10 ms |
604 KB |
Output is correct |
6 |
Correct |
9 ms |
600 KB |
Output is correct |
7 |
Correct |
9 ms |
604 KB |
Output is correct |
8 |
Correct |
8 ms |
604 KB |
Output is correct |
9 |
Correct |
9 ms |
604 KB |
Output is correct |
10 |
Correct |
10 ms |
600 KB |
Output is correct |
11 |
Correct |
8 ms |
604 KB |
Output is correct |
12 |
Correct |
8 ms |
600 KB |
Output is correct |
13 |
Correct |
11 ms |
860 KB |
Output is correct |
14 |
Correct |
11 ms |
612 KB |
Output is correct |
15 |
Correct |
8 ms |
692 KB |
Output is correct |
16 |
Correct |
9 ms |
604 KB |
Output is correct |
17 |
Correct |
10 ms |
600 KB |
Output is correct |
18 |
Correct |
9 ms |
624 KB |
Output is correct |
19 |
Correct |
9 ms |
604 KB |
Output is correct |
20 |
Correct |
8 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
604 KB |
Output is correct |
2 |
Correct |
8 ms |
604 KB |
Output is correct |
3 |
Correct |
10 ms |
672 KB |
Output is correct |
4 |
Correct |
10 ms |
1092 KB |
Output is correct |
5 |
Correct |
10 ms |
604 KB |
Output is correct |
6 |
Correct |
9 ms |
600 KB |
Output is correct |
7 |
Correct |
9 ms |
604 KB |
Output is correct |
8 |
Correct |
8 ms |
604 KB |
Output is correct |
9 |
Correct |
9 ms |
604 KB |
Output is correct |
10 |
Correct |
10 ms |
600 KB |
Output is correct |
11 |
Correct |
8 ms |
604 KB |
Output is correct |
12 |
Correct |
8 ms |
600 KB |
Output is correct |
13 |
Correct |
11 ms |
860 KB |
Output is correct |
14 |
Correct |
11 ms |
612 KB |
Output is correct |
15 |
Correct |
8 ms |
692 KB |
Output is correct |
16 |
Correct |
9 ms |
604 KB |
Output is correct |
17 |
Correct |
10 ms |
600 KB |
Output is correct |
18 |
Correct |
9 ms |
624 KB |
Output is correct |
19 |
Correct |
9 ms |
604 KB |
Output is correct |
20 |
Correct |
8 ms |
600 KB |
Output is correct |
21 |
Correct |
2421 ms |
22656 KB |
Output is correct |
22 |
Correct |
2378 ms |
22476 KB |
Output is correct |
23 |
Correct |
2433 ms |
22484 KB |
Output is correct |
24 |
Correct |
2417 ms |
22476 KB |
Output is correct |
25 |
Correct |
2469 ms |
22580 KB |
Output is correct |
26 |
Correct |
2359 ms |
22480 KB |
Output is correct |
27 |
Correct |
2436 ms |
22468 KB |
Output is correct |
28 |
Correct |
2413 ms |
22476 KB |
Output is correct |
29 |
Correct |
2469 ms |
22504 KB |
Output is correct |
30 |
Correct |
2395 ms |
22580 KB |
Output is correct |
31 |
Correct |
2444 ms |
22524 KB |
Output is correct |
32 |
Correct |
2552 ms |
22568 KB |
Output is correct |
33 |
Correct |
2477 ms |
22616 KB |
Output is correct |
34 |
Correct |
2416 ms |
22520 KB |
Output is correct |
35 |
Correct |
2476 ms |
22684 KB |
Output is correct |
36 |
Correct |
2437 ms |
22484 KB |
Output is correct |
37 |
Correct |
2408 ms |
22504 KB |
Output is correct |
38 |
Correct |
2385 ms |
22436 KB |
Output is correct |
39 |
Correct |
2473 ms |
22700 KB |
Output is correct |
40 |
Correct |
2421 ms |
22476 KB |
Output is correct |
41 |
Correct |
2453 ms |
22476 KB |
Output is correct |
42 |
Correct |
2456 ms |
22564 KB |
Output is correct |
43 |
Correct |
2481 ms |
22472 KB |
Output is correct |
44 |
Correct |
2440 ms |
22496 KB |
Output is correct |
45 |
Correct |
2525 ms |
22480 KB |
Output is correct |
46 |
Correct |
2453 ms |
22600 KB |
Output is correct |
47 |
Correct |
2492 ms |
22480 KB |
Output is correct |
48 |
Correct |
2426 ms |
22536 KB |
Output is correct |
49 |
Correct |
2448 ms |
22496 KB |
Output is correct |
50 |
Correct |
2465 ms |
22476 KB |
Output is correct |