During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of $$$n$$$ high school students numbered from $$$1$$$ to $$$n$$$. Initially, each student $$$i$$$ is on position $$$i$$$. Each student $$$i$$$ is characterized by two numbers — $$$a_i$$$ and $$$b_i$$$. Dissatisfaction of the person $$$i$$$ equals the product of $$$a_i$$$ by the number of people standing to the left of his position, add the product $$$b_i$$$ by the number of people standing to the right of his position. Formally, the dissatisfaction of the student $$$i$$$, which is on the position $$$j$$$, equals $$$a_i \cdot (j-1) + b_i \cdot (n-j)$$$.The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.Although Stas is able to solve such problems, this was not given to him. He turned for help to you.

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of people in the queue. Each of the following $$$n$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \leq a_i, b_i \leq 10^8$$$) — the characteristic of the student $$$i$$$, initially on the position $$$i$$$.

Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.

NoteIn the first example it is optimal to put people in this order: ($$$3, 1, 2$$$). The first person is in the position of $$$2$$$, then his dissatisfaction will be equal to $$$4 \cdot 1+2 \cdot 1=6$$$. The second person is in the position of $$$3$$$, his dissatisfaction will be equal to $$$2 \cdot 2+3 \cdot 0=4$$$. The third person is in the position of $$$1$$$, his dissatisfaction will be equal to $$$6 \cdot 0+1 \cdot 2=2$$$. The total dissatisfaction will be $$$12$$$.In the second example, you need to put people in this order: ($$$3, 2, 4, 1$$$). The total dissatisfaction will be $$$25$$$.

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n := 0
	fmt.Fscanf(in, "%d\n", &n)
	a := make([]int, n)
	b := make([]int, n)
	c := make([]int, n)
	ans := int64(0)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d %d\n", &a[i], &b[i])
		ans += int64(b[i])*int64(n) - int64(a[i])
		c[i] = a[i] - b[i]
	}
	sort.Ints(c)
	for j := 1; j <= n; j++ {
		ans += int64(c[j-1]) * int64(n-j+1)
	}
	fmt.Fprintf(out, "%d\n", ans)
	return
}

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

package main

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

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

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

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

var debug = false

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

func main() {
	defer writer.Flush()

	n := getLine2Int()
	diff := make([]int64, n)
	ans := int64(0)
	for i := 0; i < n; i++ {
		a, b := getLine2Int2()
		diff[i] = b - a
		ans += a * int64(n-1)
	}
	sort.Slice(diff, func(i, j int) bool { return diff[i] < diff[j] })
	for i := 0; i < n; i++ {
		ans += diff[i] * int64(n-i-1)
	}
	printf("%d\n", ans)
}

package main

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

/*
ai*(j-1)+bi*(n-j)

when ai > bi:
(ai-bi)*(j-1) + bi*(n-1)
sort by (ai-bi) desc

when bi > ai:
ai*(n-1) + (bi-ai)*(n-j)
sort by (bi-ai) asc

when bi == ai:
ai*(n-1)
in th middle

3
4 2
2 3
6 1

12

*/

type Pair struct {
	a int64
	b int64
}

type Queue []Pair

func (q Queue) Score() (score int64) {
	n := int64(len(q))
	for j, p := range q {
		jj := int64(j + 1)
		score += p.a*(jj-1) + p.b*(n-jj)
	}

	return
}

func ScanInt(s *bufio.Scanner) (int, error) {
	s.Scan()
	return strconv.Atoi(s.Text())
}

func ScanInt64(s *bufio.Scanner) (int64, error) {
	s.Scan()
	return strconv.ParseInt(s.Text(), 10, 64)
}

func main() {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)
	sdtNum, _ := ScanInt(s)
	q1 := make([]Pair, 0, sdtNum/3+1)
	q2 := make([]Pair, 0, sdtNum/3+1)
	q3 := make([]Pair, 0, sdtNum/3+1)

	for i := 0; i < sdtNum; i++ {
		a, _ := ScanInt64(s)
		b, _ := ScanInt64(s)

		if a > b {
			q1 = append(q1, Pair{a, b})
		} else if a < b {
			q2 = append(q2, Pair{a, b})
		} else {
			q3 = append(q3, Pair{a, b})
		}
	}
	// ai > bi
	sort.Slice(q1, func(i, j int) bool {
		return (q1[i].a - q1[i].b) > (q1[j].a - q1[j].b)
	})
	// bi > ai
	sort.Slice(q2, func(i, j int) bool {
		return (q2[i].b - q2[i].a) < (q2[j].b - q2[j].a)
	})

	q := Queue(append(append(q1, q3...), q2...))

	fmt.Println(q.Score())
}

package main

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

/*
ai*(j-1)+bi*(n-j)

when ai > bi:
(ai-bi)*(j-1) + bi*(n-1)
sort by (ai-bi) desc

when bi > ai:
ai*(n-1) + (bi-ai)*(n-j)
sort by (bi-ai) asc

when bi == ai:
ai*(n-1)
in th middle

3
4 2
2 3
6 1

12

*/
type Pair [2]int64
type Queue []Pair

func (q Queue) Score() (score int64) {
	n := int64(len(q))
	for j, p := range q {
		jj := int64(j + 1)
		score += p[0]*(jj-1) + p[1]*(n-jj)
	}

	return
}

func ScanInt(s *bufio.Scanner) (int, error) {
	s.Scan()
	return strconv.Atoi(s.Text())
}

func ScanInt64(s *bufio.Scanner) (int64, error) {
	s.Scan()
	return strconv.ParseInt(s.Text(), 10, 64)
}

func main() {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)
	sdtNum, _ := ScanInt(s)
	q1 := make([]Pair, 0)
	q2 := make([]Pair, 0)
	q3 := make([]Pair, 0)

	for i := 0; i < sdtNum; i++ {
		a, _ := ScanInt64(s)
		b, _ := ScanInt64(s)

		if a > b {
			q1 = append(q1, Pair{a, b})
		} else if a < b {
			q2 = append(q2, Pair{a, b})
		} else {
			q3 = append(q3, Pair{a, b})
		}
	}
	// ai > bi
	sort.Slice(q1, func(i, j int) bool {
		return (q1[i][0] - q1[i][1]) > (q1[j][0] - q1[j][1])
	})
	// bi > ai
	sort.Slice(q2, func(i, j int) bool {
		return (q2[i][1] - q2[i][0]) < (q2[j][1] - q2[j][0])
	})
	q := Queue(append(append(q1, q3...), q2...))

	fmt.Println(q.Score())
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1151D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var b int
	var n, ans int64
	Fscan(in, &n)
	a := make(sort.IntSlice, n)
	for i := range a {
		Fscan(in, &a[i], &b)
		a[i] -= b
		ans += int64(b)
	}
	ans *= n - 1
	sort.Sort(sort.Reverse(a))
	for i, v := range a {
		ans += int64(i) * int64(v)
	}
	Fprint(out, ans)
}

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

package main

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

/*
ai*(j-1)+bi*(n-j)

when ai > bi:
(ai-bi)*(j-1) + bi*(n-1)
sort by (ai-bi) desc

when bi > ai:
ai*(n-1) + (bi-ai)*(n-j)
sort by (bi-ai) asc

when bi == ai:
ai*(n-1)
in th middle

3
4 2
2 3
6 1

12

*/

type Pair struct {
	a int
	b int
}

type Queue []Pair

func (q Queue) Score() (score int) {
	n := len(q)
	for j, p := range q {
		j += 1
		score += p.a*(j-1) + p.b*(n-j)
	}

	return
}

func ScanInt(s *bufio.Scanner) (int, error) {
	s.Scan()
	return strconv.Atoi(s.Text())
}

func main() {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)
	sdtNum, _ := ScanInt(s)
	q1 := make([]Pair, 0, sdtNum/3+1)
	q2 := make([]Pair, 0, sdtNum/3+1)
	q3 := make([]Pair, 0, sdtNum/3+1)

	for i := 0; i < sdtNum; i++ {
		a, _ := ScanInt(s)
		b, _ := ScanInt(s)

		if a > b {
			q1 = append(q1, Pair{a, b})
		} else if a < b {
			q2 = append(q2, Pair{a, b})
		} else {
			q3 = append(q3, Pair{a, b})
		}
	}
	// ai > bi
	sort.Slice(q1, func(i, j int) bool {
		return (q1[i].a - q1[i].b) > (q1[j].a - q1[j].b)
	})
	// bi > ai
	sort.Slice(q2, func(i, j int) bool {
		return (q2[i].b - q2[i].a) < (q2[j].b - q2[j].a)
	})

	q := Queue(append(append(q1, q3...), q2...))

	fmt.Println(q.Score())
}

package main

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

/*
ai*(j-1)+bi*(n-j)

when ai > bi:
(ai-bi)*(j-1) + bi*(n-1)
sort by (ai-bi) desc

when bi > ai:
ai*(n-1) + (bi-ai)*(n-j)
sort by (bi-ai) asc

when bi == ai:
ai*(n-1)
in th middle

3
4 2
2 3
6 1

12

*/
type Pair [2]int64
type Queue []Pair

func (q Queue) Score() (score int64) {
	n := int64(len(q))
	for j, p := range q {
		jj := int64(j + 1)
		score += p[0]*(jj-1) + p[1]*(n-jj)
	}

	return
}

func ScanInt(s *bufio.Scanner) (int, error) {
	s.Scan()
	return strconv.Atoi(s.Text())
}

func ScanInt64(s *bufio.Scanner) (int64, error) {
	s.Scan()
	return strconv.ParseInt(s.Text(), 10, 64)
}

func main() {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)
	sdtNum, _ := ScanInt(s)
	q1 := make([]Pair, 0)
	q2 := make([]Pair, 0)
	q3 := make([]Pair, 0)

	for i := 0; i < sdtNum; i++ {
		a, _ := ScanInt64(s)
		b, _ := ScanInt64(s)

		if a > b {
			q1 = append(q1, Pair{a, b})
		} else if a < b {
			q2 = append(q2, Pair{a, b})
		} else {
			q3 = append(q3, Pair{a, b})
		}
	}
	// ai > bi
	sort.Slice(q1, func(i, j int) bool {
		return (q1[i][0] - q1[i][1]) > (q1[j][0] - q1[j][1])
	})
	// bi > ai
	sort.Slice(q2, func(i, j int) bool {
		return (q2[i][0] - q2[i][1]) < (q2[j][0] - q2[j][1])
	})
	q := Queue(append(append(q1, q3...), q2...))

	fmt.Println(q.Score())
}


For years, the Day of city N was held in the most rainy day of summer. New mayor decided to break this tradition and select a not-so-rainy day for the celebration. The mayor knows the weather forecast for the $$$n$$$ days of summer. On the $$$i$$$-th day, $$$a_i$$$ millimeters of rain will fall. All values $$$a_i$$$ are distinct.The mayor knows that citizens will watch the weather $$$x$$$ days before the celebration and $$$y$$$ days after. Because of that, he says that a day $$$d$$$ is not-so-rainy if $$$a_d$$$ is smaller than rain amounts at each of $$$x$$$ days before day $$$d$$$ and and each of $$$y$$$ days after day $$$d$$$. In other words, $$$a_d < a_j$$$ should hold for all $$$d - x \le j < d$$$ and $$$d < j \le d + y$$$. Citizens only watch the weather during summer, so we only consider such $$$j$$$ that $$$1 \le j \le n$$$.Help mayor find the earliest not-so-rainy day of summer.

The first line contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 100\,000$$$, $$$0 \le x, y \le 7$$$) — the number of days in summer, the number of days citizens watch the weather before the celebration and the number of days they do that after. The second line contains $$$n$$$ distinct integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ denotes the rain amount on the $$$i$$$-th day.

Print a single integer — the index of the earliest not-so-rainy day of summer. We can show that the answer always exists.

NoteIn the first example days $$$3$$$ and $$$8$$$ are not-so-rainy. The $$$3$$$-rd day is earlier.In the second example day $$$3$$$ is not not-so-rainy, because $$$3 + y = 6$$$ and $$$a_3 > a_6$$$. Thus, day $$$8$$$ is the answer. Note that $$$8 + y = 11$$$, but we don't consider day $$$11$$$, because it is not summer.

package main

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

/* solution */

func solve() {

	n, x, y := readInt(), readInt(), readInt()
	arr := make([]int, n)

	for i := 0; i < n; i++ {
		arr[i] = readInt()
	}

START:
	for i := 0; i < n; i++ {

		for j := i - x; j <= i+y; j++ {
			if j >= 0 && j < n && arr[i] > arr[j] {
				continue START
			}
		}

		println(strconv.Itoa(i + 1))
		return

	}
}

/* stubs */

func main() {

	f := os.Stdin
	// f, _ := os.Open("input.txt")
	defer f.Close()

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)
	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
}

func println(s string) {
	print(s)
	print("\n")
}

package main

import (
	"bufio"
	"os"
	"runtime"
	"strconv"
)

/* solution */

func solve() {

	n, x, y := readInt(), readInt(), readInt()
	arr := make([]int, n)

	for i := 0; i < n; i++ {
		arr[i] = readInt()
	}

START:
	for i := 0; i < n; i++ {

		for j := i - x; j <= i+y; j++ {
			if j >= 0 && j < n && arr[i] > arr[j] {
				continue START
			}
		}

		println(strconv.Itoa(i + 1))
		return

	}
}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
	} else {
		f = os.Stdin
	}
	defer f.Close()

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)
	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
}

func println(s string) {
	print(s)
	print("\n")
}

package main

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

const (
  inf = 0x3f3f3f3f
)

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

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

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

  n, x, y := 0, 0, 0
  fmt.Fscanf(rdr, "%d %d %d\n", &n, &x, &y)

  a := make([]int, n)

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

  res := solve(a, n, x, y)
  fmt.Printf("%d\n", res)
}

func solve(a []int, n, x, y int) int {
  k := max(x, y) + 1
  mins := make([][]int, k)
  for i := range mins {
    mins[i] = make([]int, n + 1)
  }

  for j := 0; j <= n; j++ {
    mins[0][j] = inf
  }
  for i := 1; i < k; i++ {
    for j := 0; j < n; j++ {
      mins[i][j] = min(a[j], mins[i - 1][j + 1])
    }
    mins[i][n] = inf
  }

  res := inf
  for i := 0; i < n; i++ {
    left, right := inf, inf
    if i < x {
      left = mins[i][0]
    } else {
      left = mins[x][i - x]
    }
    right = mins[y][i + 1]
    if a[i] < left && a[i] < right {
      res = i + 1
      break
    }
  }
  return res
}

package main

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

func main() {
	var j, d int64
	nums := readInts()
	n, x, y := nums[0], nums[1], nums[2]
	a := readInts()
	for d = 0; d < n; d++ {
		fnd := true
		j = d - 1
		for j >= 0 && j >= d-x {
			if a[d] >= a[j] {
				fnd = false
				break
			}
			j--
		}
		if fnd {
			j = d + 1
			for j < n && j <= d+y {
				if a[d] >= a[j] {
					fnd = false
					break
				}
				j++
			}
		}
		if fnd {
			fmt.Println(d + 1)
			break
		}
	}
}

var reader = bufio.NewReaderSize(os.Stdin, 1024*1024)

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

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

func readInt() int64 {
	temp := readLine()
	res, _ := strconv.ParseInt(temp, 10, 64)
	return res
}

func readInts() []int64 {
	nums := strings.Split(readLine(), " ")
	res := make([]int64, len(nums))
	for i, str := range nums {
		res[i], _ = strconv.ParseInt(str, 10, 64)
	}
	return res
}

func readLine() string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}
	return strings.TrimRight(string(str), "\r\n")
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var (
		n int
		x int
		y int
	)
	_, _ = fmt.Fscanf(input, "%d %d %d", &n, &x, &y)
	days := make([]int, n)
	for i := 0; i < n; i += 1 {
		_, _ = fmt.Fscan(input, &days[i])
	}

	min := days[0]
	ansIndex := 1
	count := 0
	for i := 1; i < n; i += 1 {
		if count == y {
			break
		}
		if days[i] < min {
			min = days[i]
			ansIndex = i + 1
			count = 0
		} else {
			count += 1
		}
	}
	fmt.Println(ansIndex)
}

package main

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

const maxn = 100000 + 5

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

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}
func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, x, y int
	fmt.Fscanf(reader, "%d %d %d\n", &n, &x, &y)
	summer := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &summer[i])
	}
	fmt.Fscanf(reader, "\n")
	for i := 0; i < n; i++ {
		flag := true
		for j := max(0, i - x); j <= min(n - 1, i + y); j ++ {
			if summer[j] < summer[i] {
				flag = false;
				break;
			}
		}
		if flag {
			fmt.Println(i + 1)
			break
		}
	}
}

package main

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

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	t, _ := strconv.Atoi(sc.Text())
	return t
}

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

func main() {
	var n, x, y int
	fmt.Scan(&n, &x, &y)
	a := make([]int, n+1)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 1; i <= n; i++ {
		a[i] = nextInt(sc)
	}
	var ok bool
	for day := 1; day <= n; day++ {
		ok = true
		lower := max(day-x, 1)
		upper := min(day+y, n)
		for j := lower; j <= upper; j++ {
			if j != day {
				if j < day {
					if a[j] < a[day] {
						ok = false
						break
					}
				} else {
					if a[j] < a[day] {
						ok = false
						break
					}
				}
			}
		}
		if ok {
			fmt.Println(day)
			return
		}
	}
}

package main

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

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

	var n, x, y int
	fmt.Fscanf(input, "%d %d %d\n", &n, &x, &y)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(input, "%d", &a[i])
	}
	for i := 0; i < n; i++ {
		pass := true
		for j := 0; j < x && i-j-1 > 0 && pass; j++ {
			if a[i] >= a[i-j-1] {
				pass = false
			}
		}
		for j := 0; j < y && i+j+1 < n && pass; j++ {
			if a[i] >= a[i+j+1] {
				pass = false
			}
		}
		if pass {
			fmt.Println(i + 1)
			return
		}
	}
}

package main

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

var n, x, y int

func min(vars ...int) int {
	min := vars[0]
	for _, i := range vars {
		if min > i {
			min = i
		}
	}
	return min
}

func max(vars ...int) int {
	max := vars[0]
	for _, i := range vars {
		if max < i {
			max = i
		}
	}
	return max
}

func main() {
	input := bufio.NewReader(os.Stdin)
	fmt.Fscanf(input, "%d %d %d\n", &n, &x, &y)
	var a = make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(input, "%d", &a[i])
	}
	var end int
	for idx, val := range a {
		if idx == 0 {
			continue
		}
		end = min(idx+y, n)
		bad := false
		for i := max(1, idx-x); i <= end; i++ {
			if i == idx {
				continue
			}
			if a[i] < val {
				bad = true
				break
			}
		}
		if !bad {
			fmt.Println(idx)
			return
		}
	}
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var n, x, y int

	_, err := fmt.Fscanf(input, "%d %d %d\n", &n, &x, &y)
	if err != nil {
	}

	arr := make([]int, n)
	min := math.MaxInt32
	var xx, yy, index int
	index = 1
	for i := 0; i < n; i++ {
		_, err := fmt.Fscanf(input, "%v", &arr[i])
		if err != nil {
		}

		if (xx >= x || index-x <= 0) && yy >= y {
			fmt.Println(index)
			return
		}

		if arr[i] < min {
			min = arr[i]
			index = i + 1
			xx += 1
			yy = 0
		} else {
			yy += 1
		}

	}
	fmt.Println(index)
	_, err = fmt.Fscanf(input, "\n")

}

package main

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

func main() {

	//stdin, err := os.Open("in.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("test.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)

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

	for i := 0; i < n; i++ {
		f := true
		for j := int(math.Max(float64(i-x), float64(0))); j <= int(math.Min(float64(i+y), float64(n-1))); j++ {
			if i != j {
				//	fmt.Println(a[i], a[j])
				if a[i] >= a[j] {
					f = false
				}
			} else {
				//	fmt.Println("--")
			}
		}
		//fmt.Fprintf(writer, "->%d\n", a[i])
		if f {
			fmt.Fprintf(writer, "%d\n", i+1)
			break
		}
	}

	writer.Flush()
}

// Author: sighduck
// URL: https://codeforces.com/contest/1199/problem/A

package main

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

func Solve(n int, x int, y int, a []int) int {
	answerIndex := 0
	right := 0

	for i := 1; i < n; i++ {
		if right == y {
			return answerIndex + 1
		} else if a[i] < a[answerIndex] {
			right = 0
			answerIndex = i
		} else if a[i] > a[answerIndex] {
			right += 1
		}
	}

	return answerIndex + 1
}

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

	var n int
	var x int
	var y int

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

