One day, Ahmed_Hossam went to Hemose and said "Let's solve a gym contest!". Hemose didn't want to do that, as he was playing Valorant, so he came up with a problem and told it to Ahmed to distract him. Sadly, Ahmed can't solve it... Could you help him?There is an Agent in Valorant, and he has $$$n$$$ weapons. The $$$i$$$-th weapon has a damage value $$$a_i$$$, and the Agent will face an enemy whose health value is $$$H$$$.The Agent will perform one or more moves until the enemy dies.In one move, he will choose a weapon and decrease the enemy's health by its damage value. The enemy will die when his health will become less than or equal to $$$0$$$. However, not everything is so easy: the Agent can't choose the same weapon for $$$2$$$ times in a row.What is the minimum number of times that the Agent will need to use the weapons to kill the enemy?

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 10^5)$$$. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$H$$$ $$$(2 \leq n \leq 10^3, 1 \leq H \leq 10^9)$$$ — the number of available weapons and the initial health value of the enemy. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$ — the damage values of the weapons. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

NoteIn the first test case, the Agent can use the second weapon, making health value of the enemy equal to $$$4-7=-3$$$. $$$-3 \le 0$$$, so the enemy is dead, and using weapon $$$1$$$ time was enough.In the second test case, the Agent can use the first weapon first, and then the second one. After this, the health of enemy will drop to $$$6-4-2 = 0$$$, meaning he would be killed after using weapons $$$2$$$ times.In the third test case, the Agent can use the weapons in order (third, first, third), decreasing the health value of enemy to $$$11 - 7 - 2 - 7 = -5$$$ after using the weapons $$$3$$$ times. Note that we can't kill the enemy by using the third weapon twice, as even though $$$11-7-7<0$$$, it's not allowed to use the same weapon twice in a row.

package main

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

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

func (in *CF1592A) 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 *CF1592A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1592A
Date: 20/04/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1592/A
Problem: CF1592A
**/
func (in *CF1592A) Run() {
	var str strings.Builder
	for t := in.NextInt(); t > 0; t-- {
		n, H := in.NextInt(), in.NextInt()
		a, b := 0, 0
		for i := 0; i < n; i++ {
			val := in.NextInt()
			if a < val {
				if b < a {
					b = a
				}
				a = val
			} else if b < val {
				b = val
			}
		}

		m := a + b
		x := int(math.Floor(float64(H / m)))

		diff := H - (x * m)

		ans := x * 2
		if diff > 0 {
			diff -= a
			ans++
		}
		if diff > 0 {
			diff -= b
			ans++
		}
		str.WriteString(fmt.Sprintf("%d\n", ans))
	}
	fmt.Print(str.String())
}

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

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

package main

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

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

func (in *CF1592A) 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 *CF1592A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1592A
Date: 20/04/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1592/A
Problem: CF1592A
**/
func (in *CF1592A) Run() {

	for t := in.NextInt(); t > 0; t-- {
		n, H := in.NextInt(), in.NextInt()
		a, b := 0, 0
		for i := 0; i < n; i++ {
			val := in.NextInt()
			if a < val {
				if b < a {
					b = a
				}
				a = val
			} else if b < val {
				b = val
			}
		}

		m := a + b
		x := int(math.Floor(float64(H / m)))

		diff := H - (x * m)

		ans := x * 2
		if diff > 0 {
			diff -= a
			ans++
		}
		if diff > 0 {
			diff -= b
			ans++
		}
		fmt.Println(ans)
	}
}

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

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

package main

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

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

func (in *CF1592A) 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 *CF1592A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1592A
Date: 20/04/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1592/A
Problem: CF1592A
**/
func (in *CF1592A) Run() {

	for t := in.NextInt(); t > 0; t-- {
		n, H := in.NextInt(), in.NextInt()
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			arr[i] = in.NextInt()
		}
		sort.Ints(arr)

		m := arr[n-1] + arr[n-2]
		x := int(math.Floor(float64(H / m)))

		diff := H - (x * m)

		ans := x * 2
		if diff > 0 {
			diff -= arr[n-1]
			ans++
		}
		if diff > 0 {
			diff -= arr[n-2]
			ans++
		}
		fmt.Println(ans)
	}
}

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

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

package main

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

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

func (in *CF1592A) 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 *CF1592A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1592A
Date: 20/04/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1592/A
Problem: CF1592A
**/
func (in *CF1592A) Run() {

	for t := in.NextInt(); t > 0; t-- {
		n, H := in.NextInt(), in.NextInt64()
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			arr[i] = in.NextInt()
		}
		sort.Ints(arr)

		m := int64(arr[n-1]) + int64(arr[n-2])
		x := int64(math.Floor(float64(H) / float64(m)))

		diff := H - (x * m)

		ans := x * 2
		if diff > 0 {
			diff -= int64(arr[n-1])
			ans++
		}
		if diff > 0 {
			diff -= int64(arr[n-2])
			ans++
		}
		fmt.Println(ans)
	}
}

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

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

package main

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

func main() {
        in := bufio.NewReader(os.Stdin)
        out := bufio.NewWriter(os.Stdout)
        defer out.Flush()
        var t, n, h, tmp int64
        fmt.Scan(&t)
        for ; t > 0; t-- {
                fmt.Fscan(in, &n, &h)
                var mx1, mx2 int64 = 0, 0
                for i := int64(0); i < n; i++ {
                        fmt.Fscan(in, &tmp)
                        if tmp >= mx1 {
                                mx2 = mx1
                                mx1 = tmp
                        } else if tmp > mx2 {
                                mx2 = tmp
                        }
                }
                res := int64(h/(mx1+mx2)) * 2
                leave := h % (mx1 + mx2)
                if leave > 0 && leave <= mx1 {
                        res += 1
                } else if leave > 0 && leave > mx1 {
                        res += 2
                }
                out.WriteString(fmt.Sprintln(res))
                // fmt.Printf("%d\n", res)

        }
        return
        // fmt.Println(sb.String())
}

package main

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

func main() {
        in := bufio.NewReader(os.Stdin)
        var t, n, h, tmp int64
        fmt.Scan(&t)
        for ; t > 0; t-- {
                fmt.Fscan(in, &n, &h)
                var mx1, mx2 int64 = 0, 0
                for i := int64(0); i < n; i++ {
                        fmt.Fscan(in, &tmp)
                        if tmp >= mx1 {
                                mx2 = mx1
                                mx1 = tmp
                        } else if tmp > mx2 {
                                mx2 = tmp
                        }
                }
                res := int64(h/(mx1+mx2)) * 2
                leave := h % (mx1 + mx2)
                if leave > 0 && leave <= mx1 {
                        res += 1
                } else if leave > 0 && leave > mx1 {
                        res += 2
                }
                fmt.Printf("%d\n", res)

        }
        return
        // fmt.Println(sb.String())
}

package main

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

var (
	reader  = bufio.NewReader(os.Stdin)
	t, n, h int
)

func solve(n, h int, a []int) int {
	var p, q int
	for _, x := range a {
		if x >= p {
			q = p
			p = x
		} else if x >= q {
			q = x
		}
	}
	k := h / (p + q)
	l := h % (p + q)
	if l == 0 {
		return k * 2
	}
	if l <= p {
		return k*2 + 1
	}
	return k*2 + 2
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &h)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		fmt.Println(solve(n, h, a))
	}
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
	h      int64
)

func solve(n int, h int64, a []int64) int64 {
	var p, q int64
	for _, x := range a {
		if x >= p {
			q = p
			p = x
		} else if x >= q {
			q = x
		}
	}
	k := h / (p + q)
	l := h % (p + q)
	if l == 0 {
		return k * 2
	}
	if l <= p {
		return k*2 + 1
	}
	return k*2 + 2
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &h)
		a := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		fmt.Println(solve(n, h, a))
	}
}

package main

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

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

	var t int

	for fmt.Fscan(in, &t); t > 0; t-- {
		var n, hp int
		fmt.Fscan(in, &n, &hp)

		max, premax := 0, 0

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

			if num >= max {
				premax = max
				max = num
			} else if num > premax {
				premax = num
			}
		}

		ans := 0
		sum := max + premax
		mod := hp % (sum)
		if mod == 0 {
			ans = 2 * (hp / sum)
		} else if mod <= max {
			ans = 2*(hp/sum) + 1
		} else {
			ans = 2*(hp/sum) + 2
		}

		fmt.Fprintln(out, ans)
	}
}

package main

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

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

	for i := 0; i < t; i++ {
		var n, H int
		fmt.Fscan(input, &n, &H)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
		}
		sort.Ints(arr)
		max1 := int64(arr[n-1])
		max2 := int64(arr[n-2])
		// H <= n1 * max1 + n2 * max2
		// H <= x * max1 + x * max2 ::: Case 1
		// H <= x * max1 + (x - 1) * max2 ::: Case 2

		s := max1 + max2
		d := (int64(H) + s - 1) / s
		if d*max1+(d-1)*max2 >= int64(H) {
			fmt.Println(2*d - 1)
		} else {
			fmt.Println(2 * d)
		}
	}
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N := 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		n, H, o := 0, 0, []int{}
		fmt.Fscan(f, &n, &H)
		for j := 0; j < n; j++ {
			p := 0
			fmt.Fscan(f, &p)
			o = append(o, p)
		}
		sort.Ints(o)
		if H%(o[n-1]+o[n-2]) == 0 {
			fmt.Fprintln(w, (H/(o[n-1]+o[n-2]))*2)
		} else if H%(o[n-1]+o[n-2]) <= o[n-1] {
			fmt.Fprintln(w, ((H/(o[n-1]+o[n-2]))*2)+1)
		} else {
			fmt.Fprintln(w, ((H/(o[n-1]+o[n-2]))*2)+2)
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
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 n,h int
		fmt.Fscan(in,&n,&h)
		var s,b int
		for j:=0;j<n;j++{
			var x int
			fmt.Fscan(in,&x)
			if x > b {
				s,b = b,x
				continue
			}
			if x > s {
				s = x
			}
		}
		if h <= b {
			fmt.Fprintln(out,1)
			continue
		}
		if h <= b+s {
			fmt.Fprintln(out,2)
			continue
		}
		if h%(s+b) == 0 {
			fmt.Fprintln(out,h/(s+b)*2)
		} else {
			if h%(s+b) > b {
				fmt.Fprintln(out,h/(s+b)*2+2)
			} else {
				fmt.Fprintln(out,h/(s+b)*2+1)
			}
		}
	}
}

package main

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

//One day, Ahmed_Hossam went to Hemose and said "Let's solve a gym contest!". Hemose didn't want to do that, as he was playing Valorant, so he came up with a problem and told it to Ahmed to distract him. Sadly, Ahmed can't solve it... Could you help him?
//
//There is an Agent in Valorant, and he has 𝑛 weapons. The 𝑖-th weapon has a damage value 𝑎𝑖, and the Agent will face an enemy whose health value is 𝐻.
//
//The Agent will perform one or more moves until the enemy dies.
//
//In one move, he will choose a weapon and decrease the enemy's health by its damage value. The enemy will die when his health will become less than or equal to 0. However, not everything is so easy: the Agent can't choose the same weapon for 2 times in a row.
//
//What is the minimum number of times that the Agent will need to use the weapons to kill the enemy?
//
//Input
//Each test contains multiple test cases. The first line contains the number of test cases 𝑡 (1≤𝑡≤105). Description of the test cases follows.
//
//The first line of each test case contains two integers 𝑛 and 𝐻 (2≤𝑛≤103,1≤𝐻≤109) — the number of available weapons and the initial health value of the enemy.
//
//The second line of each test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109) — the damage values of the weapons.
//
//It's guaranteed that the sum of 𝑛 over all test cases doesn't exceed 2⋅105.
//
//Output
//For each test case, print a single integer — the minimum number of times that the Agent will have to use the weapons to kill the enemy.
var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)

func scanString() string { sc.Scan(); return sc.Text() }
func scanRunes() []rune  { return []rune(scanString()) }
func scanInt() int       { a, _ := strconv.Atoi(scanString()); return a }
func scanInt64() int64   { a, _ := strconv.ParseInt(scanString(), 10, 64); return a }
func scanFloat() float64 { a, _ := strconv.ParseFloat(scanString(), 64); return a }

func scanInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanInt()
	}
	return a
}

func main() {
	sc.Split(bufio.ScanWords)
	for t := scanInt(); t > 0; t-- {
		n, h := scanInt(), scanInt()
		a := scanInts(n)
		sort.Ints(a)
		times := h / (a[n-1] + a[n-2]) * 2
		if h%(a[n-1]+a[n-2]) > 0 {
			if h%(a[n-1]+a[n-2])%a[n-1] == 0 {
				times++
			} else {
				times += h%(a[n-1]+a[n-2])/a[n-1] + 1
			}
		}
		fmt.Println(times)
	}
}

package main

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

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

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

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

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

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

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

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

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

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

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

		}
		return initial
	}
}

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

		}
		return m
	}
}

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

func PrimeFactorization(n int) (pfs map[int]int) {
	pfs = make(map[int]int)

	// Get the number of 2s that divide n
	for n%2 == 0 {
		if _, ok := pfs[2]; ok {
			pfs[2] += 1
		} else {
			pfs[2] = 1
		}
		n = n / 2
	}

	// n must be odd at this point. so we can skip one element
	// (note i = i + 2)
	for i := 3; i*i <= n; i = i + 2 {
		// while i divides n, append i and divide n
		for n%i == 0 {
			if _, ok := pfs[i]; ok {
				pfs[i] += 1
			} else {
				pfs[i] = 1
			}
			n = n / i
		}
	}

	// This condition is to handle the case when n is a prime number
	// greater than 2
	if n > 2 {
		pfs[n] = 1
	}

	return
}

func NumberOfDivisors(n int) int {
	pfs := PrimeFactorization(n)

	num := 1
	for _, exponents := range pfs {
		num *= (exponents + 1)
	}

	return num
}

func KthPrime(x int) (y int) {
	if x == 1 {
		y = 2
	}
	if x == 2 {
		y = 3
	}
	if x > 2 {
		var sum int = 2
		begin := 5
		for sum < x+1 {
			if IsItPrime(begin) == true {
				sum++
			}
			if sum == x {
				y = begin
				break
			} else {
				if begin%6 == 1 {
					begin = begin + 4
				} else {
					begin = begin + 2
				}
			}
		}
	}
	return
}

func MODULE(x, y int) int {
	if x > y {
		return x - y
	} else {
		return y - x
	}
}

func BITAND(x, y int64) int64 {
	var ans int64
	begin := x
	sec := y
	var count int64 = 1
	for 1 == 1 {
		if (begin%2)*(sec%2) == 1 {
			ans = ans + count
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}

		} else {
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}
		}
	}
	return ans
}

func Sum(n, m int) int {
	var ans int

	if n == 1 {
		return 0
	} else {
		for i := 2; i < n; i++ {
			ans = (ans + Sum(i, m)) % m
		}
		for j := 2; j < n+1; j++ {
			ans = (ans + Sum(n/j, m)) % m
		}
		return ans
	}
}

func ChangeStringToDate(x string) (t time.Time, err error) {
	t, err = time.Parse("2006-01-02", x)
	return
}

func DiffrenceBetweenTwoDates(first, second time.Time) int64 {
	date := first
	date2 := second
	k := date.Unix() - date2.Unix()
	return k
}

var t int

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

	for o := 0; o < t; o++ {
		var n int
		var H int
		fmt.Fscan(input, &n, &H)

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

		}

		sort.Ints(ARR)

		First := ARR[n-1]
		Second := ARR[n-2]

		QUAIN := H / (First + Second)
		R := H - QUAIN*(First+Second)
		if R == 0 {
			fmt.Println(QUAIN * 2)
		} else {
			if R <= First {
				fmt.Println(QUAIN*2 + 1)
			} else {
				fmt.Println(QUAIN*2 + 2)

			}
		}
	}

}

package main

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

const MOD int64 = 1000000007

var n int

var H int

var a []int

func main() {

	read := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {

		fmt.Fscan(read, &n, &H)

		a = make([]int, n)

		for j := 0; j < n; j++ {
			fmt.Fscan(read, &a[j])
		}

		fmt.Println(solve())
	}
}

func solve() int {
	sort.Ints(a)

	x := a[len(a) - 1]
	y := a[len(a) - 2]

	if H <= x {
		return 1
	}

	if H <= x + y {
		return 2
	}

	z := H / (x + y)
	r := H % (x + y)

	if r == 0 {
		return 2 * z
	}

	if r <= x {
		return 2 * z + 1
	}
	return 2 * z + 2
}

package main

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

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

func (in *CF1592A) 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 *CF1592A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1592A
Date: 20/04/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1592/A
Problem: CF1592A
**/
func (in *CF1592A) Run() {

	for t := in.NextInt(); t > 0; t-- {
		n, H := in.NextInt(), in.NextInt()
		a, b := 0, 0
		for i := 0; i < n; i++ {
			val := in.NextInt()
			if a < val {
				a = val
			} else if b < val {
				b = val
			}
		}

		m := a + b
		x := int(math.Floor(float64(H / m)))

		diff := H - (x * m)

		ans := x * 2
		if diff > 0 {
			diff -= a
			ans++
		}
		if diff > 0 {
			diff -= b
			ans++
		}
		fmt.Println(ans)
	}
}

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

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

package main

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

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

func (in *CF1592A) 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 *CF1592A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1592A
Date: 20/04/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1592/A
Problem: CF1592A
**/
func (in *CF1592A) Run() {

	for t := in.NextInt(); t > 0; t-- {
		n, H := in.NextInt(), in.NextInt64()
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			arr[i] = in.NextInt()
		}
		sort.Ints(arr)

		m := int64(arr[n-1]) + int64(arr[n-2])
		x := H / m

		ans := x * 2

		if H%m != 0 {
			ans++
		}
		fmt.Println(ans)
	}
}

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

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

package main

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

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

func (in *CF1592A) 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 *CF1592A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1592A
Date: 20/04/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1592/A
Problem: CF1592A
**/
func (in *CF1592A) Run() {

	for t := in.NextInt(); t > 0; t-- {
		n, H := in.NextInt(), in.NextInt()
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			arr[i] = in.NextInt()
		}
		sort.Ints(arr)

		m := arr[n-1] + arr[n-2]
		x := H / m

		ans := x * 2

		if H%m != 0 {
			ans++
		}
		fmt.Println(ans)
	}
}

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

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

package main

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

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

func (in *CF1592A) 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 *CF1592A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1592A
Date: 20/04/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1592/A
Problem: CF1592A
**/
func (in *CF1592A) Run() {

	for t := in.NextInt(); t > 0; t-- {
		n, H := in.NextInt(), in.NextInt()
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			arr[i] = in.NextInt()
		}
		sort.Ints(arr)
		sum := 0
		ans := 0
		for i := n - 1; i >= 0; i-- {
			sum += arr[i]
			ans++
			if H-sum <= 0 {
				break
			}
		}
		fmt.Println(ans)
	}
}

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

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

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
	h      int64
)

func solve(n int, h int64, a []int64) int64 {
	p, q := a[0], a[1]
	if p <= q {
		p, q = q, p
	}
	for i := 2; i < n; i++ {
		if a[i] >= p {
			q = p
			p = a[i]
		}
	}
	k := h / (p + q)
	l := h % (p + q)
	if l == 0 {
		return 2 * k
	}
	if l <= p {
		return 2*k + 1
	}
	return 2*k + 2
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &h)
		a := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		fmt.Println(solve(n, h, a))
	}
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
	h      int64
)

func solve(n int, h int64, a []int64) int64 {
	p, q := a[0], a[1]
	if p <= q {
		p, q = q, p
	}
	for i := 2; i < n; i++ {
		if a[i] >= p {
			q = p
			p = a[i]
		}
	}
	if h%(p+q) == 0 {
		return 2 * h / (p + q)
	}
	k := h / (p + q)
	l := h % (p + q)
	if l == 0 {
		return 2 * k
	}
	if l <= p {
		return 2*k + 1
	}
	return 2*k + 2
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &h)
		a := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		fmt.Println(solve(n, h, a))
	}
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
	h      int64
)

func solve(n int, h int64, a []int64) int64 {
	p, q := a[0], a[1]
	if p <= q {
		p, q = q, p
	}
	for i := 2; i < n; i++ {
		if a[i] >= p {
			q = p
			p = a[i]
		}
	}
	if h%(p+q) == 0 {
		return 2 * h / (p + q)
	}
	k := h / (p + q)
	l := h % (p + q)
	if l <= p {
		return 2*k + 1
	}
	return 2*k + 2
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &h)
		a := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		fmt.Println(solve(n, h, a))
	}
}

package main

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

var (
	reader  = bufio.NewReader(os.Stdin)
	t, n, h int
)

func solve(n, h int, a []int) int {
	p, q := a[0], a[1]
	if p <= q {
		p, q = q, p
	}
	for i := 2; i < n; i++ {
		if a[i] >= p {
			q = p
			p = a[i]
		}
	}
	if h%(p+q) == 0 {
		return 2 * h / (p + q)
	}
	k := h / (p + q)
	l := h % (p + q)
	if l <= p {
		return 2*k + 1
	}
	return 2*k + 2
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &h)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		fmt.Println(solve(n, h, a))
	}
}

package main

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

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

	var t int

	for fmt.Fscan(in, &t); t > 0; t-- {
		var n, hp int
		fmt.Fscan(in, &n, &hp)

		max, premax := 0, 0

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

			if num >= max {
				premax = max
				max = num
			} else if num > premax {
				premax = num
			}
		}

		ans := hp / (max + premax)
		hp -= ans * (max + premax)
		ans *= 2

		if hp >= max {
			hp -= max
			ans++
		}

		if hp >= premax {
			hp -= premax
			ans++
		}

		fmt.Fprintln(out, ans)
	}
}

package main

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

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

	var t int

	for fmt.Fscan(in, &t); t > 0; t-- {
		var n, hp int
		fmt.Fscan(in, &n, &hp)

		max, premax := 0, 0

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

			if num >= max {
				premax = max
				max = num
			} else if num > premax {
				premax = num
			}
		}

		ans := hp / (max + premax)
		hp -= ans * (max + premax)
		ans *= 2

		cnt := hp / max
		hp -= cnt * max
		ans += cnt

		cnt = hp / premax
		hp -= cnt * premax

		ans += cnt

		fmt.Fprintln(out, ans)
	}
}

package main

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

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

	for i := 0; i < t; i++ {
		var n, H int
		fmt.Fscan(input, &n, &H)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
		}
		sort.Ints(arr)
		max1 := arr[n-1]
		max2 := arr[n-2]
		// H <= n1 * max1 + n2 * max2
		// H <= x * max1 + x * max2 ::: Case 1
		// H <= x * max1 + (x - 1) * max2 ::: Case 2

		s := max1 + max2
		d := (H + s - 1) / s
		if d*max1+(d-1)*max2 >= H {
			fmt.Println(2*d - 1)
		} else {
			fmt.Println(2 * d)
		}
	}
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N := 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		n, H, o := 0, 0, []int{}
		fmt.Fscan(f, &n, &H)
		for j := 0; j < n; j++ {
			p := 0
			fmt.Fscan(f, &p)
			o = append(o, p)
		}
		sort.Ints(o)
		if H/o[n-1] == 0 || H%o[n-1] == 0 {
			fmt.Fprintln(w, 1)
		} else if H%(o[n-1]+o[n-2]) == 0 {
			fmt.Fprintln(w, (H/(o[n-1]+o[n-2]))*2)
		} else {
			fmt.Fprintln(w, ((H/(o[n-1]+o[n-2]))*2)+1)
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N := 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		n, H, o := 0, 0, []int{}
		fmt.Fscan(f, &n, &H)
		for j := 0; j < n; j++ {
			p := 0
			fmt.Fscan(f, &p)
			o = append(o, p)
		}
		sort.Ints(o)
		if H/o[n-1] == 0 {
			fmt.Fprintln(w, 1)
		} else if H%(o[n-1]+o[n-2]) == 0 {
			fmt.Fprintln(w, (H/(o[n-1]+o[n-2]))*2)
		} else {
			fmt.Fprintln(w, ((H/(o[n-1]+o[n-2]))*2)+1)
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

//One day, Ahmed_Hossam went to Hemose and said "Let's solve a gym contest!". Hemose didn't want to do that, as he was playing Valorant, so he came up with a problem and told it to Ahmed to distract him. Sadly, Ahmed can't solve it... Could you help him?
//
//There is an Agent in Valorant, and he has 𝑛 weapons. The 𝑖-th weapon has a damage value 𝑎𝑖, and the Agent will face an enemy whose health value is 𝐻.
//
//The Agent will perform one or more moves until the enemy dies.
//
//In one move, he will choose a weapon and decrease the enemy's health by its damage value. The enemy will die when his health will become less than or equal to 0. However, not everything is so easy: the Agent can't choose the same weapon for 2 times in a row.
//
//What is the minimum number of times that the Agent will need to use the weapons to kill the enemy?
//
//Input
//Each test contains multiple test cases. The first line contains the number of test cases 𝑡 (1≤𝑡≤105). Description of the test cases follows.
//
//The first line of each test case contains two integers 𝑛 and 𝐻 (2≤𝑛≤103,1≤𝐻≤109) — the number of available weapons and the initial health value of the enemy.
//
//The second line of each test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109) — the damage values of the weapons.
//
//It's guaranteed that the sum of 𝑛 over all test cases doesn't exceed 2⋅105.
//
//Output
//For each test case, print a single integer — the minimum number of times that the Agent will have to use the weapons to kill the enemy.
var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)

func scanString() string { sc.Scan(); return sc.Text() }
func scanRunes() []rune  { return []rune(scanString()) }
func scanInt() int       { a, _ := strconv.Atoi(scanString()); return a }
func scanInt64() int64   { a, _ := strconv.ParseInt(scanString(), 10, 64); return a }
func scanFloat() float64 { a, _ := strconv.ParseFloat(scanString(), 64); return a }

func scanInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanInt()
	}
	return a
}

func main() {
	sc.Split(bufio.ScanWords)
	for t := scanInt(); t > 0; t-- {
		n, h := scanInt(), scanInt()
		a := scanInts(n)
		sort.Ints(a)
		times := h / (a[n-1] + a[n-2]) * 2
		if h%(a[n-1]+a[n-2]) > 0 {
			times += h%(a[n-1]+a[n-2])/a[n-1] + 1
		}

		fmt.Println(times)
	}
}


Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement is a string of length $$$n$$$ consisting of lowercase Latin latters. Vasya thinks that the statement can be considered hard if it contains a subsequence hard; otherwise the statement is easy. For example, hard, hzazrzd, haaaaard can be considered hard statements, while har, hart and drah are easy statements. Vasya doesn't want the statement to be hard. He may remove some characters from the statement in order to make it easy. But, of course, some parts of the statement can be crucial to understanding. Initially the ambiguity of the statement is $$$0$$$, and removing $$$i$$$-th character increases the ambiguity by $$$a_i$$$ (the index of each character is considered as it was in the original statement, so, for example, if you delete character r from hard, and then character d, the index of d is still $$$4$$$ even though you delete it from the string had).Vasya wants to calculate the minimum ambiguity of the statement, if he removes some characters (possibly zero) so that the statement is easy. Help him to do it!Recall that subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

The first line contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the statement. The second line contains one string $$$s$$$ of length $$$n$$$, consisting of lowercase Latin letters — the statement written by Vasya. The third line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 998244353$$$).

Print minimum possible ambiguity of the statement after Vasya deletes some (possibly zero) characters so the resulting statement is easy.

NoteIn the first example, first two characters are removed so the result is ardh.In the second example, $$$5$$$-th character is removed so the result is hhzawde.In the third example there's no need to remove anything.

package main

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

var dp [2][]uint64
var a uint64
var n int

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

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	fmt.Fscanf(reader, "%d\n", &n)
	var s string
	fmt.Fscanf(reader, "%s\n", &s)
	prev, cur := 0, 1
	dp[0], dp[1] = make([]uint64, 4), make([]uint64, 4)
	dp[0][1] = 0xffffffffffffffff
	dp[0][2] = 0xffffffffffffffff
	dp[0][3] = 0xffffffffffffffff
	for i := 1;i <= n;i ++ {
		fmt.Fscanf(reader, "%d ", &a)
		switch s[i - 1] {
		case 'h':
			dp[cur][0] = dp[prev][0] + a
			dp[cur][1] = min(dp[prev][0], dp[prev][1])
			dp[cur][2] = dp[prev][2]
			dp[cur][3] = dp[prev][3]
		case 'a':
			dp[cur][0] = dp[prev][0]
			dp[cur][1] = dp[prev][1] + a
			dp[cur][2] = min(dp[prev][1], dp[prev][2])
			dp[cur][3] = dp[prev][3]
		case 'r':
			dp[cur][0] = dp[prev][0]
			dp[cur][1] = dp[prev][1]
			dp[cur][2] = dp[prev][2] + a
			dp[cur][3] = min(dp[prev][2], dp[prev][3])
		case 'd':
			dp[cur][0] = dp[prev][0]
			dp[cur][1] = dp[prev][1]
			dp[cur][2] = dp[prev][2]
			dp[cur][3] = dp[prev][3] + a
        default:
			dp[cur][0] = dp[prev][0]
			dp[cur][1] = dp[prev][1]
			dp[cur][2] = dp[prev][2]
			dp[cur][3] = dp[prev][3]
		}
		prev, cur = cur, prev
	}
	fmt.Printf("%d", min(min(dp[prev][0], dp[prev][1]), min(dp[prev][2], dp[prev][3])))
}

package main

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

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

func main() {
	var n int
	var input string
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n%s\n", &n, &input)
	var h, a, r, d int64
	for i := 0; i < n; i++ {
		var cost int64
		fmt.Fscanf(buf, "%d", &cost)
		switch input[i] {
		case 'h':
			h += cost
		case 'a':
			a = min(a+cost, h)
		case 'r':
			r = min(r+cost, a)
		case 'd':
			d = min(d+cost, r)
		}
	}
	fmt.Println(d)
}

package main

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

func turn(c byte) int {
	switch c {
	case byte('h'):
		return 1
	case byte('a'):
		return 2
	case byte('r'):
		return 3
	case byte('d'):
		return 4
	default:
		return -1
	}
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var n int

	var str string
	fmt.Fscanf(buf, "%d\n", &n)
	fmt.Fscanf(buf, "%s\n", &str)
	bstr := []byte(str)
	mat := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &mat[i])
	}
	dp := make([]int64, 5)
	var ans int64 = -1
	for i := 0; i < 5; i++ {
		dp[i] = -1
	}
	dp[0] = 0
	for i := 0; i < n; i++ {
		tn := turn(bstr[i])
		if tn <= 0 {
			continue
		}

		if dp[tn-1] != -1 && (dp[tn] == -1 || dp[tn] > dp[tn-1]) {
			dp[tn] = dp[tn-1]
		}
		if dp[tn-1] != -1 {
			dp[tn-1] += int64(mat[i])
		}
	//	fmt.Println("-->", dp)
	}

	for i := 0; i < 4; i++ {
		if dp[i] >= 0 &&(ans == -1 || ans > dp[i] ){
			ans = dp[i]
		}
	}
	fmt.Printf("%d\n", ans)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol1096D(reader io.Reader, writer io.Writer) {
	const inf int64 = 0x3f3f3f3f3f3f3f3f
	min := func(a, b int64) int64 {
		if a <= b {
			return a
		}
		return b
	}
	mins := func(vals ...int64) int64 {
		ans := vals[0]
		for _, val := range vals[1:] {
			if val < ans {
				ans = val
			}
		}
		return ans
	}

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	var s string
	Fscan(in, &n, &s)
	costs := make([]int, n)
	for i := range costs {
		Fscan(in, &costs[i])
	}

	dp := [2][4]int64{}
	for i := 1; i <= n; i++ {
		cur, prev := i&1, (i-1)&1
		for j := range dp[cur] {
			dp[cur][j] = dp[prev][j]
		}
		cost := int64(costs[i-1])
		switch s[i-1] {
		case 'h':
			dp[cur][0] += cost
			dp[cur][1] = min(dp[cur][1], dp[prev][0])
		case 'a':
			dp[cur][1] += cost
			dp[cur][2] = min(dp[cur][2], dp[prev][1])
		case 'r':
			dp[cur][2] += cost
			dp[cur][3] = min(dp[cur][3], dp[prev][2])
		case 'd':
			dp[cur][3] += cost
		}
	}
	Fprint(out, mins(dp[n&1][:]...))
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol1096D(reader io.Reader, writer io.Writer) {
	mins := func(vals ...int64) int64 {
		ans := vals[0]
		for _, val := range vals[1:] {
			if val < ans {
				ans = val
			}
		}
		return ans
	}

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	var s string
	Fscan(in, &n, &s)
	costs := make([]int, n)
	for i := range costs {
		Fscan(in, &costs[i])
	}

	dp := [2][4]int64{}
	for i := 1; i <= n; i++ {
		i1, i0 := i&1, (i-1)&1
		for j := range dp[i1] {
			dp[i1][j] = dp[i0][j]
		}
		idx := strings.Index("hard", string(s[i-1]))
		if idx == -1 {
			continue
		}
		dp[i1][idx] += int64(costs[i-1])
		if idx < 3 && dp[i0][idx] < dp[i1][idx+1] {
			dp[i1][idx+1] = dp[i0][idx]
		}
	}
	Fprint(out, mins(dp[n&1][:]...))
}

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

package main

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

const N = 100010
var n int
var cost[N] int
var s string
var dp[N][2][2][2] int64

type state struct {
	x int
	y int
	z int
}

func transform(a state, digit int) state {
	if digit == 0 {
		return a
	}
	if a.z == 1 && digit == 4 {
		return state{-1, -1, -1}
	}

	var newState = state{0, 0, 0}
	if a.x == 1 || digit == 1 {
		newState.x = 1
	}
	if a.y == 1 || (a.x == 1 && digit == 2) {
		newState.y = 1
	}
	if a.z == 1 || (a.y == 1 && digit == 3) {
		newState.z = 1
	}
	return newState
}

func getDigit(ch uint8) int {
	if ch == 'h' {
		return 1
	}
	if ch == 'a' {
		return 2
	}
	if ch == 'r' {
		return 3
	}
	if ch == 'd' {
		return 4
	}
	return 0
}

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

func main()  {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	fmt.Fscanf(reader, "%d\n", &n)
	fmt.Fscanf(reader, "%s\n", &s)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d ", &cost[i])
	}

	for i := 0; i <= n; i++ {
		for u := 0; u < 2; u++ {
			for v := 0; v < 2; v++ {
				for z := 0; z < 2; z++ {
					dp[i][u][v][z] = int64(1e18)
				}
			}
		}
	}

	dp[0][0][0][0] = 0

	for i := 0; i < n; i++ {
		var digit = getDigit(s[i])

		for u := 0; u < 2; u++ {
			for v := 0; v < 2; v++ {
				for k := 0; k < 2; k++ {
					dp[i + 1][u][v][k] = min(dp[i + 1][u][v][k], dp[i][u][v][k] + int64(cost[i + 1]))
					var newState = transform(state{u, v, k}, digit)
					if newState.x == -1 {
						continue
					}
					dp[i + 1][newState.x][newState.y][newState.z] = min(dp[i + 1][newState.x][newState.y][newState.z],
						dp[i][u][v][k])
				}
			}
		}
	}

	var ret = int64(1e18)
	for u := 0; u < 2; u++ {
		for v := 0; v < 2; v++ {
			for z := 0; z < 2; z++ {
				ret = min(ret, dp[n][u][v][z])
			}
		}
	}
	fmt.Fprintf(writer, "%d", ret)
}

package main

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

const N = 100010
var n int
var cost[N] int
var s string
var dp[N][2][2][2] int64

type state struct {
	x int
	y int
	z int
}

func transform(a state, digit int) state {
	if digit == 0 {
		return a
	}
	if a.z == 1 && digit == 4 {
		return state{-1, -1, -1}
	}

	var newState = state{0, 0, 0}
	if a.x == 1 || digit == 1 {
		newState.x = 1
	}
	if a.y == 1 || (a.x == 1 && digit == 2) {
		newState.y = 1
	}
	if a.z == 1 || (a.y == 1 && digit == 3) {
		newState.z = 1
	}
	return newState
}

