package main

import (
	"fmt"
	"sort"
)

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

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

type Pair struct {
	first, second int
}

type byFirst []Pair

func (s byFirst) Len() int {
	return len(s)
}

func (s byFirst) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

func (s byFirst) Less(i, j int) bool {
	return s[i].first < s[j].first
}

type bySecond []Pair

func (s bySecond) Len() int {
	return len(s)
}

func (s bySecond) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

func (s bySecond) Less(i, j int) bool {
	return s[i].second < s[j].second
}

func main() {
	var t int
	fmt.Scan(&t)
	for t > 0 {
		var s string
		fmt.Scan(&s)

		n := len(s)

		blocks := make([]int, 0)

		j := -1
		for i := 0; i < n; i++ {
			if s[i] == '0' {
				if j == -1 {
					continue
				}
				blocks = append(blocks, i-j)
				j = -1
			} else {
				if j == -1 {
					j = i
				}
			}
		}
		if j != -1 {
			blocks = append(blocks, n-j)
		}

		sort.Ints(blocks)

		score := 0
		for i := len(blocks) - 1; i >= 0; i -= 2 {
			score += blocks[i]
		}

		fmt.Println(score)

		t--
	}
}

package main

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

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

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

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

var err error

func next() (n string) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextLong() (n int64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextDouble() (n float64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func Fill(a []int, v int) {
	for i := range a {
		a[i] = v
	}
}

func FillB(a []bool, v bool) {
	for i := range a {
		a[i] = v
	}
}

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

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

func Abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

const (
	oo  = 100000000
	mod = 1
)

func mult(a, b int64) int64 {
	return (a * b) % mod
}
func add(a, b int64) int64 {
	return (a + b) % mod
}
func sub(a, b int64) int64 {
	return (a - b + mod) % mod
}

var (
	base, a, b map[string]string
	dp         []map[string]int
	pat        string
)

func dfs(i int, x string) int {
	{
		a, test := dp[i][x]
		if test {
			return a
		}
	}
	res := i
	if base[x] != "" {
		for _, c := range []byte(base[x]) {
			if res < len(pat) && pat[res] == c {
				res++
			}
		}
	} else {
		res = dfs(dfs(i, a[x]), b[x])
	}
	dp[i][x] = res
	return res
}

func solve() {
	arr := []int{}
	{
		strin := []byte(next())
		num := 0
		for _, c := range strin {
			if c == '0' {
				if num != 0 {
					arr = append(arr, num)
				}
				num = 0
			} else {
				num++
			}
		}
		if num != 0 {
			arr = append(arr, num)
		}
	}
	sort.Ints(arr)
	tot := [2]int{}
	for i, c := range arr {
		tot[i%2] += c
	}
	printf("%d\n", tot[(len(arr)+1)%2])
}

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

	// start := time.Now()

	for t := nextInt(); t > 0; t-- {
		solve()
	}

	// printf("%v\n", time.Now().Sub(start))
}

package main

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

func main() {
	var t int
	var s string
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &s)
		count := make([]int, 0)
		c := 0
		for _, v := range s {
			if v == '1' {
				c++
			} else if c != 0 {
				count = append(count, c)
				c = 0
			}
		}
		if c != 0 {
			count = append(count, c)
			c = 0
		}
		sort.Slice(count, func(i, j int) bool {
			return count[i] > count[j]
		})
		ans := 0
		for j := 0; j < len(count); j += 2 {
			ans += count[j]
		}
		fmt.Println(ans)
	}
}

package main

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

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

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

var err error

func next() (n string) {
	for {
		_, err = scan(&n)
		L("next err: ", err)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

func nextInt() (n int) {
	for {
		_, err = scan(&n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextLong() (n int64) {
	for {
		_, err = scan(&n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextDouble() (n float64) {
	for {
		_, err = scan(&n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}
var (
	out *bufio.Writer
	in  *bufio.Reader
	printLog bool
)

func init() {
	out = bufio.NewWriterSize(os.Stdout, 8192)
	in = bufio.NewReaderSize(os.Stdin, 8192)

}
func L(a ...interface{}) {
	if printLog {
		log.Println(a)
	}
}
func longest1(s string) (l, r int) {
	L("s: ", s)
	l, r = -1, -1
	b, e := 0, 0
	mx := 0
	idx := 0
	for _, c := range s {
		if c == '1' {
			if s[b] != '1' {
				b = idx
			}
		}
		if c == '0' {
			e = idx
			L("b, e: ", b, e)
			if mx < e - b && s[b] == '1'{
				mx = e - b
				l, r = b, e
				L("l, r: ", l, r)
			}
			b, e = idx, idx
		}
		L("c, c == '0', c == '1': ",c, (bool)(c == '0'), (bool)(c == '1'), reflect.TypeOf(c))
		idx++
	}
	if s[b] == '1' {
		e = idx
		L("b, e: ", b, e)
		if mx < e - b && s[b] == '1'{
			mx = e - b
			l, r = b, e
			L("l, r: ", l, r)
		}
		b, e = idx, idx
	}
	return
}

func termi(s string) bool {
	// all 0
	if strings.Count(s, "0") == len(s) {
		return true
	}
	return false
}

func main() {
	printLog = false
	defer out.Flush()
	t := nextInt()
	L("t: ", t)
	for ; t > 0; t-- {
		s := next()
		L("s: ", s)
		ans := 0
		fl := 0
		for ; termi(s) == false; {
			l, r := longest1(s)
			L("l, r: ", l, r)

			s = s[:l] + s[r:]
			if fl == 0 {
				ans += r - l
			}
			L("s: ", s)
			fl = 1 - fl
		}
		fmt.Println(ans)
	}

}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var str string
		var arr []int
		fmt.Fscan(in, &str)

		count := 0
		for j := 0; j < len(str); j++ {
			if str[j] == '0' {
				if count > 0 {
					arr = append(arr, count)
				}
				count = 0
			} else {
				count++
			}
		}
		if count > 0 {
			arr = append(arr, count)
		}
		sort.Sort(sort.IntSlice(arr))

		alice := 0
		for j := len(arr) - 1; j >= 0; j -= 2 {
			alice += arr[j]
		}

		fmt.Println(alice)

	}
}

package main

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

func main(){
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	var s string
	fmt.Scan(&t)
	for ;t>0;t--{
		fmt.Scan(&s)
		fmt.Fprintln(out,solve(s))
	}
}

func solve(s string)int{
	var point int
	var m int
	for ;;m++{
		var n int
		s,n = move(s)
		if n==0{
			break
		}
		if m%2==0{
			point+=n
		}
	}
	return point
}

func move(s string)(string,int){
	var  start,end int
	var l int
	for i := range s{
		if s[i] != '1'{
			continue
		}
		j := i+1
		for ;j<len(s)&&s[j]=='1';j++{
		}
		if j-i > l{
			l = j-i
			start = i
			end = j
		}
	}
	s = s[:start]+s[end:]
	return s,l
}

package main

import (
	. "fmt"
	"sort"
	"strings"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	T, s := 0, ""
	for Scan(&T); T > 0; T-- {
		Scan(&s)
		a := strings.Split(s, "0")
		sort.Strings(a)
		c := 0
		for i := len(a) - 1; i >= 0; i -= 2 {
			c += len(a[i])
		}
		Println(c)
	}
}

package main

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

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

	var n int
	var s string

	fmt.Fscan(io, &n)
	for ; n > 0; n-- {
		fmt.Fscan(io, &s)
		streak := 0
		streaks := []int{}
		for _, c := range s {
			if c == '1' {
				streak++
			} else if streak > 0 {
				streaks = append(streaks, streak)
				streak = 0
			}
		}
		if streak > 0 {
			streaks = append(streaks, streak)
			streak = 0
		}
		sort.Sort(sort.Reverse(sort.IntSlice(streaks)))
		alicePoints := 0
		for i, streak := range streaks {
			if i%2 == 0 {
				alicePoints += streak
			}
		}
		fmt.Println(alicePoints)
	}
}


// 292A
package main

import (
	"fmt"
)

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

func main() {
	var n, t, c, l, r, i int
	fmt.Scan(&n)
	for i = 0; i < n; i++ {
		fmt.Scan(&t, &c)
		l = max(t, l) + c
		r = max(l-t, r)
	}
	fmt.Println(l, r)
}


package main

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

func main() {
	defer os.Stdout.Sync()
	buf := bufio.NewReader(os.Stdin)
	symb := make([]byte, 62)
	for i := 0; i < 10; i++ {
		symb[i] = byte('0') + byte(i)
	}
	for i := 10; i < 10+26; i++ {
		symb[i] = byte('a') + byte(i-10)
	}
	for i := 10 + 26; i < 10+26+26; i++ {
		symb[i] = byte('A') + byte(i-10-26)
	}
	//fmt.Printf("%s\n",symb)
	var r, c, k, t int
	fmt.Fscanf(buf, "%d\n", &t)
	for ; t > 0; t-- {
		fmt.Fscanf(buf, "%d %d %d\n", &r, &c, &k)
		//fmt.Printf("%d %d %d %d\n",t,r,c,k)
		mat := make([][]byte, r)
		ans := make([][]byte,r)
		rcnt := 0
		for i := 0; i < r; i++ {
			mat[i] = make([]byte, c+1)
			ans[i] = make([]byte, c)
			fmt.Fscanf(buf, "%s\n", &mat[i])
			//fmt.Printf("--->%s\n",mat[i])
			for j := 0; j < c; j++ {
				if mat[i][j] == byte('R') {
					rcnt++
				}
			}
		}
		//fmt.Printf("rcnt=%d\n",rcnt)
		div := rcnt % k
		num := 1
		numrcnt := 0
		expectcnt := rcnt / k
		if num <= div {
			expectcnt++
		}
		x := 0
		y := 0
		front := 1

		for i := 0; i < r*c; i++ {
			//fmt.Printf("%c", symb[num-1])
			ans[x][y] = symb[num-1]
			//if (i+1)%c == 0{
			//	fmt.Printf("\n")
			//}
			if mat[x][y] == 'R' {
				numrcnt++
			}
			if numrcnt == expectcnt {
				if num < k {
					num++
					numrcnt = 0
					expectcnt = rcnt / k
					if num <= div {
						expectcnt++
					}
				}
			}
			if front > 0 {
				if y+front < c {
					y += front
				} else {
					front = -1
					x++
				}
			}else{
				if y+front >= 0{
					y += front
				}else{
					x++
					front = 1
				}
			}
		}
		for i:= 0;i< r;i++{
			for j:=0;j < c;j++{
				fmt.Printf("%c",ans[i][j])
			}
			fmt.Printf("\n")
		}

	}
}

/**
4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
 */

package main

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

var scanner *bufio.Scanner

func init() {
	scanner = bufio.NewScanner(bufio.NewReader(os.Stdin))
	//scanner.Buffer(make([]byte, 311000), 311000)
	scanner.Split(bufio.ScanWords)
}

func next() string {
	if !scanner.Scan() {
		panic("scanner.Scan() returned false")
	}
	return scanner.Text()
}

func nextInt() int {
	num, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return num
}

type PosGenerator struct{
	R, C int
	r, c int
	count int
}

func (pg *PosGenerator) hasNext() bool {
	return pg.count < pg.R*pg.C;
}

func (pg *PosGenerator) next() (int, int) {
	pg.count++;
	if pg.count==1 {
		return 0,0;
	}
	if pg.r%2==0 {
		if pg.c+1<pg.C {
			pg.c++;
		} else {
			pg.r++;
		}
	} else {
		if pg.c>0 {
			pg.c--;
		} else {
			pg.r++;
		}
	}

	return pg.r, pg.c;
}

func nextColor(b byte) byte {
	if b=='Z' {
		return 'a'
	}
	if b=='z' {
		return '0'
	}
	return b+1;
}

func shouldGet(gi, G, rice int) int {
	ricePerChicken := rice/G
	if gi<rice%G {
		ricePerChicken++
	}
	return ricePerChicken;
}

func main() {
	T := nextInt()
	for t:=0; t<T; t++ {
		R, C, G := nextInt(), nextInt(), nextInt()
		pg := PosGenerator{R:R, C:C}

		grid := make([]string, R)
		for i:= range grid {
			grid[i] = next();
		}

		rice := 0;
		field := make([][]byte, R)
		for i, row := range grid {
			field[i] = make([]byte, C)
			for _, c := range row {
				if c=='R' {
					rice++;
				}
			}
		}

		gi := 0;
		gc := 0;
		color := byte('A');

		for pg.hasNext() {
			r, c := pg.next()
			if grid[r][c] == 'R' {
				if gc==shouldGet(gi, G, rice) {
					color = nextColor(color)
					gi++
					gc=0
				}
				gc++;
			}

			field[r][c] = color;

		}

		for _, row := range field {
			fmt.Println(string(row))
		}
	}
}

package main

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

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

	var t, r, c, k, lastRi, lastRj, avg, nAvg int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &r, &c, &k)
		cells := make([][]byte, r)
		cnt := 0
		for i := range cells {
			Fscan(in, &cells[i])
			cntRow := bytes.Count(cells[i], []byte{'R'})
			if cntRow > 0 {
				cnt += cntRow
				lastRi = i
				if i & 1 == 0 {
					lastRj = bytes.LastIndex(cells[i], []byte{'R'})
				} else {
					lastRj = bytes.Index(cells[i], []byte{'R'})
				}
			}
		}
		cells[lastRi][lastRj] = '.'
		if r&1 == 1 {
			cells[r-1][c-1] = 'R'
		} else {
			cells[r-1][0] = 'R'
		}

		avg = cnt / k
		if cnt%k == 0 {
			nAvg = k
		} else {
			nAvg = (avg+1)*k - cnt
		}
		nowI := 0
		nowCnt := 0
		left := nAvg

		modify := func(i, j int, isRice bool) {
			cells[i][j] = alphabet[nowI]
			if isRice {
				nowCnt++
				if nowCnt == avg {
					nowI++
					nowCnt = 0
					left--
					if left == 0 {
						avg++
						left = k - nAvg
					}
				}
			}
		}
		for i, row := range cells {
			if i&1 == 0 {
				for j, ch := range row {
					modify(i, j, ch == 'R')
				}
			} else {
				for j := c - 1; j >= 0; j-- {
					modify(i, j, cells[i][j] == 'R')
				}
			}
		}
		for _, row := range cells {
			Fprintf(out, "%s\n", row)
		}
	}
}

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

package main

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

func main() {
	defer os.Stdout.Sync()
	buf := bufio.NewReader(os.Stdin)
	symb := make([]byte, 62)
	for i := 0; i < 10; i++ {
		symb[i] = byte('0') + byte(i)
	}
	for i := 10; i < 10+26; i++ {
		symb[i] = byte('a') + byte(i-10)
	}
	for i := 10 + 26; i < 10+26+26; i++ {
		symb[i] = byte('A') + byte(i-10-26)
	}
	//fmt.Printf("%s\n",symb)
	var r, c, k, t int
	fmt.Fscanf(buf, "%d\n", &t)
	for ; t > 0; t-- {
		fmt.Fscanf(buf, "%d %d %d\n", &r, &c, &k)
		//fmt.Printf("%d %d %d %d\n",t,r,c,k)
		mat := make([][]byte, r)
		ans := make([][]byte,r)
		rcnt := 0
		for i := 0; i < r; i++ {
			mat[i] = make([]byte, c+1)
			ans[i] = make([]byte, c)
			fmt.Fscanf(buf, "%s\n", &mat[i])
			//fmt.Printf("--->%s\n",mat[i])
			for j := 0; j < c; j++ {
				if mat[i][j] == byte('R') {
					rcnt++
				}
			}
		}
		//fmt.Printf("rcnt=%d\n",rcnt)
		div := rcnt % k
		num := 1
		numrcnt := 0
		expectcnt := rcnt / k
		if num <= div {
			expectcnt++
		}
		x := 0
		y := 0
		front := 1

		for i := 0; i < r*c; i++ {
			//fmt.Printf("%c", symb[num-1])
			ans[x][y] = symb[num-1]
			//if (i+1)%c == 0{
			//	fmt.Printf("\n")
			//}
			if mat[x][y] == 'R' {
				numrcnt++
			}
			if numrcnt == expectcnt {
				num++
				numrcnt = 0
				expectcnt = rcnt / k
				if num <= div {
					expectcnt++
				}
			}
			if front > 0 {
				if y+front < c {
					y += front
				} else {
					front = -1
					x++
				}
			}else{
				if y+front >= 0{
					y += front
				}else{
					x++
					front = 1
				}
			}
		}
		for i:= 0;i< r;i++{
			for j:=0;j < c;j++{
				fmt.Printf("%c",ans[i][j])
			}
			fmt.Printf("\n")
		}

	}
}

/**
4
3 5 3
..R..
...R.
....R
6 4 6
R..R
R..R
RRRR
RRRR
R..R
R..R
2 31 62
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
 */

package main

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

var scanner *bufio.Scanner

func init() {
	scanner = bufio.NewScanner(bufio.NewReader(os.Stdin))
	//scanner.Buffer(make([]byte, 311000), 311000)
	scanner.Split(bufio.ScanWords)
}

func next() string {
	if !scanner.Scan() {
		panic("scanner.Scan() returned false")
	}
	return scanner.Text()
}

func nextInt() int {
	num, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return num
}

type PosGenerator struct{
	R, C int
	r, c int
	count int
}

func (pg *PosGenerator) hasNext() bool {
	return pg.count < pg.R*pg.C;
}

func (pg *PosGenerator) next() (int, int) {
	pg.count++;
	if pg.count==1 {
		return 0,0;
	}
	if pg.r%2==0 {
		if pg.c+1<pg.C {
			pg.c++;
		} else {
			pg.r++;
		}
	} else {
		if pg.c>0 {
			pg.c--;
		} else {
			pg.r++;
		}
	}

	return pg.r, pg.c;
}

func nextColor(b byte) byte {
	if b=='Z' {
		return 'a'
	}
	if b=='z' {
		return '0'
	}
	return b+1;
}

func shouldGet(gi, G, rice int) int {
	ricePerChicken := rice/G
	if gi<rice%G {
		ricePerChicken++
	}
	return ricePerChicken;
}

func main() {
	T := nextInt()
	for t:=0; t<T; t++ {
		R, C, G := nextInt(), nextInt(), nextInt()
		pg := PosGenerator{R:R, C:C}

		grid := make([]string, R)
		for i:= range grid {
			grid[i] = next();
		}

		rice := 0;
		field := make([][]byte, R)
		for i, row := range grid {
			field[i] = make([]byte, C)
			for _, c := range row {
				if c=='R' {
					rice++;
				}
			}
		}

		gi := 0;
		gc := 0;
		color := byte('A');

		for pg.hasNext() {
			r, c := pg.next()
			field[r][c] = color;
			if grid[r][c] == 'R' {
				gc++;
				if gc==shouldGet(gi, G, rice) {
					color = nextColor(color)
					gi++
					gc=0
				}
			}
		}

		for _, row := range field {
			fmt.Println(string(row))
		}
	}
}

package main

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

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

	var t, r, c, k, lastRi, lastRj, avg, nAvg int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &r, &c, &k)
		cells := make([][]byte, r)
		cnt := 0
		for i := range cells {
			Fscan(in, &cells[i])
			cntRow := bytes.Count(cells[i], []byte{'R'})
			if cntRow > 0 {
				cnt += cntRow
				lastRi, lastRj = i, bytes.LastIndex(cells[i], []byte{'R'})
			}
		}
		cells[lastRi][lastRj] = '.'
		if r&1 == 1 {
			cells[r-1][c-1] = 'R'
		} else {
			cells[r-1][0] = 'R'
		}

		avg = cnt / k
		if cnt%k == 0 {
			nAvg = k
		} else {
			nAvg = (avg+1)*k - cnt
		}
		nowI := 0
		nowCnt := 0
		left := nAvg

		modify := func(i, j int, isRice bool) {
			cells[i][j] = alphabet[nowI]
			if isRice {
				nowCnt++
				if nowCnt == avg {
					nowI++
					nowCnt = 0
					left--
					if left == 0 {
						avg++
						left = k - nAvg
					}
				}
			}
		}
		for i, row := range cells {
			if i&1 == 0 {
				for j, ch := range row {
					modify(i, j, ch == 'R')
				}
			} else {
				for j := c - 1; j >= 0; j-- {
					modify(i, j, cells[i][j] == 'R')
				}
			}
		}
		for _, row := range cells {
			Fprintf(out, "%s\n", row)
		}
	}
}

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

package main

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

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

	var t, r, c, k, avg, nAvg int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &r, &c, &k)
		cells := make([][]byte, r)
		cnt := 0
		for i := range cells {
			Fscan(in, &cells[i])
			cnt += bytes.Count(cells[i], []byte{'R'})
		}

		avg = cnt / k
		if cnt%k == 0 {
			nAvg = k
		} else {
			nAvg = (avg+1)*k - cnt
		}
		nowI := 0
		nowCnt := 0
		left := nAvg

		modify := func(i, j int, isRice bool) {
			cells[i][j] = alphabet[nowI]
			if isRice {
				nowCnt++
				if nowCnt == avg {
					nowI++
					nowCnt = 0
					left--
					if left == 0 {
						left = k - nAvg
					}
				}
			}
		}
		for i, row := range cells {
			if i&1 == 0 {
				for j, ch := range row {
					modify(i, j, ch == 'R')
				}
			} else {
				for j := c - 1; j >= 0; j-- {
					modify(i, j, cells[i][j] == 'R')
				}
			}
		}
		for _, row := range cells {
			Fprintf(out, "%s\n", row)
		}
	}
}

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

package main

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

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

	var t, r, c, k, lastRi, lastRj, avg, nAvg int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &r, &c, &k)
		cells := make([][]byte, r)
		cnt := 0
		for i := range cells {
			Fscan(in, &cells[i])
			cntRow := bytes.Count(cells[i], []byte{'R'})
			if cntRow > 0 {
				cnt += cntRow
				lastRi = i
				if i & 1 == 1 {
					lastRj = bytes.LastIndex(cells[i], []byte{'R'})
				} else {
					lastRj = bytes.Index(cells[i], []byte{'R'})
				}
			}
		}
		cells[lastRi][lastRj] = '.'
		if r&1 == 1 {
			cells[r-1][c-1] = 'R'
		} else {
			cells[r-1][0] = 'R'
		}

		avg = cnt / k
		if cnt%k == 0 {
			nAvg = k
		} else {
			nAvg = (avg+1)*k - cnt
		}
		nowI := 0
		nowCnt := 0
		left := nAvg

		modify := func(i, j int, isRice bool) {
			cells[i][j] = alphabet[nowI]
			if isRice {
				nowCnt++
				if nowCnt == avg {
					nowI++
					nowCnt = 0
					left--
					if left == 0 {
						avg++
						left = k - nAvg
					}
				}
			}
		}
		for i, row := range cells {
			if i&1 == 0 {
				for j, ch := range row {
					modify(i, j, ch == 'R')
				}
			} else {
				for j := c - 1; j >= 0; j-- {
					modify(i, j, cells[i][j] == 'R')
				}
			}
		}
		for _, row := range cells {
			Fprintf(out, "%s\n", row)
		}
	}
}

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

package main

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

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

	var t, r, c, k, avg, nAvg int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &r, &c, &k)
		cells := make([][]byte, r)
		cnt := 0
		for i := range cells {
			Fscan(in, &cells[i])
			cnt += bytes.Count(cells[i], []byte{'R'})
		}

		avg = cnt / k
		if cnt%k == 0 {
			nAvg = k
		} else {
			nAvg = (avg+1)*k - cnt
		}
		nowI := 0
		nowCnt := 0
		left := nAvg

		modify := func(i, j int, isRice bool) {
			cells[i][j] = alphabet[nowI]
			if isRice {
				nowCnt++
				if nowCnt == avg {
					nowI++
					nowCnt = 0
					left--
					if left == 0 {
						avg++
						left = k - nAvg
					}
				}
			}
		}
		for i, row := range cells {
			if i&1 == 0 {
				for j, ch := range row {
					modify(i, j, ch == 'R')
				}
			} else {
				for j := c - 1; j >= 0; j-- {
					modify(i, j, cells[i][j] == 'R')
				}
			}
		}
		for _, row := range cells {
			Fprintf(out, "%s\n", row)
		}
	}
}

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

package main

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

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

	var t, r, c, k, lastRi, lastRj, avg, nAvg int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &r, &c, &k)
		cells := make([][]byte, r)
		cnt := 0
		for i := range cells {
			Fscan(in, &cells[i])
			cntRow := bytes.Count(cells[i], []byte{'R'})
			if cntRow > 0 {
				cnt += cntRow
				lastRi, lastRj = i, bytes.LastIndex(cells[i], []byte{'R'})
			}
		}
		cells[lastRi][lastRj] = '.'
		cells[r-1][c-1] = 'R'

		avg = cnt / k
		if cnt%k == 0 {
			nAvg = k
		} else {
			nAvg = (avg+1)*k - cnt
		}
		nowI := 0
		nowCnt := 0
		left := nAvg

		modify := func(i, j int, isRice bool) {
			cells[i][j] = alphabet[nowI]
			if isRice {
				nowCnt++
				if nowCnt == avg {
					nowI++
					nowCnt = 0
					left--
					if left == 0 {
						avg++
						left = k - nAvg
					}
				}
			}
		}
		for i, row := range cells {
			if i&1 == 0 {
				for j, ch := range row {
					modify(i, j, ch == 'R')
				}
			} else {
				for j := c - 1; j >= 0; j-- {
					modify(i, j, cells[i][j] == 'R')
				}
			}
		}
		for _, row := range cells {
			Fprintf(out, "%s\n", row)
		}
	}
}

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


package main

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

	"container/heap"
	"strconv"
)

var n int

const MAX = 1e5 + 5

const MOD int64 = 1e9 + 7

var a map[int][]int

var m int

var banned [][]int

var garb []int

var w *bufio.Writer

const WRITE = false

type pair struct {
	v int64
	id 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] }

type Item struct {
	priority int
	index int
	idx int
	idxElem int
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority > pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

type value struct {
	value int
	indexes []int
}

func main() {
	f, _ := os.OpenFile("D:\\GO\\src\\codeforces\\edu114\\4\\in.txt", os.O_RDWR, 0)
	defer f.Close()

	if WRITE {
		f.WriteString("2\n")

		f.WriteString("114239 ")

		for i := 1; i <= 114239; i++ {
			f.WriteString(strconv.Itoa(i))
			f.WriteString(" ")
		}
		f.WriteString("\n")

		f.WriteString("85761 ")

		for i := 1; i <= 85761; i++ {
			f.WriteString(strconv.Itoa(i))
			f.WriteString(" ")
		}
		f.WriteString("\n")

		f.WriteString("0")
		return
	}

	rand.Seed(time.Now().UnixNano())

	r := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	rand.Seed(time.Now().UnixNano())

	fmt.Fscan(r, &n)

	garb = make([]int, n)

	for i := 0; i < n; i++ {
		garb[i] =  rand.Intn(1e6)
	}

	a = make(map[int][]int)

	for j := 0; j < n; j++ {
		var c int
		fmt.Fscan(r, &c)

		cc := make([]int, c)

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

	fmt.Fscan(r, &m)

	banned = make([][]int, m)

	for j := 0; j < m ; j++ {
		banned[j] = make([]int, n)

		for k := 0; k < n; k++ {
			var v int
			fmt.Fscan(r, &v)
			v--
			banned[j][k] = v
		}
	}

	solve()

}

func solve()  {
	hashes := make(map[int64][]int)

	for i := 0; i < m; i++ {
		b := banned[i]

		h := hash(b)

		hashes[h] = append(hashes[h], i)
	}

	var arr []*value

	a0 := a[0]
	for i := 0; i < len(a0); i++ {
		var indexes []int
		indexes = append(indexes, i)
		arr = append(arr, &value{a0[i], indexes})
	}

	for i := 1; i < n; i ++ {
		slot := a[i]

		biggest := arr[len(arr) - 1].value

		var pq PriorityQueue
		heap.Init(&pq)

		for j := len(slot) - 1; j >= 0; j-- {
			heap.Push(&pq, &Item{priority: slot[j] + biggest, idx: len(arr) - 1, idxElem : j})
		}

		var temp []*value

		counter := 0

		for ;pq.Len()  > 0 && counter <= int(2e5) + 100; {

			it := heap.Pop(&pq).(*Item)
			elemIndex := it.idxElem

			idx := it.idx

			indexes := arr[idx].indexes

			var elems []int

			for i := 0; i < len(indexes); i++ {
				elems = append(elems, indexes[i])
			}

			elems = append(elems, elemIndex)

			temp = append(temp, &value{value: slot[elemIndex] + arr[idx].value, indexes: elems})

			counter++

			if idx > 0 {
				t := idx - 1

				heap.Push(&pq, &Item{priority: slot[elemIndex] + arr[t].value, idx: t, idxElem : elemIndex})
			}
		}

		idx := len(temp) - 1

		for i := 0; i < len(arr); i++ {
			arr[i] = temp[idx]
			idx--
		}

		for j := idx; j >= 0; j-- {
			arr = append(arr, temp[j])
		}
	}

	for i := len(arr) - 1; i >= 0; i-- {
		e := arr[i]
		indexes := e.indexes

		h := hash(indexes)

		if _, ok := hashes[h]; !ok {

			for j := 0; j < len(indexes); j++ {
				fmt.Print(indexes[j] + 1)
				fmt.Print(" ")
			}

			fmt.Println()
			return
		}
	}
}

func hash(indexes []int) int64 {
	var ans int64

	arr := make([]int, n)

	for j := 0; j < len(indexes); j++ {
		slot := a[j]
		index := indexes[j]

		arr[j] = slot[index]
	}

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

		pos := i + 1

		var temp = int64(pos) * int64(arr[i]) * int64(garb[i])

		ans ^= temp
	}

	return ans % MOD
}

package main

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

type item struct {
	item  Item
	index int
}

type heapImpl struct {
	p               *pq
	items           map[interface{}]*item
	key             func(Item) interface{}
	DisableIndexing bool
}

func (h *heapImpl) Push(i Item) {
	it := &item{i, 0}
	h.addItem(it)
	heap.Push(h.p, it)
}

func (h *heapImpl) Pop() Item {
	res := heap.Pop(h.p).(*item)
	h.removeItem(res.item)
	return res.item
}

func (h *heapImpl) Top() Item {
	if len(h.p.items) > 0 {
		return h.p.items[0].item
	}
	return nil
}

func (h *heapImpl) Len() int {
	return len(h.p.items)
}

func (h *heapImpl) Update(o Item, n Item) {
	old := h.items[h.key(o)]
	new := &item{n, old.index}
	h.removeItem(old.item)
	h.p.items[old.index] = new
	heap.Fix(h.p, old.index)
	h.addItem(new)
}

func (h *heapImpl) Remove(i Item) {
	index := h.items[h.key(i)].index
	h.removeItem(i)
	heap.Remove(h.p, index)
}

func (h *heapImpl) Get(key interface{}) Item {
	return h.items[key].item
}

func (h *heapImpl) removeItem(item Item) {
	if !h.DisableIndexing {
		delete(h.items, h.key(item))
	}
}

func (h *heapImpl) addItem(item *item) {
	if !h.DisableIndexing {
		h.items[h.key(item.item)] = item
	}
}

// A PriorityQueue implements heap.Interface and holds Items.
type pq struct {
	items []*item
}

func (pq pq) Len() int { return len(pq.items) }

func (pq pq) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq.items[i].item.Compare(pq.items[j].item)
}

func (pq pq) Swap(i, j int) {
	pq.items[i], pq.items[j] = pq.items[j], pq.items[i]
	pq.items[i].index = i
	pq.items[j].index = j
}

func (pq *pq) Push(x interface{}) {
	n := len(pq.items)
	item := x.(*item)
	item.index = n
	pq.items = append(pq.items, item)
}

func (pq *pq) Pop() interface{} {
	old := pq.items
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	pq.items = old[0 : n-1]
	return item
}

func NewHeapWithKey(getKey func(Item) interface{}) Heap {
	return &heapImpl{
		&pq{
			[]*item{},
		},
		make(map[interface{}]*item),
		getKey,
		true,
	}
}

func NewHeap() Heap {
	return NewHeapWithKey(func(i Item) interface{} { return i })
}

type Item interface {
	Compare(left Item) bool
}

type Heap interface {
	Push(i Item)
	Pop() Item
	Top() Item
	Len() int
	Update(old Item, new Item)
	Remove(item Item)
	Get(key interface{}) Item
}

type MinInt int

func (i MinInt) Compare(j Item) bool {
	return i < j.(MinInt)
}

type MaxInt int

func (i MaxInt) Compare(j Item) bool {
	return i > j.(MaxInt)
}

type idx struct {
	value int
	arr   [10]int
}

func (left *idx) Compare(right Item) bool {
	return left.value > right.(*idx).value
}

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

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

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

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

	seen := make(map[idx]bool)
	for i := 0; i < b; i++ {
		cur := idx{}
		for j := 0; j < n; j++ {
			fmt.Fscanf(r, "%d", &cur.arr[j])
			cur.arr[j]--
			cur.value += arr[j][cur.arr[j]]
		}
		fmt.Fscanf(r, "\n")
		seen[cur] = true
	}

	cur := idx{0, [10]int{}}
	for i := 0; i < n; i++ {
		cur.arr[i] = len(arr[i]) - 1
		cur.value += arr[i][cur.arr[i]]
	}

	h := NewHeap()
	h.Push(&cur)

	alloc := [1001000]idx{}
	a := 0

	used := make(map[idx]bool)

	for {
		cur = *h.Pop().(*idx)
		if !seen[cur] {
			break
		}
		for i := 0; i < n; i++ {
			if cur.arr[i] == 0 {
				continue
			}

			next := &alloc[a]
			a++

			next.arr = cur.arr
			next.value = cur.value - arr[i][cur.arr[i]] + arr[i][cur.arr[i]-1]
			next.arr[i]--

			if !used[*next] {
				used[*next] = true
				h.Push(next)
			}
		}
	}

	for i := 0; i < n; i++ {
		fmt.Print(cur.arr[i]+1, " ")
	}
	fmt.Println()
}

package main

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

type Item interface {
	Compare(left Item) bool
}

type Heap interface {
	Push(i Item)
	Pop() Item
	Top() Item
	Len() int
	Update(old Item, new Item)
	Remove(item Item)
	Get(key interface{}) Item
}

type MinInt int

func (i MinInt) Compare(j Item) bool {
	return i < j.(MinInt)
}

type MaxInt int

func (i MaxInt) Compare(j Item) bool {
	return i > j.(MaxInt)
}

type item struct {
	item  Item
	index int
}

type heapImpl struct {
	p               *pq
	items           map[interface{}]*item
	key             func(Item) interface{}
	DisableIndexing bool
}

func (h *heapImpl) Push(i Item) {
	it := &item{i, 0}
	h.addItem(it)
	heap.Push(h.p, it)
}

func (h *heapImpl) Pop() Item {
	res := heap.Pop(h.p).(*item)
	h.removeItem(res.item)
	return res.item
}

func (h *heapImpl) Top() Item {
	if len(h.p.items) > 0 {
		return h.p.items[0].item
	}
	return nil
}

func (h *heapImpl) Len() int {
	return len(h.p.items)
}

func (h *heapImpl) Update(o Item, n Item) {
	old := h.items[h.key(o)]
	new := &item{n, old.index}
	h.removeItem(old.item)
	h.p.items[old.index] = new
	heap.Fix(h.p, old.index)
	h.addItem(new)
}

func (h *heapImpl) Remove(i Item) {
	index := h.items[h.key(i)].index
	h.removeItem(i)
	heap.Remove(h.p, index)
}

func (h *heapImpl) Get(key interface{}) Item {
	return h.items[key].item
}

func (h *heapImpl) removeItem(item Item) {
	if !h.DisableIndexing {
		delete(h.items, h.key(item))
	}
}

func (h *heapImpl) addItem(item *item) {
	if !h.DisableIndexing {
		h.items[h.key(item.item)] = item
	}
}

// A PriorityQueue implements heap.Interface and holds Items.
type pq struct {
	items []*item
}

func (pq pq) Len() int { return len(pq.items) }

func (pq pq) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq.items[i].item.Compare(pq.items[j].item)
}

func (pq pq) Swap(i, j int) {
	pq.items[i], pq.items[j] = pq.items[j], pq.items[i]
	pq.items[i].index = i
	pq.items[j].index = j
}

