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 MAX = 1e5 + 10 ;
int arr[MAX] ;
int n ;
vector< set<int> >adj(MAX) ;
vector< pair<int , int> >vp ;
int dfs(int node , int par)
{
int x = arr[node] ;
for(auto &child : adj[node])
{
if(child == par)
continue ;
x = max(x , dfs(child , node)) ;
}
return x ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n ;
for(int i = 1 ; i <= n ; ++i)
{
cin>>arr[i] ;
vp.emplace_back(arr[i] , i) ;
}
for(int i = 0 ; i < n-1 ; ++i)
{
int x , y ;
cin>>x>>y ;
adj[x].insert(y) ;
adj[y].insert(x) ;
}
sort(vp.begin() , vp.end()) ;
reverse(vp.begin() , vp.end()) ;
long long ans = 0 ;
for(auto &p : vp)
{
int node = p.second ;
for(auto &child : adj[node])
{
ans += arr[node] + dfs(child , node) ;
adj[child].erase(node) ;
}
}
return cout<<ans<<"\n" , 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... |