题干:

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

3
1 3 3
1 2 2
1 3 2

Output

3

Input

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

Output

7

Note

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

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

题目大意:

每个点有权值,每个边也有权值,你可以任选一条路径使得 路径上的点的点权和减去边权和 最大,答案也可以只为一个点,输出这个最大值。

解题报告:

  树形dp,遍历每个点的时候维护最大值和次大值,返回那个最大值然后答案可以从它本身,他和他的一棵子树,他和他的两棵子树,分别转移过来。(注意因为题干中权值肯定是正数,所以可以直接tmp1和tmp2都赋值为0,这样就不用更新三次maxx了。。)

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 3e5 + 5;
typedef pair<int,ll> PIL;
vector<PIL> vv[MAX];
ll a[MAX];
ll dp[MAX];
ll maxx = -123123123123;
ll dfs(int cur,int rt) {
	int up = vv[cur].size();
	ll res=a[cur],tmp1=-123123123123,tmp2=-123123123123;
	for(int i = 0; i<up; i++) {
		PIL v = vv[cur][i];
		if(v.first == rt) continue;
		ll tmp = dfs(v.first,cur) - v.second;
		if(tmp >= tmp1) {
			tmp2 = tmp1;
			tmp1 = tmp;
		}
		else if(tmp >= tmp2) {
			tmp2 = tmp;
		}
		res = max(res,tmp + a[cur]);
	}
	maxx = max(maxx,tmp1+tmp2+a[cur]);
	maxx = max(maxx,tmp1+a[cur]);
	maxx = max(maxx,a[cur]);
	return res;
}
int main()
{
	int n;
	cin>>n;
	for(int i = 1; i<=n; i++) scanf("%lld",a+i);
	for(int u,v,i = 1; i<=n-1; i++) {
		ll c;
		scanf("%d%d%lld",&u,&v,&c);
		vv[u].pb(pm(v,c));
		vv[v].pb(pm(u,c)); 
	}
	dfs(1,0);
	printf("%lld\n",maxx);
	return 0 ;
}
/*
2
2 2
1 2 1
*/