#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define ff first
#define ss second
const int N = 5e5 + 5, inf = 1e9 + 7;
int n;
int a[N];
pii seg[1 << 20];
int ma[1 << 20];
pii Merge(pii u, pii v) {
pii c;
if (a[u.ff] > a[v.ff])
c.ff = u.ff, u.ff = u.ss;
else c.ff = v.ff, v.ff = v.ss;
c.ss = a[u.ff] > a[v.ff] ? u.ff : v.ff;
return c;
}
void build() {
for (int i = 1; i <= n; ++i) {
seg[i + n - 1].ff = i;
ma[i + n - 1] = a[i];
}
for (int i = n - 1; i; --i) {
seg[i] = Merge(seg[i << 1], seg[i << 1 | 1]);
ma[i] = max(ma[i << 1], ma[i << 1 | 1]);
}
}
void upd(int p) {
seg[p + n - 1].ff = ma[p + n - 1] = a[p] = 0;
for (p += n - 1, p >>= 1; p; p >>= 1) {
seg[p] = Merge(seg[p << 1], seg[p << 1 | 1]);
ma[p] = max(ma[p << 1], ma[p << 1 | 1]);
}
}
pii get_pos(int l, int r) {
pii ans(0, 0);
for (l += n - 1, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) ans = Merge(ans, seg[l++]);
if (r & 1) ans = Merge(ans, seg[--r]);
}
return ans;
}
int get_max(int l, int r) {
int ans(-inf);
for (l += n - 1, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) ans = max(ans, ma[l++]);
if (r & 1) ans = max(ans, ma[--r]);
}
return ans;
}
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
build();
int ans(-inf);
for (int cnt = n; cnt > 2; --cnt) {
pii p = get_pos(1, n);
int p1 = p.ff, p2 = p.ss;
if (p1 > p2) swap(p1, p2);
ans = max(ans, a[p1] + a[p2] + max({ get_max(1, p1 * 2 - p2), get_max(p1 + 1,(p1 + p2) >> 1), get_max(p2 * 2 - p1, n) }));
upd(p.ff);
}
cout << ans << '\n';
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
155 ms |
6800 KB |
Output is correct |
2 |
Correct |
81 ms |
7524 KB |
Output is correct |
3 |
Correct |
81 ms |
7540 KB |
Output is correct |
4 |
Incorrect |
156 ms |
7660 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |