제출 #364761

#제출 시각아이디문제언어결과실행 시간메모리
364761MohamedAhmed04Sjekira (COCI20_sjekira)C++14
40 / 110
1083 ms22376 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...