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 fi first
#define se second
using namespace std;
const int N=1e5;
int tab[N+10];
vector<pair<int,int>> e[N+10];
bool vis[N+10];
priority_queue<pair<int,int>> pq;
int dfs(int x,int p)
{
int ans=tab[x];
for(auto [v,id]:e[x])
{
if(v==p || vis[id])
continue;
ans=max(ans,dfs(v,x));
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>tab[i];
pq.push({tab[i],i});
}
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
e[a].emplace_back(b,i);
e[b].emplace_back(a,i);
}
long long ans=0;
while(!pq.empty())
{
int x=pq.top().se;
pq.pop();
for(auto [v,id]:e[x])
{
if(vis[id])
continue;
vis[id]=true;
ans+=tab[x]+dfs(v,x);
}
}
cout<<ans<<"\n";
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... |