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 int long long
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,m,q;
cin >> n >> m >> q;
vector<int> arr(n+1);
for(int i=1;i<=n;i++)cin>>arr[i];
vector<vector<int>> adj(n+1);
for(int i=1;i<n;i++){
int a,b;
cin >> a >> b;
adj[a].emplace_back(b);
adj[b].emplace_back(a);
}
int ans = 0;
int currmax = 0;
function<void(int,int,int)> dfs = [&](int x,int p,int curr){
ans+=arr[p];
curr+=arr[p];
curr-=arr[x];
currmax=max(currmax,-curr);
for(int&i:adj[x])if(i!=p)dfs(i,x,curr);
};
dfs(1,0,arr[1]);
cout << ans+currmax << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |