#include <bits/stdc++.h>
#define fi first
#define se second
#define fin(s) freopen( s, "r", stdin );
#define fout(s) freopen( s, "w", stdout );
const long long N = 200200;
const long long Q = 200200;
const long long mod = 998244353;
const long long MAGIC = sqrt(N);
using namespace std;
int n;
long long a[N];
set < int > s;
set < pair < long long, int > > f;
void solve()
{
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
s.insert(i);
f.insert({a[i], i});
}
long long ans = 0;
a[0] = a[n + 1] = -1e15;
s.insert(0);
s.insert(n + 1);
for(int i = 1; i <= (n + 1) / 2; i++){
int x = (--f.end())->se;
f.erase(--f.end());
s.erase(x);
ans += a[x];
a[x] = - a[x];
auto p = s.lower_bound(x);
if(p != s.end()){
a[x] += a[*p];
f.erase({a[*p], *p});
s.erase(p);
}
p = s.lower_bound(x);
if(p != s.begin()){
p--;
a[x] += a[*p];
f.erase({a[*p], *p});
s.erase(p);
}
s.insert(x);
f.insert({a[x], x});
cout << ans << "\n";
}
}
bool mtest = false; int main()
{
//fin("input.txt");
//fout("output.txt");
//fin("island.in");
//fout("island.out");
ios_base::sync_with_stdio(0);
int TE = 1;
if(mtest)
cin >> TE;
while(TE--)
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
504 KB |
Output is correct |
2 |
Correct |
5 ms |
612 KB |
Output is correct |
3 |
Correct |
4 ms |
704 KB |
Output is correct |
4 |
Correct |
4 ms |
924 KB |
Output is correct |
5 |
Correct |
4 ms |
924 KB |
Output is correct |
6 |
Correct |
4 ms |
924 KB |
Output is correct |
7 |
Correct |
4 ms |
924 KB |
Output is correct |
8 |
Correct |
4 ms |
940 KB |
Output is correct |
9 |
Correct |
3 ms |
940 KB |
Output is correct |
10 |
Correct |
4 ms |
956 KB |
Output is correct |
11 |
Correct |
6 ms |
960 KB |
Output is correct |
12 |
Correct |
6 ms |
960 KB |
Output is correct |
13 |
Correct |
5 ms |
960 KB |
Output is correct |
14 |
Correct |
5 ms |
1064 KB |
Output is correct |
15 |
Correct |
4 ms |
1064 KB |
Output is correct |
16 |
Correct |
4 ms |
1064 KB |
Output is correct |
17 |
Correct |
5 ms |
1064 KB |
Output is correct |
18 |
Correct |
5 ms |
1064 KB |
Output is correct |
19 |
Correct |
4 ms |
1064 KB |
Output is correct |
20 |
Correct |
5 ms |
1064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
504 KB |
Output is correct |
2 |
Correct |
5 ms |
612 KB |
Output is correct |
3 |
Correct |
4 ms |
704 KB |
Output is correct |
4 |
Correct |
4 ms |
924 KB |
Output is correct |
5 |
Correct |
4 ms |
924 KB |
Output is correct |
6 |
Correct |
4 ms |
924 KB |
Output is correct |
7 |
Correct |
4 ms |
924 KB |
Output is correct |
8 |
Correct |
4 ms |
940 KB |
Output is correct |
9 |
Correct |
3 ms |
940 KB |
Output is correct |
10 |
Correct |
4 ms |
956 KB |
Output is correct |
11 |
Correct |
6 ms |
960 KB |
Output is correct |
12 |
Correct |
6 ms |
960 KB |
Output is correct |
13 |
Correct |
5 ms |
960 KB |
Output is correct |
14 |
Correct |
5 ms |
1064 KB |
Output is correct |
15 |
Correct |
4 ms |
1064 KB |
Output is correct |
16 |
Correct |
4 ms |
1064 KB |
Output is correct |
17 |
Correct |
5 ms |
1064 KB |
Output is correct |
18 |
Correct |
5 ms |
1064 KB |
Output is correct |
19 |
Correct |
4 ms |
1064 KB |
Output is correct |
20 |
Correct |
5 ms |
1064 KB |
Output is correct |
21 |
Correct |
691 ms |
25628 KB |
Output is correct |
22 |
Correct |
723 ms |
25760 KB |
Output is correct |
23 |
Correct |
843 ms |
25764 KB |
Output is correct |
24 |
Correct |
369 ms |
25764 KB |
Output is correct |
25 |
Correct |
374 ms |
25764 KB |
Output is correct |
26 |
Correct |
367 ms |
25764 KB |
Output is correct |
27 |
Correct |
367 ms |
25764 KB |
Output is correct |
28 |
Correct |
357 ms |
25764 KB |
Output is correct |
29 |
Correct |
329 ms |
25764 KB |
Output is correct |
30 |
Correct |
356 ms |
25764 KB |
Output is correct |
31 |
Correct |
356 ms |
25764 KB |
Output is correct |
32 |
Correct |
381 ms |
25764 KB |
Output is correct |
33 |
Correct |
583 ms |
25764 KB |
Output is correct |
34 |
Correct |
495 ms |
25792 KB |
Output is correct |
35 |
Correct |
584 ms |
25860 KB |
Output is correct |
36 |
Correct |
738 ms |
25860 KB |
Output is correct |
37 |
Correct |
760 ms |
25860 KB |
Output is correct |
38 |
Correct |
876 ms |
25860 KB |
Output is correct |
39 |
Correct |
357 ms |
25860 KB |
Output is correct |
40 |
Correct |
370 ms |
25860 KB |
Output is correct |
41 |
Correct |
349 ms |
25860 KB |
Output is correct |
42 |
Correct |
346 ms |
25860 KB |
Output is correct |
43 |
Correct |
334 ms |
25860 KB |
Output is correct |
44 |
Correct |
361 ms |
25860 KB |
Output is correct |
45 |
Correct |
363 ms |
25860 KB |
Output is correct |
46 |
Correct |
362 ms |
25860 KB |
Output is correct |
47 |
Correct |
352 ms |
25860 KB |
Output is correct |
48 |
Correct |
501 ms |
25860 KB |
Output is correct |
49 |
Correct |
504 ms |
25860 KB |
Output is correct |
50 |
Correct |
482 ms |
25860 KB |
Output is correct |