Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$.Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted.Now consider the arrays $$$B_1, B_2,\ldots, B_n$$$ generated by $$$f(1,A), f(2,A),\ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,\ldots, B_n$$$.For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$.You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$)  — the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$0 \leq c_i \leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output a single line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$). If there are multiple answers, you may output any of them.

NoteHere's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$:   $$$B_1=[\color{blue}{1},1,0,1]$$$;  $$$B_2=[\color{blue}{1},\color{blue}{1},0,1]$$$;  $$$B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$$$;  $$$B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$$$  And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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

func _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func _string() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

type SegmentTree struct {
	val  int64
	lazy int64
	from int64
	to   int64
	// each node has either 2 children (from != to) or no children (from == to)
	lchild *SegmentTree
	rchild *SegmentTree
}

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

func BuildSegmentTree(a []int64) *SegmentTree {
	var build func(int64, int64) *SegmentTree
	build = func(from, to int64) *SegmentTree {
		var lchild, rchild *SegmentTree
		val := a[from]
		if from != to {
			mid := (from + to) / 2
			lchild = build(from, mid)
			rchild = build(mid+1, to)
			val = max(val, max(lchild.val, rchild.val))
		}
		return &SegmentTree{
			val:    val,
			lazy:   0,
			from:   from,
			to:     to,
			lchild: lchild,
			rchild: rchild,
		}
	}
	return build(0, int64(len(a)-1))
}

func (st *SegmentTree) overlap(l, r int64) int64 {
	if l <= st.from && st.to <= r { // total overlap
		return 1
	}
	if st.to < l || r < st.from { // no overlap
		return -1
	}
	return 0
}

func (st *SegmentTree) Query(l, r int64) int64 {
	ret, _ := st.query(l, r)
	return ret
}

func (st *SegmentTree) query(l, r int64) (int64, bool) {
	st.lazyProp()
	switch st.overlap(l, r) {
	case 1:
		return st.val, true
	case -1:
		return 0, false
	}
	lval, lfound := st.lchild.query(l, r)
	rval, rfound := st.rchild.query(l, r)
	if !lfound {
		return rval, rfound
	}
	if !rfound {
		return lval, lfound
	}
	return max(lval, rval), true
}

func (st *SegmentTree) RangeAdd(l, r int64, v int64) {
	st.lazyProp()
	switch st.overlap(l, r) {
	case 1:
		st.lazy += v
		st.lazyProp()
		return
	case -1:
		return
	}
	if st.from == st.to {
		return
	}
	st.lchild.RangeAdd(l, r, v)
	st.rchild.RangeAdd(l, r, v)
	st.val = max(st.lchild.val, st.rchild.val)
}

func (st *SegmentTree) lazyProp() {
	st.val += st.lazy
	if st.from != st.to {
		st.lchild.lazy += st.lazy
		st.rchild.lazy += st.lazy
	}
	st.lazy = 0
}
func main() {
	defer writer.Flush()
	for t := _int(); t > 0; t-- {
		n := _int64()
		c := make([]int64, n)
		for i := range c {
			c[i] = _int64()
		}
		res := sol(n, c)
		for _, d := range res {
			printf("%d ", d)
		}
		printf("\n")
	}
}

func sol(n int64, c []int64) []int64 {
	res := make([]int64, n)
	btm := BuildSegmentTree(make([]int64, n))
	sum := int64(0)
	for _, d := range c {
		sum += d
	}
	if sum%n != 0 {
		panic("no answer 1")
	}
	c1 := sum / n
	for i := n - 1; i >= 0; i-- {
		btm.RangeAdd(i-c1+1, i, 1)
		q := btm.Query(i, i)
		if c[i] == q { // right column zeroes
			continue
		}
		if c[i] == q+i { // right column ones
			res[i] = 1
			c1--
			continue
		}
		panic("no answer 2")
	}

	// this is a stupid hack to account for the first column
	if c1 != 0 {
		if c1 != 1 {
			panic("no answer 3")
		}
		res[0] = 1
		c1--
	}
	return res
}

package main

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

var rw = NewFastIO(os.Stdin, os.Stdout)

func solve() {
	n := rw.NextInt()
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = rw.NextInt()
	}
	ans := make([]int, n)

	for i := 0; i < n; i++ {
		ans[i] = 1
	}

	if v[0] < n {
		ans[v[0]] = 1
	}

	for i := 0; i < n; i++ {
		rem := v[i] - int(ans[i]*i)
		if rem < 0 {
			rem = 0
		}
		if rem >= 0 && i+rem < n {
			ans[i+rem] = 0
		}

	}

	for _, x := range ans {
		rw.Print(x, " ")
	}
	rw.Print("\n")
}

func main() {
	defer rw.Flush()
	tc := 1
	tc = rw.NextInt()
	for i := tc; i > 0; i-- {
		solve()
	}
}

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

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

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

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

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

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

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

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

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

const (
	INF = 3e5 + 10
	L0  = int64(0)
	L1  = int64(1)
)

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

	var t int
	Fscan(in, &t)

	for k := 0; k < t; k++ {
		var n int
		Fscan(in, &n)

		ans := make([]int, n+1)
		a := make([]int, n+1)
		d := make([]int, n+1)
		sum := L0
		for i := 1; i <= n; i++ {
			Fscan(in, &a[i])
			sum += int64(a[i])
		}

		sum /= int64(n)
		tmp := 0

		for i := n; i > 0; i-- {
			a[i] -= tmp
			if sum > 0 {
				d[i-int(sum)+1] += 1
				tmp += 1
				tmp -= d[i]
			} else {
				break
			}
			if a[i] == i {
				ans[i] = 1
				sum -= 1
			}
			if a[i] == 0 {
				break
			}

		}

		for i := 1; i <= n; i++ {
			Fprint(out, ans[i], " ")
		}
		Fprintln(out, "")

	}

}

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

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

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

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

		for i, v := range arr {
			if v == 0 {
				m[i] = 0
			} else {
				var idx int
				if m[i] == 1 {
					idx = v
				} else {
					idx = v + i
				}

				if idx < n {
					m[idx] = 0
				}
			}
		}

		for _, v := range m {
			fmt.Fprintf(out, "%d ", v)
		}
		fmt.Fprintln(out)
	}
}

package main

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

var rw = NewFastIO(os.Stdin, os.Stdout)

func solve() {
	n := rw.NextInt()
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = rw.NextInt()
	}
	ans := make([]int, n)

	for i := 0; i < n; i++ {
		ans[i] = 1
	}

	if v[0] < n {
		ans[v[0]] = 1
	}

	for i := 0; i < n; i++ {
		rem := v[i] - int(ans[i]*i)

		if rem >= 0 && i+rem < n {
			ans[i+rem] = 0
		}

	}

	for _, x := range ans {
		rw.Print(x, " ")
	}
	rw.Print("\n")
}

func main() {
	defer rw.Flush()
	tc := 1
	tc = rw.NextInt()
	for i := tc; i > 0; i-- {
		solve()
	}
}

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

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

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

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

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

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

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

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

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

var rw = NewFastIO(os.Stdin, os.Stdout)

func solve() {
	n := rw.NextInt()
	v := make([]int, n)
	sum := 0
	for i := 0; i < n; i++ {
		v[i] = rw.NextInt()
		sum += v[i]
	}
	ans, b := make([]int, n), make([]int, n)
	lf := n - sum/n
	for i := lf; i < n; i++ {
		b[i] = n - 1
	}

	for i := n - 1; i >= 0 && lf <= i; i-- {
		curr := v[i] - (b[i] - i)
		if curr == i+1 {
			ans[i] = 1
		} else if curr == 1 {
			ans[i] = 0
			lf--
			b[lf] = i - 1
		}
	}

	for _, x := range ans {
		rw.Print(x, " ")
	}
	rw.Print("\n")
}

func main() {
	defer rw.Flush()
	tc := 1
	tc = rw.NextInt()
	for i := tc; i > 0; i-- {
		solve()
	}
}

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

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

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

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

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

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

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

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

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

var rw = NewFastIO(os.Stdin, os.Stdout)

func solve() {
	n := rw.NextInt()
	v := make([]int, n)
	sum := 0
	for i := 0; i < n; i++ {
		v[i] = rw.NextInt()
		sum += v[i]
	}
	ans, b := make([]int, n), make([]int, n)
	lf := n - sum/n
	for i := lf; i < n; i++ {
		b[i] = n - 1
	}

	for i := n - 1; i < n && lf <= i; i-- {
		curr := v[i] - (b[i] - i)
		if curr == i+1 {
			ans[i] = 1
		} else if curr == 1 {
			ans[i] = 0
			lf--
			b[lf] = i - 1
		}
	}

	for _, x := range ans {
		rw.Print(x, " ")
	}
	rw.Print("\n")
}

func main() {
	defer rw.Flush()
	tc := 1
	tc = rw.NextInt()
	for i := tc; i > 0; i-- {
		solve()
	}
}

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

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

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

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

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

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

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

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

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

const (
	INF = 3e5 + 10
	L0  = int64(0)
	L1  = int64(1)
)

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

	var t int
	Fscan(in, &t)

	for k := 0; k < t; k++ {
		var n int
		Fscan(in, &n)

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

		for i := 1; i <= n; i++ {
			Fprint(out, ans[i], " ")
		}
		Fprintln(out, "")

	}

}

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

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


Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres.In the bookshop, Jack decides to buy two books of different genres.Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book.The books are given by indices of their genres. The genres are numbered from 1 to m.

The first line contains two positive integers n and m (2 ≤ n ≤ 2·105, 2 ≤ m ≤ 10) — the number of books in the bookstore and the number of genres. The second line contains a sequence a1, a2, ..., an, where ai (1 ≤ ai ≤ m) equals the genre of the i-th book. It is guaranteed that for each genre there is at least one book of that genre.

Print the only integer — the number of ways in which Jack can choose books. It is guaranteed that the answer doesn't exceed the value 2·109.

