#include <iostream>
#include <set>
#include <queue>
#define f first
#define s second
using namespace std;
long long n, a[200010], b[1000010], odd[200010], even[200010], sum;
priority_queue<pair<long long,pair<int,int>>> pq;
set<pair<int,int>> bst;
set<pair<int,int>>::iterator iter;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
odd[i] = odd[i-1];
even[i] = even[i-1];
if (i%2) odd[i] += a[i];
else even[i] += a[i];
}
for (int i = 1; i <= n; i++) {
bst.insert({i,i});
pq.push({a[i],{i,i}});
}
for (int i = 1; i <= (n+1)/2; i++) {
while ((bst.find(pq.top().s) == bst.end()) || ((pq.top().s.f == 0) || (pq.top().s.s == n+1))) pq.pop();
sum += pq.top().f;
int l = pq.top().s.f-1, r = pq.top().s.s+1;
bst.erase(pq.top().s);
pq.pop();
iter = bst.lower_bound({l,r});
if (iter != bst.begin()) {
iter = prev(iter);
if ((*iter).s == l) {
l = (*iter).f;
bst.erase(iter);
}
}
iter = bst.lower_bound({l,r});
if (iter != bst.end()) {
if ((*iter).f == r) {
r = (*iter).s;
bst.erase(iter);
}
}
cout << sum << "\n";
bst.insert({l,r});
if (l%2) pq.push({odd[r]-odd[l-1]-even[r]+even[l-1],{l,r}});
else pq.push({-odd[r]+odd[l-1]+even[r]-even[l-1],{l,r}});
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
456 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
668 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
456 KB |
Output is correct |
16 |
Correct |
2 ms |
656 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
456 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
668 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
456 KB |
Output is correct |
16 |
Correct |
2 ms |
656 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
600 KB |
Output is correct |
21 |
Correct |
251 ms |
20944 KB |
Output is correct |
22 |
Correct |
258 ms |
20928 KB |
Output is correct |
23 |
Correct |
243 ms |
20968 KB |
Output is correct |
24 |
Correct |
165 ms |
20916 KB |
Output is correct |
25 |
Correct |
172 ms |
20920 KB |
Output is correct |
26 |
Correct |
162 ms |
20920 KB |
Output is correct |
27 |
Correct |
221 ms |
21068 KB |
Output is correct |
28 |
Correct |
174 ms |
20920 KB |
Output is correct |
29 |
Correct |
174 ms |
20916 KB |
Output is correct |
30 |
Correct |
175 ms |
21064 KB |
Output is correct |
31 |
Correct |
180 ms |
20920 KB |
Output is correct |
32 |
Correct |
180 ms |
20920 KB |
Output is correct |
33 |
Correct |
201 ms |
20644 KB |
Output is correct |
34 |
Correct |
239 ms |
20748 KB |
Output is correct |
35 |
Correct |
190 ms |
20920 KB |
Output is correct |
36 |
Correct |
261 ms |
20916 KB |
Output is correct |
37 |
Correct |
246 ms |
20920 KB |
Output is correct |
38 |
Correct |
250 ms |
20992 KB |
Output is correct |
39 |
Correct |
160 ms |
20924 KB |
Output is correct |
40 |
Correct |
161 ms |
20868 KB |
Output is correct |
41 |
Correct |
183 ms |
21008 KB |
Output is correct |
42 |
Correct |
184 ms |
20880 KB |
Output is correct |
43 |
Correct |
173 ms |
20916 KB |
Output is correct |
44 |
Correct |
179 ms |
20920 KB |
Output is correct |
45 |
Correct |
181 ms |
20920 KB |
Output is correct |
46 |
Correct |
177 ms |
20916 KB |
Output is correct |
47 |
Correct |
183 ms |
20916 KB |
Output is correct |
48 |
Correct |
191 ms |
20680 KB |
Output is correct |
49 |
Correct |
199 ms |
20868 KB |
Output is correct |
50 |
Correct |
197 ms |
20836 KB |
Output is correct |