Little Alyona is celebrating Happy Birthday! Her mother has an array of n flowers. Each flower has some mood, the mood of i-th flower is ai. The mood can be positive, zero or negative.Let's define a subarray as a segment of consecutive flowers. The mother suggested some set of subarrays. Alyona wants to choose several of the subarrays suggested by her mother. After that, each of the flowers will add to the girl's happiness its mood multiplied by the number of chosen subarrays the flower is in.For example, consider the case when the mother has 5 flowers, and their moods are equal to 1,  - 2, 1, 3,  - 4. Suppose the mother suggested subarrays (1,  - 2), (3,  - 4), (1, 3), (1,  - 2, 1, 3). Then if the girl chooses the third and the fourth subarrays then:   the first flower adds 1·1 = 1 to the girl's happiness, because he is in one of chosen subarrays,  the second flower adds ( - 2)·1 =  - 2, because he is in one of chosen subarrays,  the third flower adds 1·2 = 2, because he is in two of chosen subarrays,  the fourth flower adds 3·2 = 6, because he is in two of chosen subarrays,  the fifth flower adds ( - 4)·0 = 0, because he is in no chosen subarrays. Thus, in total 1 + ( - 2) + 2 + 6 + 0 = 7 is added to the girl's happiness. Alyona wants to choose such subarrays from those suggested by the mother that the value added to her happiness would be as large as possible. Help her do this!Alyona can choose any number of the subarrays, even 0 or all suggested by her mother.

The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of flowers and the number of subarrays suggested by the mother. The second line contains the flowers moods — n integers a1, a2, ..., an ( - 100 ≤ ai ≤ 100). The next m lines contain the description of the subarrays suggested by the mother. The i-th of these lines contain two integers li and ri (1 ≤ li ≤ ri ≤ n) denoting the subarray a[li], a[li + 1], ..., a[ri]. Each subarray can encounter more than once.

Print single integer — the maximum possible value added to the Alyona's happiness.

NoteThe first example is the situation described in the statements.In the second example Alyona should choose all subarrays.The third example has answer 0 because Alyona can choose none of the subarrays.

package main

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

func solve() {
    n, m := readInt(), readInt()
    a := readIntArray(n)
    ans := 0
    for t := 0; t < m; t++ {
        l, r := readInt()-1, readInt()-1
        s := 0
        for i := l; i <= r; i++ {
            s += a[i]
        }
        if s > 0 {
            ans += s
        }
    }
    fmt.Println(ans)
}

// Helpers

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 {
	if x < y {
		return x
	}
	return 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 readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

// Sort

type Ints64 []int64

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

/* 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 (
	"fmt"
)

func main(){
	var n, m, ans int
	var data [105]int
	fmt.Scanf("%d %d\n", &n, &m)
	arr := data[0:n + 1]
	arr[0] = 0
	for i := 1; i <= n; i++{
		if i == n{
			fmt.Scanf("%d\n", &arr[i])
		}else{
			fmt.Scanf("%d", &arr[i])
		}
		arr[i] += arr[i - 1]
	}
	ans = 0
	for i := 0; i < m; i++{
		s, t := 0, 0
		fmt.Scanf("%d %d\n", &s, &t)
		if arr[t] - arr[s - 1] > 0{
			ans += arr[t] - arr[s - 1];
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, c int
	fmt.Scan(&n, &c)
	raw := make([]int, n)
	sum := make([]int, n)
	values := make([]int, c)
	for i := 0; i < n; i++ {
		fmt.Scan(&raw[i])
		sum[i] = raw[i]
		if i > 0 {
			sum[i] += sum[i-1]
		}
	}
	for i := 0; i < c; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		a--
		b--
		left := 0
		if a > 0 {
			left = sum[a-1]
		}
		values[i] = (sum[b] - left)
	}
	sort.Ints(values)
	ans := 0
	for i := len(values) - 1; i >= 0; i-- {
		if values[i] < 0 {
			break
		}
		ans += values[i]
	}
	fmt.Println(ans)
}

package main

import "fmt"

func main() {
	var n, m int
	fmt.Scan(&n)
	fmt.Scan(&m)

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

	var sc []int
	for i := 0; i < m; i++ {
		var l, r, s int
		fmt.Scan(&l, &r)
		for j := l - 1; j < r && j < n; j++ {
			s += a[j]
		}
		sc = append(sc, s)
	}

	var res int
	for _, s := range sc {
		if s > 0 {
			res += s
		}
	}

	fmt.Println(res)
}

package main

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

func main() {
	sc := NewScanner()
	_, m := sc.NextInt(), sc.NextInt()
	A := sc.NextIntArray()
	ans := 0
	for i := 0; i < m; i++ {
		L, R := sc.NextInt(), sc.NextInt()
		sm := 0
		for i := L - 1; i <= R - 1; i++ {
			sm += A[i]
		}
		if sm >= 0 {
			ans += sm
		}
	}
	fmt.Println(ans)

}

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

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
		}
	}
}


A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i,  j) (1  ≤ i  <  j ≤ k), xi  +  xj is a prime.You are given an array a with n positive integers a1,  a2,  ...,  an (not necessary distinct). You want to find a simple subset of the array a with the maximum size.A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it.

The first line contains integer n (1 ≤ n ≤ 1000) — the number of integers in the array a. The second line contains n integers ai (1 ≤ ai ≤ 106) — the elements of the array a.

On the first line print integer m — the maximum possible size of simple subset of a. On the second line print m integers bl — the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order.

package main

import "fmt"

const maxA = 1e6

var composite [2*maxA + 1]bool

func init() {
	for i := 2; i < len(composite); i++ {
		if composite[i] {
			continue
		}
		for j := 2 * i; j < len(composite); j += i {
			composite[j] = true
		}
	}
}

func main() {
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	ones := 0
	for i := range a {
		fmt.Scan(&a[i])
		if a[i] == 1 {
			ones++
		}
	}
	var res []int
	if ones > 0 {
		for i := 0; i < ones; i++ {
			res = append(res, 1)
		}
		for _, j := range a {
			if j != 1 && !composite[j+1] {
				res = append(res, j)
				break
			}
		}
	}
	if len(res) == 0 {
		res = append(res, a[0])
	}
	if len(res) < 2 {
	Loop:
		for i, x := range a {
			for _, y := range a[i+1:] {
				if !composite[x+y] {
					res = []int{x, y}
					break Loop
				}
			}
		}
	}
	fmt.Println(len(res))
	for i := range res {
		if i+1 == len(res) {
			fmt.Println(res[i])
		} else {
			fmt.Print(res[i], " ")
		}
	}
}

package main

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

var stdin *bufio.Reader
var stdout *bufio.Writer

func init() {
	stdin = bufio.NewReader(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func isPrime(n int) bool {

	if n == 2 || n == 3 {
		return true
	} else if n == 1 || n%2 == 0 || n%3 == 0 {
		return false
	}

	for i := 6; (i-1)*(i-1) <= n; i += 6 {
		if n%(i-1) == 0 || n%(i+1) == 0 {
			return false
		}
	}

	return true
}

func main() {
	n, max := 0, 0
	fmt.Fscanf(stdin, "%d\n", &n)

	one, odd, even := make([]int, 0), make([]int, 0), make([]int, 0)
	for i := 0; i < n; i++ {
		number := 0
		fmt.Fscanf(stdin, "%d", &number)
		switch {
		case number == 1:
			one = append(one, 1)
		case number%2 == 0:
			even = append(even, number)
		case number%2 == 1:
			odd = append(odd, number)
		}
	}
	fmt.Fscanf(stdin, "\n", nil)

	/* Choose all 1 and select 1 even number */
	if len(one) > 0 && len(even) > 0 {
		for _, value := range even {
			if isPrime(value+1) == true {
				max = len(one) + 1
				fmt.Fprintf(stdout, "%d\n", max)
				for i := 0; i < len(one); i++ {
					fmt.Fprintf(stdout, "1 ")
				}
				fmt.Fprintf(stdout, "%d\n", value)
				stdout.Flush()
				return
			}
		}
	}

	if len(one) >= 2 {
		fmt.Fprintf(stdout, "%d\n", len(one))
		for i := 0; i < len(one); i++ {
			fmt.Fprintf(stdout, "1 ")
		}
		fmt.Fprintf(stdout, "\n")
		stdout.Flush()
		return
	}

	if len(odd) > 0 && len(even) > 0 {
		for i := 0; i < len(odd); i++ {
			for j := 0; j < len(even); j++ {
				if isPrime(odd[i]+even[j]) == true {
					fmt.Fprintf(stdout, "2\n%d %d\n", odd[i], even[j])
					stdout.Flush()
					return
				}
			}
		}
	}

	if len(one) > 0 {
		fmt.Fprintf(stdout, "1\n1\n")
		stdout.Flush()
		return
	}

	if len(odd) > 0 {
		fmt.Fprintf(stdout, "1\n%d\n", odd[0])
		stdout.Flush()
		return
	}

	if len(even) > 0 {
		fmt.Fprintf(stdout, "1\n%d\n", even[0])
		stdout.Flush()
		return
	}

	fmt.Fprintf(stdout, "0\n")

	stdout.Flush()
}

package main

import "fmt"

const maxA = 1e6

var composite [2*maxA + 1]bool

func init() {
	for i := 2; i < len(composite); i++ {
		if composite[i] {
			continue
		}
		for j := 2 * i; j < len(composite); j += i {
			composite[j] = true
		}
	}
}

func main() {
	var n int
	fmt.Scan(&n)
	a := make([]int, 0, n)
	ones := 0
	for i := 0; i < n; i++ {
		var ai int
		fmt.Scan(&ai)
		if ai == 1 {
			ones++
			continue
		}
		a = append(a, ai)
	}
	var res []int
	if ones > 0 {
		for i := 0; i < ones; i++ {
			res = append(res, 1)
		}
		for _, j := range a {
			if j == 2 {
				res = append(res, 2)
				break
			}
		}
	}
	if len(res) == 0 {
		res = append(res, a[0])
	}
	if len(res) < 2 {
	Loop:
		for i, x := range a {
			for _, y := range a[i+1:] {
				if !composite[x+y] {
					res = []int{x, y}
					break Loop
				}
			}
		}
	}
	fmt.Println(len(res))
	for i := range res {
		if i+1 == len(res) {
			fmt.Println(res[i])
		} else {
			fmt.Print(res[i], " ")
		}
	}
}

