Kelukin is a businessman. Every day, he travels around cities to do some business. On August 17th, in memory of a great man, citizens will read a book named "the Man Who Changed China". Of course, Kelukin wouldn't miss this chance to make money, but he doesn't have this book. So he has to choose two city to buy and sell.
As we know, the price of this book was different in each city. It is ai yuan in it city. Kelukin will take taxi, whose price is 1yuan per km and this fare cannot be ignored.
There are n−1 roads connecting n
cities. Kelukin can choose any city to start his travel. He want to know the maximum money he can get.
Input
The first line contains an integer T
(1≤T≤10) , the number of test cases.
For each test case:
first line contains an integer n (2≤n≤100000) means the number of cities;
second line contains n numbers, the ith number means the prices in ith city; (1≤Price≤10000)
then follows n−1 lines, each contains three numbers x, y and z which means there exists a road between x and y, the distance is zkm (1≤z≤1000)
.
Output
For each test case, output a single number in a line: the maximum money he can get.
Sample Input
1 4 10 40 15 30 1 2 30 1 3 2 3 4 10
Sample Output
8
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)
const int N=1e5+10;
int v[N];
struct Edge{
int u,v,nt,w;
Edge(int _u=0,int _v=0,int _nt=0,int _w=0){
u=_u,v=_v,nt=_nt,w=_w;
}
}edge[N*2];
int head[N],cnt;
void add_edge(int u,int v,int w){
edge[cnt]=Edge(u,v,head[u],w);
head[u]=cnt++;
}
/*
对树形dp 不是很有感觉,现在觉得“子树”这个状态很重要
因为dp[]本身就是一种状态压缩,把最优值保留下来
*/
int dp[N][2];
void dfs(int u,int f){
dp[u][0]=v[u];
dp[u][1]=v[u];
for(int i=head[u];i!=-1;i=edge[i].nt){
Edge& e=edge[i];
if(e.v==f)continue;
dfs(e.v,u);
dp[u][0]=min(dp[u][0],dp[e.v][0]+e.w);
dp[u][1]=max(dp[u][1],dp[e.v][1]-e.w);
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
cnt=0;
fill(head,head+n+1,-1);
rep(i,1,n+1)scanf("%d",&v[i]);
rep(i,0,n-1){
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
add_edge(u,v,w);
add_edge(v,u,w);
}
rep(i,0,n+1)dp[i][0]=dp[i][1]=0;
dfs(1,0);
int ans=0;
rep(i,1,n+1)ans=max(ans,dp[i][1]-dp[i][0]);
printf("%d\n",ans);
}
return 0;
}