// In the name of God
// Hope is last to die
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define pb push_back
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())
const int maxn = 2e5 + 5;
const int inf = 1e9 + 7;
int n, prv[maxn], nxt[maxn];
ll val[maxn];
set<pll> st;
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
int x; cin >> x;
st.insert(mp(x, i));
val[i] = x;
prv[i] = i - 1;
nxt[i] = i + 1;
}
ll ans = 0;
while(st.size()){
ll v = (*st.rbegin()).F, i = (*st.rbegin()).S;
ans += v;
cout << ans << '\n';
st.erase(mp(v, i));
prv[nxt[i]] = prv[i];
nxt[prv[i]] = nxt[i];
if(st.size() == 0) break;
if(nxt[i] == n + 1){
st.erase(mp(val[prv[i]], prv[i]));
nxt[prv[prv[i]]] = nxt[i];
}
else if(prv[i] == 0){
st.erase(mp(val[nxt[i]], nxt[i]));
prv[nxt[nxt[i]]] = prv[i];
}
else{
st.erase(mp(val[prv[i]], prv[i]));
st.erase(mp(val[nxt[i]], nxt[i]));
nxt[prv[prv[i]]] = i;
prv[nxt[nxt[i]]] = i;
val[i] = val[prv[i]] + val[nxt[i]] - val[i];
nxt[i] = nxt[nxt[i]];
prv[i] = prv[prv[i]];
st.insert(mp(val[i], i));
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2904 KB |
Output is correct |
6 |
Correct |
2 ms |
2528 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2648 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2648 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2904 KB |
Output is correct |
6 |
Correct |
2 ms |
2528 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2648 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2648 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
211 ms |
19356 KB |
Output is correct |
22 |
Correct |
213 ms |
19308 KB |
Output is correct |
23 |
Correct |
218 ms |
19284 KB |
Output is correct |
24 |
Correct |
98 ms |
19120 KB |
Output is correct |
25 |
Correct |
110 ms |
19276 KB |
Output is correct |
26 |
Correct |
100 ms |
19280 KB |
Output is correct |
27 |
Correct |
111 ms |
19520 KB |
Output is correct |
28 |
Correct |
111 ms |
19440 KB |
Output is correct |
29 |
Correct |
108 ms |
19536 KB |
Output is correct |
30 |
Correct |
116 ms |
19428 KB |
Output is correct |
31 |
Correct |
114 ms |
19536 KB |
Output is correct |
32 |
Correct |
112 ms |
19344 KB |
Output is correct |
33 |
Correct |
163 ms |
19152 KB |
Output is correct |
34 |
Correct |
158 ms |
19128 KB |
Output is correct |
35 |
Correct |
164 ms |
19284 KB |
Output is correct |
36 |
Correct |
212 ms |
19284 KB |
Output is correct |
37 |
Correct |
211 ms |
19324 KB |
Output is correct |
38 |
Correct |
206 ms |
19280 KB |
Output is correct |
39 |
Correct |
98 ms |
19288 KB |
Output is correct |
40 |
Correct |
102 ms |
19284 KB |
Output is correct |
41 |
Correct |
98 ms |
19240 KB |
Output is correct |
42 |
Correct |
114 ms |
19280 KB |
Output is correct |
43 |
Correct |
109 ms |
19368 KB |
Output is correct |
44 |
Correct |
114 ms |
19280 KB |
Output is correct |
45 |
Correct |
114 ms |
19284 KB |
Output is correct |
46 |
Correct |
132 ms |
19380 KB |
Output is correct |
47 |
Correct |
116 ms |
19316 KB |
Output is correct |
48 |
Correct |
174 ms |
19208 KB |
Output is correct |
49 |
Correct |
168 ms |
19280 KB |
Output is correct |
50 |
Correct |
173 ms |
19104 KB |
Output is correct |