You are given a special jigsaw puzzle consisting of $$$n\cdot m$$$ identical pieces. Every piece has three tabs and one blank, as pictured below.  The jigsaw puzzle is considered solved if the following conditions hold:  The pieces are arranged into a grid with $$$n$$$ rows and $$$m$$$ columns.  For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece. Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.

The test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\le t\le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. Each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n,m \le 10^5$$$).

For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).

NoteFor the first test case, this is an example solution:   For the second test case, we can show that no solution exists.For the third test case, this is an example solution:

package main

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

func main() {
	var t, x, y int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &x, &y)
		if x == 1 || y == 1 || (x == 2 && y == 2) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

func main() {
    reader := bufio.NewReader(os.Stdin)
    ts, _ := reader.ReadString('\n')
	t, _ := strconv.Atoi(strings.TrimSpace(ts))
	for i := 0; i < t; i++ {
		inp, _ := reader.ReadString('\n')
		s := strings.Split(inp, " ")
		ns, ms := strings.TrimSpace(s[0]), strings.TrimSpace(s[1])
		n, _ := strconv.Atoi(ns)
		m, _ := strconv.Atoi(ms)
		if n == 1 || m == 1 || (n == 2 && m == 2) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}
func NextInt64() int64 {
	bytes := NextBytes()
	i, err := Atoi64(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func Atoi64(b []byte) (int64, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	var n int64
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int64(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

func reverseInts(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

func unique(a []int) int {
	newLen := 0
	n := len(a)
	for i := 0; i < n; i++ {
		if newLen == 0 || a[i] != a[newLen-1] {
			a[newLen] = a[i]
			newLen++
		}
	}
	return newLen
}

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

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

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)
	T := NextInt()

	for t := 0; t < T; t++ {
		n, m := NextInt(), NextInt()
		if n == 1 || m == 1 {
			fmt.Fprintln(out, "YES")
			continue
		}
		if n > 2 || m > 2 {
			fmt.Fprintln(out, "NO")
			continue
		}
		fmt.Fprintln(out, "YES")
	}

}

// https://codeforces.com/contest/1345/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	var (
		T int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		solver()
	}
}

func solver() {
	var n, m int64
	fmt.Fscan(input, &n, &m)
	if n == 1 || m == 1 {
		fmt.Println("YES")
		return
	}

	if n*m > 4 {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
	}
}

package main

import "fmt"

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

func main() {
   var a,b, t, i int
   fmt.Scan(&t)
   for i=0;i<t;i++{
   	fmt.Scan(&a,&b)
   	if min(a,b) == 1 || max(a,b) == 2{
   		fmt.Println("Yes")
   	}else{
   		fmt.Println("No")
   	}
   }
}

package main

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

func main() {

	scanner := bufio.NewScanner(os.Stdin)

	scanner.Scan()

	t := scanner.Text()

	testCases, err := strconv.Atoi(t)

	if err != nil {
		panic(err)
	}

	for j := 0; j < int(testCases); j ++ {

		scanner.Scan()

		a := scanner.Text()

		parts := strings.Fields(a)

		n, _ := strconv.Atoi(parts[0])

		m, _ := strconv.Atoi(parts[1])

		res := solve(n, m)

		fmt.Println(res)
	}
}

func solve(n, m int) string {

	if n == 1 || m == 1 {
		return "YES"
	}

	if n >= 3 || m >= 3 {
		return "NO"
	}

	return "YES"
}

/*
URL:
https://codeforces.com/contest/1345/problem/0
*/

package main

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

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

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

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

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

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

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

const (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

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

var (
	t    int
	n, m int
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		n, m = ReadInt2()
		solve(n, m)
	}
}

func solve(n, m int) {
	if n == 1 || m == 1 {
		fmt.Println("YES")
		return
	}

	if n == 2 && m == 2 {
		fmt.Println("YES")
		return
	}

	fmt.Println("NO")
}

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

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

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

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

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

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

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

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

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

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

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

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

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

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

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

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

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

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

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

package main

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

func solve() {
	n, m := readInt(), readInt()

	if n == 1 || m == 1 || (n == 2 && m == 2) {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

func main() {
	k := readInt()
	for i := 0; i < k; i++ {
		solve()
	}
}

// sort ------------------------------------------------------------

type xxx struct {
	x int
}

type sortArray []xxx

func (s sortArray) Len() int           { return len(s) }
func (s sortArray) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
func (s sortArray) Less(i, j int) bool { return s[i].x < s[j].x }

// -----------------------------------------------------------------

var (
	readString func() string
	readBytes  func() []byte
)

func init() {
	log.SetFlags(log.Lshortfile)
	readString, readBytes = newReadString(os.Stdin)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Bytes()
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

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

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

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

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

func sum(a []int) int {
	var ret int
	for i := range a {
		ret += a[i]
	}
	return ret
}

func sumFloat64(a []float64) float64 {
	var ret float64
	for i := range a {
		ret += a[i]
	}
	return ret
}

func gcd(m, n int) int {
	for m%n != 0 {
		m, n = n, m%n
	}
	return n
}

func lcm(m, n int) int {
	return m / gcd(m, n) * n
}

package main

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

func main() {
	for t := ScanInt(); t > 0; t-- {
		n, m := ScanInt(), ScanInt()
		if solve(n, m) {
			P("YES")
		} else {
			P("NO")
		}
	}

	close(O)
	oWg.Wait()
}
func solve(n, m int) bool {
	if n <= 1 || m <= 1 || (n == 2 && m == 2) {
		return true
	}

	return false
}

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

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var P func(a ...interface{}) (n int, err error)

func init() {
	P = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	O = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

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

func ScanWord() string { in.Scan(); return in.Text() }
func ScanInt() int     { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt()
	}
	return arr
}
func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main
import (
    "fmt"
)
func main() {
    var k,m,n int;
    fmt.Scanf("%d", &k);
    for i := 0;i<k;i++ {
        fmt.Scan(&m, &n);
        if (m == 1 || n == 1 || (m==2 && n==2)) {
            fmt.Printf("YES\n")
        }else{
            fmt.Printf("NO\n")
        }
    }
}

// https://serhatgiydiren.github.io

package main

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

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

func solve(test_id int) {
 var n,m int
 fmt.Fscanf(reader, "%d %d\n", &n, &m)
 if n > m {
  n , m = m , n
 }
 if n == 1 || ( n == 2 && m == 2 ) {
  fmt.Fprintf(writer,"YES\n")
 } else {
  fmt.Fprintf(writer,"NO\n")
 }
}

func solve_cases() {
 defer writer.Flush()
 t := 1
 fmt.Fscanf(reader, "%d\n", &t)
 for i := 0 ; i < t ; i++ {
  solve(i)
 }
}

func main() {
 solve_cases()
}

package main

import "fmt"

func main() {
	var n int64
	var result = []bool{}
	fmt.Scanf("%d\n", &n)
	for j := int64(0); j < n; j++ {
		var n1, n2 int64
		fmt.Scanf("%d %d\n", &n1, &n2)
		// fmt.Println(n1, n2)
		bindings := (n2-1)*n1 + (n1-1)*n2
		result = append(result, bindings <= n1*n2)
	}

	for _, rr := range result {
		if rr {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	_ "bufio"
	"fmt"
)

/*
// scanf tutorial
func main() {
	// reader
	//reader := bufio.NewReader(io.reader)
	fmt.Println("reading input")
	var (
		t int
		p int
		q string
		r bool
		//s []int
	)
	fmt.Scanf("%d\n", &t)
	fmt.Printf("t is %d\n", t)
	for i := 0; i < t; i++ {
		//fmt.Printf("true")
		fmt.Scanf("%d %s %t\n", &p, &q, &r)
		fmt.Printf("entered %T is %v\nentered %T is %v\nentered %T is %v\n", p, p, q, q, r, r)
	}
	// fmt.Scanf("%d %s %t", &p, &q, &r)
	// fmt.Scanf("%d", &p)
	// fmt.Scanf("%s", &q)
	// fmt.Scanf("%t", &r)
	// fmt.Scanf("%v", &s)

}
*/
func calculate(n, m int) string {
	if n == 1 || m == 1 {
		return "YES"
	}

	if n > 2 || m > 2 {
		return "NO"
	}

	return "YES"
}

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

	for i := 0; i < test; i++ {
		fmt.Scanf("%d %d\n", &n, &m)
		fmt.Println(calculate(n, m))
	}
}

package main

import (
	_ "bufio"
	"fmt"
)

/*
// scanf tutorial
func main() {
	// reader
	//reader := bufio.NewReader(io.reader)
	fmt.Println("reading input")
	var (
		t int
		p int
		q string
		r bool
		//s []int
	)
	fmt.Scanf("%d\n", &t)
	fmt.Printf("t is %d\n", t)
	for i := 0; i < t; i++ {
		//fmt.Printf("true")
		fmt.Scanf("%d %s %t\n", &p, &q, &r)
		fmt.Printf("entered %T is %v\nentered %T is %v\nentered %T is %v\n", p, p, q, q, r, r)
	}
	// fmt.Scanf("%d %s %t", &p, &q, &r)
	// fmt.Scanf("%d", &p)
	// fmt.Scanf("%s", &q)
	// fmt.Scanf("%t", &r)
	// fmt.Scanf("%v", &s)

}
*/

// accepted solution
/*
func calculate(n, m int) string {
	if n == 1 || m == 1 {
		return "YES"
	}

	if n > 2 || m > 2 {
		return "NO"
	}

	return "YES"
}
*/

func calculate(n, m int) string {
	if n*m != 4 && n >= 2 && m >= 2 {
		return "No"
	} else {
		return "YES"
	}
}

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

	for i := 0; i < test; i++ {
		fmt.Scanf("%d %d\n", &n, &m)
		fmt.Println(calculate(n, m))
	}
}

package main

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

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

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

	for i := 0; i < t; i++ {
		var n, m int
		fmt.Fscan(r, &n, &m)
		if (n == 2 && m == 2) || n == 1 || m == 1 {
			fmt.Fprintln(w, "YES")
		} else {
			fmt.Fprintln(w, "NO")
		}
	}
}

package main

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt64() int64
	NextUInt64() uint64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

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

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

func (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}

func gcd(x, y int64) int64 {
	for y != 0 {
		x, y = y, x%y
	}
	return x
}

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

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

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

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

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		n, m := reader.NextInt64(), reader.NextInt64()
		if n == 1 || m == 1 || (n == 2 && m == 2) {
			writer.Printf("YES\n")
		} else {
			writer.Printf("NO\n")
		}
	}
}

package main

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

func main() {
	var t, n int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		for j := 0; j < n; j++ {
			fmt.Printf("%d ", j+1)
		}
		fmt.Println()
	}
}

package main

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

func main() {
    reader := bufio.NewReader(os.Stdin)
    ts, _ := reader.ReadString('\n')
	t, _ := strconv.Atoi(strings.TrimSpace(ts))
	for i := 0; i < t; i++ {
		ns, _ := reader.ReadString(' ')
		n, _ := strconv.Atoi(strings.TrimSpace(ns))
		ms, _ := reader.ReadString(' ')
		m, _ := strconv.Atoi(strings.TrimSpace(ms))
		if n == 1 || m == 1 || (n == 2 && m == 2) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}
func NextInt64() int64 {
	bytes := NextBytes()
	i, err := Atoi64(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func Atoi64(b []byte) (int64, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	var n int64
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int64(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

func reverseInts(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

func unique(a []int) int {
	newLen := 0
	n := len(a)
	for i := 0; i < n; i++ {
		if newLen == 0 || a[i] != a[newLen-1] {
			a[newLen] = a[i]
			newLen++
		}
	}
	return newLen
}

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

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

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)
	T := NextInt()

	for t := 0; t < T; t++ {
		n, m := NextInt(), NextInt()
		if n == 1 || m == 1 {
			fmt.Fprintln(out, "YES")
			continue
		}
		if n > 2 && m > 2 {
			fmt.Fprintln(out, "NO")
			continue
		}
		fmt.Fprintln(out, "YES")
	}

}

// https://codeforces.com/contest/1345/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	var (
		T int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		solver()
	}
}

func solver() {
	var n, m int
	fmt.Fscan(input, &n, &m)
	if n == 1 && m == 1 {
		fmt.Println("YES")
		return
	}

	if n*m > 4 {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
	}
}

// https://codeforces.com/contest/1345/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	var (
		T int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		solver()
	}
}

func solver() {
	var n, m int
	fmt.Fscan(input, &n, &m)
	if n == 1 || m == 1 {
		fmt.Println("YES")
		return
	}

	if n*m > 4 {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
	}
}

package main

import "fmt"

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

func main() {
   var a,b, t, i int
   fmt.Scan(&t)
   for i=0;i<t;i++{
   	fmt.Scan(&a,&b)
   	if min(a,b) == 1 && max(a,b) == 2{
   		fmt.Println("Yes")
   	}else{
   		fmt.Println("No")
   	}
   }
}

package main

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

func main() {
	n, m := readInt(), readInt()

	if n == 1 || m == 1 || (n == 2 && m == 2) {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

// sort ------------------------------------------------------------

type xxx struct {
	x int
}

type sortArray []xxx

func (s sortArray) Len() int           { return len(s) }
func (s sortArray) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
func (s sortArray) Less(i, j int) bool { return s[i].x < s[j].x }

// -----------------------------------------------------------------

var (
	readString func() string
	readBytes  func() []byte
)

func init() {
	log.SetFlags(log.Lshortfile)
	readString, readBytes = newReadString(os.Stdin)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Bytes()
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

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

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

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

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

func sum(a []int) int {
	var ret int
	for i := range a {
		ret += a[i]
	}
	return ret
}

func sumFloat64(a []float64) float64 {
	var ret float64
	for i := range a {
		ret += a[i]
	}
	return ret
}

func gcd(m, n int) int {
	for m%n != 0 {
		m, n = n, m%n
	}
	return n
}

func lcm(m, n int) int {
	return m / gcd(m, n) * n
}

package main

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

func main() {
	for t := ScanInt(); t > 0; t-- {
		n, m := ScanInt(), ScanInt()
		if solve(n, m) {
			P("YES")
		} else {
			P("NO")
		}
	}

	close(O)
	oWg.Wait()
}
func solve(n, m int) bool {
	if n <= 2 || m <= 2 {
		return true
	}

	return false
}

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

var O chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var P func(a ...interface{}) (n int, err error)

func init() {
	P = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	O = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

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

func ScanWord() string { in.Scan(); return in.Text() }
func ScanInt() int     { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt()
	}
	return arr
}
func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main
import (
    "fmt"
)
func main() {
    var k,m,n int;
    fmt.Scanf("%d", &k);
    for i := 0;i<k;i++ {
        fmt.Scanf("%d %d", &m, &n);
        if (m == 1 || n == 1 || (m==2 && n==2)) {
            fmt.Printf("YES\n")
        }else{
            fmt.Printf("NO\n")
        }
    }
}

package main
import (
    "fmt"
)
func main() {
    var k,m,n int;
    fmt.Scanf("%d", &k);
    for i := 0;i<k;i++ {
        fmt.Scanf("%d %d", &m, &n);
        fmt.Print("%d %d\n", m, n);
        if (m == 1 || n == 1 || (m==2 && n==2)) {
            fmt.Printf("YES\n")
        }else{
            fmt.Printf("NO\n")
        }
    }
}

// https://serhatgiydiren.github.io

package main

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

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

func solve(test_id int) {
 var n,m int
 fmt.Fscanf(reader, "%d %d\n", &n, &m)
 if n > m {
  n , m = m , n
 }
 if n == 1 {
  fmt.Fprintf(writer,"YES\n")
 } else if n > 2 || m > 5 {
  fmt.Fprintf(writer,"NO\n")
 } else {
  fmt.Fprintf(writer,"YES\n")
 }
}

func solve_cases() {
 defer writer.Flush()
 t := 1
 fmt.Fscanf(reader, "%d\n", &t)
 for i := 0 ; i < t ; i++ {
  solve(i)
 }
}

func main() {
 solve_cases()
}

// https://serhatgiydiren.github.io

package main

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

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

func solve(test_id int) {
 var n,m int
 fmt.Fscanf(reader, "%d %d\n", &n, &m)
 if n > m {
  n , m = m , n
 }
 if n > 2 || m > 5 {
  fmt.Fprintf(writer,"NO\n")
 } else {
  fmt.Fprintf(writer,"YES\n")
 }
}

func solve_cases() {
 defer writer.Flush()
 t := 1
 fmt.Fscanf(reader, "%d\n", &t)
 for i := 0 ; i < t ; i++ {
  solve(i)
 }
}

func main() {
 solve_cases()
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	for j := 0; j < n; j++ {
		var n1, n2 int
		fmt.Scanf("%d %d\n", &n1, &n2)
		var bindings = (n2-1)*n1 + (n1-1)*n2
		if bindings <= n1*n2 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}

}

package main

import "fmt"

func main() {
	var n int
	fmt.Scanf("%d%c", &n)
	for j := 0; j < n; j++ {
		var n1, n2 int
		fmt.Scanf("%d %d%c", &n1, &n2)
		var bindings = (n2-1)*n1 + (n1-1)*n2
		if bindings <= n1*n2 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scanf("%d", &n)
	for j := 0; j < n; j++ {
		var n1, n2 int
		fmt.Scanf("%d %d", &n1, &n2)
		var bindings = (n2-1)*n1 + (n1-1)*n2
		if bindings <= n1*n2 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}

}

package main

import "fmt"

func main() {
	var n int
	var result = []bool{}
	fmt.Scanf("%d\n", &n)
	for j := 0; j < n; j++ {
		var n1, n2 int
		fmt.Scanf("%d %d\n", &n1, &n2)
		var bindings = (n2-1)*n1 + (n1-1)*n2
		result = append(result, bindings <= n1*n2)
	}

	for _, rr := range result {
		if rr {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

func main() {
	var n int
	var result = []bool{}
	fmt.Scan(&n)
	for j := 0; j < n; j++ {
		var n1, n2 int
		fmt.Scanln(&n1, &n2)
		var bindings = (n2-1)*n1 + (n1-1)*n2
		result = append(result, bindings <= n1*n2)
	}

	for _, rr := range result {
		if rr {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	for j := 0; j < n; j++ {
		var n1, n2 int
		fmt.Scanln(&n1, &n2)
		var bindings = (n2-1)*n1 + (n1-1)*n2
		if bindings <= n1*n2 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

func main() {
	var n int64
	var result = []bool{}
	fmt.Scanf("%d\n", &n)
	for j := int64(0); j < n; j++ {
		var n1, n2 int64
		fmt.Scanf("%d %d\n", &n1, &n2)
		fmt.Println(n1, n2)
		bindings := (n2-1)*n1 + (n1-1)*n2
		result = append(result, bindings <= n1*n2)
	}

	for _, rr := range result {
		if rr {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

func main() {
	var n int
	var result = []bool{}
	fmt.Scanf("%d\n", &n)
	for j := 0; j < n; j++ {
		var n1, n2 int
		fmt.Scanf("%d %d\n", &n1, &n2)
		fmt.Println(n1, n2)
		var bindings = (n2-1)*n1 + (n1-1)*n2
		result = append(result, bindings <= n1*n2)
	}

	for _, rr := range result {
		if rr {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scanf("%d%n", &n)
	for j := 0; j < n; j++ {
		var n1, n2 int
		fmt.Scanf("%d %d%n", &n1, &n2)
		var bindings = (n2-1)*n1 + (n1-1)*n2
		if bindings <= n1*n2 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	_ "bufio"
	"fmt"
)

/*
// scanf tutorial
func main() {
	// reader
	//reader := bufio.NewReader(io.reader)
	fmt.Println("reading input")
	var (
		t int
		p int
		q string
		r bool
		//s []int
	)
	fmt.Scanf("%d\n", &t)
	fmt.Printf("t is %d\n", t)
	for i := 0; i < t; i++ {
		//fmt.Printf("true")
		fmt.Scanf("%d %s %t\n", &p, &q, &r)
		fmt.Printf("entered %T is %v\nentered %T is %v\nentered %T is %v\n", p, p, q, q, r, r)
	}
	// fmt.Scanf("%d %s %t", &p, &q, &r)
	// fmt.Scanf("%d", &p)
	// fmt.Scanf("%s", &q)
	// fmt.Scanf("%t", &r)
	// fmt.Scanf("%v", &s)

}
*/

// accepted solution
/*
func calculate(n, m int) string {
	if n == 1 || m == 1 {
		return "YES"
	}

	if n > 2 || m > 2 {
		return "NO"
	}

	return "YES"
}
*/

func calculate(n, m int) string {
	if n > 2 && m > 2 {
		return "No"
	} else {
		return "YES"
	}
}

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

	for i := 0; i < test; i++ {
		fmt.Scanf("%d %d\n", &n, &m)
		fmt.Println(calculate(n, m))
	}
}

package main

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

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

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

	for i := 0; i < t; i++ {
		var n, m int
		fmt.Fscan(r, &n, &m)
		if n == 2 || n == 1 || m == 2 || m == 1 {
			fmt.Fprintln(w, "YES")
		} else {
			fmt.Fprintln(w, "NO")
		}
	}
}


Once Bob needed to find the second order statistics of a sequence of integer numbers. Lets choose each number from the sequence exactly once and sort them. The value on the second position is the second order statistics of the given sequence. In other words it is the smallest element strictly greater than the minimum. Help Bob solve this problem.

The first input line contains integer n (1 ≤ n ≤ 100) — amount of numbers in the sequence. The second line contains n space-separated integer numbers — elements of the sequence. These numbers don't exceed 100 in absolute value.

If the given sequence has the second order statistics, output this order statistics, otherwise output NO.

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"bytes"
)

func main() {
	b := make([]bool, 201, 201)
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	for i := 0;i < n; i ++ {
		var x int
		fmt.Fscanf(reader, "%d ", &x)
		b[x + 100] = true
	}
	first := false
	for i := 0;i <= 200;i ++ {
		if b[i] {
			if !first {
				first = true
			} else {
				fmt.Printf("%d", i - 100)
				return
			}
		}
	}
	fmt.Printf("NO")
	return
}

package main

import "os"
import "bufio"
import "strconv"
import "sort"

func main(){
	var n int
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	r.Scan()
	n, _ = strconv.Atoi(r.Text())
	a := make([]int, n)
	for i:=0; i<n; i++ {
		r.Scan()
		a[i], _ = strconv.Atoi(r.Text())
	}
	sort.Ints(a)
	prev := a[0]
	for j:=0; j<n; j++ {
		if a[j] != prev {
			w.WriteString(strconv.Itoa(a[j])+"\n")
			w.Flush()
			return
		}
		prev = a[j]
	}
	w.WriteString("NO\n")
	w.Flush()
}

package main

import "fmt"
import "log"

func main() {
	var n int
	nargs, err := fmt.Scan(&n);
	if err != nil {
		log.Fatal(err);
	}
	if nargs != 1 {
		log.Fatal("bad n", nargs);
	}
	if n < 2 {
		fmt.Println("NO");
		return;
	}

	var num, min1, min2 int
	idx := 0;
	for i := 0; i < n; i++ {
		nargs, err = fmt.Scan(&num);
		if err != nil {
			log.Fatal(err);
		}
		if nargs != 1 {
			log.Fatal("bad n", nargs);
		}

		switch idx {
		case 0:
			min1 = num;
			idx++;
			break;
		case 1:
			if num < min1 {
				min2 = min1;
				min1 = num;
				idx++;
			} else if (num > min1) {
				min2 = num;
				idx++;
			}
			break;
		default:
			if num == min1 {
				// Skip it...
				break;
			}
			if num < min1 {
				min2 = min1;
				min1 = num;
			} else if (num < min2) {
				min2 = num;
			}
		}
	}
	if (idx < 2) {
		fmt.Println("NO");
	} else {
		fmt.Println(min2);
	}
}

// asd1
package main

import (
    "fmt"
    "sort"
)

func main() {
    var n int
    var i int
    //min :=1000000000
    fmt.Scan(&n)
    a := make([]int,n)
    for i=0;i<n;i++{
        fmt.Scan(&a[i])
    }
    sort.Ints(a)
    for j:=1;j<n;j++{
        if a[0] != a[j]{
            fmt.Println(a[j])
            return
        }
    }
    fmt.Println("NO")

}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	number := 0
	if scanner.Scan() {
		line := scanner.Text()

		fmt.Sscanf(line, "%d", &number)
	}

	if scanner.Scan() {
		line := scanner.Text()

		tokens := strings.Fields(line)
		numbers := make(sort.IntSlice, number)

		for i := 0; i < number; i++ {
			numbers[i], _ = strconv.Atoi(tokens[i])
		}

		sort.Sort(numbers)

		for i := 0; i < number; i++ {
			if numbers[i] != numbers[0] {
				fmt.Println(numbers[i])
				return
			}
		}
		fmt.Println("NO")
	}
}

// 22A
package main

import (
	"fmt"
)

func main() {
	var n, i, a int
	var arr [300]int
	sum := 0
	fmt.Scan(&n)
	for i = 0; i < n; i++ {
		fmt.Scan(&a)
		arr[a+100]++
	}
	for i = 0; i < 300; i++ {
		if arr[i] != 0 {
			sum++
		}
		if sum == 2 {
			fmt.Print(i - 100)
			return
		}
	}
	fmt.Println("NO")
	return
}

// 22A-mic
package main

import (
	"fmt"
	"sort"
)

func main() {
	var a []int
	var n, x, i int
	fmt.Scan(&n)
	for i = 1; i <= n; i++ {
		fmt.Scan(&x)
		a = append(a, x)
	}
	if len(a) == 1 {
		goto fail
	}
	sort.Sort(sort.IntSlice(a))
	for i = 1; i < n; i++ {
		if a[0] != a[i] {
			fmt.Println(a[i])
			return
		}
	}
	goto fail
fail:
	{
		fmt.Println("NO")
	}
}

package main

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

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

const Max = 100 + 1

func main() {
        defer w.Flush()

        var n, tmp int
        a, b := Max, Max
        fmt.Fscanf(r, "%d\n", &n)
        for i := 0; i < n; i++ {
                fmt.Fscanf(r, "%d ", &tmp)
                if tmp < a {
                        b = a
                        a = tmp
                } else if tmp < b && tmp != a {
                        b = tmp
                }
        }

        if b == Max {
                fmt.Fprintf(w, "NO")
        } else {
                fmt.Fprintf(w, "%v", b)
        }
}

package main

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

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

	var n, t int
	a, b := 101, 101
	fmt.Fscan(r, &n)
	for i := 0; i < n; i++ {
		fmt.Fscan(r, &t)
		if i == 0 {
			a = t
		} else {
			if t < a {
				a, b = t, a
			} else if t < b && t != a {
				b = t
			}
		}

	}
	if b == 101{
		fmt.Fprintln(w, "NO")
	} else {
		fmt.Fprintln(w, b)
	}
}

package main

import "fmt"
import "log"

func main() {
	var n int
	nargs, err := fmt.Scan(&n);
	if err != nil {
		log.Fatal(err);
	}
	if nargs != 1 {
		log.Fatal("bad n", nargs);
	}
	if n < 2 {
		fmt.Println("NO");
		return;
	}

	var num, min1, min2 int
	idx := 0;
	for i := 0; i < n; i++ {
		nargs, err = fmt.Scan(&num);
		if err != nil {
			log.Fatal(err);
		}
		if nargs != 1 {
			log.Fatal("bad n", nargs);
		}

		switch idx {
		case 0:
			min1 = num;
			idx++;
			break;
		case 1:
			if num < min1 {
				min2 = min1;
				min1 = num;
				idx++;
			} else if (num > min1) {
				min2 = num;
				idx++;
			}
			break;
		default:
			if num == min1 {
				// Skip it...
				break;
			}
			if num < min1 {
				min2 = min1;
				min1 = num;
			} else if (num < min2) {
				min2 = num;
			}
		}
	}
	fmt.Println(min2);
}

package main

import "fmt"
import "log"

func main() {
	var n int
	nargs, err := fmt.Scan(&n);
	if err != nil {
		log.Fatal(err);
	}
	if nargs != 1 {
		log.Fatal("bad n", nargs);
	}
	if n < 2 {
		fmt.Println("NO");
		return;
	}

	var num, min1, min2 int
	for i := 0; i < n; i++ {
		nargs, err = fmt.Scan(&num);
		if err != nil {
			log.Fatal(err);
		}
		if nargs != 1 {
			log.Fatal("bad n", nargs);
		}

		switch i {
		case 0:
			min1 = num;
			break;
		case 1:
			if num < min1 {
				min2 = min1;
				min1 = num;
			} else {
				min2 = num;
			}
			break;
		default:
			if num < min1 {
				min2 = min1;
				min1 = num;
			} else if (num < min2) {
				min2 = num;
			}
		}
	}
	fmt.Println(min2);
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	number := 0
	if scanner.Scan() {
		line := scanner.Text()

		fmt.Sscanf(line, "%d", &number)
	}

	if scanner.Scan() {
		line := scanner.Text()

		tokens := strings.Fields(line)
		numbers := make(sort.IntSlice, number)

		for i := 0; i < number; i++ {
			numbers[i], _ = strconv.Atoi(tokens[i])
		}

		sort.Sort(numbers)

		for i := 0; i < number; i++ {
			if numbers[i] != numbers[0] {
				fmt.Println(numbers[i])
				break
			}
		}
	}
}

// 22A
package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	a := make([]int, 105)
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	sort.Sort(sort.IntSlice(a))
	for i := 1; i < n; i++ {
		if a[i] != a[0] {
			fmt.Println(a[i])
			return
		}
	}
	fmt.Println("NO")
}

// 22A
package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, i int
	a := make([]int, 105)
	fmt.Scan(&n)
	for i = 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	sort.Sort(sort.IntSlice(a))
	i = 2
	for a[i] == a[i-1] {
		i++
	}
	if i > n || n == 1 {
		fmt.Println(a[i])
	} else {
		fmt.Println("NO")
	}
}

package main

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

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

const Max = 100 + 1

func main() {
        defer w.Flush()

        var n, tmp int
        a, b := Max, Max
        fmt.Fscanf(r, "%d\n", &n)
        for i := 0; i < n; i++ {
                fmt.Fscanf(r, "%d ", &tmp)
                if tmp < a {
                        b = a
                        a = tmp
                } else if tmp < b && tmp != a {
                        b = tmp
                }
        }
        fmt.Println(a, b)

        if b == Max {
                fmt.Fprintf(w, "NO")
        } else {
                fmt.Fprintf(w, "%v", b)
        }
}


The heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnormally hot. But any scientific claim sounds a lot more reasonable if there are some numbers involved, so they have decided to actually calculate some value which would represent how high the temperatures are.Mathematicians of Berland State University came up with a special heat intensity value. This value is calculated as follows:Suppose we want to analyze the segment of $$$n$$$ consecutive days. We have measured the temperatures during these $$$n$$$ days; the temperature during $$$i$$$-th day equals $$$a_i$$$.We denote the average temperature of a segment of some consecutive days as the arithmetic mean of the temperature measures during this segment of days. So, if we want to analyze the average temperature from day $$$x$$$ to day $$$y$$$, we calculate it as $$$\frac{\sum \limits_{i = x}^{y} a_i}{y - x + 1}$$$ (note that division is performed without any rounding). The heat intensity value is the maximum of average temperatures over all segments of not less than $$$k$$$ consecutive days. For example, if analyzing the measures $$$[3, 4, 1, 2]$$$ and $$$k = 3$$$, we are interested in segments $$$[3, 4, 1]$$$, $$$[4, 1, 2]$$$ and $$$[3, 4, 1, 2]$$$ (we want to find the maximum value of average temperature over these segments).You have been hired by Berland State University to write a program that would compute the heat intensity value of a given period of days. Are you up to this task?

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 5000$$$) — the number of days in the given period, and the minimum number of days in a segment we consider when calculating heat intensity value, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le 5000$$$) — the temperature measures during given $$$n$$$ days.

Print one real number — the heat intensity value, i. e., the maximum of average temperatures over all segments of not less than $$$k$$$ consecutive days. Your answer will be considered correct if the following condition holds: $$$|res - res_0| < 10^{-6}$$$, where $$$res$$$ is your answer, and $$$res_0$$$ is the answer given by the jury's solution.

package main

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

func main(){
	var n,k int
	Scan(&n,&k)
	a:=make([]int,n)
	in:=bufio.NewReader(os.Stdin)
	for i:=0;i<n;i++{
		Fscan(in,&a[i])
	}

	if k==1{
		max:=a[0]
		for i:=1;i<n;i++{
			if max<a[i]{
				max=a[i]
			}
		}
		Print(max)
		return
	}
	max:=.0
	for i:=k;i<=n;i++{
		sum:=0
		for j:=0;j<i;j++{
			sum+=a[j]
		}
		ave:=float64(sum)/float64(i)
		if max<ave{
			max=ave
		}
		for j:=1;j<n-i+1;j++{
			sum-=a[j-1]
			sum+=a[j+i-1]
			ave=float64(sum)/float64(i)
			if max<ave{
				max=ave
			}
		}
	}
	Printf("%.8f",max)
}

package main

import (
	"fmt"
	"math"
)

var (
	n, k int
	a, s [5005]float64
	ans float64
)
func main() {
	fmt.Scan(&n, &k)
	for i := 1; i <= n; i++ {
		fmt.Scan(&a[i])
		s[i] = s[i - 1] + a[i]
	}
	for i := 1; i <= n; i++ {
		for j := 0; j <= i - k; j++ {
			ans = math.Max(ans, (s[i] - s[j]) / float64(i - j))
		}
	}
	fmt.Println(ans)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1003C(_r io.Reader, out io.Writer) {
	const eps = 1e-8
	in := bufio.NewReader(_r)

	var n, d int
	Fscan(in, &n, &d)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	l, r := 1.0, 5e3
o:
	for t := int(math.Log2((r - l) / eps)); t > 0; t-- {
		x := (l + r) / 2
		s := make([]float64, n+1)
		for i, v := range a {
			s[i+1] = s[i] + float64(v) - x
		}
		minI := 0
		for i, v := range s[:n+1-d] {
			if v < s[minI] {
				minI = i
			}
			if s[i+d] >= s[minI] {
				l = x
				continue o
			}
		}
		r = x
	}
	Fprintf(out, "%.8f", (l+r)/2)
}

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

package main

import "fmt"

func main() {
	var n, k int
	fmt.Scan(&n, &k)
	temps := make([]int, n)
	var max, sred float64
	max = 0
	var sum int
	for i := 0; i < n; i++ {
		fmt.Scan(&temps[i])
	}
	for i := 0; i < n; i++ {
		sum = 0
		for j := i; j < n; j++ {
			sum += temps[j]
			if j - i + 1 >= k {
				sred = float64(sum) / float64(j - i + 1)
				if max < sred {
					max = sred
				}
			}
		}
	}
	fmt.Print(max)
}

package main

import "fmt"

func main() {
	var n, k int
	fmt.Scan(&n, &k)
	temps := make([]int, n)
	var max, sred float64
	max = 0
	var sum int
	for i := 0; i < n; i++ {
		fmt.Scan(&temps[i])
	}
	for i := 0; i < n; i++ {
		for j := i + k; j < n; j++ {
			sum = 0
			for c := i; c < j; c++ {
				sum += temps[c]
			}
			sred = float64(sum) / float64(j - i)
			if max < sred {
				max = sred
			}
		}
	}
	fmt.Print(max)
}

package main

import "fmt"

func main() {
	var n, k int
	fmt.Scan(&n, &k)
	temps := make([]int, n)
	var max, sred float64
	max = 0
	var sum int
	for i := 0; i < n; i++ {
		fmt.Scan(&temps[i])
	}
	for i := 0; i < n; i++ {
		for j := i + k; j < n; j++ {
			sum = 0
			for c := i; c < j; c++ {
				sum += temps[c]
			}
			sred = float64(sum) / float64(j)
			if max < sred {
				max = sred
			}
		}
	}
	fmt.Print(max)
}


There is a country with $$$n$$$ citizens. The $$$i$$$-th of them initially has $$$a_{i}$$$ money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have.Sometimes the government makes payouts to the poor: all citizens who have strictly less money than $$$x$$$ are paid accordingly so that after the payout they have exactly $$$x$$$ money. In this case the citizens don't send a receipt.You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^{5}$$$) — the numer of citizens. The next line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_{i} \le 10^{9}$$$) — the initial balances of citizens. The next line contains a single integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^{5}$$$) — the number of events. Each of the next $$$q$$$ lines contains a single event. The events are given in chronological order. Each event is described as either 1 p x ($$$1 \le p \le n$$$, $$$0 \le x \le 10^{9}$$$), or 2 x ($$$0 \le x \le 10^{9}$$$). In the first case we have a receipt that the balance of the $$$p$$$-th person becomes equal to $$$x$$$. In the second case we have a payoff with parameter $$$x$$$.

Print $$$n$$$ integers — the balances of all citizens after all events.

NoteIn the first example the balances change as follows: 1 2 3 4 $$$\rightarrow$$$ 3 3 3 4 $$$\rightarrow$$$ 3 2 3 4 $$$\rightarrow$$$ 3 2 3 4In the second example the balances change as follows: 3 50 2 1 10 $$$\rightarrow$$$ 3 0 2 1 10 $$$\rightarrow$$$ 8 8 8 8 10 $$$\rightarrow$$$ 8 8 20 8 10

package main

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

/* solution */

func solve() {

	n := readInt()
	balance := make([]int, n)
	pointer := make([]int, n)
	for i := 0; i < n; i++ {
		balance[i] = readInt()
	}

	q := readInt()
	social := make([]int, 0)

	for i := 0; i < q; i++ {
		op := readInt()
		if op == 1 {
			p, x := readInt()-1, readInt()
			balance[p] = x
			pointer[p] = len(social)
		} else {
			x := readInt()
			social = append(social, x)
		}
	}

	for i := len(social) - 2; i >= 0; i-- {
		if social[i+1] > social[i] {
			social[i] = social[i+1]
		}
	}

	for i := 0; i < n; i++ {
		if pointer[i] < len(social) && social[pointer[i]] > balance[i] {
			printInt(social[pointer[i]])
		} else {
			printInt(balance[i])
		}
	}

}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
	} else {
		f = os.Stdin
	}
	defer f.Close()

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

	reader = bufio.NewScanner(f)
	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

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

func print(s string) {
	writer.WriteString(s)
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

var segTree = make([]int, 1000000)

func getTarget(index, target, left, right, maxLazy int) int {
	var value int
	if segTree[index] > maxLazy {
		value = segTree[index]
	} else {
		value = maxLazy
	}
	if left == right {
		return value
	} else {
		mid := (left + right) / 2
		if target <= mid {
			return getTarget(index * 2 + 1, target, left, mid, value)
		} else {
			return getTarget(index * 2 + 2, target, mid + 1, right, value)
		}
	}
}

func setTarget(index, target, value, left, right, lazy int) {
	if left == right {
		segTree[index] = value
		return
	} else {
		if segTree[index] > lazy {
			lazy = segTree[index]
		}
		segTree[index] = 0
		mid := (left + right) / 2
		if target <= mid {
			setLazy(index * 2 + 2, lazy)
			setTarget(index * 2 + 1, target, value, left, mid, lazy)
			return
		} else {
			setLazy(index * 2 + 1, lazy)
			setTarget(index * 2 + 2, target, value, mid + 1, right, lazy)
			return
		}
	}
}

func setLazy(index, lazy int) {
	if index < len(segTree) && lazy > segTree[index] {
		segTree[index] = lazy
	}
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	for i := 0;i < n;i ++ {
		var money int
		if i == n - 1 {
			fmt.Fscanf(reader, "%d\n", &money)
		} else {
			fmt.Fscanf(reader, "%d ", &money)
		}
		setTarget(0, i, money, 0, n - 1, 0)
	}
	var q int
	fmt.Fscanf(reader, "%d\n", &q)
	for i := 0;i < q;i ++ {
		var req, p, x int
		fmt.Fscanf(reader, "%d ", &req)
		if req == 1 {
			fmt.Fscanf(reader, "%d %d\n", &p, &x)
			setTarget(0, p - 1, x, 0, n - 1, 0)
		} else {
			fmt.Fscanf(reader, "%d\n", &x)
			setLazy(0, x)
		}
	}
	for i := 0;i < n;i ++ {
		fmt.Printf("%d ", getTarget(0, i, 0, n - 1, 0))
	}
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/1198/B

package main

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

type ReceiptEvent struct {
	Person  int
	Balance int
}

type PayoutEvent struct {
	Min int
}

func Solve(n int, a []int, q int, queries []interface{}) []int {
	receipts := make([]int, n+1)
	payouts := make([]int, q+2)
	for i, query := range queries {
		switch query.(type) {
		case ReceiptEvent:
			r := query.(ReceiptEvent)
			receipts[r.Person] = i
			a[r.Person] = r.Balance
		case PayoutEvent:
			p := query.(PayoutEvent)
			payouts[i+1] = p.Min
		}
	}
	for i := q - 1; i >= 0; i-- {
		if payouts[i] < payouts[i+1] {
			payouts[i] = payouts[i+1]
		}
	}
	for i := 1; i <= n; i++ {
		if a[i] < payouts[receipts[i]+1] {
			a[i] = payouts[receipts[i]+1]
		}
	}
	return a[1:]
}

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

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	a := make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}
	fmt.Fscanf(reader, "\n")
	var q int
	fmt.Fscanf(reader, "%d\n", &q)
	queries := make([]interface{}, q)
	for i := 0; i < q; i++ {
		var queryType int
		fmt.Fscanf(reader, "%d", &queryType)
		if queryType == 1 {
			r := ReceiptEvent{}
			fmt.Fscanf(reader, "%d %d", &r.Person, &r.Balance)
			queries[i] = r
		} else {
			p := PayoutEvent{}
			fmt.Fscanf(reader, "%d", &p.Min)
			queries[i] = p
		}
		fmt.Fscanf(reader, "\n")
	}

	answer := Solve(n, a, q, queries)
	for _, elem := range answer {
		fmt.Fprintf(writer, "%d ", elem)
	}
	fmt.Fprintf(writer, "\n")
}

package main

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

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

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

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

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

	var q int
	fmt.Fscanf(input, "%d\n", &q)
	cmd := make([]int, q)
	p := make([]int, q)
	x := make([]int, q)
	for i := 0; i < q; i++ {
		fmt.Fscanf(input, "%d", &cmd[i])
		switch cmd[i] {
		case 1:
			fmt.Fscanf(input, "%d %d\n", &p[i], &x[i])
		case 2:
			fmt.Fscanf(input, "%d\n", &x[i])
		}
	}

	ans := make([]int, n)
	for i := range ans {
		ans[i] = -1
	}
	var payoff int
	for i := q - 1; i >= 0; i-- {
		switch cmd[i] {
		case 1:
			if ans[p[i]-1] == -1 {
				ans[p[i]-1] = max(x[i], payoff)
			}
		case 2:
			payoff = max(x[i], payoff)
		}
	}
	for i := range ans {
		if ans[i] == -1 {
			ans[i] = max(payoff, a[i])
		}
		fmt.Printf("%d", ans[i])
		if i < n-1 {
			fmt.Printf(" ")
		} else {
			fmt.Printf("\n")
		}
	}
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

var segTree = make([]int, 1000000)

func getTarget(index, target, left, right, maxLazy int) int {
	var value int
	if segTree[index] > maxLazy {
		value = segTree[index]
	} else {
		value = maxLazy
	}
	if left == right {
		return value
	} else {
		mid := (left + right) / 2
		if target <= mid {
			return getTarget(index * 2 + 1, target, left, mid, value)
		} else {
			return getTarget(index * 2 + 2, target, mid + 1, right, value)
		}
	}
}

func setTarget(index, target, value, left, right, lazy int) {
	if left == right {
		fmt.Printf("index=%d target=%d left=%d right=%d\n", index, target, left, right)
		segTree[index] = value
		return
	} else {
		if segTree[index] > lazy {
			lazy = segTree[index]
		}
		segTree[index] = 0
		mid := (left + right) / 2
		if target <= mid {
			setLazy(index * 2 + 2, lazy)
			setTarget(index * 2 + 1, target, value, left, mid, lazy)
			return
		} else {
			setLazy(index * 2 + 1, lazy)
			setTarget(index * 2 + 2, target, value, mid + 1, right, lazy)
			return
		}
	}
}

func setLazy(index, lazy int) {
	if index < len(segTree) && lazy > segTree[index] {
		segTree[index] = lazy
	}
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	for i := 0;i < n;i ++ {
		var money int
		if i == n - 1 {
			fmt.Fscanf(reader, "%d\n", &money)
		} else {
			fmt.Fscanf(reader, "%d ", &money)
		}
		setTarget(0, i, money, 0, n - 1, 0)
	}
	var q int
	fmt.Fscanf(reader, "%d\n", &q)
	for i := 0;i < q;i ++ {
		var req, p, x int
		fmt.Fscanf(reader, "%d ", &req)
		if req == 1 {
			fmt.Fscanf(reader, "%d %d\n", &p, &x)
			setTarget(0, p - 1, x, 0, n - 1, 0)
		} else {
			fmt.Fscanf(reader, "%d\n", &x)
			setLazy(0, x)
		}
	}
	for i := 0;i < n;i ++ {
		fmt.Printf("%d ", getTarget(0, i, 0, n - 1, 0))
	}
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

var segTree = make([]int, 500000)

func getTarget(index, target, left, right, maxLazy int) int {
	var value int
	if segTree[index] > maxLazy {
		value = segTree[index]
	} else {
		value = maxLazy
	}
	if left == right {
		return value
	} else {
		mid := (left + right) / 2
		if target <= mid {
			return getTarget(index * 2 + 1, target, left, mid, value)
		} else {
			return getTarget(index * 2 + 2, target, mid + 1, right, value)
		}
	}
}

func setTarget(index, target, value, left, right, lazy int) {
	if left == right {
		segTree[index] = value
		return
	} else {
		if segTree[index] > lazy {
			lazy = segTree[index]
		}
		segTree[index] = 0
		mid := (left + right) / 2
		if target <= mid {
			setLazy(index * 2 + 2, lazy)
			setTarget(index * 2 + 1, target, value, left, mid, lazy)
			return
		} else {
			setLazy(index * 2 + 1, lazy)
			setTarget(index * 2 + 2, target, value, mid + 1, right, lazy)
			return
		}
	}
}

func setLazy(index, lazy int) {
	if lazy > segTree[index] {
		segTree[index] = lazy
	}
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	var n int
	fmt.Fscanf(reader, "%d\r\n", &n)
	for i := 0;i < n;i ++ {
		var money int
		if i == n {
			fmt.Fscanf(reader, "%d\r\n", &money)
		} else {
			fmt.Fscanf(reader, "%d ", &money)
		}
		setTarget(0, i, money, 0, n - 1, 0)
	}
	var q int
	fmt.Fscanf(reader, "%d\r\n", &q)
	for i := 0;i < q;i ++ {
		var req, p, x int
		fmt.Fscanf(reader, "%d ", &req)
		if req == 1 {
			fmt.Fscanf(reader, "%d %d\r\n", &p, &x)
			setTarget(0, p - 1, x, 0, n - 1, 0)
		} else {
			fmt.Fscanf(reader, "%d\r\n", &x)
			setLazy(0, x)
		}
	}
	for i := 0;i < n;i ++ {
		fmt.Printf("%d ", getTarget(0, i, 0, n - 1, 0))
	}
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
)

var segTree = make([]int, 500000)

func getTarget(index, target, left, right, maxLazy int) int {
	var value int
	if segTree[index] > maxLazy {
		value = segTree[index]
	} else {
		value = maxLazy
	}
	if left == right {
		return value
	} else {
		mid := (left + right) / 2
		if target <= mid {
			return getTarget(index * 2 + 1, target, left, mid, value)
		} else {
			return getTarget(index * 2 + 2, target, mid + 1, right, value)
		}
	}
}

func setTarget(index, target, value, left, right, lazy int) {
	if left == right {
		segTree[index] = value
		return
	} else {
		if segTree[index] > lazy {
			lazy = segTree[index]
		}
		segTree[index] = 0
		mid := (left + right) / 2
		if target <= mid {
			setLazy(index * 2 + 2, lazy)
			setTarget(index * 2 + 1, target, value, left, mid, lazy)
			return
		} else {
			setLazy(index * 2 + 1, lazy)
			setTarget(index * 2 + 2, target, value, mid + 1, right, lazy)
			return
		}
	}
}

func setLazy(index, lazy int) {
	if lazy > segTree[index] {
		segTree[index] = lazy
	}
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	for i := 0;i < n;i ++ {
		var money int
		if i == n {
			fmt.Fscanf(reader, "%d\n", &money)
		} else {
			fmt.Fscanf(reader, "%d ", &money)
		}
		setTarget(0, i, money, 0, n - 1, 0)
	}
	var q int
	fmt.Fscanf(reader, "%d\n", &q)
	for i := 0;i < q;i ++ {
		var req, p, x int
		fmt.Fscanf(reader, "%d ", &req)
		if req == 1 {
			fmt.Fscanf(reader, "%d %d\n", &p, &x)
			setTarget(0, p - 1, x, 0, n - 1, 0)
		} else {
			fmt.Fscanf(reader, "%d\n", &x)
			setLazy(0, x)
		}
	}
	for i := 0;i < n;i ++ {
		fmt.Printf("%d ", getTarget(0, i, 0, n - 1, 0))
	}
}

package main

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

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

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

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

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

	cmd := make([]int, n)
	p := make([]int, n)
	x := make([]int, n)

	var q int
	fmt.Fscanf(input, "%d\n", &q)
	for i := 0; i < q; i++ {
		fmt.Fscanf(input, "%d", &cmd[i])
		switch cmd[i] {
		case 1:
			fmt.Fscanf(input, "%d %d\n", &p[i], &x[i])
		case 2:
			fmt.Fscanf(input, "%d\n", &x[i])
		}
	}

	ans := make([]int, n)
	for i := range ans {
		ans[i] = -1
	}
	var payoff int
	for i := q - 1; i >= 0; i-- {
		switch cmd[i] {
		case 1:
			if ans[p[i]] == -1 {
				ans[p[i]] = max(x[i], payoff)
			}
		case 2:
			payoff = max(x[i], payoff)
		}
	}
	for i := range ans {
		if ans[i] == -1 {
			ans[i] = max(payoff, a[i])
		}
		fmt.Printf("%d", ans[i])
		if i < n-1 {
			fmt.Printf(" ")
		} else {
			fmt.Printf("\n")
		}
	}
}


This is an interactive problem.John and his imaginary friend play a game. There are $$$n$$$ lamps arranged in a circle. Lamps are numbered $$$1$$$ through $$$n$$$ in clockwise order, that is, lamps $$$i$$$ and $$$i + 1$$$ are adjacent for any $$$i = 1, \ldots, n - 1$$$, and also lamps $$$n$$$ and $$$1$$$ are adjacent. Initially all lamps are turned off.John and his friend take turns, with John moving first. On his turn John can choose to terminate the game, or to make a move. To make a move, John can choose any positive number $$$k$$$ and turn any $$$k$$$ lamps of his choosing on. In response to this move, John's friend will choose $$$k$$$ consecutive lamps and turn all of them off (the lamps in the range that were off before this move stay off). Note that the value of $$$k$$$ is the same as John's number on his last move. For example, if $$$n = 5$$$ and John have just turned three lamps on, John's friend may choose to turn off lamps $$$1, 2, 3$$$, or $$$2, 3, 4$$$, or $$$3, 4, 5$$$, or $$$4, 5, 1$$$, or $$$5, 1, 2$$$.After this, John may choose to terminate or move again, and so on. However, John can not make more than $$$10^4$$$ moves.John wants to maximize the number of lamps turned on at the end of the game, while his friend wants to minimize this number. Your task is to provide a strategy for John to achieve optimal result. Your program will play interactively for John against the jury's interactor program playing for John's friend.Suppose there are $$$n$$$ lamps in the game. Let $$$R(n)$$$ be the number of turned on lamps at the end of the game if both players act optimally. Your program has to terminate the game with at least $$$R(n)$$$ turned on lamps within $$$10^4$$$ moves. Refer to Interaction section below for interaction details.For technical reasons hacks for this problem are disabled.

NoteWhen $$$n = 3$$$, any John's move can be reversed, thus $$$R(3) = 0$$$, and terminating the game immediately is correct.$$$R(4) = 1$$$, and one strategy to achieve this result is shown in the second sample case.Blank lines in sample interactions are for clarity and should not be printed.

package main

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

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

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

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

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

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

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

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

	return i
}

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

	n := readNum(reader)

	ask := func(on []int) int {
		var buf bytes.Buffer

		buf.WriteString(fmt.Sprintf("%d ", len(on)))
		for i := 0; i < len(on); i++ {
			buf.WriteString(fmt.Sprintf("%d ", on[i]))
		}
		fmt.Println(buf.String())
		return readNum(reader)
	}

	solve(n, ask)

	fmt.Println("0")
}

func solve(n int, ask func(on []int) int) {
	// find k that maximizes n - k - [n/k]

	var k = 1
	best := n - k - n/k
	for i := 2; i <= n; i++ {
		tmp := n - i - (n+i-1)/i
		if tmp > best {
			best = tmp
			k = i
		}
	}
	if k == 1 {
		// no matter what I do, it will be reverted
		return
	}
	lamps := make([]int, n)
	for i := 0; i < n; {
		lamps[i] = -1
		i += k
	}
	on := make([]int, k)
	for {
		var p int
		for i := 0; i < n && p < k; i++ {
			if lamps[i] == 0 {
				on[p] = i + 1
				p++
				lamps[i] = 1
			}
		}

		if p < k {
			break
		}

		x := ask(on)
		x--

		for i := 0; i < k; i++ {
			j := (x + i) % n
			if lamps[j] > 0 {
				lamps[j] = 0
			}
		}
	}
}


Little penguin Polo has an n × m matrix, consisting of integers. Let's index the matrix rows from 1 to n from top to bottom and let's index the columns from 1 to m from left to right. Let's represent the matrix element on the intersection of row i and column j as aij.In one move the penguin can add or subtract number d from some matrix element. Find the minimum number of moves needed to make all matrix elements equal. If the described plan is impossible to carry out, say so.

The first line contains three integers n, m and d (1 ≤ n, m ≤ 100, 1 ≤ d ≤ 104) — the matrix sizes and the d parameter. Next n lines contain the matrix: the j-th integer in the i-th row is the matrix element aij (1 ≤ aij ≤ 104).

In a single line print a single integer — the minimum number of moves the penguin needs to make all matrix elements equal. If that is impossible, print "-1" (without the quotes).

package main
import (
	"fmt"
	"bufio"
	"os"
	"strconv"
)
func abs(x int)int {
	if x<0 {
		x*=(-1)
	}
	return x
}
func main() {
	var n,m,d,res int
	var a [10010]int
	fmt.Scan(&n,&m,&d)
	res=(1<<30)
	n*=m
	input:= bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)
	for i:=0;i<n;i++ {
		input.Scan()
		aa,_:=strconv.Atoi(input.Text())
		a[i]=aa
	}
	for i:=1;i<n;i++ {
		if ((a[i]-a[0])%d)!=0 {
			res=-1
		}
	}
	flag:=true
	if res<0 {
		fmt.Println(res)
		flag=false
	}

	for k:=0;k<n;k++ {
		//var t int
		//t=a[k]
		c:=0
		for i:=0;i<n;i++ {
			c+=abs(a[i]-a[k])
		}
		if (c/d)<res {
			res=c/d
		}
	}
	if flag==true {
		fmt.Println(res)
	}
}

package main

import (
	"fmt"
	"sort"
)

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

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

	a := make([]int, n*m, n*m)
	for i, k := 0, 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Scanf("%d", &a[k])
			k++
		}
		fmt.Scanf("\n")
	}

	for i := 1; i < n * m; i++ {
		if a[0] % d != a[i] % d {
			fmt.Println(-1)
			return
		}
	}

	sort.Ints(a)

	var mm int
	if (n * m) % 2 == 1 {
		mm = a[n * m / 2]
	} else {
		mm = (a[n * m / 2] + a[n * m / 2 - 1]) / 2
	}

	var r int
	for i := 0; i < n * m; i++ {
		r += abs(a[i] - mm)
	}

	fmt.Println(r/d)
}

package main

import (
	"fmt"
	"sort"
)

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

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

	a := make([]int, n*m, n*m)
	for i, k := 0, 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Scanf("%d", &a[k])
			k++
		}
		fmt.Scanf("\n")
	}

	for i := 1; i < n * m; i++ {
		if a[0] % d != a[i] % d {
			fmt.Println(-1)
			return
		}
	}

	sort.Ints(a)

	var mm int
	if (n * m) % 2 == 1 {
		mm = a[n * m / 2]
	} else {
		mm = (a[n * m / 2] + a[n * m / 2 - 1]) / 2
	}

	var r int
	for i := 0; i < n * m; i++ {
		r += abs(a[i] - mm)
	}

    fmt.Println(a)
	fmt.Println(r/d)
}

package main

import (
	"fmt"
	"sort"
)

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

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

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

	for i := 1; i < n * m; i++ {
		if a[0] % d != a[i] % d {
			fmt.Println(-1)
			return
		}
	}

	sort.Ints(a)

	var mm int
	if (n * m) % 2 == 1 {
		mm = a[n * m / 2]
	} else {
		mm = (a[n * m / 2] + a[n * m / 2 - 1]) / 2
	}

	var r int
	for i := 0; i < n * m; i++ {
		r += abs(a[i] - mm)
	}

	fmt.Println(r/d)
}

package main

import (
	"fmt"
	"sort"
)

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

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

	a := make([]int, n*m, n*m)
	for i := 0; i < n*m; i++ {
		fmt.Scanf("%d", &a[i])
		if i > 0 && i % n == 0 {
			fmt.Scanf("\n")
		}
	}

	for i := 1; i < n * m; i++ {
		if a[0] % d != a[i] % d {
			fmt.Println(-1)
			return
		}
	}

	sort.Ints(a)
	var mm int
	if (n * m) % 2 == 1 {
		mm = a[n * m / 2]
	} else {
		mm = (a[n * m / 2] + a[n * m / 2 - 1]) / 2
	}

	var r int
	for i := 0; i < n * m; i++ {
		r += abs(a[i] - mm)
	}

	fmt.Println(r/d)
}

package main

import (
	"fmt"
	"sort"
)

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

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

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

	for i := 1; i < n * m; i++ {
		if a[0] % d != a[i] % d {
			fmt.Println(-1)
			return
		}
	}

	sort.Ints(a)
	var mm int
	if (n * m) % 2 == 1 {
		mm = a[n * m / 2]
	} else {
		mm = (a[n * m / 2] + a[n * m / 2 - 1]) / 2
	}

	var r int
	for i := 0; i < n * m; i++ {
		r += abs(a[i] - mm)
	}

	fmt.Println(r/d)
}

package main

import (
	"fmt"
	"sort"
)

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

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

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

	for i := 1; i < n * m; i++ {
		if a[0] % d != a[i] % d {
			fmt.Println(-1)
			return
		}
	}

	sort.Ints(a)

	fmt.Println(a)

	var mm int
	if (n * m) % 2 == 1 {
		mm = a[n * m / 2]
	} else {
		mm = (a[n * m / 2] + a[n * m / 2 - 1]) / 2
	}

	var r int
	for i := 0; i < n * m; i++ {
		r += abs(a[i] - mm)
	}

	fmt.Println(r/d)
}


Bizon the Champion isn't just attentive, he also is very hardworking.Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of ai meters.Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.

The first line contains integer n (1 ≤ n ≤ 5000) — the number of fence planks. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Print a single integer — the minimum number of strokes needed to paint the whole fence.

NoteIn the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.In the third sample there is only one plank that can be painted using a single vertical stroke.

package main

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

func findMedian(slice []int, left int, right int) (int, int) {
	min := slice[left]
	median := left
	for i := left; i < right; i++ {
		if min >= slice[i] {
			min = slice[i]
			median = i
		}
	}
	return median, min
}

func solve(planks []int, left int, right int, painted int) int {
	median, min := findMedian(planks, left, right)
	currentBrush := min - painted

	var leftBrush, rightBrush int
	if median == left {
		leftBrush = 0
	} else {
		leftBrush = solve(planks, left, median, min)
	}

	if median == right-1 {
		rightBrush = 0
	} else {
		rightBrush = solve(planks, median+1, right, min)
	}

	if currentBrush+leftBrush+rightBrush >= right-left {
		return right - left
	}

	return currentBrush + leftBrush + rightBrush

}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	var n int
	if scanner.Scan() {
		line := scanner.Text()
		fmt.Sscanf(line, "%d", &n)
	}

	if scanner.Scan() {
		line := scanner.Text()

		tokens := strings.Fields(line)

		planks := make([]int, n)
		for i := 0; i < n; i++ {
			planks[i], _ = strconv.Atoi(tokens[i])
		}

		fmt.Println(solve(planks, 0, n, 0))
	}
}

