//#pragma optimization_level 3
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordset;
*/
#define fr first
#define sc second
#define vec vector
#define pb push_back
#define pii pair<int, int>
#define forn(x,y) for(int x = 1 ; x <= (int)y ; ++x)
#define all(x) (x).begin(),(x).end()
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef complex<int> point;
const int nmax = 400005;
const ll linf = 1e18;
const ll mod = 998244353;
const int inf = INT_MAX;
int n;
ll add[nmax], a[nmax];
int prv[nmax], nxt[nmax];
int left[nmax], right[nmax];
set < pair<ll,int> > st;
int pref[nmax] , odd_pref[nmax];
int used[nmax];
int main(){
fast;
cin >> n;
ll rez = 0;
forn(i,n){
cin >> add[i];
a[i] = add[i];
prv[i] = i-1;
nxt[i] = i+1;
st.insert({add[i],i});
}
add[0] = -linf;
add[n+1] = -linf;
prv[0] = 0, prv[n+1] = n, nxt[0] = 1, nxt[n+1] = n+1;
int index = n + 1;
for(int i = 1; i <= (n+1)/2; i++){
while(used[(*(prev(st.end()))).sc]) st.erase(prev(st.end()));
pair <ll,int> mx = *(prev(st.end()));
rez += add[mx.sc];
used[mx.sc] = 1;
used[prv[mx.sc]] = 1;
used[nxt[mx.sc]] = 1;
index ++;
prv[nxt[nxt[mx.sc]]] = index;
nxt[prv[prv[mx.sc]]] = index;
add[index] = add[prv[mx.sc]] + add[nxt[mx.sc]] - add[mx.sc];
prv[index] = prv[prv[mx.sc]];
nxt[index] = nxt[nxt[mx.sc]];
st.insert({add[index],index});
cout << rez << '\n';
}
cout << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
640 KB |
Output is correct |
2 |
Correct |
6 ms |
640 KB |
Output is correct |
3 |
Correct |
6 ms |
640 KB |
Output is correct |
4 |
Correct |
7 ms |
640 KB |
Output is correct |
5 |
Correct |
6 ms |
640 KB |
Output is correct |
6 |
Correct |
6 ms |
640 KB |
Output is correct |
7 |
Correct |
6 ms |
640 KB |
Output is correct |
8 |
Correct |
6 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
6 ms |
640 KB |
Output is correct |
12 |
Correct |
6 ms |
640 KB |
Output is correct |
13 |
Correct |
6 ms |
640 KB |
Output is correct |
14 |
Correct |
7 ms |
640 KB |
Output is correct |
15 |
Correct |
6 ms |
640 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
5 ms |
640 KB |
Output is correct |
18 |
Correct |
6 ms |
640 KB |
Output is correct |
19 |
Correct |
6 ms |
640 KB |
Output is correct |
20 |
Correct |
6 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
640 KB |
Output is correct |
2 |
Correct |
6 ms |
640 KB |
Output is correct |
3 |
Correct |
6 ms |
640 KB |
Output is correct |
4 |
Correct |
7 ms |
640 KB |
Output is correct |
5 |
Correct |
6 ms |
640 KB |
Output is correct |
6 |
Correct |
6 ms |
640 KB |
Output is correct |
7 |
Correct |
6 ms |
640 KB |
Output is correct |
8 |
Correct |
6 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
6 ms |
640 KB |
Output is correct |
12 |
Correct |
6 ms |
640 KB |
Output is correct |
13 |
Correct |
6 ms |
640 KB |
Output is correct |
14 |
Correct |
7 ms |
640 KB |
Output is correct |
15 |
Correct |
6 ms |
640 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
5 ms |
640 KB |
Output is correct |
18 |
Correct |
6 ms |
640 KB |
Output is correct |
19 |
Correct |
6 ms |
640 KB |
Output is correct |
20 |
Correct |
6 ms |
640 KB |
Output is correct |
21 |
Correct |
301 ms |
21776 KB |
Output is correct |
22 |
Correct |
306 ms |
21888 KB |
Output is correct |
23 |
Correct |
268 ms |
21856 KB |
Output is correct |
24 |
Correct |
144 ms |
21880 KB |
Output is correct |
25 |
Correct |
150 ms |
21820 KB |
Output is correct |
26 |
Correct |
148 ms |
21880 KB |
Output is correct |
27 |
Correct |
147 ms |
21752 KB |
Output is correct |
28 |
Correct |
149 ms |
21868 KB |
Output is correct |
29 |
Correct |
153 ms |
21880 KB |
Output is correct |
30 |
Correct |
167 ms |
21796 KB |
Output is correct |
31 |
Correct |
145 ms |
21880 KB |
Output is correct |
32 |
Correct |
150 ms |
21796 KB |
Output is correct |
33 |
Correct |
181 ms |
21880 KB |
Output is correct |
34 |
Correct |
192 ms |
21860 KB |
Output is correct |
35 |
Correct |
190 ms |
21880 KB |
Output is correct |
36 |
Correct |
279 ms |
21836 KB |
Output is correct |
37 |
Correct |
280 ms |
21880 KB |
Output is correct |
38 |
Correct |
284 ms |
22008 KB |
Output is correct |
39 |
Correct |
135 ms |
21880 KB |
Output is correct |
40 |
Correct |
138 ms |
21880 KB |
Output is correct |
41 |
Correct |
137 ms |
21880 KB |
Output is correct |
42 |
Correct |
150 ms |
21876 KB |
Output is correct |
43 |
Correct |
146 ms |
21880 KB |
Output is correct |
44 |
Correct |
149 ms |
21880 KB |
Output is correct |
45 |
Correct |
158 ms |
21932 KB |
Output is correct |
46 |
Correct |
144 ms |
21884 KB |
Output is correct |
47 |
Correct |
150 ms |
21880 KB |
Output is correct |
48 |
Correct |
196 ms |
21900 KB |
Output is correct |
49 |
Correct |
205 ms |
21860 KB |
Output is correct |
50 |
Correct |
207 ms |
21880 KB |
Output is correct |