Suppose $$$a_1, a_2, \dots, a_n$$$ is a sorted integer sequence of length $$$n$$$ such that $$$a_1 \leq a_2 \leq \dots \leq a_n$$$. For every $$$1 \leq i \leq n$$$, the prefix sum $$$s_i$$$ of the first $$$i$$$ terms $$$a_1, a_2, \dots, a_i$$$ is defined by $$$$$$ s_i = \sum_{k=1}^i a_k = a_1 + a_2 + \dots + a_i. $$$$$$Now you are given the last $$$k$$$ terms of the prefix sums, which are $$$s_{n-k+1}, \dots, s_{n-1}, s_{n}$$$. Your task is to determine whether this is possible. Formally, given $$$k$$$ integers $$$s_{n-k+1}, \dots, s_{n-1}, s_{n}$$$, the task is to check whether there is a sequence $$$a_1, a_2, \dots, a_n$$$ such that   $$$a_1 \leq a_2 \leq \dots \leq a_n$$$, and  $$$s_i = a_1 + a_2 + \dots + a_i$$$ for all $$$n-k+1 \leq i \leq n$$$.

Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) — the number of test cases. The following lines contain the description of each test case. The first line of each test case contains two integers $$$n$$$ ($$$1 \leq n \leq 10^5$$$) and $$$k$$$ ($$$1 \leq k \leq n$$$), indicating the length of the sequence $$$a$$$ and the number of terms of prefix sums, respectively. The second line of each test case contains $$$k$$$ integers $$$s_{n-k+1}, \dots, s_{n-1}, s_{n}$$$ ($$$-10^9 \leq s_i \leq 10^9$$$ for every $$$n-k+1 \leq i \leq n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, output "YES" (without quotes) if it is possible and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).

NoteIn the first test case, we have the only sequence $$$a = [1, 1, 1, 1, 1]$$$.In the second test case, we can choose, for example, $$$a = [-3, -2, -1, 0, 1, 2, 3]$$$.In the third test case, the prefix sums define the only sequence $$$a = [2, 1, 1]$$$, but it is not sorted. In the fourth test case, it can be shown that there is no sequence with the given prefix sums.

package main

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

// https://codeforces.com/problemset/problem/1738/B
func P1738b(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

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

	for iCase := 0; iCase < numCases; iCase++ {
		//fmt.Printf("### Test #%d\n", iCase)
		var n int
		fmt.Fscan(in, &n)
		var k int
		fmt.Fscan(in, &k)

		var e int
		fmt.Fscan(in, &e)
		var minNext int
		if k == n {
			minNext = e
		} else {
			var idx = n - k + 1
			//fmt.Printf("### e=%d, idx=%d\n", e, idx)
			if e%idx == 0 {
				minNext = e / idx
			} else {
				minNext = int(math.Ceil(float64(e) / float64(idx)))
			}
		}
		//fmt.Printf("### MinNext after 1: %d\n", minNext)
		var prevSum = e

		var iK int = 1
		var res = "Yes"
		for iK < k {
			iK++
			fmt.Fscan(in, &e)
			var elt = e - prevSum
			if elt < minNext {
				res = "No"
				break
			}
			minNext = elt
			prevSum = e
		}
		fmt.Fprintf(out, "%s\n", res)
		for iK < k {
			var _e int
			fmt.Fscan(in, &_e)
			iK++
		}
	}
}

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

package main

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

const cf = true

func solve() {
	var n, k int
	fmt.Fscan(in, &n, &k)
	arr := make([]int, k)
	for i := range arr {
		fmt.Fscan(in, &arr[i])
	}
	if n == 1 || k == 1 {
		fmt.Fprintln(out, "Yes")
		return
	}
	firstSum := arr[0]
	var cur int = arr[k-1] - arr[k-2]
	for i := k - 2; i >= 1; i-- {
		diff := arr[i] - arr[i-1]
		if diff > cur {
			fmt.Fprintln(out, "No")
			return
		}
		cur = diff
	}

	cnt := n - k + 1
	sum := firstSum / cnt
	if firstSum > 0 {
		sum += min(1, firstSum%cnt)
	}
	if sum > cur {
		fmt.Fprintln(out, "No")
		return
	}
	fmt.Fprintln(out, "Yes")
}

func main() {
	defer out.Flush()
	t := 1
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		solve()
	}
}

func ConvertInt(val string, base, toBase int) (string, error) {
	i, err := strconv.ParseInt(val, base, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(i, toBase), nil
}

type set[T Ordered] struct {
	itms map[T]struct{}
}

func (s *set[T]) init() {
	if s.itms == nil {
		s.itms = make(map[T]struct{})
	}
}

func (s *set[T]) add(item T) *set[T] {
	s.init()
	s.itms[item] = exists
	return s
}

func (s *set[T]) remove(item T) *set[T] {
	s.init()
	delete(s.itms, item)
	return s
}

func (s *set[T]) has(item T) bool {
	s.init()
	_, c := s.itms[item]
	return c
}

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

func (s *set[T]) items() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	return items
}

func (s *set[T]) size() int {
	s.init()
	return len(s.itms)
}

var exists = struct{}{}

const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1

func reverseStr(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

var (
	in  = getReader()
	out = getWriter()
)

func getReader() *bufio.Reader {
	if cf {
		return bufio.NewReader(os.Stdin)
	} else {
		in, _ := os.Open("input.txt")
		return bufio.NewReader(in)
	}
}

func getWriter() *bufio.Writer {
	if cf {
		return bufio.NewWriter(os.Stdout)
	} else {
		out, _ := os.Create("output.txt")
		return bufio.NewWriter(out)
	}
}

package main

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

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n, k int
		fmt.Fscan(r, &n, &k)

		s := make([]int, k)
		for i := range s { fmt.Fscan(r, &s[i]) }

		res := "YES"
		if k > 1 && (n-k+1)*s[1] < (n-k+2)*s[0] {
			res = "NO"
		} else if k > 2 {
			for j := 2; j < k; j++ {
				if s[j-2] + s[j] < 2*s[j-1] {
					res = "NO"
					break
				}
			}
		}

		fmt.Fprintln(w, res)
	}

}

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n, k int
		fmt.Fscan(in, &n, &k)

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

		d := aa[0] / (n - k + 1)
		if aa[0] > 0 && d >= 0 && aa[0]%(n-k+1) != 0 {
			d++
		}

		// fmt.Println(d)
		f := true
		for i := 1; i < k; i++ {
			if aa[i]-aa[i-1] < d {
				f = false
				break
			}
			d = aa[i] - aa[i-1]
		}
		if !f {
			fmt.Fprintln(out, "No")
		} else {
			fmt.Fprintln(out, "Yes")
		}
	}
}

package main

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

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

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

func solve() {
	var n, k int
	scan(&n, &k)
	a := make([]int, k)
	for i := 0; i < k; i++ {
		scan(&a[i])
	}
	if k == 1 {
		printf("Yes\n")
		return
	}
	first := a[1] - a[0]
	last := first
	for i := 1; i < k; i++ {
		if a[i]-a[i-1] < last {
			printf("No\n")
			return
		} else {
			last = a[i] - a[i-1]
		}
	}
	s := a[0]
	m := n - k + 1
	var ave float64 = float64(s) / float64(m)
	var f float64 = float64(first)
	if ave <= f {
		printf("Yes\n")
	} else {
		printf("No\n")
		// printf("No2 %f %f\n", ave, f)
	}
	// printf("%d\n")
}
func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

// https://codeforces.com/problemset/problem/1738/B
func P1738b(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

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

	for iCase := 0; iCase < numCases; iCase++ {
		var n int
		fmt.Fscan(in, &n)
		var k int
		fmt.Fscan(in, &k)

		var e int
		fmt.Fscan(in, &e)
		var minNext int
		if k == n {
			minNext = e
		} else {
			var idx = n - k + 1
			if e%idx == 0 {
				minNext = e / idx
			} else {
				minNext = e/idx + 1
			}
		}
		var prevSum = e

		var iK int = 1
		var res = "Yes"
		for iK < k {
			iK++
			fmt.Fscan(in, &e)
			var elt = e - prevSum
			if elt < minNext {
				res = "No"
				break
			}
			minNext = elt
			prevSum = e
		}
		fmt.Fprintf(out, "%s\n", res)
		for iK < k {
			var _e int
			fmt.Fscan(in, &_e)
			iK++
		}
	}
}

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

package main

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

// https://codeforces.com/problemset/problem/1738/B
func P1738b(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

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

	for iCase := 0; iCase < numCases; iCase++ {
		var n int
		fmt.Fscan(in, &n)
		var k int
		fmt.Fscan(in, &k)

		var e int
		fmt.Fscan(in, &e)
		var minNext int
		if k == n {
			minNext = e
		} else {
			var idx = n - k
			if e%idx == 0 {
				minNext = e / idx
			} else {
				minNext = e/idx + 1
			}
		}
		var prevSum = e

		var iK int = 1
		var res = "Yes"
		for iK < k {
			iK++
			fmt.Fscan(in, &e)
			var elt = e - prevSum
			if elt < minNext {
				res = "No"
				break
			}
			minNext = elt
			prevSum = e
		}
		fmt.Fprintf(out, "%s\n", res)
		for iK < k {
			var _e int
			fmt.Fscan(in, &_e)
			iK++
		}
	}
}

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

package main

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

// https://codeforces.com/problemset/problem/1738/B
func P1738b(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

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

	for iCase := 0; iCase < numCases; iCase++ {
		var n int
		fmt.Fscan(in, &n)
		var k int
		fmt.Fscan(in, &k)

		var e int
		fmt.Fscan(in, &e)
		var minNext int
		if k == n {
			minNext = e
		} else {
			var idx = n - k
			if e%idx == 0 {
				minNext = e / idx
			} else {
				minNext = e/idx + 1
			}
		}
		var prevSum = e

		var iK int = 1
		var res = "Yes"
		for iK < k {
			iK++
			fmt.Fscan(in, &e)
			fmt.Printf("%d\n", e)
			var elt = e - prevSum
			if elt < minNext {
				res = "No"
				break
			}
			minNext = elt
			prevSum = e
		}
		fmt.Fprintf(out, "%s\n", res)
		for iK < k {
			var _e int
			fmt.Fscan(in, &_e)
			iK++
		}
	}
}

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

package main

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

// https://codeforces.com/problemset/problem/1738/B
func P1738b(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

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

	for iCase := 0; iCase < numCases; iCase++ {
		var n int
		fmt.Fscan(in, &n)
		var k int
		fmt.Fscan(in, &k)

		var e int
		fmt.Fscan(in, &e)
		var minNext int
		if k == n {
			minNext = e
		} else {
			var idx = n - k
			if e%idx == 0 {
				minNext = e / idx
			} else {
				minNext = e/idx + 1
			}
		}
		var prevSum = e

		var iK int = 1
		var res = "Yes"
		for iK < k {
			iK++
			fmt.Fscan(in, &e)
			fmt.Printf("%d\n", e)
			var elt = e - prevSum
			if elt < minNext {
				res = "No"
				break
			}
			minNext = elt
			prevSum = e
		}
		fmt.Fprintf(out, "%s\n", res)
		for iK < k {
			var _e int
			fmt.Fscan(in, &_e)
			fmt.Printf("%d\n", _e)
			iK++
		}
	}
}

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

package main

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

// https://codeforces.com/problemset/problem/1738/B
func P1738b(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

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

	for iCase := 0; iCase < numCases; iCase++ {
		var n int
		fmt.Fscan(in, &n)
		var k int
		fmt.Fscan(in, &k)

		var e int
		fmt.Fscan(in, &e)
		var minNext int
		if k == n {
			minNext = e
		} else {
			var idx = n - k
			if e%idx == 0 {
				minNext = e / idx
			} else {
				minNext = e/idx + 1
			}
		}
		var prevSum = e

		var iK int = 1
		var res = "Yes"
		for iK < k {
			iK++
			fmt.Fscan(in, &e)
			var elt = e - prevSum
			if elt < minNext {
				res = "No"
				break
			}
			minNext = elt
			prevSum = e
		}
		fmt.Fprintf(out, "%s\n", res)
		for ; iK < k; iK++ {
			var _e int
			fmt.Fscan(in, &_e)
			iK++
		}
	}
}

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

package main

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

// https://codeforces.com/problemset/problem/1738/B
func P1738b(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

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

	for iCase := 0; iCase < numCases; iCase++ {
		var n int
		fmt.Fscan(in, &n)
		var k int
		fmt.Fscan(in, &k)

		var e int
		fmt.Fscan(in, &e)
		var minNext int
		if k == n {
			minNext = e
		} else {
			var idx = n - k
			if e%idx == 0 {
				minNext = e / idx
			} else {
				minNext = e/idx + 1
			}
		}
		var prevSum = e

		var iK int
		var res = "Yes"
		for iK = 1; iK < k; iK++ {
			fmt.Fscan(in, &e)
			var elt = e - prevSum
			if elt < minNext {
				res = "No"
				iK++
				break
			}
			minNext = elt
			prevSum = e
		}
		fmt.Fprintf(out, "%s\n", res)
		for ; iK < k; iK++ {
			var _e int
			fmt.Fscan(in, &_e)
			iK++
		}
	}
}

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

package main

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

const cf = true

func solve() {
	var n, k int
	fmt.Fscan(in, &n, &k)
	arr := make([]int, k)
	for i := range arr {
		fmt.Fscan(in, &arr[i])
	}
	if n == 1 || k == 1 {
		fmt.Fprintln(out, "Yes")
		return
	}
	firstSum := arr[0]
	var cur int = arr[k-1] - arr[k-2]
	for i := k - 2; i >= 1; i-- {
		diff := arr[i] - arr[i-1]
		if diff > cur {
			fmt.Fprintln(out, "No")
			return
		}
		cur = diff
	}

	cnt := n - k + 1
	if firstSum < 0 && -firstSum < cnt {
		fmt.Fprintln(out, "No")
		return
	}
	fmt.Fprintln(out, "Yes")
}

func main() {
	defer out.Flush()
	t := 1
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		solve()
	}
}

func ConvertInt(val string, base, toBase int) (string, error) {
	i, err := strconv.ParseInt(val, base, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(i, toBase), nil
}

type set[T Ordered] struct {
	itms map[T]struct{}
}

func (s *set[T]) init() {
	if s.itms == nil {
		s.itms = make(map[T]struct{})
	}
}

func (s *set[T]) add(item T) *set[T] {
	s.init()
	s.itms[item] = exists
	return s
}

func (s *set[T]) remove(item T) *set[T] {
	s.init()
	delete(s.itms, item)
	return s
}

func (s *set[T]) has(item T) bool {
	s.init()
	_, c := s.itms[item]
	return c
}

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

func (s *set[T]) items() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	return items
}

func (s *set[T]) size() int {
	s.init()
	return len(s.itms)
}

var exists = struct{}{}

const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1

func reverseStr(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

var (
	in  = getReader()
	out = getWriter()
)

func getReader() *bufio.Reader {
	if cf {
		return bufio.NewReader(os.Stdin)
	} else {
		in, _ := os.Open("input.txt")
		return bufio.NewReader(in)
	}
}

func getWriter() *bufio.Writer {
	if cf {
		return bufio.NewWriter(os.Stdout)
	} else {
		out, _ := os.Create("output.txt")
		return bufio.NewWriter(out)
	}
}

package main

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

const cf = true

func solve() {
	var n, k int
	fmt.Fscan(in, &n, &k)
	arr := make([]int, k)
	for i := range arr {
		fmt.Fscan(in, &arr[i])
	}
	if n == 1 || k == 1 {
		fmt.Fprintln(out, "Yes")
		return
	}
	firstSum := arr[0]
	var cur int = arr[k-1] - arr[k-2]
	for i := k - 2; i >= 1; i-- {
		diff := arr[i] - arr[i-1]
		if diff > cur {
			fmt.Fprintln(out, "No")
			return
		}
		cur = diff
	}

	cnt := n - k + 1
	firstSum = firstSum / cnt
	if firstSum < 0 {
		firstSum += max(-1, firstSum%cnt)
	}
	if firstSum > 0 {
		firstSum += min(1, firstSum%cnt)
	}
	if firstSum > cur {
		fmt.Fprintln(out, "No")
		return
	}
	fmt.Fprintln(out, "Yes")
}

func main() {
	defer out.Flush()
	t := 1
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		solve()
	}
}

func ConvertInt(val string, base, toBase int) (string, error) {
	i, err := strconv.ParseInt(val, base, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(i, toBase), nil
}

type set[T Ordered] struct {
	itms map[T]struct{}
}

func (s *set[T]) init() {
	if s.itms == nil {
		s.itms = make(map[T]struct{})
	}
}

func (s *set[T]) add(item T) *set[T] {
	s.init()
	s.itms[item] = exists
	return s
}

func (s *set[T]) remove(item T) *set[T] {
	s.init()
	delete(s.itms, item)
	return s
}

func (s *set[T]) has(item T) bool {
	s.init()
	_, c := s.itms[item]
	return c
}

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

func (s *set[T]) items() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	return items
}

func (s *set[T]) size() int {
	s.init()
	return len(s.itms)
}

var exists = struct{}{}

const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1

func reverseStr(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

var (
	in  = getReader()
	out = getWriter()
)

func getReader() *bufio.Reader {
	if cf {
		return bufio.NewReader(os.Stdin)
	} else {
		in, _ := os.Open("input.txt")
		return bufio.NewReader(in)
	}
}

func getWriter() *bufio.Writer {
	if cf {
		return bufio.NewWriter(os.Stdout)
	} else {
		out, _ := os.Create("output.txt")
		return bufio.NewWriter(out)
	}
}

package main

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

const cf = true

func solve() {
	var n, k int
	fmt.Fscan(in, &n, &k)
	arr := make([]int, k)
	for i := range arr {
		fmt.Fscan(in, &arr[i])
	}
	if n == 1 {
		fmt.Fprintln(out, "Yes")
		return
	}
	firstSum := arr[0]
	var cur int = 1e12
	if k > 1 {
		cur = arr[k-1] - arr[k-2]
		for i := k - 2; i >= 1; i-- {
			diff := arr[i] - arr[i-1]
			if diff > cur {
				fmt.Fprintln(out, "No")
				return
			}
			cur = diff
		}
	}
	cnt := n - k
	if cnt != 0 {
		firstSum = firstSum / cnt
		if firstSum < 0 {
			firstSum += max(-1, firstSum%cnt)
		}
		if firstSum > 0 {
			firstSum += min(1, firstSum%cnt)
		}
	}
	if k > 1 {
		if firstSum > cur {
			fmt.Fprintln(out, "No")
			return
		}
	}
	fmt.Fprintln(out, "Yes")
}

func main() {
	defer out.Flush()
	t := 1
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		solve()
	}
}

func ConvertInt(val string, base, toBase int) (string, error) {
	i, err := strconv.ParseInt(val, base, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(i, toBase), nil
}

type set[T Ordered] struct {
	itms map[T]struct{}
}

func (s *set[T]) init() {
	if s.itms == nil {
		s.itms = make(map[T]struct{})
	}
}

func (s *set[T]) add(item T) *set[T] {
	s.init()
	s.itms[item] = exists
	return s
}

func (s *set[T]) remove(item T) *set[T] {
	s.init()
	delete(s.itms, item)
	return s
}

func (s *set[T]) has(item T) bool {
	s.init()
	_, c := s.itms[item]
	return c
}

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

func (s *set[T]) items() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	return items
}

func (s *set[T]) size() int {
	s.init()
	return len(s.itms)
}

var exists = struct{}{}

const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1

func reverseStr(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

var (
	in  = getReader()
	out = getWriter()
)

func getReader() *bufio.Reader {
	if cf {
		return bufio.NewReader(os.Stdin)
	} else {
		in, _ := os.Open("input.txt")
		return bufio.NewReader(in)
	}
}

func getWriter() *bufio.Writer {
	if cf {
		return bufio.NewWriter(os.Stdout)
	} else {
		out, _ := os.Create("output.txt")
		return bufio.NewWriter(out)
	}
}

package main

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

const cf = true

func solve() {
	var n, k int
	fmt.Fscan(in, &n, &k)
	arr := make([]int, k)
	for i := range arr {
		fmt.Fscan(in, &arr[i])
	}
	firstSum := arr[0]
	var cur int = 1e12
	if k > 1 {
		cur = arr[k-1] - arr[k-2]
		for i := k - 2; i >= 1; i-- {
			diff := arr[i] - arr[i-1]
			if diff > cur {
				fmt.Fprintln(out, "No")
				return
			}
			cur = diff
		}
	}
	cnt := n - k
	if cnt != 0 {
		firstSum = firstSum/cnt + min(1, firstSum%cnt)
	}
	if k > 1 {
		if firstSum > cur {
			fmt.Fprintln(out, "No")
			return
		}
	}
	fmt.Fprintln(out, "Yes")
}

func main() {
	defer out.Flush()
	t := 1
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		solve()
	}
}

func ConvertInt(val string, base, toBase int) (string, error) {
	i, err := strconv.ParseInt(val, base, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(i, toBase), nil
}

type set[T Ordered] struct {
	itms map[T]struct{}
}

func (s *set[T]) init() {
	if s.itms == nil {
		s.itms = make(map[T]struct{})
	}
}

func (s *set[T]) add(item T) *set[T] {
	s.init()
	s.itms[item] = exists
	return s
}

func (s *set[T]) remove(item T) *set[T] {
	s.init()
	delete(s.itms, item)
	return s
}

func (s *set[T]) has(item T) bool {
	s.init()
	_, c := s.itms[item]
	return c
}

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

func (s *set[T]) items() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	return items
}

func (s *set[T]) size() int {
	s.init()
	return len(s.itms)
}

var exists = struct{}{}

const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1

func reverseStr(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

var (
	in  = getReader()
	out = getWriter()
)

func getReader() *bufio.Reader {
	if cf {
		return bufio.NewReader(os.Stdin)
	} else {
		in, _ := os.Open("input.txt")
		return bufio.NewReader(in)
	}
}

func getWriter() *bufio.Writer {
	if cf {
		return bufio.NewWriter(os.Stdout)
	} else {
		out, _ := os.Create("output.txt")
		return bufio.NewWriter(out)
	}
}


You are given a string $$$s[1 \dots n]$$$ consisting of lowercase Latin letters. It is guaranteed that $$$n = 2^k$$$ for some integer $$$k \ge 0$$$.The string $$$s[1 \dots n]$$$ is called $$$c$$$-good if at least one of the following three conditions is satisfied:  The length of $$$s$$$ is $$$1$$$, and it consists of the character $$$c$$$ (i.e. $$$s_1=c$$$); The length of $$$s$$$ is greater than $$$1$$$, the first half of the string consists of only the character $$$c$$$ (i.e. $$$s_1=s_2=\dots=s_{\frac{n}{2}}=c$$$) and the second half of the string (i.e. the string $$$s_{\frac{n}{2} + 1}s_{\frac{n}{2} + 2} \dots s_n$$$) is a $$$(c+1)$$$-good string;  The length of $$$s$$$ is greater than $$$1$$$, the second half of the string consists of only the character $$$c$$$ (i.e. $$$s_{\frac{n}{2} + 1}=s_{\frac{n}{2} + 2}=\dots=s_n=c$$$) and the first half of the string (i.e. the string $$$s_1s_2 \dots s_{\frac{n}{2}}$$$) is a $$$(c+1)$$$-good string. For example: "aabc" is 'a'-good, "ffgheeee" is 'e'-good.In one move, you can choose one index $$$i$$$ from $$$1$$$ to $$$n$$$ and replace $$$s_i$$$ with any lowercase Latin letter (any character from 'a' to 'z').Your task is to find the minimum number of moves required to obtain an 'a'-good string from $$$s$$$ (i.e. $$$c$$$-good string for $$$c=$$$ 'a'). It is guaranteed that the answer always exists.You have to answer $$$t$$$ independent test cases.Another example of an 'a'-good string is as follows. Consider the string $$$s = $$$"cdbbaaaa". It is an 'a'-good string, because:  the second half of the string ("aaaa") consists of only the character 'a';  the first half of the string ("cdbb") is 'b'-good string, because:   the second half of the string ("bb") consists of only the character 'b';  the first half of the string ("cd") is 'c'-good string, because:   the first half of the string ("c") consists of only the character 'c';  the second half of the string ("d") is 'd'-good string.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 131~072$$$) — the length of $$$s$$$. It is guaranteed that $$$n = 2^k$$$ for some integer $$$k \ge 0$$$. The second line of the test case contains the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).