package main

import (
	"bufio"
	"os"
	"runtime"
	"strconv"
)

/* solution */

func solve() {

	n, x, y := readInt(), readInt(), readInt()
	arr := make([]int, n)

	for i := 0; i < n; i++ {
		arr[i] = readInt()
	}

START:
	for i := 0; i < n; i++ {

		for j := i - x; j <= i+y; j++ {
			if j >= 0 && j < n && arr[i] > arr[j] {
				continue START
			}
		}

		println(strconv.Itoa(i + 1))
		return

	}
}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOOS == "windows" {
		f, _ = os.Open("input.txt")
	} else {
		f = os.Stdin
	}
	defer f.Close()

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)
	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
}

func println(s string) {
	print(s)
	print("\n")
}

package main

import (
	"bufio"
	"os"
	"runtime"
	"strconv"
)

/* solution */

func solve() {

	n, x, y := readInt(), readInt(), readInt()
	arr := make([]int, n)

	for i := 0; i < n; i++ {
		arr[i] = readInt()
	}

START:
	for i := 0; i < n; i++ {

		for j := i - x; j <= i+y; j++ {
			if j >= 0 && j < n && arr[i] > arr[j] {
				continue START
			}
		}

		println(strconv.Itoa(i + 1))
		return

	}
}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
	} else {
		f = os.Stdin
	}
	defer f.Close()

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	println(runtime.GOROOT())

	reader = bufio.NewScanner(f)
	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
}

func println(s string) {
	print(s)
	print("\n")
}

package main

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

/* solution */

func solve() {

	n, x, y := readInt(), readInt(), readInt()
	arr := make([]int, n)

	for i := 0; i < n; i++ {
		arr[i] = readInt()
	}

START:
	for i := 0; i < n; i++ {

		for j := i - x; j <= i+y; j++ {
			if j >= 0 && j < n && arr[i] > arr[j] {
				continue START
			}
		}

		println(strconv.Itoa(i + 1))
		return

	}
}

/* stubs */

func main() {

	f, _ := os.Open("input.txt")
	defer f.Close()

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)
	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
}

func println(s string) {
	print(s)
	print("\n")
}

package main

import (
  "fmt"
)

const (
  inf = 0x3f3f3f3f
)

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

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

func main() {
  n, x, y := 0, 0, 0
  fmt.Scanf("%d%d%d", &n, &x, &y)

  a := make([]int, n)

  for i := 0; i < n; i++ {
    fmt.Scanf("%d", &a[i])
  }

  res := solve(a, n, x, y)
  fmt.Printf("%d\n", res)
}

func solve(a []int, n, x, y int) int {
  k := max(x, y) + 1
  mins := make([][]int, k)
  for i := range mins {
    mins[i] = make([]int, n + 1)
  }

  for j := 0; j <= n; j++ {
    mins[0][j] = inf
  }
  for i := 1; i < k; i++ {
    for j := 0; j < n; j++ {
      mins[i][j] = min(a[j], mins[i - 1][j + 1])
    }
    mins[i][n] = inf
  }

  res := inf
  for i := 0; i < n; i++ {
    left, right := inf, inf
    if i < x {
      left = mins[i][0]
    } else {
      left = mins[x][i - x]
    }
    right = mins[y][i + 1]
    if a[i] < left && a[i] < right {
      res = i + 1
      break
    }
  }
  return res
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var (
		n int
		x int
		y int
	)
	_, _ = fmt.Fscanf(input, "%d %d %d", &n, &x, &y)
	days := make([]int, n)
	for i := 0; i < n; i += 1 {
		_, _ = fmt.Fscan(input, &days[i])
	}

	min := days[0]
	ansIndex := 1
	count := 0
	for i := 1; i < n; i += 1 {
		if days[i] < min {
			min = days[i]
			ansIndex = i + 1
			count = 0
		} else {
			count += 1
			if count == y {
				break
			}
		}
	}
	fmt.Println(ansIndex)
}

package main

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

const maxn = 100000 + 5

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

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}
func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, x, y int
	fmt.Fscanf(reader, "%d %d %d\n", &n, &x, &y)
	summer := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &summer[i])
	}
	fmt.Fscanf(reader, "\n")
	for i := 0; i < n; i++ {
		flag := true
		for j := i - 1; j >= max(0, i-x); j-- {
			if summer[j] < summer[j+1] {
				flag = false
				break
			}
		}
		if !flag {
			continue
		}
		for j := i + 1; j <= min(n-1, i+y); j++ {
			if summer[j] < summer[j-1] {
				flag = false
				break
			}
		}
		if flag {
			fmt.Println(i + 1)
			break
		}
	}
}

package main

import (
	"fmt"
)

var n, x, y int

func min(vars ...int) int {
	min := vars[0]
	for _, i := range vars {
		if min > i {
			min = i
		}
	}
	return min
}

func max(vars ...int) int {
	max := vars[0]
	for _, i := range vars {
		if max < i {
			max = i
		}
	}
	return max
}

func main() {
	fmt.Scan(&n, &x, &y)
	var a = make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Scan(&a[i])
	}
	var end int
	for idx, val := range a {
		if idx == 0 {
			continue
		}
		end = min(idx+y, n)
		bad := false
		for i := max(1, idx-x); i <= end; i++ {
			if i == idx {
				continue
			}
			if a[i] < val {
				bad = true
				break
			}
		}
		if !bad {
			println(idx)
			return
		}
	}
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var n, x, y int

	_, err := fmt.Fscanf(input, "%d %d %d\n", &n, &x, &y)
	if err != nil {
	}

	arr := make([]int, n)
	min := math.MaxInt32
	var xx, yy, index int
	for i := 0; i < n; i++ {
		_, err := fmt.Fscanf(input, "%v", &arr[i])
		if err != nil {
		}

		if xx >= x && yy >= y {
			fmt.Println(index)
			return
		}

		if arr[i] < min {
			min = arr[i]
			index = i + 1
			xx += 1
			yy = 0
		} else {
			if xx < x {
				xx = 0
				yy = 0
			} else {
				yy += 1
			}
		}

	}
	fmt.Println(index)
	_, err = fmt.Fscanf(input, "\n")

}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var n, x, y int

	_, err := fmt.Fscanf(input, "%d %d %d\n", &n, &x, &y)
	if err != nil {
	}

	arr := make([]int, n)
	min := math.MaxInt32
	var xx, yy, index int
	index = 1
	for i := 0; i < n; i++ {
		_, err := fmt.Fscanf(input, "%v", &arr[i])
		if err != nil {
		}

		if (xx >= x || index-x < 0) && yy >= y {
			fmt.Println(index)
			return
		}

		if arr[i] < min {
			min = arr[i]
			index = i + 1
			xx += 1
			yy = 0
		} else {
			yy += 1
		}

	}
	fmt.Println(index)
	_, err = fmt.Fscanf(input, "\n")

}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var n, x, y int

	_, err := fmt.Fscanf(input, "%d %d %d\n", &n, &x, &y)
	if err != nil {
	}

	arr := make([]int, n)
	min := math.MaxInt32
	var xx, yy, index int
	index = 1
	for i := 0; i < n; i++ {
		_, err := fmt.Fscanf(input, "%v", &arr[i])
		if err != nil {
		}

		if xx >= x && yy >= y {
			fmt.Println(index)
			return
		}

		if arr[i] < min {
			min = arr[i]
			index = i + 1
			xx += 1
			yy = 0
		} else {
			if xx < x {
				xx = 0
				yy = 0
			} else {
				yy += 1
			}
		}

	}
	fmt.Println(index)
	_, err = fmt.Fscanf(input, "\n")

}

package main

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

func main() {

	//stdin, err := os.Open("in.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("test.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)

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

	for i := x; i < n-y; i++ {
		f := true
		for j := i - x; j <= i+y; j++ {
			if i != j {
				if a[i] >= a[j] {
					f = false

				}
			}
		}
		if f {
			fmt.Fprintf(writer, "%d\n", i+1)
			break
		}
	}

	writer.Flush()
}


Devu and his brother love each other a lot. As they are super geeks, they only like to play with arrays. They are given two arrays a and b by their father. The array a is given to Devu and b to his brother. As Devu is really a naughty kid, he wants the minimum value of his array a should be at least as much as the maximum value of his brother's array b. Now you have to help Devu in achieving this condition. You can perform multiple operations on the arrays. In a single operation, you are allowed to decrease or increase any element of any of the arrays by 1. Note that you are allowed to apply the operation on any index of the array multiple times.You need to find minimum number of operations required to satisfy Devu's condition so that the brothers can play peacefully without fighting.

The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105). The second line will contain n space-separated integers representing content of the array a (1 ≤ ai ≤ 109). The third line will contain m space-separated integers representing content of the array b (1 ≤ bi ≤ 109).

You need to output a single integer representing the minimum number of operations needed to satisfy Devu's condition.

