The meaning of problems
Choose from a tree a communication block, such that As large as possible, in as large basis, let as large as possible
answer
We consider, apparently reverse platoon collection, if you have chosen
a, the first to choose
。
Compare the size, because
Order
, you will find
, which is known conditions, only a selected that is optimal.
but
be the better, so that a plurality of maximum value selected from a set of requirements.
Seeking to
is a root node, the maximum value of this must take down the point (same set)
clearly has:
Get the maximum sum, pitted again , each time the same as the maximum and that, As the current is equal to negative infinity, then backtracking. That can be guaranteed not to overlap.
#include<bits/stdc++.h>
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define sf(x) scanf("%d",&x)
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 405000;
int n,A[maxn];
vector<int>G[maxn];
ll dp[maxn],ans=-inf,k;
void dfs1(int u,int f){
dp[u]=A[u];
for(auto v:G[u]){
if(v==f)continue;
dfs1(v,u);
dp[u]+=max(dp[v],1ll*0);
}
ans=max(ans,dp[u]);
}
void dfs2(int u,int f){
dp[u]=A[u];
for(auto v:G[u]){
if(v==f)continue;
dfs2(v,u);
dp[u]+=max(dp[v],1ll*0);
}
if(dp[u]==ans){
k++;
dp[u]=-inf;
}
}
int main(){
cin>>n;
FOR(i,1,n)dp[i]=-inf;
FOR(i,1,n)scanf("%d",&A[i]);
FOR(i,1,n-1){
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs1(1,-1);
dfs2(1,-1);
cout<<ans*k<<" "<<k<<endl;
}