package main

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

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

var ans int64

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

	var n int
	Fscan(in, &n)

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

	}
	ans = math.MaxInt64
	for i := 0; i < n; i++ {
		Cal(i, n, a)
	}
	Fprintln(out, ans)

}

func Cal(x int, n int, a []int64) int64 {
	res := L0
	cur := L0
	for i := x + 1; i < n; i++ {
		k := (cur + a[i]) / a[i]
		res += k
		cur = a[i] * k
	}

	cur = 0
	for i := x - 1; i >= 0; i-- {
		k := (cur + a[i]) / a[i]
		res += k
		cur = a[i] * k
	}
	if res < ans {
		ans = res
	}
	return res
}

package main

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

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	nums := make([]int64, t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &nums[i])
	}
	var ans int64 = -1
	for i := 0; i < t; i++ {
		temp := int64(0)
		var last int64
		for j := i - 1; j >= 0; j-- {
			temp += last/nums[j] + 1
			last = (last/nums[j] + 1) * nums[j]
		}
		last = 0
		for j := i + 1; j < t; j++ {
			temp += last/nums[j] + 1
			last = (last/nums[j] + 1) * nums[j]
		}
		if temp < ans || ans == -1 {
			ans = temp
		}
	}
	fmt.Fprintln(out, ans)
	//for ii := 0; ii < t; ii++ {
	//	var n int
	//	var m int64
	//	fmt.Fscan(in, &n, &m)
	//	nums := make([]int64, n)
	//	for i := 0; i < n; i++ {
	//		fmt.Fscan(in, &nums[i])
	//	}
	//	sort.Slice(nums, func(i, j int) bool {
	//		return nums[i] < nums[j]
	//	})
	//	used := 1 + nums[n-1]
	//	for i := n - 2; i >= 0; i-- {
	//		if i == 0 {
	//			used += nums[n-1] + 1
	//		} else {
	//			used += nums[i] + 1
	//		}
	//	}
	//	if used <= m {
	//		fmt.Fprintln(out, "YES")
	//	} else {
	//		fmt.Fprintln(out, "NO")
	//	}
	//}
}

package main

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

var (
	a [5005]int64
)

func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewScanner(_r)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	read := func() (x int64) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int64(b&15)
		}
		return
	}
	n := read()
	var i int64
	for i = 1; i <= n; i++ {
		a[i] = read()
	}
	var ans int64 = 1e18
	for i = 1; i <= n; i++ {
		var tmp, pre int64
		for j := i - 1; j >= 1; j-- {
			x := pre/a[j] + 1
			tmp += x
			pre = x * a[j]
		}
		pre = 0
		for j := i + 1; j <= n; j++ {
			x := pre/a[j] + 1
			tmp += x
			pre = x * a[j]
		}
		if ans > tmp {
			ans = tmp
		}
	}
	Fprint(out, ans)
}

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

package main

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

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

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

	var x, n int64
	Fscan(in, &n)

	row := []int64{}
	for i := int64(0); i < n; i++ {
		Fscan(in, &x)
		row = append(row, x)
	}

	ans := int64(1<<63 - 1)
	for z := int64(0); z < n; z++ {
		pre, t := int64(0), int64(0)
		for i := z - 1; i >= 0; i-- {
			t += pre/row[i] + 1
			pre = (pre/row[i] + 1) * row[i]
		}
		pre = 0
		for i := z + 1; i < n; i++ {
			t += pre/row[i] + 1
			pre = (pre/row[i] + 1) * row[i]
		}
		if t < ans {
			ans = t
		}
	}

	Fprintln(out, ans)
}

package main

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

