package main

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

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

	var n, v, l int
	Fscan(in, &n)
	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	type pair struct{ to, l int }
	g := make([][]pair, n)
	for w := 1; w < n; w++ {
		Fscan(in, &v, &l)
		v--
		g[v] = append(g[v], pair{w, l})
	}

	const mx = 19
	pa := make([][mx]int, n)
	dep := make([]int64, n)
	var f func(int, int64)
	f = func(v int, d int64) {
		dep[v] = d
		for _, e := range g[v] {
			w := e.to
			pa[w][0] = v
			f(w, d+int64(e.l))
		}
	}
	pa[0][0] = -1
	f(0, 0)
	for k := 0; k+1 < mx; k++ {
		for v := range pa {
			if p := pa[v][k]; p != -1 {
				pa[v][k+1] = pa[p][k]
			} else {
				pa[v][k+1] = -1
			}
		}
	}

	diff := make([]int, n)
	for v, val := range a {
		down := v
	o:
		for {
			ps := pa[v]
			dv := dep[v]
			for i, p := range ps {
				if p == -1 || dv-dep[p] > val {
					if i == 0 {
						break o
					}
					break
				}
				v = p
			}
			val -= dv - dep[v]
		}
		diff[v]--
		diff[down]++
	}

	ans := make([]int, n)
	var f2 func(int) int
	f2 = func(v int) (sum int) {
		for _, e := range g[v] {
			sum += f2(e.to)
		}
		ans[v] = sum
		return sum + diff[v]
	}
	f2(0)
	for _, v := range ans {
		Fprint(out, v, " ")
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF740D(_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()
	a := make([]int, n)
	for i := range a {
		a[i] = read()
	}
	type pair struct{ to, l int }
	g := make([][]pair, n)
	for w := 1; w < n; w++ {
		v, l := read()-1, read()
		g[v] = append(g[v], pair{w, l})
	}

	const mx = 19
	pa := make([][mx]int, n)
	dep := make([]int64, n)
	var f func(int, int64)
	f = func(v int, d int64) {
		dep[v] = d
		for _, e := range g[v] {
			w := e.to
			pa[w][0] = v
			f(w, d+int64(e.l))
		}
	}
	pa[0][0] = -1
	f(0, 0)
	for k := 0; k+1 < mx; k++ {
		for v := range pa {
			if p := pa[v][k]; p != -1 {
				pa[v][k+1] = pa[p][k]
			} else {
				pa[v][k+1] = -1
			}
		}
	}

	diff := make([]int, n)
	for v, _val := range a {
		val := int64(_val)
		down := v
	o:
		for {
			ps := pa[v]
			dv := dep[v]
			for i, p := range ps {
				if p == -1 || dv-dep[p] > val {
					if i == 0 {
						break o
					}
					break
				}
				v = p
			}
			val -= dv - dep[v]
		}
		diff[v]--
		diff[down]++
	}

	ans := make([]int, n)
	var f2 func(int) int
	f2 = func(v int) (sum int) {
		for _, e := range g[v] {
			sum += f2(e.to)
		}
		ans[v] = sum
		return sum + diff[v]
	}
	f2(0)
	for _, v := range ans {
		Fprint(out, v, " ")
	}
}

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

package main

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

func Init() {
}

func Solve(io *FastIO) {
	N := io.NextInt()

	nodes := make([]*Node, N + 1)
	for i := 1; i <= N; i++ {
	  A := io.NextLong()

	  nodes[i] = NewNode(A)
	}

	for i := 2; i <= N; i++ {
	  P := io.NextInt()
	  W := io.NextLong()

	  nodes[P].children = append(nodes[P].children, NewEdge(W, nodes[i]))
	}

	UpdateDepths(nodes[1], 0)
	UpdateSize(nodes[1])
	CountLoss(nodes[1], make([]*Node, 0))

// 	for i := 1; i <= N; i++ {
// 	  fmt.Printf("%d: size = %d, loss = %d, depth = %d\n", i, nodes[i].size, nodes[i].loss, nodes[i].depth)
// 	}
	UpdateLoss(nodes[1])

	for i := 1; i <= N; i++ {
	  io.Printf("%d ", nodes[i].size - nodes[i].loss - 1)
	}
	io.Println()
}

type Node struct {
  children []*Edge
  value int64
  loss int
  size int
  depth int64
}

func NewNode(v int64) *Node {
  return &Node{
    children: make([]*Edge, 0),
    value: v,
  }
}

func UpdateDepths(u *Node, d int64) {
  if u == nil {
    return
  }
  u.depth = d
  for _, e := range u.children {
    UpdateDepths(e.dest, d + e.weight)
  }
}

func UpdateSize(u *Node) {
  if u == nil {
    return
  }
  u.size = 1
  for _, e := range u.children {
    UpdateSize(e.dest)
    u.size += e.dest.size
  }
}

func findDepth(path []*Node, d int64) *Node {
  L := 0
  R := len(path) - 1
  var good *Node
  // for _, u := range path {
  //   fmt.Printf("%d -> ", u.depth)
  // }
  // fmt.Println()
  for L <= R {
    M := (L + R) >> 1
    if path[M].depth < d {
      // fmt.Printf("  M = %d, depth = %d, want = %d\n", M, path[M].depth, d)
      L = M + 1
      good = path[M]
    } else {
      R = M - 1
    }
  }
  return good
}

func CountLoss(u *Node, path []*Node) {
  which := findDepth(path, u.depth - u.value)
  // fmt.Printf("curr depth = %d, search = %d, which = %+v\n", u.depth, u.depth - u.value, which)
  if which != nil {
    which.loss++
  }
  path = append(path, u)
  for _, e := range u.children {
    CountLoss(e.dest, path)
  }
  path = path[:len(path)-1]
}

func UpdateLoss(u *Node) {
  if u == nil {
    return
  }
  for _, e := range u.children {
    UpdateLoss(e.dest)
    u.loss += e.dest.loss
  }
}

type Edge struct {
  weight int64
  dest *Node
}

func NewEdge(w int64, d *Node) *Edge {
  return &Edge{
    weight: w,
    dest: d,
  }
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

func IsLineBreakChar(c byte) bool {
	switch c {
		case 0, '\n', '\r':
			return true
		default:
			return false
	}
}

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

type Edge struct {
    to int
    w  int64
}

func solve() {
    n := readInt()
    a, adj := readInt64Array(n), make([][]Edge, n)
    for i := 1; i < n; i++ {
        p, w := readInt()-1, readInt64()
        adj[p] = append(adj[p], Edge{i, w})
    }

    depth, stack, lim, ans := make([]int64, n), []int{0}, make([]int, n), make([]int, n)

    var dfs func() int
    dfs = func() int {
        curI := len(stack) - 1
        cur := stack[curI]

        l, r := 0, curI
        for l < r {
            if mid := (l+r) / 2; depth[stack[mid]] >= depth[cur]-a[cur] {
                r = mid
            } else {
                l = mid+1
            }
        }
        //fmt.Println(depth, stack, r)
        lim[stack[r]]++

        res := 0
        for _, ch := range adj[cur] {
            depth[ch.to] = depth[cur]+ch.w
            stack = append(stack, ch.to)
            res += dfs()
        }
        ans[cur] = res
        res += 1-lim[cur]
        stack = stack[:curI]
        //fmt.Println(cur, "=>", res)
        return res
    }
    dfs()

    for _, c := range ans {
        fmt.Print(c, " ")
    }
    fmt.Println()

}

// Helpers

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

func main() {
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// Math

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

// IO

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

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

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

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

// Sort

type Ints64 []int64

func (a Ints64) Len() int           { return len(a) }
func (a Ints64) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a Ints64) Less(i, j int) bool { return a[i] < a[j] }

/* Sort tempalte
func (a ) Len() int           { return len(a) }
func (a ) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ) Less(i, j int) bool { return  }
*/

package main

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

type v struct {
	edges []*edge
	id    int
}

type edge struct {
	v int
	w int
}

type pair struct {
	v   int
	sum int64
}

var ans []int
var a []int
var g []*v
var x []int

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		a = make([]int, n)
		ans = make([]int, n)
		x = make([]int, n)
		g = make([]*v, n, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			a[i], _ = strconv.Atoi(scanner.Text())
			g[i] = &v{make([]*edge, 0), i}
		}
		for i := 0; i < n-1; i++ {
			scanner.Scan()
			var p, w int
			p, _ = strconv.Atoi(scanner.Text())
			scanner.Scan()
			w, _ = strconv.Atoi(scanner.Text())
			g[p-1].edges = append(g[p-1].edges, &edge{i + 1, w})
		}
		root := 0
		stack := make([]*pair, 1)
		stack[0] = &pair{
			v:   root,
			sum: 0,
		}
		dfs(root, stack)
		for i := 0; i < n; i++ {
			fmt.Printf("%d ", ans[i])
		}
		fmt.Print("\n")
	}
}

func dfs(cur int, stack []*pair) {
	l := 0
	r := len(stack) - 1
	totalsum := stack[len(stack)-1].sum
	for l < r {
		mid := (l + r + 1) / 2
		cursum := stack[mid].sum
		if totalsum-cursum <= int64(a[cur]) {
			r = mid - 1
		} else {
			l = mid
		}
	}
	if totalsum-stack[l].sum > int64(a[cur]) {
		x[stack[l].v]--
	}
	t := 0
	lastsum := stack[len(stack)-1].sum
	for _, edge := range g[cur].edges {
		stack = append(stack, &pair{edge.v, lastsum + int64(edge.w)})
		dfs(edge.v, stack)
		stack = stack[:len(stack)-1]
		t += ans[edge.v] + 1
	}
	t += x[cur]
	ans[cur] = t
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF740D(_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
	}
	type edge struct{ to, l int }

	n := read()
	a := make([]int64, n)
	for i := range a {
		a[i] = int64(read())
	}
	const mx = 19
	pa := make([][mx]int, n)
	pa[0][0] = -1
	g := make([][]edge, n)
	for w := 1; w < n; w++ {
		v := read() - 1
		g[v] = append(g[v], edge{w, read()})
		pa[w][0] = v
	}
	for k := 0; k+1 < mx; k++ {
		for v := range pa {
			if p := pa[v][k]; p != -1 {
				pa[v][k+1] = pa[p][k]
			} else {
				pa[v][k+1] = -1
			}
		}
	}

	dep := make([]int64, n)
	var f func(int, int64)
	f = func(v int, d int64) {
		dep[v] = d
		for _, e := range g[v] {
			f(e.to, d+int64(e.l))
		}
	}
	f(0, 0)

	diff := make([]int, n)
	for v, val := range a {
		u := v
		dv := dep[v]
		for i := mx - 1; i >= 0; i-- {
			if p := pa[u][i]; p != -1 && dv-dep[p] <= val {
				u = p
			}
		}
		diff[v]++
		diff[u]--
	}

	ans := make([]interface{}, n)
	var f2 func(int) int
	f2 = func(v int) (sum int) {
		for _, e := range g[v] {
			sum += f2(e.to)
		}
		ans[v] = sum
		return sum + diff[v]
	}
	f2(0)
	Fprint(out, ans...)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF740D(_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()
	a := make([]int, n)
	for i := range a {
		a[i] = read()
	}
	type pair struct{ to, l int }
	g := make([][]pair, n)
	for w := 1; w < n; w++ {
		v, l := read()-1, read()
		g[v] = append(g[v], pair{w, l})
	}

	const mx = 19
	pa := make([][mx]int, n)
	dep := make([]int64, n)
	var f func(int, int64)
	f = func(v int, d int64) {
		dep[v] = d
		for _, e := range g[v] {
			w := e.to
			pa[w][0] = v
			f(w, d+int64(e.l))
		}
	}
	pa[0][0] = -1
	f(0, 0)
	for k := 0; k+1 < mx; k++ {
		for v := range pa {
			if p := pa[v][k]; p != -1 {
				pa[v][k+1] = pa[p][k]
			} else {
				pa[v][k+1] = -1
			}
		}
	}

	diff := make([]int, n)
	for v, _val := range a {
		val := int64(_val)
		down := v
	o:
		for {
			ps := pa[v]
			dv := dep[v]
			for i, p := range ps {
				if p == -1 || dv-dep[p] > val {
					if i == 0 {
						break o
					}
					break
				}
				v = p
			}
			val -= dv - dep[v]
		}
		diff[v]--
		diff[down]++
	}

	ans := make([]interface{}, n)
	var f2 func(int) int
	f2 = func(v int) (sum int) {
		for _, e := range g[v] {
			sum += f2(e.to)
		}
		ans[v] = sum
		return sum + diff[v]
	}
	f2(0)
	Fprint(out, ans...)
}

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

package main

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

func Init() {
}

func Solve(io *FastIO) {
	N := io.NextInt()

	nodes := make([]*Node, N + 1)
	for i := 1; i <= N; i++ {
	  A := io.NextLong()

	  nodes[i] = NewNode(A)
	}

	for i := 2; i <= N; i++ {
	  P := io.NextInt()
	  W := io.NextLong()

	  nodes[P].children = append(nodes[P].children, NewEdge(W, nodes[i]))
	}

	UpdateDepths(nodes[1], 0)
	UpdateSize(nodes[1])
	CountLoss(nodes[1], make([]*Node, 0))

	for i := 1; i <= N; i++ {
	  fmt.Printf("%d: size = %d, loss = %d, depth = %d\n", i, nodes[i].size, nodes[i].loss, nodes[i].depth)
	}
	UpdateLoss(nodes[1])

	for i := 1; i <= N; i++ {
	  io.Printf("%d ", nodes[i].size - nodes[i].loss - 1)
	}
	io.Println()
}

type Node struct {
  children []*Edge
  value int64
  loss int
  size int
  depth int64
}

func NewNode(v int64) *Node {
  return &Node{
    children: make([]*Edge, 0),
    value: v,
  }
}

func UpdateDepths(u *Node, d int64) {
  if u == nil {
    return
  }
  u.depth = d
  for _, e := range u.children {
    UpdateDepths(e.dest, d + e.weight)
  }
}

func UpdateSize(u *Node) {
  if u == nil {
    return
  }
  u.size = 1
  for _, e := range u.children {
    UpdateSize(e.dest)
    u.size += e.dest.size
  }
}

func findDepth(path []*Node, d int64) *Node {
  L := 0
  R := len(path) - 1
  var good *Node
  // for _, u := range path {
  //   fmt.Printf("%d -> ", u.depth)
  // }
  // fmt.Println()
  for L <= R {
    M := (L + R) >> 1
    if path[M].depth < d {
      // fmt.Printf("  M = %d, depth = %d, want = %d\n", M, path[M].depth, d)
      L = M + 1
      good = path[M]
    } else {
      R = M - 1
    }
  }
  return good
}

func CountLoss(u *Node, path []*Node) {
  which := findDepth(path, u.depth - u.value)
  // fmt.Printf("curr depth = %d, search = %d, which = %+v\n", u.depth, u.depth - u.value, which)
  if which != nil {
    which.loss++
  }
  path = append(path, u)
  for _, e := range u.children {
    CountLoss(e.dest, path)
  }
  path = path[:len(path)-1]
}

func UpdateLoss(u *Node) {
  if u == nil {
    return
  }
  for _, e := range u.children {
    UpdateLoss(e.dest)
    u.loss += e.dest.loss
  }
}

type Edge struct {
  weight int64
  dest *Node
}

func NewEdge(w int64, d *Node) *Edge {
  return &Edge{
    weight: w,
    dest: d,
  }
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

func IsLineBreakChar(c byte) bool {
	switch c {
		case 0, '\n', '\r':
			return true
		default:
			return false
	}
}

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

type v struct {
	edges []*edge
	id    int
}

type edge struct {
	v int
	w int
}

type pair struct {
	v   int
	sum int
}

var ans []int
var a []int
var g []*v
var x []int

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		a = make([]int, n)
		ans = make([]int, n)
		x = make([]int, n)
		g = make([]*v, n, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			a[i], _ = strconv.Atoi(scanner.Text())
			g[i] = &v{make([]*edge, 0), i}
		}
		for i := 0; i < n-1; i++ {
			scanner.Scan()
			var p, w int
			p, _ = strconv.Atoi(scanner.Text())
			scanner.Scan()
			w, _ = strconv.Atoi(scanner.Text())
			g[p-1].edges = append(g[p-1].edges, &edge{i + 1, w})
		}
		root := 0
		stack := make([]*pair, 1)
		stack[0] = &pair{
			v:   root,
			sum: 0,
		}
		dfs(root, stack)
		for i := 0; i < n; i++ {
			fmt.Printf("%d ", ans[i])
		}
		fmt.Print("\n")
	}
}

func dfs(cur int, stack []*pair) {
	l := 0
	r := len(stack) - 1
	totalsum := stack[len(stack)-1].sum
	for l < r {
		mid := (l + r + 1) / 2
		cursum := stack[mid].sum
		if totalsum-cursum <= a[cur] {
			r = mid - 1
		} else {
			l = mid
		}
	}
	if totalsum-stack[l].sum > a[cur] {
		x[stack[l].v]--
	}
	t := 0
	lastsum := stack[len(stack)-1].sum
	for _, edge := range g[cur].edges {
		stack = append(stack, &pair{edge.v, lastsum + edge.w})
		dfs(edge.v, stack)
		stack = stack[:len(stack)-1]
		t += ans[edge.v] + 1
	}
	t += x[cur]
	ans[cur] = t
}


package main

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

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

func main() {
	// STDOUT MUST BE FLUSHED MANUALLY!!!
	defer writer.Flush()

	var k, n int
	scanf("%d\n", &k)
	for ; k > 0; k-- {
		var s, t string
		ans := "No"
		scanf("%d\n", &n)
		scanf("%s\n", &s)
		scanf("%s\n", &t)
		var sdif, tdif []int
		for i := range s {
			if s[i] != t[i] {
				sdif = append(sdif, i)
				tdif = append(tdif, i)
			}
		}
		if len(sdif) == 2 {
			s, t = s[:sdif[0]]+string(t[tdif[1]])+s[sdif[0]+1:], t[:tdif[1]]+string(s[sdif[0]])+t[tdif[1]+1:]
			if s == t {
				ans = "Yes"
			}
		}
		if k != 1 {
			printf("%s\n", ans)
		} else {
			printf(ans)
		}
	}
}

package main

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

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		// str := strconv.FormatInt(A[i], 10)  // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var k int
var n int
var S, T []rune

func main() {
	k = ReadInt()
	for tc := 0; tc < k; tc++ {
		n = ReadInt()
		S = ReadRuneSlice()
		T = ReadRuneSlice()
		solve()
	}
}

func solve() {
	diff := 0
	for i := 0; i < n; i++ {
		if S[i] != T[i] {
			diff++
		}
	}

	if diff == 0 {
		fmt.Println("Yes")
		return
	}
	if diff != 2 {
		fmt.Println("No")
		return
	}

	pos := [2]int{}
	j := 0
	for i := 0; i < n; i++ {
		if S[i] != T[i] {
			pos[j] = i
			j++
		}
	}

	i, j := pos[0], pos[1]
	if S[i] == S[j] && T[i] == T[j] {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

/*******************************************************************/

package main

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

var inf int = 0x3f3f3f3f
var reader *bufio.Reader

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

var k, n int
var s, t string

func main() {
	fmt.Fscan(reader, &k)
	for ; k != 0; k-- {
		fmt.Fscan(reader, &n, &s, &t)
		if s == t {
			fmt.Println("Yes")
			continue
		}
		var pos []int
		for i := 0; i < n; i++ {
			if s[i] != t[i] {
				pos = append(pos, i)
			}
		}
		if len(pos) != 2 {
			fmt.Println("No")
			continue
		}
		if s[pos[0]] == s[pos[1]] && t[pos[0]] == t[pos[1]] {
			fmt.Println("Yes")
		} else {
			fmt.Println("No")
		}
	}
}

package main

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

const (
	MAX_INT int64 = math.MaxInt64
	MIN_INT int64 = math.MinInt64
)

func main() {

	for t := ScanInt64(); t > 0; t-- {
		n := ScanInt64()
		s := ScanWord()
		t := ScanWord()

		count := 0
		diff := []int{}

		for i := 0; i < int(n); i++ {
			if s[i] != t[i] {
				if count > 2 {
					break
				}
				diff = append(diff, i)
				count++
			}
		}

		if count > 2 || count == 1 {
			fmt.Println("NO")
		} else {
			if count == 0 {
				fmt.Println("YES")
			} else if CanBeEqual(s, t, diff[0], diff[1]) {
				fmt.Println("YES")
			} else {
				fmt.Println("NO")
			}
		}

	}
	close(out)
	outWg.Wait()
}

func CanBeEqual(s, t string, i, j int) bool  {
	if s[i] == s[j] && t[i] == t[j] {
		return true
	}
	return false
}
/*********************** I/O ***********************/

var out chan string
var in *bufio.Scanner
var outWg *sync.WaitGroup

func init() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(2e+5))
	in.Split(bufio.ScanWords)

	out = make(chan string, 16)
	outWg = &sync.WaitGroup{}
	outWg.Add(1)

	writer := bufio.NewWriterSize(os.Stdout, int(2e+5))
	go func(writer *bufio.Writer) {
		defer outWg.Done()
		defer writer.Flush()

		for line := range out {
			writer.WriteString(line + "\n")
		}
	}(writer)
}

