#include <bits/stdc++.h>
#define rf(x) (x)=0;while(*p<48)p++;while(47<*p)(x)=((x)<<3)+((x)<<1)+(*p++&15);
#define pb push_back
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define befv(V) ((V)[(sz(V)-2)])
#define sorv(V) sort(allv(V))
#define revv(V) reverse(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define clv(V) (V).clear()
#define upmin(a,b) (a)=min((a),(b))
#define upmax(a,b) (a)=max((a),(b))
#define rb(x) ((x)&(-(x)))
#define cb(x) (x)=(!(x))
#define INF (0x3f3f3f3f)
#define INFLL (0x3f3f3f3f3f3f3f3fll)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
const int MAXN = 200005;
set<pli> PQ;
set<pil> CH;
ll A[MAXN];
ll Ans;
int N;
int main() {
ios::sync_with_stdio(false);
cin >> N;
for(int i = 1; i <= N; i++) cin >> A[i];
for(int i = 1; i <= N; i++) {
PQ.insert(pli(A[i], i));
CH.insert(pil(i, A[i]));
}
for(int i = 0; -N <= i; i--) {
PQ.insert(pli(-INF, i));
CH.insert(pil(i, -INF));
}
for(int i = N+1; i <= N*2; i++) {
PQ.insert(pli(-INF, i));
CH.insert(pil(i, -INF));
}
for(int tc = 1; tc <= (N+1)/2; tc++) {
ll dst; int idx;
{
auto it = prev(PQ.end());
tie(dst, idx) = *it;
}
Ans += dst;
ll a = 0, b = dst, c = 0;
int ai = -1, bi = idx, ci = -1;
{
auto it = CH.lower_bound(pil(idx, dst));
{
if(CH.begin() != it) {
auto bt = prev(it);
tie(ai, a) = *bt;
}
}
{
auto bt = next(it);
if(CH.end() != bt) {
tie(ci, c) = *bt;
}
}
}
if(0 < ai) CH.erase(pil(ai, a));
if(0 < bi) CH.erase(pil(bi, b));
if(0 < ci) CH.erase(pil(ci, c));
if(0 < ai) PQ.erase(pli(a, ai));
if(0 < bi) PQ.erase(pli(b, bi));
if(0 < ci) PQ.erase(pli(c, ci));
CH.insert(pil(bi, a+c-b));
PQ.insert(pli(a+c-b, bi));
printf("%lld\n", Ans);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
1144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
1144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |