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>
#define pb push_back
#define mp make_pair
#define int long long
using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9+7;
const int inf = 1e18 + 10;
vector<int> adj[N];
int par[N], mx[N];
int find(int a){
if(a == par[a]) return a;
return par[a] = find(par[a]);
}
void merge(int a, int b){
a = find(a), b = find(b);
par[b] = a;
mx[a] = max(mx[a], mx[b]);
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
vector<pair<int, int> > val(n);
for(int i = 0; i < n; i++){
cin>>val[i].first;
val[i].second = i+1;
mx[i+1] = val[i].first;
par[i+1] = i+1;
}
for(int i = 0; i < n-1; i++){
int a, b;
cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
sort(val.begin(), val.end());
vector<int> vis(N);
int ans = 0;
for(auto itr: val){
int node = itr.second;
for(auto v: adj[node]){
if(vis[v] && find(v) != find(node)){
ans += (mx[find(node)] + mx[find(v)]);
merge(v, node);
}
}
vis[node] = 1;
}
cout<<ans<<endl;
return 0;
}
# | 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... |