Three years have passes and nothing changed. It is still raining in London, and Mr. Black has to close all the doors in his home in order to not be flooded. Once, however, Mr. Black became so nervous that he opened one door, then another, then one more and so on until he opened all the doors in his house.There are exactly two exits from Mr. Black's house, let's name them left and right exits. There are several doors in each of the exits, so each door in Mr. Black's house is located either in the left or in the right exit. You know where each door is located. Initially all the doors are closed. Mr. Black can exit the house if and only if all doors in at least one of the exits is open. You are given a sequence in which Mr. Black opened the doors, please find the smallest index $$$k$$$ such that Mr. Black can exit the house after opening the first $$$k$$$ doors.We have to note that Mr. Black opened each door at most once, and in the end all doors became open.

The first line contains integer $$$n$$$ ($$$2 \le n \le 200\,000$$$) — the number of doors. The next line contains $$$n$$$ integers: the sequence in which Mr. Black opened the doors. The $$$i$$$-th of these integers is equal to $$$0$$$ in case the $$$i$$$-th opened door is located in the left exit, and it is equal to $$$1$$$ in case it is in the right exit. It is guaranteed that there is at least one door located in the left exit and there is at least one door located in the right exit.

Print the smallest integer $$$k$$$ such that after Mr. Black opened the first $$$k$$$ doors, he was able to exit the house.

NoteIn the first example the first two doors are from the left exit, so when Mr. Black opened both of them only, there were two more closed door in the left exit and one closed door in the right exit. So Mr. Black wasn't able to exit at that moment.When he opened the third door, all doors from the right exit became open, so Mr. Black was able to exit the house.In the second example when the first two doors were opened, there was open closed door in each of the exit.With three doors opened Mr. Black was able to use the left exit.

package main

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

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

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

func main() {
	//STDOUT MUST BE FLUSHED MANUALLY
	defer writer.Flush()
	var n int
	scanf("%d\n", &n)
	doors := make([]int, n)
	for i := range doors {
		scanf("%d ", &doors[i])
	}
	var zeros, ones int
	for _, door := range doors {
		if door == 0 {
			zeros++
		} else {
			ones++
		}
	}
	var cnt int
	for i, door := range doors {
		if door == 0 {
			zeros--
		} else {
			ones--
		}
		if zeros == 0 || ones == 0 {
			cnt = i + 1
			break
		}
	}
	printf("%d\n", cnt)
}

// A. The Doors
/*
Three years have passes and nothing changed. It is still raining in London, and Mr. Black has to close all the doors in his home in order to not be flooded. Once, however, Mr. Black became so nervous that he opened one door, then another, then one more and so on until he opened all the doors in his house.

There are exactly two exits from Mr. Black's house, let's name them left and right exits. There are several doors in each of the exits, so each door in Mr. Black's house is located either in the left or in the right exit. You know where each door is located. Initially all the doors are closed. Mr. Black can exit the house if and only if all doors in at least one of the exits is open. You are given a sequence in which Mr. Black opened the doors, please find the smallest index 𝑘 such that Mr. Black can exit the house after opening the first 𝑘 doors.

We have to note that Mr. Black opened each door at most once, and in the end all doors became open.

Input
The first line contains integer 𝑛 (2≤𝑛≤200000) — the number of doors.

The next line contains 𝑛 integers: the sequence in which Mr. Black opened the doors. The 𝑖-th of these integers is equal to 0 in case the 𝑖-th opened door is located in the left exit, and it is equal to 1 in case it is in the right exit.

It is guaranteed that there is at least one door located in the left exit and there is at least one door located in the right exit.

Output
Print the smallest integer 𝑘 such that after Mr. Black opened the first 𝑘 doors, he was able to exit the house.

Examples
Input
5
0 0 1 0 0

Output
3

Input
4
1 0 0 1

Output
3

Note
In the first example the first two doors are from the left exit, so when Mr. Black opened both of them only, there were two more closed door in the left exit and one closed door in the right exit. So Mr. Black wasn't able to exit at that moment.

When he opened the third door, all doors from the right exit became open, so Mr. Black was able to exit the house.

In the second example when the first two doors were opened, there was open closed door in each of the exit.

With three doors opened Mr. Black was able to use the left exit.
*/
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	input1, _ := reader.ReadString('\n')
	n, _ := strconv.ParseInt(strings.TrimSpace(input1), 10, 64)

	input2, _ := reader.ReadString('\n')
	arr := strings.Split(strings.TrimSpace(input2), " ")

	var i, z, o int64
	for ; i < n; i++ {
		v, _ := strconv.ParseInt(arr[i], 10, 64)
		if v == 0 {
			z = i + 1
		} else {
			o = i + 1
		}
	}

	if z < o {
		fmt.Print(z)
	} else {
		fmt.Print(o)
	}
}

package main

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

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

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

	mat := make([]int,n)
	for i:=0;i < n;i++{
		fmt.Fscanf(buf, "%d", &mat[i])
	}
	fmt.Fscanf(buf, "\n")
	for i:= n-2;i >=0;i --{
		if mat[i]!=mat[i+1]{
			fmt.Printf("%d\n",i+1)
			return
		}
	}

}

	package main

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

	func sortStringWithBuble(s string) string  {
		b := []byte(s)
		for i := 0 ;i  < len(s);i++{
			for j := i + 1 ;j < len(s)  ;j++{
				if(b[i] > b[j]){
					temp := b[j];
					b[j] = b[i]
					b[i] = temp
				}
			}
		}
		rt := string(b)
		return rt
	}
	func min1(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 n int
		var x int
		fmt.Fscanf(in,"%d\n",&n)
		l,r := 0,0
		for i := 0 ; i < n;i++{
			fmt.Fscanf(in,"%d",&x)
			if(x == 0){
				l = i + 1
			}else{
				r = i + 1
			}
		}
		fmt.Fprintf(out,"%d\n",min1(l,r))

	}

package main

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

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

package main

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

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