NoteIn example 1, you can increase a1 by 1 and decrease b2 by 1 and then again decrease b2 by 1. Now array a will be [3; 3] and array b will also be [3; 3]. Here minimum element of a is at least as large as maximum element of b. So minimum number of operations needed to satisfy Devu's condition are 3.In example 3, you don't need to do any operation, Devu's condition is already satisfied.

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol439D(_r io.Reader, _w io.Writer) {
	ternarySearchInt := func(l, r int, f func(x int) int64) int64 {
		for l+3 <= r {
			m1 := l + (r-l)/3
			m2 := r - (r-l)/3
			v1, v2 := f(m1), f(m2)
			if v1 < v2 {
				r = m2
			} else {
				l = m1
			}
		}
		min := f(l)
		for i := l + 1; i <= r; i++ {
			if v := f(i); v < min {
				min = v
			}
		}
		return min
	}
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, m int
	Fscan(in, &n, &m)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	sort.Ints(a)
	b := make([]int, m)
	for i := range b {
		Fscan(in, &b[i])
	}
	sort.Ints(b)

	min, max := a[0], b[m-1]
	if min >= max {
		Fprintln(out, 0)
		return
	}
	Fprint(out, ternarySearchInt(min, max, func(tar int) (cnt int64) {
		for _, v := range a {
			if v >= tar {
				break
			}
			cnt += int64(tar - v)
		}
		for i := m - 1; i >= 0; i-- {
			if b[i] <= tar {
				break
			}
			cnt += int64(b[i] - tar)
		}
		return
	}))
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol439D(_r io.Reader, _w io.Writer) {
	ternarySearchInt := func(l, r int, f func(x int) int64) int {
		for l+3 <= r {
			m1 := l + (r-l)/3
			m2 := r - (r-l)/3
			v1, v2 := f(m1), f(m2)
			if v1 < v2 {
				r = m2
			} else {
				l = m1
			}
		}
		min, minI := f(l), l
		for i := l + 1; i <= r; i++ {
			if v := f(i); v < min {
				min, minI = v, i
			}
		}
		return minI
	}

	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, m int
	Fscan(in, &n, &m)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	sort.Ints(a)
	b := make([]int, m)
	for i := range b {
		Fscan(in, &b[i])
	}
	sort.Ints(b)

	min, max := a[0], b[m-1]
	if min >= max {
		Fprintln(out, 0)
		return
	}
	f := func(tar int) (cnt int64) {
		for _, v := range a {
			if v >= tar {
				break
			}
			cnt += int64(tar - v)
		}
		for i := m - 1; i >= 0; i-- {
			if b[i] <= tar {
				break
			}
			cnt += int64(b[i] - tar)
		}
		return
	}
	i := ternarySearchInt(min, max, f)
	Fprintln(out, f(i))
}

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

package main

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

func f(k int64, a, b []int64) int64 {
	ans := int64(0)
	for i := 0; i < len(a); i++ {
		if a[i] < k {
			ans += k - a[i]
		}
	}
	for i := 0; i < len(b); i++ {
		if b[i] > k {
			ans += b[i] - k
		}
	}
	return ans
}

func main() {

	//stdin, err := os.Open("in.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

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

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

	lo, hi := int64(0), int64(10e9)
	resp := int64(10e15)

	for i := 0; i < 100; i++ {
		mid1 := lo + (hi - lo) / 3
		mid2 := hi - (hi - lo) / 3

		if f(mid1, a, b) < resp {
			resp = f(mid1, a, b)
		}

		if f(mid2, a, b) < resp {
			resp = f(mid2, a, b)
		}

		if f(mid1, a, b) < f(mid2, a, b) {
			hi = mid2
		} else {
			lo = mid1
		}
	}

	fmt.Println(resp)
}

package main

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

func abs(a int) int {
	if a > 0 {
		return a
	}
	return -a
}

func f(k int, a, b []int) int64 {
	ans := int64(0)
	for i := 0; i < len(a); i++ {
		if a[i] < k {
			ans += int64(k - a[i])
		}
	}

	for i := 0; i < len(b); i++ {
		if b[i] > k {
			ans += int64(b[i] - k)
		}
	}

	return ans
}

func main() {

	//stdin, err := os.Open("in.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

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

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

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

	fmt.Fscanf(reader, "\n")

	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d", &b[i])
	}

	lo, hi := 0, 1000000000
	ans := int64(10e16)

	for i := 0; i < 100; i++ {
		if lo > hi {
			break
		}
		mid1 := lo + (hi - lo) / 3
		mid2 := hi - (hi - lo) / 3

		if f(mid1, a, b) < ans {
			ans = f(mid1, a, b)
		}

		if f(mid2, a, b) < ans {
			ans = f(mid2, a, b)
		}

		if f(mid1, a, b) >= f(mid2, a, b) {
			lo = mid1
		} else {
			hi = mid2
		}
	}

	fmt.Println(ans)

}

package main

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

func abs(a int) int {
	if a > 0 {
		return a
	}
	return -a
}

func f(k int, a, b []int) int64 {
	ans := int64(0)
	for i := 0; i < len(a); i++ {
		if a[i] < k {
			ans += int64(k - a[i])
		}
	}

	for i := 0; i < len(b); i++ {
		if b[i] > k {
			ans += int64(b[i] - k)
		}
	}

	return ans
}

func main() {

	//stdin, err := os.Open("in.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

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

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

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

	fmt.Fscanf(reader, "\n")

	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d", &b[i])
	}

	lo, hi := 0, 1000000000
	ans := int64(10e10)

	for i := 0; i < 100; i++ {
		if lo > hi {
			break
		}
		mid1 := lo + (hi - lo) / 3
		mid2 := hi - (hi - lo) / 3

		if f(mid1, a, b) < ans {
			ans = f(mid1, a, b)
		}

		if f(mid2, a, b) < ans {
			ans = f(mid2, a, b)
		}

		if f(mid1, a, b) >= f(mid2, a, b) {
			lo = mid1
		} else {
			hi = mid2
		}
	}

	fmt.Println(ans)

}


You are given two integers $$$l$$$ and $$$r$$$ in binary representation. Let $$$g(x, y)$$$ be equal to the bitwise XOR of all integers from $$$x$$$ to $$$y$$$ inclusive (that is $$$x \oplus (x+1) \oplus \dots \oplus (y-1) \oplus y$$$). Let's define $$$f(l, r)$$$ as the maximum of all values of $$$g(x, y)$$$ satisfying $$$l \le x \le y \le r$$$.Output $$$f(l, r)$$$.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^6$$$) — the length of the binary representation of $$$r$$$. The second line contains the binary representation of $$$l$$$ — a string of length $$$n$$$ consisting of digits $$$0$$$ and $$$1$$$ ($$$0 \le l < 2^n$$$). The third line contains the binary representation of $$$r$$$ — a string of length $$$n$$$ consisting of digits $$$0$$$ and $$$1$$$ ($$$0 \le r < 2^n$$$). It is guaranteed that $$$l \le r$$$. The binary representation of $$$r$$$ does not contain any extra leading zeros (if $$$r=0$$$, the binary representation of it consists of a single zero). The binary representation of $$$l$$$ is preceded with leading zeros so that its length is equal to $$$n$$$.

In a single line output the value of $$$f(l, r)$$$ for the given pair of $$$l$$$ and $$$r$$$ in binary representation without extra leading zeros.

NoteIn sample test case $$$l=19$$$, $$$r=122$$$. $$$f(x,y)$$$ is maximal and is equal to $$$127$$$, with $$$x=27$$$, $$$y=100$$$, for example.

package main

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

/*
	对于相邻的两个数，一个偶数和下一奇数，那么它们的异或就是1
	1.l[0] != r[0] => 10..00(n)^01..11(n) => 11.11(n)
	2.l == r => l或r
	3.l[0] == r[0] && l != r {
		策略 => 保留r[0] =>奇数个数异或
		设区间[x,y] => 包含数字 y-x+1 = z(奇数个)
		对于前面z-1个数，若是以偶数开始奇数结束
		若((z-1)/2)%2 == 0(偶数对偶数和奇数)结果为0
		若((z-1)/2)%2 == 1(奇数对偶数和奇数)结果为1
		再与第z个数异或，结果为z或z+1
		若是以奇数开始偶数结束同理
	} => r是奇数 => r+1, r是偶数 => r

*/
var l, r string

func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n int
	Fscan(in, &n, &l, &r)
	if l[0] != r[0] {
		var b strings.Builder
		for i := 0; i < n; i++ {
			b.WriteByte('1')
		}
		Fprint(out, b.String())
		return
	}
	i := 0
	for ; i < n && !(l[i] == '0' && r[i] == '1'); i++ {
	}
	for i++; i < n; i++ {
		if l[i] == '0' || r[i] == '1' {
			Fprint(out, r[:n-1]+"1")
			return
		}
	}
	Fprint(out, r)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	var n int
	var l, r string
	Fscan(bufio.NewReader(os.Stdin), &n, &l, &r)
	if l[0] != r[0] {
		Print(strings.Repeat("1", n))
		return
	}
	i := 0
	for ; i < n; i++ {
		if r[i] == '1' && l[i] == '0' {
			break
		}
	}
	for i++; i < n; i++ {
		if r[i] == '1' || l[i] == '0' {
			Print(r[:n-1] + "1")
			return
		}
	}
	Print(r)
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/big"
	"os"
	"strings"
)

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

	var n int
	var l, r string
	Fscan(in, &n, &l, &r)
	if n == 1 {
		Fprint(out, r)
	} else if l[0] == '0' {
		Fprint(out, strings.Repeat("1", n))
	} else if r[n-1] == '1' {
		Fprint(out, r)
	} else {
		v, w := &big.Int{}, &big.Int{}
		Sscanf(l, "%b", v)
		Sscanf(r, "%b", w)
		if w.Sub(w, v).Cmp(big.NewInt(1)) > 0 {
			Fprint(out, r[:n-1]+"1")
		} else {
			Fprint(out, r)
		}
	}
}

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

package main

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

/*
	对于相邻的两个数，一个偶数和下一奇数，那么它们的异或就是1
	1.l[0] != r[0] => 10..00(n)^01..11(n) => 11.11(n)
	2.l == r => l或r
	3.l[0] == r[0] && l != r {
		策略 => 保留r[0] =>奇数个数异或
		设区间[x,y] => 包含数字 y-x+1 = z(奇数个)
		对于前面z-1个数，若是以偶数开始奇数结束
		若((z-1)/2)%2 == 0(偶数对偶数和奇数)结果为0
		若((z-1)/2)%2 == 1(奇数对偶数和奇数)结果为1
		再与第z个数异或，结果为z或z+1
		若是以奇数开始偶数结束同理
	} => r是奇数 => r+1, r是偶数 => r

*/
var l, r string

func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n int
	Fscan(in, &n, &l, &r)
	if l[0] != r[0] {
		var b strings.Builder
		for i := 0; i < n; i++ {
			b.WriteByte('1')
		}
		Fprint(out, b.String())
		return
	}
	i := 0
	for ; i < n && !(l[i] == '0' && r[i] == '1'); i++ {
	}
	for i++; i < n; i++ {
		if l[i] == '0' && r[i] == '1' {
			Fprint(out, r[:n-1]+"1")
			return
		}
	}
	Fprint(out, r)
}

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

package main

import (
	. "fmt"
	"strings"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	var n int
	var l, r string
	Scan(&n, &l, &r)
	if l[0] == '0' {
		Print(strings.Repeat("1", n))
		return
	}
	i := 0
	for ; i < n; i++ {
		if r[i] == '1' && l[i] == '0' {
			break
		}
	}
	for i++; i < n; i++ {
		if r[i] == '1' || l[i] == '0' {
			Print(r[:n-1] + "1")
			return
		}
	}
	Print(r)
}


You have a deck of $$$n$$$ cards, and you'd like to reorder it to a new one.Each card has a value between $$$1$$$ and $$$n$$$ equal to $$$p_i$$$. All $$$p_i$$$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $$$p_1$$$ stands for the bottom card, $$$p_n$$$ is the top card. In each step you pick some integer $$$k > 0$$$, take the top $$$k$$$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as $$$\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$$$.Given the original deck, output the deck with maximum possible order you can make using the operation above.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of deck you have. The second line contains $$$n$$$ integers $$$p_1, p_2,\dots, p_n$$$ ($$$1 \le p_i \le n$$$; $$$p_i \neq p_j$$$ if $$$i \neq j$$$) — values of card in the deck from bottom to top. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.

For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them.

package main

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

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

func scanf(pattern string, a ...interface{})  { fmt.Fscanf(reader, pattern, a...) }
func printf(pattern string, a ...interface{}) { fmt.Fprintf(writer, pattern, a...) }
func ceil(a, b int64) int64                   { return (a + b - 1) / b }
func min(a, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}
func max(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func main() {
	defer writer.Flush()
	var T int
	scanf("%d\n", &T)
	for t := 0; t < T; t++ {
		var n int
		scanf("%d\n", &n)
		arr := make([]int64, n)
		pos := make([]int64, n+1)
		for i := 0; i < n; i++ {
			scanf("%d", &arr[i])
			pos[arr[i]] = int64(i)
		}
		scanf("\n")
		p := make([]int64, 0, n)

		visited := make([]bool, n+1)
		curMax := n
		for len(arr) > 0 {
			for visited[curMax] {
				curMax--
			}
			newP := arr[pos[curMax]:]
			p = append(p, newP...)
			arr = arr[:pos[curMax]]
			for _, i := range newP {
				visited[i] = true
			}
		}
		for i := range p {
			printf("%d ", p[i])
		}
		printf("\n")
	}
}

package main

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

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

func solve() {
	n := getI()
	nums := make([]int64, n)
	idxs := make(map[int64]int64)

	for i := int64(0); i < n; i++ {
		cur := getI()
		nums[i] = cur
		idxs[cur] = i
	}

	prev := idxs[n]
	res := nums[prev:]
	for i := n - 1; i > 0; i-- {
		cur := idxs[i]
		if cur > prev {
			continue
		}
		res = append(res, nums[cur:prev]...)
		prev = cur
	}
	out(njoin(res...))
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 4096), 1e9)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	for i := getI(); i > 0; i-- {
		solve()
	}
	out()
}

// !------- HELPERS -------!

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

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

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

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int64) []int64 {
	ret := make([]int64, N)
	for i := range ret {
		ret[i] = getI()
	}
	return ret
}

func getS() string {
	if !sc.Scan() {
		panic(sc.Err())
	}
	return sc.Text()
}

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

func nsum(data ...int64) int64 {
	var res int64
	for _, n := range data {
		res += n
	}
	return res
}

func njoin(s ...int64) string {
	return strings.Trim(fmt.Sprintf("%v", s), "[]")
}

func forEach(n int64, fn func(int64)) {
	for i := int64(0); i < n; i++ {
		fn(i)
	}
}

// min, max, asub, abs
func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

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

// min for n entry
func nmin(a ...int64) int64 {
	ret := a[0]
	for _, e := range a {
		ret = min(ret, e)
	}
	return ret
}

// max for n entry
func nmax(a ...int64) int64 {
	ret := a[0]
	for _, e := range a {
		ret = max(ret, e)
	}
	return ret
}

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

func abs(a int64) int64 {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int64, x int64) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int64, x int64) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

package main

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

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

func solve() {
	n := getI()
	var nums []int64
	var res []int64
	idxs := make(map[int64]int64)
	var f bool

	for i := int64(0); i < n; i++ {
		cur := getI()
		idxs[cur] = i

		if cur == n {
			f = true
		}

		if f {
			res = append(res, cur)
		} else {
			nums = append(nums, cur)
		}
	}

	prev := idxs[n]
	for i := n - 1; i > 0; i-- {
		cur := idxs[i]
		if cur > prev {
			continue
		}
		res = append(res, nums[cur:]...)
		nums = nums[:cur]
		prev = cur
	}
	out(njoin(res...))
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 4096), 1e9)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	for i := getI(); i > 0; i-- {
		solve()
	}
	out()
}

// !------- HELPERS -------!

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

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

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

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int64) []int64 {
	ret := make([]int64, N)
	for i := range ret {
		ret[i] = getI()
	}
	return ret
}

func getS() string {
	if !sc.Scan() {
		panic(sc.Err())
	}
	return sc.Text()
}

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

func nsum(data ...int64) int64 {
	var res int64
	for _, n := range data {
		res += n
	}
	return res
}

func njoin(s ...int64) string {
	return strings.Trim(fmt.Sprintf("%v", s), "[]")
}

func forEach(n int64, fn func(int64)) {
	for i := int64(0); i < n; i++ {
		fn(i)
	}
}

// min, max, asub, abs
func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

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

// min for n entry
func nmin(a ...int64) int64 {
	ret := a[0]
	for _, e := range a {
		ret = min(ret, e)
	}
	return ret
}

// max for n entry
func nmax(a ...int64) int64 {
	ret := a[0]
	for _, e := range a {
		ret = max(ret, e)
	}
	return ret
}

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

func abs(a int64) int64 {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int64, x int64) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int64, x int64) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

package main

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

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

func solve() {
	n := getI()
	nums := make([]int64, n)
	idxs := make(map[int64]int64)

	for i := int64(0); i < n; i++ {
		cur := getI()
		nums[i] = cur
		idxs[cur] = i
	}

	prev := idxs[n]
	res := nums[prev:]
	nums = nums[:prev]
	for i := n - 1; i > 0; i-- {
		cur := idxs[i]
		if cur > prev {
			continue
		}
		res = append(res, nums[cur:]...)
		nums = nums[:cur]
		prev = cur
	}
	out(njoin(res...))
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 4096), 1e9)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	for i := getI(); i > 0; i-- {
		solve()
	}
	out()
}

// !------- HELPERS -------!

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

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

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

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int64) []int64 {
	ret := make([]int64, N)
	for i := range ret {
		ret[i] = getI()
	}
	return ret
}

func getS() string {
	if !sc.Scan() {
		panic(sc.Err())
	}
	return sc.Text()
}

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

func nsum(data ...int64) int64 {
	var res int64
	for _, n := range data {
		res += n
	}
	return res
}

func njoin(s ...int64) string {
	return strings.Trim(fmt.Sprintf("%v", s), "[]")
}

func forEach(n int64, fn func(int64)) {
	for i := int64(0); i < n; i++ {
		fn(i)
	}
}

// min, max, asub, abs
func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

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

// min for n entry
func nmin(a ...int64) int64 {
	ret := a[0]
	for _, e := range a {
		ret = min(ret, e)
	}
	return ret
}

// max for n entry
func nmax(a ...int64) int64 {
	ret := a[0]
	for _, e := range a {
		ret = max(ret, e)
	}
	return ret
}

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

func abs(a int64) int64 {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int64, x int64) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int64, x int64) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

package main

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

func MIN(x, y int64) int64 {
	if x > y {
		return y
	} else {
		return x
	}
}

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

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}

}

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })
var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)
	for o := 0; o < t; o++ {
		var n int

		fmt.Fscan(input, &n)

		XARRAY := make([]int, n)
		init := 0

		ARRAYINDEX := make([]int, 0)

		for i := 0; i < n; i++ {
			fmt.Fscan(input, &XARRAY[i])
			if XARRAY[i] > init {
				init = XARRAY[i]
				ARRAYINDEX = append(ARRAYINDEX, i)

			}

		}
		ARRAYINDEX = append(ARRAYINDEX, n)
		l := len(ARRAYINDEX)

		for i := l - 2; i > -1; i-- {
			for j := ARRAYINDEX[i]; j < ARRAYINDEX[i+1]; j++ {
				fmt.Println(XARRAY[j])
			}

		}

	}

}

package main

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

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

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

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

		p := make([]int, n)

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

		order := make([]bool, n+1)
		MaxPO, last := n, n
		for i := n - 1; i >= 0; i-- {
			if p[i] == MaxPO {
				for a := i; a < last; a++ {
					fmt.Printf("%d ", p[a])
					order[p[a]] = true
				}
				for ; order[MaxPO]; MaxPO-- {

				}
				last = i
			}
		}
		fmt.Println()
	}

}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		ind, ele, p := make([]int, n+1), make([]int, n+1), 0
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &p)
			ind[p], ele[j+1] = j+1, p
		}
		s, e := ind[n], n
		for {
			mx := 0
			if s == 0 || e == 0 {
				break
			}
			for j := s; j <= e; j++ {
				fmt.Fprint(w, ele[j], " ")
				if ele[j] > mx {
					mx = ele[j]
				}
				ind[ele[j]] = 0
			}
			for {
				if mx > 0 && ind[mx] == 0 {
					mx--
				} else {
					break
				}
			}
			s, e = ind[mx], s-1
		}
		fmt.Fprintln(w, "")
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

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

func solveB() {
	var n int
	scan(&n)

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

	mx := make([]int, n)
	mx[0] = a[0]
	for i := 1; i < n; i++ {
		mx[i] = mx[i-1]
		if mx[i] < a[i] {
			mx[i] = a[i]
		}
	}

	to := n - 1
	i := n - 1
	need := mx[n-1]
	for ; i >= 0; i-- {
		if a[i] == need {
			for j := i; j <= to; j++ {
				printf("%d ", a[j])
			}
			to = i - 1
			if i > 0 {
				need = mx[i-1]
			}
		}
	}
	printf("\n")
}

func main() {
	defer writer.Flush()
	//redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solveB()
	}
}

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

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

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

func main() {

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

	var t int32
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n)
		b := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			b[i] = i
		}
		for i := 1; i < n; i++ {
			if a[b[i - 1]] > a[i] {
				b[i] = b[i - 1]
			}
		}
		x := n - 1
		for x >= 0 {
			y := b[x]
			for i := y; i <= x; i++ {
				fmt.Fprint(out, a[i], " ")
			}
			x = y - 1
		}
		fmt.Fprint(out, "\n")
	}

}

package main

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

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

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n)
		p := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &p[i])
		}
		moved := make([]bool, n+1)
		curMax, end := n, n
		for i := n - 1; i >= 0; i-- {
			if p[i] == curMax {
				for j := i; j < end; j++ {
					fmt.Printf("%d ", p[j])
					moved[p[j]] = true
				}
				for ; moved[curMax]; curMax-- {
				}
				end = i
			}
			//fmt.Println(p, moved, curMax)
		}
		fmt.Println()
	}
}

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