NoteThe answer to the first test sample equals 5 as Sasha can choose:  the first and second books,  the first and third books,  the first and fourth books,  the second and third books,  the third and fourth books.

package main

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

func main() {
	var bookNo, genreNo int
	fmt.Scan(&bookNo)
	fmt.Scan(&genreNo)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	books := make([]int, genreNo)
	for scanner.Scan() {
		text := scanner.Text()
		i, _ := strconv.Atoi(text)
		books[i-1]++
	}

	var way int64
	for i := 0; i < genreNo; i++ {
		for j := i + 1; j < genreNo; j++ {
			way += int64(books[i]) * int64(books[j])
		}
	}

	fmt.Println(way)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, int(math.Pow(10, 8)))

	scanner.Scan()
	nmStr := scanner.Text()
	nmArr := strings.Split(nmStr, " ")
	n, _ := strconv.Atoi(nmArr[0])
	m, _ := strconv.Atoi(nmArr[1])

	scanner.Scan()
	bookGenreStr := scanner.Text()
	bookGenreStrArr := strings.Split(bookGenreStr, " ")
	bookGenreArr := make([]int, len(bookGenreStrArr))
	for idx := range bookGenreStrArr {
		bookGenreArr[idx], _ = strconv.Atoi(bookGenreStrArr[idx])
	}

	booksPerGenre := make([]int, m)
	for idx := 0; idx < n; idx++ {
		booksPerGenre[bookGenreArr[idx]-1] += 1
	}

	//fmt.Println(booksPerGenre)
	curr := 0
	resp := 0
	for idx := 0; idx < m-1; idx++ {
		curr += booksPerGenre[idx]
		resp += booksPerGenre[idx] * (n - curr)
	}

	fmt.Println(resp)
}

package main

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

//N _
const (
	N = 2*1e5 + 5
)

func inputter(inputReader *bufio.Reader,strs chan *string){
	for {
		input , _ := inputReader.ReadString('\n')
		input = strings.TrimSpace(input)
		if len(input) > 0 {
			inputs := strings.Split(input, " ")
			for i := range inputs{
				strs <- &inputs[i]
			}
		}
	}
}

func main(){
	rs := bufio.NewReader(os.Stdin)
	ws := bufio.NewWriter(os.Stdout)

	strs := make(chan *string, 100)

	go inputter(rs, strs)

	n , _ := strconv.ParseInt(*<-strs, 10, 64)
	m , _ := strconv.ParseInt(*<-strs, 10, 64)

	a := make([]int, m)

	for i := 0; i < int(n); i++ {
		x, _ := strconv.ParseInt(*<-strs, 10, 32)
		a[int(x) - 1]++
	}
	tot := 0
	for i := 0; i < int(m); i++ {
		for j := i + 1; j < int(m); j++ {
			tot += a[i] * a[j]
		}
	}

	ws.WriteString(fmt.Sprint(tot))

	ws.Flush()
}

package main

import "fmt"

func main() {
  var n, m int
  var aa []int
  fmt.Scanf("%d %d\n", &n, &m)
  aa=make([]int, m)
  var tmp int
  for i:=0;i<n;i++ {
    fmt.Scanf("%d", &tmp)
    aa[tmp-1]++
  }
  var r int = 0
  var left int = n
  for i:=0;i<m;i++ {
    r += aa[i]*(left-aa[i])
    left -= aa[i]
  }
  fmt.Println(r)
}

package main

import "fmt"

func main() {
  var n, m int
  var aa []int
  fmt.Scanf("%d %d\n", &n, &m)
  aa=make([]int, m)
  for i:=0;i<n;i++ {
    var tmp int
    fmt.Scanf("%d", &tmp)
    aa[tmp-1]++
  }
  var r int = 0
  var left int = n
  for i:=0;i<m;i++ {
    r += aa[i]*(left-aa[i])
    left -= aa[i]
  }
  fmt.Println(r)
}

package main

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

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

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

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

	for i := 0; i < n; i++ {
		a[nextInt()-1]++
	}

	book := 0

	for i := 0; i < m-1; i++ {
		for j := i + 1; j < m; j++ {
			book += a[i] * a[j]
		}
	}

	println(book)
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

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

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }


You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.Let's define a function $$$f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i$$$.Your task is to reorder the elements (choose an arbitrary order of elements) of the array $$$b$$$ to minimize the value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$. Since the answer can be very large, you have to print it modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.

The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$ and $$$b$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_j \le 10^6$$$), where $$$b_j$$$ is the $$$j$$$-th element of $$$b$$$.

Print one integer — the minimum possible value of $$$\sum\limits_{1 \le l \le r \le n} f(l, r)$$$ after rearranging elements of $$$b$$$, taken modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.

package main

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

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

	const mod int64 = 998244353
	var n int
	Fscan(in, &n)
	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
		// https://oeis.org/A003991
		a[i] *= int64(n-i) * int64(i+1)
	}
	sort.Slice(a, func(i, j int) bool { return a[i] > a[j] })
	b := make([]int, n)
	for i := range b {
		Fscan(in, &b[i])
	}
	sort.Ints(b)

	ans := int64(0)
	for i, ai := range a {
		ans = (ans + ai%mod*int64(b[i])) % mod
	}
	Fprint(out, ans)
}

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

package main

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

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

	const mod int64 = 998244353
	var n int
	Fscan(in, &n)
	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
		// https://oeis.org/A003991
		a[i] *= int64(n-i) * int64(i+1)
	}
	sort.Slice(a, func(i, j int) bool { return a[i] > a[j] })
	b := make([]int64, n)
	for i := range b {
		Fscan(in, &b[i])
	}
	sort.Slice(b, func(i, j int) bool { return b[i] < b[j] })

	ans := int64(0)
	for i, ai := range a {
		ans = (ans + ai%mod*b[i]) % mod
	}
	Fprint(out, ans)
}

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

package main

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

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

	const mod int64 = 998244353
	var n int
	Fscan(in, &n)
	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
		// https://oeis.org/A003991
		a[i] *= int64(n-i) * int64(i+1)
	}
	sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })
	b := make([]int, n)
	for i := range b {
		Fscan(in, &b[i])
	}
	sort.Ints(b)

	ans := int64(0)
	for i, ai := range a {
		ans = (ans + ai*int64(b[n-1-i])) % mod
	}
	Fprint(out, ans)
}

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


You are given a rooted tree consisting of $$$n$$$ vertices. Vertices are numbered from $$$1$$$ to $$$n$$$. Any vertex can be the root of a tree.A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root.The tree is specified by an array of ancestors $$$b$$$ containing $$$n$$$ numbers: $$$b_i$$$ is an ancestor of the vertex with the number $$$i$$$. The ancestor of a vertex $$$u$$$ is a vertex that is the next vertex on a simple path from $$$u$$$ to the root. For example, on the simple path from $$$5$$$ to $$$3$$$ (the root), the next vertex would be $$$1$$$, so the ancestor of $$$5$$$ is $$$1$$$.The root has no ancestor, so for it, the value of $$$b_i$$$ is $$$i$$$ (the root is the only vertex for which $$$b_i=i$$$).For example, if $$$n=5$$$ and $$$b=[3, 1, 3, 3, 1]$$$, then the tree looks like this.    An example of a rooted tree for $$$n=5$$$, the root of the tree is a vertex number $$$3$$$. You are given an array $$$p$$$ — a permutation of the vertices of the tree. If it is possible, assign any positive integer weights on the edges, so that the vertices sorted by distance from the root would form the given permutation $$$p$$$.In other words, for a given permutation of vertices $$$p$$$, it is necessary to choose such edge weights so that the condition $$$dist[p_i]<dist[p_{i+1}]$$$ is true for each $$$i$$$ from $$$1$$$ to $$$n-1$$$. $$$dist[u]$$$ is a sum of the weights of the edges on the path from the root to $$$u$$$. In particular, $$$dist[u]=0$$$ if the vertex $$$u$$$ is the root of the tree.For example, assume that $$$p=[3, 1, 2, 5, 4]$$$. In this case, the following edge weights satisfy this permutation:  the edge ($$$3, 4$$$) has a weight of $$$102$$$;  the edge ($$$3, 1$$$) has weight of $$$1$$$;  the edge ($$$1, 2$$$) has a weight of $$$10$$$;  the edge ($$$1, 5$$$) has a weight of $$$100$$$. The array of distances from the root looks like: $$$dist=[1,11,0,102,101]$$$. The vertices sorted by increasing the distance from the root form the given permutation $$$p$$$.Print the required edge weights or determine that there is no suitable way to assign weights. If there are several solutions, then print any of them.

The first line of input data contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of input data sets in the test. Each test case consists of three lines. The first of them contains an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$). It is the number of vertices in the tree. The second line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$). It is guaranteed that the $$$b$$$ array encodes some rooted tree. The third line contains the given permutation $$$p$$$: $$$n$$$ of different integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases in the test does not exceed $$$2 \cdot 10^5$$$.

For each set of input data print the answer on a separate line. If the solution exists, print an array of $$$n$$$ integers $$$w_1, w_2, \dots, w_n$$$, where $$$w_i$$$ is the weight of the edge that leads from $$$b_i$$$ to $$$i$$$. For the root there is no such edge, so use the value $$$w_i=0$$$. For all other vertices, the values of $$$w_i$$$ must satisfy the inequality $$$1 \le w_i \le 10^9$$$. There can be equal numbers among $$$w_i$$$ values, but all sums of weights of edges from the root to vertices must be different and satisfy the given permutation. If there are several solutions, output any of them. If no solution exists, output -1.

NoteThe first set of input data of the example is analyzed in the main part of the statement.In the second set of input data of the example, it is impossible to assign the positive weights to obtain a given permutation of vertices.

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T, n, root, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		pa := make([]int, n)
		for i := range pa {
			Fscan(in, &pa[i])
			if pa[i]--; pa[i] == i {
				root = i
			}
		}
		Fscan(in, &v)
		ok := v-1 == root
		dep := make([]int, n)
		for i := 1; i < n; i++ {
			Fscan(in, &v)
			if !ok { continue }
			v--
			if pa[v] != root && dep[pa[v]] == 0 {
				ok = false
			}
			dep[v] = i
		}
		if ok {
			for v, d := range dep {
				Fprint(out, d-dep[pa[v]], " ")
			}
			Fprintln(out)
		} else {
			Fprintln(out, -1)
		}
	}
}

package main

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

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

	var T, n, root, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		pa := make([]int, n)
		for i := range pa {
			Fscan(in, &pa[i])
			if pa[i]--; pa[i] == i {
				root = i
			}
		}
		dep := make([]int, n)
		ok := true
		for mx := 0; n > 0; n-- {
			Fscan(in, &v)
			v--
			if mx == 0 {
				mx++
				if v == root {
					continue
				}
				ok = false
			}
			if !ok {
				continue
			}
			p := pa[v]
			if p != root && dep[p] == 0 {
				ok = false
			}
			dep[v] = mx
			mx++
		}
		if ok {
			for v, d := range dep {
				Fprint(out, d-dep[pa[v]], " ")
			}
			Fprintln(out)
		} else {
			Fprintln(out, -1)
		}
	}
}

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

package main

import (
	"bufio"
	//	"container/heap" for heap.Init()
	"fmt"
	"os"
	"runtime"
)

func check(b bool) {
	if !b {
		_, _, line, _ := runtime.Caller(1)
		fmt.Printf("check failed line %d\n", line)
		os.Exit(-1)
	}
}

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

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

type IntHeap []int

func (h IntHeap) Len() int            { return len(h) }
func (h IntHeap) Less(i, j int) bool  { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

const (
	INIT = -1
)

var (
	n    int
	root int
	b    []int
	p    []int
)

func solve() bool {

	dist := make([]int, n)
	for i := 0; i < n; i++ {
		dist[i] = INIT
	}
	dist[root] = 0

	w := make([]int, n)
	w[root] = 0

	if p[0] != root {
		return false
	}

	t := 1
	for i := 1; i < n; i++ {
		u := p[i]
		path := dist[b[u]]
		if path == INIT {
			return false
		}
		w[u] = t - path
		dist[u] = path + w[u]

		t = dist[u] + 1
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", w[i])
	}
	fmt.Println()
	return true
}

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

	var tc int
	fmt.Fscan(r, &tc)

	for ; tc > 0; tc-- {

		fmt.Fscan(r, &n)
		b = make([]int, n)
		p = make([]int, n)

		for i := 0; i < n; i++ {
			fmt.Fscan(r, &b[i])
			b[i]--
			if b[i] == i {
				root = i
			}
		}
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &p[i])
			p[i]--
		}

		if solve() {
			continue
		}

		fmt.Println(INIT)

	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var test int
	fmt.Fscan(in, &test)
	for ; test > 0; test-- {
		var n int
		fmt.Fscan(in, &n)

		ancestors := make([]int, n)
		for i := range ancestors {
			fmt.Fscan(in, &ancestors[i])
		}
		perm := make([]int, n)
		for i := range perm {
			fmt.Fscan(in, &perm[i])
		}

		root := 0
		for i := range ancestors {
			if i+1 == ancestors[i] {
				root = i
				break
			}
		}

		if root != perm[0]-1 {
			fmt.Fprintln(out, -1)
			continue
		}

		isPossible := true
		weight := 1
		weights := make([]int, n)
		ans := make([]int, n)
		for i := 1; i < n; i++ {
			p := perm[i] - 1
			a := ancestors[p] - 1
			w := weights[a]
			if w == 0 && a != root {
				isPossible = false
				break
			}
			ans[p] = weight - w
			weights[p] = ans[p] + w
			weight++
		}

		if !isPossible {
			fmt.Fprintln(out, -1)
		} else {
			for i := range ans {
				if i != 0 {
					fmt.Fprint(out, " ")
				}
				fmt.Fprint(out, ans[i])
			}
			fmt.Fprintln(out)
		}
	}
}

package main

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

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)
)

const (
	MaxN int = 2e5 + 5
)

var (
	T      int
	n      int
	parent []int
	child  [][]int
	root   int
)

func solve(order []int) []int {
	var (
		s   = make(map[int]struct{})
		ans = make([]int, len(order)+1)
		val = make([]int, len(order)+1)
	)

	val[0] = 0
	s[root] = struct{}{}
	for i, u := range order {
		if _, ok := s[u]; !ok {
			return []int{0, -1}
		}
		ans[u] = i - val[parent[u]]
		val[u] = i
		delete(s, u)
		for _, uChild := range child[u] {
			s[uChild] = struct{}{}
		}
	}
	return ans
}

func main() {
	_, _ = fmt.Fscan(rr, &T)
	for iCase := 0; iCase < T; iCase++ {
		_, _ = fmt.Fscan(rr, &n)

		parent = make([]int, n+1)
		child = make([][]int, n+1)

		for i := 1; i <= n; i++ {
			var u int
			_, _ = fmt.Fscan(rr, &u)
			if u != i {
				parent[i] = u
				child[u] = append(child[u], i)
			} else {
				root = u
			}
		}

		a := make([]int, 0, n)
		for i := 1; i <= n; i++ {
			var t int
			_, _ = fmt.Fscan(rr, &t)
			a = append(a, t)
		}

		for _, i := range solve(a)[1:] {
			_, _ = fmt.Fprintf(ww, "%d ", i)
		}
		_, _ = fmt.Fprintln(ww)
	}
	_ = ww.Flush()
}

package main

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

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

func solveCase() {
	var n int
	var foundSolution = true
	fmt.Fscan(in, &n)
	var parent []int = make([]int, n+1)
	var order []int = make([]int, n+1)
	var distance []int = make([]int, n+1)
	var seen []bool = make([]bool, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &parent[i])
	}
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &order[i])
	}

	if parent[order[1]] != order[1] {
		foundSolution = false
	} else {
		seen[order[1]] = true
		for i := 2; i <= n; i++ {
			if !seen[parent[order[i]]] {
				foundSolution = false
				break
			}
			seen[order[i]] = true
			distance[order[i]] = i
		}
	}

	if !foundSolution {
		fmt.Fprintf(out, "-1\n")
	} else {
		for i := n; i > 1; i-- {
			distance[order[i]] -= distance[parent[order[i]]]
		}
		for i := 1; i <= n; i++ {
			fmt.Fprintf(out, "%d ", distance[i])
		}
		fmt.Fprintln(out)
	}
}

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		solveCase()
	}
	out.Flush()
}

package main

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

func solve(in *bufio.Reader, out *bufio.Writer){
	defer out.Flush()
	n := 0
	Fscan(in,&n)
	var b []int
	var p []int
	for i := 0;i < n;i++{
		var item int
		Fscan(in,&item)
		item--
		b = append(b,item)
	}
	for i := 0;i < n;i++{
		var item int
		Fscan(in,&item)
		item--
		p = append(p,item)
	}
	var a1  = make([]int,n)
	var ans  = make([]int,n)
	for i := 0;i < n;i++{
		a1[p[i]] = i
	}
	for i := 0;i < n;i++{
		if a1[i] < a1[b[i]]{
			Fprintln(out,-1)
			return
		}else{
			ans[i] = a1[i] - a1[b[i]]
		}
	}
	for _,v := range ans{
		Fprintf(out,"%d ",v)
	}
	Fprintln(out)
}

func main(){
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 0
	Fscan(in,&n)
	for i := 0;i < n;i++{
		solve(in,out)
	}
}

package main

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

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

	var T, n, root, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		pa := make([]int, n)
		for i := range pa {
			Fscan(in, &pa[i])
			if pa[i]--; pa[i] == i {
				root = i
			}
		}
		dep := make([]int, n)
		ok := true
		for mx := 0; n > 0; n-- {
			Fscan(in, &v)
			v--
			if !ok || v == root {
				continue
			}
			p := pa[v]
			if p != root && dep[p] == 0 {
				ok = false
			}
			mx++
			dep[v] = mx
		}
		if ok {
			for v, d := range dep {
				Fprint(out, d-dep[pa[v]], " ")
			}
			Fprintln(out)
		} else {
			Fprintln(out, -1)
		}
	}
}

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

package main

import (
	"bufio"
	//	"container/heap" for heap.Init()
	"fmt"
	"os"
	"runtime"
)

func check(b bool) {
	if !b {
		_, _, line, _ := runtime.Caller(1)
		fmt.Printf("check failed line %d\n", line)
		os.Exit(-1)
	}
}

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

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

type IntHeap []int

func (h IntHeap) Len() int            { return len(h) }
func (h IntHeap) Less(i, j int) bool  { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

const (
	INIT = -1
)

var (
	n    int
	root int
	b    []int
	p    []int
)

func solve() bool {

	dist := make([]int, n)
	for i := 0; i < n; i++ {
		dist[i] = INIT
	}
	dist[root] = 0

	w := make([]int, n)
	w[root] = 0

	if p[0] != root {
		return false
	}

	t := 1
	for i := 1; i < n; i++ {
		u := p[i]
		path := dist[b[u]]
		if path == INIT {
			return false
		}
		w[u] = max(t+1-path, path+1)
		dist[u] = path + w[u]

		t = dist[u] + 1
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", w[i])
	}
	fmt.Println()
	return true
}

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

	var tc int
	fmt.Fscan(r, &tc)

	for ; tc > 0; tc-- {

		fmt.Fscan(r, &n)
		b = make([]int, n)
		p = make([]int, n)

		for i := 0; i < n; i++ {
			fmt.Fscan(r, &b[i])
			b[i]--
			if b[i] == i {
				root = i
			}
		}
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &p[i])
			p[i]--
		}

		if solve() {
			continue
		}

		fmt.Println(INIT)

	}
}


You are given an array $$$a$$$ of $$$n$$$ integers.You want to make all elements of $$$a$$$ equal to zero by doing the following operation exactly three times:  Select a segment, for each number in this segment we can add a multiple of $$$len$$$ to it, where $$$len$$$ is the length of this segment (added integers can be different). It can be proven that it is always possible to make all elements of $$$a$$$ equal to zero.

The first line contains one integer $$$n$$$ ($$$1 \le n \le 100\,000$$$): the number of elements of the array. The second line contains $$$n$$$ elements of an array $$$a$$$ separated by spaces: $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$).

The output should contain six lines representing three operations. For each operation, print two lines:   The first line contains two integers $$$l$$$, $$$r$$$ ($$$1 \le l \le r \le n$$$): the bounds of the selected segment.  The second line contains $$$r-l+1$$$ integers $$$b_l, b_{l+1}, \dots, b_r$$$ ($$$-10^{18} \le b_i \le 10^{18}$$$): the numbers to add to $$$a_l, a_{l+1}, \ldots, a_r$$$, respectively; $$$b_i$$$ should be divisible by $$$r - l + 1$$$.

package main

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

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

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

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

var err error

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

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

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

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

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

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

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

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

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

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

const mod = 1000000007

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

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

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

func solve() {
	n := nextInt()
	if n == 1 {
		x := nextInt()
		printf("1 1\n1\n1 1\n1\n1 1\n%d\n", -(x + 2))
		return
	}
	arr := make([]int64, n)
	for i := range arr {
		arr[i] = nextLong()
	}
	printf("%d %d\n", 1, n)
	for i, v := range arr {
		add := v % int64(n-1)
		printf("%d ", -add*int64(n))
		arr[i] -= add * int64(n)
	}
	printf("\n")
	printf("1 1\n%d\n", -arr[0])
	printf("%d %d\n", 2, n)
	for i := 1; i < n; i++ {
		printf("%d ", -arr[i])
	}
	printf("\n")
}

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

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

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

package main

import (
	"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 n int
	scanf("%d\n", &n)
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		scanf("%d", &arr[i])
	}
	if n != 1 {
		printf("1 1\n")
		printf("%d\n", -arr[0])

		printf("1 %d\n", n)
		printf("0 ")
		for i := 1; i < n; i++ {
			printf("%v ", int64(-n)*arr[i])
		}
		printf("\n")
		printf("2 %d\n", n)
		for i := 1; i < n; i++ {
			printf("%v ", int64(n-1)*arr[i])
		}
		printf("\n")
	} else {
		printf("1 1\n0\n1 1\n0\n1 1\n%v\n", -arr[0])
	}
}

package main

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

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

	var n int64
	Fscan(in, &n)
	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	if n == 1 {
		Fprint(out, "1 1\n", -a[0], "\n1 1\n0\n1 1\n0")
		return
	}
	Fprintln(out, "1 1\n", -a[0])
	a[0] = 0
	Fprintln(out, 2, n)
	for i, v := range a[1:] {
		Fprint(out, v*(n-1), " ")
		a[i+1] += v * (n - 1)
	}
	Fprintln(out)
	Fprintln(out, 1, n)
	for _, v := range a {
		Fprint(out, -v, " ")
	}
}

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

package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() int64 {
		sc.Scan()
		i, _ := strconv.ParseInt(sc.Text(), 10, 64)
		return i
	}

	n := in()

	a := make([]int64, n)

	for i := range a {
		a[i] = in()
	}

	b := make([]int64, n)

	if n == 1 {
		fmt.Println("1 1")
		fmt.Println("0")
		fmt.Println("1 1")
		fmt.Println("0")
		fmt.Println("1 1")
		fmt.Println(-1 * a[0])
		return
	}

	for j := range a {
		b[j] = (n - 1) * (a[j]%n)
	}

	fmt.Println(1, n-1)
	var i int64
	for i = 0; i < n-1; i++ {
		fmt.Print(b[i], " ")
	}
	fmt.Println()

	fmt.Println(2, n)
	for i = 0; i < n-2; i++ {
		fmt.Print("0 ")
	}
	fmt.Print(b[n-1])

	fmt.Println()
	fmt.Println(1, n)
	for i := range b {
		fmt.Print(-1*(a[i]+b[i]), " ")
	}

}

/*
URL:
https://codeforces.com/contest/1397/problem/C
*/

package main

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

var (
	n int64
	A []int64
)

func main() {
	n = readll()
	A = readlls(int(n))

	if n == 1 {
		fmt.Println(1, 1)
		fmt.Println(-A[0])
		fmt.Println(1, 1)
		fmt.Println(0)
		fmt.Println(1, 1)
		fmt.Println(0)
		return
	}

	// 長さ1
	fmt.Println(1, 1)
	fmt.Println(-A[0])

	// 長さn
	B := []int64{0}
	for i := int64(1); i < n; i++ {
		r := A[i] % (n - 1)
		times := (n - 1) - r
		val := times * n

		A[i] += val

		B = append(B, val)
	}
	fmt.Println(1, n)
	fmt.Println(PrintInts64Line(B...))

	// PrintfDebug("%v\n", A)

	// 長さn-1
	B = []int64{}
	for i := int64(1); i < n; i++ {
		val := A[i]
		B = append(B, -val)
	}
	fmt.Println(2, n)
	fmt.Println(PrintInts64Line(B...))
}

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

/********** common constants **********/

const (
	// General purpose
	MOD = 1000000000 + 7
	// MOD          = 998244353
	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
)

// modint can calculate a right residual whether value is positive or negative.
func modint(val, m int) int {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

// modll can calculate a right residual whether value is positive or negative.
func modll(val, m int64) int64 {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

/********** bufio setting **********/

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

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

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

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

//str := reads()
//i := readi()
//X := readis(n)
//S := readrs()
//a := readf()
//A := readfs(n)

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

/*********** Input ***********/

var (
	// reads returns a WORD string.
	reads  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()
	}
}

// readi returns an integer.
func readi() int {
	return int(_readInt64())
}
func readi2() (int, int) {
	return int(_readInt64()), int(_readInt64())
}
func readi3() (int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64())
}
func readi4() (int, int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64()), int(_readInt64())
}

// readll returns as integer as int64.
func readll() int64 {
	return _readInt64()
}
func readll2() (int64, int64) {
	return _readInt64(), _readInt64()
}
func readll3() (int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64()
}
func readll4() (int64, int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64(), _readInt64()
}

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

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

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

// readf returns an float64.
func readf() float64 {
	return float64(_readFloat64())
}

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

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

// readrs returns a rune slice.
func readrs() []rune {
	return []rune(reads())
}

/*********** Output ***********/

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

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

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

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

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

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.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 main() {
	var n int
	scanf("%d\n", &n)
	var a, b []int64 = make([]int64, n), make([]int64, n)
	for i := range a {
		scanf("%d ", &a[i])
	}
	b[0] = 0
	if n == 1 {
		printf("1 1\n0\n1 1\n0\n1 1\n%d\n", -a[0])
	} else {
		printf("1 1\n%d\n1 %d\n", -a[0], n)
		a[0] = 0
		for i := 0; i < n; i++ {
			printf("%d ", -int64(n)*a[i])
		}
		printf("\n2 %d\n", n)
		for i := 1; i < n; i++ {
			printf("%d ", int64(n-1)*a[i])
		}
		printf("\n")
	}
	writer.Flush()
}

package main

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

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

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

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

var err error

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

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

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

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

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

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

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

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

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

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

const mod = 1000000007

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

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

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

func solve() {
	n := nextInt()
	if n == 1 {
		x := nextInt()
		printf("1 1\n1\n1 1\n1\n1 1\n%d\n", -(x + 3))
		return
	}
	arr := make([]int64, n)
	for i := range arr {
		arr[i] = nextLong()
	}
	printf("%d %d\n", 1, n)
	for i, v := range arr {
		add := v % int64(n-1)
		printf("%d ", -add*int64(n))
		arr[i] -= add * int64(n)
	}
	printf("\n")
	printf("1 1\n%d\n", -arr[0])
	printf("%d %d\n", 2, n)
	for i := 1; i < n; i++ {
		printf("%d ", -arr[i])
	}
	printf("\n")
}

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

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

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

package main

import (
	"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 n int
	scanf("%d\n", &n)
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		scanf("%d", &arr[i])
	}

	printf("1 1\n")
	printf("%d\n", -arr[0])

	printf("1 %d\n", n)
	printf("0 ")
	for i := 1; i < n; i++ {
		printf("%v ", -n*arr[i])
	}
	printf("\n")
	printf("2 %d\n", n)
	for i := 1; i < n; i++ {
		printf("%v ", (n-1)*arr[i])
	}
	printf("\n")
}

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 n int
	scanf("%d\n", &n)
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		scanf("%d", &arr[i])
	}
	if n != 1 {
		printf("1 1\n")
		printf("%d\n", -arr[0])

		printf("1 %d\n", n)
		printf("0 ")
		for i := 1; i < n; i++ {
			printf("%v ", -n*arr[i])
		}
		printf("\n")
		printf("2 %d\n", n)
		for i := 1; i < n; i++ {
			printf("%v ", (n-1)*arr[i])
		}
		printf("\n")
	} else {
		printf("1 1\n0\n1 1\n0\n1 1\n%v\n", -arr[0])
	}
}

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 n int
	scanf("%d\n", &n)
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		scanf("%d", &arr[i])
	}

	printf("1 1\n")
	printf("%d\n", -arr[0])

	printf("1 %d\n", n)
	printf("0 ")
	for i := 1; i < n; i++ {
		printf("%v ", -2*arr[i])
	}
	printf("\n")
	printf("2 %d\n", n)
	for i := 1; i < n; i++ {
		printf("%v ", arr[i])
	}
	printf("\n")
}

package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() int64 {
		sc.Scan()
		i, _ := strconv.ParseInt(sc.Text(), 10, 64)
		return i
	}

	n := in()

	a := make([]int64, n)

	for i := range a {
		a[i] = in()
	}

	b := make([]int64, n)

	for j := range a {
		var i int64
		for i = 1; i <= n; i++ {
			if ((n-1)*i + a[j])%n == 0 {
				b[j] = (n - 1) * i
				break
			}
		}
	}

	fmt.Println(1, n-1)
	var i int64
	for i = 0; i < n-1; i++ {
		fmt.Print(b[i], " ")
	}
	fmt.Println()

	fmt.Println(2, n)
	for i = 0; i < n-2; i++ {
		fmt.Print("0 ")
	}
	fmt.Print(b[n-1])

	fmt.Println()
	fmt.Println(1, n)
	for i := range b {
		fmt.Print(-1* b[i], " ")
	}

}

package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() int64 {
		sc.Scan()
		i, _ := strconv.ParseInt(sc.Text(), 10, 64)
		return i
	}

	n := in()

	a := make([]int64, n)

	for i := range a {
		a[i] = in()
	}

	b := make([]int64, n)

	for j := range a {
		var i int64
		for i = 1; i <= n; i++ {
			if ((n-1)*i + a[j])%n == 0 {
				b[j] = (n - 1) * i
				break
			}
		}
	}

	fmt.Println(1, n-1)
	var i int64
	for i = 0; i < n-1; i++ {
		fmt.Print(b[i], " ")
	}
	fmt.Println()

	fmt.Println(2, n)
	for i = 0; i < n-2; i++ {
		fmt.Print("0 ")
	}
	fmt.Print(b[n-1])

	fmt.Println()
	fmt.Println(1, n)
	for i := range b {
		fmt.Print(-1*(a[i] + b[i]), " ")
	}

}

package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() int64 {
		sc.Scan()
		i, _ := strconv.ParseInt(sc.Text(), 10, 64)
		return i
	}

	n := in()

	a := make([]int64, n)

	for i := range a {
		a[i] = in()
	}

	b := make([]int64, n)

	if n == 1 {
		fmt.Println("1, 1")
		fmt.Println("0")
		fmt.Println("1, 1")
		fmt.Println("0")
		fmt.Println("1, 1")
		fmt.Println(-1*a[0])
		return
	}

	for j := range a {
		var i int64
		for i = 1; i <= n; i++ {
			if ((n-1)*i + a[j])%n == 0 {
				b[j] = (n - 1) * i
				break
			}
		}
	}

	fmt.Println(1, n-1)
	var i int64
	for i = 0; i < n-1; i++ {
		fmt.Print(b[i], " ")
	}
	fmt.Println()

	fmt.Println(2, n)
	for i = 0; i < n-2; i++ {
		fmt.Print("0 ")
	}
	fmt.Print(b[n-1])

	fmt.Println()
	fmt.Println(1, n)
	for i := range b {
		fmt.Print(-1*(a[i] + b[i]), " ")
	}

}

/*
URL:
https://codeforces.com/contest/1397/problem/C
*/

package main

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

var (
	n int64
	A []int64
)

func main() {
	n = readll()
	A = readlls(int(n))

	if n == 1 {
		fmt.Println(1, 1)
		fmt.Println(-A[0])
		fmt.Println(1, 1)
		fmt.Println(0)
		fmt.Println(1, 1)
		fmt.Println(0)
		return
	}

	fmt.Println(1, 1)
	fmt.Println(-A[0])

	B := []int64{}
	for i := int64(1); i < n; i++ {
		r := A[i] % (n - 1)
		times := (n - 1) - r
		val := times * n

		A[i] += val

		B = append(B, val)
	}
	fmt.Println(2, n)
	fmt.Println(PrintInts64Line(B...))

	// PrintfDebug("%v\n", A)

	B = []int64{}
	for i := int64(1); i < n; i++ {
		val := A[i]
		B = append(B, -val)
	}
	fmt.Println(2, n)
	fmt.Println(PrintInts64Line(B...))
}

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

/********** common constants **********/

const (
	// General purpose
	MOD = 1000000000 + 7
	// MOD          = 998244353
	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
)

// modint can calculate a right residual whether value is positive or negative.
func modint(val, m int) int {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

// modll can calculate a right residual whether value is positive or negative.
func modll(val, m int64) int64 {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

/********** bufio setting **********/

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

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

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

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

//str := reads()
//i := readi()
//X := readis(n)
//S := readrs()
//a := readf()
//A := readfs(n)

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

/*********** Input ***********/

var (
	// reads returns a WORD string.
	reads  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()
	}
}

// readi returns an integer.
func readi() int {
	return int(_readInt64())
}
func readi2() (int, int) {
	return int(_readInt64()), int(_readInt64())
}
func readi3() (int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64())
}
func readi4() (int, int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64()), int(_readInt64())
}

// readll returns as integer as int64.
func readll() int64 {
	return _readInt64()
}
func readll2() (int64, int64) {
	return _readInt64(), _readInt64()
}
func readll3() (int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64()
}
func readll4() (int64, int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64(), _readInt64()
}

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

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

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

// readf returns an float64.
func readf() float64 {
	return float64(_readFloat64())
}

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

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

// readrs returns a rune slice.
func readrs() []rune {
	return []rune(reads())
}

/*********** Output ***********/

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

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

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

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

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

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.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 main() {
	var n int
	scanf("%d\n", &n)
	var a, b []int64 = make([]int64, n), make([]int64, n)
	for i := range a {
		scanf("%d ", &a[i])
	}
	b[0] = 0
	if n == 1 {
		printf("1 1\n0\n1 1\n0\n1 1\n%d\n", -a[0])
	} else {
		printf("1 1\n%d\n1 %d\n", -a[0], n)
		a[0] = 0
		for i := 0; i < n; i++ {
			printf("%d ", -int64(n)*a[i])
		}
		printf("\n2 %d\n", n-1)
		for i := 1; i < n; i++ {
			printf("%d ", int64(n-1)*a[i])
		}
		printf("\n")
	}
	writer.Flush()
}


It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.There are $$$n$$$ displays placed along a road, and the $$$i$$$-th of them can display a text with font size $$$s_i$$$ only. Maria Stepanovna wants to rent such three displays with indices $$$i < j < k$$$ that the font size increases if you move along the road in a particular direction. Namely, the condition $$$s_i < s_j < s_k$$$ should be held.The rent cost is for the $$$i$$$-th display is $$$c_i$$$. Please determine the smallest cost Maria Stepanovna should pay.

The first line contains a single integer $$$n$$$ ($$$3 \le n \le 3\,000$$$) — the number of displays. The second line contains $$$n$$$ integers $$$s_1, s_2, \ldots, s_n$$$ ($$$1 \le s_i \le 10^9$$$) — the font sizes on the displays in the order they stand along the road. The third line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \le c_i \le 10^8$$$) — the rent costs for each display.

If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $$$i < j < k$$$ such that $$$s_i < s_j < s_k$$$.

NoteIn the first example you can, for example, choose displays $$$1$$$, $$$4$$$ and $$$5$$$, because $$$s_1 < s_4 < s_5$$$ ($$$2 < 4 < 10$$$), and the rent cost is $$$40 + 10 + 40 = 90$$$.In the second example you can't select a valid triple of indices, so the answer is -1.

package main

import (
	"fmt"
	"math"
)

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

	mincost1 := make([]int, n)
	mincost2 := make([]int, n)
	x := make([]int, n)
	for i := 0; i < n; i++ {
		x[i] = 1
		mincost1[i] = c[i]
		mincost2[i] = math.MaxInt32
		if i > 0 && mincost1[i] > mincost1[i-1] {
			mincost1[i] = mincost1[i-1]
		}
	}

	min := math.MaxInt32
	for i := 1; i < n; i++ {
		for j := 0; j < i; j++ {
			if s[i] > s[j] && x[i] < x[j]+1 {
				x[i] = x[j] + 1
				if x[i] == 2 {
					if c[i]+c[j] < mincost2[i] {
						mincost2[i] = c[i] + c[j]
					}
					x[i] = 1
				}
			}
		}
	}
	for i := 0; i < n; i++ {
		x[i] = 1
	}

	for i := 1; i < n; i++ {
		for j := 0; j < i; j++ {
			if s[i] > s[j] && x[i] < x[j]+1 {
				x[i] = x[j] + 1
				if x[i] == 3 {
					if c[i]+mincost2[j] < min {
						min = c[i] + mincost2[j]
					}
					x[i] = 2
				}
			}
		}
	}
	if min == math.MaxInt32 {
		fmt.Println(-1)
		return
	}
	fmt.Println(min)
}

package main

import (
	"fmt"
	"math"
)

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

	mincost2 := make([]int, n)
	x := make([]int, n)
	for i := 0; i < n; i++ {
		x[i] = 1
		mincost2[i] = math.MaxInt32
	}

	min := math.MaxInt32
	for i := 1; i < n; i++ {
		for j := 0; j < i; j++ {
			if s[i] > s[j] {
				if c[i]+c[j] < mincost2[i] {
					mincost2[i] = c[i] + c[j]
				}
			}
		}
	}

	for i := 1; i < n; i++ {
		for j := 0; j < i; j++ {
			if s[i] > s[j] && x[i] < x[j]+1 {
				x[i] = x[j] + 1
				if x[i] == 3 {
					if c[i]+mincost2[j] < min {
						min = c[i] + mincost2[j]
					}
					x[i] = 2
				}
			}
		}
	}
	if min == math.MaxInt32 {
		fmt.Println(-1)
		return
	}
	fmt.Println(min)
}

package main

import (
	"fmt"
	"math"
)

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

	mincost2 := make([]int, n)
	x := make([]int, n)
	for i := 0; i < n; i++ {
		x[i] = 1
		mincost2[i] = math.MaxInt32
	}

	min := math.MaxInt32
	for i := 1; i < n; i++ {
		for j := 0; j < i; j++ {
			if s[i] > s[j] && x[i] < x[j]+1 {
				x[i] = x[j] + 1
				if x[i] == 2 {
					if c[i]+c[j] < mincost2[i] {
						mincost2[i] = c[i] + c[j]
					}
					x[i] = 1
				}
			}
		}
	}
	for i := 0; i < n; i++ {
		x[i] = 1
	}

	for i := 1; i < n; i++ {
		for j := 0; j < i; j++ {
			if s[i] > s[j] && x[i] < x[j]+1 {
				x[i] = x[j] + 1
				if x[i] == 3 {
					if c[i]+mincost2[j] < min {
						min = c[i] + mincost2[j]
					}
					x[i] = 2
				}
			}
		}
	}
	if min == math.MaxInt32 {
		fmt.Println(-1)
		return
	}
	fmt.Println(min)
}

package main

import (
	"fmt"
)

func Min (x int64 , y int64) int64 {
	if x < y {
		return x
	}
	return y
}

func main () {
	var n int
	fmt.Scanf("%d\n",&n)

	len := []int64{}
	cost := []int64{}
	for i := 0 ;i< n ;i++ {
		var l int64
		if i == n - 1 {
			fmt.Scanf("%d\n",&l)
		} else {
			fmt.Scanf("%d",&l)
		}

		len = append(len , l)
	}

	for i := 0 ;i< n ;i++ {
		var c int64
		if i == n - 1 {
			fmt.Scanf("%d\n",&c)
		} else {
			fmt.Scanf("%d",&c)
		}

		cost = append(cost , c)
	}

	dp2 := make([]int64,4000)
	dp3 := make([]int64,4000)

	for i := 0 ;i< n ;i++ {
		dp2[i] = 0xffffffff
		for j := 0 ; j < i ; j ++ {
			if len[j] < len[i] {
				dp2[i] = Min(dp2[i],cost[j] + cost[i])
			}
		}
	}

	var ans int64 = 0xffffffff
	for i := 0 ;i< n ;i++ {
		dp3[i] = 0xffffffff
		for j := 1 ; j < i ; j ++ {
			if len[j] < len[i] {
				dp3[i] = Min(dp3[i],dp2[j] + cost[i])
			}
		}
		ans = Min(ans,dp3[i])
	}

	if ans == 0xffffffff{
		fmt.Println(-1)
	} else {
		fmt.Println(ans)
	}
}

package main

/*
	The idea to solve this problem is by fix to screen j, after that we search
	for screen i, then we search for screen k. For details:
	https://codeforces.com/blog/entry/59758
*/

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

func main() {
	var n, x int
	stdin := bufio.NewReader(os.Stdin)
	fmt.Fscanf(stdin, "%d\n", &n)
	fonts := make([]int, 0, n)
	costs := make([]int, 0, n)
	// load fonts
	for i := 0; i < n; i++ {
		format := "%d"
		if i == n-1 {
			format += "\n"
		}
		fmt.Fscanf(stdin, format, &x)
		fonts = append(fonts, x)
	}
	// load costs
	for i := 0; i < n; i++ {
		format := "%d"
		if i == n-1 {
			format += "\n"
		}
		fmt.Fscanf(stdin, format, &x)
		costs = append(costs, x)
	}
	// search process, we start by fixing in j screen
	minCost := math.MaxInt32
	for j := 0; j < n; j++ {
		// search for i screen
		screenIdx := -1
		cost := costs[j]
		for i := 0; i < j; i++ {
			// the font size must be smaller than j screen
			if fonts[i] >= fonts[j] {
				continue
			}
			// take first screen or which has lower price than first screen
			if screenIdx == -1 || costs[i] < costs[screenIdx] {
				screenIdx = i
			}
		}
		// if screen not found skip
		if screenIdx == -1 {
			continue
		}
		// add cost of renting screen
		cost += costs[screenIdx]

		// search for k screen
		screenIdx = -1
		for k := j + 1; k < n; k++ {
			// the font size must larger than j screen
			if fonts[k] <= fonts[j] {
				continue
			}
			// take first screen or which has lower price than first screen
			if screenIdx == -1 || costs[k] < costs[screenIdx] {
				screenIdx = k
			}
		}
		// if screen not found skip
		if screenIdx == -1 {
			continue
		}
		// add cost of renting screen
		cost += costs[screenIdx]

		// search for overall min cost
		if cost < minCost {
			minCost = cost
		}
	}
	if minCost == math.MaxInt32 {
		fmt.Println("-1")
	} else {
		fmt.Printf("%d\n", minCost)
	}
}

package main

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

func main() {
	var n, x int
	stdin := bufio.NewReader(os.Stdin)
	fmt.Fscanf(stdin, "%d\n", &n)
	fonts := make([]int, 0, n)
	costs := make([]int, 0, n)
	// load fonts
	for i := 0; i < n; i++ {
		format := "%d"
		if i == n-1 {
			format += "\n"
		}
		fmt.Fscanf(stdin, format, &x)
		fonts = append(fonts, x)
	}
	// load costs
	for i := 0; i < n; i++ {
		format := "%d"
		if i == n-1 {
			format += "\n"
		}
		fmt.Fscanf(stdin, format, &x)
		costs = append(costs, x)
	}
	// search process, we start by fixing in j screen
	minCost := math.MaxInt32
	for j := 0; j < n; j++ {
		// search for i screen
		screenIdx := -1
		cost := costs[j]
		for i := 0; i < j; i++ {
			// the font size must be smaller than j screen
			if fonts[i] >= fonts[j] {
				continue
			}
			// take first screen or which has lower price than first screen
			if screenIdx == -1 || costs[i] < costs[screenIdx] {
				screenIdx = i
			}
		}
		// if screen not found skip
		if screenIdx == -1 {
			continue
		}
		// add cost of renting screen
		cost += costs[screenIdx]

		// search for k screen
		screenIdx = -1
		for k := j + 1; k < n; k++ {
			// the font size must larger than j screen
			if fonts[k] <= fonts[j] {
				continue
			}
			// take first screen or which has lower price than first screen
			if screenIdx == -1 || costs[k] < costs[screenIdx] {
				screenIdx = k
			}
		}
		// if screen not found skip
		if screenIdx == -1 {
			continue
		}
		// add cost of renting screen
		cost += costs[screenIdx]

		// search for overall min cost
		if cost < minCost {
			minCost = cost
		}
	}
	if minCost == math.MaxInt32 {
		fmt.Println("-1")
	} else {
		fmt.Printf("%d\n", minCost)
	}
}

package main

import "fmt"

const (
    maxn int = 3005
    inf int = 300000005
)

var (
    n int
    s, c [maxn]int
    dp [4][maxn]int
)

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

func read() {
    fmt.Scan(&n)
    for i := 1; i <= n; i++ {
        fmt.Scan(&s[i])
    }
    for i := 1; i <= n; i++ {
        fmt.Scan(&c[i])
    }
}

func solve() {
    var ans int = inf
    for i := 1; i <= n; i++ { dp[1][i] = c[i] }
    for i := 2; i <= 3; i++ {
        for j := 1; j <= n; j++ {
            dp[i][j] = inf
            for k := 1; k < j; k++ {
                if s[k] < s[j] {
                    dp[i][j] = min(dp[i][j], dp[i - 1][k] + c[j])
                }
            }
            if i == 3 && dp[i][j] < ans { ans = dp[i][j] }
        }
    }
    if ans == inf { fmt.Println("-1") } else { fmt.Println(ans) }
}

func main() {
    read()
    solve()
}

package main

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

func solve() {
	n := readInt()
	s := readIntArray(n)
	c := readIntArray(n)
	ans := math.MaxInt32
	for i := 0; i < n; i++ {
		l := math.MaxInt32
		r := math.MaxInt32
		for j := i + 1; j < n; j++ {
			if s[i] < s[j] {
				r = min(r, c[j])
			}
		}
		for j := i - 1; j >= 0; j-- {
			if s[j] < s[i] {
				l = min(l, c[j])
			}
		}
		if l != math.MaxInt32 && r != math.MaxInt32 {
			ans = min(ans, c[i]+r+l)
		}
	}
	if ans == math.MaxInt32 {
		fmt.Println(-1)
	} else {
		fmt.Println(ans)
	}
}

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 (
	"fmt"
)

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

func main() {
	var n, i, j int64
	fmt.Scan(&n)

	var s[3001]int64
	var c[3001]int64

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

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

	var dp1[3001]int64
	var dp2[3001]int64

	i = 0
	for i < n {
		j = 0
		dp1[i] = 400000100
		for j < i {
			if s[j] < s[i] {
				dp1[i] = min(dp1[i], c[i] + c[j])
			}
			j ++
		}
		i ++
	}

	i = 0
	for i < n {
		j = 0
		dp2[i] = 400000100
		for j < i {
			if s[j] < s[i] {
				dp2[i] = min(dp2[i], dp1[j] + c[i])
			}
			j ++
		}
		i ++
	}

	var ans int64
	ans = 400000100
	i = 2
	for i < n {
		// fmt.Println(dp2[i])
		ans = min(ans, dp2[i])
		i ++
	}
	if ans == 400000100 {
		ans = -1
	}
	fmt.Println(ans)
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)

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

func main() {
	scanner.Split(bufio.ScanWords)
	n := scanNum()
	a, b := make([]int, n), make([]int, n)
	memo := [][]int{[]int{}, b}
	for i := 0; i < n; i++ {
		a[i] = scanNum()
	}
	for i := 0; i < n; i++ {
		b[i] = scanNum()
	}

	for i := 2; i < 4; i++ {
		memo = append(memo, make([]int, n))
		for j := 0; j < n; j++ {
			memo[i][j] = int(1e8) * 4
		}
		for j := 0; j <= (n - i); j++ {
			best := int(1e8) * 4

			for k := j + 1; k < n; k++ {
				if (a[k] > a[j]) && (best > (b[j] + memo[i-1][k])) {
					best = b[j] + memo[i-1][k]
				}
			}

			memo[i][j] = best
		}
	}

	sort.Ints(memo[3])
	if memo[3][0] == int(1e8)*4 {
		fmt.Println(-1)
	} else {
		fmt.Println(memo[3][0])
	}
}

package main

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

const (
	MaxN = 3050
	Inf  = 10e12
)

var n int
var s, c, dp1, dp2 [MaxN]int64

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &s[i])
		dp1[i] = Inf
		dp2[i] = Inf
	}
	fmt.Fscanf(buf, "\n")
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &c[i])
	}
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			if s[i] < s[j] && dp1[j] > c[i]+c[j] {
				dp1[j] = c[i] + c[j]
			}
		}
	}
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			if s[i] < s[j] && dp2[j] > dp1[i]+c[j] {
				dp2[j] = dp1[i] + c[j]
			}
		}
	}
	minPrice := int64(Inf)
	for i := 0; i < n; i++ {
		if minPrice > dp2[i] {
			minPrice = dp2[i]
		}
	}
	if minPrice >= Inf {
		minPrice = -1
	}
	fmt.Println(minPrice)
}

package main

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

var sc = bufio.NewScanner(os.Stdin)
const EPS = 0.00000001

func nextLine() string{
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

type Data struct {
	index int
	//font int
	cost int
}

// x: fontが大きくなるならx[i]にdataを追加していく
// i: 0~nについて、x[i] -> cost最小のdata -> x[dataのi] -> cost最小の....を3回やって最小を出す

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

	n := nextInt()

	s := make([]int64, n)
	c := make([]int64, n)

	for i:=0; i<n; i++{
		s[i] = int64(nextInt())
	}
	for i:=0; i<n; i++{
		c[i] = int64(nextInt())
	}

	var totalCost int64 = int64(math.MaxInt64)
	for j :=1; j<n-1; j++{
		var minCost1 int64 = int64(math.MaxInt64)
		for i:=0; i<j; i++{
			if s[i] < s[j] && c[i] < minCost1 {
				minCost1 = c[i]
			}
		}
		var minCost2 int64 = int64(math.MaxInt64)
		for k:=j+1; k<n; k++{
			if s[j] < s[k] && c[k] < minCost2{
				minCost2 = c[k]
			}
		}

		if minCost1 == math.MaxInt64 || minCost2 == math.MaxInt64 {
			continue;
		}

		tmpTotal := minCost1 + c[j] + minCost2
		if totalCost > tmpTotal {
			totalCost = tmpTotal
		}
	}
	if totalCost == int64(math.MaxInt64) {
		fmt.Println("-1")
		return
	}
	fmt.Printf("%v\n", totalCost)
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	s := make([]int, n)
	c := make([]int, n)
	fmt.Scanf("\n")
	for i := 0; i < n; i++ {
		var value int
		fmt.Scanf("%d", &value)
		s[i] = value
	}
	fmt.Scanf("\n")
	for i := 0; i < n; i++ {
		var value int
		fmt.Scanf("%d", &value)
		c[i] = value
	}

	mincost1 := make([]int, n)
	mincost2 := make([]int, n)
	x := make([]int, n)
	for i := 0; i < n; i++ {
		x[i] = 1
		mincost1[i] = c[i]
		mincost2[i] = 2147483647
		if i > 0 && mincost1[i] > mincost1[i-1] {
			mincost1[i] = mincost1[i-1]
		}
	}

	min := 2147483647
	for i := 1; i < n; i++ {
		for j := 0; j < i; j++ {
			if s[i] > s[j] && x[i] < x[j]+1 {
				x[i] = x[j] + 1
				if x[i] == 2 {
					if c[i]+mincost1[j] < mincost2[i] {
						mincost2[i] = c[i] + mincost1[j]
					}
					x[i] = 1
				}
			}
		}
	}
	for i := 0; i < n; i++ {
		x[i] = 1
	}

	for i := 1; i < n; i++ {
		for j := 0; j < i; j++ {
			if s[i] > s[j] && x[i] < x[j]+1 {
				x[i] = x[j] + 1
				if x[i] == 3 {
					if c[i]+mincost2[j] < min {
						min = c[i] + mincost2[j]
					}
					x[i] = 2
				}
			}
		}
	}
	if min == 2147483647 {
		fmt.Println(-1)
		return
	}
	fmt.Println(min)
}

package main

import (
	"fmt"
)

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

	mincost1 := make([]int, n)
	mincost2 := make([]int, n)
	x := make([]int, n)
	for i := 0; i < n; i++ {
		x[i] = 1
		mincost1[i] = c[i]
		mincost2[i] = 2147483647
		if i > 0 && mincost1[i] > mincost1[i-1] {
			mincost1[i] = mincost1[i-1]
		}
	}

	min := 2147483647
	for i := 1; i < n; i++ {
		for j := 0; j < i; j++ {
			if s[i] > s[j] && x[i] < x[j]+1 {
				x[i] = x[j] + 1
				if x[i] == 2 {
					if c[i]+mincost1[j] < mincost2[i] {
						mincost2[i] = c[i] + mincost1[j]
					}
					x[i] = 1
				}
			}
		}
	}
	for i := 0; i < n; i++ {
		x[i] = 1
	}

	for i := 1; i < n; i++ {
		for j := 0; j < i; j++ {
			if s[i] > s[j] && x[i] < x[j]+1 {
				x[i] = x[j] + 1
				if x[i] == 3 {
					if c[i]+mincost2[j] < min {
						min = c[i] + mincost2[j]
					}
					x[i] = 2
				}
			}
		}
	}
	if min == 2147483647 {
		fmt.Println(-1)
		return
	}
	fmt.Println(min)
}

package main

import (
	"fmt"
	"math"
)

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

	mincost2 := make([]int, n)
	x := make([]int, n)
	for i := 0; i < n; i++ {
		x[i] = 1
		mincost2[i] = math.MaxInt32
	}

	min := math.MaxInt32
	for i := 1; i < n; i++ {
		for j := 0; j < i; j++ {
			if s[i] > s[j] && x[i] < x[j]+1 {
				x[i] = x[j] + 1
				if x[i] == 2 {
					if c[i]+c[j] < mincost2[i] {
						mincost2[i] = c[i] + c[j]
					}
				}
				if x[i] == 3 {
					if c[i]+mincost2[j] < min {
						min = c[i] + mincost2[j]
					}
					x[i] = 2
				}
			}
		}
	}
	if min == math.MaxInt32 {
		fmt.Println(-1)
		return
	}
	fmt.Println(min)
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	s := make([]int, n)
	c := make([]int, n)
	for i := 0; i < n; i++ {
		var value int
		fmt.Scanf("%d", &value)
		s[i] = value
	}
	for i := 0; i < n; i++ {
		var value int
		fmt.Scanf("%d", &value)
		c[i] = value
	}

	var min int
	min = 2147483647
	for i := 2; i < n; i++ {
		for j := 1; j < i; j++ {
			for k := 0; k < j; k++ {
				if s[i] > s[j] && s[j] > s[k] {
					if min > c[i]+c[j]+c[k] {
						min = c[i] + c[j] + c[k]
					}
				}
			}
		}
	}
	fmt.Println(min)
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	s := make([]int, n)
	c := make([]int, n)
	for i := 0; i < n; i++ {
		var value int
		fmt.Scanf("%d", &value)
		s[i] = value
	}
	for i := 0; i < n; i++ {
		var value int
		fmt.Scanf("%d", &value)
		c[i] = value
	}

	mincost1 := make([]int, n)
	mincost2 := make([]int, n)
	x := make([]int, n)
	for i := 0; i < n; i++ {
		x[i] = 1
		mincost1[i] = c[i]
		mincost2[i] = 2147483647
		if i > 0 && mincost1[i] > mincost1[i-1] {
			mincost1[i] = mincost1[i-1]
		}
	}

	min := 2147483647
	for i := 1; i < n; i++ {
		for j := 0; j < i; j++ {
			if s[i] > s[j] && x[i] < x[j]+1 {
				x[i] = x[j] + 1
				if x[i] == 2 {
					if c[i]+mincost1[j] < mincost2[i] {
						mincost2[i] = c[i] + mincost1[j]
					}
					x[i] = 1
				}
			}
		}
	}
	for i := 0; i < n; i++ {
		x[i] = 1
	}

	for i := 1; i < n; i++ {
		for j := 0; j < i; j++ {
			if s[i] > s[j] && x[i] < x[j]+1 {
				x[i] = x[j] + 1
				if x[i] == 3 {
					if c[i]+mincost2[j] < min {
						min = c[i] + mincost2[j]
					}
					x[i] = 2
				}
			}
		}
	}
	if min == 2147483647 {
		fmt.Println(-1)
		return
	}
	fmt.Println(min)
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)

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

func main() {
	scanner.Split(bufio.ScanWords)
	n := scanNum()
	a, b := make([]int, n), make([]int, n)
	memo := [][]int{[]int{}, b}
	for i := 0; i < n; i++ {
		a[i] = scanNum()
	}
	for i := 0; i < n; i++ {
		b[i] = scanNum()
	}

	for i := 2; i < 4; i++ {
		memo = append(memo, make([]int, n))
		for j := 0; j < n; j++ {
			memo[i][j] = math.MaxInt32
		}
		for j := 0; j <= (n - i); j++ {
			best := math.MaxInt32

			for k := j + 1; k < n; k++ {
				if (a[k] > a[j]) && (best > (b[j] + memo[i-1][k])) {
					best = b[j] + memo[i-1][k]
				}
			}

			memo[i][j] = best
		}
	}

	sort.Ints(memo[3])
	if memo[3][0] == math.MaxInt32 {
		fmt.Println(-1)
	} else {
		fmt.Println(memo[3][0])
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)
const EPS = 0.00000001

func nextLine() string{
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

type Data struct {
	index int
	//font int
	cost int
}

// x: fontが大きくなるならx[i]にdataを追加していく
// i: 0~nについて、x[i] -> cost最小のdata -> x[dataのi] -> cost最小の....を3回やって最小を出す

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

	n := nextInt()

	s := make([]int, n)
	c := make([]int, n)

	for i:=0; i<n; i++{
		s[i] = nextInt()
	}
	for i:=0; i<n; i++{
		c[i] = nextInt()
	}

	var totalCost int = math.MaxInt32
	for j :=1; j<n-1; j++{
		var minCost1 int = math.MaxInt32
		for i:=0; i<j; i++{
			if s[i] < s[j] && c[i] < minCost1 {
				minCost1 = c[i]
			}
		}
		var minCost2 int = math.MaxInt32
		for k:=j+1; k<n; k++{
			if s[j] < s[k] && c[k] < minCost2{
				minCost2 = c[k]
			}
		}
		tmpTotal := minCost1 + c[j] + minCost2
		if totalCost > tmpTotal {
			totalCost = tmpTotal
		}
	}
	if totalCost == math.MaxInt32 {
		fmt.Println("-1")
		return
	}
	fmt.Printf("%v\n", totalCost)
}


Bob has a simple undirected connected graph (without self-loops and multiple edges). He wants to learn whether his graph is bipartite (that is, you can paint all vertices of the graph into two colors so that there is no edge connecting two vertices of the same color) or not. As he is not very good at programming, he asked Alice for help. He does not want to disclose his graph to Alice, but he agreed that Alice can ask him some questions about the graph.The only question that Alice can ask is the following: she sends $$$s$$$ — a subset of vertices of the original graph. Bob answers with the number of edges that have both endpoints in $$$s$$$. Since he doesn't want Alice to learn too much about the graph, he allows her to ask no more than $$$20000$$$ questions. Furthermore, he suspects that Alice might introduce false messages to their communication channel, so when Alice finally tells him whether the graph is bipartite or not, she also needs to provide a proof — either the partitions themselves or a cycle of odd length.Your task is to help Alice to construct the queries, find whether the graph is bipartite.

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 600$$$) — the number of vertices in Bob's graph.

NoteIn the first case, Alice learns that there are $$$4$$$ edges in the whole graph. Over the course of the next three queries, she learns that vertex $$$1$$$ has two neighbors: $$$3$$$ and $$$4$$$. She then learns that while vertex $$$2$$$ is adjacent to $$$4$$$, the vertex $$$3$$$ isn't adjacent to $$$4$$$. There is only one option for the remaining edge, and that is $$$(2, 3)$$$. This means that the graph is a cycle on four vertices, with $$$(1, 2)$$$ being one partition and $$$(3, 4)$$$ being the second. Here, it would be also valid to output "3 4" on the second line.In the second case, we also have a graph on four vertices and four edges. In the second query, Alice learns that there are three edges among vertices $$$(1, 2, 4)$$$. The only way this could possibly happen is that those form a triangle. As the triangle is not bipartite, Alice can report it as a proof. Notice that she does not learn where the fourth edge is, but she is able to answer Bob correctly anyway.

package main

import (
	"bytes"
	"fmt"
)

var cache = make(map[[10]int64]int, 20000)

func printNodes(typ byte, nodes []int) {
	var q bytes.Buffer
	fmt.Fprintf(&q, "%c %d\n", typ, len(nodes))
	for i := range nodes {
		if i+1 == len(nodes) {
			fmt.Fprintln(&q, nodes[i])
		} else {
			fmt.Fprint(&q, nodes[i], " ")
		}
	}
	fmt.Printf("%s", &q)
}

func query(nodes []int) int {
	if len(nodes) <= 1 {
		return 0
	}
	var key [10]int64
	for _, n := range nodes {
		n--
		key[n/60] |= 1 << uint(n%60)
	}
	res, ok := cache[key]
	if ok {
		return res
	}
	printNodes('?', nodes)
	fmt.Scan(&res)
	cache[key] = res
	return res
}

func numEdgesBetween(a, b []int) int {
	both := make([]int, len(a)+len(b))
	copy(both, a)
	copy(both[len(a):], b)
	return query(both) - query(a) - query(b)
}

func findEdge(a, b []int) (int, int, bool) {
	if numEdgesBetween(a, b) == 0 {
		return 0, 0, false
	}
	for len(b) > 1 {
		if numEdgesBetween(a, b[:len(b)/2]) == 0 {
			b = b[len(b)/2:]
		} else {
			b = b[:len(b)/2]
		}
	}
	for len(a) > 1 {
		if numEdgesBetween(b, a[:len(a)/2]) == 0 {
			a = a[len(a)/2:]
		} else {
			a = a[:len(a)/2]
		}
	}
	return a[0], b[0], true
}

func main() {
	var n int
	fmt.Scan(&n)
	if n <= 2 {
		fmt.Println("Y 1")
		fmt.Println("1")
		return
	}

	edges := make([][]int, n+1)
	s := []int{1}
	notS := make([]int, n-1)
	for i := range notS {
		notS[i] = i + 2
	}
	for len(s) != n {
		u, v, ok := findEdge(s, notS)
		if !ok {
			panic("nejungus?")
		}
		edges[u] = append(edges[u], v)
		edges[v] = append(edges[v], u)
		for i := range notS {
			if notS[i] == v {
				s = append(s, v)
				notS = append(notS[:i], notS[i+1:]...)
				break
			}
		}
	}

	partition := make([]int, n+1)
	var dfs func(int, bool)
	dfs = func(v int, first bool) {
		if first {
			partition[v] = 1
		} else {
			partition[v] = 2
		}
		for _, u := range edges[v] {
			if partition[u] == 0 {
				dfs(u, !first)
			}
		}
	}
	dfs(1, false)
	var first, second []int
	for i := 1; i <= n; i++ {
		switch partition[i] {
		case 1:
			first = append(first, i)
		case 2:
			second = append(second, i)
		default:
			panic(partition[i])
		}
	}
	if query(first) == 0 && query(second) == 0 {
		printNodes('Y', first)
		return
	}

	withCycle := first
	if query(first) == 0 {
		withCycle = second
	}
	for {
		if query(withCycle[len(withCycle)/2:]) != 0 {
			withCycle = withCycle[len(withCycle)/2:]
			continue
		}
		if query(withCycle[:len(withCycle)/2]) != 0 {
			withCycle = withCycle[:len(withCycle)/2]
			continue
		}
		break
	}

	u, v, ok := findEdge(withCycle[:len(withCycle)/2], withCycle[len(withCycle)/2:])
	if !ok {
		panic("oops")
	}
	edges[u] = append(edges[u], v)
	edges[v] = append(edges[v], u)

	cycle := []int{v}
	var findCycle func(int, int) bool
	findCycle = func(v, p int) bool {
		if v == cycle[0] {
			return true
		}
		for _, u := range edges[v] {
			if u == p {
				continue
			}
			if findCycle(u, v) {
				cycle = append(cycle, v)
				return true
			}
		}
		return false
	}
	findCycle(u, v)
	printNodes('N', cycle)
}


Recently Vasya got interested in finding extra-terrestrial intelligence. He made a simple extra-terrestrial signals’ receiver and was keeping a record of the signals for n days in a row. Each of those n days Vasya wrote a 1 in his notebook if he had received a signal that day and a 0 if he hadn’t. Vasya thinks that he has found extra-terrestrial intelligence if there is a system in the way the signals has been received, i.e. if all the intervals between successive signals are equal. Otherwise, Vasya thinks that the signals were sent by some stupid aliens no one cares about. Help Vasya to deduce from the information given by the receiver if he has found extra-terrestrial intelligence or not.

The first line contains integer n (3 ≤ n ≤ 100) — amount of days during which Vasya checked if there were any signals. The second line contains n characters 1 or 0 — the record Vasya kept each of those n days. It’s guaranteed that the given record sequence contains at least three 1s.

If Vasya has found extra-terrestrial intelligence, output YES, otherwise output NO.

package main
import "fmt"
import "os"

func main(){
  var(
    s string
    i,j int
    old int = 0
    ok bool = true
  )
  fin,_:=os.Open("input.txt")
  fout,_:=os.Create("output.txt")
  defer func(){
    fin.Close()
    fout.Close()
  }()
  fmt.Fscanf(fin,"%s\n%s",&s,&s)
  for i=0;s[i]==byte('0')&&i<len(s);i++{}
  for i<len(s) {
    j = i
    for i++;i<len(s)&&byte('0')==s[i];i++ {}
    if i>=len(s) {
      break
    } else if 0!=old&&old!=i-j {
      ok=false
      break
    }
    old=i-j
  }
  fmt.Fprint(fout,map[bool]string{true:"YES\n",false:"NO\n"}[ok])
}

package main

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

func main() {
    file, err := os.Open("input.txt")

    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()
    resp, errC := os.Create("output.txt")
    if errC != nil {
        log.Fatal(errC)
    }
    defer resp.Close()

    scanner := bufio.NewScanner(file)
    var i int = 0;
    var n , m string
    for scanner.Scan() {
        if(i == 0){
            n = scanner.Text()
        }else if(i == 1){
            m = scanner.Text()
            break;
        }
        i++;
    }

    var x int;
    nInt, err := strconv.Atoi(n)
    var varS int = 0
    var aux0 int = 0
    var flag bool = false;
    var first bool = true;
    var count bool = false;
    for x = 0; x < nInt; x++{
        if(m[x:x+1] == "1"){
            if(count){
                if(first){
                    varS = aux0
                    first = false
                }else{
                    if(varS != aux0){
                        _, err := resp.WriteString("NO");
                        _ = err
                        fmt.Println("output.txt")
                        return;
                    }
                }
                aux0 = 0;
            }else{
                count = true
            }
            flag = true;
        }else{
            if(flag){
                count = true
                aux0++
            }
        }

    }
     _, errF := resp.WriteString("YES");
        _ = errF
        fmt.Println("output.txt")

}