package main

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

func main() {
	var n, tmp int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n)
	manpower := 0
	untreated := 0
	for i := 0; i < n; i += 1 {
		fmt.Fscan(reader, &tmp)
		if tmp > 0 {
			manpower += tmp
		} else if manpower > 0 {
			manpower -= 1
		} else {
			untreated += 1
		}
	}
	fmt.Println(untreated)
}

package main
import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	var a [100000]int
	for i:=0;i<n;i++ {
		fmt.Scan(&a[i])
	}
	result:=0
	t:=0
	for i:=0;i<n;i++ {
		if a[i]<0 && t==0 {
			result+=t+a[i]
			if t>-a[i] {
				t=t+a[i]
			} else {
				t=0
			}
		} else {
			t+=a[i]
		}
	}
	fmt.Println(-result)
}

package main

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

func main() {
	var n, c int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n)
	p := 0
	count := 0
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &c)
		p = p + c
		if p < 0 {
			count++
			p = 0
		}
	}
	fmt.Println(count)
}

package main

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

func main() {

	//Working solution, Runtime 888ms

	// var n, in int
	// fmt.Scan(&n)
	// var count, cops int
	// for i := 0; i < n; i++ {
	// 	fmt.Scan(&in)
	// 	if in == -1 {
	// 		if cops == 0 {
	// 			count++
	// 		} else {
	// 			cops--
	// 		}
	// 	} else {
	// 		cops += in
	// 	}
	// }
	// fmt.Println(count)

	var n int
	fmt.Scan(&n)
	var count, cops int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		scanner.Scan()
		val := scanner.Text()
		if val == "-1" {
			if cops == 0 {
				count++
			} else {
				cops--
			}
		} else {
			vals, _ := strconv.Atoi(val)
			cops += vals
		}
	}
	fmt.Println(count)
}

// func toInt(buf []byte) (n int) {
// 	for _, v := range buf {
// 		n = n*10 + int(v-'0')
// 	}
// 	return
// }

package main

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

var scanner = bufio.NewScanner(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	a := 0
	numberOfRecruited := 0
	count := 0

	for i:=0; i<n; i++ {
		a = nextInt()

		if a > 0 {
			numberOfRecruited += a
		} else {
			if numberOfRecruited > 0 {
				numberOfRecruited--
			} else {
				count++
			}
		}
	}

	println(count)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func nextFloat() float64 {
	n, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

func printf(format string, a ...interface{}) {
	fmt.Fprintf(writer, format, a...)
}

package main
import (
	"fmt"
)
func main() {
	var b,c int
	fmt.Scan(&b)
	s,n:=0,0
	for i:=0; i<b; i++ {
		fmt.Scan(&c)
		if c==-1 {
			if s==0 {
				n++
			} else {s--}
		}else {
			s+=c
		}
	}
	fmt.Println(n)
}

package main

import (
	"fmt"
)

func main() {
	n, x, l, z := 0, 0, 0, 0
	fmt.Scanf("%d ", &n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &x);
		if x == -1 {
			if l > 0 {
				l--
			} else {
				z++
			}
		} else {
			l += x
		}
	}
	fmt.Println(z)
}

package main

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

var (
    reader = bufio.NewReader(os.Stdin)
)

func main() {
    var n, x, g, cnt int
    fmt.Fscan(reader, &n)
    for i := 1; i <= n; i++ {
        fmt.Fscan(reader, &x)
        if x == -1 {
            if g == 0 {
                cnt++
            } else {
                g--
            }
        } else {
            g += x
        }
    }
    fmt.Println(cnt)
}

package main

import (
	"fmt"
)

func main() {
	var n, m int
	fmt.Scan(&n)
	sum := 0
	count := 0
	for i := 0; i < n; i++ {
		fmt.Scan(&m)

		if m > 0 {
			count += m
		} else if count > 0 {
			count -= 1
		} else {
			sum += 1
		}
	}
	fmt.Println(sum)

}

package main

import "fmt"

func main() {
	var n, v int
	fmt.Scan(&n)

	untreated := 0
	police := 0
	crime := 0
	for i := 0; i < n; i++ {
		fmt.Scan(&v)
		if v >= 0 {
			police += v
		} else {
			if police > 0 {
				police -= 1
			} else {
				crime += 1
				if crime > untreated {
					untreated = crime
				}
			}
		}
	}

	fmt.Print(untreated)
}

package main

import (
	"fmt"
)

func main() {
	var n, temp int
	fmt.Scanf("%d \n", &n)
	count := 0
	free := 0
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &temp)
		if temp == -1 {
			if free == 0 {
				count++
			}
		}
		free += temp
		if free < 0 {
			free = 0
		}
	}
	fmt.Print(count)
}

package main

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

var reader *bufio.Reader

func init() {
	stdin := os.Stdin
	// stdin, _ = os.Open("1.in")
	reader = bufio.NewReaderSize(stdin, 1<<20)
}

var n int

func main() {
	fmt.Fscan(reader, &n)
	var sum, ans int
	for ; n != 0; n-- {
		var a int
		fmt.Fscan(reader, &a)
		if a < 0 {
			if sum == 0 {
				ans++
			} else {
				sum--
			}
		} else {
			sum += a
		}
	}
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	var n, temp int
	fmt.Scanf("%d \n", &n)
	count := 0
	free := 0
	reader := bufio.NewReader(os.Stdin)
	ss, _ := reader.ReadString('\n')
	list := strings.Split(strings.TrimRight(ss, "\r\n"), " ")
	for i := 0; i < n; i++ {
		t, _ := strconv.ParseInt(list[i], 10, 64)
		temp = int(t)
		if temp == -1 {
			if free == 0 {
				count++
			}
		}
		free += temp
		if free < 0 {
			free = 0
		}
	}
	fmt.Print(count)
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)

    var n int
    fmt.Scan(&n)
    ans, police := 0, 0
    for i := 0; i < n; i++ {
        scanner.Scan()
        a, _ := strconv.Atoi(scanner.Text())
        if a < 0 {
            if police > 0 {
                police--
            } else {
                ans++
            }
        } else {
            police += a
        }
    }
    fmt.Println(ans)
}

package main

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

func main() {
	var n, t, c int32
	var pos uint32
	in := bufio.NewReader(os.Stdin)
	fmt.Fscanf(in, "%d\n", &n)
	for i := int32(0); i < n-1; i++ {
		fmt.Fscanf(in, "%d", &t)
		if t < 0 {
			if pos == 0 {
				c += int32(math.Abs(float64(t)))
			} else {
				pos--
			}
		} else {
			pos += uint32(t)
		}
	}
	fmt.Fscanf(in, "%d\n", &t)
	if t < 0 {
		if pos == 0 {
			c += int32(math.Abs(float64(t)))
		} else {
			pos--
		}
	} else {
		pos += uint32(t)
	}
	fmt.Println(math.Abs(float64(c)))
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var total int
	var hired int
	var out int
	fmt.Fscan(reader, &total)
	for i := 0; i < total; i++ {
		var in int
		fmt.Fscan(reader, &in)
		if in == -1 {
			if hired == 0 {
				out++
			} else {
				hired--
			}
		}
		if in > 0 {
			hired += in
		}
	}
	fmt.Println(out)
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	var officers int
	var untreated int
	for i := 0; i < n; i++ {
		var event int
		fmt.Scanf("%d", &event)
		if event == -1 {
			if officers == 0 {
				untreated++
			} else {
				officers--
			}
		} else {
			officers += event
		}

	}
	fmt.Println(untreated)
}

package main

import (
	"bufio"
	"errors"
	"flag"
	"os"
	"strconv"
	"time"
)

// ******************************** CODE STARTS ********************************

var ()

func solve() {
	n := readInt()

	have := 0
	ans := 0
	for i := 0; i < n; i++ {
		value := readInt()
		if value == -1 {
			if have == 0 {
				ans++
			} else {
				have--
			}
		} else {
			have += value
		}
	}
	printInt(ans)
	printLine()
}

// ********************************* CODE ENDS *********************************

// IO.

var (
	scanner *bufio.Scanner
	writer  *bufio.Writer
)

func main() {
	local := flag.Bool("local", false, "Defines whether run is local or remote.")
	flag.Parse()

	var (
		input  *os.File
		output *os.File
	)

	if *local {
		input, _ = os.Open("/home/outside/coding/workspace/go/io/input.txt")
		output, _ = os.Create("/home/outside/coding/workspace/go/io/output.txt")
		startTime := time.Now()
		defer func() {
			print("Time: ")
			print(strconv.FormatFloat(time.Since(startTime).Seconds(), 'f', 5, 64))
			println("s")
		}()
	} else {
		input = os.Stdin
		output = os.Stdout
		// input, _ = os.Open("")
		// output, _ = os.Create("")
	}
	defer input.Close()
	defer output.Close()

	scanner = bufio.NewScanner(input)
	writer = bufio.NewWriter(output)
	defer writer.Flush()

	scanner.Split(bufio.ScanWords)

	solve()
}

// Read.

func readString() string {
	scanner.Scan()
	ans := scanner.Text()
	return ans
}

func readLine() string {
	scanner.Split(bufio.ScanLines)
	defer scanner.Split(bufio.ScanWords)
	return readString()
}

func readInt() int {
	ans, _ := strconv.ParseInt(readString(), 10, 0)
	return int(ans)
}

func readInt64() int64 {
	ans, _ := strconv.ParseInt(readString(), 10, 64)
	return ans
}

func readFloat64() float64 {
	ans, _ := strconv.ParseFloat(readString(), 64)
	return ans
}

// Write.

func printString(value string) {
	writer.WriteString(value)
}

func printLine() {
	printString("\n")
}

func printInt(value int) {
	printString(strconv.FormatInt(int64(value), 10))
}

func printInt64(value int64) {
	printString(strconv.FormatInt(value, 10))
}

func printFLoat64(value float64) {
	printString(strconv.FormatFloat(value, 'f', 20, 64))
}

// Min, max, abs.