package main

import (
    "fmt"
    "os"
    )

func main() {
	var fence = ReadInput()
	fmt.Print(Minimum(fence))
}

func ReadInput() []int {
	var n int
	fmt.Fscan(os.Stdin, &n)
	var fence = make([]int, n)
	for i := 0; i < n; i++ {
		var x int
		fmt.Fscan(os.Stdin, &x)
		fence[i] = x
	}

	return fence
}

func MinimumV2(fence []int) int {
	if len(fence) == 0 {
		return 0
	}

	if len(fence) == 1 {
		return 1
	}

	newFence := sub(fence, 1)

	// exclude zero
	segments := Split(newFence, 0, false)

	var horiz = 1
	for _, s := range segments {
		horiz += MinimumV2(s)
	}
	var vert = 1 + MinimumV2(fence[1:])

	return findMin(vert, horiz)
}

func sub(items []int, value int) []int {
	var ret = make([]int, len(items))
	for i, v := range items {
		ret[i] = v - value
	}

	return ret
}

// Split splits values by value into slice of integer slice
func Split(values []int, value int, copy bool) [][]int {
	var ret [][]int
	var left, right = -1, -1

	for i, v := range values {
		if v != value {
			if left == -1 {
				left = i
			}
			right = i + 1
			continue
		}

		if v == value { // should split
			if left != -1 && right != -1 {
				if copy {
					ret = append(ret, copySlice(values, left, right))
				} else {
					ret = append(ret, values[left:right])
				}

				// reset left, right
				left, right = -1, -1
			}
		}
	}

	// append the rest
	if left != -1 {
		if copy {
			ret = append(ret, copySlice(values, left, len(values)))
		} else {
			ret = append(ret, values[left:])
		}
	}

	return ret
}

