#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
//using u128 = __uint128_t;
//const int x[4] = {1, 0, -1, 0};
//const int y[4] = {0, -1, 0, 1};
const ll mod = 1e9 + 7, pr = 31;
const int mxn = 1e5 + 5, mxq = 1e5 + 5, sq = 500, mxv = 5e4 + 1;
//const int base = (1 <<18);
const ll inf = 1e9 + 5, neg = -69420, inf2 = 1e14;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// have fun!
//JOIIIIIIIIIIIIIII
int n;
ll a[mxn + 1];
int nxt[mxn + 1], last[mxn + 1];
void solve(){
cin >> n;
set<pair<ll, int>>s;
for(int i = 1; i <= n; i++){
cin >> a[i]; last[i] = i - 1; nxt[i] = i + 1; s.insert(mpp(a[i], i));
}
nxt[n] = 0;
ll ans = 0;
for(int i = 1; i <= (n + 1) / 2; i++){
auto it = s.rbegin(); ans += (*it).fi;
int pos = (*it).se;
s.erase(mpp(a[pos], pos));
if(last[pos])s.erase(mpp(a[last[pos]], last[pos]));
if(nxt[pos]){
s.erase(mpp(a[nxt[pos]], nxt[pos]));
}
if(last[pos] && nxt[pos]){
a[pos] = a[last[pos]] + a[nxt[pos]] - a[pos];
s.insert(mpp(a[pos], pos));
last[pos] = last[last[pos]]; nxt[pos] = nxt[nxt[pos]];
nxt[last[pos]] = pos; last[nxt[pos]] = pos;
}else{
nxt[last[pos]] = 0; last[nxt[pos]] = 0;
}
cout << ans << "\n";
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("DATA.inp", "r", stdin);
//freopen("DATA.out", "w", stdout);
int tt; tt = 1;
while(tt--){
solve();
}
return(0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |