## transaction transaction transaction

transaction transaction transaction

【2017 ACM/ICPC Asia Regional Shenyang Online】

【DP问题】

Solution:

```#include <iostream>
#include <vector>

using namespace std;

struct node {
int to, weight;
};

const int NIL = -1;
const int LIM = 1e5 + 10;

int val[LIM], n;
vector<node> edges[LIM];
int dp[LIM][2], ans = 0;

inline int maxi(int a, int b) {
return a > b ? a : b;
}

void init() {
ans = 0;
for (int i = 1; i <= n; i++) {
edges[i].clear();
}
}

void dfs(int crt, int from) {
dp[crt][0] = -val[crt];
dp[crt][1] = val[crt];
for (vector<node>::iterator ite = edges[crt].begin(); ite != edges[crt].end(); ite++) {
int to = ite->to;
if (to == from) continue;
dfs(to, crt);
dp[crt][0] = maxi(dp[crt][0], dp[to][0] - ite->weight);
dp[crt][1] = maxi(dp[crt][1], dp[to][1] - ite->weight);
}
ans = maxi(ans, dp[crt][0] + dp[crt][1]);
}

int main(void) {
int t, from, to, w;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
init();
for (int i = 1; i <= n; i++)
scanf("%d", val + i);
for (int i = 1; i < n; i++) {
scanf("%d%d%d", &from, &to, &w);
edges[from].push_back({to, w});
edges[to].push_back({from, w});
}
dfs(1, NIL);
printf("%d\n", ans);
}
return 0;
}```