You are given a weighted undirected connected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.Let's define the weight of the path consisting of $$$k$$$ edges with indices $$$e_1, e_2, \dots, e_k$$$ as $$$\sum\limits_{i=1}^{k}{w_{e_i}} - \max\limits_{i=1}^{k}{w_{e_i}} + \min\limits_{i=1}^{k}{w_{e_i}}$$$, where $$$w_i$$$ — weight of the $$$i$$$-th edge in the graph.Your task is to find the minimum weight of the path from the $$$1$$$-st vertex to the $$$i$$$-th vertex for each $$$i$$$ ($$$2 \le i \le n$$$).

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 2 \cdot 10^5$$$; $$$1 \le m \le 2 \cdot 10^5$$$) — the number of vertices and the number of edges in the graph. Following $$$m$$$ lines contains three integers $$$v_i, u_i, w_i$$$ ($$$1 \le v_i, u_i \le n$$$; $$$1 \le w_i \le 10^9$$$; $$$v_i \neq u_i$$$) — endpoints of the $$$i$$$-th edge and its weight respectively.

Print $$$n-1$$$ integers — the minimum weight of the path from $$$1$$$-st vertex to the $$$i$$$-th vertex for each $$$i$$$ ($$$2 \le i \le n$$$).

package main

import (
	"bufio"
	"container/heap"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
type pair73 struct {
	v int
	d int64
}
type hp73 []pair73

func (h hp73) Len() int              { return len(h) }
func (h hp73) Less(i, j int) bool    { return h[i].d < h[j].d }
func (h hp73) Swap(i, j int)         { h[i], h[j] = h[j], h[i] }
func (h *hp73) Push(v interface{})   { *h = append(*h, v.(pair73)) }
func (h *hp73) Pop() (v interface{}) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return }
func (h *hp73) push(v pair73)        { heap.Push(h, v) }
func (h *hp73) pop() pair73          { return heap.Pop(h).(pair73) }

func CF1473E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	min := func(a, b int64) int64 {
		if a < b {
			return a
		}
		return b
	}

	var n, m, v, w, wt int
	Fscan(in, &n, &m)
	type nb struct{ to, wt int }
	g := make([][]nb, n)
	for ; m > 0; m-- {
		Fscan(in, &v, &w, &wt)
		v--
		w--
		g[v] = append(g[v], nb{w, wt})
		g[w] = append(g[w], nb{v, wt})
	}

	const inf int64 = 1e18
	dis := make([]int64, n*4)
	for i := range dis {
		dis[i] = inf
	}
	dis[0] = 0
	h := hp73{{}}
	for len(h) > 0 {
		pd := h.pop()
		x := pd.v
		if dis[x] < pd.d {
			continue
		}
		// 分成四层，分别表示原图、不算最大值、额外再算一遍最小值、不算最大值且额外再算一遍最小值
		// 从第一层到第四层就是要求的最短路
		// 即便当前的边不是最小或最大，后面转移的时候也会覆盖掉
		for _, e := range g[x>>2] {
			w, wt := e.to, int64(e.wt)
			y := w<<2 | x&3
			if newD := dis[x] + wt; newD < dis[y] {
				dis[y] = newD
				h.push(pair73{y, newD})
			}
			if newD := dis[x] + wt*2; x&1 == 0 && newD < dis[y|1] {
				dis[y|1] = newD
				h.push(pair73{y | 1, newD})
			}
			if newD := dis[x]; x>>1&1 == 0 && newD < dis[y|2] {
				dis[y|2] = newD
				h.push(pair73{y | 2, newD})
			}
		}
	}
	for i := 1; i < n; i++ {
		Fprint(out, min(dis[i<<2], dis[i<<2|3]), " ")
	}
}

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

package main

import (
	"bufio"
	"container/heap"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
type pair struct {
	v   int
	dis int64
}
type hp []pair

func (h hp) Len() int              { return len(h) }
func (h hp) Less(i, j int) bool    { return h[i].dis < h[j].dis }
func (h hp) Swap(i, j int)         { h[i], h[j] = h[j], h[i] }
func (h *hp) Push(v interface{})   { *h = append(*h, v.(pair)) }
func (h *hp) Pop() (v interface{}) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return }
func (h *hp) push(v pair)          { heap.Push(h, v) }
func (h *hp) pop() pair            { return heap.Pop(h).(pair) }

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

	var n, m, v, w, wt int
	Fscan(in, &n, &m)
	type nb struct{ to, wt int }
	g := make([][]nb, n)
	for ; m > 0; m-- {
		Fscan(in, &v, &w, &wt)
		v--
		w--
		g[v] = append(g[v], nb{w, wt})
		g[w] = append(g[w], nb{v, wt})
	}

	const inf int64 = 1e18
	dis := make([]int64, n*4)
	for i := range dis {
		dis[i] = inf
	}
	dis[0] = 0
	h := hp{{}}
	for len(h) > 0 {
		p := h.pop()
		x := p.v
		if dis[x] < p.dis {
			continue
		}
		pp := x >> 1 & 1
		q := x & 1
		for _, e := range g[x>>2] {
			w, wt := e.to, int64(e.wt)
			y := w<<2 | pp<<1 | q
			if newD := dis[x] + wt; newD < dis[y] {
				dis[y] = newD
				h.push(pair{y, newD})
			}
			if newD := dis[x] + wt*2; q == 0 && newD < dis[y|1] {
				dis[y|1] = newD
				h.push(pair{y | 1, newD})
			}
			if newD := dis[x]; pp == 0 && newD < dis[y|2] {
				dis[y|2] = newD
				h.push(pair{y | 2, newD})
			}
		}
	}
	for i := 1; i < n; i++ {
		Fprint(out, min(dis[i<<2], dis[i<<2|3]), " ")
	}
}

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

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


You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the greatest common divisor.What is the minimum number of operations you need to make all of the elements equal to 1?

The first line of the input contains one integer n (1 ≤ n ≤ 2000) — the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array.

Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1.

NoteIn the first sample you can turn all numbers to 1 using the following 5 moves:  [2, 2, 3, 4, 6].  [2, 1, 3, 4, 6]  [2, 1, 3, 1, 6]  [2, 1, 1, 1, 6]  [1, 1, 1, 1, 6]  [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves.

package main

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

func gcd(x, y int) int {
	for y != 0 {
		x, y = y, x%y
	}
	return x
}

func solve() {
	n := readInt()
	a := readIntArray(n)
	cnt1 := 0
	for i := 0; i < n; i++ {
		if a[i] == 1 {
			cnt1++
		}
	}
	if cnt1 > 0 {
		fmt.Println(n - cnt1)
		return
	}
	best := 100000
	for i := 0; i < n; i++ {
		h := a[i]
		for j := i + 1; j < n; j++ {
			h = gcd(h, a[j])
			if h == 1 {
				best = min(best, j-i+n-1)
			}
		}
	}
	if best == 100000 {
		fmt.Println(-1)
	} else {
		fmt.Println(best)
	}
}

var scanner *bufio.Scanner

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

// 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
}

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

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

package main

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

type pride struct {
	n   int
	arr []int
}

func (c *pride) readInput() {
	scanner := bufio.NewReader(os.Stdin) // read n and m
	fmt.Fscanf(scanner, "%d\n", &c.n)

	c.arr = make([]int, c.n)
	for i := range c.arr {
		fmt.Fscanf(scanner, "%d", &c.arr[i])
	}
}

func (c *pride) solve() int {
	n, arr := c.n, c.arr

	var gcdTmp, gcdAll int
	var i, j, min int

	// count number of 1
	count1 := 0
	for i = range arr {
		if arr[i] == 1 {
			count1++
		}
	}
	if count1 > 0 {
		return n - count1
	}

	// check all gcd
	for gcdAll, i = arr[0], 1; i < n; i++ {
		gcdAll = gcd(arr[i], gcdAll)
	}
	if gcdAll > 1 {
		return -1
	}

	// find continuous subarray has gcd = 1 with min length
	for min, i = n+1, 1; i < n; i++ {
		for gcdTmp, j = arr[i], i-1; j >= 0; j-- {
			if gcdTmp = gcd(arr[j], gcdTmp); gcdTmp == 1 {
				if i-j < min {
					min = i - j
				}
			}
		}
	}

	return n - 1 + min
}

func gcd(a, b int) int {
	if a == 1 || b == 1 {
		return 1
	}

	if a < b {
		a, b = b, a
	}

	for b != 0 {
		a, b = b, a%b
	}

	return a
}

func main() {
	var p pride
	p.readInput()
	fmt.Print(p.solve())
}

package main

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

type pride struct {
	n   int
	arr []int
}

func (c *pride) readInput() {
	scanner := bufio.NewReader(os.Stdin) // read n and m
	fmt.Fscanf(scanner, "%d\n", &c.n)

	c.arr = make([]int, c.n)
	for i := range c.arr {
		fmt.Fscanf(scanner, "%d", &c.arr[i])
	}
}

func (c *pride) solve() int {
	n, arr := c.n, c.arr

	var gcdTmp, gcdAll int
	var i, j, min int

	// count number of 1
	count1 := 0
	for i = range arr {
		if arr[i] == 1 {
			count1++
		}
	}
	if count1 > 0 {
		return n - count1
	}

	// check all gcd
	for gcdAll, i = arr[0], 1; i < n; i++ {
		gcdAll = gcd(arr[i], gcdAll)
	}
	if gcdAll > 1 {
		return -1
	}

	// find continuous subarray has gcd = 1 with min length
	for min, i = n+1, 1; i < n; i++ {
		for gcdTmp, j = arr[i], i-1; j >= 0; j-- {
			if gcdTmp = gcd(arr[j], gcdTmp); gcdTmp == 1 {
				if i-j < min {
					min = i - j
				}
				break
			}
		}
	}

	return n - 1 + min
}

func gcd(a, b int) int {
	if a == 1 || b == 1 {
		return 1
	}

	if a < b {
		a, b = b, a
	}

	for b != 0 {
		a, b = b, a%b
	}

	return a
}

func main() {
	var p pride
	p.readInput()
	fmt.Print(p.solve())
}

/*
URL:
https://codeforces.com/problemset/problem/891/A
*/

package main

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

/********** 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 (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	n int
	A []int
)

func main() {
	n = ReadInt()
	A = ReadIntSlice(n)

	one := sub()
	if one > 0 {
		fmt.Println(n - one)
		return
	}

	diff := INF_BIT30
	for i := 0; i < n; i++ {
		g := A[i]
		for j := i + 1; j < n; j++ {
			g = Gcd(g, A[j])
			if g == 1 {
				ChMin(&diff, j-i)
			}
		}
	}

	if diff < INF_BIT30 {
		fmt.Println(diff + n - 1)
	} else {
		fmt.Println(-1)
	}
}

func sub() int {
	res := 0
	for i := 0; i < n; i++ {
		if A[i] == 1 {
			res++
		}
	}
	return res
}

// 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
}

// 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 == 0 {
		return b
	}
	if b == 0 {
		return a
	}

	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
}

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

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

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

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	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
}

// 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])
		res = append(res, []rune(str)...)

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

	return string(res)
}

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

	for i := 0; i < len(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)
}

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/891/A

package main

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

func gcd(x, y int) int {
	if y == 0 {
		return x
	}
	return gcd(y, x%y)
}

func Solve(n int, a []int) int {
	ones := 0
	for _, elem := range a {
		if elem == 1 {
			ones += 1
		}
	}
	if ones > 0 {
		return n - ones
	}
	best := math.MaxInt32
	for i, elem := range a {
		d := elem
		for j := i - 1; j >= 0; j-- {
			d = gcd(d, a[j])
			if d == 1 && i-j < best {
				best = i - j
			}
		}
	}
	if best == math.MaxInt32 {
		return -1
	}
	return best + n - 1
}

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

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, a))
}

package main

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

func gcd(x, y int) int {
	for y != 0 {
		x, y = y, x%y
	}
	return x
}

func solve() {
	n := readInt()
	a := readIntArray(n)
	for i := 1; i < n; i++ {
		if a[i-1] != a[i] {
			if gcd(a[i-1], a[i]) == 1 {
				fmt.Println(n)
				return
			} else if i >= 2 && gcd(a[i-2], gcd(a[i-1], a[i])) == 1 {
				fmt.Println(n + 1)
				return
			} else if i+1 < n && gcd(a[i+1], gcd(a[i-1], a[i])) == 1 {
				fmt.Println(n + 1)
				return
			}
		}
	}
	fmt.Println(-1)
}

var scanner *bufio.Scanner

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

// 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
}

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

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

package main

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

func gcd(x, y int) int {
	for y != 0 {
		x, y = y, x%y
	}
	return x
}

func solve() {
	n := readInt()
	a := readIntArray(n)
	for i := 0; i < n; i++ {
		if a[i] == 1 {
			fmt.Println(n - 1)
		}
	}
	for i := 1; i < n; i++ {
		if a[i-1] != a[i] {
			if gcd(a[i-1], a[i]) == 1 {
				fmt.Println(n)
				return
			} else if i >= 2 && gcd(a[i-2], gcd(a[i-1], a[i])) == 1 {
				fmt.Println(n + 1)
				return
			} else if i+1 < n && gcd(a[i+1], gcd(a[i-1], a[i])) == 1 {
				fmt.Println(n + 1)
				return
			}
		}
	}
	fmt.Println(-1)
}

var scanner *bufio.Scanner

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

// 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
}

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

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

package main

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

func gcd(x, y int) int {
	for y != 0 {
		x, y = y, x%y
	}
	return x
}

func solve() {
	n := readInt()
	a := readIntArray(n)
	cnt1 := 0
	for i := 0; i < n; i++ {
		if a[i] == 1 {
			cnt1++
		}
	}
	if cnt1 > 0 {
		fmt.Println(n - cnt1)
		return
	}
	for i := 1; i < n; i++ {
		if a[i-1] != a[i] {
			if gcd(a[i-1], a[i]) == 1 {
				fmt.Println(n)
				return
			} else if i >= 2 && gcd(a[i-2], gcd(a[i-1], a[i])) == 1 {
				fmt.Println(n + 1)
				return
			} else if i+1 < n && gcd(a[i+1], gcd(a[i-1], a[i])) == 1 {
				fmt.Println(n + 1)
				return
			}
		}
	}
	fmt.Println(-1)
}

var scanner *bufio.Scanner

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

// 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
}

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

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

package main

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

func gcd(x, y int) int {
	for y != 0 {
		x, y = y, x%y
	}
	return x
}

func solve() {
	n := readInt()
	a := readIntArray(n)
	cnt1 := 0
	for i := 0; i < n; i++ {
		if a[i] == 1 {
			cnt1++
		}
	}
	if cnt1 > 0 {
		fmt.Println(n - cnt1)
		return
	}
	best := 100000
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			if gcd(a[i], a[j]) == 1 {
				best = min(best, j-i+n-1)
			}
		}
	}
	if best == 100000 {
		fmt.Println(-1)
	} else {
		fmt.Println(best)
	}
}

var scanner *bufio.Scanner

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

// 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
}

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

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

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/891/A

package main

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

func gcd(x, y int) int {
	if y == 0 {
		return x
	}
	return gcd(y, x%y)
}

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

func Solve(n int, a []int) int {
	ones := 0
	for _, elem := range a {
		if elem == 1 {
			ones += 1
		}
	}
	if ones > 0 {
		return n - ones
	}
	best := math.MaxInt32
	for i, elem := range a {
		for j := i + 1; j < n; j++ {
			d := gcd(elem, a[j])
			if d == 1 {
				best = min(best, j-i)
			}
		}
	}
	if best == math.MaxInt32 {
		return -1
	}
	return best + n - 1
}

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

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, a))
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/891/A

package main

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

func gcd(x, y int) int {
	if y == 0 {
		return x
	}
	return gcd(y, x%y)
}

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

func Solve(n int, a []int) int {
	ones := 0
	coprime := false
	triplet := []int{}
	middle := 1
	tripletAnswer := math.MaxInt32
	for i, elem := range a {
		if elem == 1 {
			ones += 1
		}
		if i+1 <= n-1 {
			d := gcd(a[i], a[i+1])
			if d == 1 {
				coprime = true
			}
		}
		if len(triplet) == 2 && triplet[len(triplet)-1] == elem {
			middle += 1
		} else if len(triplet) == 0 || (len(triplet) != 3 && triplet[len(triplet)-1] != elem) {
			triplet = append(triplet, elem)
		}
		if len(triplet) == 3 {
			x := gcd(triplet[0], triplet[1])
			y := gcd(triplet[1], triplet[2])
			if gcd(x, y) == 1 {
				tripletAnswer = min(tripletAnswer, n+middle)
			}
			triplet = []int{triplet[1], triplet[2]}
			middle = 1
		}
	}
	if ones > 0 {
		return n - ones
	} else if coprime {
		return n
	} else if tripletAnswer == math.MaxInt32 {
		return -1
	}
	return tripletAnswer
}

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

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, a))
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/891/A

package main

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

func gcd(x, y int) int {
	if y == 0 {
		return x
	}
	return gcd(y, x%y)
}

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

func Solve(n int, a []int) int {
	ones := 0
	coprime := false
	triplet := []int{}
	middle := 1
	tripletAnswer := math.MaxInt32
	for i, elem := range a {
		if elem == 1 {
			ones += 1
		}
		if i+1 <= n-1 {
			d := gcd(a[i], a[i+1])
			if d == 1 {
				coprime = true
			}
		}
		if len(triplet) == 2 && triplet[len(triplet)-1] == elem {
			middle += 1
		} else if len(triplet) == 0 || (len(triplet) != 3 && triplet[len(triplet)-1] != elem) {
			triplet = append(triplet, elem)
		}
		if len(triplet) == 3 {
			x := gcd(triplet[0], triplet[1])
			y := gcd(triplet[1], triplet[2])
			if gcd(x, y) == 1 {
				tripletAnswer = min(tripletAnswer, n+middle)
			}
			triplet = []int{triplet[2]}
			middle = 1
		}
	}
	if ones > 0 {
		return n - ones
	} else if coprime {
		return n
	} else if tripletAnswer == math.MaxInt32 {
		return -1
	}
	return tripletAnswer
}

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

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, a))
}


In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them.

NoteIn the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3.In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles.

package main

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

func main() {
	var n int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}
	max := a[0]
	for i := 1; i < n; i++ {
		if max < a[i] {
			max = a[i]
		}
	}
	count := 0
	for i := 0; i < n; i++ {
		count += max - a[i]
	}
	fmt.Println(count)
}

// A. Holiday Of Equality
/*
In Berland it is the holiday of equality. In honor of the holiday the king
decided to equalize the welfare of all citizens in Berland by the expense of
the state treasury.

Totally in Berland there are n citizens, the welfare of each of them is
estimated as the integer in ai burles (burle is the currency in Berland).

You are the royal treasurer, which needs to count the minimum charges of the
kingdom on the king's present. The king can only give money, he hasn't a
power to take away them.

Input
The first line contains the integer n (1 ≤ n ≤ 100) — the number of
citizens in the kingdom.

The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) —
the welfare of the i-th citizen.

Output
In the only line print the integer S — the minimum number of burles which
are had to spend.

Examples
Input
5
0 1 2 3 4

Output
10

Input
5
1 1 0 1 1

Output
1

Input
3
1 3 1

Output
4

Input
1
12

Output
0

Note
In the first example if we add to the first citizen 4 burles, to the second 3,
to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.

In the second example it is enough to give one burle to the third citizen.

In the third example it is necessary to give two burles to the first and the
third citizens to make the welfare of citizens equal 3.

In the fourth example it is possible to give nothing to everyone because all
citizens have 12 burles.
*/
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	input1, _ := reader.ReadString('\n')
	n, _ := strconv.Atoi(strings.TrimSpace(input1))

	input2, _ := reader.ReadString('\n')
	aArr := strings.Split(strings.TrimSpace(input2), " ")

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

	sort.SliceStable(iArr, func(i, j int) bool {
		return iArr[i] > iArr[j]
	})

	total := 0
	for i := 1; i < n; i++ {
		total += (iArr[0] - iArr[i])
	}
	fmt.Print(total)
}

// A. Holiday Of Equality
/*
In Berland it is the holiday of equality. In honor of the holiday the king
decided to equalize the welfare of all citizens in Berland by the expense of
the state treasury.

Totally in Berland there are n citizens, the welfare of each of them is
estimated as the integer in ai burles (burle is the currency in Berland).

You are the royal treasurer, which needs to count the minimum charges of the
kingdom on the king's present. The king can only give money, he hasn't a
power to take away them.

Input
The first line contains the integer n (1 ≤ n ≤ 100) — the number of
citizens in the kingdom.

The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) —
the welfare of the i-th citizen.

Output
In the only line print the integer S — the minimum number of burles which
are had to spend.

Examples
Input
5
0 1 2 3 4

Output
10

Input
5
1 1 0 1 1

Output
1

Input
3
1 3 1

Output
4

Input
1
12

Output
0

Note
In the first example if we add to the first citizen 4 burles, to the second 3,
to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4.

In the second example it is enough to give one burle to the third citizen.

In the third example it is necessary to give two burles to the first and the
third citizens to make the welfare of citizens equal 3.

In the fourth example it is possible to give nothing to everyone because all
citizens have 12 burles.
*/
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	input1, _ := reader.ReadString('\n')
	n, _ := strconv.Atoi(strings.TrimSpace(input1))

	input2, _ := reader.ReadString('\n')
	aArr := strings.Split(strings.TrimSpace(input2), " ")

	max := 0
	iArr := make([]int, n)
	for i := 0; i < n; i++ {
		iArr[i], _ = strconv.Atoi(aArr[i])

		if iArr[i] > max {
			max = iArr[i]
		}
	}

	//	sort.SliceStable(iArr, func(i, j int) bool {
	//		return iArr[i] > iArr[j]
	//	})

	total := 0
	for i := 0; i < n; i++ {
		total += (max - iArr[i])
	}
	fmt.Print(total)
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var a, b int
	s := []int{}
	fmt.Scan(&b)
	for i := 0; i < b; i++ {
		fmt.Scan(&a)
		s = append(s, a)
	}
	sort.Ints(s)
	m := s[b-1]
	summa := 0
	for j := 0; j<(b-1); j++ {
		summa += (m-s[j])
	}
	fmt.Println(summa)
}

package main

import (
	"fmt"
	"sort"
)

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

	a := make([]int, n)
	for i := range a {
		fmt.Scan(&a[i])
	}

	sort.Ints(a)
	max := a[n-1]

	ans := 0
	for _, v := range a {
		ans += max - v
	}

	fmt.Println(ans)
}

package main

import ("fmt"
        "sort")

func main()  {
	var n,sum1 int
	var input int
	fmt.Scan(&n)
	arr:=make([]int,n)
	for i := 0; i < n; i++ {
	  fmt.Scan(&arr[i])
	  input=input+arr[i]

	}
	sort.Ints(arr)
	//fmt.Println(arr)
//	fmt.Println(arr[n-1],input)
	sum1=arr[n-1]*n
	res:=sum1-input
	fmt.Print(res)
}

package main

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

// Buffered IO for fast I/O.
var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

// Helper functions to wrap around the buffered IO.
func printf(f string, a ...interface{}) {
	fmt.Fprintf(writer, f, a...)
}

// For this function, don't forget to add '\n' every scanf() call.
func scanf(f string, a ...interface{}) {
	fmt.Fscanf(reader, f, a...)
}

// Solve
func max(array ...int) int {
	max := 0
	for _, value := range array {
		if value > max {
			max = value
		}
	}

	return max
}

func solve() {
	// Solution
	var testCases, ans int

	scanf("%d\n", &testCases)
	var welfares = make([]int, testCases)

	for index := range welfares {
		scanf("%d", &welfares[index])
	}

	maxValue := max(welfares...)

	for _, value := range welfares {
		if value != maxValue {
			ans += maxValue - value
		}
	}

	printf("%d", ans)
}

func main() {
	defer writer.Flush()
	solve()
}

package main

import "fmt"

func main() {
	var buf []int
	var n, maxn, sum, k int
	fmt.Scan(&k)
	for i:=0; i<k; i++ {
		fmt.Scan(&n)
		buf = append(buf, n)
	}
	for i:=0; i<k; i++ {
		if buf[i]>maxn {
			maxn = buf[i]
		}
	}
	for i:= 0; i<k; i++ {
		if buf[i] != maxn {
			sum += maxn-buf[i]
		}
	}
	fmt.Println(sum)
}

package main

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

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

package main

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

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	max_number := 0
	array := make([]int, n)

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; scanner.Scan(); i++ {
		a, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		array[i] = a
		if a > max_number {
			max_number = a
		}
	}

	need := 0
	for i := 0; i < n; i++ {
		need += max_number - array[i]
	}

	fmt.Println(need)
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	var a int
	var m int = 0
	var ss int = 0
	for i := 0; i < n; i++ {
		fmt.Scan(&a)
		if m < a {
			m = a
		}
		ss += a
	}
	fmt.Println(m*n - ss)
}

package main

import "fmt"

func kazna(n int, max int, arr []int) int {
	count := 0
	for i := 0; i < n; i++ {
		count += max - arr[i]
	}
	return count
}
func main() {
	var n, a int
	var max int = 0
	fmt.Scan(&n)
	arr := make([]int, 0, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a)
		arr = append(arr, a)
		if a > max {
			max = a
		}
	}
	fmt.Print(kazna(n, max, arr))
}

package main

import (
	"fmt"
)

func main() {
	var numbers_citizens int
	fmt.Scanf("%d\n", &numbers_citizens)
	array := make([]int, numbers_citizens)
	max := array[0]
	for i := 0; i < numbers_citizens; i++ {
		fmt.Scanf("%d", &array[i])
		if array[i] > max {
			max = array[i]
		}
	}
	S := 0
	for i := 0; i < len(array); i++ {
		S += max - array[i]
	}

	fmt.Println(S)
}

package main

import "fmt"

func main() {
	mx := 0
	var n int
	fmt.Scanln(&n)
	var x int
	sm := 0
	for i := 0; i < n; i++ {
		fmt.Scan(&x)
		if x > mx {
			sm += (x - mx) * i
			mx = x
		} else {
			sm += mx - x
		}
	}
	fmt.Println(sm)
}

package main

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

func solve() {
	n := readInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = readInt()
	}
	sort.Ints(a)
	res := 0
	for i := 0; i < n-1; i++ {
		res += a[n-1] - a[i]

	}
	fmt.Println(res)
}

func getStats(str string) map[byte]int {
	stat := map[byte]int{}
	for _, v := range str {
		stat[byte(v)]++
	}
	return stat
}

var scanner *bufio.Scanner

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

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

// 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
}

package main

import 	(
	"fmt"
)

func main() {
	var n int
	var m [101]int

	fmt.Scan(&n)

	max := m[0]
	sum := 0

	for i:=0; i<n; i++{
		fmt.Scan(&m[i])
		if m[i] > max{
			max = m[i]
		}
	}
	for i:=0; i<n; i++{
		sum += max - m[i]
	}
	fmt.Println(sum)

}

package main

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

func main() {
	var n, inp, sum int
	var s []int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &inp)
		s = append(s, inp)
	}
	sort.Ints(s)
	max := s[len(s)-1]
	for i := 0; i < len(s); i++ {
		sum += max - s[i]
	}
	fmt.Println(sum)
}

package main
import (
	"fmt"
)

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

	a := make([]int, n, n)

	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}

	max := a[0]
	la := len(a)
	total := 0

	for i := 1; i < la; i++ {
		if a[i] > max {
			total = total + (a[i] - max) * i
			max = a[i]
		} else {
			total = total + max - a[i]
		}
	}

	fmt.Println(total)
}

package main

import "fmt"

func counter(n int, maximum int, buf []int) int {
	result := 0
	for i := 0; i < n; i++ {
		result += maximum - buf[i]
	}
	return result
}

func main() {
	var n, a int
	var maximum int = 0
	fmt.Scan(&n)
	buf := make([]int, 0, n)
	for i := 1; i <= n; i++ {
		fmt.Scan(&a)
		buf = append(buf, a)
		if a > maximum {
			maximum = a
		}
	}
	fmt.Print(counter(n, maximum, buf))
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	var total, max int
	for i := 0; i < n; i++ {
		var tmp int
		fmt.Scan(&tmp)
		total += tmp
		if max < tmp {
			max = tmp
		}
	}
	fmt.Print(max*n - total)
}

package main

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

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

	N := io.NextInt()
	A := io.NextIntArray(N)

	sum := 0
	max := 0
	for _, v := range A {
		sum += v
		max = Max(max, v)
	}
	io.Printf("%d\n", (max * N) - sum)

	io.FlushOutput()
}

func Max(lhs, rhs int) int {
	if lhs > rhs {
		return lhs
	} else {
		return rhs
	}
}

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) 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
	}
}

package main

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

