# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
793078 | 1075508020060209tc | Sjekira (COCI20_sjekira) | C++14 | 82 ms | 20328 KiB |
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
int n;
int ar[500005];
vector<int>e[500005];
int uf[500005];
int umx[500005];
int fin(int x){
if(uf[x]==x){return x;}
uf[x]=fin(uf[x]);
return uf[x];
}
void mrg(int a,int b){
int pa=fin(a);int pb=fin(b);
if(pa==pb){return;}
uf[pa]=pb;
umx[pb]=max(umx[pa],umx[pb]);
}
signed main() {
cin>>n;
for(int i=1;i<=n;i++){
cin>>ar[i];
uf[i]=i;
umx[i]=ar[i];
}
for(int i=1;i<=n-1;i++){
int a;int b;
cin>>a>>b;
e[a].push_back(b);
e[b].push_back(a);
}
int ans=0;
vector<pair<int,int>>seq;
for(int i=1;i<=n;i++){
seq.push_back({ar[i],i});
}
//cout<<"hihi";
sort(seq.begin(),seq.end());
for(int i=0;i<seq.size();i++){
int nw=seq[i].second;
for(int j=0;j<e[nw].size();j++){
int v=e[nw][j];
if(fin(v)==fin(nw)){continue;}
ans+=umx[fin(v)]+umx[fin(nw)];
mrg(v,nw);
}
}
cout<<ans<<endl;
return 0;
}
Compilation message (stderr)
# | 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... |