#include <bits/stdc++.h>
using namespace std;
#define sp " "
//#define endl "\n"
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define LL node * 2
#define RR node * 2 + 1
#define N 300005
#define ll long long
#define int long long
const int modulo = 1e9 + 7;
const ll INF = 2e18 + 7;
int a[N], pre[2][N];
int32_t main(){
//fileio();
fastio();
int n;
cin>>n;
for (int i = 1; i <= n; i++){
cin>>a[i];
pre[0][i] = pre[0][i - 1], pre[1][i] = pre[1][i - 1];
pre[i % 2][i] += a[i];
}
set<array<int, 3>> s;
set<pii> ranges;
int ans = 0;
for (int i = 1; i <= n; i++)
s.insert({a[i], i, 0});
auto add_range = [&](int l, int r){
ranges.insert({l, r});
if (l == 1) return;
int prv = pre[r % 2][r] - pre[r % 2][l - 1];
int curr = pre[(r + 1) % 2][r + 1] - pre[(r + 1) % 2][l - 2];
if (r < n) s.insert({curr - prv, l, r});
};
auto del_range = [&](int l, int r){
ranges.erase({l, r});
if (l == 1) return;
int prv = pre[r % 2][r] - pre[r % 2][l - 1];
int curr = pre[(r + 1) % 2][r + 1] - pre[(r + 1) % 2][l - 2];
if (r < n) {
s.erase({curr - prv, l, r});
}
};
auto shift = [&](array<int, 3> x){
ans += x[0];
int l = x[1], r = x[2];
if (r == 0){
s.erase(x);
r = l;
}
else{
del_range(l, r);
l--, r++;
}
if (l > 1){
s.erase({a[l - 1], l - 1, 0});
}
if (r < n){
s.erase({a[r + 1], r + 1, 0});
}
auto it = ranges.lower_bound({r, 0});
if (it != ranges.end() && it->st == r + 2){
pii tmp = *it;
del_range(tmp.st, tmp.nd);
r = tmp.nd;
}
it = ranges.lower_bound({r, 0});
if (it != ranges.begin()){
it--;
if (it->nd == l - 2){
pii tmp = *it;
del_range(tmp.st, tmp.nd);
l = tmp.st;
}
}
add_range(l, r);
};
for (int j = 1; j <= (n + 1) / 2; j++){
assert(!s.empty());
array<int, 3> curr = *s.rbegin();
shift(curr);
cout<<ans<<endl;
}
cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2564 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
3 ms |
2648 KB |
Output is correct |
5 |
Correct |
3 ms |
2680 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
3 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
3 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
3 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2564 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
3 ms |
2648 KB |
Output is correct |
5 |
Correct |
3 ms |
2680 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
3 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
2 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
3 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
3 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2652 KB |
Output is correct |
21 |
Correct |
357 ms |
22004 KB |
Output is correct |
22 |
Correct |
344 ms |
22388 KB |
Output is correct |
23 |
Correct |
321 ms |
22096 KB |
Output is correct |
24 |
Correct |
197 ms |
21840 KB |
Output is correct |
25 |
Correct |
208 ms |
22100 KB |
Output is correct |
26 |
Correct |
197 ms |
21840 KB |
Output is correct |
27 |
Correct |
222 ms |
22096 KB |
Output is correct |
28 |
Correct |
224 ms |
22040 KB |
Output is correct |
29 |
Correct |
218 ms |
22184 KB |
Output is correct |
30 |
Correct |
218 ms |
22108 KB |
Output is correct |
31 |
Correct |
223 ms |
22048 KB |
Output is correct |
32 |
Correct |
249 ms |
22132 KB |
Output is correct |
33 |
Correct |
260 ms |
21840 KB |
Output is correct |
34 |
Correct |
249 ms |
21936 KB |
Output is correct |
35 |
Correct |
290 ms |
21956 KB |
Output is correct |
36 |
Correct |
297 ms |
22096 KB |
Output is correct |
37 |
Correct |
338 ms |
22096 KB |
Output is correct |
38 |
Correct |
328 ms |
22028 KB |
Output is correct |
39 |
Correct |
206 ms |
21852 KB |
Output is correct |
40 |
Correct |
231 ms |
22100 KB |
Output is correct |
41 |
Correct |
201 ms |
21844 KB |
Output is correct |
42 |
Correct |
252 ms |
22096 KB |
Output is correct |
43 |
Correct |
220 ms |
22100 KB |
Output is correct |
44 |
Correct |
223 ms |
22116 KB |
Output is correct |
45 |
Correct |
225 ms |
22048 KB |
Output is correct |
46 |
Correct |
212 ms |
22088 KB |
Output is correct |
47 |
Correct |
277 ms |
22168 KB |
Output is correct |
48 |
Correct |
254 ms |
21844 KB |
Output is correct |
49 |
Correct |
295 ms |
21832 KB |
Output is correct |
50 |
Correct |
259 ms |
22020 KB |
Output is correct |