func (pq *pq) Push(x interface{}) {
	n := len(pq.items)
	item := x.(*item)
	item.index = n
	pq.items = append(pq.items, item)
}

func (pq *pq) Pop() interface{} {
	old := pq.items
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	pq.items = old[0 : n-1]
	return item
}

func NewHeapWithKey(getKey func(Item) interface{}) Heap {
	return &heapImpl{
		p:               &pq{
			items: []*item{},
		},
		items:           make(map[interface{}]*item),
		key:             getKey,
		DisableIndexing: true,
	}
}

func NewHeap() Heap {
	return NewHeapWithKey(func(i Item) interface{} { return i })
}

type idx struct {
	value int
	arr   [10]int
}

func (left *idx) Compare(right Item) bool {
	return left.value > right.(*idx).value
}

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

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

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

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

	seen := make(map[idx]bool)
	for i := 0; i < b; i++ {
		cur := idx{}
		for j := 0; j < n; j++ {
			fmt.Fscanf(r, "%d", &cur.arr[j])
			cur.arr[j]--
			cur.value += arr[j][cur.arr[j]]
		}
		fmt.Fscanf(r, "\n")
		seen[cur] = true
	}

	cur := idx{0, [10]int{}}
	for i := 0; i < n; i++ {
		cur.arr[i] = len(arr[i]) - 1
		cur.value += arr[i][cur.arr[i]]
	}

	h := NewHeap()
	h.Push(&cur)

	//alloc := [1001000]idx{}
	//a := 0

	used := make(map[idx]bool)

	for ; h.Len() > 0; {
		cur = *h.Pop().(*idx)
		if !seen[cur] {
			break
		}

		for i := 0; i < n; i++ {
			if cur.arr[i] == 0 {
				continue
			}
			next := cur
			next.arr = cur.arr
			next.value = cur.value - arr[i][cur.arr[i]] + arr[i][cur.arr[i]-1]
			next.arr[i]--

			if !used[next] {
				used[next] = true
				h.Push(&next)
			}
		}
	}

	for i := 0; i < n; i++ {
		fmt.Print(cur.arr[i]+1, " ")
	}
	fmt.Println()
}

package main

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

type item struct {
	item  Item
	index int
}

type heapImpl struct {
	p               *pq
	items           map[interface{}]*item
	key             func(Item) interface{}
	DisableIndexing bool
}

func (h *heapImpl) Push(i Item) {
	it := &item{i, 0}
	h.addItem(it)
	heap.Push(h.p, it)
}

func (h *heapImpl) Pop() Item {
	res := heap.Pop(h.p).(*item)
	h.removeItem(res.item)
	return res.item
}

func (h *heapImpl) Top() Item {
	if len(h.p.items) > 0 {
		return h.p.items[0].item
	}
	return nil
}

func (h *heapImpl) Len() int {
	return len(h.p.items)
}

func (h *heapImpl) Update(o Item, n Item) {
	old := h.items[h.key(o)]
	new := &item{n, old.index}
	h.removeItem(old.item)
	h.p.items[old.index] = new
	heap.Fix(h.p, old.index)
	h.addItem(new)
}

func (h *heapImpl) Remove(i Item) {
	index := h.items[h.key(i)].index
	h.removeItem(i)
	heap.Remove(h.p, index)
}

func (h *heapImpl) Get(key interface{}) Item {
	return h.items[key].item
}

func (h *heapImpl) removeItem(item Item) {
	if !h.DisableIndexing {
		delete(h.items, h.key(item))
	}
}

func (h *heapImpl) addItem(item *item) {
	if !h.DisableIndexing {
		h.items[h.key(item.item)] = item
	}
}

// A PriorityQueue implements heap.Interface and holds Items.
type pq struct {
	items []*item
}

func (pq pq) Len() int { return len(pq.items) }

func (pq pq) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq.items[i].item.Compare(pq.items[j].item)
}

func (pq pq) Swap(i, j int) {
	pq.items[i], pq.items[j] = pq.items[j], pq.items[i]
	pq.items[i].index = i
	pq.items[j].index = j
}

func (pq *pq) Push(x interface{}) {
	n := len(pq.items)
	item := x.(*item)
	item.index = n
	pq.items = append(pq.items, item)
}

func (pq *pq) Pop() interface{} {
	old := pq.items
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	pq.items = old[0 : n-1]
	return item
}

func NewHeapWithKey(getKey func(Item) interface{}) Heap {
	return &heapImpl{
		&pq{
			[]*item{},
		},
		make(map[interface{}]*item),
		getKey,
		true,
	}
}

func NewHeap() Heap {
	return NewHeapWithKey(func(i Item) interface{} { return i })
}

type Item interface {
	Compare(left Item) bool
}

type Heap interface {
	Push(i Item)
	Pop() Item
	Top() Item
	Len() int
	Update(old Item, new Item)
	Remove(item Item)
	Get(key interface{}) Item
}

type MinInt int

func (i MinInt) Compare(j Item) bool {
	return i < j.(MinInt)
}

type MaxInt int

func (i MaxInt) Compare(j Item) bool {
	return i > j.(MaxInt)
}

type idx struct {
	value int
	arr   [10]int
}

func (left *idx) Compare(right Item) bool {
	return left.value > right.(*idx).value
}

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

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

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

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

	seen := make(map[idx]bool)
	for i := 0; i < b; i++ {
		cur := idx{}
		for j := 0; j < n; j++ {
			fmt.Fscanf(r, "%d", &cur.arr[j])
			cur.arr[j]--
			cur.value += arr[j][cur.arr[j]]
		}
		fmt.Fscanf(r, "\n")
		seen[cur] = true
	}

	cur := idx{0, [10]int{}}
	for i := 0; i < n; i++ {
		cur.arr[i] = len(arr[i]) - 1
		cur.value += arr[i][cur.arr[i]]
	}

	h := NewHeap()
	h.Push(&cur)

	alloc := [2000000]idx{}
	a := 0

	for {
		cur = *h.Pop().(*idx)
		if !seen[cur] {
			break
		}
		for i := 0; i < n; i++ {
			if cur.arr[i] == 0 {
				continue
			}
			if h.Len() > 200000 || a >= len(alloc) {
				continue
			}

			next := &alloc[a]
			a++

			next.arr = cur.arr
			next.value = cur.value - arr[i][cur.arr[i]] + arr[i][cur.arr[i]-1]
			next.arr[i]--

			h.Push(next)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Print(cur.arr[i]+1, " ")
	}
	fmt.Println()
}

package main

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

type item struct {
	item  Item
	index int
}

type heapImpl struct {
	p               *pq
	items           map[interface{}]*item
	key             func(Item) interface{}
	DisableIndexing bool
}

func (h *heapImpl) Push(i Item) {
	it := &item{i, 0}
	h.addItem(it)
	heap.Push(h.p, it)
}

func (h *heapImpl) Pop() Item {
	res := heap.Pop(h.p).(*item)
	h.removeItem(res.item)
	return res.item
}

func (h *heapImpl) Top() Item {
	if len(h.p.items) > 0 {
		return h.p.items[0].item
	}
	return nil
}

func (h *heapImpl) Len() int {
	return len(h.p.items)
}

func (h *heapImpl) Update(o Item, n Item) {
	old := h.items[h.key(o)]
	new := &item{n, old.index}
	h.removeItem(old.item)
	h.p.items[old.index] = new
	heap.Fix(h.p, old.index)
	h.addItem(new)
}

func (h *heapImpl) Remove(i Item) {
	index := h.items[h.key(i)].index
	h.removeItem(i)
	heap.Remove(h.p, index)
}

func (h *heapImpl) Get(key interface{}) Item {
	return h.items[key].item
}

func (h *heapImpl) removeItem(item Item) {
	if !h.DisableIndexing {
		delete(h.items, h.key(item))
	}
}

func (h *heapImpl) addItem(item *item) {
	if !h.DisableIndexing {
		h.items[h.key(item.item)] = item
	}
}

// A PriorityQueue implements heap.Interface and holds Items.
type pq struct {
	items []*item
}

func (pq pq) Len() int { return len(pq.items) }

func (pq pq) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq.items[i].item.Compare(pq.items[j].item)
}

func (pq pq) Swap(i, j int) {
	pq.items[i], pq.items[j] = pq.items[j], pq.items[i]
	pq.items[i].index = i
	pq.items[j].index = j
}

func (pq *pq) Push(x interface{}) {
	n := len(pq.items)
	item := x.(*item)
	item.index = n
	pq.items = append(pq.items, item)
}

func (pq *pq) Pop() interface{} {
	old := pq.items
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	pq.items = old[0 : n-1]
	return item
}

func NewHeapWithKey(getKey func(Item) interface{}) Heap {
	return &heapImpl{
		&pq{
			[]*item{},
		},
		make(map[interface{}]*item),
		getKey,
		true,
	}
}

func NewHeap() Heap {
	return NewHeapWithKey(func(i Item) interface{} { return i })
}

type Item interface {
	Compare(left Item) bool
}

type Heap interface {
	Push(i Item)
	Pop() Item
	Top() Item
	Len() int
	Update(old Item, new Item)
	Remove(item Item)
	Get(key interface{}) Item
}

type MinInt int

func (i MinInt) Compare(j Item) bool {
	return i < j.(MinInt)
}

type MaxInt int

func (i MaxInt) Compare(j Item) bool {
	return i > j.(MaxInt)
}

type idx struct {
	value int
	arr   [10]int
}

func (left *idx) Compare(right Item) bool {
	return left.value > right.(*idx).value
}

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

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

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

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

	seen := make(map[idx]bool)
	for i := 0; i < b; i++ {
		cur := idx{}
		for j := 0; j < n; j++ {
			fmt.Fscanf(r, "%d", &cur.arr[j])
			cur.arr[j]--
			cur.value += arr[j][cur.arr[j]]
		}
		fmt.Fscanf(r, "\n")
		seen[cur] = true
	}

	cur := idx{0, [10]int{}}
	for i := 0; i < n; i++ {
		cur.arr[i] = len(arr[i]) - 1
		cur.value += arr[i][cur.arr[i]]
	}

	h := NewHeap()
	h.Push(&cur)

	alloc := [2000000]idx{}
	a := 0

	for {
		cur = *h.Pop().(*idx)
		if !seen[cur] {
			break
		}
		for i := 0; i < n; i++ {
			if cur.arr[i] == 0 {
				continue
			}
			if h.Len() > 100000 || a >= len(alloc) {
				continue
			}

			next := &alloc[a]
			a++

			next.arr = cur.arr
			next.value = cur.value - arr[i][cur.arr[i]] + arr[i][cur.arr[i]-1]
			next.arr[i]--

			h.Push(next)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Print(cur.arr[i]+1, " ")
	}
	fmt.Println()
}

package main

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

type idx struct {
	arr [10]int
}

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

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

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

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

	seen := make(map[idx]bool)
	for i := 0; i < b; i++ {
		cur := idx{[10]int{}}
		for j := 0; j < n; j++ {
			fmt.Fscanf(r, "%d", &cur.arr[j])
			cur.arr[j]--
		}
		fmt.Fscanf(r, "\n")
		seen[cur] = true
	}

	cur := idx{[10]int{}}
	for i := 0; i < n; i++ {
		cur.arr[i] = len(arr[i]) - 1
	}

	for seen[cur] {
		bi := -1
		for i := 0; i < n; i++ {
			if cur.arr[i] > 0 && (bi == -1 ||
				(arr[i][cur.arr[i]]-arr[i][cur.arr[i]-1]) <
					(arr[bi][cur.arr[bi]]-arr[bi][cur.arr[bi]-1])) {
				bi = i
			}
		}
		//fmt.Println(bi, arr[bi][cur.arr[bi] ])
		cur.arr[bi]--
	}

	for i := 0; i < n; i++ {
		fmt.Print(cur.arr[i]+1, " ")
	}
	fmt.Println()
}

package main

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

	"container/heap"
	"strconv"
)

var n int

const MAX = 1e5 + 5

const MOD int64 = 1e9 + 7

var a map[int][]int

var m int

var banned [][]int

var garb []int

var w *bufio.Writer

const WRITE = false

type pair struct {
	v int64
	id 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] }