func copySlice(values []int, left, right int) []int {
	var l = right - left
	if l < 0 {
		panic("right must greater than or equals left")
	}

	if l == 0 {
		return nil
	}

	if right > len(values) {
		panic("right greater size of values")
	}

	var ret = make([]int, right-left)
	copy(ret, values[left:right])
	return ret
}

func findMin(items ...int) int {
	if len(items) == 0 {
		return 0
	}
	var min = items[0]
	for _, v := range items {
		if min > v {
			min = v
		}
	}
	return min
}

func Minimum(fence []int) int {
	if len(fence) == 0 {
		return 0
	}

	if len(fence) == 1 {
		return 1
	}

	// find min
	var mins []int
	var min = fence[0]
	for i, v := range fence {
		if min > v {
			// change min
			min = v
			// reset mins
			mins = nil
		} else if min == v {
			mins = append(mins, i) // store index of min
		}
	}

	// how many vertical lines which is equal to min?
	segments := Split(fence, min, false)
	var ret = len(fence)
	var sum = min
	for _, s := range segments {
		sum += Minimum(sub(s, min))
	}

	return findMin(ret, sum)
}

package main

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

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

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

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

	fmt.Println(Strokes(a))
}

func Strokes(a []int) int {
	if len(a) == 0 {
		return 0
	}

	mini := a[0]
	for i := 0; i < len(a); i++ {
		mini = min(mini, a[i])
	}

	numStrokes := mini
	for i := 0; i < len(a); i++ {
		a[i] -= mini
	}
	for i := 0; i < len(a); i++ {
		if a[i] == 0 {
			continue
		}
		j := i
		for j < len(a) && a[j] > 0 {
			j++
		}
		numStrokes += Strokes(a[i:j])
		i = j
	}

	return min(len(a), numStrokes)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func f(a []int, low int) int {
	if len(a) == 0 {
		return 0
	}
	mi := int(1e9)
	for _, v := range a {
		mi = min(mi, v)
	}
	c, p := mi-low, 0
	for i, v := range a {
		if v == mi {
			c += f(a[p:i], mi)
			p = i + 1
		}
	}
	c += f(a[p:], mi)
	return min(c, len(a))
}

func main() {
	in := bufio.NewReader(os.Stdin)
	var n int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	Print(f(a, 0))
}

package main

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

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

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

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

package main

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

func findMedian(slice []int, left int, right int) (int, int) {
	min := slice[left]
	median := left
	for i := left; i < right; i++ {
		if min >= slice[i] {
			min = slice[i]
			median = i
		}
	}
	return median, min
}

func solve(planks []int, left int, right int, painted int) int {
	median, min := findMedian(planks, left, right)

	var leftBrush, rightBrush int
	currentBrush := min - painted
	if currentBrush >= right-left {
		return right - left
	}

	if median == left {
		leftBrush = 0
	} else {
		leftBrush = solve(planks, left, median, min)
	}

	if median == right-1 {
		rightBrush = 0
	} else {
		rightBrush = solve(planks, median+1, right, min)
	}

	return currentBrush + leftBrush + rightBrush

}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	var n int
	if scanner.Scan() {
		line := scanner.Text()
		fmt.Sscanf(line, "%d", &n)
	}

	if scanner.Scan() {
		line := scanner.Text()

		tokens := strings.Fields(line)

		planks := make([]int, n)
		for i := 0; i < n; i++ {
			planks[i], _ = strconv.Atoi(tokens[i])
		}

		fmt.Println(solve(planks, 0, n, 0))
	}
}

package main

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

func findMedian(slice []int, left int, right int) (int, int) {
	min := slice[left]
	median := left
	for i := left; i < right; i++ {
		if min >= slice[i] {
			min = slice[i]
			median = i
		}
	}
	return median, min
}

func solve(planks []int, left int, right int, painted int) int {
	median, min := findMedian(planks, left, right)

	var leftBrush, rightBrush int
	currentBrush := min - painted

	if currentBrush >= right-left {
		return right - left
	}

	if median == left {
		if planks[left]-painted > right-left {
			leftBrush = right - left
		} else {
			leftBrush = planks[left] - painted
		}
	} else {
		leftBrush = solve(planks, left, median, min)
	}

	if median == right-1 {
		rightBrush = 0
	} else {
		rightBrush = solve(planks, median+1, right, min)
	}

	// fmt.Printf("Left: %d, Right: %d, Median: %d, CurrentBrush: %d, LeftBrush: %d, RightBrush: %d\n",
	// 	left, right, median, currentBrush, leftBrush, rightBrush)
	return currentBrush + leftBrush + rightBrush

}

/**
1 3 3 2 2 3 3 2 1 1

+1

painted = 1, min = 1, currentBrush =
3 3 2 2 3 3 2 1 1

**/
func main() {
	scanner := bufio.NewScanner(os.Stdin)

	var n int
	if scanner.Scan() {
		line := scanner.Text()
		fmt.Sscanf(line, "%d", &n)
	}

	if scanner.Scan() {
		line := scanner.Text()

		tokens := strings.Fields(line)

		planks := make([]int, n)
		for i := 0; i < n; i++ {
			planks[i], _ = strconv.Atoi(tokens[i])
		}

		fmt.Println(solve(planks, 0, n, 0))
	}
}

package main

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

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

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

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

	fmt.Println(Strokes(a))
}

func Strokes(a []int) int {
	mini, count := a[0], 1
	for i := 0; i < len(a); i++ {
		if a[i] == mini {
			count++
		} else if a[i] < mini {
			mini, count = a[i], 1
		}
	}

	if mini >= count {
		return len(a)
	}

	numStrokes := mini
	for i := 0; i < len(a); i++ {
		a[i] -= mini
	}
	for i := 0; i < len(a); i++ {
		if a[i] == 0 {
			continue
		}
		j := i
		for j < len(a) && a[j] > 0 {
			j++
		}
		numStrokes += Strokes(a[i:j])
		i = j
	}

	return numStrokes
}

package main

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

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

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

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

	fmt.Println(Strokes(a))
}

func Strokes(a []int) int {
	mini, count := a[0], 1
	for i := 0; i < len(a); i++ {
		if a[i] == mini {
			count++
		} else if a[i] < mini {
			mini, count = a[i], 1
		}
	}

	if mini > count {
		return len(a)
	}

	numStrokes := mini
	for i := 0; i < len(a); i++ {
		a[i] -= mini
	}
	for i := 0; i < len(a); i++ {
		if a[i] == 0 {
			continue
		}
		j := i
		for j < len(a) && a[j] > 0 {
			j++
		}
		numStrokes += Strokes(a[i:j])
		i = j
	}

	return numStrokes
}

package main

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

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

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

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

	fmt.Println(Strokes(a))
}

func Strokes(a []int) int {
	if len(a) == 0 {
		return 0
	}

	mini, count := a[0], 1
	for i := 1; i < len(a); i++ {
		if a[i] == mini {
			count++
		} else if a[i] < mini {
			mini, count = a[i], 1
		}
	}

	if mini > count {
		return len(a)
	}

	numStrokes := mini
	for i := 0; i < len(a); i++ {
		a[i] -= mini
	}
	for i := 0; i < len(a); i++ {
		if a[i] == 0 {
			continue
		}
		j := i
		for j < len(a) && a[j] > 0 {
			j++
		}
		numStrokes += Strokes(a[i:j])
		i = j
	}

	return numStrokes
}


There are n cows playing poker at a table. For the current betting phase, each player's status is either "ALLIN", "IN", or "FOLDED", and does not change throughout the phase. To increase the suspense, a player whose current status is not "FOLDED" may show his/her hand to the table. However, so as not to affect any betting decisions, he/she may only do so if all other players have a status of either "ALLIN" or "FOLDED". The player's own status may be either "ALLIN" or "IN".Find the number of cows that can currently show their hands without affecting any betting decisions.

The first line contains a single integer, n (2 ≤ n ≤ 2·105). The second line contains n characters, each either "A", "I", or "F". The i-th character is "A" if the i-th player's status is "ALLIN", "I" if the i-th player's status is "IN", or "F" if the i-th player's status is "FOLDED".

The first line should contain a single integer denoting the number of players that can currently show their hands.

NoteIn the first sample, cows 1, 4, 5, and 6 can show their hands. In the second sample, only cow 3 can show her hand.

package main

import (
	"fmt"
)

var ps = fmt.Printf
var ss = fmt.Scanf

const mod int = 1000000007

func main() {
	a := 0
	status := false
	var s string
	ss("%d\n%s\n", &a, &s)
	A, I := 0, 0
	for i := 0; i < a; i++ {
		if s[i] == 'A' {
			A++
		} else if s[i] == 'I' {
			status = true
			I++
		}
	}
	if status == false {
		ps("%d\n", A)
	} else {
		if I == 1 {
			ps("1\n")
		} else {
			ps("0\n")
		}
	}
}

package main

import "fmt"
//import "bufio"

