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,int)> dfs = [&](int x,int p,int curr,int curroffset){
ans+=arr[p];
curr+=arr[p];
curr-=arr[x];
if(curr<0){
curroffset-=curr;
currmax=max(currmax,curroffset);
curr = 0;
}
for(int&i:adj[x])if(i!=p)dfs(i,x,curr,curroffset);
};
dfs(1,0,arr[1],0);
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... |