For each test case, print the answer — the minimum number of moves required to obtain an 'a'-good string from $$$s$$$ (i.e. $$$c$$$-good string with $$$c =$$$ 'a'). It is guaranteed that the answer exists.

6
8
bbdcaaaa
8
asdfghjk
8
ceaaaabb
8
bbaaddcc
1
z
2
ac

package main

import (
	"fmt"
)
// https://codeforces.com/contest/1367/submission/84588408

const abc = "abcdefghijklmnopqrstuvwxyz"

func toCharStrConst(i int) string {
    return abc[i : i+1]
}

func rp(s string, c int) (int){
	n:=len(s)
	var ans int
	d:=toCharStrConst(c)
	for i:=0;i<n;i++ {
		if s[i:i+1]!=d {
			ans++
		}
	}
	// fmt.Println(s,d,ans)
	return ans
}

func solve(s string , c int) (int){
	n:=len(s)
	if n==1 {
		return rp(s,c)
	}
	a,b:= rp(s[:n/2],c) + solve(s[n/2:],c+1) , rp(s[n/2:],c) + solve(s[:n/2],c+1)
	if a>b {
		return b
	} else {
		return a
	}
}

func main(){
	//r := bufio.NewReader(os.Stdin)
	//w := bufio.NewWriter(os.Stdout)
	var t int
	fmt.Scan(&t)
	for ; t > 0 ; t--{
		var s string
		var n int
		fmt.Scan(&n)
		fmt.Scan(&s)
		fmt.Println(solve(s,0))
	}
}

package main

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

const lg = 17

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

	for t := io.nextInt(); t > 0; t-- {
		n := io.nextInt()
		s := io.next()
		a := make([]int, n+1)
		for i, c := range s {
			a[i+1] = int(c) - 'a'
		}
		cnts := make([][]int, lg)
		for c := 0; c < lg; c++ {
			cnts[c] = make([]int, n+1)
			for i := 1; i <= n; i++ {
				cnts[c][i] = cnts[c][i-1]
				if a[i] == c {
					cnts[c][i]++
				}
			}
		}

		cost := func(l, r, c int) int {
			return r - l + 1 - (cnts[c][r] - cnts[c][l-1])
		}

		var rec func(l, r, c int) int
		rec = func(l, r, c int) int {
			if l == r {
				if a[l] == c {
					return 0
				}
				return 1
			}
			m := (l + r) / 2
			ans1 := cost(l, m, c) + rec(m+1, r, c+1)
			ans2 := rec(l, m, c+1) + cost(m+1, r, c)
			if ans1 < ans2 {
				return ans1
			}
			return ans2
		}

		ans := rec(1, n, 0)
		io.println(ans)
	}
}

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

func newFastIO(r io.Reader, w io.Writer) fastIO {
	return fastIO{bufio.NewReader(r), bufio.NewWriter(w), make([]byte, 1)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p)
	for !('0' <= f.p[0] && f.p[0] <= '9') {
		f.Read(f.p)
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p)
	}
	return n
}

func (f *fastIO) next() string {
	f.Read(f.p)
	for f.p[0] <= ' ' {
		f.Read(f.p)
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p)
	}
	return string(s)
}

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

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

// https://codeforces.com/contest/x/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input *bufio.Reader

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

func main() {
	var T int

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscanln(input, &T)
	for i := 0; i < T; i++ {
		solver()
	}
}

func solver() {
	var (
		n int
		s string
	)
	fmt.Fscanln(input, &n)
	fmt.Fscanln(input, &s)
	if n == 1 {
		if s == "a" {
			fmt.Println(0)
		} else {
			fmt.Println(1)
		}
		return
	}
	fmt.Println(calc(s, 'a'))
}

func calc(s string, c byte) int {
	n := len(s) / 2
	p1, p2 := s[:n], s[n:]
	return min(rec(p1, p2, c), rec(p2, p1, c))
}

func rec(a, b string, c byte) int {
	changes := 0
	if len(a) == 1 {
		if a[0] != c {
			changes++
		}
		if b[0] != c+1 {
			changes++
		}
		return changes
	}

	for i := 0; i < len(a); i++ {
		if a[i] != c {
			changes++
		}
	}
	return changes + calc(b, c+1)
}

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

package main

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

func checkIfGood(s string, c byte) int {
	n := len(s)
	if n == 1 {
		if s[0] == c {
			return 0
		}

		return 1
	}

	changes1 := 0
	changes2 := 0

	for i := n / 2; i < n; i++ {
		if s[i] != c {
			changes1++
		}
	}

	for i := 0; i < n/2; i++ {
		if s[i] != c {
			changes2++
		}
	}

	val1 := changes1 + checkIfGood(s[0:n/2], c+1)
	val2 := changes2 + checkIfGood(s[n/2:n], c+1)

	if val1 < val2 {
		return val1
	}

	return val2
}

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

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

	inputStr := func() string {
		scanner.Scan()
		return scanner.Text()
	}

	for t := inputInt(); t > 0; t-- {
		n := inputInt()

		str := inputStr()

		fmt.Println(checkIfGood(str[0:n], 'a'))
	}
}

package main

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

func goodCorrect(str string, c byte, l, r int) int {
	if r-l == 1 {
		if str[l] == c {
			return 0
		} else {
			return 1
		}
	}

	diff1 := 0
	diff2 := 0
	for i := l; i < (l+r)/2; i++ {
		if str[i] != c {
			diff1++
		}
	}

	for i := (l + r) / 2; i < r; i++ {
		if str[i] != c {
			diff2++
		}
	}
	diff := 0
	diff1 += goodCorrect(str, c+1, (l+r)/2, r)
	diff2 += goodCorrect(str, c+1, l, (l+r)/2)

	if diff1 <= diff2 {
		diff = diff1
	} else {
		diff = diff2
	}
	return diff
}

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		var str string
		fmt.Fscan(in, &str)
		fmt.Println(goodCorrect(str, 'a', 0, n))
	}
}

package main

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

func main() {
	var cases int
	fmt.Scanf("%d\n", &cases)
	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < cases; i++ {
		readLine(reader)
		line := readLine(reader)
		fmt.Println(findMin(string(line), 'a'))
	}
}

func findMin(str string, target byte) int {
	if len(str) == 0 {
		return 0
	}
	if len(str) == 1 && str[0] == target {
		return 0
	} else if len(str) == 1 {
		return 1
	}
	l := len(str)
	left := minMatch(str[0:l/2], target) + findMin(str[l/2:], target+1)
	right := minMatch(str[l/2:], target) + findMin(str[:l/2], target+1)
	if left < right {
		return left
	}
	return right
}

func minMatch(str string, target byte) int {
	count := 0
	for i := 0; i < len(str); i++ {
		if str[i] != target {
			count++
		}
	}
	return count
}

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

package main

import "fmt"

const MaxInt = int(^uint(0) >> 1)

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

func count(low, high int, query *string, char byte) int {
	var counter int
	for i := low; i <= high; i++ {
		if (*query)[i] != char {
			counter++
		}
	}
	return counter
}

// high = n-1
// low = 0
func solve(low, high int, query *string, char byte) int {
	if low == high {
		if (*query)[low] == char {
			return 0
		} else {
			return 1
		}
	} else {
		var minCount = MaxInt
		mid := (low + high) / 2
		leftCount := solve(low, mid, query, char+1) + count(mid+1, high, query, char)
		minCount = min(leftCount, minCount)
		rightCount := solve(mid+1, high, query, char+1) + count(low, mid, query, char)
		minCount = min(rightCount, minCount)
		return minCount
	}
}

func main() {
	var TC, N int
	var query string
	fmt.Scan(&TC)

	for TC != 0 {
		fmt.Scan(&N)
		fmt.Scan(&query)

		fmt.Println(solve(0, N-1, &query, 'a'))
		TC--
	}
}

package main

import "fmt"

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

func count(low, high int, query *string, char byte) int {
	var counter int
	for i := low; i <= high; i++ {
		if (*query)[i] != char {
			counter++
		}
	}
	return counter
}

func solve(low, high int, query *string, char byte) int {
	if low == high {
		if char == (*query)[low] {
			return 0
		} else {
			return 1
		}
	} else {
		mid := (low + high) / 2
		leftRec := solve(low, mid, query, char+1) + count(mid+1, high, query, char)
		rightRec := solve(mid+1, high, query, char+1) + count(low, mid, query, char)
		return min(leftRec, rightRec)
	}
}

func main() {
	var TC int
	fmt.Scan(&TC)

	for TC != 0 {
		var n int
		var query string
		fmt.Scan(&n)
		fmt.Scan(&query)

		fmt.Println(solve(0, n-1, &query, 'a'))
		TC--
	}
}

package main

import "fmt"

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

func count(query *string, low, high int, char byte) int {
	var counter int
	for i := low; i <= high; i++ {
		if (*query)[i] != char {
			counter++
		}
	}
	return counter
}

func solve(query *string, low, high int, char byte) int {
	if low == high {
		if (*query)[low] == char {
			return 0
		} else {
			return 1
		}
	}
	mid := (low + high) / 2
	leftRec := solve(query, low, mid, char+1) + count(query, mid+1, high, char)
	rightRec := solve(query, mid+1, high, char+1) + count(query, low, mid, char)
	return min(leftRec, rightRec)
}

func main() {
	var TC int
	fmt.Scanf("%d\n", &TC)

	for TC != 0 {
		var query string
		var n int
		fmt.Scanf("%d\n", &n)
		fmt.Scanf("%s\n", &query)
		fmt.Println(solve(&query, 0, len(query)-1, 'a'))
		TC--
	}
}

package main

import "fmt"

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

func count(low, high int, char byte, query *string) int {
	var counter int
	for i := low; i <= high; i++ {
		if (*query)[i] != char {
			counter++
		}
	}
	return counter
}

func solve(low, high int, char byte, query *string) int {
	if low == high {
		if (*query)[low] == char {
			return 0
		} else {
			return 1
		}
	} else {
		mid := (low + high) / 2
		// [0-mid] is all c && [mid+1, high] is c+1 good
		left := count(low, mid, char, query) + solve(mid+1, high, char+1, query)

		// [0-mid] is c+1 good && [mid+1, high] is c+1 good
		right := count(mid+1, high, char, query) + solve(low, mid, char+1, query)
		return min(left, right)
	}
}

func main() {
	var TC int
	fmt.Scan(&TC)
	for TC != 0 {
		var n int
		var query string
		fmt.Scan(&n)
		fmt.Scan(&query)

		fmt.Println(solve(0, n-1, 'a', &query))
		TC--
	}
}

package main

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

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func f(s string, b byte) int {
	n := len(s)
	if n == 1 {
		if s[0] == b {
			return 0
		}
		return 1
	}
	n >>= 1
	return n + min(f(s[n:], b+1)-strings.Count(s[:n], string(b)), f(s[:n], b+1)-strings.Count(s[n:], string(b)))
}

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t, n, s := 0, 0, ""
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &s)
		Fprintln(out, f(s, 'a'))
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1385D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}

	var t, n int
	var s string
	var f func(s string, b byte) int
	f = func(s string, b byte) int {
		n := len(s)
		if n == 1 {
			if s[0] == b {
				return 0
			}
			return 1
		}
		n >>= 1
		return n + min(f(s[n:], b+1)-strings.Count(s[:n], string(b)), f(s[:n], b+1)-strings.Count(s[n:], string(b)))
	}
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &s)
		Fprintln(out, f(s, 'a'))
	}
}

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

package main

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

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

var s string

func calc(L, R int, c byte) int {
	if L == R {
		if s[L] == c {
			return 0
		}
		return 1
	}

	m := (L+R) >> 1
	cntL, cntR := 0, 0
	for i := L; i <= R; i++ {
		if s[i] != c {
			if i <= m {
				cntL++
			} else {
				cntR++
			}
		}
	}
	return min(calc(L, m, c+1) + cntR, calc(m+1, R, c+1) + cntL)
}

func main() {
	defer writer.Flush()
	t := NextInt()
	for t > 0 {
		t--
		n := NextInt()
		s = Next()
		Println(calc(0, n-1, 'a'))
	}
}

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

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

