#include <bits/stdc++.h>
#define int long long
using namespace std;
struct update {
int l, r;
long long val;
bool operator < ( const update &u ) const {
if ( val == u.val )
return l < u.l;
return val > u.val;
}
};
const int MAX_N = 2e5;
int leftt[MAX_N + 2], rightt[MAX_N + 2];
long long v[MAX_N + 2], val[MAX_N + 2];
set<update> updates;
signed main() {
int n;
cin >> n;
for ( int i = 1; i <= n; i++ ) {
cin >> v[i];
val[i] = v[i];
leftt[i] = rightt[i] = i;
updates.insert( { i, i, v[i] } );
}
val[0] = val[n + 1] = -1e18;
leftt[0] = 1;
rightt[n + 1] = n;
long long sum = 0;
for ( int k = 1; k <= (n + 1) / 2; k++ ) {
update u = *updates.begin();
sum += u.val;
updates.erase( { u.l, u.r, u.val } );
if ( u.l != 1 )
updates.erase( { leftt[u.l - 1], rightt[u.l - 1], val[u.l - 1] } );
if ( u.r != n )
updates.erase( { leftt[u.r + 1], rightt[u.r + 1], val[u.r + 1] } );
u.val = -u.val + val[u.l - 1] + val[u.r + 1];
u.l = leftt[u.l - 1];
u.r = rightt[u.r + 1];
leftt[u.l] = leftt[u.r] = u.l;
rightt[u.l] = rightt[u.r] = u.r;
val[u.l] = val[u.r] = u.val;
updates.insert( u );
cout << sum << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
536 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
452 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
452 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
444 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
448 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
536 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
452 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
452 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
444 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
448 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
264 ms |
22436 KB |
Output is correct |
22 |
Correct |
255 ms |
22400 KB |
Output is correct |
23 |
Correct |
287 ms |
22400 KB |
Output is correct |
24 |
Correct |
149 ms |
22292 KB |
Output is correct |
25 |
Correct |
158 ms |
22332 KB |
Output is correct |
26 |
Correct |
157 ms |
22312 KB |
Output is correct |
27 |
Correct |
168 ms |
22536 KB |
Output is correct |
28 |
Correct |
186 ms |
22500 KB |
Output is correct |
29 |
Correct |
162 ms |
22484 KB |
Output is correct |
30 |
Correct |
174 ms |
22476 KB |
Output is correct |
31 |
Correct |
167 ms |
22460 KB |
Output is correct |
32 |
Correct |
166 ms |
22404 KB |
Output is correct |
33 |
Correct |
224 ms |
22172 KB |
Output is correct |
34 |
Correct |
209 ms |
22292 KB |
Output is correct |
35 |
Correct |
216 ms |
22276 KB |
Output is correct |
36 |
Correct |
274 ms |
22392 KB |
Output is correct |
37 |
Correct |
247 ms |
22476 KB |
Output is correct |
38 |
Correct |
272 ms |
22472 KB |
Output is correct |
39 |
Correct |
147 ms |
22320 KB |
Output is correct |
40 |
Correct |
150 ms |
22424 KB |
Output is correct |
41 |
Correct |
158 ms |
22320 KB |
Output is correct |
42 |
Correct |
168 ms |
22468 KB |
Output is correct |
43 |
Correct |
167 ms |
22456 KB |
Output is correct |
44 |
Correct |
201 ms |
22512 KB |
Output is correct |
45 |
Correct |
171 ms |
22512 KB |
Output is correct |
46 |
Correct |
176 ms |
22476 KB |
Output is correct |
47 |
Correct |
193 ms |
22492 KB |
Output is correct |
48 |
Correct |
203 ms |
22220 KB |
Output is correct |
49 |
Correct |
249 ms |
22220 KB |
Output is correct |
50 |
Correct |
208 ms |
22280 KB |
Output is correct |