树形dp
dp[i][0]表示从i点及其子树中买入的最大收益(负数)
dp[i][1]表示从i点及其子树中卖出的最大收益(正数)
#include
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int ret = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) ret = (ret << 3) + (ret << 1) + (ch ^ 48), ch = getchar();
return w ? -ret : ret;
}
inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 100005;
int _, n, w[N], head[N], cnt, dp[N][2], ans;
struct Edge { int v, next, w; } edge[N<<1];
void build(){
cnt = 0, ans = -INF;
full(head, -1), full(w, 0);
}
void addEdge(int a, int b, int c){
edge[cnt].v = b, edge[cnt].next = head[a], edge[cnt].w = c, head[a] = cnt ++;
}
void dfs(int s, int fa){
dp[s][0] = -w[s], dp[s][1] = w[s];
for(int i = head[s]; i != -1; i = edge[i].next){
int u = edge[i].v;
if(u == fa) continue;
dfs(u, s);
dp[s][0] = max(dp[s][0], dp[u][0] - edge[i].w);
dp[s][1] = max(dp[s][1], dp[u][1] - edge[i].w);
}
ans = max(ans, dp[s][0] + dp[s][1]);
}
int main(){
for(_ = read(); _; _ --){
build();
n = read();
for(int i = 1; i <= n; i ++) w[i] = read();
for(int i = 0; i < n - 1; i ++){
int u = read(), v = read(), c = read();
addEdge(u, v, c), addEdge(v, u, c);
}
dfs(1, 0);
printf("%d\n", ans);
}
return 0;
}