package main

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

var (
	println = fmt.Println

	t int
	n int
	P []int
)

func main() {
	defer stdout.Flush()

	t = readi()
	for tc := 0; tc < t; tc++ {
		n = readi()
		P = readis(n)

		solve()
	}
}

func solve() {
	memo := make([]int, n+1)

	A := []int{}
	target := n
	befIdx := n
	curIdx := n - 1
	for curIdx >= 0 {
		memo[target]++
		// targetを探す
		for curIdx >= 0 && P[curIdx] != target {
			memo[P[curIdx]]++
			curIdx--
		}

		A = append(A, P[curIdx:befIdx]...)
		befIdx = curIdx
		curIdx--

		// targetの更新
		for target > 0 && memo[target] > 0 {
			target--
		}
	}

	printf("%s\n", PrintIntsLine(A...))
}

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

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

const (
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPH_N  = 26
	INF_I64 = math.MaxInt64
	INF_B60 = 1 << 60
	INF_I32 = math.MaxInt32
	INF_B30 = 1 << 30
	NIL     = -1
	EPS     = 1e-10
)

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

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

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

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

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

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

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

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

	return s
}

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

// pow is integer version of math.Pow
// pow calculate a power by Binary Power (二分累乗法(O(log e))).
func pow(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 := pow(a, halfE)
		return half * half
	}

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

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

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

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

// debugf is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func debugf(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"
	"io"
	"io/ioutil"
	"os"
	"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()

	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		a := make([]int, n)
		pos := make([]int, n+1)
		for i := range a {
			Fscan(in, &a[i])
			pos[a[i]] = i
		}
		vis := make([]bool, n+1)
		ans := []int{}
		cur := n
		for len(a) > 0 {
			for vis[cur] {
				cur--
			}
			ans = append(ans, a[pos[cur]:]...)
			for _, v := range a[pos[cur]:] {
				vis[v] = true
			}
			a = a[:pos[cur]]
		}
		for _, v := range ans {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}

	var t int
	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 (
	"bufio"
	"fmt"
	"os"
)

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

func scanf(pattern string, a ...interface{})  { fmt.Fscanf(reader, pattern, a...) }
func printf(pattern string, a ...interface{}) { fmt.Fprintf(writer, pattern, a...) }
func ceil(a, b int64) int64                   { return (a + b - 1) / b }
func min(a, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}
func max(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func main() {
	defer writer.Flush()
	var T int
	scanf("%d\n", &T)
	printf("num test case %d\n", T)
	for t := 0; t < T; t++ {
		printf("current test case: %d\n", t)
		var n int
		scanf("%d\n", &n)
		arr := make([]int64, n)
		for i := 0; i < n; i++ {
			scanf("%d ", &arr[i])
		}
		p := make([]int64, 0, n)
		for len(arr) > 0 {
			maxNum := 0
			for i := 0; i < len(arr); i++ {
				if arr[maxNum] < arr[i] {
					maxNum = i
				}
			}
			p = append(p, arr[maxNum:]...)
			arr = arr[:maxNum]
		}
		for i := range p {
			if i != len(p)-1 {
				printf("%d ", p[i])
			} else {
				printf("%d\n", p[i])
			}
		}
	}
}

package main

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

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

func scanf(pattern string, a ...interface{})  { fmt.Fscanf(reader, pattern, a...) }
func printf(pattern string, a ...interface{}) { fmt.Fprintf(writer, pattern, a...) }
func ceil(a, b int64) int64                   { return (a + b - 1) / b }
func min(a, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}
func max(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func main() {
	defer writer.Flush()
	var T int
	scanf("%d\n", &T)
	printf("num test case %d\n", T)
	for t := 0; t < T; t++ {
		var n int
		scanf("%d\n", &n)
		arr := make([]int64, n)
		for i := 0; i < n; i++ {
			scanf("%d ", &arr[i])
		}
		p := make([]int64, 0, n)
		for len(arr) > 0 {
			maxNum := 0
			for i := 0; i < len(arr); i++ {
				if arr[maxNum] < arr[i] {
					maxNum = i
				}
			}
			p = append(p, arr[maxNum:]...)
			arr = arr[:maxNum]
		}
		for i := range p {
			if i != len(p)-1 {
				printf("%d ", p[i])
			} else {
				printf("%d\n", p[i])
			}
		}
	}
}

package main

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

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

func scanf(pattern string, a ...interface{})  { fmt.Fscanf(reader, pattern, a...) }
func printf(pattern string, a ...interface{}) { fmt.Fprintf(writer, pattern, a...) }
func ceil(a, b int64) int64                   { return (a + b - 1) / b }
func min(a, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}
func max(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func main() {
	defer writer.Flush()
	var T int
	scanf("%d\n", &T)
	for t := 0; t < T; t++ {
		var n int
		scanf("%d\n", &n)
		arr := make([]int64, n)
		for i := 0; i < n; i++ {
			scanf("%d ", &arr[i])
		}
		p := make([]int64, 0, n)
		for len(arr) > 0 {
			maxNum := 0
			for i := 0; i < len(arr); i++ {
				if arr[maxNum] < arr[i] {
					maxNum = i
				}
			}
			p = append(p, arr[maxNum:]...)
			arr = arr[:maxNum]
		}
		for i := range p {
			if i != len(p)-1 {
				printf("%d ", p[i])
			} else {
				printf("%d\n", p[i])
			}
		}
	}
}

package main

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

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

func scanf(pattern string, a ...interface{})  { fmt.Fscanf(reader, pattern, a...) }
func printf(pattern string, a ...interface{}) { fmt.Fprintf(writer, pattern, a...) }
func ceil(a, b int64) int64                   { return (a + b - 1) / b }
func min(a, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}
func max(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func main() {
	defer writer.Flush()
	var T int
	scanf("%d\n", &T)
	for t := 0; t < T; t++ {
		var n int
		scanf("%d\n", &n)
		arr := make([]int64, n)
		for i := 0; i < n; i++ {
			scanf("%d ", &arr[i])
		}
		p := make([]int64, 0, n)
		for len(arr) > 0 {
			maxNum := 0
			for i := 0; i < len(arr); i++ {
				if arr[maxNum] < arr[i] {
					maxNum = i
				}
			}
			p = append(p, arr[maxNum:]...)
			arr = arr[:maxNum]
		}
		for _, i := range p {
			printf("%d ", i)
		}
		fmt.Fprintf(writer, "\n")
	}
}

package main

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

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

func scanf(pattern string, a ...interface{})  { fmt.Fscanf(reader, pattern, a...) }
func printf(pattern string, a ...interface{}) { fmt.Fprintf(writer, pattern, a...) }
func ceil(a, b int64) int64                   { return (a + b - 1) / b }
func min(a, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}
func max(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func main() {
	defer writer.Flush()
	var T int
	scanf("%d\n", &T)
	for t := 0; t < T; t++ {
		var n int
		scanf("%d\n", &n)
		arr := make([]int64, n)
		for i := 0; i < n; i++ {
			scanf("%d ", &arr[i])
		}
		p := make([]int64, 0, n)
		for len(arr) > 0 {
			maxNum := 0
			for i := 0; i < len(arr); i++ {
				if arr[maxNum] < arr[i] {
					maxNum = i
				}
			}
			p = append(p, arr[maxNum:]...)
			arr = arr[:maxNum]
		}
		for _, i := range p {
			printf("%d ", i)
		}
	}
}

package main

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

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

func scanf(pattern string, a ...interface{})  { fmt.Fscanf(reader, pattern, a...) }
func printf(pattern string, a ...interface{}) { fmt.Fprintf(writer, pattern, a...) }
func ceil(a, b int64) int64                   { return (a + b - 1) / b }
func min(a, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}
func max(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func main() {
	defer writer.Flush()
	var T int
	scanf("%d\n", &T)
	for t := 0; t < T; t++ {
		var n int
		scanf("%d\n", &n)
		arr := make([]int64, n)
		for i := 0; i < n; i++ {
			scanf("%d ", &arr[i])
		}
		p := make([]int64, 0, n)
		for len(arr) > 0 {
			maxNum := 0
			for i := 0; i < len(arr); i++ {
				if arr[maxNum] < arr[i] {
					maxNum = i
				}
			}
			p = append(p, arr[maxNum:]...)
			arr = arr[:maxNum]
		}
		for _, i := range p {
			printf("%d ", i)
		}
		printf("\n")
	}
}

package main

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

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

func solve() {
	n := getI()
	idxs := make(map[int64]int64, n-1)
	prev := n
	var nums []int64
	var res []int64

	for i := int64(0); i < n; i++ {
		cur := getI()
		if cur == n {
			prev = i
		}

		if i < prev {
			nums = append(nums, cur)
			idxs[nums[i]] = i
		} else {
			res = append(res, cur)
		}

	}

	for i := n - 1; i > 0; i-- {
		cur := idxs[i]
		if cur > prev {
			continue
		}
		res = append(res, nums[cur:prev]...)
		prev = cur
	}
	out(njoin(res...))
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 4096), 1e9)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	for i := getI(); i > 0; i-- {
		solve()
	}
	out()
}

// !------- HELPERS -------!

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

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

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

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int64) []int64 {
	ret := make([]int64, N)
	for i := range ret {
		ret[i] = getI()
	}
	return ret
}

func getS() string {
	if !sc.Scan() {
		panic(sc.Err())
	}
	return sc.Text()
}

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

func nsum(data ...int64) int64 {
	var res int64
	for _, n := range data {
		res += n
	}
	return res
}

func njoin(s ...int64) string {
	return strings.Trim(fmt.Sprintf("%v", s), "[]")
}

func forEach(n int64, fn func(int64)) {
	for i := int64(0); i < n; i++ {
		fn(i)
	}
}

// min, max, asub, abs
func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

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

// min for n entry
func nmin(a ...int64) int64 {
	ret := a[0]
	for _, e := range a {
		ret = min(ret, e)
	}
	return ret
}

// max for n entry
func nmax(a ...int64) int64 {
	ret := a[0]
	for _, e := range a {
		ret = max(ret, e)
	}
	return ret
}

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

func abs(a int64) int64 {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int64, x int64) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int64, x int64) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}


You have given an array $$$a$$$ of length $$$n$$$ and an integer $$$x$$$ to a brand new robot. What the robot does is the following: it iterates over the elements of the array, let the current element be $$$q$$$. If $$$q$$$ is divisible by $$$x$$$, the robot adds $$$x$$$ copies of the integer $$$\frac{q}{x}$$$ to the end of the array, and moves on to the next element. Note that the newly added elements could be processed by the robot later. Otherwise, if $$$q$$$ is not divisible by $$$x$$$, the robot shuts down.Please determine the sum of all values of the array at the end of the process.

The first input line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 10^5$$$, $$$2 \leq x \leq 10^9$$$) — the length of the array and the value which is used by the robot. The next line contains integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the initial values in the array. It is guaranteed that the sum of values $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case output one integer — the sum of all elements at the end of the process.

