You are given an array $$$a_1, a_2, \dots , a_n$$$. Array is good if for each pair of indexes $$$i < j$$$ the condition $$$j - a_j \ne i - a_i$$$ holds. Can you shuffle this array so that it becomes good? To shuffle an array means to reorder its elements arbitrarily (leaving the initial order is also an option).For example, if $$$a = [1, 1, 3, 5]$$$, then shuffled arrays $$$[1, 3, 5, 1]$$$, $$$[3, 5, 1, 1]$$$ and $$$[5, 3, 1, 1]$$$ are good, but shuffled arrays $$$[3, 1, 5, 1]$$$, $$$[1, 1, 3, 5]$$$ and $$$[1, 1, 5, 3]$$$ aren't.It's guaranteed that it's always possible to shuffle an array to meet this condition.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 100$$$).

For each test case print the shuffled version of the array $$$a$$$ which is good.

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, n, x int
	Fscan(in, &t)
	for ; t > 0; t-- {
		Fscan(in, &n)
		var a []int
		for i := 0; i < n; i ++ {
			Fscan(in, &x)
			a = append(a, x)
		}
		sort.Slice(a, func(i, j  int) bool {
			return a[i] > a[j]
		})
		for i := 0; i < n; i++ {
			Fprint(out, a[i])
			if (i == n-1) {
				Fprintln(out)
			} else {
				Fprint(out, " ")
			}
		}
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t, n, x int
	fmt.Fscanf(in, "%d\n", &t)
	for ; t > 0; t-- {
		fmt.Fscanf(in, "%d\n", &n)
		var a []int
		for i := 0; i < n; i ++ {
			fmt.Fscanf(in, "%d", &x)
			if(i == n - 1) {
				fmt.Fscanf(in, "\n")
			} else {
				fmt.Fscanf(in, " ")
			}
			a = append(a, x)
		}
		sort.Sort(sort.Reverse(sort.IntSlice(a)))
		for i := 0; i < n; i++ {
			fmt.Fprintf(out, "%d", a[i])
			if(i == n - 1) {
				fmt.Fprintf(out,"\n")
			} else {
				fmt.Fprintf(out, " ")
			}
		}
	}
}

package main

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

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

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

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

package main

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

func main(){
	var t,n int
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)

	defer w.Flush()
	fmt.Fscanf(r,"%d\n",&t)

	for T := 0 ; T < t ; T ++ {
		fmt.Fscanf(r,"%d\n",&n)
		//fmt.Println("N = ",n)
		a := make([]int , n)

		for i := 0 ; i < n ; i ++ {
			fmt.Fscanf(r,"%d",&a[i])
		}
		fmt.Fscanf(r,"\n")
		sort.Slice(a, func (i,j int) bool {
			return a[i] > a[j]
		})

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

		//fmt.Println(a)
	}

}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++{
		var n int
		fmt.Scan(&n)
		a := make([]int, n)
		for j := 0; j < n; j++{
			fmt.Scan(&a[j])
		}
		sort.Sort(sort.Reverse(sort.IntSlice(a)))
		for k := 0; k < len(a); k++{
			fmt.Print(a[k], " ")
		}
	}
}

package main

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

func main() {
	defer writer.Flush()

	var tests int
	Input(&tests)

	for t := 1; t <= tests; t++ {
		var n int
		Input(&n)

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

		sort.Ints(a)
		sort.Sort(sort.Reverse(sort.IntSlice(a)))

		for i := 0; i < n; i++ {
			Print(a[i], " ")
		}
		Print("\n")
	}
}

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

func Input(a ...interface{}) {
	_, _ = fmt.Fscan(reader, a...)
}

func Print(a ...interface{}) {
	_, _ = fmt.Fprint(writer, a...)
}

package main

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

func main() {
	var cases int
	fmt.Scanf("%d\n", &cases)
	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < cases; i++ {
		line := readLine(reader)
		n, _ := strconv.Atoi(string(line))
		line = readLine(reader)
		ans := strings.Split(string(line), " ")
		an := make([]int, len(ans))
		for i := 0; i < n; i++ {
			an[i], _ = strconv.Atoi(ans[i])
		}
		sort.Ints(an)
		for i := n - 1; i >= 0; i-- {
			fmt.Printf("%d ", an[i])
		}
		fmt.Println()
	}
}

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

package main

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

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

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

	for t := 0; t < ts; t++ {
		var n int
		fmt.Fscanf(r, "%d\n", &n)

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

		sort.Ints(a)

		for i := len(a) - 1; i >= 0; i-- {
			fmt.Fprintf(w, "%d ", a[i])
		}
		fmt.Fprintf(w, "\n")
	}
}

package main

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

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

	var t, n int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		sort.Ints(a)
		for n--; n >= 0; n-- {
			Fprint(out, a[n], " ")
		}
		Fprintln(out)
	}
}

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

package main

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

func main() {

	for testCase := ScanInt(); testCase > 0; testCase-- {
		count := ScanInt()
		givenArray := ScanIntSlice(count)

		sort.Slice(givenArray, func(i, j int) bool {
			return givenArray[i] > givenArray[j]
		})
		P(SArr(givenArray))
	}
	close(O)
	oWg.Wait()
}

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

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var P func(a ...interface{}) (n int, err error)

func init() {
	P = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	O = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range O {
			writer.WriteString(line + "\n")
		}
	}()
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanInt() int     { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt()
	}
	return arr
}
func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

func main() {
	for testCase := scanInt(); testCase > 0; testCase--{
		count := scanInt()
		givenArray := scanIntSlice(count)
		sort.Slice(givenArray, func(i, j int) bool {
			return givenArray[i] > givenArray[j]
		})
		printFmt(spaceSeperatedStringFromArray(givenArray))
	}
	close(channelType)
	oWg.Wait()
}

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

var channelType chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var printFmt func(a ...interface{}) (n int, err error)

func init() {
	printFmt = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	channelType = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range channelType {
			writer.WriteString(line + "\n")
		}
	}()
}

func scanString() string {in.Scan(); return in.Text() }
func scanInt() int {i, _ := strconv.Atoi(scanString()); return i}
func scanInt64() int64 {i, _ := strconv.ParseInt(scanString(), 10, 64); return i}
func scanStringSlice(n int) []string {s := make([]string, n); for i := 0; i < n; i++ {s[i] = scanString()}; return s}
func scanIntSlice(n int) []int {arr := make([]int, n); for i := 0; i < n; i++ {arr[i] = scanInt()}; return arr}
func scanInt64Slice(n int) []int64 {arr := make([]int64, n); for i := 0; i < n; i++ {arr[i] = scanInt64()}; return arr}

/*********************** Utils ***********************/

func stringEquivalent(v interface{}) string {return fmt.Sprintf("%v", v)}
func spaceSeperatedStringFromArray(a []int) string {sb := strings.Builder{}; sb.Grow(len(a) * 9); for i := range a {sb.WriteString(strconv.Itoa(a[i]) + " ")}; return sb.String()}
func spaceSeperatedStringFromArray64(a []int64) string {sb := strings.Builder{}; sb.Grow(len(a) * 9); for i := range a {sb.WriteString(strconv.FormatInt(a[i], 10) + " ")}; return sb.String()}
func minMaxFromArray(args ...int64) (int64, int64) {minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64); for _, v := range args {if v < minVal {minVal = v}; if v > maxVal {maxVal = v}}; return minVal, maxVal}
func minElementFromArray(args ...int64) int64 { mn, _ := minMaxFromArray(args...); return mn }
func maxElementFromArray(args ...int64) int64 { _, mx := minMaxFromArray(args...); return mx }
func lcmCalculation(a, b int64) int64 {return a / gcdCalculation(a, b) * b}
func gcdCalculation(a, b int64) int64 {if (a == 0) {return b}; return gcdCalculation(b%a, a)}
func sortInt64Slice(a []int64) {sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })}
func absoluteValue(x int64) int64 {if x < 0 {return -x}; return x}
func min(args ...int) int { mn, _ := minMaxFinder(args...); return mn }
func max(args ...int) int { _, mx := minMaxFinder(args...); return mx }
func minMaxFinder(args ...int) (int, int) { minVal, maxVal := int(math.MaxInt32), int(math.MinInt32); for _, v := range args { if v < minVal { minVal = v }; if v > maxVal { maxVal = v } }; return minVal, maxVal }

package main

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