func main() {
	sc := NewScanner()
	_ = sc.NextInt()
	A := sc.NextIntArray()

	mx := 0
	for _, v := range A {
		mx = max(mx, v)
	}
	ans := 0
	for _, v := range A {
		if mx > v {
			ans += mx - v
		}
	}
	fmt.Println(ans)

}

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

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) 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 *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf) + 1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf) + 1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) 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
		}
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var a, b int
	mas:= []int{}
	fmt.Scan(&b)
	for i := 0; i < b; i++ {
		fmt.Scan(&a)
		mas = append(mas, a)
	}

	sort.Ints(mas)
	x := mas[b-1]
	summ := 0

	for j := 0; j<(b-1); j++ {
		summ += (x-mas[j])
	}

	fmt.Println(summ)
}

package main

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

// https://codeforces.com/problemset/problem/758/A
// A. Holiday Of Equality
// time limit per test
// 1 second
// memory limit per test
// 256 megabytes
// input
// standard input
// output
// standard output

// In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury.

// Totally in Berland there are n citizens, the welfare of each of them is estimated as the integer in ai burles (burle is the currency in Berland).

// You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them.
// Input

// The first line contains the integer n (1 ≤ n ≤ 100) — the number of citizens in the kingdom.

// The second line contains n integers a1, a2, ..., an, where ai (0 ≤ ai ≤ 106) — the welfare of the i-th citizen.
// Output

// In the only line print the integer S — the minimum number of burles which are had to spend.

func main() {
	bufStdin := bufio.NewReader(os.Stdin)

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

	as := make([]int, n)

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

	m := max(as)

	var cost int
	for _, v := range as {
		cost += m - v
	}
	_, _ = fmt.Print(cost)
}

func max(s []int) int {
	m := s[0]
	for i := 1; i < len(s); i++ {
		if s[i] > m {
			m = s[i]
		}
	}
	return m
}

package main

import "fmt"

// https://codeforces.com/problemset/problem/758/A
// комментарии к решению
// Сначала находим максимум, а после суммируем разницу
// каждого элемента массива и максимального
func taskSolution(a []int) (ans int) {
	max := 0
	for _, ai := range a {
		if max < ai {
			max = ai
		}
	}
	for _, ai := range a {
		ans += max - ai
	}
	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 (
	"bufio"
	"os"
	"strconv"
)

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

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

func PrintInts(ints ...int) {
	for _, value := range ints {
		writer.WriteString(strconv.Itoa(value))
		writer.WriteByte(' ')
	}
}

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

	var n int = ReadInt32()
	var result, max int = 0, 0
	a := make([]int, n)

	for i, _ := range a {
		a[i] = ReadInt32()
		if a[i] > max {
			result += i * (a[i] - max)
			max = a[i]
		} else {
			result += max - a[i]
		}
	}

	PrintInts(result)
}

package main

import ("fmt"
        "sort")

func main()  {
	var n,sum1 int
	var input int
	arr:=make([]int,100)
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
	  fmt.Scan(&arr[i])
	  input=input+arr[i]

	}
	sort.Ints(arr)
	sum1=arr[n-1]*n
	res:=input-sum1
	fmt.Print(res)
}

package main

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

// Buffered IO for fast I/O.
var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

// Helper functions to wrap around the buffered IO.
func printf(f string, a ...interface{}) {
	fmt.Fprintf(writer, f, a...)
}

// For this function, don't forget to add '\n' every scanf() call.
func scanf(f string, a ...interface{}) {
	fmt.Fscanf(reader, f, a...)
}

// Solve
func max(array ...int) int {
	max := 0
	for _, value := range array {
		if value > max {
			max = value
		}
	}

	return max
}

func solve() {
	// Solution
	var testCases, ans int

	scanf("%d\n", &testCases)
	var welfares = make([]int, testCases)

	for i := 0; i < testCases; i++ {
		var tmp int
		scanf("%d\n", &tmp)
		welfares[i] = tmp
	}

	maxValue := max(welfares...)

	for _, value := range welfares {
		if value != maxValue {
			ans += maxValue - value
		}
	}

	printf("%d", ans)
}

func main() {
	defer writer.Flush()
	solve()
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scanf("%d", &n)
	var a [110]int
	var m int = 0
	var ss int = 0
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		if m < a[i] {
			m = a[i]
		}
		ss += a[i]
	}
	fmt.Println(m*n - ss)
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scanf("%d", &n)
	var a int
	var m int = 0
	var ss int = 0
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a)
		if m < a {
			m = a
		}
		ss += a
	}
	fmt.Println(n)
	fmt.Println(m)
	fmt.Println(ss)
	fmt.Println(m*n - ss)
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scanf("%d", &n)
	a := make([]int, n)
	var m int = 0
	var ss int = 0
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		if m < a[i] {
			m = a[i]
		}
		ss += a[i]
	}
	fmt.Println(m*n - ss)
}

package main

import (
	"fmt"
)

func main() {
	var numbers_citizens int
	fmt.Scanf("%d", &numbers_citizens)
	array := make([]int, numbers_citizens)
	max := array[0]
	for i := 0; i < numbers_citizens; i++ {
		fmt.Scanf("%d", &array[i])
		if array[i] > max {
			max = array[i]
		}
	}
	S := 0
	for i := 0; i < len(array); i++ {
		S += max - array[i]
	}
	fmt.Println(S)
}

package main

import (
	"fmt"
)

func main() {
	var numbers_citizens int
	fmt.Scanf("%d", &numbers_citizens)
	array := make([]int, numbers_citizens)
	max := array[0]
	for i := 0; i < numbers_citizens; i++ {
		fmt.Scanf("%d", &array[i])
		if array[i] > max {
			max = array[i]
		}
	}
	S := 0
	for i := 0; i < len(array); i++ {
		S += max - array[i]
	}
}


You are given a set of $$$n\ge 2$$$ pairwise different points with integer coordinates. Your task is to partition these points into two nonempty groups $$$A$$$ and $$$B$$$, such that the following condition holds:For every two points $$$P$$$ and $$$Q$$$, write the Euclidean distance between them on the blackboard: if they belong to the same group — with a yellow pen, and if they belong to different groups — with a blue pen. Then no yellow number is equal to any blue number.It is guaranteed that such a partition exists for any possible input. If there exist multiple partitions, you are allowed to output any of them.

The first line contains one integer $$$n$$$ $$$(2 \le n \le 10^3)$$$ — the number of points. The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$-10^6 \le x_i, y_i \le 10^6$$$) — the coordinates of the $$$i$$$-th point.  It is guaranteed that all $$$n$$$ points are pairwise different.

In the first line, output $$$a$$$ ($$$1 \le a \le n-1$$$) — the number of points in a group $$$A$$$. In the second line, output $$$a$$$ integers — the indexes of points that you include into group $$$A$$$. If there are multiple answers, print any.

NoteIn the first example, we set point $$$(0, 0)$$$ to group $$$A$$$ and points $$$(0, 1)$$$ and $$$(1, 0)$$$ to group $$$B$$$. In this way, we will have $$$1$$$ yellow number $$$\sqrt{2}$$$ and $$$2$$$ blue numbers $$$1$$$ on the blackboard.In the second example, we set points $$$(0, 1)$$$ and $$$(0, -1)$$$ to group $$$A$$$ and points $$$(-1, 0)$$$ and $$$(1, 0)$$$ to group $$$B$$$. In this way, we will have $$$2$$$ yellow numbers $$$2$$$, $$$4$$$ blue numbers $$$\sqrt{2}$$$ on the blackboard.


Some time ago Homer lived in a beautiful city. There were $$$n$$$ blocks numbered from $$$1$$$ to $$$n$$$ and $$$m$$$ directed roads between them. Each road had a positive length, and each road went from the block with the smaller index to the block with the larger index. For every two (different) blocks, there was at most one road between them. Homer discovered that for some two numbers $$$L$$$ and $$$R$$$ the city was $$$(L, R)$$$-continuous. The city is said to be $$$(L, R)$$$-continuous, if   all paths from block $$$1$$$ to block $$$n$$$ are of length between $$$L$$$ and $$$R$$$ (inclusive); and  for every $$$L \leq d \leq R$$$, there is exactly one path from block $$$1$$$ to block $$$n$$$ whose length is $$$d$$$. A path from block $$$u$$$ to block $$$v$$$ is a sequence $$$u = x_0 \to x_1 \to x_2 \to \dots \to x_k = v$$$, where there is a road from block $$$x_{i-1}$$$ to block $$$x_{i}$$$ for every $$$1 \leq i \leq k$$$. The length of a path is the sum of lengths over all roads in the path. Two paths $$$x_0 \to x_1 \to \dots \to x_k$$$ and $$$y_0 \to y_1 \to \dots \to y_l$$$ are different, if $$$k \neq l$$$ or $$$x_i \neq y_i$$$ for some $$$0 \leq i \leq \min\{k, l\}$$$. After moving to another city, Homer only remembers the two special numbers $$$L$$$ and $$$R$$$ but forgets the numbers $$$n$$$ and $$$m$$$ of blocks and roads, respectively, and how blocks are connected by roads. However, he believes the number of blocks should be no larger than $$$32$$$ (because the city was small).As the best friend of Homer, please tell him whether it is possible to find a $$$(L, R)$$$-continuous city or not.

The single line contains two integers $$$L$$$ and $$$R$$$ ($$$1 \leq L \leq R \leq 10^6$$$).

If it is impossible to find a $$$(L, R)$$$-continuous city within $$$32$$$ blocks, print "NO" in a single line. Otherwise, print "YES" in the first line followed by a description of a $$$(L, R)$$$-continuous city.  The second line should contain two integers $$$n$$$ ($$$2 \leq n \leq 32$$$) and $$$m$$$ ($$$1 \leq m \leq \frac {n(n-1)} 2$$$), where $$$n$$$ denotes the number of blocks and $$$m$$$ denotes the number of roads. Then $$$m$$$ lines follow. The $$$i$$$-th of the $$$m$$$ lines should contain three integers $$$a_i$$$, $$$b_i$$$ ($$$1 \leq a_i < b_i \leq n$$$) and $$$c_i$$$ ($$$1 \leq c_i \leq 10^6$$$) indicating that there is a directed road from block $$$a_i$$$ to block $$$b_i$$$ of length $$$c_i$$$.  It is required that for every two blocks, there should be no more than 1 road connecting them. That is, for every $$$1 \leq i < j \leq m$$$, either $$$a_i \neq a_j$$$ or $$$b_i \neq b_j$$$.

