/*
Author : DeMen100ns (a.k.a Vo Khac Trieu self-destruct)
School : VNU-HCM High school for the Gifted
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
/*Pragma*/
#pragma GCC optimize("Ofast")
//#pragma GCC optimize("O3")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC optimize("Os")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
/*Normal define*/
#define int long long
#define ll long long
//#define endl '\n'
#define gcd __gcd
#define ordered_set tree<ii, null_type,less<ii>, rb_tree_tag,tree_order_statistics_node_update>
//#define p prev
#define FastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define count1 __builtin_popcountll
/*For define*/
#define For(i,a,b) for(ll i=a;i<=b;i++)
#define foR(i,a,b) for(ll i=a;i>=b;i--)
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define forN(i,a,b,c) for(ll i=a;i<=b;i+=c)
#define foRN(i,a,b,c) for(ll i=a;i>=b;i-=c)
#define fora(i, a) for (auto i : a)
/*push-pop-pair define*/
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define x first
#define y second
/*Print define*/
#define write(a, l, r) for (ll ik = l; ik <= r; ik++) cout << a[ik] << ' '; cout << endl
#define writeln(a, l, r) for (ll ik = l; ik <= r; ik++) cout << a[ik] << endl
/*Type-def*/
typedef unsigned long long ull;
typedef vector<ll> vi;
typedef pair<ll, ll> ii;
typedef long double ld;
/* Calculation with MODULO*/
const int MOD = 1e9 + 7;
ll dec(ll x, ll y, int mod = MOD) {return (x-y+mod)%mod;}
ll add(ll x, ll y, int mod = MOD) {return (x + y) % mod;}
ll mul(ll x, ll y, int mod = MOD) {return ((x%mod) * (y%mod)) % mod;}
ll bpow(ll x, ll y, int mod = MOD) {ll res = 1; while (y) {if (y & 1) res = mul(res, x, mod); x = mul(x, x, mod); y >>= 1;} return res;}
ll ModInverse(ll x, int mod = MOD) {return bpow(x, mod - 2, mod);}
ll Div(ll x, ll y, int mod = MOD) {return mul(x, ModInverse(y, mod), mod);}
bool GetBit(int val, int num) {return ((val>>num) & 1);}
/*Some simple checking and calculation function*/
string yn(bool k){if (k) return "Yes"; else return "No";}
__int128 binpower(__int128 base, __int128 e, __int128 mod) {__int128 result = 1;base %= mod;while (e){if (e & 1)result = (__int128)result * base % mod;base = (__int128)base * base % mod;e >>= 1; }return result;}
bool check_composite(__int128 n, __int128 a, __int128 d, int s) {__int128 x = binpower(a, d, n); if (x == 1 or x == n - 1)return false;for (int r = 1; r < s; r++) {x = (__int128)x * x % n;if (x == n - 1)return false;}return true;};
bool checkprime(__int128 n) {if (n < 2)return false;int r = 0;__int128 d = n - 1;while ((d & 1) == 0) {d >>= 1;r++;}for (int a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {if (n == a)return true;if (check_composite(n, a, d, r))return false;}return true;}
bool checksqr(ll k){return (trunc(sqrt(k))*trunc(sqrt(k))==k);}
ll lcm(ll a, ll b){return (a*b)/gcd(a,b);}
ll sum(ll n) {return n*(n+1)/2;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1e6 + 5;
const int INF = 1e18 + 7;
const int B = sqrt(1e9 + 7) + 100;
int a[N], l[N], r[N], f[N];
set <ii, greater<ii>> s;
void solve(){
int n;
cin >> n;
For(i,1,n) cin >> a[i], s.insert({a[i], i}), l[i] = i - 1, r[i] = i + 1;
int ans = 0;
s.insert({-INF, 0}); a[0] = -INF; r[0] = 1;
s.insert({-INF, n + 1}); l[n + 1] = n; a[n + 1] = -INF;
For(i,1,(n + 1)/2){
auto it = s.begin();
ii state = *it;
int v = state.x, pos = state.y;
s.erase(it);
if (f[pos]){
i--; continue;
}
ans += v;
a[pos] = a[l[pos]] + a[r[pos]] - v;
f[l[pos]] = f[r[pos]] = true;
l[pos] = l[l[pos]]; r[pos] = r[r[pos]];
r[l[pos]] = pos; l[r[pos]] = pos;
s.insert({a[pos], pos});
cout << ans << endl;
//write(l,1,n);
//write(r,1,n);
}
}
signed main(){
FastIO
int t = 1; //cin >> t;
while(t--){
solve();
}
}
/*
//Delete define endl when interactive prolem
INPUT :
4 10
0 1 6 5
1 1 3 4
2 2 2 1
3 4 4
2 1 2 4
2 2 4 1
2 1 4 7
2 2 4 2
3 1 4
1 1 2 1
2 3 3 4
OUTPUT :
5
7
If it WA, check :
- Special case (Usually, n=1)
- WRONG FORMAT OUTPUT
- Check reading
- Change (ll) to (ull)
- lleger Overflow (The number that bigger than 2^63-1)
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
460 KB |
Output is correct |
2 |
Correct |
3 ms |
464 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
3 ms |
460 KB |
Output is correct |
5 |
Correct |
4 ms |
464 KB |
Output is correct |
6 |
Correct |
3 ms |
460 KB |
Output is correct |
7 |
Correct |
4 ms |
460 KB |
Output is correct |
8 |
Correct |
3 ms |
460 KB |
Output is correct |
9 |
Correct |
3 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
3 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
3 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
472 KB |
Output is correct |
17 |
Correct |
4 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
464 KB |
Output is correct |
19 |
Correct |
3 ms |
460 KB |
Output is correct |
20 |
Correct |
3 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
460 KB |
Output is correct |
2 |
Correct |
3 ms |
464 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
3 ms |
460 KB |
Output is correct |
5 |
Correct |
4 ms |
464 KB |
Output is correct |
6 |
Correct |
3 ms |
460 KB |
Output is correct |
7 |
Correct |
4 ms |
460 KB |
Output is correct |
8 |
Correct |
3 ms |
460 KB |
Output is correct |
9 |
Correct |
3 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
3 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
3 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
472 KB |
Output is correct |
17 |
Correct |
4 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
464 KB |
Output is correct |
19 |
Correct |
3 ms |
460 KB |
Output is correct |
20 |
Correct |
3 ms |
460 KB |
Output is correct |
21 |
Correct |
348 ms |
22560 KB |
Output is correct |
22 |
Correct |
422 ms |
22668 KB |
Output is correct |
23 |
Correct |
357 ms |
22560 KB |
Output is correct |
24 |
Correct |
246 ms |
22352 KB |
Output is correct |
25 |
Correct |
230 ms |
22328 KB |
Output is correct |
26 |
Correct |
248 ms |
22400 KB |
Output is correct |
27 |
Correct |
235 ms |
22476 KB |
Output is correct |
28 |
Correct |
306 ms |
22628 KB |
Output is correct |
29 |
Correct |
231 ms |
22568 KB |
Output is correct |
30 |
Correct |
250 ms |
22628 KB |
Output is correct |
31 |
Correct |
242 ms |
22464 KB |
Output is correct |
32 |
Correct |
242 ms |
22524 KB |
Output is correct |
33 |
Correct |
283 ms |
22260 KB |
Output is correct |
34 |
Correct |
295 ms |
22348 KB |
Output is correct |
35 |
Correct |
279 ms |
22320 KB |
Output is correct |
36 |
Correct |
440 ms |
22480 KB |
Output is correct |
37 |
Correct |
345 ms |
22544 KB |
Output is correct |
38 |
Correct |
351 ms |
22508 KB |
Output is correct |
39 |
Correct |
243 ms |
22420 KB |
Output is correct |
40 |
Correct |
296 ms |
22380 KB |
Output is correct |
41 |
Correct |
225 ms |
22388 KB |
Output is correct |
42 |
Correct |
262 ms |
22476 KB |
Output is correct |
43 |
Correct |
236 ms |
22536 KB |
Output is correct |
44 |
Correct |
244 ms |
22592 KB |
Output is correct |
45 |
Correct |
253 ms |
22604 KB |
Output is correct |
46 |
Correct |
242 ms |
22544 KB |
Output is correct |
47 |
Correct |
257 ms |
22468 KB |
Output is correct |
48 |
Correct |
281 ms |
22316 KB |
Output is correct |
49 |
Correct |
280 ms |
22340 KB |
Output is correct |
50 |
Correct |
284 ms |
22380 KB |
Output is correct |