An array $$$[b_1, b_2, \ldots, b_m]$$$ is a palindrome, if $$$b_i = b_{m+1-i}$$$ for each $$$i$$$ from $$$1$$$ to $$$m$$$. Empty array is also a palindrome.An array is called kalindrome, if the following condition holds: It's possible to select some integer $$$x$$$ and delete some of the elements of the array equal to $$$x$$$, so that the remaining array (after gluing together the remaining parts) is a palindrome. Note that you don't have to delete all elements equal to $$$x$$$, and you don't have to delete at least one element equal to $$$x$$$.For example :   $$$[1, 2, 1]$$$ is kalindrome because you can simply not delete a single element.  $$$[3, 1, 2, 3, 1]$$$ is kalindrome because you can choose $$$x = 3$$$ and delete both elements equal to $$$3$$$, obtaining array $$$[1, 2, 1]$$$, which is a palindrome.  $$$[1, 2, 3]$$$ is not kalindrome. You are given an array $$$[a_1, a_2, \ldots, a_n]$$$. Determine if $$$a$$$ is kalindrome or not.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — elements of the array. It's guaranteed that the sum of $$$n$$$ over all test cases won't exceed $$$2 \cdot 10^5$$$.

For each test case, print YES if $$$a$$$ is kalindrome and NO otherwise. You can print each letter in any case.

NoteIn the first test case, array $$$[1]$$$ is already a palindrome, so it's a kalindrome as well.In the second test case, we can choose $$$x = 2$$$, delete the second element, and obtain array $$$[1]$$$, which is a palindrome.In the third test case, it's impossible to obtain a palindrome.In the fourth test case, you can choose $$$x = 4$$$ and delete the fifth element, obtaining $$$[1, 4, 4, 1]$$$. You also can choose $$$x = 1$$$, delete the first and the fourth elements, and obtain $$$[4, 4, 4]$$$.

package main

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

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)

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

		l, r := 0, n-1
		isValid := true
		d := -1
		// go from left-right
		for l < r {
			if arr[l] == arr[r] {
				l++
				r--
			} else if d < 0 {
				d = arr[r]
				r--
			} else if arr[l] == d && arr[r] == d {
				l++
				r--
			} else if arr[r] == d {
				r--
			} else if arr[l] == d {
				l++
			} else {
				isValid = false
				break
			}
		}

		if !isValid {
			l, r := 0, n-1
			isValid = true
			d := -1
			// go from left-right
			for l < r {
				if arr[l] == arr[r] {
					l++
					r--
				} else if d < 0 {
					d = arr[l]
					l++
				} else if arr[l] == d && arr[r] == d {
					l++
					r--
				} else if arr[r] == d {
					r--
				} else if arr[l] == d {
					l++
				} else {
					isValid = false
					break
				}
			}
		}

		if !isValid {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
		}
	}
}

package main

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

var (
	input = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assert!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    defer output.Flush()
    //solve()
    //return
	var t int64
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
}

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

func min(a, b int64) int64{
    if a < b { return a}
    return b
}
func max(a, b int64) int64{
    if a > b { return a}
    return b
}
func solve() {
    var n int
    fmt.Fscan(input, &n)
    a := make([]int, n)
    for i := 0; i < n; i++ {
        fmt.Fscan(input, &a[i])
    }

    if check(a) {
        fmt.Fprintln(output, "YES")
    } else {
        fmt.Fprintln(output, "NO")
    }
}

func check(a []int) bool {
    if len(a) <= 1 { return true }

    last := len(a) -1
    if a[0] == a[last] { return check(a[1:last]) }

    if checkWithout(a[1:], a[0]) { return true }
    return checkWithout(a[:last], a[last])
}

func checkWithout(a []int, w int) bool {
    l, r := 0, len(a) -1
    for l < r {
        if a[l] == w {
            l++
            continue
        }
        if a[r] == w {
            r--
            continue
        }
        if a[l] != a[r] { return false }
        l++; r--
    }
    return true
}


One tradition of ACM-ICPC contests is that a team gets a balloon for every solved problem. We assume that the submission time doesn't matter and teams are sorted only by the number of balloons they have. It means that one's place is equal to the number of teams with more balloons, increased by 1. For example, if there are seven teams with more balloons, you get the eight place. Ties are allowed.You should know that it's important to eat before a contest. If the number of balloons of a team is greater than the weight of this team, the team starts to float in the air together with their workstation. They eventually touch the ceiling, what is strictly forbidden by the rules. The team is then disqualified and isn't considered in the standings.A contest has just finished. There are n teams, numbered 1 through n. The i-th team has ti balloons and weight wi. It's guaranteed that ti doesn't exceed wi so nobody floats initially.Limak is a member of the first team. He doesn't like cheating and he would never steal balloons from other teams. Instead, he can give his balloons away to other teams, possibly making them float. Limak can give away zero or more balloons of his team. Obviously, he can't give away more balloons than his team initially has.What is the best place Limak can get?

The first line of the standard input contains one integer n (2 ≤ n ≤ 300 000) — the number of teams. The i-th of n following lines contains two integers ti and wi (0 ≤ ti ≤ wi ≤ 1018) — respectively the number of balloons and the weight of the i-th team. Limak is a member of the first team.

Print one integer denoting the best place Limak can get.

NoteIn the first sample, Limak has 20 balloons initially. There are three teams with more balloons (32, 40 and 45 balloons), so Limak has the fourth place initially. One optimal strategy is: Limak gives 6 balloons away to a team with 32 balloons and weight 37, which is just enough to make them fly. Unfortunately, Limak has only 14 balloons now and he would get the fifth place. Limak gives 6 balloons away to a team with 45 balloons. Now they have 51 balloons and weight 50 so they fly and get disqualified. Limak gives 1 balloon to each of two teams with 16 balloons initially. Limak has 20 - 6 - 6 - 1 - 1 = 6 balloons. There are three other teams left and their numbers of balloons are 40, 14 and 2. Limak gets the third place because there are two teams with more balloons. In the second sample, Limak has the second place and he can't improve it.In the third sample, Limak has just enough balloons to get rid of teams 2, 3 and 5 (the teams with 81 000 000 000, 5 000 000 000 and 46 000 000 000 balloons respectively). With zero balloons left, he will get the second place (ex-aequo with team 6 and team 7).

package main

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

type Score struct {
    t, w int64
}

type ByT []Score

func (a ByT) Len() int           { return len(a) }
func (a ByT) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByT) Less(i, j int) bool { return a[i].t < a[j].t }

type Heap []int64

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

func (h *Heap) Push(x interface{}) {
	*h = append(*h, x.(int64))
}

func (h *Heap) Pop() interface{} {
	x := (*h)[len(*h)-1]
	*h = (*h)[0:len(*h)-1]
	return x
}

func solve() {
    n := readInt() - 1
    a, h := make([]Score, n), &Heap{}
    heap.Init(h)
    my := Score{readInt64(), readInt64()}
    for i := range a {
        a[i] = Score{readInt64(), readInt64()}
    }
    sort.Sort(ByT(a))

    curI, ans, done := n-1, n, false
    for !done {
        for curI >= 0 && a[curI].t > my.t {
            heap.Push(h, a[curI].w - a[curI].t + 1)
            curI--
        }
        ans = min(ans, h.Len())

        done = h.Len() == 0
        if !done {
            diff := heap.Pop(h).(int64)
            if diff <= my.t {
                my.t -= diff
            } else {
                done = true
            }
        }
    }

    fmt.Println(ans+1)
}

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)

func main() {
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

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

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

func min(x, y int) int {
	return x + y - max(x, y)
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
    v, _ := strconv.ParseInt(readString(), 10, 64)
    return v;
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

/* Sort tempalte
func (a ) Len() int           { return len(a) }
func (a ) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ) Less(i, j int) bool { return  }
*/

package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer

type NodeMin struct {
	value int64
	index int
}

type HeapMin []*NodeMin

func (nodes HeapMin) Len() int {
	return len(nodes)
}

func (nodes HeapMin) Less(i int, j int) bool {
	return nodes[i].value < nodes[j].value
}

func (nodes HeapMin) Swap(i int, j int) {
	nodes[i], nodes[j] = nodes[j], nodes[i]
	nodes[i].index = i
	nodes[j].index = j
}

func (nodes *HeapMin) Push(x interface{}) {
	node := x.(*NodeMin)
	node.index = len(*nodes)
	*nodes = append(*nodes, node)
}

func (nodes *HeapMin) Pop() interface{} {
	s := *nodes
	last := len(s) - 1
	r := s[last]
	*nodes = s[:last]
	r.index = -1
	return r
}

type Team struct {
	t int64
	w int64
}

type TeamSlice []Team

func (s TeamSlice) Len() int               { return len(s) }
func (s TeamSlice) Less(i int, j int) bool { return s[i].t > s[j].t }
func (s TeamSlice) Swap(i int, j int)      { s[i], s[j] = s[j], s[i] }

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	var n int
	var t, w int64
	var hmin HeapMin
	var queue TeamSlice

	fmt.Fscanf(bufin, "%d\n", &n)
	fmt.Fscanf(bufin, "%d %d\n", &t, &w)

	balloon := t
	rank := 1

	for i := 2; i <= n; i++ {
		fmt.Fscanf(bufin, "%d %d\n", &t, &w)
		if t > balloon {
			heap.Push(&hmin, &NodeMin{value: w - t + 1})
			rank++
		} else {
			queue = append(queue, Team{t: t, w: w})
		}
	}

	sort.Sort(queue)
	nq := len(queue)

	bestRank := rank
	pos := 0

	for hmin.Len() > 0 {
		node := heap.Pop(&hmin).(*NodeMin)

		t = node.value
		if t > balloon {
			break
		}

		rank--
		for balloon -= t; (pos < nq) && (queue[pos].t > balloon); pos++ {
			heap.Push(&hmin, &NodeMin{value: queue[pos].w - queue[pos].t + 1})
			rank++
		}

		if rank < bestRank {
			bestRank = rank
		}
	}

	fmt.Fprintf(bufout, "%d\n", bestRank)
}

package main

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

type int64Heap []int64

func (h int64Heap) Len() int              { return len(h) }
func (h int64Heap) Less(i, j int) bool    { return h[i] < h[j] }
func (h int64Heap) Swap(i, j int)         { h[i], h[j] = h[j], h[i] }
func (h *int64Heap) Push(v interface{})   { *h = append(*h, v.(int64)) }
func (h *int64Heap) Pop() (v interface{}) { n := len(*h); *h, v = (*h)[:n-1], (*h)[n-1]; return }

// github.com/EndlessCheng/codeforces-go
func CF725D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type team struct{ balls, weight int64 }

	var n, ans int
	var myBalls, w, gives int64
	Fscan(in, &n, &myBalls, &w)
	ts := make([]team, n-1, n)
	for i := range ts {
		Fscan(in, &ts[i].balls, &ts[i].weight)
	}
	sort.Slice(ts, func(i, j int) bool { return ts[i].balls > ts[j].balls })
	ts = append(ts, team{})

	for _, t := range ts {
		if t.balls <= myBalls {
			gives = myBalls - t.balls
			myBalls = t.balls
			break
		}
		ans++
	}
	h := &int64Heap{}
	for i := 0; ts[i].balls > 0; {
		for ; ts[i].balls > myBalls; i++ {
			Push(h, ts[i].weight-ts[i].balls+1)
		}
		for len(*h) > 0 {
			d := (*h)[0]
			if d > gives {
				break
			}
			Pop(h)
			gives -= d
		}
		if len(*h) < ans {
			ans = len(*h)
		}
		for _, t := range ts[i:] {
			if b := t.balls; b < myBalls {
				gives += myBalls - b
				myBalls = b
				break
			}
		}
	}
	Fprint(out, ans+1)
}

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

package main

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

type int64Heap []int64

func (h int64Heap) Len() int              { return len(h) }
func (h int64Heap) Less(i, j int) bool    { return h[i] < h[j] }
func (h int64Heap) Swap(i, j int)         { h[i], h[j] = h[j], h[i] }
func (h *int64Heap) Push(v interface{})   { *h = append(*h, v.(int64)) }
func (h *int64Heap) Pop() (v interface{}) { n := len(*h); *h, v = (*h)[:n-1], (*h)[n-1]; return }

// github.com/EndlessCheng/codeforces-go
func CF725D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type team struct{ balls, weight int64 }

	var n, ans int
	var myBalls, w, gives int64
	Fscan(in, &n, &myBalls, &w)
	ts := make([]team, n-1, n)
	for i := range ts {
		Fscan(in, &ts[i].balls, &ts[i].weight)
	}
	sort.Slice(ts, func(i, j int) bool { return ts[i].balls > ts[j].balls })
	ts = append(ts, team{})

	for _, t := range ts {
		if b := t.balls; b <= myBalls {
			gives += myBalls - b
			myBalls = b
			break
		}
		ans++
	}
	h := &int64Heap{}
	for i := 0; ts[i].balls > 0; {
		for ; ts[i].balls > myBalls; i++ {
			Push(h, ts[i].weight-ts[i].balls+1)
		}
		for len(*h) > 0 {
			d := (*h)[0]
			if d > gives {
				break
			}
			Pop(h)
			gives -= d
		}
		if len(*h) < ans {
			ans = len(*h)
		}
		for _, t := range ts[i:] {
			if b := t.balls; b < myBalls {
				gives += myBalls - b
				myBalls = b
				break
			}
		}
	}
	Fprint(out, ans+1)
}

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

package main

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

type int64Heap []int64

func (h int64Heap) Len() int              { return len(h) }
func (h int64Heap) Less(i, j int) bool    { return h[i] < h[j] }
func (h int64Heap) Swap(i, j int)         { h[i], h[j] = h[j], h[i] }
func (h *int64Heap) Push(v interface{})   { *h = append(*h, v.(int64)) }
func (h *int64Heap) Pop() (v interface{}) { n := len(*h); *h, v = (*h)[:n-1], (*h)[n-1]; return }

// github.com/EndlessCheng/codeforces-go
func CF725D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type team struct{ balls, weight int64 }

	var n, ans int
	var myBalls, w, gives int64
	Fscan(in, &n, &myBalls, &w)
	ts := make([]team, n-1, n)
	for i := range ts {
		Fscan(in, &ts[i].balls, &ts[i].weight)
	}
	sort.Slice(ts, func(i, j int) bool { return ts[i].balls > ts[j].balls })
	ts = append(ts, team{})

	for _, t := range ts {
		if t.balls <= myBalls {
			gives = myBalls - t.balls
			myBalls = t.balls
			break
		}
		ans++
	}
	h := &int64Heap{}
	for i := 0; i < n && ts[i].balls > 0; {
		for ; i < n && ts[i].balls > myBalls; i++ {
			Push(h, ts[i].weight-ts[i].balls+1)
		}
		for len(*h) > 0 {
			d := (*h)[0]
			if d > gives {
				break
			}
			Pop(h)
			gives -= d
		}
		if len(*h) < ans {
			ans = len(*h)
		}
		for j := i; j < n; j++ {
			if ts[j].balls < myBalls {
				gives += myBalls - ts[j].balls
				myBalls = ts[j].balls
				break
			}
		}
	}
	Fprint(out, ans+1)
}

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

package main

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

type int64Heap []int64

func (h int64Heap) Len() int              { return len(h) }
func (h int64Heap) Less(i, j int) bool    { return h[i] < h[j] }
func (h int64Heap) Swap(i, j int)         { h[i], h[j] = h[j], h[i] }
func (h *int64Heap) Push(v interface{})   { *h = append(*h, v.(int64)) }
func (h *int64Heap) Pop() (v interface{}) { n := len(*h); *h, v = (*h)[:n-1], (*h)[n-1]; return }

// github.com/EndlessCheng/codeforces-go
func CF725D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type team struct{ balls, weight int64 }

	var n, ans int
	var myBalls, w, gives int64
	Fscan(in, &n, &myBalls, &w)
	ts := make([]team, n-1, n)
	for i := range ts {
		Fscan(in, &ts[i].balls, &ts[i].weight)
	}
	sort.Slice(ts, func(i, j int) bool { return ts[i].balls > ts[j].balls })
	ts = append(ts, team{})

	for _, t := range ts {
		if t.balls <= myBalls {
			gives = myBalls - t.balls
			myBalls = t.balls
			break
		}
		ans++
	}
	h := &int64Heap{}
	for i := 0; ts[i].balls > 0; {
		for ; ts[i].balls > myBalls; i++ {
			Push(h, ts[i].weight-ts[i].balls+1)
		}
		for len(*h) > 0 {
			d := (*h)[0]
			if d > gives {
				break
			}
			Pop(h)
			gives -= d
		}
		if len(*h) < ans {
			ans = len(*h)
		}
		for j := i; j < n; j++ {
			if ts[j].balls < myBalls {
				gives += myBalls - ts[j].balls
				myBalls = ts[j].balls
				break
			}
		}
	}
	Fprint(out, ans+1)
}

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

package main

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

type int64Heap []int64

func (h int64Heap) Len() int              { return len(h) }
func (h int64Heap) Less(i, j int) bool    { return h[i] < h[j] }
func (h int64Heap) Swap(i, j int)         { h[i], h[j] = h[j], h[i] }
func (h *int64Heap) Push(v interface{})   { *h = append(*h, v.(int64)) }
func (h *int64Heap) Pop() (v interface{}) { n := len(*h); *h, v = (*h)[:n-1], (*h)[n-1]; return }

// github.com/EndlessCheng/codeforces-go
func CF725D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type team struct{ balls, weight int64 }

	var n int
	var myBalls, w, gives int64
	Fscan(in, &n, &myBalls, &w)
	ts := make([]team, n-1, n)
	for i := range ts {
		Fscan(in, &ts[i].balls, &ts[i].weight)
	}
	sort.Slice(ts, func(i, j int) bool { return ts[i].balls > ts[j].balls })
	ts = append(ts, team{})

	ans := n
	for _, t := range ts {
		if t.balls <= myBalls {
			gives = myBalls - t.balls
			myBalls = t.balls
			break
		}
	}
	h := &int64Heap{}
	for i := 0; i < n && ts[i].balls > 0; {
		for ; i < n && ts[i].balls > myBalls; i++ {
			Push(h, ts[i].weight-ts[i].balls+1)
		}
		for len(*h) > 0 {
			d := (*h)[0]
			if d > gives {
				break
			}
			Pop(h)
			gives -= d
		}
		if len(*h) < ans {
			ans = len(*h)
		}
		for j := i; j < n; j++ {
			if ts[j].balls < myBalls {
				gives += myBalls - ts[j].balls
				myBalls = ts[j].balls
				break
			}
		}
	}
	Fprint(out, ans+1)
}

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


You're given an array $$$b$$$ of length $$$n$$$. Let's define another array $$$a$$$, also of length $$$n$$$, for which $$$a_i = 2^{b_i}$$$ ($$$1 \leq i \leq n$$$). Valerii says that every two non-intersecting subarrays of $$$a$$$ have different sums of elements. You want to determine if he is wrong. More formally, you need to determine if there exist four integers $$$l_1,r_1,l_2,r_2$$$ that satisfy the following conditions:   $$$1 \leq l_1 \leq r_1 \lt l_2 \leq r_2 \leq n$$$;  $$$a_{l_1}+a_{l_1+1}+\ldots+a_{r_1-1}+a_{r_1} = a_{l_2}+a_{l_2+1}+\ldots+a_{r_2-1}+a_{r_2}$$$. If such four integers exist, you will prove Valerii wrong. Do they exist?An array $$$c$$$ is a subarray of an array $$$d$$$ if $$$c$$$ can be obtained from $$$d$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The first line of every test case contains a single integer $$$n$$$ ($$$2 \le n \le 1000$$$). The second line of every test case contains $$$n$$$ integers $$$b_1,b_2,\ldots,b_n$$$ ($$$0 \le b_i \le 10^9$$$).

For every test case, if there exist two non-intersecting subarrays in $$$a$$$ that have the same sum, output YES on a separate line. Otherwise, output NO on a separate line.  Also, note that each letter can be in any case.

NoteIn the first case, $$$a = [16,8,1,2,4,1]$$$. Choosing $$$l_1 = 1$$$, $$$r_1 = 1$$$, $$$l_2 = 2$$$ and $$$r_2 = 6$$$ works because $$$16 = (8+1+2+4+1)$$$.In the second case, you can verify that there is no way to select to such subarrays.

// Written by : Syed Mujtaba
// Seek knowledge from the cradle to the grave
package main

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

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

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

  for i := 0; i < t; i++ {
  	var n int
  	scanf("%d\n", &n)
  	mp := make(map[int]int)
  	for j := 0; j < n; j++ {
  		var x int
  		scanf("%d", &x)
  		mp[x] = 1
  		if j != n - 1 {
  			scanf(" ")
  		} else {
  			scanf("\n")
  		}
  	}
  	if len(mp) == n {
  		printf("NO\n")
  	} else {
  		printf("YES\n")
  	}
  }
}

package main

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

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

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

func solveA() {
	n := 0
	scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		printf("1 ")
	}
	printf("\n")
}

func solveB() {
	n := 0
	scanf("%d\n", &n)
	d := make(map[int]int)
	ans := "NO"
	for k, i := 0, 0; i < n; i++ {
		if i + 1 == n {
			scanf("%d\n", &k)
		} else {
			scanf("%d ", &k)
		}
		d[k]++
		if d[k] > 1 {
			ans = "YES"
		}
	}
	printf(ans + "\n")
}

func main() {
	defer writer.Flush()
	var t int
	scanf("%d\n", &t)
	for ; t > 0; t-- {
		solveB()
	}
}

/*
URL:
https://codeforces.com/contest/1438/problem/B
*/

package main

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

var (
	t int

	n int
	B []int
)

func main() {
	defer stdout.Flush()

	t = readi()
	for tc := 0; tc < t; tc++ {
		n = readi()
		B = readis(n)

		solve()
	}
}

func solve() {
	memo := make(map[int]bool)
	for _, b := range B {
		memo[b] = true
	}

	if len(memo) == n {
		printf("NO\n")
	} else {
		printf("YES\n")
	}
}

/*******************************************************************/

/********** common constants **********/

const (
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPH_N  = 26
	INF_I64 = math.MaxInt64
	INF_B60 = 1 << 60
	INF_I32 = math.MaxInt32
	INF_B30 = 1 << 30
	NIL     = -1
	EPS     = 1e-10
)

/********** bufio setting **********/

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	reads = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