func Println(data interface{}) {
	writer.WriteString(fmt.Sprintf("%v\n", data))
}

func Print(data interface{}) {
	writer.WriteString(fmt.Sprintf("%v", data))
}

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

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

func NextInt64() int64 {
	i64, err := strconv.ParseInt(Next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i64
}

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

package main

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

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

func main() {
	defer out.Flush()
	var nTest int
	fmt.Fscanf(in, "%d\n", &nTest)
	for i := 0; i < nTest; i++ {
		solveCase()
	}
}

var a [26][140000]int

func calc(c, l, r int) int {
	if r-l == 1 {
		return 1 - a[c][r] + a[c][l]
	}
	m := l + ((r - l) >> 1)
	one := calc(c+1, l, m) + r - m - (a[c][r] - a[c][m])
	two := calc(c+1, m, r) + m - l - (a[c][m] - a[c][l])
	if one < two {
		return one
	}
	return two
}
func solveCase() {
	var n int
	var s string
	fmt.Fscanf(in, "%d\n%s\n", &n, &s)
	for i := 1; i <= n; i++ {
		for j := 0; j < 26; j++ {
			a[j][i] = a[j][i-1]
		}
		a[s[i-1]-'a'][i]++
	}
	fmt.Fprintf(out, "%d\n", calc(0, 0, n))
}

package main

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

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

func main() {
	//ioutil.WriteFile("input.txt", []byte(generateInput()), os.ModeExclusive)

	debug := 1
	filename := "input.txt"
	if  debug != 0 && len(os.Args)>1 && os.Args[1] == "debug" {
		file, _ := os.Open(filename)
		reader = bufio.NewReaderSize(file, 1024*1024)
	} else {
		reader = bufio.NewReaderSize(os.Stdin, 1024*1024)
	}
	writer = bufio.NewWriterSize(os.Stdout, 1024 * 1024)

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

	for _case := 1; _case <= T; _case++ {
		n := readInt()
		s := readLine()
		ans := makeGood(s, 0, n, 'a')
		fmt.Println(ans)
	}

	writer.Flush()
}

func makeGood(s string, l, r int, target byte) int {
	if r-1 == l {
		if s[l] == target {
			return 0
		}
		return 1
	}

	ans1 := 0
	mid := (l+r+1)>>1
	for i:=l; i<mid; i++ {
		if s[i]!=target{
			ans1++
		}
	}
	ans1 += makeGood(s,mid,r,target+1)

	ans2 := 0
	for i:=mid; i<r; i++ {
		if s[i]!=target{
			ans2++
		}
	}
	ans2 += makeGood(s,l,mid,target+1)

	return getMin(ans1, ans2)
}

func readInts() []int {
	line := readLine()
	fields := strings.Fields(line)
	res := make([]int, len(fields))
	for i := range fields {
		v, _ := strconv.ParseInt(fields[i], 10, 64)
		res[i] = int(v)
	}
	return res
}

func readInt() int {
	line := readLine()
	n, _ := strconv.ParseInt(line, 10, 64)
	return int(n)
}

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

	return strings.TrimRight(string(str), "\r\n")
}

func printInts(arr []int, sep string) {
	n := len(arr)
	if n == 0 { panic("printInts empty") }
	fmt.Printf("%d", arr[0])
	for i := 1; i < n; i++ {
		fmt.Printf("%s%d", sep, arr[i])
	}
}

func getMin(arr ...int) int {
	if len(arr) == 0 { panic("length arr == 0") }

	var ans = arr[0]
	for _, v := range arr {
		if v < ans { ans = v }
	}
	return ans
}

package main

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

func goodCorrect(str string, c byte, l, r int) int {
	if r-l == 1 {
		if str[l] == c {
			return 0
		} else {
			return 1
		}
	}

	diff1 := 0
	diff2 := 0
	for i := l; i < (l+r)/2; i++ {
		if str[i] != c {
			diff1++
		}
	}

	for i := (l + r) / 2; i < r; i++ {
		if str[i] != c {
			diff2++
		}
	}
	diff := 0
	if diff1 <= diff2 {
		diff = diff1
		diff += goodCorrect(str, c+1, (l+r)/2, r)
	} else {
		diff = diff2
		diff += goodCorrect(str, c+1, l, (l+r)/2)
	}

	return diff
}

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		var str string
		fmt.Fscan(in, &str)
		fmt.Println(goodCorrect(str, 'a', 0, n))
	}
}


According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check any person, i.e. learn his age and the drink he is having at the same time. What minimal number of people should Vasya check additionally to make sure that there are no clients under 18 having alcohol drinks?The list of all alcohol drinks in Berland is: ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE

The first line contains an integer n (1 ≤ n ≤ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators. Only the drinks from the list given above should be considered alcohol.

Print a single number which is the number of people Vasya should check to guarantee the law enforcement.

NoteIn the sample test the second and fifth clients should be checked.

package main

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

func main(){
	var n int
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	r.Scan()
	n, _ = strconv.Atoi(r.Text())
	tot := 0
	var temp string
	for i:=0; i<n; i++ {
		r.Scan()
		temp = r.Text()
		if string(temp) == "ABSINTH" || string(temp) == "BEER" || string(temp) == "BRANDY" || string(temp) == "CHAMPAGNE" || string(temp) == "GIN" || string(temp) == "RUM" || string(temp) == "SAKE" || string(temp) == "TEQUILA" || string(temp) == "VODKA" || string(temp) == "WHISKEY" || string(temp) == "WINE" || string(temp) == "0"|| string(temp) == "1"|| string(temp) == "2"|| string(temp) == "3"|| string(temp) == "4"|| string(temp) == "5"|| string(temp) == "6"|| string(temp) == "7"|| string(temp) == "8"|| string(temp) == "9"|| string(temp) == "10"|| string(temp) == "11"|| string(temp) == "12"|| string(temp) == "13"|| string(temp) == "14"|| string(temp) == "15"|| string(temp) == "16"|| string(temp) == "17" {
			tot += 1
		}
	}
	w.WriteString(strconv.Itoa(tot)+"\n")
	w.Flush()
}

// 56A
package main

import (
	"fmt"
	"strconv"
)

func a(b int) int {
	var n string
	var ans int
	for i := 0; i < b; i++ {
		fmt.Scan(&n)
		if n == "ABSINTH" || n == "BEER" || n == "BRANDY" || n == "CHAMPAGNE" || n == "GIN" || n == "RUM" || n == "SAKE" || n == "TEQUILA" || n == "VODKA" || n == "WHISKEY" || n == "WINE" {
			ans++
		} else if n[0] == '1' || n[0] == '2' || n[0] == '3' || n[0] == '4' || n[0] == '5' || n[0] == '6' || n[0] == '7' || n[0] == '8' || n[0] == '9' || n[0] == '0' {
			c, _ := strconv.Atoi(n)
			if c < 18 {
				ans++
			}
		}
	}
	return ans
}

func main() {
	var n int
	fmt.Scan(&n)
	fmt.Println(a(n))
}

// 56A
package main

import (
	"fmt"
	"strconv"
)

func main() {
	var n int
	var y int
	var c int = 0
	var x string
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&x)
		y, _ = strconv.Atoi(x)
		if y < 18 && y != 0 || x == "0" || x == "ABSINTH" || x == "BEER" || x == "BRANDY" || x == "CHAMPAGNE" || x == "GIN" || x == "RUM" || x == "SAKE" || x == "TEQUILA" || x == "VODKA" || x == "WHISKEY" || x == "WINE" {
			c++
		}
	}
	fmt.Println(c)
}

// 56A
package main

import (
	"fmt"
	"strconv"
)

func main() {
	var n, y int
	var c int = 0
	var x string
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&x)
		y, _ = strconv.Atoi(x)
		if y < 19 && x != "ABSINTH" && x != "BEER" && x != "BRANDY" && x != "CHAMPAGNE" && x != "GIN" && x != "RUM" && x != "SAKE" && x != "TEQUILA" && x != "VODKA" && x != "WHISKEY" && x != "WINE" {
			c++
		}
	}
	fmt.Println(c)
}

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var n int
	var y int
	var c int = 0
	var x string
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&x)
		y, _ = strconv.Atoi(x)
		if y < 18 && y != 0 && len(x) < 101 || x == "ABSINTH" || x == "BEER" || x == "BRANDY" || x == "CHAMPAGNE" || x == "GIN" || x == "RUM" || x == "SAKE" || x == "TEQUILA" || x == "VODKA" || x == "WHISKEY" || x == "WINE" {
			c++
		}
	}
	fmt.Println(c)
}

// 56A
package main

import (
	"fmt"
	"strconv"
)

func main() {
	var n int
	var y int
	var c int = 0
	var x string
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&x)
		y, _ = strconv.Atoi(x)
		if y < 18 && y != 0 || x == "ABSINTH" || x == "BEER" || x == "BRANDY" || x == "CHAMPAGNE" || x == "GIN" || x == "RUM" || x == "SAKE" || x == "TEQUILA" || x == "VODKA" || x == "WHISKEY" || x == "WINE" {
			c++
		}
	}
	fmt.Println(c)
}

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var n int
	var y int
	var c int = 0
	var x string
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&x)
		y, _ = strconv.Atoi(x)
		if y < 18 || x == "ABSINTH" || x == "BEER" || x == "BRANDY" || x == "CHAMPAGNE" || x == "GIN" || x == "RUM" || x == "SAKE" || x == "TEQUILA" || x == "VODKA" || x == "WHISKEY" || x == "WINE" {
			c++
		}
	}
	fmt.Println(c)
}

// 56A
package main

import (
	"fmt"
	"strconv"
)

func main() {
	var n, y int
	var c int = 0
	var x string
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&x)
		y, _ = strconv.Atoi(x)
		if y < 18 && x != "ABSINTH" && x != "BEER" && x != "BRANDY" && x != "CHAMPAGNE" && x != "GIN" && x != "RUM" && x != "SAKE" && x != "TEQUILA" && x != "VODKA" && x != "WHISKEY" && x != "WINE" {
			c++
		}
	}
	fmt.Println(c)
}

// 56A
package main

import (
	"fmt"
	"strconv"
)

func main() {
	var n int
	var y int
	var c int = 0
	var x string
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&x)
		y, _ = strconv.Atoi(x)
		if y < 18 && y != 0 && len(x) < 100 || x == "ABSINTH" || x == "BEER" || x == "BRANDY" || x == "CHAMPAGNE" || x == "GIN" || x == "RUM" || x == "SAKE" || x == "TEQUILA" || x == "VODKA" || x == "WHISKEY" || x == "WINE" {
			c++
		}
	}
	fmt.Println(c)
}

// 56A
package main

import (
	"fmt"
	"strconv"
)

func main() {
	var n, y int
	var c int = 0
	var x string
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&x)
		y, _ = strconv.Atoi(x)
		if y < 18 && x != "ABSINTH" && x != "BEER" && x != "BRANDY" && x != "CHAMPAGNE" && x != "SAKE" && x != "TEQUILA" && x != "VODKA" && x != "WINE" {
			c++
		}
	}
	fmt.Println(c)
}

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var n int
	var y int
	var c int = 0
	var x string
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&x)
		y, _ = strconv.Atoi(x)
		if y < 18 && y > 0 && len(x) < 101 || x == "ABSINTH" || x == "BEER" || x == "BRANDY" || x == "CHAMPAGNE" || x == "GIN" || x == "RUM" || x == "SAKE" || x == "TEQUILA" || x == "VODKA" || x == "WHISKEY" || x == "WINE" {
			c++
		}
	}
	fmt.Println(c)
}

// 56A
package main

import (
	"fmt"
	"strconv"
)

func main() {
	var n, y int
	var c int = 0
	var x string
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&x)
		y, _ = strconv.Atoi(x)
		if y < 18 && x != "ABSINTH" && x != "BEER" && x != "BRANDY" && x != "CHAMPAGNE" && x != "RUM" && x != "SAKE" && x != "TEQUILA" && x != "VODKA" && x != "WHISKEY" && x != "WINE" {
			c++
		}
	}
	fmt.Println(c)
}

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var n int
	var y int
	var c int = 0
	var x string
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&x)
		y, _ = strconv.Atoi(x)
		if y < 18 && y != 0 && len(x) < 101 || x == "ABSINTH" || x == "BEER" || x == "BRANDY" || x == "CHAMPAGNE" || x == "GIN" || x == "RUM" || x == "SAKE" || x == "TEQUILA" || x == "VODKA" || x == "WHISKEY" || x == "WINE" {
			c++
			fmt.Println(len(x))
		}
	}
	fmt.Println(c)
}

// 56A
package main

import (
	"fmt"
	"strconv"
)

func main() {
	var n, y int
	var c int = 0
	var x string
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&x)
		y, _ = strconv.Atoi(x)
		if y < 18 && x != "ABSINTH" && x != "BEER" && x != "BRANDY" && x != "CHAMPAGNE" && x != "RUM" && x != "SAKE" && x != "TEQUILA" && x != "VODKA" && x != "WINE" {
			c++
		}
	}
	fmt.Println(c)
}


The new generation external memory contains an array of integers $$$a[1 \ldots n] = [a_1, a_2, \ldots, a_n]$$$.This type of memory does not support changing the value of an arbitrary element. Instead, it allows you to cut out any segment of the given array, cyclically shift (rotate) it by any offset and insert it back into the same place.Technically, each cyclic shift consists of two consecutive actions:   You may select arbitrary indices $$$l$$$ and $$$r$$$ ($$$1 \le l < r \le n$$$) as the boundaries of the segment.  Then you replace the segment $$$a[l \ldots r]$$$ with it's cyclic shift to the left by an arbitrary offset $$$d$$$. The concept of a cyclic shift can be also explained by following relations: the sequence $$$[1, 4, 1, 3]$$$ is a cyclic shift of the sequence $$$[3, 1, 4, 1]$$$ to the left by the offset $$$1$$$ and the sequence $$$[4, 1, 3, 1]$$$ is a cyclic shift of the sequence $$$[3, 1, 4, 1]$$$ to the left by the offset $$$2$$$. For example, if $$$a = [1, \color{blue}{3, 2, 8}, 5]$$$, then choosing $$$l = 2$$$, $$$r = 4$$$ and $$$d = 2$$$ yields a segment $$$a[2 \ldots 4] = [3, 2, 8]$$$. This segment is then shifted by the offset $$$d = 2$$$ to the left, and you get a segment $$$[8, 3, 2]$$$ which then takes the place of of the original elements of the segment. In the end you get $$$a = [1, \color{blue}{8, 3, 2}, 5]$$$.Sort the given array $$$a$$$ using no more than $$$n$$$ cyclic shifts of any of its segments. Note that you don't need to minimize the number of cyclic shifts. Any method that requires $$$n$$$ or less cyclic shifts will be accepted.

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain the descriptions of the test cases.  The first line of each test case description contains an integer $$$n$$$ ($$$2 \leq n \leq 50$$$) — the length of the array. The second line consists of space-separated elements of the array $$$a_i$$$ ($$$-10^9 \leq a_i \leq 10^9$$$). Elements of array $$$a$$$ may repeat and don't have to be unique.

Print $$$t$$$ answers to all input test cases.  The first line of the answer of each test case should contain an integer $$$k$$$ ($$$0 \le k \le n$$$) — the number of actions to sort the array. The next $$$k$$$ lines should contain descriptions of the actions formatted as "$$$l$$$ $$$r$$$ $$$d$$$" (without quotes) where $$$l$$$ and $$$r$$$ ($$$1 \le l < r \le n$$$) are the boundaries of the segment being shifted, while $$$d$$$ ($$$1 \le d \le r - l$$$) is the offset value. Please remember that only the cyclic shifts to the left are considered so the chosen segment will be shifted by the offset $$$d$$$ to the to the left. Note that you are not required to find the minimum number of cyclic shifts needed for sorting. Any sorting method where the number of shifts does not exceed $$$n$$$ will be accepted. If the given array $$$a$$$ is already sorted, one of the possible answers is $$$k = 0$$$ and an empty sequence of cyclic shifts. If there are several possible answers, you may print any of them.

NoteExplanation of the fourth data set in the example:   The segment $$$a[2 \ldots 4]$$$ is selected and is shifted to the left by $$$2$$$: $$$[2, \color{blue}{5, 1, 4}, 3] \longrightarrow [2, \color{blue}{4, 5, 1}, 3]$$$  The segment $$$a[1 \ldots 5]$$$ is then selected and is shifted to the left by $$$3$$$: $$$[\color{blue}{2, 4, 5, 1, 3}] \longrightarrow [\color{blue}{1, 3, 2, 4, 5}]$$$  After that the segment $$$a[1 \ldots 2]$$$ is selected and is shifted to the left by $$$1$$$: $$$[\color{blue}{1, 3}, 2, 4, 5] \longrightarrow [\color{blue}{3, 1}, 2, 4, 5]$$$  And in the end the segment $$$a[1 \ldots 3]$$$ is selected and is shifted to the left by $$$1$$$: $$$[\color{blue}{3, 1, 2}, 4, 5] \longrightarrow [\color{blue}{1, 2, 3}, 4, 5]$$$

package main

import "fmt"

type Shift struct {
	From int
	To   int
	N    int
}

func main() {
	var iterations int
	fmt.Scan(&iterations)
	for ; iterations > 0; iterations-- {
		var n int
		fmt.Scan(&n)
		var arr []int
		for ; n > 0; n-- {
			var v int
			fmt.Scan(&v)
			arr = append(arr, v)
		}

		var result []Shift
		for i := range arr {
			m := imin(arr, i)
			if m == i {
				continue
			}
			item := arr[m]
			// shift
			for j := m; j > i; j-- {
				arr[j] = arr[j-1]
			}
			arr[i] = item
			result = append(result, Shift{From: i + 1, To: m + 1, N: m - i})
		}
		fmt.Println(len(result))
		for _, x := range result {
			fmt.Printf("%d %d %d\n", x.From, x.To, x.N)
		}
	}

}

func imin(arr []int, from int) int {
	result := from
	for i := from; i < len(arr); i++ {
		if arr[i] < arr[result] {
			result = i
		}
	}
	return result
}

package main

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

var n int

var a[]int

func main() {

	r := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &n)

		a = make([]int, n)

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

		solve()
	}
}

type pair struct {
	v int
	idx int
}

type byV []*pair

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

func solve() {

	//2 4 4 1 1 5 2 4 3

	//1 2 4 4 1 5 2 4 3

	//1 1 2 4 4 5 2 4 3

	//

	start := 0

	var ans [][3]int

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

		var min int = 1e9 + 5
		var minidx int

		for j := start; j < n; j++ {
			if a[j] < min {
				min = a[j]
				minidx = j
			}
		}
		if minidx == start {
			start++
			continue
		}

		ans = append(ans, [3]int{start + 1, minidx + 1, minidx - start})

		nums := minidx - start + 1

		target := start
		elemToPut := a[target]

		a[target] = a[minidx]

		count := 0

		for ;count < nums - 1; {
			target = target - start

			p := target - minidx + start

			if p < 0 {
				p += nums
			}
			p += start
			temp := a[p]

			a[p] = elemToPut

			elemToPut = temp
			target = p

			count++
		}
		start++
	}

	fmt.Println(len(ans))

	for j := 0; j < len(ans); j++ {
		a := ans[j]
		fmt.Printf("%d %d %d", a[0], a[1], a[2])
		fmt.Println()
	}
}

package main

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

type node struct {
	val,id int
}
type sc struct {
	l,r,d int
}

func main()  {
	var t,n,s int
	in:=bufio.NewReader(os.Stdin)
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		fmt.Fscan(in,&n)
		a:=make([]node,n)
		s=0
		for i:=0;i<n;i++{
			fmt.Fscan(in,&a[i].val)
			a[i].id=i
		}
		sort.Slice(a,func(x,y int) bool {
			if a[x].val==a[y].val{
				return a[x].id<a[y].id
			}
			return a[x].val<a[y].val
		})
		cc:=[]sc{}
		for k:=0;k<n;k++{
			if a[k].id-k!=0{
				s++
				cc=append(cc,sc{k+1,a[k].id+1,a[k].id-k})
			}
			for j:=0;j<n;j++{
				if k==j{
					continue
				}
				if a[j].id<a[k].id && a[j].id>=k{
					a[j].id-=k+a[k].id-k
					if a[j].id<0{
						a[j].id+=a[k].id+1
					}
				}
			}
		}
		fmt.Println(s)
		for w:=0;w<s;w++{
			fmt.Println(cc[w].l,cc[w].r,cc[w].d)
		}
	}

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

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

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

		shifts := [][]int{}
		for i := 0; i < n; i++ {
			low := i
			for j := i + 1; j < n; j++ {
				if arr[j] < arr[low] {
					low = j
				}
			}

			if low != i {
				// misplaced
				shifts = append(shifts, []int{i + 1, low + 1, low - i})
				temp := make([]int, n)
				copy(temp, arr)
				arr[i] = arr[low]
				for x := i; x < low; x++ {
					arr[x+1] = temp[x]
				}
			}
		}

		fmt.Fprintln(out, len(shifts))
		for _, z := range shifts {
			fmt.Fprintln(out, z[0], z[1], z[2])
		}
	}
}

package main

import (
	"bytes"
	"fmt"
)

func print_shifts(slice []int) (n int, ans string) {
	n = 0
	buf := bytes.NewBufferString(ans)

	for i, val := range slice {
		j := i - 1
		for j >= 0 {
			// fmt.Println(slice[j], j)
			if val < slice[j] {
				slice[j+1] = slice[j]
				slice[j] = val
			} else {

				break
			}
			j--
		}
		l := j + 2
		r := i + 1

		if (l - r) != 0 {
			n++
			fmt.Fprintln(buf, l, r, r-l)
			// fmt.Println(l, r, r-l)
		}
	}

	ans = buf.String()
	return
}

func main() {
	var n int
	fmt.Scan(&n)

	for i := 0; i < n; i++ {
		l := 0
		fmt.Scan(&l)

		arr := make([]int, l)
		for j := 0; j < l; j++ {
			fmt.Scan(&arr[j])
		}

		n, ans := print_shifts(arr)
		fmt.Println(n)
		fmt.Print(ans)
	}
}

package main

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

type Action struct {
	left, right, shift int
}

func (a Action) String() string {
	return fmt.Sprintf("%d %d %d", a.left, a.right, a.shift)
}

var (
	reader = bufio.NewReader(os.Stdin)
	// writer  = bufio.NewWriter(os.Stdout)
	t, n int
)

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		a := make([]int, n)
		var r []Action
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		for i := 0; i < n-1; i++ {
			minp := i
			for j := i + 1; j < n; j++ {
				if a[j] < a[minp] {
					minp = j
				}
			}
			if minp > i {
				x := a[minp]
				for j := minp; j > i; j-- {
					a[j] = a[j-1]
				}
				a[i] = x
				r = append(r, Action{i + 1, minp + 1, minp - i})
			}
		}
		fmt.Println(len(r))
		for _, x := range r {
			fmt.Println(x)
		}
	}
}

package main

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

type shift struct {
	b, e, d int
}

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)

	var T int
	fmt.Fscanf(r, "%d\n", &T)

	for t := 0; t < T; t++ {
		var n int
		fmt.Fscanf(r, "%d\n", &n)
		arr := make([]int, n)
		tmp := make([]int, n)

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

		res := []shift{}
		for i := 0; i < n; i++ {
			k := i
			for j := i; j < n; j++ {
				if arr[j] < arr[k] {
					k = j
				}
			}

			//	fmt.Println(arr, "-", i, k)

			if k-i != 0 {
				d := k - i
				res = append(res, shift{i + 1, k + 1, d})
				x := arr[i : k+1]
				for j := i; j <= k; j++ {
					tmp[j] = x[(j-i+d)%(d+1)]
				}
				copy(arr[i:k+1], tmp[i:k+1])
			}
		}

		fmt.Println(len(res))
		for _, s := range res {
			fmt.Println(s.b, s.e, s.d)
		}
	}
}

package main

import "fmt"

type Shift struct {
	From int
	To   int
	N    int
}

func main() {
	var iterations int
	fmt.Scan(&iterations)
	for ; iterations > 0; iterations-- {
		var n int
		fmt.Scan(&n)
		var arr []int
		for ; n > 0; n-- {
			var v int
			fmt.Scan(&v)
			arr = append(arr, v)
		}

		var result []Shift
		for i := range arr {
			m := imin(arr, i)
			if m == i {
				continue
			}
			for j := 0; j < m-i; j++ {
				arr[i+j], arr[m-j] = arr[m-j], arr[i+j]
			}
			result = append(result, Shift{From: i + 1, To: m + 1, N: m - i})
		}
		fmt.Println(len(result))
		for _, x := range result {
			fmt.Printf("%d %d %d\n", x.From, x.To, x.N)
		}
	}

}

func imin(arr []int, from int) int {
	result := from
	for i := from; i < len(arr); i++ {
		if arr[i] < arr[result] {
			result = i
		}
	}
	return result
}

package main

import "fmt"

type Shift struct {
	From int
	To   int
	N    int
}

func main() {
	var iterations int
	fmt.Scanf("%d", &iterations)
	for ; iterations > 0; iterations-- {
		var n int
		fmt.Scanf("%d", &n)
		var arr []int
		for ; n > 0; n-- {
			var v int
			fmt.Scanf("%d", &v)
			arr = append(arr, v)
		}

		var result []Shift
		for i := range arr {
			m := imin(arr, i)
			if m == i {
				continue
			}
			for j := 0; j < m-i; j++ {
				arr[i+j], arr[m-j] = arr[m-j], arr[i+j]
			}
			result = append(result, Shift{From: i + 1, To: m + 1, N: m - i})
		}
		fmt.Println(len(result))
		for _, x := range result {
			fmt.Printf("%d %d %d\n", x.From, x.To, x.N)
		}
	}

}

func imin(arr []int, from int) int {
	result := from
	for i := from; i < len(arr); i++ {
		if arr[i] < arr[result] {
			result = i
		}
	}
	return result
}

package main

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

var n int

var a[]int

func main() {

	r := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &n)

		a = make([]int, n)

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

		solve()
	}
}

type pair struct {
	v int
	idx int
}

type byV []*pair

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

func solve() {

	//2 4 4 1 1 5 2 4 3

	//1 2 4 4 1 5 2 4 3

	//1 1 2 4 4 5 2 4 3

	//

	start := 0

	fmt.Println(n - 1)

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

		var min int = 1e9 + 5
		var minidx int

		for j := start; j < n; j++ {
			if a[j] < min {
				min = a[j]
				minidx = j
			}
		}

		fmt.Printf("%d %d %d", start + 1, minidx + 1, minidx - start)
		fmt.Println()

		nums := minidx - start + 1

		target := start
		elemToPut := a[target]

		a[target] = a[minidx]

		count := 0

		for ;count < nums - 1; {
			target = target - start

			p := target - minidx + start

			if p < 0 {
				p += nums
			}
			p += start
			temp := a[p]

			a[p] = elemToPut

			elemToPut = temp
			target = p

			count++
		}
		start++
	}
}

package main

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

var n int

var a[]int

func main() {

	r := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &n)

		a = make([]int, n)

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

		solve()
	}
}

type pair struct {
	v int
	idx int
}

type byV []*pair

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

func solve() {

	//2 4 4 1 1 5 2 4 3

	//1 2 4 4 1 5 2 4 3

	//1 1 2 4 4 5 2 4 3

	//

	start := 0

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

		var min int = 1e9 + 5
		var minidx int

		for j := start; j < n; j++ {
			if a[j] < min {
				min = a[j]
				minidx = j
			}
		}

		fmt.Printf("%d %d %d", start + 1, minidx + 1, minidx - start)
		fmt.Println()

		nums := minidx - start + 1

		target := start
		elemToPut := a[target]

		a[target] = a[minidx]

		count := 0

		for ;count < nums - 1; {
			target = target - start

			p := target - minidx + start

			if p < 0 {
				p += nums
			}
			p += start
			temp := a[p]

			a[p] = elemToPut

			elemToPut = temp
			target = p

			count++
		}
		start++
	}
}

package main

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

type node struct {
	val,id int
}

func main()  {
	var t,n int
	in:=bufio.NewReader(os.Stdin)
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		fmt.Fscan(in,&n)
		a:=make([]node,n)
		for i:=0;i<n;i++{
			fmt.Fscan(in,&a[i].val)
			a[i].id=i
		}
		sort.Slice(a,func(x,y int) bool {
			if a[x].val==a[y].val{
				return a[x].id<a[y].id
			}
			return a[x].val<a[y].val
		})
		fmt.Println(n)
		for k:=0;k<n;k++{
			fmt.Println(k+1,a[k].id+1,a[k].id-k)
			for j:=0;j<n;j++{
				if k==j{
					continue
				}
				if a[j].id<a[k].id && a[j].id>=k{
					//fmt.Println(a[j].val,a[j].id)
					a[j].id-=k+a[k].id-k
					//fmt.Println(j,a[j].val,a[j].id)
					if a[j].id<0{
						a[j].id+=a[k].id+1
					}
				}
			}
		}
	}

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


...Once upon a time a man came to the sea. The sea was stormy and dark. The man started to call for the little mermaid to appear but alas, he only woke up Cthulhu...Whereas on the other end of the world Pentagon is actively collecting information trying to predict the monster's behavior and preparing the secret super weapon. Due to high seismic activity and poor weather conditions the satellites haven't yet been able to make clear shots of the monster. The analysis of the first shot resulted in an undirected graph with n vertices and m edges. Now the world's best minds are about to determine whether this graph can be regarded as Cthulhu or not.To add simplicity, let's suppose that Cthulhu looks from the space like some spherical body with tentacles attached to it. Formally, we shall regard as Cthulhu such an undirected graph that can be represented as a set of three or more rooted trees, whose roots are connected by a simple cycle.It is guaranteed that the graph contains no multiple edges and self-loops.

The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ ). Each of the following m lines contains a pair of integers x and y, that show that an edge exists between vertices x and y (1 ≤ x, y ≤ n, x ≠ y). For each pair of vertices there will be at most one edge between them, no edge connects a vertex to itself.

Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is.

NoteLet us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).A rooted tree is a tree where one vertex is selected to be the root.

package main

import (
	"fmt"
)

type Graph struct {
	n int
	adj [][]int
}
func build(g *Graph, n int) {
	g.n = n
	g.adj = make([][]int, n)
}
func add(g *Graph, v, u int) {
	g.adj[v] = append(g.adj[v], u)
	g.adj[u] = append(g.adj[u], v)
}

var n, m int
var g Graph
var used []bool

func dfs(v int) {
	used[v] = true
	for _, u := range g.adj[v] {
		if used[u] {
			continue
		}
		dfs(u)
	}
}

func main() {
	fmt.Scanf("%d %d\n", &n, &m)
	build(&g, n)
	for i := 0; i < m; i++ {
		var v, u int
		fmt.Scanf("%d %d\n", &v, &u)
		v--
		u--
		add(&g, v, u)
	}
	used = make([]bool, n)
	dfs(0)
	ok := true
	for v := 0; v < n; v++ {
		if !used[v] {
			ok = false
			break
		}
	}
	ans := (n == m) && ok
	if ans {
		fmt.Println("FHTAGN!")
	} else {
		fmt.Println("NO")
	}
}

package main

import "fmt"

type Graph struct {
	n int
	adj [][]int
}
func build(g *Graph, n int) {
	g.n = n
	g.adj = make([][]int, n)
}
func add(g *Graph, v, u int) {
	g.adj[v] = append(g.adj[v], u)
	g.adj[u] = append(g.adj[u], v)
}

var n, m int
var g Graph
var used []bool

func dfs(v int) {
	used[v] = true
	for i := 0; i < len(g.adj[v]); i++ {
		u := g.adj[v][i]
		if used[u] {
			continue
		}
		dfs(u)
	}
}

func main() {
	fmt.Scanf("%d %d\n", &n, &m)
	build(&g, n)
	for i := 0; i < m; i++ {
		var v, u int
		fmt.Scanf("%d %d\n", &v, &u)
		v--
		u--
		add(&g, v, u)
	}
	used = make([]bool, n)
	dfs(0)
	ok := true
	for v := 0; v < n; v++ {
		if !used[v] {
			ok = false
			break
		}
	}
	ans := (n == m) && ok
	if ans {
		fmt.Println("FHTAGN!")
	} else {
		fmt.Println("NO")
	}
}

package main

import "fmt"

type Graph struct {
	n int
	adj [][]int
}
func build(g *Graph, n int) {
	g.n = n
	g.adj = make([][]int, n)
}
func add(g *Graph, v, u int) {
	g.adj[v] = append(g.adj[v], u)
	g.adj[u] = append(g.adj[u], v)
}

var n, m int
var g Graph
var used []bool

func dfs(v int) {
	used[v] = true
	for i := 0; i < len(g.adj[v]); i++ {
		cool := make([]int, 0)
		for j := 0; j < len(g.adj[v]); j++ {
			cool = append(cool, g.adj[v][j])
		}
		u := cool[i]
		if used[u] {
			continue
		}
		dfs(u)
	}
}

func main() {
	fmt.Scanf("%d %d\n", &n, &m)
	build(&g, n)
	for i := 0; i < m; i++ {
		var v, u int
		fmt.Scanf("%d %d\n", &v, &u)
		v--
		u--
		add(&g, v, u)
	}
	used = make([]bool, n)
	dfs(0)
	ok := true
	for v := 0; v < n; v++ {
		if !used[v] {
			ok = false
			break
		}
	}
	ans := (n == m) && ok
	if ans {
		fmt.Println("FHTAGN!")
	} else {
		fmt.Println("NO")
	}
}

package main

import (
	"fmt"
)

type Graph struct {
	n int
	adj [][]int
}
func (g *Graph) build(n int) {
	g.n = n
	g.adj = make([][]int, n)
}
func (g *Graph) add(v, u int) {
	g.adj[v] = append(g.adj[v], u)
	g.adj[u] = append(g.adj[u], v)
}

var n, m int
var g Graph
var used []bool

func dfs(v int) {
	used[v] = true
	for _, u := range g.adj[v] {
		if used[u] {
			continue
		}
		dfs(u)
	}
}

func main() {
	fmt.Scanf("%d %d\n", &n, &m)
	g.build(n)
	for i := 0; i < m; i++ {
		var v, u int
		fmt.Scanf("%d %d\n", &v, &u)
		v--
		u--
		g.add(v, u)
	}
	used = make([]bool, n)
	dfs(0)
	ok := true
	for v := 0; v < n; v++ {
		if !used[v] {
			ok = false
			break
		}
	}
	ans := (n == m) && ok
	if ans {
		fmt.Println("FHTAGN!")
	} else {
		fmt.Println("NO")
	}
}

package main

import "fmt"

type Graph struct {
	n int
	adj [][]int
}
func build(g *Graph, n int) {
	g.n = n
	g.adj = make([][]int, n)
}
func add(g *Graph, v, u int) {
	g.adj[v] = append(g.adj[v], u)
	g.adj[u] = append(g.adj[u], v)
}

var n, m int
var g Graph
var used []bool

func dfs(v int) {
	used[v] = true
	for i := 0; i < len(g.adj[v]); i++ {
		cool := g.adj[v]
		u := cool[i]
		if used[u] {
			continue
		}
		dfs(u)
	}
}

func main() {
	fmt.Scanf("%d %d\n", &n, &m)
	build(&g, n)
	for i := 0; i < m; i++ {
		var v, u int
		fmt.Scanf("%d %d\n", &v, &u)
		v--
		u--
		add(&g, v, u)
	}
	used = make([]bool, n)
	dfs(0)
	ok := true
	for v := 0; v < n; v++ {
		if !used[v] {
			ok = false
			break
		}
	}
	ans := (n == m) && ok
	if ans {
		fmt.Println("FHTAGN!")
	} else {
		fmt.Println("NO")
	}
}

package main

import (
	"fmt"
	"os"
)

const maxn int = 100100

var g [maxn][]int
var vis [maxn]bool

func good() {
	fmt.Println("FHTAGN!")
	os.Exit(0)
}

func bad() {
	fmt.Println("NO")
	os.Exit(0)
}

func dfs(v int) (cnt int) {
	cnt = 1
	vis[v] = true
	for _, u := range g[v] {
		if !vis[u] {
			cnt += dfs(u)
		}
	}
	return
}

func main() {
	var n, m int
	fmt.Scan(&n, &m)
	if n != m {
		bad()
	}
	for i := 0; i < m; i ++ {
		var v, u int
		fmt.Scan(&v, & u)
		v --
		u --
		g[v] = append(g[v], u)
		g[u] = append(g[u], v)
	}
	if dfs(0) != n {
		bad()
	}
	good()
	return
}


As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs.

The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1, r2, ..., rn (1 ≤ ri ≤ 109). The balls' radii can coincide.

Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.

package main

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

func main() {
	in := bufio.NewReaderSize(os.Stdin, 1<<16)
	var n int
	fmt.Fscan(in, &n)
	cnt := make(map[int]int)
	for i := 0; i < n; i++ {
		var r int
		fmt.Fscan(in, &r)
		cnt[r]++
	}
	ans := sort.Search(n/3+1, func(ans int) bool {
		got := 0
		for _, v := range cnt {
			if v > ans {
				v = ans
			}
			got += v
			if got >= ans*3 {
				return false
			}
		}
		return true
	}) - 1
	snowmen := make([][]int, ans)
	i := 0
	for k, v := range cnt {
		if i == ans*3 {
			break
		}
		if v > ans {
			v = ans
		}
		for i < ans*3 && v > 0 {
			snowmen[i%ans] = append(snowmen[i%ans], k)
			v--
			i++
		}
	}

	out := bufio.NewWriterSize(os.Stdout, 1<<16)
	defer out.Flush()
	fmt.Fprintln(out, ans)
	for _, sm := range snowmen {
		sort.Ints(sm)
		fmt.Fprintln(out, sm[2], sm[1], sm[0])
	}
}

package main

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

type pair struct {
	r, cnt int
}
type pairHeap []pair

func (h pairHeap) Len() int              { return len(h) }
func (h pairHeap) Less(i, j int) bool    { return h[i].cnt > h[j].cnt }
func (h pairHeap) Swap(i, j int)         { h[i], h[j] = h[j], h[i] }
func (h *pairHeap) Push(v interface{})   { *h = append(*h, v.(pair)) }
func (h *pairHeap) Pop() (v interface{}) { n := len(*h); *h, v = (*h)[:n-1], (*h)[n-1]; return }

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

	rCnt := map[int]int{}
	var n, r int
	for Fscan(in, &n); n > 0; n-- {
		Fscan(in, &r)
		rCnt[r]++
	}

	_h := make(pairHeap, 0, len(rCnt))
	for r, cnt := range rCnt {
		_h = append(_h, pair{r, cnt})
	}
	h := &_h
	Init(h)

	ans := [][3]int{}
	for len(_h) >= 3 {
		ps := [3]pair{Pop(h).(pair), Pop(h).(pair), Pop(h).(pair)}
		ans = append(ans, [3]int{ps[0].r, ps[1].r, ps[2].r})
		for _, p := range ps {
			p.cnt--
			if p.cnt > 0 {
				Push(h, p)
			}
		}
	}
	Fprintln(out, len(ans))
	for _, rs := range ans {
		balls := rs[:]
		sort.Ints(balls)
		Fprintln(out, balls[2], balls[1], balls[0])
	}
}

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

// Codeforces 140 C not done, need faster structures
package main

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

type RCnt struct {
	r   int
	cnt int
}
type RCnts []RCnt

func (s RCnts) Len() int {
	return len(s)
}
func (s RCnts) Less(i, j int) bool {
	return s[i].cnt < s[j].cnt
}
func (s RCnts) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}
func (s RCnts) UpHeap(i int) {
	for i>0 {
		ih := i/2
		if !s.Less(ih,i) {
			break
		}
		s.Swap(ih,i)
		i = ih
	}
}

type Snowman struct {
	a, b, c int
}

func (s *Snowman) newSnow(aa,ba,ca int) {
	s.a = aa
	s.b = ba
	s.c = ca
	// fmt.Printf("new snow %d %d %d\n", s.a, s.b, s.c)
}

func (s *Snowman) printSnow() {
	// sort to a > b > c
	// fmt.Printf("bef sort %d %d %d\n", s.a, s.b, s.c)
	if s.a < s.b {
		s.b, s.a = s.a, s.b
	}
	if s.b < s.c {
		s.b, s.c = s.c, s.b
		if s.a < s.b {
			s.b, s.a = s.a, s.b
		}
	}
	fmt.Printf("%d %d %d\n", s.a, s.b, s.c)
}

var ra RCnts
var nra,nLeft int

func (s RCnts) DownHeap(i int) {
	ra[i].cnt--
	for i<nra {
		a := ra[i].cnt
		i0 := 2*i
		i1 := 2*i+1
		if i0 >= nra {
			break
		} else if i1 < nra {
			if ra[i1].cnt > ra[i0].cnt {
				i0 = i1
			}
		}
		b := ra[i0].cnt
		if b > a {
			ra.Swap(i,i0)
			i = i0
		} else {
			break
		}
	}
	if ra[i].cnt==0 {
		nLeft--
	}
}

func next3Vals() (int,int,int) {
	if ra[0].cnt==0 || ra[1].cnt==0 || ra[2].cnt==0 {
		sort.Sort(sort.Reverse(ra))
	}
	a := ra[0].r
	b := ra[1].r
	c := ra[2].r
	ra.DownHeap(2)
	ra.DownHeap(1)
	ra.DownHeap(0)
	printRa()
	return a,b,c
}

func printRa() {
	return
	for i:=0; i<nra; i++ {
		fmt.Printf("(%d, %d) ", ra[i].cnt, ra[i].r);
	}
	fmt.Println();
}

func main() {
	// reading n
	in := bufio.NewReader(os.Stdin)
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		return
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	n, err1 := strconv.Atoi(ss[0])
	if err1 != nil {
		fmt.Println("n atoi failure", err1)
		return
	}
	// reading radiuses
	s, err = in.ReadString('\n')
	if err != nil {
		fmt.Println("second read failure", err)
		return
	}
	ss = strings.Split(strings.Trim(s, " \n\r"), " ")
	rmap := make(map[int]int)
	for i := 0; i < n; i++ {
		r, err2 := strconv.Atoi(ss[i])
		if err2 != nil {
			fmt.Println("r atoi failure", err1)
			return
		}
		c, ok := rmap[r]
		if ok {
			rmap[r] = c + 1
		} else {
			rmap[r] = 1
		}
	}
	nra = len(rmap)
	ra = RCnts(make([]RCnt, nra))
	ir := 0
	for r, c := range rmap {
		ra[ir].r = r
		ra[ir].cnt = c
		ir++
	}
	sort.Sort(sort.Reverse(ra))
	nLeft = nra
	printRa()

	// calculating
	maxSna := n / 3
	sna := make([]Snowman, maxSna)
	nSna := 0
	for nLeft >= 3 {
		a,b,c := next3Vals()
		sna[nSna].newSnow(a,b,c)
		nSna++
	}

	// print result
	fmt.Printf("%d\n", nSna)
	for i := 0; i < nSna; i++ {
		sna[i].printSnow()
	}
}

package main

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

func main() {
	in := bufio.NewReaderSize(os.Stdin, 1<<16)
	var n int
	fmt.Fscan(in, &n)
	r := make([]int, n)
	for i := range r {
		fmt.Fscan(in, &r[i])
	}
	sort.Ints(r)
	var snowmen [][]int
	full := 0
	for i := n - 1; i >= 0; i-- {
		cur := r[i]
		j := i-1
		for j >= 0 && r[j] == cur {
			j--
		}
		prevFull := full
		for k := 0; k < i - j; k++ {
			if prevFull + k >= len(snowmen) {
				snowmen = append(snowmen, make([]int, 0, 3))
			}
			snowmen[prevFull+k] = append(snowmen[prevFull+k], cur)
			if len(snowmen[prevFull+k]) == 3 {
				full++
			}
		}
		i = j+1
	}

	out := bufio.NewWriterSize(os.Stdout, 1<<16)
	defer out.Flush()
	fmt.Fprintln(out, full)
	for _, sm := range snowmen[:full] {
		fmt.Fprintln(out, sm[0], sm[1], sm[2])
	}
}


There are n cities in Berland, each of them has a unique id — an integer from 1 to n, the capital is the one with id 1. Now there is a serious problem in Berland with roads — there are no roads.That is why there was a decision to build n - 1 roads so that there will be exactly one simple path between each pair of cities.In the construction plan t integers a1, a2, ..., at were stated, where t equals to the distance from the capital to the most distant city, concerning new roads. ai equals the number of cities which should be at the distance i from the capital. The distance between two cities is the number of roads one has to pass on the way from one city to another. Also, it was decided that among all the cities except the capital there should be exactly k cities with exactly one road going from each of them. Such cities are dead-ends and can't be economically attractive. In calculation of these cities the capital is not taken into consideration regardless of the number of roads from it. Your task is to offer a plan of road's construction which satisfies all the described conditions or to inform that it is impossible.

The first line contains three positive numbers n, t and k (2 ≤ n ≤ 2·105, 1 ≤ t, k < n) — the distance to the most distant city from the capital and the number of cities which should be dead-ends (the capital in this number is not taken into consideration).  The second line contains a sequence of t integers a1, a2, ..., at (1 ≤ ai < n), the i-th number is the number of cities which should be at the distance i from the capital. It is guaranteed that the sum of all the values ai equals n - 1.

If it is impossible to built roads which satisfy all conditions, print -1. Otherwise, in the first line print one integer n — the number of cities in Berland. In the each of the next n - 1 line print two integers — the ids of cities that are connected by a road. Each road should be printed exactly once. You can print the roads and the cities connected by a road in any order. If there are multiple answers, print any of them. Remember that the capital has id 1.

package main

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

var G map[int64]bool

func addnode(from, to, n int) {
	x := int64(from)*int64(n) + int64(to)
	G[x] = true
}

func removenode(from, to, n int) {
	x := int64(from)*int64(n) + int64(to)
	G[x] = false
}

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

func main() {
	var n, t, k int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n, &t, &k)
	t++
	G = make(map[int64]bool)

	levels := make([]int, t)
	isleaf := make([]bool, n)
	leaves := 0
	levels[0] = 1
	for i := 1; i < t; i++ {
		fmt.Fscan(in, &levels[i])
	}
	cur := 1
	for i := 1; i < t; i++ {
		min := mmin(levels[i-1], levels[i])
		for j := 0; j < min; j++ {
			addnode(cur-levels[i-1]+j, cur+j, n)
		}
		if min < levels[i] {
			for j := 0; j < levels[i]-min; j++ {
				addnode(cur-levels[i-1], cur+min+j, n)
			}
		} else {
			for j := 0; j < levels[i-1]-min; j++ {
				isleaf[cur-1-j] = true
				leaves++
			}
		}
		if i == t-1 {
			for j := 0; j < levels[i]; j++ {
				leaves++
				isleaf[cur+j] = true
			}
		}
		cur += levels[i]
	}
	if leaves > k {
		fmt.Println(-1)
		goto halt
	}
	for leaves < k {
		oldleaves := leaves
		cur = 1
		for i := 1; i < t; i++ {
			for j := cur + 1; j < cur+levels[i]; j++ {
				if !isleaf[j] && leaves < k {
					removenode(j, j+levels[i], n)
					addnode(cur, j+levels[i], n)
					isleaf[j] = false
					leaves++
				}
			}
			cur += levels[i]
		}
		if oldleaves == leaves {
			fmt.Println(-1)
			goto halt
		}
	}

	fmt.Println(n)
	for key, val := range G {
		if val {
			fmt.Println(key/int64(n)+1, key%int64(n)+1)
		}
	}
halt:
}

package main

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

var G map[int]bool

func addnode(from, to, n int) {
	x := from*n + to
	G[x] = true
}

func removenode(from, to, n int) {
	x := from*n + to
	G[x] = false
}

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

func main() {
	var n, t, k int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n, &t, &k)
	t++
	G = make(map[int]bool)

	levels := make([]int, t)
	isleaf := make([]bool, n)
	leaves := 0
	levels[0] = 1
	for i := 1; i < t; i++ {
		fmt.Fscan(in, &levels[i])
	}
	cur := 1
	for i := 1; i < t; i++ {
		min := mmin(levels[i-1], levels[i])
		for j := 0; j < min; j++ {
			addnode(cur-levels[i-1]+j, cur+j, n)
		}
		if min < levels[i] {
			for j := 0; j < levels[i]-min; j++ {
				addnode(cur-levels[i-1], cur+min+j, n)
			}
		} else {
			for j := 0; j < levels[i-1]-min; j++ {
				isleaf[cur-1-j] = true
				leaves++
			}
		}
		if i == t-1 {
			for j := 0; j < levels[i]; j++ {
				leaves++
				isleaf[cur+j] = true
			}
		}
		cur += levels[i]
	}
	if leaves > k {
		fmt.Println(-1)
		goto halt
	}
	for leaves < k {
		cur = 1
		for i := 1; i < t; i++ {
			for k := cur + 1; k < cur+levels[i]; k++ {
				if !isleaf[k] && leaves < k {
					removenode(k, k+levels[i], n)
					addnode(cur, k+levels[i], n)
					isleaf[k] = true
					leaves++
				}
			}
			cur += levels[i]
		}
	}

	fmt.Println(n)
	for key, val := range G {
		if val {
			fmt.Println(key/n+1, key%n+1)
		}
	}
halt:
}

package main

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

var G map[int64]bool

func addnode(from, to, n int) {
	x := int64(from*n) + int64(to)
	G[x] = true
}

func removenode(from, to, n int) {
	x := int64(from*n) + int64(to)
	G[x] = false
}

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

func main() {
	var n, t, k int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n, &t, &k)
	t++
	G = make(map[int64]bool)

	levels := make([]int, t)
	isleaf := make([]bool, n)
	leaves := 0
	levels[0] = 1
	for i := 1; i < t; i++ {
		fmt.Fscan(in, &levels[i])
	}
	cur := 1
	for i := 1; i < t; i++ {
		min := mmin(levels[i-1], levels[i])
		for j := 0; j < min; j++ {
			addnode(cur-levels[i-1]+j, cur+j, n)
		}
		if min < levels[i] {
			for j := 0; j < levels[i]-min; j++ {
				addnode(cur-levels[i-1], cur+min+j, n)
			}
		} else {
			for j := 0; j < levels[i-1]-min; j++ {
				isleaf[cur-1-j] = true
				leaves++
			}
		}
		if i == t-1 {
			for j := 0; j < levels[i]; j++ {
				leaves++
				isleaf[cur+j] = true
			}
		}
		cur += levels[i]
	}
	if leaves > k {
		fmt.Println(-1)
		goto halt
	}
	for leaves < k {
		oldleaves := leaves
		cur = 1
		for i := 1; i < t; i++ {
			for j := cur + 1; j < cur+levels[i]; j++ {
				if !isleaf[j] && leaves < k {
					removenode(j, j+levels[i], n)
					addnode(cur, j+levels[i], n)
					isleaf[j] = false
					leaves++
				}
			}
			cur += levels[i]
		}
		if oldleaves == leaves {
			fmt.Println(-1)
			goto halt
		}
	}

	fmt.Println(n)
	for key, val := range G {
		if val {
			fmt.Println(key/int64(n)+1, key%int64(n)+1)
		}
	}
halt:
}

package main

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

var G map[int]bool

func addnode(from, to, n int) {
	x := from*n + to
	G[x] = true
}

func removenode(from, to, n int) {
	x := from*n + to
	G[x] = false
}

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

func main() {
	var n, t, k int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n, &t, &k)
	t++
	G = make(map[int]bool)

	levels := make([]int, t)
	isleaf := make([]bool, n)
	leaves := 0
	levels[0] = 1
	for i := 1; i < t; i++ {
		fmt.Fscan(in, &levels[i])
	}
	cur := 1
	for i := 1; i < t; i++ {
		min := mmin(levels[i-1], levels[i])
		for j := 0; j < min; j++ {
			addnode(cur-levels[i-1]+j, cur+j, n)
		}
		if min < levels[i] {
			for j := 0; j < levels[i]-min; j++ {
				addnode(cur-levels[i-1], cur+min+j, n)
			}
		} else {
			for j := 0; j < levels[i-1]-min; j++ {
				isleaf[cur-1-j] = true
				leaves++
			}
		}
		if i == t-1 {
			for j := 0; j < levels[i]; j++ {
				leaves++
				isleaf[cur+j] = true
			}
		}
		cur += levels[i]
	}
	if leaves > k {
		fmt.Println(-1)
		goto halt
	}
	for leaves < k {
		oldleaves := leaves
		cur = 1
		for i := 1; i < t; i++ {
			for k := cur + 1; k < cur+levels[i]; k++ {
				if !isleaf[k] && leaves < k {
					removenode(k, k+levels[i], n)
					addnode(cur, k+levels[i], n)
					isleaf[k] = true
					leaves++
				}
			}
			cur += levels[i]
		}
		if oldleaves == leaves {
			fmt.Println(-1)
			goto halt
		}
	}

	fmt.Println(n)
	for key, val := range G {
		if val {
			fmt.Println(key/n+1, key%n+1)
		}
	}
halt:
}

package main

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

var G map[int]bool

func addnode(from, to, n int) {
	x := from*n + to
	G[x] = true
}

func removenode(from, to, n int) {
	x := from*n + to
	G[x] = false
}

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

func main() {
	var n, t, k int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n, &t, &k)
	t++
	G = make(map[int]bool)

	levels := make([]int, t)
	isleaf := make([]bool, n)
	leaves := 0
	levels[0] = 1
	for i := 1; i < t; i++ {
		fmt.Fscan(in, &levels[i])
	}
	cur := 1
	for i := 1; i < t; i++ {
		min := mmin(levels[i-1], levels[i])
		for j := 0; j < min; j++ {
			addnode(cur-levels[i-1]+j, cur+j, n)
		}
		if min < levels[i] {
			for j := 0; j < levels[i]-min; j++ {
				addnode(cur-levels[i-1], cur+min+j, n)
			}
		} else {
			for j := 0; j < levels[i-1]-min; j++ {
				isleaf[cur-1-j] = true
				leaves++
			}
		}
		if i == t-1 {
			for j := 0; j < levels[i]; j++ {
				leaves++
				isleaf[cur+j] = true
			}
		}
		cur += levels[i]
	}
	if leaves > k {
		fmt.Println(-1)
		goto halt
	}
	for leaves < k {
		oldleaves := leaves
		cur = 1
		for i := 1; i < t; i++ {
			for j := cur + 1; j < cur+levels[i]; j++ {
				if !isleaf[k] && leaves < k {
					removenode(j, j+levels[i], n)
					addnode(cur, j+levels[i], n)
					isleaf[j] = false
					leaves++
				}
			}
			cur += levels[i]
		}
		if oldleaves == leaves {
			fmt.Println(-1)
			goto halt
		}
	}

	fmt.Println(n)
	for key, val := range G {
		if val {
			fmt.Println(key/n+1, key%n+1)
		}
	}
halt:
}

package main

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

var G map[int]bool

func addnode(from, to, n int) {
	x := from*n + to
	G[x] = true
}

func removenode(from, to, n int) {
	x := from*n + to
	G[x] = false
}

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

func main() {
	var n, t, k int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n, &t, &k)
	t++
	G = make(map[int]bool)

	levels := make([]int, t)
	isleaf := make([]bool, n)
	leaves := 0
	levels[0] = 1
	for i := 1; i < t; i++ {
		fmt.Fscan(in, &levels[i])
	}
	cur := 1
	for i := 1; i < t; i++ {
		min := mmin(levels[i-1], levels[i])
		for j := 0; j < min; j++ {
			addnode(cur-levels[i-1]+j, cur+j, n)
		}
		if min < levels[i] {
			for j := 0; j < levels[i]-min; j++ {
				addnode(cur-levels[i-1], cur+min+j, n)
			}
		} else {
			for j := 0; j < levels[i-1]-min; j++ {
				isleaf[cur-1-j] = true
				leaves++
			}
		}
		if i == t-1 {
			for j := 0; j < levels[i]; j++ {
				leaves++
				isleaf[cur+j] = true
			}
		}
		cur += levels[i]
	}
	if leaves > k {
		fmt.Println(-1)
		goto halt
	}
	for leaves < k {
		oldleaves := leaves
		cur = 1
		for i := 1; i < t; i++ {
			for j := cur + 1; j < cur+levels[i]; j++ {
				if !isleaf[j] && leaves < k {
					removenode(j, j+levels[i], n)
					addnode(cur, j+levels[i], n)
					isleaf[j] = false
					leaves++
				}
			}
			cur += levels[i]
		}
		if oldleaves == leaves {
			fmt.Println(-1)
			goto halt
		}
	}

	fmt.Println(n)
	for key, val := range G {
		if val {
			fmt.Println(key/n+1, key%n+1)
		}
	}
halt:
}

package main

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

var G map[int64]bool

func addnode(from, to, n int) {
	x := int64(from*n) + int64(to)
	G[x] = true
}

func removenode(from, to, n int) {
	x := int64(from*n) + int64(to)
	G[x] = false
}

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

func main() {
	var n, t, k int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n, &t, &k)
	t++
	G = make(map[int64]bool)

	levels := make([]int, t)
	isleaf := make([]bool, n)
	leaves := 0
	levels[0] = 1
	for i := 1; i < t; i++ {
		fmt.Fscan(in, &levels[i])
	}
	cur := 1
	for i := 1; i < t; i++ {
		min := mmin(levels[i-1], levels[i])
		for j := 0; j < min; j++ {
			addnode(cur-levels[i-1]+j, cur+j, n)
		}
		if min < levels[i] {
			for j := 0; j < levels[i]-min; j++ {
				addnode(cur-levels[i-1], cur+min+j, n)
			}
		} else {
			for j := 0; j < levels[i-1]-min; j++ {
				isleaf[cur-1-j] = true
				leaves++
			}
		}
		if i == t-1 {
			for j := 0; j < levels[i]; j++ {
				leaves++
				isleaf[cur+j] = true
			}
		}
		cur += levels[i]
	}
	if leaves > k {
		fmt.Println(-1)
		goto halt
	}
	for leaves < k {
		oldleaves := leaves
		cur = 1
		for i := 1; i < t; i++ {
			for j := cur + 1; j < cur+levels[i]; j++ {
				if !isleaf[j] && leaves < k {
					removenode(j, j+levels[i], n)
					addnode(cur, j+levels[i], n)
					isleaf[j] = false
					leaves++
				}
			}
			cur += levels[i]
		}
		if oldleaves == leaves {
			fmt.Println(-1)
			goto halt
		}
	}

	fmt.Println(n)
	for key, val := range G {
		if val {
			fmt.Println(key/int64(n)+int64(1), key%int64(n)+int64(1))
		}
	}
halt:
}

package main

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

var G map[int]bool

func addnode(from, to, n int) {
	x := from*n + to
	G[x] = true
}

func removenode(from, to, n int) {
	x := from*n + to
	G[x] = false
}

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

func main() {
	var n, t, k int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n, &t, &k)
	t++
	G = make(map[int]bool)

	levels := make([]int, t)
	isleaf := make([]bool, n)
	leaves := 0
	levels[0] = 1
	for i := 1; i < t; i++ {
		fmt.Fscan(in, &levels[i])
	}
	cur := 1
	for i := 1; i < t; i++ {
		min := mmin(levels[i-1], levels[i])
		for j := 0; j < min; j++ {
			addnode(cur-levels[i-1]+j, cur+j, n)
		}
		if min < levels[i] {
			for j := 0; j < levels[i]-min; j++ {
				addnode(cur-levels[i-1], cur+min+j, n)
			}
		} else {
			for j := 0; j < levels[i-1]-min; j++ {
				isleaf[cur-1-j] = true
				leaves++
			}
		}
		if i == t-1 {
			for j := 0; j < levels[i]; j++ {
				leaves++
				isleaf[cur+j] = true
			}
		}
		cur += levels[i]
	}
	if leaves > k {
		fmt.Println(-1)
		goto halt
	}
	for leaves < k {
		oldleaves := leaves
		cur = 1
		for i := 1; i < t; i++ {
			for k := cur + 1; k < cur+levels[i]; k++ {
				if !isleaf[k] && leaves < k {
					removenode(k, k+levels[i], n)
					addnode(cur, k+levels[i], n)
					isleaf[k] = false
					leaves++
				}
			}
			cur += levels[i]
		}
		if oldleaves == leaves {
			fmt.Println(-1)
			goto halt
		}
	}

	fmt.Println(n)
	for key, val := range G {
		if val {
			fmt.Println(key/n+1, key%n+1)
		}
	}
halt:
}