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

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";"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"
	"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)

	}
}


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


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


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

}


package main

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

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func out(x ...interface{}) {
	fmt.Fprint(wr, x...)
}

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

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

func nextInt64() int64 {
	i, e := strconv.ParseInt(next(), 10, 64)
	if e != nil {
		panic(e)
	}
	return i
}

func main() {
	defer wr.Flush()
	sc.Buffer(make([]byte, 0), 1000000009)
	sc.Split(bufio.ScanWords)

	t := nextInt()
	for i := 0; i < t; i++ {
		solve()
	}
}

func solve() {
	n := nextInt()

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

	for i := 0; i < n; i++ {
		l := nextInt()
		b := next()
		for j := 0; j < l; j++ {

			if b[j] == 'U' {
				a[i]--
				a[i] += 10
				a[i] %= 10
			}
			if b[j] == 'D' {
				a[i]++
				a[i] += 10
				a[i] %= 10
			}
		}
	}
	for i := 0; i < n; i++ {
		out(a[i])
		if i == n-1 {
			out("\n")
		} else {
			out(" ")
		}
	}
}

package main

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

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

func Read[T any]() T{
	var i T
	fmt.Fscan(in, &i)
	return i
}

func ReadArray[T any](N int) []T{
	ans := make([]T, N)
	for i := 0; i < N; i++{
		ans[i] = Read[T]()
	}
	return ans
}

func solve() {
	n := Read[int]()
	arr := ReadArray[int](n)
	var s string
	for i := 0; i < n; i++ {
		k := Read[int]()
		s = Read[string]()
		for j := 0; j < k; j++ {
			if s[j] == 'U' {
				arr[i]--;
				if arr[i] == -1 {
					arr[i] = 9
				}
			} else {
				arr[i]++
				if arr[i] == 10 {
					arr[i] = 0
				}
			}
		}
	}
	for i := 0; i < n; i++ {
		fmt.Print(arr[i], " ")
	}
	fmt.Println()
}
func main(){
	defer out.Flush()
	tt := 1
	tt = Read[int]()
	for tt > 0 {
		solve()
		tt--
	}
}

package main

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

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

func Read[T interface{}]() T{
	var i T
	fmt.Fscan(in, &i)
	return i
}

func ReadArray[T interface{}](N int) []T{
	ans := make([]T, N)
	for i := 0; i < N; i++{
		ans[i] = Read[T]()
	}
	return ans
}

func solve() {
	n := Read[int]()
	arr := ReadArray[int](n)
	var s string
	for i := 0; i < n; i++ {
		k := Read[int]()
		s = Read[string]()
		for j := 0; j < k; j++ {
			if s[j] == 'U' {
				arr[i]--;
				if arr[i] == -1 {
					arr[i] = 9
				}
			} else {
				arr[i]++
				if arr[i] == 10 {
					arr[i] = 0
				}
			}
		}
	}
	for i := 0; i < n; i++ {
		fmt.Print(arr[i], " ")
	}
	fmt.Println()
}
func main(){
	defer out.Flush()
	tt := 1
	tt = Read[int]()
	for tt > 0 {
		solve()
		tt--
	}
}

package main

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

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

func ReadInt() int{
	var i int
	fmt.Fscan(in, &i)
	return i
}

func ReadIntArray(N int) []int{
	ans := make([]int, N)
	for i := 0; i < N; i++{
		ans[i] = ReadInt()
	}
	return ans
}

func ReadString() string{
	var s string
	fmt.Fscan(in, &s)
	return s
}

func solve() {
	n := ReadInt()
	arr := ReadIntArray(n)
	var s string
	for i := 0; i < n; i++ {
		k := ReadInt()
		s = ReadString()
		for j := 0; j < k; j++ {
			if s[j] == 'U' {
				arr[i]--;
				if arr[i] == -1 {
					arr[i] = 9
				}
			} else {
				arr[i]++
				if arr[i] == 10 {
					arr[i] = 0
				}
			}
		}
	}
	for i := 0; i < n; i++ {
		fmt.Print(arr[i], " ")
	}
	fmt.Println()
}
func main(){
	tt := ReadInt()
	for tt >= 0 {
		solve()
		tt--
	}
}

// https://codeforces.com/problemset/problem/1703/C

// 1703C - Cypher

// Luca has a cypher made up of a sequence of n wheels, each with a digit ai written on it. On the i-th wheel, he made bi moves. Each move is one of two types:
//  • up move (denoted by U): it increases the i-th digit by 1. After applying the up move on 9, it becomes 0.
//  • down move (denoted by D): it decreases the i-th digit by 1. After applying the down move on 0, it becomes 9.
// Luca knows the final sequence of wheels and the moves for each wheel. Help him find the original sequence and crack the cypher.

// Input
// The first line contains a single integer t (1≤t≤100) — the number of test cases.
// The first line of each test case contains a single integer n (1≤n≤100) — the number of wheels.
// The second line contains n integers ai (0≤ai≤9) — the digit shown on the i-th wheel after all moves have been performed.
// Then n lines follow, the i-th of which contains the integer bi (1≤bi≤10) and bi characters that are either U or D — the number of moves performed on the i-th wheel, and the moves performed. U and D represent an up move and a down move respectively.

// Output
// For each test case, output n space-separated digits  — the initial sequence of the cypher.

// Example
// input
// 3
// 3
// 9 3 1
// 3 DDD
// 4 UDUU
// 2 DU
// 2
// 0 9
// 9 DDDDDDDDD
// 9 UUUUUUUUU
// 5
// 0 5 9 8 3
// 10 UUUUUUUUUU
// 3 UUD
// 8 UUDUUDDD
// 10 UUDUUDUDDU
// 4 UUUU
// output
// 2 1 1
// 9 0
// 0 4 9 6 9

// Note

// In the first test case, we can prove that initial sequence was [2,1,1]. In that case, the following moves were performed:
//  • On the first wheel: 2 →D 1 →D 0 →D 9.
//  • On the second wheel: 1 →U 2 →D 1 →U 2 →U 3.
//  • On the third wheel: 1 →D 0 →U 1.
// The final sequence was [9,3,1], which matches the input.

// Tutorial

// We will perform each move in reverse from the final sequence of the cypher.
//  • down move: it increases the i-th digit by 1. After applying the up move on 9, it becomes 0.
//  • up move (denoted by D): it decreases the i-th digit by 1. After applying the down move on 0, it becomes 9.
// Now we just need to implement the two types of moves. The time complexity is O(n+∑ai) per test case.

package main

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

func main() {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	var c int
	var t, n, b, i, j uint
	var a []int
	var s string

	for c, _ = fmt.Fscan(reader, &t); c == 1; c, _ = fmt.Fscan(reader, &t) {
		for ; t > 0; t-- {
			fmt.Fscan(reader, &n)
			a = make([]int, n)

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

			for i = 0; i < n; i++ {
				fmt.Fscan(reader, &b)

				if b > 0 {
					fmt.Fscan(reader, &s)

					for j = 0; j < b; j++ {
						if s[j] == 'U' {
							a[i]--
						} else if s[j] == 'D' {
							a[i]++
						}

						if a[i] < 0 {
							a[i] += 10
						}
						if a[i] > 9 {
							a[i] -= 10
						}
					}
				}
			}

			for i = 0; i+1 < n; i++ {
				fmt.Fprint(writer, a[i], " ")
			}

			fmt.Fprintln(writer, a[n-1])
		}
	}

	writer.Flush()
}

package main

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

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

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

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

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

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

/**
TO SEND
Gopherbots Tipo A ez pendiente de modificar tiempos
Run solve the problem CF1703C
Date: 14/07/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1703/C
Problem: CF1703C
**/
func (in *CF1703C) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		w := make([]int, n)
		for i := 0; i < n; i++ {
			w[i] = in.NextInt()
		}

		ans := make([]int, n)
		for i := 0; i < n; i++ {
			in.NextString()
			str := in.NextString()
			ans[i] = w[i]
			for j := 0; j < len(str); j++ {
				if str[j] == 'D' {
					ans[i]= (ans[i]+1)%10
				}else {
					ans[i] = ((10+ans[i])-1)%10
				}
			}
		}

		for i:=0;i<n;i++{
			fmt.Print(ans[i], " ")
		}
		fmt.Println()
	}
}

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

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

package main

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

func readArray(n int, t *bufio.Reader) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(t, &a[i])
	}
	return a
}

func count(t *bufio.Reader) {
	var n int
	fmt.Fscan(t, &n)
	a := readArray(n, t)
	for i := 0; i < n; i++ {
		var k int
		fmt.Fscan(t, &k)
		var s string
		fmt.Fscan(t, &s)
		balance := 0
		for _, ch := range s {
			if ch == 'U' {
				balance--
			} else {
				balance++
			}
		}
		r := (a[i] + balance) % 10
		if r < 0 {
			r += 10
		}
		a[i] = r
	}
	for _, itm := range a {
		fmt.Printf("%d ", itm)
	}
	fmt.Println()
}