func getDigit(ch uint8) int {
	if ch == 'h' {
		return 1
	}
	if ch == 'a' {
		return 2
	}
	if ch == 'r' {
		return 3
	}
	if ch == 'd' {
		return 4
	}
	return 0
}

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

func main()  {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	fmt.Fscanf(reader, "%d\n", &n)
	fmt.Fscanf(reader, "%s\n", &s)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(reader, "%d ", &cost[i])
	}

	for i := 0; i <= n; i++ {
		for u := 0; u < 2; u++ {
			for v := 0; v < 2; v++ {
				for z := 0; z < 2; z++ {
					dp[i][u][v][z] = int64(1e18)
				}
			}
		}
	}

	dp[0][0][0][0] = 0

	for i := 0; i < n; i++ {
		var digit = getDigit(s[i])

		for u := 0; u < 2; u++ {
			for v := 0; v < 2; v++ {
				for k := 0; k < 2; k++ {
					var newState = transform(state{u, v, k}, digit)
					if newState.x == -1 {
						continue
					}

					dp[i + 1][u][v][k] = min(dp[i + 1][u][v][k], dp[i][u][v][k] + int64(cost[i + 1]))
					dp[i + 1][newState.x][newState.y][newState.z] = min(dp[i + 1][newState.x][newState.y][newState.z],
						dp[i][u][v][k])
				}
			}
		}
	}

	var ret = int64(1e18)
	for u := 0; u < 2; u++ {
		for v := 0; v < 2; v++ {
			for z := 0; z < 2; z++ {
				ret = min(ret, dp[n][u][v][z])
			}
		}
	}
	fmt.Fprintf(writer, "%d", ret)
}


Valera loves segments. He has recently come up with one interesting problem.The Ox axis of coordinates has n segments, the i-th segment starts in position li and ends in position ri (we will mark it as [li, ri]). Your task is to process m queries, each consists of number cnti and a set of cnti coordinates of points located on the Ox axis. The answer to the query is the number of segments, such that each of them contains at least one point from the query. Segment [l, r] contains point q, if l ≤ q ≤ r.Valera found the solution of this problem too difficult. So he asked you to help him. Help Valera.

The first line contains two integers n, m (1 ≤ n, m ≤ 3·105) — the number of segments on the axis of coordinates and the number of queries.  Next n lines contain the descriptions of the segments. The i-th line contains two positive integers li, ri (1 ≤ li ≤ ri ≤ 106) — the borders of the i-th segment. Next m lines contain the description of the queries, one per line. Each line starts from integer cnti (1 ≤ cnti ≤ 3·105) — the number of points in the i-th query. Then the line contains cnti distinct positive integers p1, p2, ..., pcnti (1 ≤ p1 < p2 < ... < pcnti ≤ 106) — the coordinates of points in the i-th query. It is guaranteed that the total number of points in all queries doesn't exceed 3·105.

Print m non-negative integers, where the i-th number is the response to the i-th query.

package main

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

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

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

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

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

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

var MAX int = 1e6 + 2

func solve(n, m int) {
	segment := make([][]int, MAX)
	qSegment := make([][][2]int, MAX)
	tree = make([]int, MAX)
	ans := make([]int, m)
	for i := 0; i < n; i++ {
		var l, r int
		I(&l, &r)
		segment[r] = append(segment[r], l)
	}
	for i := 0; i < m; i++ {
		var cnt int
		I(&cnt)
		prev := 0
		var p int
		for j := 0; j < cnt; j++ {
			I(&p)
			qSegment[p] = append(qSegment[p], [2]int{i, prev})
			prev = p
		}
		qSegment[MAX-1] = append(qSegment[MAX-1], [2]int{i, prev})
	}
	for i := 0; i < MAX; i++ {
		for _, qs := range qSegment[i] {
			ans[qs[0]] += sum(i) - sum(qs[1])
		}
		for _, s := range segment[i] {
			update(s, 1)
		}
	}
	for _, a := range ans {
		O(n-a, "\n")
	}
}

var tree []int

func update(i, val int) {
	for i < len(tree) {
		tree[i] += val
		i += i & -i
	}
}

func sum(i int) int {
	var s int
	for i > 0 {
		s += tree[i]
		i -= i & -i
	}
	return s
}

package main

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

type Pair struct {
	l, r int
}

type BITTree struct {
	n int
	s []int
}

func NewBITTree(n int) *BITTree {
	t := BITTree{n: n, s: make([]int, n+1)}
	return &t
}

func (t *BITTree) Update(x, val int) {
	x++
	for ; x <= t.n; x += x & -x {
		t.s[x] += val
	}
}

func (t *BITTree) GetSum(x int) int {
	x++
	sum := 0
	for ; x > 0; x -= x & -x {
		//fmt.Printf("x = %d, n = %d\n", x, t.n)
		sum += t.s[x]
	}
	return sum
}

// Find the number of segments strictly fall in the selected pairs.

func main() {
	stdin := bufio.NewReader(os.Stdin)
	stdout := bufio.NewWriter(os.Stdout)

	var n, m int
	fmt.Fscan(stdin, &n, &m)
	segments := make([]Pair, n)
	for i, _ := range segments {
		fmt.Fscan(stdin, &segments[i].l, &segments[i].r)
	}
	queries := make([][]int, m)
	for i, _ := range queries {
		var cnt int
		fmt.Fscan(stdin, &cnt)
		queries[i] = make([]int, cnt)
		for j := 0; j < cnt; j++ {
			fmt.Fscan(stdin, &queries[i][j])
		}
	}
	// Enumerate all selected pairs. map[left][right]
	seps := make(map[int]map[int]int)
	addSep := func(l, r int) {
		if _, ok := seps[l]; !ok {
			seps[l] = make(map[int]int)
		}
		seps[l][r] = 0
	}
	for _, q := range queries {
		left := 0
		for _, v := range q {
			if left < v {
				addSep(left, v-1)
			}
			left = v + 1
		}
		if left <= 1000000 {
			addSep(left, 1000000)
		}
	}

	segs := make(map[int]map[int]int)
	for _, seg := range segments {
		if _, ok := segs[seg.l]; !ok {
			segs[seg.l] = make(map[int]int)
		}
		segs[seg.l][seg.r] += 1
	}
	t := NewBITTree(1000001)
	for l := 1000000; l >= 0; l-- {
		if m, ok := segs[l]; ok {
			for r, count := range m {
				t.Update(r, count)
			}
		}
		if sep, ok := seps[l]; ok {
			for r, _ := range sep {
				sep[r] = t.GetSum(r) - t.GetSum(l - 1)
			}
		}
	}

	for _, q := range queries {
		left := 0
		sum := 0
		for _, v := range q {
			if left < v {
				sum += seps[left][v-1]
			}
			left = v + 1
		}
		if left <= 1000000 {
			sum += seps[left][1000000]
		}
		exist := n - sum
		fmt.Fprintf(stdout, "%d\n", exist)
	}
	stdout.Flush()
}


You are given a matrix $$$a$$$ of size $$$n \times m$$$ consisting of integers.You can choose no more than $$$\left\lfloor\frac{m}{2}\right\rfloor$$$ elements in each row. Your task is to choose these elements in such a way that their sum is divisible by $$$k$$$ and this sum is the maximum.In other words, you can choose no more than a half (rounded down) of elements in each row, you have to find the maximum sum of these elements divisible by $$$k$$$.Note that you can choose zero elements (and the sum of such set is $$$0$$$).