NoteIn the first test case the array initially consists of a single element $$$[12]$$$, and $$$x=2$$$. After the robot processes the first element, the array becomes $$$[12, 6, 6]$$$. Then the robot processes the second element, and the array becomes $$$[12, 6, 6, 3, 3]$$$. After the robot processes the next element, the array becomes $$$[12, 6, 6, 3, 3, 3, 3]$$$, and then the robot shuts down, since it encounters an element that is not divisible by $$$x = 2$$$. The sum of the elements in the resulting array is equal to $$$36$$$.In the second test case the array initially contains integers $$$[4, 6, 8, 2]$$$, and $$$x=2$$$. The resulting array in this case looks like $$$ [4, 6, 8, 2, 2, 2, 3, 3, 4, 4, 1, 1, 1, 1, 1, 1]$$$.

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

func main() {
var input io.Reader = bufio.NewReader(os.Stdin)

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

for o:=0;o<t;o++{
var n, x int
fmt.Fscan(input, &n)
fmt.Fscan(input, &x)

ARRAY := make([]int, n)

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

b := 7

var SUM int64
for m :=0; m< n; m++{
SUM = SUM + int64(ARRAY[m])
}

q := x
for l :=0 ;b > 6;{
l = l +1
M :=0
for i :=0; i<len(ARRAY); i++{
if ARRAY[i] % q == 0{
SUM = SUM + int64(ARRAY[i])
M = M + 1
}else{
b = 5
break
}
}
if M == n{
q = q * x
M = 0
}else{
b = 5
break
}

}

fmt.Println(SUM)

}
}

package main

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

type Age struct {
	key int
	cnt int
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

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

		var ans int64
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
			ans += int64(arr[i])
		}

		y := make([]Age, 0)
		ok := true
		for i := 0; i < n; i++ {
			if arr[i]%x == 0 {
				yy := Age{}
				yy.key = arr[i] / x
				yy.cnt = x

				ans += int64(yy.key * yy.cnt)

				y = append(y, yy)
			} else {
				ok = false
				break
			}
		}

		if ok {
			pos := 0
			for {
				if y[pos].key%x == 0 {
					yy := Age{}
					yy.key = y[pos].key / x
					yy.cnt = x * y[pos].cnt

					ans += int64(yy.key * yy.cnt)

					y = append(y, yy)
					y = y[1:]
				} else {
					break
				}
			}
		}

		y = y[0:0]

		fmt.Println(ans)
	}
}

package main

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

type Age struct {
	key int
	cnt int
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

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

		var ans int64
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
			ans += int64(arr[i])
		}

		y := make([]Age, 0)
		ok := true
		for i := 0; i < n; i++ {
			if arr[i]%x == 0 {
				yy := Age{}
				yy.key = arr[i] / x
				yy.cnt = x

				ans += int64(yy.key * yy.cnt)

				y = append(y, yy)
			} else {
				ok = false
				break
			}
		}

		if ok {
			/*l := len(y)
			for i := 0; i < l; i++ {
				if y[i].key%x == 0 {
					yy := Age{}
					yy.key = y[i].key / x
					yy.cnt = x * y[i].cnt

					ans += int64(yy.key * yy.cnt)

					y = append(y, yy)
					l++
				} else {
					break
				}
			}*/

			pos := 0
			for {
				if y[pos].key%x == 0 {
					yy := Age{}
					yy.key = y[pos].key / x
					yy.cnt = x * y[pos].cnt

					ans += int64(yy.key * yy.cnt)

					y = append(y, yy)
					y = y[1:]
				} else {
					break
				}
			}
		}

		fmt.Println(ans)
	}
}

package main

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

type Age struct {
	key int
	cnt int
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

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

		var ans int64
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
			ans += int64(arr[i])
		}

		y := make([]Age, 0)
		ok := true
		for i := 0; i < n; i++ {
			if arr[i]%x == 0 {
				yy := Age{}
				yy.key = arr[i] / x
				yy.cnt = x

				ans += int64(yy.key * yy.cnt)

				y = append(y, yy)
			} else {
				ok = false
				break
			}
		}

		if ok {
			l := len(y)
			for i := 0; i < l; i++ {
				if y[i].key%x == 0 {
					yy := Age{}
					yy.key = y[i].key / x
					yy.cnt = x * y[i].cnt

					ans += int64(yy.key * yy.cnt)

					y = append(y, yy)
					l++
				} else {
					break
				}
			}
		}

		fmt.Println(ans)
	}
}

/*
5 2
8 16 84 58 17
349
*/

package main

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

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
	t   int
	n   int
	x   int
)

func main() {

	defer out.Flush()

	fmt.Fscan(in, &t)

	for ; t > 0; t-- {

		fmt.Fscan(in, &n, &x)
		a := make([]int, n)

		//輸入資料
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}

		fmt.Fprintln(out, solve3(n, x, a))

	}

}

func solve3(n int, x int, a []int) int64 {

	flag := false

	//把原本的加起來
	var SUM int64
	for m := 0; m < n; m++ {
		SUM += int64(a[m])
	}

	q := x
	for !flag {

		for i := 0; i < len(a); i++ {
			if a[i]%q == 0 {
				SUM += int64(a[i])
			} else {
				flag = true
				break
			}
		}

		q *= x

	}

	return SUM

}

//印出數字陣列
func printArr(array []int) {

	for i := range array {
		fmt.Printf("%d ", array[i])
	}

	fmt.Println()

}

func solve2(n int, x int, a []int) int64 {

	sum := int64(0)
	index := 0

	for a[index]%x == 0 {

		for i := 0; i < x; i++ {
			a = append(a, a[index]/x)
		}

		index++

	}

	//printArr(a)

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

	return sum
}

func solve(n int, x int, a []int) int64 {

	sum := int64(0)

	for i := range a {

		num := int64(a[i])
		sum += int64(a[i])

		fmt.Println(sum)

		for a[i]%x == 0 {
			sum += num
			a[i] /= x
			fmt.Println(sum)
		}

	}

	return sum
}

// https://codeforces.com/contest/1472/problem/E

package main

import (
	"bufio"
	"fmt"
	"os"
)
var (
    input = bufio.NewReader(os.Stdin)
    output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assertion!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
func main() {
    var t int
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
    output.Flush()
}
var (
    x int64 // min of stop
    m map[int]int
)

func solve() {
    var n int64
    fmt.Fscan(input, &n, &x)
    a := make([]int64, n)
    for i := 0; i < len(a); i++ {
        fmt.Fscan(input, &a[i])
    }

    fmt.Fprintln(output, calc(1, a))
}

// returns stop movie
func calc(pow int64, a []int64) int64 {
    if len(a) == 0 { return 0 }

    sum := int64(0)
    for i := 0; i < len(a); i++ {
        sum += a[i] * pow
    }

    anew := make([]int64, 0)
    for i := 0; i < len(a); i++ {
        if a[i] % x != 0 {
            for _, v := range anew {
                sum += v * pow * x
            }
            return sum
        }
        anew = append(anew, a[i] / x)
    }

    return sum + calc(pow * x, anew)
}

package main

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

/*

 */

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		var n, x int64
		scanf("%d %d\n", &n, &x)

		type pair struct {
			num int64
			cnt int64
		}

		a := make([]pair, n)

		for i := range a {
			var num int64
			scanf("%d", &num)

			a[i] = pair{num, 1}
		}
		scanf("\n")

		for i := 0; i < len(a); i++ {
			p := a[i]

			if p.num%x == 0 {
				a = append(a, pair{p.num / x, x * p.cnt})
			} else {
				break
			}
		}

		sum := int64(0)
		for _, p := range a {
			sum += p.num * p.cnt
		}

		fmt.Fprintln(writer, sum)
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div != 0 {
		quot++
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	minimum := nums[0]
	for _, num := range nums {
		if num < minimum {
			minimum = num
		}
	}
	return minimum
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	maximum := nums[0]
	for _, num := range nums {
		if num > maximum {
			maximum = num
		}
	}
	return maximum
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

package main

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

var fields []string

func init() {
	f, err := os.Open("in.txt")

	if err == nil {
		defer f.Close()
	} else {
		f = os.Stdin
	}

	bytes, err := ioutil.ReadAll(bufio.NewReader(f))

	if err != nil {
		panic(err)
	}

	fields = strings.Fields(string(bytes))
}

func ScanString() string {
	for {
		field := fields[0]
		fields = fields[1:]

		if field != "" {
			return field
		}
	}
}

func ScanInt() int {
	n, err := strconv.Atoi(ScanString())

	if err != nil {
		panic(err)
	}

	return n
}

func ScanInt64() int64 {
	n, err := strconv.ParseInt(ScanString(), 10, 64)

	if err != nil {
		panic(err)
	}

	return n
}

func main() {
	for t := ScanInt(); t > 0; t-- {
		n, x := ScanInt(), ScanInt64()
		a := make([]int64, n)
		c := make([]int64, n)
		var res int64

		for i := range a {
			a[i] = ScanInt64()
			c[i] = 1
		}

		for i := 0; a[i]%x == 0; i++ {
			a = append(a, a[i]/x)
			c = append(c, c[i]*x)
		}

		for i := range a {
			res += a[i] * c[i]
		}

		fmt.Println(res)
	}
}

package main

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

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

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

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 sbytes() []byte {
	scanner.Scan()
	return scanner.Bytes()
}

// [12] x=2
// => 12, 6, 6, 3 (stop), 3, 3, 3
// => 36

// [4, 6, 8, 2] x=2
// => 4, 6, 8, 2, 2, 2, 3, 3, 4, 4, 1, 1, 1, 1 ...
func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n, x := sint64(), sint64()
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		printf("%d\n", sol(n, x, a))
	}
}