func maxInt(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func minInt(a, b int) int {
	if a < b {
		return a
	} else {
		return b
	}
}

// Set Key interface implementations.

type Int int

func (p Int) Equals(key Key) bool {
	return int(p) == int(key.(Int))
}

func (p Int) Less(key Key) bool {
	return int(p) < int(key.(Int))
}

// Set.

type Key interface {
	Equals(key Key) bool
	Less(key Key) bool
}

type node struct {
	key   Key
	left  *node
	right *node
}

func newNode(key Key) *node {
	return &node{key, nil, nil}
}

type SplayTree struct {
	root *node
	tmp  *node
	len  int
}

func NewSplayTree() *SplayTree {
	return &SplayTree{nil, newNode(nil), 0}
}

func rotateLeft(x, p *node) {
	p.right = x.left
	x.left = p
}

func rotateRight(x, p *node) {
	p.left = x.right
	x.right = p
}

func (p *SplayTree) splay(x *node, key Key) *node {
	if x == nil {
		return nil
	}

	left := p.tmp
	right := p.tmp

	for {
		if key.Less(x.key) {
			y := x.left
			if y == nil {
				break
			}
			if key.Less(y.key) { // zig-zig
				rotateRight(y, x)
				x = y
				if x.left == nil {
					break
				}
			}
			// link right
			right.left = x
			right = x
			// move left
			x = x.left
		} else if x.key.Less(key) {
			y := x.right
			if y == nil {
				break
			}
			if y.key.Less(key) { // zig-zig
				rotateLeft(y, x)
				x = y
				if x.right == nil {
					break
				}
			}
			// link left
			left.right = x
			left = x
			// move right
			x = x.right
		} else {
			break
		}
	}

	left.right = x.left
	right.left = x.right
	x.left = p.tmp.right
	x.right = p.tmp.left

	return x
}

// left <= key < right
func (p *SplayTree) split(key Key) (left, right *node) {
	p.root = p.splay(p.root, key)
	if p.root.key.Equals(key) || p.root.key.Less(key) {
		right := p.root.right
		p.root.right = nil
		return p.root, right
	} else {
		left := p.root.left
		p.root.left = nil
		return left, p.root
	}
}

// keys from left tree must be less then keys from right tree
func (p *SplayTree) join(left, right *node) *node {
	if left == nil {
		return right
	} else if right == nil {
		return left
	}
	left = p.splay(left, right.key)
	left.right = right
	return left
}

type Set interface {
	Len() int
	Insert(key Key) error
	Find(key Key) bool
	Remove(key Key) error
}

func NewSet() Set {
	return Set(NewSplayTree())
}

func (p *SplayTree) Len() int {
	return p.len
}

func (p *SplayTree) Insert(key Key) error {
	if p.root == nil {
		p.root = newNode(key)
		p.len++
	} else {
		p.root = p.splay(p.root, key)
		if p.root.key.Equals(key) {
			return errors.New("Such key already exists")
		} else {
			left, right := p.split(key)
			p.root = newNode(key)
			p.root.left = left
			p.root.right = right
			p.len++
		}
	}
	return nil
}

func (p *SplayTree) Find(key Key) bool {
	if p.root == nil {
		return false
	}
	p.root = p.splay(p.root, key)
	return p.root.key.Equals(key)
}

func (p *SplayTree) Remove(key Key) error {
	p.root = p.splay(p.root, key)
	if p.root == nil || !p.root.key.Equals(key) {
		return errors.New("Such key doesn't exist")
	}
	p.root = p.join(p.split(key))
	return nil
}

package main

import "fmt"

func main() {
	var n, in int
	fmt.Scan(&n)
	var count, cops int
	for i := 0; i < n; i++ {
		fmt.Scan(&in)
		if in == -1 {
			if cops == 0 {
				count++
			} else {
				cops--
			}
		} else {
			cops += in
		}
	}
	fmt.Println(count)
}

package main

import (
    "fmt"
    )

func main() {
    var n int
    fmt.Scan(&n)

    event := 0
    police := 0
    result := 0
    for i := 0; i < n; i++ {
        fmt.Scan(&event)
        if event < 0 {
            if police > 0 {
                police--
            } else {
                result++
            }
        } else {
            police += event
        }
    }

    fmt.Println(result)
}

package main

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

func main() {
	var noOfEvents int
	reader := bufio.NewReader(os.Stdin)

	fmt.Fscan(reader, &noOfEvents)
	var noOfOfficers, untreatedCrimes, event int
	for index := 0; index < noOfEvents; index++ {
		fmt.Fscan(reader, &event)
		if event != -1 { //Case hire offices
			noOfOfficers += event
		} else { //Case new case
			if noOfOfficers == 0 { //No officers
				untreatedCrimes++
			} else {
				//Assign offices to case
				noOfOfficers--
			}
		}
	}
	fmt.Println(untreatedCrimes)
}

package main

import "fmt"

func main() {
	var noOfEvents int
	fmt.Scan(&noOfEvents)
	var noOfOfficers, untreatedCrimes, event int
	for index := 0; index < noOfEvents; index++ {
		fmt.Scan(&event)
		if event != -1 { //Case hire offices
			noOfOfficers += event
		} else { //Case new case
			if noOfOfficers == 0 { //No officers
				untreatedCrimes++
			} else {
				//Assign offices to case
				noOfOfficers--
			}
		}
	}
	fmt.Println(untreatedCrimes)
}

// 427A-mic
package main

import (
    "fmt"
    "math"
)

func main() {
    var n, crime, police, k int
    fmt.Scan(&n)
    for i := 0; i < n; i++ {
        fmt.Scan(&k)
        if k < 0 && police == 0 {
            crime += k
        } else {
            police += k
        }
    }
    fmt.Println(int(math.Abs(float64(crime))))
}

package main

import ("fmt")

type set map[byte]bool
type long int64
type fl float64

func main(){
	var n, t, w, p int
	scan(&n)
	for i := 0; i < n; i++{
		scan(&t)
		if t > 0{
			p+=t
			}else{
				if p > 0{
					p--
				}else{
					w++
				}
			}
	}
	sout(w)
}

func scan(in ... interface{}){
	for _,k := range in{
		fmt.Scan(k)
	}
}

func sout(obj ... interface{}){
	for _, k := range obj{
		fmt.Print(k)
	}
	fmt.Println()
}

package main

import ("bufio";"os";"strconv")

func main(){
	defer out.Flush()
    scan.Split(bufio.ScanWords)
    var n, t, w, p int
	n = nextInt()
	for i := 0; i < n; i++{
		t = nextInt()
		if t > 0{
			p+=t
			}else{
				if p > 0{
					p--
				}else{
					w++
				}
			}
	}
	sout(w)

}

var scan *bufio.Scanner = bufio.NewScanner(os.Stdin)

var out *bufio.Writer = bufio.NewWriter(os.Stdout)

func nextInt() int {
    scan.Scan()
    ans, _ := strconv.Atoi(scan.Text())
    return ans
}

func next()string{
	scan.Scan()
	return scan.Text()
}

func sout(obj ... int) {
    for _, value := range obj {
        out.WriteString(strconv.Itoa(value))
      //  out.WriteByte(' ')
    }
}

package main

// https://codeforces.com/problemset/problem/427/A
// A. Police Recruits
// time limit per test
// 1 second
// memory limit per test
// 256 megabytes
// input
// standard input
// output
// standard output

// The police department of your city has just started its journey. Initially, they don’t have any manpower. So, they started hiring new recruits in groups.

// Meanwhile, crimes keeps occurring within the city. One member of the police force can investigate only one crime during his/her lifetime.

// If there is no police officer free (isn't busy with crime) during the occurrence of a crime, it will go untreated.

// Given the chronological order of crime occurrences and recruit hirings, find the number of crimes which will go untreated.
// Input

// The first line of input will contain an integer n (1 ≤ n ≤ 105), the number of events. The next line will contain n space-separated integers.

// If the integer is -1 then it means a crime has occurred. Otherwise, the integer will be positive, the number of officers recruited together at that time. No more than 10 officers will be recruited at a time.
// Output

// Print a single integer, the number of crimes which will go untreated.

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

var (
    bufStdin  = bufio.NewReader(os.Stdin)
    bufStdout = bufio.NewWriter(os.Stdout)
)

func main() {
    defer bufStdout.Flush()

    var n int
    _, _ = fmt.Fscan(bufStdin, &n)

    events := make([]int, n)
    for i := 0; i < n; i++ {
        _, _ = fmt.Fscan(bufStdin, &events[i])
    }

    var copsAvailable, untreatedCrimes int
    for _, event := range events {
        if event == -1 {
            // crime occurred
            if copsAvailable == 0 {
                untreatedCrimes++
            } else {
                copsAvailable--
            }
        } else {
            // some number of cops was recruited
            copsAvailable += event
        }
    }

    _, _ = fmt.Fprintln(bufStdout, untreatedCrimes)
}

package main

import "fmt"

// https://codeforces.com/problemset/problem/427/A
// комментарии к решению
// Для решения нам необходимо на каждом событии знать кол-во
// свободных полицейских, а далее мы просто итерируемся по массиву,
// идентифицируя каждое событие как один из 3-х возможных вариантов
func taskSolution(a []int) (ans int) {
	for i, free_officers := 0, 0; i < len(a); i += 1 {
		if a[i] > 0 {
			free_officers += a[i]
		} else if free_officers > 0 {
			free_officers -= 1
		} else {
			ans += 1
		}
	}
	return ans
}

func main() {
	var n int
	if _, err := fmt.Scan(&n); err != nil {
		panic(err)
	}

	a := []int{}
	for i := 0; i < n; i += 1 {
		var ai int
		if _, err := fmt.Scan(&ai); err != nil {
			panic(err)
		}
		a = append(a, ai)
	}

	fmt.Println(taskSolution(a))
}

package main

import "fmt"

func main()  {
	var noOfEvents int
	fmt.Scan(&noOfEvents)

	var noOfCrimes int
	noORecruits := 0
	for index := 0; index < noOfEvents; index++ {
		var event int
		fmt.Scan(&event)
		if event < 0 {
			if noORecruits > 0 {
				noORecruits = noORecruits - 1
			} else {
				noOfCrimes = noOfCrimes + 1
			}
		} else {
			noORecruits = noORecruits + event
		}
	}
	fmt.Println(noOfCrimes)
}

package main

import (
	"fmt"
)

func main() {
	n, x, l, z := 0, 0, 0, 0
	fmt.Scanf("%d", &n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &x);
		if x == -1 {
			if l > 0 {
				l--
			} else {
				z++
			}
		} else {
			l += x
		}
	}
	fmt.Println(z)
}