func ScanWord() string {
	in.Scan()
	return in.Text()
}

func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}

func ScanInt64() int64 {
	intStr := ScanWord()
	i, err := strconv.ParseInt(intStr, 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

func Max(args ...int64) int64 {
	maxVal := MIN_INT
	for _, v := range args {
		if v > maxVal {
			maxVal = v
		}
	}
	return maxVal
}

func Min(args ...int64) int64 {
	minVal := MAX_INT
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
	}
	return minVal
}

func LCM(a, b int64) int64 {
	return a / GCD(a, b) * b
}

func GCD(a, b int64) int64 {
	if (a == 0) {
		return b
	}
	return GCD(b%a, a)
}

func iToS(v int64) string {
	return strconv.FormatInt(v, 10)
}

package main

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

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

	fmt.Scanf("%d\n", &k)

	iterResults := make([]string, k)
	for j := 0; j < k; j++ {
		reader.ReadString('\n')

		text, _ := reader.ReadString('\n')
		a := []byte(strings.TrimSpace(text))

		text2, _ := reader.ReadString('\n')
		b := []byte(strings.TrimSpace(text2))

		iterResults[j] = handle(a, b)
	}

	for i := 0; i < k; i++ {
		fmt.Println(iterResults[i])
	}
}

func handle(a []byte, b []byte) string {
	res := make(map[string]int)

	for i := 0; i < len(a); i++ {
		if a[i] != b[i] {
			if v, ok := res[string([]byte{a[i], b[i]})]; ok {
				if v == 2 {
					return "No"
				}
				res[string([]byte{a[i], b[i]})] = v + 1
			} else {
				res[string([]byte{a[i], b[i]})] = 1
			}
		}
	}

	if len(res) > 1 {
		return "No"
	}

	if len(res) == 1 {
		for _, v := range res {
			if v != 2 {
				return "No"
			}
		}
	}

	return "Yes"
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)
)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

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

func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

func possible(s1, s2 string) bool {
	if len(s1) != len(s2) {
		return false
	}
	cnt := 0
	change := make([]int, 0, len(s1))
	for i := range s1 {
		if s1[i] != s2[i] {
			cnt++
			change = append(change,i)
		}
	}
	if cnt == 0 {
		return true
	}
	if cnt != 2{
		return false
	}
	return s1[change[0]] == s1[change[1]] && s2[change[0]] == s2[change[1]]
}

func fun() {
	var n int64
	var s1, s2 string
	scanf("%d\n", &n)
	scanf("%s\n", &s1)
	scanf("%s\n", &s2)
	if possible(s1,s2) {
		printf("YES\n")
	} else {
		printf("NO\n")
	}
}

func main() {
	// Flush stdout
	defer writer.Flush()

	var n int
	scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		fun()
	}
}

package main

// Template
import (
	"bufio"
	"fmt"
	"os"
)

var (
	reader = bufio.NewReader(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)
)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

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

func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

func readNumberFromLine(n int64) []int64 {
	arr := make([]int64, n)
	for i := int64(0); i < n; i++ {
		if i == n-1 {
			scanf("%d\n", &arr[i])
		} else {
			scanf("%d ", &arr[i])
		}
	}
	return arr
}

func possible(s1, s2 string) bool {
	if len(s1) != len(s2) {
		return false
	}
	cnt := 0
	change := make([]int, 0, len(s1))
	for i := range s1 {
		if s1[i] != s2[i] {
			cnt++
			change = append(change,i)
		}
	}
	if cnt == 0 {
		return true
	}
	if cnt != 2{
		return false
	}
	return s1[change[0]] == s1[change[1]] && s2[change[0]] == s2[change[1]]
}

func solve() {
	var n int64
	var s1, s2 string
	scanf("%d\n", &n)
	scanf("%s\n", &s1)
	scanf("%s\n", &s2)
	if possible(s1,s2) {
		printf("YES\n")
	} else {
		printf("NO\n")
	}
}

func main() {
	// Flush stdout
	defer writer.Flush()

	var n int
	scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

const (
	MAX_INT int64 = math.MaxInt64
	MIN_INT int64 = math.MinInt64
)

func main() {

	for t := ScanInt64(); t > 0; t-- {
		n := ScanInt64()
		s := ScanWord()
		t := ScanWord()

		count, l, r := 0, 0, 0

		for i := 0; i < int(n); i++ {
			if s[i] != t[i] {
				count++
			}
		}

		if count > 2 || count == 1 {
			fmt.Println("NO")
		} else {
			if count == 0 {
				fmt.Println("YES")
			} else if CanBeEqual(s, t, l, r) {
				fmt.Println("YES")
			} else {
				fmt.Println("NO")
			}
		}

	}
	close(out)
	outWg.Wait()
}

func CanBeEqual(s, t string, i, j int) bool  {
	if s[i] == s[j] && t[i] == t[j] {
		return true
	}
	return false
}
/*********************** I/O ***********************/

var out chan string
var in *bufio.Scanner
var outWg *sync.WaitGroup

func init() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(2e+5))
	in.Split(bufio.ScanWords)

	out = make(chan string, 16)
	outWg = &sync.WaitGroup{}
	outWg.Add(1)

	writer := bufio.NewWriterSize(os.Stdout, int(2e+5))
	go func(writer *bufio.Writer) {
		defer outWg.Done()
		defer writer.Flush()

		for line := range out {
			writer.WriteString(line + "\n")
		}
	}(writer)
}

func ScanWord() string {
	in.Scan()
	return in.Text()
}

func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}

func ScanInt64() int64 {
	intStr := ScanWord()
	i, err := strconv.ParseInt(intStr, 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

func Max(args ...int64) int64 {
	maxVal := MIN_INT
	for _, v := range args {
		if v > maxVal {
			maxVal = v
		}
	}
	return maxVal
}

func Min(args ...int64) int64 {
	minVal := MAX_INT
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
	}
	return minVal
}

func LCM(a, b int64) int64 {
	return a / GCD(a, b) * b
}

func GCD(a, b int64) int64 {
	if (a == 0) {
		return b
	}
	return GCD(b%a, a)
}

func iToS(v int64) string {
	return strconv.FormatInt(v, 10)
}


package main

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

func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

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 < T; t++ {
		var n, k int
		fmt.Fscan(in, &n, &k)

		aa := make([]int64, k)
		for i := 0; i < n; i++ {
			var v int64
			fmt.Fscan(in, &v)
			aa[i%k] = max(aa[i%k], v)
		}

		var tot int64
		for _, v := range aa {
			tot += v
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

var a [100]int

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

	var T int
	for fmt.Fscan(in, &T); T > 0; T-- {
		var n, k int
		fmt.Fscan(in, &n, &k)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		ans := 0
		for i := 0; i < k; i++ {
			max := 0
			for j := i; j < n; j += k {
				if max < a[j] {
					max = a[j]
				}
			}
			ans += max
		}
		fmt.Fprintln(out, ans)
	}
}

package main

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

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

	var T int
	for fmt.Fscan(in, &T); T > 0; T-- {
		var n, k int
		fmt.Fscan(in, &n, &k)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		ans := 0
		for i := 0; i < k; i++ {
			max := 0
			for j := i; j < n; j += k {
				if max < a[j] {
					max = a[j]
				}
			}
			ans += max
		}
		fmt.Fprintln(out, ans)
	}
}

package main

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

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

	var T int
	for fmt.Fscan(in, &T); T > 0; T-- {
		var n, k int
		fmt.Fscan(in, &n, &k)
		a := make([]int, n)
		mx := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			mx[i%k] = max(mx[i%k], a[i])
		}
		ans := 0
		for j := 0; j < k; j++ {
			ans += mx[j]
		}
		fmt.Fprintln(out, ans)
	}
}

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

package main

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

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

	var T, n, k int
	for fmt.Fscan(in, &T); T > 0; T-- {
		fmt.Fscan(in, &n, &k)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		mx := make([]int, n)
		for j := 0; j < n; j++ {
			mx[j%k] = max(mx[j%k], a[j])
		}
		ans := 0
		for j := 0; j < k; j++ {
			ans += mx[j]
		}
		fmt.Fprintln(out, ans)
	}
}

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

package main

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

var (
	input  = bufio.NewReaderSize(os.Stdin, 55000)
	output = bufio.NewWriterSize(os.Stdout, 55000)
)

func main() {
	var t int
	fmt.Fscan(input, &t)
	for t > 0 {
		var k, n int
		fmt.Fscan(input, &n, &k)

		a := make([]int, k)
		for i := 0; i < n; i++ {
			var val int
			fmt.Fscan(input, &val)
			if a[i%k] < val {
				a[i%k] = val
			}
		}

		sum := 0
		for i := 0; i < k; i++ {
			sum += a[i]
		}

		fmt.Println(sum)

		t--
	}
}

package main

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

type arr []int

func (a *arr) Len() int {
	return len(*a)
}
func (a *arr) Less(ai, bi int) bool {
	if (*a)[ai] > (*a)[bi] {
		return true
	} else {
		return false
	}
}

func (a *arr) Swap(ai, bi int) {
	(*a)[ai], (*a)[bi] = (*a)[bi], (*a)[ai]
}

func main() {
	var testCases int

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

	fmt.Fscan(reader, &testCases)

	for testCase := 1; testCase <= testCases; testCase++ {
		var n, k int
		fmt.Fscan(reader, &n)
		fmt.Fscan(reader, &k)

		a := make(arr, n)
		for ai := 0; ai < n; ai++ {
			var i int
			fmt.Fscan(reader, &i)
			a[ai] = i
		}

		output := solveA(n, k, &a)

		fmt.Fprintln(writer, output)
	}
}

func solveA(n, k int, a *arr) int {

	kConsecutive := make([]int, k)
	for i := 0; i < n; i++ {
		if (*a)[i] > kConsecutive[i%k] {
			kConsecutive[i%k] = (*a)[i]
		}
	}
	sum := 0

	for ki := 0; ki < k; ki++ {
		sum += kConsecutive[ki]
	}
	return sum
}

package main

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

type CF1733A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1733A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1733A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1733A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1733A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1733A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1733A
Date: 9/25/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1733/A
Problem: CF1733A
**/
func (in *CF1733A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, k := in.NextInt(), in.NextInt()
		arr := make([]int64, n)
		for i := 0; i < n; i++ {
			arr[i] = in.NextInt64()
		}
		for i := 0; i < n; i++ {
			if arr[i] > arr[i%k] {
				arr[i], arr[i%k] = arr[i%k], arr[i]
			}
		}
		sum := int64(0)
		for i := 0; i < k; i++ {
			sum += arr[i]
		}

		fmt.Println(sum)
	}
}

func NewCF1733A(r *bufio.Reader) *CF1733A {
	return &CF1733A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1733A(bufio.NewReader(os.Stdin)).Run()
}

package main
// github.com/jamesgrimard/codeforces
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 i:=0;i<t;i++{
		var n,m int
		fmt.Fscan(in,&n,&m)
		a:= make([]int,n)
		for j:=0;j<n;j++{
			fmt.Fscan(in,&a[j])
		}
		if n == m {
			var add int
			for j:=0;j<n;j++{
				add += a[j]
			}
			fmt.Fprintln(out,add)
			continue
		}
		for j:=0;j<n-m;j++{
			if a[j] > a[j+m] {
				a[j],a[j+m] = a[j+m],a[j]
			}
		}
		var total int
		for j:=n-1;j>=m;j--{
			var sum int
			for k:=0;k<m;k++{
				sum += a[j-k]
			}
			if sum > total {
				total = sum
			}
		}
		fmt.Fprintln(out,total)
	}
}

// rearrange array to group peaks
// constraint : like indices can only be swapped i mod k == j mod k
// length of the peak corresponds to k consecutive elements
// find largest sum of peak

package main

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

func main() {
	var t, k, n, temp int

	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	fmt.Fscan(r, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(r, &n, &k)
		s := make([]int, k)
		for j := 0; j < n; j++ {
			fmt.Fscan(r, &temp)
			if temp > s[j % k] {
				s[j % k] = temp
			}
		}

		sum := 0
		for j := 0; j < k; j++ {
			sum = sum + s[j]
		}

		fmt.Fprintln(w, sum)
	}
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)
)

func Println(a ...interface{}) { _, _ = fmt.Fprintln(writer, a...) }

func main() {
	defer func() {
		_ = writer.Flush()
	}()

	T := ReadInt()
	for t := 0; t < T; t++ {
		Solve()
	}
}

func Solve() {
	n, k := ReadInt(), ReadInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = ReadInt()
	}
	for i := k; i < n; i++ {
		a[i%k] = Max(a[i], a[i%k])
	}
	ans := int64(0)
	for i := 0; i < k; i++ {
		ans += int64(a[i])
	}
	Println(ans)
}

func ReadInt() (value int) {
	_, _ = fmt.Fscan(reader, &value)
	return
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Float interface {
	~float32 | ~float64
}

type Ordered interface {
	Integer | Float | ~string
}

func Max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

package main

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

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

func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n, k int
		var num int64
		fmt.Fscan(in, &n, &k)
		res := make([]int64, k)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &num)
			res[i%k] = max(res[i%k], num)
		}
		sum := int64(0)
		for i := 0; i < k; i++ {
			sum += res[i]
		}
		fmt.Fprintln(out, sum)
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	txt, _ := reader.ReadString('\n')
	t, _ := strconv.Atoi(strings.TrimSpace(txt))
	for ; t > 0; t-- {
		txt, _ = reader.ReadString('\n')
		arr := strings.Split(strings.TrimSpace(txt), " ")
		n, _ := strconv.Atoi(arr[0])
		k, _ := strconv.Atoi(arr[1])
		txt, _ = reader.ReadString('\n')
		arr = strings.Split(strings.TrimSpace(txt), " ")
		digits := make([]int, n, n)
		a := make(map[int]int)
		for i := 0; i < n; i++ {
			digits[i], _ = strconv.Atoi(arr[i])
			if a[i%k] < digits[i] {
				a[i%k] = digits[i]
			}
		}
		s := 0
		for i := 0; i < k; i++ {
			s += a[i]
		}
		fmt.Println(s)
	}
}

package main

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

var (
	reader  = bufio.NewReader(os.Stdin)
	t, n, k int
)

func max(x, y int64) int64 {
	if x > y {
		return x
	}
	return y
}

func solve(n, k int, a []int64) int64 {
	b := make([]int64, k)
	for i, v := range a {
		b[i%k] = max(b[i%k], v)
	}
	r := int64(0)
	for _, v := range b {
		r += v
	}
	return r
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &k)
		a := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		fmt.Println(solve(n, k, a))
	}
}

package main

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

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

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 < T; t++ {
    var n, k int
    fmt.Fscan(in, &n, &k)

    best := make([]int, k)
    for i := 0; i < n; i++ {
      var v int
      fmt.Fscan(in, &v)
      best[i % k] = max(best[i % k], v)
    }

    sum := int64(0)
    for _, v := range best {
      sum += int64(v)
    }

    fmt.Fprintln(out, sum)
  }
}

package main

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

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

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

func main() {

	defer out.Flush()

	t := 0
	if _, err := fmt.Fscan(in, &t); err != nil {
		return
	}

	for ; t > 0; t-- {

		n, k := 0, 0
		if _, err := fmt.Fscan(in, &n, &k); err != nil {
			return
		}

		nums := make([]int, n)
		mxNums := make([]int, k)
		//mxIdxs := make(map[int]int)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &nums[i])
			mxNums[i%k] = max(mxNums[i%k], nums[i])
		}

		ans := 0
		for i := 0; i < k; i++ {
			ans += mxNums[i]
		}

		fmt.Fprintln(out, ans)
	}
}

package main

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

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

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}
func numDigit(n int) int {
	cnt := 0
	for n > 0 {
		cnt++
		n /= 10
	}
	return cnt
}
func solve() {
	var n, k, x int
	scan(&n, &k)
	a := make([]int, k+1)
	for i := 1; i <= n; i++ {
		scan(&x)
		id := i % k
		if id == 0 {
			id = k
		}
		if x > a[id] {
			// printf("%d-%d\n", i, x)
			a[id] = x
		}
	}
	res := 0
	for i := 1; i <= k; i++ {
		res += a[i]
	}

	printf("%d\n", res)
}
func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

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

func a1733(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T int
	var n int
	var k int
	var x int

	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		Fscan(in, &k)

		r := make([]int, n)
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			r[i] = x
		}

		ma := make([]int, k)
		for i := 0; i < n; i++ {
			ma[i%k] = max(ma[i%k], r[i])
		}

		ans := int64(0)
		for _, v := range ma {
			ans += int64(v)
		}

		Fprintln(out, ans)
	}
}

package main

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

var a [105]int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var T int
	fmt.Fscan(in, &T)
	for cs := 0; cs < T; cs++ {
		var n, k int
		fmt.Fscan(in, &n, &k)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		ans := int64(0)
		for i := 0; i < k; i++ {
			max := 0
			for j := i; j < n; j += k {
				if max < a[j] {
					max = a[j]
				}
			}
			ans += int64(max)
		}
		fmt.Fprintln(out, ans)
	}
}

package main

import (
	"fmt"
	"os"
)

func packAddrs(n []int) []interface{} {
	p := make([]interface{}, len(n))
	for i := range n {
		p[i] = &n[i]
	}
	return p
}

func main() {
	var l int
	fmt.Scanf("%d\n", &l)
	res := make([]int, l)
	for i := 0; i < l; i++ {
		var n, k int
		fmt.Scanf("%d %d\n", &n, &k)
		a := make([]int, n)
		fmt.Fscan(os.Stdin, packAddrs(a)...)
		//fmt.Println(SumOfSeq(k, a))
		res[i] = SumOfSeq(k, a)
	}
	for i := 0; i < l; i++ {
		fmt.Println(res[i])
	}
}

func SumOfSeq(k int, a []int) int {
	for i := k; i < len(a); i++ {
		if rem := i % k; rem < k && a[i] > a[rem] {
			a[i], a[rem] = a[rem], a[i]
		}
	}
	res := 0
	for i := 0; i < k; i++ {
		res += a[i]
	}
	return res
}

package main

import (
	"fmt"
	"os"
)

func packAddrs(n []int) []interface{} {
	p := make([]interface{}, len(n))
	for i := range n {
		p[i] = &n[i]
	}
	return p
}

func main() {
	var l int
	fmt.Scanf("%d\n", &l)
	for i := 0; i < l; i++ {
		var n, k int
		fmt.Scanf("%d %d\n", &n, &k)
		a := make([]int, n)
		fmt.Fscan(os.Stdin, packAddrs(a)...)
		//fmt.Println(a)
		fmt.Println(SumOfSeq(k, a))
	}
}

func SumOfSeq(k int, a []int) int {
	for i := k; i < len(a); i++ {
		if rem := i % k; rem < k && a[i] > a[rem] {
			a[i], a[rem] = a[rem], a[i]
		}
	}
	res := 0
	for i := 0; i < k; i++ {
		res += a[i]
	}
	return res
}

package main

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

var (
	input  = bufio.NewReaderSize(os.Stdin, 55000)
	output = bufio.NewWriterSize(os.Stdout, 55000)
)

func main() {
	var t int
	fmt.Fscan(input, &t)
	for t >= 0 {
		var k, n int
		fmt.Fscan(input, &n, &k)

		a := make([]int, k)
		for i := 0; i < n; i++ {
			var val int
			fmt.Fscan(input, &val)
			if a[i%k] < val {
				a[i%k] = val
			}
		}

		sum := 0
		for i := 0; i < k; i++ {
			sum += a[i]
		}

		fmt.Println(sum)

		t--
	}
}

package main

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