func main(){
	var n int
	fmt.Scan(&n)
	var sapi string
	fmt.Scan(&sapi)
	var a, i, f int = 0, 0, 0
	for j:=0; j<n; j++ {
		if string(sapi[j]) == "A" {
			a += 1
		} else if string(sapi[j]) == "I" {
			i += 1
		} else {
			f += 1
		}
	}
//	fmt.Println(a, i, f)
	if a > 0 && i < 1 {
		fmt.Println(a)
	} else if i > 0 && i <= 1 {
		fmt.Println(i)
	} else {
		fmt.Println(0)
	}
}

package main
import (
	"fmt"
	//"sort"
	"unicode/utf8"
)
func main() {
	var n int
	var s string
	a,b:=0,0
	fmt.Scan(&n,&s)
	len:=utf8.RuneCountInString(s)
	for i:=0;i<len;i++ {
		if s[i]=='A' {
			a++
		} else if s[i]=='I' {
			b++
		}
	}
	if b>1 {
		fmt.Println("0")
	} else if b==1 {
		fmt.Println("1")
	} else {
		fmt.Println(a)
	}

}

package main

import "fmt"

func main() {
    var n int
    var s string
    fmt.Scan(&n)
    fmt.Scan(&s)
    A, I := 0, 0
    for _, v := range s {
        if v == 'A' {
            A++
        } else if v == 'I' {
            I++
        }
    }
    if I == 0 { fmt.Println(A) } else { fmt.Println(1/I) }
}

// 284B
package main

import (
	"fmt"
)

func main() {
	var n int
	var ln string
	fmt.Scan(&n)
	fmt.Scan(&ln)
	A, I := 0, 0
	for _, v := range ln {
		if v == 'A' {
			A++
		} else if v == 'I' {
			I++
		}
	}
	if I == 0 {
		fmt.Println(A)
	} else {
		fmt.Println(1 / I)
	}
}

// 284B-mic
package main

import (
    "fmt"
)

func main() {
    var s string
    var n, k, c int
    l := 1
    v := 0
    fmt.Scan(&n)
    fmt.Scan(&s)
    for i := 0; i < n; i++ {
        if s[i] == 'A' {
            k++
        } else if s[i] == 'I' {
            c++
        }
    }
    if c == 0 {
        fmt.Printf("%d\n", k)
    } else if c == 1 {
        fmt.Printf("%d\n", l)
    } else {
        fmt.Printf("%d\n", v)
    }
}

package main

import (
	"fmt"
)

var ps = fmt.Printf
var ss = fmt.Scanf

const mod int = 1000000007

func main() {
	var s string
	ss("%s\n", &s)
	A := 0
	for i := 0; i < len(s); i++ {
		if s[i] == 'A' {
			A++
		}
	}
	ps("%d", A)
}

package main

import (
	"fmt"
)

var ps = fmt.Printf
var ss = fmt.Scanf

const mod int = 1000000007

func main() {
	 a:=0
	var s string
	ss("%d\n%s\n",&a, &s)
	A := 0
	for i := 0; i < a; i++ {
		if s[i] == 'A' {
			A++
		}
	}
	ps("%d", A)
}

package main

import "fmt"
//import "bufio"

func main(){
	var n int
	fmt.Scan(&n)
	var sapi string
	fmt.Scan(&sapi)
	var a, i, f int = 0, 0, 0
	for i:=0; i<n; i++ {
		if string(sapi[i]) == "A" {
			a += 1
		} else if string(sapi[i]) == "I" {
			i += 1
		} else {
			f += 1
		}
	}
	if a > 0 && i < 1 {
		fmt.Println(a)
	} else if a < 1 && i > 0 {
		fmt.Println(i)
	} else {
		fmt.Println(0)
	}
}

package main

import "fmt"

func main() {
    var n int
    var s string
    fmt.Scan(&n)
    fmt.Scan(&s)
    A, I := 0, 0
    for _, v := range s {
        if v == 'A' {
            A++
        } else if v == 'I' {
            I++
        }
    }
    if I == 0 { fmt.Println(A) } else { fmt.Println(1) }
}

// 284B-mic
package main

import (
	"fmt"
)

func main() {
	var s string
	var n, k, i int
	fmt.Scan(&n)
	fmt.Scan(&s)
	for i := 0; i < n; i++ {
		if s[i] == 'A' {
			k++
		} else if s[i] == 'I' {
			i++
		}
	}
	if i == 0 {
		fmt.Printf("%d\n", k)
	} else if i == 1 {
		fmt.Printf("1\n")
	} else {
		fmt.Printf("0\n")
	}
}


You are given a directed graph consisting of n vertices and m edges (each edge is directed, so it can be traversed in only one direction). You are allowed to remove at most one edge from it.Can you make this graph acyclic by removing at most one edge from it? A directed graph is called acyclic iff it doesn't contain any cycle (a non-empty path that starts and ends in the same vertex).

The first line contains two integers n and m (2 ≤ n ≤ 500, 1 ≤ m ≤ min(n(n - 1), 100000)) — the number of vertices and the number of edges, respectively. Then m lines follow. Each line contains two integers u and v denoting a directed edge going from vertex u to vertex v (1 ≤ u, v ≤ n, u ≠ v). Each ordered pair (u, v) is listed at most once (there is at most one directed edge from u to v).

If it is possible to make this graph acyclic by removing at most one edge, print YES. Otherwise, print NO.

NoteIn the first example you can remove edge , and the graph becomes acyclic.In the second example you have to remove at least two edges (for example,  and ) in order to make the graph acyclic.

package main

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

var graph [][]int
var parent []int
var visited []int
var ex_edge [] int

func searchCycle(u int) []int {
    visited[u] = 1
    for _, v := range graph[u] {
        if ex_edge != nil && u == ex_edge[0] && v == ex_edge[1] {
            continue
        }

        if visited[v] == 0 {
            parent[v] = u
            cycle := searchCycle(v)
            if cycle != nil {
                return cycle
            }
        } else if visited[v] == 1 {
            cycle := []int{v}
            for u != v {
                cycle = append(cycle, u)
                u = parent[u]
            }
            cycle = append(cycle, u)
            return cycle
        }
    }
    visited[u] = 2
    return nil
}

func solve() bool {
    var cycle []int
    for u := 0; u < len(graph) && cycle == nil; u++ {
        if visited[u] == 0 {
            cycle = searchCycle(u)
        }
    }
    if cycle == nil {
        return true
    }
    for i := 1; i < len(cycle); i++ {
        ex_edge = []int{cycle[i], cycle[i - 1]}
        for u, _ := range visited {
            visited[u] = 0
        }
        found_cycle := false
        for u := 0; u < len(graph) && !found_cycle; u++ {
            if visited[u] == 0 && searchCycle(u) != nil {
                found_cycle = true
            }
        }
        if !found_cycle {
            return true
        }
    }
    return false
}

func main() {
    var n, m int
    input := bufio.NewReader(os.Stdin)
    fmt.Fscan(input, &n, &m)
    graph = make([][]int, n)
    parent = make([]int, n)
    visited = make([]int, n)
    for i := 0; i < m; i++ {
        var u, v int
        fmt.Fscan(input, &u, &v)
        graph[u - 1] = append(graph[u - 1], v - 1)
    }

    if solve() {
        fmt.Println("YES")
    } else {
        fmt.Println("NO")
    }
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF915D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, m, v, w int
	Fscan(in, &n, &m)
	g := make([][]int, n)
	dd := make([]int, n)
	for i := 0; i < m; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		dd[w]++
	}

	for i, d := range dd {
		if d == 0 {
			continue
		}
		deg := make([]int, n)
		copy(deg, dd)
		deg[i]--
		q := []int{}
		for v, d := range deg {
			if d == 0 {
				q = append(q, v)
			}
		}
		c := 0
		for len(q) > 0 {
			v, q = q[0], q[1:]
			c++
			for _, w := range g[v] {
				deg[w]--
				if deg[w] == 0 {
					q = append(q, w)
				}
			}
		}
		if c == n {
			Fprint(out, "YES")
			return
		}
	}
	Fprint(out, "NO")
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF915D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, m, v, w int
	Fscan(in, &n, &m)
	g := make([][]int, n)
	dd := make([]int, n)
	for i := 0; i < m; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		dd[w]++
	}

	deg := make([]int, n)
	for i, d := range dd {
		if d == 0 {
			continue
		}
		copy(deg, dd)
		deg[i]--
		q := []int{}
		for v, d := range deg {
			if d == 0 {
				q = append(q, v)
			}
		}
		c := 0
		for len(q) > 0 {
			v, q = q[0], q[1:]
			c++
			for _, w := range g[v] {
				deg[w]--
				if deg[w] == 0 {
					q = append(q, w)
				}
			}
		}
		if c == n {
			Fprint(out, "YES")
			return
		}
	}
	Fprint(out, "NO")
}

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

package main

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

var graph [][]int
var parent []int
var visited []int
var ex_edge [] int

func searchCycle(u int) []int {
    visited[u] = 1
    for _, v := range graph[u] {
        if ex_edge != nil && u == ex_edge[0] && v == ex_edge[1] {
            continue
        }

        if visited[v] == 0 {
            parent[v] = u
            cycle := searchCycle(v)
            if cycle != nil {
                return cycle
            }
        } else if visited[v] == 1 {
            cycle := []int{v}
            for u != v {
                cycle = append(cycle, u)
                u = parent[u]
            }
            cycle = append(cycle, u)
            return cycle
        }
    }
    visited[u] = 2
    return nil
}

func solve() bool {
    var cycle []int
    for u := 0; u < len(graph) && cycle == nil; u++ {
        if visited[u] == 0 {
            cycle = searchCycle(u)
        }
    }
    if cycle == nil {
        return true
    }
    for i := 1; i < len(cycle); i++ {
        ex_edge = []int{cycle[i], cycle[i - 1]}
        for u, _ := range visited {
            visited[u] = 0
        }
        cycle = nil
        for u := 0; u < len(graph) && cycle == nil; u++ {
            if visited[u] == 0 {
                cycle = searchCycle(u)
            }
        }
        if cycle == nil {
            return true
        }
    }
    return false
}

func main() {
    var n, m int
    input := bufio.NewReader(os.Stdin)
    fmt.Fscan(input, &n, &m)
    graph = make([][]int, n)
    parent = make([]int, n)
    visited = make([]int, n)
    for i := 0; i < m; i++ {
        var u, v int
        fmt.Fscan(input, &u, &v)
        graph[u - 1] = append(graph[u - 1], v - 1)
    }

    if solve() {
        fmt.Println("YES")
    } else {
        fmt.Println("NO")
    }
}