NoteIn the first example there is only one path from block $$$1$$$ to block $$$n = 2$$$, and its length is $$$1$$$. In the second example there are three paths from block $$$1$$$ to block $$$n = 5$$$, which are $$$1 \to 2 \to 5$$$ of length $$$4$$$, $$$1 \to 3 \to 5$$$ of length $$$5$$$ and $$$1 \to 4 \to 5$$$ of length $$$6$$$.

package main

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

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

	L, R := readTwoNums(reader)

	n, E := solve(L, R)

	var buf bytes.Buffer

	buf.WriteString(fmt.Sprintf("YES\n%d %d\n", n, len(E)))

	for i := 0; i < len(E); i++ {
		buf.WriteString(fmt.Sprintf("%d %d %d\n", E[i][0], E[i][1], E[i][2]))
	}

	fmt.Print(buf.String())
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(L, R int) (int, [][]int) {
	var edges [][]int

	add := func(u, v, w int) {
		edges = append(edges, []int{u, v, w})
	}

	var process func(l, r int) int

	process = func(l, r int) int {
		if l > 1 {
			k := process(1, r-l+1)
			add(k, k+1, l-1)
			return k + 1
		}

		if (r & -r) == r {
			k := ctz(r)
			add(1, 2, 1)
			for i := 3; i <= k+2; i++ {
				add(1, i, 1)
				for j := 2; j < i; j++ {
					add(j, i, 1<<(j-2))
				}
			}
			return k + 2
		}

		var k int

		for 1<<(k+1) <= r-1 {
			k++
		}

		process(1, 1<<k)

		add(1, k+3, 1)
		for i := 0; i <= k; i++ {
			if ((r-1)>>i)&1 == 1 {
				add(i+2, k+3, 1+((r-1)>>(i+1)<<(i+1)))
			}
		}
		return k + 3
	}

	k := process(L, R)

	return k, edges
}

func ctz(num int) int {
	if num == 0 {
		return -1
	}
	var i int
	for num&1 == 0 {
		num >>= 1
		i++
	}
	return i
}

package main

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

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

	L, R := readTwoNums(reader)

	n, E := solve(L, R)

	var buf bytes.Buffer

	buf.WriteString(fmt.Sprintf("YES\n%d %d\n", n, len(E)))

	for i := 0; i < len(E); i++ {
		buf.WriteString(fmt.Sprintf("%d %d %d\n", E[i][0], E[i][1], E[i][2]))
	}

	fmt.Print(buf.String())
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(L, R int) (int, [][]int) {
	var edges [][]int

	add := func(u, v, w int) {
		edges = append(edges, []int{u, v, w})
	}

	var process func(l, r int) int

	process = func(l, r int) int {
		if l > 1 {
			k := process(1, r-l+1)
			add(k, k+1, l-1)
			return k + 1
		}

		if (r & -r) == r {
			k := ctz(r)
			add(1, 2, 1)
			for i := 3; i <= k+2; i++ {
				add(1, i, 1)
				for j := 2; j < i; j++ {
					add(j, i, 1<<(j-2))
				}
			}
			return k + 2
		}

		var k int

		for 1<<(k+1) <= r-1 {
			k++
		}

		process(1, 1<<k)

		add(1, k+3, 1)
		for i := 0; i <= k; i++ {
			if ((R-1)>>i)&1 == 1 {
				add(i+2, k+3, 1+((r-1)>>(i+1)<<(i+1)))
			}
		}
		return k + 3
	}

	k := process(L, R)

	return k, edges
}

func ctz(num int) int {
	if num == 0 {
		return -1
	}
	var i int
	for num&1 == 0 {
		num >>= 1
		i++
	}
	return i
}

package main

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

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

	L, R := readTwoNums(reader)

	n, E := solve(L, R)

	var buf bytes.Buffer

	buf.WriteString(fmt.Sprintf("YES\n%d %d\n", n, len(E)))

	for i := 0; i < len(E); i++ {
		buf.WriteString(fmt.Sprintf("%d %d %d\n", E[i][0], E[i][1], E[i][2]))
	}

	fmt.Print(buf.String())
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(L, R int) (int, [][]int) {
	var edges [][]int

	add := func(u, v, w int) {
		edges = append(edges, []int{u, v, w})
	}

	var process func(l, r int) int

	process = func(l, r int) int {
		if l > 1 {
			k := process(1, r-l+1)
			add(k, k+1, l-1)
			return k + 1
		}

		if (r & -r) == r {
			k := ctz(r)
			add(1, 2, 1)
			for i := 3; i <= k+2; i++ {
				add(1, i, 1)
				for j := 2; j < i; j++ {
					add(j, i, 1<<(j-2))
				}
			}
			return k + 2
		}

		var k int

		for 1<<(k+1) <= r-1 {
			k++
		}

		process(1, 1<<k)

		add(1, k+3, 1)
		for i := 0; i <= k; i++ {
			if ((R-1)>>i)&1 == 1 {
				add(i+2, k+3, 1+((r-1)>>(i+1)<<(i+1)))
			}
		}
		return l + 3
	}

	k := process(L, R)

	return k, edges
}

func ctz(num int) int {
	if num == 0 {
		return -1
	}
	var i int
	for num&1 == 0 {
		num >>= 1
		i++
	}
	return i
}

package main

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

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

	L, R := readTwoNums(reader)

	n, E := solve(L, R)

	var buf bytes.Buffer

	buf.WriteString(fmt.Sprintf("YES\n%d %d\n", n, len(E)))

	for i := 0; i < len(E); i++ {
		buf.WriteString(fmt.Sprintf("%d %d %d\n", E[i][0], E[i][1], E[i][2]))
	}

	fmt.Print(buf.String())
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(L, R int) (int, [][]int) {
	var edges [][]int

	add := func(u, v, w int) {
		edges = append(edges, []int{u, v, w})
	}

	var process func(l, r int) int

	process = func(l, r int) int {
		if l > 1 {
			k := process(1, r-l+1)
			add(k, k+1, l-1)
			return k + 1
		}

		if (r & -r) == r {
			k := ctz(r)
			add(1, 2, 1)
			for i := 3; i <= k+2; i++ {
				add(1, i, 1)
				for j := 2; j < i; j++ {
					add(j, i, 1<<(j-2))
				}
			}
			return k + 2
		}

		var k int

		for 1<<(k+1) <= r-1 {
			k++
		}

		process(1, 1<<k)

		add(1, k+3, 1)
		for i := 0; i <= k; i++ {
			if ((R-1)>>i)&1 == 1 {
				add(i+2, k+3, 1+1+((r-1)>>(i+1)<<(i+1)))
			}
		}
		return l + 3
	}

	k := process(L, R)

	return k, edges
}

func ctz(num int) int {
	if num == 0 {
		return -1
	}
	var i int
	for num&1 == 0 {
		num >>= 1
		i++
	}
	return i
}


Little Chris is participating in a graph cutting contest. He's a pro. The time has come to test his skills to the fullest.Chris is given a simple undirected connected graph with n vertices (numbered from 1 to n) and m edges. The problem is to cut it into edge-distinct paths of length 2. Formally, Chris has to partition all edges of the graph into pairs in such a way that the edges in a single pair are adjacent and each edge must be contained in exactly one pair.For example, the figure shows a way Chris can cut a graph. The first sample test contains the description of this graph.  You are given a chance to compete with Chris. Find a way to cut the given graph or determine that it is impossible!

The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 105), the number of vertices and the number of edges in the graph. The next m lines contain the description of the graph's edges. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ n; ai ≠ bi), the numbers of the vertices connected by the i-th edge. It is guaranteed that the given graph is simple (without self-loops and multi-edges) and connected. Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.

If it is possible to cut the given graph into edge-distinct paths of length 2, output  lines. In the i-th line print three space-separated integers xi, yi and zi, the description of the i-th path. The graph should contain this path, i.e., the graph should contain edges (xi, yi) and (yi, zi). Each edge should appear in exactly one path of length 2. If there are multiple solutions, output any of them. If it is impossible to cut the given graph, print "No solution" (without quotes).

/*********************************************************************************
*     File Name           :     E.go
*     Created By          :     YIMMON, yimmon.zhuang@gmail.com
*     Creation Date       :     [2014-03-30 19:42]
*     Last Modified       :     [2014-03-30 20:01]
*     Description         :
**********************************************************************************/

package main

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

type Edge struct {
    to, next, id int
}

var (
    n, m, ep int
    head     []int
    edge     []Edge
    vst      []bool
)

func addedge(a, b, id int) {
    edge[ep].to = b
    edge[ep].next = head[a]
    edge[ep].id = id
    head[a] = ep
    ep++
}

func dfs(x int) int {
    nei := make([]int, 0, 2)
    for i := head[x]; i != -1; i = edge[i].next {
        if !vst[edge[i].id] {
            vst[edge[i].id] = true
            r := dfs(edge[i].to)
            if r == 0 {
                nei = append(nei, edge[i].to)
                if len(nei) == 2 {
                    fmt.Println(nei[0], x, nei[1])
                    nei = nei[:0]
                }
            } else {
                fmt.Println(r, edge[i].to, x)
            }
        }
    }
    if len(nei) != 0 {
        return nei[0]
    }
    return 0
}

func solve() {
    if m&1 == 1 {
        fmt.Println("No solution")
        return
    }
    dfs(1)
}