package main

import (
	"fmt"
)

func main() {
	var n, x int
	l, z := 0, 0
	fmt.Scanf("%d", &n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &x);
		if x == -1 {
			if l > 0 {
				l--
			} else {
				z++
			}
		} else {
			l += x
		}
	}
	fmt.Println(z)
}

package main

import (
	"fmt"
)

func main() {
	var n, m int
	fmt.Scan(&n)
	sum := 0
	count := 0
	for i := 0; i < n; i++ {
		fmt.Scan(&m)

		if m > 0 {
			count += 1
		} else if count > 0 {
			count -= 1
		} else {
			sum += 1
		}
	}
	fmt.Println(sum)

}

package main

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

var reader *bufio.Reader

func init() {
	stdin := os.Stdin
	// stdin, _ = os.Open("1.in")
	reader = bufio.NewReaderSize(stdin, 1<<20)
}

var n int

func main() {
	fmt.Fscan(reader, &n)
	var sum, ans int
	for ; n != 0; n-- {
		var a int
		fmt.Fscan(reader, &a)
		if a < 0 {
			if sum == 0 {
				ans++
			} else {
				sum--
			}
		} else {
			sum = a
		}
	}
	fmt.Println(ans)
}

package main

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

func main() {
	var n, t, c int16
	var pos uint16
	in := bufio.NewReader(os.Stdin)
	fmt.Fscanf(in, "%d\n", &n)
	for i:=int16(0); i < n - 1; i++{
		fmt.Fscanf(in, "%d", &t)
		if t < 0{
			if pos == 0{
				c += t
			}else{
				pos--
			}
		}else{
			pos++
		}
	}
	fmt.Fscanf(in, "%d\n", &t)
	if t < 0{
		if pos == 0{
			c += t
		}
	}else{
		pos++
	}
	fmt.Println(math.Abs(float64(c)))
}

package main

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

func main() {
	var n, t, c int16
	var pos uint16
	in := bufio.NewReader(os.Stdin)
	fmt.Fscanf(in, "%d\n", &n)
	for i := int16(0); i < n-1; i++ {
		fmt.Fscanf(in, "%d", &t)
		if t < 0 {
			if pos == 0 {
				c += int16(math.Abs(float64(t)))
			} else {
				pos--
			}
		} else {
			pos += uint16(t)
		}
	}
	fmt.Fscanf(in, "%d\n", &t)
	if t < 0 {
		if pos == 0 {
			c += int16(math.Abs(float64(t)))
		} else {
			pos--
		}
	} else {
		pos += uint16(t)
	}
	fmt.Println(math.Abs(float64(c)))
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdout)
	var total int
	var hired int
	var out int
	fmt.Fscan(reader, &total)
	for i := 0; i < total; i++ {
		var in int
		fmt.Fscan(reader, &in)
		if in == -1 {
			if hired == 0 {
				out++
			} else {
				hired--
			}
		}
		if in > 0 {
			hired += hired
		}
		//fmt.Println(in,out,hired)
	}
	fmt.Println(out)
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var total int
	var hired int
	var out int
	fmt.Fscan(reader, &total)
	for i := 0; i < total; i++ {
		var in int
		fmt.Fscan(reader, &in)
		if in == -1 {
			if hired == 0 {
				out++
			} else {
				hired--
			}
		}
		if in > 0 {
			hired += 1
		}
	}
	fmt.Println(out)
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdout)
	var total int
	var hired int
	var out int
	fmt.Fscan(reader, &total)
	for i := 0; i < total; i++ {
		var in int
		fmt.Fscan(reader, &in)
		if in == -1 {
			if hired == 0 {
				out++
			} else if hired > 0 {
				hired--
			}
		}
		if in > 0 {
			hired++
		}
	}
	fmt.Println(out)
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var total int
	var hired int
	var out int
	fmt.Fscan(reader, &total)
	for i := 0; i < total; i++ {
		var in int
		fmt.Fscan(reader, &in)
		if in == -1 {
			if hired == 0 {
				out++
			} else {
				hired--
			}
		}
		if in > 0 {
			hired += hired
		}
		//fmt.Println(in,out,hired)
	}
	fmt.Println(out)
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdout)
	var total int
	var hired int
	var out int
	fmt.Fscan(reader, &total)
	for i := 0; i < total; i++ {
		var in int
		fmt.Fscan(reader, &in)
		if in == -1 {
			if hired == 0 {
				out++
			} else if hired > 0 {
				hired--
			}
		}
		if in > 0 {
			hired++
		}
		//fmt.Println(in,out,hired)
	}
	fmt.Println(out)
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var total int
	var hired int
	var out int
	fmt.Fscan(reader, &total)
	for i := 0; i < total; i++ {
		var in int
		fmt.Fscan(reader, &in)
		if in == -1 {
			if hired == 0 {
				out++
			} else {
				hired--
			}
		}
		if in > 0 {
			hired += 1
		}
		fmt.Println(in, out, hired)
	}
	fmt.Println(out)
}

package main

import "fmt"

func main() {
	var noOfEvents int
	fmt.Scan(&noOfEvents)
	var noOfOfficers, untreatedCrimes, event int
	for index := 0; index < noOfEvents; index++ {
		fmt.Scan(&event)
		if event == 1 { //Case hire offices
			noOfOfficers++
		} else { //Case new case
			if noOfOfficers == 0 { //No officers
				untreatedCrimes++
			} else {
				//Assign offices to case
				noOfOfficers--
			}
		}
	}
	fmt.Println(untreatedCrimes)
}

package main

import "fmt"

func main()  {
	var noOfEvents int
	fmt.Scan(&noOfEvents)

	noOfCrimes := 0
	noORecruits := 0

	for index := 0; index < noOfEvents; index++ {
		var event int
		fmt.Scan(&event)
		if event < 0 {
			if noORecruits >= noOfCrimes {
				noOfCrimes = noOfCrimes - noORecruits
				noORecruits = noORecruits - 1
			}

			if noORecruits > 0 {
				noORecruits = noORecruits - 1
				noOfCrimes = noOfCrimes - 1
			} else {
				noOfCrimes = noOfCrimes + 1
			}
		} else {
			noORecruits = noORecruits + event
		}
	}
	fmt.Println(noOfCrimes)
}

package main
import (
	"fmt"
)
func main() {
	var b,c int
	fmt.Scan(&b)
	s,n:=0,0
	for i:=0; i<b; i++ {
		fmt.Scan(&c)
		if c==-1 {
			if s==0 {
				n++
			} else {s--}
		}else {
			s+=b
		}
	}
	fmt.Println(n)
}

package main

import (
	"bufio"
	"errors"
	"flag"
	"os"
	"strconv"
	"time"
)

// ******************************** CODE STARTS ********************************

var ()

func solve() {
	n := readInt()

	have := 0
	ans := 0
	for i := 0; i < n; i++ {
		value := readInt()
		if value == -1 {
			if have == 0 {
				ans++
			}
		} else {
			have += value
		}
	}
	printInt(ans)
	printLine()
}

// ********************************* CODE ENDS *********************************

// IO.

var (
	scanner *bufio.Scanner
	writer  *bufio.Writer
)

func main() {
	local := flag.Bool("local", false, "Defines whether run is local or remote.")
	flag.Parse()

	var (
		input  *os.File
		output *os.File
	)

	if *local {
		input, _ = os.Open("/home/outside/coding/workspace/go/io/input.txt")
		output, _ = os.Create("/home/outside/coding/workspace/go/io/output.txt")
		startTime := time.Now()
		defer func() {
			print("Time: ")
			print(strconv.FormatFloat(time.Since(startTime).Seconds(), 'f', 5, 64))
			println("s")
		}()
	} else {
		input = os.Stdin
		output = os.Stdout
		// input, _ = os.Open("")
		// output, _ = os.Create("")
	}
	defer input.Close()
	defer output.Close()

	scanner = bufio.NewScanner(input)
	writer = bufio.NewWriter(output)
	defer writer.Flush()

	scanner.Split(bufio.ScanWords)

	solve()
}

// Read.

func readString() string {
	scanner.Scan()
	ans := scanner.Text()
	return ans
}

func readLine() string {
	scanner.Split(bufio.ScanLines)
	defer scanner.Split(bufio.ScanWords)
	return readString()
}

func readInt() int {
	ans, _ := strconv.ParseInt(readString(), 10, 0)
	return int(ans)
}

func readInt64() int64 {
	ans, _ := strconv.ParseInt(readString(), 10, 64)
	return ans
}

func readFloat64() float64 {
	ans, _ := strconv.ParseFloat(readString(), 64)
	return ans
}

// Write.

func printString(value string) {
	writer.WriteString(value)
}

func printLine() {
	printString("\n")
}

func printInt(value int) {
	printString(strconv.FormatInt(int64(value), 10))
}

func printInt64(value int64) {
	printString(strconv.FormatInt(value, 10))
}

func printFLoat64(value float64) {
	printString(strconv.FormatFloat(value, 'f', 20, 64))
}

// Min, max, abs.

