Nastya baked $$$m$$$ pancakes and spread them on $$$n$$$ dishes. The dishes are in a row and numbered from left to right. She put $$$a_i$$$ pancakes on the dish with the index $$$i$$$.Seeing the dishes, Vlad decided to bring order to the stacks and move some pancakes. In one move, he can shift one pancake from any dish to the closest one, that is, select the dish $$$i$$$ ($$$a_i > 0$$$) and do one of the following: if $$$i > 1$$$, put the pancake on a dish with the previous index, after this move $$$a_i = a_i - 1$$$ and $$$a_{i - 1} = a_{i - 1} + 1$$$; if $$$i < n$$$, put the pancake on a dish with the following index, after this move $$$a_i = a_i - 1$$$ and $$$a_{i + 1} = a_{i + 1} + 1$$$.Vlad wants to make the array $$$a$$$non-increasing, after moving as few pancakes as possible. Help him find the minimum number of moves needed for this.The array $$$a=[a_1, a_2,\dots,a_n]$$$ is called non-increasing if $$$a_i \ge a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$.

The first line of the input contains two numbers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 250$$$) — the number of dishes and the number of pancakes, respectively. The second line contains $$$n$$$ numbers $$$a_i$$$ ($$$0 \le a_i \le m$$$), the sum of all $$$a_i$$$ is equal to $$$m$$$.

Print a single number: the minimum number of moves required to make the array $$$a$$$ non-increasing.

NoteIn the first example, you first need to move the pancake from the dish $$$1$$$, after which $$$a = [4, 4, 2, 3, 3, 3]$$$. After that, you need to move the pancake from the dish $$$2$$$ to the dish $$$3$$$ and the array will become non-growing $$$a = [4, 3, 3, 3, 3, 3]$$$.

package main

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

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

var MX int = 300

const MM int = 1000000

var N, M int
var A []int

func gen(n, m, l int, v int) [][][]int {
	dp := make([][][]int, n)
	for i := 0; int(i) < n; i++ {
		dp[i] = make([][]int, m)
		for j := 0; int(j) < m; j++ {
			dp[i][j] = make([]int, l)
		}
	}

	for i := 0; int(i) < n; i++ {
		for j := 0; int(j) < m; j++ {
			for k := 0; int(k) < l; k++ {
				dp[i][j][k] = v
			}
		}
	}
	return dp
}

func solve() int {
	dp_in := gen(2, MX, MX, MM)
	dp_out := gen(2, MX, MX, MM)

	for j := int(0); j+A[0] < MX; j++ {
		dp_in[0][j][j+A[0]] = 0
		for k := int(1); k < MX; k++ {
			dp_in[0][j][k] = min(dp_in[0][j][k-1], dp_in[0][j][k])
		}
	}

	for j := int(0); j <= A[0]; j++ {
		dp_out[0][j][A[0]-j] = 0
		for k := int(1); k < MX; k++ {
			dp_in[0][j][k] = min(dp_in[0][j][k-1], dp_in[0][j][k])
		}
	}

	for i := int(1); i < N; i++ {
		a := A[i]
		for j := int(0); j < MX; j++ {
			for k := int(0); k < MX; k++ {
				// in
				a = A[i] + j
				if a >= k {
					mi := int(MM)
					if a-k < MX {
						mi = min(mi, dp_in[(i-1)%2][a-k][k])
					}
					dp_in[i%2][j][k] = mi + a - k
				} else { // (i-1) is out
					mi := int(MM)
					if k-a < MX {
						mi = min(mi, dp_out[(i-1)%2][k-a][k])
					}
					dp_in[i%2][j][k] = mi + k - a
				}

				//out
				a = A[i] - j
				if a >= k {
					mi := int(MM)
					if a-k < MX {
						mi = min(mi, dp_in[(i-1)%2][a-k][k])
					}
					dp_out[i%2][j][k] = mi + a - k
				} else {
					mi := int(MM)
					if k-a < MX {
						mi = min(mi, dp_out[(i-1)%2][k-a][k])
					}
					dp_out[i%2][j][k] = mi + k - a
				}

			}

			for k := int(1); k < MX; k++ {
				dp_in[i%2][j][k] = min(dp_in[i%2][j][k], dp_in[i%2][j][k-1])
				dp_out[i%2][j][k] = min(dp_out[i%2][j][k], dp_out[i%2][j][k-1])
			}

		}
	}

	res := int(MM)
	for k := int(0); k < MX; k++ {
		res = min(res, dp_in[(N-1)%2][0][k])
		res = min(res, dp_out[(N-1)%2][0][k])
	}

	return res

}

func main() {
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &N, &M)
	MX = M + 1
	A = make([]int, N)
	for i := N - 1; int(i) >= 0; i-- {
		fmt.Fscan(in, &A[i])
	}

	fmt.Println(solve())
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func min(a,b int) int { if a > b { return b }; return a }
const inf  = 1000000000
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	N,M := gi(),gi(); A := gis(N);
	psum := make([]int,N); psum[0] = A[0]; for i:=1;i<N;i++ { psum[i] = psum[i-1] + A[i] }
	parr := make([]int,0,M); for i:=0;i<N;i++ { for j:=0;j<A[i];j++ { parr = append(parr,i) } }
	parrcum := make([]int,M+1); parrcum[0] = 0; for i:=0;i<M;i++ { parrcum[i+1] = parrcum[i] + parr[i] }
	// dp[i][j][k] = cheapest way to fix prefix through i with last term at least j pancakes and prefix sum == k
	dp := twodi(M+1,M+1,inf)
	ndp := twodi(M+1,M+1,inf)
	for j:=0;j<=M;j++ { if psum[0] >= j { dp[j][j] = psum[0]-j } else { dp[j][j] = parrcum[j] } }
	for j:=M-1;j>=0;j-- { for k:=j;k<=M;k++ { dp[j][k] = min(dp[j][k],dp[j+1][k]) } } // fixes the "at least" part
	for i:=1;i<N;i++ {
		for j:=0;j<=M;j++ { for k:=0;k<=M;k++ { ndp[j][k] = inf } }
		for j:=0;j<=M;j++ {
			for k:=j; k<=M; k++ {
				add := 0
				if psum[i] >= k {
					add = psum[i] - k
				} else {
					lend := min(j,k-psum[i])
					add = parrcum[k] - parrcum[k-lend] - i * lend
				}
				ndp[j][k] = dp[j][k-j] + add
			}
		}
		for j:=M-1;j>=0;j-- { for k:=j;k<=M;k++ { ndp[j][k] = min(ndp[j][k],ndp[j+1][k]) } }
		dp,ndp = ndp,dp
	}
	ans := dp[0][M]
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	_ "runtime/pprof"
)

const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	return x
}

func (myio *MyIO) ReadInt64() (x int64) {
	rc := myio.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int64(b&15)
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

var myio *MyIO

const (
	inf = 1 << 30
)

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

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

func sol() {
	var n, m int
	n = myio.ReadInt()
	m = myio.ReadInt()
	a := make([]int, n)
	s := make([]int, n+1)
	s[0] = 0
	for i := 0; i < n; i++ {
		a[i] = myio.ReadInt()
		s[i+1] = s[i] + a[i]
	}
	dp := make([][]int, n+1)
	for i := 0; i <= n; i++ {
		dp[i] = make([]int, m+1)
		for j := 0; j <= m; j++ {
			dp[i][j] = inf
		}
	}
	dp[0][0] = 0
	for k := m; k >= 0; k-- {
		for i := 0; i < n; i++ {
			for j := 0; j <= m-k; j++ {
				if dp[i][j] == inf {
					continue
				}
				var p=j+k
				dp[i+1][p] = min(dp[i+1][p], dp[i][j]+abs(p-s[i+1]))
			}
		}
	}
	myio.Println(dp[n][m])
}

func main() {
	in, out := os.Stdin, os.Stdout
	myio = NewIO(in, out)
	defer myio.Flush()
	sol()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	_ "runtime/pprof"
)

const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	return x
}

func (myio *MyIO) ReadInt64() (x int64) {
	rc := myio.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int64(b&15)
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

var myio *MyIO

const (
	inf = 1 << 30
)

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

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

func sol() {
	var n, m int
	n = myio.ReadInt()
	m = myio.ReadInt()
	a := make([]int, n)
	s := make([]int, n+1)
	s[0] = 0
	for i := 0; i < n; i++ {
		a[i] = myio.ReadInt()
		s[i+1] = s[i] + a[i]
	}
	dp := make([][]int, n+1)
	for i := 0; i <= n; i++ {
		dp[i] = make([]int, m+1)
		for j := 0; j <= m; j++ {
			dp[i][j] = inf
		}
	}
	dp[0][0] = 0
	for k := m; k >= 0; k-- {
		for i := 0; i < n; i++ {
			for j := 0; j <= m-k; j++ {
				if dp[i][j] == inf {
					continue
				}
				dp[i+1][j+k] = min(dp[i+1][j+k], dp[i][j]+abs(j+k-s[i+1]))
			}
		}
	}
	myio.Println(dp[n][m])
}

func main() {
	in, out := os.Stdin, os.Stdout
	myio = NewIO(in, out)
	defer myio.Flush()
	sol()
}

package main

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

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

func nextInt() int {
	rd.Scan()
	if n, err := strconv.Atoi(rd.Text()); err == nil {
		return n
	}
	return 0
}
func Max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}
func Min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func Abs(a int) int {
	if a > 0 {
		return a
	}
	return -a
}
func main() {
	defer wr.Flush()
	rd.Buffer(make([]byte, 0), 1000000009)
	rd.Split(bufio.ScanWords)
	T := 1
	for Case := 0; Case < T; Case++ {
		solve()
	}
}

const N int = 255

var f [N][N][N]int

func solve() {
	for i1 := range f {
		for j := range f[i1] {
			for k := range f[i1][j] {
				f[i1][j][k] = 0x3f3f3f3f
			}
		}
	}
	n, m := nextInt(), nextInt()
	a := make([]int, n+2)
	s := make([]int, n+2)
	for i:=n;i>=1;i-- {
		a[i]=nextInt()
	}
	for i := 1; i <= n; i++ {
		s[i] = s[i-1] + a[i]
	}
	f[0][0][0] = 0
	for i := 1; i <= n; i++ {
		for j := 0; j <= m; j++ {
			minv := 0x3f3f3f3f
			for k := 0; j+k <= m; k++ {
				minv = Min(minv, f[i-1][j][k])
				f[i][j+k][k] = minv + Abs(s[i]-j-k)
			}
		}
	}
	res := 0x3f3f3f3f
	for p := 1; p <= m; p++ {
		res = Min(res, f[n][m][p])
	}
	fmt.Fprint(wr, res, "\n")
}

package main

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

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

const MX int64 = 300

var N, M int64
var A []int64

func gen(n, m, l int64, v int64) [][][]int64 {
	dp := make([][][]int64, n)
	for i := 0; int64(i) < n; i++ {
		dp[i] = make([][]int64, m)
		for j := 0; int64(j) < m; j++ {
			dp[i][j] = make([]int64, l)
		}
	}

	for i := 0; int64(i) < n; i++ {
		for j := 0; int64(j) < m; j++ {
			for k := 0; int64(k) < l; k++ {
				dp[i][j][k] = v
			}
		}
	}
	return dp
}

func solve() int64 {
	dp_in := gen(N, MX, MX, 10000)
	dp_out := gen(N, MX, MX, 10000)

	for j := int64(0); int64(j)+A[0] < MX; j++ {
		dp_in[0][j][j+A[0]] = 0
	}

	for j := int64(0); j <= A[0]; j++ {
		dp_out[0][j][A[0]-j] = 0
	}

	for i := int64(1); i < N; i++ {
		a := A[i]
		for j := int64(0); j < MX; j++ {
			for k := int64(0); k < MX; k++ {
				// in
				a = A[i] + j
				if a >= k {
					mi := int64(10000)
					for kk := int64(0); kk <= k; kk++ {
						if a-k >= 0 && a-k < MX {
							mi = min(mi, dp_in[i-1][a-k][kk])
						}
					}
					dp_in[i][j][k] = mi + a - k
				} else { // (i-1) is out
					mi := int64(10000)
					for kk := int64(0); kk <= k; kk++ {
						if k-a >= 0 && k-a < MX {
							mi = min(mi, dp_out[i-1][k-a][kk])
						}
					}
					dp_in[i][j][k] = mi + k - a
				}

				//out
				a = A[i] - j
				if a >= k {
					mi := int64(10000)
					for kk := int64(0); kk <= k; kk++ {
						if k-a >= 0 && k-a < MX {
							mi = min(mi, dp_in[i-1][a-k][kk])
						}
					}
					dp_out[i][j][k] = mi + a - k
				} else {
					mi := int64(10000)
					for kk := int64(0); kk <= k; kk++ {
						if k-a >= 0 && k-a < MX {
							mi = min(mi, dp_out[i-1][k-a][kk])
						}
					}
					dp_out[i][j][k] = mi + k - a
				}

			}
		}
	}

	res := int64(10000)
	for k := int64(0); k < MX; k++ {
		res = min(res, dp_in[N-1][0][k])
		res = min(res, dp_out[N-1][0][k])
	}

	return res

}

func main() {
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &N, &M)
	A = make([]int64, N)
	for i := N - 1; int64(i) >= 0; i-- {
		fmt.Fscan(in, &A[i])
	}

	fmt.Println(solve())
}


There is a matrix A of size x × y filled with integers. For every ,  Ai, j = y(i - 1) + j. Obviously, every integer from [1..xy] occurs exactly once in this matrix. You have traversed some path in this matrix. Your path can be described as a sequence of visited cells a1, a2, ..., an denoting that you started in the cell containing the number a1, then moved to the cell with the number a2, and so on.From the cell located in i-th line and j-th column (we denote this cell as (i, j)) you can move into one of the following cells: (i + 1, j) — only if i < x;  (i, j + 1) — only if j < y;  (i - 1, j) — only if i > 1;  (i, j - 1) — only if j > 1.Notice that making a move requires you to go to an adjacent cell. It is not allowed to stay in the same cell. You don't know x and y exactly, but you have to find any possible values for these numbers such that you could start in the cell containing the integer a1, then move to the cell containing a2 (in one step), then move to the cell containing a3 (also in one step) and so on. Can you choose x and y so that they don't contradict with your sequence of moves?

The first line contains one integer number n (1 ≤ n ≤ 200000) — the number of cells you visited on your path (if some cell is visited twice, then it's listed twice). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the integers in the cells on your path.

If all possible values of x and y such that 1 ≤ x, y ≤ 109 contradict with the information about your path, print NO. Otherwise, print YES in the first line, and in the second line print the values x and y such that your path was possible with such number of lines and columns in the matrix. Remember that they must be positive integers not exceeding 109.

NoteThe matrix and the path on it in the first test looks like this:  Also there exist multiple correct answers for both the first and the third examples.

package main

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

func Init() {
}

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

	diffs := make(map[int]bool)
	for i := 1; i < N; i++ {
	  d := Abs(A[i] - A[i - 1])
	  if d == 0 {
	    io.Println("NO")
	    return
	  }
	  diffs[d] = true
	}
	delete(diffs, 1)

	if len(diffs) == 0 {
	  io.Println("YES")
	  io.Println("1000000000 1000000000")
	  return
	}
	if len(diffs) > 1 {
	  io.Println("NO")
	  return
	}

	var y int
	for k := range diffs {
	  y = k
	}

	for i := 1; i < N; i++ {
	  d := Abs(A[i] - A[i - 1])
	  if d == 1 && (A[i] - 1) / y != (A[i - 1] - 1) / y {
	    io.Println("NO")
	    return
	  }
	}
	io.Println("YES")
	io.Printf("1000000000 %d\n", y)
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	for {
		if _, err := fmt.Fscan(reader, &n); err != nil {
			break
		}
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		gao(n, a)
	}
}

func gao(n int, a []int) {
	if n == 1 {
		fmt.Println("YES")
		fmt.Println(a[0], 1)
		return
	} // n > 1 below

	candidate := map[int]bool{}
	for i := 1; i < n; i++ {
		diff := abs(a[i] - a[i-1])
		if diff == 0 {
			fmt.Println("NO")
			return
		}
		if diff > 1 {
			candidate[diff] = true
		}
	}

	if len(candidate) == 0 {
		fmt.Println("YES")
		y := 0
		for i := 0; i < n; i++ {
			y = max(y, a[i])
		}
		fmt.Println(1, y)
		return
	}
	if len(candidate) > 1 {
		fmt.Println("NO")
		return
	}
	// len(candidate) == 1 below

	var y int
	for key := range candidate {
		y = key
	}
	const boundary = 1000 * 1000 * 1000
	if y > boundary {
		fmt.Println("NO")
		return
	}

	x := 0
	for _, av := range a {
		x = max(x, (av-1)/y+1)
	}
	if x > boundary {
		fmt.Println("NO")
		return
	}

	// fmt.Println("debug x, y", x, y)

	// Double check.
	for i := 1; i < n; i++ {
		x1, y1 := (a[i-1]-1)/y, (a[i-1]-1)%y
		x2, y2 := (a[i]-1)/y, (a[i]-1)%y
		if !(x1 == x2 || y1 == y2) {
			fmt.Println("NO")
			return
		}
		if x1 == x2 && y2-y1 != a[i]-a[i-1] {
			fmt.Println("NO")
			return
		}
		if y1 == y2 && sign(x2-x1)*y != a[i]-a[i-1] {
			fmt.Println("NO")
			return
		}
	}

	fmt.Println("YES")
	fmt.Println(x, y)
}

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

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

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

package main

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

func main() {
	scanner, n := bufio.NewScanner(os.Stdin), 0
	scanner.Split(bufio.ScanWords)
	fmt.Scan(&n)
	a, max := make([]int, n), 0
	for i := 0; i < n; i++ {
		scanner.Scan()
		a[i], _ = strconv.Atoi(scanner.Text())
		if a[i] > max {
			max = a[i]
		}
		if (i > 0) && (a[i-1] == a[i]) {
			// It is not allowed to stay in the same cell.
			fmt.Println("NO")
			os.Exit(0)
		}
	}

	y := 1
	for i := 0; i < (n - 1); i++ {
		d := a[i+1] - a[i]
		if d < 0 {
			d = -d
		}
		if d > 1 {
			if y == 1 {
				y = d
			}
			if y != d {
				fmt.Println("NO")
				os.Exit(0)
			}
		}
	}

	if y > 1 {
		// Handle the bad +1, -1
		for i := 0; i < (n - 1); i++ {
			isBadPlusOne := (a[i]%y == 0) && (a[i] == (a[i+1] - 1))
			isBadMinusOne := (a[i]%y == 1) && (a[i] == (a[i+1] + 1))
			if isBadMinusOne || isBadPlusOne {
				fmt.Println("NO")
				os.Exit(0)
			}
		}
	}

	// max > y because minA is 1
	fmt.Println("YES")
	if n == 1 {
		fmt.Println(max, y)
		os.Exit(0)
	}
	fmt.Println(int64(1e9), y)
}

package main

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

func Init() {
}

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

	diffs := make(map[int]bool)
	for i := 1; i < N; i++ {
	  d := Abs(A[i] - A[i - 1])
	  if d == 0 {
	    io.Println("NO")
	    return
	  }
	  diffs[d] = true
	}
	delete(diffs, 1)

	if len(diffs) == 0 {
	  io.Println("YES")
	  io.Println("1000000000 1000000000")
	  return
	}
	if len(diffs) > 1 {
	  io.Println("NO")
	  return
	}

	var y int
	for k := range diffs {
	  y = k
	}

	for i := 1; i < N; i++ {
	  d := Abs(A[i] - A[i - 1])
	  if d == 1 && (A[i] - 1) / y != (A[i - 1] - 1) / y {
	    io.Println("NO", i)
	    return
	  }
	}
	io.Println("YES")
	io.Printf("1000000000 %d\n", y)
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(reader, &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
	}

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

	candidate := map[int]bool{}
	for i := 1; i < n; i++ {
		diff := abs(a[i] - a[i-1])
		if diff > 1 {
			candidate[diff] = true
		}
	}

	if len(candidate) != 1 {
		fmt.Println("NO")
		return
	}

	var y int
	for key := range candidate {
		y = key
	}
	const boundary = 1000 * 1000 * 1000
	if y > boundary {
		fmt.Println("NO")
		return
	}

	x := 0
	for _, av := range a {
		x = max(x, (av-1)/y+1)
	}
	if x > boundary {
		fmt.Println("NO")
		return
	}
	fmt.Println("YES")
	fmt.Println(x, y)
}

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
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(reader, &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
	}

	candidate := map[int]bool{}
	for i := 1; i < n; i++ {
		diff := abs(a[i] - a[i-1])
		if diff > 1 {
			candidate[diff] = true
		}
	}

	if len(candidate) != 1 {
		fmt.Println("NO")
		return
	}

	var y int
	for key := range candidate {
		y = key
	}
	const boundary = 1000 * 1000 * 1000
	if y > boundary {
		fmt.Println("NO")
		return
	}

	x := 0
	for _, av := range a {
		x = max(x, (av-1)/y+1)
	}
	if x > boundary {
		fmt.Println("NO")
		return
	}
	fmt.Println("YES")
	fmt.Println(x, y)
}

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
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	for {
		if _, err := fmt.Fscan(reader, &n); err != nil {
			break
		}
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		gao(n, a)
	}
}

func gao(n int, a []int) {
	if n == 1 {
		fmt.Println("YES")
		fmt.Println(a[0], 1)
		return
	}

	candidate := map[int]bool{}
	for i := 1; i < n; i++ {
		diff := abs(a[i] - a[i-1])
		if diff == 0 {
			fmt.Println("NO")
			return
		}
		if diff > 1 {
			candidate[diff] = true
		}
	}

	if len(candidate) != 1 {
		fmt.Println("NO")
		return
	}

	var y int
	for key := range candidate {
		y = key
	}
	const boundary = 1000 * 1000 * 1000
	if y > boundary {
		fmt.Println("NO")
		return
	}

	x := 0
	for _, av := range a {
		x = max(x, (av-1)/y+1)
	}
	if x > boundary {
		fmt.Println("NO")
		return
	}

	// Double check.
	for i := 1; i < n; i++ {
		x1, y1 := (a[i-1]-1)/y, (a[i-1]-1)%y
		x2, y2 := (a[i]-1)/y, (a[i]-1)%y
		if !(x1 == x2 || y1 == y2) {
			fmt.Println("NO")
			return
		}
		if x1 == x2 && y2-y1 != a[i]-a[i-1] {
			fmt.Println("NO")
			return
		}
		if y1 == y2 && sign(x2-x1)*y != a[i]-a[i-1] {
			fmt.Println("NO")
			return
		}
	}

	fmt.Println("YES")
	fmt.Println(x, y)
}

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

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

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

package main

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

func main() {
	scanner, n := bufio.NewScanner(os.Stdin), 0
	scanner.Split(bufio.ScanWords)
	fmt.Scan(&n)
	a, max := make([]int, n), 0
	for i := 0; i < n; i++ {
		scanner.Scan()
		a[i], _ = strconv.Atoi(scanner.Text())
		if a[i] > max {
			max = a[i]
		}
		if (i > 0) && (a[i-1] == a[i]) {
			// It is not allowed to stay in the same cell.
			fmt.Println("NO")
			os.Exit(0)
		}
	}

	y := 1
	for i := 0; i < (n - 1); i++ {
		d := a[i+1] - a[i]
		if d < 0 {
			d = -d
		}
		if d > 1 {
			if y == 1 {
				y = d
			}
			if y != d {
				fmt.Println("NO")
				os.Exit(0)
			}
		}
	}

	if y > 1 {
		// Handle the bad +1, -1
		for i := 0; i < (n - 1); i++ {
			isBadPlusOne := (a[i]%y == 0) && (a[i] == (a[i+1] - 1))
			isBadMinusOne := (a[i]%y == 1) && (a[i] == (a[i+1] + 1))
			if isBadMinusOne || isBadPlusOne {
				fmt.Println("NO")
				os.Exit(0)
			}
		}
	}

	// max > y because minA is 1
	fmt.Println("YES")
	if n == 1 {
		fmt.Println(max, y)
		os.Exit(0)
	}
	fmt.Println(max/y+1, y)
}

package main

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

func main() {
	scanner, n := bufio.NewScanner(os.Stdin), 0
	scanner.Split(bufio.ScanWords)
	fmt.Scan(&n)
	a, max := make([]int, n), 0
	for i := 0; i < n; i++ {
		scanner.Scan()
		a[i], _ = strconv.Atoi(scanner.Text())
		if a[i] > max {
			max = a[i]
		}
		if (i > 0) && (a[i-1] == a[i]) {
			// It is not allowed to stay in the same cell.
			fmt.Println("NO")
			os.Exit(0)
		}
	}

	y := 1
	for i := 0; i < (n - 1); i++ {
		d := a[i+1] - a[i]
		if d < 0 {
			d = -d
		}
		if d > 1 {
			if y == 1 {
				y = d
			}
			if y != d {
				fmt.Println("NO")
				os.Exit(0)
			}
		}
	}

	if y > 1 {
		// Handle the bad +1, -1
		for i := 0; i < (n - 1); i++ {
			isBadPlusOne := (a[i]%y == 0) && (a[i] == (a[i+1] - 1))
			isBadMinusOne := (a[i]%y == 1) && (a[i] == (a[i+1] + 1))
			if isBadMinusOne || isBadPlusOne {
				fmt.Println("NO")
				os.Exit(0)
			}
		}
	}

	fmt.Println(max/y+1, y)
}

package main

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

func main() {
	scanner, n := bufio.NewScanner(os.Stdin), 0
	scanner.Split(bufio.ScanWords)
	fmt.Scan(&n)
	a, max := make([]int, n), 0
	for i := 0; i < n; i++ {
		scanner.Scan()
		a[i], _ = strconv.Atoi(scanner.Text())
		if a[i] > max {
			max = a[i]
		}
		if (i > 0) && (a[i-1] == a[i]) {
			// It is not allowed to stay in the same cell.
			fmt.Println("NO")
			os.Exit(0)
		}
	}

	y := 1
	for i := 0; i < (n - 1); i++ {
		d := a[i+1] - a[i]
		if d < 0 {
			d = -d
		}
		if d > 1 {
			if y == 1 {
				y = d
			}
			if y != d {
				fmt.Println("NO")
				os.Exit(0)
			}
		}
	}

	if y > 1 {
		// Handle the bad +1, -1
		for i := 0; i < (n - 1); i++ {
			isBadPlusOne := (a[i]%y == 0) && (a[i] == (a[i+1] - 1))
			isBadMinusOne := (a[i]%y == 1) && (a[i] == (a[i+1] + 1))
			if isBadMinusOne || isBadPlusOne {
				fmt.Println("NO")
				os.Exit(0)
			}
		}
	}

	// max > y because minA is 1
	fmt.Println("YES")
	fmt.Println(max/y, y)
}

package main

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

func main() {
	scanner, n := bufio.NewScanner(os.Stdin), 0
	scanner.Split(bufio.ScanWords)
	fmt.Scan(&n)
	a, max := make([]int, n), 0
	for i := 0; i < n; i++ {
		scanner.Scan()
		a[i], _ = strconv.Atoi(scanner.Text())
		if a[i] > max {
			max = a[i]
		}
		if (i > 0) && (a[i-1] == a[i]) {
			// It is not allowed to stay in the same cell.
			fmt.Println("NO")
			os.Exit(0)
		}
	}

	y := 1
	for i := 0; i < (n - 1); i++ {
		d := a[i+1] - a[i]
		if d < 0 {
			d = -d
		}
		if d > 1 {
			if y == 1 {
				y = d
			}
			if y != d {
				fmt.Println("NO")
				os.Exit(0)
			}
		}
	}

	if y > 1 {
		// Handle the bad +1, -1
		for i := 0; i < (n - 1); i++ {
			isBadPlusOne := (a[i]%y == 0) && (a[i] == (a[i+1] - 1))
			isBadMinusOne := (a[i]%y == 1) && (a[i] == (a[i+1] + 1))
			if isBadMinusOne || isBadPlusOne {
				fmt.Println("NO")
				os.Exit(0)
			}
		}
	}

	// max > y because minA is 1
	fmt.Println("YES")
	fmt.Println(max/y+1, y)
}

package main

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

func main() {
	scanner, n := bufio.NewScanner(os.Stdin), 0
	scanner.Split(bufio.ScanWords)
	fmt.Scan(&n)
	a, max := make([]int, n), 0
	for i := 0; i < n; i++ {
		scanner.Scan()
		a[i], _ = strconv.Atoi(scanner.Text())
		if a[i] > max {
			max = a[i]
		}
		if (i > 0) && (a[i-1] == a[i]) {
			// It is not allowed to stay in the same cell.
			fmt.Println("NO")
			os.Exit(0)
		}
	}

	y := 1
	for i := 0; i < (n - 1); i++ {
		d := a[i+1] - a[i]
		if d < 0 {
			d = -d
		}
		if d > 1 {
			if y == 1 {
				y = d
			}
			if y != d {
				fmt.Println("NO")
				os.Exit(0)
			}
		}
	}

	if y > 1 {
		// Handle the bad +1, -1
		for i := 0; i < (n - 1); i++ {
			isBadPlusOne := (a[i]%y == 0) && (a[i] == (a[i+1] - 1))
			isBadMinusOne := (a[i]%y == 1) && (a[i] == (a[i+1] + 1))
			if isBadMinusOne || isBadPlusOne {
				fmt.Println("NO")
				os.Exit(0)
			}
		}
	}

	// max > y because minA is 1
	fmt.Println("YES")
	if n == 1 {
		fmt.Println(max, y)
		os.Exit(0)
	}
	fmt.Println(1e9, y)
}


The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.

The first input line contains an integer n (2 ≤ n ≤ 100). The second line contains n - 1 integers di (1 ≤ di ≤ 100). The third input line contains two integers a and b (1 ≤ a < b ≤ n). The numbers on the lines are space-separated.

Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.

package main
import (
	"fmt"
	//"unicode/utf8"
	//"strconv"
)

func main() {
	var n,a,b int
	s:=0
	k:=make([]int,100)
	fmt.Scan(&n)
	for i:=1;i<n;i++ {
		fmt.Scan(&k[i])
	}
	fmt.Scan(&a,&b)
	for i:=a;i<b;i++ {
		s+=k[i]
	}
	fmt.Println(s)
}

package main

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

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

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

	var years int = 0
	for i := di[n-1]; i < di[n]; i++ {
		years += di[i-1]
	}

	fmt.Println(years)
}

package main

import "fmt"

func main() {
    var n,a,b int
    var d [105]int
    fmt.Scan(&n)
    for i := 1; i < n; i++ {
        fmt.Scan(&d[i])
    }
    fmt.Scan(&a,&b)
    ans := 0
    for a < b {
        ans += d[a]
        a++
    }
    fmt.Println(ans)
}

// 38A-mic
package main

import (
	"fmt"
)

func main() {
	var n, a, b, sum int
	var m [10000]int
	fmt.Scan(&n)
	for i := 1; i <= n-1; i++ {
		fmt.Scan(&m[i])
	}
	fmt.Scan(&a, &b)
	for j := a; j <= b-1; j++ {
		sum += m[j]
	}
	fmt.Println(sum)
}

// 38A
package main

import (
	"fmt"
)

func main() {
	var n, a, b int
	y := 0
	d := [100]int{0}
	fmt.Scan(&n)
	for i := 1; i < n; i++ {
		fmt.Scan(&d[i])
	}
	fmt.Scan(&a, &b)
	for a < b {
		y += d[a]
		a++
	}
	fmt.Println(y)
}

// 38A
package main

import (
	"fmt"
)

func main() {
	var n, a, b, y int
	d := [100]int{0}
	fmt.Scan(&n)
	for i := 1; i < n; i++ {
		fmt.Scan(&d[i])
	}
	fmt.Scan(&a, &b)
	for i := a; i < b-1; i++ {
		y += d[i]
	}
	fmt.Println(y)
}


You are given a prime number $$$n$$$, and an array of $$$n$$$ integers $$$b_1,b_2,\ldots, b_n$$$, where $$$0 \leq b_i < n$$$ for each $$$1 \le i \leq n$$$.You have to find a matrix $$$a$$$ of size $$$n \times n$$$ such that all of the following requirements hold:  $$$0 \le a_{i,j} < n$$$ for all $$$1 \le i, j \le n$$$.  $$$a_{r_1, c_1} + a_{r_2, c_2} \not\equiv a_{r_1, c_2} + a_{r_2, c_1} \pmod n$$$ for all positive integers $$$r_1$$$, $$$r_2$$$, $$$c_1$$$, and $$$c_2$$$ such that $$$1 \le r_1 < r_2 \le n$$$ and $$$1 \le c_1 < c_2 \le n$$$. $$$a_{i,i} = b_i$$$ for all $$$1 \le i \leq n$$$. Here $$$x \not \equiv y \pmod m$$$ denotes that $$$x$$$ and $$$y$$$ give different remainders when divided by $$$m$$$.If there are multiple solutions, output any. It can be shown that such a matrix always exists under the given constraints.

The first line contains a single positive integer $$$n$$$ ($$$2 \le n < 350$$$).  The second line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$0 \le b_i < n$$$) — the required values on the main diagonal of the matrix. It is guaranteed that $$$n$$$ is prime.

Print $$$n$$$ lines. On the $$$i$$$-th line, print $$$n$$$ integers $$$a_{i, 1}, a_{i, 2}, \ldots, a_{i, n}$$$, each separated with a space. If there are multiple solutions, output any.

NoteIn the first example, the answer is valid because all entries are non-negative integers less than $$$n = 2$$$, and $$$a_{1,1}+a_{2,2} \not\equiv a_{1,2}+a_{2,1} \pmod 2$$$ (because $$$a_{1,1}+a_{2,2} = 0 + 0 \equiv 0 \pmod 2$$$ and $$$a_{1,2}+a_{2,1} = 1 + 0 \equiv 1 \pmod 2 $$$). Moreover, the values on the main diagonals are equal to $$$0,0$$$ as required.In the second example, the answer is correct because all entries are non-negative integers less than $$$n = 3$$$, and the second condition is satisfied for all quadruplets $$$(r_1, r_2, c_1, c_2)$$$. For example:   When $$$r_1=1$$$, $$$r_2=2$$$, $$$c_1=1$$$ and $$$c_2=2$$$, $$$a_{1,1}+a_{2,2} \not\equiv a_{1,2}+a_{2,1} \pmod 3$$$ because $$$a_{1,1}+a_{2,2} = 1 + 1 \equiv 2 \pmod 3$$$ and $$$a_{1,2}+a_{2,1} = 2 + 1 \equiv 0 \pmod 3 $$$.  When $$$r_1=2$$$, $$$r_2=3$$$, $$$c_1=1$$$, and $$$c_2=3$$$, $$$a_{2,1}+a_{3,3} \not\equiv a_{2,3}+a_{3,1} \pmod 3$$$ because $$$a_{2,1}+a_{3,3} = 1 + 1 \equiv 2 \pmod 3$$$ and $$$a_{2,3}+a_{3,1} = 0 + 1 \equiv 1 \pmod 3 $$$.  Moreover, the values on the main diagonal are equal to $$$1,1,1$$$ as required.

package main

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

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

	for t, _ = fmt.Fscan(reader, &n); t == 1; t, _ = fmt.Fscan(reader, &n) {
		b = make([]uint, n)

		for i = 0; i < n; i++ {
			fmt.Fscan(reader, &b[i])
			aux = (n + b[i] - ((i * i) % n)) % n

			for j = 0; j < n; j++ {
				fmt.Fprint(writer, (((i*j)%n)+aux)%n)

				if j < n-1 {
					fmt.Fprint(writer, " ")
				}
			}

			fmt.Fprintln(writer)
		}
	}

	writer.Flush()
}

package main

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

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

	var n int
	Fscan(in, &n)
	b := make([]int, n)
	for i := range b {
		Fscan(in, &b[i])
	}
	for i, v := range b {
		a := make([]int, n)
		a[i] = v
		k := i+1
		for j := 1; j < n; j++ {
			a[k%n] = (a[(k-1)%n] + i) % n
			k++
		}
		for _, v := range a {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}
}

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

package main

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

var in = bufio.NewReader(os.Stdin)

func read(out *int) error {
	var ans, sign int = 0, 1
	var readed = false
	c, err := in.ReadByte()
	for ; err == nil && (c < '0' || '9' < c); c, err = in.ReadByte() {
		if c == '-' {
			sign = -sign
		}
	}
	for ; err == nil && '0' <= c && c <= '9'; c, err = in.ReadByte() {
		ans = ans<<3 + ans<<1 + int(c-'0')
		readed = true
	}
	if readed {
		*out = ans * sign
		return nil
	}
	return err
}

var a [1000006]int

func main() {
	var n int
	read(&n)
	for i := 1; i <= n; i++ {
		read(&a[i])
	}
	for i := 1; i <= n; i++ {
		d := ((a[i]-i*i)%n + n) % n
		for j := 1; j <= n; j++ {
			fmt.Printf("%d ", (i*j+d)%n)
		}
		fmt.Println()
	}
}


Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In each square he wrote an integer number, possibly negative. He became interested in how many ways exist to cut this stripe into two pieces so that the sum of numbers from one piece is equal to the sum of numbers from the other piece, and each piece contains positive integer amount of squares. Would you help Bob solve this problem?

The first input line contains integer n (1 ≤ n ≤ 105) — amount of squares in the stripe. The second line contains n space-separated numbers — they are the numbers written in the squares of the stripe. These numbers are integer and do not exceed 10000 in absolute value.

Output the amount of ways to cut the stripe into two non-empty pieces so that the sum of numbers from one piece is equal to the sum of numbers from the other piece. Don't forget that it's allowed to cut the stripe along the squares' borders only.

package main

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

var r *bufio.Reader = bufio.NewReader(os.Stdin)
var w *bufio.Writer = bufio.NewWriter(os.Stdout)

func solve(n int, a []int) int {
	s := 0
	for _, v := range a {
		s += v
	}
	ans := 0
	lsum := 0
	for i := 0; i < n - 1; i++ {
		lsum += a[i]
		if (lsum == s - lsum) {
			ans += 1
		}
	}

	return ans
}

func main() {
	defer w.Flush()

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

	fmt.Fprintf(w, "%v", solve(n, a))
}

package main

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

var r *bufio.Reader = bufio.NewReader(os.Stdin)
var w *bufio.Writer = bufio.NewWriter(os.Stdout)

func main() {
	defer w.Flush()

	var n int
	fmt.Fscanf(r, "%d\n", &n)
	a := make([]int, n)
	dp := make([]int, n)
	s := 0
	lsum := 0
	for i := 0; i < n; i++ {
		fmt.Fscanf(r, "%d ", &a[i])
		s += a[i]
		lsum += a[i]
		dp[i] += lsum
	}
	ans := 0
	for i := 0; i < n - 1; i++ {
		if (dp[i] == s - dp[i]) {
			ans += 1
		}
	}

	fmt.Fprintf(w, "%v", ans)
}

package main

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

var r *bufio.Reader = bufio.NewReader(os.Stdin)
var w *bufio.Writer = bufio.NewWriter(os.Stdout)

func solve(n int, a []int) int {
	s := 0
	for _, v := range a {
		s += v
	}
	ans := 0
	lsum := 0
	for i := 0; i < n - 1; i++ {
		lsum += a[i]
		if (lsum == s - lsum) {
			ans += 1
		}
	}

	return ans
}

func main() {
	defer w.Flush()

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

	fmt.Fprintf(w, "%v", solve(n, a))
}

package main

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

var r *bufio.Reader = bufio.NewReader(os.Stdin)
var w *bufio.Writer = bufio.NewWriter(os.Stdout)

func solve(n, s int, dp []int) int {
	ans := 0
	for i := 0; i < n - 1; i++ {
		if (dp[i] == s - dp[i]) {
			ans += 1
		}
	}

	return ans
}

func main() {
	defer w.Flush()

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

	fmt.Fprintf(w, "%v", solve(n, s, dp))
}


Bajtek, known for his unusual gifts, recently got an integer array $$$x_0, x_1, \ldots, x_{k-1}$$$.Unfortunately, after a huge array-party with his extraordinary friends, he realized that he'd lost it. After hours spent on searching for a new toy, Bajtek found on the arrays producer's website another array $$$a$$$ of length $$$n + 1$$$. As a formal description of $$$a$$$ says, $$$a_0 = 0$$$ and for all other $$$i$$$ ($$$1 \le i \le n$$$) $$$a_i = x_{(i-1)\bmod k} + a_{i-1}$$$, where $$$p \bmod q$$$ denotes the remainder of division $$$p$$$ by $$$q$$$.For example, if the $$$x = [1, 2, 3]$$$ and $$$n = 5$$$, then:  $$$a_0 = 0$$$,  $$$a_1 = x_{0\bmod 3}+a_0=x_0+0=1$$$,  $$$a_2 = x_{1\bmod 3}+a_1=x_1+1=3$$$,  $$$a_3 = x_{2\bmod 3}+a_2=x_2+3=6$$$,  $$$a_4 = x_{3\bmod 3}+a_3=x_0+6=7$$$,  $$$a_5 = x_{4\bmod 3}+a_4=x_1+7=9$$$. So, if the $$$x = [1, 2, 3]$$$ and $$$n = 5$$$, then $$$a = [0, 1, 3, 6, 7, 9]$$$.Now the boy hopes that he will be able to restore $$$x$$$ from $$$a$$$! Knowing that $$$1 \le k \le n$$$, help him and find all possible values of $$$k$$$ — possible lengths of the lost array.

The first line contains exactly one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the length of the array $$$a$$$, excluding the element $$$a_0$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^6$$$). Note that $$$a_0$$$ is always $$$0$$$ and is not given in the input.

The first line of the output should contain one integer $$$l$$$ denoting the number of correct lengths of the lost array. The second line of the output should contain $$$l$$$ integers — possible lengths of the lost array in increasing order.

NoteIn the first example, any $$$k$$$ is suitable, since $$$a$$$ is an arithmetic progression.Possible arrays $$$x$$$:  $$$[1]$$$ $$$[1, 1]$$$ $$$[1, 1, 1]$$$ $$$[1, 1, 1, 1]$$$  $$$[1, 1, 1, 1, 1]$$$In the second example, Bajtek's array can have three or five elements.Possible arrays $$$x$$$:  $$$[1, 2, 2]$$$ $$$[1, 2, 2, 1, 2]$$$For example, $$$k = 4$$$ is bad, since it leads to $$$6 + x_0 = 8$$$ and $$$0 + x_0 = 1$$$, which is an obvious contradiction.In the third example, only $$$k = n$$$ is good.Array $$$[1, 4, -2]$$$ satisfies the requirements.Note that $$$x_i$$$ may be negative.

package main

import "fmt"

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

	var ans []int
	for k := 1; k <= n; k++ {
		x := make([]int, k)
		for i := range x {
			x[i] = a[i+1] - a[i]
		}
		good := true
		for i := range a[1:] {
			if a[i+1]-a[i] != x[i%k] {
				good = false
				break
			}
		}
		if good {
			ans = append(ans, k)
		}
	}

	fmt.Println(len(ans))
	for i := range ans {
		if i == len(ans)-1 {
			fmt.Println(ans[i])
		} else {
			fmt.Print(ans[i], " ")
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var N int
	var A [1010]int

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

	var diff [1010]int
	diff[0] = A[0]
	for i := 0; i < N-1; i++ {
		diff[i+1] = A[i+1] - A[i]
	}

	var res []int
	for i := 1; i <= N; i++ {
		ok := true
		for j, k := 0, i; ok && k < N; {
			if diff[j] != diff[k] {
				ok = false
			}

			j++
			k++
		}

		if ok {
			res = append(res, i)
		}
	}

	fmt.Printf("%v\n", len(res))
	for i := 0; i < len(res); i++ {
		if i > 0 {
			fmt.Printf(" ")
		}
		fmt.Printf("%v", res[i])
	}
	fmt.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)
	a := [1111]int{}
	for i := 1; i <= n; i++ {
		scanf("%d ", &a[i])
	}
	x := make([]int, n)
	ans := make([]int, 0)
	aa := make([]int, n+1)
	for j := 1; j <= n; j++ {
		x[j-1] = a[j] - a[j-1]
	}
	for i := 1; i <= n; i++ {

		flag := true

		for j := 1; j <= n; j++ {
			aa[j] = x[(j-1)%i] + aa[j-1]
			if aa[j] != a[j] {
				flag = false
				break
			}
		}

		if flag {
			ans = append(ans, i)
		}
	}
	printf("%d\n", len(ans))
	for i := 0; i < len(ans); i++ {
		printf("%d ", ans[i])
	}
}

package main

import "fmt"

func main() {

	var n, k int

	fmt.Scan(&n)
	A := make([]int, n+1)
	A[0] = 0
	for i := 1; i <= n; i++ {
		fmt.Scan(&A[i])
	}

	k = 1
	var X []int
	lens := make([]int, n+1)
	length := 0

	for ; k < n + 1; k++ {
		X = make([]int, k)
		for num := 0; num < k; num++ {

			X[num] = A[num + 1] - A[num]
		}

		anotherFlag := false
		for num := k; num < n; num++ {
			if X[num % k] != A[num+1]  - A[num] {
				anotherFlag = true
				break
			}
		}

		if !anotherFlag {
			length++
			lens[length-1] = k
		}
	}

	fmt.Printf("%d\n", length)
	for i := 0; i < length; i++ {
		fmt.Printf("%d ", lens[i])
	}
}

package main

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

func Init() {
}

func Solve(io *FastIO) {
	N := io.NextInt()
	A := io.NextIntArrayOffset(N, 1)

	var ans []int
	for k := 1; k <= N; k++ {
		if check(A, k) {
			ans = append(ans, k)
		}
	}
	io.Println(len(ans))
	for _, v := range ans {
		io.Printf("%d ", v)
	}
	io.Println()
}

func check(A []int, k int) bool {
	for i := 0; i+1 < len(A); i++ {
		d := A[i+1] - A[i]
		for j := i; j+1 < len(A); j += k {
			if A[j+1]-A[j] != d {
				return false
			}
		}
	}
	return true
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


Polycarp is practicing his problem solving skill. He has a list of $$$n$$$ problems with difficulties $$$a_1, a_2, \dots, a_n$$$, respectively. His plan is to practice for exactly $$$k$$$ days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all $$$n$$$ problems in exactly $$$k$$$ days.Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in $$$k$$$ days he will solve all the $$$n$$$ problems.The profit of the $$$j$$$-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the $$$j$$$-th day (i.e. if he solves problems with indices from $$$l$$$ to $$$r$$$ during a day, then the profit of the day is $$$\max\limits_{l \le i \le r}a_i$$$). The total profit of his practice is the sum of the profits over all $$$k$$$ days of his practice.You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all $$$n$$$ problems between $$$k$$$ days satisfying the conditions above in such a way, that the total profit is maximum.For example, if $$$n = 8, k = 3$$$ and $$$a = [5, 4, 2, 6, 5, 1, 9, 2]$$$, one of the possible distributions with maximum total profit is: $$$[5, 4, 2], [6, 5], [1, 9, 2]$$$. Here the total profit equals $$$5 + 6 + 9 = 20$$$.

The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2000$$$) — the number of problems and the number of days, respectively. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2000$$$) — difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).

In the first line of the output print the maximum possible total profit. In the second line print exactly $$$k$$$ positive integers $$$t_1, t_2, \dots, t_k$$$ ($$$t_1 + t_2 + \dots + t_k$$$ must equal $$$n$$$), where $$$t_j$$$ means the number of problems Polycarp will solve during the $$$j$$$-th day in order to achieve the maximum possible total profit of his practice. If there are many possible answers, you may print any of them.

NoteThe first example is described in the problem statement.In the second example there is only one possible distribution.In the third example the best answer is to distribute problems in the following way: $$$[1, 2000], [2000, 2]$$$. The total profit of this distribution is $$$2000 + 2000 = 4000$$$.

package main

import (
	"fmt"
	"sort"
)

func ReadInt64() int64 {
	var ret int64
	cnt, err := fmt.Scanf("%d", &ret)
	if cnt != 1 || err != nil {
		//fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		_, _ =fmt.Scanf("%d", &ret)

		return ret
	}
	return ret
}

func ReadString() string {
	var ret string
	cnt, err := fmt.Scanf("%s", ret)
	if cnt != 1 || err != nil {
		fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		return ""
	}
	return ret
}

type pair struct {
	first  int64
	second int64
}

func main() {
	n, k, sum := int64(0), int64(0), int64(0)
	n, k = ReadInt64(), ReadInt64()
	a, b := make([]pair, n), make([]pair, k+1)

	for i := int64(0); i < n; i++ {
		a[i].first = ReadInt64()
		a[i].second = i + 1
	}
	//fmt.Print(a)
	sort.Slice(a, func(i, j int) bool {
		if a[i].first != a[j].first {
			return a[i].first < a[j].first
		} else {
			return a[i].second < a[j].second
		}
	})

	//fmt.Print(a)
	for i := int64(0); i < k; i++ {
		sum += a[n-i-1].first
	}
	fmt.Printf("%d\n", sum)
	for i := int64(1); i <= k; i++ {
		b[i] = a[n-i]
	}
	//fmt.Print(b)
	sort.Slice(b, func(i, j int) bool { return b[i].second < b[j].second })
	//fmt.Print(b)
	for i := int64(1); i < k; i++ {
		fmt.Printf("%d ", b[i].second-b[i-1].second)
	}
	fmt.Printf("%d\n", n-b[k-1].second)
}

package main

import (
	"fmt"
	"sort"
)

const (
	M = 2e3 + 5
)

type Array struct {
	x int
	idx int
}

type intArray []Array
type intSlice []int

var n, k int

func (s intArray) Len() int { return len(s) }
func (s intArray) Swap(i, j int){
	s[i].idx, s[j].idx = s[j].idx, s[i].idx
	s[i].x, s[j].x = s[j].x, s[i].x
}
func (s intArray) Less(i, j int) bool { return s[i].x < s[j].x }

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

func main() {
	var i int
	fmt.Scanf("%d%d\n", &n, &k)
	var a = make([]int, n)
	var b = make([]Array, n)
	for i = 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		b[i].idx = i
		b[i].x = a[i]
	}
	sort.Sort(intArray(b))
	var ans = make([]int, k)
	var res int = 0

	for i = 0; i < k; i++ {
		ans[i] = b[n - 1 - i].idx
		res += b[n - 1 - i].x
	}
	sort.Sort(intSlice(ans))
	fmt.Println(res)
	if k == 1 {
		fmt.Print(n)
		return
	}
	for i = 0; i < k; i++ {
		if i == 0 {
			fmt.Print(ans[i] + 1, " ")
		} else if i == k - 1{
			fmt.Print(n - ans[i - 1] - 1, " ")
		} else {
			fmt.Print(ans[i] - ans[i - 1], " ")
		}
	}
}

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"sort"
)

//Item eh um elemento da fila de prioridades
type Item struct {
	value    int
	priority int
	index    int
}

//PriorityQueue implmenta a heap interface
type PriorityQueue []*Item

//Len retorna o tamanho da  pq
func (pq PriorityQueue) Len() int { return len(pq) }

//Less verifica se um tem maior priotidade que o outro
func (pq PriorityQueue) Less(i, j int) bool { return pq[i].priority < pq[j].priority }

//Swap troca os elementos de lugar
func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

//Push insere um elemento na pq
func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

//Pop remove um elemento da pq
func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 //for safety
	*pq = old[0 : n-1]
	return item
}

//update modifica o valor e a prioridade de um item na fila
func (pq *PriorityQueue) update(item *Item, value int, priority int) {
	item.value = value
	item.priority = priority
	heap.Fix(pq, item.index)
}

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

type pair struct {
	first  interface{}
	second interface{}
}

type intPair struct {
	first  int64
	second int64
}

type vertex struct {
	list []intPair
}

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

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

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

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

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

var (
	n, k         int
	dificuldades []int
	ordenados    []int
	maiores      = make(map[int]int)
	tabela       [100000]int
	result       []int

	cnt int
)

func main() {
	var num int

	fmt.Scan(&n, &k)

	for i := 0; i < n; i++ {
		fmt.Scan(&num)
		dificuldades = append(dificuldades, num)
		ordenados = append(ordenados, num)
		maiores[num] = 0
	}

	sort.Ints(ordenados)

	for i := n - 1; i >= n-k; i-- {
		maiores[ordenados[i]]++
		cnt += ordenados[i]
	}

	fmt.Println(cnt)

	cnt = 0
	for i := 0; i < n; i++ {
		cnt++
		if maiores[dificuldades[i]] > 0 {
			maiores[dificuldades[i]]--
			for i < n-1 && maiores[dificuldades[i+1]] == 0 {
				cnt++
				i++
			}
			result = append(result, cnt)
			cnt = 0
		}
	}

	for i := 0; i < len(result); i++ {
		if i != 0 {
			fmt.Print(" ")
		}
		fmt.Print(result[i])
	}
	fmt.Println()
}

package main

import (
	"fmt"
	"sort"
)

func ReadInt64() int64 {
	var ret int64
	_, err := fmt.Scan(&ret)
	if err != nil {
		return 0
	}
	return ret
}

func ReadString() string {
	var ret string
	cnt, err := fmt.Scanf("%s", ret)
	if cnt != 1 || err != nil {
		fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		return ""
	}
	return ret
}

type pair struct {
	first  int64
	second int64
}

func main() {
	n, k, sum := int64(0), int64(0), int64(0)
	n, k = ReadInt64(), ReadInt64()
	a, b := make([]pair, n), make([]pair, k+1)

	for i := int64(0); i < n; i++ {
		a[i].first = ReadInt64()
		a[i].second = i + 1
	}
	sort.Slice(a, func(i, j int) bool { return a[i].first < a[j].first })
	for i := int64(0); i < k; i++ {
		sum += a[n-i-1].first
	}
	fmt.Printf("%d\n", sum)
	for i := int64(1); i <= k; i++ {
		b[i] = a[n-i]
	}
	sort.Slice(b, func(i, j int) bool { return b[i].second < b[j].second })
	for i := int64(1); i < k; i++ {
		fmt.Printf("%d ", b[i].second-b[i-1].second)
	}
	fmt.Printf("%d\n", n-b[k-1].second)
}

package main

import (
	"fmt"
	"sort"
)

func ReadInt64() int64 {
	var ret int64
	cnt, err := fmt.Scanf("%d", &ret)
	if cnt != 1 || err != nil {
		//fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		return 0
	}
	return ret
}

func ReadString() string {
	var ret string
	cnt, err := fmt.Scanf("%s", ret)
	if cnt != 1 || err != nil {
		fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		return ""
	}
	return ret
}

type pair struct {
	first  int64
	second int64
}

func main() {
	n, k, sum := int64(0), int64(0), int64(0)
	n, k = ReadInt64(), ReadInt64()
	a, b := make([]pair, n), make([]pair, k+1)

	for i := int64(0); i < n; i++ {
		a[i].first = ReadInt64()
		a[i].second = i + 1
	}
	sort.Slice(a, func(i, j int) bool {
		if a[i].first != a[j].first {
			return a[i].first < a[j].first
		} else {
			return a[i].second < a[j].second
		}
	})

	//fmt.Print(a)
	for i := int64(0); i < k; i++ {
		sum += a[n-i-1].first
	}
	fmt.Printf("%d\n", sum)
	for i := int64(1); i <= k; i++ {
		b[i] = a[n-i]
	}
	//fmt.Print(b)
	sort.Slice(b, func(i, j int) bool { return b[i].second < b[j].second })
	//fmt.Print(b)
	for i := int64(1); i < k; i++ {
		fmt.Printf("%d ", b[i].second-b[i-1].second)
	}
	fmt.Printf("%d\n", n-b[k-1].second)
}

package main

import (
	"fmt"
	"sort"
)

func ReadInt64() int64 {
	var ret int64
	cnt, err := fmt.Scanf("%d", &ret)
	if cnt != 1 || err != nil {
		//fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		return 0
	}
	return ret
}

func ReadString() string {
	var ret string
	cnt, err := fmt.Scanf("%s", ret)
	if cnt != 1 || err != nil {
		fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		return ""
	}
	return ret
}

type pair struct {
	first  int64
	second int64
}

func main() {
	n, k, sum := int64(0), int64(0), int64(0)
	n, k = ReadInt64(), ReadInt64()
	a, b := make([]pair, n), make([]pair, k+1)

	for i := int64(0); i < n; i++ {
		a[i].first = ReadInt64()
		a[i].second = i + 1
	}
	fmt.Print(a)
	sort.Slice(a, func(i, j int) bool {
		if a[i].first != a[j].first {
			return a[i].first < a[j].first
		} else {
			return a[i].second < a[j].second
		}
	})

	fmt.Print(a)
	for i := int64(0); i < k; i++ {
		sum += a[n-i-1].first
	}
	fmt.Printf("%d\n", sum)
	for i := int64(1); i <= k; i++ {
		b[i] = a[n-i]
	}
	fmt.Print(b)
	sort.Slice(b, func(i, j int) bool { return b[i].second < b[j].second })
	fmt.Print(b)
	for i := int64(1); i < k; i++ {
		fmt.Printf("%d ", b[i].second-b[i-1].second)
	}
	fmt.Printf("%d\n", n-b[k-1].second)
}

package main

import (
	"fmt"
	"sort"
)

func ReadInt64() int64 {
	var ret int64
	cnt, err := fmt.Scanf("%d", &ret)
	if cnt != 1 || err != nil {
		//fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		return 0
	}
	return ret
}

func ReadString() string {
	var ret string
	cnt, err := fmt.Scanf("%s", ret)
	if cnt != 1 || err != nil {
		fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		return ""
	}
	return ret
}

type pair struct {
	first  int64
	second int64
}

func main() {
	n, k, sum := int64(0), int64(0), int64(0)
	n, k = ReadInt64(), ReadInt64()
	a, b := make([]pair, n), make([]pair, k+1)

	for i := int64(0); i < n; i++ {
		a[i].first = ReadInt64()
		a[i].second = i + 1
	}
	sort.Slice(a, func(i, j int) bool { return a[i].first < a[j].first })
	fmt.Print(a)
	for i := int64(0); i < k; i++ {
		sum += a[n-i-1].first
	}
	fmt.Printf("%d\n", sum)
	for i := int64(1); i <= k; i++ {
		b[i] = a[n-i]
	}
	fmt.Print(b)
	sort.Slice(b, func(i, j int) bool { return b[i].second < b[j].second })
	fmt.Print(b)
	for i := int64(1); i < k; i++ {
		fmt.Printf("%d ", b[i].second-b[i-1].second)
	}
	fmt.Printf("%d\n", n-b[k-1].second)
}

package main

import (
	"fmt"
	"sort"
)

func ReadInt64() int64 {
	var ret int64
	cnt, err := fmt.Scanf("%d", &ret)
	if cnt != 1 || err != nil {
		//fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		_, _ =fmt.Scanf("%d", &ret)

		return 0
	}
	return ret
}

func ReadString() string {
	var ret string
	cnt, err := fmt.Scanf("%s", ret)
	if cnt != 1 || err != nil {
		fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		return ""
	}
	return ret
}

type pair struct {
	first  int64
	second int64
}

func main() {
	n, k, sum := int64(0), int64(0), int64(0)
	n, k = ReadInt64(), ReadInt64()
	a, b := make([]pair, n), make([]pair, k+1)

	for i := int64(0); i < n; i++ {
		a[i].first = ReadInt64()
		a[i].second = i + 1
	}
	fmt.Print(a)
	sort.Slice(a, func(i, j int) bool {
		if a[i].first != a[j].first {
			return a[i].first < a[j].first
		} else {
			return a[i].second < a[j].second
		}
	})

	fmt.Print(a)
	for i := int64(0); i < k; i++ {
		sum += a[n-i-1].first
	}
	fmt.Printf("%d\n", sum)
	for i := int64(1); i <= k; i++ {
		b[i] = a[n-i]
	}
	fmt.Print(b)
	sort.Slice(b, func(i, j int) bool { return b[i].second < b[j].second })
	fmt.Print(b)
	for i := int64(1); i < k; i++ {
		fmt.Printf("%d ", b[i].second-b[i-1].second)
	}
	fmt.Printf("%d\n", n-b[k-1].second)
}

package main

import (
	"fmt"
	"sort"
)

func ReadInt64() int64 {
	var ret int64
	cnt, err := fmt.Scanf("%d", &ret)
	if cnt != 1 || err != nil {
		//fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		_, _ =fmt.Scanf("%d", &ret)

		return ret
	}
	return ret
}

func ReadString() string {
	var ret string
	cnt, err := fmt.Scanf("%s", ret)
	if cnt != 1 || err != nil {
		fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		return ""
	}
	return ret
}

type pair struct {
	first  int64
	second int64
}

func main() {
	n, k, sum := int64(0), int64(0), int64(0)
	n, k = ReadInt64(), ReadInt64()
	a, b := make([]pair, n), make([]pair, k+1)

	for i := int64(0); i < n; i++ {
		a[i].first = ReadInt64()
		a[i].second = i + 1
	}
	fmt.Print(a)
	sort.Slice(a, func(i, j int) bool {
		if a[i].first != a[j].first {
			return a[i].first < a[j].first
		} else {
			return a[i].second < a[j].second
		}
	})

	fmt.Print(a)
	for i := int64(0); i < k; i++ {
		sum += a[n-i-1].first
	}
	fmt.Printf("%d\n", sum)
	for i := int64(1); i <= k; i++ {
		b[i] = a[n-i]
	}
	fmt.Print(b)
	sort.Slice(b, func(i, j int) bool { return b[i].second < b[j].second })
	fmt.Print(b)
	for i := int64(1); i < k; i++ {
		fmt.Printf("%d ", b[i].second-b[i-1].second)
	}
	fmt.Printf("%d\n", n-b[k-1].second)
}

package main

import (
	"fmt"
	"sort"
)

const (
	M = 2e3 + 5
)

type Array struct {
	x int
	idx int
}

type intArray []Array
type intSlice []int

var n, k int

func (s intArray) Len() int { return len(s) }
func (s intArray) Swap(i, j int){
	s[i].idx, s[j].idx = s[j].idx, s[i].idx
	s[i].x, s[j].x = s[j].x, s[i].x
}
func (s intArray) Less(i, j int) bool { return s[i].x < s[j].x }

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

func main() {
	var i int
	fmt.Scanf("%d%d", &n, &k)
	var a = make([]int, n)
	var b = make([]Array, n)
	for i = 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		fmt.Print(i)
		b[i].idx = i
		b[i].x = a[i]
		fmt.Print(b[i].idx)
	}
	sort.Sort(intArray(b))
	var ans = make([]int, 0)
	var res int = 0

	for i = 0; i < k; i++ {
		fmt.Print(b[n - 1 - i].idx)
		ans = append(ans, b[n - 1 - i].idx)
		res += b[n - 1 - i].x
	}
	sort.Sort(intSlice(ans))
	fmt.Print(ans)
	fmt.Println(res)
	for i = 0; i < k; i++ {
		if i == 0 {
			fmt.Print(ans[i] + 1, " ")
		} else {
			fmt.Print(ans[i] - ans[i - 1], " ")
		}
	}
}

package main

import (
	"fmt"
	"sort"
)

const (
	M = 2e3 + 5
)

type Array struct {
	x int
	idx int
}

type intArray []Array
type intSlice []int

var n, k int

func (s intArray) Len() int { return len(s) }
func (s intArray) Swap(i, j int){
	s[i].idx, s[j].idx = s[j].idx, s[i].idx
	s[i].x, s[j].x = s[j].x, s[i].x
}
func (s intArray) Less(i, j int) bool { return s[i].x < s[j].x }

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

func main() {
	var i int
	fmt.Scanf("%d%d", &n, &k)
	var a = make([]int, n)
	var b = make([]Array, n)
	for i = 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		//fmt.Print(i)
		b[i].idx = i
		b[i].x = a[i]
		//fmt.Print(b[i].idx)
	}
	sort.Sort(intArray(b))
	var ans = make([]int, k)
	var res int = 0

	for i = 0; i < k; i++ {
		fmt.Print(b[n - 1 - i].idx)
		ans[i] = b[n - 1 - i].idx
		res += b[n - 1 - i].x
	}
	fmt.Println()
	fmt.Print(b[0].idx, b[0].x)
	fmt.Print(ans)
	fmt.Println()
	sort.Sort(intSlice(ans))
	fmt.Print(ans)
	fmt.Println(res)
	for i = 0; i < k; i++ {
		if i == 0 {
			fmt.Print(ans[i] + 1, " ")
		} else {
			fmt.Print(ans[i] - ans[i - 1], " ")
		}
	}
}

package main

import (
	"fmt"
	"sort"
)

const (
	M = 2e3 + 5
)

type Array struct {
	x int
	idx int
}

type intArray []Array
type intSlice []int

var n, k int

func (s intArray) Len() int { return len(s) }
func (s intArray) Swap(i, j int){
	s[i].idx, s[j].idx = s[j].idx, s[i].idx
	s[i].x, s[j].x = s[j].x, s[i].x
}
func (s intArray) Less(i, j int) bool { return s[i].x < s[j].x }

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

func main() {
	var i int
	fmt.Scanf("%d%d", &n, &k)
	fmt.Print(n, k)
	var a = make([]int, n)
	var b = make([]Array, n)
	for i = 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		b[i].idx = i
		b[i].x = a[i]
	}
	sort.Sort(intArray(b))
	var ans = make([]int, 0)
	var res int = 0

	for i = 0; i < k; i++ {
		ans = append(ans, b[n - 1 - i].idx)
		res += b[n - 1 - i].x
	}
	sort.Sort(intSlice(ans))
	fmt.Println(res)
	for i = 0; i < k; i++ {
		if i == 0 {
			fmt.Print(ans[i] + 1, " ")
		} else {
			fmt.Print(ans[i] - ans[i - 1], " ")
		}
	}
}

package main

import (
	"fmt"
	"sort"
)

const (
	M = 2e3 + 5
)

type Array struct {
	x int
	idx int
}

type intArray []Array
type intSlice []int

var n, k int

func (s intArray) Len() int { return len(s) }
func (s intArray) Swap(i, j int){
	s[i].idx, s[j].idx = s[j].idx, s[i].idx
	s[i].x, s[j].x = s[j].x, s[i].x
}
func (s intArray) Less(i, j int) bool { return s[i].x < s[j].x }

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

func main() {
	var i int
	fmt.Scanf("%d%d", &n, &k)
	var a = make([]int, n)
	var b = make([]Array, n)
	for i = 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		b[i].idx = i
		b[i].x = a[i]
	}
	sort.Sort(intArray(b))
	var ans = make([]int, 0)
	var res int = 0

	for i = 0; i < k; i++ {
		ans = append(ans, b[n - 1 - i].idx)
		res += b[n - 1 - i].x
	}
	sort.Sort(intSlice(ans))
	fmt.Println(res)
	for i = 0; i < k; i++ {
		if i == 0 {
			fmt.Print(ans[i] + 1, " ")
		} else {
			fmt.Print(ans[i] - ans[i - 1], " ")
		}
	}
}

package main

import (
	"fmt"
	"sort"
)

const (
	M = 2e3 + 5
)

type Array struct {
	x int
	idx int
}

type intArray []Array
type intSlice []int

var n, k int

func (s intArray) Len() int { return len(s) }
func (s intArray) Swap(i, j int){
	s[i].idx, s[j].idx = s[j].idx, s[i].idx
	s[i].x, s[j].x = s[j].x, s[i].x
}
func (s intArray) Less(i, j int) bool { return s[i].x < s[j].x }

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

func main() {
	var i int
	fmt.Scanf("%d%d\n", &n, &k)
	var a = make([]int, n)
	var b = make([]Array, n)
	for i = 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		b[i].idx = i
		b[i].x = a[i]
	}
	sort.Sort(intArray(b))
	var ans = make([]int, k)
	var res int = 0

	for i = 0; i < k; i++ {
		ans[i] = b[n - 1 - i].idx
		res += b[n - 1 - i].x
	}
	sort.Sort(intSlice(ans))
	fmt.Println(res)
	for i = 0; i < k; i++ {
		if i == 0 {
			fmt.Print(ans[i] + 1, " ")
		} else {
			fmt.Print(ans[i] - ans[i - 1], " ")
		}
	}
}

package main

import (
	"fmt"
	"sort"
)

const (
	M = 2e3 + 5
)

type Array struct {
	x int
	idx int
}

type intArray []Array
type intSlice []int

var n, k int

func (s intArray) Len() int { return len(s) }
func (s intArray) Swap(i, j int){
	s[i].idx, s[j].idx = s[j].idx, s[i].idx
	s[i].x, s[j].x = s[j].x, s[i].x
}
func (s intArray) Less(i, j int) bool { return s[i].x < s[j].x }

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

func main() {
	var i int
	fmt.Scanf("%d%d", &n, &k)
	var a = make([]int, n)
	var b = make([]Array, n)
	for i = 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		//fmt.Print(i)
		b[i].idx = i
		b[i].x = a[i]
		//fmt.Print(b[i].idx)
	}
	sort.Sort(intArray(b))
	var ans = make([]int, 0)
	var res int = 0

	for i = 0; i < k; i++ {
		//fmt.Print(b[n - 1 - i].idx)
		ans = append(ans, b[n - 1 - i].idx)
		res += b[n - 1 - i].x
	}
	fmt.Print(b[0].idx, b[0].x)
	sort.Sort(intSlice(ans))
	fmt.Print(ans)
	fmt.Println(res)
	for i = 0; i < k; i++ {
		if i == 0 {
			fmt.Print(ans[i] + 1, " ")
		} else {
			fmt.Print(ans[i] - ans[i - 1], " ")
		}
	}
}

package main

import (
	"fmt"
	"sort"
)

const (
	M = 2e3 + 5
)

type Array struct {
	x int
	idx int
}

type intArray []Array
type intSlice []int

var n, k int

func (s intArray) Len() int { return len(s) }
func (s intArray) Swap(i, j int){
	s[i].idx, s[j].idx = s[j].idx, s[i].idx
	s[i].x, s[j].x = s[j].x, s[i].x
}
func (s intArray) Less(i, j int) bool { return s[i].x < s[j].x }

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

func main() {
	var i int
	fmt.Scanf("%d%d", &n, &k)
	var a = make([]int, n)
	var b = make([]Array, n)
	for i = 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		//fmt.Print(i)
		b[i].idx = i
		b[i].x = a[i]
		//fmt.Print(b[i].idx)
	}
	sort.Sort(intArray(b))
	var ans = make([]int, k)
	var res int = 0

	for i = 0; i < k; i++ {
		//fmt.Print(b[n - 1 - i].idx)
		ans[i] = b[n - 1 - i].idx
		res += b[n - 1 - i].x
	}
	fmt.Print(b[0].idx, b[0].x)
	sort.Sort(intSlice(ans))
	fmt.Print(ans)
	fmt.Println(res)
	for i = 0; i < k; i++ {
		if i == 0 {
			fmt.Print(ans[i] + 1, " ")
		} else {
			fmt.Print(ans[i] - ans[i - 1], " ")
		}
	}
}

package main

import (
	"fmt"
	"sort"
)

const (
	M = 2e3 + 5
)

type Array struct {
	x int
	idx int
}

type intArray []Array
type intSlice []int

var n, k int

func (s intArray) Len() int { return len(s) }
func (s intArray) Swap(i, j int){
	s[i].idx, s[j].idx = s[j].idx, s[i].idx
	s[i].x, s[j].x = s[j].x, s[i].x
}
func (s intArray) Less(i, j int) bool { return s[i].x < s[j].x }

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

func main() {
	var i int
	fmt.Scanf("%d%d", &n, &k)
	var a = make([]int, n)
	var b = make([]Array, n)
	for i = 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		//fmt.Print(i)
		b[i].idx = i
		b[i].x = a[i]
		//fmt.Print(b[i].idx)
	}
	fmt.Print(b)
	sort.Sort(intArray(b))
	fmt.Print(b)
	var ans = make([]int, k)
	var res int = 0

	for i = 0; i < k; i++ {
		ans[i] = b[n - 1 - i].idx
		res += b[n - 1 - i].x
	}
	sort.Sort(intSlice(ans))
	fmt.Println(res)
	for i = 0; i < k; i++ {
		if i == 0 {
			fmt.Print(ans[i] + 1, " ")
		} else {
			fmt.Print(ans[i] - ans[i - 1], " ")
		}
	}
}

package main

import (
	"fmt"
	"sort"
)

const (
	M = 2e3 + 5
)

type Array struct {
	x int
	idx int
}

type intArray []Array
type intSlice []int

var n, k int

func (s intArray) Len() int { return len(s) }
func (s intArray) Swap(i, j int){
	s[i].idx, s[j].idx = s[j].idx, s[i].idx
	s[i].x, s[j].x = s[j].x, s[i].x
}
func (s intArray) Less(i, j int) bool { return s[i].x < s[j].x }

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

func main() {
	var i int
	fmt.Scanf("%d%d", &n, &k)
	var a = make([]int, n)
	var b = make([]Array, n)
	for i = 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
		b[i].idx = i
		b[i].x = a[i]
	}
	sort.Sort(intArray(b))
	var ans = make([]int, 0)
	var res int = 0

	for i = 0; i < k; i++ {
		ans = append(ans, b[n - 1 - i].idx)
		res += b[n - 1 - i].x
	}
	sort.Sort(intSlice(ans))
	fmt.Print(ans)
	fmt.Println(res)
	for i = 0; i < k; i++ {
		if i == 0 {
			fmt.Print(ans[i] + 1, " ")
		} else {
			fmt.Print(ans[i] - ans[i - 1], " ")
		}
	}
}

package main

import (
	"fmt"
	"sort"
)

func ReadInt64() int64 {
	var ret int64
	cnt, err := fmt.Scanf("%d", &ret)
	if cnt != 1 || err != nil {
		//fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		return 0
	}
	return ret
}

func ReadString() string {
	var ret string
	cnt, err := fmt.Scanf("%s", ret)
	if cnt != 1 || err != nil {
		fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		return ""
	}
	return ret
}

type pair struct {
	first  int64
	second int64
}

func main() {
	n, k, sum := int64(0), int64(0), int64(0)
	n, k = ReadInt64(), ReadInt64()
	a, b := make([]pair, n), make([]pair, k+1)

	for i := int64(0); i < n; i++ {
		a[i].first = ReadInt64()
		a[i].second = i + 1
	}
	sort.Slice(a, func(i, j int) bool { return a[i].first < a[j].first })
	for i := int64(0); i < k; i++ {
		sum += a[n-i-1].first
	}
	fmt.Printf("%d\n", sum)
	for i := int64(1); i <= k; i++ {
		b[i] = a[n-i]
	}
	sort.Slice(b, func(i, j int) bool { return b[i].second < b[j].second })
	for i := int64(1); i < k; i++ {
		fmt.Printf("%d ", b[i].second-b[i-1].second)
	}
	fmt.Printf("%d\n", n-b[k-1].second)
}

package main

import (
	"fmt"
	"sort"
)

func ReadInt64() int64 {
	var ret int64
	_, err := fmt.Scanf("%d", &ret)
	if err != nil {
		//fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		return 0
	}
	return ret
}

func ReadString() string {
	var ret string
	cnt, err := fmt.Scanf("%s", ret)
	if cnt != 1 || err != nil {
		fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		return ""
	}
	return ret
}

type pair struct {
	first  int64
	second int64
}

func main() {
	n, k, sum := int64(0), int64(0), int64(0)
	n, k = ReadInt64(), ReadInt64()
	a, b := make([]pair, n), make([]pair, k+1)

	for i := int64(0); i < n; i++ {
		a[i].first = ReadInt64()
		a[i].second = i + 1
	}
	sort.Slice(a, func(i, j int) bool { return a[i].first < a[j].first })
	for i := int64(0); i < k; i++ {
		sum += a[n-i-1].first
	}
	fmt.Printf("%d\n", sum)
	for i := int64(1); i <= k; i++ {
		b[i] = a[n-i]
	}
	sort.Slice(b, func(i, j int) bool { return b[i].second < b[j].second })
	for i := int64(1); i < k; i++ {
		fmt.Printf("%d ", b[i].second-b[i-1].second)
	}
	fmt.Printf("%d\n", n-b[k-1].second)
}

package main

import (
	"fmt"
	"sort"
)

func ReadInt64() int64 {
	var ret int64
	cnt, err := fmt.Scanf("%d", &ret)
	if cnt != 1 || err != nil {
		fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		return 0
	}
	return ret
}

func ReadString() string {
	var ret string
	cnt, err := fmt.Scanf("%s", ret)
	if cnt != 1 || err != nil {
		fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		return ""
	}
	return ret
}

type pair struct {
	first  int64
	second int64
}

func main() {
	n, k, sum := int64(0), int64(0), int64(0)
	n, k = ReadInt64(), ReadInt64()
	a, b := make([]pair, n), make([]pair, k+1)

	for i := int64(0); i < n; i++ {
		a[i].first = ReadInt64()
		a[i].second = i + 1
	}
	sort.Slice(a, func(i, j int) bool { return a[i].first < a[j].first })
	for i := int64(0); i < k; i++ {
		sum += a[n-i-1].first
	}
	fmt.Printf("%d\n", sum)
	for i := int64(1); i <= k; i++ {
		b[i] = a[n-i]
	}
	sort.Slice(b, func(i, j int) bool { return b[i].second < b[j].second })
	for i := int64(1); i < k; i++ {
		fmt.Printf("%d ", b[i].second-b[i-1].second)
	}
	fmt.Printf("%d\n", n-b[k-1].second)
}

package main

import (
	"fmt"
	"sort"
)

func ReadInt64() int64 {
	var ret int64
	fmt.Scanf("%d", &ret)
	return ret
}

func ReadString() string {
	var ret string
	cnt, err := fmt.Scanf("%s", ret)
	if cnt != 1 || err != nil {
		fmt.Printf("cnt=%d, err=%s\n", cnt, err)
		return ""
	}
	return ret
}

type pair struct {
	first  int64
	second int64
}

func main() {
	n, k, sum := int64(0), int64(0), int64(0)
	n, k = ReadInt64(), ReadInt64()
	a, b := make([]pair, n), make([]pair, k+1)

	for i := int64(0); i < n; i++ {
		a[i].first = ReadInt64()
		a[i].second = i + 1
	}
	sort.Slice(a, func(i, j int) bool { return a[i].first < a[j].first })
	for i := int64(0); i < k; i++ {
		sum += a[n-i-1].first
	}
	fmt.Printf("%d\n", sum)
	for i := int64(1); i <= k; i++ {
		b[i] = a[n-i]
	}
	sort.Slice(b, func(i, j int) bool { return b[i].second < b[j].second })
	for i := int64(1); i < k; i++ {
		fmt.Printf("%d ", b[i].second-b[i-1].second)
	}
	fmt.Printf("%d\n", n-b[k-1].second)
}


Polycarp has decided to decorate his room because the New Year is soon. One of the main decorations that Polycarp will install is the garland he is going to solder himself.Simple garlands consisting of several lamps connected by one wire are too boring for Polycarp. He is going to solder a garland consisting of $$$n$$$ lamps and $$$n - 1$$$ wires. Exactly one lamp will be connected to power grid, and power will be transmitted from it to other lamps by the wires. Each wire connectes exactly two lamps; one lamp is called the main lamp for this wire (the one that gets power from some other wire and transmits it to this wire), the other one is called the auxiliary lamp (the one that gets power from this wire). Obviously, each lamp has at most one wire that brings power to it (and this lamp is the auxiliary lamp for this wire, and the main lamp for all other wires connected directly to it).Each lamp has a brightness value associated with it, the $$$i$$$-th lamp has brightness $$$2^i$$$. We define the importance of the wire as the sum of brightness values over all lamps that become disconnected from the grid if the wire is cut (and all other wires are still working).Polycarp has drawn the scheme of the garland he wants to make (the scheme depicts all $$$n$$$ lamp and $$$n - 1$$$ wires, and the lamp that will be connected directly to the grid is marked; the wires are placed in such a way that the power can be transmitted to each lamp). After that, Polycarp calculated the importance of each wire, enumerated them from $$$1$$$ to $$$n - 1$$$ in descending order of their importance, and then wrote the index of the main lamp for each wire (in the order from the first wire to the last one).The following day Polycarp bought all required components of the garland and decided to solder it — but he could not find the scheme. Fortunately, Polycarp found the list of indices of main lamps for all wires. Can you help him restore the original scheme?

The first line contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of lamps. The second line contains $$$n - 1$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_{n - 1}$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the index of the main lamp for the $$$i$$$-th wire (wires are numbered in descending order of importance).

If it is impossible to restore the original scheme, print one integer $$$-1$$$. Otherwise print the scheme as follows. In the first line, print one integer $$$k$$$ ($$$1 \le k \le n$$$) — the index of the lamp that is connected to the power grid. Then print $$$n - 1$$$ lines, each containing two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le n$$$, $$$x_i \ne y_i$$$) — the indices of the lamps connected by some wire. The descriptions of the wires (and the lamps connected by a wire) can be printed in any order. The printed description must correspond to a scheme of a garland such that Polycarp could have written the list $$$a_1$$$, $$$a_2$$$, ..., $$$a_{n - 1}$$$ from it. If there are multiple such schemes, output any of them.

NoteThe scheme for the first example (R denotes the lamp connected to the grid, the numbers on wires are their importance values):

package main

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

var writer *bufio.Writer
var reader *bufio.Reader

type edge struct {
    x, y int
}

func (e edge) print(writer *bufio.Writer) {
    fmt.Fprintf(writer, "%d %d\n", e.x, e.y)
}

var n int
var a []int
var was []bool
var edges []edge

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

func solve() {
    edges = make([]edge, 0, n - 1)
    was = make([]bool, n + 1, n + 1)
    var target = n
    was[target] = true
    var last = -1
    for i := 1; i < n; i++ {
        if was[a[i]] {
            if last > 0 {
                edges = append(edges, edge{last, target})
                was[target] = true
            }
            target--
            for ; was[target]; target-- {
            }
            was[target] = true
            //fmt.Fprintln(io.Writer(os.Stderr), target)
            last = a[i]
        } else {
            was[a[i]] = true
            if last > 0 {
                edges = append(edges, edge{last, a[i]})
            }
            last = a[i]
        }
    }
    edges = append(edges, edge{last, target})
}

func print_answer() {
    fmt.Fprintf(writer, "%d\n", a[1])
    for i := range edges {
        //fmt.Fprintf(writer, "%d %d\n", edges[i].x, edges[i].y)
        edges[i].print(writer)
    }
}

func main() {
    reader = bufio.NewReader(os.Stdin)
    writer = bufio.NewWriter(os.Stdout)
    defer writer.Flush()
    init_data()
    solve()
    print_answer()
}

package main

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

var writer *bufio.Writer
var reader *bufio.Reader

type edge struct {
    x, y int
}

func (e edge) print(writer *bufio.Writer) {
    fmt.Fprintf(writer, "%d %d\n", e.x, e.y)
}

var n int
var a []int
var was []bool
var edges []edge

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

func solve() {
    edges = make([]edge, 0, n - 1)
    was = make([]bool, n + 1, n + 1)
    var target = n
    was[target] = true
    var last = -1
    for i := 1; i < n; i++ {
        if was[a[i]] {
            if last > 0 {
                edges = append(edges, edge{last, target})
                was[target] = true
            }
            target--
            for ; was[target]; target-- {
            }
            was[target] = true
            //fmt.Fprintln(io.Writer(os.Stderr), target)
            last = a[i]
        } else {
            was[a[i]] = true
            if last > 0 {
                edges = append(edges, edge{last, a[i]})
            }
            last = a[i]
        }
    }
    edges = append(edges, edge{last, target})
}

func print_answer() {
    for i := range edges {
        //fmt.Fprintf(writer, "%d %d\n", edges[i].x, edges[i].y)
        edges[i].print(writer)
    }
}

func main() {
    reader = bufio.NewReader(os.Stdin)
    writer = bufio.NewWriter(os.Stdout)
    defer writer.Flush()
    init_data()
    solve()
    print_answer()
}


This is the easy version of the problem. The difference between the versions is the constraint on $$$n$$$ and the required number of operations. You can make hacks only if all versions of the problem are solved.There are two binary strings $$$a$$$ and $$$b$$$ of length $$$n$$$ (a binary string is a string consisting of symbols $$$0$$$ and $$$1$$$). In an operation, you select a prefix of $$$a$$$, and simultaneously invert the bits in the prefix ($$$0$$$ changes to $$$1$$$ and $$$1$$$ changes to $$$0$$$) and reverse the order of the bits in the prefix.For example, if $$$a=001011$$$ and you select the prefix of length $$$3$$$, it becomes $$$011011$$$. Then if you select the entire string, it becomes $$$001001$$$.Your task is to transform the string $$$a$$$ into $$$b$$$ in at most $$$3n$$$ operations. It can be proved that it is always possible.

The first line contains a single integer $$$t$$$ ($$$1\le t\le 1000$$$)  — the number of test cases. Next $$$3t$$$ lines contain descriptions of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1\le n\le 1000$$$)  — the length of the binary strings. The next two lines contain two binary strings $$$a$$$ and $$$b$$$ of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$1000$$$.

For each test case, output an integer $$$k$$$ ($$$0\le k\le 3n$$$), followed by $$$k$$$ integers $$$p_1,\ldots,p_k$$$ ($$$1\le p_i\le n$$$). Here $$$k$$$ is the number of operations you use and $$$p_i$$$ is the length of the prefix you flip in the $$$i$$$-th operation.

NoteIn the first test case, we have $$$01\to 11\to 00\to 10$$$.In the second test case, we have $$$01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$$$.In the third test case, the strings are already the same. Another solution is to flip the prefix of length $$$2$$$, which will leave $$$a$$$ unchanged.

package main

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

/*
6
OEYCDK
EYOKCD
KDCEOY
CKHOYE
YOEDKC
DCKYEO
3
IWL
GIW
WLI
*/

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

type priorityQueue [][2]int

func (pq priorityQueue) Len() int {
	return len(pq)
}

func (pq priorityQueue) Less(i, j int) bool {
	return pq[i][1] > pq[j][1]
}

func (pq priorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *priorityQueue) Push(x interface{}) {
	*pq = append(*pq, x.([2]int))
}

func (pq *priorityQueue) Pop() (res interface{}) {
	res = (*pq)[len(*pq)-1]
	*pq = (*pq)[:len(*pq)-1]
	return res
}

var err error

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 next() (n string) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

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
}

var (
	names map[string]int
	words []string
	withs [][]string
	res   []string
	has   []bool
)

func dfs(ind int) {
	if has[ind] {
		return
	}
	has[ind] = true
	for i := len(withs[ind]) - 1; i >= 0; i-- {
		dfs(names[withs[ind][i]])
	}
	res = append(res, words[ind])
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	if false {
		name := "in"
		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()

	for cases := nextInt(); cases > 0; cases-- {
		n := nextInt()
		a, b := next(), next()
		ord := make([]int, n)
		for i := 0; i <= (n-1)/2; i++ {
			ord[2*i] = i
		}
		for i, curr := n-1, 1; curr < n; i, curr = i-1, curr+2 {
			ord[curr] = i
		}
		res := []int{}
		for i := 0; i < n; i++ {
			x, v := n-1-i, byte(0)
			if i%2 == 1 {
				v = a[ord[i]] ^ 1
			} else {
				v = a[ord[i]]
			}
			if v == b[x] {
				res = append(res, 1)
			}
			res = append(res, x+1)
		}
		printf("%d", len(res))
		for _, v := range res {
			printf(" %d", v)
		}
		printf("\n")
	}
}

package main

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

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

	for t := io.nextInt(); t > 0; t-- {
		n := io.nextInt()
		a, b := []byte(io.next()), []byte(io.next())

		i, j, inv, dir := 0, n-1, byte(0), 1
		ops := []int{}
		for k := n - 1; k >= 0; k-- {
			if a[j]^inv != b[k] {
				if a[i]^inv == b[k] {
					ops = append(ops, 1)
					a[i] ^= 1
				}
				ops = append(ops, k+1)
				i, j = j, i+dir
				dir = -dir
				inv ^= 1
			} else {
				j -= dir
			}
		}
		io.print(len(ops), " ")
		io.printlnInts(ops...)
	}
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for !('0' <= f.p[0] && f.p[0] <= '9') {
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	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) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

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

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

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

	var T, n int
	var a, b []byte
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &a, &b)
		ans := []interface{}{}
		for i, c := range a {
			if i > 0 && c != a[i-1] {
				ans = append(ans, i)
			}
		}
		if a[n-1] != b[n-1] {
			ans = append(ans, n)
		}
		for i := len(b) - 2; i >= 0; i-- {
			if b[i] != b[i+1] {
				ans = append(ans, i+1)
			}
		}
		Fprint(out, len(ans), " ")
		Fprintln(out, ans...)
	}
}

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

package main

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

func main() {
	var t int
	fmt.Scan(&t)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	for i := 0; i < t; i++ {
		scanner.Scan()
		n := toInt(scanner.Bytes())

		scanner.Scan()
		a := scanner.Text()

		scanner.Scan()
		b := scanner.Text()
		var ans []int

		for j := 0; j < n; j++ {
			if string(a[j]) != string(b[j]) {
				ans = append(ans, (j + 1))
				ans = append(ans, 1)
				ans = append(ans, (j + 1))
			}
		}
		fmt.Printf("%d ", len(ans))
		for _, v := range ans {
			fmt.Printf("%d ", v)
		}
	}
}

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

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF = int64(math.MaxInt64)
	MOD = 1e9 + 7
)

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

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

func abs(a int64) int64 {
	if a < 0 {
		return a * -1
	}
	return a
}

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		_ = sint64()
		a := sstring()
		b := sstring()
		res := sol([]byte(a), []byte(b))
		printf("%d ", len(res))
		for _, i := range res {
			printf("%d ", i)
		}
		printf("\n")
	}
}

func sol(a, b []byte) []int64 {
	ret := []int64{}
	for i := int64(len(b) - 1); i > 0; i-- {
		if a[i] == b[i] {
			continue
		}
		if a[0] != a[i] {
			ret = append(ret, 1)
			if a[0] == '0' {
				a[0] = '1'
			} else {
				a[0] = '0'
			}
		}
		ret = append(ret, i+1)
		l := int64(0)
		r := i
		for l < r {
			a[l], a[r] = a[r], a[l]
			l++
			r--
		}
		for j := int64(0); j <= i; j++ {
			if a[j] == '0' {
				a[j] = '1'
			} else {
				a[j] = '0'
			}
		}
	}
	if a[0] != b[0] {
		ret = append(ret, 1)
	}
	// printf("%s %s\n", string(a), string(b))
	return ret
}