func sol(n, x int64, a []int64) int64 {
	deg := make([]int64, n)
	for i := range a {
		tmp := a[i]
		d := int64(0)
		for tmp > 0 {
			if tmp%x != 0 {
				break
			}
			tmp /= x
			d++
		}
		deg[i] = d
	}
	minDeg := min(deg)
	ret := sum(a) * (minDeg + 1)
	for i := range a {
		if deg[i] > minDeg {
			ret += a[i]
		} else {
			break
		}
	}
	return ret
}

func min(deg []int64) int64 {
	ret := deg[0]
	for _, d := range deg {
		if d < ret {
			ret = d
		}
	}
	return ret
}

func sum(a []int64) int64 {
	ret := int64(0)
	for _, num := range a {
		ret += num
	}
	return ret
}

package main

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

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

	var T, n, x int
	for Fscan(in, &T); T > 0; T-- {
		s := int64(0)
		Fscan(in, &n, &x)
		a := make([]int, n)
		c := make([]int8, n)
		mi := 0
		for i := range a {
			Fscan(in, &a[i])
			v := a[i]
			for s += int64(v); v%x == 0; v /= x {
				c[i]++
			}
			if c[i] < c[mi] {
				mi = i
			}
		}
		s *= int64(c[mi] + 1)
		for _, v := range a[:mi] {
			s += int64(v)
		}
		Fprintln(out, s)
	}
}

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

package main

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

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

	var t, n int
	var x int64
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &x)
		a := make([]int64, n)

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

		multiplier := int64(1)
		for i := 0; i < n; i++ {
			if a[i]%x != 0 {
				break
			}
			sum += a[i] * multiplier
			a[i] = a[i] / x
			if i == n-1 {
				i = -1
				multiplier *= x
			}
		}

		fmt.Println(sum)
	}
}

package main

import (

"bufio"

"fmt"

"io"

"os"

)

func main() {

var input io.Reader = bufio.NewReader(os.Stdin)

var t int

fmt.Fscan(input, &t)

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

var n, x int

fmt.Fscan(input, &n)

fmt.Fscan(input, &x)

ARRAY := make([]int, n)

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

fmt.Fscan(input, &ARRAY[i])}

b := 7

var SUM int64

for m :=0; m< n; m++{

SUM = SUM + int64(ARRAY[m])
}

q := x

for l :=0 ;b > 6;{
l = l +1
M :=0

for i :=0; i<len(ARRAY); i++{

if ARRAY[i] % q == 0{

SUM = SUM + int64(ARRAY[i])

M = M + 1

}

}

if M == n{

q = q * x

}else{

b = 5

break

}

}

fmt.Println(SUM)

}
}

package main

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

type Age struct {
	key int
	cnt int
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

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

		var ans int64
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
			ans += int64(arr[i])
		}

		y := make([]Age, 0)
		ok := true
		ff := true
		for i := 0; i < n; i++ {
			if arr[i]%x == 0 {
				yy := Age{}
				yy.key = arr[i] / x
				yy.cnt = x

				if yy.key%x != 0 {
					ff = false
				}

				ans += int64(yy.key * yy.cnt)

				if ff {
					y = append(y, yy)
				}
			} else {
				ok = false
				break
			}
		}

		if ok {
			for {

				if len(y) == 0 {
					break
				}

				if y[0].key%x == 0 {
					yy := Age{}
					yy.key = y[0].key / x
					yy.cnt = x * y[0].cnt

					ans += int64(yy.key * yy.cnt)

					y = append(y, yy)
					y = y[1:]
				} else {
					break
				}
			}
		}

		fmt.Println(ans)
	}
}

package main

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

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
	t   int
	n   int
	x   int
)

func main() {

	defer out.Flush()

	fmt.Fscan(in, &t)

	for ; t > 0; t-- {

		var a []int
		flag := false
		sum := int64(0)
		fmt.Fscan(in, &n, &x)

		//輸入資料
		for i := 0; i < n; i++ {
			temp := 0
			fmt.Fscan(in, &temp)

			sum += int64(temp)

			if temp%x != 0 {
				flag = true
			}

			if !flag {
				for i := 0; i < x; i++ {
					a = append(a, temp/x)
				}
			}

		}

		var b []int
		var t []int

		//擴展資料
		for !flag {

			b = t

			for i := range a {

				sum += int64(a[i])

				if a[i]%x != 0 {
					flag = true
				}

				if !flag {
					for j := 0; j < x; j++ {
						b = append(b, a[i]/x)
					}
				}

			}

			a = b

		}

		//最後面
		for i := range b {
			sum += int64(b[i])
		}

		fmt.Fprintln(out, sum)

	}

}

//印出數字陣列
func printArr(array []int) {

	for i := range array {
		fmt.Printf("%d ", array[i])
	}

	fmt.Println()

}

func solve2(n int, x int, a []int) int64 {

	sum := int64(0)
	index := 0

	for a[index]%x == 0 {

		for i := 0; i < x; i++ {
			a = append(a, a[index]/x)
		}

		index++

	}

	//printArr(a)

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

	return sum
}

func solve(n int, x int, a []int) int64 {

	sum := int64(0)

	for i := range a {

		num := int64(a[i])
		sum += int64(a[i])

		fmt.Println(sum)

		for a[i]%x == 0 {
			sum += num
			a[i] /= x
			fmt.Println(sum)
		}

	}

	return sum
}

// https://codeforces.com/contest/1472/problem/E

package main

import (
	"bufio"
	"fmt"
	"os"
)
var (
    input = bufio.NewReader(os.Stdin)
    output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assertion!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
func main() {
    var t int
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
    output.Flush()
}
var (
    x int64 // min of stop
    m map[int]int
)

func solve() {
    var n int64
    fmt.Fscan(input, &n, &x)
    a := make([]int64, n)
    for i := 0; i < len(a); i++ {
        fmt.Fscan(input, &a[i])
    }

    fmt.Fprintln(output, calc(1, a))
}

// returns stop movie
func calc(pow int64, a []int64) int64 {
    if len(a) == 0 { return 0 }

    sum := int64(0)
    for i := 0; i < len(a); i++ {
        sum += a[i] * pow
    }

    anew := make([]int64, 0)
    for i := 0; i < len(a); i++ {
        if a[i] % x != 0 { break }
        anew = append(anew, a[i] / x)
    }

    return sum + calc(pow * x, anew)
}


In an attempt to escape the Mischievous Mess Makers' antics, Farmer John has abandoned his farm and is traveling to the other side of Bovinia. During the journey, he and his k cows have decided to stay at the luxurious Grand Moo-dapest Hotel. The hotel consists of n rooms located in a row, some of which are occupied.Farmer John wants to book a set of k + 1 currently unoccupied rooms for him and his cows. He wants his cows to stay as safe as possible, so he wishes to minimize the maximum distance from his room to the room of his cow. The distance between rooms i and j is defined as |j - i|. Help Farmer John protect his cows by calculating this minimum possible distance.

The first line of the input contains two integers n and k (1 ≤ k < n ≤ 100 000) — the number of rooms in the hotel and the number of cows travelling with Farmer John. The second line contains a string of length n describing the rooms. The i-th character of the string will be '0' if the i-th room is free, and '1' if the i-th room is occupied. It is guaranteed that at least k + 1 characters of this string are '0', so there exists at least one possible choice of k + 1 rooms for Farmer John and his cows to stay in.

Print the minimum possible distance between Farmer John's room and his farthest cow.

NoteIn the first sample, Farmer John can book room 3 for himself, and rooms 1 and 4 for his cows. The distance to the farthest cow is 2. Note that it is impossible to make this distance 1, as there is no block of three consecutive unoccupied rooms.In the second sample, Farmer John can book room 1 for himself and room 3 for his single cow. The distance between him and his cow is 2.In the third sample, Farmer John books all three available rooms, taking the middle room for himself so that both cows are next to him. His distance from the farthest cow is 1.

package main

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

func main() {
	defer out.Flush()
	n := in.NextInt()
	k := in.NextInt()
	rooms := in.NextLine()
	sum := make([]int, n)
	for i := 0; i < n; i++ {
		if i != 0 {
			sum[i] += sum[i-1]
		}
		if rooms[i] == '0' {
			sum[i]++
		}
	}
	ans := binarySearch(-1, n, func(d int) bool {
		for i := 0; i < n; i++ {
			if rooms[i] == '0' && getSum(sum, i-d, i-1)+getSum(sum, i+1, i+d) >= k {
				return true
			}
		}
		return false
	})
	fmt.Fprintln(out, ans)
}

func getSum(sum []int, a, b int) int {
	if a < 0 {
		return getSum(sum, 0, b)
	}
	if b >= len(sum) {
		return getSum(sum, a, len(sum)-1)
	}
	if a > b {
		return 0
	}
	if a == 0 {
		return sum[b]
	}
	return sum[b] - sum[a-1]
}

func binarySearch(a, b int, p func(int) bool) int {
	for a != b-1 {
		m := a + (b-a)/2
		if p(m) {
			b = m
		} else {
			a = m
		}
	}
	return b
}

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

type FastReader struct {
	r   *bufio.Reader
	buf string
}

func NewFastReader(r io.Reader) *FastReader {
	return &FastReader{
		r: bufio.NewReader(r),
	}
}

func (r *FastReader) advance() {
	for len(r.buf) != 0 && r.buf[0] == ' ' {
		r.buf = r.buf[1:]
	}
	for len(r.buf) == 0 {
		var buf bytes.Buffer
		for {
			chunk, more, _ := r.r.ReadLine()
			buf.Write(chunk)
			if !more {
				break
			}
		}
		r.buf = buf.String()
	}
}

func (r *FastReader) NextLine() string {
	r.advance()
	line := r.buf
	r.buf = ""
	return line
}

func (r *FastReader) NextWord() string {
	r.advance()
	var word string
	wordStart := 0
	for i := 0; i < len(r.buf); i++ {
		b := r.buf[i]
		if b == ' ' {
			if i != wordStart {
				word = r.buf[wordStart:i]
				r.buf = r.buf[i:]
				break
			}
			wordStart = i + 1
		}
	}
	if len(word) == 0 {
		word = r.buf
		r.buf = ""
	}
	return word
}

func (r *FastReader) NextInt() int {
	return parseInt(r.NextWord())
}

func parseInt(word string) int {
	sign := 1
	if word[0] == '-' {
		sign = -1
		word = word[1:]
	}
	result := 0
	for i := 0; i < len(word); i++ {
		result = result*10 + int(word[i]) - '0'
	}
	result *= sign
	return result
}