func main() {
	// f, _ := os.Open("input.txt")
	t := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(t, &n)
	for i := 0; i < n; i++ {
		count(t)
	}
}

package main
import(
    "fmt"
)
func main() {
	var t int
	fmt.Scan(&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Scan(&n)
		var list []int = make([]int,n)
		for j:=0;j<n;j++{
			fmt.Scan(&list[j])
		}
		for k:=0;k<n;k++{
			var x int
			var moves string
			fmt.Scan(&x,&moves)
			counter:=0
			for j:=0;j<x;j++{
				if string(moves[j]) == "D" {
					counter++
				} else {
					counter--
				}
			}
			if list[k]+counter > 9 {
				fmt.Printf("%v ",list[k]+counter-10)
			} else if list[k]+counter < 0 {
				fmt.Printf("%v ",list[k]+counter+10)
			} else {
				fmt.Printf("%v ",list[k]+counter)
			}
			fmt.Println()
		}
	}
}
func abs(x int) int {
	var result int
	if x > 0 {
		result = x
	} else {
		if x < 0 {
			result = x*-1
		}
	}
	return result
}

package main

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

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

	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		A := readNNums(reader, n)
		moves := make([]string, n)
		for i := 0; i < n; i++ {
			s := readString(reader)
			if s[1] == ' ' {
				moves[i] = s[2:]
			} else {
				moves[i] = s[3:]
			}
		}
		res := solve(n, A, moves)
		for i := 0; i < n; i++ {
			buf.WriteString(fmt.Sprintf("%d ", res[i]))
		}
		buf.WriteByte('\n')
	}

	fmt.Print(buf.String())
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' || s[i] == '\r' {
			return s[:i]
		}
	}
	return s
}

func normalize(s string) string {

	for i := len(s); i > 0; i-- {
		if s[i-1] >= 'a' && s[i-1] <= 'z' {
			return s[:i]
		}
	}
	return ""
}

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

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

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

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

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

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

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

	return i
}

func solve(n int, A []int, moves []string) []int {
	for i := 0; i < n; i++ {
		var cnt int
		for j := 0; j < len(moves[i]); j++ {
			if moves[i][j] == 'U' {
				cnt--
			} else {
				cnt++
			}
		}
		cnt = (cnt%10 + 10) % 10
		A[i] = (A[i] + cnt) % 10
	}

	return A
}

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 i := 0; i < t; i++ {
		var n, c int
		var s string
		fmt.Fscan(in, &n)
		var a [100]int

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

		for j := 0; j < n; j++ {
			fmt.Fscan(in, &c)
			fmt.Fscan(in, &s)
			for k := 0; k < c; k++ {
				if s[k] == 'U' {
					a[j]--
				} else {
					a[j]++
				}
			}
		}

		for j := 0; j < n; j++ {
			fmt.Fprintf(out, "%d ", (a[j]+10)%10)
		}
		fmt.Fprintln(out, "")
	}

}

package main

import (
	"fmt"
)

func main() {
	var T, n, m int
	fmt.Scan(&T)
	for T>0 {
		fmt.Scan(&n)
		d:=make([]int,n,n)
		for i:=0;i<n;i++{
			fmt.Scan(&d[i])
		}
		var s string
		for i:=0;i<n;i++{
			fmt.Scan(&m,&s)
			for j:=0;j<m;j++{
				if s[j]=='U'{
					d[i]=(d[i]+9)%10
				} else {
					d[i]=(d[i]+1)%10
				}
			}
			fmt.Print(d[i]," ")
		}
		fmt.Println()
		T--
	}

}

package main

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

var (
	reader  = bufio.NewReader(os.Stdin)
	t, n, x int
	a       []int
	b       []string
)

func solve(n int, a []int, b []string) string {
	var result = make([]int, n)
	for i := 0; i < n; i++ {
		var moved int
		for _, c := range b[i] {
			if c == 'D' {
				moved++
			} else {
				moved--
			}
		}
		result[i] = (a[i] + moved) % 10
		if result[i] < 0 {
			result[i] += 10
		}
	}
	return strings.Trim(fmt.Sprint(result), "[]")
}

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

package main
import "fmt"

func main() {
    var T int
    fmt.Scan(&T)
    for tt := 0; tt < T; tt++ {
        var n int
        fmt.Scan(&n)
        a := make([]int, n)
        for i := 0; i < n; i++ {
            fmt.Scan(&a[i])
        }
        for i := 0; i < n; i++ {
            var l int
            var s string
            fmt.Scan(&l)
            fmt.Scan(&s)
            for j := 0; j < l; j++ {
                if s[j] == 'U' {
                    a[i] = (a[i] + 9) % 10
                } else {
                    a[i] = (a[i] + 1) % 10
                }
            }
        }
        for i := 0; i < n; i++ {
            fmt.Printf("%d ", a[i])
        }
        fmt.Println()
    }
}

package main

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

const mod = 998244353
const mx = int64(1e9 + 7)

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &arr[i])
	}
	for i := 0; i < n; i++ {
		var l int
		fmt.Fscan(in, &l)
		var s string
		fmt.Fscan(in, &s)
		for j := 0; j < l; j++ {
			arr[i] += 10
			if s[j] != 'U' {
				arr[i]++
			} else {
				arr[i]--
			}
			arr[i] %= 10
		}
	}
	for i := 0; i < n; i++ {
		fmt.Fprintf(out, "%d ", arr[i])
	}
	fmt.Fprintln(out)
}

func abs(a int64) int64 {
	if a > 0 {
		return a
	}
	return -a
}
func sum(a, b int64) int64 {
	return (b + a) * (b - a + 1) / 2
}

func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}
func gcd(a, b int) int {
	if a%b == 0 {
		return b
	}
	return gcd(b, a%b)
}

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

package main

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

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

func ReadInt() int{
	var i int
	fmt.Fscan(in, &i)
	return i
}

func ReadIntArray(N int) []int{
	ans := make([]int, N)
	for i := 0; i < N; i++{
		ans[i] = ReadInt()
	}
	return ans
}

func ReadString() string{
	var s string
	fmt.Fscan(in, &s)
	return s
}

func main(){
	defer out.Flush()
	testCases := ReadInt()
	for ; testCases > 0; testCases--{
		N := ReadInt()
		A := ReadIntArray(N)
		for i := 0; i < N; i++{
			l := ReadInt()
			s := ReadString()
			add := 0
			for j := 0; j < l; j++{
				if s[j] == 'U'{
					add++
				} else{
					add--
				}
			}
			fmt.Fprintf(out, "%d ", (A[i] - add + 10) % 10)
		}
		fmt.Fprintln(out)
	}
}

package main

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

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

func slove() {
	var n int
	fmt.Fscan(rr, &n)
	nums := make([]int, n)
	for i:=0;i<len(nums);i++ {
		fmt.Fscan(rr, &nums[i])
	}
	for i:=0;i<n;i++ {
		var m int
		fmt.Fscan(rr, &m)
		var s string
		fmt.Fscan(rr, &s)
		for j := range s {
			if s[j] == 'D' {
				nums[i]++
			} else {
				nums[i]--
			}
		}
		nums[i] += 1000
		nums[i] %= 10
	}
	for i := range nums {
		fmt.Fprintf(ww, "%d ", nums[i])
	}
	fmt.Fprintln(ww)
}

func main() {
	defer ww.Flush()
	var n int
	fmt.Fscan(rr, &n)
	for i:=1;i<=n;i++ {
		slove()
	}
}

package main

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

// https://space.bilibili.com/206214
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod = 10

	var T, n, m int
	var s string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		for _, v := range a {
			Fscan(in, &m, &s)
			v += strings.Count(s, "D")*2 - m
			v = (v%mod + mod) % mod
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}
}

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

package main

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

func main() {
	var (
		t      int
		n      int
		ai     []int
		wheel  []int
		swheel []string
		temp   int
		temps  string
	)
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanf(reader, "%d\n", &t)

	for i := 0; i < t; i++ {
		// taking input line 1
		fmt.Fscanf(reader, "%d\n", &n)
		ai = make([]int, n)
		wheel = make([]int, n)
		swheel = make([]string, n)

		// input line 2
		for j := 0; j < n; j++ {
			fmt.Fscanf(reader, "%d", &temp)
			ai[j] = temp
		}
		fmt.Fscanf(reader, "\n")

		// input n lines
		for j := 0; j < n; j++ {
			fmt.Fscanf(reader, "%d ", &temp)
			fmt.Fscanf(reader, "%s\n", &temps)
			wheel[j] = temp
			swheel[j] = temps
		}

		// fmt.Println("ai", ai)
		// fmt.Println( "wheel" , wheel)
		// fmt.Println("swheel", swheel)

		// solving que line by line
		for j := 0; j < n; j++ {
			sarr := strings.Split(swheel[j], "")
			for k := 0; k < wheel[j]; k++ {
				if sarr[k] == "U" {
					ai[j]--
					if ai[j] == -1 {
						ai[j] = 9
					}
				} else {
					ai[j]++
					if ai[j] == 10 {
						ai[j] = 0
					}
				}
			}
		}

		// fmt.Println(ai)
		printArray(ai)
	}
}

func printArray(arr []int) {
	for i := range arr {
		fmt.Printf("%d ",arr[i])
	}
	fmt.Println()
}

package main

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

func unlock(combinations []int, moves []string) []int {
	for i := 0; i < len(combinations); i++ {
		for j := 0; j < len(moves[i]); j++ {
			move := moves[i][j]
			if move == 'D' {
				combinations[i]++
				if combinations[i] > 9 {
					combinations[i] = 0
				}
			} else {
				combinations[i]--
				if combinations[i] < 0 {
					combinations[i] = 9
				}
			}
		}
	}
	return combinations
}

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 16*1024*1024)
	T, err := strconv.Atoi(strings.TrimSpace(readLine(reader)))
	checkError(err)
	var results = make([][]int, T)
	for i := 0; i < T; i++ {
		N, err := strconv.Atoi(strings.TrimSpace(readLine(reader)))
		checkError(err)
		tmpComb := strings.Split(strings.TrimSpace(readLine(reader)), " ")
		combinations := make([]int, N)
		for i := 0; i < N; i++ {
			item, err := strconv.Atoi(tmpComb[i])
			checkError(err)
			combinations[i] = item
		}
		moves := make([]string, N)
		for i := 0; i < N; i++ {
			tmp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
			moves[i] = tmp[1]
		}
		results[i] = unlock(combinations, moves)
	}
	for _, result := range results {
		for _, r := range result {
			fmt.Print(r, " ")
		}
		fmt.Println()
	}
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err != nil {
		return ""
	}
	return strings.TrimRight(string(str), "\r\n")
}

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

//package main
//
//import (
//	"context"
//	"gerRpc"
//	"gerRpc/xclient"
//	"log"
//	"sync"
//
//	"net"
//	"time"
//)
//
////
////func startServer(addr chan string)  {
////	l, err := net.Listen("tcp", ":0")
////	if err != nil{
////		log.Fatal("network error:", err)
////	}
////	log.Println("start rpc server on:", l.Addr())
////	addr <- l.Addr().String()
////	gerRpc.Accept(l)
////}
////
////func main()  {
////
////	//addr := make(chan string)
////	//go startServer(addr)
////	//client, _ := gerRpc.Dial("tcp", <-addr)
////	//defer func() {_ = client.Close()}()
////
////	//time.Sleep(time.Second)
////	//var wg sync.WaitGroup
////	//for i := 0; i < 5; i ++{
////	//	wg.Add(1)
////	//	go func(i int) {
////	//		defer wg.Done()
////	//		args := fmt.Sprintf("geerpc req %d", i)
////	//		var reply string
////	//		if err := client.Call("Foo.sum", args, &reply); err != nil{
////	//			log.Fatal("call Foo.Sum error:", err)
////	//		}
////	//		log.Println("reply")
////	//	}(i)
////	//}
////	//wg.Wait()
////}
//
////
////func main() {
////	addr := make(chan string)
////	go startServer(addr)
////
////	// in fact, following code is like a simple geerpc client
////	conn, _ := net.Dial("tcp", <-addr)
////	defer func() { _ = conn.Close() }()
////
////	time.Sleep(time.Second)
////	// send options
////	_ = json.NewEncoder(conn).Encode(gerRpc.DefaultOption)
////	cc := codec.NewGobCodec(conn)
////	// send request & receive response
////	for i := 0; i < 5; i++ {
////		h := &codec.Header{
////			ServiceMethod: "Foo.Sum",
////			Seq:           uint64(i),
////		}
////		_ = cc.Write(h, fmt.Sprintf("geerpc req %d", h.Seq))
////		_ = cc.ReadHeader(h)
////		var reply string
////		_ = cc.ReadBody(&reply)
////		log.Println("reply:", reply)
////	}
////}
////
////
////type Foo int
////
////type Args struct {
////	Num1, Num2 int
////}
////
////func (f Foo)Sum(args Args, reply *int)error  {
////	*reply = args.Num1 + args.Num2
////	return nil
////}
////func startServer(addr chan string) {
////	var foo Foo
////	if err := gerRpc.Register(&foo); err != nil {
////		log.Fatal("register error:", err)
////	}
////	// pick a free port
////	l, err := net.Listen("tcp", ":0")
////	if err != nil {
////		log.Fatal("network error:", err)
////	}
////	log.Println("start rpc server on", l.Addr())
////	addr <- l.Addr().String()
////	gerRpc.Accept(l)
////}
////
////func main()  {
////	log.SetFlags(0)
////	addr := make(chan string)
////	go startServer(addr)
////	client, _ := gerRpc.Dial("tcp", <-addr)
////	defer func() {_ = client.Close()}()
////
////	time.Sleep(time.Second)
////	var wg sync.WaitGroup
////	for i := 0; i < 5; i ++{
////		wg.Add(1)
////		go func(i int) {
////			defer wg.Done()
////			args := &Args{
////				Num1: i,
////				Num2: i * i,
////			}
////			ctx, _ := context.WithTimeout(context.Background(), time.Second)
////			var reply int
////			if err := client.Call(ctx,"Foo.Sum", args, &reply); err != nil{
////				log.Fatal("call Foo.Sum error:", err)
////			}
////			log.Printf("%d + %d = %d", args.Num1, args.Num2, reply)
////		}(i)
////	}
////	wg.Wait()
////}
//type Foo int
//
//type Args struct{ Num1, Num2 int }
//
//func (f Foo) Sum(args Args, reply *int) error {
//	*reply = args.Num1 + args.Num2
//	return nil
//}
//
//func (f Foo) Sleep(args Args, reply *int) error {
//	time.Sleep(time.Second * time.Duration(args.Num1))
//	*reply = args.Num1 + args.Num2
//	return nil
//}
//
//func startServer(addrCh chan string) {
//	var foo Foo
//	l, _ := net.Listen("tcp", ":0")
//	server := gerRpc.NewServer()
//	_ = server.Register(&foo)
//	addrCh <- l.Addr().String()
//	server.Accept(l)
//}
//
//func foo(xc *xclient.Xclient, ctx context.Context, typ, serviceMethod string, args *Args)  {
//	var reply int
//	var err error
//	switch typ {
//	case "call":
//		err = xc.Call(ctx, serviceMethod, args, &reply)
//	case "broadcast":
//		err = xc.Broadcast(ctx, serviceMethod, args, &reply)
//	}
//	if err != nil{
//		log.Printf("%s %s error: %v", typ, serviceMethod, err)
//	}else{
//		log.Printf("%s %s success: %d + %d = %d", typ, serviceMethod, args.Num1, args.Num2, reply)
//	}
//}
//
//func call(addr1, addr2 string)  {
//	d := xclient.NewMultiServerDiscovery([]string{"tcp@"+addr1, "tcp@"+addr2})
//	xc := xclient.NewXClient(d, xclient.RandomSelect, nil)
//	defer func() {_ = xc.Close()}()
//
//	var wg sync.WaitGroup
//	for i := 0; i < 5; i ++{
//		wg.Add(1)
//		go func(i int) {
//			defer wg.Done()
//			foo(xc, context.Background(), "call", "Foo.Sum", &Args{
//				Num1: i,
//				Num2: i * i,
//			})
//		}(i)
//	}
//	wg.Wait()
//}
//
//func broadcast(addr1, addr2 string) {
//	d := xclient.NewMultiServerDiscovery([]string{"tcp@" + addr1, "tcp@" + addr2})
//	xc := xclient.NewXClient(d, xclient.RandomSelect, nil)
//	defer func() { _ = xc.Close() }()
//	var wg sync.WaitGroup
//	for i := 0; i < 5; i++ {
//		wg.Add(1)
//		go func(i int) {
//			defer wg.Done()
//			foo(xc, context.Background(), "broadcast", "Foo.Sum", &Args{Num1: i, Num2: i * i})
//			// expect 2 - 5 timeout
//			ctx, _ := context.WithTimeout(context.Background(), time.Second*2)
//			foo(xc, ctx, "broadcast", "Foo.Sleep", &Args{Num1: i, Num2: i * i})
//		}(i)
//	}
//	wg.Wait()
//}
//
//
//func main() {
//	log.SetFlags(0)
//	ch1 := make(chan string)
//	ch2 := make(chan string)
//	// start two servers
//	go startServer(ch1)
//	go startServer(ch2)
//
//	addr1 := <-ch1
//	addr2 := <-ch2
//
//	time.Sleep(time.Second)
//	call(addr1, addr2)
//	broadcast(addr1, addr2)
//}
//
//package main
//
//import (
//	"bufio"
//	"fmt"
//	"os"
//	"strings"
//)
//
//var in = bufio.NewReader(os.Stdin)
//
//func main()  {
//	var T int
//	fmt.Fscan(in, &T)
//	for ;T > 0; T--{
//		var str string
//		fmt.Fscan(in, &str)
//
//		if strings.EqualFold(strings.ToLower(str), "yes"){
//			fmt.Println("YES")
//		}else{
//			fmt.Println("NO")
//		}
//	}
//}

//
//package main
//
//import (
//	"bufio"
//	"fmt"
//	"os"
//)
//
//var in = bufio.NewReader(os.Stdin)
//
//func main()  {
//	var T int
//	fmt.Fscan(in, &T)
//	for ;T > 0; T--{
//		var n int
//		fmt.Fscan(in, &n)
//		var str string
//		fmt.Fscan(in, &str)
//		mp := make(map[byte]bool)
//		cnt := 0
//		for i := 0; i < n; i ++{
//			if !mp[str[i]] {
//				cnt += 2
//				mp[str[i]] = true
//			}else{
//				cnt ++
//			}
//		}
//		fmt.Println(cnt)
//	}
//}

package main

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

var in = bufio.NewReader(os.Stdin)

func main()  {
	var T int
	fmt.Fscan(in, &T)
	for ;T > 0; T--{
		var n int
		fmt.Fscan(in, &n)
		num := make([]int, n)
		for i := 0; i < n; i ++{
			fmt.Fscan(in, &num[i])
		}
		for i := 0; i < n; i ++{
			var k int
			var str string
			fmt.Fscan(in, &k, &str)
			for j := 0; j < k; j ++{
				if str[j] == 'U'{
					num[i] = ((num[i] - 1) % 10 + 10) % 10
				}else{
					num[i] = ((num[i] + 1) % 10 + 10) % 10
				}
			}
		}
		for _, v := range num{
			fmt.Print(v, " ")
		}
		fmt.Println()
	}
}

package main

import . "fmt"

func main() {
	var (
		b, n, t int
		a       []int
		s, c    string
	)
	Scan(&t)
	for i := 0; i < t; i++ {
		Scan(&n)
		a = make([]int, n)
		for j := 0; j < n; j++ {
			Scan(&a[j])
		}
		for j := 0; j < n; j++ {
			Scan(&b)
			Scan(&s)
			for k := 0; k < b; k++ {
				c = string(s[k])
				if c == "U" {
					if a[j] == 0 {
						a[j] = 9
					} else {
						a[j]--
					}
				} else {
					if a[j] == 9 {
						a[j] = 0
					} else {
						a[j]++
					}
				}
			}
			Printf("%d ", a[j])
		}
		Printf("\n")
	}
}

// 1703C. Cypher
package main

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

func get_val(s string) int {
	ans := 0
	for i := 0; i < len(s); i++ {
		if s[i] == 'U' {
			ans--
		} else {
			ans++
		}
	}
	return ans
}

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		fmt.Fscan(reader, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		f := []interface{}{}
		for i := 0; i < n; i++ {
			var b int
			var s string
			fmt.Fscan(reader, &b, &s)
			f = append(f, (10+a[i]+get_val(s))%10)
		}
		fmt.Println(f...)

	}
}

package main

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

var _, _, _ = main, rw, do

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

func rw(r io.Reader, w io.Writer) {
	var t int
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n int
		fmt.Fscan(r, &n)
		a := make([]int, n)
		for i := range a {
			fmt.Fscan(r, &a[i])
		}
		b := make([]string, n)
		var c int
		for i := range b {
			fmt.Fscan(r, &c, &b[i])
		}
		w1 := do(a, b)
		for i := range a {
			fmt.Fprint(w, w1[i])
			fmt.Fprint(w, " ")
		}
		fmt.Fprintln(w)
	}
}

func do(a []int, b []string) []int {
	for i, s := range b {
		for j := len(s) - 1; j >= 0; j-- {
			if s[j] == 'U' {
				a[i]--
			} else {
				a[i]++
			}
			if a[i] < 0 {
				a[i] = 9
			}
			if a[i] > 9 {
				a[i] = 0
			}
		}
	}
	return a
}

package main

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

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

	var T int
	for fmt.Fscan(in, &T); T > 0; T-- {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n)
		for i := 0;i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		for i := 0;i < n; i++ {
			var len int
			var s string
			fmt.Fscan(in, &len, &s)
			cnt := 0
			for j := 0;j < len; j++ {
				if s[j] == 'U' {
					cnt--
				} else {
					cnt++
				}
			}
			a[i] = ((a[i] + cnt) % 10 + 10) % 10
		}
		for i := 0;i < n; i++ {
			fmt.Fprintf(out, "%d ", a[i])
		}
		fmt.Fprintln(out)
	}
}

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

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)

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

		for i := 0; i < n; i++ {
			var b int
			var s string
			fmt.Fscan(in, &b, &s)

			for _, ch := range s {
				if ch == 'D' {
					aa[i]++
				} else {
					aa[i]--
					if aa[i] < 0 {
						aa[i] = 9
					}
				}
				aa[i] %= 10
			}
		}

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

// https://codeforces.com/problemset/problem/1703/C

// 1703C - Cypher

// Luca has a cypher made up of a sequence of n wheels, each with a digit ai written on it. On the i-th wheel, he made bi moves. Each move is one of two types:
//  • up move (denoted by U): it increases the i-th digit by 1. After applying the up move on 9, it becomes 0.
//  • down move (denoted by D): it decreases the i-th digit by 1. After applying the down move on 0, it becomes 9.
// Luca knows the final sequence of wheels and the moves for each wheel. Help him find the original sequence and crack the cypher.

// Input
// The first line contains a single integer t (1≤t≤100) — the number of test cases.
// The first line of each test case contains a single integer n (1≤n≤100) — the number of wheels.
// The second line contains n integers ai (0≤ai≤9) — the digit shown on the i-th wheel after all moves have been performed.
// Then n lines follow, the i-th of which contains the integer bi (1≤bi≤10) and bi characters that are either U or D — the number of moves performed on the i-th wheel, and the moves performed. U and D represent an up move and a down move respectively.

// Output
// For each test case, output n space-separated digits  — the initial sequence of the cypher.

// Example
// input
// 3
// 3
// 9 3 1
// 3 DDD
// 4 UDUU
// 2 DU
// 2
// 0 9
// 9 DDDDDDDDD
// 9 UUUUUUUUU
// 5
// 0 5 9 8 3
// 10 UUUUUUUUUU
// 3 UUD
// 8 UUDUUDDD
// 10 UUDUUDUDDU
// 4 UUUU
// output
// 2 1 1
// 9 0
// 0 4 9 6 9

// Note

// In the first test case, we can prove that initial sequence was [2,1,1]. In that case, the following moves were performed:
//  • On the first wheel: 2 →D 1 →D 0 →D 9.
//  • On the second wheel: 1 →U 2 →D 1 →U 2 →U 3.
//  • On the third wheel: 1 →D 0 →U 1.
// The final sequence was [9,3,1], which matches the input.

// Tutorial

// We will perform each move in reverse from the final sequence of the cypher.
//  • down move: it increases the i-th digit by 1. After applying the up move on 9, it becomes 0.
//  • up move (denoted by D): it decreases the i-th digit by 1. After applying the down move on 0, it becomes 9.
// Now we just need to implement the two types of moves. The time complexity is O(n+∑ai) per test case.

package main

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

func main() {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	var c int
	var t, n, b, i, j uint
	var a []int
	var s string

	for c, _ = fmt.Fscan(reader, &t); c == 1; c, _ = fmt.Fscan(reader, &t) {
		for ; t > 0; t-- {
			fmt.Fscan(reader, &n)
			a = make([]int, n)

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

			for i = 0; i < n; i++ {
				fmt.Fscan(reader, &b)

				if b > 0 {
					fmt.Fscan(reader, &s)

					for j = 0; j < b; j++ {
						if s[j] == 'U' {
							a[i]--
						} else if s[j] == 'D' {
							a[i]++
						}

						if a[i] < 0 {
							a[i] += 10
						}
						if a[i] > 10 {
							a[i] -= 10
						}
					}
				}
			}

			for i = 0; i+1 < n; i++ {
				fmt.Fprint(writer, a[i], " ")
			}

			fmt.Fprintln(writer, a[n-1])
		}
	}

	writer.Flush()
}

package main

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

func readArray(n int, t *bufio.Reader) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(t, &a[i])
	}
	return a
}

func count(t *bufio.Reader) {
	var n int
	fmt.Fscan(t, &n)
	a := readArray(n, t)
	for i := 0; i < n; i++ {
		var k int
		fmt.Fscan(t, &k)
		var s string
		fmt.Fscan(t, &s)
		balance := 0
		for _, ch := range s {
			if ch == 'U' {
				balance--
			} else {
				balance++
			}
		}
		r := (a[i] + balance) % 10
		if r < 0 {
			r += 10
		}
		a[i] = r
	}
	for _, itm := range a {
		fmt.Printf("%d ", itm)
	}
	fmt.Println()
}

func main() {
	f, _ := os.Open("input.txt")
	t := bufio.NewReader(f) //os.Stdin)
	var n int
	fmt.Fscan(t, &n)
	for i := 0; i < n; i++ {
		count(t)
	}
}