// mod can calculate a right residual whether value is positive or negative.
func mod(val, m int) int {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

// min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// chmin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func chmin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// chmax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func chmax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// sum returns multiple integers sum.
func sum(integers ...int) int {
	var s int
	s = 0

	for _, i := range integers {
		s += i
	}

	return s
}

// abs is integer version of math.Abs
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// pow is integer version of math.Pow
// pow calculate a power by Binary Power (二分累乗法(O(log e))).
func pow(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := pow(a, halfE)
		return half * half
	}

	return a * pow(a, e-1)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

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

//str := reads()
//i := readi()
//X := readis(n)
//S := readrs()
//a := readf()
//A := readfs(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*********** Input ***********/

var (
	// reads returns a WORD string.
	reads  func() string
	stdout *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// readi returns an integer.
func readi() int {
	return int(_readInt64())
}
func readi2() (int, int) {
	return int(_readInt64()), int(_readInt64())
}
func readi3() (int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64())
}
func readi4() (int, int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64()), int(_readInt64())
}

// readll returns as integer as int64.
func readll() int64 {
	return _readInt64()
}
func readll2() (int64, int64) {
	return _readInt64(), _readInt64()
}
func readll3() (int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64()
}
func readll4() (int64, int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64(), _readInt64()
}

func _readInt64() int64 {
	i, err := strconv.ParseInt(reads(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// readis returns an integer slice that has n integers.
func readis(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = readi()
	}
	return b
}

// readlls returns as int64 slice that has n integers.
func readlls(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = readll()
	}
	return b
}

// readf returns an float64.
func readf() float64 {
	return float64(_readFloat64())
}

func _readFloat64() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func readfs(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = readf()
	}
	return b
}

// readrs returns a rune slice.
func readrs() []rune {
	return []rune(reads())
}

/*********** Output ***********/

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// Printf is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func printf(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

/*********** Debugging ***********/

// debugf is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func debugf(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

package main

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

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

	solve := func(Case int) bool {
		var n int
		Fscan(in, &n)
		b := make([]int, n)
		for i := range b {
			Fscan(in, &b[i])
		}
		cnts := map[int]int{}
		for _, v := range b {
			cnts[v]++
			if cnts[v] > 1 {
				return true
			}
		}
		return  false
	}

	var t int
	Fscan(in, &t)
	for Case := 1; Case <= t; Case++ {
		Fprintln(out, map[bool]string{true: "YES", false: "NO"}[solve(Case)])
	}
}

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

package main

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

func main() {
	//rd, _ := os.Open("input")
	//in := bufio.NewReader(rd)
	in := bufio.NewReader(os.Stdin)

	var t, n int
	var b []int
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &n)
		b = make([]int, n)
		for i := range b {
			fmt.Fscan(in, &b[i])
		}
		sort.Ints(b)
		result := false
		for i := 1; i < n; i++ {
			if b[i] == b[i-1] {
				result = true
				break
			}
		}
		if result {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

var reader = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(reader, &n)

		b := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(reader, &b[j])
		}

		sort.Ints(b)

		result := "NO"
		for j := 1; j < n; j++ {
			if b[j] == b[j-1] {
				result = "YES"
				break
			}
		}

		fmt.Println(result)
	}
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF = int64(math.MaxInt64)
	MOD = 1e9 + 7
)

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

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

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		printf("%s\n", sol(a))
	}
}

func sol(a []int64) string {
	cache := map[int64]bool{}
	for _, d := range a {
		if cache[d] {
			return "YES"
		}
		cache[d] = true
	}
	return "NO"
}

// Written by : Syed Mujtaba
// Seek knowledge from the cradle to the grave
package main

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

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

func main() {
  defer writer.Flush()
  var t int
  scanf("%d\n", &t)

  for i := 0; i < t; i++ {
  	var n int
  	scanf("%d\n", &n)
  	mp := make(map[int]int)
  	for j := 0; j < n; j++ {
  		var x int
  		scanf("%d ", &x)
  		mp[x] = 1
  	}
  	if len(mp) == n {
  		printf("NO\n")
  	} else {
  		printf("YES\n")
  	}
  }
}

package main

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

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

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

func solveA() {
	n := 0
	scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		printf("1 ")
	}
	printf("\n")
}

func solveB() {
	n := 0
	scanf("%d\n", &n)
	d := make(map[int]int)
	ans := "NO"
	for k, i := 0, 0; i < n; i++ {
		scanf("%d ", &k)
		d[k]++
		if d[k] > 1 {
			ans = "YES"
		}
	}
	printf(ans + "\n")
}

func main() {
	defer writer.Flush()
	var t int
	scanf("%d\n", &t)
	for ; t > 0; t-- {
		solveB()
	}
}

package main

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

func main() {
	rd, _ := os.Open("input")
	in := bufio.NewReader(rd)
	//in := bufio.NewReader(os.Stdin)

	var t, n int
	var b []int
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &n)
		b = make([]int, n)
		for i := range b {
			fmt.Fscan(in, &b[i])
		}
		sort.Ints(b)
		result := false
		for i := 1; i < n; i++ {
			if b[i] == b[i-1] {
				result = true
				break
			}
		}
		if result {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}


A bracket sequence is a string containing only characters "(" and ")".A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not.You are given $$$n$$$ bracket sequences $$$s_1, s_2, \dots , s_n$$$. Calculate the number of pairs $$$i, j \, (1 \le i, j \le n)$$$ such that the bracket sequence $$$s_i + s_j$$$ is a regular bracket sequence. Operation $$$+$$$ means concatenation i.e. "()(" + ")()" = "()()()".If $$$s_i + s_j$$$ and $$$s_j + s_i$$$ are regular bracket sequences and $$$i \ne j$$$, then both pairs $$$(i, j)$$$ and $$$(j, i)$$$ must be counted in the answer. Also, if $$$s_i + s_i$$$ is a regular bracket sequence, the pair $$$(i, i)$$$ must be counted in the answer.

The first line contains one integer $$$n \, (1 \le n \le 3 \cdot 10^5)$$$ — the number of bracket sequences. The following $$$n$$$ lines contain bracket sequences — non-empty strings consisting only of characters "(" and ")". The sum of lengths of all bracket sequences does not exceed $$$3 \cdot 10^5$$$.

In the single line print a single integer — the number of pairs $$$i, j \, (1 \le i, j \le n)$$$ such that the bracket sequence $$$s_i + s_j$$$ is a regular bracket sequence.

NoteIn the first example, suitable pairs are $$$(3, 1)$$$ and $$$(2, 2)$$$.In the second example, any pair is suitable, namely $$$(1, 1), (1, 2), (2, 1), (2, 2)$$$.

package main

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

type Parentheses struct {
	sum    int
	lowest int
	cnt    int
}

func NewParentheses(str string) *Parentheses {
	sum, lowest := getSum(str)
	par := &Parentheses{}
	par.sum = sum
	par.lowest = lowest
	par.cnt = 1
	return par
}

func solve() {
	n := readInt()
	a := make([]string, n)
	p := make(map[string]*Parentheses)
	for i := 0; i < n; i++ {
		str := readString()
		if prev, exists := p[str]; exists {
			prev.cnt++
		} else {
			p[str] = NewParentheses(str)
		}
		a[i] = str
	}
	ans := int64(0)
	for _, p1 := range p {
		if p1.lowest < 0 {
			continue
		}
		for _, p2 := range p {
			if p1.sum+p2.lowest < 0 {
				continue
			}
			if p1.sum+p2.sum == 0 {
				ans += int64(p1.cnt) * int64(p2.cnt)
			}
		}
	}
	fmt.Println(ans)
}

func getSum(str string) (int, int) {
	sum := 0
	lowest := 0
	for _, ch := range str {
		if ch == '(' {
			sum++
		} else {
			sum--
		}
		lowest = min(lowest, sum)
	}
	return sum, lowest
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

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

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

package main

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

func solve() {
	n := readInt()
	a := make([]string, n)
	cnt := make(map[string]int, 0)
	for i := 0; i < n; i++ {
		a[i] = readString()
		cnt[a[i]]++
	}
	ans := int64(0)
	for k1, v1 := range cnt {
		cntOpen := 0
		ok := true
		for _, ch := range k1 {
			if ch == '(' {
				cntOpen++
			} else {
				cntOpen--
			}
			if cntOpen < 0 {
				ok = false
				break
			}
		}
		if !ok {
			continue
		}
		for k2, v2 := range cnt {
			tmpCntOpen := cntOpen
			for _, ch := range k2 {
				if ch == '(' {
					tmpCntOpen++
				} else {
					tmpCntOpen--
				}
			}
			if ok && tmpCntOpen == 0 {
				ans += int64(v2) * int64(v1)
			}
		}
	}
	fmt.Println(ans)
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

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

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


This is the hard version of the problem. The only difference is that in this version $$$0 \leq k \leq 20$$$.There is an array $$$a_1, a_2, \ldots, a_n$$$ of $$$n$$$ positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square.Moreover, it is allowed to do at most $$$k$$$ such operations before the division: choose a number in the array and change its value to any positive integer.What is the minimum number of continuous segments you should use if you will make changes optimally?

The first line contains a single integer $$$t$$$ $$$(1 \le t \le 1000)$$$  — the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \leq k \leq 20$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^7$$$). It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case print a single integer  — the answer to the problem.

NoteIn the first test case it is possible to change the array this way: $$$[\underline{3}, 6, 2, 4, \underline{5}]$$$ (changed elements are underlined). After that the array does not need to be divided, so the answer is $$$1$$$.In the second test case it is possible to change the array this way: $$$[6, 2, \underline{3}, 8, 9, \underline{5}, 3, 6, \underline{10}, \underline{11}, 7]$$$. After that such division is optimal:   $$$[6, 2, 3]$$$  $$$[8, 9, 5, 3, 6, 10, 11, 7]$$$

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
)

// 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 mx int = 1e7
	lpf := make([]int, mx+1)
	lpf[1] = 1
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			for j := i; j <= mx; j += i {
				if lpf[j] == 0 {
					lpf[j] = i
				}
			}
		}
	}
	core := func(x int) int {
		c := 1
		for x > 1 {
			p := lpf[x]
			e := 1
			for x /= p; lpf[x] == p; x /= p {
				e ^= 1
			}
			if e > 0 {
				c *= p
			}
		}
		return c
	}

	solve := func(Case int) {
		var n, k int
		Fscan(in, &n, &k)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			a[i] = core(a[i])
		}
		//Println(a)

		dp := make([][]int, n)
		for i := range dp {
			dp[i] = make([]int, k+1)
			for j := range dp[i] {
				dp[i][j] = -1
			}
		}
		cache := make([][]int, n)
		var f func(int, int) int
		f = func(p, left int) (res int) {
			if p == n {
				return
			}
			dv := &dp[p][left]
			if *dv != -1 {
				return *dv
			}
			defer func() { *dv = res }()
			res = 1e9

			ch := cache[p]
			if ch != nil {
				for _, q := range ch {
					res = min(res, 1+f(q, left))
					if left == 0 {
						break
					}
					left--
				}
				return
			}
			pp := p
			has := map[int]bool{}
			for ; p < n; p++ {
				if has[a[p]] {
					res = min(res, 1+f(p, left)) // cache ps[p...]
					if left == 0 {
						break
					}
					ch = append(ch, p)
					left--
					continue
				}
				has[a[p]] = true
			}
			ch = append(ch, p)
			cache[pp]=ch
			if p == n {
				return 1
			}
			return
		}
		ans := f(0, k)

		//ans := 0
		//for i := 0; i < n; {
		//	has := map[int]bool{}
		//	for ; i < n; i++ {
		//		if has[a[i]] {
		//			if k == 0 {
		//				break
		//			}
		//			k--
		//			continue
		//		}
		//		has[a[i]] = true
		//	}
		//	ans++
		//}
		Fprintln(out, ans)
	}

	var t int
	Fscan(in, &t)
	for Case := 1; Case <= t; Case++ {
		solve(Case)
	}

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

func main() { run(os.Stdin, os.Stdout) }
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
)

// 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 mx int = 1e7
	lpf := make([]int, mx+1)
	lpf[1] = 1
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			for j := i; j <= mx; j += i {
				if lpf[j] == 0 {
					lpf[j] = i
				}
			}
		}
	}
	core := func(x int) int {
		c := 1
		for x > 1 {
			p := lpf[x]
			e := 1
			for x /= p; lpf[x] == p; x /= p {
				e ^= 1
			}
			if e > 0 {
				c *= p
			}
		}
		return c
	}

	solve := func(Case int) {
		var n, k int
		Fscan(in, &n, &k)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			a[i] = core(a[i])
		}
		//Println(a)
		ans := 0
		for i := 0; i < n; {
			has := map[int]bool{}
			for ; i < n; i++ {
				if has[a[i]] {
					if k == 0 {
						break
					}
					k--
					continue
				}
				has[a[i]] = true
			}
			ans++
		}
		Fprintln(out, ans)
	}

	var t int
	Fscan(in, &t)
	for Case := 1; Case <= t; Case++ {
		solve(Case)
	}

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

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


You have a multiset containing several integers. Initially, it contains $$$a_1$$$ elements equal to $$$1$$$, $$$a_2$$$ elements equal to $$$2$$$, ..., $$$a_n$$$ elements equal to $$$n$$$.You may apply two types of operations:  choose two integers $$$l$$$ and $$$r$$$ ($$$l \le r$$$), then remove one occurrence of $$$l$$$, one occurrence of $$$l + 1$$$, ..., one occurrence of $$$r$$$ from the multiset. This operation can be applied only if each number from $$$l$$$ to $$$r$$$ occurs at least once in the multiset;  choose two integers $$$i$$$ and $$$x$$$ ($$$x \ge 1$$$), then remove $$$x$$$ occurrences of $$$i$$$ from the multiset. This operation can be applied only if the multiset contains at least $$$x$$$ occurrences of $$$i$$$. What is the minimum number of operations required to delete all elements from the multiset?

The first line contains one integer $$$n$$$ ($$$1 \le n \le 5000$$$). The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$).

Print one integer — the minimum number of operations required to delete all elements from the multiset.

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	n := readNum(reader)
	A := readNNums(reader, n)
	fmt.Println(solve(n, A))
}

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

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

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

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

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

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

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

	return i
}

func solve(n int, A []int) int {

	var loop func(l, r int, x int) int

	loop = func(l, r int, x int) int {
		y := A[l]
		for i := l + 1; i <= r; i++ {
			if A[i] < y {
				y = A[i]
			}
		}
		cnt := r - l + 1
		if y-x >= cnt {
			return cnt
		}
		res := y - x
		i := l
		for i <= r {
			if A[i] == y {
				i++
				continue
			}
			// A[i] != y
			j := i
			for i <= r && A[i] > y {
				i++
			}
			res += loop(j, i-1, y)
		}
		return min(res, cnt)
	}

	return loop(0, n-1, 0)
}

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

package main

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

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n := io.NextInt()
	a := io.NextInts(n)

	var solve func(l, r, d int) int64
	solve = func(l, r, d int) int64 {
		if l == r {
			return 0
		}
		m := l
		for i := l + 1; i < r; i++ {
			if a[i] < a[m] {
				m = i
			}
		}
		ver := int64(r - l)
		res := int64(a[m]-d) + solve(l, m, a[m]) + solve(m+1, r, a[m])
		if ver < res {
			return ver
		}
		return res
	}

	io.Println(solve(0, n, 0))
}

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1400E(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}

	var f func([]int, int) int
	f = func(a []int, low int) int {
		if len(a) == 0 {
			return 0
		}
		mi := int(1e9)
		for _, v := range a {
			mi = min(mi, v)
		}
		c, p := mi-low, 0
		for i, v := range a {
			if v == mi {
				c += f(a[p:i], mi)
				p = i + 1
			}
		}
		c += f(a[p:], mi)
		return min(c, len(a))
	}
	var n int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	Fprint(out, f(a, 0))
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1400E(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}

	var f func([]int, int) int
	f = func(a []int, low int) int {
		if len(a) <= 1 {
			return len(a)
		}
		mi := int(1e9)
		for _, v := range a {
			mi = min(mi, v)
		}
		c, p := mi-low, 0
		for i, v := range a {
			if v == mi {
				c += f(a[p:i], mi)
				p = i + 1
			}
		}
		c += f(a[p:], mi)
		return min(c, len(a))
	}
	var n int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	Fprint(out, f(a, 0))
}

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


Author has gone out of the stories about Vasiliy, so here is just a formal task description.You are given q queries and a multiset A, initially containing only integer 0. There are three types of queries:  "+ x" — add integer x to multiset A. "- x" — erase one occurrence of integer x from multiset A. It's guaranteed that at least one x is present in the multiset A before this query. "? x" — you are given integer x and need to compute the value , i.e. the maximum value of bitwise exclusive OR (also know as XOR) of integer x and some integer y from the multiset A.Multiset is a set, where equal elements are allowed.

The first line of the input contains a single integer q (1 ≤ q ≤ 200 000) — the number of queries Vasiliy has to perform. Each of the following q lines of the input contains one of three characters '+', '-' or '?' and an integer xi (1 ≤ xi ≤ 109). It's guaranteed that there is at least one query of the third type. Note, that the integer 0 will always be present in the set A.

For each query of the type '?' print one integer — the maximum value of bitwise exclusive OR (XOR) of integer xi and some integer from the multiset A.

NoteAfter first five operations multiset A contains integers 0, 8, 9, 11, 6 and 1.The answer for the sixth query is integer  — maximum among integers , , ,  and .

package main

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

type node struct {
	counter int
	child   [2]*node
}

var root = &node{}

func alloc(pn **node) *node {
	if *pn == nil {
		*pn = &node{}
	}
	return *pn
}

func visitRec(n *node, val uint, mask uint, delta int) {
	n.counter += delta
	if mask == 0 {
		return
	}
	idx := 0
	if val&mask != 0 {
		idx = 1
	}
	visitRec(alloc(&n.child[idx]), val, mask>>1, delta)
}

func add(val uint) {
	visitRec(root, val, 0x80000000, 1)
}

func remove(val uint) {
	visitRec(root, val, 0x80000000, -1)
}

func findRec(n *node, val uint, mask uint) uint {
	if mask == 0 {
		return 0
	}
	idx := 0
	if val&mask != 0 {
		idx = 1
	}
	if n.child[idx] != nil && n.child[idx].counter > 0 {
		return mask + findRec(n.child[idx], val, mask>>1)
	} else {
		return findRec(n.child[1-idx], val, mask>>1)
	}
}

func find(val uint) uint {
	return findRec(root, val, 0x80000000)
}

func main() {
	in := bufio.NewScanner(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	in.Split(bufio.ScanWords)
	defer out.Flush()

	in.Scan()
	entries, _ := strconv.Atoi(in.Text())

	values := make([]uint, 0)
	add(0)

	for i := 0; i < entries; i++ {
		in.Scan()
		sign := in.Text()

		in.Scan()
		x, _ := strconv.Atoi(in.Text())
		value := uint(x)

		switch sign {
		case "+":
			add(value)
		case "-":
			remove(value)
		case "?":
			values = append(values, find(^value))
		}
	}
	for _, x := range values {
		fmt.Printf("%d\n", x)
	}
}

package main

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

type Node struct {
	left *Node
	right *Node
	value int
}

const (
	BYTE_COUNT = 31
)

func (n *Node) Change (numberBytes []int, delta int) {
	if len(numberBytes) == 0 {
		return
	}

	numberByte := numberBytes[0]

	if numberByte == 0 {
		if n.left == nil { n.left = &Node{} }
		n.left.value += delta
		n.left.Change(numberBytes[1:], delta)
	} else {
		if n.right == nil { n.right = &Node{} }
		n.right.value += delta
		n.right.Change(numberBytes[1:], delta)
	}
}

func (n *Node) FindMax (numberBytes []int) []int {
	result := make([]int, 0)
	currentNode := n
	for _, numberByte := range numberBytes {
		if numberByte == 0 {
			if currentNode.right != nil && currentNode.right.value > 0 {
				result = append(result, 1)
				currentNode = currentNode.right
			} else {
				result = append(result, 0)
				currentNode = currentNode.left
			}
		} else {
			if currentNode.left != nil && currentNode.left.value > 0 {
				result = append(result, 1)
				currentNode = currentNode.left
			} else {
				result = append(result, 0)
				currentNode = currentNode.right
			}
		}
	}
	return result
}

func leftPad2Len(s string, padStr string, overallLen int) string {
	var padCountInt int
	padCountInt = 1 + ((overallLen-len(padStr))/len(padStr))
	var retStr = strings.Repeat(padStr, padCountInt) + s
	return retStr[(len(retStr)-overallLen):]
}

func toBytes(x int) []int {
	var err error
	var parsedInt int64
	result := make([]int, 0)
	for _, i := range leftPad2Len(fmt.Sprintf("%b", x), "0", BYTE_COUNT) {
		parsedInt, err = strconv.ParseInt(string(i), 10, 0)
		if err != nil {
			fmt.Println("ANOTHER_ZALUPEHA", err)
		}
		result = append(result, int(parsedInt))
	}
	return result
}

func fromBytes(numberBytes []int) int {
	strs := make([]string, len(numberBytes))
	for idx, numberByte := range numberBytes {
		strs[idx] = strconv.Itoa(numberByte)
	}

	result, err := strconv.ParseInt(strings.Join(strs, ""), 2, 0)
	if err != nil {
		fmt.Println("ZALUPEHA", err)
	}

	return int(result)
}

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}

func getI() int {
  return int(getI64())
}

func getS() string {
  scanner.Scan()
  return scanner.Text()
}

func main()  {
	var (
		q, x int
		command string
	)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	q = getI()

	root := Node{}
	root.Change(toBytes(0), 1)

	for i := 0; i < q; i++ {
		command, x = getS(), getI()

		//fmt.Println(command, x)

		if command == "+" {
			root.Change(toBytes(x), 1)
		} else if command == "-" {
			root.Change(toBytes(x), -1)
		} else {
			fmt.Println(fromBytes(root.FindMax(toBytes(x))))
		}
	}

}

package main

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

var in *bufio.Scanner
var out *bufio.Writer

// Node is a Trie node
type Node struct {
	val   int
	child [2]*Node
}

func create(v int) *Node {
	r := &Node{
		val:   0,
		child: [2]*Node{nil, nil},
	}
	return r
}

func add(x int, i int, node *Node) {
	node.val++
	if i < 0 {
		return
	}
	bit := (x >> uint(i)) & 1
	if node.child[bit] == nil {
		node.child[bit] = create(0)
	}
	add(x, i-1, node.child[bit])
}

func del(x int, i int, node *Node) {
	node.val--
	if i < 0 {
		return
	}
	bit := (x >> uint(i)) & 1
	del(x, i-1, node.child[bit])
}

func query(x int, i int, node *Node, res int) int {
	if i < 0 {
		return res
	}

	bit := (x >> uint(i)) & 1
	rbit := 1 - bit

	if (node.child[rbit] != nil) && (node.child[rbit].val > 0) {
		return query(x, i-1, node.child[rbit], res+(1<<uint(i)))
	}

	if (node.child[bit] != nil) && (node.child[bit].val > 0) {
		return query(x, i-1, node.child[bit], res)
	}

	return -1
}

const maxbit = 30

var n int
var root *Node

func main() {
	in = bufio.NewScanner(os.Stdin)
	out = bufio.NewWriter(os.Stdout)

	in.Split(bufio.ScanWords)
	defer out.Flush()

	in.Scan()
	n, _ = strconv.Atoi(in.Text())

	root = create(0)
	add(0, maxbit, root)

	for i := 0; i < n; i++ {
		in.Scan()
		c := in.Text()

		in.Scan()
		x, _ := strconv.Atoi(in.Text())

		if c == "+" {
			add(x, maxbit, root)
		} else if c == "-" {
			del(x, maxbit, root)
		} else {
			res := query(x, maxbit, root, 0)
			out.WriteString(strconv.Itoa(res))
			out.WriteByte('\n')
		}
	}
}

package main

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

var scanner *bufio.Scanner

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

type Trie struct {
  Next []*Trie
  Count int
}

const numBits = 30

func newTrie() *Trie {
  return &Trie{ make([]*Trie, 2), 0 }
}

func insert(trie *Trie, x, delta int) {
  mask := 1 << (numBits - 1)
  for mask > 0 {
    a := 0
    if x & mask > 0 {
      a = 1
    }
    if trie.Next[a] == nil {
      trie.Next[a] = newTrie()
    }
    trie = trie.Next[a]
    trie.Count += delta
    mask >>= 1
  }
}

func xor(trie *Trie, x int) int {
  mask := 1 << (numBits - 1)
  result := 0
  for mask > 0 {
    if mask & x == 0 && trie.Next[1] != nil && trie.Next[1].Count > 0 {
      result |= mask
      trie = trie.Next[1]
    } else if mask & x != 0 && trie.Next[0] != nil && trie.Next[0].Count > 0 {
      result |= mask
      trie = trie.Next[0]
    } else if trie.Next[0] != nil && trie.Next[0].Count > 0 {
      trie = trie.Next[0]
    } else {
      trie = trie.Next[1]
    }
    mask >>= 1
  }
  return result
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  q := getI()
  trie := newTrie()
  insert(trie, 0, 1)
  for qi := 0; qi < q; qi++ {
    op, x := getS(), getI()
    if op == "+" {
      insert(trie, x, 1)
    } else if op == "-" {
      insert(trie, x, -1)
    } else {
      writer.WriteString(fmt.Sprintf("%d\n", xor(trie, x)))
    }
  }
}

package main

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

// node represents a node in a trie that is used to track the elements of the multiset.
// The trie has 33 levels of nodes and so 32 levels of edges.
type node struct {
	count    int
	children [2]*node
}

var root *node = &node{}

func alloc(pn **node) *node {
	if *pn == nil {
		*pn = &node{}
	}
	return *pn
}

func visitRec(n *node, val uint, mask uint, delta int) {
	n.count += delta
	if mask == 0 {
		return
	}
	idx := 0
	if val&mask != 0 {
		idx = 1
	}
	visitRec(alloc(&n.children[idx]), val, mask>>1, delta)
}

func add(val uint) {
	visitRec(root, val, 0x80000000, 1)
}

func remove(val uint) {
	visitRec(root, val, 0x80000000, -1)
}

func findRec(n *node, val uint, mask uint) uint {
	if mask == 0 {
		return 0
	}
	idx := 0
	if val&mask != 0 {
		idx = 1
	}
	if n.children[idx] != nil && n.children[idx].count > 0 {
		return mask + findRec(n.children[idx], val, mask>>1)
	} else {
		return findRec(n.children[1-idx], val, mask>>1)
	}
}

func find(val uint) uint {
	return findRec(root, val, 0x80000000)
}

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

	var q int
	fmt.Fscanf(reader, "%d ", &q)
	ret := make([]uint, 0)
	add(0)
	for i := 0; i < q; i++ {
		var op string
		fmt.Fscanf(reader, "%s ", &op)
		var val uint
		fmt.Fscanf(reader, "%d ", &val)
		switch op {
		case "+":
			add(val)
		case "-":
			remove(val)
		case "?":
			ret = append(ret, find(^val))
		}
	}
	for _, x := range ret {
		fmt.Printf("%d\n", x)
	}
}

package main

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

var _x = uint(1)

func fastRand() uint {
	_x ^= _x << 13
	_x ^= _x >> 17
	_x ^= _x << 5
	return _x
}

type node struct {
	lr       [2]*node
	priority uint
	key      int
	value    int
}

func (o *node) rotate(d int) *node {
	x := o.lr[d^1]
	o.lr[d^1] = x.lr[d]
	x.lr[d] = o
	return x
}

type treap struct {
	root       *node
	comparator func(a, b int) int
}

func newTreap() *treap {
	return &treap{comparator: func(a, b int) int {
		if a < b {
			return 0
		}
		if a > b {
			return 1
		}
		return -1
	}}
}

func (t *treap) _put(o *node, key int) *node {
	if o == nil {
		return &node{priority: fastRand(), key: key, value: 1}
	}
	if cmp := t.comparator(key, o.key); cmp >= 0 {
		o.lr[cmp] = t._put(o.lr[cmp], key)
		if o.lr[cmp].priority > o.priority {
			o = o.rotate(cmp ^ 1)
		}
	} else {
		o.value++
	}
	return o
}

func (t *treap) put(key int) { t.root = t._put(t.root, key) }

func (t *treap) _delete(o *node, key int) *node {
	if o == nil {
		return nil
	}
	if cmp := t.comparator(key, o.key); cmp >= 0 {
		o.lr[cmp] = t._delete(o.lr[cmp], key)
	} else {
		if o.value > 1 {
			o.value--
		} else {
			if o.lr[1] == nil {
				return o.lr[0]
			}
			if o.lr[0] == nil {
				return o.lr[1]
			}
			cmp2 := 0
			if o.lr[0].priority > o.lr[1].priority {
				cmp2 = 1
			}
			o = o.rotate(cmp2)
			o.lr[cmp2] = t._delete(o.lr[cmp2], key)
		}
	}
	return o
}

func (t *treap) delete(key int) { t.root = t._delete(t.root, key) }

func (t *treap) max() int {
	var max *node
	for o := t.root; o != nil; o = o.lr[1] {
		max = o
	}
	return max.key
}

func (t *treap) floor(key int) int {
	var floor *node
	for o := t.root; o != nil; {
		switch cmp := t.comparator(key, o.key); {
		case cmp == 0:
			o = o.lr[0]
		case cmp > 0:
			floor = o
			o = o.lr[1]
		default:
			return o.key
		}
	}
	return floor.key
}

// github.com/EndlessCheng/codeforces-go
func Sol706D(reader io.Reader, writer io.Writer) {
	bitLength := func(n int) (c int) {
		for ; n > 0; n >>= 1 {
			c++
		}
		return
	}
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	t := newTreap()
	t.put(0)

	nextUp := func(bitPos int, up, x int) (int, int) {
		for i := bitPos; i >= 0; i-- {
			ui := uint(i)
			if up>>ui&1 == 1 && x>>ui&1 == 1 {
				check := up&^(1<<ui) | (1<<ui - 1)
				return t.floor(check), i - 1
			}
		}
		return -1, -1
	}

	var q, x int
	var op string
	for Fscan(in, &q); q > 0; q-- {
		switch Fscan(in, &op, &x); op[0] {
		case '+':
			t.put(x)
		case '-':
			t.delete(x)
		default:
			up := t.max()
			if up == 0 {
				Fprintln(out, x)
				continue
			}
			ans := up ^ x
			minL := bitLength(up)
			if l := bitLength(x); l < minL {
				minL = l
			}
			var newUp int
			for i := minL - 1; i >= 0; {
				newUp, i = nextUp(i, up, x)
				if newUp == -1 {
					break
				}
				if newXor := newUp ^ x; newXor > ans {
					ans = newXor
					up = newUp
				}
			}
			Fprintln(out, ans)
		}
	}
}

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

package main

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

var _x = uint(time.Now().UnixNano())

func fastRand() uint {
	_x ^= _x << 13
	_x ^= _x >> 17
	_x ^= _x << 5
	return _x
}

type node struct {
	lr    [2]*node
	p     uint
	key   int
	value int
}

func (o *node) rotate(d int) *node {
	x := o.lr[d^1]
	o.lr[d^1] = x.lr[d]
	x.lr[d] = o
	return x
}

type treap struct {
	root       *node
	comparator func(a, b int) int
}

func newTreap() *treap {
	return &treap{comparator: func(a, b int) int {
		if a < b {
			return 0
		}
		if a > b {
			return 1
		}
		return -1
	}}
}

func (t *treap) _put(o *node, key int) *node {
	if o == nil {
		return &node{p: fastRand(), key: key, value: 1}
	}
	if cmp := t.comparator(key, o.key); cmp >= 0 {
		o.lr[cmp] = t._put(o.lr[cmp], key)
		if o.lr[cmp].p > o.p {
			o = o.rotate(cmp ^ 1)
		}
	} else {
		o.value++
	}
	return o
}

func (t *treap) put(key int) { t.root = t._put(t.root, key) }

func (t *treap) _delete(o *node, key int) *node {
	if o == nil {
		return nil
	}
	if cmp := t.comparator(key, o.key); cmp >= 0 {
		o.lr[cmp] = t._delete(o.lr[cmp], key)
	} else {
		if o.value > 1 {
			o.value--
		} else {
			if o.lr[1] == nil {
				return o.lr[0]
			}
			if o.lr[0] == nil {
				return o.lr[1]
			}
			cmp2 := 0
			if o.lr[0].p > o.lr[1].p {
				cmp2 = 1
			}
			o = o.rotate(cmp2)
			o.lr[cmp2] = t._delete(o.lr[cmp2], key)
		}
	}
	return o
}

func (t *treap) delete(key int) { t.root = t._delete(t.root, key) }

func (t *treap) max() int {
	var max *node
	for o := t.root; o != nil; o = o.lr[1] {
		max = o
	}
	return max.key
}

func (t *treap) ceiling(key int) int {
	var ceiling *node
	for o := t.root; o != nil; {
		switch cmp := t.comparator(key, o.key); {
		case cmp == 0:
			ceiling = o
			o = o.lr[0]
		case cmp > 0:
			o = o.lr[1]
		default:
			return o.key
		}
	}
	return ceiling.key
}

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

	t := newTreap()
	t.put(0)
	var q, x int
	var op string
	for Fscan(in, &q); q > 0; q-- {
		switch Fscan(in, &op, &x); op[0] {
		case '+':
			t.put(x)
		case '-':
			t.delete(x)
		default:
			mx := t.max()
			mix := 0
			for i := 29; i >= 0; i-- {
				ui := uint(i)
				mix |= 1 << ui &^ x
				if mix > mx {
					mix ^= 1 << ui
				} else if up := t.ceiling(mix); up>>ui != mix>>ui {
					mix ^= 1 << ui
				}
			}
			Fprintln(out, mix^x)
		}
	}
}

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

package main

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

var _x = uint(time.Now().UnixNano())

func fastRand() uint {
	_x ^= _x << 13
	_x ^= _x >> 17
	_x ^= _x << 5
	return _x
}

type node struct {
	lr    [2]*node
	p     uint
	key   int
	value int
}

func (o *node) rotate(d int) *node {
	x := o.lr[d^1]
	o.lr[d^1] = x.lr[d]
	x.lr[d] = o
	return x
}

type treap struct {
	root       *node
	comparator func(a, b int) int
}

func newTreap() *treap {
	return &treap{comparator: func(a, b int) int {
		if a < b {
			return 0
		}
		if a > b {
			return 1
		}
		return -1
	}}
}