The first line of the input contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 \le n, m, k \le 70$$$) — the number of rows in the matrix, the number of columns in the matrix and the value of $$$k$$$. The next $$$n$$$ lines contain $$$m$$$ elements each, where the $$$j$$$-th element of the $$$i$$$-th row is $$$a_{i, j}$$$ ($$$1 \le a_{i, j} \le 70$$$).

Print one integer — the maximum sum divisible by $$$k$$$ you can obtain.

NoteIn the first example, the optimal answer is $$$2$$$ and $$$4$$$ in the first row, $$$5$$$ and $$$2$$$ in the second row and $$$7$$$ and $$$4$$$ in the third row. The total sum is $$$2 + 4 + 5 + 2 + 7 + 4 = 24$$$.

package main

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

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

var n, m, k int
var a [80][80]int
var dp [80][80][80][80]int

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

func main() {
	_, _ = fmt.Fscan(_r, &n, &m, &k)
	for i := 1; i <= n; i++ {
		for j := 1; j <= m; j++ {
			_, _ = fmt.Fscan(_r, &a[i][j])
		}
	}
	for x := 0; x <= n; x++ {
		for y := 0; y <= m; y++ {
			for cnt := 0; cnt <= m; cnt++ {
				for mod := 0; mod <= k; mod++ {
					dp[x][y][cnt][mod] = -1e9
				}
			}
		}
	}

	dp[1][1][0][0] = 0
	for i := 1; i <= n; i++ {
		for j := 1; j <= m; j++ {
			for cnt := 0; cnt <= m/2; cnt++ {
				for mod := 0; mod < k; mod++ {
					if dp[i][j][cnt][mod] == -1e9 {
						continue
					}
					var nxt_i, nxt_j int
					if j == m {
						nxt_i = i + 1
					} else {
						nxt_i = i
					}
					if j == m {
						nxt_j = 1
					} else {
						nxt_j = j + 1
					}

					if i != nxt_i {
						dp[nxt_i][nxt_j][0][mod] = max(dp[nxt_i][nxt_j][0][mod], dp[i][j][cnt][mod])
					} else {
						dp[nxt_i][nxt_j][cnt][mod] = max(dp[nxt_i][nxt_j][cnt][mod], dp[i][j][cnt][mod])
					}

					if cnt+1 <= m/2 {
						nxt_mod := (mod + a[i][j]) % k
						if i != nxt_i {
							dp[nxt_i][nxt_j][0][nxt_mod] = max(dp[nxt_i][nxt_j][0][nxt_mod], dp[i][j][cnt][mod]+a[i][j])
						} else {
							dp[nxt_i][nxt_j][cnt+1][nxt_mod] = max(dp[nxt_i][nxt_j][cnt+1][nxt_mod], dp[i][j][cnt][mod]+a[i][j])
						}
					}
				}
			}
		}
	}

	fmt.Println(max(0, dp[n+1][1][0][0]))
}

package main

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

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

	var n, m, K, v int
	Fscan(in, &n, &m, &K)
	dp := make([][]int, n)
	d1 := make([][]int, m/2+1)
	for i := 0; i < n; i++ {
		for c := range d1 {
			d1[c] = make([]int, K)
			for k := range d1[c] {
				d1[c][k] = -1e9
			}
		}
		d1[0][0] = 0
		for j := 0; j < m; j++ {
			Fscan(in, &v)
			for c := m/2 - 1; c >= 0; c-- {
				for k, dv := range d1[c] {
					if dv >= 0 {
						d1[c+1][(k+v)%K] = max(d1[c+1][(k+v)%K], dv+v)
					}
				}
			}
		}
		d2 := make([]int, K)
		for k := range d2 {
			d2[k] = -1e9
		}
		for _, ds := range d1 {
			for k, dv := range ds {
				d2[k] = max(d2[k], dv)
			}
		}
		if i == 0 {
			dp[i] = d2
		} else {
			dp[i] = make([]int, K)
			for k := range dp[i] {
				dp[i][k] = -1e9
			}
			for k, v := range dp[i-1] {
				for k2, w := range d2 {
					dp[i][(k+k2)%K] = max(dp[i][(k+k2)%K], v+w)
				}
			}
		}
	}
	Fprint(out, dp[n-1][0])
}

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


Grandma Capa has decided to knit a scarf and asked Grandpa Sher to make a pattern for it, a pattern is a string consisting of lowercase English letters. Grandpa Sher wrote a string $$$s$$$ of length $$$n$$$.Grandma Capa wants to knit a beautiful scarf, and in her opinion, a beautiful scarf can only be knit from a string that is a palindrome. She wants to change the pattern written by Grandpa Sher, but to avoid offending him, she will choose one lowercase English letter and erase some (at her choice, possibly none or all) occurrences of that letter in string $$$s$$$.She also wants to minimize the number of erased symbols from the pattern. Please help her and find the minimum number of symbols she can erase to make string $$$s$$$ a palindrome, or tell her that it's impossible. Notice that she can only erase symbols equal to the one letter she chose.A string is a palindrome if it is the same from the left to the right and from the right to the left. For example, the strings 'kek', 'abacaba', 'r' and 'papicipap' are palindromes, while the strings 'abb' and 'iq' are not.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The next $$$2 \cdot t$$$ lines contain the description of test cases. The description of each test case consists of two lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the string. The second line of each test case contains the string $$$s$$$ consisting of $$$n$$$ lowercase English letters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case print the minimum number of erased symbols required to make the string a palindrome, if it is possible, and $$$-1$$$, if it is impossible.

5
8
abcaacab
6
xyzxyz
4
abba
8
rprarlap
10
khyyhhyhky

NoteIn the first test case, you can choose a letter 'a' and erase its first and last occurrences, you will get a string 'bcaacb', which is a palindrome. You can also choose a letter 'b' and erase all its occurrences, you will get a string 'acaaca', which is a palindrome as well.In the second test case, it can be shown that it is impossible to choose a letter and erase some of its occurrences to get a palindrome.In the third test case, you don't have to erase any symbols because the string is already a palindrome.

package main

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

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

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {
		var s string
		var n int
		fmt.Fscan(r, &n, &s)
		f, res := false, math.MaxInt32
		for i := 0; i < 26; i++ {
			c := byte(i + 'a')
			left, right := 0, n-1
			found, ans := true, 0
			for left < right {
				if s[left] == s[right] {
					left++
					right--
					continue
				}
				if s[left] == c || s[right] == c {
					if s[left] == c {
						left++
						ans++
						continue
					}
					if s[right] == c {
						right--
						ans++
					}
					continue
				}
				found = false
				break
			}
			if found {
				f = true
				if res > ans {
					res = ans
				}
			}
		}
		if f {
			fmt.Fprintln(w, res)
		} else {
			fmt.Fprintln(w, -1)
		}
	}
}

package main

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

/*

 */

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	var s string
	scanf("%d\n%s\n", &n, &s)

	minCnt := maxInt

	for r := byte('a'); r <= 'z'; r++ {
		cnt := 0

		i, j := 0, n-1
		for i < j {
			if s[i] == s[j] {
				i++
				j--
			} else if s[i] == r {
				i++
				cnt++
			} else if s[j] == r {
				j--
				cnt++
			} else {
				cnt = -1
				break
			}
		}

		if cnt >= 0 {
			minCnt = min(minCnt, cnt)
		}
	}

	if minCnt == maxInt {
		minCnt = -1
	}

	fmt.Fprintln(writer, minCnt)
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

package main

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

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

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

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

		min := n
		for i := 0; i < 26; i++ {
			ch := byte(i + 'a')
			l, r := 0, n-1
			var tot int
			for r-l > 0 {
				if s[l] == s[r] {
					l++
					r--
				} else if s[l] == ch {
					l++
					tot++
				} else if s[r] == ch {
					r--
					tot++
				} else if s[l] != s[r] {
					break
				}
			}

			if r-l <= 0 && tot < min {
				min = tot
			}
		}

		if min == n {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, min)
		}
	}
}

package main

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

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

	t := readInt(reader)

	for t > 0 {
		n := readInt(reader)
		s := []rune(readString(reader))

		var (
			min = n
		)
		for c := 'a'; c <= 'z'; c++ {
			var (
				l = 0
				r = n-1
				cnt = 0
			)
			for l < r {
				if s[l] != s[r] {
					if s[l] == c {
						l++
						cnt++
					} else if s[r] == c {
						r--
						cnt++
					} else {
						break
					}
				} else {
					l++
					r--
				}
			}
			if min > cnt && (l == r || l > r) {
				min = cnt
			}
		}

		if min == n {
			fmt.Fprintln(writer, -1)
		} else {
			fmt.Fprintln(writer, min)
		}

		t--
	}

	writer.Flush()
}

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, 64)
		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 n int

var s string

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

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

		fmt.Fscan(r, &s)

		ans := solve()

		fmt.Println(ans)
	}
}

func solve() int {
	var ans int = 1e9

	mp := make(map[byte]int)

	for i := 0; i < n; i++ {
		mp[s[i]]++
	}

	for k, _ := range mp {

		var c int = 0

		bad := false

		for i, j := 0, n - 1; i < j;  {
			if s[i] == s[j] {
				i++
				j--
				continue
			}
			if s[i] == k {
				i++
				c++

			} else if s[j] == k {
				j--
				c++
			} else {
				bad = true
				break
			}
		}
		if !bad {
			ans = min(ans, c)
		}
	}

	if ans == 1e9 {
		return -1
	}

	return ans
}

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


Apart from plush toys, Imp is a huge fan of little yellow birds!  To summon birds, Imp needs strong magic. There are n trees in a row on an alley in a park, there is a nest on each of the trees. In the i-th nest there are ci birds; to summon one bird from this nest Imp needs to stay under this tree and it costs him costi points of mana. However, for each bird summoned, Imp increases his mana capacity by B points. Imp summons birds one by one, he can summon any number from 0 to ci birds from the i-th nest. Initially Imp stands under the first tree and has W points of mana, and his mana capacity equals W as well. He can only go forward, and each time he moves from a tree to the next one, he restores X points of mana (but it can't exceed his current mana capacity). Moving only forward, what is the maximum number of birds Imp can summon?

The first line contains four integers n, W, B, X (1 ≤ n ≤ 103, 0 ≤ W, B, X ≤ 109) — the number of trees, the initial points of mana, the number of points the mana capacity increases after a bird is summoned, and the number of points restored when Imp moves from a tree to the next one. The second line contains n integers c1, c2, ..., cn (0 ≤ ci ≤ 104) — where ci is the number of birds living in the i-th nest. It is guaranteed that . The third line contains n integers cost1, cost2, ..., costn (0 ≤ costi ≤ 109), where costi is the mana cost to summon a bird from the i-th nest.

Print a single integer — the maximum number of birds Imp can summon.

NoteIn the first sample base amount of Imp's mana is equal to 12 (with maximum capacity also equal to 12). After he summons two birds from the first nest, he loses 8 mana points, although his maximum capacity will not increase (since B = 0). After this step his mana will be 4 of 12; during the move you will replenish 4 mana points, and hence own 8 mana out of 12 possible. Now it's optimal to take 4 birds from the second nest and spend 8 mana. The final answer will be — 6.In the second sample the base amount of mana is equal to 1000. The right choice will be to simply pick all birds from the last nest. Note that Imp's mana doesn't restore while moving because it's initially full.

package main

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

var dpCur, dpPrev [10001]uint64
func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	var n, w, b, x uint64;
	fmt.Fscanf(reader, "%d %d %d %d\n", &n, &w, &b, &x)
	c := make([]uint64, n + 1)
	cost := make([]uint64, n + 1)
	for i := uint64(0);i < n - 1;i ++ {
		fmt.Fscanf(reader, "%d ", &c[i + 1])
	}
	fmt.Fscanf(reader, "%d\n", &c[n])
	for i := uint64(0);i < n - 1;i ++ {
		fmt.Fscanf(reader, "%d ", &cost[i + 1])
	}
	fmt.Fscanf(reader, "%d\n", &cost[n])

	dpPrev[0] = w + 1

	totalBirds := uint64(0)
	for i := uint64(1);i <= n;i ++ {
		totalBirds += c[i]
		var refresh uint64
		if i != 1 {
			refresh = x
		}
		for j := uint64(0);j <= totalBirds;j ++ {
			dpCur[j] = 0
			for k := uint64(0);k <= c[i] && k <= j;k ++ {
				if dpPrev[j - k] != 0 {
					mana := dpPrev[j - k] + refresh
					if mana > w + 1 + (j - k) * b {
						mana = w + 1 + (j - k) * b
					}
					manaCost := k * cost[i]
					if mana >= manaCost + 1 && dpCur[j] < mana - manaCost{
						dpCur[j] = mana - manaCost
					}
				}
			}
		}
		dpCur, dpPrev = dpPrev, dpCur
	}
	for i := totalBirds;i >= 0;i -- {
		if dpPrev[i] != 0 {
			fmt.Printf("%d", i)
			return
		}
	}
}

package main

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

var dpCur, dpPrev [10001]int
func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	var n, w, b, x int;
	fmt.Fscanf(reader, "%d %d %d %d\n", &n, &w, &b, &x)
	c := make([]int, n + 1)
	cost := make([]int, n + 1)
	for i := 0;i < n - 1;i ++ {
		fmt.Fscanf(reader, "%d ", &c[i + 1])
	}
	fmt.Fscanf(reader, "%d\n", &c[n])
	for i := 0;i < n - 1;i ++ {
		fmt.Fscanf(reader, "%d ", &cost[i + 1])
	}
	fmt.Fscanf(reader, "%d\n", &cost[n])

	dpPrev[0] = w + 1

	totalBirds := 0
	for i := 1;i <= n;i ++ {
		totalBirds += c[i]
		var refresh int
		if i != 1 {
			refresh = x
		}
		for j := 0;j <= totalBirds;j ++ {
			dpCur[j] = 0
			for k := 0;k <= c[i] && k <= j;k ++ {
				if dpPrev[j - k] != 0 {
					mana := dpPrev[j - k] + refresh
					if mana > w + 1 + (j - k) * b {
						mana = w + 1 + (j - k) * b
					}
					manaCost := k * cost[i]
					if mana >= manaCost + 1 && dpCur[j] < mana - manaCost{
						dpCur[j] = mana - manaCost
					}
				}
			}
		}
		dpCur, dpPrev = dpPrev, dpCur
	}
	for i := totalBirds;i >= 0;i -- {
		if dpPrev[i] != 0 {
			fmt.Printf("%d", i)
			return
		}
	}
}