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 int long long
#define ff first
#define ss second
#define pb push_back
const int N=3e5+5;
int par[N],c[N],sbtr[N],mx[N];
int ans=0;
int find(int x){
if(x==par[x])return x;
return par[x]=find(par[x]);
}
void merge(int x, int y){
int px=find(x),py=find(y);
if(sbtr[px]<sbtr[py]){
swap(py,px);
}
ans+=mx[px]+mx[py];
sbtr[px]+=sbtr[py];
mx[px]=max(mx[px],mx[py]);
par[py]=px;
}
signed main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>c[i];
par[i]=i;
sbtr[i]=1;
mx[i]=c[i];
}
vector<pair<int,pair<int,int>>>vc;
for(int i=1;i<n;i++){
int a,b;
cin>>a>>b;
vc.pb({max(c[a],c[b]),{a,b}});
}
sort(vc.begin(),vc.end());
for(auto p:vc){
int a=p.ss.ff,b=p.ss.ss;
merge(a,b);
}
cout<<ans;
}
# | 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... |