func p(a ...interface{})                { fmt.Fprint(writer, a...) }
func pln(a ...interface{})              { fmt.Fprintln(writer, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func scan(a ...interface{})            { fmt.Fscan(reader, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func main() {
	defer writer.Flush()

	var n, a int
	left, right := 0, 0
	scan(&n)
	for i := 1; i <= n; i++ {
		scan(&a)
		if a == 0 {
			left = i
		} else if a == 1 {
			right = i
		}
	}

	if left < right {
		pln(left)
	} else {
		pln(right)
	}
	return
}

package main

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

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

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

	lastLeft := 0
	lastRight := 0
	for i := 1; i <= n; i++ {
		var door rune
		fmt.Fscan(in, &door)

		if door == 0 {
			lastLeft = i
		} else if door == 1 {
			lastRight = i
		} else {
			fmt.Println("hell", door)
		}
	}

	if lastLeft < lastRight {
		fmt.Println(lastLeft)
	} else {
		fmt.Println(lastRight)
	}
}

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

package main

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

func Solve(n int, doors []int) int {
	var zeros, ones int
	for i, elem := range doors {
		if elem == 0 {
			zeros = i + 1
		} else if elem == 1 {
			ones = i + 1
		}
	}
	if zeros < ones {
		return zeros
	}
	return ones
}

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

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

	fmt.Fprintf(writer, "%d\n", Solve(n, doors))
}


You are given an array of n integers a1... an. The cost of a subsegment is the number of unordered pairs of distinct indices within the subsegment that contain equal elements. Split the given array into k non-intersecting non-empty subsegments so that the sum of their costs is minimum possible. Each element should be present in exactly one subsegment.

The first line contains two integers n and k (2 ≤ n ≤ 105, 2 ≤ k ≤ min (n, 20))  — the length of the array and the number of segments you need to split the array into. The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of the array.

Print single integer: the minimum possible total cost of resulting subsegments.

NoteIn the first example it's optimal to split the sequence into the following three subsegments: [1], [1, 3], [3, 3, 2, 1]. The costs are 0, 0 and 1, thus the answer is 1.In the second example it's optimal to split the sequence in two equal halves. The cost for each half is 4.In the third example it's optimal to split the sequence in the following way: [1, 2, 2, 2, 1], [2, 1, 1, 1, 2], [2, 1, 1]. The costs are 4, 4, 1.

package main

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

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 toInt(buf []byte) (o int) {
	for _, v := range buf {
		o = o*10 + int(v-'0')
	}
	return
}

type pair struct {
	first int
	second int
}

type pairs []pair

func (a pairs) Len() int {return len(a)}
func (a pairs) Swap(i,j int) {a[i], a[j] = a[j], a[i]}
func (a pairs) Less(i,j int) bool {
	return a[i].first < a[j].first
	if a[i].first > a[j].first {
		return false
	}
	return a[i].second <= a[j].second
}

var n,k,left,right int
var dp [21][100001]int64
var a[100001]int
var cost int64
var cnt[100001]int64

func solve(l,r,L,R,t int) {
	if l > r || L > R {
		return
	}
	m := (r + l) >> 1
	//P := min(R,m)
	for i := left;i < L; i++ {
		cost -= cnt[a[i]] - 1
		cnt[a[i]]--
	}
	for i := left - 1;i >= L; i -- {
		cost += cnt[a[i]]
		cnt[a[i]]++
	}
	for i := right + 1;i <= m; i ++ {
		cost += cnt[a[i]]
		cnt[a[i]]++
	}
	for i := right;i > m; i -- {
		cost -= cnt[a[i]] - 1
		cnt[a[i]]--
	}
	left = L
	right = m
	M := 0
	var best int64
	best = 200000000000000000
	for i := L; i <= min(R,m); i++ {
		if dp[t - 1][i - 1] + cost < best {
			best = dp[t - 1][i - 1] + cost
			M = i
		}
		if i != min(R,m) {
			cost -= cnt[a[left]] - 1
			cnt[a[left]]--
			left++
		}
	}
	dp[t][m] = best
	//fmt.Println(t, m, dp[t][m])
	if r < t {
		return
	}
	solve(l, m - 1, L, M, t)
	solve(m + 1, r, M, R, t)
}

func main() {
	in := bufio.NewReader(os.Stdin)
	//in, _ := os.Open("a.txt")
	fmt.Fscanf(in, "%d %d\n", &n,&k)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(in, "%d", &a[i])
		cost += cnt[a[i]]
		cnt[a[i]]++
		dp[1][i] = cost
	}
	for i:=2; i<=k; i++ {
		left = 1
		right = 1
		for j:=1; j <= n; j++ {
			cnt[j] = 0
		}
		cnt[a[1]] = 1
		cost = 0
		solve(i, n,i, n, i)
	}
	fmt.Println(dp[k][n])
}


You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59.You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format.For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39.

The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes.

The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them.

package main

import (
        "fmt"
        "strconv"
        "strings"
)

func main() {
        var format int
        fmt.Scanln(&format)
        var str string
        fmt.Scanln(&str)
        t := strings.Split(str, ":")
        aStr, bStr := t[0], t[1]
        a, _ := strconv.Atoi(aStr)
        b, _ := strconv.Atoi(bStr)
        if format == 12 {
                if a > 12 || a == 0 {
                        a %= 10
                        if a == 0 {
                                aStr = "10"
                        } else {
                                aStr = fmt.Sprintf("0%v", a)
                        }
                }
        } else if format == 24 {
                if a > 23 {
                        a %= 10
                        aStr = fmt.Sprintf("0%v", a)
                }
        }
        if b > 59 {
                b %= 10
                bStr = fmt.Sprintf("0%v", b)
        }
        fmt.Println(aStr + ":" + bStr)
}

package main

import "bufio"
import "os"
import "io/ioutil"
import "fmt"

func changeHour(cat int, hour int) int {
	if cat == 12 {
		if (hour >= 1) && (hour <= 12) {
			return hour
		}
		if hour%10 == 0 {
			return 10
		}
		return hour % 10
	}
	if hour <= 23 {
		return hour
	}
	return hour % 10
}

func changeMinute(min int) int {
	if min <= 59 {
		return min
	}
	return min % 10
}

func main() {
	var cat, hour, min int
	readInt(&cat)
	readInt(&hour)
	readInt(&min)

	h := changeHour(cat, hour)
	m := changeMinute(min)
	fmt.Printf("%02d:%02d\n", h, m)
}

var out *bufio.Writer
var iPointer int
var nPointer int
var iBuffer []byte

func init() {
	iPointer = 0
	iBuffer, _ = ioutil.ReadAll(os.Stdin)
	nPointer = len(iBuffer)
}

func isDigit(c byte) bool {
	return (c >= '0') && (c <= '9')
}

func readInt(v *int) bool {
	neg := false
	for (iPointer < nPointer) && !isDigit(iBuffer[iPointer]) {
		neg = (iBuffer[iPointer] == '-')
		iPointer++
	}
	if iPointer >= nPointer {
		return false
	}
	var a int
	for (iPointer < nPointer) && isDigit(iBuffer[iPointer]) {
		a = a*10 + int(iBuffer[iPointer]-'0')
		iPointer++
	}
	if neg {
		*v = -a
	} else {
		*v = a
	}
	return true
}

// http://codeforces.com/contest/722/problem/0
package main

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

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

const (
	MOD = 1e9 + 7
)

type (

)

func main () {
	var (
		N int
		str string
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &N)
	fmt.Fscanln(input, &str)

	ans := []rune(str)
	if N == 12 {
		if ans[0] > '1' {
			if ans[1] == '0' {
				ans[0] = '1'
			} else {
				ans[0] = '0'
			}
		}

		if ans[0] == '1' && ans[1] > '2' {
			ans[1] = '0'
		}

		if ans[0] == '0' && ans[1] == '0' {
			ans[1] = '1'
		}
	} else {
		if ans[1] > '9' || ans[1] < '0' {
			ans[1] = '0'
		}

		if ans[0] > '2' || ans[0] < '0' {
			ans[0] = '0'
		}

		if ans[0] == '2' && ans[1] > '3' {
			ans[1] = '0'
		}
	}

	if ans[3] > '5' || ans[3] < '0' {
		ans[3] = '0'
	}

	fmt.Println(string(ans))
}

package main

import (
	"fmt"
)

func main() {
	var format, hour, minute int
	fmt.Scanf("%d\n", &format)
	fmt.Scanf("%d:%d", &hour, &minute)
	if minute > 59 {
		minute = 10 + minute%10
	}
	if format == 24 {
		if hour > 23 {
			hour = hour % 10
		}
	}
	if format == 12 {
		if hour/10 > 1 {
			if hour%10 == 0 {
				hour = 10 + hour%10
			} else {
				hour = hour % 10
			}
		}
		if hour > 12 {
			hour = hour % 10
		}
		if hour == 0 {
			hour = 10
		}
	}
	fmt.Printf("%02d:%02d\n", hour, minute)
}

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	var time_format int
	var time string
	fmt.Scanf("%d\n%s", &time_format, &time)

	new_time := []byte(time)
	time_list := strings.Split(time, ":")
	hours, _ := strconv.Atoi(time_list[0])
	minutes, _ := strconv.Atoi(time_list[1])
	if minutes > 59 {
		new_time[3] = '1'
	}
	if time_format == 12 {
		if hours < 1 {
			new_time[1] = '1'
		} else if hours > 12 {
			if time[1] != '0' {
				new_time[0] = '0'
			} else {
				new_time[0] = '1'
			}
		}
	} else if time_format == 24 && hours > 23 {
		new_time[0] = '0'
	}
	fmt.Println(string(new_time))
}

package main

import "bufio"
import "os"
import "io/ioutil"
import "fmt"

func changeHour(cat int, hour int) int {
	if cat == 12 {
		if (hour >= 1) && (hour <= 12) {
			return hour
		}
		hour = hour % 10
		if hour == 0 {
			hour++
		}
		return hour
	}
	if hour <= 23 {
		return hour
	}
	return hour % 10
}

func changeMinute(min int) int {
	if min <= 59 {
		return min
	}
	return min % 10
}

func main() {
	var cat, hour, min int
	readInt(&cat)
	readInt(&hour)
	readInt(&min)

	h := changeHour(cat, hour)
	m := changeMinute(min)
	fmt.Printf("%02d:%02d\n", h, m)
}

var out *bufio.Writer
var iPointer int
var nPointer int
var iBuffer []byte

func init() {
	iPointer = 0
	iBuffer, _ = ioutil.ReadAll(os.Stdin)
	nPointer = len(iBuffer)
}

func isDigit(c byte) bool {
	return (c >= '0') && (c <= '9')
}

