-
Notifications
You must be signed in to change notification settings - Fork 1
/
D.cpp
61 lines (46 loc) · 1.04 KB
/
D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
ll oil[300009];
vector <ll> tree[300009], cst[300009];
ll ans = 0;
ll dfs(ll u, ll p)
{
ll mx1 = 0, mx2 = 0;
for(ll i = 0; i < tree[u].size(); i++) {
ll nd = tree[u][i];
if(nd == p)
continue;
ll ret = dfs(nd, u);
ret = max(0LL, ret - cst[u][i]);
if(ret >= mx1) {
mx2 = mx1;
mx1 = ret;
}
else if(ret > mx2) mx2 = ret;
}
ans = max(ans, oil[u] + mx1 + mx2);
//cout << oil[u] + mx1 + mx2 << " " << u << " " << mx1 << " " << mx2 << endl;
return oil[u] + mx1;
}
int main()
{
ll n;
cin >> n;
for(ll i = 1; i <= n; i++) {
scanf("%lld", &oil[i]);
ans = max(ans, oil[i]);
}
for(ll i = 1; i < n; i++) {
ll u, v, w;
scanf("%lld %lld %lld", &u, &v, &w);
tree[u].pb(v);
tree[v].pb(u);
cst[u].pb(w);
cst[v].pb(w);
}
dfs(1, -1);
cout << ans << endl;
return 0;
}