func main() {
	var testCases int

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

	fmt.Fscan(reader, &testCases)

	for testCase := 1; testCase <= testCases; testCase++ {
		var n, k int
		fmt.Fscan(reader, &n)
		fmt.Fscan(reader, &k)

		a := make([]int, n)
		for ai := 0; ai < n; ai++ {
			var i int
			fmt.Fscan(reader, &i)
			a[ai] = i
		}

		output := solveA(n, k, &a)

		fmt.Fprintln(writer, output)
	}
}

func solveA(n, k int, a *[]int) int {

	// kConsecutive := make([]int, k)
	ki := 1
	for i := 0; i < n && ki <= k; i++ {
		for j := i + 1; j < n && ki <= k; j++ {
			if i%k == j%k {
				if (*a)[i] > (*a)[j] {
					(*a)[i], (*a)[j] = (*a)[j], (*a)[i]
					ki++
				}
			}
		}
	}
	sum := 0

	for ki := 1; ki <= k; ki++ {
		sum += (*a)[n-ki]
	}
	return sum
}

package main

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

type CF1733A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1733A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1733A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1733A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1733A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1733A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1733A
Date: 23/09/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1733/A
Problem: CF1733A
**/
func (in *CF1733A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, k := in.NextInt(), in.NextInt()
		maxi := int64(0)
		arr := make([]int, n+1)
		for i := 0; i < n; i++ {
			arr[i] = in.NextInt()
		}

		for i := k; i < len(arr); i++ {
			arr[i], arr[i%k] = arr[i%k], arr[i]
			for j := k; j < len(arr); j++ {
				sum := int64(0)
				for p, q := 0, j; p < k; p++ {
					sum += int64(arr[q])
					q--
				}
				if sum > maxi {
					maxi = sum
				}
			}
			arr[i], arr[i%k] = arr[i%k], arr[i]
		}

		fmt.Println(maxi)
	}
}

func NewCF1733A(r *bufio.Reader) *CF1733A {
	return &CF1733A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1733A(bufio.NewReader(os.Stdin)).Run()
}


package main

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

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

	var line string
	scanner := bufio.NewScanner(os.Stdin)
	buf := make([]byte, 0, 12*100000)
	scanner.Buffer(buf, 12*100000)

	if scanner.Scan() {
		line = scanner.Text()
	}

	strList := strings.Split(line, " ")

	list := make([]int, n)
	for i := 0; i < n; i++ {
		list[i], _ = strconv.Atoi(strList[i])
	}

	if n < 3 {
		fmt.Println("0")
		return
	}

	changes := [][]int{
		{0, 0},
		{1, 0},
		{0, 1},
		{1, 1},
		{-1, 0},
		{0, -1},
		{-1, -1},
		{1, -1},
		{-1, 1},
	}

	min := n + 1
	for _, change := range changes {
		//fmt.Print(change)
		first := list[0] + change[0]
		last := list[n-1] + change[1]

		diff := (float64)(last-first) / (float64)(n-1)
		if diff != math.Trunc(diff) {
			//fmt.Println()
			continue
		}

		count := 0
		for i, v := range list {
			listN := first + i*(int)(diff)
			//fmt.Printf(" -> %d %d", listN, v)
			listN -= v
			if listN > 1 || listN < -1 {
				count = -1
				break
			} else if listN != 0 {
				count++
			}
		}

		//fmt.Println()

		if min > count && count >= 0 {
			min = count
		}
	}

	if min == n+1 {
		fmt.Println("-1")
	} else {
		fmt.Println(min)
	}

}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, 2048*2048), 2048*2048)
	scanner.Split(bufio.ScanWords)

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())

	a := make([]int, n)

	for i := 0; i < n; i++ {
		scanner.Scan()
		a[i], _ = strconv.Atoi(scanner.Text())
	}

	if n == 1 {
		fmt.Println(0)
		return
	}

	ans := n + 1

	for j := a[0] - 1; j <= a[0]+1; j++ {
		diff := a[1] - j
		if t := check(a, j, diff-1); t != -1 {
			ans = min(ans, t)
		}
		if t := check(a, j, diff); t != -1 {
			ans = min(ans, t)
		}
		if t := check(a, j, diff+1); t != -1 {
			ans = min(ans, t)
		}
	}
	if ans == n+1 {
		fmt.Println(-1)
		return
	}

	fmt.Println(ans)
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func check(a []int, start int, step int) int {
	answer := 0
	next := start

	for i := 0; i < len(a); i++ {
		if a[i]-next == 0 {
			next += step
			continue
		}
		if a[i]-next == -1 || a[i]-next == 1 {
			next += step
			answer++
			continue
		}
		return -1
	}

	return answer
}

package main

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

func abs(x int) int {
	if x < 0 {
		return -x
	} else {
		return x
	}
}

func min(x, y int) int {
	if x < y {
		return x
	} else {
		return y
	}
}

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

	var n int
	fmt.Fscan(in, &n)
	a := make([]int, n)
	b := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}
	if n < 3 {
		fmt.Fprintln(out, "0")
		out.Flush()
		os.Exit(0)
	}
	ret := int(1e8)
	d := [3]int{-1, 0, 1}
	for j := 0; j < 3; j++ {
		b[0] = a[0] + d[j]
		if b[0] < 0 {
			continue
		}
		for k := 0; k < 3; k++ {
			b[1] = a[1] + d[k]
			if b[1] < 0 {
				continue
			}
			t := b[1] - b[0]
			flag := true
			tmp := 0
			if b[0] != a[0] {
				tmp++
			}
			if b[1] != a[1] {
				tmp++
			}
			for i := 2; i < n; i++ {
				if abs(b[i-1]+t-a[i]) > 1 || b[i-1]+t < 0 {
					flag = false
					break
				}
				if b[i-1]+t != a[i] {
					tmp++
				}
				b[i] = b[i-1] + t
			}
			if flag {
				ret = min(ret, tmp)
			}
		}
	}
	if ret > 1e7 {
		fmt.Fprintln(out, "-1")
	} else {
		fmt.Fprintln(out, ret)
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF978D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	abs := func(x int) int {
		if x < 0 {
			return -x
		}
		return x
	}

	var n int
	Fscan(in, &n)
	if n < 3 {
		Fprint(out, 0)
		return
	}
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	ans := n + 1
	for i := -2; i <= 2; i++ {
	o:
		for a0 := a[0] - 1; a0 <= a[0]+1; a0++ {
			c, d := 0, (a[n-1]-a[0])/n+i
			for j, v := range a {
				f := abs(a0 + j*d - v)
				if f > 1 {
					continue o
				}
				if f == 1 {
					c++
				}
			}
			if c < ans {
				ans = c
			}
		}
	}
	if ans == n+1 {
		ans = -1
	}
	Fprint(out, ans)
}

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

package main

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

const (
	MaxN = 100005
)

var delta = []int{-1, 0, 1}
var n int
var a [MaxN]int
var best int

func abs(val int) int {
	if val < 0 {
		return -val
	}
	return val
}

func check(a []int, delta, cnt int) int {
	l := a[1]
	for i := 2; i < len(a); i++ {
		l += delta
		k := abs(l - a[i])
		if k <= 1 {
			cnt += k
		} else {
			return -1
		}
	}
	return cnt
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}

	if n < 3 {
		fmt.Println(0)
		return
	}

	best = MaxN
	for _, dx := range delta {
		for _, dy := range delta {
			a[0] += dx
			a[1] += dy
			cnt := abs(dx) + abs(dy)
			m := check(a[:n], a[1] - a[0], cnt)
			if m != -1 && m < best {
				best = m
			}
			a[0] -= dx
			a[1] -= dy
		}
	}
	if best == MaxN {
		fmt.Println(-1)
	} else {
		fmt.Println(best)
	}
}

package main

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

func abs(x int) int {
	if x >= 0 {
		return x
	}

	return -x
}

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

	var n int

	fmt.Fscan(in, &n)

	b := make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(in, &b[i])
	}

	if n <= 2 {
		fmt.Fprintln(out, 0)
		return
	}

	bestRes := -1
	c := make([]int, n)
	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			fail := false
			for k := 0; k < n; k++ {
				c[k] = b[k]
			}

			res := 0
			if i != 0 {
				res++
			}
			if j != 0 {
				res++
			}

			c[0] += i
			c[1] += j

			d := c[1] - c[0]

			for k := 2; k < n; k++ {
				r := abs(c[k] - (c[k-1] + d))
				if r == 1 {
					res++
					c[k] = c[k-1] + d
				} else if r > 1 {
					fail = true
					break
				}
			}

			if !fail {
				if bestRes == -1 || res < bestRes {
					bestRes = res
				}
			}

		}
	}

	fmt.Fprintln(out, bestRes)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, 2048*2048), 2048*2048)
	scanner.Split(bufio.ScanWords)

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())

	a := make([]int, n)

	for i := 0; i < n; i++ {
		scanner.Scan()
		a[i], _ = strconv.Atoi(scanner.Text())
	}

	sort.Ints(a)

	if n == 1 {
		fmt.Println(0)
		return
	}

	ans := n + 1
	diff := a[1] - a[0]

	for j := a[0] - 1; j <= a[0]+1; j++ {
		if t := check(a, j, diff-1); t != -1 {
			ans = min(ans, t)
		}
		if t := check(a, j, diff); t != -1 {
			ans = min(ans, t)
		}
		if t := check(a, j, diff+1); t != -1 {
			ans = min(ans, t)
		}
	}

	fmt.Println(ans)
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func check(a []int, start int, step int) int {
	answer := 0
	next := start

	for i := 0; i < len(a); i++ {
		if a[i]-next == 0 {
			next += step
			continue
		}
		if a[i]-next == -1 || a[i]-next == 1 {
			next += step
			answer++
			continue
		}
		return -1
	}

	return answer
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, 2048*2048), 2048*2048)
	scanner.Split(bufio.ScanWords)

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())

	a := make([]int, n)

	for i := 0; i < n; i++ {
		scanner.Scan()
		a[i], _ = strconv.Atoi(scanner.Text())
	}

	sort.Ints(a)

	if n == 1 {
		fmt.Println(0)
		return
	}

	ans := n + 1
	diff := a[1] - a[0]

	for j := a[0] - 1; j <= a[0]+1; j++ {
		if t := check(a, j, diff-1); t != -1 {
			ans = min(ans, t)
		}
		if t := check(a, j, diff); t != -1 {
			ans = min(ans, t)
		}
		if t := check(a, j, diff+1); t != -1 {
			ans = min(ans, t)
		}
	}
	if ans == n+1 {
		fmt.Println(-1)
		return
	}

	fmt.Println(ans)
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func check(a []int, start int, step int) int {
	answer := 0
	next := start

	for i := 0; i < len(a); i++ {
		if a[i]-next == 0 {
			next += step
			continue
		}
		if a[i]-next == -1 || a[i]-next == 1 {
			next += step
			answer++
			continue
		}
		return -1
	}

	return answer
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, 2048*2048), 2048*2048)
	scanner.Split(bufio.ScanWords)

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())

	a := make([]int, n)

	for i := 0; i < n; i++ {
		scanner.Scan()
		a[i], _ = strconv.Atoi(scanner.Text())
	}

	sort.Ints(a)

	if n == 1 {
		fmt.Println(0)
		return
	}

	ans := n + 1

	for j := a[0] - 1; j <= a[0]+1; j++ {
		diff := a[1] - j
		if t := check(a, j, diff-1); t != -1 {
			ans = min(ans, t)
		}
		if t := check(a, j, diff); t != -1 {
			ans = min(ans, t)
		}
		if t := check(a, j, diff+1); t != -1 {
			ans = min(ans, t)
		}
	}
	if ans == n+1 {
		fmt.Println(-1)
		return
	}

	fmt.Println(ans)
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func check(a []int, start int, step int) int {
	answer := 0
	next := start

	for i := 0; i < len(a); i++ {
		if a[i]-next == 0 {
			next += step
			continue
		}
		if a[i]-next == -1 || a[i]-next == 1 {
			next += step
			answer++
			continue
		}
		return -1
	}

	return answer
}

