-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3366.cpp
64 lines (58 loc) · 1.23 KB
/
3366.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using pii = std::pair<int,int>;
const int MAX = 1e6;
int parents[MAX];
pii go[MAX];
int find(int x) {
if (parents[x] == x) return x;
auto [l,r] = go[x];
auto [pl,pr] = go[parents[x]];
go[parents[x]] = go[x] = {
std::min(l, pl),
std::max(r, pr)
};
return parents[x] = find(parents[x]);
}
void merge(int x, int y) {
parents[find(x)] = parents[find(y)];
find(x);
}
void solve(){
int N; std::cin >> N;
pii arr[N];
pii arr2[N];
int arr3[N];
for (int i = 0; i < N; i++) {
std::cin >> arr[i].first;
arr[i].second = i;
arr3[i] = arr[i].first;
parents[i] = i;
go[i] = {std::max(0,i-1), std::min(N-1, i+1)};
}
sort(arr,arr+N);
long long result = 0;
for (int i = 0; i < N - 1; i++) {
auto [a,b] = arr[i];
int d = 2e9;
int index = -1;
auto [l,r] = go[find(b)];
if (d >= arr3[l] && find(b) != find(l)) {
index = find(l);
d = arr3[index];
}
if (d >= arr3[r] && find(b) != find(r)) {
index = find(r);
d = arr3[index];
}
d = std::max(a,d);
merge(b, index);
arr3[index] = arr3[b] = d;
result += d;
}
std::cout << result;
}
int main(){
std::cin.tie(0)->sync_with_stdio(false);
solve();
return 0;
}