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;
const int mxx=300+10;
typedef long long ll;
int inf=1e9+10;
const int mod=1e9+7;
int a[mxx];
ll ans=0;
pair<int,int>edge[mxx];
int n;int par[mxx];
int mx[mxx];
int siz[mxx];
vector<int>adj[mxx];
void init(){
for(int i=0;i<n;i++){
par[i]=i;
mx[i]=a[i];
siz[i]=1;
}
}
int fin(int node){
if(par[node]!=node){
return par[node]=fin(par[node]);
}else{
return node;
}
}
void unit(int a,int b){
int aa=fin(a);
int bb=fin(b);
if(siz[a]>siz[b]){
swap(a,b);
}
if(aa==bb){
return;
}
par[aa]=bb;
ans+=(mx[aa]+mx[bb]);
mx[bb]=max(mx[bb],mx[aa]);
return;
}
int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
vector<int>v;
for(int i=0;i<n-1;i++){
int x,y;
cin>>x>>y;
x--;y--;
adj[x].push_back(y);
adj[y].push_back(x);
edge[i]={x,y};
v.push_back(i);
}
init();
ll ans2=inf;
//cout<<fin(2);
unit(1,2);
//cout<<fin(2);
int c=-1;
while(next_permutation(v.begin(),v.end())){
ans=0;
init();
for(int i=v.size()-1;i>=0;i--){
unit(edge[v[i]].first,edge[v[i]].second);
}
if(c==-1){
c=1;
ans2=ans;
}else{
ans2=min(ans2,ans);}
}
cout<<ans2;
}
# | 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... |