The only difference between the easy and hard versions is that the given string $$$s$$$ in the easy version is initially a palindrome, this condition is not always true for the hard version.A palindrome is a string that reads the same left to right and right to left. For example, "101101" is a palindrome, while "0101" is not.Alice and Bob are playing a game on a string $$$s$$$ (which is initially a palindrome in this version) of length $$$n$$$ consisting of the characters '0' and '1'. Both players take alternate turns with Alice going first.In each turn, the player can perform one of the following operations:   Choose any $$$i$$$ ($$$1 \le i \le n$$$), where $$$s[i] =$$$ '0' and change $$$s[i]$$$ to '1'. Pay 1 dollar.  Reverse the whole string, pay 0 dollars. This operation is only allowed if the string is currently not a palindrome, and the last operation was not reverse. That is, if Alice reverses the string, then Bob can't reverse in the next move, and vice versa. Reversing a string means reordering its letters from the last to the first. For example, "01001" becomes "10010" after reversing.The game ends when every character of string becomes '1'. The player who spends minimum dollars till this point wins the game and it is a draw if both spend equal dollars. If both players play optimally, output whether Alice wins, Bob wins, or if it is a draw.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$). Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^3$$$). The second line of each test case contains the string $$$s$$$ of length $$$n$$$, consisting of the characters '0' and '1'. It is guaranteed that the string $$$s$$$ is a palindrome and contains at least one '0'.  Note that there is no limit on the sum of $$$n$$$ over test cases.

For each test case print a single word in a new line:    "ALICE", if Alice will win the game,  "BOB", if Bob will win the game,  "DRAW", if the game ends in a draw.

NoteIn the first test case of the example,   in the $$$1$$$-st move Alice has to perform the $$$1$$$-st operation, since the string is currently a palindrome.  in the $$$2$$$-nd move Bob reverses the string.  in the $$$3$$$-rd move Alice again has to perform the $$$1$$$-st operation. All characters of the string are '1', game over.  Alice spends $$$2$$$ dollars while Bob spends $$$0$$$ dollars. Hence, Bob always wins.

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
    var number_test_case int
    fmt.Sscan(scanner.Text(), &number_test_case)
    for i:=0; i < number_test_case; i++ {
        var l int // lenght of the string
        var s string
        scanner.Scan()
        fmt.Sscan(scanner.Text(), &l)
        scanner.Scan()
        s = scanner.Text()
        count0 := 0
        for _,c := range s{
            if c =='0' {
                count0 ++
            }
        }
        if count0 > 1 && count0 % 2 == 1 {
            fmt.Println("ALICE")
        } else {
            fmt.Println("BOB")
        }
    }
}

package main
import("bufio";."fmt";."os";."strings")

// github.com/EndlessCheng/codeforces-go
func main(){
	in:=bufio.NewReader(Stdin)
	T,n,s:=0,0,""
	for Fscan(in,&T);T>0;T--{
		Fscan(in,&n,&s)
		if c:=Count(s,"0");c&1>0&&c>1{Println("ALICE")
		}else{Println("BOB")}
	}
}

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

	solve := func(Case int) {
		var n int
		var s string
		Fscan(in, &n, &s)
		c0 := strings.Count(s, "0")
		if c0&1 == 0 {
			Fprintln(out, "BOB")
		} else {
			if c0 == 1 {
				Fprintln(out, "BOB")
			} else {
				Fprintln(out, "ALICE")
			}
		}
	}

	T := 1
	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) }

package main

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

func run(r io.Reader, w io.Writer) {
	in := bufio.NewReader(r)
	out := bufio.NewWriter(w)
	defer out.Flush()

	var t int
	for Fscan(in, &t); t > 0; t-- {
		var n int
		Fscan(in, &n)
		s := make([]byte, n)
		Fscan(in, &s)

		cnt := bytes.Count(s, []byte{'0'})
		if cnt == 1 || cnt & 0x1 == 0 {
			Fprintln(out, "BOB")
		} else {
			Fprintln(out, "ALICE")
		}
	}
}

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

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

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

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

// link: https://codeforces.com/problemset/problem/1527/B1

package main

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

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

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

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

		var numberOfZero int
		for j := 0; j < n; j++ {
			if p[j] == '0' {
				numberOfZero++
			}
		}

		if numberOfZero%2 == 0 {
			fmt.Fprintln(out, "BOB")
		} else {
			if numberOfZero == 1 {
				fmt.Fprintln(out, "BOB")
			} else {
				fmt.Fprintln(out, "ALICE")
			}
		}
	}
}

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

package main
import (
    "os"
    "fmt"
    "bufio"
)
var rd = bufio.NewReader(os.Stdin)
func main() {
    q := 0
    fmt.Fscan(rd, &q)
    for t := 0; t < q; t++ {
        n := 0
        fmt.Fscan(rd, &n)
        s, cnt := "", 0
        fmt.Fscan(rd, &s)
        for i := range s {
            if s[i] == '0' {
                cnt++
            }
        }
        if cnt == 1 {
            fmt.Println("BOB")
        } else if cnt % 2 == 1 {
            fmt.Println("ALICE")
        } else {
            fmt.Println("BOB")
        }
    }
}

package main

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

func main(){

  in := bufio.NewReader(os.Stdin)

  var t,n int
  fmt.Fscan(in, &t)
  for tt:=0;tt<t;tt++ {
    var s string
    fmt.Fscan(in, &n)
    fmt.Fscan(in, &s)
    z:=0
    for _,ch := range s{
      if ch == '0' {
        z++
      }
    }

    if n%2==0{
      fmt.Println("BOB")
    }else if s[n/2]=='1' || z==1{
      fmt.Println("BOB")
    }else{
      fmt.Println("ALICE")
    }
  }
}

package main
import("bufio";."fmt";."os";."strings")

func main() {
	in := bufio.NewReader(Stdin)
	var T, n int
	var s string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &s)
		c := Count(s, "0")
		if c&1 > 0 && c > 1 {
			Println("ALICE")
		} else {
			Println("BOB")
		}
	}
}

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

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

func MIN(x, y int64) int64 {
	if x > y {
		return y
	} else {
		return x
	}
}

func MKO(x, y int64) int64 {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

func MAX(x, y int64) int64 {
	if x > y {
		return x
	} else {
		return y
	}
}

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func GCD(x, y int64) int64 {
	var l, q int64
	l = MAX(x, y)
	q = MIN(x, y)
	var m int64
	for l*q > 0 {
		a := l % q
		b := l - a
		cc := int64(b / q)
		remain := l - cc*q
		l = MAX(remain, q)
		q = MIN(remain, q)
		if l*q == 0 {
			m = l + q
			break
		}

	}
	return m

}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}

}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)

	for o := 0; o < t; o++ {
		var n int
		fmt.Fscan(input, &n)
		STRING := make([]byte, 0)
		fmt.Fscan(input, &STRING)
		ZeroISThere := 0
		for i := 0; i < n; i++ {
			if string(STRING[i]) == "0" {
				ZeroISThere++
			}
		}

		if n%2 == 0 {
			if ZeroISThere == 0 {
				fmt.Println("DRAW")
			} else {
				fmt.Println("BOB")
			}
		} else {
			q := int((n - 1) / 2)
			if string(STRING[q]) == "1" {
				if ZeroISThere == 0 {
					fmt.Println("DRAW")
				} else {
					fmt.Println("BOB")
				}
			} else {
				if ZeroISThere == 1 {
					fmt.Println("BOB")
				} else {
					fmt.Println("ALICE")

				}
			}

		}
	}

}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
    var number_test_case int
    fmt.Sscan(scanner.Text(), &number_test_case)
    for i:=0; i < number_test_case; i++ {
        var l int // lenght of the string
        var s string
        scanner.Scan()
        fmt.Sscan(scanner.Text(), &l)
        scanner.Scan()
        s = scanner.Text()
        count0 := 0
        for _,c := range s{
            if c =='0' {
                count0 ++
            }
        }
        if count0 == 0 {
            fmt.Println("DRAW")
        } else {
            fmt.Println("BOB")
        }
    }
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
    var number_test_case int
    fmt.Sscan(scanner.Text(), &number_test_case)
    for i:=0; i < number_test_case; i++ {
        var l int // lenght of the string
        var s string
        scanner.Scan()
        fmt.Sscan(scanner.Text(), &l)
        scanner.Scan()
        s = scanner.Text()
        count0 := 0
        for _,c := range s{
            if c =='0' {
                count0 ++
            }
        }
        if count0 % 4 == 0 {
            fmt.Println("ALICE")
        } else {
            fmt.Println("BOB")
        }
    }
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
    var number_test_case int
    fmt.Sscan(scanner.Text(), &number_test_case)
    for i:=0; i < number_test_case; i++ {
        var l int // lenght of the string
        var s string
        scanner.Scan()
        fmt.Sscan(scanner.Text(), &l)
        scanner.Scan()
        s = scanner.Text()
        count0 := 0
        for _,c := range s{
            if c =='0' {
                count0 ++
            }
        }
            fmt.Println("BOB")
    }
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
    var number_test_case int
    fmt.Sscan(scanner.Text(), &number_test_case)
    for i:=0; i < number_test_case; i++ {
        var l int // lenght of the string
        var s string
        scanner.Scan()
        fmt.Sscan(scanner.Text(), &l)
        scanner.Scan()
        s = scanner.Text()
        count0 := 0
        for _,c := range s{
            if c =='0' {
                count0 ++
            }
        }
        if count0 % 4 == 0 {
            fmt.Println("DRAW")
        } else {
            fmt.Println("BOB")
        }
    }
}

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

	solve := func(Case int) {
		var n int
		var s string
		Fscan(in, &n, &s)
		c0 := strings.Count(s, "0")
		if c0&1 == 0{
			if c0/2&1> 0 {
				Fprintln(out, "BOB")
			} else {
				Fprintln(out, "DRAW")
			}
			return
		}
		if c0/2&1 == 0 {
			Fprintln(out, "BOB")
		} else {
			Fprintln(out, "ALICE")
		}
	}

	T := 1
	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) }

package main

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

func run(r io.Reader, w io.Writer) {
	in := bufio.NewReader(r)
	out := bufio.NewWriter(w)
	defer out.Flush()

	var t int
	for Fscan(in, &t); t > 0; t-- {
		var n int
		Fscan(in, &n)
		s := make([]byte, n)
		Fscan(in, &s)

		cnt := bytes.Count(s, []byte{'0'})
		if (n == 1 && s[0] == '0') || cnt & 0x1 == 0 {
			Fprintln(out, "BOB")
		} else {
			Fprintln(out, "ALICE")
		}
	}
}

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

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

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

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

// link: https://codeforces.com/problemset/problem/1527/B1

package main

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

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

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

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

		var numberOfZero int
		for j := 0; j < n; j++ {
			if p[j] == '0' {
				numberOfZero++
			}
		}

		if numberOfZero%4 == 0 {
			fmt.Fprintln(out, "DRAW")
		} else {
			fmt.Fprintln(out, "BOB")
		}
	}
}

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

package main
import (
    "os"
    "fmt"
    "bufio"
)
var rd = bufio.NewReader(os.Stdin)
func main() {
    q := 0
    fmt.Fscan(rd, &q)
    for t := 0; t < q; t++ {
        n := 0
        fmt.Fscan(rd, &n)
        s, cnt := "", 0
        fmt.Fscan(rd, &s)
        for i := range s {
            if s[i] == 1 {
                cnt++
            }
        }
        if cnt == 1 {
            fmt.Println("BOB")
        } else if cnt % 2 == 1 {
            fmt.Println("ALICE")
        } else {
            fmt.Println("BOB")
        }
    }
}


Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with $$$n$$$ elements. The $$$i$$$-th element is $$$a_i$$$ ($$$i$$$ = $$$1, 2, \ldots, n$$$). He gradually takes the first two leftmost elements from the deque (let's call them $$$A$$$ and $$$B$$$, respectively), and then does the following: if $$$A > B$$$, he writes $$$A$$$ to the beginning and writes $$$B$$$ to the end of the deque, otherwise, he writes to the beginning $$$B$$$, and $$$A$$$ writes to the end of the deque. We call this sequence of actions an operation.For example, if deque was $$$[2, 3, 4, 5, 1]$$$, on the operation he will write $$$B=3$$$ to the beginning and $$$A=2$$$ to the end, so he will get $$$[3, 4, 5, 1, 2]$$$.The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him $$$q$$$ queries. Each query consists of the singular number $$$m_j$$$ $$$(j = 1, 2, \ldots, q)$$$. It is required for each query to answer which two elements he will pull out on the $$$m_j$$$-th operation.Note that the queries are independent and for each query the numbers $$$A$$$ and $$$B$$$ should be printed in the order in which they will be pulled out of the deque.Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides.

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq q \leq 3 \cdot 10^5$$$) — the number of elements in the deque and the number of queries. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$, where $$$a_i$$$ $$$(0 \leq a_i \leq 10^9)$$$ — the deque element in $$$i$$$-th position. The next $$$q$$$ lines contain one number each, meaning $$$m_j$$$ ($$$1 \leq m_j \leq 10^{18}$$$).

For each teacher's query, output two numbers $$$A$$$ and $$$B$$$ — the numbers that Valeriy pulls out of the deque for the $$$m_j$$$-th operation.

package main

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

type Response struct {
	First  int64
	Second int64
}

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

func maxArray(n int64, a []int64) []int64 {
	maxArray := make([]int64, n)
	maxArray[0] = a[0]
	for i := int64(1); i < n; i++ {
		maxArray[i] = max(a[i], maxArray[i-1])
	}
	return maxArray
}

func maxElemIndex(n int64, a []int64) int64 {
	maxElemIndex := int64(0)
	maxElem := a[0]
	for i := int64(1); i < n; i++ {
		if a[i] > maxElem {
			maxElemIndex = i
			maxElem = a[i]
		}
	}
	return maxElemIndex
}

func mutateDeque(n int64, a []int64, moves int64) []int64 {
	b := make([]int64, n)
	copy(b, a)
	for i := int64(0); i < moves; i++ {
		if b[0] > b[1] {
			loser := b[1]
			b = append(b[:1], b[2:]...)
			b = append(b, loser)
		} else {
			loser := b[0]
			b = b[1:]
			b = append(b, loser)
		}
	}
	return b
}

func getResponse(n int64, a []int64, query int64, maxArray []int64, maxElemIndex int64, deque []int64) Response {
	if query < maxElemIndex+1 {
		return Response{First: maxArray[query-1], Second: a[query]}
	}
	secondIndex := (query-(maxElemIndex+1))%(n-1) + 1
	return Response{First: maxArray[maxElemIndex], Second: deque[secondIndex]}
}

func Solve(n int64, q int64, a []int64, m []int64) []Response {
	maxArray := maxArray(n, a)
	maxElemIndex := maxElemIndex(n, a)
	d := mutateDeque(n, a, maxElemIndex)
	responses := make([]Response, q)
	for i := int64(0); i < q; i++ {
		responses[i] = getResponse(n, a, m[i], maxArray, maxElemIndex, d)
	}
	return responses
}

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

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

	for _, response := range Solve(n, q, a, m) {
		fmt.Fprintf(writer, "%d %d\n", response.First, response.Second)
	}
}


You are given array $$$a_1, a_2, \dots, a_n$$$. Find the subsegment $$$a_l, a_{l+1}, \dots, a_r$$$ ($$$1 \le l \le r \le n$$$) with maximum arithmetic mean $$$\frac{1}{r - l + 1}\sum\limits_{i=l}^{r}{a_i}$$$ (in floating-point numbers, i.e. without any rounding).If there are many such subsegments find the longest one.

The first line contains single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the array $$$a$$$.

Print the single integer — the length of the longest subsegment with maximum possible arithmetic mean.

NoteThe subsegment $$$[3, 4]$$$ is the longest among all subsegments with maximum arithmetic mean.

package main

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

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

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

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

	var n, max, tmp, counter, maxCounter int
	max = -1
	scanf("%d\n", &n)
	// input := make([]int, n)
	for i := 0; i < n; i++ {
		scanf("%d ", &tmp)
		if tmp > max {
			max = tmp
			counter = 1
			maxCounter = 1
		} else if tmp == max {
			counter++
		} else if tmp != max {
			counter = 0
		}
		if counter > maxCounter {
			maxCounter = counter
		}
	}
	// count maximum length of sequence where each elem equal to max
	printf("%+v\n", maxCounter)
}

package main

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

func main() {
	  in := bufio.NewReader(os.Stdin)
    best, ile := -1,-1;
		running := 0;
		var n int;
    fmt.Fscanf(in, "%d\n", &n);
		for ;n != 0; n-- {
			var x int;
			fmt.Fscanf(in, "%d", &x);
			if x < best {
				running = 0;
			} else if x == best {
					running++;
					if running > ile {
						ile = running;
					}
			}
			if x > best {
				best = x;
				ile = 1;
				running = 1;
			}
		}
		fmt.Println(ile);
}

package main

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

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

	var maxx int = -1
	var n ,ans ,v ,cnt int
	fmt.Fscanf(buf, "%d\n", &n)
	ans=1
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &v)
		if v > maxx{
			maxx=v
			cnt=1
			ans=1
			continue
		}
		if v == maxx{
			cnt++
			if cnt > ans{
				ans = cnt
			}
		}else{
			cnt=0
		}
	}
	fmt.Printf("%d\n",ans)

}

/**
8 5
8 6 2 1 4 5 7 5
6 3 3 2 6 2 3 2
2 8
1 4
4 7
3 4
6 10
 */

package main

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

func main()  {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n,x int
	fmt.Fscan(in,&n)
	a := make([]int,0)
	mn := 0
	for i := 0 ; i <n ;i++{
		fmt.Fscan(in,&x)
		a = append(a,x)
		mn = max(mn,x)
	}
	len := 0
	ans := 0
	for i := 0 ;i < n;i++{
		if(a[i] == mn){
			len ++
		}else{
				ans = max(len,ans)
				len = 0
		}
	}
	ans = max(len,ans)
	fmt.Fprint(out,ans)

}

func max(i int, i2 int) int {
	if(i < i2){
		return i2
	}else{
		return i
	}
}
func min(i int , i2 int)  int {
	if(i < i2){
		return i
	}else{
		return i2
	}

}

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 max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func main() {
	defer writer.Flush()
	var n, mx, t1 int
	mx = -1
	last := -2
	ans := 0
	cur := 0

	arr := []int{}

	scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		scanf("%d", &t1)
		arr = append(arr, t1)
		if t1 > mx {
			mx = t1
		}
	}

	for i := 0; i < n; i++ {
		t1 = arr[i]
		if t1 == mx && last == mx {
			cur++
			ans = max(ans, cur)
		} else if t1 == mx {
			cur = 1
			ans = max(ans, cur)
		} else {
			cur = 0
		}
		last = t1
	}
	printf("%d", ans)
}

package main

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

func main() {
	sc := NewScanner()
	sc.NextInt()
	A := sc.NextIntArray()

	A = append(A, -1)
	K := maxA(A...)

	l := -1
	mx := 0
	for i := 0; i < len(A); i++ {
		if K == A[i] {
			if l == -1 {
				l = i
			}
		} else {
			if l >= 0 {
				mx = max(mx, i-l)
				l = -1
			}
		}
	}
	fmt.Println(mx)

}

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

func maxA(arg ...int) int {
	var s = arg[0]
	for _, v := range arg {
		s = max(s, v)
	}
	return s
}

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

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

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

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

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

	return mp
}

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

package main

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

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

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

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

	var n, max, tmp, counter, maxCounter int
	max = -1
	scanf("%d\n", &n)
	// input := make([]int, n)
	for i := 0; i < n; i++ {
		scanf("%d ", &tmp)
		if tmp > max {
			max = tmp
			counter = 1
		} else if tmp == max {
			counter++
		} else if tmp != max {
			counter = 0
		}
		if counter > maxCounter {
			maxCounter = counter
		}
	}
	// count maximum length of sequence where each elem equal to max
	printf("%+v\n", maxCounter)
}

package main

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

func main() {
	  in := bufio.NewReader(os.Stdin)
    best, ile := -1,-1;
		running := 0;
		var n int;
    fmt.Fscanf(in, "%d", &n);
		for ;n != 0; n-- {
			var x int;
			fmt.Fscanf(in, "%d", &x);
			if x < best {
				running = 0;
			} else if x == best {
					running++;
					if running > ile {
						ile = running;
					}
			}
			if x > best {
				best = x;
				ile = 1;
				running = 1;
			}
		}
		fmt.Println(ile);
}

package main

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

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

	var maxx int = -1
	var n ,ans ,v ,cnt int
	fmt.Fscanf(buf, "%d\n", &n)
	ans=1
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &v)
		if v > maxx{
			maxx=v
			cnt=1
			continue
		}
		if v == maxx{
			cnt++
			if cnt > ans{
				ans = cnt
			}
		}else{
			cnt=0
		}
	}
	fmt.Printf("%d\n",ans)

}

/**
8 5
8 6 2 1 4 5 7 5
6 3 3 2 6 2 3 2
2 8
1 4
4 7
3 4
6 10
 */

package main

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

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

	var maxx int = -1
	var n ,ans ,v ,cnt int
	fmt.Fscanf(buf, "%d\n", &n)
	cnt=1
	for i := 0; i < n; i++ {

		fmt.Fscanf(buf, "%d", &v)
		if v > maxx{
			maxx=v
			cnt=1
			continue
		}
		if v == maxx{
			cnt++
			if cnt > ans{
				ans = cnt
			}
		}else{
			cnt=0
		}
	}
	fmt.Printf("%d\n",ans)

}

/**
8 5
8 6 2 1 4 5 7 5
6 3 3 2 6 2 3 2
2 8
1 4
4 7
3 4
6 10
 */

package main

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

func main()  {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n,x int
	fmt.Fscan(in,&n)
	a := make([]int,0)
	mn := 0
	for i := 0 ; i <n ;i++{
		fmt.Fscan(in,&x)
		a = append(a,x)
		mn = max(mn,x)
	}
	len := 0
	ans := 0
	for i := 0 ;i < n;i++{
		if(a[i] == mn){
			len ++
		}else{
				ans = max(len,ans)
				len = 0
		}
	}
	fmt.Fprint(out,ans)

}

func max(i int, i2 int) int {
	if(i < i2){
		return i2
	}else{
		return i
	}
}
func min(i int , i2 int)  int {
	if(i < i2){
		return i
	}else{
		return i2
	}

}


Tokitsukaze has a sequence $$$a$$$ of length $$$n$$$. For each operation, she selects two numbers $$$a_i$$$ and $$$a_j$$$ ($$$i \ne j$$$; $$$1 \leq i,j \leq n$$$).   If $$$a_i = a_j$$$, change one of them to $$$0$$$.  Otherwise change both of them to $$$\min(a_i, a_j)$$$. Tokitsukaze wants to know the minimum number of operations to change all numbers in the sequence to $$$0$$$. It can be proved that the answer always exists.

The first line contains a single positive integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the length of the sequence $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq 100$$$) — the sequence $$$a$$$.

For each test case, print a single integer — the minimum number of operations to change all numbers in the sequence to $$$0$$$.

NoteIn the first test case, one of the possible ways to change all numbers in the sequence to $$$0$$$:In the $$$1$$$-st operation, $$$a_1 < a_2$$$, after the operation, $$$a_2 = a_1 = 1$$$. Now the sequence $$$a$$$ is $$$[1,1,3]$$$.In the $$$2$$$-nd operation, $$$a_1 = a_2 = 1$$$, after the operation, $$$a_1 = 0$$$. Now the sequence $$$a$$$ is $$$[0,1,3]$$$.In the $$$3$$$-rd operation, $$$a_1 < a_2$$$, after the operation, $$$a_2 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,3]$$$.In the $$$4$$$-th operation, $$$a_2 < a_3$$$, after the operation, $$$a_3 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,0]$$$.So the minimum number of operations is $$$4$$$.

package main

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

var (
	_r = bufio.NewReader(os.Stdin)
	_w = bufio.NewWriter(os.Stdout)
)