func maxInt(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func minInt(a, b int) int {
	if a < b {
		return a
	} else {
		return b
	}
}

// Set Key interface implementations.

type Int int

func (p Int) Equals(key Key) bool {
	return int(p) == int(key.(Int))
}

func (p Int) Less(key Key) bool {
	return int(p) < int(key.(Int))
}

// Set.

type Key interface {
	Equals(key Key) bool
	Less(key Key) bool
}

type node struct {
	key   Key
	left  *node
	right *node
}

func newNode(key Key) *node {
	return &node{key, nil, nil}
}

type SplayTree struct {
	root *node
	tmp  *node
	len  int
}

func NewSplayTree() *SplayTree {
	return &SplayTree{nil, newNode(nil), 0}
}

func rotateLeft(x, p *node) {
	p.right = x.left
	x.left = p
}

func rotateRight(x, p *node) {
	p.left = x.right
	x.right = p
}

func (p *SplayTree) splay(x *node, key Key) *node {
	if x == nil {
		return nil
	}

	left := p.tmp
	right := p.tmp

	for {
		if key.Less(x.key) {
			y := x.left
			if y == nil {
				break
			}
			if key.Less(y.key) { // zig-zig
				rotateRight(y, x)
				x = y
				if x.left == nil {
					break
				}
			}
			// link right
			right.left = x
			right = x
			// move left
			x = x.left
		} else if x.key.Less(key) {
			y := x.right
			if y == nil {
				break
			}
			if y.key.Less(key) { // zig-zig
				rotateLeft(y, x)
				x = y
				if x.right == nil {
					break
				}
			}
			// link left
			left.right = x
			left = x
			// move right
			x = x.right
		} else {
			break
		}
	}

	left.right = x.left
	right.left = x.right
	x.left = p.tmp.right
	x.right = p.tmp.left

	return x
}

// left <= key < right
func (p *SplayTree) split(key Key) (left, right *node) {
	p.root = p.splay(p.root, key)
	if p.root.key.Equals(key) || p.root.key.Less(key) {
		right := p.root.right
		p.root.right = nil
		return p.root, right
	} else {
		left := p.root.left
		p.root.left = nil
		return left, p.root
	}
}

// keys from left tree must be less then keys from right tree
func (p *SplayTree) join(left, right *node) *node {
	if left == nil {
		return right
	} else if right == nil {
		return left
	}
	left = p.splay(left, right.key)
	left.right = right
	return left
}

type Set interface {
	Len() int
	Insert(key Key) error
	Find(key Key) bool
	Remove(key Key) error
}

func NewSet() Set {
	return Set(NewSplayTree())
}

func (p *SplayTree) Len() int {
	return p.len
}

func (p *SplayTree) Insert(key Key) error {
	if p.root == nil {
		p.root = newNode(key)
		p.len++
	} else {
		p.root = p.splay(p.root, key)
		if p.root.key.Equals(key) {
			return errors.New("Such key already exists")
		} else {
			left, right := p.split(key)
			p.root = newNode(key)
			p.root.left = left
			p.root.right = right
			p.len++
		}
	}
	return nil
}

func (p *SplayTree) Find(key Key) bool {
	if p.root == nil {
		return false
	}
	p.root = p.splay(p.root, key)
	return p.root.key.Equals(key)
}

func (p *SplayTree) Remove(key Key) error {
	p.root = p.splay(p.root, key)
	if p.root == nil || !p.root.key.Equals(key) {
		return errors.New("Such key doesn't exist")
	}
	p.root = p.join(p.split(key))
	return nil
}


package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

var scanner *bufio.Scanner
var t [1e6 + 1]bool

func init_scanner() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024 * 20
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
}

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int64 {
	scanner.Scan()
	val, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return val
}

func main() {
	init_scanner()
	k, d, t := readInt(), readInt(), readInt()
	//fmt.Println(k, d, t)
	y := ((k-1)/d + 1) * d
	x := float64(k) + float64(y-k)/2.
	z := float64(t) - math.Floor(float64(t)/x)*x
	//fmt.Println(x, y, z, math.Floor(float64(t)/x))
	//fmt.Println(math.Floor(float64(t)/x)*float64(y), math.Max(0., z-float64(k)))
	fmt.Println(math.Floor(float64(t)/x)*float64(y) + z + math.Max(0., z-float64(k)))
}

package main

import "fmt"

func main() {
	var k, d, t int64
	fmt.Scan(&k, &d, &t)
	x := (k + (d - 1)) / d // po kelių kartų ras išjungtą
	p := x * d
	t *= 2
	perP := k*2 + p - k
	ans := float64(t / perP * p)
	t %= perP
	if t <= k*2 {
		ans += float64(t) / 2
	} else {
		ans += float64(k + t - k*2)
	}
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

var scanner *bufio.Scanner
var t [1e6 + 1]bool

func init_scanner() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024 * 20
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
}

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	scanner.Scan()
	val, _ := strconv.Atoi(scanner.Text())
	return val
}

func main() {
	init_scanner()
	k, d, t := readInt(), readInt(), readInt()

	y := ((k-1)/d + 1) * d
	x := float64(k) + float64(y-k)/2.
	z := float64(t) - math.Floor(float64(t)/x)*x
	fmt.Println(x, y, z)
	fmt.Println(math.Floor(float64(t)/x)*float64(y) + z + math.Max(0., z-float64(k)))
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

var scanner *bufio.Scanner
var t [1e6 + 1]bool

func init_scanner() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024 * 20
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
}

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	scanner.Scan()
	val, _ := strconv.Atoi(scanner.Text())
	return val
}

func main() {
	init_scanner()
	k, d, t := readInt(), readInt(), readInt()

	y := ((k-1)/d + 1) * d
	x := float64(k) + float64(y-k)/2.
	z := float64(t) - math.Floor(float64(t)/x)*x
	// fmt.Println(x, y, z)
	fmt.Println(math.Floor(float64(t)/x)*float64(y) + z + math.Max(0., z-float64(k)))
}


package main

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

const (
	maxN = 2000005
)

var A [maxN]int
var MI [maxN]int

func main() {
	fsc := NewFastScanner()
	N := fsc.NextInt()
	for i := 0; i < N; i++ {
		x := fsc.NextInt()
		A[x] = 1
	}
	for i := 1; i < maxN; i++ {
		if A[i] == 1 {
			MI[i] = i
		} else {
			MI[i] = MI[i-1]
		}
	}
	ma := 0
	for i := 1; i < 1000001; i++ {
		if A[i] == 1 {
			for j := 2; j*i-1 < maxN; j++ {
				// fmt.Println(i, j, j*i-1)
				ma = IntMax(ma, MI[j*i-1]%i)
			}
		}
	}
	fmt.Println(ma)
}

//template
type FastScanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewFastScanner() *FastScanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1024)
	return &FastScanner{r: rdr}
}
func (s *FastScanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *FastScanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *FastScanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *FastScanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *FastScanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *FastScanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

func IntMax(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func Int64Max(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func Float64Max(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

func IntMin(a, b int) int {
	if a > b {
		return b
	}
	return a
}

func Int64Min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}
func Float64Min(a, b float64) float64 {
	if a > b {
		return b
	}
	return a
}

package main

import (
	"fmt"
	"bufio"
	"strconv"
	"os"
	"sort"
)

var in = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

const MAXN = 2000010

func main() {

	defer out.Flush()
	in.Split(bufio.ScanWords)

	var n int = NextInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = NextInt()
	}

	sort.Ints(a)

	p := make([]int, MAXN)
	for i := 1; i < MAXN; i++ {
		var lo, hi int = -1, n
		for hi - lo > 1 {
			var m int = lo + (hi - lo) / 2
			if a[m] < i {
				lo = m
			}else {
				hi = m
			}
		}
		p[i] = lo
	}
	var ret int = -1
	for i := 0; i < n; i++ {
		if i > 0 && a[i - 1] == a[i] {
			continue
		}
		for j := 2 * a[i]; j < MAXN; j += a[i] {
			ret = Max(ret, a[p[j]] % a[i])
		}
	}

	fmt.Fprintln(out, ret)
}

func Max(a, b int) int{
	if a > b {
		return a
	}
	return b
}

func Next() string{
	if !in.Scan() {
		panic("Scan error")
	}
	return in.Text()
}

func NextInt() int{
	ret, _ := strconv.Atoi(Next())
	return ret
}

package main

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

var in = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

const MAXN = 2000010

func main() {

	defer out.Flush()
	in.Split(bufio.ScanWords)

	var n int = NextInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = NextInt()
	}

	p := make([]int, MAXN)
	for i := 1; i < MAXN; i++ {
		var lo, hi int = -1, n
		for hi - lo > 1 {
			var m int = lo + (hi - lo) / 2
			if a[m] < i {
				lo = m
			}else {
				hi = m
			}
		}
		p[i] = lo
	}
	var ret int = -1
	for i := 0; i < n; i++ {
		if i > 0 && a[i - 1] == a[i] {
			continue
		}
		for j := 2 * a[i]; j < MAXN; j += a[i] {
			ret = Max(ret, a[p[j]] % a[i])
		}
	}

	fmt.Fprintln(out, ret)
}

func Max(a, b int) int{
	if a > b {
		return a
	}
	return b
}

func Next() string{
	if !in.Scan() {
		panic("Scan error")
	}
	return in.Text()
}

func NextInt() int{
	ret, _ := strconv.Atoi(Next())
	return ret
}

package main

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

var in = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

const MAXN = 20000010

func main() {

	defer out.Flush()
	in.Split(bufio.ScanWords)
	var n int = NextInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = NextInt()
	}

	p := make([]int, MAXN)
	for i := 1; i < MAXN; i++ {
		var lo, hi int = -1, n
		for hi - lo > 1 {
			var m int = lo + (hi - lo) / 2
			if a[m] < i {
				lo = m
			}else {
				hi = m
			}
		}
		p[i] = lo
	}
	var ret int = -1
	for i := 0; i < n; i++ {
		if i > 0 && a[i] == a[i-1] {
			continue
		}
		for j := 2 * a[i]; j < MAXN; j += a[i] {
			var pos int = p[j]
			ret = Max(ret, a[pos] % a[i])
		}
	}

	fmt.Fprintln(out, ret)
}