func main() {
	for t := ReadInt(); t > 0; t-- {
		n:=ReadInt()
		var a []int
		for i:=1;i<=n;i++ {
			x:=ReadInt()
			a=append(a,x)
		}
		sort.Slice(a, func(i, j int) bool {
			return a[i]>a[j]
		})
		for _,i:=range a{
			fmt.Printf("%d ",i)
		}
		fmt.Println()
	}
}

////////////////////////////////////

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

var out chan string
var in *bufio.Scanner
var outWg *sync.WaitGroup

func ReadString() string {
	in.Scan()
	return in.Text()
}
func ReadStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ReadString()
	}
	return s
}
func ReadInt() int {
	intStr := ReadString()
	i, _ := strconv.ParseInt(intStr, 10, 32)
	return int(i)
}
func ReadInt64() int64 {
	intstr := ReadString()
	i, _ := strconv.ParseInt(intstr, 10, 64)
	return int64(i)
}
func ReadIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt()
	}
	return arr
}
func init() {
	//set input
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(2e+5))
	in.Split(bufio.ScanWords)

	//set output
	out = make(chan string, 16)
	outWg = &sync.WaitGroup{}
	outWg.Add(1)

	writer := bufio.NewWriterSize(os.Stdout, int(2e+5))
	go func(write *bufio.Writer) {
		defer outWg.Done()
		defer write.Flush()

		for line := range out {
			write.WriteString(line + "\n")
		}
	}(writer)
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var t, n int
	fmt.Fscan(reader, &t)
	for j := 0; j < t; j++ {
		fmt.Fscan(reader, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		sort.Ints(a)
		for i := n - 1; i >= 0; i-- {
			fmt.Fprintf(writer, "%d ", a[i])
		}
		fmt.Fprintln(writer)
	}
}//2020-07-31 14:14:22.768

package main

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

const INFINITY = math.MaxInt32/2 - 1

func exec(stdin *Stdin, stdout *Stdout) {
	t := stdin.ReadInt()
	for i := 0; i < t; i++ {
		n := stdin.ReadInt()
		a := stdin.ReadIntSlice(n)
		sort.Sort(sort.Reverse(sort.IntSlice(a)))
		stdout.Println(strings.Join(ToStringSlice(a), " "))
	}
}

func main() {
	stdout := NewStdout()
	defer stdout.Flush()
	exec(NewStdin(bufio.ScanWords), stdout)
}

type Stdin struct {
	stdin *bufio.Scanner
}

func NewStdin(split bufio.SplitFunc) *Stdin {
	s := Stdin{bufio.NewScanner(os.Stdin)}
	s.stdin.Split(split)
	s.stdin.Buffer(make([]byte, bufio.MaxScanTokenSize), INFINITY)
	return &s
}

func (s *Stdin) Read() string {
	s.stdin.Scan()
	return s.stdin.Text()
}

func (s *Stdin) ReadInt() int {
	n, _ := strconv.Atoi(s.Read())
	return n
}

func (s *Stdin) ReadFloat64() float64 {
	n, _ := strconv.ParseFloat(s.Read(), 64)
	return n
}

func (s *Stdin) ReadIntSlice(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = s.ReadInt()
	}
	return a
}

func (s *Stdin) ReadStringSlice(n int) []string {
	a := make([]string, n)
	for i := 0; i < n; i++ {
		a[i] = s.Read()
	}
	return a
}

func (s *Stdin) ReadFloat64Slice(n int) []float64 {
	a := make([]float64, n)
	for i := 0; i < n; i++ {
		a[i] = s.ReadFloat64()
	}
	return a
}

type Stdout struct {
	stdout *bufio.Writer
}

func NewStdout() *Stdout {
	return &Stdout{bufio.NewWriter(os.Stdout)}
}

func (s *Stdout) Flush() {
	s.stdout.Flush()
}

func (s *Stdout) Println(a ...interface{}) {
	fmt.Fprintln(s.stdout, a...)
}

func Min(a int, b ...int) int {
	for _, v := range b {
		if v < a {
			a = v
		}
	}
	return a
}

func Max(a int, b ...int) int {
	for _, v := range b {
		if a < v {
			a = v
		}
	}
	return a
}

func Abs(x int) int {
	if x > 0 {
		return x
	} else {
		return x * -1
	}
}

func Pow(x, y int) int {
	z := 1
	for y > 0 {
		if y%2 == 0 {
			x *= x
			y /= 2
		} else {
			z *= x
			y -= 1
		}
	}
	return z
}

func IsPrime(x int) bool {
	if x < 2 {
		return false
	}

	for i := 2; i*i <= x; i++ {
		if x%i == 0 {
			return false
		}
	}
	return true
}

func Gcd(x, y int) int {
	if x < y {
		x, y = y, x
	}

	for y > 0 {
		x, y = y, x%y
	}

	return x
}

func Lcm(x, y int) int {
	return x * y / Gcd(x, y)
}

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

func LCS(s, t string) string {
	n := len(s)
	m := len(t)
	dp := make([][]int, n+1)
	for i := 0; i <= n; i++ {
		dp[i] = make([]int, m+1)
	}

	for i := 1; i <= n; i++ {
		for j := 1; j <= m; j++ {
			if s[i-1] == t[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = Max(dp[i-1][j], dp[i][j-1])
			}
		}
	}

	x := n
	y := m
	u := make([]byte, dp[n][m])
	i := dp[n][m] - 1
	for x > 0 && y > 0 {
		if dp[x][y] == dp[x-1][y] {
			x--
		} else if dp[x][y] == dp[x][y-1] {
			y--
		} else {
			x--
			y--
			u[i] = s[x]
			i--
		}
	}

	return string(u)
}

// [0..n) から r個選んだ組合せを生成する。
// r < 0 の場合はすべての組合せを生成する。
func Combinations(n int, r int) [][]int {
	a := [][]int{}
	for i := uint(0); i < (1 << uint(n)); i++ {
		b := []int{}
		for j := uint(0); j < uint(n); j++ {
			if i&(uint(1)<<j) != 0 {
				b = append(b, int(j))
			}
		}

		if r < 0 || len(b) == r {
			a = append(a, b)
		}
	}
	return a
}

// [0..n)の順列を生成する。
func Permutations(n int) [][]int {
	type e struct {
		bit uint
		a   []int
	}

	q := []*e{}
	for i := 0; i < n; i++ {
		q = append(q, &e{bit: 1 << uint(i), a: []int{i}})
	}

	p := [][]int{}
	for x := 0; x < len(q); x++ {
		cur := q[x]
		if cur.bit == (1<<uint(n) - 1) {
			p = append(p, cur.a)
			continue
		}

		for i := 0; i < n; i++ {
			if cur.bit&(1<<uint(i)) != 0 {
				continue
			}

			m := len(cur.a) + 1
			b := make([]int, m)
			copy(b, cur.a)
			b[m-1] = i
			q = append(q, &e{bit: cur.bit | (1 << uint(i)), a: b})
		}
	}
	return p
}

func ReverseSlice(v interface{}) {
	if reflect.TypeOf(v).Kind() == reflect.Slice {
		slice := reflect.ValueOf(v)
		for i1, n := 0, slice.Len(); i1 < n/2; i1++ {
			i2 := n - i1 - 1
			v1 := reflect.ValueOf(slice.Index(i1).Interface())
			slice.Index(i1).Set(slice.Index(i2))
			slice.Index(i2).Set(v1)
		}
	}
}

func ReverseString(s string) string {
	t := []rune(s)
	ReverseSlice(t)
	return string(t)
}

func ToStringSlice(slice interface{}) []string {
	// 本来はerrを返却すべき。 利用する側(=自分)は引数がsliceであることを知っている。
	if reflect.TypeOf(slice).Kind() != reflect.Slice {
		panic(fmt.Sprintf("%v is not slice", slice))
	}

	src := reflect.ValueOf(slice)
	n := src.Len()
	dst := make([]string, n)
	for i := 0; i < n; i++ {
		dst[i] = fmt.Sprintf("%v", src.Index(i))
	}
	return dst
}

func CreateMatrix(x, y int) [][]int {
	matrix := make([][]int, x)
	for i := 0; i < x; i++ {
		matrix[i] = make([]int, y)
	}
	return matrix
}

package main

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

func main() {
	for testCase := scanInt(); testCase > 0; testCase-- {
		count := scanInt()
		givenArray := scanIntSlice(count)
		for i := 0; i < count; i++ {
			for j := count - 1; j > 0; j-- {
				if i < j && j-givenArray[j] != i-givenArray[i] {
					givenArray[i], givenArray[j] = givenArray[j], givenArray[i]
				}
			}
		}
		printFmt(SArr(givenArray))
		//printFmt(givenArray)
	}
	close(channelType)
	oWg.Wait()
}

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

var channelType chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var printFmt func(a ...interface{}) (n int, err error)

func init() {
	printFmt = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	channelType = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range channelType {
			writer.WriteString(line + "\n")
		}
	}()
}

func scanString() string { in.Scan(); return in.Text() }
func scanInt() int       { i, _ := strconv.Atoi(scanString()); return i }
func scanInt64() int64   { i, _ := strconv.ParseInt(scanString(), 10, 64); return i }
func scanStringSlice(n int) []string {
	s := make([]string, n);
	for i := 0; i < n; i++ {
		s[i] = scanString()
	};
	return s
}
func scanIntSlice(n int) []int {
	arr := make([]int, n);
	for i := 0; i < n; i++ {
		arr[i] = scanInt()
	};
	return arr
}
func scanInt64Slice(n int) []int64 {
	arr := make([]int64, n);
	for i := 0; i < n; i++ {
		arr[i] = scanInt64()
	};
	return arr
}

/*********************** Utils ***********************/

func stringEquivalent(v interface{}) string { return fmt.Sprintf("%v", v) }
func spaceSeperatedStringFromArray(a []int) string {
	sb := strings.Builder{};
	sb.Grow(len(a) * 9);
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	};
	return sb.String()
}
func spaceSeperatedStringFromArray64(a []int64) string {
	sb := strings.Builder{};
	sb.Grow(len(a) * 9);
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	};
	return sb.String()
}
func minMaxFromArray(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64);
	for _, v := range args {
		if v < minVal {
			minVal = v
		};
		if v > maxVal {
			maxVal = v
		}
	};
	return minVal, maxVal
}
func minElementFromArray(args ...int64) int64 { mn, _ := minMaxFromArray(args...); return mn }
func maxElementFromArray(args ...int64) int64 { _, mx := minMaxFromArray(args...); return mx }
func lcmCalculation(a, b int64) int64         { return a / gcdCalculation(a, b) * b }
func gcdCalculation(a, b int64) int64 {
	if (a == 0) {
		return b
	};
	return gcdCalculation(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func absoluteValue(x int64) int64 {
	if x < 0 {
		return -x
	};
	return x
}
func min(args ...int) int { mn, _ := minMaxFinder(args...); return mn }
func max(args ...int) int { _, mx := minMaxFinder(args...); return mx }
func minMaxFinder(args ...int) (int, int) {
	minVal, maxVal := int(math.MaxInt32), int(math.MinInt32);
	for _, v := range args {
		if v < minVal {
			minVal = v
		};
		if v > maxVal {
			maxVal = v
		}
	};
	return minVal, maxVal
}
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}

package main

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

func main() {
	for testCase := scanInt(); testCase > 0; testCase--{
		count := scanInt()
		givenArray := scanIntSlice(count)
		for i := 0; i < count; i++{
			for j := 0; j < count; j++{
				if j - givenArray[j] != i - givenArray[i] && i < j{
					givenArray[i], givenArray[j] = givenArray[j], givenArray[i]
				}
			}
		}
		//printFmt(spaceSeperatedStringFromArray(givenArray))
		printFmt(givenArray)
	}
	close(channelType)
	oWg.Wait()
}

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

var channelType chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var printFmt func(a ...interface{}) (n int, err error)

func init() {
	printFmt = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	channelType = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range channelType {
			writer.WriteString(line + "\n")
		}
	}()
}

func scanString() string {in.Scan(); return in.Text() }
func scanInt() int {i, _ := strconv.Atoi(scanString()); return i}
func scanInt64() int64 {i, _ := strconv.ParseInt(scanString(), 10, 64); return i}
func scanStringSlice(n int) []string {s := make([]string, n); for i := 0; i < n; i++ {s[i] = scanString()}; return s}
func scanIntSlice(n int) []int {arr := make([]int, n); for i := 0; i < n; i++ {arr[i] = scanInt()}; return arr}
func scanInt64Slice(n int) []int64 {arr := make([]int64, n); for i := 0; i < n; i++ {arr[i] = scanInt64()}; return arr}

/*********************** Utils ***********************/

func stringEquivalent(v interface{}) string {return fmt.Sprintf("%v", v)}
func spaceSeperatedStringFromArray(a []int) string {sb := strings.Builder{}; sb.Grow(len(a) * 9); for i := range a {sb.WriteString(strconv.Itoa(a[i]) + " ")}; return sb.String()}
func spaceSeperatedStringFromArray64(a []int64) string {sb := strings.Builder{}; sb.Grow(len(a) * 9); for i := range a {sb.WriteString(strconv.FormatInt(a[i], 10) + " ")}; return sb.String()}
func minMaxFromArray(args ...int64) (int64, int64) {minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64); for _, v := range args {if v < minVal {minVal = v}; if v > maxVal {maxVal = v}}; return minVal, maxVal}
func minElementFromArray(args ...int64) int64 { mn, _ := minMaxFromArray(args...); return mn }
func maxElementFromArray(args ...int64) int64 { _, mx := minMaxFromArray(args...); return mx }
func lcmCalculation(a, b int64) int64 {return a / gcdCalculation(a, b) * b}
func gcdCalculation(a, b int64) int64 {if (a == 0) {return b}; return gcdCalculation(b%a, a)}
func sortInt64Slice(a []int64) {sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })}
func absoluteValue(x int64) int64 {if x < 0 {return -x}; return x}
func min(args ...int) int { mn, _ := minMaxFinder(args...); return mn }
func max(args ...int) int { _, mx := minMaxFinder(args...); return mx }
func minMaxFinder(args ...int) (int, int) { minVal, maxVal := int(math.MaxInt32), int(math.MinInt32); for _, v := range args { if v < minVal { minVal = v }; if v > maxVal { maxVal = v } }; return minVal, maxVal }

package main

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

func main() {
	for testCase := scanInt(); testCase > 0; testCase--{
		count := scanInt()
		givenArray := scanIntSlice(count)
		//answer := make([]int, count)
		for i := 0; i < count; i++{
			for j := 0; j < count; j++{
				if j - givenArray[j] != i - givenArray[i]{
					givenArray[i], givenArray[j] = givenArray[j], givenArray[i]
				}
			}
		}
		printFmt(spaceSeperatedStringFromArray(givenArray))
	}
	close(channelType)
	oWg.Wait()
}

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

var channelType chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var printFmt func(a ...interface{}) (n int, err error)

func init() {
	printFmt = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	channelType = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range channelType {
			writer.WriteString(line + "\n")
		}
	}()
}

func scanString() string {in.Scan(); return in.Text() }
func scanInt() int {i, _ := strconv.Atoi(scanString()); return i}
func scanInt64() int64 {i, _ := strconv.ParseInt(scanString(), 10, 64); return i}
func scanStringSlice(n int) []string {s := make([]string, n); for i := 0; i < n; i++ {s[i] = scanString()}; return s}
func scanIntSlice(n int) []int {arr := make([]int, n); for i := 0; i < n; i++ {arr[i] = scanInt()}; return arr}
func scanInt64Slice(n int) []int64 {arr := make([]int64, n); for i := 0; i < n; i++ {arr[i] = scanInt64()}; return arr}

/*********************** Utils ***********************/

func stringEquivalent(v interface{}) string {return fmt.Sprintf("%v", v)}
func spaceSeperatedStringFromArray(a []int) string {sb := strings.Builder{}; sb.Grow(len(a) * 9); for i := range a {sb.WriteString(strconv.Itoa(a[i]) + " ")}; return sb.String()}
func spaceSeperatedStringFromArray64(a []int64) string {sb := strings.Builder{}; sb.Grow(len(a) * 9); for i := range a {sb.WriteString(strconv.FormatInt(a[i], 10) + " ")}; return sb.String()}
func minMaxFromArray(args ...int64) (int64, int64) {minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64); for _, v := range args {if v < minVal {minVal = v}; if v > maxVal {maxVal = v}}; return minVal, maxVal}
func minElementFromArray(args ...int64) int64 { mn, _ := minMaxFromArray(args...); return mn }
func maxElementFromArray(args ...int64) int64 { _, mx := minMaxFromArray(args...); return mx }
func lcmCalculation(a, b int64) int64 {return a / gcdCalculation(a, b) * b}
func gcdCalculation(a, b int64) int64 {if (a == 0) {return b}; return gcdCalculation(b%a, a)}
func sortInt64Slice(a []int64) {sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })}
func absoluteValue(x int64) int64 {if x < 0 {return -x}; return x}
func min(args ...int) int { mn, _ := minMaxFinder(args...); return mn }
func max(args ...int) int { _, mx := minMaxFinder(args...); return mx }
func minMaxFinder(args ...int) (int, int) { minVal, maxVal := int(math.MaxInt32), int(math.MinInt32); for _, v := range args { if v < minVal { minVal = v }; if v > maxVal { maxVal = v } }; return minVal, maxVal }

package main

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

func main() {
	for t := ReadInt(); t > 0; t-- {
		n:=ReadInt()
		var a []int
		for i:=1;i<=n;i++ {
			x:=ReadInt()
			a=append(a,x)
		}
		sort.Slice(a, func(i, j int) bool {
			return a[i]>a[j]
		})
		fmt.Println(a)
	}
}

////////////////////////////////////

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

var out chan string
var in *bufio.Scanner
var outWg *sync.WaitGroup

func ReadString() string {
	in.Scan()
	return in.Text()
}
func ReadStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ReadString()
	}
	return s
}
func ReadInt() int {
	intStr := ReadString()
	i, _ := strconv.ParseInt(intStr, 10, 32)
	return int(i)
}
func ReadInt64() int64 {
	intstr := ReadString()
	i, _ := strconv.ParseInt(intstr, 10, 64)
	return int64(i)
}
func ReadIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt()
	}
	return arr
}
func init() {
	//set input
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(2e+5))
	in.Split(bufio.ScanWords)

	//set output
	out = make(chan string, 16)
	outWg = &sync.WaitGroup{}
	outWg.Add(1)

	writer := bufio.NewWriterSize(os.Stdout, int(2e+5))
	go func(write *bufio.Writer) {
		defer outWg.Done()
		defer write.Flush()

		for line := range out {
			write.WriteString(line + "\n")
		}
	}(writer)
}


After learning a lot about space exploration, a little girl named Ana wants to change the subject.Ana is a girl who loves palindromes (string that can be read the same backwards as forward). She has learned how to check for a given string whether it's a palindrome or not, but soon she grew tired of this problem, so she came up with a more interesting one and she needs your help to solve it:You are given an array of strings which consist of only small letters of the alphabet. Your task is to find how many palindrome pairs are there in the array. A palindrome pair is a pair of strings such that the following condition holds: at least one permutation of the concatenation of the two strings is a palindrome. In other words, if you have two strings, let's say "aab" and "abcac", and you concatenate them into "aababcac", we have to check if there exists a permutation of this new string such that it is a palindrome (in this case there exists the permutation "aabccbaa"). Two pairs are considered different if the strings are located on different indices. The pair of strings with indices $$$(i,j)$$$ is considered the same as the pair $$$(j,i)$$$.

The first line contains a positive integer $$$N$$$ ($$$1 \le N \le 100\,000$$$), representing the length of the input array. Eacg of the next $$$N$$$ lines contains a string (consisting of lowercase English letters from 'a' to 'z') — an element of the input array.  The total number of characters in the input array will be less than $$$1\,000\,000$$$.

Output one number, representing how many palindrome pairs there are in the array.

NoteThe first example:  aa $$$+$$$ bb $$$\to$$$ abba. The second example:  aab $$$+$$$ abcac $$$=$$$ aababcac $$$\to$$$ aabccbaa aab $$$+$$$ aa $$$=$$$ aabaa abcac $$$+$$$ aa $$$=$$$ abcacaa $$$\to$$$ aacbcaa dffe $$$+$$$ ed $$$=$$$ dffeed $$$\to$$$ fdeedf dffe $$$+$$$ aade $$$=$$$ dffeaade $$$\to$$$ adfaafde ed $$$+$$$ aade $$$=$$$ edaade $$$\to$$$ aeddea

package main

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

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

	var n int
	var s string
	mp := map[string]int{}
	for Fscan(in, &n); n > 0; n-- {
		Fscan(in, &s)
		cnt := [26]int{}
		for i := range s {
			cnt[s[i]-'a']++
		}
		t := []byte{}
		for i, c := range cnt {
			if c&1 == 1 {
				t = append(t, byte(i+'a'))
			}
		}
		mp[string(t)]++
	}
	ans := int64(0)
	for s, _c := range mp {
		c := int64(_c)
		ans += c * (c - 1) / 2
		for i := range s {
			ans += c * int64(mp[s[:i]+s[i+1:]])
		}
	}
	Fprint(out, ans)
}

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

package main

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

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

	var n int
	var s string
	mp := map[string]int{}
	cnt1 := 0
	for Fscan(in, &n); n > 0; n-- {
		Fscan(in, &s)
		cnt := [26]int{}
		for i := range s {
			cnt[s[i]-'a']++
		}
		t := []byte{}
		for i, c := range cnt {
			if c&1 == 1 {
				t = append(t, byte(i+'a'))
			}
		}
		mp[string(t)]++
		if len(t) == 1 {
			cnt1++
		}
	}
	cnt0 := int64(mp[""])
	ans := cnt0*(cnt0-1)/2 + cnt0*int64(cnt1)
	delete(mp, "")
	for _, c := range mp {
		ans += int64(c) * (int64(c) - 1) / 2
	}
	Fprint(out, ans)
}

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


Alice and Bob are playing yet another card game. This time the rules are the following. There are $$$n$$$ cards lying in a row in front of them. The $$$i$$$-th card has value $$$a_i$$$. First, Alice chooses a non-empty consecutive segment of cards $$$[l; r]$$$ ($$$l \le r$$$). After that Bob removes a single card $$$j$$$ from that segment $$$(l \le j \le r)$$$. The score of the game is the total value of the remaining cards on the segment $$$(a_l + a_{l + 1} + \dots + a_{j - 1} + a_{j + 1} + \dots + a_{r - 1} + a_r)$$$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $$$0$$$.Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.What segment should Alice choose so that the score is maximum possible? Output the maximum score.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of cards. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-30 \le a_i \le 30$$$) — the values on the cards.

Print a single integer — the final score of the game.

NoteIn the first example Alice chooses a segment $$$[1;5]$$$ — the entire row of cards. Bob removes card $$$3$$$ with the value $$$10$$$ from the segment. Thus, the final score is $$$5 + (-2) + (-1) + 4 = 6$$$.In the second example Alice chooses a segment $$$[1;4]$$$, so that Bob removes either card $$$1$$$ or $$$3$$$ with the value $$$5$$$, making the answer $$$5 + 2 + 3 = 10$$$.In the third example Alice can choose any of the segments of length $$$1$$$: $$$[1;1]$$$, $$$[2;2]$$$ or $$$[3;3]$$$. Bob removes the only card, so the score is $$$0$$$. If Alice chooses some other segment then the answer will be less than $$$0$$$.

package main

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

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

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

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

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

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

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

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

	return i
}

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

	n := readNum(reader)
	A := readNNums(reader, n)

	fmt.Println(solve(n, A))
}

const INF = math.MaxInt32 >> 1
const N_INF = -INF

func solve(n int, A []int) int {
	var ans int64
	for mx := int64(0); mx < 31; mx++ {
		var cur int64
		var best int64

		for i := 0; i < n; i++ {
			v := int64(A[i])
			if v > mx {
				v = N_INF
			}
			cur += v
			if cur < best {
				best = cur
			}
			if cur-best-mx > ans {
				ans = cur - best - mx
			}
		}
	}

	return int(ans)
}

func solve1(n int, A []int) int {
	N := n + 1
	arr := make([]int, 2*N)

	reset := func() {
		for i := 0; i < len(arr); i++ {
			arr[i] = INF
		}
	}

	reset()

	update := func(p int, v int) {
		p += N
		arr[p] = v
		for p > 1 {
			arr[p>>1] = min(arr[p], arr[p^1])
			p >>= 1
		}
	}

	get := func(l, r int) int {
		var res = INF

		l += N
		r += N

		for l < r {
			if l&1 == 1 {
				res = min(res, arr[l])
				l++
			}
			if r&1 == 1 {
				r--
				res = min(res, arr[r])
			}
			l >>= 1
			r >>= 1
		}
		return res
	}

	stack := make([]int, n)
	var p int

	left := make([]int, n)
	var sum int
	update(0, sum)
	for i := 0; i < n; i++ {
		for p > 0 && A[stack[p-1]] <= A[i] {
			p--
		}
		var j = 0
		if p > 0 {
			j = stack[p-1] + 1
		}
		sum += A[i]
		update(i+1, sum)
		left[i] = sum - get(j, i+1)
		stack[p] = i
		p++
	}

	reset()

	sum = 0
	p = 0
	var best = 0
	update(0, sum)
	// i + j == n
	for i := n - 1; i >= 0; i-- {
		for p > 0 && A[stack[p-1]] <= A[i] {
			p--
		}

		sum += A[i]
		update(n-i, sum)

		var j = 0
		if p > 0 {
			j = n - stack[p-1] + 1
		}
		tmp := sum - get(j, n-i)

		if tmp+left[i]-2*A[i] > best {
			best = tmp + left[i] - 2*A[i]
		}
		stack[p] = i
		p++
	}

	return best
}

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

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

package main

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

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

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

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

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

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

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

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

	return i
}

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

	n := readNum(reader)
	A := readNNums(reader, n)

	fmt.Println(solve(n, A))
}

const INF = math.MaxInt32 >> 1

func solve(n int, A []int) int {
	N := n + 1
	arr := make([]int, 2*N)

	reset := func() {
		for i := 0; i < len(arr); i++ {
			arr[i] = INF
		}
	}

	reset()

	update := func(p int, v int) {
		p += N
		arr[p] = v
		for p > 1 {
			arr[p>>1] = min(arr[p], arr[p^1])
			p >>= 1
		}
	}

	get := func(l, r int) int {
		var res = INF

		l += N
		r += N

		for l < r {
			if l&1 == 1 {
				res = min(res, arr[l])
				l++
			}
			if r&1 == 1 {
				r--
				res = min(res, arr[r])
			}
			l >>= 1
			r >>= 1
		}
		return res
	}

	stack := make([]int, n)
	var p int

	left := make([]int, n)
	var sum int
	update(0, sum)
	for i := 0; i < n; i++ {
		for p > 0 && A[stack[p-1]] <= A[i] {
			p--
		}
		var j = 0
		if p > 0 {
			j = stack[p-1] + 1
		}
		sum += A[i]
		update(i+1, sum)
		left[i] = sum - get(j, i+1)
		stack[p] = i
		p++
	}

	reset()

	sum = 0
	p = 0
	var best = 0
	update(0, sum)
	// i + j == n
	for i := n - 1; i >= 0; i-- {
		for p > 0 && A[stack[p-1]] <= A[i] {
			p--
		}

		sum += A[i]
		update(n-i, sum)

		var j = 0
		if p > 0 {
			j = n - stack[p-1] + 1
		}
		tmp := sum - get(j, n-i)

		if tmp+left[i]-2*A[i] > best {
			best = tmp + left[i] - 2*A[i]
		}
		stack[p] = i
		p++
	}

	return best
}

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

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

package main

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

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

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

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

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

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

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

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

	return i
}

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

	n := readNum(reader)
	A := readNNums(reader, n)

	fmt.Println(solve(n, A))
}

const INF = math.MaxInt32 >> 1
const N_INF = -INF

func solve(n int, A []int) int {
	var ans int64
	for mx := int64(0); mx < 31; mx++ {
		var cur int64
		var best int64

		for i := 0; i < n; i++ {
			v := int64(A[i])
			if v > mx {
				v = N_INF
			}
			cur += v
			if cur > best {
				best = cur
			}
			if cur < 0 {
				cur = 0
			}
		}

		if best-mx > ans {
			ans = best - mx
		}
	}

	return int(ans)
}

func solve2(n int, A []int) int {
	var ans int64
	for mx := int64(0); mx < 31; mx++ {
		var cur int64
		var best int64

		for i := 0; i < n; i++ {
			v := int64(A[i])
			if v > mx {
				v = N_INF
			}
			cur += v
			if cur < best {
				best = cur
			}
			if cur-best-mx > ans {
				ans = cur - best - mx
			}
		}
	}

	return int(ans)
}

func solve1(n int, A []int) int {
	N := n + 1
	arr := make([]int, 2*N)

	reset := func() {
		for i := 0; i < len(arr); i++ {
			arr[i] = INF
		}
	}

	reset()

	update := func(p int, v int) {
		p += N
		arr[p] = v
		for p > 1 {
			arr[p>>1] = min(arr[p], arr[p^1])
			p >>= 1
		}
	}

	get := func(l, r int) int {
		var res = INF

		l += N
		r += N

		for l < r {
			if l&1 == 1 {
				res = min(res, arr[l])
				l++
			}
			if r&1 == 1 {
				r--
				res = min(res, arr[r])
			}
			l >>= 1
			r >>= 1
		}
		return res
	}

	stack := make([]int, n)
	var p int

	left := make([]int, n)
	var sum int
	update(0, sum)
	for i := 0; i < n; i++ {
		for p > 0 && A[stack[p-1]] <= A[i] {
			p--
		}
		var j = 0
		if p > 0 {
			j = stack[p-1] + 1
		}
		sum += A[i]
		update(i+1, sum)
		left[i] = sum - get(j, i+1)
		stack[p] = i
		p++
	}

	reset()

	sum = 0
	p = 0
	var best = 0
	update(0, sum)
	// i + j == n
	for i := n - 1; i >= 0; i-- {
		for p > 0 && A[stack[p-1]] <= A[i] {
			p--
		}

		sum += A[i]
		update(n-i, sum)

		var j = 0
		if p > 0 {
			j = n - stack[p-1] + 1
		}
		tmp := sum - get(j, n-i)

		if tmp+left[i]-2*A[i] > best {
			best = tmp + left[i] - 2*A[i]
		}
		stack[p] = i
		p++
	}

	return best
}

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

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

package main

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

var nn int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Fscanf(in, "%d\n", &q)
	for ii := 0; ii < q; ii++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")
		f := make([][]int, n)
		for i := 0; i < n; i++ {
			f[i] = make([]int, 31)
			for j := 0; j < 31; j++ {
				f[i][j] = -1000
			}
		}
		f[0][0] = 0
		if a[0] > 0 {
			f[0][a[0]] = 0
		}
		//fmt.Println(f[0])
		for i := 1; i < n; i++ {
			if a[i] > 0 {
				f[i][a[i]] = 0
			} else {
				f[i][0] = 0
			}
			for j := 0; j < 31; j++ {
				if f[i-1][j] != -1000 {
					if a[i] > j {
						f[i][a[i]] = max(f[i][a[i]], f[i-1][j]+j)
					} else {
						f[i][j] = max(f[i][j], f[i-1][j]+a[i])
					}
				}
			}
			//fmt.Println(f[i])
		}
		ans := 0
		for i := 0; i < n; i++ {
			for j := 0; j < 31; j++ {
				if f[i][j] > ans {
					ans = f[i][j]
				}
			}
		}
		fmt.Fprintf(out, "%d\n", ans)
	}
	return
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1359D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, ans int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}
	for mx := 1; mx <= 30; mx++ {
		i := 0
		for {
			for ; i < n && a[i] > mx; i++ {
			}
			if i == n {
				break
			}
			s, mxS := a[i], a[i]
			for i++; i < n && a[i] <= mx; i++ {
				s = max(s+a[i], a[i])
				mxS = max(mxS, s)
			}
			ans = max(ans, mxS-mx)
		}
	}
	Fprint(out, ans)
}

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


Ralph is going to collect mushrooms in the Mushroom Forest. There are m directed paths connecting n trees in the Mushroom Forest. On each path grow some mushrooms. When Ralph passes a path, he collects all the mushrooms on the path. The Mushroom Forest has a magical fertile ground where mushrooms grow at a fantastic speed. New mushrooms regrow as soon as Ralph finishes mushroom collection on a path. More specifically, after Ralph passes a path the i-th time, there regrow i mushrooms less than there was before this pass. That is, if there is initially x mushrooms on a path, then Ralph will collect x mushrooms for the first time, x - 1 mushrooms the second time, x - 1 - 2 mushrooms the third time, and so on. However, the number of mushrooms can never be less than 0.For example, let there be 9 mushrooms on a path initially. The number of mushrooms that can be collected from the path is 9, 8, 6 and 3 when Ralph passes by from first to fourth time. From the fifth time and later Ralph can't collect any mushrooms from the path (but still can pass it).Ralph decided to start from the tree s. How many mushrooms can he collect using only described paths?

The first line contains two integers n and m (1 ≤ n ≤ 106, 0 ≤ m ≤ 106), representing the number of trees and the number of directed paths in the Mushroom Forest, respectively. Each of the following m lines contains three integers x, y and w (1 ≤ x, y ≤ n, 0 ≤ w ≤ 108), denoting a path that leads from tree x to tree y with w mushrooms initially. There can be paths that lead from a tree to itself, and multiple paths between the same pair of trees. The last line contains a single integer s (1 ≤ s ≤ n) — the starting position of Ralph.

Print an integer denoting the maximum number of the mushrooms Ralph can collect during his route.

NoteIn the first sample Ralph can pass three times on the circle and collect 4 + 4 + 3 + 3 + 1 + 1 = 16 mushrooms. After that there will be no mushrooms for Ralph to collect.In the second sample, Ralph can go to tree 3 and collect 8 mushrooms on the path from tree 1 to tree 3.

package main

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

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

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

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

type pair struct{
	a,b int
}

var n,m,c,new_name int
var vertex_weight int64
var g,gr,out [1000006][]pair
var edge_sum map[int64]int64
var vertex,sum,dp [1000006]int64
var used [1000006]bool
var name,comp [1000006]int
var ans,order,component []int

func calc(x int) int64 {
	f := (math.Sqrt(1+8*float64(x)) - 1) / 2
	var e int = int(f)
	if (e + 1)*(e + 2) /2 <= x {
		e++
	}
	return int64(x) * int64(e + 1) - sum[e]
}

func dfs(v int) {
	used[v] = true
	for i:= range out[v] {
		to := out[v][i].a
		to = name[to]
		if !used[to] {
			dfs(to)
		}
	}
	ans = append(ans, v)
}

func dfs1(v int) {
	used[v] = true
	for i:= range g[v] {
		if !used[g[v][i].a] {
			dfs1(g[v][i].a)
		}
	}
	order = append (order, v)
}

func dfs2(v,c int) {
	comp[v] = c
	new_name = min(new_name, v)
	component = append(component, v)
	vertex_weight += vertex[v]
	for i:= range gr[v]{
		to := gr[v][i].a
		w := gr[v][i].b
		if comp[to] == comp[v] || comp[to] == 0{
			vertex_weight += calc(w)
		}
		if comp[to] == 0 {
			dfs2(to,c)
		}
	}
}

func main() {
	for i:=1;i<=14200;i++ {
		sum[i] = sum[i-1] + int64(i)
	}
	for i:=1;i<=14200;i++ {
		sum[i] += sum[i-1]
	}

	fmt.Scanf("%d%d",&n,&m)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i:=0;i<m;i++{
		scanner.Scan()
		var a,b,w int
		a = toInt(scanner.Bytes())
		scanner.Scan()
		b = toInt(scanner.Bytes())
		scanner.Scan()
		w = toInt(scanner.Bytes())
		val := calc(w)
		// Remove loops

		if a == b {
			vertex[a] += val
			continue
		}

		// Adding the edge

		g[a] = append(g[a], pair{b,w})
		gr[b] = append(gr[b], pair{a,w})
	}
	for i:=1;i<=n;i++ {
		if !used[i] {
			dfs1(i)
		}
	}

	for i:=1;i<=n;i++ {
		v := order[n-i]
		if comp[v] == 0 {
			vertex_weight = 0
			new_name = 1000006
			c++
			dfs2(v,c)

			for i:= range component {
				name[component[i]] = new_name
				for j := range g[component[i]] {
					out[new_name] = append(out[new_name], g[component[i]][j])
				}
			}
			vertex[new_name] = vertex_weight
			component = nil
		}
	}

	for i:= range used {
		used[i] = false
	}
	for i:=1;i<=n;i++{
		if !used[name[i]] {
			dfs(name[i])
		}
	}
	for i:= range ans {
		v := ans[i]
		dp[v] = max(dp[v], vertex[v])
		for j:= range out[v]{
			to := name[out[v][j].a]
			w := out[v][j].b
			if v == to {
				continue
			}
			dp[v] = max(dp[v], vertex[v] + int64(w) + dp[to])
		}
	}
	var s int
	scanner.Scan()
	s = toInt(scanner.Bytes())
	fmt.Println(dp[name[s]])
}

package main

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

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

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

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

type pair struct{
	a,b int
}

var n,m,c,new_name int
var vertex_weight int64
var g,gr,out [1000006][]pair
var edge_sum map[int64]int64
var vertex,sum,dp [1000006]int64
var used [1000006]bool
var name,comp [1000006]int
var ans,order,component []int

func calc(x int) int64 {
	f := (math.Sqrt(1+8*float64(x)) - 1) / 2
	var e int = int(f)
	if (e + 1)*(e + 2) /2 <= x {
		e++
	}
	return int64(x) * int64(e + 1) - sum[e]
}

func dfs(v int) {
	used[v] = true
	for i:= range out[v] {
		to := out[v][i].a
		to = name[to]
		if !used[to] {
			dfs(to)
		}
	}
	ans = append(ans, v)
}

func dfs1(v int) {
	used[v] = true
	for i:= range g[v] {
		if !used[g[v][i].a] {
			dfs1(g[v][i].a)
		}
	}
	order = append (order, v)
}

func dfs2(v,c int) {
	comp[v] = c
	new_name = min(new_name, v)
	component = append(component, v)
	vertex_weight += vertex[v]
	for i:= range gr[v]{
		to := gr[v][i].a
		w := gr[v][i].b
		if comp[to] == comp[v] || comp[to] == 0{
			vertex_weight += calc(w)
		}
		if comp[to] == 0 {
			dfs2(to,c)
		}
	}
}

func main() {
	for i:=1;i<=14200;i++ {
		sum[i] = sum[i-1] + int64(i)
	}
	for i:=1;i<=14200;i++ {
		sum[i] += sum[i-1]
	}

	fmt.Scanf("%d%d",&n,&m)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i:=0;i<m;i++{
		scanner.Scan()
		var a,b,w int
		a = toInt(scanner.Bytes())
		scanner.Scan()
		b = toInt(scanner.Bytes())
		scanner.Scan()
		w = toInt(scanner.Bytes())
		val := calc(w)
		// Remove loops

		if a == b {
			vertex[a] += val
			continue
		}

		// Adding the edge

		g[a] = append(g[a], pair{b,w})
		gr[b] = append(gr[b], pair{a,w})
	}
	for i:=1;i<=n;i++ {
		if !used[i] {
			dfs1(i)
		}
	}

	for i:=1;i<=n;i++ {
		v := order[n-i]
		if comp[v] == 0 {
			vertex_weight = 0
			new_name = 1000006
			c++
			dfs2(v,c)

			for i:= range component {
				name[component[i]] = new_name
				for j := range g[component[i]] {
					out[new_name] = append(out[new_name], g[component[i]][j])
				}
			}
			vertex[new_name] = vertex_weight
			component = nil
		}
	}

	for i:= range used {
		used[i] = false
	}
	for i:=1;i<=n;i++{
		if !used[name[i]] {
			dfs(name[i])
		}
	}
	for i:= range ans {
		v := ans[i]
		dp[v] = max(dp[v], vertex[v])
		for j:= range out[v]{
			to := name[out[v][j].a]
			w := out[v][j].b
			if v == to {
				continue
			}
			dp[v] = max(dp[v], vertex[v] + int64(w) + dp[to])
		}
	}
	var s int
	fmt.Scanf("%d",&s)
	fmt.Println(dp[name[s]])
}


You are given an array $$$a_1, a_2, \ldots, a_n$$$ of integers. This array is non-increasing.Let's consider a line with $$$n$$$ shops. The shops are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. The cost of a meal in the $$$i$$$-th shop is equal to $$$a_i$$$.You should process $$$q$$$ queries of two types:  1 x y: for each shop $$$1 \leq i \leq x$$$ set $$$a_{i} = max(a_{i}, y)$$$.  2 x y: let's consider a hungry man with $$$y$$$ money. He visits the shops from $$$x$$$-th shop to $$$n$$$-th and if he can buy a meal in the current shop he buys one item of it. Find how many meals he will purchase. The man can buy a meal in the shop $$$i$$$ if he has at least $$$a_i$$$ money, and after it his money decreases by $$$a_i$$$.

The first line contains two integers $$$n$$$, $$$q$$$ ($$$1 \leq n, q \leq 2 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_{1},a_{2}, \ldots, a_{n}$$$ $$$(1 \leq a_{i} \leq 10^9)$$$ — the costs of the meals. It is guaranteed, that $$$a_1 \geq a_2 \geq \ldots \geq a_n$$$. Each of the next $$$q$$$ lines contains three integers $$$t$$$, $$$x$$$, $$$y$$$ ($$$1 \leq t \leq 2$$$, $$$1\leq x \leq n$$$, $$$1 \leq y \leq 10^9$$$), each describing the next query. It is guaranteed that there exists at least one query of type $$$2$$$.

For each query of type $$$2$$$ output the answer on the new line.

NoteIn the first query a hungry man will buy meals in all shops from $$$3$$$ to $$$10$$$.In the second query a hungry man will buy meals in shops $$$4$$$, $$$9$$$, and $$$10$$$.After the third query the array $$$a_1, a_2, \ldots, a_n$$$ of costs won't change and will be $$$\{10, 10, 10, 6, 6, 5, 5, 5, 3, 1\}$$$.In the fourth query a hungry man will buy meals in shops $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$9$$$, and $$$10$$$.After the fifth query the array $$$a$$$ of costs will be $$$\{10, 10, 10, 7, 6, 5, 5, 5, 3, 1\}$$$.In the sixth query a hungry man will buy meals in shops $$$2$$$ and $$$4$$$.

package main

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

// github.com/EndlessCheng/codeforces-go
type seg []struct {l, r, todo, mx, mi int; s int64}

func (t seg) maintain(o int) {
	l, r := t[o<<1], t[o<<1|1]
	t[o].mx = l.mx
	t[o].mi = r.mi
	t[o].s = l.s + r.s
}

func (t seg) build(a []int, o, l, r int) {
	t[o].l, t[o].r = l, r
	if l == r {
		t[o].mx = a[l-1]
		t[o].mi = a[l-1]
		t[o].s = int64(a[l-1])
		return
	}
	m := (l + r) >> 1
	t.build(a, o<<1, l, m)
	t.build(a, o<<1|1, m+1, r)
	t.maintain(o)
}

func (t seg) do(O, v int) {
	o := &t[O]
	o.todo = v
	o.mx = v
	o.mi = v
	o.s = int64(o.r-o.l+1) * int64(v)
}

func (t seg) spread(o int) {
	if v := t[o].todo; v > 0 {
		t.do(o<<1, v)
		t.do(o<<1|1, v)
		t[o].todo = 0
	}
}

func (t seg) update(o, r, v int) {
	if t[o].l > r || v <= t[o].mi {
		return
	}
	if t[o].r <= r && v >= t[o].mx {
		t.do(o, v)
		return
	}
	t.spread(o)
	t.update(o<<1, r, v)
	t.update(o<<1|1, r, v)
	t.maintain(o)
}

func (t seg) query(o, l int, v *int64) int {
	if t[o].r < l || *v < int64(t[o].mi) {
		return 0
	}
	if l <= t[o].l && *v >= t[o].s {
		*v -= t[o].s
		return t[o].r - t[o].l + 1
	}
	t.spread(o)
	return t.query(o<<1, l, v) + t.query(o<<1|1, l, v)
}

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

	var n, q, tp, p, v int
	Fscan(in, &n, &q)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	t := make(seg, 4*n)
	t.build(a, 1, 1, n)
	for ; q > 0; q-- {
		if Fscan(in, &tp, &p, &v); tp == 1 {
			t.update(1, p, v)
		} else {
			v := int64(v)
			Fprintln(out, t.query(1, p, &v))
		}
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
type seg []struct {
	l, r, todo, max, min int
	sum                  int64
}

func (t seg) maintain(o int) {
	l, r := t[o<<1], t[o<<1|1]
	t[o].max = l.max
	t[o].min = r.min
	t[o].sum = l.sum + r.sum
}

func (t seg) build(a []int, o, l, r int) {
	t[o].l, t[o].r = l, r
	if l == r {
		t[o].max = a[l-1]
		t[o].min = a[l-1]
		t[o].sum = int64(a[l-1])
		return
	}
	m := (l + r) >> 1
	t.build(a, o<<1, l, m)
	t.build(a, o<<1|1, m+1, r)
	t.maintain(o)
}

func (t seg) do(O, v int) {
	o := &t[O]
	o.todo = v
	o.max = v
	o.min = v
	o.sum = int64(o.r-o.l+1) * int64(v)
}

func (t seg) spread(o int) {
	if v := t[o].todo; v > 0 {
		t.do(o<<1, v)
		t.do(o<<1|1, v)
		t[o].todo = 0
	}
}

func (t seg) update(o, r, v int) {
	if v <= t[o].min {
		return
	}
	if t[o].r <= r && v >= t[o].max {
		t.do(o, v)
		return
	}
	t.spread(o)
	t.update(o<<1, r, v)
	if (t[o].l+t[o].r)>>1 < r {
		t.update(o<<1|1, r, v)
	}
	t.maintain(o)
}

func (t seg) query(o, l int, v *int64) int {
	if *v < int64(t[o].min) {
		return 0
	}
	if l <= t[o].l && *v >= t[o].sum {
		*v -= t[o].sum
		return t[o].r - t[o].l + 1
	}
	t.spread(o)
	m := (t[o].l + t[o].r) >> 1
	if l > m {
		return t.query(o<<1|1, l, v)
	}
	vl := t.query(o<<1, l, v)
	vr := t.query(o<<1|1, l, v)
	return vl + vr
}

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

	var n, q, tp, p, v int
	Fscan(in, &n, &q)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	t := make(seg, 4*n)
	t.build(a, 1, 1, n)
	for ; q > 0; q-- {
		if Fscan(in, &tp, &p, &v); tp == 1 {
			t.update(1, p, v)
		} else {
			v := int64(v)
			Fprintln(out, t.query(1, p, &v))
		}
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
type seg []struct {
	l, r, todo, min int
	sum             int64
}

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

func (t seg) maintain(o int) {
	l, r := t[o<<1], t[o<<1|1]
	t[o].min = min(l.min, r.min)
	t[o].sum = l.sum + r.sum
}

func (t seg) build(a []int, o, l, r int) {
	t[o].l, t[o].r = l, r
	if l == r {
		t[o].min = a[l-1]
		t[o].sum = int64(a[l-1])
		return
	}
	m := (l + r) >> 1
	t.build(a, o<<1, l, m)
	t.build(a, o<<1|1, m+1, r)
	t.maintain(o)
}

func (t seg) do(O, v int) {
	o := &t[O]
	o.min = v
	o.sum = int64(o.r-o.l+1) * int64(v)
}

func (t seg) spread(o int) {
	if v := t[o].todo; v != 0 {
		t.do(o<<1, v)
		t.do(o<<1|1, v)
		t[o].todo = 0
	}
}

func (t seg) update(o, l, r, v int) {
	if v <= t[o].min {
		return
	}
	if l <= t[o].l && t[o].r <= r {
		t.do(o, v)
		return
	}
	t.spread(o)
	m := (t[o].l + t[o].r) >> 1
	if l <= m {
		t.update(o<<1, l, r, v)
	}
	if m < r {
		t.update(o<<1|1, l, r, v)
	}
	t.maintain(o)
}

func (t seg) query(o, l int, v *int64) int {
	if *v < int64(t[o].min) {
		return 0
	}
	if l <= t[o].l && *v >= t[o].sum {
		*v -= t[o].sum
		return t[o].r - t[o].l + 1
	}
	t.spread(o)
	m := (t[o].l + t[o].r) >> 1
	if l > m {
		return t.query(o<<1|1, l, v)
	}
	vl := t.query(o<<1, l, v)
	vr := t.query(o<<1|1, l, v)
	return vl + vr
}

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

	var n, q, tp, p, v int
	Fscan(in, &n, &q)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	t := make(seg, 4*len(a))
	t.build(a, 1, 1, len(a))
	for ; q > 0; q-- {
		if Fscan(in, &tp, &p, &v); tp == 1 {
			t.update(1, 1, p, v)
		} else {
			v := int64(v)
			Fprintln(out, t.query(1, p, &v))
		}
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
type seg []struct {
	l, r, todo, min int
	sum             int64
}

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

func (t seg) maintain(o int) {
	l, r := t[o<<1], t[o<<1|1]
	t[o].min = min(l.min, r.min)
	t[o].sum = l.sum + r.sum
}

func (t seg) build(a []int, o, l, r int) {
	t[o].l, t[o].r = l, r
	if l == r {
		t[o].min = a[l-1]
		t[o].sum = int64(a[l-1])
		return
	}
	m := (l + r) >> 1
	t.build(a, o<<1, l, m)
	t.build(a, o<<1|1, m+1, r)
	t.maintain(o)
}

func (t seg) do(O, v int) {
	o := &t[O]
	o.todo = v
	o.min = v
	o.sum = int64(o.r-o.l+1) * int64(v)
}

func (t seg) spread(o int) {
	if v := t[o].todo; v != 0 {
		t.do(o<<1, v)
		t.do(o<<1|1, v)
		t[o].todo = 0
	}
}

func (t seg) update(o, r, v int) {
	if v <= t[o].min {
		return
	}
	if t[o].r <= r {
		t.do(o, v)
		return
	}
	t.spread(o)
	t.update(o<<1, r, v)
	if (t[o].l+t[o].r)>>1 < r {
		t.update(o<<1|1, r, v)
	}
	t.maintain(o)
}

func (t seg) query(o, l int, v *int64) int {
	if *v < int64(t[o].min) {
		return 0
	}
	if l <= t[o].l && *v >= t[o].sum {
		*v -= t[o].sum
		return t[o].r - t[o].l + 1
	}
	t.spread(o)
	m := (t[o].l + t[o].r) >> 1
	if l > m {
		return t.query(o<<1|1, l, v)
	}
	vl := t.query(o<<1, l, v)
	vr := t.query(o<<1|1, l, v)
	return vl + vr
}

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

	var n, q, tp, p, v int
	Fscan(in, &n, &q)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	t := make(seg, 4*n)
	t.build(a, 1, 1, n)
	for ; q > 0; q-- {
		if Fscan(in, &tp, &p, &v); tp == 1 {
			t.update(1, p, v)
		} else {
			v := int64(v)
			Fprintln(out, t.query(1, p, &v))
		}
	}
}

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


This is an interactive problem.Natasha is going to fly to Mars. Finally, Natasha sat in the rocket. She flies, flies... but gets bored. She wishes to arrive to Mars already! So she decides to find something to occupy herself. She couldn't think of anything better to do than to calculate the distance to the red planet.Let's define $$$x$$$ as the distance to Mars. Unfortunately, Natasha does not know $$$x$$$. But it is known that $$$1 \le x \le m$$$, where Natasha knows the number $$$m$$$. Besides, $$$x$$$ and $$$m$$$ are positive integers.Natasha can ask the rocket questions. Every question is an integer $$$y$$$ ($$$1 \le y \le m$$$). The correct answer to the question is $$$-1$$$, if $$$x<y$$$, $$$0$$$, if $$$x=y$$$, and $$$1$$$, if $$$x>y$$$. But the rocket is broken — it does not always answer correctly. Precisely: let the correct answer to the current question be equal to $$$t$$$, then, if the rocket answers this question correctly, then it will answer $$$t$$$, otherwise it will answer $$$-t$$$.In addition, the rocket has a sequence $$$p$$$ of length $$$n$$$. Each element of the sequence is either $$$0$$$ or $$$1$$$. The rocket processes this sequence in the cyclic order, that is $$$1$$$-st element, $$$2$$$-nd, $$$3$$$-rd, $$$\ldots$$$, $$$(n-1)$$$-th, $$$n$$$-th, $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$\ldots$$$, $$$(n-1)$$$-th, $$$n$$$-th, $$$\ldots$$$. If the current element is $$$1$$$, the rocket answers correctly, if $$$0$$$ — lies. Natasha doesn't know the sequence $$$p$$$, but she knows its length — $$$n$$$.You can ask the rocket no more than $$$60$$$ questions.Help Natasha find the distance to Mars. Assume, that the distance to Mars does not change while Natasha is asking questions.Your solution will not be accepted, if it does not receive an answer $$$0$$$ from the rocket (even if the distance to Mars is uniquely determined by the already received rocket's answers).

The first line contains two integers $$$m$$$ and $$$n$$$ ($$$1 \le m \le 10^9$$$, $$$1 \le n \le 30$$$) — the maximum distance to Mars and the number of elements in the sequence $$$p$$$.

NoteIn the example, hacking would look like this:5 2 31 0This means that the current distance to Mars is equal to $$$3$$$, Natasha knows that it does not exceed $$$5$$$, and the rocket answers in order: correctly, incorrectly, correctly, incorrectly ...Really:on the first query ($$$1$$$) the correct answer is $$$1$$$, the rocket answered correctly: $$$1$$$;on the second query ($$$2$$$) the correct answer is $$$1$$$, the rocket answered incorrectly: $$$-1$$$;on the third query ($$$4$$$) the correct answer is $$$-1$$$, the rocket answered correctly: $$$-1$$$;on the fourth query ($$$5$$$) the correct answer is $$$-1$$$, the rocket answered incorrectly: $$$1$$$;on the fifth query ($$$3$$$) the correct and incorrect answer is $$$0$$$.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1010B() {
	var m, n, t int
	Scan(&m, &n)
	a := make([]bool, n)
	for i := range a {
		Println(1)
		Scan(&t)
		if t == 0 {
			return
		}
		a[i] = t == 1
	}
	cnt := 0
	sort.Search(m+1, func(x int) bool {
		if x == 0 {
			return false
		}
		Println(x)
		Scan(&t)
		if t == 0 {
			os.Exit(0)
		}
		if !a[cnt%n] {
			t = -t
		}
		cnt++
		return t == -1
	})
}

func main() { CF1010B() }

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m := 0, 0
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	truthT := make([]bool, m)
	for i := 0; i < m; i++ {
		fmt.Fprintf(out, "%d\n", 1)
		out.Flush()
		res := 0
		fmt.Fscanf(in, "%d\n", &res)
		if res == 0 {
			return
		}
		if res == 1 {
			truthT[i] = true
		} else {
			truthT[i] = false
		}
	}
	l := 1
	r := n
	for i := 0; i < 600; i++ {
		mid := (l + r) / 2
		fmt.Fprintf(out, "%d\n", mid)
		out.Flush()
		res := 0
		fmt.Fscanf(in, "%d\n", &res)
		if res == 0 {
			return
		}
		if !truthT[i%m] {
			res = -res
		}
		if res == 1 {
			l = mid + 1
		} else {
			r = mid - 1
		}
	}
	return
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1010B() {
	var m, n, res, ans int
	Scan(&m, &n)
	a := make([]bool, n)
	for i := range a {
		Println(0)
		Scan(&res)
		a[i] = res == 1
	}
	cnt := 0
	sort.Search(m+1, func(x int) bool {
		if x == 0 {
			return false
		}
		if ans > 0 {
			return x >= ans
		}
		Println(x)
		Scan(&res)
		if res == 0 {
			os.Exit(0)
		}
		if !a[cnt%n] {
			res = -res
		}
		cnt++
		return res == -1
	})
}

func main() { CF1010B() }

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1010B() {
	var m, n, t int
	Scan(&m, &n)
	a := make([]bool, n)
	for i := range a {
		Println(-1)
		Scan(&t)
		a[i] = t == 1
	}
	cnt := 0
	sort.Search(m+1, func(x int) bool {
		if x == 0 {
			return false
		}
		Println(x)
		Scan(&t)
		if t == 0 {
			os.Exit(0)
		}
		if !a[cnt%n] {
			t = -t
		}
		cnt++
		return t == -1
	})
}

func main() { CF1010B() }

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m := 0, 0
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	truthT := make([]bool, m)
	for i := 0; i < m; i++ {
		fmt.Fprintf(out, "%d\n", 1)
		out.Flush()
		res := 0
		fmt.Fscanf(in, "%d\n", &res)
		if res == 0 {
			return
		}
		if res == 1 {
			truthT[i] = true
		} else {
			truthT[i] = false
		}
	}
	l := 1
	r := n
	for i := 0; i < m; i++ {
		mid := (l + r) / 2
		fmt.Fprintf(out, "%d\n", mid)
		out.Flush()
		res := 0
		fmt.Fscanf(in, "%d\n", &res)
		if res == 0 {
			return
		}
		if !truthT[i] {
			res = -res
		}
		if res == 1 {
			l = mid + 1
		} else {
			r = mid - 1
		}
	}
	return
}