// In the name of the God
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;
const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 1e5+5;
int n, a[maxn], pre[maxn], nxt[maxn];
set <pii, greater <pii> > st;
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
st.insert(mp(a[i], i));
pre[i] = i-1, nxt[i] = i+1;
}
nxt[0] = 1, pre[n+1] = n;
int ans = 0;
for (int i = 0; i < (n+1)/2; i++) {
int v = st.begin() -> F, j = st.begin() -> S;
ans += v;
cout << ans << '\n';
st.erase(st.begin());
if (pre[j] == 0) {
st.erase(mp(a[nxt[j]], nxt[j]));
pre[nxt[nxt[j]]] = 0;
nxt[0] = nxt[nxt[j]];
}
else if (nxt[j] == n+1) {
st.erase(mp(a[pre[j]], pre[j]));
pre[nxt[nxt[j]]] = 0;
nxt[0] = nxt[nxt[j]];
}
else {
st.erase(mp(a[pre[j]], pre[j])), st.erase(mp(a[nxt[j]], nxt[j]));
a[j] = a[pre[j]]+a[nxt[j]]-a[j];
nxt[pre[nxt[nxt[j]]] = j] = nxt[nxt[j]];
pre[nxt[pre[pre[j]]] = j] = pre[pre[j]];
st.insert(mp(a[j], j));
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2648 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2648 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2648 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2648 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |