There are $$$n$$$ robots driving along an OX axis. There are also two walls: one is at coordinate $$$0$$$ and one is at coordinate $$$m$$$.The $$$i$$$-th robot starts at an integer coordinate $$$x_i~(0 < x_i < m)$$$ and moves either left (towards the $$$0$$$) or right with the speed of $$$1$$$ unit per second. No two robots start at the same coordinate.Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.For each robot find out if it ever explodes and print the time of explosion if it happens and $$$-1$$$ otherwise.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$2 \le m \le 10^8$$$) — the number of robots and the coordinate of the right wall. The second line of each testcase contains $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$0 < x_i < m$$$) — the starting coordinates of the robots. The third line of each testcase contains $$$n$$$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right). All coordinates $$$x_i$$$ in the testcase are distinct. The sum of $$$n$$$ over all testcases doesn't exceed $$$3 \cdot 10^5$$$.

For each testcase print $$$n$$$ integers — for the $$$i$$$-th robot output the time it explodes at if it does and $$$-1$$$ otherwise.

NoteHere is the picture for the seconds $$$0, 1, 2$$$ and $$$3$$$ of the first testcase:   Notice that robots $$$2$$$ and $$$3$$$ don't collide because they meet at the same point $$$2.5$$$, which is not integer.After second $$$3$$$ robot $$$6$$$ just drive infinitely because there's no robot to collide with.

package main

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

type robot struct {
	x, i  int
	right bool
}

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

	var T, n, m int
	var dir string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		robots := [2][]robot{}
		pos := make([]int, n)
		for i := range pos {
			Fscan(in, &pos[i])
		}
		for i, x := range pos {
			Fscan(in, &dir)
			robots[x&1] = append(robots[x&1], robot{x, i, dir == "R"})
		}

		ans := make([]int, n)
		for _, a := range robots {
			sort.Slice(a, func(i, j int) bool { return a[i].x < a[j].x })
			var l, r []robot
			for _, p := range a {
				if p.right {
					r = append(r, p)
				} else if len(r) == 0 {
					l = append(l, p)
				} else {
					q := r[len(r)-1]
					r = r[:len(r)-1]
					t := (p.x - q.x) / 2
					ans[p.i] = t
					ans[q.i] = t
				}
			}
			for len(l) > 1 {
				p, q := l[0], l[1]
				l = l[2:]
				t := (p.x + q.x) / 2
				ans[p.i] = t
				ans[q.i] = t
			}
			for len(r) > 1 {
				p, q := r[len(r)-1], r[len(r)-2]
				r = r[:len(r)-2]
				t := (2*m - p.x - q.x) / 2
				ans[p.i] = t
				ans[q.i] = t
			}
			if len(l) == 1 && len(r) == 1 {
				p, q := l[0], r[0]
				t := (2*m - q.x + p.x) / 2
				ans[p.i] = t
				ans[q.i] = t
			}
		}
		for _, t := range ans {
			if t == 0 {
				t = -1
			}
			Fprint(out, t, " ")
		}
		Fprintln(out)
	}
}

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

// github.com/EndlessCheng/codeforces-go
func main(){
	in:=bufio.NewReader(Stdin)
	out:=bufio.NewWriter(Stdout)
	type car struct{x,i int;r bool}
	var T,n,m int;s:=""
	for Fscan(in,&T);T>0;T--{
		Fscan(in,&n,&m)
		a:=[2][]car{}
		b:=make([]int,n)
		for i:=range b{Fscan(in,&b[i])}
		for i,x:=range b{Fscan(in,&s);a[x&1]=append(a[x&1],car{x,i,s=="R"})}
		ans:=make([]int,n)
		for _,a:=range a{
			sort.Slice(a,func(i,j int)bool{return a[i].x<a[j].x})
			var l,r []car
			for _,p:=range a{
				if p.r{r=append(r,p)
				}else if len(r)==0{l=append(l,p)
				}else{
					q:=r[len(r)-1]
					r=r[:len(r)-1]
					t:=(p.x-q.x)/2
					ans[p.i]=t
					ans[q.i]=t
				}
			}
			for len(l)>1{
				p,q:=l[0],l[1]
				l=l[2:]
				t:=(p.x+q.x)/2
				ans[p.i]=t
				ans[q.i]=t
			}
			for len(r)>1{
				p,q:=r[len(r)-1],r[len(r)-2]
				r=r[:len(r)-2]
				t:=(2*m-p.x-q.x)/2
				ans[p.i]=t
				ans[q.i]=t
			}
			if len(l)+len(r)>1{
				p,q:=l[0],r[0]
				t:=(2*m-q.x+p.x)/2
				ans[p.i]=t
				ans[q.i]=t
			}
		}
		for _,v:=range ans{
			if v==0{v=-1}
			Fprint(out,v," ")
		}
		Fprintln(out)
	}
	out.Flush()
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"sort"
	"strings"
)

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct {
		x, id int
		right bool
	}

	solve := func(Case int) {
		var n, m int
		var s string
		Fscan(in, &n, &m)
		a := [2][]pair{}
		tmp := make([]int, n)
		for i := range tmp {
			Fscan(in, &tmp[i])
		}
		for i, x := range tmp {
			Fscan(in, &s)
			a[x&1] = append(a[x&1], pair{x, i, s[0] == 'R'})
		}

		ans := make([]int, n)
		for i := range ans {
			ans[i] = -1
		}
		for _, a := range a {
			sort.Slice(a, func(i, j int) bool { return a[i].x < a[j].x })
			left := []pair{}
			right := []pair{}
			for _, p := range a {
				if p.right {
					right = append(right, p)
				} else if len(right) == 0 {
					left = append(left, p)
				} else {
					q := right[len(right)-1]
					right = right[:len(right)-1]
					t := (p.x - q.x) / 2
					ans[p.id] = t
					ans[q.id] = t
				}
			}
			for len(left) > 1 {
				p, q := left[0], left[1]
				left = left[2:]
				t := (p.x + q.x) / 2
				ans[p.id] = t
				ans[q.id] = t
			}
			for len(right) > 1 {
				p, q := right[len(right)-1], right[len(right)-2]
				right = right[:len(right)-2]
				t := (2*m - p.x - q.x) / 2
				ans[p.id] = t
				ans[q.id] = t
			}
			if len(left) == 1 && len(right) == 1 {
				p, q := left[0], right[0]
				t := (2*m - q.x + p.x) / 2
				ans[p.id] = t
				ans[q.id] = t
			}
		}
		for _, v := range ans {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}

	T := 1
	Fscan(in, &T) //
	for Case := 1; Case <= T; Case++ {
		solve(Case)
	}

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

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

package main

import (

	"fmt"

	"sort"
	"math/rand"
	"time"
	"bufio"
	"os"
)

func main() {

	rand.Seed(time.Now().UnixNano())
	w := bufio.NewWriter(os.Stdout)

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)
	//t = 1000

	//start := time.Now().UnixNano()

	for i := 0; i < t; i++ {

		var n, m int
		fmt.Fscan(r, &n, &m)
		//n = 300
		//m = 100000000

		x := make([]int, n)

		for j := 0; j < n; j++ {
			fmt.Fscan(r, &x[j])
			//x[j] = 1 + rand.Intn(m - 1)
		}
		d := make([]string, n)

		for j := 0; j < n; j++ {
			fmt.Fscan(r, &d[j])
			/*v := rand.Intn(1)
			if v == 1 {
				d[j] = "R"
			} else {
				d[j] = "L"
			}*/

		}

		ans := solve(x, d, m)

		for i := 0 ; i < len(ans); i++ {
			fmt.Fprintf(w, "%d ", ans[i])
		}
		fmt.Fprintln(w, "\n")
	}
	w.Flush()

	//end := time.Now().UnixNano()
	//fmt.Println((end - start)/1000000)
}

type pair struct {
	v int
	i int
}

type ByV []pair

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

func solve(x []int, d[]string, m int) []int64 {

	var pairs []pair

	for j := 0; j < len(x); j++ {
		pairs = append(pairs, pair{x[j], j})
	}

	sort.Sort(ByV(pairs))

	ans := make([]int64, len(x))
	for j := 0; j < len(ans); j++ {
		ans[j] = -1
	}

	var stack []int

	for i := 0 ; i < len(pairs); i++ {
		p := pairs[i]
		if p.v % 2 == 0 {
			if d[p.i] == "L" {
				if len(stack) != 0 {
					idx := stack[len(stack) - 1]
					stack = stack[ : len(stack) - 1]

					if d[idx] == "L" {
						t := (x[idx] + x[p.i])/2
						ans[p.i] = int64(t)
						ans[idx] = int64(t)
					} else {
						t := (x[p.i] - x[idx])/2
						ans[p.i] = int64(t)
						ans[idx] = int64(t)
					}

				} else {
					stack = append(stack, p.i)
				}
			} else {
				stack = append(stack, p.i)
			}

		}
	}

	if len(stack) >= 2 {

		for len(stack) >= 2 {

			last := stack[len(stack) - 1]
			prev := stack[len(stack) - 2]

			if d[prev] == "R" {
				t := (m - x[last] + m - x[prev])/2
				ans[last] = int64(t)
				ans[prev] = int64(t)

			} else {

				if m - x[last] > x[prev] {

					d := (m - x[last]) - x[prev]

					t := (m - d)/2 + m - x[last]
					ans[last] = int64(t)
					ans[prev] = int64(t)
				} else {

					t := x[prev] + (m + (m - x[last]) - x[prev])/2
					ans[last] = int64(t)
					ans[prev] = int64(t)
				}
			}

			stack = stack[: len(stack) - 2]
		}
	}

	stack = []int{}

	for i := 0 ; i < len(pairs); i++ {
		p := pairs[i]
		if p.v % 2 == 1 {
			if d[p.i] == "L" {
				if len(stack) != 0 {
					idx := stack[len(stack) - 1]
					stack = stack[ : len(stack) - 1]

					if d[idx] == "L" {
						t := (x[idx] + x[p.i])/2
						ans[p.i] = int64(t)
						ans[idx] = int64(t)
					} else {
						t := (x[p.i] - x[idx])/2
						ans[p.i] = int64(t)
						ans[idx] = int64(t)
					}

				} else {
					stack = append(stack, p.i)
				}
			} else {
				stack = append(stack, p.i)
			}

		}
	}

	if len(stack) >= 2 {

		for len(stack) >= 2 {

			last := stack[len(stack) - 1]
			prev := stack[len(stack) - 2]

			if d[prev] == "R" {
				t := (m - x[last] + m - x[prev])/2
				ans[last] = int64(t)
				ans[prev] = int64(t)

			} else {

				if m - x[last] > x[prev] {

					d := (m - x[last]) - x[prev]

					t := (m - d)/2 + m - x[last]
					ans[last] = int64(t)
					ans[prev] = int64(t)
				} else {

					t := x[prev] + (m + (m - x[last]) - x[prev])/2
					ans[last] = int64(t)
					ans[prev] = int64(t)
				}

			}

			stack = stack[: len(stack) - 2]
		}
	}

	return ans
}

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

// github.com/EndlessCheng/codeforces-go
func main(){
	in:=bufio.NewReader(Stdin)
	out:=bufio.NewWriter(Stdout)
	type car struct{x,i int;r bool}
	var T,n,m int;s:=""
	for Fscan(in,&T);T>0;T--{
		Fscan(in,&n,&m)
		a:=[2][]car{}
		b:=make([]int,n)
		for i:=range b{Fscan(in,&b[i])}
		for i,x:=range b{Fscan(in,&s);a[x&1]=append(a[x&1],car{x,i,s=="R"})}
		ans:=make([]int,n)
		for _,a:=range a{
			sort.Slice(a,func(i,j int)bool{return a[i].x<a[j].x})
			var l,r []car
			for _,p:=range a{
				if p.r{r=append(r,p)
				}else if len(r)==0{l=append(l,p)
				}else{
					q:=r[len(r)-1]
					r=r[:len(r)-1]
					t:=(p.x-q.x)/2
					ans[p.i]=t
					ans[q.i]=t
				}
			}
			for len(l)>1{
				p,q:=l[0],l[1]
				l=l[2:]
				t:=(p.x+q.x)/2
				ans[p.i]=t
				ans[q.i]=t
			}
			for len(r)>1{
				p,q:=r[len(r)-1],r[len(r)-2]
				r=r[:len(r)-2]
				t:=(2*m-p.x-q.x)/2
				ans[p.i]=t
				ans[q.i]=t
			}
			if len(l)+len(r)>1{
				p,q:=l[0],r[0]
				t:=(2*m-q.x+p.x)/2
				ans[p.i]=t
				ans[q.i]=t
			}
		}
		for _,v:=range ans{
			if v==0{v=-1}
			Fprint(out,v,"")
		}
		Fprintln(out)
	}
	out.Flush()
}

package main

import (

	"fmt"

	"sort"
	"math/rand"
	"time"
	"bufio"
	"os"
)

func main() {

	rand.Seed(time.Now().UnixNano())
	w := bufio.NewWriter(os.Stdout)

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)
	//t = 1000

	start := time.Now().UnixNano()

	for i := 0; i < t; i++ {

		var n, m int
		fmt.Fscan(r, &n, &m)
		//n = 300
		//m = 100000000

		x := make([]int, n)

		for j := 0; j < n; j++ {
			fmt.Fscan(r, &x[j])
			//x[j] = 1 + rand.Intn(m - 1)
		}
		d := make([]string, n)

		for j := 0; j < n; j++ {
			fmt.Fscan(r, &d[j])
			/*v := rand.Intn(1)
			if v == 1 {
				d[j] = "R"
			} else {
				d[j] = "L"
			}*/

		}

		ans := solve(x, d, m)

		for i := 0 ; i < len(ans); i++ {
			fmt.Fprintf(w, "%d ", ans[i])
		}
		fmt.Fprintln(w, "\n")
	}
	w.Flush()

	end := time.Now().UnixNano()
	fmt.Println((end - start)/1000000)
}

type pair struct {
	v int
	i int
}

type ByV []pair

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

func solve(x []int, d[]string, m int) []int64 {

	var pairs []pair

	for j := 0; j < len(x); j++ {
		pairs = append(pairs, pair{x[j], j})
	}

	sort.Sort(ByV(pairs))

	ans := make([]int64, len(x))
	for j := 0; j < len(ans); j++ {
		ans[j] = -1
	}

	var stack []int

	for i := 0 ; i < len(pairs); i++ {
		p := pairs[i]
		if p.v % 2 == 0 {
			if d[p.i] == "L" {
				if len(stack) != 0 {
					idx := stack[len(stack) - 1]
					stack = stack[ : len(stack) - 1]

					if d[idx] == "L" {
						t := (x[idx] + x[p.i])/2
						ans[p.i] = int64(t)
						ans[idx] = int64(t)
					} else {
						t := (x[p.i] - x[idx])/2
						ans[p.i] = int64(t)
						ans[idx] = int64(t)
					}

				} else {
					stack = append(stack, p.i)
				}
			} else {
				stack = append(stack, p.i)
			}

		}
	}

	if len(stack) >= 2 {

		for len(stack) >= 2 {

			last := stack[len(stack) - 1]
			prev := stack[len(stack) - 2]

			if d[prev] == "R" {
				t := (m - x[last] + m - x[prev])/2
				ans[last] = int64(t)
				ans[prev] = int64(t)

			} else {

				if m - x[last] > x[prev] {

					d := (m - x[last]) - x[prev]

					t := (m - d)/2 + m - x[last]
					ans[last] = int64(t)
					ans[prev] = int64(t)
				} else {

					t := x[prev] + (m + (m - x[last]) - x[prev])/2
					ans[last] = int64(t)
					ans[prev] = int64(t)
				}
			}

			stack = stack[: len(stack) - 2]
		}
	}

	stack = []int{}

	for i := 0 ; i < len(pairs); i++ {
		p := pairs[i]
		if p.v % 2 == 1 {
			if d[p.i] == "L" {
				if len(stack) != 0 {
					idx := stack[len(stack) - 1]
					stack = stack[ : len(stack) - 1]

					if d[idx] == "L" {
						t := (x[idx] + x[p.i])/2
						ans[p.i] = int64(t)
						ans[idx] = int64(t)
					} else {
						t := (x[p.i] - x[idx])/2
						ans[p.i] = int64(t)
						ans[idx] = int64(t)
					}

				} else {
					stack = append(stack, p.i)
				}
			} else {
				stack = append(stack, p.i)
			}

		}
	}

	if len(stack) >= 2 {

		for len(stack) >= 2 {

			last := stack[len(stack) - 1]
			prev := stack[len(stack) - 2]

			if d[prev] == "R" {
				t := (m - x[last] + m - x[prev])/2
				ans[last] = int64(t)
				ans[prev] = int64(t)

			} else {

				if m - x[last] > x[prev] {

					d := (m - x[last]) - x[prev]

					t := (m - d)/2 + m - x[last]
					ans[last] = int64(t)
					ans[prev] = int64(t)
				} else {

					t := x[prev] + (m + (m - x[last]) - x[prev])/2
					ans[last] = int64(t)
					ans[prev] = int64(t)
				}

			}

			stack = stack[: len(stack) - 2]
		}
	}

	return ans
}


You are given $$$n$$$ rectangles on a plane with coordinates of their bottom left and upper right points. Some $$$(n-1)$$$ of the given $$$n$$$ rectangles have some common point. A point belongs to a rectangle if this point is strictly inside the rectangle or belongs to its boundary.Find any point with integer coordinates that belongs to at least $$$(n-1)$$$ given rectangles.

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 132\,674$$$) — the number of given rectangles. Each the next $$$n$$$ lines contains four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$ and $$$y_2$$$ ($$$-10^9 \le x_1 < x_2 \le 10^9$$$, $$$-10^9 \le y_1 < y_2 \le 10^9$$$) — the coordinates of the bottom left and upper right corners of a rectangle.

Print two integers $$$x$$$ and $$$y$$$ — the coordinates of any point that belongs to at least $$$(n-1)$$$ given rectangles.

NoteThe picture below shows the rectangles in the first and second samples. The possible answers are highlighted.  The picture below shows the rectangles in the third and fourth samples.

package main

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

type rectangle struct {
	x1 int
	y1 int
	x2 int
	y2 int
}

// MAX - baby
const MAX = 1000000000 //10^9

func swap(sl []rectangle, x int, y int) {
	tmp := sl[x]
	sl[x] = sl[y]
	sl[y] = tmp
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	var n int
	if scanner.Scan() {
		n, _ = strconv.Atoi(scanner.Text())
	}
	recs := make([]rectangle, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		recs[i].x1, _ = strconv.Atoi(scanner.Text())

		scanner.Scan()
		recs[i].y1, _ = strconv.Atoi(scanner.Text())

		scanner.Scan()
		recs[i].x2, _ = strconv.Atoi(scanner.Text())

		scanner.Scan()
		recs[i].y2, _ = strconv.Atoi(scanner.Text())
	}

	r := rand.New(rand.NewSource(123456))
	for i := 0; i < n; i++ {
		j := r.Intn(n)
		if i == j {
			continue
		}
		swap(recs, i, j)
	}

	lidx := 0
	current := rectangle{x1: -MAX, y1: -MAX, x2: MAX, y2: MAX}
	for {
		count := 0
		ridx := -1
		sofar := current
		//fmt.Println(lidx)
		for i := lidx; i < n; i++ {
			next, applied := cross(&current, &recs[i])
			//fmt.Println(lidx, i, applied)
			if !applied {
				count++
				ridx = i
				if count == 2 {
					break
				}
			} else {
				current = next
			}
		}
		if count == 0 {
			break
		}
		if count == 1 {
			swap(recs, ridx, n-1)
			break
		}
		current = sofar
		for ridx < n {
			swap(recs, lidx, ridx)
			current, _ = cross(&current, &recs[lidx])
			ridx++
			lidx++
		}
	}
	current = rectangle{x1: -MAX, y1: -MAX, x2: MAX, y2: MAX}
	for i := 0; i < (n - 1); i++ {
		current, _ = cross(&current, &recs[i])
	}

	fmt.Println(current.x1, current.y1)
}

func cross(rec1 *rectangle, rec2 *rectangle) (rectangle, bool) {
	x1 := int(math.Max(float64(rec1.x1), float64(rec2.x1)))
	y1 := int(math.Max(float64(rec1.y1), float64(rec2.y1)))

	x2 := int(math.Min(float64(rec1.x2), float64(rec2.x2)))
	y2 := int(math.Min(float64(rec1.y2), float64(rec2.y2)))

	if x1 <= x2 && y1 <= y2 {
		return rectangle{x1: x1, y1: y1, x2: x2, y2: y2}, true
	}
	return rectangle{}, false
}


Let's call a list of positive integers $$$a_0, a_1, ..., a_{n-1}$$$ a power sequence if there is a positive integer $$$c$$$, so that for every $$$0 \le i \le n-1$$$ then $$$a_i = c^i$$$.Given a list of $$$n$$$ positive integers $$$a_0, a_1, ..., a_{n-1}$$$, you are allowed to:  Reorder the list (i.e. pick a permutation $$$p$$$ of $$$\{0,1,...,n - 1\}$$$ and change $$$a_i$$$ to $$$a_{p_i}$$$), then  Do the following operation any number of times: pick an index $$$i$$$ and change $$$a_i$$$ to $$$a_i - 1$$$ or $$$a_i + 1$$$ (i.e. increment or decrement $$$a_i$$$ by $$$1$$$) with a cost of $$$1$$$. Find the minimum cost to transform $$$a_0, a_1, ..., a_{n-1}$$$ into a power sequence.

The first line contains an integer $$$n$$$ ($$$3 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_0, a_1, ..., a_{n-1}$$$ ($$$1 \le a_i \le 10^9$$$).

Print the minimum cost to transform $$$a_0, a_1, ..., a_{n-1}$$$ into a power sequence.

NoteIn the first example, we first reorder $$$\{1, 3, 2\}$$$ into $$$\{1, 2, 3\}$$$, then increment $$$a_2$$$ to $$$4$$$ with cost $$$1$$$ to get a power sequence $$$\{1, 2, 4\}$$$.

package main

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

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

	in := func() int {
		sc.Scan()
		i, _ := strconv.Atoi(sc.Text())
		return i
	}

	n := in()
	a := make([]float64, n)

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

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	c := math.Ceil(math.Pow(a[n-1], 1.0/float64(n-1)))

	var minRes float64 = math.MaxFloat64

	for j := c; j >= 1; j-- {
		var res float64 = 0

		var val float64 = 1
		for i := range a {
			res += math.Abs(a[i]-val)
			if val > math.MaxFloat64/j {
				res =  math.MaxFloat64
				break
			}
			val *= j
		}

		if res <= minRes {
			minRes = res
		} else {
			break
		}
	}

	fmt.Println(uint64(minRes))
}

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

package main

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

var (
	n int
	A []int64
)

func main() {
	n = readi()
	A = readlls(n)

	sort.Slice(A, func(i, j int) bool {
		return A[i] < A[j]
	})
	// PrintfDebug("%v\n", A)

	base := C1()

	if n > 60 {
		fmt.Println(base)
		return
	}

	ans := base
	for c := int64(2); c <= 100000; c++ {
		tmp := calc(c)
		ChMin(&ans, tmp)
	}
	fmt.Println(ans)
}

func calc(a int64) int64 {
	res := int64(0)

	cur := int64(1)
	res += AbsInt(A[0] - cur)

	// F := Kiriage(INF_BIT60, a)

	for i := 1; i <= n-1; i++ {
		// if cur > F {
		// 	return INF_BIT60
		// }
		if !IsProductLeq(cur, a, INF_BIT60) {
			return INF_BIT60
		}

		cur *= a
		res += AbsInt(A[i] - cur)
	}

	return res
}

// IsProductLeq returns whether a*b <= ub or not.
// IsProductLeq panics when it accepts negative integers.
func IsProductLeq(a, b, ub int64) bool {
	if a < 0 || b < 0 || ub < 0 {
		panic("IsProductLeq does not accept negative integers")
	}

	return a <= ub/b
}

// IsSumLeq returns whether a*b <= ub or not.
// IsSumLeq panics when it accepts negative integers.
func IsSumLeq(a, b, ub int64) bool {
	if a < 0 || b < 0 || ub < 0 {
		panic("IsSumLeq does not accept negative integers")
	}

	return a <= ub-b
}

func C1() int64 {
	ans := int64(0)
	for i := 0; i < n; i++ {
		ans += AbsInt(A[i] - 1)
	}
	return ans
}

// Kiriage returns Ceil(a/b)
// a >= 0, b > 0
func Kiriage(a, b int64) int64 {
	return (a + (b - 1)) / b
}

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

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

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

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

const (
	// General purpose
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

	return res
}

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

package main

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

var (
	n int
	A []int64
)

func main() {
	n = readi()
	A = readlls(n)

	sort.Slice(A, func(i, j int) bool {
		return A[i] < A[j]
	})
	// PrintfDebug("%v\n", A)

	base := C1()

	if n > 60 {
		fmt.Println(base)
		return
	}

	ans := base
	for c := int64(2); c <= 100000; c++ {
		tmp := calc(c)
		ChMin(&ans, tmp)
	}
	fmt.Println(ans)
}

func calc(a int64) int64 {
	res := int64(0)

	cur := int64(1)
	res += AbsInt(A[0] - cur)

	F := Kiriage(INF_BIT60, a)

	for i := 1; i <= n-1; i++ {
		if cur > F {
			return INF_BIT60
		}

		cur *= a
		res += AbsInt(A[i] - cur)
	}

	return res
}

func C1() int64 {
	ans := int64(0)
	for i := 0; i < n; i++ {
		ans += AbsInt(A[i] - 1)
	}
	return ans
}

// Kiriage returns Ceil(a/b)
// a >= 0, b > 0
func Kiriage(a, b int64) int64 {
	return (a + (b - 1)) / b
}

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

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

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

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

const (
	// General purpose
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

	return res
}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	abs := func(x int64) int64 {
		if x < 0 {
			return -x
		}
		return x
	}

	n, ans := 0, int64(1e18)
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	sort.Ints(a)
o:
	for c := int64(1); ; c++ {
		var s, p int64 = 0, 1
		for _, v := range a {
			if p > 1e10 {
				break o
			}
			s += abs(int64(v) - p)
			p *= c
		}
		if s < ans {
			ans = s
		}
	}
	Print(ans)
}

package main

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

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

const inf int64 = 100000000000000000

func add(a, b int64) int64 {
	if a+b > inf {
		return inf
	}
	return a + b
}

func mul(a, b int64) int64 {
	if inf/a > b {
		return a * b
	}
	return inf
}

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

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

func main() {
	defer writer.Flush()
	var n int
	scanf("%d\n", &n)
	// fmt.Println(n)
	a := make([]int, n)
	for i := range a {
		scanf("%d ", &a[i])
	}
	sort.Ints(a)

	if n < 3 {
		printf("%d\n", a[0] - 1)
	} else {
		var minCost int64 = 0
		for _, v := range a {
			minCost += int64(v)
		}
		minCost -= int64(n)
		var x int64 = 0
		for x = 1; ; x++ {
			var cost, powCur int64 = 0, 1
			for i := range a {
				cost = add(cost, abs(int64(a[i])-powCur))
				powCur = mul(powCur, x)
			}
			if powCur >= inf || powCur/x > minCost+int64(a[n-1]) {
				break
			}
			minCost = min(minCost, cost)
		}
		printf("%d\n", minCost)
	}
}

package main

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

func abs(i, j uint64) uint64 {
	if i < j {
		return j - i
	}

	return i - j
}

func calc(i, n uint64) uint64 {
	if i == 1 {
		return n
	}

	val := uint64(math.Pow(float64(i), float64(n)))
	val--
	val /= (i - 1)

	return val
}

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

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

	n := in()
	a := make([]uint64, n)

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

	var sum uint64 = 0

	for i := range a {
		sum += a[i]
	}

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	// var e uint64 = uint64(math.Pow(float64(sum), 1/float64(n-1))) + 1

	var prevCost, i uint64 = math.MaxUint64, 0
	for i = 1; ; i++ {
		var ccost uint64 = 0
		var val uint64 = 1

		for j := range a {
			ccost += abs(a[j], val)
			val *= i
		}

		if ccost < prevCost {
			prevCost = ccost
		} else {
			break
		}
	}

	fmt.Println(prevCost)
}

package main

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

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

	in := func() int {
		sc.Scan()
		i, _ := strconv.Atoi(sc.Text())
		return i
	}

	n := in()
	a := make([]float64, n)

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

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	c := math.Ceil(math.Pow(a[n-1], 1.0/float64(n-1)))

	var minRes float64 = math.MaxFloat64

	for j := c; j >= 1; j-- {
		var res float64 = 0

		var val float64 = 1
		for i := range a {
			res += math.Abs(a[i]-val)
			val *= j
		}

		if res < minRes {
			minRes = res
		} else {
			break
		}
	}

	fmt.Println(uint64(minRes))
}

package main

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

func abs(i, j uint64) uint64 {
	if i < j {
		return j - i
	}

	return i - j
}

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

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

	n := in()
	a := make([]uint64, n)

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

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	uval := math.Pow(float64(a[n-1]), 1.0/float64(n-1))
	val := uint64(math.Ceil(uval))

	var cost uint64 = 0
	var cost2 uint64 = 0

	var p uint64 = 1

	for i := range a {
		cost += abs(a[i], p)
		p *= val
	}

	p = 1

	for i := range a {
		cost2 += abs(a[i], p)
		p *= (val - 1)
	}

	if cost < cost2 {
		fmt.Println(cost)
	} else {
		fmt.Println(cost2)
	}

}

package main

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

func abs(i, j uint64) uint64 {
	if i < j {
		return j - i
	}

	return i - j
}

func calc(i, n uint64) uint64 {
	if i == 1 {
		return n
	}

	val := uint64(math.Pow(float64(i), float64(n)))
	val--
	val /= (i - 1)

	return val
}

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

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

	n := in()
	a := make([]uint64, n)

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

	var sum uint64 = 0

	for i := range a {
		sum += a[i]
	}

	var e uint64 = uint64(math.Pow(float64(sum), 1/float64(n-1)))+1

	var prevCost, i uint64 = math.MaxUint64, 0
	for i = 1; i < e; i++ {
		var ccost uint64 = 0
		var val uint64 = 1
		for j := range a {
			ccost += abs(a[j], val)
			val *= i
		}

		if ccost < prevCost {
			prevCost = ccost
		}
	}

	fmt.Println(prevCost)
}

package main

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

func abs(i, j uint64) uint64 {
	if i < j {
		return j - i
	}

	return i - j
}

func calc(i, n uint64) uint64 {
	if i == 1 {
		return n
	}

	val := uint64(math.Pow(float64(i), float64(n)))
	val--
	val /= (i - 1)

	return val
}

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

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

	n := in()
	a := make([]uint64, n)

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

	var sum uint64 = 0

	for i := range a {
		sum += a[i]
	}

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	// var e uint64 = uint64(math.Pow(float64(sum), 1/float64(n-1))) + 1

	var prevCost, i uint64 = math.MaxUint64, 0
	for i = 0; ; i++ {
		var ccost uint64 = 0
		var val uint64 = 1

		if  i == 0 {
			val--
		}

		for j := range a {
			ccost += abs(a[j], val)
			val *= i
		}

		if ccost < prevCost {
			prevCost = ccost
		} else {
			break
		}
	}

	fmt.Println(prevCost)
}

package main

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

func abs(i, j uint64) uint64 {
	if i < j {
		return j - i
	}

	return i - j
}

func calc(i, n uint64) uint64 {
	if i == 1 {
		return n
	}

	val := uint64(math.Pow(float64(i), float64(n)))
	val--
	val /= (i - 1)

	return val
}

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

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

	n := in()
	a := make([]uint64, n)

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

	var sum uint64 = 0

	for i := range a {
		sum += a[i]
	}

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	var e uint64 = uint64(math.Pow(float64(sum), 1/float64(n-1))) + 1

	var prevCost, i uint64 = math.MaxUint64, 0
	for i = 1; i <= e; i++ {
		var ccost uint64 = 0
		var val uint64 = 1

		for j := range a {
			ccost += abs(a[j], val)
			val *= i
		}

		if ccost < prevCost {
			prevCost = ccost
		}
	}

	fmt.Println(prevCost)
}

package main

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

func abs(i, j uint64) uint64 {
	if i < j {
		return j - i
	}

	return i - j
}

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

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

	n := in()
	a := make([]uint64, n)

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

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	uval := math.Pow(float64(a[n-1]), 1.0/float64(n-1))
	val := uint64(math.Ceil(uval))

	var cost uint64 = 0
	var p uint64 = 1
	for i := range a {
		cost += abs(a[i], p)
		p *= val
	}

	fmt.Println(cost)

}


You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations:  Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$).  Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).

For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.

NoteConsider the first test case of the example:  $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$;  $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$;  $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$;  $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$;  $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$;  $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$;  $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once.

package main

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

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

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

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

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

func main() {
	out = bufio.NewWriterSize(os.Stdout, 8192)
	defer out.Flush()
	if false {
		file, err := os.Open("in.txt")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file, 8192)
	} else {
		in = bufio.NewReaderSize(os.Stdin, 8192)
	}

	var t int
	var n, k, x int
	var num map[int]int
	var res int64
	scanf("%d", &t)

	for ; t > 0; t-- {
		scanf("\n%d %d\n", &n, &k)
		num = make(map[int]int)
		res = -1
		for ; n > 0; n-- {
			scanf("%d", &x)
			x = k - x%k
			if x != k {
				res = max(res, int64(num[x])*int64(k)+int64(x))
				num[x]++
			}
		}
		printf("%d\n", res+1)
	}
}

package main

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

const MAX_N int = 2e5 + 10

var read *bufio.Reader = bufio.NewReader(os.Stdin)
var write *bufio.Writer = bufio.NewWriter(os.Stdout)
var num [MAX_N]int
var cnt map[int]int

func main() {
	defer write.Flush()
	var t int
	fmt.Fscan(read, &t)
	for ; t > 0; t-- {
		var n, k int
		fmt.Fscan(read, &n, &k)
		cnt = make(map[int]int, 0)
		for i := 0; i < n; i++ {
			fmt.Fscan(read, &num[i])
			if num[i] % k != 0 {
				cnt[k - num[i] % k]++
			}
		}
		var ans int = 0
		cnt[0] = 0
		for key, value := range cnt {
			if value > cnt[ans] || (value == cnt[ans] && key > ans) {
				ans = key
			}
		}
		//fmt.Fprintln(write, cnt)
		if ans == 0 {
			fmt.Fprintln(write, 0)
		} else {
			fmt.Fprintln(write, int64(cnt[ans] - 1) * int64(k) + int64(ans) + 1)
		}
	}
}

package main

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

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

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n, k := readTwoNums(reader)
		A := readNNums(reader, n)
		fmt.Println(solve(n, k, A))
	}
}

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

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

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

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

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

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

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

	return i
}

func solve(n, k int, A []int) int64 {
	//change A to A[i] - k * A[i]/ k
	mem := make(map[int]int)

	for i := 0; i < n; i++ {
		if A[i]%k == 0 {
			continue
		}
		r := k - A[i]%k
		//A[i] needs at least r operation from 0
		mem[r]++
	}

	if len(mem) == 0 {
		return 0
	}
	K := int64(k)
	var res int64
	for r, v := range mem {
		res = max(res, int64(v-1)*K+int64(r))
	}

	return res + 1
}

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

package main

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

func main() {
	nCases := 0
	bufIO := bufio.NewReader(os.Stdin)
	fmt.Scanln(&nCases)

	for iCase := 0; iCase < nCases; iCase++ {
		n, k := 0, 0
		fmt.Fscan(bufIO, &n, &k)

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

		for i := range numbs {
			if numbs[i]%k == 0 {
				numbs[i] = 0
			} else {
				numbs[i] = k - (numbs[i] % k)
			}
		}

		sort.Ints(numbs)
		//fmt.Println(numbs)
		firstI, first, count := 0, numbs[0], 0
		for i, numb := range numbs {
			if numb == 0 {
				continue
			} else {
				first, firstI = numb, i
				break
			}
		}
		if first == 0 {
			fmt.Println(0)
			continue
		}
		maxElem, maxCount := 0, 0
		for i := firstI + 1; i < len(numbs); i++ {
			numb := numbs[i]

			if numb == first {
				count++
				continue
			} else {
				if count >= maxCount {
					maxElem = first
					maxCount = count
				}

				first = numb
				count = 0
			}
		}
		if maxCount <= count {
			maxCount = count
			maxElem = first
		}
		ans := int64(maxElem) + int64(maxCount)*int64(k)

		fmt.Println(ans + 1)
	}
}

package main

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

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

	input := func() uint64 {
		scanner.Scan()
		i, _ := strconv.ParseUint(scanner.Text(), 10, 64)
		return i
	}

	var t, n, k, x uint64

	t = input()

	for t > 0 {
		t--

		n = input()
		k = input()

		rem := make(map[uint64]uint64)

		var i uint64
		for i = 0; i < n; i++ {
			x = input()
			rem[x%k]++
		}

		var max uint64 = 0

		for r, c := range rem {

			if r == 0 {
				continue
			}

			val := k*(c-1) + (k + 1 - r)
			if max < val {
				max = val
			}
		}

		fmt.Println(max)

	}
}

package main

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

func main() {
	var cases int
	fmt.Scanf("%d\n", &cases)
	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < cases; i++ {
		line := readLine(reader)
		strs := strings.Split(string(line), " ")
		n, _ := strconv.ParseInt(strs[0], 10, 64)
		k, _ := strconv.ParseInt(strs[1], 10, 64)
		line = readLine(reader)
		ans := strings.Split(string(line), " ")
		m := make(map[int64]int)
		for i := int64(0); i < n; i++ {
			ai, _ := strconv.ParseInt(ans[i], 10, 64)
			left := ai % k
			if left == 0 {
				continue
			}
			left = k - left
			m[left] = m[left] + 1
		}
		max := int64(0)
		// fmt.Println(m)
		if len(m) == 0 {
			fmt.Println(0)
			continue
		}
		for key, val := range m {
			temp := key + int64(val - 1) * k
			if temp > max {
				max = temp
			}
		}
		fmt.Println(max + 1)
	}
}

func readLine(reader *bufio.Reader) []byte {
	result := make([]byte, 0)
	for {
		s, isPrefix, err := reader.ReadLine()
		if err != nil {
			fmt.Printf("error=%+v\n", err)
			break
		}
		result = append(result, s...)
		if !isPrefix {
			break
		}
	}
	return result
}

package main

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

var nTest, n int
var k int64
var a [200001]int64

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

	fmt.Fscan(in, &nTest)
	for t := 0; t < nTest; t++ {
		fmt.Fscan(in, &n, &k)
		cnt := map[int64]int{}
		mx := int64(0)
		vmx := int64(0)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			a[i] %= int64(k)
			if a[i] != 0 {
				a[i] = int64(k) - a[i]
			}

		}

		sort.Slice(a[:n], func(i, j int) bool {
			return a[i] < a[j]
		})

		// fmt.Println(a[:n])
		for i := 0; i < n; i++ {
			if a[i] != 0 {
				cnt[a[i]]++
			}

			if cnt[a[i]] >= int(mx) {
				mx = int64(cnt[a[i]])
				vmx = a[i]
			}
		}

		// fmt.Println(mx, vmx)
		var res int64
		if mx == 0 {
			res = 0
		} else {
			res = k*(mx-1) + vmx + 1
		}
		fmt.Fprintln(out, res)

	}

	out.Flush()
}

package main

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

func min(a, b int) int {
	return int(math.Min(float64(a), float64(b)))
}

func max(a, b int) int {
	return int(math.Max(float64(a), float64(b)))
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscanf(in, "%v\n", &t)
	for now := 0; now < t; now++ {
		var n, k int64
		fmt.Fscanf(in, "%v %v\n", &n, &k)
		mas := make([]int64, n)
		m := make(map[int64]int64)
		var i int64
		for ;i < n;i++ {
			fmt.Fscanf(in, "%v", &mas[i])
			m[(k - (mas[i] % k)) % k]++
		}
		fmt.Fscanf(in, "\n")
		var mx, idx int64
		for ind, i := range m {
			if ind == 0 {
				continue
			}
			if i > mx {
				idx = int64(ind)
				mx = i
			} else if (mx == i) && (ind > idx){
				idx = int64(ind)
				mx = i
			}
		}
		if idx == 0 {
			fmt.Fprintln(out, 0)
			continue
		}
		fmt.Fprintln(out, (mx - 1) * (k) + idx + 1)

	}
	return
}

package main

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

func main() {
	t := NextInt()
	for t > 0 {
		t--
		n, k := NextInt(), NextInt()
		a := make([]int, n)

		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			a[i] = NextInt()
			a[i] %= k
			if a[i] > 0 {
				mp[a[i]]++
			}
		}

		ans := int64(0)
		for key, cnt := range mp {
			if int64(cnt) * int64(k) - int64(key) > ans {
				ans = int64(cnt) * int64(k) - int64(key)
			}
		}
		if ans > 0 {
			ans++
		}
		fmt.Println(ans)

	}
}

var sc = bufio.NewScanner(os.Stdin)

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

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

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

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

package main

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

type data struct{ n, s int64 }
type mySlice []data

func (p mySlice) Len() int           { return len(p) }
func (p mySlice) Less(i, j int) bool { return p[i].n < p[j].n }
func (p mySlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

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

	var t int
	fmt.Fscanf(reader, "%d\n", &t)

	var n int
	var k int64

	for ; t > 0; t-- {
		fmt.Fscanf(reader, "%d %d\n", &n, &k)
		var temp int64
		res := make(map[int64]int, n)
		for i := 1; i <= n; i++ {
			if i != n {
				fmt.Fscanf(reader, "%d ", &temp)
			} else {
				fmt.Fscanf(reader, "%d\n", &temp)
			}

			temp = temp % k
			if temp > 0 {
				res[k-temp]++
			}
		}

		mysort := make(mySlice, 0, len(res))
		for key, value := range res {
			mysort = append(mysort, data{n: key, s: int64(value)})
		}
		sort.Sort(mysort)

		maxCrc := int64(0)
		maxNum := int64(0)

		for i := 0; i < len(mysort); i++ {
			if mysort[i].s >= maxCrc {
				maxCrc = mysort[i].s
				maxNum = mysort[i].n
			}
		}

		if maxCrc == 0 && maxNum == 0 {
			fmt.Println(0)
		} else {
			fmt.Println(maxNum + 1 + (maxCrc-1)*k)
		}
	}
}

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-- {
		n := sint64()
		k := sint64()
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		printf("%d\n", sol(a, k))
	}
}

func sol(a []int64, k int64) int64 {
	counts := map[int64]int64{}
	for i := range a {
		if a[i]%k == 0 {
			continue
		}
		toAdd := (k * ((a[i] / k) + 1)) - a[i]
		counts[toAdd]++
	}
	ret := int64(0)
	for i := range counts {
		ret = max(ret, k*(counts[i]-1)+int64(i))
	}
	if ret == 0 {
		return 0
	}
	return ret + 1
}

package main

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

const MAX_N int = 2e5 + 10

var read *bufio.Reader = bufio.NewReader(os.Stdin)
var write *bufio.Writer = bufio.NewWriter(os.Stdout)
var num [MAX_N]int
var cnt map[int]int

func main() {
	defer write.Flush()
	var t int
	fmt.Fscan(read, &t)
	for ; t > 0; t-- {
		var n, k int
		fmt.Fscan(read, &n, &k)
		cnt = make(map[int]int, 0)
		for i := 0; i < n; i++ {
			fmt.Fscan(read, &num[i])
			if num[i] % k != 0 {
				cnt[k - num[i] % k]++
			}
		}
		var ans int = 0
		cnt[0] = 0
		for key, value := range cnt {
			if value > cnt[ans] || (value == cnt[ans] && key > ans) {
				ans = key
			}
		}
		//fmt.Fprintln(write, cnt)
		if ans == 0 {
			fmt.Fprintln(write, 0)
		} else {
			fmt.Fprintln(write, (cnt[ans] - 1) * k + ans + 1)
		}
	}
}

package main

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

// An IntHeap is a min-heap of ints.
type IntHeap []int

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

func (h *IntHeap) Push(x interface{}) {
	// Push and Pop use pointer receivers because they modify the slice's length,
	// not just its contents.
	*h = append(*h, x.(int))
}

func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

func main() {
	nCases := 0
	bufIO := bufio.NewReader(os.Stdin)
	fmt.Scanln(&nCases)

	for iCase := 0; iCase < nCases; iCase++ {
		n, k := 0, 0
		fmt.Fscan(bufIO, &n, &k)

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

		for i := range numbs {
			//fmt.Println(numbs[i])
			if numbs[i]%k == 0 {
				numbs[i] = 0
			} else {
				numbs[i] = k - (numbs[i] % k)
			}
		}
		numbHeap := IntHeap(numbs)
		heap.Init(&numbHeap)

		ans, lastTop := 0, -1
		for numbHeap.Len() > 0 {
			top := heap.Pop(&numbHeap).(int)
			if top == 0 {
				continue
			}
			if top+1 > ans {
				ans = top + 1
			}
			if top == lastTop {
				heap.Push(&numbHeap, top+k)
			}
			lastTop = top
		}
		fmt.Println(ans)
	}
}

package main

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

func main() {
	nCases := 0
	bufIO := bufio.NewReader(os.Stdin)
	fmt.Scanln(&nCases)

	for iCase := 0; iCase < nCases; iCase++ {
		n, k := 0, 0
		fmt.Fscan(bufIO, &n, &k)

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

		for i := range numbs {
			if numbs[i]%k == 0 {
				numbs[i] = 0
			} else {
				numbs[i] = k - (numbs[i] % k)
			}
		}

		sort.Ints(numbs)
		//fmt.Println(numbs)
		firstI, first, count := 0, numbs[0], 0
		for i, numb := range numbs {
			if numb == 0 {
				continue
			} else {
				first, firstI = numb, i
				break
			}
		}
		if first == 0 {
			fmt.Println(0)
			continue
		}
		maxElem, maxCount := 0, 0
		for i := firstI + 1; i < len(numbs); i++ {
			numb := numbs[i]

			if numb == first {
				count++
				continue
			} else {
				maxElem = first
				maxCount = count

				first = numb
				count = 0
			}
		}
		if maxCount <= count {
			maxCount = count
			maxElem = first
		}
		ans := int64(maxElem) + int64(maxCount)*int64(k)

		fmt.Println(ans + 1)
	}
}

package main

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

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

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

	var t, n, k, x int

	t = input()

	for t > 0 {
		t--

		n = input()
		k = input()

		rem := make(map[int]int)

		for i := 0; i < n; i++ {
			x = input()
			rem[x%k]++
		}

		max := 0

		for r, c := range rem {

			if r == 0 {
				continue
			}

			val := k*(c-1) + (k + 1 - r)
			if max < val {
				max = val
			}
		}

		fmt.Println(max)

	}
}

package main

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

var nTest, n int
var k int64
var a [200001]int64

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

	fmt.Fscan(in, &nTest)
	for t := 0; t < nTest; t++ {
		fmt.Fscan(in, &n, &k)
		var cnt [200001]int
		mx := int64(0)
		vmx := int64(0)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			a[i] %= int64(k)
			if a[i] != 0 {
				a[i] = int64(k) - a[i]
			}
			cnt[a[i]]++

			if cnt[a[i]] >= int(mx) {
				mx = int64(cnt[a[i]])

				if vmx < a[i] {
					vmx = a[i]
				}
			}
		}
		if vmx == 0 {
			fmt.Fprintln(out, 0)
			continue
		}
		res := k*(mx-1) + vmx + 1
		fmt.Fprintln(out, res)

	}

	out.Flush()
}

package main

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

var nTest, n int
var k int64
var a [200001]int64

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

	fmt.Fscan(in, &nTest)
	for t := 0; t < nTest; t++ {
		fmt.Fscan(in, &n, &k)
		var cnt [200001]int
		mx := int64(0)
		vmx := int64(0)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			a[i] %= int64(k)
			if a[i] != 0 {
				a[i] = int64(k) - a[i]
			}

		}

		sort.Slice(a[:n], func(i, j int) bool {
			return a[i] < a[j]
		})

		for i := 0; i < n; i++ {
			cnt[a[i]]++

			if cnt[a[i]] >= int(mx) {
				mx = int64(cnt[a[i]])
				vmx = a[i]
			}
		}
		if vmx == 0 {
			fmt.Fprintln(out, 0)
			continue
		}

		res := k*(mx-1) + vmx + 1
		fmt.Fprintln(out, res)

	}

	out.Flush()
}

package main

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

func main() {
	t := NextInt()
	for t > 0 {
		t--
		n, k := NextInt(), NextInt()
		a := make([]int, n)

		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			a[i] = NextInt()
			a[i] %= k
			if a[i] > 0 {
				mp[k-a[i]]++
			}
		}

		ans := 0
		exist := make(map[int]int)
		for key, cnt := range mp {
			cur := key
			for i := 0; i < cnt; i++ {
				for exist[cur] > 0 {
					cur += k
				}
				exist[cur]++
				if cur > ans {
					ans = cur
				}
			}
		}
		if ans > 0 {
			ans++
		}
		fmt.Println(ans)

	}
}

var sc = bufio.NewScanner(os.Stdin)

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

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

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

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

package main

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

func main() {
	t := NextInt()
	for t > 0 {
		t--
		n, k := NextInt(), NextInt()
		a := make([]int, n)

		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			a[i] = NextInt()
			a[i] %= k
			if a[i] > 0 {
				mp[a[i]]++
			}
		}

		ans := int64(0)
		for key, cnt := range mp {
			if int64(cnt * k) - int64(key) > ans {
				ans = int64(cnt * k) - int64(key)
			}
		}
		if ans > 0 {
			ans++
		}
		fmt.Println(ans)

	}
}

var sc = bufio.NewScanner(os.Stdin)

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

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

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

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

package main

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

func main() {
	t := NextInt()
	for t > 0 {
		t--
		n, k := NextInt(), NextInt()
		a := make([]int, n)

		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			a[i] = NextInt()
			a[i] %= k
			if a[i] > 0 {
				mp[a[i]]++
			}
		}

		ans := 0
		for key, cnt := range mp {
			if cnt * k - key > ans {
				ans = cnt * k - key
			}
		}
		if ans > 0 {
			ans++
		}
		fmt.Println(ans)

	}
}

var sc = bufio.NewScanner(os.Stdin)

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

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

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

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


The Central Company has an office with a sophisticated security system. There are $$$10^6$$$ employees, numbered from $$$1$$$ to $$$10^6$$$.The security system logs entrances and departures. The entrance of the $$$i$$$-th employee is denoted by the integer $$$i$$$, while the departure of the $$$i$$$-th employee is denoted by the integer $$$-i$$$.The company has some strict rules about access to its office:  An employee can enter the office at most once per day.  He obviously can't leave the office if he didn't enter it earlier that day.  In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day.Some examples of valid or invalid days:  $$$[1, 7, -7, 3, -1, -3]$$$ is a valid day ($$$1$$$ enters, $$$7$$$ enters, $$$7$$$ leaves, $$$3$$$ enters, $$$1$$$ leaves, $$$3$$$ leaves).  $$$[2, -2, 3, -3]$$$ is also a valid day.  $$$[2, 5, -5, 5, -5, -2]$$$ is not a valid day, because $$$5$$$ entered the office twice during the same day.  $$$[-4, 4]$$$ is not a valid day, because $$$4$$$ left the office without being in it.  $$$[4]$$$ is not a valid day, because $$$4$$$ entered the office and didn't leave it before the end of the day. There are $$$n$$$ events $$$a_1, a_2, \ldots, a_n$$$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events.You must partition (to cut) the array $$$a$$$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day.For example, if $$$n=8$$$ and $$$a=[1, -1, 1, 2, -1, -2, 3, -3]$$$ then he can partition it into two contiguous subarrays which are valid days: $$$a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$$$.Help the administrator to partition the given array $$$a$$$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^6 \le a_i \le 10^6$$$ and $$$a_i \neq 0$$$).

If there is no valid partition, print $$$-1$$$. Otherwise, print any valid partition in the following format:   On the first line print the number $$$d$$$ of days ($$$1 \le d \le n$$$).  On the second line, print $$$d$$$ integers $$$c_1, c_2, \ldots, c_d$$$ ($$$1 \le c_i \le n$$$ and $$$c_1 + c_2 + \ldots + c_d = n$$$), where $$$c_i$$$ is the number of events in the $$$i$$$-th day.  If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days.

NoteIn the first example, the whole array is a valid day.In the second example, one possible valid solution is to split the array into $$$[1, -1]$$$ and $$$[1, 2, -1, -2, 3, -3]$$$ ($$$d = 2$$$ and $$$c = [2, 6]$$$). The only other valid solution would be to split the array into $$$[1, -1]$$$, $$$[1, 2, -1, -2]$$$ and $$$[3, -3]$$$ ($$$d = 3$$$ and $$$c = [2, 4, 2]$$$). Both solutions are accepted.In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Fscanf(in, "%d\n", &q)
	for ii := 0; ii < q; ii++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")
		ans := []int{}
		cur := 0
		status := make(map[int]int) // 0 not 1 in 2 out
		round := 0
		impossible := false
		inOffice := 0
		for i := 0; i < n; i++ {
			if a[i] > 0 {
				if status[a[i]] == round * 3 + 1 {
					impossible = true
					break
				} else if status[a[i]] == round * 3 + 2 {
					if inOffice != 0 {
						impossible = true
						break
					}
					ans = append(ans, cur)
					cur = 1
					round++
					status[a[i]] = round * 3 + 1
					inOffice++
				} else {
					status[a[i]] = round * 3 + 1
					cur++
					inOffice++
				}
			} else if a[i] < 0 {
				if status[-a[i]] != round * 3 +1 {
					impossible = true
					break
				} else {
					status[-a[i]] = round * 3 + 2
					cur++
					inOffice--
					if inOffice == 0 {
						ans = append(ans, cur)
						cur = 0
						round++
					}
				}
			}
			//fmt.Println(a[i], status, cur, ans)
		}
		if inOffice != 0 {
			impossible = true
		}
		if impossible {
			fmt.Fprintf(out, "-1\n")
		} else {
			fmt.Fprintf(out, "%d\n", len(ans))
			for i := 0; i < len(ans)-1; i++ {
				fmt.Fprintf(out, "%d ", ans[i])
			}
			fmt.Fprintf(out, "%d\n", ans[len(ans)-1])
		}
	}
	return
}

package main

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

	return s
}

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

	if e == 0 {
		return 1
	}

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

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

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

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

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

	return a
}

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

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

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

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

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

var n int
var A []int

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

	if n%2 == 1 {
		fmt.Println(-1)
		return
	}

	// memo := [1000000+5]int{}
	memo := make(map[int]int)  // オフィス
	times := make(map[int]int) // ある一日の入場回数
	answers := []int{}
	count := 0
	for i := 0; i < n; i++ {
		a := A[i]

		if a < 0 {
			a = -a
			// 退出
			if _, ok := memo[a]; ok {
				delete(memo, a)
			} else {
				// 入場前退出のためアウト
				fmt.Println(-1)
				return
			}
		} else {
			// 入場
			if times[a] == 0 {
				memo[a] = 1
				times[a] = 1
			} else {
				// 一日に2回登場したのでアウト
				fmt.Println(-1)
				return
			}
		}

		count++
		// 空室判定
		if len(memo) == 0 {
			// 空室なので次の日へリセット
			answers = append(answers, count)
			count = 0
			memo = make(map[int]int)
			times = make(map[int]int)
		}
		// else {
		// 	// 継続
		// 	count++
		// }
	}

	if len(memo) != 0 {
		fmt.Println(-1)
		return
	}

	fmt.Println(len(answers))
	fmt.Println(PrintIntsLine(answers...))
}

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

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Fscanf(in, "%d\n", &q)
	for ii := 0; ii < q; ii++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")
		ans := []int{}
		cur := 0
		status := make(map[int]int) // 0 not 1 in 2 out
		round := 0
		impossible := false
		for i := 0; i < n; i++ {
			if a[i] > 0 {
				if status[a[i]] == round * 3 + 1 {
					impossible = true
					break
				} else if status[a[i]] == round * 3 + 2 {
					ans = append(ans, cur)
					cur = 1
					round++
					status[a[i]] = round * 3 + 1
				} else {
					status[a[i]] = round * 3 + 1
					cur++
				}
			} else if a[i] < 0 {
				if status[-a[i]] != round * 3 +1 {
					impossible = true
					break
				} else {
					status[-a[i]] = round * 3 + 2
					cur++
				}
			}
			//fmt.Println(a[i], status, cur, ans)
		}
		for _, v := range status {
			if v % 3 == 1 {
				impossible = true
			}
		}
		ans = append(ans, cur)
		if impossible {
			fmt.Fprintf(out, "-1\n")
		} else {
			fmt.Fprintf(out, "%d\n", len(ans))
			for i := 0; i < len(ans)-1; i++ {
				fmt.Fprintf(out, "%d ", ans[i])
			}
			fmt.Fprintf(out, "%d\n", ans[len(ans)-1])
		}
	}
	return
}

func minstep(a, b byte) int {
	return min(int(a-b+26)%26, int(b-a+26)%26)
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Fscanf(in, "%d\n", &q)
	for ii := 0; ii < q; ii++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")
		ans := []int{}
		cur := 0
		status := make(map[int]int) // 0 not 1 in 2 out
		round := 0
		impossible := false
		for i := 0; i < n; i++ {
			if a[i] > 0 {
				if status[a[i]] == round * 3 + 1 {
					impossible = true
					break
				} else if status[a[i]] == round * 3 + 2 {
					ans = append(ans, cur)
					cur = 1
					round++
					status[a[i]] = round * 3 + 1
				} else {
					status[a[i]] = round * 3 + 1
					cur++
				}
			} else if a[i] < 0 {
				if status[-a[i]] != round * 3 +1 {
					impossible = true
					break
				} else {
					status[-a[i]] = round * 3 + 2
					cur++
				}
			}
			//fmt.Println(a[i], status, cur, ans)
		}
		ans = append(ans, cur)
		if impossible {
			fmt.Fprintf(out, "-1\n")
		} else {
			fmt.Fprintf(out, "%d\n", len(ans))
			for i := 0; i < len(ans)-1; i++ {
				fmt.Fprintf(out, "%d ", ans[i])
			}
			fmt.Fprintf(out, "%d\n", ans[len(ans)-1])
		}
	}
	return
}

func minstep(a, b byte) int {
	return min(int(a-b+26)%26, int(b-a+26)%26)
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Fscanf(in, "%d\n", &q)
	for ii := 0; ii < q; ii++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")
		ans := []int{}
		cur := 0
		status := make(map[int]int) // 0 not 1 in 2 out
		round := 0
		impossible := false
		inOffice := 0
		for i := 0; i < n; i++ {
			if a[i] > 0 {
				if status[a[i]] == round * 3 + 1 {
					impossible = true
					break
				} else if status[a[i]] == round * 3 + 2 {
					if inOffice != 0 {
						impossible = true
						break
					}
					ans = append(ans, cur)
					cur = 1
					round++
					status[a[i]] = round * 3 + 1
					inOffice++
				} else {
					status[a[i]] = round * 3 + 1
					cur++
					inOffice++
				}
			} else if a[i] < 0 {
				if status[-a[i]] != round * 3 +1 {
					impossible = true
					break
				} else {
					status[-a[i]] = round * 3 + 2
					cur++
					inOffice--
				}
			}
			//fmt.Println(a[i], status, cur, ans)
		}
		if inOffice != 0 {
			impossible = true
		}
		ans = append(ans, cur)
		if impossible {
			fmt.Fprintf(out, "-1\n")
		} else {
			fmt.Fprintf(out, "%d\n", len(ans))
			for i := 0; i < len(ans)-1; i++ {
				fmt.Fprintf(out, "%d ", ans[i])
			}
			fmt.Fprintf(out, "%d\n", ans[len(ans)-1])
		}
	}
	return
}

func minstep(a, b byte) int {
	return min(int(a-b+26)%26, int(b-a+26)%26)
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Fscanf(in, "%d\n", &q)
	for ii := 0; ii < q; ii++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")
		ans := []int{}
		cur := 0
		status := make(map[int]int) // 0 not 1 in 2 out
		round := 0
		impossible := false
		inOffice := 0
		for i := 0; i < n; i++ {
			if a[i] > 0 {
				if status[a[i]] == round * 3 + 1 {
					impossible = true
					break
				} else if status[a[i]] == round * 3 + 2 {
					if inOffice != 0 {
						impossible = true
						break
					}
					ans = append(ans, cur)
					cur = 1
					round++
					status[a[i]] = round * 3 + 1
					inOffice++
				} else {
					status[a[i]] = round * 3 + 1
					cur++
					inOffice++
				}
			} else if a[i] < 0 {
				if status[-a[i]] != round * 3 +1 {
					impossible = true
					break
				} else {
					status[-a[i]] = round * 3 + 2
					cur++
					inOffice--
				}
			}
			//fmt.Println(a[i], status, cur, ans)
		}
		for _, v := range status {
			if v % 3 == 1 {
				impossible = true
			}
		}
		ans = append(ans, cur)
		if impossible {
			fmt.Fprintf(out, "-1\n")
		} else {
			fmt.Fprintf(out, "%d\n", len(ans))
			for i := 0; i < len(ans)-1; i++ {
				fmt.Fprintf(out, "%d ", ans[i])
			}
			fmt.Fprintf(out, "%d\n", ans[len(ans)-1])
		}
	}
	return
}

func minstep(a, b byte) int {
	return min(int(a-b+26)%26, int(b-a+26)%26)
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Fscanf(in, "%d\n", &q)
	for ii := 0; ii < q; ii++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")
		ans := []int{}
		cur := 0
		status := make([]int, 2000000) // 0 not 1 in 2 out
		round := 0
		impossible := false
		inOffice := 0
		for i := 0; i < n; i++ {
			if a[i] > 0 {
				if status[a[i]] == round * 3 + 1 {
					impossible = true
					break
				} else if status[a[i]] == round * 3 + 2 {
					if inOffice != 0 {
						impossible = true
						break
					}
					ans = append(ans, cur)
					cur = 1
					round++
					status[a[i]] = round * 3 + 1
					inOffice++
				} else {
					status[a[i]] = round * 3 + 1
					cur++
					inOffice++
				}
			} else if a[i] < 0 {
				if status[-a[i]] != round * 3 +1 {
					impossible = true
					break
				} else {
					status[-a[i]] = round * 3 + 2
					cur++
					inOffice--
				}
			}
			//fmt.Println(a[i], status, cur, ans)
		}
		if inOffice != 0 {
			impossible = true
		}
		ans = append(ans, cur)
		if impossible {
			fmt.Fprintf(out, "-1\n")
		} else {
			fmt.Fprintf(out, "%d\n", len(ans))
			for i := 0; i < len(ans)-1; i++ {
				fmt.Fprintf(out, "%d ", ans[i])
			}
			fmt.Fprintf(out, "%d\n", ans[len(ans)-1])
		}
	}
	return
}

package main

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

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

	return s
}

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

	if e == 0 {
		return 1
	}

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

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

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

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

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

	return a
}

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

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

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

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

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

var n int
var A []int

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

	if n%2 == 1 {
		fmt.Println(-1)
		return
	}

	// memo := [1000000+5]int{}
	memo := make(map[int]int)  // オフィス
	times := make(map[int]int) // ある一日の入場回数
	answers := []int{}
	count := 0
	for i := 0; i < n; i++ {
		a := A[i]

		if a < 0 {
			a = -a
			// 退出
			if _, ok := memo[a]; ok {
				delete(memo, a)
			} else {
				// 入場前退出のためアウト
				fmt.Println(-1)
				return
			}
		} else {
			// 入場
			if times[a] == 0 {
				memo[a] = 1
				times[a] = 1
			} else {
				// 一日に2回登場したのでアウト
				fmt.Println(-1)
				return
			}
		}

		count++
		// 空室判定
		if len(memo) == 0 {
			// 空室なので次の日へリセット
			answers = append(answers, count)
			count = 0
			memo = make(map[int]int)
			times = make(map[int]int)
		}
		// else {
		// 	// 継続
		// 	count++
		// }
	}

	fmt.Println(len(answers))
	fmt.Println(PrintIntsLine(answers...))
}

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

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


Bob has a string $$$s$$$ consisting of lowercase English letters. He defines $$$s'$$$ to be the string after removing all "a" characters from $$$s$$$ (keeping all other characters in the same order). He then generates a new string $$$t$$$ by concatenating $$$s$$$ and $$$s'$$$. In other words, $$$t=s+s'$$$ (look at notes for an example).You are given a string $$$t$$$. Your task is to find some $$$s$$$ that Bob could have used to generate $$$t$$$. It can be shown that if an answer exists, it will be unique.

The first line of input contains a string $$$t$$$ ($$$1 \leq |t| \leq 10^5$$$) consisting of lowercase English letters.

Print a string $$$s$$$ that could have generated $$$t$$$. It can be shown if an answer exists, it is unique. If no string exists, print ":(" (without double quotes, there is no space between the characters).

NoteIn the first example, we have $$$s = $$$ "aaaaa", and $$$s' = $$$ "".In the second example, no such $$$s$$$ can work that will generate the given $$$t$$$.In the third example, we have $$$s = $$$ "ababacac", and $$$s' = $$$ "bbcc", and $$$t = s + s' = $$$ "ababacacbbcc".

package main

import "fmt"
import "strings"
import "bufio"
import "os"

func main() {
	var t string
	fmt.Scanln(&t)

	var s strings.Builder
	for _, c := range t {
		if c != 'a' {
			s.WriteRune(c)
		}
	}

	res := s.String()

	if len(res) % 2 != 0 {
		fmt.Println(":(")
		return
	}

	for i := 0; i < len(res) / 2; i++ {
		if res[i] != res[i + len(res) / 2] {
			fmt.Println(":(")
			return
		}
		if res[i] != t[len(t) - len(res) / 2 + i] {
			fmt.Println(":(")
			return
		}
	}

	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	for i := 0; i < len(t) - len(res) / 2; i++ {
		out.WriteByte(t[i])
	}
}