// 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 = 2e5+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]));
nxt[pre[nxt[nxt[j]]] = 0] = nxt[nxt[j]];
}
else if (nxt[j] == n+1) {
st.erase(mp(a[pre[j]], pre[j]));
pre[nxt[pre[pre[j]]] = n+1] = pre[pre[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 |
2 ms |
4700 KB |
Output is correct |
2 |
Correct |
2 ms |
4700 KB |
Output is correct |
3 |
Correct |
2 ms |
4700 KB |
Output is correct |
4 |
Correct |
2 ms |
4700 KB |
Output is correct |
5 |
Correct |
2 ms |
4700 KB |
Output is correct |
6 |
Correct |
2 ms |
4700 KB |
Output is correct |
7 |
Correct |
2 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
2 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
2 ms |
4696 KB |
Output is correct |
12 |
Correct |
2 ms |
4696 KB |
Output is correct |
13 |
Correct |
2 ms |
4700 KB |
Output is correct |
14 |
Correct |
2 ms |
4696 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
16 |
Correct |
2 ms |
4700 KB |
Output is correct |
17 |
Correct |
2 ms |
4700 KB |
Output is correct |
18 |
Correct |
2 ms |
4696 KB |
Output is correct |
19 |
Correct |
2 ms |
4852 KB |
Output is correct |
20 |
Correct |
2 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4700 KB |
Output is correct |
2 |
Correct |
2 ms |
4700 KB |
Output is correct |
3 |
Correct |
2 ms |
4700 KB |
Output is correct |
4 |
Correct |
2 ms |
4700 KB |
Output is correct |
5 |
Correct |
2 ms |
4700 KB |
Output is correct |
6 |
Correct |
2 ms |
4700 KB |
Output is correct |
7 |
Correct |
2 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
2 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
2 ms |
4696 KB |
Output is correct |
12 |
Correct |
2 ms |
4696 KB |
Output is correct |
13 |
Correct |
2 ms |
4700 KB |
Output is correct |
14 |
Correct |
2 ms |
4696 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
16 |
Correct |
2 ms |
4700 KB |
Output is correct |
17 |
Correct |
2 ms |
4700 KB |
Output is correct |
18 |
Correct |
2 ms |
4696 KB |
Output is correct |
19 |
Correct |
2 ms |
4852 KB |
Output is correct |
20 |
Correct |
2 ms |
4700 KB |
Output is correct |
21 |
Correct |
232 ms |
19792 KB |
Output is correct |
22 |
Correct |
229 ms |
20816 KB |
Output is correct |
23 |
Correct |
218 ms |
20792 KB |
Output is correct |
24 |
Correct |
97 ms |
20568 KB |
Output is correct |
25 |
Correct |
98 ms |
20560 KB |
Output is correct |
26 |
Correct |
103 ms |
20472 KB |
Output is correct |
27 |
Correct |
108 ms |
20700 KB |
Output is correct |
28 |
Correct |
110 ms |
20820 KB |
Output is correct |
29 |
Correct |
111 ms |
20896 KB |
Output is correct |
30 |
Correct |
117 ms |
20808 KB |
Output is correct |
31 |
Correct |
119 ms |
20816 KB |
Output is correct |
32 |
Correct |
128 ms |
20796 KB |
Output is correct |
33 |
Correct |
172 ms |
20560 KB |
Output is correct |
34 |
Correct |
173 ms |
20636 KB |
Output is correct |
35 |
Correct |
181 ms |
20564 KB |
Output is correct |
36 |
Correct |
206 ms |
20924 KB |
Output is correct |
37 |
Correct |
205 ms |
20820 KB |
Output is correct |
38 |
Correct |
215 ms |
20908 KB |
Output is correct |
39 |
Correct |
98 ms |
20464 KB |
Output is correct |
40 |
Correct |
96 ms |
20620 KB |
Output is correct |
41 |
Correct |
100 ms |
20516 KB |
Output is correct |
42 |
Correct |
111 ms |
20820 KB |
Output is correct |
43 |
Correct |
114 ms |
20820 KB |
Output is correct |
44 |
Correct |
113 ms |
20820 KB |
Output is correct |
45 |
Correct |
115 ms |
20908 KB |
Output is correct |
46 |
Correct |
119 ms |
20736 KB |
Output is correct |
47 |
Correct |
129 ms |
20788 KB |
Output is correct |
48 |
Correct |
175 ms |
20560 KB |
Output is correct |
49 |
Correct |
188 ms |
20660 KB |
Output is correct |
50 |
Correct |
180 ms |
20676 KB |
Output is correct |