type Item struct {
	priority int
	index int
	idx int
	idxElem int
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority > pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

type value struct {
	value int
	indexes []int
}

func main() {
	f, _ := os.OpenFile("D:\\GO\\src\\codeforces\\edu114\\4\\in.txt", os.O_RDWR, 0)
	defer f.Close()

	if WRITE {
		f.WriteString("2\n")

		f.WriteString("114239 ")

		for i := 1; i <= 114239; i++ {
			f.WriteString(strconv.Itoa(i))
			f.WriteString(" ")
		}
		f.WriteString("\n")

		f.WriteString("85761 ")

		for i := 1; i <= 85761; i++ {
			f.WriteString(strconv.Itoa(i))
			f.WriteString(" ")
		}
		f.WriteString("\n")

		f.WriteString("0")
		return
	}

	rand.Seed(time.Now().UnixNano())

	r := bufio.NewReader(f)
	w = bufio.NewWriter(os.Stdout)

	rand.Seed(time.Now().UnixNano())

	fmt.Fscan(r, &n)

	garb = make([]int, n)

	for i := 0; i < n; i++ {
		garb[i] =  rand.Intn(1e6)
	}

	a = make(map[int][]int)

	for j := 0; j < n; j++ {
		var c int
		fmt.Fscan(r, &c)

		cc := make([]int, c)

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

	fmt.Fscan(r, &m)

	banned = make([][]int, m)

	for j := 0; j < m ; j++ {
		banned[j] = make([]int, n)

		for k := 0; k < n; k++ {
			var v int
			fmt.Fscan(r, &v)
			v--
			banned[j][k] = v
		}
	}

	solve()

}

func solve()  {
	hashes := make(map[int64][]int)

	for i := 0; i < m; i++ {
		b := banned[i]

		h := hash(b)

		hashes[h] = append(hashes[h], i)
	}

	var arr []*value

	a0 := a[0]
	for i := 0; i < len(a0); i++ {
		var indexes []int
		indexes = append(indexes, i)
		arr = append(arr, &value{a0[i], indexes})
	}

	for i := 1; i < n; i ++ {
		slot := a[i]

		biggest := arr[len(arr) - 1].value

		var pq PriorityQueue
		heap.Init(&pq)

		for j := len(slot) - 1; j >= 0; j-- {
			heap.Push(&pq, &Item{priority: slot[j] + biggest, idx: len(arr) - 1, idxElem : j})
		}

		var temp []*value

		counter := 0

		for ;pq.Len()  > 0 && counter <= int(2e5) + 100; {

			it := heap.Pop(&pq).(*Item)
			elemIndex := it.idxElem

			idx := it.idx

			indexes := arr[idx].indexes

			var elems []int

			for i := 0; i < len(indexes); i++ {
				elems = append(elems, indexes[i])
			}

			elems = append(elems, elemIndex)

			temp = append(temp, &value{value: slot[elemIndex] + arr[idx].value, indexes: elems})

			counter++

			if idx > 0 {
				t := idx - 1

				heap.Push(&pq, &Item{priority: slot[elemIndex] + arr[t].value, idx: t, idxElem : elemIndex})
			}
		}

		idx := len(temp) - 1

		for i := 0; i < len(arr); i++ {
			arr[i] = temp[idx]
			idx--
		}

		for j := idx; j >= 0; j-- {
			arr = append(arr, temp[j])
		}
	}

	for i := len(arr) - 1; i >= 0; i-- {
		e := arr[i]
		indexes := e.indexes

		h := hash(indexes)

		if _, ok := hashes[h]; !ok {

			for j := 0; j < len(indexes); j++ {
				fmt.Print(indexes[j] + 1)
				fmt.Print(" ")
			}

			fmt.Println()
			return
		}
	}
}

func hash(indexes []int) int64 {
	var ans int64

	arr := make([]int, n)

	for j := 0; j < len(indexes); j++ {
		slot := a[j]
		index := indexes[j]

		arr[j] = slot[index]
	}

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

		pos := i + 1

		var temp = int64(pos) * int64(arr[i]) * int64(garb[i])

		ans ^= temp
	}

	return ans % MOD
}

package main

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

	"container/heap"
)

var n int

const MAX = 1e5 + 5

const MOD int64 = 1e9 + 7

var a map[int][]int

var m int

var banned [][]int

var garb []int

type pair struct {
	v int64
	id 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] }

type Item struct {
	priority int
	index int
	idx int
	idxElem int
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].priority > pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

type value struct {
	value int
	indexes []int
}

func main() {

	r := bufio.NewReader(os.Stdin)

	rand.Seed(time.Now().UnixNano())

	fmt.Fscan(r, &n)

	garb = make([]int, n)

	for i := 0; i < n; i++ {
		garb[i] =  rand.Intn(1e6)
	}

	a = make(map[int][]int)

	for j := 0; j < n; j++ {
		var c int
		fmt.Fscan(r, &c)

		cc := make([]int, c)

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

	fmt.Fscan(r, &m)

	banned = make([][]int, m)

	for j := 0; j < m ; j++ {
		banned[j] = make([]int, n)

		for k := 0; k < n; k++ {
			var v int
			fmt.Fscan(r, &v)
			v--
			banned[j][k] = v
		}
	}

	solve()

}

func solve()  {
	hashes := make(map[int64][]int)

	for i := 0; i < m; i++ {
		b := banned[i]

		h := hash(b)

		hashes[h] = append(hashes[h], i)
	}

	var arr []*value

	a0 := a[0]
	for i := 0; i < len(a0); i++ {
		var indexes []int
		indexes = append(indexes, i)
		arr = append(arr, &value{a0[i], indexes})
	}

	for i := 1; i < n; i ++ {
		slot := a[i]

		biggest := arr[len(arr) - 1].value

		var pq PriorityQueue
		heap.Init(&pq)

		for j := len(slot) - 1; j >= 0; j-- {
			heap.Push(&pq, &Item{priority: slot[j] + biggest, idx: len(arr) - 1, idxElem : j})
		}

		var temp []*value

		counter := 0

		for ;pq.Len()  > 0 && counter <= int(1e5) + 100; {

			it := heap.Pop(&pq).(*Item)
			elemIndex := it.idxElem

			idx := it.idx

			indexes := arr[idx].indexes
			indexes = append(indexes, elemIndex)

			temp = append(temp, &value{value: slot[elemIndex] + arr[idx].value, indexes: indexes})

			counter++

			if idx > 0 {
				t := idx - 1

				heap.Push(&pq, &Item{priority: slot[elemIndex] + arr[t].value, idx: t, idxElem : elemIndex})
			}
		}

		idx := len(temp) - 1

		for i := 0; i < len(arr); i++ {
			arr[i] = temp[idx]
			idx--
		}

		for j := idx; j >= 0; j-- {
			arr = append(arr, temp[j])
		}
	}

	for i := len(arr) - 1; i >= 0; i-- {
		e := arr[i]
		indexes := e.indexes

		h := hash(indexes)

		if _, ok := hashes[h]; !ok {

			for j := 0; j < len(indexes); j++ {
				fmt.Print(indexes[j] + 1)
				fmt.Print(" ")
			}

			fmt.Println()
			return
		}
	}
}

func hash(indexes []int) int64 {
	var ans int64

	arr := make([]int, n)

	for j := 0; j < len(indexes); j++ {
		slot := a[j]
		index := indexes[j]

		arr[j] = slot[index]
	}

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

		pos := i + 1

		var temp = int64(pos) * int64(arr[i]) * int64(garb[i])

		ans ^= temp
	}

	return ans % MOD
}

package main

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

type Item interface {
	Compare(left Item) bool
}

type Heap interface {
	Push(i Item)
	Pop() Item
	Top() Item
	Len() int
	Update(old Item, new Item)
	Remove(item Item)
	Get(key interface{}) Item
}

type MinInt int

func (i MinInt) Compare(j Item) bool {
	return i < j.(MinInt)
}

type MaxInt int

func (i MaxInt) Compare(j Item) bool {
	return i > j.(MaxInt)
}

type item struct {
	item  Item
	index int
}

type heapImpl struct {
	p               *pq
	items           map[interface{}]*item
	key             func(Item) interface{}
	DisableIndexing bool
}

func (h *heapImpl) Push(i Item) {
	it := &item{i, 0}
	h.addItem(it)
	heap.Push(h.p, it)
}

func (h *heapImpl) Pop() Item {
	res := heap.Pop(h.p).(*item)
	h.removeItem(res.item)
	return res.item
}

func (h *heapImpl) Top() Item {
	if len(h.p.items) > 0 {
		return h.p.items[0].item
	}
	return nil
}

func (h *heapImpl) Len() int {
	return len(h.p.items)
}

func (h *heapImpl) Update(o Item, n Item) {
	old := h.items[h.key(o)]
	new := &item{n, old.index}
	h.removeItem(old.item)
	h.p.items[old.index] = new
	heap.Fix(h.p, old.index)
	h.addItem(new)
}

func (h *heapImpl) Remove(i Item) {
	index := h.items[h.key(i)].index
	h.removeItem(i)
	heap.Remove(h.p, index)
}

func (h *heapImpl) Get(key interface{}) Item {
	return h.items[key].item
}

func (h *heapImpl) removeItem(item Item) {
	if !h.DisableIndexing {
		delete(h.items, h.key(item))
	}
}

func (h *heapImpl) addItem(item *item) {
	if !h.DisableIndexing {
		h.items[h.key(item.item)] = item
	}
}

// A PriorityQueue implements heap.Interface and holds Items.
type pq struct {
	items []*item
}

func (pq pq) Len() int { return len(pq.items) }

func (pq pq) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq.items[i].item.Compare(pq.items[j].item)
}

func (pq pq) Swap(i, j int) {
	pq.items[i], pq.items[j] = pq.items[j], pq.items[i]
	pq.items[i].index = i
	pq.items[j].index = j
}

func (pq *pq) Push(x interface{}) {
	n := len(pq.items)
	item := x.(*item)
	item.index = n
	pq.items = append(pq.items, item)
}

func (pq *pq) Pop() interface{} {
	old := pq.items
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	pq.items = old[0 : n-1]
	return item
}

func NewHeapWithKey(getKey func(Item) interface{}) Heap {
	return &heapImpl{
		p:               &pq{
			items: []*item{},
		},
		items:           make(map[interface{}]*item),
		key:             getKey,
		DisableIndexing: true,
	}
}

func NewHeap() Heap {
	return NewHeapWithKey(func(i Item) interface{} { return i })
}

type idx struct {
	value int
	arr   [10]int
}

func (left *idx) Compare(right Item) bool {
	return left.value > right.(*idx).value
}

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

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

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

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

	seen := make(map[idx]bool)
	for i := 0; i < b; i++ {
		cur := idx{}
		for j := 0; j < n; j++ {
			fmt.Fscanf(r, "%d", &cur.arr[j])
			cur.arr[j]--
			cur.value += arr[j][cur.arr[j]]
		}
		fmt.Fscanf(r, "\n")
		seen[cur] = true
	}

	cur := idx{0, [10]int{}}
	for i := 0; i < n; i++ {
		cur.arr[i] = len(arr[i]) - 1
		cur.value += arr[i][cur.arr[i]]
	}

	h := NewHeap()
	h.Push(&cur)

	//alloc := [1001000]idx{}
	//a := 0

	used := make(map[idx]bool)

	for ; h.Len() > 0; {
		cur = *h.Pop().(*idx)
		if !seen[cur] {
			break
		}

		next := cur
		for i := 0; i < n; i++ {
			if cur.arr[i] == 0 {
				continue
			}

			next.arr = cur.arr
			next.value = cur.value - arr[i][cur.arr[i]] + arr[i][cur.arr[i]-1]
			next.arr[i]--

			if !used[next] {
				used[next] = true
				h.Push(&next)
			}
		}
	}

	for i := 0; i < n; i++ {
		fmt.Print(cur.arr[i]+1, " ")
	}
	fmt.Println()
}


package main

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

func dc(l, r float64, X []float64, T []float64, x0 float64) float64 {
	if r - l < 1e-6 { return x0 }

	var m, maxl, minr float64
	m = (l + r) / 2
	maxl = 0
	minr = 1e13

	for i := range X {
		cl := X[i]
		if T[i] < m { cl = X[i] + T[i] - m }
		if cl > maxl { maxl = cl }

		cr := X[i]
		if T[i] < m { cr = X[i] - T[i] + m }
		if cr < minr { minr = cr }
	}

	if maxl < minr {
		return dc(l, m, X, T, maxl)
	} else {
		return dc(m, r, X, T, maxl)
	}
}

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 int
		fmt.Fscan(r, &n)

		X := make([]float64, n)
		for i := range X { fmt.Fscan(r, &X[i]) }

		T := make([]float64, n)
		for i := range T { fmt.Fscan(r, &T[i]) }

		fmt.Fprintf(w, "%.7f\n", dc(1e-7, 1e12, X, T, 0))
	}

}

// https://codeforces.com/problemset/problem/1730/B

// 1730B - Meeting on the Line

// n  people live on the coordinate line, the i-th one lives at the point xi (1≤i≤n). They want to choose a position x0 to meet. The i-th person will spend |xi−x0| minutes to get to the meeting place. Also, the i-th person needs ti minutes to get dressed, so in total he or she needs ti+|xi−x0| minutes.
// Here |y| denotes the absolute value of y.
// These people ask you to find a position x0 that minimizes the time in which all n people can gather at the meeting place.

// Input
// The first line contains a single integer t (1≤t≤10^3) — the number of test cases. Then the test cases follow.
// Each test case consists of three lines.
// The first line contains a single integer n (1≤n≤10^5) — the number of people.
// The second line contains n integers x1,x2,…,xn (0≤xi≤10^8) — the positions of the people.
// The third line contains n integers t1,t2,…,tn (0≤ti≤10^8), where ti is the time i-th person needs to get dressed.
// It is guaranteed that the sum of n over all test cases does not exceed 2⋅10^5.

// Output
// For each test case, print a single real number — the optimum position x0. It can be shown that the optimal position x0 is unique.
// Your answer will be considered correct if its absolute or relative error does not exceed 10^−6. Formally, let your answer be a, the jury's answer be b. Your answer will be considered correct if |a−b|/max(1,|b|)≤10^−6.

// Example
// 7
// 1
// 0
// 3
// 2
// 3 1
// 0 0
// 2
// 1 4
// 0 0
// 3
// 1 2 3
// 0 0 0
// 3
// 1 2 3
// 4 1 2
// 3
// 3 3 3
// 5 3 3
// 6
// 5 4 7 2 10 4
// 3 2 5 1 4 6
// output
// 0
// 2
// 2.5
// 2
// 1
// 3
// 6

// Note

//  • In the 1-st test case there is one person, so it is efficient to choose his or her position for the meeting place. Then he or she will get to it in 3 minutes, that he or she need to get dressed.
//  • In the 2-nd test case there are 2 people who don't need time to get dressed. Each of them needs one minute to get to position 2.
//  • In the 5-th test case the 1-st person needs 4 minutes to get to position 1 (4 minutes to get dressed and 0 minutes on the way); the 2-nd person needs 2 minutes to get to position 1 (1 minute to get dressed and 1 minute on the way); the 3-rd person needs 4 minutes to get to position 1 (2 minutes to get dressed and 2 minutes on the way).

// Tutorial

// There are many solutions to this problem, here are 2 of them.
// 1) Let people be able to meet in time T, then they could meet in time T+ε, where ε>0. So we can find T by binary search. It remains to learn how to check whether people can meet for a specific time T. To do this, for the i-th person, find a segment of positions that he can get to in time T: if T<ti then this segment is empty, otherwise it is [xi−(T−ti),xi+(T−ti)]. Then people will be able to meet only if these segments intersect, that is, the minimum of the right borders is greater than or equal to the maximum of the left borders. In order to find the answer by the minimum T, you need to intersect these segments in the same way (should get a point, but due to accuracy, most likely, a segment of a very small length will turn out) and take any point of this intersection. Asymptotics is O(nlogn).
// 2) If all ti were equal to 0, then this would be a classical problem, the solution of which would be to find the average of the minimum and maximum coordinates of people. We can reduce our problem to this one if we replace the person (xi, ti) with two people: (xi−ti, 0) and (xi+ti, 0). Proof. Let the meeting be at the point y. Let xi≤y. Then this person will need ti+y−xi of time to get to her, and the two we want to replace him with — y−xi+ti and y−xi−ti. it is easy to see that the first value is equal to the initial value, and the second is not greater than the initial value, then the maximum of these two values is equal to the initial value. The proof is similar for the case y≤xi. Then for any meeting point, the time in the original problem and after the replacement does not differ, which means that such a replacement will not change the answer and it can be done. Asymptotics is O(n).

package main

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

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

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

func main() {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	var s, c, n, i, mn, mx, sum int
	var x, t []int

	for s, _ = fmt.Fscan(reader, &c); s == 1; s, _ = fmt.Fscan(reader, &c) {
		for ; c > 0; c-- {
			fmt.Fscan(reader, &n)
			x = make([]int, n)
			t = make([]int, n)

			for i = 0; i < n; i++ {
				fmt.Fscan(reader, &x[i])
			}

			for i = 0; i < n; i++ {
				fmt.Fscan(reader, &t[i])
			}

			mn, mx = x[0]+t[0], x[0]-t[0]

			for i = 0; i < n; i++ {
				mn = min(mn, x[i]-t[i])
				mx = max(mx, x[i]+t[i])
			}

			sum = mn + mx
			fmt.Fprint(writer, sum>>1)

			if sum&1 == 1 {
				fmt.Fprint(writer, ".5")
			}

			fmt.Fprintln(writer)
		}
	}

	writer.Flush()
}

// https://codeforces.com/problemset/problem/1730/B

// 1730B - Meeting on the Line

// n  people live on the coordinate line, the i-th one lives at the point xi (1≤i≤n). They want to choose a position x0 to meet. The i-th person will spend |xi−x0| minutes to get to the meeting place. Also, the i-th person needs ti minutes to get dressed, so in total he or she needs ti+|xi−x0| minutes.
// Here |y| denotes the absolute value of y.
// These people ask you to find a position x0 that minimizes the time in which all n people can gather at the meeting place.

// Input
// The first line contains a single integer t (1≤t≤10^3) — the number of test cases. Then the test cases follow.
// Each test case consists of three lines.
// The first line contains a single integer n (1≤n≤10^5) — the number of people.
// The second line contains n integers x1,x2,…,xn (0≤xi≤10^8) — the positions of the people.
// The third line contains n integers t1,t2,…,tn (0≤ti≤10^8), where ti is the time i-th person needs to get dressed.
// It is guaranteed that the sum of n over all test cases does not exceed 2⋅10^5.

// Output
// For each test case, print a single real number — the optimum position x0. It can be shown that the optimal position x0 is unique.
// Your answer will be considered correct if its absolute or relative error does not exceed 10^−6. Formally, let your answer be a, the jury's answer be b. Your answer will be considered correct if |a−b|/max(1,|b|)≤10^−6.

// Example
// 7
// 1
// 0
// 3
// 2
// 3 1
// 0 0
// 2
// 1 4
// 0 0
// 3
// 1 2 3
// 0 0 0
// 3
// 1 2 3
// 4 1 2
// 3
// 3 3 3
// 5 3 3
// 6
// 5 4 7 2 10 4
// 3 2 5 1 4 6
// output
// 0
// 2
// 2.5
// 2
// 1
// 3
// 6

// Note

//  • In the 1-st test case there is one person, so it is efficient to choose his or her position for the meeting place. Then he or she will get to it in 3 minutes, that he or she need to get dressed.
//  • In the 2-nd test case there are 2 people who don't need time to get dressed. Each of them needs one minute to get to position 2.
//  • In the 5-th test case the 1-st person needs 4 minutes to get to position 1 (4 minutes to get dressed and 0 minutes on the way); the 2-nd person needs 2 minutes to get to position 1 (1 minute to get dressed and 1 minute on the way); the 3-rd person needs 4 minutes to get to position 1 (2 minutes to get dressed and 2 minutes on the way).

// Tutorial

// There are many solutions to this problem, here are 2 of them.
// 1) Let people be able to meet in time T, then they could meet in time T+ε, where ε>0. So we can find T by binary search. It remains to learn how to check whether people can meet for a specific time T. To do this, for the i-th person, find a segment of positions that he can get to in time T: if T<ti then this segment is empty, otherwise it is [xi−(T−ti),xi+(T−ti)]. Then people will be able to meet only if these segments intersect, that is, the minimum of the right borders is greater than or equal to the maximum of the left borders. In order to find the answer by the minimum T, you need to intersect these segments in the same way (should get a point, but due to accuracy, most likely, a segment of a very small length will turn out) and take any point of this intersection. Asymptotics is O(nlogn).
// 2) If all ti were equal to 0, then this would be a classical problem, the solution of which would be to find the average of the minimum and maximum coordinates of people. We can reduce our problem to this one if we replace the person (xi, ti) with two people: (xi−ti, 0) and (xi+ti, 0). Proof. Let the meeting be at the point y. Let xi≤y. Then this person will need ti+y−xi of time to get to her, and the two we want to replace him with — y−xi+ti and y−xi−ti. it is easy to see that the first value is equal to the initial value, and the second is not greater than the initial value, then the maximum of these two values is equal to the initial value. The proof is similar for the case y≤xi. Then for any meeting point, the time in the original problem and after the replacement does not differ, which means that such a replacement will not change the answer and it can be done. Asymptotics is O(n).

package main

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

func main() {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	var s, c, n, i, min, max, sum int
	var x, t []int

	for s, _ = fmt.Fscan(reader, &c); s == 1; s, _ = fmt.Fscan(reader, &c) {
		for ; c > 0; c-- {
			fmt.Fscan(reader, &n)
			x = make([]int, n)
			t = make([]int, n)

			for i = 0; i < n; i++ {
				fmt.Fscan(reader, &x[i])
			}

			for i = 0; i < n; i++ {
				fmt.Fscan(reader, &t[i])
			}

			min, max = x[0]+t[0], x[0]-t[0]

			for i = 0; i < n; i++ {
				if min > x[i]-t[i] {
					min = x[i] - t[i]
				}

				if max < x[i]+t[i] {
					max = x[i] + t[i]
				}
			}

			sum = min + max
			fmt.Fprint(writer, sum>>1)

			if sum&1 == 1 {
				fmt.Fprint(writer, ".5")
			}

			fmt.Fprintln(writer)
		}
	}

	writer.Flush()
}

// https://codeforces.com/problemset/problem/1730/B

// 1730B - Meeting on the Line

// n  people live on the coordinate line, the i-th one lives at the point xi (1≤i≤n). They want to choose a position x0 to meet. The i-th person will spend |xi−x0| minutes to get to the meeting place. Also, the i-th person needs ti minutes to get dressed, so in total he or she needs ti+|xi−x0| minutes.
// Here |y| denotes the absolute value of y.
// These people ask you to find a position x0 that minimizes the time in which all n people can gather at the meeting place.

// Input
// The first line contains a single integer t (1≤t≤10^3) — the number of test cases. Then the test cases follow.
// Each test case consists of three lines.
// The first line contains a single integer n (1≤n≤10^5) — the number of people.
// The second line contains n integers x1,x2,…,xn (0≤xi≤10^8) — the positions of the people.
// The third line contains n integers t1,t2,…,tn (0≤ti≤10^8), where ti is the time i-th person needs to get dressed.
// It is guaranteed that the sum of n over all test cases does not exceed 2⋅10^5.

// Output
// For each test case, print a single real number — the optimum position x0. It can be shown that the optimal position x0 is unique.
// Your answer will be considered correct if its absolute or relative error does not exceed 10^−6. Formally, let your answer be a, the jury's answer be b. Your answer will be considered correct if |a−b|/max(1,|b|)≤10^−6.

// Example
// 7
// 1
// 0
// 3
// 2
// 3 1
// 0 0
// 2
// 1 4
// 0 0
// 3
// 1 2 3
// 0 0 0
// 3
// 1 2 3
// 4 1 2
// 3
// 3 3 3
// 5 3 3
// 6
// 5 4 7 2 10 4
// 3 2 5 1 4 6
// output
// 0
// 2
// 2.5
// 2
// 1
// 3
// 6

// Note

//  • In the 1-st test case there is one person, so it is efficient to choose his or her position for the meeting place. Then he or she will get to it in 3 minutes, that he or she need to get dressed.
//  • In the 2-nd test case there are 2 people who don't need time to get dressed. Each of them needs one minute to get to position 2.
//  • In the 5-th test case the 1-st person needs 4 minutes to get to position 1 (4 minutes to get dressed and 0 minutes on the way); the 2-nd person needs 2 minutes to get to position 1 (1 minute to get dressed and 1 minute on the way); the 3-rd person needs 4 minutes to get to position 1 (2 minutes to get dressed and 2 minutes on the way).

