제출 #445905

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