#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define ull unsigned ll
#define f first
#define s second
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) for (int i=(n-1); i>=0; --i)
#define REP1(i,n) FOR(i,1,n+1)
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define SQ(x) (x)*(x)
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#ifdef BALBIT
#define IOS()
#define bug(...) fprintf(stderr,"#%d (%s) = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define bug(...)
#endif
const int iinf = 1<<29;
const ll inf = 1ll<<60;
const ll mod = 1e9+7;
void GG(){cout<<"-1\n"; exit(0);}
ll mpow(ll a, ll n, ll mo = mod){ // a^n % mod
ll re=1;
while (n>0){
if (n&1) re = re*a %mo;
a = a*a %mo;
n>>=1;
}
return re;
}
ll inv (ll b, ll mo = mod){
if (b==1) return b;
return (mo-mo/b) * inv(mo%b) % mo;
}
const int maxn = 2e5+5;
ll val[maxn];
signed main(){
IOS();
int n; cin>>n;
set<int> alive;
priority_queue<pair<ll, ll> > pq;
REP(i,n) {
cin>>val[i];
alive.insert(i);
pq.push({val[i],i});
}
ll re = 0;
REP(cnt, (n+1)/2) {
while (val[pq.top().s] != pq.top().f) {
pq.pop();
}
int at = pq.top().s; pq.pop();
if (at == *alive.begin() or at == *alive.rbegin()) {
ll x;
if (at!=*alive.begin()) x=*prev(alive.lower_bound(at));
else x = *alive.upper_bound(at);
re += val[at]; val[at]=val[x]=-inf;
alive.erase(at); alive.erase(x);
}else{
ll x = *prev(alive.lower_bound(at));
ll y = *alive.upper_bound(at);
alive.erase(x); alive.erase(y);
re += val[at]; val[at] = -val[at] + val[x] + val[y];
val[x] = val[y] = -inf;
pq.push({val[at],at});
}
cout<<re<<endl;
}
}
/*
5
3
5
1
7
6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
524 KB |
Output is correct |
2 |
Correct |
4 ms |
484 KB |
Output is correct |
3 |
Correct |
4 ms |
504 KB |
Output is correct |
4 |
Correct |
4 ms |
572 KB |
Output is correct |
5 |
Correct |
3 ms |
508 KB |
Output is correct |
6 |
Correct |
4 ms |
528 KB |
Output is correct |
7 |
Correct |
4 ms |
540 KB |
Output is correct |
8 |
Correct |
4 ms |
476 KB |
Output is correct |
9 |
Correct |
3 ms |
476 KB |
Output is correct |
10 |
Correct |
3 ms |
476 KB |
Output is correct |
11 |
Correct |
4 ms |
480 KB |
Output is correct |
12 |
Correct |
4 ms |
556 KB |
Output is correct |
13 |
Correct |
4 ms |
508 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
3 ms |
508 KB |
Output is correct |
17 |
Correct |
3 ms |
504 KB |
Output is correct |
18 |
Correct |
3 ms |
504 KB |
Output is correct |
19 |
Correct |
3 ms |
528 KB |
Output is correct |
20 |
Correct |
3 ms |
508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
524 KB |
Output is correct |
2 |
Correct |
4 ms |
484 KB |
Output is correct |
3 |
Correct |
4 ms |
504 KB |
Output is correct |
4 |
Correct |
4 ms |
572 KB |
Output is correct |
5 |
Correct |
3 ms |
508 KB |
Output is correct |
6 |
Correct |
4 ms |
528 KB |
Output is correct |
7 |
Correct |
4 ms |
540 KB |
Output is correct |
8 |
Correct |
4 ms |
476 KB |
Output is correct |
9 |
Correct |
3 ms |
476 KB |
Output is correct |
10 |
Correct |
3 ms |
476 KB |
Output is correct |
11 |
Correct |
4 ms |
480 KB |
Output is correct |
12 |
Correct |
4 ms |
556 KB |
Output is correct |
13 |
Correct |
4 ms |
508 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
3 ms |
508 KB |
Output is correct |
17 |
Correct |
3 ms |
504 KB |
Output is correct |
18 |
Correct |
3 ms |
504 KB |
Output is correct |
19 |
Correct |
3 ms |
528 KB |
Output is correct |
20 |
Correct |
3 ms |
508 KB |
Output is correct |
21 |
Correct |
347 ms |
17936 KB |
Output is correct |
22 |
Correct |
337 ms |
17940 KB |
Output is correct |
23 |
Correct |
340 ms |
17892 KB |
Output is correct |
24 |
Correct |
232 ms |
17680 KB |
Output is correct |
25 |
Correct |
232 ms |
17804 KB |
Output is correct |
26 |
Correct |
231 ms |
17768 KB |
Output is correct |
27 |
Correct |
258 ms |
17960 KB |
Output is correct |
28 |
Correct |
256 ms |
17896 KB |
Output is correct |
29 |
Correct |
256 ms |
17924 KB |
Output is correct |
30 |
Correct |
266 ms |
17968 KB |
Output is correct |
31 |
Correct |
272 ms |
17980 KB |
Output is correct |
32 |
Correct |
263 ms |
17884 KB |
Output is correct |
33 |
Correct |
292 ms |
17636 KB |
Output is correct |
34 |
Correct |
290 ms |
17640 KB |
Output is correct |
35 |
Correct |
286 ms |
17756 KB |
Output is correct |
36 |
Correct |
377 ms |
17856 KB |
Output is correct |
37 |
Correct |
375 ms |
17864 KB |
Output is correct |
38 |
Correct |
342 ms |
17940 KB |
Output is correct |
39 |
Correct |
231 ms |
17716 KB |
Output is correct |
40 |
Correct |
227 ms |
17756 KB |
Output is correct |
41 |
Correct |
232 ms |
17856 KB |
Output is correct |
42 |
Correct |
260 ms |
17980 KB |
Output is correct |
43 |
Correct |
267 ms |
17900 KB |
Output is correct |
44 |
Correct |
258 ms |
17880 KB |
Output is correct |
45 |
Correct |
266 ms |
17888 KB |
Output is correct |
46 |
Correct |
267 ms |
17884 KB |
Output is correct |
47 |
Correct |
269 ms |
17880 KB |
Output is correct |
48 |
Correct |
290 ms |
17640 KB |
Output is correct |
49 |
Correct |
289 ms |
17716 KB |
Output is correct |
50 |
Correct |
288 ms |
17640 KB |
Output is correct |