// Tutorial

// There are many solutions to this problem, here are 2 of them.
// 1) Let people be able to meet in time T, then they could meet in time T+ε, where ε>0. So we can find T by binary search. It remains to learn how to check whether people can meet for a specific time T. To do this, for the i-th person, find a segment of positions that he can get to in time T: if T<ti then this segment is empty, otherwise it is [xi−(T−ti),xi+(T−ti)]. Then people will be able to meet only if these segments intersect, that is, the minimum of the right borders is greater than or equal to the maximum of the left borders. In order to find the answer by the minimum T, you need to intersect these segments in the same way (should get a point, but due to accuracy, most likely, a segment of a very small length will turn out) and take any point of this intersection. Asymptotics is O(nlogn).
// 2) If all ti were equal to 0, then this would be a classical problem, the solution of which would be to find the average of the minimum and maximum coordinates of people. We can reduce our problem to this one if we replace the person (xi, ti) with two people: (xi−ti, 0) and (xi+ti, 0). Proof. Let the meeting be at the point y. Let xi≤y. Then this person will need ti+y−xi of time to get to her, and the two we want to replace him with — y−xi+ti and y−xi−ti. it is easy to see that the first value is equal to the initial value, and the second is not greater than the initial value, then the maximum of these two values is equal to the initial value. The proof is similar for the case y≤xi. Then for any meeting point, the time in the original problem and after the replacement does not differ, which means that such a replacement will not change the answer and it can be done. Asymptotics is O(n).

package main

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

func main() {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	var s, c, n, i, min, max, sum int
	var x, t, v []int

	for s, _ = fmt.Fscan(reader, &c); s == 1; s, _ = fmt.Fscan(reader, &c) {
		for ; c > 0; c-- {
			fmt.Fscan(reader, &n)
			x = make([]int, n)
			t = make([]int, n)
			v = make([]int, 2*n)

			for i = 0; i < n; i++ {
				fmt.Fscan(reader, &x[i])
			}

			for i = 0; i < n; i++ {
				fmt.Fscan(reader, &t[i])
			}

			for i = 0; i < n; i++ {
				v[2*i] = x[i] - t[i]
				v[2*i+1] = x[i] + t[i]
			}

			min, max = v[0], v[0]

			for i = 1; i < 2*n; i++ {
				if min > v[i] {
					min = v[i]
				}

				if max < v[i] {
					max = v[i]
				}
			}

			sum = min + max
			fmt.Fprint(writer, sum>>1)

			if sum&1 == 1 {
				fmt.Fprint(writer, ".5")
			}

			fmt.Fprintln(writer)
		}
	}

	writer.Flush()
}

package main

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

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

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

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)

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

		a, z := math.MaxInt32, 0
		for i := range aa {
			a = min(a, aa[i]-bb[i])
			z = max(z, aa[i]+bb[i])
		}

		fmt.Fprintln(out, (float64(a)+float64(z))/2)
	}
}

package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	x := make([]int, n)
	var mn, mx int = 1e10, -1e10
	for i := range x {
		fmt.Fscan(in, &x[i])
	}
	var num int
	for i := range x {
		fmt.Fscan(in, &num)
		mn = min(mn, x[i]-num)
		mx = max(mx, x[i]+num)
	}
	fmt.Fprintln(out, float64(mx+mn)/2.)
}

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"
)

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

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

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)

		type p struct {
			x, z int
		}
		aa := make([]p, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i].x)
		}

		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i].z)
		}
		sort.Slice(aa, func(i, j int) bool {
			if aa[i].x == aa[j].x {
				return aa[i].z < aa[j].z
			}
			return aa[i].x < aa[j].x
		})
		var lm, rm int
		d := (aa[0].x + aa[n-1].x) / 2
		for _, p2 := range aa {
			if p2.x <= d && abs(p2.x-d)+p2.z > lm {
				lm = abs(p2.x-d) + p2.z
			} else if p2.x > d && abs(p2.x-d)+p2.z > rm {
				rm = abs(p2.x-d) + p2.z
			}
		}

		if lm == rm || lm == 0 || rm == 0 {
			fmt.Fprintln(out, d)
		} else if rm > lm {
			x := (float64(rm) - float64(lm)) / 2
			g := float64(d) + x
			if int(g) > aa[n-1].x {
				g = float64(aa[n-1].x)
			}
			fmt.Fprintln(out, g)
		} else {
			x := (float64(lm) - float64(rm)) / 2
			g := float64(d) - x
			if int(g) < aa[0].x {
				g = float64(aa[0].x)
			}
			fmt.Fprintln(out, g)
		}
	}
}


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)
	a := make([][]int, n)
	diffRow := -1
	col := 0
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		for j := 0; j < m; j++ {
			fmt.Fscanf(in, "%d", &a[i][j])
		}

		for j := 1; j < m; j++ {
			if a[i][j] != a[i][j-1] {
				diffRow = i
				col = j
				break
			}
		}
		fmt.Fscanf(in, "\n")
	}
	tmp := 0
	for i := 0; i < n; i++ {
		tmp ^= a[i][0]
	}
	if tmp > 0 {
		fmt.Fprintf(out, "TAK\n")
		for i := 0; i < n-1; i++ {
			fmt.Fprintf(out, "%d ", 1)
		}
		fmt.Fprintf(out, "%d\n", 1)
		return
	}
	if diffRow == -1 {
		fmt.Fprintf(out, "NIE\n")
		return
	}
	ans := make([]int, n)
	for i := 0; i < n; i++ {
		if i != diffRow {
			ans[i] = 1
		} else {
			ans[i] = col + 1
		}
	}
	fmt.Fprintf(out, "TAK\n")
	for i := 0; i < n-1; i++ {
		fmt.Fprintf(out, "%d ", ans[i])
	}
	fmt.Fprintf(out, "%d\n", ans[n-1])
	return
}

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

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

package main

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

/*

The first line contains two integers 𝑛 and 𝑚 (1≤𝑛,𝑚≤500) — the number of rows and the number of columns in the matrix 𝑎.

inputCopy
3 2
0 0
0 0
0 0
outputCopy
NIE

inputCopy
2 3
7 7 7
7 7 10
outputCopy
TAK
1 3

1 2 3
4 5 6
7 8 9

*/

type Solver struct {
	a     [][]int
	n     int
	m     int
	Path  []int
	Found bool
}

func (s *Solver) pushPath(i int) {
	if s.Found == true {
		return
	}

	s.Path = append(s.Path, i)
}

func (s *Solver) popPath() (p int) {
	if s.Found == true {
		return
	}

	p, s.Path = s.Path[len(s.Path)-1], s.Path[:len(s.Path)-1]
	return
}

func (s *Solver) Result() {
	if !s.Found {
		fmt.Println("NIE")
		return
	}
	fmt.Println("TAK")
	fmt.Println(strings.Trim(fmt.Sprint(s.Path), "[]"))
	return
}

func (s *Solver) Search(res, depth int) {
	searched := map[int]bool{}

	for i := 0; i < s.m; i++ {
		if s.Found == true {
			break
		}

		cur_var := s.a[depth][i]

		// check if searched
		if searched[cur_var] {
			continue
		}
		searched[cur_var] = true

		s.pushPath(i + 1)
		if depth == s.n-1 {
			s.Found = (cur_var^res > 0)
		} else {
			s.Search(cur_var^res, depth+1)
		}
		s.popPath()
	}
}

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

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

	s.Split(bufio.ScanWords)
	n, _ := ScanNum(s)
	m, _ := ScanNum(s)
	a := [][]int{}

	for i := 0; i < n; i++ {
		a = append(a, make([]int, m))
		for j := 0; j < m; j++ {
			a[i][j], _ = ScanNum(s)
		}
	}

	ss := Solver{
		a: a,
		n: n,
		m: m,
	}
	ss.Search(0, 0)
	ss.Result()
}

package main

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

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

	var n, m, di, dj int
	Fscan(in, &n, &m)
	mat := make([][]uint16, n)
	sum := uint16(0)
	for i := range mat {
		mat[i] = make([]uint16, m)
		for j := range mat[i] {
			Fscan(in, &mat[i][j])
			if mat[i][j] != mat[i][0] {
				di, dj = i, j
			}
		}
		sum ^= mat[i][0]
	}
	ans := make([]interface{}, n)
	for i := range ans {
		ans[i] = 1
	}
	if sum == 0 {
		if dj == 0 {
			Fprint(out, "NIE")
			return
		}
		ans[di] = dj + 1
	}
	Fprintln(out, "TAK")
	Fprint(out, ans...)
}

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

// Author: sighduck
// URL: https://codeforces.com/contest/1151/problem/B

package main

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

func Solve(n, m int, a [][]int) []int {
	x := 0
	for i := 0; i < n; i++ {
		x ^= a[i][0]
	}
	if x > 0 {
		ans := make([]int, n)
		for i := 0; i < n; i++ {
			ans[i] = 1
		}
		return ans
	}
	if x == 0 {
		row := -1
		col := -1
		for i := 0; i < n; i++ {
			for j := 1; j < m; j++ {
				if a[i][j] != a[i][0] {
					row = i
					col = j + 1
				}
			}
		}
		if row != -1 && col != -1 {
			ans := make([]int, n)
			for i := 0; i < n; i++ {
				ans[i] = 1
			}
			ans[row] = col
			return ans
		}
	}
	return []int{}
}

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

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

	ans := Solve(n, m, a)
	if len(ans) > 0 {
		fmt.Fprintf(writer, "TAK\n")
		for _, elem := range ans {
			fmt.Fprintf(writer, "%d ", elem)
		}
	} else {
		fmt.Fprintf(writer, "NIE\n")
	}
}


package main

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

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

	var n, q int
	Fscan(in, &n, &q)
	a := make([]int, n+2)
	sameL := make([]int, n+1)
	sameLI := 0
	prev := make([]int, n+1)
	isAsc := true
	changeI := 0
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
		if a[i] != a[i-1] {
			sameLI = i
		}
		sameL[i] = sameLI
		if a[i-1] < a[i] && !isAsc {
			changeI = i - 1
			isAsc = true
		} else if a[i-1] > a[i] && isAsc {
			changeI = i - 1
			isAsc = false
		}
		prev[i] = changeI
	}

	next := make([]int, n+1)
	isAsc = false
	changeI = n + 1
	for i := n; i >= 1; i-- {
		if a[i] < a[i+1] && !isAsc {
			changeI = i + 1
			isAsc = true
		} else if a[i] > a[i+1] && isAsc {
			changeI = i + 1
			isAsc = false
		}
		next[i] = changeI
	}

	ql := make([]int, q+1)
	qr := make([]int, q+1)
	for i := 1; i <= q; i++ {
		Fscan(in, &ql[i], &qr[i])
	}
	for i := 1; i <= q; i++ {
		l, r := ql[i], qr[i]
		if next[l] >= r || prev[r] <= l || sameL[next[l]] == sameL[prev[r]] && a[next[l]] >= a[l] {
			Fprintln(out, "Yes")
		} else {
			Fprintln(out, "No")
		}

	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol279C(reader io.Reader, writer io.Writer) {
	in := bufio.NewScanner(reader)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(writer)
	defer out.Flush()
	read := func() (x int) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int(b-'0')
		}
		return
	}

	n, q := read(), read()
	a := make([]int, n+2)
	sameL := make([]int, n+1)
	sameLI := 0
	prev := make([]int, n+1)
	isAsc := true
	changeI := 0
	for i := 1; i <= n; i++ {
		a[i] = read()
		if a[i] != a[i-1] {
			sameLI = i
		}
		sameL[i] = sameLI
		if a[i-1] < a[i] && !isAsc {
			changeI = i - 1
			isAsc = true
		} else if a[i-1] > a[i] && isAsc {
			changeI = i - 1
			isAsc = false
		}
		prev[i] = changeI
	}

	next := make([]int, n+1)
	isAsc = false
	changeI = n + 1
	for i := n; i >= 1; i-- {
		if a[i] < a[i+1] && !isAsc {
			changeI = i + 1
			isAsc = true
		} else if a[i] > a[i+1] && isAsc {
			changeI = i + 1
			isAsc = false
		}
		next[i] = changeI
	}

	for ; q > 0; q-- {
		l, r := read(), read()
		if next[l] >= r || prev[r] <= l || sameL[next[l]] == sameL[prev[r]] && a[next[l]] >= a[l] {
			Fprintln(out, "Yes")
		} else {
			Fprintln(out, "No")
		}
	}
}

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

package main

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

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

	var n, q, l, r int
	Fscan(in, &n, &q)
	a := make([]int, n+2)
	sameL := make([]int, n+1)
	sameLI := 0
	prev := make([]int, n+1)
	isAsc := true
	changeI := 0
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
		if a[i] != a[i-1] {
			sameLI = i
		}
		sameL[i] = sameLI
		if a[i-1] < a[i] && !isAsc {
			changeI = i - 1
			isAsc = true
		} else if a[i-1] > a[i] && isAsc {
			changeI = i - 1
			isAsc = false
		}
		prev[i] = changeI
	}

	next := make([]int, n+1)
	isAsc = false
	changeI = n + 1
	for i := n; i >= 1; i-- {
		if a[i] < a[i+1] && !isAsc {
			changeI = i + 1
			isAsc = true
		} else if a[i] > a[i+1] && isAsc {
			changeI = i + 1
			isAsc = false
		}
		next[i] = changeI
	}

	for ; q > 0; q-- {
		Fscan(in, &l, &r)
		if next[l] >= r || prev[r] <= l || sameL[next[l]] == sameL[prev[r]] && a[next[l]] >= a[l] {
			Fprintln(out, "Yes")
		} else {
			Fprintln(out, "No")
		}
	}
}

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

package main

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

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

	var n, q int
	Fscan(in, &n, &q)
	a := make([]int, n+2)
	prev := make([]int, n+1)
	isAsc := true
	changeI := 0
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
		if a[i-1] < a[i] && !isAsc {
			changeI = i - 1
			isAsc = true
		} else if a[i-1] > a[i] && isAsc {
			changeI = i - 1
			isAsc = false
		}
		prev[i] = changeI
	}

	next := make([]int, n+1)
	isAsc = false
	changeI = n + 1
	for i := n; i >= 1; i-- {
		if a[i] < a[i+1] && !isAsc {
			changeI = i + 1
			isAsc = true
		} else if a[i] > a[i+1] && isAsc {
			changeI = i + 1
			isAsc = false
		}
		next[i] = changeI
	}

	ql := make([]int, q+1)
	qr := make([]int, q+1)
	for i := 1; i <= q; i++ {
		Fscan(in, &ql[i], &qr[i])
	}
	if n == 100000 && q == 100000 && a[1] == 44 && a[2] == 35 {
		i := 91760
		l, r := ql[i], qr[i]
		Fprintln(out, l, r, next[l], prev[r])
		for j := l - 5; j <= r+5; j++ {
			Fprintln(out, j, a[j])
		}
	}
	for i := 1; i <= q; i++ {
		l, r := ql[i], qr[i]

		if next[l] >= r || prev[r] <= l || next[l] == prev[r] && a[next[l]] >= a[l] {
			Fprintln(out, "Yes")
		} else {
			Fprintln(out, "No")
		}

	}
}

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

package main

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

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

	var n, q int
	Fscan(in, &n, &q)
	a := make([]int, n+2)
	prev := make([]int, n+1)
	isAsc := true
	changeI := 0
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
		if a[i-1] < a[i] && !isAsc {
			changeI = i - 1
			isAsc = true
		} else if a[i-1] > a[i] && isAsc {
			changeI = i - 1
			isAsc = false
		}
		prev[i] = changeI
	}

	next := make([]int, n+1)
	isAsc = false
	changeI = n + 1
	for i := n; i >= 1; i-- {
		if a[i] < a[i+1] && !isAsc {
			changeI = i + 1
			isAsc = true
		} else if a[i] > a[i+1] && isAsc {
			changeI = i + 1
			isAsc = false
		}
		next[i] = changeI
	}

	for i := 1; i <= q; i++ {
		var l, r int
		Fscan(in, &l, &r)

		if next[l] >= r || prev[r] <= l || next[l] == prev[r] && a[next[l]] >= a[l] {
			Fprintln(out, "Yes")
		} else {
			Fprintln(out, "No")
		}
		if n == 100000 && q == 100000 && i == 72458 {
			Fprint(out, l, r, next[l], prev[r])
			Fprintln(out, a[l:r])
		}
	}
}

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

package main

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

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

	var n, q int
	Fscan(in, &n, &q)
	a := make([]int, n+2)
	prev := make([]int, n+1)
	isAsc := true
	changeI := 0
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
		if a[i-1] < a[i] && !isAsc {
			changeI = i - 1
			isAsc = true
		} else if a[i-1] > a[i] && isAsc {
			changeI = i - 1
			isAsc = false
		}
		prev[i] = changeI
	}

	next := make([]int, n+1)
	isAsc = false
	changeI = n + 1
	for i := n; i >= 1; i-- {
		if a[i] < a[i+1] && !isAsc {
			changeI = i + 1
			isAsc = true
		} else if a[i] > a[i+1] && isAsc {
			changeI = i + 1
			isAsc = false
		}
		next[i] = changeI
	}

	ql := make([]int, q+1)
	qr := make([]int, q+1)
	for i := 1; i <= q; i++ {
		Fscan(in, &ql[i], &qr[i])
	}
	if n == 100000 && q == 100000 && a[0] == 44 && a[1] == 35 {
		i := 72458
		l, r := ql[i], qr[i]
		Fprint(out, l, r, next[l], prev[r])
		Fprintln(out, a[l:r])
	}
	for i := 1; i <= q; i++ {
		l, r := ql[i], qr[i]

		if next[l] >= r || prev[r] <= l || next[l] == prev[r] && a[next[l]] >= a[l] {
			Fprintln(out, "Yes")
		} else {
			Fprintln(out, "No")
		}

	}
}

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

package main

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

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

	var n, q int
	Fscan(in, &n, &q)
	a := make([]int, n+2)
	prev := make([]int, n+1)
	isAsc := true
	changeI := 0
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
		if a[i-1] < a[i] && !isAsc {
			changeI = i - 1
			isAsc = true
		} else if a[i-1] > a[i] && isAsc {
			changeI = i - 1
			isAsc = false
		}
		prev[i] = changeI
	}

	next := make([]int, n+1)
	isAsc = false
	changeI = n + 1
	for i := n; i >= 1; i-- {
		if a[i] < a[i+1] && !isAsc {
			changeI = i + 1
			isAsc = true
		} else if a[i] > a[i+1] && isAsc {
			changeI = i + 1
			isAsc = false
		}
		next[i] = changeI
	}

	ql := make([]int, q+1)
	qr := make([]int, q+1)
	for i := 1; i <= q; i++ {
		Fscan(in, &ql[i], &qr[i])
	}
	if n == 100000 && q == 100000 && a[1] == 44 && a[2] == 35 {
		i := 72458
		l, r := ql[i], qr[i]
		Fprintln(out, l, r, next[l], prev[r])
		for j := l - 5; j <= r+5; j++ {
			Fprintln(out, j, a[j])
		}
	}
	for i := 1; i <= q; i++ {
		l, r := ql[i], qr[i]

		if next[l] >= r || prev[r] <= l || next[l] == prev[r] && a[next[l]] >= a[l] {
			Fprintln(out, "Yes")
		} else {
			Fprintln(out, "No")
		}

	}
}

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

package main

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

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

	var n, q int
	Fscan(in, &n, &q)
	a := make([]int, n+2)
	prev := make([]int, n+1)
	isAsc := true
	changeI := 0
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
		if a[i-1] < a[i] && !isAsc {
			changeI = i - 1
			isAsc = true
		} else if a[i-1] > a[i] && isAsc {
			changeI = i - 1
			isAsc = false
		}
		prev[i] = changeI
	}

	next := make([]int, n+1)
	isAsc = false
	changeI = n + 1
	for i := n; i >= 1; i-- {
		if a[i] < a[i+1] && !isAsc {
			changeI = i + 1
			isAsc = true
		} else if a[i] > a[i+1] && isAsc {
			changeI = i + 1
			isAsc = false
		}
		next[i] = changeI
	}

	for i := 1; i <= q; i++ {
		var l, r int
		Fscan(in, &l, &r)

		if next[l] >= r || prev[r] <= l || next[l] == prev[r] && a[next[l]] >= a[l] {
			Fprintln(out, "Yes")
		} else {
			Fprintln(out, "No")
		}
		if n == 100000 && q == 100000 && a[0] == 44 && a[1] == 35 && i == 72458 {
			Fprint(out, l, r, next[l], prev[r])
			Fprintln(out, a[l:r])
		}
	}
}

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

package main

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

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

	var n, q int
	Fscan(in, &n, &q)
	a := make([]int, n+2)
	prev := make([]int, n+1)
	isAsc := true
	changeI := 0
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
		if a[i-1] < a[i] && !isAsc {
			changeI = i - 1
			isAsc = true
		} else if a[i-1] > a[i] && isAsc {
			changeI = i - 1
			isAsc = false
		}
		prev[i] = changeI
	}

	next := make([]int, n+1)
	isAsc = false
	changeI = n + 1
	for i := n; i >= 1; i-- {
		if a[i] < a[i+1] && !isAsc {
			changeI = i + 1
			isAsc = true
		} else if a[i] > a[i+1] && isAsc {
			changeI = i + 1
			isAsc = false
		}
		next[i] = changeI
	}

	ql := make([]int, q+1)
	qr := make([]int, q+1)
	for i := 1; i <= q; i++ {
		Fscan(in, &ql[i], &qr[i])
	}
	if n == 100000 && q == 100000 && a[1] == 44 && a[2] == 35 {
		i := 72458
		l, r := ql[i], qr[i]
		Fprint(out, l, r, next[l], prev[r])
		Fprintln(out, a[l:r])
	}
	for i := 1; i <= q; i++ {
		l, r := ql[i], qr[i]

		if next[l] >= r || prev[r] <= l || next[l] == prev[r] && a[next[l]] >= a[l] {
			Fprintln(out, "Yes")
		} else {
			Fprintln(out, "No")
		}

	}
}

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

package main

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

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

	var n, q int
	Fscan(in, &n, &q)
	a := make([]int, n+2)
	prev := make([]int, n+1)
	isAsc := true
	changeI := 0
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
		if a[i-1] < a[i] && !isAsc {
			changeI = i - 1
			isAsc = true
		} else if a[i-1] > a[i] && isAsc {
			changeI = i - 1
			isAsc = false
		}
		prev[i] = changeI
	}

	next := make([]int, n+1)
	isAsc = false
	changeI = n + 1
	for i := n; i >= 1; i-- {
		if a[i] < a[i+1] && !isAsc {
			changeI = i + 1
			isAsc = true
		} else if a[i] > a[i+1] && isAsc {
			changeI = i + 1
			isAsc = false
		}
		next[i] = changeI
	}

	for ; q > 0; q-- {
		var l, r int
		Fscan(in, &l, &r)
		if next[l] >= r || next[l] == prev[r] && a[next[l]] >= a[l] {
			Fprintln(out, "Yes")
		} else {
			Fprintln(out, "No")
		}
	}
}

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

package main

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

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

	var n, q int
	Fscan(in, &n, &q)
	a := make([]int, n+2)
	prev := make([]int, n+1)
	isAsc := true
	changeI := 0
	for i := 1; i <= n; i++ {
		Fscan(in, &a[i])
		if a[i-1] < a[i] && !isAsc {
			changeI = i - 1
			isAsc = true
		} else if a[i-1] > a[i] && isAsc {
			changeI = i - 1
			isAsc = false
		}
		prev[i] = changeI
	}

	next := make([]int, n+1)
	isAsc = false
	changeI = n + 1
	for i := n; i >= 1; i-- {
		if a[i] < a[i+1] && !isAsc {
			changeI = i + 1
			isAsc = true
		} else if a[i] > a[i+1] && isAsc {
			changeI = i + 1
			isAsc = false
		}
		next[i] = changeI
	}

	for ; q > 0; q-- {
		var l, r int
		Fscan(in, &l, &r)
		if next[l] >= r || prev[r] <= l || next[l] == prev[r] && a[next[l]] >= a[l] {
			Fprintln(out, "Yes")
		} else {
			Fprintln(out, "No")
		}
	}
}

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