-
Notifications
You must be signed in to change notification settings - Fork 519
/
1083A.go
54 lines (49 loc) · 987 Bytes
/
1083A.go
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
package main
import (
"bufio"
. "fmt"
"io"
)
// github.com/EndlessCheng/codeforces-go
func CF1083A(_r io.Reader, out io.Writer) {
in := bufio.NewReader(_r)
max := func(a, b int64) int64 {
if a > b {
return a
}
return b
}
var n, v, w, wt int
Fscan(in, &n)
a := make([]int, n)
for i := range a {
Fscan(in, &a[i])
}
type nb struct{ to, wt int }
g := make([][]nb, n)
for i := 1; i < n; i++ {
Fscan(in, &v, &w, &wt)
v--
w--
g[v] = append(g[v], nb{w, wt})
g[w] = append(g[w], nb{v, wt})
}
ans := int64(0)
var f func(int, int) int64
f = func(v, fa int) int64 {
mx := int64(a[v])
ans = max(ans, mx)
for _, e := range g[v] {
if w := e.to; w != fa {
if m := f(w, v) - int64(e.wt); m >= 0 {
ans = max(ans, mx+m) // 拼接来自两颗不同子树的路径
mx = max(mx, m+int64(a[v])) // 更新子树路径的最大值
}
}
}
return mx
}
f(0, -1)
Fprint(out, ans)
}
//func main() { CF1083A(os.Stdin, os.Stdout) }