You are given a sequence $$$a=[a_1,a_2,\dots,a_n]$$$ consisting of $$$n$$$ positive integers.Let's call a group of consecutive elements a segment. Each segment is characterized by two indices: the index of its left end and the index of its right end. Denote by $$$a[l,r]$$$ a segment of the sequence $$$a$$$ with the left end in $$$l$$$ and the right end in $$$r$$$, i.e. $$$a[l,r]=[a_l, a_{l+1}, \dots, a_r]$$$.For example, if $$$a=[31,4,15,92,6,5]$$$, then $$$a[2,5]=[4,15,92,6]$$$, $$$a[5,5]=[6]$$$, $$$a[1,6]=[31,4,15,92,6,5]$$$ are segments.We split the given sequence $$$a$$$ into segments so that:   each element is in exactly one segment;  the sums of elements for all segments are equal. For example, if $$$a$$$ = [$$$55,45,30,30,40,100$$$], then such a sequence can be split into three segments: $$$a[1,2]=[55,45]$$$, $$$a[3,5]=[30, 30, 40]$$$, $$$a[6,6]=[100]$$$. Each element belongs to exactly segment, the sum of the elements of each segment is $$$100$$$.Let's define thickness of split as the length of the longest segment. For example, the thickness of the split from the example above is $$$3$$$.Find the minimum thickness among all possible splits of the given sequence of $$$a$$$ into segments in the required way.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case is described by two lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the length of the sequence $$$a$$$. The second line of each test case contains exactly $$$n$$$ integers: $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — elements of the sequence $$$a$$$. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2000$$$.

For each test case, output one integer — the minimum possible thickness of a split of the sequence $$$a$$$ into segments. Note that there always exist a split, you can always consider whole sequence as one segment.

NoteThe split in the first test case is explained in the statement, it can be shown that it is optimal.In the second test case, it is possible to split into segments only by leaving a single segment. Then the thickness of this split is equal to the length of the entire sequence, that is, $$$4$$$.In the third test case, the optimal split will be $$$[10, 55], [35, 30], [65]$$$. The thickness of the split equals to $$$2$$$.In the fourth test case possible splits are:  $$$[4] + [1, 1, 1, 1] + [4]$$$;  $$$[4, 1, 1] + [1, 1, 4]$$$.

package main

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

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

func solve() string {
	var n int
	fmt.Fscan(in, &n)

	var sum int
	v := make([]int, n)
	for i := range v {
		fmt.Fscan(in, &v[i])
		sum += v[i]
	}

	var res = n
	for numOfGroups := 1; numOfGroups <= n; numOfGroups++ {
		if sum%numOfGroups != 0 {
			continue
		}

		target := sum / numOfGroups
		cnt, maxCnt, curSum := 0, 0, 0
		for j := 0; j < n; j++ {
			curSum += v[j]
			cnt++
			if curSum > target {
				maxCnt = n
				break
			}
			if curSum == target {
				if cnt > maxCnt {
					maxCnt = cnt
				}
				curSum = 0
				cnt = 0
			}
		}
		if maxCnt < res {
			res = maxCnt
		}
	}

	return strconv.Itoa(res)
}

func main() {
	defer out.Flush()
	var t int
	for fmt.Fscanln(in, &t); t > 0; t-- {
		fmt.Fprintln(out, solve())
	}
}

// util functions
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

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

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

func makeMatrix(n, m int) [][]int {
	x := make([][]int, n)
	for i := range x {
		x[i] = make([]int, m)
	}
	return x
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func solve() {
	var n int
	scanf("%d\n", &n)
	a := make([]int, n)
	for i:=0; i<n; i++ {
		scanf("%d ", &a[i])
	}
	scanf("\n")

	var res int = n
	prefSum := 0
	for i:=0; i<n; i++ {
		prefSum += a[i]
		j := i+1
		pos := true
		curSum := 0
		mxLen := i+1
		prev := i
		for j<n {
			curSum += a[j]
			if curSum > prefSum {
				pos = false
				break
			}
			if curSum == prefSum {
				curLen := j - prev
				if curLen > mxLen {
					mxLen = curLen
				}
				prev = j
				j++
				curSum = 0
				continue
			}
			j++
		}

		if pos && curSum == 0 {
			lastLen := j - prev
			if lastLen > mxLen {
				mxLen = lastLen
			}
			if mxLen < res {
				res = mxLen
			}
		}
	}
	printf("%d\n", res)
}

func main() {
	defer writer.Flush()

	var T int
	scanf("%d\n", &T)
	for tt:=1; tt<=T; tt++ {
		solve()
	}
}

package main

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

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

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

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

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

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

		a := make([]int, n)

		for i := range a {
			fmt.Fscan(r, &a[i])
		}

		m := n
		sum := 0
		for i := range a {
			sum += a[i]

			rm := 0
			rsum := 0
			rcnt := 0
			dont := false

			for _, j := range a {
				rsum += j
				rcnt++

				if rsum == sum {
					rm = max(rm, rcnt)
					rsum = 0
					rcnt = 0
				}

				if rsum > sum {
					dont = true
					break
				}
			}

			if rsum != 0 || dont {
				continue
			}

			m = min(m, rm)
		}
		fmt.Fprintln(w, m)
	}

}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var num int
	fmt.Scan(&num)
	for i := 0; i < num; i++ {

		var num1 int
		fmt.Scan(&num1)

		var numArr []int
		for i := 0; i < num1; i++ {
			var num2 int
			fmt.Scan(&num2)
			numArr = append(numArr, num2)
		}
		var res int
		res = thri(numArr)
		fmt.Println(res)
	}

}

// [2,6,10,11,19,22]
func thri(arr []int) int {
	pre := 0
	next := 1
	var prefixSum []int
	sum := 0
	for i := 0; i < len(arr); i++ {
		sum += arr[i]
		prefixSum = append(prefixSum, sum)
	}
	var maxSum int
	pre2 := 0
	var arrCount []int
	var realCount []int
	flag := false
	for pre < len(prefixSum) {
		maxSum = prefixSum[pre]

		for next < len(prefixSum) {
			if maxSum < prefixSum[next]-prefixSum[pre2] {
				break
			} else if maxSum > prefixSum[next]-prefixSum[pre2] {
				next++
			} else {
				if next == len(prefixSum)-1 {
					flag = true
				}
				arrCount = append(arrCount, next-pre2)
				pre2 = next
				next++
			}
		}

		if flag {
			arrCount = append(arrCount, pre+1)
			sort.Ints(arrCount)
			realCount = append(realCount, arrCount[len(arrCount)-1])

			arrCount = nil
			flag = false
		}

		pre++
		pre2 = pre
		next = pre2 + 1
	}
	sort.Ints(realCount)
	if len(realCount) == 0 {
		return len(arr)
	}
	return realCount[0]
}

package main

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

const mod = 998244353
const mx = int64(1e9 + 7)

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n int
	Fscan(in, &n)
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		Fscan(in, &arr[i])
	}
	sum := int64(0)
	ans := n + 1
	for i := 0; i < n; i++ {
		sum += arr[i]
		cur := int64(0)
		curMax := i + 1
		prev := i
		for j := i + 1; j < n; j++ {
			cur += arr[j]
			if cur == sum {
				curMax = max(j-prev, curMax)
				cur = 0
				prev = j
			}
		}
		if cur == 0 {
			ans = min(ans, curMax)
		}
	}
	if ans != n+1 {
		Fprintln(out, ans)
		return
	}
	Fprintln(out, 1)
}

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

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}
func gcd(a, b int) int {
	if a%b == 0 {
		return b
	}
	return gcd(b, a%b)
}

func main() {
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 1
	Fscan(in, &n)
	for i := 0; i < n; i++ {
		solve(in, out)
	}
}

package main

import (
	"fmt"
)

const (
	N = 2010
)

var t, n int
var a []int

func solve() {
	sum := 0
	for i := 0; i < n; i++ {
		sum += a[i]
	}
	total := 0x3f3f3f3f
	for cnt := n; cnt > 0; cnt-- {
		b := sum / cnt
		if sum%cnt != 0 {
			continue
		} else {
			now, tt, flag, ans := 0, 0, true, 0
			for i := 0; i < n; i++ {
				now += a[i]
				tt++

				if now == b {
					if ans < tt {
						ans = tt
					}
					now = 0
					tt = 0
				} else if now > b {
					flag = false
					break
				}
			}
			if flag {
				if total > ans {
					total = ans
				}
			}
		}
	}
	fmt.Println(total)
}

func main() {
	fmt.Scan(&t)
	for ; t != 0; t-- {
		fmt.Scan(&n)
		a = make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Scan(&a[i])
		}
		solve()
	}
}

package main

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

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	scanner := bufio.NewScanner(os.Stdin)
	// mod := int64(998244353998244353)
	for a := 0; a < t; a++ {
		if scanner.Scan() {
			scanner.Text()
		}
		var s string
		var arr []int
		if scanner.Scan() {
			s = scanner.Text()
			temp := strings.Split(s, " ")
			for _, v := range temp {
				x, _ := strconv.Atoi(v)
				arr = append(arr, x)
			}
		}
		n := len(arr)
		var thickness int = math.MaxInt32
		for i := 1; i <= n; i++ { // for each length of 1st segment
			var sum int
			for j := 0; j < i; j++ {
				sum += arr[j]
			}

			// find next segment with `sum`
			var curSum, curLength int
			maxLength := i
			var possible bool = true
			for j := i; j < n; j++ {
				curSum += arr[j]
				curLength++
				if curSum == sum { // go to next segment
					curSum = 0
					if curLength > maxLength {
						maxLength = curLength
					}
					curLength = 0
				} else if curSum > sum || j == n-1 { // not possible
					possible = false
					break
				}
			}

			if possible && maxLength < thickness {
				thickness = maxLength
			}
		}

		fmt.Println(thickness)
	}
}

package main

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

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

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

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

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

		var sum int64
		z := n
		for i := 0; i < n; i++ {
			sum += aa[i]
			f := true
			mx := i + 1
			var tally int64
			start := i
			for j := i + 1; j < n; j++ {
				tally += aa[j]
				if tally > sum {
					f = false
					break
				} else if tally == sum {
					mx = max(mx, j-start)
					tally = 0
					start = j
				}
			}

			if f && tally == 0 {
				z = min(z, mx)
			}
		}

		fmt.Fprintln(out, z)
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)

const INF = 1000000000
const N = 2e5 + 5

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

	T := nextInt()
	for T > 0 {
		T--
		n := nextInt()
		a := make([]int, n)
		sum := 0
		for i := 0; i < n; i++ {
			a[i] = nextInt()
			sum += a[i]
		}
		f := func(m int) (bool, int) {
			if sum%m != 0 {
				return false, 0
			}
			res := 0
			for i, t, s := 0, 0, 0; i < n; i++ {
				if t+a[i] < sum/m {
					t += a[i]
				} else if t+a[i] == sum/m {
					t = 0
					res = max(res, i-s+1)
					s = i + 1
				} else {
					return false, 0
				}
			}
			return true, res
		}
		ans := INF
		for i := 1; i <= n; i++ {
			b, v := f(i)
			if b {
				ans = min(ans, v)
			}
		}
		fmt.Println(ans)
	}
}

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

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

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

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

package main

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

var a [2010]int
var n int

func calc(len, sum int, out *bufio.Writer) int {
	mx := len
// 	if len == 2 {
// 		Fprintln(out, "test ", sum)
// 	}
	for i := len + 1; i <= n; {
		cur, idx := 0, -1
		for j := i; j <= n; j++ {
			cur += a[j]
			if cur == sum {
				idx = j
				mx = max(mx, j-i+1)
				break
			} else if cur > sum {
				return n + 1
			}
		}
// 		if len == 2 {
// 		    Fprintln(out, cur, " ", idx, " ", mx)
// 		}
		if idx == -1 {
			return n + 1
		}
		i = idx + 1
	}
	return mx
}

func solve(_r io.Reader, _w io.Writer) {

	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T int
	Fscan(in, &T)
	for ; T > 0; T-- {
		Fscan(in, &n)
		res := n
		for i := 1; i <= n; i++ {
			Fscan(in, &a[i])
		}
		for i := 1; i <= n; i++ {
			sum, len := 0, i
			for j := 1; j <= i; j++ {
				sum += a[j]
			}
			res = min(res, calc(len, sum, out))
		}
		Fprintln(out, res)
	}

}

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

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

func gcd(x, y int) int {
	if y == 0 {
		return x
	}
	return gcd(y, x%y)
}

func lcm(x, y int) int {
	return x / gcd(x, y) * y
}

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

package main

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

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

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

	var T int
	var n int
	var x int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)

		r := make([]int, n)
		s := 0
		d := map[int]int{}
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			r[i] = x
			s += x
			d[s] = i
		}

		//Fprintln(out, r)
		//Fprintln(out, d)

		ans := n
		t := 0

	loop:
		for i := 0; i < n; i++ {
			t += r[i]
			if s%t == 0 {
				mx := i+1
				l := 0
				for nx := t; nx <= s; nx += t {
					if _, ok := d[nx]; !ok {
						continue loop
					}
					if d[nx] - l  > mx {
						mx = d[nx]-l
					}
					l = d[nx]
				}
				//Fprintln(out, i, mx)
				if mx < ans {
					ans = mx
				}
			}
		}

		Fprintln(out, ans)
	}
}

package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	for i := range arr {
		fmt.Fscan(in, &arr[i])
	}
	curSum := 0
	ans := n
	for i := range arr {
		curSum += arr[i]
		success := true
		curAns := i + 1
		localSum := 0
		localAns := 0
		for i := i + 1; i < n; i++ {
			localSum += arr[i]
			localAns++
			if localSum > curSum {
				success = false
				break
			} else if localSum == curSum {
				localSum = 0
				curAns = max(curAns, localAns)
				localAns = 0
			}
		}
		if localSum == 0 && success {
			ans = min(ans, curAns)
		}
	}
	fmt.Fprintln(out, ans)
}

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

type SqrtInt interface {
	SignedSqrt | UnsignedSqrt
}

type SignedSqrt interface {
	~int | ~int32 | ~int64
}

type UnsignedSqrt interface {
	~uint | ~uint32 | ~uint64 | ~uintptr
}

func sqrt[T SqrtInt](x T) T {
	l, r := T(0), T(2000000123)
	for l < r {
		mid := (l + r) / 2
		if mid*mid > x {
			r = mid
		} else {
			l = mid + 1
		}
	}
	return l - 1
}

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

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

package main

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

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

func readInt() int {
	var i int
	fmt.Fscan(reader, &i)
	return i
}

func max(u, v int) int {
	if u < v {
		u = v
	}

	return u
}

func min(u, v int) int {
	if u > v {
		u = v
	}

	return u
}

func main(){
	defer writer.Flush()
	T := readInt()

	for ;T > 0; T-- {
		n := readInt()
		a := make([]int, n)
		for i := 0; i < n; i++ {
			a[i] = readInt()
		}
		ans := n
		cur := 0
		firstMove := 1

		for ;firstMove <= n; firstMove++ {
			cur += a[firstMove - 1]
			c, cnt, l := 0, 0, firstMove
			complete := true

			for i := firstMove; i < n; i++ {
				c += a[i]
				cnt++
				if c == cur {
					l = max(l, cnt)
					cnt = 0
					c = 0
				}
				if c > cur {
					complete = false
					break
				}
			}

			complete = complete && (c == 0)
			if complete {
				ans = min(ans, l)
			}
		}
		fmt.Fprintln(writer, ans)
	}
}

package main

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

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

func solve() string {
	var n int
	fmt.Fscan(in, &n)

	var sum int
	v := make([]int, n)
	for i := range v {
		fmt.Fscan(in, &v[i])
		sum += v[i]
	}

	var res = n
	for numOfGroups := 1; numOfGroups < n; numOfGroups++ {
		if sum%numOfGroups != 0 {
			continue
		}

		target := sum / numOfGroups
		cnt, maxCnt, curSum := 0, 0, 0
		for j := 0; j < n; j++ {
			curSum += v[j]
			cnt++
			if curSum > target {
				maxCnt = n
				break
			}
			if curSum == target {
				if cnt > maxCnt {
					maxCnt = cnt
				}
				curSum = 0
				cnt = 0
			}
		}
		if maxCnt < res {
			res = maxCnt
		}
	}

	return strconv.Itoa(res)
}

func main() {
	defer out.Flush()
	var t int
	for fmt.Fscanln(in, &t); t > 0; t-- {
		fmt.Fprintln(out, solve())
	}
}

// util functions
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

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

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

func makeMatrix(n, m int) [][]int {
	x := make([][]int, n)
	for i := range x {
		x[i] = make([]int, m)
	}
	return x
}

package main

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

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

func solve() string {
	var n int
	fmt.Fscan(in, &n)

	var sum int
	v := make([]int, n)
	for i := range v {
		fmt.Fscan(in, &v[i])
		sum += v[i]
	}

	var res = n
	for i := 1; i < n; i++ {
		if sum%i != 0 {
			continue
		}

		target := sum / i
		cnt, maxCnt, curSum := 0, 0, 0
		for j := 0; j < n; j++ {
			curSum += v[j]
			cnt++
			if curSum == target {
				curSum = 0
				if cnt > maxCnt {
					maxCnt = cnt
				}
				cnt = 0
			}
			if curSum > target {
				maxCnt = 2001
				break
			}
		}
		if maxCnt < res {
			res = maxCnt
		}
	}

	return strconv.Itoa(res)
}

func main() {
	defer out.Flush()
	var t int
	for fmt.Fscanln(in, &t); t > 0; t-- {
		fmt.Fprintln(out, solve())
	}
}

// util functions
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

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

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

func makeMatrix(n, m int) [][]int {
	x := make([][]int, n)
	for i := range x {
		x[i] = make([]int, m)
	}
	return x
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func solve() {
	var n int
	scanf("%d\n", &n)
	a := make([]int, n)
	for i:=0; i<n; i++ {
		scanf("%d ", &a[i])
	}
	scanf("\n")

	var res int = n
	prefSum := 0
	for i:=0; i<n; i++ {
		prefSum += a[i]
		j := i+1
		pos := true
		curSum := 0
		mxLen := i+1
		prev := i
		for j<n {
			curSum += a[j]
			if curSum > prefSum {
				pos = false
				break
			}
			if curSum == prefSum {
				curLen := j - prev
				if curLen > mxLen {
					mxLen = curLen
				}
				j++
				curSum = 0
				prev = j
				continue
			}
			j++
		}

		if pos && curSum == 0 {
			lastLen := j - prev
			if lastLen > mxLen {
				mxLen = lastLen
			}
			if mxLen < res {
				res = mxLen
			}
		}
	}
	printf("%d\n", res)
}

func main() {
	defer writer.Flush()

	var T int
	scanf("%d\n", &T)
	for tt:=1; tt<=T; tt++ {
		solve()
	}
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func solve() {
	var n int
	scanf("%d\n", &n)
	a := make([]int, n)
	for i:=0; i<n; i++ {
		scanf("%d ", &a[i])
	}
	scanf("\n")

	var res int = n
	prefSum := 0
	for i:=0; i<n; i++ {
		prefSum += a[i]
		j := i+1
		pos := true
		curSum := 0
		mxLen := i+1
		prev := i
		for j<n {
			curSum += a[j]
			if curSum > prefSum {
				pos = false
				break
			}
			if curSum == prefSum {
				curLen := j - prev
				if curLen > mxLen {
					mxLen = curLen
				}
				j++
				curSum = 0
				prev = j
				continue
			}
			j++
		}

		if pos && curSum == 0 {
			if mxLen < res {
				res = mxLen
			}
		}
	}
	printf("%d\n", res)
}

func main() {
	defer writer.Flush()

	var T int
	scanf("%d\n", &T)
	for tt:=1; tt<=T; tt++ {
		solve()
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var num int
	fmt.Scan(&num)
	for i := 0; i < num; i++ {

		var num1 int
		fmt.Scan(&num1)

		var numArr []int
		for i := 0; i < num1; i++ {
			var num2 int
			fmt.Scan(&num2)
			numArr = append(numArr, num2)
		}
		var res int
		res = thri(numArr)
		fmt.Println(res)
	}

}

func thri(arr []int) int {
	pre := 0
	next := 1
	var prefixSum []int
	sum := 0
	for i := 0; i < len(arr); i++ {
		sum += arr[i]
		prefixSum = append(prefixSum, sum)
	}
	var maxSum int
	pre2 := 0
	var arrCount []int
	var realCount []int
	flag := false
	for pre < len(prefixSum) {
		maxSum = prefixSum[pre]

		for next < len(prefixSum) {
			if maxSum < prefixSum[next]-prefixSum[pre2] {
				break
			} else if maxSum > prefixSum[next]-prefixSum[pre2] {
				next++
			} else {
				if next == len(prefixSum)-1 {
					flag = true
				}
				arrCount = append(arrCount, next-pre2)
				pre2 = next
				next++
			}
		}

		if flag {
			sort.Ints(arrCount)
			realCount = append(realCount, arrCount[len(arrCount)-1])

			arrCount = nil
			flag = false
		}

		pre++
		pre2 = pre
		next = pre2 + 1
	}
	sort.Ints(realCount)
	if len(realCount) == 0 {
		return len(arr)
	}

	return realCount[0]
}


There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.

The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.  The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.

In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.  Print the only integer -1, if the collision of particles doesn't happen.

NoteIn the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.

package main

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

var n int
var s string
var a []int

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

	fmt.Fscan(buf, &n)
	fmt.Fscan(buf, &s)

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

	tmin := -1
	for i := 1; i < n; i++ {
		if (s[i-1] == 'R') && (s[i] == 'L') {
			t := (a[i] - a[i-1]) / 2
			if (tmin == -1) || (t < tmin) {
				tmin = t
			}
		}
	}

	fmt.Println(tmin)
}

package main

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

var n int
var s string
var a []int

func main() {
	buf := bufio.NewReaderSize(os.Stdin, 64*1024)
	fmt.Fscan(buf, &n)
	fmt.Fscan(buf, &s)

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

	res := -1
	p := -1

	for i := 0; i < n; i++ {
		if s[i] == 'R' {
			p = i
		} else if p >= 0 {
			t := (a[i] - a[p]) / 2
			if (res == -1) || (t < res) {
				res = t
			}
		}

	}

	fmt.Println(res)
}

package main

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

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	var directions string
	coordinates := make([]int, n)
	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)

	if _d, err := reader.ReadString('\n'); err != nil {
		if err != io.EOF {
			return
		}
	} else {
		directions = re.ReplaceAllString(_d, "")
	}

	if _c, err := reader.ReadString('\n'); err != nil {
		if err != io.EOF {
			return
		}
	} else {
		_c := re.ReplaceAllString(_c, "")
		ss := strings.Split(_c, " ")
		for i, s := range ss {
			x, err := strconv.Atoi(s)
			if err != nil {
				return
			}
			coordinates[i] = x
		}
	}

	min_distance := -1
	for i := 0; i < n-1; i++ {
		if directions[i] == 'R' && directions[i+1] == 'L' {
			dist := (coordinates[i+1] - coordinates[i]) / 2
			if dist < min_distance || min_distance < 0 {
				min_distance = dist
			}
		}
	}

	fmt.Println(min_distance)
}

package main

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

func readInt(buffer *bufio.Reader) (result int) {
	var negative, started bool
READER_LOOP:
	for {
		char, err := buffer.ReadByte()
		switch {
		case err != nil:
			break READER_LOOP
		case char == '-':
			negative = true
		case unicode.IsDigit(rune(char)):
			started = true
			result = result*10 + int(char&0xf)
		case started:
			break READER_LOOP
		}
	}
	if negative {
		result = -result
	}
	return
}

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

	var n int
	fmt.Fscanf(buffer, "%d\n", &n)
	var str string
	for {
		pref, isPrefix, err := buffer.ReadLine()
		if err != nil {
			panic(err)
		}
		str += string(pref)
		if !isPrefix {
			break
		}
	}
	if len(str) != n {
		panic(fmt.Errorf("Expected %d characters but got %d", n, len(str)))
	}

	maj := make([]int, n)
	for i := range maj {
		maj[i] = readInt(buffer)
	}

	best := -1
	for i := 1; i < n; i++ {
		if str[i-1] == 'R' && str[i] == 'L' {
			if prop := (maj[i] - maj[i-1]) / 2; best < 0 || best > prop {
				best = prop
			}
		}
	}
	fmt.Println(best)
}

package main

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

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

	xn := make([]int, n, n)
	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		bs.Scan()
		xn[i], _ = strconv.Atoi(bs.Text())
	}

	pre := -1
	ans := -1
	for i := 0; i < n; i++ {
		if moves[i] == 'R' {
			pre = i
		} else {
			if pre != -1 && (ans == -1 || ans > (xn[i]-xn[pre])/2) {
				ans = (xn[i] - xn[pre]) / 2
			}
		}
	}

	fmt.Println(ans)
}

package main

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

func readInt(buffer *bufio.Reader) (result int) {
	var negative, started bool
READER_LOOP:
	for {
		char, err := buffer.ReadByte()
		switch {
		case err != nil:
			break READER_LOOP
		case char == '-':
			negative = true
		case unicode.IsDigit(rune(char)):
			started = true
			result = result*10 + int(char&0xf)
		case started:
			break READER_LOOP
		}
	}
	if negative {
		result = -result
	}
	return
}

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

	var n int
	fmt.Fscanf(buffer, "%d\n", &n)
	str, isPrefix, err := buffer.ReadLine()
	if err != nil {
		panic(err)
	}
	if isPrefix {
		panic("Expected full line but found prefix")
	}
	if len(str) != n {
		panic(fmt.Errorf("Expected %d characters but got %d", n, len(str)))
	}

	maj := make([]int, n)
	for i := range maj {
		maj[i] = readInt(buffer)
	}

	for i := 1; i < n; i++ {
		if str[i-1] == 'R' && str[i] == 'L' {
			fmt.Println((maj[i] - maj[i-1]) / 2)
			return
		}
	}
	fmt.Println(-1)
}

package main

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

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

	xn := make([]int, n, n)
	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		bs.Scan()
		xn[i], _ = strconv.Atoi(bs.Text())
	}

	pre := -1
	ans := -1
	for i := 0; i < n; i++ {
		if moves[i] == 'R' {
			pre = i
		} else {
			if pre != -1 && (ans == -1 || ans > xn[i]-xn[pre]) {
				ans = (xn[i] - xn[pre])
			}
		}
	}

	fmt.Println(ans / 2)
}

package main

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

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

	xn := make([]int, n, n)
	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		bs.Scan()
		xn[i], _ = strconv.Atoi(bs.Text())
	}

	pre := -1
	ans := -1
	for i := 0; i < n; i++ {
		if moves[i] == 'R' {
			pre = i
		} else {
			if pre != -1 && (ans == -1 || ans > xn[i]-xn[pre]) {
				ans = (xn[i] - xn[pre]) / 2
			}
		}
	}

	fmt.Println(ans)
}

package main

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

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

	xn := make([]int, n, n)
	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		bs.Scan()
		xn[i], _ = strconv.Atoi(bs.Text())
	}

	pre := -1
	ans := -1
	for i := 0; i < n; i++ {
		if moves[i] == 'R' {
			pre = i
		} else {
			if pre != -1 && (ans == -1 || ans > i-pre) {
				ans = (xn[i] - xn[pre]) / 2
			}
		}
	}

	fmt.Println(ans)
}

package main

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

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

	xn := make([]int, n, n)
	bs := bufio.NewScanner(os.Stdin)
	bs.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		bs.Scan()
		xn[i], _ = strconv.Atoi(bs.Text())
	}

	pre := -1
	ans := -1
	for i := 0; i < n; i++ {
		if moves[i] == 'R' {
			pre = i
		} else {
			if pre != -1 && (ans == -1 || ans > (xn[i]-xn[pre]/2)) {
				ans = (xn[i] - xn[pre]) / 2
			}
		}
	}

	fmt.Println(ans)
}


Today, as a friendship gift, Bakry gave Badawy $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ and challenged him to choose an integer $$$X$$$ such that the value $$$\underset{1 \leq i \leq n}{\max} (a_i \oplus X)$$$ is minimum possible, where $$$\oplus$$$ denotes the bitwise XOR operation.As always, Badawy is too lazy, so you decided to help him and find the minimum possible value of $$$\underset{1 \leq i \leq n}{\max} (a_i \oplus X)$$$.

The first line contains integer $$$n$$$ ($$$1\le n \le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 2^{30}-1$$$).

Print one integer — the minimum possible value of $$$\underset{1 \leq i \leq n}{\max} (a_i \oplus X)$$$.

NoteIn the first sample, we can choose $$$X = 3$$$.In the second sample, we can choose $$$X = 5$$$.

package main
import (
	"fmt"
	"bufio"
	"os"
)

func solve(v []int, bit int) int64 {
	if bit < 0 {
		return 0
	}
	var on, off []int
	for i := 0; i < len(v); i++ {
		x := (1 << bit) & v[i]
		if x == 0 {
			off = append(off, v[i])
		} else {
			on = append(on, v[i])
		}
	}
	if len(on) == 0 || len(off) == 0 {
		return solve(v, bit - 1)
	}
	x1 := solve(on, bit - 1)
	x2 := solve(off, bit - 1)
	if x1 < x2 {
		return x1 + (1 << bit)
	}
	return x2 + (1 << bit)
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n, x int
	fmt.Fscanf(in, "%d\n", &n)
	var arr []int
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d ", &x)
		arr = append(arr, x)
	}
	fmt.Fprintf(out, "%v\n", solve(arr, 30))
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	//fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")
		fmt.Fprintf(out, "%d\n", solve(a, 31))
	}
	return
}

func solve(a []int64, pos int64) int64 {
	//fmt.Println(a, pos)
	if pos == 0 {
		for i := 1; i < len(a); i++ {
			if a[i]&1 != a[0]&1 {
				return 1
			}
		}
		return 0
	}
	b := []int64{}
	c := []int64{}
	cur := int64(1) << uint64(pos)
	for i := 0; i < len(a); i++ {
		if a[i]&cur > 0 {
			b = append(b, a[i])
		} else {
			c = append(c, a[i])
		}
	}
	if len(b) == 0 {
		return solve(c, pos-1)
	}
	if len(c) == 0 {
		return solve(b, pos-1)
	}
	return cur + min(solve(b, pos-1), solve(c, pos-1))
}

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

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"sync"
)
func gcd(a,b int64)int64{
	if(a==0){
		return b
	}
	return gcd(b%a,a)
}
func lcm(a,b int64)int64{
	return a/gcd(a,b)*b
}
func min(a,b int64)int64{
	if(a>b){
		return b
	}else{
		return a
	}
}
func max(a,b int64)int64{
	if(a>b){
		return a
	}
	return b
}
func dfs(x int64,a []int64)int64{
	if(x<0){
		return 0
	}
	var L,R []int64
	Len:=len(a)
	for i:=0;i<Len;i++{
		if (a[i]&(1<<uint64(x)))>0 {
			L=append(L, a[i])
		}else{
			R=append(R,a[i])
		}
	}
	if(len(L)==0){
		return dfs(x-1,R)
	}
	if(len(R)==0){
		return dfs(x-1,L)
	}
	return min(dfs(x-1,L),dfs(x-1,R))+(1<<uint64(x))
}
func main(){
	var n int64
	fmt.Scan(&n)
	a:=make([]int64,n)
	for i:=int64(0);i<n;i++{
		a[i]=ReadInt64()
	}
	fmt.Println(dfs(30,a))
}

/*********************** utility ***********************/

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

func init() {
	//set input
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(2e+5))
	in.Split(bufio.ScanWords)

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

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

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

func ReadString() string {
	in.Scan()
	return in.Text()
}

func ReadStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ReadString()
	}
	return s
}

func ReadInt64() int64 {
	intStr := ReadString()
	i, _ := strconv.ParseInt(intStr,10,64)
	return i
}

func ReadIntSlice(n int64) []int64 {
	arr := make([]int64, n)
	for i := int64(0); i < n; i++ {
		arr[i] = ReadInt64()
	}
	return arr
}

package main

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

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

func scanInt() int {
	sc.Scan()
	a, _ := strconv.Atoi(sc.Text())
	return a
}

func scanInt64() int64 {
	sc.Scan()
	a, _ := strconv.ParseInt(sc.Text(), 10, 64)
	return a
}

func scanInts(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = scanInt()
	}
	return res
}

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

func printInts(a ...int) {
	for i, e := range a {
		fmt.Fprint(wr, e)
		if i != len(a)-1 {
			fmt.Fprint(wr, " ")
		}
	}
	fmt.Fprintln(wr)
	wr.Flush()
}

const LOG = 30

var (
	n       int
	a       []int
	ch      [][2]int
	trieCnt int
	root    int
)

func insert(x uint) {
	u := root
	for i := uint(LOG - 1); i >= 0; i-- {
		if ch[u][x>>i&1] == 0 {
			trieCnt++
			ch[u][x>>i&1] = trieCnt
		}
		u = ch[u][x>>i&1]
		if i == 0 {
			break
		}
	}
}

func check(x uint, u uint, d int) bool {
	if d < 0 {
		return true
	}
	if u == 0 && d != LOG-1 {
		return true
	}
	if x>>uint(d)&1 == 1 {
		return check(x, uint(ch[u][0]), d-1) || check(x, uint(ch[u][1]), d-1)
	}
	if ch[u][0] != 0 && ch[u][1] != 0 {
		return false
	}
	return check(x, uint(ch[u][0])|uint(ch[u][1]), d-1)
}

func main() {
	sc.Split(bufio.ScanWords)
	n = scanInt()
	a = scanInts(n)
	ch = make([][2]int, n*LOG+1)
	for _, x := range a {
		insert(uint(x))
	}
	low, high := uint(0), (uint(1)<<30)-1
	mid, ans := uint(0), high

	for low <= high && ans != 0 {
		mid = (low + high)>>1
		if check(mid, uint(root), LOG-1) {
			ans = mid
			high = mid - 1
		} else {
			low = mid + 1
		}
	}
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"sync"
)
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
var out chan string
var in *bufio.Scanner
var outWg *sync.WaitGroup
func ReadString() string {
	in.Scan()
	return in.Text()
}
func ReadStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ReadString()
	}
	return s
}
func ReadInt() int {
	intStr := ReadString()
	i, _  := strconv.Atoi(intStr)
	return i
}
func ReadIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt()
	}
	return arr
}
func init() {
	//set input
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(2e+5))
	in.Split(bufio.ScanWords)

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

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

		for line := range out {
			write.WriteString(line + "\n")
		}
	}(writer)
}
const maxn int = 100005
var ch [maxn*30][2]int
var sz int = 0
var ans int = 1 << 30
func dfs(o, pos, val int) {
	if pos < 0 {
		ans = min(ans, val)
		return
	}
	if (ch[o][0] > 0 && ch[o][1] > 0) {
		dfs(ch[o][0], pos - 1, val | int(1) << uint(pos))
		dfs(ch[o][1], pos - 1, val | int(1) << uint(pos))
	} else if (ch[o][1] > 0) {
		dfs(ch[o][1], pos - 1, val)
	} else {
		dfs(ch[o][0], pos - 1, val)
	}
}
func work() {
	n := ReadInt()
	a := ReadIntSlice(n)
	for i := 0; i < n * 30; i++ {
		ch[i][0], ch[i][1] = 0, 0
	}
	for i := 0; i < n; i++ {
		o := 0
		for j := 29; j >= 0; j-- {
			to := 0
			if (a[i] >> uint(j) & 1) > 0 {
				to = 1
			}
			if ch[o][to] == 0 {
				sz++
				ch[o][to] = sz
			}
			o = ch[o][to]
		}
	}
	dfs(0, 29, 0)
	fmt.Println(ans)

}
func main() {

	work()
	close(out)
}


After celebrating the midcourse the students of one of the faculties of the Berland State University decided to conduct a vote for the best photo. They published the photos in the social network and agreed on the rules to choose a winner: the photo which gets most likes wins. If multiple photoes get most likes, the winner is the photo that gets this number first.Help guys determine the winner photo by the records of likes.

The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the total likes to the published photoes.  The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 1 000 000), where ai is the identifier of the photo which got the i-th like.

Print the identifier of the photo which won the elections.

NoteIn the first test sample the photo with id 1 got two likes (first and fifth), photo with id 2 got two likes (third and fourth), and photo with id 3 got one like (second). Thus, the winner is the photo with identifier 2, as it got:  more likes than the photo with id 3;  as many likes as the photo with id 1, but the photo with the identifier 2 got its second like earlier.

package main

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

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

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

func PrintInts(ints ...int) {
	for _, value := range ints {
		writer.WriteString(strconv.Itoa(value))
		//writer.WriteByte(' ')
	}
}

func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	var cnt int = ReadInt32()
	var photos map[int]int
	photos = make(map[int]int)
	res := make(map[int][]int)
	top := 0

	for c := 0; c < cnt; c++ {
		like := ReadInt32()

		photos[like] = photos[like] + 1
		//if !res[photos[like]] {
		//	res[photos[like]] = make([]int, 1)
		//}
		res[photos[like]] = append(res[photos[like]], like)
		if top < photos[like] {
			top = photos[like]
		}
	}
	PrintInts(res[top][0])
	writer.WriteByte('\n')
}

package main

import "fmt"

func main()  {
  var N int
  fmt.Scanf("%d\n", &N)
  L := make(map[int]int)

  ret := 0
  max := 0

  for i := 0; i < N; i++ {
    var a int
    fmt.Scanf("%d", &a)
    L[a]++

    if L[a] > max {
      max = L[a]
      ret = a
    }
  }

  fmt.Println(ret)
}

package main

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

func main() {
	defer out.Flush()
	n := in.NextInt()
	count := map[int]int{}
	time := map[int]int{}
	for i := 0; i < n; i++ {
		a := in.NextInt()
		count[a]++
		time[a] = i
	}
	ans := 0
	for a, c := range count {
		if c > count[ans] || (c == count[ans] && time[a] < time[ans]) {
			ans = a
		}
	}
	fmt.Fprintln(out, ans)
}

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

type FastReader struct {
	r   *bufio.Reader
	buf string
}

func NewFastReader(r io.Reader) *FastReader {
	return &FastReader{
		r: bufio.NewReader(r),
	}
}

func (r *FastReader) advance() {
	for len(r.buf) != 0 && r.buf[0] == ' ' {
		r.buf = r.buf[1:]
	}
	for len(r.buf) == 0 {
		var buf bytes.Buffer
		for {
			chunk, more, _ := r.r.ReadLine()
			buf.Write(chunk)
			if !more {
				break
			}
		}
		r.buf = buf.String()
	}
}

func (r *FastReader) NextLine() string {
	r.advance()
	line := r.buf
	r.buf = ""
	return line
}

func (r *FastReader) NextWord() string {
	r.advance()
	var word string
	wordStart := 0
	for i := 0; i < len(r.buf); i++ {
		b := r.buf[i]
		if b == ' ' {
			if i != wordStart {
				word = r.buf[wordStart:i]
				r.buf = r.buf[i:]
				break
			}
			wordStart = i + 1
		}
	}
	if len(word) == 0 {
		word = r.buf
		r.buf = ""
	}
	return word
}

func (r *FastReader) NextInt() int {
	return parseInt(r.NextWord())
}

func parseInt(word string) int {
	sign := 1
	if word[0] == '-' {
		sign = -1
		word = word[1:]
	}
	result := 0
	for i := 0; i < len(word); i++ {
		result = result*10 + int(word[i]) - '0'
	}
	result *= sign
	return result
}

package main

import (
	"fmt"
)

func main() {
	var n, max, winner int
	fmt.Scan(&n)
	var poll = make(map[int]int, n)

	for i := 0; i < n; i++ {
		var vote int
		fmt.Scan(&vote)
		poll[vote]++
		if poll[vote] > max {
			max = poll[vote]
			winner = vote
		}
	}
	fmt.Println(winner)
}


Polycarp has an array $$$a$$$ consisting of $$$n$$$ integers.He wants to play a game with this array. The game consists of several moves. On the first move he chooses any element and deletes it (after the first move the array contains $$$n-1$$$ elements). For each of the next moves he chooses any element with the only restriction: its parity should differ from the parity of the element deleted on the previous move. In other words, he alternates parities (even-odd-even-odd-... or odd-even-odd-even-...) of the removed elements. Polycarp stops if he can't make a move.Formally:   If it is the first move, he chooses any element and deletes it;  If it is the second or any next move:   if the last deleted element was odd, Polycarp chooses any even element and deletes it;  if the last deleted element was even, Polycarp chooses any odd element and deletes it.   If after some move Polycarp cannot make a move, the game ends. Polycarp's goal is to minimize the sum of non-deleted elements of the array after end of the game. If Polycarp can delete the whole array, then the sum of non-deleted elements is zero.Help Polycarp find this value.

The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements of $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.

Print one integer — the minimum possible sum of non-deleted elements of the array after end of the game.

package main

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

func main() {
	var n, x, cnt, cnt1, sum int
	scr := bufio.NewReader(os.Stdin)
	// ocr := bufio.NewWriter(os.Stdout)

	fmt.Fscanln(scr, &n)
	even := make([]int, 0, n)
	odd := make([]int, 0, n)

	for i := 0; i < n; i++ {
		fmt.Fscanf(scr, "%d", &x)
		if x%2 == 0 {
			even = append(even, x)
		} else {
			odd = append(odd, x)
		}
	}

	sort.Sort(sort.IntSlice(odd))
	sort.Sort(sort.IntSlice(even))

	if len(odd) > len(even)+1 {
		cnt = len(odd) - len(even) - 1
	} else if len(even) > len(odd)+1 {
		cnt1 = len(even) - len(odd) - 1
	}
	if cnt > 0 {
		for i := 0; i < cnt; i++ {
			sum += odd[i]
		}
		fmt.Println(sum)
	} else if cnt1 > 0 {
		for i := 0; i < cnt1; i++ {
			sum += even[i]
		}
		fmt.Println(sum)
	} else {
		fmt.Println(0)
	}

}

package main

import (
. "fmt"
"sort"
)

func main() {
    var N int
    Scan(&N)
    ganjil := []int{}
    genap := []int{}
    for i := 0; i < N; i++ {
        var x int
        Scan(&x)
        if x%2 == 1 {
            ganjil = append(ganjil, x)
        } else {
            genap = append(genap, x)
        }
    }
    if len(ganjil) - len(genap) > 1 {
        sum := 0
        sort.Ints(ganjil)
        for i := 0; i < len(ganjil) - len(genap) - 1; i++ {
            sum += ganjil[i]
        }
        Print(sum)
    } else if len(genap) - len(ganjil) > 1 {
        sum := 0
        sort.Ints(genap)
        for i := 0; i < len(genap) - len(ganjil) - 1; i++ {
            sum += genap[i]
        }
        Print(sum)
    } else {
        Print("0")
    }
}

package main

import (
	"fmt"
	"sort"
)

func min(a,b int) int  {
	if(a < b){
		return a
		}
	return b
}
func katta(a,b int) int  {
	if(a < b){
		return b
	}
	return a
}
func ab(x int) int {
	if(x <= -1){
		x *= -1
	}
	return x
}

func main()  {

	var n int
	fmt.Scan(&n)
	m := make([]int,n)
	jf := 0
	for i := 0; i < n;i++{
		fmt.Scan(&m[i])
		if(m[i] % 2 == 0){
			jf ++
		}
	}
	toq := n - jf
	cnt := min(toq,jf)
	temp := cnt
	sort.Ints(m)
	for i := n - 1 ; i >= 0  && temp >= 1;i --{
		if(m[i] % 2 == 0 && m[i] >= 1){
			m[i] = 0
			temp --
		}
	}
	temp = cnt
	for i := n - 1 ;i >= 0 && temp >= 1 ; i--{
		if(m[i] % 2 != 0){
			m[i] = 0
			temp --
		}
	}
	for i := n - 1 ;i >= 0 ; i --{
		if(m[i] >= 1){
			m[i] = 0
			break
		}
	}
	ans := 0
	for i := 0 ; i < n;i ++{
		ans += m[i]
	}
	fmt.Println(ans)

}

package main

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

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 getLine() string {
	s, _ := reader.ReadString('\n')
	for len(s) > 0 && (s[len(s)-1] == '\n' || s[len(s)-1] == '\r') {
		s = s[:len(s)-1]
	}
	return s
}

func main() {
	// STDOUT MUST BE FLUSHED MANUALLY!!!
	defer writer.Flush()

	var n int
	scanf("%d\n", &n)

	var odda []int
	var evena []int
	for ; n >= 1; n-- {
		var a int
		scanf("%d", &a)
		if a%2 == 0 {
			evena = append(evena, a)
		} else {
			odda = append(odda, a)
		}
	}

	var longer []int
	var diffc int
	if len(odda)-len(evena) >= 0 {
		longer = odda
		diffc = len(odda) - len(evena)
	} else {
		longer = evena
		diffc = len(evena) - len(odda)
	}

	var sum int
	if diffc > 1 {
		sort.Ints(longer)
		for i := 0; i < diffc-1; i++ {
			sum += longer[i]
		}
	}
	printf("%d\n", sum)

}

// Codeforces 1144 B compet
package main

import (
	"bufio"
	"fmt"
	"strings"
	"strconv"
	"os"
	"sort"
)
var in *bufio.Reader

func readFive() (int,int,int,int,int) {
	var a [5]int
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for i:=0; i<len(ss); i++ {
		a[i], err = strconv.Atoi(ss[i])
		if err != nil {
			fmt.Println("n atoi failure", err)
			panic(err)
		}
	}
	return a[0],a[1],a[2],a[3],a[4]
}

func readLineNumbs(n int) []int {
	a := make([]int, n)
	delim := byte(' ')
	for i:=0; i<n; i++ {
		if i==(n-1) {
			delim = byte('\n')
		}
		s, err := in.ReadString(delim)
		s = strings.Trim(s, " \n\r")
		if err != nil {
			fmt.Println("read failure", err)
			panic(err)
		}
		aa,err := strconv.Atoi(s)
		if err != nil {
			fmt.Println("aa conv failed", err)
			panic(err)
		}
		a[i] = aa
	}
	return a
}

func readString() string {
	delim := byte('\n')
	s, err := in.ReadString(delim)
	s = strings.Trim(s, " \n\r")
	if err != nil {
		fmt.Println("read failure", err)
		panic(err)
	}
	return s
}

var n int
var a []int
var nonDel int64

func readData() {
	in = bufio.NewReader(os.Stdin)
	n,_,_,_,_ = readFive()
	a = readLineNumbs(n)
}

func solve() {
	var no,ne int
	no = 0
	ne = 0
	for i:=0; i<n; i++ {
		if a[i] %2 == 0 {
			ne++
		} else {
			no++
		}
	}
	ae := make([]int, ne)
	ao := make([]int, no)
	no=0; ne=0
	for i:=0; i<n; i++ {
		if a[i] %2 == 0 {
			ae[ne] = a[i];
			ne++
		} else {
			ao[no] = a[i];
			no++
		}
	}
	sort.Ints(ae)
	sort.Ints(ao)
	var ra []int
	var nrem int
	if no >= ne-1 && no <= ne+1 {
		nonDel = 0
		return
	} else if no < ne {
		ra = ae
		nrem = ne - no - 1
	} else {
		ra = ao
		nrem = no - ne - 1
	}
	nonDel = 0
	for i:=0; i<nrem; i++ {
		nonDel += int64(ra[i])
	}
}

func printRes() {
	fmt.Println(nonDel)
}

func main() {
	readData()
	solve()
	printRes()
}

package main

import (
	"fmt"
	"sort"
)

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

	even := make([]int, 0)
	odd := make([]int, 0)

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

		if val % 2 == 0 {
			even = append(even, val)
		} else {
			odd = append(odd, val)
		}
	}

	sort.Ints(even)
	sort.Ints(odd)

	var sum int
	var arr []int
	if len(even) > len(odd) {
		arr = even[:len(even) - len(odd) - 1]
	} else if len(odd) > len(even) {
		arr = odd[:len(odd) - len(even) - 1]
	}
	for _, m := range arr {
		sum += m
	}

	fmt.Println(sum)
}

package main

import (
	"fmt"
	"sort"
)
func abs(c int) int {
	if c < 0 {
		return -c
	}
	return c
}

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

type info struct {
	cnt int
	sum int64
}
func main()  {
	var n int
	fmt.Scan(&n)
	data := make([]int, n, n)
	for i := 0 ;i < n; i++ {
		fmt.Scan(&data[i])
	}
	sort.Ints(data)

	o := 0
	e := 0
	for i := 0 ; i < n ; i++ {
		if data[i] % 2 == 1 {
			e ++
		} else {
			o ++
		}
	}
	need :=  abs(o - e) - 1
	if need  <= 0 {
		fmt.Println(0)
		return
	}

	key := 0
	if o > e {
		key = 0
	} else {
		key = 1
	}

	wat := info{cnt:0, sum:0}

	for i := 0 ; i < n; i++ {
		if data[i] % 2 == key {
			wat.cnt ++
			wat.sum += int64(data[i])
			if wat.cnt == need {
				break
			}
 		}
	}
	fmt.Println(wat.sum)
}

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

package main

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

func sumK(a []int, k int) int {
	sum := 0
	for i := 0; i < k; i++ {
		sum += a[i]
	}
	return sum
}

func Solve(n int, a []int) int {
	evens := []int{}
	odds := []int{}
	for _, elem := range a {
		if elem%2 == 0 {
			evens = append(evens, elem)
		} else {
			odds = append(odds, elem)
		}
	}
	sort.Ints(evens)
	sort.Ints(odds)
	evenCount, oddCount := len(evens), len(odds)
	if evenCount+1 == oddCount || oddCount+1 == evenCount || oddCount == evenCount {
		return 0
	}
	if evenCount > oddCount {
		return sumK(evens, evenCount-(oddCount+1))
	}
	return sumK(odds, oddCount-(evenCount+1))
}

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

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

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

package main

import (
	"fmt"
	"sort"
)

func min(a,b int) int  {
	if(a < b){
		return a
		}
	return b
}
func katta(a,b int) int  {
	if(a < b){
		return b
	}
	return a
}
func ab(x int) int {
	if(x <= -1){
		x *= -1
	}
	return x
}

func main()  {

	var n int
	fmt.Scan(&n)
	m := make([]int,n)
	jf := 0
	for i := 0; i < n;i++{
		fmt.Scan(&m[i])
		if(m[i] % 2 == 0){
			jf ++
		}
	}
	toq := n - jf
	cnt := min(toq,jf)
	cnt += cnt + 1
	n -= cnt
	sort.Ints(m)
	ans := 0
	for i := 0; i < n;i ++{
		ans += m[i]
	}
	fmt.Println(ans)

}

package main

import (
	"fmt"
	"sort"
)

func min(a,b int) int  {
	if(a < b){
		return a
		}
	return b
}
func katta(a,b int) int  {
	if(a < b){
		return b
	}
	return a
}
func ab(x int) int {
	if(x <= -1){
		x *= -1
	}
	return x
}

func main()  {

	var n int
	fmt.Scan(&n)
	m := make([]int,n)
	jf := 0
	for i := 0; i < n;i++{
		fmt.Scan(&m[i])
		if(m[i] % 2 == 0){
			jf ++
		}
	}
	toq := n - jf
	cnt := min(toq,jf)
	cnt += cnt + 1
	n -= cnt
	sort.Ints(m)
	var ans int64
	for i := 0; i < n;i ++{
		ans += int64(m[i])
	}
	fmt.Println(ans)

}

package main

import (
	"fmt"
	"sort"
)
func abs(c int) int {
	if c < 0 {
		return -c
	}
	return c
}

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

type info struct {
	cnt int
	sum int64
}
func main()  {
	var n int
	fmt.Scan(&n)
	data := make([]int, n, n)
	for i := 0 ;i < n; i++ {
		fmt.Scan(&data[i])
	}
	sort.Ints(data)

	o := 0
	e := 0
	for i := 0 ; i < n ; i++ {
		if data[i] % 2 == 1 {
			e ++
		} else {
			o ++
		}
	}
	need :=  abs(o - e) - 1
	fmt.Println(need)
	if need  <= 0 {
		fmt.Println(0)
		return
	}

	key := 0
	if o > e {
		key = 0
	} else {
		key = 1
	}

	wat := info{cnt:0, sum:0}

	for i := 0 ; i < n; i++ {
		if data[i] % 2 == key {
			wat.cnt ++
			wat.sum += int64(data[i])
			if wat.cnt == need {
				break
			}
 		}
	}
	fmt.Println(wat.sum)
}

package main

import (
	"fmt"
	"sort"
)
func abs(c int) int {
	if c < 0 {
		return -c
	}
	return c
}

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

type info struct {
	cnt int
	sum int64
}
func main()  {
	var n int
	fmt.Scan(&n)
	data := make([]int, n, n)
	for i := 0 ;i < n; i++ {
		fmt.Scan(&data[i])
	}
	sort.Ints(data)

	o := 0
	e := 0
	for i := 0 ; i < n ; i++ {
		if data[i] % 2 == 1 {
			e ++
		} else {
			o ++
		}
	}
	need :=  abs(o - e) - 1
	key := 0
	if o > e {
		key = 0
	} else {
		key = 1
	}

	if need  < 0 {
		fmt.Println(0)
		return
	}

	wat := info{cnt:0, sum:0}

	for i := 0 ; i < n; i++ {
		if data[i] % 2 == key {
			wat.cnt ++
			wat.sum += int64(data[i])
			if wat.cnt == need {
				break
			}
 		}
	}
	fmt.Println(wat.sum)
}

package main

import (
	"fmt"
	"sort"
)
func abs(c int) int {
	if c < 0 {
		return -c
	}
	return c
}

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

type info struct {
	cnt int
	sum int64
}
func main()  {
	var n int
	fmt.Scan(&n)
	data := make([]int, n, n)
	for i := 0 ;i < n; i++ {
		fmt.Scan(&data[i])
	}
	sort.Ints(data)

	o := 0
	e := 0
	for i := 0 ; i < n ; i++ {
		if data[i] % 2 == 1 {
			e ++
		} else {
			o ++
		}
	}
	need :=  abs(o - e) - 1
	if need  <= 0 {
		return
	}

	key := 0
	if o > e {
		key = 0
	} else {
		key = 1
	}

	wat := info{cnt:0, sum:0}

	for i := 0 ; i < n; i++ {
		if data[i] % 2 == key {
			wat.cnt ++
			wat.sum += int64(data[i])
			if wat.cnt == need {
				break
			}
 		}
	}
	fmt.Println(wat.sum)
}


You are playing a computer game. In this game, you have to fight $$$n$$$ monsters.To defend from monsters, you need a shield. Each shield has two parameters: its current durability $$$a$$$ and its defence rating $$$b$$$. Each monster has only one parameter: its strength $$$d$$$.When you fight a monster with strength $$$d$$$ while having a shield with current durability $$$a$$$ and defence $$$b$$$, there are three possible outcomes:  if $$$a = 0$$$, then you receive $$$d$$$ damage;  if $$$a > 0$$$ and $$$d \ge b$$$, you receive no damage, but the current durability of the shield decreases by $$$1$$$;  if $$$a > 0$$$ and $$$d < b$$$, nothing happens. The $$$i$$$-th monster has strength $$$d_i$$$, and you will fight each of the monsters exactly once, in some random order (all $$$n!$$$ orders are equiprobable). You have to consider $$$m$$$ different shields, the $$$i$$$-th shield has initial durability $$$a_i$$$ and defence rating $$$b_i$$$. For each shield, calculate the expected amount of damage you will receive if you take this shield and fight the given $$$n$$$ monsters in random order.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the number of monsters and the number of shields, respectively. The second line contains $$$n$$$ integers $$$d_1$$$, $$$d_2$$$, ..., $$$d_n$$$ ($$$1 \le d_i \le 10^9$$$), where $$$d_i$$$ is the strength of the $$$i$$$-th monster. Then $$$m$$$ lines follow, the $$$i$$$-th of them contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i \le n$$$; $$$1 \le b_i \le 10^9$$$) — the description of the $$$i$$$-th shield.

Print $$$m$$$ integers, where the $$$i$$$-th integer represents the expected damage you receive with the $$$i$$$-th shield as follows: it can be proven that, for each shield, the expected damage is an irreducible fraction $$$\dfrac{x}{y}$$$, where $$$y$$$ is coprime with $$$998244353$$$. You have to print the value of $$$x \cdot y^{-1} \bmod 998244353$$$, where $$$y^{-1}$$$ is the inverse element for $$$y$$$ ($$$y \cdot y^{-1} \bmod 998244353 = 1$$$).

package main

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

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

	n, m := readTwoNums(reader)
	D := readNNums(reader, n)

	solver := NewSolver(n, D)
	var buf bytes.Buffer
	for m > 0 {
		m--
		a, b := readTwoNums(reader)
		res := solver.solve(a, b)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

const MOD = 998244353

func pow(a, b int64) int64 {
	var r int64 = 1
	for b > 0 {
		if b&1 == 1 {
			r = (r * a) % MOD
		}
		a = (a * a) % MOD
		b >>= 1
	}
	return r
}

func inverse(num int64) int64 {
	return pow(num, MOD-2)
}

func modDiv(a, b int64) int64 {
	return a * inverse(b) % MOD
}

func modMul(a, b int64) int64 {
	return a * b % MOD
}

type Solver struct {
	sums []int64
	arr  []int
}

func NewSolver(n int, A []int) Solver {
	sort.Ints(A)
	sums := make([]int64, n+1)
	for i := 0; i < n; i++ {
		sums[i+1] = (sums[i] + int64(A[i])) % MOD
	}

	return Solver{sums, A}
}

func (solver Solver) solve(a, b int) int {
	n := len(solver.arr)

	i := sort.SearchInts(solver.arr, b)

	//if i < n && solver.arr[i] == b {
	//	i--
	//}

	big := n - i

	if a > big || a == n {
		return 0
	}
	tmp := (solver.sums[n] - solver.sums[i] + MOD) % MOD
	// tmp * (1 - a/big)
	res := modMul(tmp, modDiv(int64(big-a), int64(big)))

	tmp = solver.sums[i]

	res += modMul(tmp, modDiv(int64(big+1-a), int64(big+1)))

	res %= MOD

	return int(res)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod int64 = 998244353
	pow := func(x, n int64) (res int64) {
		res = 1
		for ; n > 0; n >>= 1 {
			if n&1 == 1 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	div := func(a, b int64) int64 { return a * pow(b, mod-2) % mod }

	var n, q, def int
	var dur int64
	Fscan(in, &n, &q)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	sort.Ints(a)
	sum := make([]int64, n+1)
	for i, v := range a {
		sum[i+1] = (sum[i] + int64(v)) % mod
	}
	for ; q > 0; q-- {
		Fscan(in, &dur, &def)
		p := sort.SearchInts(a, def)
		cnt := int64(n - p)
		if cnt < dur {
			Fprintln(out, 0)
			continue
		}
		ans := div(cnt+1-dur, cnt+1) * sum[p]
		if cnt > dur {
			ans += div(cnt-dur, cnt) * (sum[n] - sum[p] + mod)
		}
		Fprintln(out, ans%mod)
	}
}

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

package main

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

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

	n, m := readTwoNums(reader)
	D := readNNums(reader, n)

	solver := NewSolver(n, D)
	var buf bytes.Buffer
	for m > 0 {
		m--
		a, b := readTwoNums(reader)
		res := solver.solve(a, b)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

const MOD = 998244353

func pow(a, b int64) int64 {
	var r int64 = 1
	for b > 0 {
		if b&1 == 1 {
			r = (r * a) % MOD
		}
		a = (a * a) % MOD
		b >>= 1
	}
	return r
}

func inverse(num int64) int64 {
	return pow(num, MOD-2)
}

func modDiv(a, b int64) int64 {
	return a * inverse(b) % MOD
}

func modMul(a, b int64) int64 {
	return a * b % MOD
}

type Solver struct {
	sums []int64
	arr  []int
}

func NewSolver(n int, A []int) Solver {
	sort.Ints(A)
	sums := make([]int64, n+1)
	for i := 0; i < n; i++ {
		sums[i+1] = (sums[i] + int64(A[i])) % MOD
	}

	return Solver{sums, A}
}

func (solver Solver) solve(a, b int) int {
	n := len(solver.arr)

	i := sort.SearchInts(solver.arr, b)

	//if i < n && solver.arr[i] == b {
	//	i--
	//}

	big := n - i

	if a > big || a == n {
		return 0
	}
	tmp := (solver.sums[n] - solver.sums[i] + MOD) % MOD
	// tmp * (1 - a/big)
	res := modMul(tmp, modDiv(int64(big-a), int64(big)))

	tmp = solver.sums[i]

	res += modMul(tmp, modDiv(int64(big+1-a), int64(big+1)))

	return int(res)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod int64 = 998244353
	pow := func(x, n int64) (res int64) {
		res = 1
		for ; n > 0; n >>= 1 {
			if n&1 == 1 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	div := func(a, b int64) int64 { return a * pow(b, mod-2) % mod }

	var n, q, def int
	var dur int64
	Fscan(in, &n, &q)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	sort.Ints(a)
	sum := make([]int64, n+1)
	for i, v := range a {
		sum[i+1] = sum[i] + int64(v)
	}
	for ; q > 0; q-- {
		Fscan(in, &dur, &def)
		p := sort.SearchInts(a, def)
		cnt := int64(n - p)
		if cnt < dur {
			Fprintln(out, 0)
			continue
		}
		ans := div(cnt+1-dur, cnt+1) * sum[p]
		if cnt > dur {
			ans += div(cnt-dur, cnt) * (sum[n] - sum[p])
		}
		Fprintln(out, ans%mod)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod int64 = 998244353
	pow := func(x, n int64) (res int64) {
		res = 1
		for ; n > 0; n >>= 1 {
			if n&1 == 1 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	invP := func(a int64) int64 { return pow(a, mod-2) }
	divP := func(a, b int64) int64 { return a * invP(b) % mod }

	var n, q, def int
	var dur int64
	Fscan(in, &n, &q)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	sort.Ints(a)
	sum := make([]int64, n+1)
	for i, v := range a {
		sum[i+1] = sum[i] + int64(v)
	}
	for ; q > 0; q-- {
		Fscan(in, &dur, &def)
		p := sort.SearchInts(a, def)
		big := int64(n - p)
		ans := int64(0)
		if big > dur {
			ans += divP(big-dur, big) * (sum[n] - sum[p]) % mod
		}
		if big >= dur {
			ans += divP(big+1-dur, big+1) * sum[p] % mod
		}
		Fprintln(out, ans%mod)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod int64 = 998244353
	pow := func(x, n int64) (res int64) {
		res = 1
		for ; n > 0; n >>= 1 {
			if n&1 == 1 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	div := func(a, b int64) int64 { return a * pow(b, mod-2) % mod }

	var n, q, def int
	var dur int64
	Fscan(in, &n, &q)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	sort.Ints(a)
	sum := make([]int64, n+1)
	for i, v := range a {
		sum[i+1] = (sum[i] + int64(v)) % mod
	}
	for ; q > 0; q-- {
		Fscan(in, &dur, &def)
		p := sort.SearchInts(a, def)
		cnt := int64(n - p)
		if cnt < dur {
			Fprintln(out, 0)
			continue
		}
		ans := div(cnt+1-dur, cnt+1) * sum[p]
		if cnt > dur {
			ans += div(cnt-dur, cnt) * (sum[n] - sum[p])
		}
		Fprintln(out, ans%mod)
	}
}

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


Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size $$$n \times m$$$ consisting of characters '.' (empty cell of the desktop) and '*' (an icon).The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement.In one move, you can take one icon and move it to any empty cell in the desktop.Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer $$$q$$$ queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon?Note that queries are permanent and change the state of the desktop.

The first line of the input contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \le n, m \le 1000; 1 \le q \le 2 \cdot 10^5$$$) — the number of rows in the desktop, the number of columns in the desktop and the number of queries, respectively. The next $$$n$$$ lines contain the description of the desktop. The $$$i$$$-th of them contains $$$m$$$ characters '.' and '*' — the description of the $$$i$$$-th row of the desktop. The next $$$q$$$ lines describe queries. The $$$i$$$-th of them contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i \le n; 1 \le y_i \le m$$$) — the position of the cell which changes its state (if this cell contained the icon before, then this icon is removed, otherwise an icon appears in this cell).

Print $$$q$$$ integers. The $$$i$$$-th of them should be the minimum number of moves required to make the desktop good after applying the first $$$i$$$ queries.

package main

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

func readInt(in *bufio.Scanner) (x int) {
	in.Scan()
	x, _ = strconv.Atoi(string(in.Bytes()))
	return
}

func readPositiveInt(in *bufio.Scanner) (x int) {
	in.Scan()
	for _, b := range in.Bytes() {
		x = x*10 + int(b&15)
	}
	return
}

func readString(in *bufio.Scanner) (s string) {
	in.Scan()
	s = string(in.Bytes())
	return
}

func readBytes(in *bufio.Scanner) (s []byte) {
	in.Scan()
	s = in.Bytes()
	return
}

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)
	n := readInt(in)
	m := readInt(in)
	q := readInt(in)
	ss := make([][]byte, 0)
	for i := 0; i < n; i++ {
		s := readBytes(in)
		st := make([]byte, m)
		copy(st, s)
		ss = append(ss, st)
	}
	cnt := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if ss[i][j] == '*' {
				cnt++
			}
		}
	}
	val := 0
	for j := 0; j < m; j++ {
		for i := 0; i < n; i++ {
			if j*n+i >= cnt && ss[i][j] == '*' {
				val++
			}
		}
	}

	for i := 0; i < q; i++ {
		a := readPositiveInt(in)
		b := readPositiveInt(in)
		a--
		b--
		j := a + b*n
		if j >= cnt {
			if ss[a][b] == '*' {
				val--
			} else {
				val++
			}
		}
		if ss[a][b] == '*' {
			// cnt-1
			ss[a][b] = '.'
			cnt--
			if ss[cnt%n][cnt/n] == '*' {
				val++
			}
		} else {
			// cnt
			ss[a][b] = '*'
			if ss[cnt%n][cnt/n] == '*' {
				val--
			}
			cnt++
		}
		fmt.Println(val)
	}
}

package main

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

const NN = 20

func readAllInt(in *bufio.Scanner) (x int) {
	in.Scan()
	x, _ = strconv.Atoi(string(in.Bytes()))
	return
}

func readInt(in *bufio.Scanner) (x int) {
	in.Scan()
	for _, b := range in.Bytes() {
		x = x*10 + int(b&15)
	}
	return
}

func readString(in *bufio.Scanner) (s string) {
	in.Scan()
	s = string(in.Bytes())
	return
}

func readBytes(in *bufio.Scanner) (s []byte) {
	in.Scan()
	s = in.Bytes()
	return
}

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)
	n := readInt(in)
	m := readInt(in)
	q := readInt(in)
	ss := make([][]byte, 0)
	for i := 0; i < n; i++ {
		s := readBytes(in)
		st := make([]byte, m)
		copy(st, s)
		ss = append(ss, st)
	}
	cnt := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if ss[i][j] == '*' {
				cnt++
			}
		}
	}
	val := 0
	for j := 0; j < m; j++ {
		for i := 0; i < n; i++ {
			if j*n+i >= cnt && ss[i][j] == '*' {
				val++
			}
		}
	}

	for i := 0; i < q; i++ {
		a := readInt(in)
		b := readInt(in)
		a--
		b--
		j := a + b*n
		if j >= cnt {
			if ss[a][b] == '*' {
				val--
			} else {
				val++
			}
		}
		if ss[a][b] == '*' {
			// cnt-1
			ss[a][b] = '.'
			cnt--
			if ss[cnt%n][cnt/n] == '*' {
				val++
			}
		} else {
			// cnt
			ss[a][b] = '*'
			if ss[cnt%n][cnt/n] == '*' {
				val--
			}
			cnt++
		}
		fmt.Println(val)
	}
}

package main

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

const NN = 20

func readInt(in *bufio.Scanner) (x int) {
	in.Scan()
	x, _ = strconv.Atoi(string(in.Bytes()))
	// if err != nil {
	// 	fmt.Println(err)
	// }
	return
}

// func readPositiveInt(in *bufio.Scanner) (x int) {
// 	in.Scan()
// 	for _, b := range in.Bytes() {
// 		x = x*10 + int(b&15)
// 	}
// 	return
// }

func readString(in *bufio.Scanner) (s string) {
	in.Scan()
	s = string(in.Bytes())
	return
}

func readBytes(in *bufio.Scanner) (s []byte) {
	in.Scan()
	s = in.Bytes()
	return
}

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)
	n := readInt(in)
	m := readInt(in)
	q := readInt(in)
	ss := make([][]byte, 0)
	for i := 0; i < n; i++ {
		s := readBytes(in)
		ss = append(ss, []byte(string(s)))
	}
	// fmt.Println(ss)
	cnt := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if ss[i][j] == '*' {
				cnt++
			}
		}
	}
	val := 0
	for j := 0; j < m; j++ {
		for i := 0; i < n; i++ {
			if j*n+i >= cnt && ss[i][j] == '*' {
				val++
			}
		}
	}

	for i := 0; i < q; i++ {
		a := readInt(in)
		b := readInt(in)
		a--
		b--
		// if n == 999 {
		// 	if i > NN {
		// 		fmt.Println(a, b, cnt, val, cnt%n, cnt/n)
		// 	}
		// }
		j := a + b*n
		if j >= cnt {
			if ss[a][b] == '*' {
				val--
			} else {
				val++
			}
		}
		if ss[a][b] == '*' {
			// cnt-1
			ss[a][b] = '.'
			cnt--
			if ss[cnt%n][cnt/n] == '*' {
				val++
			}
		} else {
			// cnt
			ss[a][b] = '*'
			if ss[cnt%n][cnt/n] == '*' {
				val--
			}
			cnt++
		}
		// if n == 999 {
		// 	if i > NN {
		// 		fmt.Println(i, val)
		// 	}
		// } else {
		fmt.Println(val)
		// }
	}
}

package main

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

const N = 1000

var z [N][N + 1]bool

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

	var n, m, q int
	fmt.Fscan(in, &n, &m, &q)

	var c int
	for i := 0; i < n; i++ {
		var s string
		fmt.Fscan(in, &s)

		for j, ch := range s {
			if ch == '*' {
				z[i][j] = true
				c++
			}
		}
	}

	var req int
	for i := 0; i < c; i++ {
		if !z[i%n][i/n] {
			req++
		}
	}

	for i := 0; i < q; i++ {
		var x, y int
		fmt.Fscan(in, &x, &y)

		x--
		y--
		if z[x][y] {
			// was valid, now off.
			if y*n+x < c {
				req++
			}
			z[x][y] = false
			c--
			if !z[c%n][c/n] {
				req--
			}

		} else {
			if !z[c%n][c/n] {
				req++
			}
			c++
			z[x][y] = true
			if y*n+x < c {
				req--
			}
		}

		fmt.Fprintln(out, req)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func I(a int) int64 { return int64(a) }
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int64 { i,e := strconv.ParseInt(gs(),10,64); if e != nil {panic(e)}; return i }
func gi2() (int64,int64) { return gi(),gi() }
func gi3() (int64,int64,int64) { return gi(),gi(),gi() }
func gi4() (int64,int64,int64,int64) { return gi(),gi(),gi(),gi() }
func gis(n int64) []int64 { res := make([]int64,n); for i:=I(0);i<n;i++ { res[i] = gi() }; return res }
func gf() float64 { f,e := strconv.ParseFloat(gs(),64); if e != nil {panic(e)}; return f }
func gbs() []byte { return []byte(gs()) }
func gfs(n int64) []float64  { res := make([]float64,n); for i:=I(0);i<n;i++ { res[i] = gf() }; return res }
func gss(n int64) []string  { res := make([]string,n); for i:=I(0);i<n;i++ { res[i] = gs() }; return res }
func ia(m int64) []int64 { return make([]int64,m) }
func iai(m int64,v int64) []int64 { a := make([]int64,m); for i:=I(0);i<m;i++ { a[i] = v }; return a }
func twodi(n int64,m int64,v int64) [][]int64 {
	r := make([][]int64,n); for i:=I(0);i<n;i++ { x := make([]int64,m); for j:=I(0);j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill2(m int64) ([]int64,[]int64) { a,b := ia(m),ia(m); for i:=I(0);i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func fill3(m int64) ([]int64,[]int64,[]int64) { a,b,c := ia(m),ia(m),ia(m); for i:=I(0);i<m;i++ {a[i],b[i],c[i] = gi(),gi(),gi()}; return a,b,c }
func fill4(m int64) ([]int64,[]int64,[]int64,[]int64) { a,b,c,d := ia(m),ia(m),ia(m),ia(m); for i:=I(0);i<m;i++ {a[i],b[i],c[i],d[i] = gi(),gi(),gi(),gi()}; return a,b,c,d }
func abs(a int64) int64 { if a < 0 { return -a }; return a }
func rev(a []int64) { i,j := I(0),I(len(a)-1); for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max(a,b int64) int64 { if a > b { return a }; return b }
func min(a,b int64) int64 { if a > b { return b }; return a }
func tern(cond bool, a int64, b int64) int64 { if cond { return a }; return b }
func terns(cond bool, a string, b string) string { if cond { return a }; return b }
func maxarr(a []int64) int64 { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr(a []int64) int64 { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr(a []int64) int64 { ans := I(0); for _,aa := range(a) { ans += aa }; return ans }
func zeroarr(a []int64) { for i:=I(0); i<I(len(a)); i++ { a[i] = 0 } }
func powmod(a,e,mod int64) int64 { res, m := I(1), a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint(a,e int64) int64 { res, m := I(1), a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd(a,b int64) int64 { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended(a,b int64) (int64,int64,int64) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv(a,m int64) (int64,bool) { g,x,_ := gcdExtended(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func makefact(n int64,mod int64) ([]int64,[]int64) {
	fact,factinv := make([]int64,n+1),make([]int64,n+1)
	fact[0] = 1; for i:=I(1);i<=n;i++ { fact[i] = fact[i-1] * i % mod }
	factinv[n] = powmod(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * (i+1) % mod }
	return fact,factinv
}
const inf int64 = 1000000000000000000
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	N,M,Q := gi(),gi(),gi()
	bd := make([][]byte,N)
	for i:=I(0);i<N;i++ { bd[i] = gbs() }
	X,Y := fill2(Q); for i:=I(0);i<Q;i++ { X[i]--; Y[i]-- }
	nicons := I(0)
	for i:=I(0);i<N;i++ { for j:=I(0);j<M;j++ { if bd[i][j] == '*' { nicons++ }}}
	bc,br := nicons/N,nicons % N // First non-icon
	goodicons := I(0)
	for i:=I(0);i<N;i++ { for j:=I(0);j<M;j++ { if (j < bc || j == bc && i < br) && bd[i][j] == '*' { goodicons++ } } }
	add := func(ii,jj int64) {
		// Add element -- check good -- expand good -- check added element
		bd[ii][jj] = '*'
		nicons++
		if jj < bc || jj == bc && ii < br { goodicons++ }
		if bc < M && bd[br][bc] == '*' { goodicons++ }
		br++; if br==N { bc++; br = 0}
	}
	sub := func(ii,jj int64) {
		bd[ii][jj] = '.'
		nicons--
		if jj < bc || jj == bc && ii < br { goodicons-- }
		br--; if br < 0 { bc--; br = N-1}
		if bd[br][bc] == '*' { goodicons-- }
	}
	for i:=I(0);i<Q;i++ {
		x,y := X[i],Y[i]
		if bd[x][y] == '*' { sub(x,y) } else { add(x,y) }
		fmt.Fprintln(wrtr,nicons-goodicons)
	}
}

package main

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

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

	var n, m, q, c, x, y int
	Fscan(in, &n, &m, &q)
	a := make([][]byte, n)
	for i := range a {
		Fscan(in, &a[i])
		c += bytes.Count(a[i], []byte{'*'})
	}

	ans := c
	for i, r := range a {
		j := c / n
		if i < c%n {
			j++
		}
		ans -= bytes.Count(r[:j], []byte{'*'})
	}

	for ; q > 0; q-- {
		Fscan(in, &x, &y)
		x--
		y--
		p := y*n + x
		if a[x][y] == '*' {
			c--
			if p > c {
				ans--
			}
			if p != c && a[c%n][c/n] == '*' {
				ans++
			}
		} else {
			if p > c {
				ans++
			}
			if p != c && a[c%n][c/n] == '*' {
				ans--
			}
			c++
		}
		Fprintln(out, ans)
		a[x][y] ^= 4
	}
}

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

package main

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

type IO struct {
	in  io.Reader
	out io.Writer
}

func NewIO(in io.Reader, out io.Writer) *IO {
	return &IO{in: bufio.NewReader(in), out: out}
}

func (io *IO) ReadInt() int {
	var res int
	_, _ = fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadPairInt() (int, int) {
	var a, b int
	_, _ = fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadString() string {
	var res string
	_, _ = fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) ReadIntSlice(n int) sort.IntSlice {
	var t int
	res := make(sort.IntSlice, 0, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Fscanf(io.in, "%d", &t)
		res = append(res, t)
	}
	_, _ = fmt.Fscanf(io.in, "\n")
	return res
}

func (io *IO) Println(v ...interface{}) {
	_, _ = fmt.Fprintln(io.out, v...)
}

func (io *IO) Print(v ...interface{}) {
	_, _ = fmt.Fprint(io.out, v...)
}

func (io *IO) Printf(format string, v ...interface{}) {
	_, _ = fmt.Fprintf(io.out, format, v...)
}

func (io *IO) PrintIntSlice(s sort.IntSlice) {
	for _, v := range s {
		io.Printf("%d ", v)
	}
}

func (io *IO) PrintlnIntSlice(s sort.IntSlice) {
	io.PrintIntSlice(s)
	io.Printf("\n")
}

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

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

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

func main() {
	io := NewIO(os.Stdin, os.Stdout)
	params := io.ReadIntSlice(3)
	n, m, q := params[0], params[1], params[2]

	field := make([]bool, n*m)

	cnt := 0
	prefixCnt := 0

	for i := 0; i < n; i++ {
		s := io.ReadString()
		for j := 0; j < len(s); j++ {
			if s[j] == '*' {
				field[j*n+i] = true
				cnt++
			}
		}
	}

	for i := 0; i < cnt; i++ {
		if field[i] {
			prefixCnt++
		}
	}

	for i := 0; i < q; i++ {
		params = io.ReadIntSlice(2)
		pos := (params[1]-1)*n + (params[0] - 1)
		//fmt.Println("DEBUG", pos, field, prefixCnt, cnt)
		if field[pos] {
			field[pos] = false
			if pos < cnt {
				prefixCnt--
			}
			if pos != cnt-1 && field[cnt-1] {
				prefixCnt--
			}
			cnt--
		} else {
			field[pos] = true
			cnt++
			if pos < cnt {
				prefixCnt++
			}
			if pos != cnt-1 && field[cnt-1] {
				prefixCnt++
			}
		}
		fmt.Println(cnt - prefixCnt)
	}
}

package main

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

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

func main() {
	defer w.Flush()
	var n, m, q int
	fmt.Fscan(r, &n, &m, &q)
	grid := make([][]byte, n)
	cnt := 0
	for i:=0;i<n;i++ {
		var s string
		fmt.Fscan(r, &s)
		grid[i] = []byte(s)
		for j := range grid[i] {
			if grid[i][j] == '.' {
				grid[i][j] = 0
			} else {
				grid[i][j] = 1
				cnt++
			}
		}
	}
	fit := 0
	x, y := (cnt-1)%n, (cnt-1)/n
	for i:=0;i<n;i++ {
		for j := range grid[i] {
			if grid[i][j] == 1 && (j < y || (j==y&&i<=x)) {
				fit++
			}
		}
	}
	for k:=0;k<q;k++ {
		a, b := 0, 0
		fmt.Fscan(r, &a, &b)
		a, b = a-1, b-1
		if grid[a][b] == 0 {
			grid[a][b] = 1
			cnt++
			x++
			if x == n {
				x = 0
				y++
			}
			if x == a && y == b {
				fit++
			} else {
				if grid[x][y] == 1 {
					fit++
				}
				if b<y || (b==y&&a<=x) {
					fit++
				}
			}
		} else {
			grid[a][b] = 0
			cnt--
			if x == a && y == b {
				fit--
			} else {
				if grid[x][y] == 1 {
					fit--
				}
				if b<y || (b==y&&a<=x) {
					fit--
				}
			}
			x--
			if x == -1 {
				x = n-1
				y--
			}
		}
		fmt.Fprintln(w, cnt - fit)
	}
}

package main

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

const NN = 20

func readInt(in *bufio.Scanner) (x int) {
	in.Scan()
	x, _ = strconv.Atoi(string(in.Bytes()))
	// if err != nil {
	// 	fmt.Println(err)
	// }
	return
}

// func readPositiveInt(in *bufio.Scanner) (x int) {
// 	in.Scan()
// 	for _, b := range in.Bytes() {
// 		x = x*10 + int(b&15)
// 	}
// 	return
// }

func readString(in *bufio.Scanner) (s string) {
	in.Scan()
	s = string(in.Bytes())
	return
}

func readBytes(in *bufio.Scanner) (s []byte) {
	in.Scan()
	s = in.Bytes()
	return
}

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)
	n := readInt(in)
	m := readInt(in)
	q := readInt(in)
	ss := make([][]byte, 0)
	for i := 0; i < n; i++ {
		s := readBytes(in)
		ss = append(ss, []byte(string(s)))
	}
	// fmt.Println(ss)
	cnt := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if ss[i][j] == '*' {
				cnt++
			}
		}
	}
	val := 0
	for j := 0; j < m; j++ {
		for i := 0; i < n; i++ {
			if j*n+i >= cnt && ss[i][j] == '*' {
				val++
			}
		}
	}

	for i := 0; i < q; i++ {
		a := readInt(in)
		b := readInt(in)
		a--
		b--
		// if n == 999 {
		// 	if i > NN {
		// 		fmt.Println(a, b, cnt, val, cnt%n, cnt/n)
		// 	}
		// }
		j := a + b*n
		if j >= cnt {
			if ss[a][b] == '*' {
				val--
			} else {
				val++
			}
		}
		if ss[a][b] == '*' {
			// cnt-1
			ss[a][b] = '.'
			cnt--
			if ss[cnt%n][cnt/n] == '*' {
				val++
			}
		} else {
			// cnt
			if ss[cnt%n][cnt/n] == '*' {
				val--
			}
			ss[a][b] = '*'
			cnt++
		}
		// if n == 999 {
		// 	if i > NN {
		// 		fmt.Println(i, val)
		// 	}
		// } else {
		fmt.Println(val)
		// }
	}
}

package main

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

const NN = 20

func readInt(in *bufio.Scanner) (x int) {
	in.Scan()
	x, err := strconv.Atoi(string(in.Bytes()))
	if err != nil {
		fmt.Println(err)
	}
	return
}

// func readPositiveInt(in *bufio.Scanner) (x int) {
// 	in.Scan()
// 	for _, b := range in.Bytes() {
// 		x = x*10 + int(b&15)
// 	}
// 	return
// }

func readString(in *bufio.Scanner) (s string) {
	in.Scan()
	s = string(in.Bytes())
	return
}

func readBytes(in *bufio.Scanner) (s []byte) {
	in.Scan()
	s = in.Bytes()
	return
}

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)
	n := readInt(in)
	m := readInt(in)
	q := readInt(in)
	ss := make([][]byte, 0)
	for i := 0; i < n; i++ {
		s := readBytes(in)
		ss = append(ss, []byte(string(s)))
	}
	// fmt.Println(ss)
	cnt := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if ss[i][j] == '*' {
				cnt++
			}
		}
	}
	val := 0
	for j := 0; j < m; j++ {
		for i := 0; i < n; i++ {
			if j*n+i >= cnt && ss[i][j] == '*' {
				val++
			}
		}
	}

	for i := 0; i < q; i++ {
		a := readInt(in)
		b := readInt(in)
		a--
		b--
		if n == 999 {
			if i > NN {
				fmt.Println(a, b, cnt, val, cnt%n, cnt/n)
			}
		}
		j := a + b*n
		if j >= cnt {
			if ss[a][b] == '*' {
				val--
			} else {
				val++
			}
		}
		if ss[a][b] == '*' {
			// cnt-1
			ss[a][b] = '.'
			cnt--
			if ss[cnt%n][cnt/n] == '*' {
				val++
			}
		} else {
			// cnt
			if ss[cnt%n][cnt/n] == '*' {
				val--
			}
			ss[a][b] = '*'
			cnt++
		}
		if n == 999 {
			if i > NN {
				fmt.Println(i, val)
			}
		} else {
			fmt.Println(val)
		}
	}
}

package main

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

var z [1000003]bool

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

	var n, m, q int
	fmt.Fscan(in, &n, &m, &q)

	var c int
	for i := 0; i < n; i++ {
		var s string
		fmt.Fscan(in, &s)

		for j, ch := range s {
			if ch == '*' {
				z[j*n+i] = true
				c++
			}
		}
	}

	var o int
	for i := 0; i < c; i++ {
		if z[i] {
			o++
		}
	}

	for i := 0; i < q; i++ {
		var x, y int
		fmt.Fscan(in, &x, &y)

		x--
		y--
		pos := y*n + x
		if z[pos] {
			// was valid, now off.
			if pos < c {
				o--
			}

			c--
			if pos != c && z[c] {
				o--
			}

		} else {
			if pos < c {
				o++
			}
			if z[c] {
				o++
			}
			c++
		}
		z[pos] = !z[pos]

		fmt.Fprintln(out, c-o)
	}
}

package main

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

var z [1000003]bool

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

	var n, m, q int
	fmt.Fscan(in, &n, &m, &q)

	var c int
	for i := 0; i < n; i++ {
		var s string
		fmt.Fscan(in, &s)

		for j, ch := range s {
			if ch == '*' {
				z[j*n+i] = true
				c++
			}
		}
	}

	var o int
	for i := 0; i < c; i++ {
		if z[i] {
			o++
		}
	}

	for i := 0; i < q; i++ {
		var x, y int
		fmt.Fscan(in, &x, &y)

		x--
		y--
		pos := y*n + x
		if z[pos] {
			// was valid, now off.
			if pos < c {
				o--
			}

			c--
			if pos != c && z[c] {
				o--
			}

		} else {
			if pos < c {
				o++
			}
			if pos != c && z[c] {
				o++
			}
			c++
		}
		z[pos] = !z[pos]

		fmt.Fprintln(out, c-o)
	}
}

package main

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

var z [1000003]bool

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

	var n, m, q int
	fmt.Fscan(in, &n, &m, &q)

	var c int
	for i := 0; i < n; i++ {
		var s string
		fmt.Fscan(in, &s)

		for j, ch := range s {
			if ch == '*' {
				z[j*n+i] = true
				c++
			}
		}
	}

	var o int
	for i := 0; i < c; i++ {
		if z[i] {
			o++
		}
	}

	for i := 0; i < q; i++ {
		var x, y int
		fmt.Fscan(in, &x, &y)

		x--
		y--
		pos := y*n + x
		if z[pos] {
			// was valid, now off.
			if pos < c {
				o--
			}

			c--
			if pos != c && z[c] {
				o--
			}

		} else {
			if pos < c {
				o++
			}
			if pos+1 != c && z[c] {
				o++
			}
			c++
		}
		z[pos] = !z[pos]

		fmt.Fprintln(out, c-o)
	}
}


Xenia the beginner programmer has a sequence a, consisting of 2n non-negative integers: a1, a2, ..., a2n. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value v for a.Namely, it takes several iterations to calculate value v. At the first iteration, Xenia writes a new sequence a1 or a2, a3 or a4, ..., a2n - 1 or a2n, consisting of 2n - 1 elements. In other words, she writes down the bit-wise OR of adjacent elements of sequence a. At the second iteration, Xenia writes the bitwise exclusive OR of adjacent elements of the sequence obtained after the first iteration. At the third iteration Xenia writes the bitwise OR of the adjacent elements of the sequence obtained after the second iteration. And so on; the operations of bitwise exclusive OR and bitwise OR alternate. In the end, she obtains a sequence consisting of one element, and that element is v.Let's consider an example. Suppose that sequence a = (1, 2, 3, 4). Then let's write down all the transformations (1, 2, 3, 4)  →  (1 or 2 = 3, 3 or 4 = 7)  →  (3 xor 7 = 4). The result is v = 4.You are given Xenia's initial sequence. But to calculate value v for a given sequence would be too easy, so you are given additional m queries. Each query is a pair of integers p, b. Query p, b means that you need to perform the assignment ap = b. After each query, you need to print the new value v for the new sequence a.

The first line contains two integers n and m (1 ≤ n ≤ 17, 1 ≤ m ≤ 105). The next line contains 2n integers a1, a2, ..., a2n (0 ≤ ai < 230). Each of the next m lines contains queries. The i-th line contains integers pi, bi (1 ≤ pi ≤ 2n, 0 ≤ bi < 230) — the i-th query.

Print m integers — the i-th integer denotes value v for sequence a after the i-th query.

NoteFor more information on the bit operations, you can follow this link: http://en.wikipedia.org/wiki/Bitwise_operation

package main

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

type node struct {
	l, r   int
	val    int
	needOR bool
}
type segmentTree []node

func (t segmentTree) _pushUp(o int) {
	lo, ro := t[o<<1], t[o<<1|1]
	if lo.needOR {
		t[o].val = lo.val | ro.val
	} else {
		t[o].val = lo.val ^ ro.val
	}
}

func (t segmentTree) _build(arr []int, o, l, r int) {
	t[o].l, t[o].r = l, r
	if l == r {
		t[o].val = arr[l]
		t[o].needOR = true
		return
	}
	mid := (l + r) >> 1
	t._build(arr, o<<1, l, mid)
	t._build(arr, o<<1|1, mid+1, r)
	t[o].needOR = !t[o<<1].needOR
	t._pushUp(o)
}

func (t segmentTree) _update(o, idx int, val int) {
	if t[o].l == t[o].r {
		t[o].val = val
		return
	}
	if idx <= (t[o].l+t[o].r)>>1 {
		t._update(o<<1, idx, val)
	} else {
		t._update(o<<1|1, idx, val)
	}
	t._pushUp(o)
}

func (t segmentTree) init(arr []int)          { t._build(arr, 1, 1, len(arr)-1) }
func (t segmentTree) update(idx int, val int) { t._update(1, idx, val) }

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

	var n, m int
	Fscan(in, &n, &m)
	n = 1 << uint(n)
	arr := make([]int, n+1)
	for i := 1; i <= n; i++ {
		Fscan(in, &arr[i])
	}

	t := make(segmentTree, 2*n)
	t.init(arr)
	for ; m > 0; m-- {
		var idx, val int
		Fscan(in, &idx, &val)
		t.update(idx, val)
		Fprintln(out, t[1].val)
	}
}

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

package main

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

type node struct {
	val    int
	needOR bool
}
type segmentTree []node

func (t segmentTree) _pushUp(o int) {
	lo, ro := t[o<<1], t[o<<1|1]
	if lo.needOR {
		t[o].val = lo.val | ro.val
	} else {
		t[o].val = lo.val ^ ro.val
	}
}

func (t segmentTree) _build(arr []int, o, l, r int) {
	if l == r {
		t[o].val = arr[l]
		t[o].needOR = true
		return
	}
	mid := (l + r) >> 1
	t._build(arr, o<<1, l, mid)
	t._build(arr, o<<1|1, mid+1, r)
	t[o].needOR = !t[o<<1].needOR
	t._pushUp(o)
}

func (t segmentTree) _update(o, l, r int, idx int, val int) {
	if l == r {
		t[o].val = val
		return
	}
	mid := (l + r) >> 1
	if idx <= mid {
		t._update(o<<1, l, mid, idx, val)
	} else {
		t._update(o<<1|1, mid+1, r, idx, val)
	}
	t._pushUp(o)
}

func (t segmentTree) init(arr []int)                 { t._build(arr, 1, 1, len(arr)-1) }
func (t segmentTree) update(n int, idx int, val int) { t._update(1, 1, n, idx, val) }

func Sol339D(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, m int
	Fscan(in, &n, &m)
	n = 1 << uint(n)
	arr := make([]int, n+1)
	for i := 1; i <= n; i++ {
		Fscan(in, &arr[i])
	}

	t := make(segmentTree, 2*n)
	t.init(arr)
	for ; m > 0; m-- {
		var idx, val int
		Fscan(in, &idx, &val)
		t.update(n, idx, val)
		Fprintln(out, t[1].val)
	}
}

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

// 339D
package main

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

var (
	i, j, m, n uint64
	f          [1 << 19]uint64
)

func modify(x, v, p uint64) {
	if p == 1 {
		f[x] = v
	} else {
		if p%2 != 0 {
			f[x] = f[x<<1|1] ^ f[x<<1]
		} else {
			f[x] = f[x<<1|1] | f[x<<1]
		}
	}
	if x > 1 {
		modify(x>>1, v, p+1)
	}
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Scan(&n, &m)
	n = uint64(1 << n)
	for i = 0; i < n; i++ {
		var c uint64
		fmt.Fscan(in, &c)
		modify(n+i, c, 1)
	}
	for ; m > 0; m-- {
		var pos, b uint64
		fmt.Fscan(in,&pos, &b)
		modify(n+pos-1, b, 1)
		fmt.Fprintln(out, f[1])
	}
	out.Flush()
	return
}

package main

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

var (
	sc   *bufio.Scanner = bufio.NewScanner(os.Stdin)
	n, m int
	tr   []int
)

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

func nextInt() int {
	n, _ := strconv.Atoi(nextString())
	return n
}

func calcNode(i, lev int) {
	if lev%2 == 0 {
		tr[i] = tr[2*i] | tr[2*i+1]
	} else {
		tr[i] = tr[2*i] ^ tr[2*i+1]
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n, m = 1<<uint(nextInt()), nextInt()
	tr = make([]int, 2*n)
	for i := 0; i < n; i++ {
		tr[n+i] = nextInt()
	}
	for lev, levSz, i := 0, n/2, n-1; levSz >= 1; levSz /= 2 {
		for j := 0; j < levSz; j++ {
			calcNode(i-j, lev)
		}
		i -= levSz
		lev++
	}
	for i := 0; i < m; i++ {
		ix, v := nextInt()+n-1, nextInt()
		tr[ix] = v
		for j, lev := ix/2, 0; j >= 1; j /= 2 {
			calcNode(j, lev)
			lev++
		}
		fmt.Println(tr[1])
	}
}

// 339D
package main

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

var (
	i, j, m, n uint64
	f          [1 << 19]uint64
)

func modify(x, v, p uint64) {
	if p == 1 {
		f[x] = v
	} else {
		if p%2 != 0 {
			f[x] = f[x<<1|1] ^ f[x<<1]
		} else {
			f[x] = f[x<<1|1] | f[x<<1]
		}
	}
	if x > 1 {
		modify(x>>1, v, p+1)
	}
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Scan(&n, &m)
	n = uint64(1 << n)
	for i = 0; i < n; i++ {
		var c uint64
		fmt.Fscan(in, &c)
		modify(n+i, c, 1)
	}
	for ; m > 0; m-- {
		var pos, b uint64
		fmt.Scan(&pos, &b)
		modify(n+pos-1, b, 1)
		fmt.Fprintln(out, f[1])
	}
	out.Flush()
	return
}

// 339D
package main

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

var (
	i, j, m, n uint64
	f          [1 << 19]uint64
)

func modify(x, v, p uint64) {
	if p == 1 {
		f[x] = v
	} else {
		if p%2 != 0 {
			f[x] = f[x<<1|1] ^ f[x<<1]
		} else {
			f[x] = f[x<<1|1] | f[x<<1]
		}
	}
	if x > 1 {
		modify(x>>1, v, p+1)
	}
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Scan(&n, &m)
	n = uint64(1 << n)
	for i = 0; i < n; i++ {
		var c uint64
		fmt.Fscan(in, &c)
		modify(n+i, c, 1)
	}
	for ; m > 0; m-- {
		var pos, b uint64
		fmt.Scan(&pos, &b)
		modify(n+pos-1, b, 1)
		fmt.Fprintln(out, f[1])
	}
	return
}