#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ll long long
#define ld long double
#define all(_v) _v.begin(), _v.end()
#define sz(_v) (int)_v.size()
#define pii pair <int, int>
#define pll pair <ll, ll>
#define veci vector <int>
#define vecll vector <ll>
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
const int MAXN = 2e5 + 10;
int l[MAXN], r[MAXN], p[MAXN];
ll a[MAXN];
int n;
void solve() {
cin >> n;
set <pair <ll, int> > st;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
l[i] = i;
r[i] = i;
p[i] = i;
st.insert(mp(a[i], i));
}
r[n + 1] = n + 1;
ll ans = 0;
for(int cs = 1; cs <= (n + 1) / 2; ++cs) {
auto it = *st.rbegin();
st.erase(it);
ll val = it.f;
int i = it.s;
ans += val;
cout << ans << '\n';
if(l[i] - 1 > 0) st.erase(mp(a[p[l[i] - 1]], p[l[i] - 1]));
if(r[i] + 1 <= n) st.erase(mp(a[p[r[i] + 1]], p[r[i] + 1]));
int L = l[i], R = r[i];
if(p[L - 1] == L - 1) l[i] = L - 1;
else l[i] = l[p[L - 1]], a[i] -= a[p[L - 1]] - a[l[i]];
if(p[R + 1] == R + 1) r[i] = R + 1;
else r[i] = r[p[R + 1]], a[i] -= a[p[R + 1]] - a[r[i]];
p[l[i]] = p[r[i]] = p[l[i] + 1] = p[r[i] - 1] = i;
a[i] = a[l[i]] + a[r[i]] - a[i];
if(l[i] != 0 && r[i] != n + 1) st.insert(mp(a[i], i));
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int CNT_TESTS = 1;
///cin >> CNT_TESTS;
for(int NUMCASE = 1; NUMCASE <= CNT_TESTS; ++NUMCASE) {
solve();
if(NUMCASE != CNT_TESTS) cout << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |