Maxim always goes to the supermarket on Sundays. Today the supermarket has a special offer of discount systems.There are m types of discounts. We assume that the discounts are indexed from 1 to m. To use the discount number i, the customer takes a special basket, where he puts exactly qi items he buys. Under the terms of the discount system, in addition to the items in the cart the customer can receive at most two items from the supermarket for free. The number of the "free items" (0, 1 or 2) to give is selected by the customer. The only condition imposed on the selected "free items" is as follows: each of them mustn't be more expensive than the cheapest item out of the qi items in the cart.Maxim now needs to buy n items in the shop. Count the minimum sum of money that Maxim needs to buy them, if he use the discount system optimally well.Please assume that the supermarket has enough carts for any actions. Maxim can use the same discount multiple times. Of course, Maxim can buy items without any discounts.

The first line contains integer m (1 ≤ m ≤ 105) — the number of discount types. The second line contains m integers: q1, q2, ..., qm (1 ≤ qi ≤ 105).  The third line contains integer n (1 ≤ n ≤ 105) — the number of items Maxim needs. The fourth line contains n integers: a1, a2, ..., an (1 ≤ ai ≤ 104) — the items' prices. The numbers in the lines are separated by single spaces.

In a single line print a single integer — the answer to the problem.

NoteIn the first sample Maxim needs to buy two items that cost 100 and get a discount for two free items that cost 50. In that case, Maxim is going to pay 200.In the second sample the best strategy for Maxim is to buy 3 items and get 2 items for free using the discount. In that case, Maxim is going to pay 150.

package main

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

func main(){
	var m, n int
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	r.Scan()
	m, _ = strconv.Atoi(r.Text())
	q := make([]int, m)
	for i:=0; i<m; i++ {
		r.Scan()
		q[i], _ = strconv.Atoi(r.Text())
	}
	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())
	}
	sum := 0
	sort.Ints(q)
	sort.Ints(a)
	for i:=0; i<n; i++ {
		if i % (q[0]+2) < q[0] {
			sum += a[n-i-1]
		}
	}
	w.WriteString(strconv.Itoa(sum)+"\n")
	w.Flush()
}

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

func main() {
	var m,n int
	input := bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)
	fmt.Scan(&m)
	q:=make([]int,m)
	for i:=0;i<m;i++ {
		input.Scan()
		aa,_:=strconv.Atoi(input.Text())
		q[i]=aa
	}
	input.Scan()
	ab,_:=strconv.Atoi(input.Text())
	n=ab
	a:=make([]int,n)
	for i:=0;i<n;i++ {
		input.Scan()
		ac,_:=strconv.Atoi(input.Text())
		a[i]=ac
	}
	sort.Ints(q)
	sort.Ints(a)
	goo:=q[0]
	coo:=0
	sum:=0
	i:=n-1
	for ;i>=0;i-- {
		sum+=a[i]
		coo++
		if coo==goo {
			i-=2
			coo=0
		}
	}
	fmt.Println(sum)
}

package main

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

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

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    var m,n,q int
    ScanInts(&m)
    min := 1000000
    for i := 0; i < m; i++ {
        ScanInts(&q)
        if min > q { min = q }
    }
    ScanInts(&n)
    a := make([]int,n)
    for i := 0; i < n; i++ {
        ScanInts(&a[i])
    }
    sort.Ints(a)
    ans := 0
    for i := len(a)-1; i >= 0; i-=2 {
        for j := 0; (j < min) && (i >= 0); j++ {
            ans += a[i]
            i--
        }
    }
    fmt.Println(ans)
}

// 261A-mic
package main

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

func main() {
	var q, n, m int
	mnq := 1000000
	z := bufio.NewReader(os.Stdin)
	amount := 0
	fmt.Scan(&m)
	for i := 1; i <= m; i++ {
		fmt.Fscan(z, &q)
		mnq = int(math.Min(float64(mnq), float64(q)))
	}
	fmt.Fscan(z, &n)
	a := make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(z, &a[i])
	}
	sort.Sort(sort.IntSlice(a))
	f := n
	for {
		for i := f; i > int(math.Max(float64(f-mnq), 0.0)); i-- {
			amount += a[i]
		}

		f -= mnq + 2
		if f < 1 {
			break
		}
	}
	fmt.Println(amount)
}

package main

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

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

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    var m,n,q int
    ScanInts(&m)
    min := 1000000
    for i := 0; i < m; i++ {
        ScanInts(&q)
        if min > q { min = q }
    }
    ScanInts(&n)
    a := make([]int,n)
    for i := 0; i < n; i++ {
        ScanInts(&a[i])
    }
    sort.Ints(a)
    ans := 0
    l, r := 0, len(a)-1
    for l <= r {
        for i := 0; (i < min) && (l <= r); i++ {
            ans += a[r]
            r--
        }
        l += 2
    }
    fmt.Println(ans)
}

package main

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

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

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    var m,n,q int
    ScanInts(&m)
    min := 1000000
    for i := 0; i < m; i++ {
        ScanInts(&q)
        if min > q { min = q }
    }
    ScanInts(&n)
    a := make([]int,n)
    for i := 0; i < n; i++ {
        ScanInts(&a[i])
    }
    sort.Ints(a)
    ans := 0
    l, r := len(a)%(min+2), len(a)-1
    for i := 0; i < l; i++ { ans += a[l] }
    for l <= r {
        for i := 0; (i < min) && (l <= r); i++ {
            ans += a[r]
            r--
        }
        l += 2
    }
    fmt.Println(ans)
}


Polycarp has prepared $$$n$$$ competitive programming problems. The topic of the $$$i$$$-th problem is $$$a_i$$$, and some problems' topics may coincide.Polycarp has to host several thematic contests. All problems in each contest should have the same topic, and all contests should have pairwise distinct topics. He may not use all the problems. It is possible that there are no contests for some topics.Polycarp wants to host competitions on consecutive days, one contest per day. Polycarp wants to host a set of contests in such a way that:  number of problems in each contest is exactly twice as much as in the previous contest (one day ago), the first contest can contain arbitrary number of problems;  the total number of problems in all the contests should be maximized. Your task is to calculate the maximum number of problems in the set of thematic contests. Note, that you should not maximize the number of contests.

The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of problems Polycarp has prepared. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) where $$$a_i$$$ is the topic of the $$$i$$$-th problem.

Print one integer — the maximum number of problems in the set of thematic contests.

NoteIn the first example the optimal sequence of contests is: $$$2$$$ problems of the topic $$$1$$$, $$$4$$$ problems of the topic $$$2$$$, $$$8$$$ problems of the topic $$$10$$$.In the second example the optimal sequence of contests is: $$$3$$$ problems of the topic $$$3$$$, $$$6$$$ problems of the topic $$$6$$$.In the third example you can take all the problems with the topic $$$1337$$$ (the number of such problems is $$$3$$$ so the answer is $$$3$$$) and host a single contest.

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)
func main() {
	buf := bufio.NewReader(os.Stdin)

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

	sort.Ints(mat)
	var cnt int = 0
	var cntmat []int = make([]int,0)
	for i := 0 ;i < n;i++{
		cnt++
		if i + 1 >= n || mat[i]!=mat[i+1]{
			cntmat = append(cntmat,cnt)
			cnt = 0
		}
	}
	sort.Ints(cntmat)
	var ans int = 0
	var tmp int = 0
	var len int = len(cntmat)
	for i := cntmat[len-1];i > 0;i--{
		tmp = i;
		var term int = i
		for j := len-2;(term&1)==0&&j >= 0;j--{
			term >>= 1
			if cntmat[j] < term{
				break
			}
			tmp += term
		}
		if tmp > ans{
			ans = tmp
		}
	}
	fmt.Print(ans)
}

package main

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

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

	var n, v, ans int
	Fscan(in, &n)
	cntMp := map[int]int{}
	for i := 0; i < n; i++ {
		Fscan(in, &v)
		cntMp[v]++
	}
	cnts := make([]int, 0, len(cntMp))
	for _, c := range cntMp {
		cnts = append(cnts, c)
	}
	sort.Ints(cnts)
	for st := 1; st <= n; st++ {
		i := 0
		for v = st; v <= n; v <<= 1 {
			i += sort.SearchInts(cnts[i:], v) + 1
			if i > len(cnts) {
				break
			}
		}
		if sum := v - st; sum > ans {
			ans = sum
		}
	}
	Fprint(out, ans)
}

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

package main

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

var n int
var problems map[int]int
var a []int

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &n)
	problems = make(map[int]int)
	for i := 0; i < n; i++ {
		var tmp int
		fmt.Fscanf(buf, "%d", &tmp)
		problems[tmp]++
	}

	for key := range problems {
		a = append(a, problems[key])
	}

	sort.Ints(a)

	best := 0
	for i := 0; i < len(a); i++ {
		for j := 1; j <= a[i]; j++ {
			curr := 0
			val := j
			startAt := i + 1
			for {
				curr += val
				next := 2 * val
				ind2 := sort.Search(len(a) - startAt, func (ind int) bool {
					return a[startAt + ind] >= next
				})
				if ind2 == len(a) - startAt {
					break
				}
				val = next
				startAt = startAt + ind2 + 1
			}
			if curr > best {
				best = curr
			}
		}
	}

	fmt.Println(best)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)
func main() {
	buf := bufio.NewReader(os.Stdin)

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

	var cnt int = 0
	if n < 100 {
		sort.Ints(mat)
		for i := 0; i < n; i++ {
			if i+1 >= n || mat[i] != mat[i+1] {
				cnt++
			}
		}
	} else {
		fmt.Printf("so slow!!!\n")
		return
	}
	var cntmat []int = make([]int,cnt)
	cnt = 0
	for i := 0 ;i < n;i++{
		cnt++
		if i + 1 >= n || mat[i]!=mat[i+1]{
			cntmat = append(cntmat,cnt)
			cnt = 0
		}
	}
	sort.Ints(cntmat)
	var ans int = 0
	var tmp int = 0
	var len int = len(cntmat)
	if n > 100{
		fmt.Printf("maxcnt=%d len=%d\n",cntmat[len-1],len)
		return
	}
	for i := cntmat[len-1];i > 0;i--{
		tmp = i;
		var term int = i
		for j := len-2;(term&1)==0&&j >= 0;j--{
			term >>= 1
			if cntmat[j] < term{
				break
			}
			tmp += term
		}
		if tmp > ans{
			ans = tmp
		}
	}
	fmt.Print(ans)
}


You are given an array $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.Your problem is to find such pair of indices $$$i, j$$$ ($$$1 \le i < j \le n$$$) that $$$lcm(a_i, a_j)$$$ is minimum possible.$$$lcm(x, y)$$$ is the least common multiple of $$$x$$$ and $$$y$$$ (minimum positive number such that both $$$x$$$ and $$$y$$$ are divisors of this number).

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

Print two integers $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) such that the value of $$$lcm(a_i, a_j)$$$ is minimum among all valid pairs $$$i, j$$$. If there are multiple answers, you can print any.

package main

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

type point struct {
	w1, w2 int
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanf(buf, "%d\n", &n)
	var v int
	mapp := make([]int,1e7+2)
	var maxv int
	mat := make([]int,n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(buf, "%d", &v)
		mat[i] = v
		mapp[v]++
		if maxv < v {
			maxv = v
		}
	}
	fmt.Fscanf(buf, "\n")
	var minlcm int64
	var ansx, ansy,pre int
	var ans1,ans2 int
	ansx = 0
	ansy = 0
	for i := 1; i <= maxv; i++ {
		pre = 0
		for j := i; j <= maxv; j += i {
			if mapp[j] > 0 {
			//	fmt.Printf("%d %d %d\n",item.w1,item.w2,j)
				if pre > 0 {
					if minlcm == 0 || minlcm > int64(j/i)*int64(pre) {
						minlcm = int64(j/i) * int64(pre)
						ans1 = pre
						ans2 = j
					}
					break
				} else if mapp[j] > 1 {
					if minlcm == 0 || minlcm > int64(j) {
						minlcm = int64(j)
						ans1 = j
						ans2 = ans1
					}
					break
				}
				pre = j
			}
		}
	}
	for i:= 1;i <= n;i++{
		if mat[i] == ans1||ans2 == mat[i]{
			if ansx==0{
				ansx = i
				if mat[i]==ans1{
					ans1 = -1
				}else{
					ans2 = -1
				}
			}else {
				ansy = i
				break
			}
		}
	}
//	fmt.Printf("%d\n",minlcm)
	if ansx < ansy {
		fmt.Printf("%d %d\n", ansx, ansy)
	} else {
		fmt.Printf("%d %d\n", ansy, ansx)
	}

}

package main

import (
	. "fmt"
	"io"
	"os"
	"runtime/debug"
)

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

	id := [1e7 + 1]int{}
	min, x, y := int64(1e18), 0, 0
	n := r()
	for i := 1; i <= n; i++ {
		if v := r(); id[v] == 0 {
			id[v] = i
		} else if int64(v) < min {
			min, x, y = int64(v), id[v], i
		}
	}
	for i := 1; i <= 1e7; i++ {
		v := 0
		for j := i; j <= 1e7; j += i {
			if id[j] > 0 {
				if v > 0 {
					if l := int64(v) * int64(j) / int64(i); l < min {
						min, x, y = l, id[v], id[j]
					}
					break
				}
				v = j
			}
		}
	}
	if x > y {
		x, y = y, x
	}
	Fprint(out, x, y)
}

func main() {
	debug.SetGCPercent(-1)
	CF1154G(os.Stdin, os.Stdout)
}

package main

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

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

	id := [1e7 + 1]int{}
	min, x, y := int64(1e18), 0, 0
	n := r()
	for i := 1; i <= n; i++ {
		if v := r(); id[v] == 0 {
			id[v] = i
		} else if int64(v) < min {
			min, x, y = int64(v), id[v], i
		}
	}
	for i := 1; i <= 1e7; i++ {
		v := 0
		for j := i; j <= 1e7; j += i {
			if id[j] > 0 {
				if v > 0 {
					if l := int64(v) * int64(j) / int64(i); l < min {
						min, x, y = l, id[v], id[j]
					}
					break
				}
				v = j
			}
		}
	}
	if x > y {
		x, y = y, x
	}
	Fprint(out, x, y)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1154G(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	id := [1e7 + 1]int{}
	min := int64(1e18)
	var n, v, x, y int
	Fscan(in, &n)
	for i := 1; i <= n; i++ {
		Fscan(in, &v)
		if id[v] == 0 {
			id[v] = i
		} else if int64(v) < min {
			min, x, y = int64(v), id[v], i
		}
	}
	for i := 1; i <= 1e7; i++ {
		v = 0
		for j := i; j <= 1e7; j += i {
			if id[j] > 0 {
				if v > 0 {
					if l := int64(v) * int64(j) / int64(i); l < min {
						min, x, y = l, id[v], id[j]
					}
					break
				}
				v = j
			}
		}
	}
	if x > y {
		x, y = y, x
	}
	Fprint(out, x, y)
}

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

package main

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

type point struct {
	w1, w2 int
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanf(buf, "%d\n", &n)
	var v int
	mapp := make(map[int]point)
	var maxv int
	for i := 1; i <= n; i++ {
		fmt.Fscanf(buf, "%d", &v)
		item, ok := mapp[v]
		if ok {
			item.w2 = i
		} else {
			mapp[v] = point{
				w1: i,
			}
		}
		if maxv < v {
			maxv = v
		}
	}
	fmt.Fscanf(buf, "\n")
	var minlcm, pre int64
	var ansx, ansy, prew int
	ansx = 0
	ansy = 0
	for i := 1; i <= maxv; i++ {
		pre = 0
		for j := i; j <= maxv; j += i {
			if item, ok := mapp[j]; ok {
				if pre > 0 {
					if minlcm == 0 || minlcm > int64(j/i)*pre {
						minlcm = int64(j/i) * int64(pre)
						ansx = prew
						ansy = item.w1
						break
					}
				} else if item.w2 > 0 {
					if minlcm == 0 || minlcm > int64(j) {
						minlcm = int64(j)
						ansx = item.w1
						ansy = item.w2
						break
					}
				}
				pre = int64(j)
				prew = item.w1
			}
		}
	}
	if ansx < ansy{
		fmt.Printf("%d %d\n",ansx,ansy)
	}else{
		fmt.Printf("%d %d\n",ansy,ansx)
	}

}

package main

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

type point struct {
	w1, w2 int
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanf(buf, "%d\n", &n)
	var v int
	mapp := make(map[int]point)
	var maxv int
	for i := 1; i <= n; i++ {
		fmt.Fscanf(buf, "%d", &v)
		item, ok := mapp[v]
		if ok {
			item.w2 = i
		} else {
			mapp[v] = point{
				w1: i,
			}
		}
		if maxv < v {
			maxv = v
		}
	}
	fmt.Fscanf(buf, "\n")
	var minlcm, pre int64
	var ansx, ansy, prew int
	ansx = 0
	ansy = 0
	for i := 1; i <= maxv; i++ {
		pre = 0
		for j := i; j <= maxv; j += i {
			if item, ok := mapp[j]; ok {
				if pre > 0 {
					if minlcm == 0 || minlcm > int64(j/i)*pre {
						minlcm = int64(j/i) * int64(pre)
						ansx = prew
						ansy = item.w1
						break
					}
				} else if item.w2 > 0 {
					if minlcm == 0 || minlcm > int64(j) {
						minlcm = int64(j)
						ansx = item.w1
						ansy = item.w2
						break
					}
				}
				pre = int64(j)
				prew = item.w1
			}
		}
	}
	fmt.Printf("%d %d\n",ansx,ansy)

}


You've got an n × m matrix. The matrix consists of integers. In one move, you can apply a single transformation to the matrix: choose an arbitrary element of the matrix and increase it by 1. Each element can be increased an arbitrary number of times.You are really curious about prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors: itself and number one. For example, numbers 2, 3, 5 are prime and numbers 1, 4, 6 are not. A matrix is prime if at least one of the two following conditions fulfills:  the matrix has a row with prime numbers only;  the matrix has a column with prime numbers only; Your task is to count the minimum number of moves needed to get a prime matrix from the one you've got.

The first line contains two integers n, m (1 ≤ n, m ≤ 500) — the number of rows and columns in the matrix, correspondingly. Each of the following n lines contains m integers — the initial matrix. All matrix elements are positive integers. All numbers in the initial matrix do not exceed 105. The numbers in the lines are separated by single spaces.

Print a single integer — the minimum number of moves needed to get a prime matrix from the one you've got. If you've got a prime matrix, print 0.

NoteIn the first sample you need to increase number 1 in cell (1, 1). Thus, the first row will consist of prime numbers: 2, 2, 3.In the second sample you need to increase number 8 in cell (1, 2) three times. Thus, the second column will consist of prime numbers: 11, 2.In the third sample you don't have to do anything as the second column already consists of prime numbers: 3, 2.

package main

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

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

func main(){
	var n, m int
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	var p [100050]int
	var a [501][501]int
	ans := 1000000000
	temp := 0
	p[1] = 1
	for i:=2; i<100050; i++ {
		if p[i] == 0 {
			for j:=i+i; j<100050; j+=i {
				p[j] = 1
			}
		}
	}
	r.Scan()
	n, _ = strconv.Atoi(r.Text())
	r.Scan()
	m, _ = strconv.Atoi(r.Text())
	for i:=0; i<n; i++ {
		for j:=0; j<m; j++ {
			r.Scan()
			temp, _ = strconv.Atoi(r.Text())
			k := 0
			for p[temp+k] == 1 {
				k += 1
			}
			a[i][j] = k
		}
	}
	for i:=0; i<n; i++ {
		s := 0
		for j:=0; j<m; j++ {
			s += a[i][j]
		}
		ans = min(ans, s)
	}
	for j:=0; j<m; j++ {
		s := 0
		for i:=0; i<n; i++ {
			s += a[i][j]
		}
		ans = min(ans, s)
	}
	w.WriteString(strconv.Itoa(ans)+"\n")
	w.Flush()
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    f := make([]bool,100500)
    prime := make([]int,0,10000)
    for i := 2; i <= 100100; i++ {
        if !f[i] {
            prime = append(prime,i)
            a := int64(i)*int64(i)
            for a <= 100100 {
                f[a] = true
                a += int64(i)
            }
        }
    }
    var n,m int
    fmt.Scan(&n,&m)
    rows, cols := make([]int,n), make([]int,m)
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            scanner.Scan()
            a, _ := strconv.Atoi(scanner.Text())
            l, r := 0, len(prime)
            for (r-l) > 1 {
                m := (l+r)/2
                if a < prime[m] {
                    r = m
                } else {
                    l = m
                }
            }
            if a > prime[l] { l++ }
            rows[i] += prime[l]-a
            cols[j] += prime[l]-a
        }
    }
    ans := 1000000000
    for i := 0; i < n; i++ {
        if ans > rows[i] { ans = rows[i] }
    }
    for i := 0; i < m; i++ {
        if ans > cols[i] { ans = cols[i] }
    }
    fmt.Println(ans)
}

package main

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

func main() {
	// file, _ := os.Open("input")
	file := os.Stdin
	reader := bufio.NewReader(file)
	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	a := make([][]int, n)
	count := make([][]int, n)

	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		count[i] = make([]int, m)
	}

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

	// Create Eratos Primes
	size := 200000
	eratos := make([]bool, size)
	for i := 0; i < size; i++ {
		eratos[i] = true
	}

	for i := 2; i < size; i++ {
		if eratos[i] {
			for j := 2; j*i < size; j++ {
				eratos[i*j] = false
			}
		}
	}

	var primes []int
	for i := 2; i < size; i++ {
		if eratos[i] {
			primes = append(primes, i)
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			nextPrimePos := sort.SearchInts(primes, a[i][j])
			if primes[nextPrimePos] != a[i][j] {
				count[i][j] = primes[nextPrimePos] - a[i][j]
			} else {
				count[i][j] = 0
			}
		}
	}

	result := math.MaxInt32
	for i := 0; i < n; i++ {
		counter := 0
		for j := 0; j < m; j++ {
			counter += count[i][j]
		}

		result = min(result, counter)
	}

	for j := 0; j < m; j++ {
		counter := 0
		for i := 0; i < n; i++ {
			counter += count[i][j]
		}

		result = min(result, counter)
	}

	fmt.Print(result)
}

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

	return b
}

// 271B-mic_rizal
package main

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

func main() {
    var num, a [1000010]int
    z := bufio.NewReader(os.Stdin)
    var i, j, n, m, v int
    l := 100005
    var x [505][505]int
    for i = 2; i <= l; i++ {
        a[i] = 0
    }
    i = 2
    cou := 0
    for i <= l {
        if a[i] == 0 {
            num[i] = 1
        }
        j = 2
        for i*j <= l {
            a[i*j] = 1
            j++
        }
        i++
    }
    num[1] = 0
    fmt.Scan(&n, &m)
    min := 10000007
    for i = 0; i < n; i++ {
        for j = 0; j < m; j++ {
            fmt.Fscan(z, &x[i][j])
        }
    }
    for i = 0; i < n; i++ {
        v = 0
        for j = 0; j < m; j++ {
            if num[x[i][j]] == 0 {
                cou = x[i][j]
                for num[cou] == 0 {
                    cou++
                    v++
                }
            }
        }
        min = int(math.Min(float64(min), float64(v)))
    }
    for i = 0; i < m; i++ {
        v = 0
        for j = 0; j < n; j++ {
            if num[x[j][i]] == 0 {
                cou = x[j][i]
                for num[cou] == 0 {
                    cou++
                    v++
                }
            }
        }
        min = int(math.Min(float64(min), float64(v)))
    }
    fmt.Println(min)
}

package main

import "fmt"

func main() {
    f := make([]bool,100500)
    prime := make([]int,0,10000)
    for i := 2; i <= 100100; i++ {
        if !f[i] {
            prime = append(prime,i)
            a := int64(i)*int64(i)
            for a <= 100100 {
                f[a] = true
                a += int64(i)
            }
        }
    }
    var n,m,a int
    fmt.Scan(&n,&m)
    //rows, cols := make([]int,n), make([]int,m)
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            fmt.Scan(&a)
            /*
            l := 0
            for ; (l < len(prime)) && (a > prime[l]); l++ {
            }
            /*
            l, r := 0, len(prime)
            for (r-l) > 1 {
                m := (l+r)/2
                if a < prime[m] {
                    r = m
                } else {
                    l = m
                }
            }
            if a > prime[l] { l++ }
            */
    //        rows[i] += a
    //        cols[j] += a
        }
    }
    ans := 1000000000
    /*
    for i := 0; i < n; i++ {
        if ans > rows[i] { ans = rows[i] }
    }
    for i := 0; i < m; i++ {
        if ans > cols[i] { ans = cols[i] }
    }
    */
    fmt.Println(ans)
}


A string $$$s$$$ of length $$$n$$$ ($$$1 \le n \le 26$$$) is called alphabetical if it can be obtained using the following algorithm:  first, write an empty string to $$$s$$$ (i.e. perform the assignment $$$s$$$ := "");  then perform the next step $$$n$$$ times;  at the $$$i$$$-th step take $$$i$$$-th lowercase letter of the Latin alphabet and write it either to the left of the string $$$s$$$ or to the right of the string $$$s$$$ (i.e. perform the assignment $$$s$$$ := $$$c+s$$$ or $$$s$$$ := $$$s+c$$$, where $$$c$$$ is the $$$i$$$-th letter of the Latin alphabet). In other words, iterate over the $$$n$$$ first letters of the Latin alphabet starting from 'a' and etc. Each time we prepend a letter to the left of the string $$$s$$$ or append a letter to the right of the string $$$s$$$. Strings that can be obtained in that way are alphabetical.For example, the following strings are alphabetical: "a", "ba", "ab", "bac" and "ihfcbadeg". The following strings are not alphabetical: "z", "aa", "ca", "acb", "xyz" and "ddcba".From the given string, determine if it is alphabetical.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case is written on a separate line that contains one string $$$s$$$. String $$$s$$$ consists of lowercase letters of the Latin alphabet and has a length between $$$1$$$ and $$$26$$$, inclusive.

Output $$$t$$$ lines, each of them must contain the answer to the corresponding test case. Output YES if the given string $$$s$$$ is alphabetical and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive answer).

11
a
ba
ab
bac
ihfcbadeg
z
aa
ca
acb
xyz
ddcba

NoteThe example contains test cases from the main part of the condition.

package main

import "fmt"

// 代码相当易懂，不多说
func algo(s string) bool {
	length := len(s)
	for i := length - 1; i >= 0; i-- {
		expected_letter := byte('a' + i)
		if s[i] == expected_letter {
			s = s[:i]
		} else if s[0] == expected_letter {
			s = s[1:]
		} else {
			return false
		}
	}
	return true
}

func main() {
	num_case := 0
	fmt.Scan(&num_case)

	for i := 0; i < num_case; i++ {
		var s string
		fmt.Scan(&s)
		if algo(s) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

func algo(s string) bool {
	length := len(s)
	for i := length - 1; i >= 0; i-- {
		expected_char := byte('a' + i)
		if s[i] == expected_char {
			s = s[:i]
		} else if s[0] == expected_char {
			s = s[1:]
		} else {
			return false
		}
	}
	return true
	// i, j := 0, len(s)-1

	// if j == 0 {
	// 	return s[j] == 'a'
	// }

	// for i < j {
	// 	expected_char := byte('a' + (j - i))
	// 	if s[i] < s[j] {
	// 		if s[j] != expected_char {
	// 			return false
	// 		}
	// 		if int(s[j]-s[j-1]) == 1 {
	// 			j -= 2
	// 		} else if int(s[j]-s[i]) == 1 {
	// 			i++
	// 			j--
	// 		} else {
	// 			return false
	// 		}
	// 	} else if s[i] > s[j] {
	// 		if s[i] != expected_char {
	// 			return false
	// 		}
	// 		if int(s[i]-s[i+1]) == 1 {
	// 			i += 2
	// 		} else if int(s[i]-s[j]) == 1 {
	// 			i++
	// 			j--
	// 		} else {
	// 			return false
	// 		}
	// 	} else {
	// 		return false
	// 	}
	// }

	// return true
}

func input() []string {
	num_case := 0
	fmt.Scan(&num_case)
	testcases := make([]string, num_case)

	for i := 0; i < num_case; i++ {
		fmt.Scan(&testcases[i])
	}

	return testcases
}

func main() {
	strs := input()

	for _, s := range strs {
		if algo(s) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

/*
 * Created by Reverie
 * 2021.07.11
 * 02:23:13
 */

package main

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

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

	solve := func(Case int) {
		var s string
		Fscan(in, &s)

		n := len(s)
		isValid := true
		for i := n - 1; i >= 0; i-- {
			if s[0] == 'a'+byte(i) {
				s = s[1:]
			} else if s[i] == 'a'+byte(i) {
				s = s[:i]
			} else {
				isValid = false
				break
			}
		}

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

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

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

package main

import (
	"fmt"
)

type Point struct {
	x, y int
}

func main() {
	T := 0
	fmt.Scanf("%d\n", &T)
	for ; T > 0; T-- {
		var s string
		fmt.Scanf("%s\n", &s)

		n := len(s)
		isValid := true
		for i := n - 1; i >= 0; i-- {
			if s[0] == 'a'+byte(i) {
				s = s[1:]
			} else if s[i] == 'a'+byte(i) {
				s = s[:i]
			} else {
				isValid = false
				break
			}
		}

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

package main

import (
	"fmt"
)

func solve() {

	var s string
	fmt.Scan(&s)
	n := len(s)
	left := 0
	right := n - 1
	var max byte
	if s[left] > s[right] {
		max = s[left]
	} else {
		max = s[right]
	}
	if int(max-'a') != (n - 1) {
		fmt.Println("NO")
		return
	}
	for left <= right {
		if s[left] == max {
			max--
			left++
		} else if s[right] == max {
			max--
			right--
		} else {
			fmt.Println("NO")
			return
		}
	}
	fmt.Println("YES")

}

func main() {
	var t int
	fmt.Scan(&t)
	for t > 0 {
		solve()
		t--
	}

}

package main

import (
	"fmt"
)

func solve() {

	var s string
	fmt.Scan(&s)
	in := -1
	for i := range s {
		if s[i] == 'a' {
			in = i
			break
		}
	}
	if in == -1 {
		fmt.Println("NO")
	} else {
		left := in - 1
		right := in + 1
		n := len(s)
		var ch byte = 1
		for left >= 0 && right < n {
			if s[left]-'a' == ch {
				left--
				ch++
			} else if s[right]-'a' == ch {
				right++
				ch++
			} else {
				break
			}
		}
		for left >= 0 {
			if s[left]-'a' == ch {
				left--
				ch++
			} else {
				break
			}
		}
		for right < n {
			if s[right]-'a' == ch {
				right++
				ch++
			} else {
				break
			}
		}
		if left == -1 && right == n {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func main() {
	var t int
	fmt.Scan(&t)
	for t > 0 {
		solve()
		t--
	}

}

package main

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

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

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

		f := true
		arr := make([]int, len(s))
		z := (len(s) - 1) * (len(s)) / 2
		m := make([]bool, 26)
		var seen bool
		for i, ch := range s {
			arr[i] = int(ch - 'a')
			z -= int(ch - 'a')

			if m[int(ch-'a')] {
				f = false
				break
			} else if !seen && ch == 'a' {
				seen = true
			} else if !seen && i > 0 {
				if arr[i-1] <= arr[i] {
					f = false
					break
				}
			} else if seen {
				if arr[i-1] >= arr[i] {
					f = false
					break
				}
			}
			m[int(ch-'a')] = true
		}

		if !f || z != 0 {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
		}
	}
}

package main

import (
	"fmt"
)

func solve() {
	var s string
	fmt.Scan(&s)
	length := len(s)
	pos := -1
	for i := 0; i < length; i++ {
		if s[i] == 'a' {
			pos = i
		}
	}
	if pos == -1 {
		fmt.Println("NO")
	} else {
		L := pos - 1
		R := pos + 1
		for i := 1; i < length; i++ {
			if L >= 0 && int(s[L]) == 'a'+i {
				L--
			} else if R < length && int(s[R]) == 'a'+i {
				R++
			} else {
				fmt.Println("NO")
				return
			}
		}
		fmt.Println("YES")
	}
}

func main() {
	// fmt.Println("Enter how much number")
	var t int
	// fmt.Scanf("%d", &t)
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		solve()
	}
}

package main

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

func solve(r io.Reader) {
	var n int
	var s string

	abc := "abcdefghijklmnopqrstuvwxyz"
	m := make(map[string]int)
	for i, r := range abc {
		c := string(r)
		m[c] = i + 1
	}

	reader := bufio.NewReader(r)
	s, _ = reader.ReadString('\n')
	s = strings.TrimSpace(s)
	n, _ = strconv.Atoi(s)

	for i := 0; i < n; i++ {
		s, _ = reader.ReadString('\n')
		s = strings.TrimSpace(s)
		prev_j := -1
		res := 0
		b := make([]int, len(abc))
		for j := 0; j < len(abc); j++ {
			b[j] = 1
		}

		for _, r := range s {
			c := string(r)
			if prev_j != -1 {
				if m[c] < prev_j {
					if res == 1 {
						res = -1
						break
					}
					res = 0
				} else {
					res = 1
				}
			}
			prev_j = m[c]
			b[m[c]-1] = 0
		}

		sum := 0
		for j := 0; j < len(s); j++ {
			sum += b[j]
		}

		if res != -1 && sum == 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

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

package main
import(
    "fmt"
    "bufio"
    "os"
)
var letters = map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
	"f": 6,
	"g": 7,
	"h": 8,
	"i": 9,
	"j": 10,
	"k": 11,
	"l": 12,
	"m": 13,
	"n": 14,
	"o": 15,
	"p": 16,
	"q": 17,
	"r": 18,
	"s": 19,
	"t": 20,
	"u": 21,
	"v": 22,
	"w": 23,
	"x": 24,
	"y": 25,
	"z": 26,
}

func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var word string
		fmt.Fscan(in,&word)
		list:= make([]int,len(word))
		for j:=0;j<len(word);j++{
			list[j]= letters[string(word[j])]
		}
		indice:=0
		for j:=1;j<=len(list);j++{
			counter:=0
			for k:=1;k<=len(list);k++{
				if list[k-1] == j {
					counter++
				}
			}
			if counter == 0 || counter >= 2 {
				fmt.Fprintln(out,"NO")
				goto out
			}
		}
		for j:=0;j<len(list);j++{
			if list[j] == 1 {
				indice= j
				break
			} else {
				if j == len(list)-1 {
					fmt.Fprintln(out,"NO")
					goto out
				} else {
					if list[j] <= list[j+1] {
						fmt.Fprintln(out,"NO")
						goto out
					}
				}
			}
		}
		for j:=indice;j<len(list)-1;j++{
			if list[j] >= list[j+1] {
				fmt.Fprintln(out,"NO")
				goto out
			}
		}
		fmt.Fprintln(out,"YES")
		out:
	}
}

package main

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

type ele struct {
	v   rune
	ind int
}

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N := 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		s, o, ok := "", []ele{}, true
		fmt.Fscan(f, &s)
		for j := 0; j < len(s); j++ {
			o = append(o, ele{v: rune(s[j]), ind: j})
		}
		sort.Slice(o, func(i, j int) bool { return o[i].v < o[j].v })
		l, r, first := 0, 0, rune(97)
		for j := 0; j < len(o); j++ {
			if j == 0 {
				l, r, ok = o[j].ind, o[j].ind, o[j].v == first
			} else {
				if o[j].v != first+rune(j) || !(o[j].ind == l-1 || o[j].ind == r+1) {
					ok = false
				}
				if o[j].ind == l-1 {
					l = l - 1
				}
				if o[j].ind == r+1 {
					r = r + 1
				}
			}
		}
		if ok {
			fmt.Fprintln(w, "YES")
		} else {
			fmt.Fprintln(w, "NO")
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t      int
	s      string
)

func solve(s string) string {
	l := len(s)
	p := 0
	q := l - 1
	for i := 'a' + byte(l-1); i >= 'a'; i-- {
		if s[p] != i && s[q] != i {
			return "NO"
		}
		if s[p] == i {
			p++
		}
		if s[q] == i {
			q--
		}
	}
	return "YES"
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &s)
		fmt.Println(solve(s))
	}
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1547B
Date: 18/11/21
User: pepradere
URL: https://codeforces.com/problemset/problem/1547/B
Problem: CF1547B
**/
func (in *CF1547B) Run() {
	t := in.NextInt()
	for ; t > 0; t-- {
		str := in.NextString()
		index := strings.Index(str, "a")
		ans := true
		if index == -1 {
			ans = false
		} else {
			letter := uint8('b')

			for left, right := index-1, index+1; ans ; letter++ {
				if left >= 0 {
					if str[left] == letter {
						left--
						continue
					}
				}
				if right < len(str) {
					if str[right] == letter {
						right++
						continue
					}
				}
				if left < 0 && right >= len(str){
					break
				}
				ans = false
			}
		}

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

	}
}

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

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

package main

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

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

	var t int
	for Fscan(in, &t); t > 0; t-- {
		var s string
		Fscan(in, &s)

		a := -1
		for i, c := range s {
			if c == 'a' {
				a = i
				break
			}
		}
		if a == -1 {
			Fprintln(out, "NO")
			continue
		}

		head, tail, curr, flag := a-1, a+1, a, true
		for head != -1 || tail != len(s) {
			if head != -1 && s[head] == s[curr] + 1 {
				curr = head; head--
			} else if tail != len(s) && s[tail] == s[curr] + 1 {
				curr = tail; tail++
			} else {
				Fprintln(out, "NO")
				flag = false
				break
			}
		}
		if flag {
			Fprintln(out, "YES")
		}
	}

}

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

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

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

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

package main

import "fmt"

var t int

func main() {
	fmt.Scan(&t)

	for i := 0; i < t; i++ {
		var s string
		var l, r uint8

		fmt.Scan(&s)

		length := uint8(len(s))
		l = 0
		r = length-1
		for ; length > 0; length-- {
			if s[l] == length+96 {
				l++
			} else if s[r] == length+96 {
				r--
			} else {
				fmt.Println("NO")
				break
			}
		}

		if length == 0 { fmt.Println("YES") }
	}
}

package main

import (
	"fmt"
	"os"
)

func main() {
	var T int
	fmt.Fscan(os.Stdin,&T)
	for (T>0) {
		T--
		var s string
		fmt.Fscan(os.Stdin,&s)
		buff:=[]byte(s)
		length := len(buff)
		ok:=true
		for i:=length-1;i>=0;i-- {
			if( int(buff[len(buff)-1])==('a'+i)) {
				buff=buff[:len(buff)-1]
			}else if(int(buff[0])=='a'+i) {
				buff=buff[1:]
			}else {
				ok=false
			}
		}
		if (ok) {
			fmt.Println("YES")
		}else {
			fmt.Println("NO")
		}
	}
}

package main

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

const MOD = 1000000007

var (
	in  *bufio.Reader
	out *bufio.Writer
)

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

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

	for tc := 1; tc <= t; tc++ {
		solveTest(tc)
	}

	out.Flush()
}

func solveTest(tc int) {
	var s string
	fmt.Fscan(in, &s)

	if isAlphabetical(s) {
		fmt.Fprintln(out, "YES")
	} else {
		fmt.Fprintln(out, "NO")
	}
}

func isAlphabetical(s string) bool {
	n := len(s)
	if n == 0 {
		return true
	}

	c := rune(n - 1 + int('a'))
	if rune(s[0]) == c {
		return isAlphabetical(s[1:])
	} else if rune(s[n - 1]) == c {
		return isAlphabetical(s[0:n - 1])
	}
	return false
}

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

func min(xs ...int) int {
	result := xs[0]
	for i := 1; i < len(xs); i++ {
		if result > xs[i] {
			result = xs[i]
		}
	}
	return result
}

func max(xs ...int) int {
	result := xs[0]
	for i := 1; i < len(xs); i++ {
		if result < xs[i] {
			result = xs[i]
		}
	}
	return result
}

package main

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

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

	var tcs int
	_, _ = fmt.Fscan(in, &tcs)

	for tc := 0; tc < tcs; tc++ {
		var s string
		_, _ = fmt.Fscan(in, &s)

		ans := true
		for i, n, j := 0, len(s), len(s)-1; i <= j; n-- {
			x, y := int(s[i])-int('a'), int(s[j])-int('a')

			if n-1 == x {
				i++
			} else if n-1 == y {
				j--
			} else {
				ans = false
				break
			}
		}

		if ans {
			_, _ = fmt.Fprintln(out, "YES")
		} else {
			_, _ = fmt.Fprintln(out, "NO")
		}
	}

	_ = out.Flush()
}

func abs(n int) int {
	if n < 0 {
		n = -n
	}

	return n
}

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
}

package main

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

func main() {
	io := NewIO(os.Stdin, os.Stdout)
	some: for t := io.ReadInt(); t > 0; t-- {
		s := io.ReadString()
		in := -1
		for i := range s {
			if s[i] == 'a' {
				in = i
			}
		}
		if in == -1 {
			io.Println("NO")
			continue
		}

		n := len(s)
		l, r := 1, 1

		for i := 1; i < n; i++ {
			if in-l >= 0 && s[in-l] == byte(int('a')+i) {
				l++
			} else if in+r < n && s[in+r] == byte(int('a')+i) {
				r++
			} else {
				io.Println("NO")
				continue some
			}
		}

		io.Println("YES")
	}
}

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

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

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		printf("%s\n", sol(sstring()))
	}
}