package main

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

func abs(x int) int {
	if x < 0 {
		return -x
	} else {
		return x
	}
}

func min(x, y int) int {
	if x < y {
		return x
	} else {
		return y
	}
}

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

	var n int
	fmt.Fscan(in, &n)
	a := make([]int, n)
	b := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}
	if n < 3 {
		fmt.Fprintln(out, 0)
		os.Exit(0)
	}
	ret := int(1e8)
	d := [3]int{-1, 0, 1}
	for j := 0; j < 3; j++ {
		b[0] = a[0] + d[j]
		if b[0] < 0 {
			continue
		}
		for k := 0; k < 3; k++ {
			b[1] = a[1] + d[k]
			if b[1] < 0 {
				continue
			}
			t := b[1] - b[0]
			flag := true
			tmp := 0
			if b[0] != a[0] {
				tmp++
			}
			if b[1] != a[1] {
				tmp++
			}
			for i := 2; i < n; i++ {
				if abs(b[i-1]+t-a[i]) > 1 || b[i-1]+t < 0 {
					flag = false
					break
				}
				if b[i-1]+t != a[i] {
					tmp++
				}
				b[i] = b[i-1] + t
			}
			if flag {
				ret = min(ret, tmp)
			}
		}
	}
	if ret > 1e7 {
		fmt.Fprintln(out, "-1")
	} else {
		fmt.Fprintln(out, ret)
	}
}

package main

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

func abs(x int) int {
	if x < 0 {
		return -x
	} else {
		return x
	}
}

func min(x, y int) int {
	if x < y {
		return x
	} else {
		return y
	}
}

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

	var n int
	fmt.Fscan(in, &n)
	a := make([]int, n)
	b := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}
	ret := int(1e8)
	d := [3]int{-1, 0, 1}
	for j := 0; j < 3; j++ {
		b[0] = a[0] + d[j]
		if b[0] < 0 {
			continue
		}
		for k := 0; k < 3; k++ {
			b[1] = a[1] + d[k]
			if b[1] < 0 {
				continue
			}
			t := b[1] - b[0]
			flag := true
			tmp := 0
			if b[0] != a[0] {
				tmp++
			}
			if b[1] != a[1] {
				tmp++
			}
			for i := 2; i < n; i++ {
				if abs(b[i-1]+t-a[i]) > 1 || b[i-1]+t < 0 {
					flag = false
					break
				}
				if b[i-1]+t == a[i] {
					tmp++
				}
				b[i] = b[i-1] + t
			}
			if flag {
				ret = min(ret, tmp)
			}
		}
	}
	if ret > 1e7 {
		fmt.Fprintln(out, "-1")
	} else {
		fmt.Fprintln(out, ret)
	}
}


package main

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

var in = bufio.NewReader(os.Stdin)
var out = bufio.NewWriter(os.Stdout)
var to [][]int
var vis []bool

func dfs(n, f, t int) int {
	vis[n] = true
	ans := 1
	if n == t {
		return -1
	}
	for _, i := range to[n] {
		if vis[i] {
			continue
		}
		ret := dfs(i, f, t)
		if ret == -1 && n != f {
			return -1
		}
		if ret > 0 {
			ans += ret
		}
	}
	return ans
}

func main() {
	defer out.Flush()

	var n, x, y, u, v int
	fmt.Fscan(in, &n, &x, &y)
	to = make([][]int, n+1)
	vis = make([]bool, n+1)

	for i := 1; i <= n; i++ {
		to[i] = make([]int, 0)
	}
	for i := 1; i < n; i++ {
		fmt.Fscan(in, &u, &v)
		to[u] = append(to[u], v)
		to[v] = append(to[v], u)
	}

	a1 := dfs(x, x, y)
	vis = make([]bool, n+1)
	a2 := dfs(y, y, x)
	fmt.Fprintln(out, int64(n)*int64(n-1) - int64(a1)*int64(a2))
}

package main

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

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

	var n int64
	var flowrisa, beetopia, v, w int
	Fscan(in, &n, &flowrisa, &beetopia)
	g := make([][]int, n+1)
	for i := 1; i < int(n); i++ {
		Fscan(in, &v, &w)
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	size := make([]int, n+1)
	fa := make([]int, n+1)
	var f func(int, int) int
	f = func(v, p int) int {
		fa[v] = p
		sz := 1
		for _, w := range g[v] {
			if w != p {
				sz += f(w, v)
			}
		}
		size[v] = sz
		return sz
	}
	f(flowrisa, 0)
	v = beetopia
	for ; fa[v] != flowrisa; v = fa[v] {
	}
	Fprint(out, n*(n-1)-(n-int64(size[v]))*int64(size[beetopia]))
}

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

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/979/C

package main

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

type Edge struct {
	from int
	to   int
}

func find(x int, y int, graph map[int][]int) map[int]bool {
	visited := make(map[int]bool)
	stack := []int{x}
	visited[x] = true
	for len(stack) > 0 {
		currentNode := stack[len(stack)-1]
		if currentNode == y {
			break
		}
		flag := true
		for _, vertex := range graph[currentNode] {
			if !visited[vertex] {
				visited[vertex] = true
				stack = append(stack, vertex)
				flag = false
				break
			}
		}
		if flag {
			stack = stack[:len(stack)-1]
		}
	}
	shortestPathSet := make(map[int]bool)
	for _, vertex := range stack {
		shortestPathSet[vertex] = true
	}
	return shortestPathSet
}

func bfs(root int, graph map[int][]int, shortestPathSet map[int]bool) int {
	queue := []int{root}
	visited := make(map[int]bool)
	count := 0
	for len(queue) > 0 {
		currentNode := queue[0]
		queue = queue[1:]
		count += 1
		for _, vertex := range graph[currentNode] {
			if !visited[vertex] && !shortestPathSet[vertex] {
				visited[vertex] = true
				queue = append(queue, vertex)
			}
		}
	}
	return count
}

func Solve(n int, x int, y int, edges []Edge) int64 {
	graph := make(map[int][]int)
	for _, edge := range edges {
		graph[edge.from] = append(graph[edge.from], edge.to)
		graph[edge.to] = append(graph[edge.to], edge.from)
	}
	shortestPathSet := find(x, y, graph)
	xCount := bfs(x, graph, shortestPathSet)
	yCount := bfs(y, graph, shortestPathSet)
	return int64(n)*int64(n-1) - int64(xCount)*int64(yCount)
}

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

	var n, x, y int
	fmt.Fscanf(reader, "%d %d %d\n", &n, &x, &y)
	edges := make([]Edge, n-1)
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(reader, "%d %d\n", &edges[i].from, &edges[i].to)
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, x, y, edges))
}

package main

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

var in = bufio.NewReader(os.Stdin)
var out = bufio.NewWriter(os.Stdout)
var to [][]int
var vis []bool

func dfs(n, f, t int) int {
	vis[n] = true
	ans := 1
	if n == t {
		return -1
	}
	for _, i := range to[n] {
		if vis[i] {
			continue
		}
		ret := dfs(i, f, t)
		if ret == -1 && n != f {
			return -1
		}
		if ret > 0 {
			ans += ret
		}
	}
	return ans
}

func main() {
	defer out.Flush()

	var n, x, y, u, v int
	fmt.Fscan(in, &n, &x, &y)
	to = make([][]int, n+1)
	vis = make([]bool, n+1)

	for i := 1; i <= n; i++ {
		to[i] = make([]int, 0)
	}
	for i := 1; i < n; i++ {
		fmt.Fscan(in, &u, &v)
		to[u] = append(to[u], v)
		to[v] = append(to[v], u)
	}

	a1 := dfs(x, x, y)
	vis = make([]bool, n+1)
	a2 := dfs(y, y, x)
	fmt.Fprintln(out, n*(n-1) - a1*a2)
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/979/C

package main

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

type Edge struct {
	from int
	to   int
}

func find(x int, y int, graph map[int][]int) map[int]bool {
	visited := make(map[int]bool)
	stack := []int{x}
	visited[x] = true
	for len(stack) > 0 {
		currentNode := stack[len(stack)-1]
		if currentNode == y {
			break
		}
		flag := true
		for _, vertex := range graph[currentNode] {
			if !visited[vertex] {
				visited[vertex] = true
				stack = append(stack, vertex)
				flag = false
				break
			}
		}
		if flag {
			stack = stack[:len(stack)-1]
		}
	}
	shortestPathSet := make(map[int]bool)
	for _, vertex := range stack {
		shortestPathSet[vertex] = true
	}
	return shortestPathSet
}

func bfs(root int, graph map[int][]int, shortestPathSet map[int]bool) int {
	queue := []int{root}
	visited := make(map[int]bool)
	count := 0
	for len(queue) > 0 {
		currentNode := queue[0]
		queue = queue[1:]
		count += 1
		for _, vertex := range graph[currentNode] {
			if !visited[vertex] && !shortestPathSet[vertex] {
				visited[vertex] = true
				queue = append(queue, vertex)
			}
		}
	}
	return count
}

func Solve(n int, x int, y int, edges []Edge) int64 {
	graph := make(map[int][]int)
	for _, edge := range edges {
		graph[edge.from] = append(graph[edge.from], edge.to)
		graph[edge.to] = append(graph[edge.to], edge.from)
	}
	shortestPathSet := find(x, y, graph)
	xCount := bfs(x, graph, shortestPathSet)
	yCount := bfs(y, graph, shortestPathSet)
	return int64(n*(n-1)) - int64(xCount*yCount)
}

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

	var n, x, y int
	fmt.Fscanf(reader, "%d %d %d\n", &n, &x, &y)
	edges := make([]Edge, n-1)
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(reader, "%d %d\n", &edges[i].from, &edges[i].to)
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, x, y, edges))
}