/*
6
2 5 4 2 3 1
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	a := make([]int, n)

	for i := range a {
		scanf("%d", &a[i])
	}
	scanf("\n")

	const maxInt = int64(^uint64(0) >> 1)
	minCnt := maxInt

	for i := 0; i < n; i++ {
		cnt := int64(0)

		prev := int64(0)
		for j := i + 1; j < n; j++ {
			k := prev/int64(a[j]) + 1
			cnt += k
			prev = k * int64(a[j])
		}

		prev = int64(0)
		for j := i - 1; j >= 0; j-- {
			k := prev/int64(a[j]) + 1
			cnt += k
			prev = k * int64(a[j])
		}

		minCnt = min(minCnt, cnt)
	}

	fmt.Fprintln(writer, minCnt)
}

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

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

	for ; t > 0; t-- {
		solve()
	}
}

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 reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

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

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int64) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

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

//goland:noinspection GoUnhandledErrorResult
func factorize(num int) []int {
	factors := []int{1}

	sqrtA := int(math.Sqrt(float64(num)))

	for ftr := 2; ftr <= sqrtA; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > sqrtA {
		factors = append(factors, num)
	}

	return factors
}

package main

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

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

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

	var n int64
	var ans int64 = 1e16
	Fscan(in, &n)
	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}

	for pos := int64(0); pos < n; pos++ { //枚举0所在的位置
		var sum int64
		//分别处理[0, pos) (pos, n)
		pre := int64(0)
		for left := pos - 1; left >= 0; left-- {
			pre--
			k := cal(-pre, a[left])
			sum += k
			pre = -(k * a[left])
		}
		pre = int64(0)
		for right := pos + 1; right < n; right++ {
			pre++
			k := cal(pre, a[right])
			sum += k
			pre = k * a[right]
		}
		if sum < ans {
			ans = sum
		}
	}
	Fprintln(out, ans)
}

func cal(x, y int64) int64 {
	if x%y == 0 {
		return x / y
	}
	return x/y + 1
}

package main

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

func main() {
	//CF1667A(os.Stdin, os.Stdout)
	cf1667A(os.Stdin, os.Stdout)
}

func cf1667A(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n int
	fmt.Fscan(in, &n)
	a := make([]int64, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	ans := int64(1e18)
	for i := range a {
		cur := int64(0)
		for j, pre := i+1, int64(0); j < n; j++ {
			cnt := pre/a[j] + 1
			cur += cnt
			pre = a[j] * cnt
		}
		for j, pre := i-1, int64(0); j >= 0; j-- {
			cnt := pre/a[j] + 1
			cur += cnt
			pre = a[j] * cnt
		}
		if ans > cur {
			ans = cur
		}
	}
	fmt.Fprintln(out, ans)
}

package main

import (
	"fmt"
	"math"
)

func main() {
	//notAdding()
	makeItIncreasing()
}

func makeItIncreasing() {
	n := 0
	fmt.Scan(&n)
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&arr[i])
	}
	var ans int64 = math.MaxInt64
	for i := 0; i < n; i++ {
		var cnt int64 = 0
		var cur int64 = 0
		for j := i + 1; j < n; j++ {
			d := cur/arr[j] + 1
			cur = arr[j] * d
			cnt += d
		}
		cur = 0
		for j := i - 1; j >= 0; j-- {
			d := cur/(-arr[j]) + 1
			cur = -arr[j] * d
			cnt += d
		}
		ans = min(ans, cnt)
	}
	fmt.Println(ans)
}

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

package main

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

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

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

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

	tot := int64(math.MaxInt64)
	for i := 0; i < n; i++ {
		var sum int64
		var last int64
		for j := i - 1; j >= 0; j-- {
			d := (last / arr[j]) + 1
			sum += d
			last = d * arr[j]
		}

		last = 0
		for j := i + 1; j < n; j++ {
			d := (last / arr[j]) + 1
			sum += d
			last = d * arr[j]
		}
		if sum < tot {
			tot = sum
		}
	}
	fmt.Fprintln(out, tot)
}

package main

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

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

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

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

	score := func(idx int) int64 {
		var tot int64
		var last int64
		for i := idx - 1; i >= 0; i-- {
			d := (last / arr[i]) + 1
			tot += d
			last = d * arr[i]
		}

		last = 0
		for i := idx + 1; i < n; i++ {
			d := (last / arr[i]) + 1
			tot += d
			last = d * arr[i]
		}

		return tot
	}

	tot := score(0)
	for i := 1; i < n; i++ {
		d := score(i)
		if d < tot {
			tot = d
		}
	}
	fmt.Fprintln(out, tot)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1667A(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n int
	Fscan(in, &n)
	if n == 2 {
		Fprint(out, 1)
		return
	}
	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	ans := int64(1e18)
	for i := 1; i < n-1; i++ {
		c := int64(2)
		for j, pre := i-2, a[i-1]; j >= 0; j-- {
			v := pre/a[j] + 1
			c += v
			pre = a[j] * v
		}
		for j, pre := i+2, a[i+1]; j < n; j++ {
			v := pre/a[j] + 1
			c += v
			pre = a[j] * v
		}
		if c < ans {
			ans = c
		}
	}
	Fprintln(out, ans)
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var n int
	fmt.Fscan(in, &n)
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}
	res := int64(1e18)
	for i := 0; i < n; i++ {
		cur := int64(0)
		pr := int64(0)

		for j := i + 1; j < n; j++ {
			pr = (pr/a[j] + 1) * a[j]
			cur += pr / int64(a[j])
		}
		pr = 0
		for j := i - 1; j >= 0; j-- {
			pr = (pr/a[j] + 1) * a[j]
			cur += pr / int64(a[j])
		}
		if cur < res {
			res = cur
		}
	}
	fmt.Fprintln(out, res)
}

package main

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

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

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

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

	tot := int64(math.MaxInt64)
	for i := 0; i < n; i++ {
		var sum int64
		var last int64
		for j := i - 1; j >= 0; j-- {
			d := (last / arr[j]) + 1
			sum += d
			last = d * arr[j]
		}

		last = 0
		for j := i + 1; j < n; j++ {
			d := (last / arr[j]) + 1
			tot += d
			last = d * arr[j]
		}
		if sum < tot {
			tot = sum
		}
	}
	fmt.Fprintln(out, tot)
}

package main

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

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

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

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

	score := func(idx int) int64 {
		var tot int64
		var last int64
		for i := idx - 1; i >= 0; i-- {
			d := (last / arr[i]) + 1
			tot += d
			last = d * arr[i]
		}

		last = 0
		for i := idx + 1; i < n; i++ {
			d := (last / arr[i]) + 1
			tot += d
			last = d * arr[i]
		}

		return tot
	}

	tot := score(0)
	for i := 1; i < n; i++ {
		d := score(i)
		if d > tot {
			break
		}
		tot = d
	}
	fmt.Fprintln(out, tot)
}

package main

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

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

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

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

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

	max := int64(0)
	m := make([]int64, n)
	for i := 1; i < n-1; i++ {
		m[i] = abs(arr[i-1]-arr[i]) + abs(arr[i+1]-arr[i])
		if m[i] > max {
			max = m[i]
		}
	}

	score := func(idx int) int64 {
		var tot int64
		var last int64
		for i := idx - 1; i >= 0; i-- {
			var v int64
			for v >= last {
				v -= arr[i]
				tot++
			}

			last = v
		}

		last = 0
		for i := idx + 1; i < n; i++ {
			var v int64
			for v <= last {
				v += arr[i]
				tot++
			}
			last = v
		}

		return tot
	}

	tot := score(0)
	for i, v := range m {
		if v != max {
			continue
		}

		d := score(i)
		if d < tot {
			tot = d
		}
	}
	fmt.Fprintln(out, tot)
}

package main

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

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

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

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

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

	max := int64(0)
	m := make([]int64, n)
	for i := 1; i < n-1; i++ {
		m[i] = abs(arr[i-1]-arr[i]) + abs(arr[i+1]-arr[i])
		if m[i] > max {
			max = m[i]
		}
	}

	score := func(idx int) int64 {
		var tot int64
		var last int64
		for i := idx - 1; i >= 0; i-- {
			var v int64
			for v >= last {
				v -= arr[i]
				tot++
			}

			last = v
		}

		last = 0
		for i := idx + 1; i < n; i++ {
			var v int64
			for v <= last {
				v += arr[i]
				tot++
			}
			last = v
		}

		return tot
	}

	tot := int64(math.MaxInt64)
	for i, v := range m {
		if v != max {
			continue
		}

		d := score(i)
		if d < tot {
			tot = d
		}
	}
	fmt.Fprintln(out, tot)
}

package main

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

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

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

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

	max := int64(0)
	min := int64(math.MaxInt64)
	for i := 1; i < n; i++ {
		if arr[i] > max {
			max = arr[i]
		}

		if arr[i] < min {
			min = arr[i]
		}
	}

	score := func(idx int) int64 {
		var tot int64
		var last int64
		for i := idx - 1; i >= 0; i-- {
			var v int64
			for v >= last {
				v -= arr[i]
				tot++
			}

			last = v
		}

		last = 0
		for i := idx + 1; i < n; i++ {
			var v int64
			for v <= last {
				v += arr[i]
				tot++
			}
			last = v
		}

		return tot
	}

	tot := int64(math.MaxInt64)
	for i := 0; i < n; i++ {
		if arr[i] != max && arr[i] != min {
			continue
		}

		d := score(i)
		if d < tot {
			tot = d
		}
	}
	fmt.Fprintln(out, tot)
}

package main

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

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

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

	arr := make([]int64, n)
	max := int64(0)
	min := int64(math.MaxInt32)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &arr[i])
		if arr[i] > max {
			max = arr[i]
		}

		if arr[i] < min {
			min = arr[i]
		}
	}

	score := func(idx int) int64 {
		var tot int64
		var last int64
		for i := idx - 1; i >= 0; i-- {
			var v int64
			for v >= last {
				v -= arr[i]
				tot++
			}

			last = v
		}

		last = 0
		for i := idx + 1; i < n; i++ {
			var v int64
			for v <= last {
				v += arr[i]
				tot++
			}
			last = v
		}

		return tot
	}

	tot := int64(math.MaxInt32)
	for i := 0; i < n; i++ {
		if arr[i] != max && arr[i] != min {
			continue
		}

		d := score(i)
		if d < tot {
			tot = d
		}
	}
	fmt.Fprintln(out, tot)
}

package main

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

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

var ans int64

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

	var n int
	Fscan(in, &n)

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

	}
	ans = math.MaxInt64
	for i := 0; i < n; i++ {
		Cal(i, n, a)
	}
	Fprintln(out, ans)

}

func Cal(x int, n int, a []int) int64 {
	res := L0
	cur := 0
	for i := x + 1; i < n; i++ {
		k := (cur + a[i]) / a[i]
		res += int64(k)
		cur = a[i] * k
	}

	cur = 0
	for i := x - 1; i >= 0; i-- {
		k := (cur + a[i]) / a[i]
		res += int64(k)
		cur = a[i] * k
	}
	if res < ans {
		ans = res
	}
	return res
}

package main

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

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

var ans int

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

	var n int
	Fscan(in, &n)

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

	}
	ans = math.MaxInt
	for i := 0; i < n; i++ {
		Cal(i, n, a)
	}
	Fprintln(out, ans)

}

func Cal(x int, n int, a []int) int {
	res := 0
	cur := 0
	for i := x + 1; i < n; i++ {
		k := (cur + a[i]) / a[i]
		res += k
		cur = a[i] * k
	}

	cur = 0
	for i := x - 1; i >= 0; i-- {
		k := (cur + a[i]) / a[i]
		res += k
		cur = a[i] * k
	}
	if res < ans {
		ans = res
	}
	return res
}

package main

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

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	nums := make([]int64, t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &nums[i])
	}
	ans := int64(t * t)
	for i := 0; i < t; i++ {
		temp := int64(0)
		var last int64
		for j := i - 1; j >= 0; j-- {
			temp += last/nums[j] + 1
			last = (last/nums[j] + 1) * nums[j]
		}
		last = 0
		for j := i + 1; j < t; j++ {
			temp += last/nums[j] + 1
			last = (last/nums[j] + 1) * nums[j]
		}
		if temp < ans {
			ans = temp
		}
	}
	fmt.Fprintln(out, ans)
	//for ii := 0; ii < t; ii++ {
	//	var n int
	//	var m int64
	//	fmt.Fscan(in, &n, &m)
	//	nums := make([]int64, n)
	//	for i := 0; i < n; i++ {
	//		fmt.Fscan(in, &nums[i])
	//	}
	//	sort.Slice(nums, func(i, j int) bool {
	//		return nums[i] < nums[j]
	//	})
	//	used := 1 + nums[n-1]
	//	for i := n - 2; i >= 0; i-- {
	//		if i == 0 {
	//			used += nums[n-1] + 1
	//		} else {
	//			used += nums[i] + 1
	//		}
	//	}
	//	if used <= m {
	//		fmt.Fprintln(out, "YES")
	//	} else {
	//		fmt.Fprintln(out, "NO")
	//	}
	//}
}

//4444444444
//4444444444
//4444444444
//4444444444
//4444444444

package main

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

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	nums := make([]int64, t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &nums[i])
	}
	ans := t * (t - 1) / 2
	for i := 0; i < t; i++ {
		temp := 0
		var last int64
		for j := i - 1; j >= 0; j-- {
			temp += int(last/nums[j]) + 1
			last = (last/nums[j] + 1) * nums[j]
		}
		last = 0
		for j := i + 1; j < t; j++ {
			temp += int(last/nums[j]) + 1
			last = (last/nums[j] + 1) * nums[j]
		}
		if temp < ans {
			ans = temp
		}
	}
	fmt.Fprintln(out, ans)
	//for ii := 0; ii < t; ii++ {
	//	var n int
	//	var m int64
	//	fmt.Fscan(in, &n, &m)
	//	nums := make([]int64, n)
	//	for i := 0; i < n; i++ {
	//		fmt.Fscan(in, &nums[i])
	//	}
	//	sort.Slice(nums, func(i, j int) bool {
	//		return nums[i] < nums[j]
	//	})
	//	used := 1 + nums[n-1]
	//	for i := n - 2; i >= 0; i-- {
	//		if i == 0 {
	//			used += nums[n-1] + 1
	//		} else {
	//			used += nums[i] + 1
	//		}
	//	}
	//	if used <= m {
	//		fmt.Fprintln(out, "YES")
	//	} else {
	//		fmt.Fprintln(out, "NO")
	//	}
	//}
}

//4444444444
//4444444444
//4444444444
//4444444444
//4444444444

package main

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

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

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

	n := int64(0)
	Fscan(in, &n)

	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	ans := int64(1e15)
	for k := int64(0); k <= n; k++ {
		b := make([]int64, n)
		b[0] = -a[0] * k
		tmp := k
		for i := int64(1); i < n; i++ {
			if b[i-1]+1 < 0 {
				m := (b[i-1] + 1) / (-a[i])
				tmp += m
				b[i] = -(m * a[i])
			} else {
				m := cal(b[i-1]+1, a[i])
				tmp += m
				b[i] = m * a[i]
			}
		}
		if tmp < ans {
			ans = tmp
		}
	}
	Fprintln(out, ans)
}

func cal(x, y int64) int64 {
	if x%y == 0 {
		return x / y
	}
	return x/y + 1
}

package main

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

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

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

	n := int64(0)
	Fscan(in, &n)

	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	cnt := n + 10
	ans := int64(1e13)
	for k := int64(0); k <= cnt; k++ {
		b := make([]int64, n)
		b[0] = -a[0] * k
		tmp := k
		for i := int64(1); i < n; i++ {
			if b[i-1]+1 < 0 {
				m := -(b[i-1] + 1) / a[i]
				tmp += m
				b[i] = -(m * a[i])
			} else {
				m := cal(b[i-1]+1, a[i])
				tmp += m
				b[i] = m * a[i]
			}
		}
		if tmp < ans {
			ans = tmp
		}
	}
	Fprintln(out, ans)
}

func cal(x, y int64) int64 {
	if x%y == 0 {
		return x / y
	}
	return x/y + 1
}

package main

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

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

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

	n := int64(0)
	Fscan(in, &n)

	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	cnt := n + 1
	ans := int64(1e13)
	for k := int64(0); k <= cnt; k++ {
		b := make([]int64, n)
		b[0] = -a[0] * k
		tmp := k
		for i := int64(1); i < n; i++ {
			if b[i-1]+1 < 0 {
				m := -(b[i-1] + 1) / a[i]
				tmp += m
				b[i] = -(m * a[i])
			} else {
				m := cal(b[i-1]+1, a[i])
				tmp += m
				b[i] = m * a[i]
			}
		}
		if tmp < ans {
			if tmp == 12989782 {
				Println(b[0], b[1], b[2])
			}
			ans = tmp
		}
	}
	Fprintln(out, ans)
}

func cal(x, y int64) int64 {
	if x%y == 0 {
		return x / y
	}
	return x/y + 1
}

package main

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

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

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

	n := int64(0)
	Fscan(in, &n)

	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	cnt := n + 1
	ans := int64(1e13)
	for k := int64(0); k <= cnt; k++ {
		b := make([]int64, n)
		b[0] = -a[0] * k
		tmp := k
		for i := int64(1); i < n; i++ {
			if b[i-1]+1 < 0 {
				m := -(b[i-1]+1) / a[i]
				tmp += m
				b[i] = -(m * a[i])
			} else {
				m := cal(b[i-1]+1, a[i])
				tmp += m
				b[i] = m * a[i]
			}
		}
		if tmp < ans {
			ans = tmp
		}
	}
	Fprintln(out, ans)
}

func cal(x, y int64) int64 {
	if x%y == 0 {
		return x / y
	}
	return x/y + 1
}

package main

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

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

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

	n := int64(0)
	Fscan(in, &n)

	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	cnt := n + 10
	ans := int64(1e18)
	for k := int64(0); k <= cnt; k++ {
		b := make([]int64, n)
		b[0] = -a[0] * k
		tmp := k
		for i := int64(1); i < n; i++ {
			if b[i-1]+1 < 0 {
				m := ((-b[i-1]) - 1) / a[i]
				tmp += m
				b[i] = -(m * a[i])
			} else {
				m := cal(b[i-1]+1, a[i])
				tmp += m
				b[i] = m * a[i]
			}
		}
		if tmp < ans {
			ans = tmp
		}
	}
	Fprintln(out, ans)
}

func cal(x, y int64) int64 {
	if x%y == 0 {
		return x / y
	}
	return x/y + 1
}

package main

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

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

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

	n := int64(0)
	Fscan(in, &n)

	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	cnt := n + 10
	ans := cnt
	for k := int64(0); k <= cnt; k++ {
		b := make([]int64, n)
		b[0] = -a[0] * k
		tmp := k
		for i := int64(1); i < n; i++ {
			if b[i-1]+1 < 0 {
				m := ((-b[i-1]) - 1) / a[i]
				tmp += m
				b[i] = -(m * a[i])
			} else {
				m := cal(b[i-1]+1, a[i])
				tmp += m
				b[i] = m * a[i]
			}
		}
		if tmp < ans {
			ans = tmp
		}
	}
	Fprintln(out, ans)
}

func cal(x, y int64) int64 {
	if x%y == 0 {
		return x / y
	}
	return x/y + 1
}

package main

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

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

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

	n := 0
	Fscan(in, &n)

	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	cnt := n + 10
	ans := cnt
	for k := 0; k <= cnt; k++ {
		b := make([]int, n)
		b[0] = -a[0] * k
		tmp := k
		for i := 1; i < n; i++ {
			if b[i-1]+1 < 0 {
				m := ((-b[i-1]) - 1) / a[i]
				tmp += m
				b[i] = -(m * a[i])
			} else {
				m := cal(b[i-1]+1, a[i])
				tmp += m
				b[i] = m * a[i]
			}
		}
		if tmp < ans {
			ans = tmp
		}
	}
	Fprintln(out, ans)
}

func cal(x, y int) int {
	if x % y == 0 {
		return x / y
	}
	return x/y + 1
}

package main

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

func main() {
	//CF1667A(os.Stdin, os.Stdout)
	cf1667A(os.Stdin, os.Stdout)
}

func cf1667A(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n int
	fmt.Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	ans := int64(1e18)
	for i := range a {
		cur := int64(0)
		for j, pre := i+1, 0; j < n; j++ {
			cnt := pre/a[j] + 1
			cur += int64(cnt)
			pre = a[j] * cnt
		}
		for j, pre := i-1, 0; j >= 0; j-- {
			cnt := pre/a[j] + 1
			cur += int64(cnt)
			pre = a[j] * cnt
		}
		if ans > cur {
			ans = cur
		}
	}
	fmt.Fprintln(out, ans)
}

package main

import (
	"fmt"
	"math"
)

func main() {
	//notAdding()
	makeItIncreasing()
}

func makeItIncreasing() {
	n := 0
	fmt.Scan(&n)
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&arr[i])
	}
	ans := math.MaxInt
	for i := 0; i < n; i++ {
		cnt := 0
		cur := 0
		for j := i + 1; j < n; j++ {
			d := cur/arr[j] + 1
			cur = arr[j] * d
			cnt += d
		}
		cur = 0
		for j := i - 1; j >= 0; j-- {
			d := cur/(-arr[j]) + 1
			cur = -arr[j] * d
			cnt += d
		}
		ans = min(ans, cnt)
	}
	fmt.Println(ans)
}

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


package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

type problem struct {
	a, t, index int
}

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

	n := readInt()
	T := readInt()
	pr := make([]problem, n)
	for i := range pr {
		pr[i].a = readInt()
		pr[i].t = readInt()
		pr[i].index = i + 1
	}
	sort.Slice(pr, func(i, j int) bool { return pr[i].t < pr[j].t })
	if pr[0].t > T {
		fmt.Println(0)
		fmt.Println(0)
		return
	}
	ans := sort.Search(n+1, func(solved int) bool {
		if solved == 0 {
			return false
		}
		cur := 0
		timeSpent := 0
		for i := range pr {
			if timeSpent+pr[i].t > T {
				break
			}
			if solved <= pr[i].a {
				cur++
				timeSpent += pr[i].t
			}
		}
		return cur < solved
	}) - 1
	fmt.Fprintln(out, ans)
	fmt.Fprintln(out, ans)
	printed := 0
	for i := range pr {
		if ans <= pr[i].a {
			printed++
			if printed == ans {
				fmt.Fprintln(out, pr[i].index)
				break
			} else {
				fmt.Fprint(out, pr[i].index, " ")
			}
		}
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol913D(reader io.Reader, writer io.Writer) {
	type pair struct {
		a, t, idx int
	}

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, t int
	Fscan(in, &n, &t)
	arr := make([]pair, n)
	for i := range arr {
		Fscan(in, &arr[i].a, &arr[i].t)
		arr[i].idx = i + 1
	}

	sort.Slice(arr, func(i, j int) bool { return arr[i].t < arr[j].t })
	f := func(target int) bool {
		if target == 0 {
			return !true
		}
		score, spent := 0, 0
		for _, p := range arr {
			if p.a >= target {
				if spent+p.t > t {
					break
				}
				spent += p.t
				score++
				if score == target {
					break
				}
			}
		}
		return !(score == target)
	}
	ans := sort.Search(n+1, f) - 1
	Fprintln(out, ans)
	Fprintln(out, ans)
	if ans == 0 {
		Fprintln(out)
		return
	}
	score := 0
	for _, p := range arr {
		if p.a >= ans {
			Fprint(out, p.idx, " ")
			score++
			if score == ans {
				break
			}
		}
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol913D(reader io.Reader, writer io.Writer) {
	type pair struct {
		a, t, idx int
	}

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, t int
	Fscan(in, &n, &t)
	arr := make([]pair, n)
	for i := range arr {
		Fscan(in, &arr[i].a, &arr[i].t)
		arr[i].idx = i + 1
	}

	sort.Slice(arr, func(i, j int) bool { return arr[i].t < arr[j].t })
	var idxAns []interface{}
	f := func(target int) bool {
		if target == 0 {
			return !true
		}
		idxAns = []interface{}{}
		spent := 0
		for _, p := range arr {
			if p.a >= target {
				if spent+p.t > t {
					break
				}
				spent += p.t
				idxAns = append(idxAns, p.idx)
				if len(idxAns) == target {
					break
				}
			}
		}
		return !(len(idxAns) == target)
	}
	ans := sort.Search(n+1, f) - 1
	f(ans)
	Fprintln(out, ans)
	Fprintln(out, ans)
	Fprintln(out, idxAns...)
}

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

package main

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

type data struct {
	point int
	time  int
	idx   int
}
type datas []data

func (d datas) Len() int           { return len(d) }
func (d datas) Less(i, j int) bool { return d[i].time < d[j].time }
func (d datas) Swap(i, j int)      { d[i], d[j] = d[j], d[i] }

func main() {
	var n, T int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	var a datas
	fmt.Sscanf(scanner.Text(), "%d %d\n", &n, &T)
	a = make(datas, n)
	for i := 0; i < n; i++ {
		a[i].idx = i
		scanner.Scan()
		fmt.Sscanf(scanner.Text(), "%d %d\n", &a[i].point, &a[i].time)
	}

	sort.Sort(a)

	lo, hi := 1, n
	for lo <= hi {
		mi, cnt, tTotal := (lo+hi)/2, 0, 0
		for i := 0; i < n; i++ {
			if a[i].point >= mi && tTotal+a[i].time <= T {
				tTotal += a[i].time
				cnt++
			}
		}
		if cnt < mi {
			hi = mi - 1
		} else {
			lo = mi + 1
		}
	}

	fmt.Println(hi)
	cnt, tTotal := 0, 0
	fmt.Println(hi)
	for i := 0; i < n && cnt < hi; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			fmt.Printf("%d ", a[i].idx+1)
			cnt++
		}
	}
	fmt.Println()
}

package main

import (
	"fmt"
	"sort"
)

type data struct {
	point int
	time  int
	idx   int
}

type datas []data

func (d datas) Len() int           { return len(d) }
func (d datas) Less(i, j int) bool { return d[i].time < d[j].time }
func (d datas) Swap(i, j int)      { d[i], d[j] = d[j], d[i] }

func main() {
	var n, T int
	fmt.Scanf("%d %d", &n, &T)
	a := make(datas, n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d %d", &a[i].point, &a[i].time)
		a[i].idx = i
	}
	sort.Sort(a)

	lo, hi := 1, n
	for lo < hi {
		mi, cnt, tTotal := (lo+hi)/2, 0, 0
		for i := 0; i < n; i++ {
			if a[i].point >= mi && tTotal+a[i].time <= T {
				tTotal += a[i].time
				cnt++
			}
		}
		if cnt < mi {
			hi = mi - 1
		} else {
			lo = mi + 1
		}
	}
	fmt.Println(hi)
	ans, tTotal := 0, 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			ans++
		}
	}
	fmt.Println(ans)
	tTotal = 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			fmt.Printf("%d ", a[i].idx+1)
		}
	}
}

package main

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

type data struct {
	point int
	time  int
	idx   int
}
type datas []data

func (d datas) Len() int           { return len(d) }
func (d datas) Less(i, j int) bool { return d[i].time < d[j].time }
func (d datas) Swap(i, j int)      { d[i], d[j] = d[j], d[i] }

func main() {
	var n, T int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	var a datas
	fmt.Sscanf(scanner.Text(), "%d %d\n", &n, &T)
	a = make(datas, n)
	for i := 0; i < n; i++ {
		a[i].idx = i
		scanner.Scan()
		fmt.Sscanf(scanner.Text(), "%d %d\n", &a[i].point, &a[i].time)
	}

	sort.Sort(a)

	lo, hi := 1, n
	for lo <= hi {
		mi, cnt, tTotal := (lo+hi)/2, 0, 0
		for i := 0; i < n; i++ {
			if a[i].point >= mi && tTotal+a[i].time <= T {
				tTotal += a[i].time
				cnt++
			}
		}
		if cnt < mi {
			hi = mi - 1
		} else {
			lo = mi + 1
		}
	}

	fmt.Println(hi)
	ans, tTotal := 0, 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			ans++
		}
	}
	fmt.Println(ans)
	tTotal = 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			fmt.Printf("%d ", a[i].idx+1)
		}
	}
	fmt.Println()
}

package main

import (
	"fmt"
	"math/rand"
)

type data struct {
	point int
	time  int
	idx   int
}

func qsort(a []data) []data {
	if len(a) < 2 {
		return a
	}

	left, right := 0, len(a)-1

	// Pick a pivot
	pivotIndex := rand.Int() % len(a)

	// Move the pivot to the right
	a[pivotIndex], a[right] = a[right], a[pivotIndex]

	// Pile elements smaller than the pivot on the left
	for i := range a {
		if a[i].time < a[right].time {
			a[i], a[left] = a[left], a[i]
			left++
		}
	}

	// Place the pivot after the last smaller element
	a[left], a[right] = a[right], a[left]

	// Go down the rabbit hole
	qsort(a[:left])
	qsort(a[left+1:])

	return a
}

func main() {
	var n, T int
	fmt.Scanf("%d %d\n", &n, &T)
	var a []data
	for i := 0; i < n; i++ {
		inp := data{}
		inp.idx = i
		fmt.Scanf("%d %d\n", &inp.point, &inp.time)
		a = append(a, inp)
		if i > 10000 {
			return
		}
	}
	qsort(a)

	lo, hi := 1, n
	for lo <= hi {
		mi, cnt, tTotal := (lo+hi)/2, 0, 0
		for i := 0; i < n; i++ {
			if a[i].point >= mi && tTotal+a[i].time <= T {
				tTotal += a[i].time
				cnt++
			}
		}
		if cnt < mi {
			hi = mi - 1
		} else {
			lo = mi + 1
		}
	}

	fmt.Println(hi)
	ans, tTotal := 0, 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			ans++
		}
	}
	fmt.Println(ans)
	tTotal = 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			fmt.Printf("%d ", a[i].idx+1)
		}
	}
	fmt.Println()
}

package main

import (
	"fmt"
	"math/rand"
)

type data struct {
	point int
	time  int
	idx   int
}

func qsort(a []data) []data {
	if len(a) < 2 {
		return a
	}

	left, right := 0, len(a)-1

	// Pick a pivot
	pivotIndex := rand.Int() % len(a)

	// Move the pivot to the right
	a[pivotIndex], a[right] = a[right], a[pivotIndex]

	// Pile elements smaller than the pivot on the left
	for i := range a {
		if a[i].time < a[right].time {
			a[i], a[left] = a[left], a[i]
			left++
		}
	}

	// Place the pivot after the last smaller element
	a[left], a[right] = a[right], a[left]

	// Go down the rabbit hole
	qsort(a[:left])
	qsort(a[left+1:])

	return a
}

func main() {
	var n, T int
	fmt.Scanln(&n, &T)
	var a []data
	a = make([]data, n)
	for i := 0; i < n; i++ {
		a[i].idx = i
		fmt.Scanln(&a[i].point, &a[i].time)
		if i > 10000 {
			return
		}
	}
	qsort(a)

	lo, hi := 1, n
	for lo <= hi {
		mi, cnt, tTotal := (lo+hi)/2, 0, 0
		for i := 0; i < n; i++ {
			if a[i].point >= mi && tTotal+a[i].time <= T {
				tTotal += a[i].time
				cnt++
			}
		}
		if cnt < mi {
			hi = mi - 1
		} else {
			lo = mi + 1
		}
	}

	fmt.Println(hi)
	ans, tTotal := 0, 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			ans++
		}
	}
	fmt.Println(ans)
	tTotal = 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			fmt.Printf("%d ", a[i].idx+1)
		}
	}
	fmt.Println()
}

package main

import (
	"fmt"
	"math/rand"
)

type data struct {
	point int
	time  int
	idx   int
}

func qsort(a []data) []data {
	if len(a) < 2 {
		return a
	}

	left, right := 0, len(a)-1

	// Pick a pivot
	pivotIndex := rand.Int() % len(a)

	// Move the pivot to the right
	a[pivotIndex], a[right] = a[right], a[pivotIndex]

	// Pile elements smaller than the pivot on the left
	for i := range a {
		if a[i].time < a[right].time {
			a[i], a[left] = a[left], a[i]
			left++
		}
	}

	// Place the pivot after the last smaller element
	a[left], a[right] = a[right], a[left]

	// Go down the rabbit hole
	qsort(a[:left])
	qsort(a[left+1:])

	return a
}

func main() {
	var n, T int
	fmt.Scanln(&n, &T)
	var a []data
	for i := 0; i < n; i++ {
		inp := data{}
		inp.idx = i
		fmt.Scanln(&inp.point, &inp.time)
		a = append(a, inp)
		if i > 10000 {
			return
		}
	}
	qsort(a)

	lo, hi := 1, n
	for lo <= hi {
		mi, cnt, tTotal := (lo+hi)/2, 0, 0
		for i := 0; i < n; i++ {
			if a[i].point >= mi && tTotal+a[i].time <= T {
				tTotal += a[i].time
				cnt++
			}
		}
		if cnt < mi {
			hi = mi - 1
		} else {
			lo = mi + 1
		}
	}

	fmt.Println(hi)
	ans, tTotal := 0, 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			ans++
		}
	}
	fmt.Println(ans)
	tTotal = 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			fmt.Printf("%d ", a[i].idx+1)
		}
	}
	fmt.Println()
}

package main

import (
	"fmt"
	"math/rand"
)

type data struct {
	point int
	time  int
	idx   int
}

func qsort(a []data) []data {
	if len(a) < 2 {
		return a
	}

	left, right := 0, len(a)-1

	// Pick a pivot
	pivotIndex := rand.Int() % len(a)

	// Move the pivot to the right
	a[pivotIndex], a[right] = a[right], a[pivotIndex]

	// Pile elements smaller than the pivot on the left
	for i := range a {
		if a[i].time < a[right].time {
			a[i], a[left] = a[left], a[i]
			left++
		}
	}

	// Place the pivot after the last smaller element
	a[left], a[right] = a[right], a[left]

	// Go down the rabbit hole
	qsort(a[:left])
	qsort(a[left+1:])

	return a
}

func main() {
	var n, T int
	fmt.Scanf("%d %d\n", &n, &T)
	var a []data
	inp := data{}
	for i := 0; i < n; i++ {
		inp.idx = i
		fmt.Scanf("%d %d\n", &inp.point, &inp.time)
		a = append(a, inp)
		if i > 10000 {
			return
		}
	}
	qsort(a)

	lo, hi := 1, n
	for lo <= hi {
		mi, cnt, tTotal := (lo+hi)/2, 0, 0
		for i := 0; i < n; i++ {
			if a[i].point >= mi && tTotal+a[i].time <= T {
				tTotal += a[i].time
				cnt++
			}
		}
		if cnt < mi {
			hi = mi - 1
		} else {
			lo = mi + 1
		}
	}

	fmt.Println(hi)
	ans, tTotal := 0, 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			ans++
		}
	}
	fmt.Println(ans)
	tTotal = 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			fmt.Printf("%d ", a[i].idx+1)
		}
	}
	fmt.Println()
}

package main

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

type data struct {
	point int
	time  int
	idx   int
}

func qsort(a []data) []data {
	if len(a) < 2 {
		return a
	}

	left, right := 0, len(a)-1

	// Pick a pivot
	pivotIndex := rand.Int() % len(a)

	// Move the pivot to the right
	a[pivotIndex], a[right] = a[right], a[pivotIndex]

	// Pile elements smaller than the pivot on the left
	for i := range a {
		if a[i].time < a[right].time {
			a[i], a[left] = a[left], a[i]
			left++
		}
	}

	// Place the pivot after the last smaller element
	a[left], a[right] = a[right], a[left]

	// Go down the rabbit hole
	qsort(a[:left])
	qsort(a[left+1:])

	return a
}

func main() {
	var n, T int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	fmt.Sscanf(scanner.Text(), "%d %d\n", &n, &T)
	var a []data
	a = make([]data, n)
	for i := 0; i < n; i++ {
		a[i].idx = i
		scanner.Scan()
		fmt.Sscanf(scanner.Text(), "%d %d\n", &a[i].point, &a[i].time)
		if i > 10000 {
			return
		}
	}
	qsort(a)

	lo, hi := 1, n
	for lo <= hi {
		mi, cnt, tTotal := (lo+hi)/2, 0, 0
		for i := 0; i < n; i++ {
			if a[i].point >= mi && tTotal+a[i].time <= T {
				tTotal += a[i].time
				cnt++
			}
		}
		if cnt < mi {
			hi = mi - 1
		} else {
			lo = mi + 1
		}
	}

	fmt.Println(hi)
	ans, tTotal := 0, 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			ans++
		}
	}
	fmt.Println(ans)
	tTotal = 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			fmt.Printf("%d ", a[i].idx+1)
		}
	}
	fmt.Println()
}

package main

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

type data struct {
	point int
	time  int
	idx   int
}
type datas []data

func (d datas) Len() int           { return len(d) }
func (d datas) Less(i, j int) bool { return d[i].time < d[j].time }
func (d datas) Swap(i, j int)      { d[i], d[j] = d[j], d[i] }

func main() {
	var n, T int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	var a datas
	fmt.Sscanf(scanner.Text(), "%d %d\n", &n, &T)
	a = make(datas, n)
	for i := 0; i < n; i++ {
		a[i].idx = i
		scanner.Scan()
		fmt.Sscanf(scanner.Text(), "%d %d\n", &a[i].point, &a[i].time)
	}

	sort.Sort(a)

	if n > 10000 {
		return
	}
	lo, hi := 1, n
	for lo <= hi {
		mi, cnt, tTotal := (lo+hi)/2, 0, 0
		for i := 0; i < n; i++ {
			if a[i].point >= mi && tTotal+a[i].time <= T {
				tTotal += a[i].time
				cnt++
			}
		}
		if cnt < mi {
			hi = mi - 1
		} else {
			lo = mi + 1
		}
	}

	fmt.Println(hi)
	ans, tTotal := 0, 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			ans++
		}
	}
	fmt.Println(ans)
	tTotal = 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			fmt.Printf("%d ", a[i].idx+1)
		}
	}
	fmt.Println()
}

package main

import (
	"fmt"
	"sort"
)

type data struct {
	point int
	time  int
	idx   int
}

type datas []data

func (d datas) Len() int           { return len(d) }
func (d datas) Less(i, j int) bool { return d[i].time < d[j].time }
func (d datas) Swap(i, j int)      { d[i], d[j] = d[j], d[i] }

func main() {
	var n, T int
	fmt.Scanf("%d %d", &n, &T)
	var a datas
	for i := 0; i < n; i++ {
		inp := data{}
		inp.idx = i
		fmt.Scanf("%d %d", &inp.point, &inp.time)
		a = append(a, inp)
	}
	sort.Sort(a)

	lo, hi := 1, n
	for lo < hi {
		mi, cnt, tTotal := (lo+hi)/2, 0, 0
		for i := 0; i < n; i++ {
			if a[i].point >= mi && tTotal+a[i].time <= T {
				tTotal += a[i].time
				cnt++
			}
		}
		if cnt < mi {
			hi = mi - 1
		} else {
			lo = mi + 1
		}
	}

	fmt.Println(hi)
	ans, tTotal := 0, 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			ans++
		}
	}
	fmt.Println(ans)
	tTotal = 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			fmt.Printf("%d ", a[i].idx+1)
		}
	}
	fmt.Println()
}

package main

import (
	"fmt"
	"sort"
)

type data struct {
	point int
	time  int
	idx   int
}

type datas []data

func (d datas) Len() int           { return len(d) }
func (d datas) Less(i, j int) bool { return d[i].time < d[j].time }
func (d datas) Swap(i, j int)      { d[i], d[j] = d[j], d[i] }

func main() {
	var n, T int
	fmt.Scanf("%d %d", &n, &T)
	a := make(datas, n)
	for i := 0; i < n; i++ {
		a[i].idx = i
		fmt.Scanf("%d %d", &a[i].point, &a[i].time)
	}
	sort.Sort(a)

	lo, hi := 1, n
	for lo < hi {
		mi, cnt, tTotal := (lo+hi)/2, 0, 0
		for i := 0; i < n; i++ {
			if a[i].point >= mi && tTotal+a[i].time <= T {
				tTotal += a[i].time
				cnt++
			}
		}
		if cnt < mi {
			hi = mi - 1
		} else {
			lo = mi + 1
		}
	}
	fmt.Println(hi)
	ans, tTotal := 0, 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			ans++
		}
	}
	fmt.Println(ans)
	tTotal = 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			fmt.Printf("%d ", a[i].idx+1)
		}
	}
}

package main

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

type data struct {
	point int
	time  int
	idx   int
}

func qsort(a []data) []data {
	if len(a) < 2 {
		return a
	}

	left, right := 0, len(a)-1

	// Pick a pivot
	pivotIndex := rand.Int() % len(a)

	// Move the pivot to the right
	a[pivotIndex], a[right] = a[right], a[pivotIndex]

	// Pile elements smaller than the pivot on the left
	for i := range a {
		if a[i].time < a[right].time {
			a[i], a[left] = a[left], a[i]
			left++
		}
	}

	// Place the pivot after the last smaller element
	a[left], a[right] = a[right], a[left]

	// Go down the rabbit hole
	qsort(a[:left])
	qsort(a[left+1:])

	return a
}

func main() {
	var n, T int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	fmt.Sscanf(scanner.Text(), "%d %d\n", &n, &T)
	var a []data
	a = make([]data, n)
	for i := 0; i < n; i++ {
		a[i].idx = i
		scanner.Scan()
		fmt.Sscanf(scanner.Text(), "%d %d\n", &a[i].point, &a[i].time)
	}
	if n > 100000 {
		return
	}
	qsort(a)

	lo, hi := 1, n
	for lo <= hi {
		mi, cnt, tTotal := (lo+hi)/2, 0, 0
		for i := 0; i < n; i++ {
			if a[i].point >= mi && tTotal+a[i].time <= T {
				tTotal += a[i].time
				cnt++
			}
		}
		if cnt < mi {
			hi = mi - 1
		} else {
			lo = mi + 1
		}
	}

	fmt.Println(hi)
	ans, tTotal := 0, 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			ans++
		}
	}
	fmt.Println(ans)
	tTotal = 0
	for i := 0; i < n; i++ {
		if a[i].point >= hi && tTotal+a[i].time <= T {
			tTotal += a[i].time
			fmt.Printf("%d ", a[i].idx+1)
		}
	}
	fmt.Println()
}

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol913D(reader io.Reader, writer io.Writer) {
	type pair struct {
		a, t, idx int
	}

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, t int
	Fscan(in, &n, &t)
	arr := make([]pair, n)
	for i := range arr {
		Fscan(in, &arr[i].a, &arr[i].t)
		arr[i].idx = i + 1
	}

	sort.Slice(arr, func(i, j int) bool { return arr[i].t < arr[j].t })
	var idxAns []interface{}
	f := func(target int) bool {
		if target == 0 {
			return !true
		}
		idxAns = []interface{}{}
		spent := 0
		for _, p := range arr {
			if p.a >= target {
				if spent+p.t > t {
					break
				}
				idxAns = append(idxAns, p.idx)
				spent += p.t
			}
		}
		return !(len(idxAns) >= target)
	}
	ans := sort.Search(n+1, f) - 1
	Fprintln(out, ans)
	Fprintln(out, len(idxAns))
	Fprintln(out, idxAns...)
}

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


package main

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

func main() {

	scanner := bufio.NewScanner(os.Stdin)

	var n int
	var q int
	fmt.Scanln(&n, &q)

	var s string
	fmt.Scanln(&s)

	abcs := 0
	for i := 3; i <= n; i++ {
		if s[i-3:i] == "abc" {
			abcs++
		}
	}

	var split []string

	for ; q > 0; q-- {
		scanner.Scan()
		split = strings.Split(scanner.Text(), " ")
		i, _ := strconv.Atoi(split[0])
		i--
		c := split[1][0]
		if i > 1 && s[i-2:i+1] == "abc" {
			abcs--
		}
		if i > 0 && i+1 < n && s[i-1:i+2] == "abc" {
			abcs--
		}
		if i+2 < n && s[i:i+3] == "abc" {
			abcs--
		}

		s = s[:i] + string(c) + s[i+1:]
		if c == 'c' && i > 1 && s[i-2:i] == "ab" {
			abcs++
		}
		if c == 'b' && i > 0 && i+1 < n && s[i-1:i+2] == "abc" {
			abcs++
		}
		if c == 'a' && i+2 < n && s[i:i+3] == "abc" {
			abcs++
		}

		fmt.Println(abcs)
	}

}

package main

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

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

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

	var s string
	fmt.Fscan(in, &s)

	chars := make([]rune, n)
	for i := 0; i < n; i++ {
		chars[i] = rune(s[i])
	}

	var total int
	for i := 0; i < n-2; i++ {
		if chars[i] == 'a' && chars[i+1] == 'b' && chars[i+2] == 'c' {
			total++
			i += 2
		}
	}

	for i := 0; i < q; i++ {
		var pos int
		var c string
		fmt.Fscan(in, &pos, &c)

		pos--
		if chars[pos] == rune(c[0]) {
			fmt.Fprintln(out, total)
			continue
		}

		l, r := 0, n
		if pos-2 > l {
			l = pos - 2
		}

		if pos+3 < r {
			r = pos + 3
		}

		orig := string(chars[l:r])
		chars[pos] = rune(c[0])
		s2 := string(chars[l:r])

		if strings.Contains(orig, "abc") && !strings.Contains(s2, "abc") {
			total--
		} else if !strings.Contains(orig, "abc") && strings.Contains(s2, "abc") {
			total++
		}

		fmt.Fprintln(out, total)
	}
}

package main

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

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

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    defer output.Flush()
    solve()
    return
	var t int64
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
}

func solve() {
    var n, q int
    fmt.Fscan(input, &n, &q)
    var s string
    fmt.Fscan(input, &s)
    cnt := 0
    for i := 0; i + 3 <= len(s); i++ {
        if s[i:i+3] == "abc" { cnt++ }
    }

    a := []byte(s)
    good := func(p int) bool {
        if p - 2 >= 0 && p + 1 <= len(a) && string(a[p - 2:p + 1]) == "abc" { return true}
        if p - 1 >= 0 && p + 2 <= len(a) && string(a[p - 1:p + 2]) == "abc" { return true}
        if p >= 0 && p + 3 <= len(a) && string(a[p:p + 3]) == "abc" { return true}
        return false
    }

    for q > 0 {
        q--
        var pos int
        var ss string
        fmt.Fscan(input, &pos, &ss)
        pos--
        c := ss[0]

        if good(pos) { cnt-- }
        a[pos] = c
        if good(pos) { cnt++ }

        fmt.Fprintln(output, cnt)
    }
}

package main

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

/*
9 6
aabcabcbc
1 a
2 a
2 c
2 a
7 b
7 a
---
2
2
1
2
1
2
*/

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

	bld := strings.Builder{}

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

	for ; t > 0; t-- {
		var n, q int
		var st string
		scanf("%d %d\n%s\n", &n, &q, &st)

		s := []byte(st)

		abcs := make(map[int]bool)

		for j := 0; j < n-2; j++ {
			if s[j] == 'a' && s[j+1] == 'b' && s[j+2] == 'c' {
				abcs[j] = true
			}
		}

		for k := 0; k < q; k++ {
			var i int
			var s2 string
			scanf("%d %s\n", &i, &s2)
			i--

			ch := s2[0]

			s[i] = ch

			for j := max(0, i-2); j < min(n-2, i+1); j++ {
				if s[j] == 'a' && s[j+1] == 'b' && s[j+2] == 'c' {
					abcs[j] = true
				} else {
					delete(abcs, j)
				}
			}

			fmt.Fprintln(&bld, len(abcs))
		}
	}

	printf("%s", bld.String())
}

const maxUint = ^uint(0)

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

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

//goland:noinspection GoUnusedType
type pair struct {
	fst int
	snd int
}

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

//goland:noinspection GoUnusedFunction
func printf(f string, a ...interface{}) {
	if _, err := fmt.Fprintf(writer, f, a...); err != nil {
		panic(err)
	}
}

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

//goland:noinspection GoUnusedFunction
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

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

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

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

	for i := 5; i*i <= n; i += 6 {
		if n%i == 0 || n%(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, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 2
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	lenNum := int(math.Log10(float64(num))) + 1
	if 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 (
	"fmt"
	"bufio"
	"os"
)

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

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    defer output.Flush()
    solve()
    return
	var t int64
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
}

func solve() {
    var n, q int
    fmt.Fscan(input, &n, &q)
    var s string
    fmt.Fscan(input, &s)
    cnt := 0
    for i := 0; i < len(s); i++ {
        if i + 3 <= len(s) && s[i:i+3] == "abc" { cnt++ }
    }

    a := []byte(s)
    good := func(p int) bool {
        if p - 2 >= 0 && p + 1 <= len(a) && string(a[p - 2:p + 1]) == "abc" { return true}
        if p - 1 >= 0 && p + 2 <= len(a) && string(a[p - 1:p + 2]) == "abc" { return true}
        if p >= 0 && p + 3 <= len(a) && string(a[p:p + 3]) == "abc" { return true}
        return false
    }

    for q > 0 {
        q--
        var pos int
        var ss string
        fmt.Fscan(input, &pos, &ss)
        pos--
        c := ss[0]
        if c == a[pos] {
            fmt.Fprintln(output, cnt)
            continue
        }

        if good(pos) {
            cnt--
            a[pos] = c
        } else {
            a[pos] = c
            if good(pos) {
                cnt++
            }
        }

        fmt.Fprintln(output, cnt)
    }
}

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


package main

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

func f(n int) int {
	r := 1
	for n > 0 {
		if n%10 > 0 {
			r *= n % 10
		}
		n /= 10
	}
	return r
}

func g(n int) int {
	if n < 10 {
		return n
	}
	return g(f(n))
}

var prefix [1e6 + 47][10]int

func init() {
	for i := 1; i < len(prefix); i++ {
		prefix[i] = prefix[i-1]
		r := g(i)
		prefix[i][r]++
	}
}

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

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

	for i := 0; i < n; i++ {
		var l, r, k int
		fmt.Fscan(in, &l, &r, &k)
		fmt.Fprintln(out, prefix[r][k]-prefix[l-1][k])
	}
}

package main

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

var f[1000010] int
var c[10][1000010] int

func getF(x int) int{
	if f[x] > 0{
		return f[x]
	}
	tmp := 1
	for ;;{
		if x == 0{
			break
		}
		if x % 10 > 0{
			tmp = tmp * (x % 10)
		}
		x /= 10
	}
	return getF(tmp)
}

func prework(){
	for i := 1; i < 10; i++{
		f[i] = i
		c[i][i] = 1
	}
	for i := 10; i < 1000010; i++{
		f[i] = getF(i)
		c[f[i]][i] = 1
	}
	for i := 1; i < 10; i++{
		for j:= 1; j < 1000010; j++{
			c[i][j] = c[i][j - 1] + c[i][j]
		}
	}
}

func main(){
	in := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(in, &n)
	prework()
	for i := 1; i <= n; i++{
		var l, r, k int
		fmt.Fscan(in, &l, &r, &k)
		fmt.Println(c[k][r] - c[k][l - 1])
	}
}


package main

import (
	"fmt"
	"strconv"
)

func Process(width int, k int) (string, [4][]string) {
	var cityArray = [4][]string{}

	for i := 0; i < 4; i++ {
		cityArray[i] = make([]string, width)

		for key := range cityArray[i] {
			cityArray[i][key] = "."
		}
	}

	if k == 0 {
		return "YES", cityArray
	}

	var space = 2 * (width - 2)

	if k > space {
		return "NO", cityArray
	}

	for row := 0; row < 4; row++ {
		if k == 0 {
			break
		}

		if row == 0 || row == 3 {
			continue
		}

		if k%2 == 1 || space == k {
			cityArray[row][width/2] = "#"
			k--
		}

		for col := 0; col < width/2; col++ {
			if k == 0 {
				break
			}

			if col == 0 || col == width-1 {
				continue
			}

			cityArray[row][col] = "#"
			k--

			if k == 0 {
				break
			}

			if col == width/2 {
				continue
			}

			cityArray[row][width-1-col] = "#"
			k--
		}
	}

	for rowKey, row := range cityArray {
		if rowKey == 0 || rowKey == 3 {
			continue
		}

		for colKey := range row {
			if colKey == 0 || colKey == width-1 {
				continue
			}

		}
	}

	return "YES", cityArray
}

func main() {
	var widthString string
	var countString string
	fmt.Scanln(&widthString, &countString)

	var width, _ = strconv.Atoi(widthString)
	var count, _ = strconv.Atoi(countString)

	var result, city = Process(width, count)

	fmt.Println(result)

	if result == "YES" {
		for _, row := range city {
			for _, col := range row {
				fmt.Print(col)
			}
			fmt.Println()
		}
	}
}

package main

import "fmt"

func main() {

	var n, k int
	const length = 4
	fmt.Scan(&n, &k)

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

	counter := 0
	for i := 1; i < int(n/2) && counter < k-1; i++ {
		cityMap[1][i], cityMap[1][n-1-i] = 1, 1
		counter += 2
	}

	if k % 2 == 1 {
		cityMap[1][n/2] = 1
		counter++
	}

	if counter < k {
		for i := 1; i < int(n/2) && counter < k; i++ {
			cityMap[2][i], cityMap[2][n-1-i] = 1, 1
			counter += 2
		}
	}

	if k == 2 * (n-2) {
		cityMap[1][n/2], cityMap[2][n/2] = 1, 1
	}

	fmt.Println("YES")

	for i := 0; i < length; i++ {
		for j := 0; j < n; j++ {
			if cityMap[i][j] == 0 {
				fmt.Print(".")
			} else {
				fmt.Print("#")
			}
		}
		fmt.Println()
	}
}

package main

import (
	"fmt"
	"strconv"
)

func Process(width int, k int) (string, [4][]string) {
	var cityArray = [4][]string{}

	for i := 0; i < 4; i++ {
		cityArray[i] = make([]string, width)

		for key := range cityArray[i] {
			cityArray[i][key] = "."
		}
	}

	if k == 0 {
		return "YES", cityArray
	}

	var space = 2 * (width - 2)

	if k > space {
		return "NO", cityArray
	}

	for row := 0; row < 4; row++ {
		if k == 0 {
			break
		}

		if row == 0 || row == 3 {
			continue
		}

		for col := 0; col < width; col++ {
			if k == 0 {
				break
			}

			if col == 0 || col == width-1 {
				continue
			}

			cityArray[row][col] = "#"
			k--

			if k == 0 {
				break
			}

			if cityArray[row][width-1-col] == "#" {
				continue
			}

			cityArray[row][width-1-col] = "#"
			k--
		}
	}

	for rowKey, row := range cityArray {
		if rowKey == 0 || rowKey == 3 {
			continue
		}

		for colKey := range row {
			if colKey == 0 || colKey == width-1 {
				continue
			}

		}
	}

	return "YES", cityArray
}

func main() {
	var widthString string
	var countString string
	fmt.Scanln(&widthString, &countString)

	var width, _ = strconv.Atoi(widthString)
	var count, _ = strconv.Atoi(countString)

	var result, city = Process(width, count)

	fmt.Println(result)

	if result == "YES" {
		for _, row := range city {
			for _, col := range row {
				fmt.Print(col)
			}
			fmt.Println()
		}
	}
}

package main

import (
	"fmt"
	"strconv"
)

func Process(width int, k int) (string, [4][]string) {
	var cityArray = [4][]string{}

	for i := 0; i < 4; i++ {
		cityArray[i] = make([]string, width)

		for key := range cityArray[i] {
			cityArray[i][key] = "."
		}
	}

	if k == 0 {
		return "YES", cityArray
	}

	var space = 2 * (width - 2)

	if k > space {
		return "NO", cityArray
	}

	for row := 0; row < 4; row++ {
		if k == 0 {
			break
		}

		if row == 0 || row == 3 {
			continue
		}

		for col := 0; col < width; col++ {
			if k == 0 {
				break
			}

			if col == 0 || col == width-1 {
				continue
			}

			cityArray[row][col] = "#"
			k--

			if k == 0 {
				break
			}

			cityArray[row][width-1-col] = "#"
			k--
		}
	}

	for rowKey, row := range cityArray {
		if rowKey == 0 || rowKey == 3 {
			continue
		}

		for colKey := range row {
			if colKey == 0 || colKey == width-1 {
				continue
			}

		}
	}

	return "YES", cityArray
}

func main() {
	var widthString string
	var countString string
	fmt.Scanln(&widthString, &countString)

	var width, _ = strconv.Atoi(widthString)
	var count, _ = strconv.Atoi(countString)

	var result, city = Process(width, count)

	fmt.Println(result)

	if result == "YES" {
		for _, row := range city {
			for _, col := range row {
				fmt.Print(col)
			}
			fmt.Println()
		}
	}
}

package main

import (
	"fmt"
	"strconv"
)

func Process(width int, k int) (string, [4][]string) {
	var cityArray = [4][]string{}

	for i := 0; i < 4; i++ {
		cityArray[i] = make([]string, width)

		for key := range cityArray[i] {
			cityArray[i][key] = "."
		}
	}

	if k == 0 {
		return "YES", cityArray
	}

	var space = 2 * (width - 2)

	if k > space {
		return "NO", cityArray
	}

	for row := 0; row < 4; row++ {
		if k == 0 {
			break
		}

		if row == 0 || row == 3 {
			continue
		}

		for col := 0; col < width; col++ {
			if k == 0 {
				break
			}

			if col == 0 || col == width-1 {
				continue
			}

			if k == 1 {
				var middle = width / 2
				cityArray[row][middle] = "#"
				k--
			}

			if k == 0 {
				break
			}

			cityArray[row][col] = "#"
			k--

			if k == 0 {
				break
			}

			if cityArray[row][width-1-col] == "#" {
				continue
			}

			cityArray[row][width-1-col] = "#"
			k--
		}
	}

	for rowKey, row := range cityArray {
		if rowKey == 0 || rowKey == 3 {
			continue
		}

		for colKey := range row {
			if colKey == 0 || colKey == width-1 {
				continue
			}

		}
	}

	return "YES", cityArray
}

func main() {
	var widthString string
	var countString string
	fmt.Scanln(&widthString, &countString)

	var width, _ = strconv.Atoi(widthString)
	var count, _ = strconv.Atoi(countString)

	var result, city = Process(width, count)

	fmt.Println(result)

	if result == "YES" {
		for _, row := range city {
			for _, col := range row {
				fmt.Print(col)
			}
			fmt.Println()
		}
	}
}

package main

import (
	"fmt"
	"strconv"
)

func Process(width int, k int) (string, [4][]string) {
	var cityArray = [4][]string{}

	for i := 0; i < 4; i++ {
		cityArray[i] = make([]string, width)

		for key := range cityArray[i] {
			cityArray[i][key] = "."
		}
	}

	if k == 0 {
		return "YES", cityArray
	}

	var space = 2 * (width - 2)

	if k > space {
		return "NO", cityArray
	}

	for row := 0; row < 4; row++ {
		if k == 0 {
			break
		}

		if row == 0 || row == 3 {
			continue
		}

		for col := 0; col < width/2; col++ {
			if k == 0 {
				break
			}

			if col == 0 || col == width-1 {
				continue
			}

			if k == 1 {
				cityArray[row][width/2] = "#"
				k--
			}

			if k == 0 {
				break
			}

			cityArray[row][col] = "#"
			k--

			if k == 0 {
				break
			}

			cityArray[row][width-1-col] = "#"
			k--

			if k == 1 {
				cityArray[row][width/2] = "#"
				k--
			}
		}
	}

	for rowKey, row := range cityArray {
		if rowKey == 0 || rowKey == 3 {
			continue
		}

		for colKey := range row {
			if colKey == 0 || colKey == width-1 {
				continue
			}

		}
	}

	return "YES", cityArray
}

func main() {
	var widthString string
	var countString string
	fmt.Scanln(&widthString, &countString)

	var width, _ = strconv.Atoi(widthString)
	var count, _ = strconv.Atoi(countString)

	var result, city = Process(width, count)

	fmt.Println(result)

	if result == "YES" {
		for _, row := range city {
			for _, col := range row {
				fmt.Print(col)
			}
			fmt.Println()
		}
	}
}

package main

import (
	"fmt"
	"strconv"
)

func Process(width int, k int) (string, [4][]string) {
	var cityArray = [4][]string{}

	for i := 0; i < 4; i++ {
		cityArray[i] = make([]string, width)

		for key := range cityArray[i] {
			cityArray[i][key] = "."
		}
	}

	if k == 0 {
		return "YES", cityArray
	}

	var space = 2 * (width - 2)

	if k > space {
		return "NO", cityArray
	}

	for row := 0; row < 4; row++ {
		if k == 0 {
			break
		}

		if row == 0 || row == 3 {
			continue
		}

		for col := 0; col < width; col++ {
			if k == 0 {
				break
			}

			if col == 0 || col == width-1 {
				continue
			}

			if k == 1 {
				var middle = width/2 + 1
				cityArray[row][middle] = "#"
			}

			cityArray[row][col] = "#"
			k--

			if k == 0 {
				break
			}

			if cityArray[row][width-1-col] == "#" {
				continue
			}

			cityArray[row][width-1-col] = "#"
			k--
		}
	}

	for rowKey, row := range cityArray {
		if rowKey == 0 || rowKey == 3 {
			continue
		}

		for colKey := range row {
			if colKey == 0 || colKey == width-1 {
				continue
			}

		}
	}

	return "YES", cityArray
}

func main() {
	var widthString string
	var countString string
	fmt.Scanln(&widthString, &countString)

	var width, _ = strconv.Atoi(widthString)
	var count, _ = strconv.Atoi(countString)

	var result, city = Process(width, count)

	fmt.Println(result)

	if result == "YES" {
		for _, row := range city {
			for _, col := range row {
				fmt.Print(col)
			}
			fmt.Println()
		}
	}
}

package main

import (
	"fmt"
	"strconv"
)

func Process(width int, k int) (string, [4][]string) {
	var cityArray = [4][]string{}

	for i := 0; i < 4; i++ {
		cityArray[i] = make([]string, width)

		for key := range cityArray[i] {
			cityArray[i][key] = "."
		}
	}

	if k == 0 {
		return "YES", cityArray
	}

	var space = 2 * (width - 2)

	if k > space {
		return "NO", cityArray
	}

	for row := 0; row < 4; row++ {
		if k == 0 {
			break
		}

		if row == 0 || row == 3 {
			continue
		}

		for col := 0; col < width; col++ {
			if k == 0 {
				break
			}

			if col == 0 || col == width-1 {
				continue
			}

			if k == 1 {
				var middle = width/2 + 1
				cityArray[row][middle] = "#"
				k--
			}

			if k == 0 {
				break
			}

			cityArray[row][col] = "#"
			k--

			if k == 0 {
				break
			}

			if cityArray[row][width-1-col] == "#" {
				continue
			}

			cityArray[row][width-1-col] = "#"
			k--
		}
	}

	for rowKey, row := range cityArray {
		if rowKey == 0 || rowKey == 3 {
			continue
		}

		for colKey := range row {
			if colKey == 0 || colKey == width-1 {
				continue
			}

		}
	}

	return "YES", cityArray
}

func main() {
	var widthString string
	var countString string
	fmt.Scanln(&widthString, &countString)

	var width, _ = strconv.Atoi(widthString)
	var count, _ = strconv.Atoi(countString)

	var result, city = Process(width, count)

	fmt.Println(result)

	if result == "YES" {
		for _, row := range city {
			for _, col := range row {
				fmt.Print(col)
			}
			fmt.Println()
		}
	}
}

package main

import (
	"fmt"
	"strconv"
)

func Process(width int, k int) (string, [4][]string) {
	var cityArray = [4][]string{}

	for i := 0; i < 4; i++ {
		cityArray[i] = make([]string, width)

		for key := range cityArray[i] {
			cityArray[i][key] = "."
		}
	}

	if k == 0 {
		return "YES", cityArray
	}

	var space = 2 * (width - 2)

	if k > space {
		return "NO", cityArray
	}

	for row := 0; row < 4; row++ {
		if k == 0 {
			break
		}

		if row == 0 || row == 3 {
			continue
		}

		for col := 0; col < width/2; col++ {
			if k == 0 {
				break
			}

			if col == 0 || col == width-1 {
				continue
			}

			if k == 1 {
				cityArray[row][width/2] = "#"
				k--
			}

			if k == 0 {
				break
			}

			cityArray[row][col] = "#"
			k--

			if k == 1 {
				cityArray[row][width/2] = "#"
				k--
			}

			if k == 0 {
				break
			}

			cityArray[row][width-1-col] = "#"
			k--

			if k == 1 {
				cityArray[row][width/2] = "#"
				k--
			}
		}
	}

	for rowKey, row := range cityArray {
		if rowKey == 0 || rowKey == 3 {
			continue
		}

		for colKey := range row {
			if colKey == 0 || colKey == width-1 {
				continue
			}

		}
	}

	return "YES", cityArray
}

func main() {
	var widthString string
	var countString string
	fmt.Scanln(&widthString, &countString)

	var width, _ = strconv.Atoi(widthString)
	var count, _ = strconv.Atoi(countString)

	var result, city = Process(width, count)

	fmt.Println(result)

	if result == "YES" {
		for _, row := range city {
			for _, col := range row {
				fmt.Print(col)
			}
			fmt.Println()
		}
	}
}

package main

import (
	"fmt"
	"strconv"
)

func Process(width int, k int) (string, [4][]string) {
	var cityArray = [4][]string{}

	for i := 0; i < 4; i++ {
		cityArray[i] = make([]string, width)

		for key := range cityArray[i] {
			cityArray[i][key] = "."
		}
	}

	if k == 0 {
		return "YES", cityArray
	}

	var space = 2 * (width - 2)

	if k > space {
		return "NO", cityArray
	}

	for row := 0; row < 4; row++ {
		if k == 0 {
			break
		}

		if row == 0 || row == 3 {
			continue
		}

		if k%2 == 1 || width == 3 {
			cityArray[row][width/2] = "#"
			k--
		}

		for col := 0; col < width/2; col++ {
			if k == 0 {
				break
			}

			if col == 0 || col == width-1 {
				continue
			}

			cityArray[row][col] = "#"
			k--

			if k == 0 {
				break
			}

			if col == width/2 {
				continue
			}

			cityArray[row][width-1-col] = "#"
			k--
		}
	}

	for rowKey, row := range cityArray {
		if rowKey == 0 || rowKey == 3 {
			continue
		}

		for colKey := range row {
			if colKey == 0 || colKey == width-1 {
				continue
			}

		}
	}

	return "YES", cityArray
}

func main() {
	var widthString string
	var countString string
	fmt.Scanln(&widthString, &countString)

	var width, _ = strconv.Atoi(widthString)
	var count, _ = strconv.Atoi(countString)

	var result, city = Process(width, count)

	fmt.Println(result)

	if result == "YES" {
		for _, row := range city {
			for _, col := range row {
				fmt.Print(col)
			}
			fmt.Println()
		}
	}
}

package main

import "fmt"

func main() {

	var n, k int
	const length = 4
	fmt.Scan(&n, &k)

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

	counter := 0
	for i := 1; i < int(n/2) && counter < k-1; i++ {
		cityMap[1][i], cityMap[1][n-1-i] = 1, 1
		counter += 2
	}

	if k % 2 == 1 {
		fmt.Println("ADDED")
		cityMap[1][n/2] = 1
		counter++
	}

	if counter < k {
		for i := 1; i < int(n/2) && counter < k; i++ {
			cityMap[2][i], cityMap[2][n-1-i] = 1, 1
			counter += 2
		}
	}

	if k == 2 * (n-2) {
		cityMap[1][n/2], cityMap[2][n/2] = 1, 1
	}

	fmt.Println("YES")

	for i := 0; i < length; i++ {
		for j := 0; j < n; j++ {
			if cityMap[i][j] == 0 {
				fmt.Print(".")
			} else {
				fmt.Print("#")
			}
		}
		fmt.Println()
	}
}


package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type sliceInt64 []int64
func (a sliceInt64) Len() int {
  return len(a)
}
func (a sliceInt64) Swap(i, j int) {
  a[i], a[j] = a[j], a[i]
}
func (a sliceInt64) Less(i, j int) bool {
  return a[i] < a[j]
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n := getI()
  values := make([]int64, n)
  ceilings := make([]int64, n)
  for i := range values {
    values[i] = getI64()
    ceilings[i] = values[i]
  }
  sort.Sort(sliceInt64(ceilings))

  cost := make([]int64, n)
  for _, x := range values {
    pos := 0
    for ; pos < n && ceilings[pos] < x; pos++ {
      cost[pos] += x - ceilings[pos]
    }
    best := cost[pos]
    for ; pos < n; pos++ {
      attempt := cost[pos] + ceilings[pos] - x
      if attempt < best {
        best = attempt
      }
      cost[pos] = best
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", cost[n - 1]))
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"sort"
)

func abs(x int) int64 {
	if x > 0 {
		return int64(x)
	} else {
		return int64(-x)
	}
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewReader(bs)
	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	a := make([]int, n)
	b := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d ", &a[i])
		b[i] = a[i]
	}
	sort.Ints(b)
	var dp [2][]int64
	dp[0] = make([]int64, n)
	dp[1] = make([]int64, n)

	dp[0][0] = abs(a[0] - b[0])
	for i := 1; i < n; i++ {
		dp[0][i] = abs(a[0] - b[i])
		if dp[0][i] > dp[0][i-1] {
			dp[0][i] = dp[0][i-1]
		}
	}
	prev, cur := 0, 1
	for i := 1; i < n; i++ {
		dp[cur][0] = dp[prev][0] + abs(a[i]-b[0])
		for j := 1; j < n; j++ {
			if dp[prev][j]+abs(a[i]-b[j]) > dp[cur][j-1] {
				dp[cur][j] = dp[cur][j-1]
			} else {
				dp[cur][j] = dp[prev][j] + abs(a[i]-b[j])
			}
		}
		prev, cur = cur, prev
	}
	fmt.Printf("%d", dp[prev][n-1])
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type sliceInt64 []int64
func (a sliceInt64) Len() int {
  return len(a)
}
func (a sliceInt64) Swap(i, j int) {
  a[i], a[j] = a[j], a[i]
}
func (a sliceInt64) Less(i, j int) bool {
  return a[i] < a[j]
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n := getI()
  values := make([]int64, n)
  ceilings := make([]int64, n)
  for i := range values {
    values[i] = getI64()
    ceilings[i] = values[i]
  }
  sort.Sort(sliceInt64(ceilings))

  cost := make([]int64, n)
  for _, x := range values {
    pos := 0
    for ; pos < n && ceilings[pos] < x; pos++ {
      cost[pos] += x - ceilings[pos]
    }
    pos += 1
    for ; pos < n; pos++ {
      cost[pos] += ceilings[pos] - x
      if cost[pos - 1] < cost[pos] {
        cost[pos] = cost[pos - 1]
      }
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", cost[n - 1]))
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type sliceInt64 []int64
func (a sliceInt64) Len() int {
  return len(a)
}
func (a sliceInt64) Swap(i, j int) {
  a[i], a[j] = a[j], a[i]
}
func (a sliceInt64) Less(i, j int) bool {
  return a[i] < a[j]
}

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

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n := getI()
  values := make([]int64, n)
  ceilings := make([]int64, n)
  for i := range values {
    values[i] = getI64()
    ceilings[i] = values[i]
  }
  sort.Sort(sliceInt64(ceilings))

  cost := make([]int64, n)
  for _, x := range values {
    pos := 0
    for ; pos < n && ceilings[pos] < x; pos++ {
      cost[pos] += x - ceilings[pos]
    }
    pos += 1
    for ; pos < n; pos++ {
      cost[pos] = min(cost[pos - 1], cost[pos] + ceilings[pos] - x)
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", cost[n - 1]))
}

// https://codeforces.com/contest/13/problem/C
package main

import (
	"fmt"
	"sort"
)

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

func main() {
	var (
		n int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	fmt.Scan(&n)
	a := make([]int64, n)
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
		b[i] = a[i]
	}
	sort.Slice(b, func (i, j int) bool {return b[i] < b[j]})
	m := 0
	for i := 1; i < n; i++ {
		if b[m] != b[i] {
			m++
			b[m] = b[i]
		}
	}

	f := make([]int64, m + 1)
	f[0] = abs(a[0] - b[0])
	for k := 1; k <= m; k++ {
		f[k] = min(abs(a[0] - b[k]), f[k - 1])
	}

	for i := 1; i < n; i++ {
		g := make([]int64, m + 1)
		for k := 0; k <= m; k++ {
			if k > 0 {
				g[k] = min(g[k - 1], abs(a[i] - b[k]) + f[k])
			} else {
				g[k] = abs(a[i] - b[k]) + f[k]
			}
		}
		f = g
	}

	fmt.Println(f[m])
}

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

	return b
}

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

package main

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

type hp struct{ sort.IntSlice }

func (h hp) Less(i, j int) bool  { return h.IntSlice[i] > h.IntSlice[j] }
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() interface{}   { a := h.IntSlice; v := a[len(a)-1]; h.IntSlice = a[:len(a)-1]; return v }

// github.com/EndlessCheng/codeforces-go
func CF13C(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, v int
	Fscan(in, &n)
	ans := int64(0)
	for h := (hp{}); n > 0; n-- {
		Fscan(in, &v)
		heap.Push(&h, v)
		if d := h.IntSlice[0] - v; d > 0 {
			ans += int64(d)
			h.IntSlice[0] = v
			heap.Fix(&h, 0)
		}
	}
	Fprint(out, ans)
}

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

package main

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

type hp struct{ sort.IntSlice }

func (h hp) Less(i, j int) bool  { return h.IntSlice[i] > h.IntSlice[j] }
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() interface{}   { a := h.IntSlice; v := a[len(a)-1]; h.IntSlice = a[:len(a)-1]; return v }

// github.com/EndlessCheng/codeforces-go
func CF13C(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, v int
	Fscan(in, &n)
	ans := int64(0)
	for h := (hp{}); n > 0; n-- {
		Fscan(in, &v)
		heap.Push(&h, v)
		if d := h.IntSlice[0] - v; d > 0 {
			ans += int64(0)
			h.IntSlice[0] = v
			heap.Fix(&h, 0)
		}
	}
	Fprint(out, ans)
}

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

package main

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

type hp struct{ sort.IntSlice }

func (h hp) Less(i, j int) bool  { return h.IntSlice[i] > h.IntSlice[j] }
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() interface{}   { a := h.IntSlice; v := a[len(a)-1]; h.IntSlice = a[:len(a)-1]; return v }

// github.com/EndlessCheng/codeforces-go
func CF13C(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, v, ans int
	Fscan(in, &n)
	for h := (hp{}); n > 0; n-- {
		Fscan(in, &v)
		heap.Push(&h, v)
		if d := h.IntSlice[0] - v; d > 0 {
			ans += d
			h.IntSlice[0] = v
			heap.Fix(&h, 0)
		}
	}
	Fprint(out, ans)
}

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


package main

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

// github.com/EndlessCheng/codeforces-go
func CF700B(_r io.Reader, out io.Writer) {
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}
	in := bufio.NewReader(_r)
	var n, k, v, w int
	Fscan(in, &n, &k)
	k *= 2
	has := make([]bool, n)
	for i := k; i > 0; i-- {
		Fscan(in, &v)
		has[v-1] = true
	}
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	ans := int64(0)
	var f func(v, fa int) int
	f = func(v, fa int) (sz int) {
		if has[v] {
			sz = 1
		}
		for _, w := range g[v] {
			if w != fa {
				s := f(w, v)
				ans += int64(min(s, k-s))
				sz += s
			}
		}
		return
	}
	f(0, -1)
	Fprint(out, ans)
}

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

package main

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

type FastIO struct {
	in  *bufio.Scanner
	out *bufio.Writer
}

func NewFastIO() *FastIO {
	f := FastIO{
		in:  bufio.NewScanner(os.Stdin),
		out: bufio.NewWriter(os.Stdout),
	}

	f.in.Split(bufio.ScanWords)
	return &f
}

func (f *FastIO) Flush() {
	f.out.Flush()
}

func (f *FastIO) ReadInt() int {
	f.in.Scan()
	val, _ := strconv.Atoi(f.in.Text())
	return val
}

func (f *FastIO) WriteInt64(val int64) {
	f.out.WriteString(strconv.FormatInt(val, 10))
}

var n, k, e, m, h int
var univ, adj, next, last []int
var res int64

func AddEdge(x int, y int) {
	adj[e] = y
	next[e] = last[x]
	last[x] = e
	e++
}

func dfs(x int, par int) int {
	cntX := univ[x]

	for e := last[x]; e != -1; e = next[e] {
		y := adj[e]
		if y != par {
			cntY := dfs(y, x)
			cntZ := h - cntY
			if cntY < cntZ {
				res += int64(cntY)
			} else {
				res += int64(cntZ)
			}
			cntX += cntY
		}
	}

	return cntX
}

func main() {
	f := NewFastIO()
	defer f.Flush()

	n = f.ReadInt()
	k = f.ReadInt()

	e = 0
	m = 2 * n
	h = 2 * k

	univ = make([]int, n)
	adj = make([]int, m)
	next = make([]int, m)
	last = make([]int, n)

	for i := 0; i < n; i++ {
		univ[i] = 0
		last[i] = -1
	}

	for i := 0; i < 2*k; i++ {
		u := f.ReadInt() - 1
		univ[u]++
	}

	for i := 0; i < n-1; i++ {
		x := f.ReadInt() - 1
		y := f.ReadInt() - 1
		AddEdge(x, y)
		AddEdge(y, x)
	}

	res = 0
	dfs(0, 0)
	f.WriteInt64(res)
}

package main

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

type FastIO struct {
	in  *bufio.Scanner
	out *bufio.Writer
}

func NewFastIO() *FastIO {
	f := FastIO{
		in:  bufio.NewScanner(os.Stdin),
		out: bufio.NewWriter(os.Stdout),
	}

	f.in.Split(bufio.ScanWords)
	return &f
}

func (f *FastIO) Flush() {
	f.out.Flush()
}

func (f *FastIO) ReadInt() int {
	f.in.Scan()
	val, _ := strconv.Atoi(f.in.Text())
	return val
}

func (f *FastIO) WriteInt64(val int64) {
	f.out.WriteString(strconv.FormatInt(val, 10))
}

type Graph struct {
	e    int
	adj  []int
	next []int
	last []int
}

func NewGraph(n int, m int) *Graph {
	g := Graph{
		e:    0,
		adj:  make([]int, m),
		next: make([]int, m),
		last: make([]int, n),
	}
	for i := 0; i < n; i++ {
		g.last[i] = -1
	}
	return &g
}

func (g *Graph) AddEdge(x int, y int) {
	g.adj[g.e] = y
	g.next[g.e] = g.last[x]
	g.last[x] = g.e
	g.e++
}

var n, k int
var univ []bool
var g *Graph
var cnt []int

func dfs(x int) {
	if univ[x] {
		cnt[x] = 1
	} else {
		cnt[x] = 0
	}
	for e := g.last[x]; e != -1; e = g.next[e] {
		y := g.adj[e]
		if cnt[y] == -1 {
			dfs(y)
			cnt[x] += cnt[y]
		}
	}
}

func main() {
	f := NewFastIO()
	defer f.Flush()

	n = f.ReadInt()
	k = f.ReadInt()

	univ = make([]bool, n)
	for i := 0; i < n; i++ {
		univ[i] = false
	}

	for i := 0; i < 2*k; i++ {
		u := f.ReadInt() - 1
		univ[u] = true
	}

	g = NewGraph(n, 2*n)
	for i := 0; i < n-1; i++ {
		x := f.ReadInt() - 1
		y := f.ReadInt() - 1
		g.AddEdge(x, y)
		g.AddEdge(y, x)
	}

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

	res := int64(0)
	for y := 1; y < n; y++ {
		a := int64(cnt[y])
		b := int64(2*k) - a
		if a < b {
			res += a
		} else {
			res += b
		}
	}

	f.WriteInt64(res)
}

package main

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

var in *bufio.Scanner
var out *bufio.Writer
var n, k, e, m, h int
var univ, adj, next, last []int
var res int64

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

func writeInt64(val int64) {
	out.WriteString(strconv.FormatInt(val, 10))
}

func addEdge(x int, y int) {
	adj[e] = y
	next[e] = last[x]
	last[x] = e
	e++
}

func dfs(x int, par int) int {
	xc := univ[x]
	for e := last[x]; e != -1; e = next[e] {
		y := adj[e]
		if y != par {
			yc := dfs(y, x)
			zc := h - yc
			xc += yc
			if yc < zc {
				res += int64(yc)
			} else {
				res += int64(zc)
			}
		}
	}
	return xc
}

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

	in.Split(bufio.ScanWords)
	defer out.Flush()

	n = readInt()
	k = readInt()

	e = 0
	m = 2 * n
	h = 2 * k

	univ = make([]int, n)
	adj = make([]int, m)
	next = make([]int, m)
	last = make([]int, n)

	for i := 0; i < n; i++ {
		univ[i] = 0
		last[i] = -1
	}

	for i := 0; i < 2*k; i++ {
		u := readInt() - 1
		univ[u]++
	}

	for i := 0; i < n-1; i++ {
		x := readInt() - 1
		y := readInt() - 1
		addEdge(x, y)
		addEdge(y, x)
	}

	res = 0
	dfs(0, 0)
	writeInt64(res)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF700B(_r io.Reader, out io.Writer) {
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}
	in := bufio.NewReader(_r)
	var n, k, v, w int
	Fscan(in, &n, &k)
	k *= 2
	size := make([]int, n)
	for i := k; i > 0; i-- {
		Fscan(in, &v)
		size[v-1] = 1
	}
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	ans := int64(0)
	var f func(v, fa int) int
	f = func(v, fa int) int {
		sz := size[v]
		for _, w := range g[v] {
			if w != fa {
				s := f(w, v)
				ans += int64(min(s, k-s))
				sz += s
			}
		}
		return sz
	}
	f(0, -1)
	Fprint(out, ans)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF700B(_r io.Reader, out io.Writer) {
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}
	buf := make([]byte, 4096)
	_i := len(buf)
	rc := func() byte {
		if _i == len(buf) {
			_r.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}
	n, k := r(), r()*2
	has := make([]bool, n)
	for i := k; i > 0; i-- {
		has[r()-1] = true
	}
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		v, w := r()-1, r()-1
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	ans := int64(0)
	var f func(v, fa int) int
	f = func(v, fa int) (sz int) {
		if has[v] {
			sz = 1
		}
		for _, w := range g[v] {
			if w != fa {
				s := f(w, v)
				ans += int64(min(s, k-s))
				sz += s
			}
		}
		return
	}
	f(0, -1)
	Fprint(out, ans)
}

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

package main

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

type FastIO struct {
	in  *bufio.Scanner
	out *bufio.Writer
}

func NewFastIO() *FastIO {
	f := FastIO{
		in:  bufio.NewScanner(os.Stdin),
		out: bufio.NewWriter(os.Stdout),
	}

	f.in.Split(bufio.ScanWords)
	return &f
}

func (f *FastIO) Flush() {
	f.out.Flush()
}

func (f *FastIO) ReadInt() int {
	f.in.Scan()
	val, _ := strconv.Atoi(f.in.Text())
	return val
}

func (f *FastIO) WriteInt(val int) {
	f.out.WriteString(strconv.Itoa(val))
}

type Graph struct {
	e    int
	adj  []int
	next []int
	last []int
}

func NewGraph(n int, m int) *Graph {
	g := Graph{
		e:    0,
		adj:  make([]int, m),
		next: make([]int, m),
		last: make([]int, n),
	}
	for i := 0; i < n; i++ {
		g.last[i] = -1
	}
	return &g
}

func (g *Graph) AddEdge(x int, y int) {
	g.adj[g.e] = y
	g.next[g.e] = g.last[x]
	g.last[x] = g.e
	g.e++
}

var n, k int
var univ []bool
var g *Graph
var cnt []int

func dfs(x int) {
	if univ[x] {
		cnt[x] = 1
	} else {
		cnt[x] = 0
	}
	for e := g.last[x]; e != -1; e = g.next[e] {
		y := g.adj[e]
		if cnt[y] == -1 {
			dfs(y)
			cnt[x] += cnt[y]
		}
	}
}

func main() {
	f := NewFastIO()
	defer f.Flush()

	n = f.ReadInt()
	k = f.ReadInt()

	univ = make([]bool, n)
	for i := 0; i < n; i++ {
		univ[i] = false
	}

	for i := 0; i < 2*k; i++ {
		u := f.ReadInt() - 1
		univ[u] = true
	}

	g = NewGraph(n, 2*n)
	for i := 0; i < n-1; i++ {
		x := f.ReadInt() - 1
		y := f.ReadInt() - 1
		g.AddEdge(x, y)
		g.AddEdge(y, x)
	}

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

	res := 0
	for y := 1; y < n; y++ {
		a := cnt[y]
		b := 2*k - a
		if a < b {
			res += a
		} else {
			res += b
		}
	}

	f.WriteInt(res)
}


package main

import "fmt"

func main() {
    var t,n,p int // variabel t, n, p adalah integer
    fmt.Scan(&t) // meminta input t
    for ; t > 0; t-- { // lakukan selama kondisi ini
        fmt.Scan(&n,&p) //meminta input n dan p
        for e,a,b := 2*n+p,1,2; e > 0; e-- { // lakukan selama kondisi ini
            fmt.Println(a,b) // mencetak output
            b++ // lakukan terus selama kondisi berlaku
            if b > n { // kondisi
                a++ // lakukan terus selama kondisi berlaku
                b = a+1 // a+1 dimasukan kedalam b
            }
        }
    }
}

// 402C
package main

import (
	"fmt"
)

func main() {
	var N, K, i, j, n, m, p int
	fmt.Scan(&N)
	for K = 1; K <= N; K++ {
		fmt.Scan(&n, &p)
		m = 0
		for i = 1; i <= n; i++ {
			for j = i + 1; j <= n; j++ {
				fmt.Printf("%d %d\n", i, j)
				m++
				if m == 2*n+p {
					break
				}
			}
			if m == 2*n+p {
				break
			}
		}
	}
}

// 402C-mic
package main

import (
	"fmt"
)

func main() {
	var t int
	var a []int
	fmt.Scan(&t)
	for pi := 0; pi < t; pi++ {
		var n, p, e, x int
		x = 1
		fmt.Scan(&n, &p)
		for e < (2*n)+p {
			i := x + 1
			for e < (2*n)+p && i <= n {
				a = append(a, x)
				a = append(a, i)
				e++
				i++
			}
			x++
		}

	}
	for i := 0; i < len(a); i += 2 {
		fmt.Println(a[i], a[i+1])
	}
}

package main

import (
    "fmt"
)

const maxn = 100

var c [][]int

var n, p int

func PrintEdge(x int, y int) {
    x, y = x%n, y%n
    c[x][y], c[y][x] = 1, 1
    fmt.Printf("%d %d\n", x+1, y+1)
}

func main() {
    var cas int
    fmt.Scanf("%d\n", &cas)
    for cas != 0 {
        cas--
        fmt.Scanf("%d %d\n", &n, &p)
        c = make([][]int, n)
        for i := 0; i < n; i++ {
            c[i] = make([]int, n)
            for j := 0; j < n; j++ {
                c[i][j] = 0
            }
        }
        for i := 0; i < n; i++ {
            PrintEdge(i, i+1)
            PrintEdge(i, i+2)
        }
        for i := 0; i < n; i++ {
            for j := 0; j < n; j++ {
                if i != j && c[i][j] == 0 && p != 0 {
                    PrintEdge(i, j)
                    p--
                }
            }
        }
    }
}

/*********************************************************************************
*     File Name           :     C.go
*     Created By          :     YIMMON, yimmon.zhuang@gmail.com
*     Creation Date       :     [2014-03-17 00:20]
*     Last Modified       :     [2014-03-17 00:27]
*     Description         :
**********************************************************************************/

package main

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

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

    var t, n, p int
    t = scanner.NextInt()
    for i := 0; i < t; i++ {
        scanner.NextInts(&n, &p)
        tmp := n + n + p
    NEXT:
        for j := 1; j <= n; j++ {
            for k := j + 1; k <= n; k++ {
                fmt.Println(j, k)
                tmp--
                if tmp == 0 {
                    break NEXT
                }
            }
        }
    }
}

type Scanner struct {
    *bufio.Scanner
}

var scanner = Scanner{bufio.NewScanner(os.Stdin)}

func (self *Scanner) Next() string {
    if !self.Scan() {
        panic(self.Err())
    }
    return self.Text()
}

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

func (self *Scanner) NextInts(arg ...*int) {
    for _, p := range arg {
        *p = self.NextInt()
    }
}

package main

import "os"
import "bufio"
import "strconv"

func main(){
	var t int
	var n, p int
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	r.Scan()
	t, _ = strconv.Atoi(r.Text())
	x := 0
	for i:=0; i<t; i++ {
		r.Scan()
		n, _ = strconv.Atoi(r.Text())
		r.Scan()
		p, _ = strconv.Atoi(r.Text())
		for j:=1; j<n; j++ {
			for k:=j+1; k<=n; k++ {
				x += 1
				w.WriteString(strconv.Itoa(j)+" "+strconv.Itoa(k)+"\n")
				if x == 2 * n + p {
					x = 0
					k = n
					j = n
				}
			}
		}
		w.Flush()
	}

}

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

func main() {
	var t,n,p,e,a,b int
	out:=bufio.NewWriter(os.Stdout)
	fmt.Scan(&t)
	for t=t;t>0;t-- {
		fmt.Scan(&n,&p)
		e=2*n+p
		a=1
		b=1
		for e=e;e>0;e-- {
			b++
			if b>n {
				a++
				b=a+1
			}
			out.WriteString(strconv.Itoa(a)+" "+strconv.Itoa(b)+"\n")
		}
	}
	out.Flush()
}

package main

import "fmt"

func main() {
    var t,n,p int
    fmt.Scan(&t)
    for ; t > 0; t-- {
        fmt.Scan(&n,&p)
        for e,a,b := 2*n+p,1,2; e > 0; e-- {
            fmt.Println(a,b)
            b++
            if b > n {
                a++
                b = a+1
            }
        }
    }
}

// 402C
package main

import (
	"fmt"
)

func main() {
	var a, n, m int
	fmt.Scan(&a)
	for ; ; a-- {
		fmt.Scan(&n, &m)
		x := 1
		y := 2
		e := 2*n + m
		for ; ; e-- {
			fmt.Println(x, y)
			y++
			if y > n {
				x++
			}
			y = x + 1
			return
		}
	}
}

// 402C-mic
package main

import (
    "fmt"
)

func main() {
    var t, n, p int
    a := 1
    fmt.Scan(&t)
    for ; t > 0; t-- {
        fmt.Scan(&n, &p)
        for i := 1; i <= n && a <= 2*n+p; i++ {
            for j := i + 1; j <= n && a <= 2*n+p; j++ {
                fmt.Println(i, j)
                a++
            }
        }
    }
}

// 402C-mic
package main

import (
	"fmt"
)

func main() {
	var t, n, p, a, i, j int32
	a = 1
	fmt.Scan(&t)
	for ; t > 0; t-- {
		fmt.Scan(&n, &p)
		for i = 1; i <= n && a <= 2*n+p; i++ {
			for j = i + 1; j <= n && a <= 2*n+p; j++ {
				fmt.Println(i, j)
				a++
			}
		}
	}
}