package main

import "fmt"

const maxA = 1e6

var composite [2*maxA + 1]bool

func init() {
	for i := 2; i < len(composite); i++ {
		if composite[i] {
			continue
		}
		for j := 2 * i; j < len(composite); j += i {
			composite[j] = true
		}
	}
}

func main() {
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	for i := range a {
		fmt.Scan(&a[i])
	}
	res := a[:1]
	for i, x := range a {
		for j, y := range a[i+1:] {
			if composite[x+y] {
				continue
			}
			p := []int{x, y}
			for _, z := range a[i+1+j+1:] {
				if composite[x+z] || composite[y+z] {
					continue
				}
				p = append(p, z)
			}
			if len(p) == 2 {
				if len(res) < len(p) {
					res = p
				}
				continue
			}
			cur := make([]int, len(p))
		Loop:
			for m := 1; m < 1<<uint(len(p)); m++ {
				cur = cur[:0]
				for k := range p {
					if (1<<uint(k))&m == 0 {
						continue
					}
					for _, c := range cur {
						if composite[c+p[k]] {
							continue Loop
						}
					}
					cur = append(cur, p[k])
				}
				if len(cur) > len(res) {
					res = append(res[:0], cur...)
				}
			}
		}
	}
	fmt.Println(len(res))
	for i := range res {
		if i+1 == len(res) {
			fmt.Println(res[i])
		} else {
			fmt.Print(res[i], " ")
		}
	}
}

package main

import "fmt"

const maxA = 1e6

var composite [2*maxA + 1]bool

func init() {
	for i := 2; i < len(composite); i++ {
		if composite[i] {
			continue
		}
		for j := 2 * i; j < len(composite); j += i {
			composite[j] = true
		}
	}
}

func main() {
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	for i := range a {
		fmt.Scan(&a[i])
	}
	res := a[:1]
	for i, x := range a {
		for j, y := range a[i+1:] {
			if composite[x+y] {
				continue
			}
			p := []int{x, y}
			for _, z := range a[i+1+j+1:] {
				if composite[x+z] || composite[y+z] {
					continue
				}
				p = append(p, z)
			}
			if len(p) == 2 {
				if len(res) < len(p) {
					res = p
				}
				continue
			}
			if len(p)-2 > 30 {
				panic(len(p))
			}
			cur := make([]int, len(p))
			cur[0] = x
			cur[1] = y
		Loop:
			for m := 1; m < 1<<uint(len(p)-2); m++ {
				cur = cur[:2]
				for k := range p[2:] {
					if (1<<uint(k))&m == 0 {
						continue
					}
					for _, c := range cur {
						if composite[c+p[k+2]] {
							continue Loop
						}
					}
					cur = append(cur, p[k+2])
				}
				if len(cur) > len(res) {
					res = append(res[:0], cur...)
				}
			}
		}
	}
	fmt.Println(len(res))
	for i := range res {
		if i+1 == len(res) {
			fmt.Println(res[i])
		} else {
			fmt.Print(res[i], " ")
		}
	}
}

package main

import "fmt"

const maxA = 1e6

var composite [2*maxA + 1]bool

func init() {
	for i := 2; i < len(composite); i++ {
		if composite[i] {
			continue
		}
		for j := 2 * i; j < len(composite); j += i {
			composite[j] = true
		}
	}
}

func main() {
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	ones := 0
	for i := range a {
		fmt.Scan(&a[i])
		if a[i] == 1 {
			ones++
		}
	}
	var res []int
	if ones > 0 {
		for i := 0; i < ones; i++ {
			res = append(res, 1)
		}
		for _, j := range a {
			if j == 2 {
				res = append(res, 2)
				break
			}
		}
	}
	if len(res) == 0 {
		res = append(res, a[0])
	}
	if len(res) < 2 {
	Loop:
		for i, x := range a {
			for _, y := range a[i+1:] {
				if !composite[x+y] {
					res = []int{x, y}
					break Loop
				}
			}
		}
	}
	fmt.Println(len(res))
	for i := range res {
		if i+1 == len(res) {
			fmt.Println(res[i])
		} else {
			fmt.Print(res[i], " ")
		}
	}
}

package main

import "fmt"

const maxA = 1e6

var composite [2*maxA + 1]bool

func init() {
	for i := 2; i < len(composite); i++ {
		if composite[i] {
			continue
		}
		for j := 2 * i; j < len(composite); j += i {
			composite[j] = true
		}
	}
}

func main() {
	var n int
	fmt.Scan(&n)
	a := make([]int, 0, n)
	ones := 0
	for i := 0; i < n; i++ {
		var ai int
		fmt.Scan(&ai)
		if ai == 1 {
			ones++
			continue
		}
		a = append(a, ai)
	}
	var res []int
	if ones > 0 {
		for i := 0; i < ones; i++ {
			res = append(res, 1)
		}
		for _, j := range a {
			if j == 2 {
				res = append(res, 2)
				break
			}
		}
	}
	if len(res) == 0 {
		res = append(res, a[0])
	}
	for i, x := range a {
		for j, y := range a[i+1:] {
			if composite[x+y] {
				continue
			}
			p := []int{x, y}
			for _, z := range a[i+1+j+1:] {
				if composite[x+z] || composite[y+z] {
					continue
				}
				p = append(p, z)
			}
			if len(p) == 2 {
				if len(res) < len(p) {
					res = p
				}
				continue
			}
			if len(p)-2 > 30 {
				panic(len(p))
			}
			cur := make([]int, len(p))
			cur[0] = x
			cur[1] = y
		Loop:
			for m := 1; m < 1<<uint(len(p)-2); m++ {
				cur = cur[:2]
				for k := range p[2:] {
					if (1<<uint(k))&m == 0 {
						continue
					}
					for _, c := range cur {
						if composite[c+p[k+2]] {
							continue Loop
						}
					}
					cur = append(cur, p[k+2])
				}
				if len(cur) > len(res) {
					res = append(res[:0], cur...)
				}
			}
		}
	}
	fmt.Println(len(res))
	for i := range res {
		if i+1 == len(res) {
			fmt.Println(res[i])
		} else {
			fmt.Print(res[i], " ")
		}
	}
}

package main

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

var stdin *bufio.Reader
var stdout *bufio.Writer

func init() {
	stdin = bufio.NewReader(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func isPrime(n int) bool {

	if n == 2 || n == 3 {
		return true
	} else if n == 1 || n%2 == 0 || n%3 == 0 {
		return false
	}

	for i := 6; (i-1)*(i-1) <= n; i += 6 {
		if n%(i-1) == 0 || n%(i+1) == 0 {
			return false
		}
	}

	return true
}

func main() {
	n, max := 0, 0
	fmt.Fscanf(stdin, "%d\n", &n)

	one, odd, even := make([]int, 0), make([]int, 0), make([]int, 0)
	for i := 0; i < n; i++ {
		number := 0
		fmt.Fscanf(stdin, "%d", &number)
		switch {
		case number == 1:
			one = append(one, 1)
		case number%2 == 0:
			even = append(even, number)
		case number%2 == 1:
			odd = append(odd, number)
		}
	}
	fmt.Fscanf(stdin, "\n", nil)

	/* Choose all 1 and select 1 even number */
	if len(one) > 0 && len(even) > 0 {
		for _, value := range even {
			if isPrime(value+1) == true {
				max = len(one) + 1
				fmt.Fprintf(stdout, "%d\n", max)
				for i := 0; i < len(one); i++ {
					fmt.Fprintf(stdout, "1 ")
				}
				fmt.Fprintf(stdout, "%d\n", value)
				stdout.Flush()
				return
			}
		}
	}

	if len(odd) > 0 && len(even) > 0 {
		for i := 0; i < len(odd); i++ {
			for j := 0; j < len(even); j++ {
				if isPrime(odd[i]+even[j]) == true {
					fmt.Fprintf(stdout, "2\n%d %d\n", odd[i], even[j])
					stdout.Flush()
					return
				}
			}
		}
	}

	if len(one) > 0 {
		fmt.Fprintf(stdout, "1\n1\n")
		stdout.Flush()
		return
	}

	if len(odd) > 0 {
		fmt.Fprintf(stdout, "1\n%d\n", odd[0])
		stdout.Flush()
		return
	}

	if len(even) > 0 {
		fmt.Fprintf(stdout, "1\n%d\n", even[0])
		stdout.Flush()
		return
	}

	fmt.Fprintf(stdout, "0\n")

	stdout.Flush()
}


Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $$$n$$$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.Suppose in the first round participant A took $$$x$$$-th place and in the second round — $$$y$$$-th place. Then the total score of the participant A is sum $$$x + y$$$. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every $$$i$$$ from $$$1$$$ to $$$n$$$ exactly one participant took $$$i$$$-th place in first round and exactly one participant took $$$i$$$-th place in second round.Right after the end of the Olympiad, Nikolay was informed that he got $$$x$$$-th place in first round and $$$y$$$-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases to solve. Each of the following $$$t$$$ lines contains integers $$$n$$$, $$$x$$$, $$$y$$$ ($$$1 \leq n \leq 10^9$$$, $$$1 \le x, y \le n$$$) — the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round.

Print two integers — the minimum and maximum possible overall place Nikolay could take.

NoteExplanation for the first example:Suppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows:  However, the results of the Olympiad could also look like this:  In the first case Nikolay would have taken first place, and in the second — third place.

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		n, x, y := 0, 0, 0
		fmt.Fscanf(in, "%d %d %d\n", &n, &x, &y)
		if x > y {
			x, y = y, x
		}
		fmt.Fprintf(out, "%d %d\n", min(getAns1(n, x, y), getAns2(n, y, x)), max(getAns2(n, x, y), getAns2(n, y, x)))
	}
	return
}

func getAns1(n, x, y int) int {
	p := x - 1
	q := n - y - 1
	if q > 0 {
		p -= q
		if p < 0 {
			p = 0
		}
		return p + 1
	} else if q == 0 {
		return x
	} else {
		ans1 := x + 1
		if ans1 > n {
			ans1 = n
		}
		return ans1
	}

}

func getAns2(n, x, y int) int {
	q := y - 1
	ans2 := x + q
	if ans2 > n {
		ans2 = n
	}
	return ans2
}

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
}

// yun xu you better ge geng haode ren
func ok1(n, x, y, better int) bool {
	if better < (x - 1) {
		return can1(n-better, x-better, y-better)
	} else {
		return true
	}
}

// can be no1
func can1(n, x, y int) bool {
	if (x + y) < 1+n {
		return true
	}
	return false
}

package main

import "fmt"

func main() {
	var cases int
	fmt.Scanf("%d\n", &cases)
	for i := 0; i < cases; i++ {
		var n, a, b int
		fmt.Scanf("%d %d %d\n", &n, &a, &b) // 3 3 // 3
		if n == 1 {
			fmt.Println(1, 1)
			continue
		}
		if n == 2 {

		}
		worst, best := 0, 0
		place := a + b
		worst = place - 1
		if worst > n {
			worst = n
		}
		best = place - n + 1
		if place == 2 {
			best = 1
		}
		if best > n {
			best = n
		}
		if best <= 0 {
			best = 1
		}
		fmt.Println(best, worst)
	}
}

package main

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

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

	solve := func() (ans1, ans2 int) {
		var n, x, y int
		Fscan(in, &n, &x, &y)
		if x > y {
			x, y = y, x
		}
		if y == 1 {
			return 1, 1
		}

		if x>= n-1 {
			ans1 = y
		} else {
			if x+y<n+1 {
				ans1=1
			} else {
				remove := n-x-1
				ans1 = y-remove
			}
		}

		if x+y < n+1 {
			ans2 = x + y - 1
		} else {
			ans2 = n
		}

		return
	}

	var t int
	Fscan(in, &t)
	for _case := 1; _case <= t; _case++ {
		a,b:=solve()
		Fprintln(out, a,b)
	}
}

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

package main

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

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

	solve := func() (ans1, ans2 int) {
		var n, x, y int
		Fscan(in, &n, &x, &y)
		if x > y {
			x, y = y, x
		}
		if x >= n-1 {
			ans1 = y // 比赛时这里我写成了 n
		} else {
			if x+y < n+1 {
				ans1 = 1
			} else {
				remove := n - x - 1
				ans1 = y - remove
			}
		}
		if x+y < n+1 {
			ans2 = x + y - 1
		} else {
			ans2 = n
		}
		return
	}
	_ = solve

	solve2 := func() (ans1, ans2 int) {
		var n, x, y int
		Fscan(in, &n, &x, &y)
		if x+y <= n {
			return 1, x + y - 1
		}
		if x+y < 2*n {
			return x + y - n + 1, n
		}
		return n, n
	}
	_ = solve2

	var t int
	Fscan(in, &t)
	for _case := 1; _case <= t; _case++ {
		a, b := solve2()
		Fprintln(out, a, b)
	}
}

// 打表就行了
// 2 1 1 1 1
// 3 1 2 1 2
// 3 2 1 1 2
// 4 1 3 1 3
// 4 2 2 1 3
// 4 3 1 1 3
// 5 1 4 1 4
// 5 2 3 1 4
// 5 3 2 1 4
// 5 4 1 1 4
// 6 1 5 2 5
// 6 2 4 2 5
// 6 3 3 2 5
// 6 4 2 2 5
// 6 5 1 2 5
// 7 2 5 3 5
// 7 3 4 3 5
// 7 4 3 3 5
// 7 5 2 3 5
// 8 3 5 4 5
// 8 4 4 4 5
// 8 5 3 4 5
// 9 4 5 5 5
// 9 5 4 5 5
//10 5 5 5 5
func table(n int) {
	permutations := func(n, r int, do func(indexes []int)) {
		indexes := make([]int, n)
		for i := range indexes {
			indexes[i] = i + 1
		}
		do(indexes[:r])
		cycles := make([]int, r)
		for i := range cycles {
			cycles[i] = n - i
		}
		for {
			i := r - 1
			for ; i >= 0; i-- {
				cycles[i]--
				if cycles[i] == 0 {
					tmp := indexes[i]
					copy(indexes[i:], indexes[i+1:])
					indexes[n-1] = tmp
					cycles[i] = n - i
				} else {
					j := cycles[i]
					indexes[i], indexes[n-j] = indexes[n-j], indexes[i]
					do(indexes[:r])
					break
				}
			}
			if i == -1 {
				return
			}
		}
	}
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}

	getPlace := func(indexes1, indexes2 []int) (ans int) {
		score := indexes1[0] + indexes2[0]
		for i := 1; i < 5; i++ {
			if indexes1[i]+indexes2[i] <= score {
				ans++
			}
		}
		return ans + 1
	}

	for sum := 0; sum < 2*n-1; sum++ {
		for x := 0; x <= sum; x++ {
			y := sum - x
			xx, yy := x+1, y+1
			if xx > n || yy > n {
				continue
			}
			minPlace, maxPlace := int(1e9), 0
			permutations(n, n, func(indexes1 []int) {
				if indexes1[0] == xx {
					permutations(n, n, func(indexes2 []int) {
						if indexes2[0] == yy {
							p := getPlace(indexes1, indexes2)
							minPlace = min(minPlace, p)
							maxPlace = max(maxPlace, p)
						}
					})
				}
			})
			Println(xx+yy, xx, yy, minPlace, maxPlace)
		}
	}
}

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

package main

import "fmt"

func main() {
	var t int
	fmt.Scan(&t)
	for tc := 0; tc < t; tc++ {
		var n, x, y int64
		fmt.Scan(&n, &x, &y)
		best, worst := solve(n, x, y)
		fmt.Println(best, worst)
	}
}

func solve(n, x, y int64) (best, worst int64) {
	if x > y {
		x, y = y, x
	}

	best = x - (n - y - 1)
	if best < 1 {
		best = 1
	} else if best > n {
		best = n
	}
	worst = y + x - 1
	if worst < 1 {
		worst = 1
	} else if worst > n {
		worst = n
	}
	return best, worst
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		n, x, y := 0, 0, 0
		fmt.Fscanf(in, "%d %d %d\n", &n, &x, &y)
		if x > y {
			x, y = y, x
		}
		ans1 := x
		ans2 := x
		p := x - 1
		q := n - y - 1
		if q > 0 {
			p -= q
			if p < 0 {
				p = 0
			}
		}
		ans1 = p + 1
		x, y = y, x
		p = n - x
		q = y - 1
		ans2 = x + q
		if ans2 > n {
			ans2 = n
		}
		fmt.Fprintf(out, "%d %d\n", ans1, ans2)
	}
	return
}

// yun xu you better ge geng haode ren
func ok1(n, x, y, better int) bool {
	if better < (x - 1) {
		return can1(n-better, x-better, y-better)
	} else {
		return true
	}
}

// can be no1
func can1(n, x, y int) bool {
	if (x + y) < 1+n {
		return true
	}
	return false
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		n, x, y := 0, 0, 0
		fmt.Fscanf(in, "%d %d %d\n", &n, &x, &y)
		if x > y {
			x, y = y, x
		}
		fmt.Fprintf(out, "%d %d\n", min(getAns1(n, x, y), getAns2(n, y, x)), max(getAns2(n, x, y), getAns2(n, y, x)))
	}
	return
}

func getAns1(n, x, y int) int {
	p := x - 1
	q := n - y - 1
	if q > 0 {
		p -= q
		if p < 0 {
			p = 0
		}
	}
	ans1 := p + 1
	return ans1
}

func getAns2(n, x, y int) int {
	q := y - 1
	ans2 := x + q
	if ans2 > n {
		ans2 = n
	}
	return ans2
}

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
}

// yun xu you better ge geng haode ren
func ok1(n, x, y, better int) bool {
	if better < (x - 1) {
		return can1(n-better, x-better, y-better)
	} else {
		return true
	}
}

// can be no1
func can1(n, x, y int) bool {
	if (x + y) < 1+n {
		return true
	}
	return false
}

package main

import "fmt"

func main() {
	var cases int
	fmt.Scanf("%d\n", &cases)
	for i := 0; i < cases; i++ {
		var n, a, b int
		fmt.Scanf("%d %d %d\n", &n, &a, &b) // 3 3 3
		worst, best := 0, 0
		place := a + b
		worst = place - 1
		if worst > n {
			worst = n
		}
		best = place - n + 1
		if best <= 0 {
			best = 1
		}
		fmt.Println(best, worst)
	}
}

package main

import "fmt"

func main() {
	var cases int
	fmt.Scanf("%d\n", &cases)
	for i := 0; i < cases; i++ {
		var n, a, b int
		fmt.Scanf("%d %d %d\n", &n, &a, &b) // 3 3 3
		worst, best := 0, 0
		place := a + b
		worst = place - 1
		if worst > n {
			worst = n
		}
		best = place - n + 1
		if place == 2 {
			best = 1
		}
		if best <= 0 {
			best = 1
		}
		fmt.Println(best, worst)
	}
}

package main

import "fmt"

func main() {
	var cases int
	fmt.Scanf("%d\n", &cases)
	for i := 0; i < cases; i++ {
		var n, a, b int
		fmt.Scanf("%d %d %d\n", &n, &a, &b) // 3 3 3
		worst, best := 0, 0
		place := a + b
		bb := place - 2
		worst = bb+1
		best = place - n + 1
		if best <= 0 {
			best = 1
		}
		fmt.Println(best, worst)
	}
}

package main

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

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

	solve := func() (ans1, ans2 int) {
		var n, x, y int
		Fscan(in, &n, &x, &y)
		if x > y {
			x, y = y, x
		}
		if y == 1 {
			return 1, 1
		}

		if x>= n-1 {
			ans1 = n
		} else {
			if x+y<n+1 {
				ans1=1
			} else {
				remove := n-x-1
				ans1 = y-remove
			}
		}

		if x+y < n+1 {
			ans2 = x + y - 1
		} else {
			ans2 = n
		}

		return
	}

	var t int
	Fscan(in, &t)
	for _case := 1; _case <= t; _case++ {
		a,b:=solve()
		Fprintln(out, a,b)
	}
}

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

