#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
int L[MAXN], R[MAXN];
long long V[MAXN];
struct comp {
bool operator () (int x, int y) const {
return V[x] > V[y] ||
(V[x] == V[y] && x > y);
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
int N; cin >> N;
set<int, comp> S;
for (int i = 1; i <= N; i++) {
cin >> V[i];
L[i] = R[i] = i;
S.insert(i);
}
long long res = 0;
while (S.size()) {
auto it = S.begin();
S.erase(it);
int l = *it, r = R[*it];
res += V[l];
if (S.count(r + 1) &&
S.count(L[l - 1])) {
S.erase(r + 1);
V[l] -= V[r + 1];
r = R[r + 1];
S.erase(L[l - 1]);
V[L[l - 1]] -= V[l];
l = L[l - 1];
L[r] = l; R[l] = r;
S.insert(l);
} else {
S.erase(r + 1);
S.erase(L[l - 1]);
}
cout << res << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
3 ms |
532 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
2 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
3 ms |
532 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
2 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
460 KB |
Output is correct |
21 |
Correct |
440 ms |
16156 KB |
Output is correct |
22 |
Correct |
478 ms |
16312 KB |
Output is correct |
23 |
Correct |
468 ms |
16228 KB |
Output is correct |
24 |
Correct |
193 ms |
16068 KB |
Output is correct |
25 |
Correct |
194 ms |
16068 KB |
Output is correct |
26 |
Correct |
198 ms |
16056 KB |
Output is correct |
27 |
Correct |
209 ms |
16228 KB |
Output is correct |
28 |
Correct |
202 ms |
16228 KB |
Output is correct |
29 |
Correct |
206 ms |
16200 KB |
Output is correct |
30 |
Correct |
216 ms |
16240 KB |
Output is correct |
31 |
Correct |
224 ms |
16216 KB |
Output is correct |
32 |
Correct |
223 ms |
16196 KB |
Output is correct |
33 |
Correct |
347 ms |
16032 KB |
Output is correct |
34 |
Correct |
340 ms |
15956 KB |
Output is correct |
35 |
Correct |
312 ms |
15980 KB |
Output is correct |
36 |
Correct |
485 ms |
16164 KB |
Output is correct |
37 |
Correct |
423 ms |
16176 KB |
Output is correct |
38 |
Correct |
486 ms |
16200 KB |
Output is correct |
39 |
Correct |
192 ms |
16016 KB |
Output is correct |
40 |
Correct |
188 ms |
16068 KB |
Output is correct |
41 |
Correct |
212 ms |
16068 KB |
Output is correct |
42 |
Correct |
202 ms |
16260 KB |
Output is correct |
43 |
Correct |
203 ms |
16240 KB |
Output is correct |
44 |
Correct |
207 ms |
16176 KB |
Output is correct |
45 |
Correct |
213 ms |
16160 KB |
Output is correct |
46 |
Correct |
222 ms |
16304 KB |
Output is correct |
47 |
Correct |
216 ms |
16196 KB |
Output is correct |
48 |
Correct |
326 ms |
16024 KB |
Output is correct |
49 |
Correct |
329 ms |
16020 KB |
Output is correct |
50 |
Correct |
331 ms |
16024 KB |
Output is correct |