#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
const int N = (int)(1e6) + 5;
int s[N];
int lf[N];
int rg[N];
ll lv[N];
ll rv[N];
int main(void)
{
ios_base :: sync_with_stdio(0);
int n;
cin>>n;
for (int i = 1;i <= n;++i)
cin>>s[i];
for (int i = 0;i <= n;++i) {
lf[i] = rg[i] = -1;
lv[i] = rv[i] = -1e16;
if (i > 0)
lf[i] = i - 1,lv[i] = s[i];
if (i < n)
rg[i] = i + 1,rv[i] = s[i + 1];
}
priority_queue < pair < ll , pii > > Q;
for (int i = 1;i <= n;++i)
Q.push(mp(s[i],mp(i - 1,i)));
ll answer = 0;
for (int cs = 1;cs * 2 <= n + 1;++cs) {
const int u = Q.top().se.fi;
const int v = Q.top().se.se;
const ll cost = Q.top().fi;
Q.pop();
if (lf[v] != u || rg[u] != v) {
--cs;
continue;
}
answer += cost;
int L = lf[u];
int R = rg[v];
lf[u] = rg[v] = -1;
if (L != -1 && R != -1) {
const ll new_cost = lv[u] + rv[v] - cost;
lf[R] = L;rg[L] = R;
rv[L] = lv[R] = new_cost;
Q.push(mp(new_cost,mp(L,R)));
}
cout << answer << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |