You are given a string $$$s$$$ consisting of characters 0 and/or 1.You have to remove several (possibly zero) characters from the beginning of the string, and then several (possibly zero) characters from the end of the string. The string may become empty after the removals. The cost of the removal is the maximum of the following two values:  the number of characters 0 left in the string;  the number of characters 1 removed from the string. What is the minimum cost of removal you can achieve?

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 2 \cdot 10^5$$$), consisting of characters 0 and/or 1. The total length of strings $$$s$$$ in all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print one integer — the minimum cost of removal you can achieve.

NoteConsider the test cases of the example:  in the first test case, it's possible to remove two characters from the beginning and one character from the end. Only one 1 is deleted, only one 0 remains, so the cost is $$$1$$$;  in the second test case, it's possible to remove three characters from the beginning and six characters from the end. Two characters 0 remain, three characters 1 are deleted, so the cost is $$$3$$$;  in the third test case, it's optimal to remove four characters from the beginning;  in the fourth test case, it's optimal to remove the whole string;  in the fifth test case, it's optimal to leave the string as it is.

package main

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

type Int int64

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

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var s string
		Fscan(in, &s)

		pre := make([]int, len(s)+1)

		f := make([]int, 0, len(s))

		for i, ch := range s {
			pre[i+1] = pre[i]
			if ch == '0' {
				pre[i+1] += 1
			} else {
				f = append(f, pre[i+1])
			}
		}

		l := 0
		r := len(f) - 1

		for l <= r {
			tmpAns := (l + r) >> 1
			flag := false
			for i := 0; i <= tmpAns; i++ {
				if f[len(f)-1-(tmpAns-i)]-f[i] <= tmpAns {
					flag = true
				}
			}
			if flag {
				r = tmpAns - 1
			} else {
				l = tmpAns + 1
			}
		}

		Fprintln(out, r+1)

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

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

func main_edu_128_C() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var s string
		Fscan(in, &s)
		var n = len(s)
		var wins, c1 int
		for i := 0; i < n; i++ {
			if s[i] == '1' {
				wins++
			}
		}
		var ans = int(1e9)
		for i, j := 0, 0; i < n; i++ {
			if s[i] == '1' {
				c1++
			}
			for i-j+1 > wins {
				if s[j] == '1' {
					c1--
				}
				j++
			}
			if i-j+1 == wins {
				if wins-c1 < ans {
					ans = wins - c1
				}
			}
		}
		Fprintln(out, ans)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() {
	main_edu_128_C()
}

package main

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

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

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var niter int
	fmt.Fscan(in, &niter)
	for iii := 0; iii < niter; iii++ {
		var s string
		fmt.Fscan(in, &s)
		b := []byte(s)
		n := len(s)
		del1l := make([]int, n+1)
		del0l := make([]int, n+1)
		del1r := make([]int, n+1)
		del0r := make([]int, n+1)
		c0, c1 := 0, 0
		for i := 0; i < n; i++ {
			if b[i] == '1' {
				c1++
			} else {
				c0++
			}
			del0l[i+1] = c0
			del1l[i+1] = c1
		}
		c0, c1 = 0, 0
		for i := n - 1; i >= 0; i-- {
			if b[i] == '1' {
				c1++
			} else {
				c0++
			}
			del0r[n-i] = c0
			del1r[n-i] = c1
		}
		cs := del0r[n] // cs
		//bs := make([]int, n+1)
		m := 999999
		for i := 0; i <= n; i++ {
			c1 := del1l[i]
			c2 := del0l[i]
			j := cs - c1 - c2
			if i+j <= n && j >= 0 {
				m = min(m, max(del1l[i]+del1r[j], cs-del0l[i]-del0r[j]))
			}
			// if j >= 0 {
			// 	fmt.Println("!!!!", i, j, cs, c1, c2)
			// 	fmt.Println("!!!!", del1l[i]+del1r[j], cs-del0l[i]-del0r[j])
			// 	fmt.Println("!!", del0l, del0r, del1l, del1r)
			// }
		}
		fmt.Fprintln(out, m)
	}
}

package main

import "fmt"

func main() {
	var test int
	fmt.Scanln(&test)

	for test > 0 {
		test--

		var str string
		fmt.Scanln(&str)

		fmt.Println(solve(str))
	}
}

func solve(str string) int {
	ones := []int{}
	for i, ch := range str {
		if ch == '1' {
			ones = append(ones, i)
		}
	}

	res := len(ones)
	l := 0
	r := len(ones)
	for l <= r {
		m := l + (r-l)/2
		if possible(&ones, m) {
			res = m
			r = m - 1
		} else {
			l = m + 1
		}
	}

	return res
}

func possible(ones *[]int, k int) bool {
	if len(*ones) == 0 {
		return true
	}

	oneLeft := len(*ones) - k
	for i := 0; i < len(*ones)-oneLeft+1; i++ {
		zeroInRange := (*ones)[i+oneLeft-1] - (*ones)[i] - oneLeft + 1
		if zeroInRange <= k {
			return true
		}
	}
	return false
}

package main

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

var s string
var n int
var zeroes int
var ones int

func f(budget int) bool {
	r := 0
	ones_eaten := ones
	zeroes_left := 0
	left_ones_eaten := 0
	for l := -1; l < n; l++ {
		if l != -1 {
			if s[l] == '1' {
				ones_eaten++
				left_ones_eaten++
			} else {
				zeroes_left--
			}
		}
		if left_ones_eaten > budget {
			break
		}
		for r < n && (r <= l || ones_eaten > budget) {
			if s[r] == '1' {
				ones_eaten--
			} else {
				zeroes_left++
			}
			r++
		}
		if zeroes_left <= budget {
			// fmt.Println(ones_eaten, budget)
			return true
		}
	}
	return false
}

func main() {
	var reader *bufio.Reader = bufio.NewReader(os.Stdin)
	var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var T int
	fmt.Scan(&T)
	for t := 0; t < T; t++ {
		fmt.Fscan(reader, &s)
		n = len(s)
		zeroes = 0
		for i := 0; i < n; i++ {
			if s[i] == '0' {
				zeroes++
			}
		}
		ones = n - zeroes
		// ahhhhh can't figure out how to do it in O(n) so let's do O(n log n) w/ binary search?
		// or w/ a log(n) structure like in LCS
		// don't want to do that so let's do binary search
		l := -1
		r := ones
		// fmt.Println(l, r)
		for r-l > 1 {
			m := (l + r + 1) / 2
			can := f(m)
			if can {
				r = m
			} else {
				l = m
			}
		}
		fmt.Fprintln(writer, r)
	}
}

package main

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

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

	var T int
	var s string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &s)
		n := len(s)
		s1 := make([]int, n+1)
		for i, b := range s {
			s1[i+1] = s1[i] + int(b&1)
		}
		Fprintln(out, sort.Search(n, func(mx int) bool {
			c, l := 0, 0
			for r, b := range s {
				c += int(b&1 ^ 1)
				for c > mx {
					c -= int(s[l]&1 ^ 1)
					l++
				}
				if s1[n]-(s1[r+1]-s1[l]) <= mx {
					return true
				}
			}
			return false
		}))
	}
}

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

package main

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

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 s string
		fmt.Fscan(in, &s)

		s = strings.Trim(s, "0")
		var k0, k1 int
		for _, ch := range s {
			if ch == '1' {
				k1++
			}
		}

		tot := k1
		for i, j := 0, 0; i < len(s); i++ {
			for j < len(s) && (k0 < k1 || j < i) {
				if s[j] == '1' {
					k1--
				} else {
					k0++
				}
				j++
			}

			tot = min(tot, max(k0, k1))
			if j > i {
				if s[j-1] == '0' {
					tot = min(tot, max(k0-1, k1))
				} else {
					tot = min(tot, max(k0, k1+1))
				}
			}
			if s[i] == '1' {
				k1++
			} else {
				k0--
			}
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

func acc(op func(int64, int64) int64, c ...int64) int64 {
	res := c[0]
	for _, i := range c {
		res = op(res, i)
	}
	return res
}

func max(c ...int64) int64 {
	return acc(func(a int64, b int64) int64 {
		if a < b {
			return b
		}
		return a
	}, c...)
}

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

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

func solve(inout io.ReadWriter) {
	scan := func(inp ...interface{}) {
		fmt.Fscan(inout, inp...)
	}
	printf := func(format string, outp ...interface{}) {
		fmt.Fprintf(inout, format, outp...)
	}

	var s string
	scan(&s)

	y := 0
	for _, c := range s {
		if c == '1' {
			y++
		}
	}

	a := 0
	for i := 0; i < y; i++ {
		if s[i] == '0' {
			a++
		}
	}
	ans := a
	for i := y; i < len(s); i++ {
		if s[i] == '0' {
			a++
		}
		if s[i-y] == '0' {
			a--
		}
		if a < ans {
			ans = a
		}
	}
	printf("%d\n", ans)

}

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

	var T int = 1
	fmt.Fscan(inout, &T)
	for ; T > 0; T-- {
		solve(inout)
	}
}

package main

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

type Int int64

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

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var s string
		Fscan(in, &s)

		for len(s) > 0 && s[len(s)-1] == '0' {
			s = s[0 : len(s)-1]
		}

		for len(s) > 0 && s[0] == '0' {
			s = s[1:]
		}

		left := L0
		for _, ch := range s {
			if ch == '0' {
				left++
			}
		}
		ans := left

		a := make([]Int, len(s)+10)
		b := make([]Int, len(s)+10)
		c := make([]Int, len(s)+10)

		r := Int(len(s) - 1)
		add := L0
		del := L0
		cnt := L0

		a[cnt] = add
		b[cnt] = del
		c[cnt] = r
		cnt++

		for r >= 0 {
			for r >= 0 && s[r] == '1' {
				add++
				r--
			}
			for r >= 0 && s[r] == '0' {
				del++
				r--
			}
			a[cnt] = add
			b[cnt] = del
			c[cnt] = r
			cnt++
		}

		l := L0
		tmpDel := L0
		r = L0

		for r+1 < cnt && Max(tmpDel+a[r+1], left-b[r+1]) < left {
			r++
		}

		ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

		for l < Int(len(s)) {
			for l < Int(len(s)) && s[l] == '1' {
				l++
				tmpDel++
			}
			for l < Int(len(s)) && s[l] == '0' {
				l++
				left--
			}

			for r > 0 && c[r] < l {
				r--
			}

			for r > 0 && Max(tmpDel+a[r-1], left-b[r-1]) <= Max(tmpDel+a[r], left-b[r]) {
				r--
			}
			ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

			if tmpDel > left {
				break
			}

		}

		Fprintln(out, ans)

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

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

type Int int64

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

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var s string
		Fscan(in, &s)

		for len(s) > 0 && s[len(s)-1] == '0' {
			s = s[0 : len(s)-1]
		}

		for len(s) > 0 && s[0] == '0' {
			s = s[1:]
		}

		left := L0
		for _, ch := range s {
			if ch == '0' {
				left++
			}
		}
		ans := left

		a := make([]Int, len(s)+10)
		b := make([]Int, len(s)+10)
		c := make([]Int, len(s)+10)

		r := Int(len(s) - 1)
		add := L0
		del := L0
		cnt := L0

		a[cnt] = add
		b[cnt] = del
		c[cnt] = r
		cnt++

		for r >= 0 {
			for r >= 0 && s[r] == '1' {
				add++
				r--
			}
			for r >= 0 && s[r] == '0' {
				del++
				r--
			}
			a[cnt] = add
			b[cnt] = del
			c[cnt] = r
			cnt++
		}

		l := L0
		tmpDel := L0
		r = L0

		for r+1 < cnt && tmpDel+a[r+1] <= left-b[r+1] {
			r++
		}

		ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

		for l < Int(len(s)) {
			for l < Int(len(s)) && s[l] == '1' {
				l++
				tmpDel++
			}
			for l < Int(len(s)) && s[l] == '0' {
				l++
				left--
			}

			for r > 0 && c[r] < l {
				r--
			}

			for r > 0 && tmpDel+a[r] > left-b[r] {
				r--
			}
			ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

			if tmpDel > left {
				break
			}

		}

		Fprintln(out, ans)

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

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

type Int int64

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

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var s string
		Fscan(in, &s)

		for len(s) > 0 && s[len(s)-1] == '0' {
			s = s[0 : len(s)-1]
		}

		for len(s) > 0 && s[0] == '0' {
			s = s[1:]
		}

		left := L0
		for _, ch := range s {
			if ch == '0' {
				left++
			}
		}
		ans := left

		a := make([]Int, len(s)+10)
		b := make([]Int, len(s)+10)
		c := make([]Int, len(s)+10)

		r := Int(len(s) - 1)
		add := L0
		del := L0
		cnt := L0

		a[cnt] = add
		b[cnt] = del
		c[cnt] = r
		cnt++

		for r >= 0 {
			for r >= 0 && s[r] == '1' {
				add++
				r--
			}
			for r >= 0 && s[r] == '0' {
				del++
				r--
			}
			a[cnt] = add
			b[cnt] = del
			c[cnt] = r
			cnt++
		}

		l := L0

		tmpDel := L0

		r = L0
		for r+1 < cnt && tmpDel+a[r+1] <= left-b[r] {
			r++
		}

		ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

		for l < Int(len(s)) {
			for l < Int(len(s)) && s[l] == '1' {
				l++
				tmpDel++
			}
			for l < Int(len(s)) && s[l] == '0' {
				l++
				left--
			}

			for r > 0 && c[r] < l {
				r--
			}

			for r > 0 && tmpDel+a[r] > left-b[r] {
				r--
			}
			ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

			if tmpDel > left {
				break
			}

		}

		Fprintln(out, ans)

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

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

type Int int64

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

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var s string
		Fscan(in, &s)

		for len(s) > 0 && s[len(s)-1] == '0' {
			s = s[0 : len(s)-1]
		}

		for len(s) > 0 && s[0] == '0' {
			s = s[1:]
		}

		left := L0
		for _, ch := range s {
			if ch == '0' {
				left++
			}
		}
		ans := left

		a := make([]Int, 0, 100000)
		b := make([]Int, 0, 100000)

		r := Int(len(s) - 1)
		add := L0
		del := L0

		a = append(a, add)
		b = append(b, del)

		for r >= 0 {
			for r >= 0 && s[r] == '1' {
				add++
				r--
			}
			for r >= 0 && s[r] == '0' {
				del++
				r--
			}
			a = append(a, add)
			b = append(b, del)

		}

		l := L0

		tmpDel := L0

		r = L0
		for r+1 < Int(len(a)) && tmpDel+a[r+1] <= left-b[r] {
			r++
		}

		ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

		for l < Int(len(s)) {
			for l < Int(len(s)) && s[l] == '1' {
				l++
				tmpDel++
			}
			for l < Int(len(s)) && s[l] == '0' {
				l++
				left--
			}

			for r > 0 && tmpDel+a[r] > left-b[r] {
				r--
			}
			ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

			if tmpDel > left {
				break
			}

		}

		Fprintln(out, ans)

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

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

type Int int64

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

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var s string
		Fscan(in, &s)

		for len(s) > 0 && s[len(s)-1] == '0' {
			s = s[0 : len(s)-1]
		}

		for len(s) > 0 && s[0] == '0' {
			s = s[1:]
		}

		left := L0
		for _, ch := range s {
			if ch == '0' {
				left++
			}
		}
		ans := left

		a := make([]Int, 0, 100000)
		b := make([]Int, 0, 100000)

		r := Int(len(s) - 1)
		add := L0
		del := L0

		a = append(a, add)
		b = append(b, del)

		for r >= 0 {
			for r >= 0 && s[r] == '1' {
				add++
				r--
			}
			for r >= 0 && s[r] == '0' {
				del++
				r--
			}
			a = append(a, add)
			b = append(b, del)

		}

		l := L0

		tmpDel := L0

		r = L0
		for r+1 < Int(len(a)) && tmpDel+a[r+1] <= left-b[r] {
			r++
		}

		ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

		for l < Int(len(s)) {
			for l < Int(len(s)) && s[l] == '1' {
				l++
				tmpDel++
			}
			for l < Int(len(s)) && s[l] == '0' {
				l++
				left--
			}

			for r > 0 && tmpDel+a[r] > left-b[r] {
				r--
			}
			ans = Min(ans, Max(tmpDel+a[r], left-b[r]))

		}

		Fprintln(out, ans)

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

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

type Int int64

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

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var s string
		Fscan(in, &s)

		for len(s) > 0 && s[len(s)-1] == '0' {
			s = s[0 : len(s)-1]
		}

		for len(s) > 0 && s[0] == '0' {
			s = s[1:]
		}

		left := 0
		for _, ch := range s {
			if ch == '0' {
				left++
			}
		}

		l := 0
		ans1 := 0
		left1 := left
		for l < len(s) {
			if s[l] == '1' {
				if ans1 < left1 {
					ans1++
					l++
				} else {
					break
				}
			}
			for l < len(s) && s[l] == '0' {
				l++
				left1--
			}
		}

		ans2 := 0
		left2 := left
		r := len(s) - 1
		for r >= 0 {
			if s[r] == '1' {
				if ans2 < left2 {
					ans2++
					r--
				} else {
					break
				}
			}
			for r >= 0 && s[r] == '0' {
				r--
				left2--
			}
		}

		Fprintln(out, Min(Max(Int(ans1), Int(left1)), Max(Int(ans2), Int(left2))))

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

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

type Int int64

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

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var s string
		Fscan(in, &s)

		for len(s) > 0 && s[len(s)-1] == '0' {
			s = s[0 : len(s)-1]
		}

		for len(s) > 0 && s[0] == '0' {
			s = s[1:]
		}

		left := 0
		for _, ch := range s {
			if ch == '0' {
				left++
			}
		}

		l := 0
		ans1 := 0
		left1 := left
		for l < len(s) {
			if s[l] == '1' {
				if ans1 < left1 {
					ans1++
					l++
				} else {
					break
				}
			}
			for l < len(s) && s[l] == '0' {
				l++
				left1--
			}
		}

		ans2 := 0
		left2 := left
		r := len(s) - 1
		for r >= 0 {
			if s[r] == '1' {
				if ans1 < left2 {
					ans2++
					r--
				} else {
					break
				}
			}
			for r >= 0 && s[r] == '0' {
				r--
				left2--
			}
		}

		Fprintln(out, Min(Max(Int(ans1), Int(left1)), Max(Int(ans2), Int(left2))))

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

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

type Int int64

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

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

	var T int
	Fscan(in, &T)

	for tt := 0; tt < T; tt++ {
		var s string
		Fscan(in, &s)

		for len(s) > 0 && s[len(s)-1] == '0' {
			s = s[0 : len(s)-1]
		}

		for len(s) > 0 && s[0] == '0' {
			s = s[1:]
		}

		ans := 0

		left := 0
		for _, ch := range s {
			if ch == '0' {
				left++
			}
		}

		l := 0
		for l < len(s) {
			if s[l] == '1' {
				if ans < left {
					ans++
					l++
				} else {
					break
				}
			}
			for l < len(s) && s[l] == '0' {
				l++
				left--
			}
		}
		Fprintln(out, Max(Int(ans), Int(left)))

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

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

func main_edu_128_A() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		/* var l1, l2, r1, r2 int
		Fscan(in, &l1, &r1, &l2, &r2)
		if l1 > l2 {
			l1, r1, l2, r2 = l2, r2, l1, r1
		}
		if l1 == l2 {
			Fprintln(out, l1)
			return
		}
		if r1 < l2 {
			Fprintln(out, l1+l2)
			return
		}
		Fprintln(out, l2) */
		/* var n, m, col int
		col = -1
		Fscan(in, &n, &m)
		a := make([]string, n)
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				if a[i][j] == 'R' {
					col = j
					break
				}
			}
			if col != -1{
				break
			}
		}
		for i := 0; i < n; i++ {
			for j := 0; j < col; j++ {
				if a[i][j] == 'R' {
				//	Fprintln(out, ":", i, j, col)
					Fprintln(out, "NO")
					return
				}
			}
		}
		Fprintln(out, "YES") */
		var s string
		Fscan(in, &s)
		var n = len(s)
		var zero, one int
		for i := 0; i < n; i++ {
			if s[i] == '0' {
				zero++
			}
		}

		/* var max func(a, b int) int
		max = func(a, b int) int {
			if a > b {
				return a
			}
			return b
		} */
		l, r := 0, n-1
		for l <= r && zero > one {
			for l <= r && s[l] == '0' && zero > one {
				l++
				zero--
			}
			for l <= r && s[r] == '0' && zero > one {
				r--
				zero--
			}
			if one >= zero {
				break
			}
			if l <= r {
				L, R := 0, 0
				for i := l + 1; i <= r && s[i] == '0'; i++ {
					L++
				}
				for j := r - 1; j >= l && s[j] == '0'; j-- {
					R++
				}
				if L >= R {
					l++
				} else {
					r--
				}
				one++
			}
		}
		Fprintln(out, zero)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() {
	main_edu_128_A()
}

package main

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

func main_edu_128_A() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		/* var l1, l2, r1, r2 int
		Fscan(in, &l1, &r1, &l2, &r2)
		if l1 > l2 {
			l1, r1, l2, r2 = l2, r2, l1, r1
		}
		if l1 == l2 {
			Fprintln(out, l1)
			return
		}
		if r1 < l2 {
			Fprintln(out, l1+l2)
			return
		}
		Fprintln(out, l2) */
		/* var n, m, col int
		col = -1
		Fscan(in, &n, &m)
		a := make([]string, n)
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				if a[i][j] == 'R' {
					col = j
					break
				}
			}
			if col != -1{
				break
			}
		}
		for i := 0; i < n; i++ {
			for j := 0; j < col; j++ {
				if a[i][j] == 'R' {
				//	Fprintln(out, ":", i, j, col)
					Fprintln(out, "NO")
					return
				}
			}
		}
		Fprintln(out, "YES") */
		var s string
		Fscan(in, &s)
		var n = len(s)
		var zero, one int
		for i := 0; i < n; i++ {
			if s[i] == '0' {
				zero++
			}
		}

		var max func(a, b int) int
		max = func(a, b int) int {
			if a > b {
				return a
			}
			return b
		}
		l, r := 0, n-1
		for l <= r {
			for l <= r && s[l] == '0' && zero > one {
				l++
				zero--
			}
			for l <= r && s[r] == '0' && zero > one {
				r--
				zero--
			}
			if one >= zero {
				break
			}
			if l <= r {
				L, R := 0, 0
				for i := l + 1; s[i] == '0'; i++ {
					L++
				}
				for j := r - 1; s[j] == '0'; j-- {
					R++
				}
				if L >= R {
					l++
				} else {
					r--
				}
				one++
			}
		}
		Fprintln(out, max(zero, one))
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() {
	main_edu_128_A()
}

package main

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

func main_edu_128_A() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		/* var l1, l2, r1, r2 int
		Fscan(in, &l1, &r1, &l2, &r2)
		if l1 > l2 {
			l1, r1, l2, r2 = l2, r2, l1, r1
		}
		if l1 == l2 {
			Fprintln(out, l1)
			return
		}
		if r1 < l2 {
			Fprintln(out, l1+l2)
			return
		}
		Fprintln(out, l2) */
		/* var n, m, col int
		col = -1
		Fscan(in, &n, &m)
		a := make([]string, n)
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
		}
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				if a[i][j] == 'R' {
					col = j
					break
				}
			}
			if col != -1{
				break
			}
		}
		for i := 0; i < n; i++ {
			for j := 0; j < col; j++ {
				if a[i][j] == 'R' {
				//	Fprintln(out, ":", i, j, col)
					Fprintln(out, "NO")
					return
				}
			}
		}
		Fprintln(out, "YES") */
		var s string
		Fscan(in, &s)
		var n = len(s)
		var zero, one int
		for i := 0; i < n; i++ {
			if s[i] == '0' {
				zero++
			}
		}

		var max func(a, b int) int
		max = func(a, b int) int {
			if a > b {
				return a
			}
			return b
		}
		l, r := 0, n-1
		for l <= r {
			for l <= r && s[l] == '0' && zero > one {
				l++
				zero--
			}
			for l <= r && s[r] == '0' && zero > one {
				r--
				zero--
			}
			if one >= zero {
				break
			}
			if l <= r {
				L, R := 0, 0
				for i := l + 1; s[i] == '0'; i++ {
					L++
				}
				for j := r - 1; s[j] == '-'; j-- {
					R++
				}
				if L >= R {
					l++
				} else {
					r--
				}
				one++
			}
		}
		Fprintln(out, max(zero, one))
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() {
	main_edu_128_A()
}

package main

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

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

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var niter int
	fmt.Fscan(in, &niter)
	for iii := 0; iii < niter; iii++ {
		var s string
		fmt.Fscan(in, &s)
		b := []byte(s)
		n := len(s)
		del1l := make([]int, n+1)
		del0l := make([]int, n+1)
		del1r := make([]int, n+1)
		del0r := make([]int, n+1)
		c0, c1 := 0, 0
		for i := 0; i < n; i++ {
			if b[i] == '1' {
				c1++
			} else {
				c0++
			}
			del0l[i+1] = c0
			del1l[i+1] = c1
		}
		c0, c1 = 0, 0
		for i := n - 1; i >= 0; i-- {
			if b[i] == '1' {
				c1++
			} else {
				c0++
			}
			del0r[n-i] = c0
			del1r[n-i] = c1
		}
		cs := del0r[n] // cs
		//bs := make([]int, n+1)
		m := 999999
		for i := 0; i <= n; i++ {
			c1 := del1l[i]
			c2 := del0l[i]
			j := cs - c1 - c2
			if i+j <= n && j >= 0 {
				m = min(m, max(del1l[i]+del1r[j], j))
			}
			//fmt.Println("!!!!", cs, i, j, i+j)
		}
		fmt.Fprintln(out, m)
	}
}

package main

import "fmt"

func main() {
	var test int
	fmt.Scanln(&test)

	for test > 0 {
		test--

		var str string
		fmt.Scanln(&str)

		fmt.Println(solve(str))
	}
}

func solve(str string) int {
	ones := []int{}
	for i, ch := range str {
		if ch == '1' {
			ones = append(ones, i)
		}
	}

	res := len(ones)
	l := 0
	r := len(ones)
	for l <= r {
		m := l + (r-l)/2
		if possible(&ones, m) {
			res = m
			r = m - 1
		} else {
			l = m + 1
		}
	}

	return res
}

func possible(ones *[]int, k int) bool {
	if len(*ones) == 0 {
		return true
	}

	oneLeft := len(*ones) - k
	for i := 0; i < len(*ones)-oneLeft+1; i++ {
		zeroInRange := (*ones)[i+oneLeft-1] - (*ones)[i] - oneLeft
		if zeroInRange <= k {
			return true
		}
	}
	return false
}

package main

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

var s string
var n int
var zeroes int
var ones int

func f(budget int) bool {
	if zeroes <= budget {
		return true
	}
	r := 0
	ones_eaten := ones
	zeroes_left := 0
	left_ones_eaten := 0
	for l := 0; l < n; l++ {
		if s[l] == '1' {
			ones_eaten++
			left_ones_eaten++
		} else {
			zeroes_left--
		}
		if left_ones_eaten > budget {
			break
		}
		for r < n && (r <= l || ones_eaten > budget) {
			if s[r] == '1' {
				ones_eaten--
			} else {
				zeroes_left++
			}
			r++
		}
		if zeroes_left <= budget {
			// fmt.Println(ones_eaten, budget)
			return true
		}
	}
	return false
}

func main() {
	var reader *bufio.Reader = bufio.NewReader(os.Stdin)
	var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var T int
	fmt.Scan(&T)
	for t := 0; t < T; t++ {
		fmt.Fscan(reader, &s)
		n = len(s)
		zeroes = 0
		for i := 0; i < n; i++ {
			if s[i] == '0' {
				zeroes++
			}
		}
		ones = n - zeroes
		// ahhhhh can't figure out how to do it in O(n) so let's do O(n log n) w/ binary search?
		// or w/ a log(n) structure like in LCS
		// don't want to do that so let's do binary search
		l := -1
		r := ones
		// fmt.Println(l, r)
		for r-l > 1 {
			m := (l + r + 1) / 2
			can := f(m)
			if can {
				r = m
			} else {
				l = m
			}
		}
		fmt.Fprintln(writer, r)
	}
}


You are given an array of positive integers. While there are at least two equal elements, we will perform the following operation. We choose the smallest value $$$x$$$ that occurs in the array $$$2$$$ or more times. Take the first two occurrences of $$$x$$$ in this array (the two leftmost occurrences). Remove the left of these two occurrences, and the right one is replaced by the sum of this two values (that is, $$$2 \cdot x$$$).Determine how the array will look after described operations are performed.For example, consider the given array looks like $$$[3, 4, 1, 2, 2, 1, 1]$$$. It will be changed in the following way: $$$[3, 4, 1, 2, 2, 1, 1]~\rightarrow~[3, 4, 2, 2, 2, 1]~\rightarrow~[3, 4, 4, 2, 1]~\rightarrow~[3, 8, 2, 1]$$$.If the given array is look like $$$[1, 1, 3, 1, 1]$$$ it will be changed in the following way: $$$[1, 1, 3, 1, 1]~\rightarrow~[2, 3, 1, 1]~\rightarrow~[2, 3, 2]~\rightarrow~[3, 4]$$$.

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 150\,000$$$) — the number of elements in the array. The second line contains a sequence from $$$n$$$ elements $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^{9}$$$) — the elements of the array.

In the first line print an integer $$$k$$$ — the number of elements in the array after all the performed operations. In the second line print $$$k$$$ integers — the elements of the array after all the performed operations.

NoteThe first two examples were considered in the statement.In the third example all integers in the given array are distinct, so it will not change.

package main

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

type data struct {
	i int
	v int64
}
type dataHeap []data

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

func nextInt() int {
	in.Scan()
	r := 0
	for _, c := range in.Bytes() {
		r *= 10
		r += int(c - '0')
	}
	return r
}

func main() {
	in.Split(bufio.ScanWords)
	var d dataHeap
	var r []data
	for i, n := 0, nextInt(); i < n; i++ {
		heap.Push(&d, data{i, int64(nextInt())})
	}
	for len(d) > 1 {
		a := heap.Pop(&d).(data)
		b := heap.Pop(&d).(data)
		if a.v == b.v {
			b.v *= 2
		} else {
			r = append(r, a)
		}
		heap.Push(&d, b)
	}
	r = append(r, heap.Pop(&d).(data))
	sort.Slice(r, func(i, j int) bool { return r[i].i < r[j].i })
	fmt.Fprintln(out, len(r))
	for i, r := range r {
		if i != 0 {
			out.WriteByte(' ')
		}
		fmt.Fprint(out, r.v)
	}
	out.Flush()
}

func (h dataHeap) Len() int { return len(h) }
func (h dataHeap) Less(i, j int) bool {
	if h[i].v != h[j].v {
		return h[i].v < h[j].v
	}
	return h[i].i < h[j].i
}
func (h dataHeap) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *dataHeap) Push(x interface{}) { *h = append(*h, x.(data)) }
func (h *dataHeap) Pop() interface{}   { r := (*h)[len(*h)-1]; *h = (*h)[:len(*h)-1]; return r }

package main

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

type caseInput struct {
	originalSize uint32
	valueToLastIndex map[uint64]uint32
}

func readCaseInput(scanner *bufio.Scanner) caseInput {
	valueToLastIndex := make(map[uint64]uint32)
	size := uint32(readInt(scanner))

	for i := uint32(0); i < size; i++ {
		v := uint64(readInt64(scanner))

		for {
			_, prs := valueToLastIndex[v]

			if !prs {
				valueToLastIndex[v] = i
				break
			} else {
				delete(valueToLastIndex, v)
				v *= 2
			}
		}
	}

	return caseInput{size, valueToLastIndex}
}

type caseOutput struct {
	nums []uint64
}

func writeCaseOutput(writer *bufio.Writer, out caseOutput) {
	writef(writer, "%d\n", len(out.nums))
	for idx, num := range out.nums {
		if idx+1 < len(out.nums) {
			writef(writer, "%d ", num)
		} else {
			writef(writer, "%d\n", num)
		}
	}
}

func solveCase(in caseInput) caseOutput {
	temp := make([]uint64, in.originalSize)
	for value, lastIndex := range in.valueToLastIndex {
		temp[lastIndex] = value
	}

	res := make([]uint64, 0, len(in.valueToLastIndex))
	for _, v := range temp {
		if v > 0 {
			res = append(res, v)
		}
	}

	return caseOutput{res}
}

func solveSequential(scanner *bufio.Scanner, writer *bufio.Writer) {
	writeCaseOutput(writer, solveCase(readCaseInput(scanner)))
}

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)

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

	solveSequential(scanner, writer)
}

func readInt64(sc *bufio.Scanner) int64 {
	sc.Scan()
	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)
	}
}

package main

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

func main() {
	// map to save index of previous occurence
	indexes := make(map[int64]int)
	var exist bool
	var index int
	// slice to save number
	var arr []int64
	var total int
	var n int64

	// read len from console
	var reader *bufio.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscanf(reader, "%d\n", &total)

	// make a slice with cap total
	arr = make([]int64, total)

	// read num by num
	for i := 0; i < cap(arr); i++ {
		fmt.Fscanf(reader, "%d", &n)
		// attempt to get n from map
		index, exist = indexes[n]

		if exist == false {
			arr[i] = n
			indexes[n] = i
			continue
		}

		for exist == true {
			arr[index] = 0 // set to null
			delete(indexes, n) // forget
			total-- // decrement total of num by 1
			n *= 2
			arr[i] = n
			// repetively search for n * 2
			index, exist = indexes[n]
		}

		// update map when n becomes the first occurence
		indexes[n] = i
	}

	// print out result. omit 0 elements
	fmt.Println(total)
	for _, elem := range arr {
		if elem != 0 {
			fmt.Printf("%d ", elem)
		}
	}
}

package main

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

type num struct {
	val int64
	index int
}

type hp []*num

func (h hp) Len() int {
	return len(h)
}

func (h hp) Less(i, j int) bool {
	if h[i].val < h[j].val {
		return true
	}
	if h[i].val > h[j].val {
		return false
	}
	return h[i].index < h[j].index
}

func (h hp) Swap(i, j int) {
	h[i], h[j] = h[j], h[i]
}

func (h *hp) Push(x interface{}) {
	*h = append(*h, x.(*num))
}

func (h *hp) Pop() interface{} {
	old := *h
	size := len(old)
	last := old[size-1]
	*h = old[0 : size-1]
	return last
}

var n int
var h hp
var b []num

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d\n", &n)
	h = make([]*num, n)
	for i := 0; i < n; i++ {
		var tmp int64
		fmt.Fscanf(inBuf, "%d", &tmp)
		h[i] = &num{val: tmp, index: i}
	}
	heap.Init(&h)
	b = make([]num, 0)
	for len(h) > 0 {
		t1 := heap.Pop(&h).(*num)
		if len(h) == 0 {
			b = append(b, *t1)
			break
		}
		var t2 *num
		for len(h) > 0 {
			t2 = heap.Pop(&h).(*num)
			if t1.val == t2.val {
				heap.Push(&h, &num{val: 2 * t1.val, index: t2.index})
				break
			}
			b = append(b, *t1)
			t1 = t2
			if len(h) == 0 {
				b = append(b, *t2)
			}
		}
	}
	sort.Slice(b, func(i, j int) bool {
		return b[i].index < b[j].index
	})
	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() } ()
	fmt.Fprintln(outBuf, len(b))
	for i := 0; i < len(b); i++ {
		fmt.Fprintf(outBuf, "%d ", b[i].val)
	}
}

package main

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

type data struct{ i, v int }
type dataHeap []data

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

func nextInt() int {
	in.Scan()
	r := 0
	for _, c := range in.Bytes() {
		r *= 10
		r += int(c - '0')
	}
	return r
}

func main() {
	in.Split(bufio.ScanWords)
	var d dataHeap
	var r []data
	for i, n := 0, nextInt(); i < n; i++ {
		heap.Push(&d, data{i, nextInt()})
	}
	for len(d) > 1 {
		a := heap.Pop(&d).(data)
		b := heap.Pop(&d).(data)
		if a.v == b.v {
			b.v *= 2
		} else {
			r = append(r, a)
		}
		heap.Push(&d, b)
	}
	r = append(r, heap.Pop(&d).(data))
	sort.Slice(r, func(i, j int) bool { return r[i].i < r[j].i })
	fmt.Fprintln(out, len(r))
	for i, r := range r {
		if i != 0 {
			out.WriteByte(' ')
		}
		fmt.Fprint(out, r.v)
	}
	out.Flush()
}

func (h dataHeap) Len() int { return len(h) }
func (h dataHeap) Less(i, j int) bool {
	if h[i].v != h[j].v {
		return h[i].v < h[j].v
	}
	return h[i].i < h[j].i
}
func (h dataHeap) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *dataHeap) Push(x interface{}) { *h = append(*h, x.(data)) }
func (h *dataHeap) Pop() interface{}   { r := (*h)[len(*h)-1]; *h = (*h)[:len(*h)-1]; return r }

package main

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

type caseInput struct {
	nums []int
}

//	FIXME read the input
func readCaseInput(scanner *bufio.Scanner) caseInput {
	size := readInt(scanner)
	nums := make([]int, size)
	for i := range nums {
		nums[i] = readInt(scanner)
	}
	in := caseInput{nums}
	return in
}

type caseOutput struct {
	nums []int
}

func writeCaseOutput(writer *bufio.Writer, out caseOutput) {
	writef(writer, "%d\n", len(out.nums))
	for idx, num := range out.nums {
		if idx+1 < len(out.nums) {
			writef(writer, "%d ", num)
		} else {
			writef(writer, "%d\n", num)
		}
	}
}

func solveCase(in caseInput) caseOutput {
	// FIXME actual solution
	v2indexes, vSorted := buildIndexes(in)

	for i := 0; i < len(vSorted); i++ {
		v := vSorted[i]
		indexes := v2indexes[v]
		for len(indexes) > 1 {
			deleted := indexes[:2]
			inserted := deleted[1]
			indexes = indexes[2:]
			v2indexes[v] = indexes

			insertedIndexes, prs := v2indexes[2*v]
			if prs {
				iSearch := sort.SearchInts(insertedIndexes, inserted)
				if iSearch == len(insertedIndexes) {
					v2indexes[2*v] = append(insertedIndexes, inserted)
				} else if insertedIndexes[iSearch] != inserted {
					v2indexes[2*v] = append(insertedIndexes[:iSearch], append([]int{inserted}, insertedIndexes[iSearch:]...)...)
				}
			} else {
				v2indexes[2*v] = append(make([]int, 0, 2), inserted)

				vSearch := sort.SearchInts(vSorted, 2*v)
				if vSearch == len(vSorted) {
					vSorted = append(vSorted, 2*v)
				} else if vSorted[vSearch] != 2*v {
					vSorted = append(vSorted[:vSearch], append([]int{2 * v}, vSorted[vSearch:]...)...)
				}
			}
		}
	}

	temp := make([]int, len(in.nums))
	for v, indexes := range v2indexes {
		for _, index := range indexes {
			temp[index] = v
		}
	}

	res := make([]int, 0, len(in.nums))
	for _, v := range temp {
		if v > 0 {
			res = append(res, v)
		}
	}

	return caseOutput{res}
}

func buildIndexes(in caseInput) (map[int][]int, []int) {
	v2indexes := make(map[int][]int)
	vSorted := make([]int, 0, len(in.nums))
	for i, v := range in.nums {

		indexes, prs := v2indexes[v]
		if prs {
			v2indexes[v] = append(indexes, i)
		} else {
			v2indexes[v] = append(make([]int, 0, 2), i)
		}

		vSearch := sort.SearchInts(vSorted, v)
		if vSearch == len(vSorted) {
			vSorted = append(vSorted, v)
		} else if vSorted[vSearch] != v {
			vSorted = append(vSorted[:vSearch], append([]int{v}, vSorted[vSearch:]...)...)
		}
	}
	return v2indexes, vSorted
}

func solveSequential(scanner *bufio.Scanner, writer *bufio.Writer) {
	writeCaseOutput(writer, solveCase(readCaseInput(scanner)))
}

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)

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

	solveSequential(scanner, writer)
}

func readInt64(sc *bufio.Scanner) int64 {
	sc.Scan()
	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)
	}
}

package main

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

type caseInput struct {
	nums []int
}

func readCaseInput(scanner *bufio.Scanner) caseInput {
	size := readInt(scanner)
	nums := make([]int, size)
	for i := range nums {
		nums[i] = readInt(scanner)
	}
	in := caseInput{nums}
	return in
}

type caseOutput struct {
	nums []int
}

func writeCaseOutput(writer *bufio.Writer, out caseOutput) {
	writef(writer, "%d\n", len(out.nums))
	for idx, num := range out.nums {
		if idx+1 < len(out.nums) {
			writef(writer, "%d ", num)
		} else {
			writef(writer, "%d\n", num)
		}
	}
}

func solveCase(in caseInput) caseOutput {
	v2indexes, vSorted := buildIndexes(in)

	for i := 0; i < len(vSorted); i++ {
		v := vSorted[i]
		indexes := v2indexes[v]
		for len(indexes) > 1 {
			deleted := indexes[:2]
			inserted := deleted[1]
			indexes = indexes[2:]
			v2indexes[v] = indexes

			insertedIndexes, prs := v2indexes[2*v]
			if prs {
				iSearch := sort.SearchInts(insertedIndexes, inserted)
				if iSearch == len(insertedIndexes) {
					v2indexes[2*v] = append(insertedIndexes, inserted)
				} else if insertedIndexes[iSearch] != inserted {
					v2indexes[2*v] = append(insertedIndexes[:iSearch], append([]int{inserted}, insertedIndexes[iSearch:]...)...)
				}
			} else {
				v2indexes[2*v] = append(make([]int, 0, 2), inserted)

				vSearch := sort.SearchInts(vSorted, 2*v)
				if vSearch == len(vSorted) {
					vSorted = append(vSorted, 2*v)
				} else if vSorted[vSearch] != 2*v {
					vSorted = append(vSorted[:vSearch], append([]int{2 * v}, vSorted[vSearch:]...)...)
				}
			}
		}
	}

	temp := make([]int, len(in.nums))
	for v, indexes := range v2indexes {
		for _, index := range indexes {
			temp[index] = v
		}
	}

	res := make([]int, 0, len(in.nums))
	for _, v := range temp {
		if v > 0 {
			res = append(res, v)
		}
	}

	return caseOutput{res}
}

func buildIndexes(in caseInput) (map[int][]int, []int) {
	v2indexes := make(map[int][]int)
	vSorted := make([]int, 0, len(in.nums))
	for i, v := range in.nums {

		indexes, prs := v2indexes[v]
		if prs {
			v2indexes[v] = append(indexes, i)
		} else {
			v2indexes[v] = append(make([]int, 0, 2), i)
		}

		vSearch := sort.SearchInts(vSorted, v)
		if vSearch == len(vSorted) {
			vSorted = append(vSorted, v)
		} else if vSorted[vSearch] != v {
			vSorted = append(vSorted[:vSearch], append([]int{v}, vSorted[vSearch:]...)...)
		}
	}
	return v2indexes, vSorted
}

func solveSequential(scanner *bufio.Scanner, writer *bufio.Writer) {
	writeCaseOutput(writer, solveCase(readCaseInput(scanner)))
}

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)

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

	solveSequential(scanner, writer)
}

func readInt64(sc *bufio.Scanner) int64 {
	sc.Scan()
	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)
	}
}

package main

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

func main() {
	// map to save index of previous occurence
	indexes := make(map[int]int)
	var exist bool
	var index int
	// slice to save number
	var arr []int
	var total, n int

	// read len from console
	var reader *bufio.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscanf(reader, "%d\n", &total)

	// make a slice with cap total
	arr = make([]int, total)

	// read num by num
	for i := 0; i < cap(arr); i++ {
		fmt.Fscanf(reader, "%d", &n)
		// attempt to get n from map
		index, exist = indexes[n]

		if exist == false {
			arr[i] = n
			indexes[n] = i
			continue
		}

		for exist == true {
			arr[index] = 0 // set to null
			delete(indexes, n) // forget
			total-- // decrement total of num by 1
			n *= 2
			arr[i] = n
			// repetively search for n * 2
			index, exist = indexes[n]
		}

		// update map when n becomes the first occurence
		indexes[n] = i
	}

	// print out result. omit 0 elements
	fmt.Println(total)
	for _, elem := range arr {
		if elem != 0 {
			fmt.Printf("%d ", elem)
		}
	}
}

package main

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

type num struct {
	val, index int
}

type hp []*num

func (h hp) Len() int {
	return len(h)
}

func (h hp) Less(i, j int) bool {
	if h[i].val < h[j].val {
		return true
	}
	if h[i].val > h[j].val {
		return false
	}
	return h[i].index < h[j].index
}

func (h hp) Swap(i, j int) {
	h[i], h[j] = h[j], h[i]
}

func (h *hp) Push(x interface{}) {
	*h = append(*h, x.(*num))
}

func (h *hp) Pop() interface{} {
	old := *h
	size := len(old)
	last := old[size-1]
	*h = old[0 : size-1]
	return last
}

var n int
var h hp
var b []num

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d\n", &n)
	h = make([]*num, n)
	for i := 0; i < n; i++ {
		var tmp int
		fmt.Fscanf(inBuf, "%d", &tmp)
		h[i] = &num{val: tmp, index: i}
	}
	heap.Init(&h)
	b = make([]num, 0)
	for len(h) > 0 {
		t1 := heap.Pop(&h).(*num)
		if len(h) == 0 {
			b = append(b, *t1)
			break
		}
		var t2 *num
		for len(h) > 0 {
			t2 = heap.Pop(&h).(*num)
			if t1.val == t2.val {
				heap.Push(&h, &num{val: 2 * t1.val, index: t2.index})
				break
			}
			b = append(b, *t1)
			t1 = t2
			if len(h) == 0 {
				b = append(b, *t2)
			}
		}
	}
	sort.Slice(b, func(i, j int) bool {
		return b[i].index < b[j].index
	})
	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() } ()
	fmt.Fprintln(outBuf, len(b))
	for i := 0; i < len(b); i++ {
		fmt.Fprintf(outBuf, "%d ", b[i].val)
	}
}


You're given an array $$$a$$$ of length $$$n$$$. You can perform the following operations on it:  choose an index $$$i$$$ $$$(1 \le i \le n)$$$, an integer $$$x$$$ $$$(0 \le x \le 10^6)$$$, and replace $$$a_j$$$ with $$$a_j+x$$$ for all $$$(1 \le j \le i)$$$, which means add $$$x$$$ to all the elements in the prefix ending at $$$i$$$.  choose an index $$$i$$$ $$$(1 \le i \le n)$$$, an integer $$$x$$$ $$$(1 \le x \le 10^6)$$$, and replace $$$a_j$$$ with $$$a_j \% x$$$ for all $$$(1 \le j \le i)$$$, which means replace every element in the prefix ending at $$$i$$$ with the remainder after dividing it by $$$x$$$. Can you make the array strictly increasing in no more than $$$n+1$$$ operations?

The first line contains an integer $$$n$$$ $$$(1 \le n \le 2000)$$$, the number of elements in the array $$$a$$$. The second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, $$$\dots$$$, $$$a_n$$$ $$$(0 \le a_i \le 10^5)$$$, the elements of the array $$$a$$$.

On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "$$$1$$$ $$$i$$$ $$$x$$$"; to print a modding operation, use the format "$$$2$$$ $$$i$$$ $$$x$$$". If $$$i$$$ or $$$x$$$ don't satisfy the limitations above, or you use more than $$$n+1$$$ operations, you'll get wrong answer verdict.

NoteIn the first sample, the array is already increasing so we don't need any operations.In the second sample:In the first step: the array becomes $$$[8,6,3]$$$.In the second step: the array becomes $$$[0,2,3]$$$.

package main

import (
	"fmt"
	"bufio"
	"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(){
	defer writer.Flush()
	var n, i int
	var a [2004]int
	var ans []int
	scanf("%d\n", &n)
	for i=0;i<n;i++ {
		scanf("%d", &a[i])
		a[i] += 900000
	}
	ans = append(ans, 900000)
	for i=0;i<n-1;i++ {
		ans = append(ans, a[i]-i)
		a[i] %= a[i]-i
	}
	printf("%d\n1 %d 900000\n", n, n)
	for i=1;i<n;i++ {
		printf("2 %d %d\n", i, ans[i])
	}
	// printf("debug:")
	// for i=0;i<n;i++ {
	// 	printf(" %d", a[i])
	// }
	// printf("\n")
}

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)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &a[i])
	}
	currentSum := 0
	fmt.Fprintf(out, "%d\n", n+1)
	for i := n - 1; i >= 0; i-- {
		j := i - (a[i]+currentSum)%n
		if j < 0 {
			j += n
		}
		currentSum += j
		a[i] = (a[i] + currentSum) % n
		fmt.Fprintf(out, "%d %d %d\n", 1, i+1, j)
	}
	fmt.Fprintf(out, "%d %d %d\n", 2, n, n)
	//fmt.Println(a)
	return
}

package main
import "fmt"
func main() {
	var n int
	fmt.Scanf("%d",&n)
	for i:= 1;i<=n;i++{
		var x int
		fmt.Scanf("%d",&x)
	}
	ps := 1000000
	fmt.Println(n+1)
	fmt.Printf("2 %d 1\n",n)
	fmt.Printf("1 %d %d\n",n,ps)
	for i:=1;i<n;i++{
		fmt.Printf("2 %d %d\n",i,ps+1-i)
	}
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)

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

func main() {
	scanner.Split(bufio.ScanWords)
	n := scanNum()
	res := []string{}
	// n is also gen the final 2 n n+1
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanNum()
	}
	counter := 0
	for i := 1; i < n; i++ {
		if a[i] > a[i-1] {
			counter++
		}
	}
	// if counter == (len(a) - 1) {
	// 	fmt.Println(0)
	// 	os.Exit(0)
	// }

	sum, width := 0, n+1
	for i := n - 1; i >= 0; i-- {
		if (a[i]+sum)%width == i {
			continue
		}
		add := i - (a[i]+sum)%width
		if add < 0 {
			add += width
		}
		res = append(res, fmt.Sprintln(1, i+1, add))
		sum = (sum + add) % width
	}
	if len(res) == 0 {
		fmt.Println(0)
	} else {
		fmt.Println(len(res) + 1)
		for _, v := range res {
			fmt.Print(v)
		}
		fmt.Println(2, n, width)
	}
}

package main

import (
	"fmt"
	"bufio"
	"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(){
	defer writer.Flush()
	var n, i int
	var a [2004]int
	var ans []int
	scanf("%d\n", &n)
	for i=0;i<n;i++ {
		scanf("%d", &a[i])
		a[i] += 900000
	}
	ans = append(ans, 900000)
	for i=0;i<n-1;i++ {
		ans = append(ans, a[i]-i)
		a[i] %= a[i]-i
	}
	printf("%d\n1 %d 900000\n", n, n)
	for i=0;i<n-1;i++ {
		printf("2 %d %d\n", i, ans[i+1])
	}
	// printf("debug:")
	// for i=0;i<n;i++ {
	// 	printf(" %d", a[i])
	// }
	// printf("\n")
}

package main

import (
	"fmt"
	"bufio"
	"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(){
	defer writer.Flush()
	var n, i, j int
	var a [2004]int
	var ans []int
	scanf("%d\n", &n)
	for i=0;i<n;i++ {
		scanf("%d", &a[i])
		a[i] += 1000000
	}
	ans = append(ans, 1000000)
	for i=n-2;i>=0;i-- {
		ans = append(ans, a[i+1])
		for j=0;j<=i;j++ {
			a[j] %= a[i+1]
		}
	}
	printf("%d\n1 %d 1000000\n", n, n)
	for i=1;i<n;i++ {
		printf("2 %d %d\n", n-i, ans[i])
	}
	// printf("debug:")
	// for i=0;i<n;i++ {
	// 	printf(" %d", a[i])
	// }
	// printf("\n")
}

package main
import "fmt"
func main() {
	var n int
	fmt.Scanf("%d",&n)
	for i:= 1;i<=n;i++{
		var x int
		fmt.Scanf("%d",&x)
	}
	ps := 1000000
	fmt.Println(n+1)
	fmt.Printf("2 %d 1\n",n)
	fmt.Printf("2 %d %d\n",n,ps)
	for i:=1;i<n;i++{
		fmt.Printf("2 %d %d\n",i,ps+1-i)
	}
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)

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

func main() {
	scanner.Split(bufio.ScanWords)
	n := scanNum()
	// n is also gen the final 2 n n+1
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanNum()
	}
	counter := 0
	for i := 1; i < n; i++ {
		if a[i] > a[i-1] {
			counter++
		}
	}
	if counter == (len(a) - 1) {
		fmt.Println(0)
		os.Exit(0)
	}

	sum, printNone, width := 0, true, n+1
	for i := n - 1; i >= 0; i-- {
		if (a[i]+sum)%width == i {
			continue
		}
		add := i - (a[i]+sum)%width
		if add < 0 {
			add += width
		}
		fmt.Println(1, i+1, add)
		printNone, sum = false, (sum+add)%width
	}
	if printNone {
		fmt.Println(0)
	} else {
		fmt.Println(2, n, width)
	}
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)

func scanNum() int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}
func main() {
	scanner.Split(bufio.ScanWords)
	n := scanNum()
	// n is also gen the final 2 n n+1
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanNum()
	}

	sum, printNone, width := 0, true, n+1
	for i := n - 1; i >= 0; i-- {
		expectVal := i + 1
		if (a[i]+sum)%width == expectVal {
			continue
		}
		add := expectVal - (a[i]+sum)%width
		if add < 0 {
			add += width
		}
		fmt.Println(1, i+1, add)
		printNone, sum = false, sum+add
	}
	if printNone {
		fmt.Println(0)
	} else {
		fmt.Println(2, n, width)
	}
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)

func scanNum() int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}
func main() {
	scanner.Split(bufio.ScanWords)
	n := scanNum()
	// n is also gen the final 2 n n+1
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanNum()
	}

	sum, pos, printNone, width := 0, n, true, n+1
	for i := n - 1; i >= 0; i-- {
		cur := a[i] + sum
		if cur%width != pos {
			diff := pos - cur%width
			add := width + diff
			fmt.Println(1, i+1, add)
			printNone, sum = false, (sum+add)%width
		}
		pos--
	}
	if printNone {
		fmt.Println(0)
	} else {
		fmt.Println(2, n, width)
	}
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)

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

func main() {
	scanner.Split(bufio.ScanWords)
	n := scanNum()
	// n is also gen the final 2 n n+1
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanNum()
	}

	sum, printNone, width := 0, true, n+1
	for i := n - 1; i >= 0; i-- {
		if (a[i]+sum)%width == i {
			continue
		}
		add := i - (a[i]+sum)%width
		if add < 0 {
			add += width
		}
		fmt.Println(1, i+1, add)
		printNone, sum = false, (sum+add)%width
	}
	if printNone {
		fmt.Println(0)
	} else {
		fmt.Println(2, n, width)
	}
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)

func scanNum() int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}
func main() {
	scanner.Split(bufio.ScanWords)
	n := scanNum()
	// n is also gen the final 2 n n+1
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanNum()
	}

	sum, width := 0, n+1
	for i := n - 1; i >= 0; i-- {
		expectVal := i + 1
		add := expectVal - (a[i]+sum)%width
		if add < 0 {
			add += width
		}
		fmt.Println(1, i+1, add)
		sum += add
	}

	fmt.Println(2, n, width)
}


It is the hard version of the problem. The only difference is that in this version $$$a_i \le 10^9$$$.You are given an array of $$$n$$$ integers $$$a_0, a_1, a_2, \ldots a_{n - 1}$$$. Bryap wants to find the longest beautiful subsequence in the array.An array $$$b = [b_0, b_1, \ldots, b_{m-1}]$$$, where $$$0 \le b_0 < b_1 < \ldots < b_{m - 1} < n$$$, is a subsequence of length $$$m$$$ of the array $$$a$$$.Subsequence $$$b = [b_0, b_1, \ldots, b_{m-1}]$$$ of length $$$m$$$ is called beautiful, if the following condition holds:   For any $$$p$$$ ($$$0 \le p < m - 1$$$) holds: $$$a_{b_p} \oplus b_{p+1} < a_{b_{p+1}} \oplus b_p$$$. Here $$$a \oplus b$$$ denotes the bitwise XOR of $$$a$$$ and $$$b$$$. For example, $$$2 \oplus 4 = 6$$$ and $$$3 \oplus 1=2$$$.Bryap is a simple person so he only wants to know the length of the longest such subsequence. Help Bryap and find the answer to his question.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 3 \cdot 10^5$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_0,a_1,...,a_{n-1}$$$ ($$$0 \leq a_i \leq 10^9$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.

For each test case print a single integer — the length of the longest beautiful subsequence.

NoteIn the first test case, we can pick the whole array as a beautiful subsequence because $$$1 \oplus 1 < 2 \oplus 0$$$.In the second test case, we can pick elements with indexes $$$1$$$, $$$2$$$ and $$$4$$$ (in $$$0$$$ indexation). For this elements holds: $$$2 \oplus 2 < 4 \oplus 1$$$ and $$$4 \oplus 4 < 1 \oplus 2$$$.

package main

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

var _, _, _ = main, rw, do

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

func rw(r io.Reader, w io.Writer) {
	var t int
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n int
		fmt.Fscan(r, &n)
		a := make([]int, n)
		for i := range a {
			fmt.Fscan(r, &a[i])
		}
		w1 := do(n, a)
		fmt.Fprintln(w, w1)
	}
}

const mx = 30

func do(n int, a []int) int {
	t := &node{}
	getjb := func(vb, ib int) int {
		if vb == 0 {
			return ib ^ 1
		}
		return ib
	}
	z := 0
	for i := range a {
		p := t
		v := a[i] ^ i
		r := 0
		for k := mx - 1; k >= 0 && p != nil; k-- {
			vb := (v >> k) & 1
			ib := (i >> k) & 1
			if q := p.c[vb^1]; q != nil {
				jb := getjb(vb, ib)
				if r < q.v[jb] {
					r = q.v[jb]
				}
			}
			p = p.c[vb]
		}
		r++
		p = t
		for k := mx - 1; k >= 0; k-- {
			vb := (v >> k) & 1
			ib := (i >> k) & 1
			q := p.c[vb]
			if q == nil {
				q = &node{}
				p.c[vb] = q
			}
			if q.v[ib] < r {
				q.v[ib] = r
			}
			p = p.c[vb]
		}
		if z < r {
			z = r
		}
	}
	return z
}

type node struct {
	c [2]*node
	v [2]int
}


Bob programmed a robot to navigate through a 2d maze.The maze has some obstacles. Empty cells are denoted by the character '.', where obstacles are denoted by '#'.There is a single robot in the maze. Its start position is denoted with the character 'S'. This position has no obstacle in it. There is also a single exit in the maze. Its position is denoted with the character 'E'. This position has no obstacle in it.The robot can only move up, left, right, or down.When Bob programmed the robot, he wrote down a string of digits consisting of the digits 0 to 3, inclusive. He intended for each digit to correspond to a distinct direction, and the robot would follow the directions in order to reach the exit. Unfortunately, he forgot to actually assign the directions to digits.The robot will choose some random mapping of digits to distinct directions. The robot will map distinct digits to distinct directions. The robot will then follow the instructions according to the given string in order and chosen mapping. If an instruction would lead the robot to go off the edge of the maze or hit an obstacle, the robot will crash and break down. If the robot reaches the exit at any point, then the robot will stop following any further instructions.Bob is having trouble debugging his robot, so he would like to determine the number of mappings of digits to directions that would lead the robot to the exit.

The first line of input will contain two integers n and m (2 ≤ n, m ≤ 50), denoting the dimensions of the maze. The next n lines will contain exactly m characters each, denoting the maze. Each character of the maze will be '.', '#', 'S', or 'E'. There will be exactly one 'S' and exactly one 'E' in the maze. The last line will contain a single string s (1 ≤ |s| ≤ 100) — the instructions given to the robot. Each character of s is a digit from 0 to 3.

Print a single integer, the number of mappings of digits to directions that will lead the robot to the exit.

NoteFor the first sample, the only valid mapping is , where D is down, L is left, U is up, R is right.

package main

import "fmt"

// Next permutation.
func next(p []int) bool {
	var k, l int
	for k = len(p) - 2; ; k-- {
		if k < 0 {
			return false
		}
		if p[k] < p[k+1] {
			break
		}
	}
	for l = len(p) - 1; p[k] >= p[l]; l-- {
	}
	p[k], p[l] = p[l], p[k]
	for i, j := k+1, len(p)-1; i < j; i++ {
		p[i], p[j] = p[j], p[i]
		j--
	}
	return true
}

func main() {
	var n, m int
	fmt.Scan(&n, &m)
	maze := make([]string, n)
	for i := range maze {
		fmt.Scan(&maze[i])
	}
	var s string
	fmt.Scan(&s)

	perm := []int{0, 1, 2, 3}
	ans := 0
	for {
		if exit(maze, perm, s) {
			ans++
		}
		if !next(perm) {
			break
		}
	}
	fmt.Println(ans)
}

func exit(maze []string, instr []int, s string) bool {
	r, c := find(maze)
	for i := range s {
		switch instr[s[i]-'0'] {
		case 0:
			r++
		case 1:
			r--
		case 2:
			c++
		case 3:
			c--
		}
		if r < 0 || r >= len(maze) || c < 0 || c >= len(maze[0]) || maze[r][c] == '#' {
			return false
		}
		if maze[r][c] == 'E' {
			return true
		}
	}
	return false
}

func find(m []string) (r, c int) {
	for r = 0; r < len(m); r++ {
		for c = 0; c < len(m[0]); c++ {
			if m[r][c] == 'S' {
				return
			}
		}
	}
	return
}

package main

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

var start Pos
var goal Pos

var check = map[int]int{}
var s string
var N, M int

var ans int

func recv(field []string, count int) {

	if count == 4 {

		current := start

		for i, _ := range s {
			m, _ := strconv.Atoi(string(s[i]))
			v := check[m]
			next := current

			if v == 0 {
				next.x = current.x + 1
			} else if v == 1 {
				next.x = current.x - 1
			} else if v == 2 {
				next.y = current.y + 1
			} else if v == 3 {
				next.y = current.y - 1
			}
			if 0 <= next.x && next.x < N && 0 <= next.y && next.y < M {

			} else {
				return
			}
			if field[next.x][next.y] == '#' {
				return
			}
			if field[next.x][next.y] == 'E' {

				ans++
				return
			}

			current = next
		}
		return
	}
	for i := 0; i < 4; i++ {
		if _, ok := check[i]; ok {
			continue
		}
		check[i] = count
		recv(field, count+1)
		delete(check, i)
	}
}

type Pos struct {
	x int
	y int
}

func main() {
	sc := NewScanner()
	N, M = sc.NextInt(), sc.NextInt()

	field := make([]string, N)

	for i := 0; i < N; i++ {
		field[i] = sc.NextLine()
		for j := 0; j < M; j++ {
			if field[i][j] == 'S' {
				start = Pos{i, j}
			} else if field[i][j] == 'E' {
				goal = Pos{i, j}
			}
		}
	}
	s = sc.NextLine()
	recv(field, 0)
	fmt.Println(ans)
}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}


Igor has been into chess for a long time and now he is sick of the game by the ordinary rules. He is going to think of new rules of the game and become world famous.Igor's chessboard is a square of size n × n cells. Igor decided that simple rules guarantee success, that's why his game will have only one type of pieces. Besides, all pieces in his game are of the same color. The possible moves of a piece are described by a set of shift vectors. The next passage contains a formal description of available moves.Let the rows of the board be numbered from top to bottom and the columns be numbered from left to right from 1 to n. Let's assign to each square a pair of integers (x, y) — the number of the corresponding column and row. Each of the possible moves of the piece is defined by a pair of integers (dx, dy); using this move, the piece moves from the field (x, y) to the field (x + dx, y + dy). You can perform the move if the cell (x + dx, y + dy) is within the boundaries of the board and doesn't contain another piece. Pieces that stand on the cells other than (x, y) and (x + dx, y + dy) are not important when considering the possibility of making the given move (for example, like when a knight moves in usual chess).Igor offers you to find out what moves his chess piece can make. He placed several pieces on the board and for each unoccupied square he told you whether it is attacked by any present piece (i.e. whether some of the pieces on the field can move to that cell). Restore a possible set of shift vectors of the piece, or else determine that Igor has made a mistake and such situation is impossible for any set of shift vectors.

The first line contains a single integer n (1 ≤ n ≤ 50). The next n lines contain n characters each describing the position offered by Igor. The j-th character of the i-th string can have the following values:  o — in this case the field (i, j) is occupied by a piece and the field may or may not be attacked by some other piece; x — in this case field (i, j) is attacked by some piece; . — in this case field (i, j) isn't attacked by any piece. It is guaranteed that there is at least one piece on the board.

If there is a valid set of moves, in the first line print a single word 'YES' (without the quotes). Next, print the description of the set of moves of a piece in the form of a (2n - 1) × (2n - 1) board, the center of the board has a piece and symbols 'x' mark cells that are attacked by it, in a format similar to the input. See examples of the output for a full understanding of the format. If there are several possible answers, print any of them. If a valid set of moves does not exist, print a single word 'NO'.

NoteIn the first sample test the piece is a usual chess rook, and in the second sample test the piece is a usual chess knight.

package main

import "fmt"

func work(Board, Moves [][]byte, n int) {
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if Board[i][j] == 'o' {
				l := n-i-1
				for ; l < 2*n-i-1; l++ {
					for r := n-j-1; r < 2*n-j-1; r++ {
						if Board[i+l-n+1][j+r-n+1] == '.' {
							Moves[l][r] = '.'
						}
					}
				}
			}
		}
	}
}

func workHard(Board, Moves [][]byte, n int) {
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if Board[i][j] == 'o' {
				l := n-i-1
				for ; l < 2*n-i-1; l++ {
					for r := n-j-1; r < 2*n-j-1; r++ {
						if Moves[l][r] == 'x' && Board[i+l-n+1][j+r-n+1] != 'o' {
							Board[i+l-n+1][j+r-n+1] = '.'
						}
					}
				}
			}
		}
	}
}

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

	Board := make([][]byte, n)
	var buf string
	for i := 0; i < len(Board); i++ {
		fmt.Scanf("%s\n", &buf)
		Board[i] = []byte(buf)
	}

	Moves := make([][]byte, 2*n - 1)
	for i := 0; i < len(Moves); i++ {
		for j := 0; j < len(Moves); j++ {
			Moves[i] = append(Moves[i], 'x')
		}
	}
	Moves[n-1][n-1] = 'o'

	work(Board, Moves, n)
	workHard(Board, Moves, n)

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if Board[i][j] == 'x' {
				fmt.Println("NO")
				return
			}
		}
	}

	fmt.Println("YES")
	for i := 0; i < 2*n-1; i++ {
		fmt.Println(string(Moves[i]))
	}
}

package main

import "fmt"

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

	Board := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%s\n", &Board[i])
	}

	Moves := make([]string, 2*n - 1)
	for i := 0; i < len(Moves); i++ {
		for j := 0; j < len(Moves); j++ {
			if i == n-1 && j == n-1 {
				Moves[i] += "o"
			} else {
				Moves[i] += "x"
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if Board[i][j] == 'o' {
				l := n-i-1
				for ; l < 2*n-i-1; l++ {
					for r := n-j-1; r < 2*n-j-1; r++ {
						if Board[i+l-n+1][j+r-n+1] == '.' {
							Moves[l] = Moves[l][:r] + "." + Moves[l][r+1:]
						}
					}
				}
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if Board[i][j] == 'o' {
				l := n-i-1
				for ; l < 2*n-i-1; l++ {
					for r := n-j-1; r < 2*n-j-1; r++ {
						if Moves[l][r] == 'x' && Board[i+l-n+1][j+r-n+1] != 'o' {
							Board[i+l-n+1] = Board[i+l-n+1][:j+r-n+1] + "." + Board[i+l-n+1][j+r-n+2:]
						}
					}
				}
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if Board[i][j] == 'x' {
				fmt.Println("NO")
				return
			}
		}
	}

	fmt.Println("YES")
	for i := 0; i < 2*n-1; i++ {
		fmt.Println(Moves[i])
	}
}

package main

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

func work(Board, Moves [][]byte, n int) {
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if Board[i][j] == 'o' {
				l := n-i-1
				for ; l < 2*n-i-1; l++ {
					for r := n-j-1; r < 2*n-j-1; r++ {
						if Board[i+l-n+1][j+r-n+1] == '.' {
							Moves[l][r] = '.'
						}
					}
				}
			}
		}
	}
}

func workHard(Board, Moves [][]byte, n int) {
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if Board[i][j] == 'o' {
				l := n-i-1
				for ; l < 2*n-i-1; l++ {
					for r := n-j-1; r < 2*n-j-1; r++ {
						if Moves[l][r] == 'x' && Board[i+l-n+1][j+r-n+1] != 'o' {
							Board[i+l-n+1][j+r-n+1] = '.'
						}
					}
				}
			}
		}
	}
}

func read() (int, [][]byte, [][]byte) {
	stdin := bufio.NewReader(os.Stdin)

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

	Board := make([][]byte, n)
	var buf string
	for i := 0; i < len(Board); i++ {
		fmt.Fscanf(stdin, "%s\n", &buf)
		Board[i] = []byte(buf)
	}

	Moves := make([][]byte, 2*n - 1)
	for i := 0; i < len(Moves); i++ {
		for j := 0; j < len(Moves); j++ {
			Moves[i] = append(Moves[i], 'x')
		}
	}
	Moves[n-1][n-1] = 'o'
	return n, Board, Moves
}

func main() {
	n, Board, Moves := read()

	work(Board, Moves, n)
	workHard(Board, Moves, n)

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if Board[i][j] == 'x' {
				fmt.Println("NO")
				return
			}
		}
	}

	s := "YES\n"
	for i := 0; i < 2*n-1; i++ {
		s += string(Moves[i]) + "\n"
	}
	fmt.Print(s)
}

package main

import (
	"fmt"
)

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

	Board := make([][]byte, n)
	var buf string
	for i := 0; i < len(Board); i++ {
		fmt.Scanf("%s\n", &buf)
		Board[i] = []byte(buf)
	}

	Moves := make([][]byte, 2*n - 1)
	for i := 0; i < len(Moves); i++ {
		for j := 0; j < len(Moves); j++ {
			Moves[i] = append(Moves[i], 'x')
		}
	}
	Moves[n-1][n-1] = 'o'

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if Board[i][j] == 'o' {
				l := n-i-1
				for ; l < 2*n-i-1; l++ {
					for r := n-j-1; r < 2*n-j-1; r++ {
						if Board[i+l-n+1][j+r-n+1] == '.' {
							Moves[l][r] = '.'
						}
					}
				}
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if Board[i][j] == 'o' {
				l := n-i-1
				for ; l < 2*n-i-1; l++ {
					for r := n-j-1; r < 2*n-j-1; r++ {
						if Moves[l][r] == 'x' && Board[i+l-n+1][j+r-n+1] != 'o' {
							Board[i+l-n+1][j+r-n+1] = '.'
						}
					}
				}
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if Board[i][j] == 'x' {
				fmt.Println("NO")
				return
			}
		}
	}

	fmt.Println("YES")
	for i := 0; i < 2*n-1; i++ {
		fmt.Println(string(Moves[i]))
	}
}

package main

import "fmt"

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

	Board := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%s\n", &Board[i])
	}

	Moves := make([]string, 2*n - 1)
	for i := 0; i < len(Moves); i++ {
		for j := 0; j < len(Moves); j++ {
			if i == n-1 && j == n-1 {
				Moves[i] += "o"
			} else {
				Moves[i] += "x"
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if Board[i][j] == 'o' {
				l := n-i-1
				for ; l < 2*n-i-1; l++ {
					for r := n-j-1; r < 2*n-j-1; r++ {
						if Board[i+l-n+1][j+r-n+1] == '.' {
							Moves[l] = Moves[l][:r] + "." + Moves[l][r+1:]
						}
					}
					fmt.Println("")
				}
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if Board[i][j] == 'o' {
				l := n-i-1
				for ; l < 2*n-i-1; l++ {
					for r := n-j-1; r < 2*n-j-1; r++ {
						if Moves[l][r] == 'x' {
							Board[i+l-n+1] = Board[i+l-n+1][:j+r-n+1] + "." + Board[i+l-n+1][j+r-n+2:]
						}
					}
				}
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if Board[i][j] == 'x' {
				fmt.Println("NO")
				return
			}
		}
	}

	fmt.Println("YES")
	for i := 0; i < 2*n-1; i++ {
		fmt.Println(Moves[i])
	}
}


Haiku is a genre of Japanese traditional poetry.A haiku poem consists of 17 syllables split into three phrases, containing 5, 7 and 5 syllables correspondingly (the first phrase should contain exactly 5 syllables, the second phrase should contain exactly 7 syllables, and the third phrase should contain exactly 5 syllables). A haiku masterpiece contains a description of a moment in those three phrases. Every word is important in a small poem, which is why haiku are rich with symbols. Each word has a special meaning, a special role. The main principle of haiku is to say much using a few words.To simplify the matter, in the given problem we will consider that the number of syllable in the phrase is equal to the number of vowel letters there. Only the following letters are regarded as vowel letters: "a", "e", "i", "o" and "u".Three phases from a certain poem are given. Determine whether it is haiku or not.

The input data consists of three lines. The length of each line is between 1 and 100, inclusive. The i-th line contains the i-th phrase of the poem. Each phrase consists of one or more words, which are separated by one or more spaces. A word is a non-empty sequence of lowercase Latin letters. Leading and/or trailing spaces in phrases are allowed. Every phrase has at least one non-space character. See the example for clarification.

Print "YES" (without the quotes) if the poem is a haiku. Otherwise, print "NO" (also without the quotes).

on  codeforces
beta round is running
   a rustling of keys

how many gallons
of edo s rain did you drink
                                cuckoo

package main

import "fmt"
import "bufio"
import "os"

func main(){
	phrases := make([]string, 3)
	ans := []int{5, 7, 5}
	r := bufio.NewScanner(os.Stdin)
	for i:=0; i<3; i++ {
		count := 0
		r.Scan()
		phrases[i] = r.Text()
		for k:=0; k<len(phrases[i]); k++ {
			if string(phrases[i][k]) == "a" || string(phrases[i][k]) == "i" || string(phrases[i][k]) == "u" || string(phrases[i][k]) == "e" || string(phrases[i][k]) == "o" {
				count += 1
			}
		}
		if count != ans[i] {
			fmt.Println("NO")
			return
		}
	}
	fmt.Println("YES")
}

package main

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

var scanner *bufio.Scanner

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

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

  vowels := "aeiou"
  counts := make([]int, 3)
  for i := range counts {
    line, _ := reader.ReadString('\n')
    for _, ch := range line {
      if strings.IndexRune(vowels, ch) != -1 {
        counts[i]++
      }
    }
  }
  if counts[0] == 5 && counts[1] == 7 && counts[2] == 5 {
    writer.WriteString("YES\n")
  } else {
    writer.WriteString("NO\n")
  }
}

package main

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

func CountVowel(s string) int {
    cnt := 0
    for _, v := range s {
        if (v == 'a') || (v == 'e') || (v == 'i') || (v == 'o') || (v == 'u') {
            cnt++
        }
    }
    return cnt
}

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    var s [3]string
    for i := 0; i < 3; i++ {
        scanner.Scan()
        s[i] = scanner.Text()
    }
    if (CountVowel(s[0]) == 5) && (CountVowel(s[1]) == 7) && (CountVowel(s[2]) == 5) {
        fmt.Println("YES")
    } else {
        fmt.Println("NO")
    }
}

// 78A
package main

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

func main() {
	var s string
	in := bufio.NewScanner(os.Stdin)
	i, k, j, b := 0, 0, 0, 0
	for j = 0; j < 3; j++ {
		in.Scan()
		s = in.Text()
		for k = 0; k < len(s); k++ {
			if s[k] == 'a' || s[k] == 'o' || s[k] == 'u' || s[k] == 'e' || s[k] == 'i' {
				i++
			}
		}

		if j == 0 && i != 5 {
			b = 1
			break
		}
		if j == 1 && i != 7 {
			b = 1
			break
		}
		if j == 2 && i != 5 {
			b = 1
			break
		}
		i = 0
	}

	if b == 0 {
		fmt.Print("YES")
	}
	if b == 1 {
		fmt.Print("NO")
	}
}

package main
import (
    "fmt"
    "bufio"
    "os"
)
func main() {
    var b [3]int
    var s string
    input:= bufio.NewScanner(os.Stdin)
    for i:=0;i<3;i++ {
        input.Scan()
        s =input.Text()
        for j:=0;j<len(s);j++ {
            if s[j]=='a' || s[j]=='e' || s[j]=='o' || s[j]=='u' || s[j]=='i'{
                b[i]++
            }
        }
    }
    if b[0]==5 && b[1]==7 && b[2]==5 {
        fmt.Println("YES")
    } else {
        fmt.Println("NO")
    }
}

package main

import "fmt"
import "bufio"
import "os"

func main(){
	phrases := make([]string, 3)
	for i:=0; i<3; i++ {
		r := bufio.NewScanner(os.Stdin)
		r.Scan()
		phrases[i] = r.Text()
	}
	count := make([]int, 3)
	for j:=0; j<3; j++ {
		len := len(phrases[j])
		for k:=0; k<len; k++ {
			if string(phrases[j][k]) == "a" || string(phrases[j][k]) == "i" || string(phrases[j][k]) == "u" || string(phrases[j][k]) == "e" || string(phrases[j][k]) == "o"{
				count[j] += 1
			}
		}
	}
	if count[0] == 5 && count[1] == 7 && count[2] == 5 {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
		fmt.Println(count[0], count[1], count[2])
	}
}

package main

import "fmt"
import "bufio"
import "os"

func main(){
	phrases := make([]string, 3)
	for i:=0; i<3; i++ {
		r := bufio.NewScanner(os.Stdin)
		r.Scan()
		phrases[i] = r.Text()
	}
	count := make([]int, 3)
	for j:=0; j<3; j++ {
		len := len(phrases[j])
		for k:=0; k<len; k++ {
			if string(phrases[j][k]) == "a" || string(phrases[j][k]) == "i" || string(phrases[j][k]) == "u" || string(phrases[j][k]) == "e" || string(phrases[j][k]) == "o"{
				count[j] += 1
			}
		}
	}
	if count[0] == 5 && count[1] == 7 && count[2] == 5 {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}