package main

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

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

	var line string
	scanner := bufio.NewScanner(os.Stdin)
	buf := make([]byte, 0, 12*200000)
	scanner.Buffer(buf, 12*200000)

	if scanner.Scan() {
		line = scanner.Text()
	}

	strList := strings.Split(line, " ")

	dArray := make([]int, n)
	for i := 0; i < n; i++ {
		dArray[i], _ = strconv.Atoi(strList[i])
	}

	si := 0
	ei := len(dArray) - 1

	var max, sum1, sum3 uint64
	max = 0
	sum1 = uint64(dArray[si])
	sum3 = uint64(dArray[ei])
	for si < ei {
		if sum1 > sum3 {
			ei--
			sum3 += uint64(dArray[ei])
		} else if sum3 > sum1 {
			si++
			sum1 += uint64(dArray[si])
		} else {
			max = sum1
			si++
			ei--
			sum1 += uint64(dArray[si])
			sum3 += uint64(dArray[ei])
		}
	}

	fmt.Println(max)
}

package main

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

const (
	MaxN = 200005
)

var n int
var a, forwards, backwards [MaxN]uint64

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &n)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(buf, "%d ", &a[i])
	}
	for i := 1; i <= n; i++ {
		forwards[i] += forwards[i - 1] + a[i]
		backwards[n - i + 1] += backwards[n - i + 2] + a[n - i + 1]
	}
	max := uint64(0)
	for i := n; i >= 1; i-- {
		index := sort.Search(i, func(pos int) bool {
			return forwards[pos] >= backwards[i]
		})
		if index != i && forwards[index] == backwards[i] && max < backwards[i] {
			max = backwards[i]
		}
	}
	fmt.Println(max)
}

package main

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

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

	var line string
	scanner := bufio.NewScanner(os.Stdin)
	buf := make([]byte, 0, 12*200000)
	scanner.Buffer(buf, 12*200000)

	if scanner.Scan() {
		line = scanner.Text()
	}

	strList := strings.Split(line, " ")

	dArray := make([]int, n)
	for i := 0; i < n; i++ {
		dArray[i], _ = strconv.Atoi(strList[i])
	}

	si := 0
	ei := len(dArray) - 1

	var max, sum1, sum3 uint64
	max = 0
	sum1 = uint64(dArray[si])
	sum3 = uint64(dArray[ei])
	for si <= ei {
		if sum1 > sum3 {
			ei--
			sum3 += uint64(dArray[ei])
		} else if sum3 > sum1 {
			si++
			sum1 += uint64(dArray[si])
		} else {
			max = sum1
			si++
			ei--
			sum1 += uint64(dArray[si])
			sum3 += uint64(dArray[ei])
		}
	}

	fmt.Println(max)
}

package main

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

const (
	MaxN = 200005
)

var n int
var a, forwards, backwards [MaxN]int

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &n)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(buf, "%d ", &a[i])
	}
	for i := 1; i <= n; i++ {
		forwards[i] += forwards[i - 1] + a[i]
		backwards[n - i + 1] += backwards[n - i + 2] + a[n - i + 1]
	}
	max := 0
	for i := n; i >= 1; i-- {
		index := sort.Search(i, func(pos int) bool {
			return forwards[pos] >= backwards[i]
		})
		if index != i && forwards[index] == backwards[i] && max < backwards[i] {
			max = backwards[i]
		}
	}
	fmt.Println(max)
}


package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	Q := func(tp string, v int) int { Println(tp, v); Fscan(in, &v); return v }
	var n, v, w int
	Fscan(in, &n)
	g := make([][]int, n+1)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	type node struct{ d, hson int }
	nodes := make([]node, n+1)
	var _f func(v, fa, d int) int
	_f = func(v, fa, d int) int {
		sz, maxSz, hson := 1, 0, -1
		for _, w := range g[v] {
			if w != fa {
				s := _f(w, v, d+1)
				sz += s
				if s > maxSz {
					maxSz = s
					hson = w
				}
			}
		}
		nodes[v] = node{d, hson}
		return sz
	}
	_f(1, 0, 0)

	depX := Q("d", 1)
	var f func(v int) int
	f = func(v int) int {
		hp := []int{v}
		for o := nodes[v]; o.hson != -1; o = nodes[o.hson] {
			hp = append(hp, o.hson)
		}
		end := hp[len(hp)-1]
		depAX := (depX + nodes[end].d - Q("d", end)) / 2
		ax := hp[depAX-nodes[v].d]
		if depAX == depX {
			return ax
		}
		return f(Q("s", ax))
	}
	Println("!", f(1))
}

package main

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

type (
	input struct {
		n int
		g [][]int
	}
	guess struct{ ans int }
	qIn   struct {
		tp string
		v  int
	}
	qOut struct{ v int }
)

// github.com/EndlessCheng/codeforces-go
func run(in input, Q func(qIn) qOut) (gs guess) {
	n, g := in.n, in.g
	type node struct{ d, sz, hson int }
	nodes := make([]node, n+1)
	var f func(v, fa, d int) int
	f = func(v, fa, d int) int {
		nodes[v] = node{d, 1, -1}
		o := &nodes[v]
		for _, w := range g[v] {
			if w != fa {
				sz := f(w, v, d+1)
				o.sz += sz
				if o.hson == -1 || sz > nodes[o.hson].sz {
					o.hson = w
				}
			}
		}
		return o.sz
	}
	f(1, 0, 0)

	depX := Q(qIn{"d", 1}).v
	var f2 func(v int) int
	f2 = func(v int) int {
		hp := []int{v}
		for o := nodes[v]; o.hson != -1; o = nodes[o.hson] {
			hp = append(hp, o.hson)
		}
		end := hp[len(hp)-1]
		depAX := (depX + nodes[end].d - Q(qIn{"d", end}).v) / 2
		ax := hp[depAX-nodes[v].d]
		if depAX == depX {
			return ax
		}
		return f2(Q(qIn{"s", ax}).v)
	}
	gs.ans = f2(1)
	return
}