func main() {
    in.Split(bufio.ScanWords)

    n, m = in.NextInt(), in.NextInt()
    ep = 0
    head = make([]int, n+1)
    edge = make([]Edge, m*2)
    vst = make([]bool, m)
    for i := 0; i <= n; i++ {
        head[i] = -1
    }
    for i := 0; i < m; i++ {
        a, b := in.NextInt(), in.NextInt()
        addedge(a, b, i)
        addedge(b, a, i)
    }
    solve()
}

type Scanner struct {
    *bufio.Scanner
}

var in = Scanner{bufio.NewScanner(os.Stdin)}

func (self *Scanner) Next() string {
    if !self.Scan() {
        panic(self.Err())
    }
    return self.Text()
}

func (self *Scanner) NextInt() int {
    x, e := strconv.Atoi(self.Next())
    if e != nil {
        panic(e)
    }
    return x
}


Let's call an array $$$a_1, a_2, \dots, a_m$$$ of nonnegative integer numbers good if $$$a_1 + a_2 + \dots + a_m = 2\cdot(a_1 \oplus a_2 \oplus \dots \oplus a_m)$$$, where $$$\oplus$$$ denotes the bitwise XOR operation.For example, array $$$[1, 2, 3, 6]$$$ is good, as $$$1 + 2 + 3 + 6 = 12 = 2\cdot 6 = 2\cdot (1\oplus 2 \oplus 3 \oplus 6)$$$. At the same time, array $$$[1, 2, 1, 3]$$$ isn't good, as $$$1 + 2 + 1 + 3 = 7 \neq 2\cdot 1 = 2\cdot(1\oplus 2 \oplus 1 \oplus 3)$$$.You are given an array of length $$$n$$$: $$$a_1, a_2, \dots, a_n$$$. Append at most $$$3$$$ elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n \le 10^5)$$$ — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0\le a_i \le 10^9$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, output two lines. In the first line, output a single integer $$$s$$$ ($$$0\le s\le 3$$$) — the number of elements you want to append. In the second line, output $$$s$$$ integers $$$b_1, \dots, b_s$$$ ($$$0\le b_i \le 10^{18}$$$) — the elements you want to append to the array. If there are different solutions, you are allowed to output any of them.

NoteIn the first test case of the example, the sum of all numbers is $$$12$$$, and their $$$\oplus$$$ is $$$6$$$, so the condition is already satisfied.In the second test case of the example, after adding $$$4, 4$$$, the array becomes $$$[8, 4, 4]$$$. The sum of numbers in it is $$$16$$$, $$$\oplus$$$ of numbers in it is $$$8$$$.

package main

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

type node struct {
	diff int
	time int
}

type ByTime []node

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		sum := int64(0)
		xor := int64(0)
		for i := 0; i < n; i++ {
			tmp := int64(0)
			fmt.Fscanf(in, "%d", &tmp)
			sum += tmp
			xor ^= tmp
		}
		fmt.Fscanf(in, "\n")
		ans := int64(0)
		t1 := sum
		t2 := xor
		if sum%2 == 1 {
			ans = 1
			t1 = sum + 1
			t2 = xor ^ 1
		}
		// a := make([]int, 0)
		// b := make([]int, 0)
		// for sum > 0 {
		// 	a = append(a, sum%2)
		// 	sum /= 2
		// }
		// for xor > 0 {
		// 	b = append(b, xor%2)
		// 	xor /= 2
		// }
		tmp := int64(2)
		for t1 != t2*2 {
			q1 := t1 / 2
			q1 = q1 % 2
			q2 := t2 % 2
			if q1 != q2 {
				ans += tmp
				t1 += 2
				t2 ^= 2
			}
			t1 /= 2
			t2 /= 2
			tmp *= 2
		}
		//fmt.Println("debugans", sum, xor, sum+ans, xor^ans)
		fmt.Fprintf(out, "%d\n", 1)
		fmt.Fprintf(out, "%d\n", ans)
	}
	return
}

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

func same(s string, ss string) bool {
	a := make([]int, 26)
	for i := 0; i < len(s); i++ {
		b := int(s[i] - 'a')
		a[b]++
	}
	for i := 0; i < len(ss); i++ {
		b := int(ss[i] - 'a')
		a[b]--
		if a[b] == -1 {
			return false
		}
	}
	for i := 0; i < 26; i++ {
		if a[i] != 0 {
			return false
		}
	}
	return true
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.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() {
	defer writer.Flush()

	var t, n int
	for scanf("%d\n", &t); t > 0; t-- {
		scanf("%d\n", &n)
		tmp, xor, sum := int64(0), int64(0), int64(0)
		for i := 0; i < n; i++ {
			scanf("%d", &tmp)
			xor ^= tmp
			sum += tmp
		}
		scanf("\n")
		printf("2\n%d %d\n", xor, sum+xor)
	}
}

package main

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

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

	t := readInt(reader)

	for _i := uint64(0); _i < t; _i++ {
		n := readInt(reader)
		arr := make([]uint64, n)
		line := strings.Split(readLine(reader), " ")
		for i := range arr {
			arr[i] = parseInt(line[i])
		}

		v := uint64(0)
		s := uint64(0)
		for _, x := range arr {
			v = v ^ x
			s += x
		}

		s += v

		fmt.Println(2)
		fmt.Println(v, s)
	}
}

func readInt(reader *bufio.Reader) uint64 {
	return parseInt(readLine(reader))
}

func parseInt(s string) uint64 {
	v, err := strconv.ParseInt(s, 10, 64)
	checkError(err)
	return uint64(v)
}

func readLine(reader *bufio.Reader) string {
	str, err := reader.ReadString('\n')
	if err == io.EOF {
		return ""
	}

	return strings.TrimSpace(str)
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

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

package main

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

type heap struct {
  array []int
}

func (h *heap) push(val int) {
  h.array = append(h.array, val)
  cur := len(h.array) - 1
  for cur > 0 {
    var parent int
    if (cur & 1) == 1 {
      parent = (cur - 1) / 2
    } else {
      parent = (cur - 2) / 2
    }
    if h.array[cur] < h.array[parent] {
      h.array[cur], h.array[parent] = h.array[parent], h.array[cur]
      cur = parent
    } else {
      break
    }
  }
}

func (h *heap) pop() int {
  res := h.array[0]
  h.array[0], h.array[len(h.array)-1] = h.array[len(h.array)-1], h.array[0]
  h.array = h.array[:len(h.array)-1]
  cur := 0
  for cur < len(h.array) {
    smallest := cur
    if cur*2+1 < len(h.array) && h.array[smallest] > h.array[cur*2+1] {
      smallest = cur*2 + 1
    }
    if cur*2+2 < len(h.array) && h.array[smallest] > h.array[cur*2+2] {
      smallest = cur*2 + 2
    }
    if smallest != cur {
      h.array[smallest], h.array[cur] = h.array[cur], h.array[smallest]
      cur = smallest
    } else {
      break
    }
  }
  return res
}

func main() {
  in := bufio.NewReader(os.Stdin)
  out := bufio.NewWriter(os.Stdout)
  defer out.Flush()
  var T int
  Fscan(in, &T)
  for T > 0 {
    T--
    var n int
    var temp int64
    Fscan(in, &n)
    var add, xor int64 = 0, 0
    for i := 0; i < n; i++ {
      Fscan(in, &temp)
      add += temp
      xor ^= temp
    }
    var var0, var1, var2 int64 = 0, 0, 0
    if (add & 1) == 1 {
      var0 = 1
      add += 1
      xor ^= 1
    }
    for i := int64(2); add > 2*xor || 2*xor-add < 2; i <<= 1 {
      if (xor & i) == 0 {
        xor |= i
        var0 |= i
        add += i
      }
    }
    var1, var2 = (2*xor-add)/2, (2*xor-add)/2
    Fprintln(out, 3)
    Fprintln(out, var0, var1, var2)
  }
}

package main

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

type node struct {
	diff int
	time int
}

type ByTime []node

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		sum := int64(0)
		xor := int64(0)
		for i := 0; i < n; i++ {
			tmp := int64(0)
			fmt.Fscanf(in, "%d", &tmp)
			sum += tmp
			xor ^= tmp
		}
		fmt.Fscanf(in, "\n")
		ans := 0
		t1 := sum
		t2 := xor
		if sum%2 == 1 {
			ans = 1
			t1 = sum + 1
			t2 = xor ^ 1
		}
		// a := make([]int, 0)
		// b := make([]int, 0)
		// for sum > 0 {
		// 	a = append(a, sum%2)
		// 	sum /= 2
		// }
		// for xor > 0 {
		// 	b = append(b, xor%2)
		// 	xor /= 2
		// }
		tmp := 2
		for t1 != t2*2 {
			q1 := t1 / 2
			q1 = q1 % 2
			q2 := t2 % 2
			if q1 != q2 {
				ans += tmp
				t1 += 2
				t2 ^= 2
			}
			t1 /= 2
			t2 /= 2
			tmp *= 2
		}
		//fmt.Println("debugans", sum, xor, sum+ans, xor^ans)
		fmt.Fprintf(out, "%d\n", 1)
		fmt.Fprintf(out, "%d\n", ans)
	}
	return
}

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

