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;
typedef long long ll;
typedef pair<ll,ll> ii;
int p[100010];
int mx[100010];
int val[100010];
vector<ii> v;
bool comp(ii a,ii b){
ll x=max(val[a.first],val[a.second]);
ll y=max(val[b.first],val[b.second]);
return x<y;
}
int findset(int x){
if(x==p[x])return x;
return p[x]=findset(p[x]);
}
ll ans=0;
void unionset(int i,int j){
i=findset(i);
j=findset(j);
if(i!=j){
ans+=mx[i]+mx[j];
mx[j]=max(mx[i],mx[j]);
p[i]=j;
}
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>val[i];
mx[i]=val[i];
p[i]=i;
}
int a,b;
for(int i=1;i<n;i++){
cin>>a>>b;
v.push_back(ii(a,b));
}
sort(v.begin(),v.end(),comp);
for(int i=0;i<n-1;i++){
unionset(v[i].first,v[i].second);
}
cout<<ans<<endl;
}
# | 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... |