func readInt(v *int) bool {
	neg := false
	for (iPointer < nPointer) && !isDigit(iBuffer[iPointer]) {
		neg = (iBuffer[iPointer] == '-')
		iPointer++
	}
	if iPointer >= nPointer {
		return false
	}
	var a int
	for (iPointer < nPointer) && isDigit(iBuffer[iPointer]) {
		a = a*10 + int(iBuffer[iPointer]-'0')
		iPointer++
	}
	if neg {
		*v = -a
	} else {
		*v = a
	}
	return true
}

// http://codeforces.com/contest/722/problem/0
package main

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

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

const (
	MOD = 1e9 + 7
)

type (

)

func main () {
	var (
		N int
		str string
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &N)
	fmt.Fscanln(input, &str)

	ans := []rune(str)
	if N == 12 {
		if ans[0] > '1' || ans[0] < '0' {
			ans[0] = '0'
		}

		if ans[1] > '9' || ans[1] < '0' {
			ans[1] = '0'
		}

		if ans[0] == '1' && ans[1] > '2' {
			ans[1] = '0'
		}

		if ans[0] == '0' && ans[1] == '0' {
			ans[1] = '1'
		}
	} else {
		if ans[1] > '9' || ans[1] < '0' {
			ans[1] = '0'
		}

		if ans[0] > '2' || ans[0] < '0' {
			ans[0] = '0'
		}

		if ans[0] == '2' && ans[1] > '3' {
			ans[1] = '0'
		}
	}

	if ans[3] > '5' || ans[3] < '0' {
		ans[3] = '0'
	}

	fmt.Println(string(ans))
}

// http://codeforces.com/contest/722/problem/0
package main

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

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

const (
	MOD = 1e9 + 7
)

type (

)

func main () {
	var (
		N int
		str string
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscanln(input, &N)
	fmt.Fscanln(input, &str)

	ans := []rune(str)
	if N == 12 {
		if ans[0] > '1' || ans[0] < '0' {
			ans[0] = '0'
		}

		if ans[0] == '1' && ans[1] > '2' {
			ans[1] = '0'
		}

		if ans[3] > '5'  {
			ans[3] = '0'
		}

	} else {
		if ans[0] > '2' || ans[0] < '0' {
			ans[0] = '0'
		}

		if ans[0] == '2' && ans[1] > '4' {
			ans[1] = '0'
		}

		if ans[3] > '5'  {
			ans[3] = '0'
		}
	}

	fmt.Println(string(ans))
}

package main

import (
	"fmt"
)

func main() {
	var format, hour, minute int
	fmt.Scanf("%d\n", &format)
	fmt.Scanf("%d:%d", &hour, &minute)
	if minute > 59 {
		minute = 10 + minute%10
	}
	if format == 24 {
		if hour > 23 {
			hour = hour % 10
		}
	}
	if format == 12 {
		if hour > 12 {
			hour = hour % 10
		}
	}
	fmt.Printf("%02d:%d\n", hour, minute)
}

package main

import (
	"fmt"
)

func main() {
	var format, hour, minute int
	fmt.Scanf("%d\n", &format)
	fmt.Scanf("%d:%d", &hour, &minute)
	if minute > 59 {
		minute = 10 + minute%10
	}
	if format == 24 {
		if hour > 23 {
			hour = hour % 10
		}
	}
	if format == 12 {
		if hour > 12 {
			hour = hour % 10
		}
	}
	fmt.Printf("%02d:%02d\n", hour, minute)
}

package main

import (
	"fmt"
)

func main() {
	var format, hour, minute int
	fmt.Scanf("%d\n", &format)
	fmt.Scanf("%d:%d", &hour, &minute)
	if minute > 59 {
		minute = 10 + minute%10
	}
	if format == 24 {
		if hour > 23 {
			hour = hour % 10
		}
	}
	if format == 12 {
		if hour > 12 {
			hour = hour % 10
		}
		if hour == 0 {
			hour = 1
		}
	}
	fmt.Printf("%02d:%02d\n", hour, minute)
}

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	var time_format int
	var time string
	fmt.Scanf("%d\n%s", &time_format, &time)

	new_time := []byte(time)
	time_list := strings.Split(time, ":")
	hours, _ := strconv.Atoi(time_list[0])
	minutes, _ := strconv.Atoi(time_list[1])
	if minutes > 59 {
		new_time[3] = '1'
	}
	if (time_format == 12 && hours > 12) || (time_format == 24 && hours > 23) {
		new_time[0] = '0'
	}
	fmt.Println(string(new_time))
}

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	var time_format int
	var time string
	fmt.Scanf("%d\n%s", &time_format, &time)

	new_time := []byte(time)
	time_list := strings.Split(time, ":")
	hours, _ := strconv.Atoi(time_list[0])
	minutes, _ := strconv.Atoi(time_list[1])
	if minutes > 59 {
		new_time[3] = '1'
	}
	if time_format == 12 && hours < 1 {
		new_time[1] = '1'
	} else if (time_format == 12 && (hours > 12 || hours < 1)) || (time_format == 24 && hours > 23) {
		new_time[0] = '0'
	}
	fmt.Println(string(new_time))
}


Naman has two binary strings $$$s$$$ and $$$t$$$ of length $$$n$$$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $$$s$$$ into $$$t$$$ using the following operation as few times as possible.In one operation, he can choose any subsequence of $$$s$$$ and rotate it clockwise once.For example, if $$$s = 1\textbf{1}101\textbf{00}$$$, he can choose a subsequence corresponding to indices ($$$1$$$-based) $$$\{2, 6, 7 \}$$$ and rotate them clockwise. The resulting string would then be $$$s = 1\textbf{0}101\textbf{10}$$$.A string $$$a$$$ is said to be a subsequence of string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deleting some characters without changing the ordering of the remaining characters.To perform a clockwise rotation on a sequence $$$c$$$ of size $$$k$$$ is to perform an operation which sets $$$c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$$$ simultaneously.Determine the minimum number of operations Naman has to perform to convert $$$s$$$ into $$$t$$$ or say that it is impossible.

The first line contains a single integer $$$n$$$ $$$(1 \le n \le 10^6)$$$ — the length of the strings. The second line contains the binary string $$$s$$$ of length $$$n$$$. The third line contains the binary string $$$t$$$ of length $$$n$$$.

If it is impossible to convert $$$s$$$ to $$$t$$$ after any number of operations, print $$$-1$$$. Otherwise, print the minimum number of operations required.

NoteIn the first test, Naman can choose the subsequence corresponding to indices $$$\{2, 6\}$$$ and rotate it once to convert $$$s$$$ into $$$t$$$.In the second test, he can rotate the subsequence corresponding to all indices $$$5$$$ times. It can be proved, that it is the minimum required number of operations.In the last test, it is impossible to convert $$$s$$$ into $$$t$$$.

package main

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

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

	n := readNum(reader)
	s, _ := reader.ReadString('\n')
	t, _ := reader.ReadString('\n')
	fmt.Println(solve(n, s, t))
}

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

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

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

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

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

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

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

	return i
}

func solve(n int, s, t string) int {
	A := make([]int, n)
	var sum int
	for i := 0; i < n; i++ {
		if s[i] > t[i] {
			A[i] = 1
		} else if s[i] < t[i] {
			A[i] = -1
		}
		sum += A[i]
	}

	if sum != 0 {
		return -1
	}

	get := func(x int) int {
		var cur int
		var best int

		for i := 0; i < n; i++ {
			cur += x * A[i]
			if best < cur {
				best = cur
			}
			if cur < 0 {
				cur = 0
			}
		}
		return best
	}

	return max(get(1), get(-1))
}

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

package main

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

var (
	inn = bufio.NewScanner(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func main() {
	defer out.Flush()
	inn.Buffer([]byte{}, 10000000)
	inn.Split(bufio.ScanWords)
	//prep
	n, s, t := rd(), rds(), rds()
	var sum int
	a := make([]int, n)
	for i := range a {
		if s[i] != t[i] {
			if s[i] == '1' {
				a[i] = 1
			} else {
				a[i] = -1
			}
		}
		sum += a[i]
	}
	if sum != 0 {
		Fprint(out, -1)
		return
	}
	cal := func(x int) int {
		var cur, mx int
		for _, it := range a {
			cur = max(cur+it*x, it*x)
			mx = max(mx, cur)
		}
		return mx
	}
	Fprint(out, max(cal(1), cal(-1)))
}

//-------------------template------------------
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func maxll(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func minll(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func rd() (x int) {
	inn.Scan()
	x, _ = strconv.Atoi(string(inn.Bytes()))
	return x
}
func rdll() (x int64) {
	inn.Scan()
	x, _ = strconv.ParseInt(string(inn.Bytes()), 10, 64)
	return x
}
func rds() string {
	inn.Scan()
	return string(inn.Bytes())
}

package main

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

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

	n := readNum(reader)
	s, _ := reader.ReadString('\n')
	t, _ := reader.ReadString('\n')
	fmt.Println(solve(n, s, t))
}

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

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

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

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

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

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

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

	return i
}

func solve(n int, s, t string) int {
	A := make([]int, n)
	var sum int
	for i := 0; i < n; i++ {
		if s[i] > t[i] {
			A[i] = 1
		} else if s[i] < t[i] {
			A[i] = -1
		}
		sum += A[i]
	}

	if sum != 0 {
		return -1
	}

	get := func(x int) int {
		var cur int
		var best int

		for i := 0; i < n; i++ {
			cur += x * A[i]
			if best < cur {
				best = cur
			}
			if cur < 0 {
				cur = 0
			}
		}
		return best
	}

	return max(get(1), get(-1))
}

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


Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers:  the number of ways to distribute the cards so that Alex wins;  the number of ways to distribute the cards so that Boris wins;  the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows:  if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw;  if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).

For each test case, print three integers:   the number of ways to distribute the cards so that Alex wins;  the number of ways to distribute the cards so that Boris wins;  the number of ways to distribute the cards so that the game ends in a draw.  Since the answers can be large, print them modulo $$$998244353$$$.

NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case:   Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$;  Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$;  the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.

package main

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

var sc = bufio.NewScanner(os.Stdin)

const mod = 998244353
const N = 61

func main() {
	sc.Buffer(make([]byte, 500000*1+1), 500000)
	sc.Split(bufio.ScanWords)

	c := make([][]int, N)
	for i := 0; i < N; i++ {
		c[i] = make([]int, N)
	}
	f := make([]int, N)
	func() {
		c[0][0] = 1
		for i := 1; i < N; i++ {
			for j := 0; j < N; j++ {
				if 0 == j {
					c[i][j] = 1
				} else {
					c[i][j] = (c[i-1][j-1] + c[i-1][j]) % mod
				}
			}
		}

		f[2] = 1
		f[4] = 3
		for i := 6; i < N; i += 2 {
			f[i] = (c[i-1][i/2-1] + c[i-4][i/2-3] + f[i-4]) % mod
		}
	}()
	T := nextInt()
	for T > 0 {
		T--
		n := nextInt()
		// 2022-10-12 20:14:57
		// 2022-10-12 20:22:43
		// 2022-10-12 22:36:22
		fmt.Println(f[n], ((c[n][n/2]-f[n]-1)%mod+mod)%mod, 1)
		// 2022-10-12 22:51:14
	}
}

func max(args ...int) int {
	res := args[0]
	for _, v := range args {
		if v > res {
			res = v
		}
	}
	return res
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

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

package main

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

func ncr(n, mod int) [][]int {
	comb := make([][]int, n)
	for i := range comb {
		comb[i] = make([]int, n)
		comb[i][0] = 1
		if i == 0 { continue }
		for j := 1; j < n; j++ {
			comb[i][j] = (comb[i-1][j-1] + comb[i-1][j]) % mod
		}
	}
	return comb
}

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

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

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

		n = N/2
		dp := make([][]int, n + 1)
		comb := ncr(N, mod)

		dp[1] = make([]int, 3)
		dp[1][0] = 1
		dp[1][1] = 0
		dp[1][2] = 1

		for i := 2; i <= n; i++ {
			dp[i] = make([]int, 3)
			dp[i][0] = (comb[2*i - 1][i] + dp[i-1][1]) % mod
			dp[i][1] = (comb[2*i - 2][i - 2] + dp[i-1][0]) % mod
			dp[i][2] = 1
		}

		fmt.Fprintf(w, "%d %d %d\n", dp[n][0], dp[n][1], dp[n][2])
	}
}

package main

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

var facts = map[int]*big.Int{
	0: big.NewInt(1),
	1: big.NewInt(1),
	2: big.NewInt(2),
}

func fact(n int) *big.Int {
	if facts[n] != nil {
		return facts[n]
	}
	m := big.NewInt(int64(n))
	f := fact(n - 1)
	m.Mul(m, f)
	facts[n] = m
	return facts[n]
}

func choose(n, k int) *big.Int {
	factN := fact(n)
	factK := fact(k)
	factNMinusK := fact(n - k)
	res := big.NewInt(0)
	res.Div(factN, factK)
	res.Div(res, factNMinusK)
	return res
}

func calc(n int) (*big.Int, *big.Int, *big.Int) {
	if n == 2 {
		return big.NewInt(1), big.NewInt(0), big.NewInt(1)
	} else {
		a, b, _ := calc(n - 2)
		c1 := choose(n-1, n/2)
		c2 := choose(n-2, n/2)
		return c1.Add(c1, b), c2.Add(c2, a), big.NewInt(1)
	}
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	txt, _ := reader.ReadString('\n')
	q, _ := strconv.Atoi(strings.TrimSpace(txt))
	for ; q > 0; q-- {
		txt, _ = reader.ReadString('\n')
		n, _ := strconv.Atoi(strings.TrimSpace(txt))
		a, b, z := calc(n)
		mod := big.NewInt(998244353)
		fmt.Println(a.Mod(a, mod), b.Mod(b, mod), z.Mod(z, mod))
	}
}

package main

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

const M = 998244353

var COMBINATIONS = [][]int{}

func init() {
	N := 60

	cur := []int{1}
	COMBINATIONS = append(COMBINATIONS, cur)

	for n := 0; n < N; n++ {
		tmp := make([]int, len(cur) + 1)

		tmp[0] = 1
		for i := 0; i + 1 < len(cur); i++ {
			tmp[i + 1] = (cur[i] + cur[i + 1]) % M
		}
		tmp[len(tmp) - 1] = 1

		cur = tmp
		COMBINATIONS = append(COMBINATIONS, cur)
	}
}

func combinations(n, k int) int {
	return COMBINATIONS[n][k]
}

func solve(n int) (int, int, int) {
	a, b := 1, 0

	for cards := 4; cards <= n; cards += 2 {
		c1 := combinations(cards - 2, cards / 2)
		c2 := combinations(cards - 2, cards / 2 - 1)
		a, b = (c1 + c2 + b) % M, (c1 + a) % M
	}

	return a, b, 1
}

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

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

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

		x, y, z := solve(n)
		fmt.Println(x, y, z)
	}
}

package main

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

const MOD = 998244353

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)

		dp := make([][3]int64, n+1)
		dp[2][0] = 1
		dp[2][1] = 0
		dp[2][2] = 1

		var walk func(n int)
		walk = func(n int) {
			if n == 2 {
				return
			}
			walk(n - 2)

			a := new(big.Int).Binomial(int64(n-1), int64(n/2)).Int64()
			b := new(big.Int).Binomial(int64(n-2), int64(n/2)).Int64()
			dp[n][0] = (a + int64(dp[n-2][1])) % MOD
			dp[n][1] = (b + int64(dp[n-2][0])) % MOD
			dp[n][2] = dp[n-2][2] % MOD
		}

		walk(n)
		fmt.Fprintln(out, dp[n][0], dp[n][1], dp[n][2])
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)

const mod = 998244353
const N = 61

func main() {
	sc.Buffer(make([]byte, 500000*1+1), 500000)
	sc.Split(bufio.ScanWords)

	c := make([][]int, N)
	for i := 0; i < N; i++ {
		c[i] = make([]int, N)
	}
	f := make([]int, N)
	func() {
		c[0][0] = 1
		for i := 1; i < N; i++ {
			for j := 0; j < N; j++ {
				if 0 == j {
					c[i][j] = 1
				} else {
					c[i][j] = (c[i-1][j-1] + c[i-1][j]) % mod
				}
			}
		}

		f[2] = 1
		f[4] = 3
		for i := 6; i < N; i += 2 {
			f[i] = (c[i-1][i/2-1] + c[i-4][i/2-3] + f[i-4]) % mod
		}
	}()
	T := nextInt()
	for T > 0 {
		T--
		n := nextInt()
		// 2022-10-12 20:14:57
		// 2022-10-12 20:22:43
		// 2022-10-12 22:36:22
		fmt.Println(f[n], (c[n][n/2]-f[n]-1)%mod, 1)
		// 2022-10-12 22:51:14
	}
}

func max(args ...int) int {
	res := args[0]
	for _, v := range args {
		if v > res {
			res = v
		}
	}
	return res
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

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


Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types:  $$$+~x~y$$$ — Polycarp earns a bill of size $$$x \times y$$$;  $$$?~h~w$$$ — Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) — the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types:   $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) — Polycarp earns a bill of size $$$x \times y$$$;  $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) — Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$.  It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.

NoteThe queries of type $$$2$$$ of the example:  Neither bill fits;  Both bills fit (just checking that you got that bills can overlap);  Both bills fit (both bills are actually the same);  All bills fit (too much of free space in a wallet is not a problem);  Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").

package main

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

func solveSequential(scanner *bufio.Scanner, writer *bufio.Writer) {
	maxX := uint64(0)
	maxY := uint64(0)

	queries := readInt(scanner)
	for q := 0; q < queries; q++ {
		queryType := readString(scanner)
		if queryType == "+" {
			x := uint64(readInt64(scanner))
			y := uint64(readInt64(scanner))
			if y > x {
				maxX = max64(maxX, y)
				maxY = max64(maxY, x)
			} else {
				maxX = max64(maxX, x)
				maxY = max64(maxY, y)
			}
		} else if queryType == "?" {
			h := uint64(readInt64(scanner))
			w := uint64(readInt64(scanner))

			if h > w {
				h,w = w,h
			}

			if h >= maxY && w >= maxX {
				writef(writer, "YES\n")
			} else {
				writef(writer, "NO\n")
			}
		}
	}
}

func main() {
	var scanner *bufio.Scanner
	if len(os.Args) > 1 {
		reader, err := os.Open(os.Args[1])
		if err != nil {
			panic(err)
		}
		defer reader.Close()
		scanner = bufio.NewScanner(reader)
	} else {
		scanner = bufio.NewScanner(os.Stdin)
	}
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 0, 1024*1024), 1024*1024)

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

	solveSequential(scanner, writer)
}

//	GoLang shorthand methods for I/O go below
//	TODO wipe unused methods before submitting

func readString(sc *bufio.Scanner) string {
	if !sc.Scan() {
		panic("failed to scan next token")
	}

	return sc.Text()
}

func readInt64(sc *bufio.Scanner) int64 {
	if !sc.Scan() {
		panic("failed to scan next token")
	}

	res, err := strconv.ParseInt(sc.Text(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func readInt(sc *bufio.Scanner) int {
	return int(readInt64(sc))
}

func writef(writer *bufio.Writer, formatStr string, values ...interface{}) {
	out := fmt.Sprintf(formatStr, values...)
	_, err := writer.WriteString(out)
	if err != nil {
		panic(err)
	}
}

func max64(a, b uint64) uint64 {
	if a > b {
		return a
	}
	return b
}

package main

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

func main() {
	start := time.Now()
	defer func() {
		run := time.Since(start)
		_ = run
		// fmt.Println("Run in", run)
	}()

	var stdin *bufio.Reader
	// LOCAL
	f, _ := os.Open("input.txt")
	stdin = bufio.NewReader(f)

	// ONLINE JUDGE
	stdin = bufio.NewReader(os.Stdin)

	var n, x, y, w, h int
	var c byte

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

	fmt.Fscanln(stdin, &n)
	for ; n > 0; n-- {
		// var s string
		// fmt.Fscan(stdin, &s)
		// fmt.Println(s)
		fmt.Fscanf(stdin, "%c %d %d\n", &c, &x, &y)
		x, y = swap(x, y)
		if c == '+' {
			w = max(w, x)
			h = max(h, y)
		} else {
			if x < w || y < h {
				fmt.Println("NO")
			} else {
				fmt.Println("YES")
			}
		}
	}

}

package main

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

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

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

func init() {
	// const maxCapacity = 500050
	// buf := make([]byte, maxCapacity)
	// scanner.Buffer(buf, maxCapacity)
	scanner.Split(bufio.ScanWords)
}

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

type point struct {
	x,y int
}

func main() {
	defer writer.Flush()

	t := ReadInt()

	maior := point{-1,-1}

	for ;t > 0; t-- {

		scanner.Scan()
		tipo := scanner.Text()

		if tipo == "+" {

			v := make([]int, 2)

			v[0] = ReadInt()
			v[1] = ReadInt()

			sort.Slice(v, func (i, j int) bool {
				return v[i] > v[j]
			})

			maior.x = max(maior.x, v[0])
			maior.y = max(maior.y, v[1])

		} else {

			x := ReadInt()
			y := ReadInt()

			if (x >= maior.x && y >= maior.y) || (y >= maior.x && x >= maior.y) {
				writer.WriteString("YES\n")
			} else{
				writer.WriteString("NO\n")

			}
		}
	}
}

package main

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

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

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

func init() {
	// const maxCapacity = 500050
	// buf := make([]byte, maxCapacity)
	// scanner.Buffer(buf, maxCapacity)
	scanner.Split(bufio.ScanWords)
}

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

type point struct {
	x,y int
}

func main() {
	defer writer.Flush()

	t := ReadInt()

	maiorX := point{-1,-1}
	maiorY := point{-1,-1}

	for ;t > 0; t-- {

		scanner.Scan()
		tipo := scanner.Text()

		if tipo == "+" {

			x := ReadInt()
			y := ReadInt()

			if x > maiorX.x {
				maiorX = point{x,y}
			} else if x == maiorX.x {
				maiorX = point{x, max(y, maiorX.y)}
			}

			if y > maiorY.y {
				maiorY = point{x,y}
			} else if y == maiorY.y {
				maiorY = point{max(x, maiorY.x), y}
			}

		} else {

			x := ReadInt()
			y := ReadInt()

			acc := 0

			if x >= maiorX.x && y >= maiorX.y {
				acc++
			} else if y >= maiorX.x && x >= maiorX.y {
				acc++
			}

			if x >= maiorY.x && y >= maiorY.y {
				acc++
			} else if y >= maiorY.x && x >= maiorY.y {
				acc++
			}

			if acc == 2 {
				writer.WriteString("YES\n")
			} else {
				writer.WriteString("NO\n")
			}

		}
	}
}


You are given an array $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$.In one operation you can choose two elements of the array and replace them with the element equal to their sum (it does not matter where you insert the new element). For example, from the array $$$[2, 1, 4]$$$ you can obtain the following arrays: $$$[3, 4]$$$, $$$[1, 6]$$$ and $$$[2, 5]$$$.Your task is to find the maximum possible number of elements divisible by $$$3$$$ that are in the array after performing this operation an arbitrary (possibly, zero) number of times.You have to answer $$$t$$$ independent queries.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \le n \le 100$$$). The second line of each query contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$).

For each query print one integer in a single line — the maximum possible number of elements divisible by $$$3$$$ that are in the array after performing described operation an arbitrary (possibly, zero) number of times.

NoteIn the first query of the example you can apply the following sequence of operations to obtain $$$3$$$ elements divisible by $$$3$$$: $$$[3, 1, 2, 3, 1] \rightarrow [3, 3, 3, 1]$$$.In the second query you can obtain $$$3$$$ elements divisible by $$$3$$$ with the following sequence of operations: $$$[1, 1, 1, 1, 1, 2, 2] \rightarrow [1, 1, 1, 1, 2, 3] \rightarrow [1, 1, 1, 3, 3] \rightarrow [2, 1, 3, 3] \rightarrow [3, 3, 3]$$$.

package main

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

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

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	t := readInt(scanner)
	for i := 0; i < t; i++ {
		n := readInt(scanner)
		d := [3]int{0, 0, 0}

		for j := 0; j < n; j++ {
			d[readInt(scanner) % 3] += 1
		}

		ans := d[0]
		m := min(d[1], d[2])
		ans += m
		d[1] -= m
		d[2] -= m
		ans += d[1] / 3
		ans += d[2] / 3

		fmt.Println(ans)
	}
}

package main

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

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()

	q, _ := strconv.Atoi(scanner.Text())

	for q > 0 {
		scanner.Scan()
		larr, _ := strconv.Atoi(scanner.Text())
		arr1 := 0
		arr2 := 0
		r := 0
		scanner.Scan()
		in := strings.Split(scanner.Text(), " ")
		count := 0
		for count < larr {
			a, _ := strconv.Atoi(in[count])

			switch a % 3 {
			case 1:
				arr1++
			case 2:
				arr2++
			case 0:
				r++
			}

			count++
		}

		diff := abs(arr1 - arr2)
		r += diff / 3
		r += int(math.Min(float64(arr1), float64(arr2)))
		fmt.Println(r)
		q--
	}
}

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

package main

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

func Solve(n int, a []int64) int {
	var remainders [3]int
	for i := 0; i < n; i++ {
		remainders[a[i] % 3] += 1
	}
	if remainders[1] > remainders[2] {
		return remainders[0] + remainders[2] + (remainders[1] - remainders[2])/3
	} else if remainders[2] > remainders[1] {
		return remainders[0] + remainders[1] + (remainders[2] - remainders[1])/3
	}
	return remainders[0] + remainders[1]
}

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

	var t int
	var n int

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

package main

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

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()

	q, _ := strconv.Atoi(scanner.Text())

	for q > 0 {
		scanner.Scan()
		larr, _ := strconv.Atoi(scanner.Text())
		arr1 := 0
		arr2 := 0
		r := 0
		scanner.Scan()
		in := strings.Split(scanner.Text(), " ")
		count := 0
		for count < larr {
			a, _ := strconv.Atoi(in[count])

			switch a % 3 {
			case 1:
				arr1++
			case 2:
				arr2++
			case 0:
				r++
			}

			count++
		}

		diff := abs(arr1 - arr2)
		r += diff / 3
		r += int(math.Min(float64(arr1), float64(arr2)))
		println(r)
		q--
	}
}


This is the hard version of the problem. The difference in the constraints between both versions are colored below in red. You can make hacks only if all versions of the problem are solved.Marin and Gojou are playing hide-and-seek with an array.Gojou initially perform the following steps:   First, Gojou chooses $$$2$$$ integers $$$l$$$ and $$$r$$$ such that $$$l \leq r$$$.  Then, Gojou will make an array $$$a$$$ of length $$$r-l+1$$$ which is a permutation of the array $$$[l,l+1,\ldots,r]$$$.  Finally, Gojou chooses a secret integer $$$x$$$ and sets $$$a_i$$$ to $$$a_i \oplus x$$$ for all $$$i$$$ (where $$$\oplus$$$ denotes the bitwise XOR operation). Marin is then given the values of $$$l,r$$$ and the final array $$$a$$$. She needs to find the secret integer $$$x$$$ to win. Can you help her?Note that there may be multiple possible $$$x$$$ that Gojou could have chosen. Marin can find any possible $$$x$$$ that could have resulted in the final value of $$$a$$$.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) — the number of test cases. In the first line of each test case contains two integers $$$l$$$ and $$$r$$$ ($$$\color{red}{\boldsymbol{0} \boldsymbol{\le} \boldsymbol{l}} \le r < 2^{17}$$$). The second line contains $$$r - l + 1$$$ space-seperated integers of $$$a_1,a_2,\ldots,a_{r-l+1}$$$ ($$$0 \le a_i < 2^{17}$$$). It is guaranteed that array $$$a$$$ is valid. It is guaranteed that the sum of $$$r - l + 1$$$ over all test cases does not exceed $$$2^{17}$$$.

For each test case print an integer $$$x$$$. If there are multiple answers, print any.

NoteIn the first test case, the original array is $$$[7, 6, 5, 4]$$$. In the second test case, the original array is $$$[4, 7, 6, 5]$$$.In the third test case, the original array is $$$[3, 1, 2]$$$.

package main

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

const (
	INF = 3e5 + 10
)

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

	var n int
	Fscan(in, &n)

	for i := 0; i < n; i++ {
		var l, r int64
		Fscan(in, &l, &r)

		a := make([]int64, r-l+1)
		aMap := make(map[int64]bool)
		for j := l; j <= r; j++ {
			Fscan(in, &a[j-l])
			aMap[a[j-l]] = true

		}
		ans := solver(&aMap, l, r)

		Fprintln(out, ans)
	}

}

func solver(a *map[int64]bool, l int64, r int64) int64 {
	if l%2 == 0 && r%2 == 1 {
		tmpMap := make(map[int64]bool)
		for key := range *a {
			tmpMap[key>>1] = true
		}
		return solver(&tmpMap, l>>1, r>>1) << 1
	} else {
		var ans int64
		if l%2 == 0 {
			ans = r
		} else {
			ans = l
		}
		for v := range *a {
			if !(*a)[v^1] {
				cur := v ^ ans
				f := true
				for j := range *a {
					f = f && ((cur^j) >= l && (cur^j) <= r)
				}
				if f {
					ans = cur
					break
				}
			}
		}
		return ans
	}

}

package main

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

const (
	INF = 3e5 + 10
)

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

	var n int
	Fscan(in, &n)

	for i := 0; i < n; i++ {
		var l, r int64
		Fscan(in, &l, &r)

		a := make([]int64, r-l+1)
		aMap := make(map[int64]bool)
		for j := l; j <= r; j++ {
			Fscan(in, &a[j-l])
			aMap[a[j-l]] = true

		}
		ans := solver(&aMap, l, r)

		Fprintln(out, ans)
	}

}

func solver(a *map[int64]bool, l int64, r int64) int64 {
	if l%2 == 0 && r%2 == 1 {
		tmpMap := make(map[int64]bool)
		for key := range *a {
			tmpMap[key>>1] = true
		}
		return solver(&tmpMap, l>>1, r>>1) << 1
	} else {
		var ans int64
		if l%2 == 0 {
			ans = r
		} else {
			ans = l
		}
		for v := range *a {
			cur := v ^ ans
			if !(*a)[cur] {
				f := true
				for j := range *a {
					f = f && ((cur^j) >= l && (cur^j) <= r)
				}
				if f {
					ans = cur
					break
				}
			}
		}
		return ans
	}

}

package main

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

const (
	INF = 3e5 + 10
)

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

	var n int
	Fscan(in, &n)

	for i := 0; i < n; i++ {
		var l, r int64
		Fscan(in, &l, &r)

		a := make([]int64, r-l+1)
		aMap := make(map[int64]bool)
		for j := l; j <= r; j++ {
			Fscan(in, &a[j-l])
			aMap[a[j-l]] = true

		}
		ans := solver(&aMap, l, r)

		Fprintln(out, ans)
	}

}

func solver(a *map[int64]bool, l int64, r int64) int64 {
	if l%2 == 0 && r%2 == 1 {
		tmpMap := make(map[int64]bool)
		for key := range *a {
			tmpMap[key>>1] = true
		}
		return solver(&tmpMap, l>>1, r>>1) << 1
	} else {
		var ans int64
		if l%2 == 0 {
			ans = r
		} else {
			ans = l
		}
		for v := range *a {
			cur := v ^ int64(1)
			if !(*a)[cur] {
				f := true
				for j := range *a {
					f = f && ((cur^j) >= l && (cur^j) <= r)
				}
				if f {
					ans = cur
					break
				}
			}
		}
		return ans
	}

}

package main

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

const (
	INF = 3e5 + 10
)

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

	var n int
	Fscan(in, &n)

	for i := 0; i < n; i++ {
		var l, r int64
		Fscan(in, &l, &r)

		a := make([]int64, r-l+1)
		aMap := make(map[int64]bool)
		for j := l; j <= r; j++ {
			Fscan(in, &a[j-l])
			aMap[a[j-l]] = true

		}
		ans := solver(&a, &aMap, l, r)

		Fprintln(out, ans)
	}

}

func solver(origin *[]int64, a *map[int64]bool, l int64, r int64) int64 {
	if l%2 == 0 && r%2 == 1 {
		tmpMap := make(map[int64]bool)
		for key := range *a {
			tmpMap[key>>1] = true
		}
		return solver(origin, &tmpMap, l>>1, r>>1) << 1
	} else {
		var ans int64
		if l%2 == 0 {
			ans = r
		} else {
			ans = l
		}
		for v := range *a {
			cur := v ^ int64(1)
			if !(*a)[cur] {
				f := true
				for _, j := range *origin {
					f = f && ((cur^j) >= l && (cur^j) <= r)
				}
				if f {
					ans = cur
					break
				}
			}
		}
		return ans
	}

}

package main

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

const (
	INF = 3e5 + 10
)

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

	var n int
	Fscan(in, &n)

	for i := 0; i < n; i++ {
		var l, r int64
		Fscan(in, &l, &r)

		a := make([]int64, r-l+1)
		aMap := make(map[int64]bool)
		for j := l; j <= r; j++ {
			Fscan(in, &a[j-l])
			aMap[a[j-l]] = true

		}
		ans := solver(&a, &aMap, l, r)

		Fprintln(out, ans)
	}

}

func solver(origin *[]int64, a *map[int64]bool, l int64, r int64) int64 {
	if l%2 == 0 && r%2 == 1 {
		tmpMap := make(map[int64]bool)
		for key := range *a {
			tmpMap[key>>1] = true
		}
		return solver(origin, &tmpMap, l>>1, r>>1) << 1
	} else {
		ans := int64(0)
		if (r-l+1)%2 == 1 {
			for i := l; i <= r; i++ {
				ans ^= i
			}
			for i := range *a {
				ans ^= i
			}
		} else {
			for v := range *a {
				cur := v ^ int64(1)
				if !(*a)[cur] {
					f := true
					for _, j := range *origin {
						f = f && ((cur^j) >= l && (cur^j) <= r)
					}
					if f {
						ans = cur
						break
					}
				}
			}
		}
		return ans
	}

}

package main

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

const (
	INF = 3e5 + 10
)

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

	var n int
	Fscan(in, &n)

	for i := 0; i < n; i++ {
		var l, r int64
		Fscan(in, &l, &r)

		a := make([]int64, r-l+1)
		aMap := make(map[int64]bool)
		for j := l; j <= r; j++ {
			Fscan(in, &a[j-l])
			aMap[a[j-l]] = true

		}
		ans := solver(&aMap, l, r)

		Fprintln(out, ans)
	}

}

func solver(a *map[int64]bool, l int64, r int64) int64 {
	if (r-l+1)%2 == 0 && r != 1 {
		tmpMap := make(map[int64]bool)
		for key := range *a {
			tmpMap[key>>1] = true
		}
		return (solver(&tmpMap, l>>1, r>>1) << 1) + 1
	} else {
		ans := int64(0)
		for i := l; i <= r; i++ {
			ans ^= i
		}
		for v := range *a {
			ans ^= v
		}
		return ans
	}

}

package main

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

const (
	INF = 3e5 + 10
)

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

	var n int
	Fscan(in, &n)

	for i := 0; i < n; i++ {
		var l, r int64
		Fscan(in, &l, &r)

		a := make([]int64, r-l+1)
		aMap := make(map[int64]bool)
		for j := l; j <= r; j++ {
			Fscan(in, &a[j-l])
			aMap[a[j-l]] = true

		}
		ans := solver(&aMap, l, r)

		Fprintln(out, ans)
	}

}

func solver(a *map[int64]bool, l int64, r int64) int64 {
	if (r-l+1)%2 == 0 {
		tmpMap := make(map[int64]bool)
		for key := range *a {
			tmpMap[key>>1] = true
		}
		return (solver(&tmpMap, l>>1, r>>1) << 1) + 1
	} else {
		ans := int64(0)
		for i := l; i <= r; i++ {
			ans ^= i
		}
		for v := range *a {
			ans ^= v
		}
		return ans
	}

}

package main

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

const (
	INF = 3e5 + 10
)

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

	var n int
	Fscan(in, &n)

	for i := 0; i < n; i++ {
		var l, r int64
		Fscan(in, &l, &r)

		a := make([]int64, r-l+1)
		aMap := make(map[int64]bool)
		for j := l; j <= r; j++ {
			Fscan(in, &a[j-l])
			aMap[a[j-l]] = true

		}
		ans := solver(&aMap, l, r)

		if ans == 0 {
			ans = a[0]
		}
		Fprintln(out, ans)
	}

}

func solver(a *map[int64]bool, l int64, r int64) int64 {
	if (r-l+1)%2 == 0 {
		tmpMap := make(map[int64]bool)
		for key := range *a {
			tmpMap[key>>1] = true
		}
		return solver(&tmpMap, l>>1, r>>1) << 1
	} else {
		ans := int64(0)
		for i := l; i <= r; i++ {
			ans ^= i
		}
		for v := range *a {
			ans ^= v
		}
		return ans
	}

}

package main

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

const (
	INF = 3e5 + 10
)

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

	var n int
	Fscan(in, &n)

	for i := 0; i < n; i++ {
		var l, r int64
		Fscan(in, &l, &r)

		a := make([]int64, r-l+1)
		aMap := make(map[int64]bool)
		for j := l; j <= r; j++ {
			Fscan(in, &a[j-l])
			aMap[a[j-l]] = true

		}

		Fprintln(out, solver(&aMap, l, r))
	}

}

func solver(a *map[int64]bool, l int64, r int64) int64 {
	if (r-l+1)%2 == 0 {
		tmpMap := make(map[int64]bool)
		for key := range *a {
			tmpMap[key>>1] = true
		}
		return solver(&tmpMap, l>>1, r>>1) << 1
	} else {
		ans := int64(0)
		for i := l; i <= r; i++ {
			ans ^= i
		}
		for v := range *a {
			ans ^= v
		}
		return ans
	}

}


You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right. Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height hi of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it. Finally you ended up with the following conditions to building the castle:  h1 ≤ H: no sand from the leftmost spot should go over the fence;  For any  |hi - hi + 1| ≤ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen;  : you want to spend all the sand you brought with you. As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible. Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.

The only line contains two integer numbers n and H (1 ≤ n, H ≤ 1018) — the number of sand packs you have and the height of the fence, respectively.

Print the minimum number of spots you can occupy so the all the castle building conditions hold.

NoteHere are the heights of some valid castles:   n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...]  n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied) The first list for both cases is the optimal answer, 3 spots are occupied in them.And here are some invalid ones:  n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...]  n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]

package main

import "fmt"

const inf int64 = 1e10

var (
    n, h int64
)

func sum(x int64) int64 {
    sum := (1 + x) * x / 2
    if x > h {
        sum += (h + x - 1) * (x - h) / 2
    }
    return sum
}

func main() {
    fmt.Scan(&n, &h)
    var l, r, mid int64 = 1, inf, 0
    for l < r {
        mid = (l + r) >> 1
        if sum(mid) >= n { r = mid } else { l = mid + 1 }
    }
    if l <= h {
        fmt.Println(l)
    } else {
        if sum(l - 1) + l - 1 >= n {
            fmt.Println(l - 1 + l - h)
        } else {
            fmt.Println(l + l - h)
        }
    }
}


The Olympic Games have just started and Federico is eager to watch the marathon race.There will be $$$n$$$ athletes, numbered from $$$1$$$ to $$$n$$$, competing in the marathon, and all of them have taken part in $$$5$$$ important marathons, numbered from $$$1$$$ to $$$5$$$, in the past. For each $$$1\le i\le n$$$ and $$$1\le j\le 5$$$, Federico remembers that athlete $$$i$$$ ranked $$$r_{i,j}$$$-th in marathon $$$j$$$ (e.g., $$$r_{2,4}=3$$$ means that athlete $$$2$$$ was third in marathon $$$4$$$).Federico considers athlete $$$x$$$ superior to athlete $$$y$$$ if athlete $$$x$$$ ranked better than athlete $$$y$$$ in at least $$$3$$$ past marathons, i.e., $$$r_{x,j}<r_{y,j}$$$ for at least $$$3$$$ distinct values of $$$j$$$.Federico believes that an athlete is likely to get the gold medal at the Olympics if he is superior to all other athletes.Find any athlete who is likely to get the gold medal (that is, an athlete who is superior to all other athletes), or determine that there is no such athlete.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1\le n\le 50\,000$$$) — the number of athletes. Then $$$n$$$ lines follow, each describing the ranking positions of one athlete. The $$$i$$$-th of these lines contains the $$$5$$$ integers $$$r_{i,1},\,r_{i,2},\,r_{i,3},\,r_{i,4},\, r_{i,5}$$$ ($$$1\le r_{i,j}\le 50\,000$$$) — the ranking positions of athlete $$$i$$$ in the past $$$5$$$ marathons. It is guaranteed that, in each of the $$$5$$$ past marathons, the $$$n$$$ athletes have distinct ranking positions, i.e., for each $$$1\le j\le 5$$$, the $$$n$$$ values $$$r_{1,j},\, r_{2, j},\, \dots,\, r_{n, j}$$$ are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$50\,000$$$.

For each test case, print a single integer — the number of an athlete who is likely to get the gold medal (that is, an athlete who is superior to all other athletes). If there are no such athletes, print $$$-1$$$. If there is more than such one athlete, print any of them.

NoteExplanation of the first test case: There is only one athlete, therefore he is superior to everyone else (since there is no one else), and thus he is likely to get the gold medal.Explanation of the second test case: There are $$$n=3$$$ athletes.   Athlete $$$1$$$ is superior to athlete $$$2$$$. Indeed athlete $$$1$$$ ranks better than athlete $$$2$$$ in the marathons $$$1$$$, $$$2$$$ and $$$3$$$.  Athlete $$$2$$$ is superior to athlete $$$3$$$. Indeed athlete $$$2$$$ ranks better than athlete $$$3$$$ in the marathons $$$1$$$, $$$2$$$, $$$4$$$ and $$$5$$$.  Athlete $$$3$$$ is superior to athlete $$$1$$$. Indeed athlete $$$3$$$ ranks better than athlete $$$1$$$ in the marathons $$$3$$$, $$$4$$$ and $$$5$$$. Explanation of the third test case: There are $$$n=3$$$ athletes.   Athlete $$$1$$$ is superior to athletes $$$2$$$ and $$$3$$$. Since he is superior to all other athletes, he is likely to get the gold medal.  Athlete $$$2$$$ is superior to athlete $$$3$$$.  Athlete $$$3$$$ is not superior to any other athlete. Explanation of the fourth test case: There are $$$n=6$$$ athletes.   Athlete $$$1$$$ is superior to athletes $$$3$$$, $$$4$$$, $$$6$$$.  Athlete $$$2$$$ is superior to athletes $$$1$$$, $$$4$$$, $$$6$$$.  Athlete $$$3$$$ is superior to athletes $$$2$$$, $$$4$$$, $$$6$$$.  Athlete $$$4$$$ is not superior to any other athlete.  Athlete $$$5$$$ is superior to athletes $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$6$$$. Since he is superior to all other athletes, he is likely to get the gold medal.  Athlete $$$6$$$ is only superior to athlete $$$4$$$.

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var test int
	fmt.Fscan(in, &test)
	for ; test > 0; test-- {
		var n int
		fmt.Fscan(in, &n)

		ar := make([][]int, n)
		for i := range ar {
			ar[i] = make([]int, 5)
			for j := range ar[i] {
				fmt.Fscan(in, &ar[i][j])
			}
		}

		if n == 1 {
			fmt.Fprintln(out, 1)
			continue
		}

		w := 0
		for i := range ar {
			if better(i, w, ar) {
				w = i
			}
		}

		isSups := true
		for i := range ar {
			if i != w {
				if better(i, w, ar) {
					isSups = false
				}
			}
		}

		if isSups {
			fmt.Fprintln(out, w+1)
		} else {
			fmt.Fprintln(out, -1)
		}
	}
}

func better(i, j int, ar [][]int) bool {
	t := 0
	for k := 0; k < 5; k++ {
		if ar[i][k] < ar[j][k] {
			t++
		}
	}
	return t >= 3
}

package main

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

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

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

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

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

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

func _string() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := _int(); t > 0; t-- {
		n := _int()
		r := make([][5]int, n)
		for i := range r {
			for j := range r[i] {
				r[i][j] = _int()
			}
		}
		printf("%d\n", sol(n, r))
	}
}

func sol(n int, r [][5]int) int {
	skipCandidate := make([]bool, n)
	for i := range r {
		if skipCandidate[i] {
			continue
		}
		for offset := 1; offset < n; offset++ {
			j2 := (i + offset) % n
			sup := superior(r[i], r[j2])
			if sup == -1 {
				skipCandidate[i] = true
				skipCandidate[j2] = true
				break
			}
			if sup == 0 {
				skipCandidate[j2] = true
			}
			if sup == 1 {
				skipCandidate[i] = true
				break
			}
		}
		if !skipCandidate[i] {
			return i + 1
		}
	}
	for i := range skipCandidate {
		if skipCandidate[i] == false {
			return i + 1
		}
	}
	return -1
}

// -1 = none superior
// 0 = a superior
// 1 = b superior
func superior(a, b [5]int) int {
	as := 0
	bs := 0
	for i := range a {
		if a[i] < b[i] {
			as++
		}
		if b[i] < a[i] {
			bs++
		}
	}
	if as >= 3 {
		return 0
	}
	if bs >= 3 {
		return 1
	}
	return -1
}

package main

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

func superior(a, b []int) bool {
	c := 0
	for i := 0; i < 5; i++ {
		if a[i] < b[i] {
			c++
		}
	}
	return c >= 3
}

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

	var T int

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

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

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

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

		skip := make([]bool, n)
		found := false
		for i := 0; i < n; i++ {
			if !skip[i] {
				for j := i + 1; j < n; j++ {
					if superior(arr[i], arr[j]) {
						skip[j] = true
					} else {
						skip[i] = true
						break
					}
				}

				if !skip[i] {
					j := 0
					for ; j < i; j++ {
						if !superior(arr[i], arr[j]) {
							break
						}
					}

					if j == i {
						found = true
						fmt.Println(i + 1)
						break
					}
				}
			}
		}

		if !found {
			fmt.Println(-1)
		}
	}
}

package main

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

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

func main() {
	var test int
	fmt.Fscan(in, &test)
	for i := 0; i < test; i++ {
		solve()
	}

}

func solve() {
	var n int
	fmt.Fscan(in, &n)
	var matrix = make([][]int, n)
	for i := 0; i < n; i++ {
		matrix[i] = make([]int, 5)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < 5; j++ {
			var temp int
			fmt.Fscan(in, &temp)
			matrix[i][j] = temp
		}
	}
	winner := 0
	for i := 1; i < n; i++ {
		maxBetter := 0
		for j := 0; j < 5; j++ {
			if matrix[i][j] < matrix[winner][j] {
				maxBetter++
			}
			if maxBetter >= 3 {
				winner = i
				break
			}
		}
	}
	for i := 0; i < n; i++ {
		if i == winner {
			continue
		}
		maxBetter := 0
		for j := 0; j < 5; j++ {
			if matrix[i][j] < matrix[winner][j] {
				maxBetter++
			}
			if maxBetter >= 3 {
				fmt.Println(-1)
				return
			}
		}
	}
	fmt.Println(winner + 1)

}

func printArray(arr []int) {
	for i := 1; i < len(arr); i++ {
		fmt.Print(arr[i], " ")
	}
	fmt.Println()
}

type runeSort []rune

func (r runeSort) Len() int           { return len(r) }
func (r runeSort) Swap(i, j int)      { r[i], r[j] = r[j], r[i] }
func (r runeSort) Less(i, j int) bool { return r[i] < r[j] }

package main

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

type Scanner struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) *Scanner {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	return &Scanner{scanner}
}

func (s *Scanner) Word() string {
	s.Scan()
	return s.Text()
}

func (s *Scanner) Int() int {
	s.Scan()
	n, _ := strconv.Atoi(s.Text())
	return n
}

func (s *Scanner) Int64() int64 {
	s.Scan()
	n, _ := strconv.ParseInt(s.Text(), 10, 64)
	return n
}

func (s *Scanner) Words(count int) []string {
	words := make([]string, count)
	for i := range words {
		words[i] = s.Word()
	}
	return words
}

func (s *Scanner) Ints(count int) []int {
	ints := make([]int, count)
	for i := range ints {
		ints[i] = s.Int()
	}
	return ints
}

func (s *Scanner) Int64s(count int) []int64 {
	int64s := make([]int64, count)
	for i := range int64s {
		int64s[i] = s.Int64()
	}
	return int64s
}

type athlete []int

func (a athlete) superior(o athlete) bool {
	better := 0
	for i := range a {
		if a[i] < o[i] {
			better += 1
		}
	}
	return better >= 3
}

func canWin(bestPos int, a []athlete) bool {
	for i := range a {
		if i != bestPos && !a[bestPos].superior(a[i]) {
			return false
		}
	}
	return true
}

func solve(a []athlete) (int, bool) {
	bestPos := 0
	for i := range a {
		if a[i].superior(a[bestPos]) {
			bestPos = i
		}
	}

	if canWin(bestPos, a) {
		return bestPos, true
	}
	return -1, false
}

func main() {
	stdin := NewWordScanner(os.Stdin)
	tests := stdin.Int()

	for i := 0; i < tests; i += 1 {
		n := stdin.Int()
		a := make([]athlete, n)
		for j := range a {
			a[j] = stdin.Ints(5)
		}

		pos, found := solve(a)
		if found {
			fmt.Println(pos + 1)
		} else {
			fmt.Println(-1)
		}
	}
}