func same(s string, ss string) bool {
	a := make([]int, 26)
	for i := 0; i < len(s); i++ {
		b := int(s[i] - 'a')
		a[b]++
	}
	for i := 0; i < len(ss); i++ {
		b := int(ss[i] - 'a')
		a[b]--
		if a[b] == -1 {
			return false
		}
	}
	for i := 0; i < 26; i++ {
		if a[i] != 0 {
			return false
		}
	}
	return true
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.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() {
	defer writer.Flush()

	var t, n int
	for scanf("%d\n", &t); t > 0; t-- {
		scanf("%d\n", &n)
		tmp, xor, sum := 0, 0, 0
		for i := 0; i < n; i++ {
			scanf("%d", &tmp)
			xor ^= tmp
			sum += tmp
		}
		printf("2\n%d %d\n", xor, sum+xor)
	}
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.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() {
	defer writer.Flush()

	var t, n int
	for scanf("%d\n", &t); t > 0; t-- {
		scanf("%d\n", &n)
		tmp, xor, sum := 0, 0, 0
		for i := 0; i < n; i++ {
			scanf("%d", &tmp)
			xor ^= tmp
			sum += tmp
		}
		scanf("\n")
		printf("2\n%d %d\n", xor, sum+xor)
	}
}

package main

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

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

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

	t := readInt(reader)

	for _i := 0; _i < t; _i++ {
		n := readInt(reader)
		arr := make([]int, n)
		line := strings.Split(readLine(reader), " ")
		for i := range arr {
			arr[i] = parseInt(line[i])
		}

		v := 0
		s := 0
		for _, x := range arr {
			v = v ^ x
			s += x
		}

		s += v

		fmt.Println(2)
		fmt.Println(v, s)
	}
}

func readInt(reader *bufio.Reader) int {
	return parseInt(readLine(reader))
}

func parseInt(s string) int {
	v, err := strconv.ParseInt(s, 10, 64)
	checkError(err)
	return int(v)
}

func readLine(reader *bufio.Reader) string {
	str, err := reader.ReadString('\n')
	if err == io.EOF {
		return ""
	}

	return strings.TrimSpace(str)
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

package main

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

type heap struct {
  array []int
}

func (h *heap) push(val int) {
  h.array = append(h.array, val)
  cur := len(h.array) - 1
  for cur > 0 {
    var parent int
    if (cur & 1) == 1 {
      parent = (cur - 1) / 2
    } else {
      parent = (cur - 2) / 2
    }
    if h.array[cur] < h.array[parent] {
      h.array[cur], h.array[parent] = h.array[parent], h.array[cur]
      cur = parent
    } else {
      break
    }
  }
}

func (h *heap) pop() int {
  res := h.array[0]
  h.array[0], h.array[len(h.array)-1] = h.array[len(h.array)-1], h.array[0]
  h.array = h.array[:len(h.array)-1]
  cur := 0
  for cur < len(h.array) {
    smallest := cur
    if cur*2+1 < len(h.array) && h.array[smallest] > h.array[cur*2+1] {
      smallest = cur*2 + 1
    }
    if cur*2+2 < len(h.array) && h.array[smallest] > h.array[cur*2+2] {
      smallest = cur*2 + 2
    }
    if smallest != cur {
      h.array[smallest], h.array[cur] = h.array[cur], h.array[smallest]
      cur = smallest
    } else {
      break
    }
  }
  return res
}

func main() {
  in := bufio.NewReader(os.Stdin)
  out := bufio.NewWriter(os.Stdout)
  defer out.Flush()
  var T int
  Fscan(in, &T)
  for T > 0 {
    T--
    var n int
    var temp int64
    Fscan(in, &n)
    var add, xor int64 = 0, 0
    for i := 0; i < n; i++ {
      Fscan(in, &temp)
      add += temp
      xor ^= temp
    }
    var var0, var1, var2 int64 = 0, 0, 0
    if (add & 1) == 1 {
      var0 = 1
      add ^= 1
      xor ^= 1
    }
    for i := int64(2); add > 2*xor; i <<= 1 {
      if (xor & i) == 0 {
        xor |= i
        var0 |= i
        add += i
      }
    }
    var1, var2 = (2*xor-add)/2, (2*xor-add)/2
    Fprintln(out, 3)
    Fprintln(out, var0, var1, var2)
  }
}

package main

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

type heap struct {
  array []int
}

func (h *heap) push(val int) {
  h.array = append(h.array, val)
  cur := len(h.array) - 1
  for cur > 0 {
    var parent int
    if (cur & 1) == 1 {
      parent = (cur - 1) / 2
    } else {
      parent = (cur - 2) / 2
    }
    if h.array[cur] < h.array[parent] {
      h.array[cur], h.array[parent] = h.array[parent], h.array[cur]
      cur = parent
    } else {
      break
    }
  }
}

func (h *heap) pop() int {
  res := h.array[0]
  h.array[0], h.array[len(h.array)-1] = h.array[len(h.array)-1], h.array[0]
  h.array = h.array[:len(h.array)-1]
  cur := 0
  for cur < len(h.array) {
    smallest := cur
    if cur*2+1 < len(h.array) && h.array[smallest] > h.array[cur*2+1] {
      smallest = cur*2 + 1
    }
    if cur*2+2 < len(h.array) && h.array[smallest] > h.array[cur*2+2] {
      smallest = cur*2 + 2
    }
    if smallest != cur {
      h.array[smallest], h.array[cur] = h.array[cur], h.array[smallest]
      cur = smallest
    } else {
      break
    }
  }
  return res
}

func main() {
  in := bufio.NewReader(os.Stdin)
  out := bufio.NewWriter(os.Stdout)
  defer out.Flush()
  var T int
  Fscan(in, &T)
  for T > 0 {
    T--
    var n int
    var temp int64
    Fscan(in, &n)
    var add, xor int64 = 0, 0
    for i := 0; i < n; i++ {
      Fscan(in, &temp)
      add += temp
      xor ^= temp
    }
    var var0, var1, var2 int64 = 0, 0, 0
    if (add & 1) == 1 {
      var0 = 1
      add ^= 1
      xor ^= 1
    }
    for i := int64(2); add > 2*xor || 2*xor-add < 2; i <<= 1 {
      if (xor & i) == 0 {
        xor |= i
        var0 |= i
        add += i
      }
    }
    var1, var2 = (2*xor-add)/2, (2*xor-add)/2
    Fprintln(out, 3)
    Fprintln(out, var0, var1, var2)
  }
}


You are given a rooted tree, consisting of $$$n$$$ vertices. The vertices are numbered from $$$1$$$ to $$$n$$$, the root is the vertex $$$1$$$.You can perform the following operation at most $$$k$$$ times:   choose an edge $$$(v, u)$$$ of the tree such that $$$v$$$ is a parent of $$$u$$$;  remove the edge $$$(v, u)$$$;  add an edge $$$(1, u)$$$ (i. e. make $$$u$$$ with its subtree a child of the root). The height of a tree is the maximum depth of its vertices, and the depth of a vertex is the number of edges on the path from the root to it. For example, the depth of vertex $$$1$$$ is $$$0$$$, since it's the root, and the depth of all its children is $$$1$$$.What's the smallest height of the tree that can be achieved?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 2 \cdot 10^5$$$; $$$0 \le k \le n - 1$$$) — the number of vertices in the tree and the maximum number of operations you can perform. The second line contains $$$n-1$$$ integers $$$p_2, p_3, \dots, p_n$$$ ($$$1 \le p_i < i$$$) — the parent of the $$$i$$$-th vertex. Vertex $$$1$$$ is the root. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$.

For each testcase, print a single integer — the smallest height of the tree that can achieved by performing at most $$$k$$$ operations.

package main

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

func stats(parents []int) ([]int ,[]int) {
	depths := make([]int, len(parents))

	var getDepth func(n int) int

	getDepth = func(n int) int {
		if n == 0 {
			return 0
		}

		if depths[n] == 0 {
			depths[n] = 1 + getDepth(parents[n])
		}

		return depths[n]
	}

	for n := range parents {
		depths[n] = getDepth(n)
	}

	// fmt.Println("Depths", depths)

	groups := make([][]int, len(parents))

	for n, d := range depths {
		groups[d] = append(groups[d], n)
	}

	sortedByDepth := make([]int, 0, len(parents))

	for d := len(groups) - 1; d >= 0; d-- {
		sortedByDepth = append(sortedByDepth, groups[d]...)
	}

	// fmt.Println("Sorted by depths", sortedByDepth)
	return depths, sortedByDepth
}

func cut(parents, depths, sortedByDepth []int, h int) int {
	seen := make([]bool, len(parents))

	makeCut := func(n int) bool {
		seen[n] = true

		for i := 0; i < h - 1; i++ {
			n = parents[n]

			if seen[n] {
				return false
			}

			seen[n] = true
		}

		return true
	}

	count := 0

	for _, n := range sortedByDepth {
		if seen[n] {
			continue
		}

		d := depths[n]

		if d > h && makeCut(n) {
			count++
		}
	}

	return count
}

func solve(parents []int, k int) int {
	depths, sortedByDepth := stats(parents)

	f := func(h int) int {
		return cut(parents, depths, sortedByDepth, h)
	}

	l, r := 1, len(parents) - 1

	fl := f(l)

	if fl <= k {
		return l
	}

	for l + 1 < r {
		m := (l + r) / 2

		if f(m) > k {
			l = m
		} else {
			r = m
		}
	}

	return r
}

func main() {
	in := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		var n, k int
		fmt.Fscan(in, &n, &k)

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

		fmt.Println(solve(parents, k))
	}
}


You are given two strings $$$s$$$ and $$$t$$$, both consisting of exactly $$$k$$$ lowercase Latin letters, $$$s$$$ is lexicographically less than $$$t$$$.Let's consider list of all strings consisting of exactly $$$k$$$ lowercase Latin letters, lexicographically not less than $$$s$$$ and not greater than $$$t$$$ (including $$$s$$$ and $$$t$$$) in lexicographical order. For example, for $$$k=2$$$, $$$s=$$$"az" and $$$t=$$$"bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"].Your task is to print the median (the middle element) of this list. For the example above this will be "bc".It is guaranteed that there is an odd number of strings lexicographically not less than $$$s$$$ and not greater than $$$t$$$.

The first line of the input contains one integer $$$k$$$ ($$$1 \le k \le 2 \cdot 10^5$$$) — the length of strings. The second line of the input contains one string $$$s$$$ consisting of exactly $$$k$$$ lowercase Latin letters. The third line of the input contains one string $$$t$$$ consisting of exactly $$$k$$$ lowercase Latin letters. It is guaranteed that $$$s$$$ is lexicographically less than $$$t$$$. It is guaranteed that there is an odd number of strings lexicographically not less than $$$s$$$ and not greater than $$$t$$$.

