#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const int MAXN = 2e5 + 7 ;
const ll inf = 1e16 ;
int n ;
ll a[ MAXN ] ;
ll sm[ MAXN ] ;
int st[ MAXN ] , en[ MAXN ] ;
bool done[ MAXN ] ;
priority_queue < pair < ll , int > > q ;
void solve ( ) {
cin >> n ;
for ( int i = 1 ; i <= n ; ++ i ) {
cin >> a[ i ] ;
}
en[ 0 ] = 1 , st[ n + 1 ] = n ;
sm[ 0 ] = sm[ n + 1 ] = -inf ;
for ( int i = 1 ; i <= n ; ++ i ) {
sm[ i ] = a[ i ] ;
st[ i ] = i - 1 ;
en[ i ] = i + 1 ;
q.push ( { sm[ i ] , i } ) ;
}
ll ans = 0 ;
for ( int i = 1 ; i <= ( n + 1 ) / 2 ; ++ i ) {
while ( q.empty ( ) == false ) {
auto [ aux , wh ] = q.top ( ) ;
q.pop ( ) ;
if ( done[ wh ] == true ) { continue ; }
ans += aux ;
cout << ans << "\n" ;
sm[ wh ] = sm[ st[ wh ] ] + sm[ en[ wh ] ] - sm[ wh ] ;
done[ st[ wh ] ] = done[ en[ wh ] ] = true ;
q.push ( { sm[ wh ] , wh } ) ;
st[ wh ] = st[ st[ wh ] ] ;
en[ st[ wh ] ] = wh ;
en[ wh ] = en[ en[ wh ] ] ;
st[ en[ wh ] ] = wh ;
break ;
}
}
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ; // cin >> t ;
while ( t -- ) { solve ( ) ; }
return 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
484 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
484 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
484 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
476 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
480 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
484 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
484 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
484 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
476 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
480 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
109 ms |
11900 KB |
Output is correct |
22 |
Correct |
93 ms |
11784 KB |
Output is correct |
23 |
Correct |
89 ms |
11820 KB |
Output is correct |
24 |
Correct |
80 ms |
11724 KB |
Output is correct |
25 |
Correct |
73 ms |
11704 KB |
Output is correct |
26 |
Correct |
76 ms |
11740 KB |
Output is correct |
27 |
Correct |
81 ms |
11824 KB |
Output is correct |
28 |
Correct |
83 ms |
11892 KB |
Output is correct |
29 |
Correct |
81 ms |
11800 KB |
Output is correct |
30 |
Correct |
96 ms |
11956 KB |
Output is correct |
31 |
Correct |
77 ms |
11840 KB |
Output is correct |
32 |
Correct |
84 ms |
11904 KB |
Output is correct |
33 |
Correct |
115 ms |
11568 KB |
Output is correct |
34 |
Correct |
81 ms |
11652 KB |
Output is correct |
35 |
Correct |
80 ms |
11668 KB |
Output is correct |
36 |
Correct |
96 ms |
11792 KB |
Output is correct |
37 |
Correct |
89 ms |
11868 KB |
Output is correct |
38 |
Correct |
90 ms |
11784 KB |
Output is correct |
39 |
Correct |
70 ms |
11736 KB |
Output is correct |
40 |
Correct |
78 ms |
11740 KB |
Output is correct |
41 |
Correct |
85 ms |
11700 KB |
Output is correct |
42 |
Correct |
75 ms |
11820 KB |
Output is correct |
43 |
Correct |
77 ms |
11840 KB |
Output is correct |
44 |
Correct |
76 ms |
11892 KB |
Output is correct |
45 |
Correct |
89 ms |
11860 KB |
Output is correct |
46 |
Correct |
74 ms |
11816 KB |
Output is correct |
47 |
Correct |
74 ms |
11836 KB |
Output is correct |
48 |
Correct |
82 ms |
11576 KB |
Output is correct |
49 |
Correct |
104 ms |
11680 KB |
Output is correct |
50 |
Correct |
83 ms |
11668 KB |
Output is correct |