题目链接:https://codeforces.com/contest/1088/problem/E
题目大意:给一棵树,需要找k个连通块,使得其中所有权值相加除以k最大,如果有多种可能最大就使得k尽可能大
题目思路:因为前提是使得权值和尽可能大..所以...各个块的权值一定相等..不然有人拖后腿的话就肯定是不要那个拖后腿的会比较大..太机智了...........
以下是代码:
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
const int MAXN = 3e5+5;
ll a[MAXN],dp[MAXN],ans,tnt;
int n,x,y;
vector<int>g[MAXN];
void dfs(int u,int fa){
int len=g[u].size();
rep(i,0,len-1){
if(g[u][i]==fa)continue;
dfs(g[u][i],u);
dp[u]+=max(0ll,dp[g[u][i]]);
}
ans=max(dp[u],ans);
}
void dfs2(int u,int fa){
int len=g[u].size();
rep(i,0,len-1){
if(g[u][i]==fa)continue;
dfs2(g[u][i],u);
dp[u]+=max(0ll,dp[g[u][i]]);
}
if(dp[u]==ans){
dp[u]=0;
tnt++;
}
}
int main(){
while(~scanf("%d",&n)){
ans=-1e18;
tnt=0;
rep(i,1,n){
scanf("%I64d",&a[i]);
dp[i]=a[i];
g[i].clear();
}
rep(i,1,n-1){
scanf("%d%d",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1,-1);
rep(i,1,n)dp[i]=a[i];
dfs2(1,-1);
printf("%I64d %I64d\n",tnt*ans,tnt);
}
return 0;
}