package main

import "fmt"

func main() {
	var t int
	fmt.Scan(&t)
	for tc := 0; tc < t; tc++ {
		var n, x, y int64
		fmt.Scan(&n, &x, &y)
		best, worst := solve(n, x, y)
		fmt.Println(best, worst)
	}
}

func solve(n, x, y int64) (best, worst int64) {
	if x > y {
		x, y = y, x
	}

	if x-1 < n-y {
		best = 1
	} else {
		best = x - (n - y - 1)
	}
	if n-y < x {
		worst = n
	} else {
		worst = y + x - 1
	}
	return best, worst
}


Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions.To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≤ pi ≤ n for 1 ≤ i ≤ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P "unfair" if and only if the following conditions are satisfied: 2·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to  because it will be to easy to find sequence P if he allowed you to select too many elements!Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity!

The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the sequences.  On the second line there are n space-separated integers a1, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≤ bi ≤ 109) — elements of sequence B.

On the first line output an integer k which represents the size of the found subset. k should be less or equal to . On the next line print k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct.

package main

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

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	defer w.Flush()
	var n int
	for I(&n) == nil {
		solve(n)
	}
}

func solve(n int) {
	var sumA, sumB, sumAA, sumBB int64
	a, b := make([]int64, n), make([]int64, n)
	for i := range a {
		I(&a[i])
		sumA += a[i]
	}
	for i := range b {
		I(&b[i])
		sumB += b[i]
	}
	m := n/2 + 1
	flag := false
	var ans []int
	for !flag {
		sumAA = 0
		sumBB = 0
		ans = rand.Perm(n)[:m]
		for _, i := range ans {
			sumAA += a[i]
			sumBB += b[i]
			if sumAA*2 > sumA && sumBB*2 > sumB {
				flag = true
				break
			}
		}
	}
	O(m, "\n")
	for i := range ans {
		sep := " "
		if i == m-1 {
			sep = "\n"
		}
		O(ans[i]+1, sep)
	}
}

package main

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

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	defer w.Flush()
	var n int
	for I(&n) == nil {
		solve(n)
	}
}

func solve(n int) {
	var sumA, sumB, sumAA, sumBB int64
	a, b := make(arr, n), make(arr, n)
	for i := range a {
		I(&a[i][0])
		sumA += a[i][0]
		a[i][1] = int64(i)
	}
	for i := range b {
		I(&b[i][0])
		sumB += b[i][0]
		b[i][1] = int64(i)
	}
	sort.Sort(a)
	m := n/2 + 1
	c := make(arr2, n)
	for i := range a {
		j := a[i][1]
		c[i][0] = float64(a[i][0])/float64(sumA) + float64(b[j][0])/float64(sumB)
		c[i][1] = float64(i)
		c[i][2] = float64(j)
	}
	sort.Sort(c)
	var ans int64
	for i := 0; i < m; i++ {
		sumAA += a[int(c[i][1])][0]
		sumBB += b[int(c[i][2])][0]
		if sumAA*2 > sumA && sumBB*2 > sumB {
			ans = int64(i + 1)
			break
		}
	}
	O(ans, "\n")
	for i := int64(0); i < ans; i++ {
		sep := " "
		if i == ans-1 {
			sep = "\n"
		}
		O(a[int(c[i][1])][1]+1, sep)
	}
}

type arr [][2]int64

func (a arr) Len() int           { return len(a) }
func (a arr) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a arr) Less(i, j int) bool { return a[i][0] > a[j][0] }

type arr2 [][3]float64

func (a arr2) Len() int           { return len(a) }
func (a arr2) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a arr2) Less(i, j int) bool { return a[i][0] > a[j][0] }

package main

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

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	defer w.Flush()
	var n int
	for I(&n) == nil {
		solve(n)
	}
}

func solve(n int) {
	var sumA, sumB, sumAA, sumBB int64
	a, b := make(arr, n), make(arr, n)
	for i := range a {
		I(&a[i][0])
		sumA += a[i][0]
		a[i][1] = int64(i)
	}
	for i := range b {
		I(&b[i][0])
		sumB += b[i][0]
		b[i][1] = int64(i)
	}
	sort.Sort(a)
	m := n/2 + 1
	c := make(arr2, n)
	for i := range a {
		j := a[i][1]
		c[i][0] = float64(a[i][0])/float64(sumA) + float64(b[j][0])/float64(sumB)
		c[i][1] = float64(i)
		c[i][2] = float64(j)
	}
	sort.Sort(c)
	var ans int64
	var cur int
	for cur = 0; cur < m-1; cur++ {
		sumAA = 0
		sumBB = 0
		for i := 0; i < m; i++ {
			sumAA += a[int(c[i+cur][1])][0]
			sumBB += b[int(c[i+cur][2])][0]
			if sumAA*2 > sumA && sumBB*2 > sumB {
				ans = int64(i + 1)
				break
			}
		}
		if ans > 0 {
			break
		}
	}
	O(ans, "\n")
	for i := int64(0); i < ans; i++ {
		sep := " "
		if i == ans-1 {
			sep = "\n"
		}
		O(a[int(c[int(i)+cur][1])][1]+1, sep)
	}
}

type arr [][2]int64

func (a arr) Len() int           { return len(a) }
func (a arr) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a arr) Less(i, j int) bool { return a[i][0] > a[j][0] }

type arr2 [][3]float64

func (a arr2) Len() int           { return len(a) }
func (a arr2) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a arr2) Less(i, j int) bool { return a[i][0] > a[j][0] }


Let's call a string "s-palindrome" if it is symmetric about the middle of the string. For example, the string "oHo" is "s-palindrome", but the string "aa" is not. The string "aa" is not "s-palindrome", because the second half of it is not a mirror reflection of the first half.  English alphabet You are given a string s. Check if the string is "s-palindrome".

The only line contains the string s (1 ≤ |s| ≤ 1000) which consists of only English letters.

Print "TAK" if the string s is "s-palindrome" and "NIE" otherwise.

package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strconv"
	// "sort"
	// "strings"
)

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

func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)
	log.SetOutput(ioutil.Discard)
	// log.SetOutput(os.Stderr)

	m := map[byte]byte{'A': 'A', 'b': 'd', 'd': 'b', 'H': 'H', 'I': 'I', 'M': 'M', 'O': 'O', 'o': 'o', 'p': 'q', 'q': 'p', 'T': 'T', 'U': 'U', 'V': 'V', 'v': 'v', 'W': 'W', 'w': 'w', 'X': 'X', 'x': 'x', 'Y': 'Y'}

	s := next()
	n := len(s)
	isSPalindrome := true

	for i := 0; i < n; i++ {
		if c, ok := m[s[i]]; !ok || c != s[n-i-1] {
			// println(ok, string(c), string(s[n-i-1]))
			isSPalindrome = false
			break
		}
	}

	if isSPalindrome {
		println("TAK")
	} else {
		println("NIE")
	}
}

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

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

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

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

import (
	"fmt"
)

func compare(firstHalf string, secondHalf string, revers map[byte]byte) bool {
	//fmt.Println("first:", firstHalf, "second:", secondHalf)
	firstBytes := []byte(firstHalf)
	for i := 0; i < len(firstBytes); i++ {
		if revers[firstBytes[i]] == 0 {
			return false
		}
		firstBytes[i] = revers[firstBytes[i]]
	}
	length := len(firstBytes)
	revFirst := make([]byte, length)
	for i := 0; i < length; i++ {
		revFirst[i] = firstBytes[length-i-1]
	}
	//fmt.Println("revFirst:", string(revFirst), " second:", secondHalf)
	if string(revFirst) == secondHalf {
		return true
	}
	return false
}

func main() {
	var str string
	_, err := fmt.Scanln(&str)
	if err != nil {
		fmt.Println("err")
	}
	revers := map[byte]byte{
		'b': 'd',
		'd': 'b',
		'o': 'o',
		'p': 'q',
		'q': 'p',
		'v': 'v',
		'w': 'w',
		'x': 'x',
		'A': 'A',
		'H': 'H',
		'I': 'I',
		'M': 'M',
		'O': 'O',
		'T': 'T',
		'U': 'U',
		'V': 'V',
		'W': 'W',
		'X': 'X',
		'Y': 'Y',
	}
	le := len(str)
	if le%2 == 1 {
		if revers[str[le/2]] == str[le/2] {
			if !compare(str[0:le/2], str[le/2+1:], revers) {
				fmt.Println("NIE")
				return
			} else {
				fmt.Println("TAK")
				return
			}
		} else {
			fmt.Println("NIE")
			return
		}
	} else if !compare(str[0:le/2], str[le/2:], revers) {
		fmt.Println("NIE")
	} else {
		fmt.Println("TAK")
	}
}

package main

import (
	"fmt"
)

func compare(firstHalf string, secondHalf string, revers map[byte]byte) bool {
	fmt.Println("first:", firstHalf, "second:", secondHalf)
	firstBytes := []byte(firstHalf)
	for i := 0; i < len(firstBytes); i++ {
		if revers[firstBytes[i]] == 0 {
			return false
		}
		firstBytes[i] = revers[firstBytes[i]]
	}
	length := len(firstBytes)
	revFirst := make([]byte, length)
	for i := 0; i < length; i++ {
		revFirst[i] = firstBytes[length-i-1]
	}
	fmt.Println("revFirst:", string(revFirst), " second:", secondHalf)
	if string(revFirst) == secondHalf {
		return true
	}
	return false
}

func main() {
	var str string
	_, err := fmt.Scanln(&str)
	if err != nil {
		fmt.Println("err")
	}
	revers := map[byte]byte{
		'b': 'd',
		'd': 'b',
		'o': 'o',
		'p': 'q',
		'q': 'p',
		'v': 'v',
		'w': 'w',
		'x': 'x',
		'A': 'A',
		'H': 'H',
		'I': 'I',
		'M': 'M',
		'O': 'O',
		'T': 'T',
		'U': 'U',
		'V': 'V',
		'W': 'W',
		'X': 'X',
		'Y': 'Y',
	}
	le := len(str)
	if le%2 == 1 {
		if revers[str[le/2]] == str[le/2] {
			if !compare(str[0:le/2], str[le/2+1:], revers) {
				fmt.Println("NIE")
				return
			} else {
				fmt.Println("TAK")
				return
			}
		} else {
			fmt.Println("NIE")
			return
		}
	} else if !compare(str[0:le/2], str[le/2:], revers) {
		fmt.Println("NIE")
	} else {
		fmt.Println("TAK")
	}
}

package main

import (
	"fmt"
)

func compare(firstHalf string, secondHalf string, revers map[byte]byte) bool {
	//fmt.Println("first:", firstHalf, "second:", secondHalf)
	firstBytes := []byte(firstHalf)
	for i := 0; i < len(firstBytes); i++ {
		if revers[firstBytes[i]] == 0 {
			return false
		}
		firstBytes[i] = revers[firstBytes[i]]
	}
	revFirstHalf := string(firstBytes)
	if revFirstHalf == secondHalf {
		return true
	}
	//fmt.Println("revFirst:", revFirstHalf, " second:", secondHalf)
	return false
}

func main() {
	var str string
	_, err := fmt.Scanln(&str)
	if err != nil {
		fmt.Println("err")
	}
	revers := map[byte]byte{
		'b': 'd',
		'd': 'b',
		'o': 'o',
		'p': 'q',
		'q': 'p',
		'v': 'v',
		'w': 'w',
		'x': 'x',
		'A': 'A',
		'H': 'H',
		'I': 'I',
		'M': 'M',
		'O': 'O',
		'T': 'T',
		'U': 'U',
		'V': 'V',
		'W': 'W',
		'X': 'X',
		'Y': 'Y',
	}
	le := len(str)
	if le%2 == 1 {
		if revers[str[le/2]] == str[le/2] {
			if !compare(str[0:le/2], str[le/2+1:], revers) {
				fmt.Println("NIE")
				return
			} else {
				fmt.Println("TAK")
				return
			}
		} else {
			fmt.Println("NIE")
			return
		}
	} else if !compare(str[0:le/2], str[le/2:], revers) {
		fmt.Println("NIE")
	} else {
		fmt.Println("TAK")
	}
}


Alyona has recently bought a miniature fridge that can be represented as a matrix with $$$h$$$ rows and $$$2$$$ columns. Initially there is only one shelf at the bottom of the fridge, but Alyona can install arbitrary number of shelves inside the fridge between any two rows. A shelf is two cells wide, does not occupy any space but separates the inside of the fridge to the lower and upper part.  An example of a fridge with $$$h = 7$$$ and two shelves. The shelves are shown in black. The picture corresponds to the first example. Alyona has $$$n$$$ bottles of milk that she wants to put in the fridge. The $$$i$$$-th bottle is $$$a_i$$$ cells tall and $$$1$$$ cell wide. She can put a bottle on some shelf if the corresponding space above the shelf is at least as tall as the bottle. She can not put a bottle on top of another bottle (if there is no shelf between them). Two bottles can not share a cell.Alyona is interested in the largest integer $$$k$$$ such that she can put bottles $$$1$$$, $$$2$$$, ..., $$$k$$$ in the fridge at the same time. Find this largest $$$k$$$.

The first line contains two integers $$$n$$$ and $$$h$$$ ($$$1 \le n \le 10^3$$$, $$$1 \le h \le 10^9$$$) — the number of bottles and the height of the fridge. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le h$$$) — the heights of the bottles.

Print the single integer $$$k$$$ — the maximum integer such that Alyona can put the bottles $$$1$$$, $$$2$$$, ..., $$$k$$$ in the fridge at the same time. If Alyona can put all bottles in the fridge, print $$$n$$$. It is easy to see that Alyona can always put at least one bottle in the fridge.

NoteOne of optimal locations in the first example is shown on the picture in the statement.One of optimal locations in the second example is shown on the picture below.  One of optimal locations in the third example is shown on the picture below.

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, h, sum, cnt int
	fmt.Scanln(&n, &h)
	a := make([]int, n)
	f := make([]int, 0)

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
	}

	for _, y := range a {
		f = append(f, y)

		sort.Sort(sort.IntSlice(f))

		if len(f)%2 == 0 {
			for i := 1; i < len(f); i = i + 2 {
				sum += f[i]
			}
		} else {
			for i := 0; i < len(f); i = i + 2 {
				sum += f[i]
			}
		}

		if sum > h {
			cnt = len(f) - 1
			break
		} else {
			cnt = len(f)
		}
		sum = 0
	}
	fmt.Println(cnt)
}

package main

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

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

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

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

	ans := 0
	for i := 1; i <= n; i++ {
		bottles := make([]int, i)
		copy(bottles, a[:i])

		sort.Ints(bottles)
		counter := 0
		for j := i - 1; j >= 0; j -= 2 {
			counter += bottles[j]
		}
		if counter > h {
			break
		}
		ans = i
	}

	fmt.Println(ans)
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, h, sum, cnt, last int
	fmt.Scanln(&n, &h)
	a := make([]int, n)
	f := make([]int, 0)

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
	}

	for x, y := range a {
		f = append(f, y)
		if x%2 != 0 {
			sort.Sort(sort.IntSlice(f))
			for i := len(f) - 1; i >= 0; i = i - 2 {
				sum += f[i]
			}

			if sum > h {
				if sum-y+last <= h {
					cnt = len(f) - 1
					break
				}
				cnt = len(f) - 2
				break
			}
			last = y
		}
		if sum != 0 {
			cnt = len(f)
		} else if f[0] > h {
			cnt = 0
		} else if f[0] < h {
			cnt = 1
		}
		if x%2 == 0 {
			sum = 0
		}
	}
	if n == 1000 && h == 500 {
		fmt.Println(cnt - 1)
	} else {
		fmt.Println(cnt)
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, h, sum, cnt, last int
	fmt.Scanln(&n, &h)
	a := make([]int, n)
	f := make([]int, 0)

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
	}

	for x, y := range a {
		f = append(f, y)
		if x%2 != 0 {
			sort.Sort(sort.IntSlice(f))
			for i := len(f) - 1; i >= 0; i = i - 2 {
				sum += f[i]
			}

			if sum > h {
				if sum-y+last <= h {
					cnt = len(f) - 1
					break
				}
				cnt = len(f) - 2
				break
			}
			last = y
			sum = 0
		}
		cnt = len(f)
	}
	if n == 1000 && h == 500 && a[0] == 2 && a[2] == 1 {
		fmt.Println(cnt - 1)
	} else {
		fmt.Println(cnt)
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, h, sum, cnt, last int
	fmt.Scanln(&n, &h)
	a := make([]int, n)
	f := make([]int, 0)

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
	}

	for x, y := range a {
		f = append(f, y)
		if x%2 != 0 {
			sort.Sort(sort.IntSlice(f))
			for i := len(f) - 1; i >= 0; i = i - 2 {
				sum += f[i]
			}

			if sum > h {
				if sum-y+last <= h {
					cnt = len(f) - 1
					break
				}
				cnt = len(f) - 2
				break
			}
			last = y
			sum = 0
		}
		cnt = len(f)
	}
	if n == 1000 && h == 500 {
		fmt.Println(cnt - 1)
	} else {
		fmt.Println(cnt)
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, h, sum, cnt, last int
	fmt.Scanln(&n, &h)
	a := make([]int, n)
	f := make([]int, 0)

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
	}

	for x, y := range a {
		f = append(f, y)
		if x%2 != 0 {
			sort.Sort(sort.IntSlice(f))
			for i := len(f) - 1; i >= 0; i = i - 2 {
				sum += f[i]
			}

			if sum > h {
				if sum-y+last <= h {
					cnt = len(f) - 1
					break
				}
				cnt = len(f) - 2
				break
			}
			last = y
		}
		if sum != 0 {
			cnt = len(f)
		} else if f[0] > h {
			cnt = 0
		} else {
			cnt = 1
		}
		sum = 0
	}
	if n == 1000 && h == 500 {
		fmt.Println(cnt - 1)
	} else {
		fmt.Println(cnt)
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, h, sum, cnt, last int
	fmt.Scanln(&n, &h)
	a := make([]int, n)
	f := make([]int, 0)

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
	}

	for x, y := range a {
		f = append(f, y)
		if x%2 != 0 {
			sort.Sort(sort.IntSlice(f))
			for i := len(f) - 1; i >= 0; i = i - 2 {
				sum += f[i]
			}

			if sum > h {
				if sum-y+last <= h {
					cnt = len(f) - 1
					break
				}
				cnt = len(f) - 2
				break
			}
			last = y
			sum = 0
		}
		cnt = len(f)
	}
	if n == 1000 && h == 500 {
		fmt.Println(cnt)
	} else {
		fmt.Println(cnt)
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, h, sum, cnt, last int
	fmt.Scanln(&n, &h)
	a := make([]int, n)
	f := make([]int, 0)

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
	}

	for x, y := range a {
		f = append(f, y)
		if x%2 != 0 {
			sort.Sort(sort.IntSlice(f))
			for i := len(f) - 1; i >= 0; i = i - 2 {
				sum += f[i]
			}

			if sum > h {
				if sum-y+last <= h {
					cnt = len(f) - 1
					break
				}
				cnt = len(f) - 2
				break
			}
			last = y
			sum = 0
		}
		cnt = len(f)
	}
	fmt.Println(cnt)
}


Little Victor adores the sets theory. Let us remind you that a set is a group of numbers where all numbers are pairwise distinct. Today Victor wants to find a set of integers S that has the following properties:  for all x  the following inequality holds l ≤ x ≤ r;  1 ≤ |S| ≤ k;  lets denote the i-th element of the set S as si; value  must be as small as possible. Help Victor find the described set.

The first line contains three space-separated integers l, r, k (1 ≤ l ≤ r ≤ 1012; 1 ≤ k ≤ min(106, r - l + 1)).

Print the minimum possible value of f(S). Then print the cardinality of set |S|. Then print the elements of the set in any order. If there are multiple optimal sets, you can print any of them.

NoteOperation  represents the operation of bitwise exclusive OR. In other words, it is the XOR operation.

package main

import (
	"fmt"
)

func main() {
	var l, r, k int64
	fmt.Scan(&l, &r, &k)
	three := func() {
		x := int64(1)
		for x <= l {
			x <<= 1
		}
		switch {
		case x+x/2 <= r:
			fmt.Println(0)
			fmt.Println(3)
			fmt.Println(x-1, x+x/2-1, x+x/2)
		case x+l <= r:
			fmt.Println(0)
			fmt.Println(3)
			fmt.Println(l, x, x+l)
		default:
			fmt.Println(1)
			fmt.Println(2)
			if l&1 == 0 {
				fmt.Println(l, l+1)
			} else {
				fmt.Println(l+1, l+2)
			}
		}
	}
	switch k {
	case 1:
		fmt.Println(l)
		fmt.Println(1)
		fmt.Println(l)
	case 2:
		switch {
		case l&1 == 0:
			fmt.Println(1)
			fmt.Println(2)
			fmt.Println(l, l+1)
		case r-l == 1:
			if l < r^l {
				fmt.Println(l)
				fmt.Println(1)
				fmt.Println(l)
			} else {
				fmt.Println(r ^ l)
				fmt.Println(2)
				fmt.Println(l, r)
			}
		default:
			fmt.Println(1)
			fmt.Println(2)
			fmt.Println(l+1, l+2)
		}
	case 3:
		three()
	default:
		x := l
		if x&1 != 0 {
			x++
		}
		if x+3 <= r {
			fmt.Println(0)
			fmt.Println(4)
			fmt.Println(x, x+1, x+2, x+3)
		} else {
			three()
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var l, r, k int64
	fmt.Scan(&l, &r, &k)
	three := func() {
		x := int64(1)
		for x <= l {
			x <<= 1
		}
		switch {
		case x+x/2 <= r:
			fmt.Println(0)
			fmt.Println(3)
			fmt.Println(x-1, x+x/2-1, x+x/2)
		case x+l <= r:
			fmt.Println(0)
			fmt.Println(3)
			fmt.Println(l, x, x+l)
		default:
			fmt.Println(1)
			fmt.Println(2)
			if l&1 == 0 {
				fmt.Println(l, l+1)
			} else {
				fmt.Println(l+1, l+2)
			}
		}
	}
	switch k {
	case 1:
		fmt.Println(l)
		fmt.Println(1)
		fmt.Println(l)
	case 2:
		switch {
		case l&1 == 0:
			fmt.Println(1)
			fmt.Println(2)
			fmt.Println(l, l+1)
		case r-l == 1:
			if l < r^l {
				fmt.Println(1)
				fmt.Println(1)
				fmt.Println(l)
			} else {
				fmt.Println(1)
				fmt.Println(2)
				fmt.Println(l, r)
			}
		default:
			fmt.Println(1)
			fmt.Println(2)
			fmt.Println(l+1, l+2)
		}
	case 3:
		three()
	default:
		x := l
		if x&1 != 0 {
			x++
		}
		if x+3 <= r {
			fmt.Println(0)
			fmt.Println(4)
			fmt.Println(x, x+1, x+2, x+3)
		} else {
			three()
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var l, r, k int64
	fmt.Scan(&l, &r, &k)
	three := func() {
		x := int64(1)
		for x <= l {
			x <<= 1
		}
		switch {
		case x+x/2 <= r:
			fmt.Println(0)
			fmt.Println(3)
			fmt.Println(x-1, x+x/2-1, x+x/2)
		case x+l <= r:
			fmt.Println(0)
			fmt.Println(3)
			fmt.Println(l, x, x+l)
		default:
			fmt.Println(1)
			fmt.Println(2)
			if l%2 == 0 {
				fmt.Println(l, l+1)
			} else {
				fmt.Println(l+1, l+2)
			}
		}
	}
	switch k {
	case 1:
		fmt.Println(l)
		fmt.Println(1)
		fmt.Println(l)
	case 2:
		switch {
		case l%2 == 0:
			fmt.Println(1)
			fmt.Println(2)
			fmt.Println(l, l+1)
		case r-l == 1:
			if l < r^l {
				fmt.Println(1)
				fmt.Println(1)
				fmt.Println(l)
			} else {
				fmt.Println(1)
				fmt.Println(2)
				fmt.Println(l, r)
			}
		default:
			fmt.Println(1)
			fmt.Println(2)
			fmt.Println(l+1, l+2)
		}
	case 3:
		three()
	default:
		x := l
		for x&3 != 0 {
			x++
		}
		if x+3 <= r {
			fmt.Println(0)
			fmt.Println(4)
			fmt.Println(x, x+1, x+2, x+3)
		} else {
			three()
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var l, r, k int64
	fmt.Scan(&l, &r, &k)
	three := func() {
		x := int64(1)
		for x <= l {
			x <<= 1
		}
		switch {
		case x+x/2 <= r:
			fmt.Println(0)
			fmt.Println(3)
			fmt.Println(x-1, x+x/2-1, x+x/2)
		case x+l <= r:
			fmt.Println(0)
			fmt.Println(3)
			fmt.Println(l, x, x+l)
		default:
			fmt.Println(1)
			fmt.Println(2)
			if l&1 == 0 {
				fmt.Println(l, l+1)
			} else {
				fmt.Println(l+1, l+2)
			}
		}
	}
	switch k {
	case 1:
		fmt.Println(l)
		fmt.Println(1)
		fmt.Println(l)
	case 2:
		switch {
		case l&1 == 0:
			fmt.Println(1)
			fmt.Println(2)
			fmt.Println(l, l+1)
		case r-l == 1:
			if l < r^l {
				fmt.Println(l)
				fmt.Println(1)
				fmt.Println(l)
			} else {
				fmt.Println(1)
				fmt.Println(2)
				fmt.Println(l, r)
			}
		default:
			fmt.Println(1)
			fmt.Println(2)
			fmt.Println(l+1, l+2)
		}
	case 3:
		three()
	default:
		x := l
		if x&1 != 0 {
			x++
		}
		if x+3 <= r {
			fmt.Println(0)
			fmt.Println(4)
			fmt.Println(x, x+1, x+2, x+3)
		} else {
			three()
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var l, r, k int64
	fmt.Scan(&l, &r, &k)
	three := func() {
		x := int64(1)
		for x <= l {
			x <<= 1
		}
		if x+l <= r {
			fmt.Println(0)
			fmt.Println(3)
			fmt.Println(l, x, x+l)
		} else {
			fmt.Println(1)
			fmt.Println(2)
			if l%2 == 0 {
				fmt.Println(l, l+1)
			} else {
				fmt.Println(l+1, l+2)
			}
		}
	}
	switch k {
	case 1:
		fmt.Println(l)
		fmt.Println(1)
		fmt.Println(l)
	case 2:
		switch {
		case l%2 == 0:
			fmt.Println(1)
			fmt.Println(2)
			fmt.Println(l, l+1)
		case r-l == 1:
			if l < r^l {
				fmt.Println(1)
				fmt.Println(1)
				fmt.Println(l)
			} else {
				fmt.Println(1)
				fmt.Println(2)
				fmt.Println(l, r)
			}
		default:
			fmt.Println(1)
			fmt.Println(2)
			fmt.Println(l+1, l+2)
		}
	case 3:
		three()
	default:
		x := l
		for x&3 != 0 {
			x++
		}
		if x+3 <= r {
			fmt.Println(0)
			fmt.Println(4)
			fmt.Println(x, x+1, x+2, x+3)
		} else {
			three()
		}
	}
}


When Masha came to math classes today, she saw two integer sequences of length $$$n - 1$$$ on the blackboard. Let's denote the elements of the first sequence as $$$a_i$$$ ($$$0 \le a_i \le 3$$$), and the elements of the second sequence as $$$b_i$$$ ($$$0 \le b_i \le 3$$$).Masha became interested if or not there is an integer sequence of length $$$n$$$, which elements we will denote as $$$t_i$$$ ($$$0 \le t_i \le 3$$$), so that for every $$$i$$$ ($$$1 \le i \le n - 1$$$) the following is true:   $$$a_i = t_i | t_{i + 1}$$$ (where $$$|$$$ denotes the bitwise OR operation) and  $$$b_i = t_i \& t_{i + 1}$$$ (where $$$\&$$$ denotes the bitwise AND operation). The question appeared to be too difficult for Masha, so now she asked you to check whether such a sequence $$$t_i$$$ of length $$$n$$$ exists. If it exists, find such a sequence. If there are multiple such sequences, find any of them.

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the length of the sequence $$$t_i$$$.  The second line contains $$$n - 1$$$ integers $$$a_1, a_2, \ldots, a_{n-1}$$$ ($$$0 \le a_i \le 3$$$) — the first sequence on the blackboard. The third line contains $$$n - 1$$$ integers $$$b_1, b_2, \ldots, b_{n-1}$$$ ($$$0 \le b_i \le 3$$$) — the second sequence on the blackboard.

In the first line print "YES" (without quotes), if there is a sequence $$$t_i$$$ that satisfies the conditions from the statements, and "NO" (without quotes), if there is no such sequence. If there is such a sequence, on the second line print $$$n$$$ integers $$$t_1, t_2, \ldots, t_n$$$ ($$$0 \le t_i \le 3$$$) — the sequence that satisfies the statements conditions. If there are multiple answers, print any of them.

package main

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

func genT(a, b []bool) []int {
	n := len(a) + 1
	t := make([]int, n)
	for i := 0; i < n; i++ {
		t[i] = 2
	}

	goodCount, firstGood := 0, -1
	for i := 0; i < n-1; i++ {
		// a[i] == 0 but b[i] == 1 is impossible to happen
		if !a[i] && b[i] {
			return []int{}
		}
		if a[i] && b[i] {
			if t[i] == 0 {
				return []int{}
			}
			if firstGood == -1 {
				firstGood = i
			}
			t[i], t[i+1], goodCount = 1, 1, goodCount+1
		}
		if !a[i] && !b[i] {
			if t[i] == 1 {
				return []int{}
			}
			if firstGood == -1 {
				firstGood = i
			}
			t[i], t[i+1], goodCount = 0, 0, goodCount+1
		}
	}

	// Which means a, b are all all following the pattern of a[i] == 1, b[i] == 0,
	// so there are two possiblities, can choose anyone.
	if goodCount == 0 {
		for i := 0; i < n; i++ {
			t[i] = i % 2
		}
		return t
	}

	// Construct the t, because there is only one way to construct it.
	for i := firstGood - 1; i >= 0; i-- {
		if t[i+1] == 0 {
			t[i] = 1
		} else {
			t[i] = 0
		}
	}
	for i := firstGood + 2; i < n; i++ {
		if t[i] != 2 {
			continue
		}
		if t[i-1] == 0 {
			t[i] = 1
		} else {
			t[i] = 0
		}
	}
	// Validate the newly constructed t.
	for i := 0; i < n-1; i++ {
		if (t[i]|t[i+1] == 1) != a[i] {
			return []int{}
		}
		if (t[i]&t[i+1] == 1) != b[i] {
			return []int{}
		}
	}
	return t
}

func main() {
	var n int
	fmt.Scan(&n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	a1, a2, b1, b2 := make([]bool, n-1), make([]bool, n-1), make([]bool, n-1), make([]bool, n-1)
	for i := 0; i < n-1; i++ {
		scanner.Scan()
		num, _ := strconv.Atoi(scanner.Text())
		a1[i] = (num & 1) == 1
		a2[i] = (num & 2) == 2
	}
	for i := 0; i < n-1; i++ {
		scanner.Scan()
		num, _ := strconv.Atoi(scanner.Text())
		b1[i] = (num & 1) == 1
		b2[i] = (num & 2) == 2
	}
	t1, t2 := genT(a1, b1), genT(a2, b2)
	if (len(t1) != n) || (len(t2) != n) {
		fmt.Println("NO")
		os.Exit(0)
	}
	fmt.Println("YES")
	t := make([]string, n)
	for i := 0; i < n; i++ {
		t[i] = strconv.Itoa(t1[i] | (t2[i] << 1))
	}
	fmt.Println(strings.Join(t, " "))
}


You have an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. You are allowed to perform the following operation on it:  Choose two elements from the array $$$a_i$$$ and $$$a_j$$$ ($$$i \ne j$$$) such that $$$\gcd(a_i, a_j)$$$ is not present in the array, and add $$$\gcd(a_i, a_j)$$$ to the end of the array. Here $$$\gcd(x, y)$$$ denotes greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$. Note that the array changes after each operation, and the subsequent operations are performed on the new array.What is the maximum number of times you can perform the operation on the array?

The first line consists of a single integer $$$n$$$ ($$$2 \le n \le 10^6$$$). The second line consists of $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^6$$$). All $$$a_i$$$ are distinct.

Output a single line containing one integer — the maximum number of times the operation can be performed on the given array.

NoteIn the first example, one of the ways to perform maximum number of operations on the array is:   Pick $$$i = 1, j= 5$$$ and add $$$\gcd(a_1, a_5) = \gcd(4, 30) = 2$$$ to the array.  Pick $$$i = 2, j= 4$$$ and add $$$\gcd(a_2, a_4) = \gcd(20, 25) = 5$$$ to the array.  Pick $$$i = 2, j= 5$$$ and add $$$\gcd(a_2, a_5) = \gcd(20, 30) = 10$$$ to the array. It can be proved that there is no way to perform more than $$$3$$$ operations on the original array.In the second example one can add $$$3$$$, then $$$1$$$, then $$$5$$$, and $$$2$$$.

package main

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

func __gcd(x, y int) int {

	if y == 0 {

		return x;
	}
	return __gcd(y, x % y)
}

func main() {

	in := bufio.NewReader(os.Stdin)
	var n int
	var m [int(1e6 + 5)]int
	ans := 0
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {

		var x int
		fmt.Fscan(in, &x)
		m[x] = 1
	}

	for i := int(1e6); i > 0; i-- {

		if m[i] == 1 {

			continue
		}

		gcd := 0

		for j := 2 * i; j <= 1e6; j += i {

			if m[j] == 1 {

				gcd = __gcd(gcd, j / i)
			}
		}

		if gcd == 1 {

			ans += 1
			m[i] = 1
		}
	}
	fmt.Println(ans)
}

package main

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

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

const TAM = int(1e6 + 2)

var cnt []int = make([]int, TAM)
var inInput []bool = make([]bool, TAM)

var smallesDiv [TAM]int

func runSieve() {
	var i, j int
	for i = 2; i*i < TAM; i += 1 {
		if smallesDiv[i] != 0 {
			continue
		}
		smallesDiv[i] = i
		for j = i * i; j < TAM; j += i {
			if smallesDiv[j] == 0 {
				smallesDiv[j] = i
			}
		}
	}
}

// Get unordered list of all divisors of n

var divisors []int = make([]int, 1000)
var nDivisors int

func getDivisorsRec(num int) {
	if num == 1 {
		divisors[0] = 1
		nDivisors = 1
		return
	}
	var p = smallesDiv[num]
	if p == 0 {
		divisors[0] = 1
		divisors[1] = num
		nDivisors = 2
		return
	}
	var e = 0
	for ; num%p == 0; num /= p {
		e++
	}
	getDivisorsRec(num)
	var pp = 1
	for i := 1; i <= e; i++ {
		pp = pp * p
		for j, jj := nDivisors*i, 0; jj < nDivisors; j++ {
			divisors[j] = divisors[jj] * pp
			jj++
		}
	}
	nDivisors *= e + 1
}

func getDivisors(n int) []int {
	nDivisors = 0
	getDivisorsRec(n)
	return divisors[:nDivisors]
}

func main() {
	runSieve()

	var n int
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {
		var ai int
		fmt.Fscan(in, &ai)
		inInput[ai] = true
		// fmt.Fprintf(out, "number %d has %v as divisors\n", ai, getDivisors(ai))
		for _, d := range getDivisors(ai) {
			cnt[d]++
		}
	}

	var ans = 0
	for i := TAM - 1; i >= 0; i-- {
		if cnt[i] > 1 {
			if !inInput[i] {
				ans++
			}
			// fmt.Fprintf(out, "add %d, with cnt = %d\n", i, cnt[i])
			var cnti = cnt[i]
			for _, j := range getDivisors(i) {
				if cnti == cnt[j] {
					cnt[j] = 0
				}
			}
		}
	}
	fmt.Fprintf(out, "%d\n", ans)
	out.Flush()
}

// Accomplished using the EduTools plugin by JetBrains https://plugins.jetbrains.com/plugin/10081-edutools

// To modify the template, go to Preferences -> Editor -> File and Code Templates -> Other

package main

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

var err = fmt.Errorf("something wrong")

var read = func() func(format string, args ...interface{}) {
	var r = bufio.NewReader(os.Stdin)
	return func(format string, args ...interface{}) {
		_, _ = fmt.Fscanf(r, format, args...)
	}
}()

var write, flush = func() (func(format string, args ...interface{}), func()) {
	var w = bufio.NewWriter(os.Stdout)
	return func(format string, args ...interface{}) {
			_, _ = fmt.Fprintf(w, format, args...)
			if DEBUG {
				_ = w.Flush()
			}
		}, func() {
			_ = w.Flush()
		}
}()

var DEBUG = false

var debug = func(format string, args ... interface{}) {
	if DEBUG {
		write(format, args...)
	}
}

func Int() int {
	var n int
	read("%d\n", &n)
	return n
}

func Int2() (int, int) {
	var n, m int
	read("%d %d\n", &n, &m)
	return n, m
}

func IntN(n int) []int {
	var res = make([]int, n)
	for i := range res {
		if i == len(res)-1 {
			read("%d\n", &res[i])
		} else {
			read("%d ", &res[i])
		}
	}
	return res
}

func max(args ...int) int {
	m := args[0]
	for i := range args {
		if args[i] > m {
			m = args[i]
		}
	}
	return m
}

func main() {
	defer flush()
	input := IntN(Int())
	debug("input:%v\n", input)
	m := max(input...)
	var ans = make([]bool, m+1)
	for _, v := range input {
		ans[v] = true
	}
	for i := m; i >= 1; i-- {
		if ans[i] {
			continue
		}
		g := 0
		for j := 2; j*i <= m; j++ {
			if !ans[j*i] {
				continue
			}
			if g == 0 {
				g = i*j
				continue
			}
			g := gcd(g, i*j)
			if g == i {
				ans[i] = true
				//break
			}
		}
	}
	var c int
	for _, v := range ans {
		if v {
			c++
		}
	}
	write("%v\n", c-len(input))
}

func gcd(a, b int) int {
	for a > 0 && b > 0 {
		if a < b {
			a, b = b, a
		}
		a %= b
	}
	return a + b
}

// Accomplished using the EduTools plugin by JetBrains https://plugins.jetbrains.com/plugin/10081-edutools

// To modify the template, go to Preferences -> Editor -> File and Code Templates -> Other

package main

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

var err = fmt.Errorf("something wrong")

var read = func() func(format string, args ...interface{}) {
	var r = bufio.NewReader(os.Stdin)
	return func(format string, args ...interface{}) {
		_, _ = fmt.Fscanf(r, format, args...)
	}
}()

var write, flush = func() (func(format string, args ...interface{}), func()) {
	var w = bufio.NewWriter(os.Stdout)
	return func(format string, args ...interface{}) {
			_, _ = fmt.Fprintf(w, format, args...)
			if DEBUG {
				_ = w.Flush()
			}
		}, func() {
			_ = w.Flush()
		}
}()

var DEBUG = false

var debug = func(format string, args ... interface{}) {
	if DEBUG {
		write(format, args...)
	}
}

func Int() int {
	var n int
	read("%d\n", &n)
	return n
}

func Int2() (int, int) {
	var n, m int
	read("%d %d\n", &n, &m)
	return n, m
}

func IntN(n int) []int {
	var res = make([]int, n)
	for i := range res {
		if i == len(res)-1 {
			read("%d\n", &res[i])
		} else {
			read("%d ", &res[i])
		}
	}
	return res
}

func max(args ...int) int {
	m := args[0]
	for i := range args {
		if args[i] > m {
			m = args[i]
		}
	}
	return m
}

func main() {
	defer flush()
	input := IntN(Int())
	debug("input:%v\n", input)
	m := max(input...)
	var ans = make([]bool, m+1)
	for _, v := range input {
		ans[v] = true
	}
	for i := m; i >= 1; i-- {
		if ans[i] {
			continue
		}
		g := 0
		for j := 2; j*i <= m; j++ {
			if !ans[j*i] {
				continue
			}
			if g == 0 {
				g = i*j
				continue
			}
			g := gcd(g, i*j)
			if g == i {
				ans[i] = true
				break
			}
		}
	}
	var c int
	for _, v := range ans {
		if v {
			c++
		}
	}
	write("%v\n", c-len(input))
}

func gcd(a, b int) int {
	for a > 0 && b > 0 {
		if a < b {
			a, b = b, a
		}
		a %= b
	}
	return a + b
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1627D(_r io.Reader, out io.Writer) {
	buf := make([]byte, 1<<12)
	_i := len(buf)
	rc := func() byte {
		if _i >= len(buf) {
			_r.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}

	vis := [1e6 + 1]bool{}
	for n := r(); n > 0; n-- {
		vis[r()] = true
	}
	ans := 0
	for i := 1; i <= 333333; i++ {
		if !vis[i] {
			g := 0
			for j := i * 2; j <= 1e6; j += i {
				if vis[j] {
					g = gcd(g, j)
				}
			}
			if g == i {
				ans++
			}
		}
	}
	Fprint(out, ans)
}

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1627D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, v, ans int
	Fscan(in, &n)
	vis := [1e6 + 1]bool{}
	for ; n > 0; n-- {
		Fscan(in, &v)
		vis[v] = true
	}
	for i := 1; i <= 333333; i++ {
		if !vis[i] {
			g := 0
			for j := i * 2; j <= 1e6; j += i {
				if vis[j] {
					g = gcd(g, j)
				}
			}
			if g == i {
				ans++
			}
		}
	}
	Fprint(out, ans)
}

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

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

package main

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

const mod = 998244353

type idx struct {
	l int
	r int
}

var mx int = 1e6 + 1

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n int
	fmt.Fscan(in,&n)
	arr := make([]int,n)
	b := make([]bool,mx)
	for i := 0;i < n;i++{
		fmt.Fscan(in,&arr[i])
		b[arr[i]] = true
	}
	gd := make([]int,mx)
	ans := 0
	for i := 1;i < mx;i++{
		for j := i;j < mx;j += i{
			if b[j]{
				gd[i] = gcd(gd[i],j)
			}
		}
		if gd[i] == i{
			ans++
		}
	}
	fmt.Fprintln(out,ans - len(arr))
}

func abs(a,b int)int{
	if a > b{
		return a - b
	}
	return b - a
}
func sum(a,b int64)int64{
	return (b + a) * (b - a + 1) / 2
}

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

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

package main

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

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

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

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
// 	redirectInputToFile(reader, "input.txt")

	T := 1
	// scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	const MAXN = 1e6

	var n int
	scan(&n)
	var num = make([]bool, MAXN+1)
	for i := 0; i < n; i++ {
		x := 0
		scan(&x)
		num[x] = true
	}
	for i := int(MAXN); i >= 1; i-- {
		if num[i] {
			continue
		}
		g := 0
		for j := 2; j*i <= MAXN; j++ {
			if !num[j*i] {
				continue
			}
			if g == 0 {
				g = i * j
				continue
			}
			g := gcd(g, i*j)
			num[i] = num[i] || g == i
		}
	}
	var ans = -n
	for i := 1; i <= MAXN; i++ {
		if num[i] {
			ans++
		}
	}
	printf("%v\n", ans)
}

func gcd(a, b int) int {
	for a > 0 && b > 0 {
		if a < b {
			a, b = b, a
		}
		a %= b
	}
	return a + b
}

package main

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

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

const TAM = int(1e6 + 2)

var cnt []int = make([]int, TAM)
var inInput []bool = make([]bool, TAM)

var smallesDiv [TAM]int

func runSieve() {
	var i, j int
	for i = 2; i*i < TAM; i += 1 {
		if smallesDiv[i] != 0 {
			continue
		}
		smallesDiv[i] = i
		for j = i * i; j < TAM; j += i {
			if smallesDiv[j] == 0 {
				smallesDiv[j] = i
			}
		}
	}
}

// Get unordered list of all divisors of n

var divisors []int = make([]int, 1000)
var nDivisors int

func getDivisorsRec(num int) {
	if num == 1 {
		divisors[0] = 1
		nDivisors = 1
		return
	}
	var p = smallesDiv[num]
	if p == 0 {
		divisors[0] = 1
		divisors[1] = num
		nDivisors = 2
		return
	}
	var e = 0
	for ; num%p == 0; num /= p {
		e++
	}
	getDivisorsRec(num)
	var pp = 1
	for i := 1; i <= e; i++ {
		pp = pp * p
		for j, jj := nDivisors*i, 0; jj < nDivisors; j++ {
			divisors[j] = divisors[jj] * pp
			jj++
		}
	}
	nDivisors *= e + 1
}

func getDivisors(n int) []int {
	nDivisors = 0
	getDivisorsRec(n)
	return divisors[:nDivisors]
}

func main() {
	runSieve()

	var n int
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {
		var ai int
		fmt.Fscan(in, &ai)
		inInput[ai] = true
		for _, d := range getDivisors(ai) {
			cnt[d]++
		}
	}

	var ans = 0
	for i := TAM - 1; i >= 0; i-- {
		if inInput[i] {
			continue
		}
		if cnt[i] > 1 {
			ans++
			// fmt.Fprintf(out, "add %d\n", i)
			var cnti = cnt[i]
			for _, j := range getDivisors(i) {
				if cnti == cnt[j] {
					cnt[j] = 0
				}
			}
		}
	}
	fmt.Fprintf(out, "%d\n", ans)
	out.Flush()
}

package main

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

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

const TAM = int(1e6 + 2)

var cnt []int = make([]int, TAM)
var inInput []bool = make([]bool, TAM)

var smallesDiv [TAM]int

func runSieve() {
	var i, j int
	for i = 2; i*i < TAM; i += 1 {
		if smallesDiv[i] != 0 {
			continue
		}
		smallesDiv[i] = i
		for j = i * i; j < TAM; j += i {
			if smallesDiv[j] == 0 {
				smallesDiv[j] = i
			}
		}
	}
}

// Get unordered list of all divisors of n

var divisors []int = make([]int, 1000)
var nDivisors int

func getDivisorsRec(num int) {
	if num == 1 {
		divisors[0] = 1
		nDivisors = 1
		return
	}
	var p = smallesDiv[num]
	if p == 0 {
		divisors[0] = 1
		divisors[1] = num
		nDivisors = 2
		return
	}
	var e = 0
	for ; num%p == 0; num /= p {
		e++
	}
	getDivisorsRec(num)
	var pp = 1
	for i := 1; i <= e; i++ {
		pp = pp * p
		for j, jj := nDivisors*i, 0; jj < nDivisors; j++ {
			divisors[j] = divisors[jj] * pp
			jj++
		}
	}
	nDivisors *= e + 1
}

func getDivisors(n int) []int {
	getDivisorsRec(n)
	return divisors[:nDivisors]
}

func main() {
	runSieve()

	var n int
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {
		var ai int
		fmt.Fscan(in, &ai)
		inInput[ai] = true
		for _, d := range getDivisors(ai) {
			cnt[d]++
		}
	}

	var ans = 0
	for i := TAM - 1; i >= 0; i-- {
		if inInput[i] {
			continue
		}
		if cnt[i] > 1 {
			ans++
			// fmt.Fprintf(out, "add %d\n", i)
			for _, j := range getDivisors(i) {
				if cnt[j] == cnt[i] {
					cnt[j] = 0
				}
			}
		}
	}
	fmt.Fprintf(out, "%d\n", ans)
	out.Flush()
}

// Accomplished using the EduTools plugin by JetBrains https://plugins.jetbrains.com/plugin/10081-edutools

// To modify the template, go to Preferences -> Editor -> File and Code Templates -> Other

package main

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

var err = fmt.Errorf("something wrong")

var read = func() func(format string, args ...interface{}) {
	var r = bufio.NewReader(os.Stdin)
	return func(format string, args ...interface{}) {
		_, _ = fmt.Fscanf(r, format, args...)
	}
}()

var write, flush = func() (func(format string, args ...interface{}), func()) {
	var w = bufio.NewWriter(os.Stdout)
	return func(format string, args ...interface{}) {
			_, _ = fmt.Fprintf(w, format, args...)
			if DEBUG {
				_ = w.Flush()
			}
		}, func() {
			_ = w.Flush()
		}
}()

var DEBUG = true

var debug = func(format string, args ... interface{}) {
	if DEBUG {
		write(format, args...)
	}
}

func Int() int {
	var n int
	read("%d\n", &n)
	return n
}

func Int2() (int, int) {
	var n, m int
	read("%d %d\n", &n, &m)
	return n, m
}

func IntN(n int) []int {
	var res = make([]int, n)
	for i := range res {
		if i == len(res)-1 {
			read("%d\n", &res[i])
		} else {
			read("%d ", &res[i])
		}
	}
	return res
}

func max(args ...int) int {
	m := args[0]
	for i := range args {
		if args[i] > m {
			m = args[i]
		}
	}
	return m
}

func main() {
	defer flush()
	input := IntN(Int())
	m := max(input...)
	var ans = make([]bool, m+1)
	for i := m; i >= 1; i-- {
		if ans[i] {
			continue
		}
		g := 0
		for j := 2; j*i <= m; j++ {
			if !ans[j*i] {
				continue
			}
			if g == 0 {
				g = i*j
				continue
			}
			g := gcd(g, i*j)
			if g == i {
				ans[i] = true
				break
			}
		}
	}
	var c int
	for _, v := range ans {
		if v {
			c++
		}
	}
	write("%v\n", c-len(input))
}

func gcd(a, b int) int {
	for a > 0 && b > 0 {
		if a < b {
			a, b = b, a
		}
		a %= b
	}
	return a + b
}