func ioq() {
	in := bufio.NewReader(os.Stdin)
	Q := func(q qIn) (resp qOut) { Println(q.tp, q.v); Fscan(in, &resp.v); return }
	d := input{}
	Fscan(in, &d.n)
	g := make([][]int, d.n+1)
	for i := 1; i < d.n; i++ {
		var v, w int
		Fscan(in, &v, &w)
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	d.g = g
	gs := run(d, Q)
	Println("!", gs.ans)
}

func main() { ioq() }

package main

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

type (
	input struct {
		n int
		g [][]int
	}
	guess struct{ ans int }
	qIn   struct {
		tp string
		v  int
	}
	qOut struct{ v int }
)

// github.com/EndlessCheng/codeforces-go
func run(in input, Q func(qIn) qOut) (gs guess) {
	n, g := in.n, in.g
	type node struct{ dep, fa int }
	nodes := make([]node, n+1)
	var _f func(v, fa, d int)
	_f = func(v, fa, d int) {
		nodes[v] = node{d, fa}
		for _, w := range g[v] {
			if w != fa {
				_f(w, v, d+1)
			}
		}
	}
	_f(1, 0, 0)

	used := make([]bool, n+1)
	size := make([]int, n+1)
	var calcSize func(v, fa int) int
	calcSize = func(v, fa int) int {
		sz := 1
		for _, w := range g[v] {
			if w != fa && !used[w] {
				sz += calcSize(w, v)
			}
		}
		size[v] = sz
		return sz
	}
	var compSize int
	var find func(v, fa int) int
	find = func(v, fa int) int {
		for _, w := range g[v] {
			if w != fa && !used[w] && size[w] > compSize>>1 {
				return find(w, v)
			}
		}
		return v
	}

	depX := Q(qIn{"d", 1}).v
	var f func(v int) int
	f = func(v int) int {
		calcSize(v, 0)
		compSize = size[v]
		c := find(v, 0)
		d := Q(qIn{"d", c}).v
		if d == 0 {
			return c
		}
		used[c] = true
		o := nodes[c]
		if o.dep+d == depX {
			return f(Q(qIn{"s", c}).v)
		}
		return f(o.fa)
	}
	gs.ans = f(1)
	return
}

func ioq() {
	in := bufio.NewReader(os.Stdin)
	Q := func(q qIn) (resp qOut) { Println(q.tp, q.v); Fscan(in, &resp.v); return }
	d := input{}
	Fscan(in, &d.n)
	g := make([][]int, d.n+1)
	for i := 1; i < d.n; i++ {
		var v, w int
		Fscan(in, &v, &w)
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	d.g = g
	gs := run(d, Q)
	Println("!", gs.ans)
}

func main() { ioq() }

package main

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

type (
	input struct {
		n int
		g [][]int
	}
	guess struct{ ans int }
	qIn   struct {
		tp string
		v  int
	}
	qOut struct{ v int }
)

// github.com/EndlessCheng/codeforces-go
func run(in input, Q func(qIn) qOut) (gs guess) {
	n, g := in.n, in.g
	type node struct{ d, sz, hson int }
	nodes := make([]node, n+1)
	var f func(v, fa, d int) *node
	f = func(v, fa, d int) *node {
		nodes[v] = node{d, 1, -1}
		o := &nodes[v]
		for _, w := range g[v] {
			if w != fa {
				son := f(w, v, d+1)
				o.sz += son.sz
				if o.hson == -1 || son.sz > nodes[o.hson].sz {
					o.hson = w
				}
			}
		}
		return o
	}
	f(1, 0, 0)

	depX := Q(qIn{"d", 1}).v
	var f2 func(v int) int
	f2 = func(v int) int {
		hp := []int{v}
		for o := nodes[v]; o.hson != -1; o = nodes[o.hson] {
			hp = append(hp, o.hson)
		}
		end := hp[len(hp)-1]
		depAX := (depX + nodes[end].d - Q(qIn{"d", end}).v) / 2
		ax := hp[depAX-nodes[v].d]
		if depAX == depX {
			return ax
		}
		return f2(Q(qIn{"s", ax}).v)
	}
	gs.ans = f2(1)
	return
}

func ioq() {
	in := bufio.NewReader(os.Stdin)
	Q := func(q qIn) (resp qOut) { Println(q.tp, q.v); Fscan(in, &resp.v); return }
	d := input{}
	Fscan(in, &d.n)
	g := make([][]int, d.n+1)
	for i := 1; i < d.n; i++ {
		var v, w int
		Fscan(in, &v, &w)
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	d.g = g
	gs := run(d, Q)
	Println("!", gs.ans)
}

func main() { ioq() }

package main

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

type (
	input struct {
		n int
		g [][]int
	}
	guess struct{ ans int }
	qIn   struct {
		tp string
		v  int
	}
	qOut struct{ v int }
)

// github.com/EndlessCheng/codeforces-go
func run(in input, Q func(qIn) qOut) (gs guess) {
	n, g := in.n, in.g
	type node struct{ d, sz, hson int }
	nodes := make([]node, n+1)
	var f func(v, fa, d int) int
	f = func(v, fa, d int) int {
		sz, hson := 1, -1
		for _, w := range g[v] {
			if w != fa {
				s := f(w, v, d+1)
				sz += s
				if hson == -1 || s > nodes[hson].sz {
					hson = w
				}
			}
		}
		nodes[v] = node{d, sz, hson}
		return sz
	}
	f(1, 0, 0)

	depX := Q(qIn{"d", 1}).v
	var f2 func(v int) int
	f2 = func(v int) int {
		hp := []int{v}
		for o := nodes[v]; o.hson != -1; o = nodes[o.hson] {
			hp = append(hp, o.hson)
		}
		end := hp[len(hp)-1]
		depAX := (depX + nodes[end].d - Q(qIn{"d", end}).v) / 2
		ax := hp[depAX-nodes[v].d]
		if depAX == depX {
			return ax
		}
		return f2(Q(qIn{"s", ax}).v)
	}
	gs.ans = f2(1)
	return
}

func ioq() {
	in := bufio.NewReader(os.Stdin)
	Q := func(q qIn) (resp qOut) { Println(q.tp, q.v); Fscan(in, &resp.v); return }
	d := input{}
	Fscan(in, &d.n)
	g := make([][]int, d.n+1)
	for i := 1; i < d.n; i++ {
		var v, w int
		Fscan(in, &v, &w)
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	d.g = g
	gs := run(d, Q)
	Println("!", gs.ans)
}

func main() { ioq() }

package main

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

type (
	input struct {
		n int
		g [][]int
	}
	guess struct{ ans int }
	qIn   struct {
		tp string
		v  int
	}
	qOut struct{ v int }
)

// github.com/EndlessCheng/codeforces-go
func run(in input, Q func(qIn) qOut) (gs guess) {
	n, g := in.n, in.g
	dep := make([]int, n)
	size := make([]int, n)
	var f func(v, fa, d int) int
	f = func(v, fa, d int) int {
		dep[v] = d
		sz := 1
		for _, w := range g[v] {
			if w != fa {
				sz += f(w, v, d+1)
			}
		}
		size[v] = sz
		return sz
	}
	f(0, -1, 0)
	getHP := func(st int) (hp []int) {
		var f func(v, fa int)
		f = func(v, fa int) {
			hp = append(hp, v)
			hson := -1
			for _, w := range g[v] {
				if w != fa && (hson == -1 || size[w] > size[hson]) {
					hson = w
				}
			}
			if hson != -1 {
				f(hson, v)
			}
		}
		f(st, -1)
		return
	}

	depX := Q(qIn{"d", 1}).v
	var f2 func(v int) int
	f2 = func(v int) int {
		hp := getHP(v)
		low := hp[len(hp)-1]
		depAX := (depX + dep[low] - Q(qIn{"d", low + 1}).v) / 2
		ax := hp[depAX-dep[v]]
		if depAX == depX {
			return ax
		}
		return f2(Q(qIn{"s", ax}).v - 1)
	}
	gs.ans = f2(0) + 1
	return
}

func ioq() {
	in := bufio.NewReader(os.Stdin)
	Q := func(q qIn) (resp qOut) { Println(q.tp, q.v); Fscan(in, &resp.v); return }
	d := input{}
	Fscan(in, &d.n)
	g := make([][]int, d.n)
	for i := 1; i < d.n; i++ {
		var v, w int
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	d.g = g
	gs := run(d, Q)
	Println("!", gs.ans)
}

func main() { ioq() }

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	Q := func(tp string, v int) int { Println(tp, v); Fscan(in, &v); return v }
	var n, v, w int
	Fscan(in, &n)
	g := make([][]int, n+1)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	type node struct{ d, sz, hson int }
	nodes := make([]node, n+1)
	var _f func(v, fa, d int) int
	_f = func(v, fa, d int) int {
		sz, hson := 1, -1
		for _, w := range g[v] {
			if w != fa {
				s := _f(w, v, d+1)
				sz += s
				if hson == -1 || sz > nodes[hson].sz {
					hson = w
				}
			}
		}
		nodes[v] = node{d, sz, hson}
		return sz
	}
	_f(1, 0, 0)

	depX := Q("d", 1)
	var f func(v int) int
	f = func(v int) int {
		hp := []int{v}
		for o := nodes[v]; o.hson != -1; o = nodes[o.hson] {
			hp = append(hp, o.hson)
		}
		end := hp[len(hp)-1]
		depAX := (depX + nodes[end].d - Q("d", end)) / 2
		ax := hp[depAX-nodes[v].d]
		if depAX == depX {
			return ax
		}
		return f(Q("s", ax))
	}
	Println("!", f(1))
}


package main

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

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

var err error

func next() (n string) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextLong() (n int64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextDouble() (n float64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func Fill(a []int, v int) {
	for i := range a {
		a[i] = v
	}
}

func FillB(a []bool, v bool) {
	for i := range a {
		a[i] = v
	}
}

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

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

func Abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func Gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return Gcd(b, a%b)
}

const mod = 1000000007

func Add(a, b int64) int64 {
	return (a + b) % mod
}

func Mult(a, b int64) int64 {
	return (a * b) % mod
}

func Sub(a, b int64) int64 {
	return (a - b + mod) % mod
}

func solve() {
	n := nextInt()

	arr := make([]int, n)
	for i := range arr {
		arr[i] = nextInt()
	}

	used := make([]bool, n)

	gcd := 0
	for i := 0; i < n; i++ {
		j, max := 0, 0
		for ind, v := range arr {
			if used[ind] {
				continue
			}
			curr := Gcd(gcd, v)
			if curr > max {
				j, max = ind, curr
			}
		}
		gcd = max
		printf("%d ", arr[j])
		used[j] = true
	}

	printf("\n")
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	if false {
		name := "foreign"
		file1, err := os.Open(name + ".in")
		if err != nil {
			log.Fatal(err)
		}
		file2, err := os.Create(name + ".out")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file1, 8192)
		out = bufio.NewWriterSize(file2, 8192)
	}
	defer out.Flush()
	// start := time.Now()

	for t, i := nextInt(), 1; t > 0; t, i = t-1, i+1 {
		// 	printf("Case #%d:\n", i)
		solve()
	}

	// printf("%v\n", time.Now().Sub(start))
}

package main

import (
	"fmt"
	"sort"
)

func gcd(a int, b int) int {
	if a < 0 {
		return -1
	}
	if b == 0 {
		return a
	}
	return gcd(b, a % b)
}

func main()  {
	//fileInput, _ := os.Open("input.txt")
	//os.Stdin = fileInput

	//fileOutput, _ := os.Create("output.txt")
	//os.Stdout = fileOutput

	var n, t int
	fmt.Scan(&t)

	for i := 0; i < t; i++ {
		fmt.Scan(&n)
		a := make([]int, n)
		for j := 0; j < n ; j++ {
			fmt.Scan(&a[j])
		}
		sort.Ints(a)
		curr := a[n-1]

		for j := 0; j < n; j++ {
			var maxK, maxGcd int
			maxGcd = -1
			for k := 0; k < n; k++ {
				gcd := gcd(a[k], curr)
				if gcd > maxGcd {
					maxGcd = gcd
					maxK = k
				}
			}
			fmt.Print(a[maxK], " ")
			a[maxK] = -1
			curr = maxGcd
		}
		fmt.Println("")
	}
}

package main

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

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	for t := io.NextInt(); t > 0; t-- {
		n := io.NextInt()
		a := io.NextInts(n)

		prev := 0
		var ans []int
		for len(a) > 0 {
			var mx int
			for i, x := range a {
				if gcd(x, prev) > gcd(a[mx], prev) {
					mx = i
				}
			}
			prev = gcd(a[mx], prev)
			ans = append(ans, a[mx])
			a = append(a[:mx], a[mx+1:]...)
		}
		io.PrintlnInts(ans...)
	}
}

func gcd(i, j int) int {
	if j == 0 {
		return i
	}
	return gcd(j, i%j)
}

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}

func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}

func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}

package main

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

func gcd(a int, b int) int {
	if a < 0 {
		return -1
	}
	if b == 0 {
		return a
	}
	return gcd(b, a % b)
}

func main()  {
	fileInput, _ := os.Open("input.txt")
	os.Stdin = fileInput

	//fileOutput, _ := os.Create("output.txt")
	//os.Stdout = fileOutput

	var n, t int
	fmt.Scan(&t)

	for i := 0; i < t; i++ {
		fmt.Scan(&n)
		a := make([]int, n)
		for j := 0; j < n ; j++ {
			fmt.Scan(&a[j])
		}
		sort.Ints(a)
		curr := a[n-1]

		for j := 0; j < n; j++ {
			var maxK, maxGcd int
			maxGcd = -1
			for k := 0; k < n; k++ {
				gcd := gcd(a[k], curr)
				if gcd > maxGcd {
					maxGcd = gcd
					maxK = k
				}
			}
			fmt.Print(a[maxK], " ")
			a[maxK] = -1
			curr = maxGcd
		}
		fmt.Println("")
	}
}