Submission #488047

#TimeUsernameProblemLanguageResultExecution timeMemory
488047inksamuraiSjekira (COCI20_sjekira)C++17
40 / 110
1034 ms21268 KiB
#include <bits/stdc++.h> #define fi first #define se second #define pb push_back #define sz(a) (int)a.size() #define all(a) a.begin(),a.end() #define rep(i,n) for(int i=0;i<n;i++) #define crep(i,x,n) for(int i=x;i<n;i++) #define drep(i,n) for(int i=n-1;i>=0;i--) #define vec(...) vector<__VA_ARGS__> #define _3HaBFkZ ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) using namespace std; typedef long long ll; typedef long double ld; using pii=pair<int,int>; using vi=vector<int>; struct dsu{ int n,size_; vector<int> par,siz,maxima; dsu(int m,vi a){ init(m,a); } void init(int m,vi a){ n=m; size_=m; par.resize(n,0); siz.resize(n,0); maxima.resize(n,0); for(int i=0;i<n;i++){ par[i]=i; siz[i]=1; maxima[i]=a[i]; } } void merge(int x,int y,int type=0){ int a=parent(x),b=parent(y); if(a==b) return; size_--; if(siz[a]>siz[b]) { siz[a]+=siz[b]; par[b]=a; maxima[a]=max(maxima[a],maxima[b]); }else{ siz[b]+=siz[a]; par[a]=b; maxima[b]=max(maxima[b],maxima[a]); } } int parent(int x){ return par[x]==x?x:parent(par[x]); } bool same(int x, int y){ return parent(x)==parent(y); } int size(){ return size_; } }; int main(){ _3HaBFkZ; int n; cin>>n; vi a(n); rep(i,n){ cin>>a[i]; } vec(set<int>) adj(n); rep(i,n-1){ int u,v; cin>>u>>v; u--,v--; adj[u].insert(v); adj[v].insert(u); } vec(pii) rbts; rep(i,n){ rbts.pb({a[i],i}); } sort(all(rbts)); reverse(all(rbts)); auto dfs=[&](auto self,int v,int par)->ll{ ll cost=a[v]; for(auto u : adj[v]){ if(u!=par){ cost=max(cost,self(self,u,v)); } } return cost; }; ll ans=0; rep(i,sz(rbts)){ int v=rbts[i].se; ll val=rbts[i].fi; for(auto u : adj[v]){ adj[u].erase(v); } for(auto u : adj[v]){ ll cost=dfs(dfs,u,v); ans+=val+cost; } } 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...