func Max(a, b int) int{
	if a > b {
		return a
	}
	return b
}

func Next() string{
	if !in.Scan() {
		panic("Scan error")
	}
	return in.Text()
}

func NextInt() int{
	ret, _ := strconv.Atoi(Next())
	return ret
}


package main

import(
  "os"
  "bufio"
  "strconv"
  "fmt"
  // "sort"
)

var scanner * bufio.Scanner

func next_int()(result int64){
  if !scanner.Scan() {
    panic("Unable to read next integer. Input has no data remain")
  }
  res, err := strconv.ParseInt(scanner.Text(), 10, 32)
  if err != nil {
    panic(err)
  }
  return res
}

func next_str()(result string){
  if !scanner.Scan() {
    panic("Unable to read next token. Input has no data remain")
  }
  return scanner.Text()
}

func min(a, b int)(int) {
  if a < b {
    return a
  }
  return b;
}

var INF int = 2 * 1000 * 1000 * 1000
var c []int = nil
var next []int = nil
var cost []int = nil
var used []int8 = nil
var stack []int = nil

func check_in_stack(v int)(bool) {
  return (used[v] & (1 << 1)) > 0
}
func invert_in_stack(v int)() {
  used[v] = used[v] ^ (1 << 1);
  return
}
func is_used(v int)(bool){
  return (used[v] & 1) > 0
}
func set_used(v int)(){
  used[v] = used[v] ^ 1;
}

func get_min_cost(v int)(int, bool){
  set_used(v)
  invert_in_stack(v)
  stack = append(stack, v)

  res := INF
  solved_before := false
  if check_in_stack(next[v]) { // cycle found!
    for j := len(stack)-1; j >= 0; j-- {
      res = min(res, c[stack[j]])
      if stack[j] == next[v] {
        break
      }
    }
  } else if is_used(next[v]) { // it was solved before, just extract the answer
    res = cost[next[v]]
    solved_before = true
  } else { // no cycle, not solved before - go deeper
    ch_res, solved_ch := get_min_cost(next[v])
    res = ch_res
    solved_before = solved_ch
  }

  invert_in_stack(v)
  stack = stack[0:len(stack)-1]
  return res, solved_before
}

func main() {

  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)

  n := int(next_int());
  c = make([]int, n, n)
  next = make([]int, n, n)
  cost = make([]int, n, n)
  stack = make([]int, 0 , n)
  used = make([]int8, n, n)

  for i := 0; i < n; i++ {
    c[i] = int(next_int());
    used[i] = 0
    cost[i] = INF
  }
  for i := 0; i < n; i++ {
    next[i] = int(next_int() - 1);
  }
  var ans int = 0
  for i := 0; i < n; i++ {
    if 0 == used[i] {
      minc, solved_before := get_min_cost(i)
      if !solved_before {
        ans = ans + minc
      }
    }
  }
  fmt.Println(ans);
  return
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1027D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}

	var n, s int
	Fscan(in, &n)
	c := make([]int, n)
	for i := range c {
		Fscan(in, &c[i])
	}
	g := make([]int, n)
	deg := make([]int, n)
	for i := range g {
		Fscan(in, &g[i])
		g[i]--
		deg[g[i]]++
	}

	vis := make([]bool, n)
	q := []int{}
	for i, d := range deg {
		if d == 0 {
			q = append(q, i)
		}
	}
	for len(q) > 0 {
		v := q[0]
		vis[v] = true
		q = q[1:]
		w := g[v]
		deg[w]--
		if deg[w] == 0 {
			q = append(q, w)
		}
	}

	var f func(int) int
	f = func(v int) int {
		vis[v] = true
		if vis[g[v]] {
			return c[v]
		}
		return min(f(g[v]), c[v])
	}
	for i, b := range vis {
		if !b {
			s += f(i)
		}
	}
	Fprint(out, s)
}

func main() { CF1027D(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1027D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, s int
	Fscan(in, &n)
	c := make([]int, n)
	for i := range c {
		Fscan(in, &c[i])
	}
	g := make([]int, n)
	deg := make([]int, n)
	for i := range g {
		Fscan(in, &g[i])
		g[i]--
		deg[g[i]]++
	}

	vis := make([]bool, n)
	q := []int{}
	for i, d := range deg {
		if d == 0 {
			q = append(q, i)
		}
	}
	for len(q) > 0 {
		v := q[0]
		q = q[1:]
		vis[v] = true
		w := g[v]
		deg[w]--
		if deg[w] == 0 {
			q = append(q, w)
		}
	}

	for i, b := range vis {
		if !b {
			min := int(1e9)
			for v := i; !vis[v]; v = g[v] {
				vis[v] = true
				if c[v] < min {
					min = c[v]
				}
			}
			s += min
		}
	}
	Fprint(out, s)
}

func main() { CF1027D(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1027D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, min, s int
	Fscan(in, &n)
	c := make([]int, n)
	for i := range c {
		Fscan(in, &c[i])
	}
	g := make([]int, n)
	deg := make([]int, n)
	for i := range g {
		Fscan(in, &g[i])
		g[i]--
		deg[g[i]]++
	}

	vis := make([]int8, n)
	var f func(v int)
	f = func(v int) {
		if vis[v] == 2 {
			return
		}
		vis[v]++
		if vis[v] == 2 && c[v] < min {
			min = c[v]
		}
		f(g[v])
	}
	for i, d := range deg {
		if d == 0 {
			min = 1e9
			f(i)
			if min < 1e9 {
				s += min
			}
		}
	}
	for i, c := range vis {
		if c == 0 {
			min = 1e9
			f(i)
			if min < 1e9 {
				s += min
			}
		}
	}
	Fprint(out, s)
}

func main() { CF1027D(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1027D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, min, s int
	Fscan(in, &n)
	c := make([]int, n)
	for i := range c {
		Fscan(in, &c[i])
	}
	g := make([]int, n)
	for i := range g {
		Fscan(in, &g[i])
		g[i]--
	}

	vis := make([]int8, n)
	var f func(v int)
	f = func(v int) {
		if vis[v] == 2 || vis[v] == 0 && vis[g[v]] == 2 {
			return
		}
		vis[v]++
		if vis[v] == 2 && c[v] < min {
			min = c[v]
		}
		f(g[v])
	}
	for i, c := range vis {
		if c == 0 {
			min = 1e9
			f(i)
			if min < 1e9 {
				s += min
			}
		}
	}
	Fprint(out, s)
}

func main() { CF1027D(os.Stdin, os.Stdout) }


package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
func CF1506F(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct{ r, c int }

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]pair, n, n+1)
		for i := range a {
			Fscan(in, &a[i].r)
		}
		for i := range a {
			Fscan(in, &a[i].c)
		}
		a = append(a, pair{1, 1})
		sort.Slice(a, func(i, j int) bool { return a[i].r < a[j].r })
		s := 0
		for i, p := range a[:n] {
			rc := p.r - p.c
			if d := a[i+1].r - a[i+1].c - rc; d+rc&1 > 1 {
				s += (d + rc&1) / 2
			} else if d == 0 && rc&1 == 0 {
				s += a[i+1].r - p.r
			}
		}
		Fprintln(out, s)
	}
}

func main() { CF1506F(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	_i, buf := 1<<12, make([]byte, 1<<12)
	rc := func() byte {
		if _i == 1<<12 {
			os.Stdin.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}
	type pair struct{ r, c int }
	for T := r(); T > 0; T-- {
		n := r()
		a := make([]pair, n, n+1)
		for i := range a {
			a[i].r = r()
		}
		for i := range a {
			a[i].c = r()
		}
		a = append(a, pair{1, 1})
		sort.Slice(a, func(i, j int) bool { return a[i].r < a[j].r })
		s := 0
		for i, p := range a[:n] {
			if d := a[i+1].r - a[i+1].c - (p.r-p.c)>>1<<1; d > 1 {
				s += d >> 1
			} else if d == 0 {
				s += a[i+1].r - p.r
			}
		}
		Fprintln(out, s)
	}
}

package main
import (
	"bufio"
	. "fmt"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	type pair struct{ r, c int }
	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]pair, n, n+1)
		for i := range a {
			Fscan(in, &a[i].r)
		}
		for i := range a {
			Fscan(in, &a[i].c)
		}
		a = append(a, pair{1, 1})
		sort.Slice(a, func(i, j int) bool { return a[i].r < a[j].r })
		s := 0
		for i, p := range a[:n] {
			if d := a[i+1].r - a[i+1].c - (p.r-p.c)/2*2; d > 1 {
				s += d / 2
			} else if d == 0 {
				s += a[i+1].r - p.r
			}
		}
		Println(s)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		t--
		var n int
		fmt.Fscan(in, &n)
		rs := make([]int, n)
		cs := make([]int, n)
		for i := range rs {
			fmt.Fscan(in, &rs[i])
			rs[i]--
		}
		for i := range cs {
			fmt.Fscan(in, &cs[i])
			cs[i]--
		}
		sort.Ints(rs)
		sort.Ints(cs)
		calc := func(r, c int) int64 {
			if r == c {
				return int64(r)
			}
			if c == r-1 {
				return 0
			}
			sr := c + 1
			return int64((r - sr + 1) / 2)
		}
		cost := calc(rs[0], cs[0])
		for i := 1; i<n; i++ {
			if rs[i] == cs[i] {
				cost += calc(rs[i], cs[i]) - calc(rs[i-1], cs[i-1])
				continue
			}
			sr := rs[i-1]+cs[i]-cs[i-1]
			if sr == cs[i] {
				sr++
			}
			if (sr+cs[i])%2 == 0 {
				cost += int64((rs[i] - sr) / 2)
			}else {
				cost += int64((rs[i] - sr + 1)/2)
			}
			if (sr+cs[i])%2 == 0 && sr == rs[i] {
				cost+= int64(cs[i] - cs[i-1])
			}
		}
		fmt.Println(cost)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		t--
		var n int
		fmt.Fscan(in, &n)
		rs := make([]int, n)
		cs := make([]int, n)
		for i := range rs {
			fmt.Fscan(in, &rs[i])
			rs[i]--
		}
		for i := range cs {
			fmt.Fscan(in, &cs[i])
			cs[i]--
		}
		sort.Ints(rs)
		sort.Ints(cs)
		calc := func(r, c int) int64 {
			if r == c {
				return int64(r)
			}
			if c == r-1 {
				return 0
			}
			sr := c + 1
			return int64((r - sr + 1) / 2)
		}
		cost := calc(rs[0], cs[0])
		for i := 1; i<n; i++ {
			if rs[i] == cs[i] {
				cost += calc(rs[i], cs[i]) - calc(rs[i-1], cs[i-1])
				continue
			}
			sr := rs[i-1]+cs[i]-cs[i-1]
			if sr == cs[i] {
				sr++
			}
			if (sr+cs[i])%2 == 0 {
				cost += int64((rs[i] - sr) / 2)
			}else {
				cost += int64((rs[i] - sr + 1)/2)
			}
			if (sr+cs[i])%2 == 0 && sr == rs[i] {
				cost++
			}
		}
		fmt.Println(cost)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		t--
		var n int
		fmt.Fscan(in, &n)
		rs := make([]int, n)
		cs := make([]int, n)
		for i := range rs {
			fmt.Fscan(in, &rs[i])
			rs[i]--
		}
		for i := range rs {
			fmt.Fscan(in, &cs[i])
			cs[i]--
		}
		calc := func(r, c int) int64 {
			if r == c {
				return int64(r)
			}
			if c == r-1 {
				return 0
			}
			sr := c + 1
			return int64((r - sr + 1) / 2)
		}
		cost := calc(rs[0], cs[0])
		for i := 1; i<n; i++ {
			if rs[i] == cs[i] {
				cost += calc(rs[i], cs[i]) - calc(rs[i-1], cs[i-1])
				continue
			}
			sr := rs[i-1]+cs[i]-cs[i-1]
			if sr == cs[i] {
				sr++
			}
			cost += calc(rs[i], cs[i])
			cost -= calc(sr, cs[i])
		}
		fmt.Println(cost)
	}
}


package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
)

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	solve := func(Case int) {
		var n, k int
		Fscan(in, &n, &k)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			a[i]--
		}

		dp := make([][]int, n+1)
		for i := range dp {
			dp[i] = make([]int, n+1)
		}

		for i, v := range a {
			for j := 0; j <= i; j++ {
				if v == i-j {
					dp[i+1][j] = max(dp[i+1][j], dp[i][j]+1)
				} else {
					dp[i+1][j] = max(dp[i+1][j], dp[i][j])
				}
				dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j])
			}
		}

		for i, dv := range dp[n] {
			if dv >= k {
				Fprintln(out, i)
				return
			}
		}
		Fprintln(out, -1)
	}

	T := 1
	Fscan(in, &T)
	for Case := 1; Case <= T; Case++ {
		solve(Case)
	}

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}
func main()  { run(os.Stdin, os.Stdout) }
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}


package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1304E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	const mx = 18
	pa := [mx][1e5]int{}
	dep := make([]int, n)
	var dfs func(v, p, d int)
	dfs = func(v, p, d int) {
		pa[0][v] = p
		dep[v] = d
		for _, w := range g[v] {
			if w != p {
				dfs(w, v, d+1)
			}
		}
	}
	dfs(0, -1, 0)
	for k := 0; k+1 < mx; k++ {
		for v := 0; v < n; v++ {
			if p := pa[k][v]; p == -1 {
				pa[k+1][v] = -1
			} else {
				pa[k+1][v] = pa[k][p]
			}
		}
	}
	_lca := func(v, w int) int {
		if dep[v] > dep[w] {
			v, w = w, v
		}
		for k := uint(0); k < mx; k++ {
			if (dep[w]-dep[v])>>k&1 == 1 {
				w = pa[k][w]
			}
		}
		if v == w {
			return v
		}
		for k := mx - 1; k >= 0; k-- {
			if pa[k][v] != pa[k][w] {
				v, w = pa[k][v], pa[k][w]
			}
		}
		return pa[0][v]
	}
	_d := func(v, w int) int { return dep[v] + dep[w] - dep[_lca(v, w)]<<1 }

_q:
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		for _, d := range []int{_d(a, b), _d(a, x) + _d(b, y) + 1, _d(a, y) + _d(b, x) + 1} {
			if d <= k && d&1 == k&1 {
				Fprintln(out, "YES")
				continue _q
			}
		}
		Fprintln(out, "NO")
	}
}

func main() { CF1304E(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1304E(_r io.Reader, _w io.Writer) {
	in := bufio.NewScanner(_r)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	read := func() (x int) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int(b-'0')
		}
		return
	}

	n := read()
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		v, w := read()-1, read()-1
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		a, b := st[l][k], st[r-(1<<k)+1][k]
		if a.v < b.v {
			return a.i
		}
		return b.i
	}
	lca := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	_d := func(v, w int) int { return dis[v] + dis[w] - dis[lca(v, w)]<<1 }

_q:
	for q := read(); q > 0; q-- {
		x, y, a, b, k := read()-1, read()-1, read()-1, read()-1, read()
		for _, d := range []int{_d(a, b), _d(a, x) + _d(b, y) + 1, _d(a, y) + _d(b, x) + 1} {
			if d <= k && d&1 == k&1 {
				Fprintln(out, "YES")
				continue _q
			}
		}
		Fprintln(out, "NO")
	}
}

