Codeforces Round #526 (Div. 2) ------ D. The Fair Nut and the Best Path

D. The Fair Nut and the Best Path

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The Fair Nut is going to travel to the Tree Country, in which there are nn cities. Most of the land of this country is covered by forest. Furthermore, the local road system forms a tree (connected graph without cycles). Nut wants to rent a car in the city uu and go by a simple path to city vv. He hasn't determined the path, so it's time to do it. Note that chosen path can consist of only one vertex.

A filling station is located in every city. Because of strange law, Nut can buy only wiwi liters of gasoline in the ii-th city. We can assume, that he has infinite money. Each road has a length, and as soon as Nut drives through this road, the amount of gasoline decreases by length. Of course, Nut can't choose a path, which consists of roads, where he runs out of gasoline. He can buy gasoline in every visited city, even in the first and the last.

He also wants to find the maximum amount of gasoline that he can have at the end of the path. Help him: count it.

Input

The first line contains a single integer nn (1≤n≤3⋅1051≤n≤3⋅105) — the number of cities.

The second line contains nn integers w1,w2,…,wnw1,w2,…,wn (0≤wi≤1090≤wi≤109) — the maximum amounts of liters of gasoline that Nut can buy in cities.

Each of the next n−1n−1 lines describes road and contains three integers uu, vv, cc (1≤u,v≤n1≤u,v≤n, 1≤c≤1091≤c≤109, u≠vu≠v), where uu and vv — cities that are connected by this road and cc — its length.

It is guaranteed that graph of road connectivity is a tree.

Output

Print one number — the maximum amount of gasoline that he can have at the end of the path.

Examples

input

Copy

3
1 3 3
1 2 2
1 3 2

output

Copy

3

input

Copy

5
6 3 2 5 0
1 2 10
2 3 3
2 4 1
1 5 1

output

Copy

7

Note

The optimal way in the first example is 2→1→32→1→3.

Codeforces Round #526 (Div. 2) ------ D. The Fair Nut and the Best Path_第1张图片

The optimal way in the second example is 2→42→4.

Codeforces Round #526 (Div. 2) ------ D. The Fair Nut and the Best Path_第2张图片

 

无根树转有根树,然后就是树的dp,思路参考 树的最远对、或树的直径

原题链接:https://codeforces.com/contest/1084/problem/D

#include 
#include 
#include 
#include 
#include 
#include 
#define _for(i,a,b) for(int i=a;i=b;i--)
#define mset(a,val,n) for(int i=0;i G[300005];

LL dfs(int i){
    if(vis[i])return d[i];
    vis[i]=1;
    LL _max=0,_cmax=0;
    //get j
    int len=G[i].size();
    _for(k,0,len){
        int j=G[i][k]/t,c=G[i][k]%t;
        if(vis[j])continue;
        LL next=max((LL)0,dfs(j)-c);
        _cmax=max(_cmax,next);
        if(_cmax>_max)swap(_cmax,_max);
    }
    ans=max(ans,_max+_cmax+w[i]);
    return d[i]=_max+w[i];
}
int main(){
    RI(n);int lw=0;
    _for(i,1,n+1){RI(w[i]);d[i]=w[i];}
    _for(k,0,n-1){
        int a,b,c;
        RI(a);RI(b);RI(c);lw=c;
        G[a].push_back(b*t+c);
        G[b].push_back(a*t+c);
    }
    dfs(1);
    cout<

 

你可能感兴趣的:(树,动态规划,ACM,算法,数据结构)