# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
368423 | 1306439119 | Traffic (IOI10_traffic) | C++11 | 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>
using namespace std;
#define ll long long
#define pi pair<int, int>
#define endl '\n'
#define f first
#define s second
void setIO(string name = "") {
ios_base::sync_with_stdio(0);
cin.tie(0);
if (name.length()) {
freopen((name+".in").c_str(), "r", stdin);
freopen((name+".out").c_str(), "w", stdout);
}
}
struct node{
int id, person;
vector<node*> adj;
node(int i, int p, vector<node*> a){
id = i;
person = p;
adj = a;
}
};
int n;
node* A[1000000];
ll dfs(node* curr, int prev){
ll sum = curr->person;
for(node* next : curr->adj){
if(prev != next->id){
sum += dfs(next, curr->id);
}
}
return sum;
}
ll root(node* root){
ll ret = 0;
for(node* next : root->adj){
ret = max(ret, dfs(next, root->id));
}
return ret;
}
int main(){
setIO("");
cin >> n;
for(int i = 0; i < n; i++){
int p; cin >> p;
A[i] = new node(i, p, vector<node*>());
}
for(int i = 1; i < n; i++){
int s, d; cin >> s >> d;
A[s]->adj.push_back(A[d]);
A[d]->adj.push_back(A[s]);
}
ll res = LLONG_MAX;
for(int i = 0; i < n; i++){
res = min(res, root(A[i]));
}
cout << res << endl;
return 0;
}