← Home
For problem statement at 0-999/800-899/850-859/856/problemD.txt this is a correct solution, but verifier at 0-999/800-899/850-859/856/verifierD.go ends with case 1 failed: runtime error: exit status 2
panic: runtime error: index out of range [2] with length 0

goroutine 1 [running]:
main.main()
	/tmp/build-100262197/solution.go:64 +0xccc

input:
6 1
1 1 1 1 5
5 6 7
exit status 1 can you fix the verifier? package main

import (
	"bufio"
	"fmt"
	"os"
)

var scanner *bufio.Scanner

func init() {
	scanner = bufio.NewScanner(os.Stdin)
	buf := make([]byte, 1024*1024)
	scanner.Buffer(buf, 1024*1024*10)
	scanner.Split(bufio.ScanWords)
}

func nextInt() int {
	scanner.Scan()
	res := 0
	for _, b := range scanner.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

type Path struct {
	u, v int
	c    int64
}

func main() {
	if !scanner.Scan() {
		return
	}
	n := 0
	for _, b := range scanner.Bytes() {
		n = n*10 + int(b-'0')
	}
	m := nextInt()

	parent := make([]int, n+1)
	children := make([][]int, n+1)
	for i := 2; i <= n; i++ {
		p := nextInt()
		parent[i] = p
		children[p] = append(children[p], i)
	}

	LOG := 19
	up := make([][]int, n+1)
	for i := 1; i <= n; i++ {
		up[i] = make([]int, LOG)
	}
	depth := make([]int, n+1)
	depth[1] = 0
	up[1][0] = 1

	for i := 2; i <= n; i++ {
		p := parent[i]
		depth[i] = depth[p] + 1
		up[i][0] = p
		for j := 1; j < LOG; j++ {
			up[i][j] = up[up[i][j-1]][j-1]
		}
	}

	getLCA := func(u, v int) int {
		if depth[u] < depth[v] {
			u, v = v, u
		}
		diff := depth[u] - depth[v]
		for j := 0; j < LOG; j++ {
			if (diff&(1<<j)) != 0 {
				u = up[u][j]
			}
		}
		if u == v {
			return u
		}
		for j := LOG - 1; j >= 0; j-- {
			if up[u][j] != up[v][j] {
				u = up[u][j]
				v = up[v][j]
			}
		}
		return up[u][0]
	}

	paths := make([][]Path, n+1)
	for i := 0; i < m; i++ {
		u := nextInt()
		v := nextInt()
		c := int64(nextInt())
		lca := getLCA(u, v)
		paths[lca] = append(paths[lca], Path{u, v, c})
	}

	in := make([]int, n+1)
	out := make([]int, n+1)
	timer := 1

	type frame struct {
		u       int
		edgeIdx int
	}
	stack := make([]frame, 0, n)
	stack = append(stack, frame{1, 0})
	in[1] = timer
	timer++

	for len(stack) > 0 {
		curr := &stack[len(stack)-1]
		u := curr.u
		if curr.edgeIdx < len(children[u]) {
			v := children[u][curr.edgeIdx]
			curr.edgeIdx++
			in[v] = timer
			timer++
			stack = append(stack, frame{v, 0})
		} else {
			out[u] = timer
			stack = stack[:len(stack)-1]
		}
	}

	bit := make([]int64, n+2)
	add := func(idx int, val int64) {
		for ; idx < len(bit); idx += idx & -idx {
			bit[idx] += val
		}
	}
	query := func(idx int) int64 {
		var sum int64 = 0
		for ; idx > 0; idx -= idx & -idx {
			sum += bit[idx]
		}
		return sum
	}

	dp := make([]int64, n+1)
	S := make([]int64, n+1)

	for i := n; i >= 1; i-- {
		var sumS int64 = 0
		for _, child := range children[i] {
			sumS += dp[child]
		}
		S[i] = sumS
		dp[i] = S[i]

		for _, p := range paths[i] {
			cand := p.c + S[i] + query(in[p.u]) + query(in[p.v])
			if cand > dp[i] {
				dp[i] = cand
			}
		}

		val := S[i] - dp[i]
		add(in[i], val)
		add(out[i], -val)
	}

	fmt.Println(dp[1])
}