func (t *treap) _put(o *node, key int) *node {
	if o == nil {
		return &node{p: fastRand(), key: key, value: 1}
	}
	if cmp := t.comparator(key, o.key); cmp >= 0 {
		o.lr[cmp] = t._put(o.lr[cmp], key)
		if o.lr[cmp].p > o.p {
			o = o.rotate(cmp ^ 1)
		}
	} else {
		o.value++
	}
	return o
}

func (t *treap) put(key int) { t.root = t._put(t.root, key) }

func (t *treap) _delete(o *node, key int) *node {
	if o == nil {
		return nil
	}
	if cmp := t.comparator(key, o.key); cmp >= 0 {
		o.lr[cmp] = t._delete(o.lr[cmp], key)
	} else {
		if o.value > 1 {
			o.value--
		} else {
			if o.lr[1] == nil {
				return o.lr[0]
			}
			if o.lr[0] == nil {
				return o.lr[1]
			}
			cmp2 := 0
			if o.lr[0].p > o.lr[1].p {
				cmp2 = 1
			}
			o = o.rotate(cmp2)
			o.lr[cmp2] = t._delete(o.lr[cmp2], key)
		}
	}
	return o
}

func (t *treap) delete(key int) { t.root = t._delete(t.root, key) }

func (t *treap) max() int {
	var max *node
	for o := t.root; o != nil; o = o.lr[1] {
		max = o
	}
	return max.key
}

func (t *treap) ceiling(key int) int {
	var ceiling *node
	for o := t.root; o != nil; {
		switch cmp := t.comparator(key, o.key); {
		case cmp == 0:
			ceiling = o
			o = o.lr[0]
		case cmp > 0:
			o = o.lr[1]
		default:
			return o.key
		}
	}
	return ceiling.key
}

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

	t := newTreap()
	t.put(0)
	var q, x int
	var op string
	for Fscan(in, &q); q > 0; q-- {
		switch Fscan(in, &op, &x); op[0] {
		case '+':
			t.put(x)
		case '-':
			t.delete(x)
		default:
			mx := t.max()
			mix := 0
			for i := 30; i >= 0; i-- {
				ui := uint(i)
				mix |= 1 << ui &^ x
				if mix > mx {
					mix ^= 1 << ui
				} else if up := t.ceiling(mix); up>>ui != mix>>ui {
					mix ^= 1 << ui
				}
			}
			Fprintln(out, mix^x)
		}
	}
}

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

package main

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

var _x = uint(1)

func fastRand() uint {
	_x ^= _x << 13
	_x ^= _x >> 17
	_x ^= _x << 5
	return _x
}

type node struct {
	lr    [2]*node
	p     uint
	key   int
	value int
}

func (o *node) rotate(d int) *node {
	x := o.lr[d^1]
	o.lr[d^1] = x.lr[d]
	x.lr[d] = o
	return x
}

type treap struct {
	root       *node
	comparator func(a, b int) int
}

func newTreap() *treap {
	return &treap{comparator: func(a, b int) int {
		if a < b {
			return 0
		}
		if a > b {
			return 1
		}
		return -1
	}}
}

func (t *treap) _put(o *node, key int) *node {
	if o == nil {
		return &node{p: fastRand(), key: key, value: 1}
	}
	if cmp := t.comparator(key, o.key); cmp >= 0 {
		o.lr[cmp] = t._put(o.lr[cmp], key)
		if o.lr[cmp].p > o.p {
			o = o.rotate(cmp ^ 1)
		}
	} else {
		o.value++
	}
	return o
}

func (t *treap) put(key int) { t.root = t._put(t.root, key) }

func (t *treap) _delete(o *node, key int) *node {
	if o == nil {
		return nil
	}
	if cmp := t.comparator(key, o.key); cmp >= 0 {
		o.lr[cmp] = t._delete(o.lr[cmp], key)
	} else {
		if o.value > 1 {
			o.value--
		} else {
			if o.lr[1] == nil {
				return o.lr[0]
			}
			if o.lr[0] == nil {
				return o.lr[1]
			}
			cmp2 := 0
			if o.lr[0].p > o.lr[1].p {
				cmp2 = 1
			}
			o = o.rotate(cmp2)
			o.lr[cmp2] = t._delete(o.lr[cmp2], key)
		}
	}
	return o
}

func (t *treap) delete(key int) { t.root = t._delete(t.root, key) }

func (t *treap) max() int {
	var max *node
	for o := t.root; o != nil; o = o.lr[1] {
		max = o
	}
	return max.key
}

func (t *treap) ceiling(key int) int {
	var ceiling *node
	for o := t.root; o != nil; {
		switch cmp := t.comparator(key, o.key); {
		case cmp == 0:
			ceiling = o
			o = o.lr[0]
		case cmp > 0:
			o = o.lr[1]
		default:
			return o.key
		}
	}
	return ceiling.key
}

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

	t := newTreap()
	t.put(0)
	var q, x int
	var op string
	for Fscan(in, &q); q > 0; q-- {
		switch Fscan(in, &op, &x); op[0] {
		case '+':
			t.put(x)
		case '-':
			t.delete(x)
		default:
			mx := t.max()
			mix := 0
			for i := 30; i >= 0; i-- {
				ui := uint(i)
				mix |= 1 << ui &^ x
				if mix > mx {
					mix ^= 1 << ui
				} else if up := t.ceiling(mix); up>>ui != mix>>ui {
					mix ^= 1 << ui
				}
			}
			Fprintln(out, mix^x)
		}
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

var _x = uint(1)

func fastRand() uint {
	_x ^= _x << 13
	_x ^= _x >> 17
	_x ^= _x << 5
	return _x
}

type node struct {
	lr       [2]*node
	priority uint
	key      int
	value    int
}

func (o *node) rotate(d int) *node {
	x := o.lr[d^1]
	o.lr[d^1] = x.lr[d]
	x.lr[d] = o
	return x
}

type treap struct {
	root       *node
	comparator func(a, b int) int
}

func newTreap() *treap {
	return &treap{comparator: func(a, b int) int {
		if a < b {
			return 0
		}
		if a > b {
			return 1
		}
		return -1
	}}
}

func (t *treap) _put(o *node, key int) *node {
	if o == nil {
		return &node{priority: fastRand(), key: key, value: 1}
	}
	if cmp := t.comparator(key, o.key); cmp >= 0 {
		o.lr[cmp] = t._put(o.lr[cmp], key)
		if o.lr[cmp].priority > o.priority {
			o = o.rotate(cmp ^ 1)
		}
	} else {
		o.value++
	}
	return o
}

func (t *treap) put(key int) { t.root = t._put(t.root, key) }

func (t *treap) _delete(o *node, key int) *node {
	if o == nil {
		return nil
	}
	if cmp := t.comparator(key, o.key); cmp >= 0 {
		o.lr[cmp] = t._delete(o.lr[cmp], key)
	} else {
		if o.value > 1 {
			o.value--
		} else {
			if o.lr[1] == nil {
				return o.lr[0]
			}
			if o.lr[0] == nil {
				return o.lr[1]
			}
			cmp2 := 0
			if o.lr[0].priority > o.lr[1].priority {
				cmp2 = 1
			}
			o = o.rotate(cmp2)
			o.lr[cmp2] = t._delete(o.lr[cmp2], key)
		}
	}
	return o
}

func (t *treap) delete(key int) { t.root = t._delete(t.root, key) }

func (t *treap) max() int {
	var max *node
	for o := t.root; o != nil; o = o.lr[1] {
		max = o
	}
	return max.key
}

func (t *treap) floor(key int) int {
	var floor *node
	for o := t.root; o != nil; {
		switch cmp := t.comparator(key, o.key); {
		case cmp == 0:
			o = o.lr[0]
		case cmp > 0:
			floor = o
			o = o.lr[1]
		default:
			return o.key
		}
	}
	return floor.key
}

// github.com/EndlessCheng/codeforces-go
func Sol706D(reader io.Reader, writer io.Writer) {
	min := func(a, b int) int {
		if a <= b {
			return a
		}
		return b
	}
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	t := newTreap()
	t.put(0)

	nextFloor := func(bitPos int, floor, x int) (f int, i int) {
		for i = bitPos; i >= 0; i-- {
			ui := uint(i)
			bitFloor := floor >> ui & 1
			bitX := x >> ui & 1
			if bitFloor == 1 && bitX == 1 {
				// mask floor
				check := floor&^(1<<ui) | (1<<ui - 1)
				newFloor := t.floor(check)
				return newFloor, i - 1
			}
		}
		return -1, -1
	}

	var q, x int
	var op string
	for Fscan(in, &q); q > 0; q-- {
		switch Fscan(in, &op, &x); op[0] {
		case '+':
			t.put(x)
		case '-':
			t.delete(x)
		default:
			floor := t.max()
			if floor == 0 {
				Fprintln(out, x)
				continue
			}
			ans := floor ^ x
			var newFloor int
			for i := min(bits.Len(uint(floor)), bits.Len(uint(x))) - 1; i >= 0; {
				newFloor, i = nextFloor(i, floor, x)
				if newFloor == -1 {
					break
				}
				if newXor := newFloor ^ x; newXor > ans {
					ans = newXor
					floor = newFloor
				}
			}
			Fprintln(out, ans)
		}
	}
}

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

package main

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

var _x = uint(1)

func fastRand() uint {
	_x ^= _x << 13
	_x ^= _x >> 17
	_x ^= _x << 5
	return _x
}

type node struct {
	lr       [2]*node
	priority uint
	key      int
	value    int
}

func (o *node) rotate(d int) *node {
	x := o.lr[d^1]
	o.lr[d^1] = x.lr[d]
	x.lr[d] = o
	return x
}

type treap struct {
	root       *node
	comparator func(a, b int) int
}

func newTreap() *treap {
	return &treap{comparator: func(a, b int) int {
		if a < b {
			return 0
		}
		if a > b {
			return 1
		}
		return -1
	}}
}

func (t *treap) _put(o *node, key int) *node {
	if o == nil {
		return &node{priority: fastRand(), key: key, value: 1}
	}
	if cmp := t.comparator(key, o.key); cmp >= 0 {
		o.lr[cmp] = t._put(o.lr[cmp], key)
		if o.lr[cmp].priority > o.priority {
			o = o.rotate(cmp ^ 1)
		}
	} else {
		o.value++
	}
	return o
}

func (t *treap) put(key int) { t.root = t._put(t.root, key) }

func (t *treap) _delete(o *node, key int) *node {
	if o == nil {
		return nil
	}
	if cmp := t.comparator(key, o.key); cmp >= 0 {
		o.lr[cmp] = t._delete(o.lr[cmp], key)
	} else {
		if o.value > 1 {
			o.value--
		} else {
			if o.lr[1] == nil {
				return o.lr[0]
			}
			if o.lr[0] == nil {
				return o.lr[1]
			}
			cmp2 := 0
			if o.lr[0].priority > o.lr[1].priority {
				cmp2 = 1
			}
			o = o.rotate(cmp2)
			o.lr[cmp2] = t._delete(o.lr[cmp2], key)
		}
	}
	return o
}

func (t *treap) delete(key int) { t.root = t._delete(t.root, key) }

func (t *treap) max() int {
	var max *node
	for o := t.root; o != nil; o = o.lr[1] {
		max = o
	}
	return max.key
}

func (t *treap) floor(key int) int {
	var floor *node
	for o := t.root; o != nil; {
		switch cmp := t.comparator(key, o.key); {
		case cmp == 0:
			o = o.lr[0]
		case cmp > 0:
			floor = o
			o = o.lr[1]
		default:
			return o.key
		}
	}
	return floor.key
}

// github.com/EndlessCheng/codeforces-go
func Sol706D(reader io.Reader, writer io.Writer) {
	bitLength := func(n int) int {
		c := 1
		if n>>16 > 0 {
			c += 16
			n >>= 16
		}
		if n>>8 > 0 {
			c += 8
			n >>= 8
		}
		if n>>4 > 0 {
			c += 4
			n >>= 4
		}
		if n>>2 > 0 {
			c += 2
			n >>= 2
		}
		if n-1 > 0 {
			c++
		}
		return c
	}
	min := func(a, b int) int {
		if a <= b {
			return a
		}
		return b
	}
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	t := newTreap()
	t.put(0)

	nextFloor := func(bitPos int, floor, x int) (f int, i int) {
		for i = bitPos; i >= 0; i-- {
			ui := uint(i)
			bitFloor := floor >> ui & 1
			bitX := x >> ui & 1
			if bitFloor == 1 && bitX == 1 {
				// mask floor
				check := floor&^(1<<ui) | (1<<ui - 1)
				newFloor := t.floor(check)
				return newFloor, i - 1
			}
		}
		return -1, -1
	}

	var q, x int
	var op string
	for Fscan(in, &q); q > 0; q-- {
		switch Fscan(in, &op, &x); op[0] {
		case '+':
			t.put(x)
		case '-':
			t.delete(x)
		default:
			floor := t.max()
			if floor == 0 {
				Fprintln(out, x)
				continue
			}
			ans := floor ^ x
			var newFloor int
			for i := min(bitLength(floor), bitLength(x)) - 1; i >= 0; {
				newFloor, i = nextFloor(i, floor, x)
				if newFloor == -1 {
					break
				}
				if newXor := newFloor ^ x; newXor > ans {
					ans = newXor
					floor = newFloor
				}
			}
			Fprintln(out, ans)
		}
	}
}

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

package main

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

var _x = uint(1)

func fastRand() uint {
	_x ^= _x << 13
	_x ^= _x >> 17
	_x ^= _x << 5
	return _x
}

type node struct {
	lr       [2]*node
	priority uint
	key      int
	value    int
}

func (o *node) rotate(d int) *node {
	x := o.lr[d^1]
	o.lr[d^1] = x.lr[d]
	x.lr[d] = o
	return x
}

type treap struct {
	root       *node
	comparator func(a, b int) int
}

func newTreap() *treap {
	return &treap{comparator: func(a, b int) int {
		if a < b {
			return 0
		}
		if a > b {
			return 1
		}
		return -1
	}}
}

func (t *treap) _put(o *node, key int) *node {
	if o == nil {
		return &node{priority: fastRand(), key: key, value: 1}
	}
	if cmp := t.comparator(key, o.key); cmp >= 0 {
		o.lr[cmp] = t._put(o.lr[cmp], key)
		if o.lr[cmp].priority > o.priority {
			o = o.rotate(cmp ^ 1)
		}
	} else {
		o.value++
	}
	return o
}

func (t *treap) put(key int) { t.root = t._put(t.root, key) }

func (t *treap) _delete(o *node, key int) *node {
	if o == nil {
		return nil
	}
	if cmp := t.comparator(key, o.key); cmp >= 0 {
		o.lr[cmp] = t._delete(o.lr[cmp], key)
	} else {
		if o.value > 1 {
			o.value--
		} else {
			if o.lr[1] == nil {
				return o.lr[0]
			}
			if o.lr[0] == nil {
				return o.lr[1]
			}
			cmp2 := 0
			if o.lr[0].priority > o.lr[1].priority {
				cmp2 = 1
			}
			o = o.rotate(cmp2)
			o.lr[cmp2] = t._delete(o.lr[cmp2], key)
		}
	}
	return o
}

func (t *treap) delete(key int) { t.root = t._delete(t.root, key) }

func (t *treap) max() int {
	var max *node
	for o := t.root; o != nil; o = o.lr[1] {
		max = o
	}
	return max.key
}

func (t *treap) floor(key int) int {
	var floor *node
	for o := t.root; o != nil; {
		switch cmp := t.comparator(key, o.key); {
		case cmp == 0:
			o = o.lr[0]
		case cmp > 0:
			floor = o
			o = o.lr[1]
		default:
			return o.key
		}
	}
	return floor.key
}

//

func (o *node) String() string {
	var s string
	if o.value == 1 {
		s = Sprintf("%v", o.key)
	} else {
		s = Sprintf("%v(%v)", o.key, o.value)
	}
	return s
}

func (o *node) draw(prefix string, isTail bool, str *string) {
	if o.lr[1] != nil {
		newPrefix := prefix
		if isTail {
			newPrefix += "│   "
		} else {
			newPrefix += "    "
		}
		o.lr[1].draw(newPrefix, false, str)
	}
	*str += prefix
	if isTail {
		*str += "└── "
	} else {
		*str += "┌── "
	}
	*str += o.String() + "\n"
	if o.lr[0] != nil {
		newPrefix := prefix
		if isTail {
			newPrefix += "    "
		} else {
			newPrefix += "│   "
		}
		o.lr[0].draw(newPrefix, true, str)
	}
}

func (t *treap) String() string {
	if t.root == nil {
		return "BST (empty)\n"
	}
	str := "BST\n"
	t.root.draw("", true, &str)
	return str
}

func (t *treap) keys() (keys []int) {
	var o *node
	q := []*node{t.root}
	for len(q) > 0 {
		o, q = q[0], q[1:]
		if o == nil {
			continue
		}
		keys = append(keys, o.key)
		for _, ch := range o.lr {
			q = append(q, ch)
		}
	}
	sort.Ints(keys)
	return
}

// github.com/EndlessCheng/codeforces-go
func Sol706D(reader io.Reader, writer io.Writer) {
	bitLength := func(n int) int {
		c := 1
		if n>>16 > 0 {
			c += 16
			n >>= 16
		}
		if n>>8 > 0 {
			c += 8
			n >>= 8
		}
		if n>>4 > 0 {
			c += 4
			n >>= 4
		}
		if n>>2 > 0 {
			c += 2
			n >>= 2
		}
		if n-1 > 0 {
			c++
		}
		return c
	}
	min := func(a, b int) int {
		if a <= b {
			return a
		}
		return b
	}
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	t := newTreap()
	t.put(0)

	nextFloor := func(bitPos int, floor, x int) (f int, i int) {
		for i = bitPos; i >= 0; i-- {
			ui := uint(i)
			bitFloor := floor >> ui & 1
			bitX := x >> ui & 1
			if bitFloor != bitX {
				continue
			}
			if bitFloor == 1 {
				// mask floor
				check := floor&^(1<<ui) | (1<<ui - 1)
				newFloor := t.floor(check)
				return newFloor, i - 1
			}
		}
		return -1, -1
	}

	var q, x int
	var op string
	for Fscan(in, &q); q > 0; q-- {
		switch Fscan(in, &op, &x); op[0] {
		case '+':
			t.put(x)
		case '-':
			t.delete(x)
		default:
			floor := t.max()
			if floor == 0 {
				Fprintln(out, x)
				continue
			}
			ans := floor ^ x
			fl := bitLength(floor)
			xl := bitLength(x)
			minL := min(fl, xl)
			var newFloor int
			for i := minL - 1; i >= 0; {
				newFloor, i = nextFloor(i, floor, x)
				if floor == -1 {
					break
				}
				if newXor := newFloor ^ x; newXor > ans {
					ans = newXor
					floor = newFloor
				}
			}
			Fprintln(out, ans)
		}
	}
}

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

package main

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

type node struct {
	counter int
	child   [2]*node
}

var root = &node{}

func alloc(pn **node) *node {
	if *pn == nil {
		*pn = &node{}
	}
	return *pn
}

func visitRec(n *node, val uint, mask uint, delta int) {
	n.counter += delta
	if mask == 0 {
		return
	}
	idx := 0
	if val&mask != 0 {
		idx = 1
	}
	visitRec(alloc(&n.child[idx]), val, mask>>1, delta)
}

func add(val uint) {
	visitRec(root, val, 0x80000000, 1)
}

func remove(val uint) {
	visitRec(root, val, 0x80000000, -1)
}

func findRec(n *node, val uint, mask uint) uint {
	if mask == 0 {
		return 0
	}
	idx := 0
	if val&mask != 0 {
		idx = 1
	}
	if n.child[idx] != nil && n.child[idx].counter > 0 {
		return mask + findRec(n.child[idx], val, mask>>1)
	} else {
		return findRec(n.child[1-idx], val, mask>>1)
	}
}

func find(val uint) uint {
	return findRec(root, val, 0x80000000)
}

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

	var entries int
	fmt.Fscanf(reader, "%d ", &entries)
	values := make([]uint, 0)
	add(0)

	for i := 0; i < entries; i++ {
		var sign string
		fmt.Fscanf(reader, "%s ", &sign)
		var value uint
		fmt.Fscanf(reader, "%d ", &value)
		switch sign {
		case "+":
			add(value)
		case "-":
			remove(value)
		case "?":
			values = append(values, find(^value))
		}
	}
	for _, x := range values {
		fmt.Printf("%d \n", x)
	}
}

package main

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

type node struct {
	counter int
	child   [2]*node
}

var root = &node{}

func alloc(pn **node) *node {
	if *pn == nil {
		*pn = &node{}
	}
	return *pn
}

func visitRec(n *node, val uint, mask uint, delta int) {
	n.counter += delta
	if mask == 0 {
		return
	}
	idx := 0
	if val&mask != 0 {
		idx = 1
	}
	visitRec(alloc(&n.child[idx]), val, mask>>1, delta)
}

func add(val uint) {
	visitRec(root, val, 0x80000000, 1)
}

func remove(val uint) {
	visitRec(root, val, 0x80000000, -1)
}

func findRec(n *node, val uint, mask uint) uint {
	if mask == 0 {
		return 0
	}
	idx := 0
	if val&mask != 0 {
		idx = 1
	}
	if n.child[idx] != nil && n.child[idx].counter > 0 {
		return mask + findRec(n.child[idx], val, mask>>1)
	} else {
		return findRec(n.child[1-idx], val, mask>>1)
	}
}

func find(val uint) uint {
	return findRec(root, val, 0x80000000)
}

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

	var entries int
	_, _ = fmt.Fscanf(reader, "%d ", &entries)
	values := make([]uint, 0)
	add(0)

	for i := 0; i < entries; i++ {
		var sign string
		_, _ = fmt.Fscanf(reader, "%s ", &sign)
		var value uint
		_, _ = fmt.Fscanf(reader, "%d ", &value)
		switch sign {
		case "+":
			add(value)
		case "-":
			remove(value)
		case "?":
			values = append(values, find(^value))
		}
	}
	for _, x := range values {
		fmt.Printf("%d\n", x)
	}
}

package main

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

type node struct {
	counter int
	child   [2]*node
}

var root = &node{}

func alloc(pn **node) *node {
	if *pn == nil {
		*pn = &node{}
	}
	return *pn
}

func visitRec(n *node, val uint, mask uint, delta int) {
	n.counter += delta
	if mask == 0 {
		return
	}
	idx := 0
	if val&mask != 0 {
		idx = 1
	}
	visitRec(alloc(&n.child[idx]), val, mask>>1, delta)
}

func add(val uint) {
	visitRec(root, val, 0x80000000, 1)
}

func remove(val uint) {
	visitRec(root, val, 0x80000000, -1)
}

func findRec(n *node, val uint, mask uint) uint {
	if mask == 0 {
		return 0
	}
	idx := 0
	if val&mask != 0 {
		idx = 1
	}
	if n.child[idx] != nil && n.child[idx].counter > 0 {
		return mask + findRec(n.child[idx], val, mask>>1)
	} else {
		return findRec(n.child[1-idx], val, mask>>1)
	}
}

func find(val uint) uint {
	return findRec(root, val, 0x80000000)
}

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

	var entries int
	fmt.Fscanf(reader, "%d ", &entries)
	values := make([]uint, 0)
	add(0)

	for i := 0; i < entries; i++ {
		var sign string
		fmt.Fscanf(reader, "%s ", &sign)
		var value uint
		fmt.Fscanf(reader, "%d ", &value)
		switch sign {
		case "+":
			add(value)
		case "-":
			remove(value)
		case "?":
			values = append(values, find(^value))
		}
	}
	for _, x := range values {
		fmt.Printf("%d\n", x)
	}
}

package main

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

var in *bufio.Scanner
var out *bufio.Writer

// Node is a Trie node
type Node struct {
	val   int
	child [2]*Node
}

func create(v int) *Node {
	r := &Node{
		val:   0,
		child: [2]*Node{nil, nil},
	}
	return r
}

func add(x int, i int, node *Node) {
	node.val++
	if i < 0 {
		return
	}
	bit := (x >> uint(i)) & 1
	if node.child[bit] == nil {
		node.child[bit] = create(0)
	}
	add(x, i-1, node.child[bit])
}

func del(x int, i int, node *Node) {
	node.val--
	if i < 0 {
		return
	}
	bit := (x >> uint(i)) & 1
	del(x, i-1, node.child[bit])
}

func query(x int, i int, node *Node, res int) int {
	if i < 0 {
		return res
	}

	bit := (x >> uint(i)) & 1
	rbit := 1 - bit

	if (node.child[rbit] != nil) && (node.child[rbit].val > 0) {
		return query(x, i-1, node.child[rbit], res+(1<<uint(i)))
	}

	if (node.child[bit] != nil) && (node.child[bit].val > 0) {
		return query(x, i-1, node.child[bit], res)
	}

	return -1
}

const maxbit = 30

var n int
var root *Node

func main() {
	in = bufio.NewScanner(os.Stdin)
	out = bufio.NewWriter(os.Stdout)

	in.Split(bufio.ScanWords)
	defer out.Flush()

	in.Scan()
	n, _ = strconv.Atoi(in.Text())

	root = create(0)

	for i := 0; i < n; i++ {
		in.Scan()
		c := in.Text()

		in.Scan()
		x, _ := strconv.Atoi(in.Text())

		if c == "+" {
			add(x, maxbit, root)
		} else if c == "-" {
			del(x, maxbit, root)
		} else {
			res := query(x, maxbit, root, 0)
			out.WriteString(strconv.Itoa(res))
			out.WriteByte('\n')
		}
	}
}

package main

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

var _x = uint(1)

func fastRand() uint {
	_x ^= _x << 13
	_x ^= _x >> 17
	_x ^= _x << 5
	return _x
}

type node struct {
	lr    [2]*node
	p     uint
	key   int
	value int
}

func (o *node) rotate(d int) *node {
	x := o.lr[d^1]
	o.lr[d^1] = x.lr[d]
	x.lr[d] = o
	return x
}

type treap struct {
	root       *node
	comparator func(a, b int) int
}

func newTreap() *treap {
	return &treap{comparator: func(a, b int) int {
		if a < b {
			return 0
		}
		if a > b {
			return 1
		}
		return -1
	}}
}

func (t *treap) _put(o *node, key int) *node {
	if o == nil {
		return &node{p: fastRand(), key: key, value: 1}
	}
	if cmp := t.comparator(key, o.key); cmp >= 0 {
		o.lr[cmp] = t._put(o.lr[cmp], key)
		if o.lr[cmp].p > o.p {
			o = o.rotate(cmp ^ 1)
		}
	} else {
		o.value++
	}
	return o
}

func (t *treap) put(key int) { t.root = t._put(t.root, key) }

func (t *treap) _delete(o *node, key int) *node {
	if o == nil {
		return nil
	}
	if cmp := t.comparator(key, o.key); cmp >= 0 {
		o.lr[cmp] = t._delete(o.lr[cmp], key)
	} else {
		if o.value > 1 {
			o.value--
		} else {
			if o.lr[1] == nil {
				return o.lr[0]
			}
			if o.lr[0] == nil {
				return o.lr[1]
			}
			cmp2 := 0
			if o.lr[0].p > o.lr[1].p {
				cmp2 = 1
			}
			o = o.rotate(cmp2)
			o.lr[cmp2] = t._delete(o.lr[cmp2], key)
		}
	}
	return o
}

func (t *treap) delete(key int) { t.root = t._delete(t.root, key) }

func (t *treap) max() int {
	var max *node
	for o := t.root; o != nil; o = o.lr[1] {
		max = o
	}
	return max.key
}

func (t *treap) ceiling(key int) int {
	var ceiling *node
	for o := t.root; o != nil; {
		switch cmp := t.comparator(key, o.key); {
		case cmp == 0:
			ceiling = o
			o = o.lr[0]
		case cmp > 0:
			o = o.lr[1]
		default:
			return o.key
		}
	}
	return ceiling.key
}

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

	t := newTreap()
	t.put(0)
	var q, x int
	var op string
	for Fscan(in, &q); q > 0; q-- {
		switch Fscan(in, &op, &x); op[0] {
		case '+':
			t.put(x)
		case '-':
			t.delete(x)
		default:
			mx := t.max()
			mix := 0
			for i := 29; i >= 0; i-- {
				ui := uint(i)
				s1 := 1 << ui &^ x
				if s1 == 0 {
					continue
				}
				mix |= s1
				if mix > mx {
					mix ^= 1 << ui
				} else if up := t.ceiling(mix); up>>ui != mix>>ui {
					mix ^= 1 << ui
				}
			}
			Fprintln(out, mix^x)
		}
	}
}

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

package main

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

var _x = uint(1)

func fastRand() uint {
	_x ^= _x << 13
	_x ^= _x >> 17
	_x ^= _x << 5
	return _x
}

type node struct {
	lr       [2]*node
	priority uint
	key      int
	value    int
}

func (o *node) rotate(d int) *node {
	x := o.lr[d^1]
	o.lr[d^1] = x.lr[d]
	x.lr[d] = o
	return x
}

type treap struct {
	root       *node
	comparator func(a, b int) int
}

func newTreap() *treap {
	return &treap{comparator: func(a, b int) int {
		if a < b {
			return 0
		}
		if a > b {
			return 1
		}
		return -1
	}}
}

func (t *treap) _put(o *node, key int) *node {
	if o == nil {
		return &node{priority: fastRand(), key: key, value: 1}
	}
	if cmp := t.comparator(key, o.key); cmp >= 0 {
		o.lr[cmp] = t._put(o.lr[cmp], key)
		if o.lr[cmp].priority > o.priority {
			o = o.rotate(cmp ^ 1)
		}
	} else {
		o.value++
	}
	return o
}

func (t *treap) put(key int) { t.root = t._put(t.root, key) }

func (t *treap) _delete(o *node, key int) *node {
	if o == nil {
		return nil
	}
	if cmp := t.comparator(key, o.key); cmp >= 0 {
		o.lr[cmp] = t._delete(o.lr[cmp], key)
	} else {
		if o.value > 1 {
			o.value--
		} else {
			if o.lr[1] == nil {
				return o.lr[0]
			}
			if o.lr[0] == nil {
				return o.lr[1]
			}
			cmp2 := 0
			if o.lr[0].priority > o.lr[1].priority {
				cmp2 = 1
			}
			o = o.rotate(cmp2)
			o.lr[cmp2] = t._delete(o.lr[cmp2], key)
		}
	}
	return o
}

func (t *treap) delete(key int) { t.root = t._delete(t.root, key) }

func (t *treap) max() int {
	var max *node
	for o := t.root; o != nil; o = o.lr[1] {
		max = o
	}
	return max.key
}

func (t *treap) floor(key int) int {
	var floor *node
	for o := t.root; o != nil; {
		switch cmp := t.comparator(key, o.key); {
		case cmp == 0:
			o = o.lr[0]
		case cmp > 0:
			floor = o
			o = o.lr[1]
		default:
			return o.key
		}
	}
	if floor == nil {
		return 0
	}
	return floor.key
}

func (t *treap) ceiling(key int) (ceiling *node) {
	for o := t.root; o != nil; {
		switch cmp := t.comparator(key, o.key); {
		case cmp == 0:
			ceiling = o
			o = o.lr[0]
		case cmp > 0:
			o = o.lr[1]
		default:
			return o
		}
	}
	return
}

//

func (o *node) String() string {
	var s string
	if o.value == 1 {
		s = Sprintf("%v", o.key)
	} else {
		s = Sprintf("%v(%v)", o.key, o.value)
	}
	return s
}

func (o *node) draw(prefix string, isTail bool, str *string) {
	if o.lr[1] != nil {
		newPrefix := prefix
		if isTail {
			newPrefix += "│   "
		} else {
			newPrefix += "    "
		}
		o.lr[1].draw(newPrefix, false, str)
	}
	*str += prefix
	if isTail {
		*str += "└── "
	} else {
		*str += "┌── "
	}
	*str += o.String() + "\n"
	if o.lr[0] != nil {
		newPrefix := prefix
		if isTail {
			newPrefix += "    "
		} else {
			newPrefix += "│   "
		}
		o.lr[0].draw(newPrefix, true, str)
	}
}

func (t *treap) String() string {
	if t.root == nil {
		return "BST (empty)\n"
	}
	str := "BST\n"
	t.root.draw("", true, &str)
	return str
}

// github.com/EndlessCheng/codeforces-go
func Sol706D(reader io.Reader, writer io.Writer) {
	bitLength := func(n int) int {
		c := 1
		if n>>16 > 0 {
			c += 16
			n >>= 16
		}
		if n>>8 > 0 {
			c += 8
			n >>= 8
		}
		if n>>4 > 0 {
			c += 4
			n >>= 4
		}
		if n>>2 > 0 {
			c += 2
			n >>= 2
		}
		if n-1 > 0 {
			c++
		}
		return c
	}
	min := func(a, b int) int {
		if a <= b {
			return a
		}
		return b
	}
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	t := newTreap()
	t.put(0)

	nextFloor := func(bitPos int, floor, x int) (f int, i int) {
		for i = bitPos; i >= 0; i-- {
			ui := uint(i)
			bitFloor := floor >> ui & 1
			bitX := x >> ui & 1
			if bitFloor != bitX {
				continue
			}
			if bitFloor == 1 {
				// mask floor
				check := floor&^(1<<ui) | (1<<ui - 1)
				newFloor := t.floor(check)
				return newFloor, i
			}
		}
		return -1, -1
	}

	var q, x int
	var op string
	for Fscan(in, &q); q > 0; q-- {
		switch Fscan(in, &op, &x); op[0] {
		case '+':
			t.put(x)
		case '-':
			t.delete(x)
		default:
			floor := t.max()
			if floor == 0 {
				Fprintln(out, x)
				continue
			}
			ans := floor ^ x
			fl := bitLength(floor)
			xl := bitLength(x)
			minL := min(fl, xl)
			for i := minL - 1; i >= 0; {
				floor, i = nextFloor(i, floor, x)
				if floor == -1 {
					break
				}
				if newXor := floor ^ x; newXor > ans {
					ans = newXor
				}
			}
			Fprintln(out, ans)
		}
	}
}

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