func sol(s string) string {
	aPos := findA(s)
	if aPos == -1 {
		return "NO"
	}
	for i := aPos + 1; i < len(s); i++ {
		if s[i-1] >= s[i] {
			return "NO"
		}
	}
	for i := aPos - 1; i >= 0; i-- {
		if s[i] <= s[i+1] {
			return "NO"
		}
	}
	letters := []rune(s)
	sort.Slice(letters, func(il, ir int) bool {
		return letters[il] < letters[ir]
	})
	for i := range letters {
		if letters[i] != rune('a')+rune(i) {
			return "NO"
		}
	}
	return "YES"
}

func findA(s string) int {
	for i := range s {
		if s[i] == 'a' {
			return i
		}
	}
	return -1
}

package main

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

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

	solve := func(Case int) (_b bool) {
		var s []byte
		Fscan(in, &s)
		n := len(s)
		cur := byte('a'+n-1)
		for len(s) >0 {
			if s[0] == cur {
				s = s[1:]
			} else if s[len(s)-1] == cur {
				s = s[:len(s)-1]
			} else {
				return false
			}
			cur--
		}
		return true
	}

	var T int
	Fscan(in, &T)
	for Case := 1; Case <= T; Case++ {
		if solve(Case) {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}

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

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

package main

import "fmt"

func algo(s string) bool {
	i, j := 0, len(s)-1

	if j == 0 {
		return s[j] == 'a'
	}

	for i < j {
		expected_char := byte('a' + (j - i))
		if s[i] < s[j] {
			if s[j] != expected_char {
				return false
			}
			if int(s[j]-s[j-1]) == 1 {
				j -= 2
			} else if int(s[j]-s[i]) == 1 {
				i++
				j--
			} else {
				return false
			}
		} else if s[i] > s[j] {
			if s[i] != expected_char {
				return false
			}
			if int(s[i]-s[i+1]) == 1 {
				i += 2
			} else if int(s[i]-s[j]) == 1 {
				i++
				j--
			} else {
				return false
			}
		} else {
			return false
		}
	}

	return true
}

func input() []string {
	num_case := 0
	fmt.Scan(&num_case)
	testcases := make([]string, num_case)

	for i := 0; i < num_case; i++ {
		fmt.Scan(&testcases[i])
	}

	return testcases
}

func main() {
	strs := input()

	for _, s := range strs {
		if algo(s) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

func algo(s string) bool {
	i, j := 0, len(s)-1
	for i < j {
		expected_char := byte('a' + (j - i))
		if s[i] < s[j] {
			if s[j] != expected_char {
				return false
			}
			if int(s[j]-s[j-1]) == 1 {
				j -= 2
			} else if int(s[j]-s[i]) == 1 {
				i++
				j--
			} else {
				return false
			}
		} else if s[i] > s[j] {
			if s[i] != expected_char {
				return false
			}
			if int(s[i]-s[i+1]) == 1 {
				i += 2
			} else if int(s[i]-s[j]) == 1 {
				i++
				j--
			} else {
				return false
			}
		} else {
			return false
		}
	}

	if i == len(s) || s[i] == 'a' {
		return true
	} else {
		return false
	}
}

func input() []string {
	num_case := 0
	fmt.Scan(&num_case)
	testcases := make([]string, num_case)

	for i := 0; i < num_case; i++ {
		fmt.Scan(&testcases[i])
	}

	return testcases
}

func main() {
	strs := input()
	for _, s := range strs {
		if algo(s) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

func algo(s string) bool {
	i, j := 0, len(s)-1
	for i < j {
		if s[i] < s[j] {
			if int(s[j]-s[j-1]) == 1 {
				j -= 2
			} else if int(s[j]-s[i]) == 1 {
				i++
				j--
			} else {
				return false
			}
		} else if s[i] > s[j] {
			if int(s[i]-s[i+1]) == 1 {
				i += 2
			} else if int(s[i]-s[j]) == 1 {
				i++
				j--
			} else {
				return false
			}
		} else {
			return false
		}
	}

	if i == len(s) || s[i] == 'a' {
		return true
	} else {
		return false
	}
}

func input() []string {
	num_case := 0
	fmt.Scan(&num_case)
	testcases := make([]string, num_case)

	for i := 0; i < num_case; i++ {
		fmt.Scan(&testcases[i])
	}

	return testcases
}

func main() {
	strs := input()
	for _, s := range strs {
		if algo(s) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

/*
 * Created by Reverie
 * 2021.07.11
 * 02:23:13
 */

package main

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

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

	solve := func(Case int) {
		var s string
		Scan(&s)

		n := len(s)
		isValid := true
		for i := n - 1; i >= 0; i-- {
			if s[0] == 'a'+byte(i) {
				s = s[1:]
			} else if s[i] == 'a'+byte(i) {
				s = s[:i]
			} else {
				isValid = false
				break
			}
		}

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

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

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

/*
 * Created by Reverie
 * 2021.07.11
 * 02:20:12
 */

package main

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

type Point struct {
	x, y int
}

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

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

	solve := func(Case int) {
		var a, b, f Point
		Fscan(in, &a.x, &a.y, &b.x, &b.y, &f.x, &f.y)

		ans := 0
		if a.x == b.x {
			if a.y > b.y {
				a.y, b.y = b.y, a.y
			}
			ans = b.y - a.y
			if f.x == a.x && f.y > a.y && f.y < b.y {
				ans += 2
			}
		} else if a.y == b.y {
			if a.x > b.x {
				a.x, b.x = b.x, a.x
			}
			ans = b.x - a.x
			if f.y == a.y && f.x > a.x && f.x < b.x {
				ans += 2
			}
		} else {
			ans = abs(a.x-b.x) + abs(a.y-b.y)
		}
		Fprintln(out, ans)
	}

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

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

package main

import (
	"fmt"
)

func solve() {

	var s string
	fmt.Scan(&s)
	n := len(s)
	left := 0
	right := n - 1
	var max byte
	if s[left] > s[right] {
		max = s[left]
	} else {
		max = s[right]
	}
	if int(max-'a') != (n - 1) {
		fmt.Println("NO")
		return
	}
	for left < right {
		if s[left] == max {
			max--
			left++
		} else if s[right] == max {
			max--
			right--
		} else {
			fmt.Println("NO")
			return
		}
	}
	fmt.Println("YES")

}

func main() {
	var t int
	fmt.Scan(&t)
	for t > 0 {
		solve()
		t--
	}

}

package main

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

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

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

		f := true
		arr := make([]int, len(s))
		z := (len(s) - 1) * (len(s)) / 2
		var seen bool
		for i, ch := range s {
			arr[i] = int(ch - 'a')
			z -= int(ch - 'a')

			if !seen && ch == 'a' {
				seen = true
			} else if !seen && i > 0 {
				if arr[i-1] <= arr[i] {
					f = false
					break
				}
			} else if seen {
				if arr[i-1] >= arr[i] {
					f = false
					break
				}
			}
		}

		if !f || z != 0 {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
		}
	}
}

package main

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

func solve(r io.Reader) {
	var n int
	var s string

	abc := "abcdefghijklmnopqrstuvwxyz"
	m := make(map[string]int)
	for i, r := range abc {
		c := string(r)
		m[c] = i + 1
	}

	reader := bufio.NewReader(r)
	s, _ = reader.ReadString('\n')
	s = strings.TrimSpace(s)
	n, _ = strconv.Atoi(s)

	for i := 0; i < n; i++ {
		s, _ = reader.ReadString('\n')
		s = strings.TrimSpace(s)
		prev_j := -1
		res := 0
		length := len(s)
		sum := (1 + length) * length / 2
		for _, r := range s {
			c := string(r)
			if prev_j != -1 {
				if m[c] < prev_j {
					if res == 1 {
						res = -1
						break
					}
					res = 0
				} else {
					res = 1
				}
			}
			prev_j = m[c]
			sum -= m[c]
		}
		if res != -1 && sum == 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

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


You are given a permutation $$$a_1, a_2, \ldots, a_n$$$ of size $$$n$$$, where each integer from $$$1$$$ to $$$n$$$ appears exactly once.You can do the following operation any number of times (possibly, zero):  Choose any three indices $$$i, j, k$$$ ($$$1 \le i < j < k \le n$$$).  If $$$a_i > a_k$$$, replace $$$a_i$$$ with $$$a_i + a_j$$$. Otherwise, swap $$$a_j$$$ and $$$a_k$$$. Determine whether you can make the array $$$a$$$ sorted in non-descending order.

Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 10$$$) — the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\dots,a_n$$$ ($$$1 \le a_i \le n$$$, $$$a_i \neq a_j$$$ if $$$i \neq j$$$) — the elements of the array $$$a$$$.

For each test case, output "Yes" (without quotes) if the array can be sorted in non-descending order, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "YES", "yEs" and "yes" will be recognized as a positive response).

NoteIn the first test case, $$$[1,2,3]$$$ is already sorted in non-descending order.In the second test case, we can choose $$$i = 1,j = 2,k = 3$$$. Since $$$a_1 \le a_3$$$, swap $$$a_2$$$ and $$$a_3$$$, the array then becomes $$$[1,2,3]$$$, which is sorted in non-descending order.In the seventh test case, we can do the following operations successively:  Choose $$$i = 5,j = 6,k = 7$$$. Since $$$a_5 \le a_7$$$, swap $$$a_6$$$ and $$$a_7$$$, the array then becomes $$$[1,2,6,7,4,5,3]$$$.  Choose $$$i = 5,j = 6,k = 7$$$. Since $$$a_5 > a_7$$$, replace $$$a_5$$$ with $$$a_5+a_6=9$$$, the array then becomes $$$[1,2,6,7,9,5,3]$$$.  Choose $$$i = 2,j = 5,k = 7$$$. Since $$$a_2 \le a_7$$$, swap $$$a_5$$$ and $$$a_7$$$, the array then becomes $$$[1,2,6,7,3,5,9]$$$.  Choose $$$i = 2,j = 4,k = 6$$$. Since $$$a_2 \le a_6$$$, swap $$$a_4$$$ and $$$a_6$$$, the array then becomes $$$[1,2,6,5,3,7,9]$$$.  Choose $$$i = 1,j = 3,k = 5$$$. Since $$$a_1 \le a_5$$$, swap $$$a_3$$$ and $$$a_5$$$, the array then becomes $$$[1,2,3,5,6,7,9]$$$, which is sorted in non-descending order. In the third, the fourth, the fifth and the sixth test cases, it can be shown that the array cannot be sorted in non-descending order.

package main

import (
	"fmt"
)

var eps float64 = 1e-4

func sq_root(n float64) float64 {
	var l, r float64 = 0, n
	m := (r + l) / 2

	for (r - l) > eps {
		m = (r + l) / 2
		if m*m > n {
			r = m
		} else {
			l = m
		}
	}
	return l
}

func main() {
	t := 0
	fmt.Scan(&t)
	for ; t > 0; t-- {
		n := 0
		fmt.Scan(&n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Scan(&a[i])
		}
		if a[0] == 1 {
			fmt.Print("YES\n")
		} else {
			fmt.Print("NO\n")
		}
	}
}

package main

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

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

	tc := int32(0)
	fmt.Fscan(in, &tc)

	for ; tc > 0; tc-- {
		n := int32(0)
		fmt.Fscan(in, &n)

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

		if arr[0] == 1 {
			fmt.Fprintf(out, "yes\n")
		} else {
			fmt.Fprintf(out, "no\n")
		}
	}

	out.Flush()
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1750A
Date: 11/15/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1750/A
Problem: CF1750A
**/
func (in *CF1750A) Run() {
  for t := in.NextInt(); t > 0; t-- {
    n := in.NextInt()
    min := -1
    f := -1
    for i := 0; i < n; i++ {
      val := in.NextInt()
      if f == -1 {
        f = val
      }
      if min == -1 {
        min = val
      } else if min > val {
        min = val
      }
    }

    if f == min {
      fmt.Println("YES")
    } else {
      fmt.Println("NO")
    }

  }
}

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

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

package main

import "fmt"

func main() {
	var test int
	fmt.Scan(&test)

	for test > 0 {
		solve()
		test = test - 1
	}
}

func solve() {
	var n int
	var first int
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		var x int
		fmt.Scan(&x)
		if i == 0 {
			first = x
		}
	}

	if first == 1 {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

package main

import (
	"fmt"
	"sort"
)

func solve(e []int, n int) bool {
	fmt.Println(e)
	s := sort.IntsAreSorted(e)
	if s {
		return true
	}
	for i := n - 3; i >= 0; i-- {
		for j := i + 1; j < n; j++ {
			for k := j + 1; k < n; k++ {
				if e[i] > e[k] {
					e[i] = e[i] + e[j]
				} else {
					e[j], e[k] = e[k], e[j]
				}
				fmt.Println(i+1, j+1, k+1, e)
				s := sort.IntsAreSorted(e)
				if s {
					return true
				}
			}
		}
	}

	s = sort.IntsAreSorted(e)
	return s
}

func main() {
	var t, n int
	fmt.Scan(&t)
	for c := 0; c < t; c++ {
		var e []int
		fmt.Scan(&n)
		for i := 0; i < n; i++ {
			var x int
			fmt.Scan(&x)
			e = append(e, x)
		}

		s := e[0] == 1
		if s {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

func solve(reader *bufio.Reader, writer *bufio.Writer) {
	_ = readInt(reader)
	a := readArrayInt(reader)
	if a[0] == 1 {
		fmt.Fprintln(writer, "Yes")
	} else {
		fmt.Fprintln(writer, "No")
	}
}

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		solve(reader, writer)
		t--
	}

	writer.Flush()
}

func min[T int | int64](a, b T) T {
	if a < b {
		return a
	}

	return b
}

func max[T int | int64](a, b T) T {
	if a > b {
		return a
	}

	return b
}

func abs[T int | int64](a T) T {
	if a < 0 {
		return a * -1
	} else {
		return a
	}
}

func gcd[T int | int64](x, y T) T {
	for y > 0 {
		x, y = y, x%y
	}

	return x
}

func lcm[T int | int64](x, y T) T {
	return x * y / gcd(x, y)
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 32)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

package main

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

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

const (
	DEBUG = false
)

func debug(a ...interface{}) {
	if !DEBUG {
		return
	}
	for _, e := range a {
		printf("%v ", e)
	}
	printf("\n")
	writer.Flush()
}

func debugCondn(condn bool, a ...interface{}) {
	if !DEBUG || !condn {
		return
	}
	for _, e := range a {
		printf("%v ", e)
	}
	printf("\n")
	writer.Flush()
}

func assert(condn bool) {
	if !condn {
		panic("assertion error")
	}
}

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

	if a[0] == 1 {
		printf("Yes\n")
		return
	}
	printf("No\n")
}

func main() {
	defer writer.Flush()

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

package main

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

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

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

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

		res := "NO"
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &u)
			if i == 0 && u == 1 {
				res = "YES"
			}
		}

		fmt.Fprintln(w, res)
	}

}

package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n)
	mn := n + 1
	for i := range arr {
		fmt.Fscan(in, &arr[i])
		mn = min(mn, arr[i])
	}
	if mn != arr[0] {
		fmt.Fprintln(out, "No")
	} else {
		fmt.Fprintln(out, "Yes")
	}
}

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

func LCM(a, b int) int {
	return a * b / GCD(a, b)
}

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

	return a
}

type SqrtInt interface {
	SignedSqrt | UnsignedSqrt
}

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

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

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

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

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