func main() { CF1304E(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1304E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	calcLCA := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	calcDis := func(v, w int) int { return dis[v] + dis[w] - dis[calcLCA(v, w)]<<1 }

	solve := func() bool {
		if d := calcDis(a, b); d <= k && d&1 == k&1 {
			return true
		}
		if d := calcDis(a, x) + calcDis(b, y) + 1; d <= k && d&1 == k&1 {
			return true
		}
		d := calcDis(a, y) + calcDis(b, x) + 1
		return d <= k && d&1 == k&1
	}
	Fscan(in, &q)
	for _case := 1; _case <= q; _case++ {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		if solve() {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

func main() { CF1304E(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1304E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	const mx = 18
	pa := make([][mx]int, n)
	dep := make([]int, n)
	var dfs func(v, p, d int)
	dfs = func(v, p, d int) {
		pa[v][0] = p
		dep[v] = d
		for _, w := range g[v] {
			if w != p {
				dfs(w, v, d+1)
			}
		}
	}
	dfs(0, -1, 0)
	for k := 0; k+1 < mx; k++ {
		for v := range pa {
			if p := pa[v][k]; p == -1 {
				pa[v][k+1] = -1
			} else {
				pa[v][k+1] = pa[p][k]
			}
		}
	}
	_lca := func(v, w int) int {
		if dep[v] > dep[w] {
			v, w = w, v
		}
		for k := uint(0); k < mx; k++ {
			if (dep[w]-dep[v])>>k&1 == 1 {
				w = pa[w][k]
			}
		}
		if v == w {
			return v
		}
		for k := mx - 1; k >= 0; k-- {
			if pa[v][k] != pa[w][k] {
				v, w = pa[v][k], pa[w][k]
			}
		}
		return pa[v][0]
	}
	_d := func(v, w int) int { return dep[v] + dep[w] - dep[_lca(v, w)]<<1 }

_q:
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		for _, d := range []int{_d(a, b), _d(a, x) + _d(b, y) + 1, _d(a, y) + _d(b, x) + 1} {
			if d <= k && d&1 == k&1 {
				Fprintln(out, "YES")
				continue _q
			}
		}
		Fprintln(out, "NO")
	}
}

func main() { CF1304E(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

type Graph struct {
	n     int
	m     int
	edges int
	fst   []int
	nxt   []int
	end   []int
}

func (g *Graph) Add(a, b int) {
	g.nxt[g.edges] = g.fst[a]
	g.fst[a] = g.edges
	g.end[g.edges] = b
	g.edges++
}

func (g *Graph) Adjacent(a int, fn func(b int)) {
	for u := g.fst[a]; u != -1; u = g.nxt[u] {
		fn(g.end[u])
	}
}

func NewGraph(n, m int) *Graph {
	g := &Graph{n, m, 0, make([]int, n), make([]int, m), make([]int, m)}
	for i := 0; i < n; i++ {
		g.fst[i] = -1
	}
	return g
}

type LCATree struct {
	g            *Graph
	maxLogHeight int
	root         int
	parent       []int
	up           [][]int
	h            []int
	entrance     []int
	exit         []int
}

func (t *LCATree) GoDown(v int, par int, time int) int {
	t.parent[v] = par
	t.up[v][0] = par
	t.entrance[v] = time
	time++
	for i := 1; i < t.maxLogHeight; i++ {
		t.up[v][i] = t.up[t.up[v][i-1]][i-1]
	}
	t.g.Adjacent(v, func(to int) {
		if to == par {
			return
		}
		t.h[to] = t.h[v] + 1
		time = t.GoDown(to, v, time)
	})
	t.exit[v] = time
	time++
	// stderr("vertex", v, "entrance", t.entrance[v], "exit", t.exit[v])
	return time
}

func (t *LCATree) Prepare() {
	t.GoDown(t.root, t.root, 0)
}

func (t *LCATree) IsParent(u, v int) bool {
	return t.entrance[u] <= t.entrance[v] && t.exit[u] >= t.exit[v]
}

func (t *LCATree) LCA(u, v int) int {
	if t.IsParent(u, v) {
		return u
	}
	for h := t.maxLogHeight - 1; h >= 0; {
		for h >= 0 && t.IsParent(t.up[u][h], v) {
			h--
		}
		if h >= 0 {
			u = t.up[u][h]
		}
	}
	return t.up[u][0]
}

func (t *LCATree) Distance(u, v int) int {
	lca := t.LCA(u, v)
	// stderr("lca of", u, v, "is", lca)
	return t.h[u] + t.h[v] - 2*t.h[lca]
}

func NewLCATree(g *Graph) *LCATree {
	parent := make([]int, g.n)
	var mh = 18
	up := make([][]int, g.n)
	for i := 0; i < g.n; i++ {
		up[i] = make([]int, mh)
	}
	h := make([]int, g.n)
	entrance := make([]int, g.n)
	exit := make([]int, g.n)
	return &LCATree{g, mh, 0, parent, up, h, entrance, exit}
}

func check(d, k int) bool {
	return d <= k && (d&1) == (k&1)
}

func main() {
	inf := os.Stdin
	out := os.Stdout
	// f, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)

	n := NextInt()
	g := NewGraph(n, 2*(n-1))

	for i := 0; i < n-1; i++ {
		u, v := NextInt(), NextInt()
		u--
		v--
		g.Add(u, v)
		g.Add(v, u)
	}
	t := NewLCATree(g)
	t.Prepare()
	q := NextInt()

	for i := 0; i < q; i++ {
		x, y, a, b, k := NextInt(), NextInt(), NextInt(), NextInt(), NextInt()
		x--
		y--
		a--
		b--
		// stderr("query", x, y, a, b, k)
		dab := t.Distance(a, b)
		dax := t.Distance(a, x)
		day := t.Distance(a, y)
		dbx := t.Distance(b, x)
		dby := t.Distance(b, y)
		// stderr("dab", dab, "ax ay", dax, dby, "ay bx", day, dbx)
		if check(dab, k) || check(dax+dby+1, k) || check(day+dbx+1, k) {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanf(reader, "%d\n", &n)

	g := make(map[int][]int)
	for i := 0; i < n-1; i++ {
		var u, v int
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		g[u] = append(g[u], v)
		g[v] = append(g[v], u)
	}

	t := newTree(g, n)

	var q int
	fmt.Fscanf(reader, "%d\n", &q)

	for i := 0; i < q; i++ {
		var x, y, a, b, k int
		fmt.Fscanf(reader, "%d %d %d %d %d\n", &x, &y, &a, &b, &k)
		fmt.Fprintf(writer, "%s\n", can(t, x, y, a, b, k))
	}
}

func can(t *Tree, x, y, a, b, k int) string {
	d1 := t.path(a, b)
	if isOk(d1, k) {
		return "YES"
	}

	d2 := t.path(a, x) + 1 + t.path(y, b)
	if isOk(d2, k) {
		return "YES"
	}

	d3 := t.path(a, y) + 1 + t.path(x, b)
	if isOk(d3, k) {
		return "YES"
	}

	return "NO"
}

func isOk(d, k int) bool {
	return d <= k && (k-d)%2 == 0
}

type Tree struct {
	n   int
	t   map[int][]int
	par map[int]int
	lv  map[int]int
	anc [][]int
}

func newTree(g map[int][]int, n int) *Tree {
	t := new(Tree)
	t.t = g
	t.n = n
	t.par = make(map[int]int, n)
	t.lv = make(map[int]int, n)

	t.dfs(1, 0, 0)
	t.makeLcaTable()

	return t
}

func (t *Tree) dfs(r, l, p int) {
	t.par[r] = p
	t.lv[r] = l

	for _, ch := range t.t[r] {
		if ch == p {
			continue
		}
		t.dfs(ch, l+1, r)
	}
}

const Lv = 21

func (t *Tree) makeLcaTable() {
	t.anc = make([][]int, Lv)
	for j := 0; j < Lv; j++ {
		t.anc[j] = make([]int, t.n+1)
	}

	for i := 1; i <= t.n; i++ {
		t.anc[0][i] = t.par[i]
	}

	for j := 1; j < Lv; j++ {
		for i := 1; i <= t.n; i++ {
			t.anc[j][i] = t.anc[j-1][t.anc[j-1][i]]
		}
	}
}

func (t *Tree) lca(u, v int) int {
	if t.lv[u] > t.lv[v] {
		u, v = v, u
	}

	for j := Lv - 1; j >= 0; j-- {
		a := t.anc[j][v]
		if a != 0 && t.lv[a] >= t.lv[u] {
			v = a
		}
	}

	if t.lv[u] != t.lv[v] {
		panic(fmt.Sprintf("Expect Lv(%d) = Lv(%d)", u, v))
	}

	if u == v {
		return u
	}

	for j := Lv - 1; j >= 0; j-- {
		if t.anc[j][u] != t.anc[j][v] {
			u = t.anc[j][u]
			v = t.anc[j][v]
		}
	}

	return t.par[u]
}

func (t *Tree) path(u, v int) int {
	lca := t.lca(u, v)
	return t.lv[u] + t.lv[v] - 2*t.lv[lca]
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1304E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	const mx = 17
	pa := make([][mx]int, n)
	dep := make([]int, n)
	var dfs func(v, p, d int)
	dfs = func(v, p, d int) {
		pa[v][0] = p
		dep[v] = d
		for _, w := range g[v] {
			if w != p {
				dfs(w, v, d+1)
			}
		}
	}
	dfs(0, -1, 0)
	for k := 0; k+1 < mx; k++ {
		for v := range pa {
			if p := pa[v][k]; p == -1 {
				pa[v][k+1] = -1
			} else {
				pa[v][k+1] = pa[p][k]
			}
		}
	}
	_lca := func(v, w int) int {
		if dep[v] > dep[w] {
			v, w = w, v
		}
		for k := uint(0); k < mx; k++ {
			if (dep[w]-dep[v])>>k&1 == 1 {
				w = pa[w][k]
			}
		}
		if v == w {
			return v
		}
		for k := mx - 1; k >= 0; k-- {
			if pa[v][k] != pa[w][k] {
				v, w = pa[v][k], pa[w][k]
			}
		}
		return pa[v][0]
	}
	_d := func(v, w int) int { return dep[v] + dep[w] - dep[_lca(v, w)]<<1 }

_q:
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		for _, d := range []int{_d(a, b), _d(a, x) + _d(b, y) + 1, _d(a, y) + _d(b, x) + 1} {
			if d <= k && d&1 == k&1 {
				Fprintln(out, "YES")
				continue _q
			}
		}
		Fprintln(out, "NO")
	}
}

func main() { CF1304E(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1304E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	const mx = 18
	pa := [1e5][mx]int{}
	dep := make([]int, n)
	var dfs func(v, p, d int)
	dfs = func(v, p, d int) {
		pa[v][0] = p
		dep[v] = d
		for _, w := range g[v] {
			if w != p {
				dfs(w, v, d+1)
			}
		}
	}
	dfs(0, -1, 0)
	for k := 0; k+1 < mx; k++ {
		for v := range pa {
			if p := pa[v][k]; p == -1 {
				pa[v][k+1] = -1
			} else {
				pa[v][k+1] = pa[p][k]
			}
		}
	}
	_lca := func(v, w int) int {
		if dep[v] > dep[w] {
			v, w = w, v
		}
		for k := uint(0); k < mx; k++ {
			if (dep[w]-dep[v])>>k&1 == 1 {
				w = pa[w][k]
			}
		}
		if v == w {
			return v
		}
		for k := mx - 1; k >= 0; k-- {
			if pa[v][k] != pa[w][k] {
				v, w = pa[v][k], pa[w][k]
			}
		}
		return pa[v][0]
	}
	_d := func(v, w int) int { return dep[v] + dep[w] - dep[_lca(v, w)]<<1 }

_q:
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		for _, d := range []int{_d(a, b), _d(a, x) + _d(b, y) + 1, _d(a, y) + _d(b, x) + 1} {
			if d <= k && d&1 == k&1 {
				Fprintln(out, "YES")
				continue _q
			}
		}
		Fprintln(out, "NO")
	}
}

func main() { CF1304E(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1304E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	lca := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	_d := func(v, w int) int { return dis[v] + dis[w] - dis[lca(v, w)]<<1 }

_q:
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		for _, d := range []int{_d(a, b), _d(a, x) + _d(b, y) + 1, _d(a, y) + _d(b, x) + 1} {
			if d <= k && d&1 == k&1 {
				Fprintln(out, "YES")
				continue _q
			}
		}
		Fprintln(out, "NO")
	}
}

func main() { CF1304E(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	calcLCA := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	calcDis := func(v, w int) int { return dis[v] + dis[w] - 2*dis[calcLCA(v, w)] }

	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		loop := calcDis(x, y) + 1
		if dab := calcDis(a, b); k >= dab && (k-dab)%loop == 0 {
			Fprintln(out, "YES")
			continue
		}
		if lcaXA := calcLCA(x, a); lcaXA != x && lcaXA != a {
			x, y = y, x
		}
		if dab := calcDis(x, a) + calcDis(y, b) + 1; k >= dab && (k-dab)%loop == 0 {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

func main() { run(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1304E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	calcLCA := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	calcDis := func(v, w int) int { return dis[v] + dis[w] - dis[calcLCA(v, w)]<<1 }

	solve := func() bool {
		dab := calcDis(a, b)
		if dab <= k && dab&1 == k&1 {
			return true
		}
		lcaAB, lcaXY := calcLCA(a, b), calcLCA(x, y)
		if lcaAB == lcaXY {
			if calcDis(a, x) > calcDis(a, y) {
				x, y = y, x
			}
			if calcDis(b, y) > calcDis(b, x) {
				x, y = y, x
			}
			dab2 := calcDis(x, a) + calcDis(y, b) + 1
			if dab2 <= k && dab2&1 == k&1 {
				return true
			}
			if dxy := calcDis(x, y); dxy&1 == 0 {
				if dab2 < dab {
					dab = dab2
				}
				return dab+dxy+1 <= k
			}
			return false
		}
		if lcaAB == b {
			a, b = b, a
		}
		if lcaXY == y {
			x, y = y, x
		}
		if dxy := calcDis(x, y); dxy&1 == 0 {
			dax := calcDis(a, x)
			return dab+2*dax+dxy+1 <= k
		}
		return false
	}
	Fscan(in, &q)
	for _case := 1; _case <= q; _case++ {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		//  expected YES, found NO [83rd token]
		if _case == 83 {
			Fprintln(out, Sprintf("%d,%d,%d,%d,%d", x,y,a,b,k))
			return
		}
		if solve() {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

func main() { CF1304E(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1304E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	calcLCA := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	calcDis := func(v, w int) int { return dis[v] + dis[w] - dis[calcLCA(v, w)]<<1 }

	solve := func() bool {
		dab := calcDis(a, b)
		if dab <= k && dab&1 == k&1 {
			return true
		}
		lcaAB, lcaXY := calcLCA(a, b), calcLCA(x, y)
		if lcaAB == lcaXY {
			if lcaAX := calcLCA(a, x); lcaAX != a && lcaAX != x {
				x, y = y, x
			}
			dab2 := calcDis(x, a) + calcDis(y, b) + 1
			if dab2 <= k && dab2&1 == k&1 {
				return true
			}
			if dxy := calcDis(x, y); dxy&1 == 0 {
				if dab2 < dab {
					dab = dab2
				}
				return dab+dxy+1 <= k
			}
			return false
		}
		if lcaAB == b {
			a, b = b, a
		}
		if lcaXY == y {
			x, y = y, x
		}
		if dxy := calcDis(x, y); dxy&1 == 0 {
			dax := calcDis(a, x)
			return dab+2*dax+dxy+1 <= k
		}
		return false
	}
	for Fscan(in, &q); q > 0; q-- {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		if solve() {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

func main() { CF1304E(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1304E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	calcLCA := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	calcDis := func(v, w int) int { return dis[v] + dis[w] - dis[calcLCA(v, w)]<<1 }

	solve := func() bool {
		dab := calcDis(a, b)
		if dab <= k && dab&1 == k&1 {
			return true
		}
		lcaAB, lcaXY := calcLCA(a, b), calcLCA(x, y)
		if lcaAB == lcaXY {
			if calcDis(a, x) > calcDis(a, y) {
				x, y = y, x
			}
			dab2 := calcDis(x, a) + calcDis(y, b) + 1
			if dab2 <= k && dab2&1 == k&1 {
				return true
			}
			if dxy := calcDis(x, y); dxy&1 == 0 {
				if dab2 < dab {
					dab = dab2
				}
				return dab+dxy+1 <= k
			}
			return false
		}
		if lcaAB == b {
			a, b = b, a
		}
		if lcaXY == y {
			x, y = y, x
		}
		if dxy := calcDis(x, y); dxy&1 == 0 {
			dax := calcDis(a, x)
			return dab+2*dax+dxy+1 <= k
		}
		return false
	}
	Fscan(in, &q)
	for _case := 1; _case <= q; _case++ {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		if _case == 62 {
			Fprintln(out, x,y,a,b,k)
			return
		}
		if solve() {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

func main() { CF1304E(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1304E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	calcLCA := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	calcDis := func(v, w int) int { return dis[v] + dis[w] - dis[calcLCA(v, w)]<<1 }

	solve := func() bool {
		dab := calcDis(a, b)
		if dab <= k && dab&1 == k&1 {
			return true
		}
		lcaAB, lcaXY := calcLCA(a, b), calcLCA(x, y)
		if lcaAB == lcaXY {
			if calcDis(a, x) > calcDis(a, y) {
				x, y = y, x
			}
			dab2 := calcDis(x, a) + calcDis(y, b) + 1
			if dab2 <= k && dab2&1 == k&1 {
				return true
			}
			if dxy := calcDis(x, y); dxy&1 == 0 {
				if dab2 < dab {
					dab = dab2
				}
				return dab+dxy+1 <= k
			}
			return false
		}
		if lcaAB == b {
			a, b = b, a
		}
		if lcaXY == y {
			x, y = y, x
		}
		if dxy := calcDis(x, y); dxy&1 == 0 {
			dax := calcDis(a, x)
			return dab+2*dax+dxy+1 <= k
		}
		return false
	}
	Fscan(in, &q)
	for _case := 1; _case <= q; _case++ {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		if solve() {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

func main() { CF1304E(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1304E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	calcLCA := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	calcDis := func(v, w int) int { return dis[v] + dis[w] - dis[calcLCA(v, w)]<<1 }

	solve := func() bool {
		dab := calcDis(a, b)
		if dab <= k && dab&1 == k&1 {
			return true
		}
		lcaAB, lcaXY := calcLCA(a, b), calcLCA(x, y)
		if lcaAB == lcaXY {
			if calcDis(a, x) > calcDis(a, y) {
				x, y = y, x
			}
			if calcDis(b, y) > calcDis(b, x) {
				x, y = y, x
			}
			dab2 := calcDis(x, a) + calcDis(y, b) + 1
			if dab2 <= k && dab2&1 == k&1 {
				return true
			}
			if dxy := calcDis(x, y); dxy&1 == 0 {
				if dab2 < dab {
					dab = dab2
				}
				return dab+dxy+1 <= k
			}
			return false
		}
		if lcaAB == b {
			a, b = b, a
		}
		if lcaXY == y {
			x, y = y, x
		}
		if dxy := calcDis(x, y); dxy&1 == 0 {
			dax := calcDis(a, x)
			return dab+2*dax+dxy+1 <= k
		}
		return false
	}
	Fscan(in, &q)
	for _case := 1; _case <= q; _case++ {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		if solve() {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

func main() { CF1304E(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1304E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	calcLCA := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	calcDis := func(v, w int) int { return dis[v] + dis[w] - dis[calcLCA(v, w)]<<1 }

	solve := func() bool {
		dab := calcDis(a, b)
		if dab <= k && dab&1 == k&1 {
			return true
		}
		lcaAB, lcaXY := calcLCA(a, b), calcLCA(x, y)
		if lcaAB == lcaXY {
			if calcDis(a, x) > calcDis(a, y) {
				x, y = y, x
			}
			dab2 := calcDis(x, a) + calcDis(y, b) + 1
			if dab2 <= k && dab2&1 == k&1 {
				return true
			}
			if dxy := calcDis(x, y); dxy&1 == 0 {
				if dab2 < dab {
					dab = dab2
				}
				return dab+dxy+1 <= k
			}
			return false
		}
		if lcaAB == b {
			a, b = b, a
		}
		if lcaXY == y {
			x, y = y, x
		}
		if dxy := calcDis(x, y); dxy&1 == 0 {
			dax := calcDis(a, x)
			return dab+2*dax+dxy+1 <= k
		}
		return false
	}
	Fscan(in, &q)
	for _case := 1; _case <= q; _case++ {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		if _case == 62 {
			Fprintln(out, Sprintf("%d,%d,%d,%d,%d", x,y,a,b,k))
			return
		}
		if solve() {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

func main() { CF1304E(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1304E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, q, v, w, x, y, a, b, k int
	Fscan(in, &n)
	g := make([][]int, n)
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	vs := make([]int, 0, 2*n-1)
	pos := make([]int, n)
	depths := make([]int, 0, 2*n-1)
	dis := make([]int, n)
	var dfs func(v, fa, d int)
	dfs = func(v, fa, d int) {
		pos[v] = len(vs)
		vs = append(vs, v)
		depths = append(depths, d)
		dis[v] = d
		for _, w := range g[v] {
			if w != fa {
				dfs(w, v, d+1)
				vs = append(vs, v)
				depths = append(depths, d)
			}
		}
	}
	dfs(0, -1, 0)

	type pair struct{ v, i int }
	var st [][18]pair
	stInit := func(a []int) {
		n := len(a)
		st = make([][18]pair, n)
		for i := range st {
			st[i][0] = pair{a[i], i}
		}
		for j := uint(1); 1<<j <= n; j++ {
			for i := 0; i+(1<<j)-1 < n; i++ {
				st0, st1 := st[i][j-1], st[i+(1<<(j-1))][j-1]
				if st0.v < st1.v {
					st[i][j] = st0
				} else {
					st[i][j] = st1
				}
			}
		}
	}
	stInit(depths)
	stQuery := func(l, r int) int {
		k := uint(bits.Len(uint(r-l+1)) - 1)
		st0, st1 := st[l][k], st[r-(1<<k)+1][k]
		if st0.v < st1.v {
			return st0.i
		}
		return st1.i
	}
	calcLCA := func(v, w int) int {
		pv, pw := pos[v], pos[w]
		if pv > pw {
			pv, pw = pw, pv
		}
		return vs[stQuery(pv, pw)]
	}
	calcDis := func(v, w int) int { return dis[v] + dis[w] - dis[calcLCA(v, w)]<<1 }

	solve := func() bool {
		dab := calcDis(a, b)
		if dab <= k && dab&1 == k&1 {
			return true
		}
		lcaAB, lcaXY := calcLCA(a, b), calcLCA(x, y)
		if lcaAB == lcaXY {
			if calcDis(a, x) > calcDis(a, y) {
				x, y = y, x
			}
			if calcDis(b, y) > calcDis(a, x) {
				x, y = y, x
			}
			dab2 := calcDis(x, a) + calcDis(y, b) + 1
			if dab2 <= k && dab2&1 == k&1 {
				return true
			}
			if dxy := calcDis(x, y); dxy&1 == 0 {
				if dab2 < dab {
					dab = dab2
				}
				return dab+dxy+1 <= k
			}
			return false
		}
		if lcaAB == b {
			a, b = b, a
		}
		if lcaXY == y {
			x, y = y, x
		}
		if dxy := calcDis(x, y); dxy&1 == 0 {
			dax := calcDis(a, x)
			return dab+2*dax+dxy+1 <= k
		}
		return false
	}
	Fscan(in, &q)
	for _case := 1; _case <= q; _case++ {
		Fscan(in, &x, &y, &a, &b, &k)
		x--
		y--
		a--
		b--
		if solve() {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

func main() { CF1304E(os.Stdin, os.Stdout) }