# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
503220 | _AnKahng | Traffic (IOI10_traffic) | C++14 | 0 ms | 0 KiB |
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 fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int M = 1e6+5;
int n,p[M],id;
long long sum,f[M],res=LLONG_MAX;
vector<int> dsk[M];
void dfs(int u, int par){
long long mx = -1, o = p[u];
for(auto v : dsk[u]) if(v != par){
dfs(v,u);
mx = max(mx,f[v]);
o += f[v];
}
f[u] = o;
mx = max(mx,sum - o);
if(res > mx) res = mx, id = u;
}
int main()
{
cin >> n;
fo(i,1,n) cin >> p[i], sum = 0LL + sum + p[i];
fo(i,1,n-1){
int x,y;
cin >> x >> y;
x ++; y ++;
dsk[x].push_back(y);
dsk[y].push_back(x);
}
dfs(1,1);
cout << id - 1;
}