type Tint interface {
	int | int32 | int64
}

func min[T Tint](x, y T) T {
	if x <= y {
		return x
	}
	return y
}
func max[T Tint](x, y T) T {
	if x >= y {
		return x
	}
	return y
}
func main() {
	defer _w.Flush()
	var _t int
	// n = 1
	fmt.Fscan(_r, &_t)
	for ; _t > 0; _t-- {
		var n int
		fmt.Fscan(_r, &n)
		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			var x int
			fmt.Fscan(_r, &x)
			mp[x]++
		}
		ans := 0
		f := false
		for k, v := range mp {
			if k == 0 {
				f = true
				continue
			}
			if v > 1 {
				f = true
			}
			ans += v
		}
		if f == false {
			ans++
		}
		fmt.Fprintln(_w, ans)
	}
}

package main

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

func solve(in io.Reader, out io.Writer) {
	var n int
	Fscan(in, &n)

	mp := make(map[int]bool)

	var a int
	zeroCnt := 0
	dup := false
	for i := 0; i < n; i++ {
		Fscan(in, &a)

		if a == 0 {
			zeroCnt++
		}

		_, ok := mp[a]
		if ok {
			dup = true
		} else {
			mp[a] = true
		}
	}

	var ans int
	if zeroCnt != 0 {
		ans = n - zeroCnt
	} else if dup {
		ans = n
	} else {
		ans = n + 1
	}

	Fprintln(out, ans)

}

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

package main

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

const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()
	neg := false

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	if neg {
		return -x
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

func main() {
	myio := NewIO(os.Stdin, os.Stdout)
	defer myio.Flush()

	t := myio.ReadInt()
	sol := func() {
		n := myio.ReadInt()
		same := false
		c := make([]int, 110)
		for i := 0; i < n; i++ {
			v := myio.ReadInt()
			c[v]++
			if c[v] > 1 {
				same = true
			}
		}
		if c[0] == 0 {
			if same {
				myio.Println(n)
			} else {
				myio.Println(n + 1)
			}
		} else {
			myio.Println(n - c[0])
		}
	}
	for ; t > 0; t-- {
		sol()
	}
}

package main

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

const MAXN = 2e5

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var ntests int
	fmt.Fscan(in, &ntests)
	for iiter := 0; iiter < ntests; iiter++ {
		var n int
		fmt.Fscan(in, &n)
		m := make(map[int]bool)
		same := false
		zero := false
		res := n
		for i := 0; i < n; i++ {
			var el int
			fmt.Fscan(in, &el)
			if el == 0 {
				zero = true
				res--
			}
			if _, ok := m[el]; ok {
				same = true
			} else {
				m[el] = true
			}
		}
		if zero {
			fmt.Fprintln(out, res)
		} else {
			if same {
				fmt.Fprintln(out, res)
			} else {
				fmt.Fprintln(out, res+1)
			}
		}
	}
}

package main

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

var bufferedStdin = bufio.NewReader(os.Stdin)
var bufferedStdout = bufio.NewWriter(os.Stdout)

func solve() {
	var n int
	_, _ = fmt.Fscan(bufferedStdin, &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Fscan(bufferedStdin, &a[i])
	}
	sort.Ints(a)
	var cnt = 0
	for i := 0; i < n; i++ {
		if a[i] != 0 {
			cnt++
		}
	}
	if a[0] == 0 {
		_, _ = fmt.Fprintf(bufferedStdout, "%d\n", cnt)
		return
	}
	for i := 1; i < n; i++ {
		if a[i] == a[i-1] {
			_, _ = fmt.Fprintf(bufferedStdout, "%d\n", n)
			return
		}
	}
	_, _ = fmt.Fprintf(bufferedStdout, "%d\n", n+1)
}

func main() {
	var t int
	_, _ = fmt.Fscan(bufferedStdin, &t)
	for i := 0; i < t; i++ {
		solve()
	}
	_ = bufferedStdout.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N := gi(); A := gis(N)
		sb := make([]bool,101)
		cnt := 0; cntzero := 0
		for _,a := range A {
			if a == 0 { cntzero++ }
			if !sb[a] { cnt++; sb[a] = true }
		}
		ans := 0
		if cntzero > 0 {
			ans = N - cntzero // Can make at most one zero in each operation
		} else if cnt < N {
			ans = N // already have a matching pair, so can use that to make first zero, and then one zero per op afterwards
		} else {
			ans = N+1 // need to use min to create matching pair, then same as above
		}
		fmt.Fprintln(wrtr,ans)
	}
}

package main

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

type CF1678A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1678A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1678A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1678A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1678A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1678A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1678A
Date: 6/06/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1678/A
Problem: CF1678A
**/
func (in *CF1678A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		mapa := map[int]int{}

		repeat := false
		for i := 0; i < n; i++ {
			num := in.NextInt()
			value, _ := mapa[num]
			mapa[num] = value + 1
			if value+1 >= 2 {
				repeat = true
			}
		}

		zero, exist := mapa[0]
		ans := 0
		if exist {
			ans = n - zero
		} else if repeat {
			ans = n
		} else {
			ans = n + 1
		}
		fmt.Println(ans)
	}
}

func NewCF1678A(r *bufio.Reader) *CF1678A {
	return &CF1678A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1678A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

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

func slove() {
	defer w.Flush()
	var n int
	fmt.Fscan(r, &n)
	nums := make([]int, n)
	cnt := map[int]int{}
	ans := 0
	is_zero := false
	for i:=0;i<n;i++ {
		fmt.Fscan(r, &nums[i])
		if nums[i] == 0 {
			is_zero = true
			continue
		}
		cnt[nums[i]]++
		if cnt[nums[i]] == 2 {
			cnt[nums[i]]--
			ans++
		}
	}
	if ans > 0 || is_zero {
		ans += len(cnt)
	} else {
		ans = 1 + len(cnt)
	}
	fmt.Fprintln(w, ans)
}

func main() {
	var n int
	fmt.Fscan(r, &n)
	for i:=1;i<=n;i++ {
		slove()
	}
}

package main

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

type Int int64

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

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n, x int
		Fscan(in, &n)
		ma := make(map[int]int)
		mx := 0
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			ma[x]++
			if ma[x] > mx {
				mx = ma[x]
			}
		}

		ans := n - ma[0]

		if ma[0] == 0 {
			if mx == 1 {
				ans += 1
			}
		}
		Fprintln(out, ans)

	}

}

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

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

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

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

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

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

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

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

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

func doNothing(m Int) Int {
	return m
}

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

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

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

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

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

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

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

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

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

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

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

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

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

package main

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

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)

		m := make([]int, 101)
		var z int
		var hasDuplicate bool
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)

			if v == 0 {
				z++
			} else {
				if m[v] > 0 {
					hasDuplicate = true
				}
				m[v]++
			}
		}

		if z > 0 {
			fmt.Fprintln(out, n-z)
		} else if hasDuplicate {
			fmt.Fprintln(out, n)
		} else {
			fmt.Fprintln(out, n+1)
		}
	}
}

package main

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

func main() {
	io := NewIO(os.Stdin, os.Stdout)
	for t := io.ReadInt(); t > 0; t-- {
		n := io.ReadInt()
		a := io.ReadIntSlice(n)
		sort.Ints(a)
		var od bool
		var zeros int
		for i := range a {
			if a[i] == 0 {
				zeros++
			}
			if i != 0 && a[i] == a[i-1] {
				od = true
			}
		}

		if zeros != 0 {
			fmt.Println(len(a) - zeros)
		} else {
			if od {
				fmt.Println(len(a))
			} else {
				fmt.Println(len(a) + 1)
			}
		}
	}
}

package main

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

var (
	_r = bufio.NewReader(os.Stdin)
	_w = bufio.NewWriter(os.Stdout)
)

type Tint interface {
	int | int32 | int64
}

func min[T Tint](x, y T) T {
	if x <= y {
		return x
	}
	return y
}
func max[T Tint](x, y T) T {
	if x >= y {
		return x
	}
	return y
}
func main() {
	var _t int
	// n = 1
	fmt.Fscan(_r, &_t)
	for ; _t > 0; _t-- {
		var n int
		fmt.Fscan(_r, &n)
		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			var x int
			fmt.Fscan(_r, &x)
			mp[x]++
		}
		ans := 0
		f := false
		for k, v := range mp {
			if k == 0 {
				f = true
				continue
			}
			if v > 1 {
				f = true
			}
			ans += v
		}
		if f == false {
			ans++
		}
		fmt.Fprintln(_w, ans)
	}
}

package main

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

func solve(in io.Reader, out io.Writer) {
	var n int
	Fscan(in, &n)

	mp := make(map[int]bool)

	var a int
	zeroCnt := 0
	dup := false
	for i := 0; i < n; i++ {
		Fscan(in, &a)

		if a == 0 {
			zeroCnt++
		}

		_, ok := mp[a]
		if ok {
			dup = true
		} else {
			mp[a] = true
		}
	}

	var ans int
	if zeroCnt != 0 {
		ans = n - zeroCnt
	} else if dup {
		ans = n + 1
	} else {
		ans = n + 2
	}

	Fprintln(out, ans)

}

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

package main

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

const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()
	neg := false

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	if neg {
		return -x
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

func main() {
	myio := NewIO(os.Stdin, os.Stdout)
	defer myio.Flush()

	t := myio.ReadInt()
	sol := func() {
		n := myio.ReadInt()
		same := false
		c := make([]int, 110)
		for i := 0; i < n; i++ {
			v := myio.ReadInt()
			c[v]++
			if c[v] > 1 {
				same = true
			}
		}
		if c[0] == 0 {
			if same {
				fmt.Println(n)
			} else {
				fmt.Println(n + 1)
			}
		} else {
			myio.Println(n - c[0])
		}
	}
	for ; t > 0; t-- {
		sol()
	}
}


You are given $$$n$$$ elements numbered from $$$1$$$ to $$$n$$$, the element $$$i$$$ has value $$$a_i$$$ and color $$$c_i$$$, initially, $$$c_i = 0$$$ for all $$$i$$$.The following operation can be applied:  Select three elements $$$i$$$, $$$j$$$ and $$$k$$$ ($$$1 \leq i < j < k \leq n$$$), such that $$$c_i$$$, $$$c_j$$$ and $$$c_k$$$ are all equal to $$$0$$$ and $$$a_i = a_k$$$, then set $$$c_j = 1$$$. Find the maximum value of $$$\sum\limits_{i=1}^n{c_i}$$$ that can be obtained after applying the given operation any number of times.

The first line contains an integer $$$n$$$ ($$$3 \leq n \leq 2 \cdot 10^5$$$) — the number of elements. The second line consists of $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq n$$$), where $$$a_i$$$ is the value of the $$$i$$$-th element.

Print a single integer in a line — the maximum value of $$$\sum\limits_{i=1}^n{c_i}$$$ that can be obtained after applying the given operation any number of times.

NoteIn the first test, it is possible to apply the following operations in order:

package main

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

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

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

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

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
		}
	}

	// printf("ans1 = %d\n", ans)

	// printf("pairs = %v\n", pairs)
	dp := make([]int, n+2)
	match := make([]int, n+1)

	dp[len(pairs)] = 0

	resetBit()
	acc := make([]int, n+1)
	for _, p := range pairs {
		match[p[0]] = p[1]
		acc[p[0]]++
		acc[p[1]]++
		updateBit(p[0], p[1])
	}
	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}

	for i := 1; i <= n; i++ {
		j := match[i]
		if j > 0 {
			cur := 1
			jj := queryBit(j)
			for jj > j {
				j = jj
				cur++
				jj = queryBit(j)
			}
			ans += (acc[j] - acc[i-1]) - (cur + 1)
			i = j
		}
	}

	printf("%d\n", ans)
}

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

	var n int
	Fscan(in, &n)
	a := make([]int, n)
	pr := make([]int, n+1)
	for i := range a {
		Fscan(in, &a[i])
		pr[a[i]] = i
	}
	ans := n
	for i, l := 0, 0; i < n; {
		ans--
		r := l
		for _, v := range a[i : l+1] {
			if pr[v] > r {
				r = pr[v]
			}
		}
		if r == l {
			i = l + 1
			l++
		} else {
			i = l
			l = r
		}
	}
	Fprint(out, ans)
}

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

package main

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

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

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

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

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
		}
	}

	// printf("ans1 = %d\n", ans)

	// printf("pairs = %v\n", pairs)
	dp := make([]int, n+2)
	match := make([]int, n+1)

	dp[len(pairs)] = 0

	acc := make([]int, n+1)
	for _, p := range pairs {
		match[p[0]] = p[1]
		acc[p[0]]++
		acc[p[1]]++
	}

	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}

	resetBit()
	updateBit(n+1, acc[n])
	for i := n; i >= 1; i-- {
		j := match[i]
		if j > 0 {
			// dp[i] = dp[k] + acc[k-1]-acc[i], k <= j+1
			dp[i] = max(dp[i+1], queryBit(j)-acc[i])
			// dp[i] = max(dp[i], dp[j+1]+acc[j-1]-acc[i])
			// printf("dp[%d] = max(%d,%d + %d-%d)\n", i, dp[i], dp[j], acc[j-1], acc[i])
		}
		updateBit(i, dp[i]+acc[i-1])
	}

	// printf("dp = %v\n", dp)

	ans += dp[1]
	printf("%d\n", ans)
}

package main

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

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

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

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

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
		}
	}

	// printf("ans1 = %d\n", ans)

	// printf("pairs = %v\n", pairs)
	dp := make([]int, n+2)
	match := make([]int, n+1)

	dp[len(pairs)] = 0

	acc := make([]int, n+1)
	for _, p := range pairs {
		match[p[0]] = p[1]
		acc[p[0]]++
		acc[p[1]]++
	}

	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}

	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}

	for i := n; i >= 1; i-- {
		j := match[i]
		if j > 0 {
			// dp[i] = dp[k] + acc[k-1]-acc[i], k <= j
			dp[i] = max(dp[i+1], queryBit(j-1)-acc[i])
			dp[i] = max(dp[i], dp[j+1]+acc[j-1]-acc[i])
			// printf("dp[%d] = max(%d,%d + %d-%d)\n", i, dp[i], dp[j], acc[j-1], acc[i])
			updateBit(i, dp[i]+acc[i-1])
		}
	}

	// printf("dp = %v\n", dp)

	ans += dp[1]
	printf("%d\n", ans)
}

package main

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

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

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

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

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		if last[i] == 0 {
			reach := queryBit(first[i] - 1)
			if reach > first[i] {
				ans++
			}
			first[i] = 0
		} else {
			pairs = append(pairs, []int{first[i], last[i]})
		}
	}

	// printf("ans1 = %d\n", ans)

	// printf("pairs = %v\n", pairs)
	dp := make([]int, n+2)
	match := make([]int, n+1)

	dp[len(pairs)] = 0

	acc := make([]int, n+1)
	for _, p := range pairs {
		match[p[0]] = p[1]
		acc[p[0]]++
		acc[p[1]]++
	}

	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}

	for i := n; i >= 1; i-- {
		dp[i] = dp[i+1]
		j := match[i]
		if j > 0 {
			dp[i] = max(dp[i], dp[j+1]+acc[j-1]-acc[i])
		}
	}

	// printf("dp = %v\n", dp)

	ans += dp[1]
	printf("%d\n", ans)
}

package main

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

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

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

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

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
		}
	}

	// printf("ans1 = %d\n", ans)

	// printf("pairs = %v\n", pairs)
	dp := make([]int, n+2)
	match := make([]int, n+1)

	dp[len(pairs)] = 0

	acc := make([]int, n+1)
	for _, p := range pairs {
		match[p[0]] = p[1]
		acc[p[0]]++
		acc[p[1]]++
	}

	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}

	for i := n; i >= 1; i-- {
		dp[i] = dp[i+1]
		j := match[i]
		if j > 0 {
			dp[i] = max(dp[i], dp[j+1]+acc[j-1]-acc[i])
		}
	}

	// printf("dp = %v\n", dp)

	ans += dp[1]
	printf("%d\n", ans)
}

package main

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

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

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

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

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
		}
	}

	// printf("ans1 = %d\n", ans)

	sort.Slice(pairs, func(i, j int) bool {
		return pairs[i][0] < pairs[j][0]
	})

	// printf("pairs = %v\n", pairs)
	dp := make([]int, len(pairs)+1)
	dp[len(pairs)] = 0

	acc := make([]int, n+1)
	for _, p := range pairs {
		acc[p[0]]++
		acc[p[1]]++
	}
	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}

	for i := len(pairs) - 1; i >= 0; i-- {
		dp[i] = dp[i+1]
		lo := i
		hi := len(pairs)
		var mi int
		for lo < hi {
			mi = (lo + hi) / 2
			// printf("lo=%d, hi=%d, mi=%d\n", lo, hi, mi)
			if pairs[i][1] >= pairs[mi][0] {
				lo = mi + 1
			} else {
				hi = mi
			}
		}
		// printf("from %d goes to %d\n", i, lo)
		dp[i] = max(dp[i], dp[lo]+acc[pairs[i][1]-1]-acc[pairs[i][0]])
	}

	// printf("dp = %v\n", dp)

	ans += dp[0]
	printf("%d\n", ans)
}

package main

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

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

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

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

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
		}
	}

	// printf("ans1 = %d\n", ans)

	sort.Slice(pairs, func(i, j int) bool {
		return pairs[i][0] < pairs[j][0]
	})

	// printf("pairs = %v\n", pairs)
	dp := make([]int, len(pairs)+1)
	dp[len(pairs)] = 0

	acc := make([]int, n+1)
	for _, p := range pairs {
		acc[p[0]]++
		acc[p[1]]++
	}
	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}

	for i := len(pairs) - 1; i >= 0; i-- {
		dp[i] = dp[i+1]
		lo := i
		hi := len(pairs)
		var mi int
		for lo < hi {
			mi = (lo + hi) / 2
			// printf("lo=%d, hi=%d, mi=%d\n", lo, hi, mi)
			if pairs[i][1] > pairs[mi][0] {
				lo = mi + 1
			} else {
				hi = mi
			}
		}
		// printf("from %d goes to %d\n", i, lo)
		if i < lo {
			dp[i] = max(dp[i], dp[lo]+acc[pairs[i][1]-1]-acc[pairs[i][0]])
		}
	}

	// printf("dp = %v\n", dp)

	ans += dp[0]
	printf("%d\n", ans)
}

package main

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

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

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

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

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
		}
	}

	// printf("ans1 = %d\n", ans)

	sort.Slice(pairs, func(i, j int) bool {
		return pairs[i][0] < pairs[j][0]
	})

	// printf("pairs = %v\n", pairs)
	dp := make([]int, len(pairs)+1)
	dp[len(pairs)] = 0

	acc := make([]int, n+1)
	for _, p := range pairs {
		acc[p[0]]++
		acc[p[1]]++
	}
	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}

	for i := len(pairs) - 1; i >= 0; i-- {
		dp[i] = dp[i+1]
		lo := i
		hi := len(pairs)
		var mi int
		for lo < hi {
			mi = (lo + hi) / 2
			// printf("lo=%d, hi=%d, mi=%d\n", lo, hi, mi)
			if pairs[i][1] > pairs[mi][0] {
				lo = mi + 1
			} else {
				hi = mi
			}
		}
		// printf("from %d goes to %d\n", i, lo)
		if i > lo {
			dp[i] = max(dp[i], dp[lo]+acc[pairs[i][1]-1]-acc[pairs[i][0]])
		}
	}

	// printf("dp = %v\n", dp)

	ans += dp[0]
	printf("%d\n", ans)
}

package main

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

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

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

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

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
		}
	}

	// printf("ans1 = %d\n", ans)

	sort.Slice(pairs, func(i, j int) bool {
		return pairs[i][0] < pairs[j][0]
	})

	// printf("pairs = %v\n", pairs)
	dp := make([]int, len(pairs)+1)
	dp[len(pairs)] = 0

	acc := make([]int, n+1)
	for _, p := range pairs {
		acc[p[0]]++
		acc[p[1]]++
	}
	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}

	for i := len(pairs) - 1; i >= 0; i-- {
		dp[i] = dp[i+1]
		lo := i
		hi := len(pairs)
		var mi int
		for lo < hi {
			mi = (lo + hi) / 2
			// printf("lo=%d, hi=%d, mi=%d\n", lo, hi, mi)
			if pairs[i][1] > pairs[mi][0] {
				lo = mi + 1
			} else {
				hi = mi
			}
		}
		// printf("from %d goes to %d\n", i, lo)
		dp[i] = max(dp[i], dp[lo]+acc[pairs[i][1]-1]-acc[pairs[i][0]])
	}

	// printf("dp = %v\n", dp)

	ans += dp[0]
	printf("%d\n", ans)
}

package main

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

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

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

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

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	var lonelyPair []bool
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
			lonelyPair = append(lonelyPair, reach < first[i])
		}
	}

	// printf("ans1 = %d\n", ans)
	// printf("pairs = %v\n", pairs)
	// printf("lonelyPair = %v\n", lonelyPair)

	resetBit()
	for i := 0; i < len(pairs); i++ {
		newFirst := n - pairs[i][1] + 1
		newLast := n - pairs[i][0] + 1
		pairs[i][0] = newFirst
		pairs[i][1] = newLast
		updateBit(pairs[i][0], pairs[i][1])
	}

	entangledPairs := 0
	for i, pair := range pairs {
		reach := queryBit(pair[0] - 1)
		lonelyPair[i] = lonelyPair[i] && reach < pair[0]
		if !lonelyPair[i] {
			entangledPairs++
		}
	}

	// printf("pairs = %v\n", pairs)
	// printf("lonelyPair = %v\n", lonelyPair)

	// printf("entangledPairs = %d\n", entangledPairs)
	ans += max(entangledPairs-1, 0)
	printf("%d\n", ans)
}

package main

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

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

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = int(2e5 + 10)

var n int
var first [TAM]int
var last [TAM]int
var bit [TAM]int

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

func updateBit(i, v int) {
	for i++; i < TAM; i += (i & (-i)) {
		bit[i] = max(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & (-i)) {
		r = max(r, bit[i])
	}
	return r
}

func resetBit() {
	for i := 0; i < TAM; i++ {
		bit[i] = 0
	}
}

func main() {
	defer flush()

	ans := 0
	scan(&n)
	for i := 1; i <= n; i++ {
		var ai = 0
		scan(&ai)
		if first[ai] == 0 {
			first[ai] = i
		} else if last[ai] == 0 {
			last[ai] = i
		} else {
			ans++
			last[ai] = i
		}
	}

	// printf("ans0 = %d\n", ans)
	for i := 1; i <= n; i++ {
		if last[i] > 0 {
			updateBit(first[i], last[i])
		}
	}

	var pairs [][]int
	var lonelyPair []bool
	for i := 1; i <= n; i++ {
		if first[i] == 0 {
			continue
		}
		reach := queryBit(first[i] - 1)
		if reach > max(first[i], last[i]) {
			ans++
			if last[i] > 0 {
				ans++
			}
			first[i] = 0
			last[i] = 0
		}

		if last[i] > 0 {
			pairs = append(pairs, []int{first[i], last[i]})
			lonelyPair = append(lonelyPair, reach < first[i])
		}
	}

	// printf("ans1 = %d\n", ans)

	resetBit()
	for i := 0; i < len(pairs); i++ {
		newFirst := n - pairs[i][1] + 1
		newLast := n - pairs[i][0] + 1
		pairs[i][0] = newFirst
		pairs[i][1] = newLast
		updateBit(pairs[i][0], pairs[i][1])
	}

	entangledPairs := 0
	for i, pair := range pairs {
		reach := queryBit(pair[0] - 1)
		lonelyPair[i] = lonelyPair[i] && reach < pair[0]
		if !lonelyPair[i] {
			entangledPairs++
		}
	}

	// printf("entangledPairs = %d\n", entangledPairs)
	ans += entangledPairs - 1
	printf("%d\n", ans)
}