Print one string consisting exactly of $$$k$$$ lowercase Latin letters — the median (the middle element) of list of strings of length $$$k$$$ lexicographically not less than $$$s$$$ and not greater than $$$t$$$.

package main

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

func main() {
	//	fmt.Printf("%d\n",(((199999*(200000))%p)*199999)%p)
	buf := bufio.NewReader(os.Stdin)
	var n int

	fmt.Fscanf(buf, "%d\n", &n)
	str1 := make([]byte, n+1)
	str2 := make([]byte, n+1)
	mat := make([]int, n+2)
	fmt.Fscanf(buf, "%s\n", &str1)
	fmt.Fscanf(buf, "%s\n", &str2)
	st := 2 * int('a')
	sum := 0
	//fmt.Printf("%v\n", str1)
	//fmt.Printf("%v\n", str2)
	//fmt.Printf("st=%d\n", st)
	for i := 0; i < n; i++ {
		sum += int(str1[n-i-1]) + int(str2[n-i-1]) - st
		mat[i] = sum % 26
		sum /= 26
	}
	if sum > 0{
		mat[n] = sum
	}
	//fmt.Printf("%v\n", mat)
	sum = mat[n]
	len := 0
	for i := n - 1; i >= 0; i-- {
		sum = 26*sum + mat[i]
		str1[len] = 'a' + (byte)(sum/2);
		len++
		sum %= 2
	}

	//str1[len]=0
	//	fmt.Printf("ans1=%d\n",ans)

	fmt.Printf("%s\n", str1)

}

package main

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

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

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

func getLine() string {
	s, _ := reader.ReadString('\n')
	if strings.HasSuffix(s, "\r\n") {
		s = s[:len(s)-2]
	} else if strings.HasSuffix(s, "\n") {
		s = s[:len(s)-1]
	}
	return s
}
func getLine2Int() (n int) {
	scanf("%d\n", &n)
	return
}
func getLine2Int2() (a, b int) {
	scanf("%d %d\n", &a, &b)
	return
}
func getLine2Ints(n int) []int {
	ans := make([]int, n)
	for i := 0; i < n; i++ {
		scanf("%d ", &ans[i])
	}
	scanf("%d\n", &ans[n-1])
	return ans
}

var debug = false

func see(a interface{}) {
	if debug {
		printf("DEBUG:%v\n", a)
	}
}

func main() {
	defer writer.Flush()

	n := getLine2Int()
	input := getLine()
	input2 := getLine()
	ans := make([]byte, n)
	lastAvg := int((input[n-1]+input2[n-1])/2 - 'a')
	for i := n - 2; i >= 0; i-- {
		lastAvg += int(input[i]+input2[i]-2*'a') * 13
		ans[i+1] = byte(lastAvg%26) + 'a'
		lastAvg /= 26
	}
	ans[0] = byte(lastAvg) + 'a'
	printf("%s\n", string(ans))
}

package main

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

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

	var n int
	var s, t []byte
	Fscan(in, &n, &s, &t)
	m := make([]byte, n)
	m[n-1] = (s[n-1] + t[n-1]) / 2
	for i := n - 1; i >= 0; i-- {
		m[i] = (s[i] + t[i]) / 2
		if s[i]&1 != t[i]&1 {
			m[i+1] += 13
			if m[i+1] > 'z' {
				m[i+1] -= 26
				m[i]++
			}
		}
	}
	Fprint(out, string(m))
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1144E(_r io.Reader, _w io.Writer) {
	toInts := func(s []byte) []int {
		ints := make([]int, len(s))
		for i, b := range s {
			ints[i] = int(b)
		}
		return ints
	}
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n int
	var _s, _t []byte
	Fscan(in, &n, &_s, &_t)
	s, t := toInts(_s), toInts(_t)
	ans := make([]byte, n)
	for i := range s[:n-1] {
		v1 := (s[i]-'a')*26 + s[i+1] - 'a'
		v2 := (t[i]-'a')*26 + t[i+1] - 'a'
		m := (v1+v2)/52 + 'a'
		if 2*m > s[i]+t[i] {
			s[i+1] -= 26
		} else if 2*m < s[i]+t[i] {
			t[i+1] += 26
		}
		ans[i] = byte(m)
	}
	ans[n-1] = byte((s[n-1] + t[n-1]) / 2)
	for i := n - 1; i > 0; i-- {
		if ans[i] > 'z' {
			ans[i] -= 26
			ans[i-1]++
		}
	}
	Fprint(out, string(ans))
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)

	var k int
	fmt.Fscanln(in, &k)

	s, t := make([]rune, k), make([]rune, k)
	for i := range s {
		t[i], _, _ = in.ReadRune()
	}
	in.ReadLine()
	for i := range t {
		s[i], _, _ = in.ReadRune()
	}

	result := make([]rune, k)
	result[k - 1] = (s[k - 1] + t[k - 1]) / 2
	for i := k - 2; i >= 0; i-- {
		result[i] = (s[i] + t[i]) / 2
		if s[i] % 2 != t[i] % 2 {
			result[i + 1] += 13
			if result[i + 1] > 'z' {
				result[i + 1] -= 26
				result[i]++
			}
		}
	}

	fmt.Println(string(result))
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
func main()  {
	in := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanln(in, &n)

	one := make([]byte, n, n)
	two := make([]byte, n, n)

	fmt.Fscanln(in, &one)
	fmt.Fscanln(in, &two)

	tmp := make([]byte, n, n)
	bit := byte(0)
	// 输入 进位
	for i:= n - 1; i >= 0; i -- {
		tmp[i] = one[i] - 'a' + two[i] - 'a'
		if bit != 0 {
			tmp[i] = tmp[i] + bit
		}
		if tmp[i] >= 26 && i != 0{
			tmp[i] = tmp[i] - 26
			bit = 1
		} else {
			bit = 0
		}
	}
	// 模拟除法
	bit = byte(0)
	for i := 0 ; i < len(tmp); i ++ {
		tmp[i] = tmp[i] + bit
		if tmp[i] & 1  == 0 {
			tmp[i] = tmp [i] >> 1
			bit = 0
		} else {
			tmp[i] = tmp [i] >> 1
			bit = 26
		}
	}
	for i := 0; i < n; i ++ {
		tmp[i] = tmp[i] + 'a'
	}
	fmt.Printf("%s\n", tmp)
}

package main

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

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

	var n int
	var s, t []byte
	Fscan(in, &n, &s, &t)
	ans := make([]byte, n)
	for i := range s[:n-1] {
		v1 := int(s[i]-'a')*26 + int(s[i+1]-'a')
		v2 := int(t[i]-'a')*26 + int(t[i+1]-'a')
		ans[i] = byte((v1+v2)/52) + 'a'
		if 2*ans[i] > s[i]+t[i] {
			s[i+1] -= 26
		} else if 2*ans[i] < s[i]+t[i] {
			t[i+1] += 26
		}
	}
	ans[n-1] = byte((int(s[n-1]) + int(t[n-1])) / 2)
	Fprint(out, string(ans))
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1144E(_r io.Reader, _w io.Writer) {
	toInts := func(s []byte) []int {
		ints := make([]int, len(s))
		for i, b := range s {
			ints[i] = int(b)
		}
		return ints
	}
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n int
	var _s, _t []byte
	Fscan(in, &n, &_s, &_t)
	s, t := toInts(_s), toInts(_t)
	ans := make([]byte, n)
	for i := range s[:n-1] {
		v1 := (s[i]-'a')*26 + s[i+1] - 'a'
		v2 := (t[i]-'a')*26 + t[i+1] - 'a'
		m := (v1+v2)/52 + 'a'
		if 2*m > s[i]+t[i] {
			s[i+1] -= 26
		} else if 2*m < s[i]+t[i] {
			t[i+1] += 26
		}
		ans[i] = byte(m)
	}
	ans[n-1] = byte((s[n-1] + t[n-1]) / 2)
	Fprint(out, string(ans))
}

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

package main

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

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

	var n int
	var s, t []byte
	Fscan(in, &n, &s, &t)
	ans := make([]byte, n)
	for i := range s[:n-1] {
		v1 := int(s[i]-'a')*26 + int(s[i+1]-'a')
		v2 := int(t[i]-'a')*26 + int(t[i+1]-'a')
		ans[i] = byte((v1+v2)/52) + 'a'
		if 2*ans[i] > s[i]+t[i] {
			s[i+1] -= 26
		} else if 2*ans[i] < s[i]+t[i] {
			t[i+1] += 26
		}
	}
	ans[n-1] = (s[n-1] + t[n-1]) / 2
	Fprint(out, string(ans))
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1144E(_r io.Reader, _w io.Writer) {
	toInts := func(s []byte) []int {
		ints := make([]int, len(s))
		for i, b := range s {
			ints[i] = int(b)
		}
		return ints
	}
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n int
	var _s, _t []byte
	Fscan(in, &n, &_s, &_t)
	s, t := toInts(_s), toInts(_t)
	ans := make([]byte, n)
	for i := range s[:n-1] {
		v1 := (s[i]-'a')*26 + s[i+1] - 'a'
		v2 := (t[i]-'a')*26 + t[i+1] - 'a'
		m := (v1+v2)/52 + 'a'
		if 2*m > s[i]+t[i] {
			s[i+1] -= 26
		} else if 2*m < s[i]+t[i] {
			t[i+1] += 26
		}
		if m > 'z' {
			m -= 26
			ans[i-1]++
		}
		ans[i] = byte(m)
	}
	ans[n-1] = byte((s[n-1] + t[n-1]) / 2)
	if ans[n-1] > 'z' {
		ans[n-1] -= 26
		ans[n-2]++
	}
	Fprint(out, string(ans))
}

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