This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 998244353;
const ll LOGN = 18;
const ll INF = 1e15;
const ll MAXN = 1e5 + 5;
vector<pair<ll,pair<int,int>>> edge;
ll t[MAXN];
int par[MAXN], sz[MAXN], mx[MAXN];
int find(int node) {
if (node == par[node])
return node;
return par[node] = find(par[node]);
}
void unite(int a, int b) {
a = find(a);
b = find(b);
if (sz[a] < sz[b])
swap(a, b);
mx[a] = max(mx[a], mx[b]);
par[b] = a;
sz[a] += sz[b];
}
int main() {
fast
int n, a, b;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> t[i];
for (int i = 1; i <= n; i++)
par[i] = i, sz[i] = 1, mx[i] = t[i];
for (int i = 1; i < n; i++) {
cin >> a >> b;
edge.push_back({max(t[a], t[b]), {a, b}});
}
sort(edge.begin(), edge.end());
ll ans = 0;
for (auto u : edge) {
int x = find(u.s.f);
int y = find(u.s.s);
ans += mx[x] + mx[y];
unite(x, y);
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |