1. 程式人生 > >Codeforces Round #525 (Div. 2)—E. Ehab and a component choosing problem(樹形dp)

Codeforces Round #525 (Div. 2)—E. Ehab and a component choosing problem(樹形dp)

E. Ehab and a component choosing problem

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You're given a tree consisting of n

nodes. Every node u has a weight au. You want to choose an integer k (1≤k≤n) and then choose k connected components of nodes that don't overlap (i.e every node is in at most 1 component). Let the set of nodes you chose be s

. You want to maximize:

 

 

 

 

In other words, you want to maximize the sum of weights of nodes in s

divided by the number of connected components you chose. Also, if there are several solutions, you want to maximize k

.

Note that adjacent nodes can belong to different components. Refer to the third sample.

Input

The first line contains the integer n

(1≤n≤3⋅105)

, the number of nodes in the tree.

The second line contains n

space-separated integers a1, a2, …, an (|ai|≤109)

, the weights of the nodes.

The next n−1

lines, each contains 2 space-separated integers u and v (1≤u,v≤n) which means there's an edge between u and v

.

Output

Print the answer as a non-reduced fraction represented by 2 space-separated integers. The fraction itself should be maximized and if there are several possible ways, you should maximize the denominator. See the samples for a better understanding.

Examples

Input

Copy

3
1 2 3
1 2
1 3

Output

Copy

6 1

Input

Copy

1
-2

Output

Copy

-2 1

Input

Copy

3
-1 -1 -1
1 2
2 3

Output

Copy

-3 3

Input

Copy

3
-1 -2 -1
1 2
1 3

Output

Copy

-2 2

Note

A connected component is a set of nodes such that for any node in the set, you can reach all other nodes in the set passing only nodes in the set.

In the first sample, it's optimal to choose the whole tree.

In the second sample, you only have one choice (to choose node 1) because you can't choose 0 components.

In the third sample, notice that we could've, for example, chosen only node 1, or node 1 and node 3, or even the whole tree, and the fraction would've had the same value (-1), but we want to maximize k

.

In the fourth sample, we'll just choose nodes 1 and 3.

題意:就是找k個連通塊。聯通塊的代價就是節點的總和。求圖中公式的最大值。

連通塊不能有相交的地方。

思路:肯定是分母越大,分子越小這個值最大。那麼就是這個聯通塊的值最大,最大的,連通塊數目肯定小。看有幾個。

第一遍dfs,預處理出u節點為根的最大值。dp[u].

第二遍dfs2.當這個值為ans時,將其dp[v]置為0,這樣就不會有重疊了,數數幾個。

注意longlong

程式碼:

#include<bits/stdc++.h>
using namespace std;
const int maxn=300000+100;
const int inf=0x3f3f3f3f;
typedef long long LL;
int n;
LL ans;
int flag;
LL a[maxn],dp[maxn];
LL cnt;
vector<LL>vec[maxn];
void dfs(int u,int fa){
    dp[u]=a[u];
    int num=vec[u].size();
    for(int i=0;i<num;i++){
        int v=vec[u][i];
        if(v==fa)
            continue;
        dfs(v,u);
        if(dp[v]>0)
            dp[u]+=dp[v];
    }
    ans=max(ans,dp[u]);
}
void dfs2(int u,int fa){
      dp[u]=a[u];
    int num=vec[u].size();
    for(int i=0;i<num;i++){
        int v=vec[u][i];
        if(v==fa)
            continue;
        dfs2(v,u);
        if(dp[v]>0)
            dp[u]+=dp[v];
    }
    if(dp[u]==ans){
        dp[u]=0;
        cnt++;
    }
}
int main()
{
    LL u,v;
   while(scanf("%d",&n)!=EOF){
    memset(vec,0,sizeof(vec));
   memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
    }
    for(int i=1;i<=n-1;i++){
        scanf("%lld%lld",&u,&v);
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    ans=-inf;cnt=0;
    dfs(1,-1);
    dfs2(1,-1);
    long long pp=ans*cnt;
    cout<<pp<<" "<<cnt<<endl;
   }
}