You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \le p, f \le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \le cnt_s, cnt_w \le 2 \cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \le s, w \le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \cdot 10^5$$$.

For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.

NoteIn the first test case:   you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \cdot 5 + 3 \cdot 6 = 33 \le 33$$$  and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \cdot 5 + 2 \cdot 6 = 27 \le 27$$$.  $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \cdot 10 + 5 \cdot 10 \le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \cdot 5 \le 19$$$.

package main

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

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

	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--
		p, f := readTwoNums(reader)
		cnts, cntw := readTwoNums(reader)
		s, w := readTwoNums(reader)
		res := solve(p, f, cnts, cntw, s, w)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

func solve(p, f int, cnts int, cntw int, s, w int) int64 {
	// a * s + b * w <= p
	// c * s + d * w <= f
	// a + c <= cnts
	// b + d <= cntw
	// max a + b + c + d
	//if cnts > cntw {
	//	cnts, cntw = cntw, cnts
	//	s, w = w, s
	//}
	P := int64(p)
	F := int64(f)
	S := int64(s)
	W := int64(w)
	var best int64
	for a := 0; a <= cnts; a++ {
		A := int64(a)
		if A*S > P {
			break
		}
		B := min((P-A*S)/W, int64(cntw))
		var C, D int64
		if S <= W {
			C = min(int64(cnts)-A, F/S)
			D = min(int64(cntw)-B, (F-C*S)/W)
		} else {
			D = min(int64(cntw)-B, F/W)
			C = min(int64(cnts)-A, (F-D*W)/S)
		}

		if C < 0 || D < 0 || C*S+D*W > F {
			continue
		}

		if A+B+C+D > best {
			best = A + B + C + D
		}
	}
	return best
}

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

package main

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

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	for t := io.NextInt(); t > 0; t-- {
		p, f := io.NextInt(), io.NextInt()
		cs, cw := io.NextInt(), io.NextInt()
		s, w := io.NextInt(), io.NextInt()
		if p < f {
			p, f = f, p
		}
		if s > w {
			s, w, cs, cw = w, s, cw, cs
		}

		ans := 0
		for pCarryS := min(cs, p/s); pCarryS >= 0; pCarryS-- {
			fCarryS := min(cs-pCarryS, f/s)
			pCarryW := min(cw, (p-pCarryS*s)/w)
			fCarryW := min(cw-pCarryW, (f-fCarryS*s)/w)
			ans = max(ans, pCarryS+pCarryW+fCarryS+fCarryW)
		}
		io.Println(ans)
	}
}

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}

func min(i, j int) int {
	if i < j {
		return i
	}
	return j
}

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

package main

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

func cost(s, w uint64, cs, cw, p, f uint64) uint64 {
	var x1, max uint64
	for x1 = 0; x1 <= cs; x1++ {
		if p >= s*x1 {
			y1 := (p - s*x1) / w

			if y1 > cw {
				y1 = cw
			}

			x2 := (cs - x1)

			if f < s*x2 {
				x2 = f / s
			}

			y2 := (f - s*x2) / w

			if y2 > (cw - y1) {
				y2 = (cw - y1)
			}

			if max < (x1 + y1 + x2 + y2) {
				max = (x1 + y1 + x2 + y2)
			}
		}
	}

	return max
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() uint64 {
		sc.Scan()
		i, _ := strconv.ParseUint(sc.Text(), 10, 64)
		return i
	}

	for t := in(); t > 0; t-- {
		p, f := in(), in()

		cs, cw := in(), in()

		s, w := in(), in()

		val := make([]uint64, 4)

		val[1] = cost(s, w, cs, cw, p, f)
		val[2] = cost(s, w, cs, cw, f, p)
		val[3] = cost(w, s, cw, cs, f, p)
		val[0] = cost(w, s, cw, cs, p, f)

		max := val[0]

		for _, v := range val {
			if v > max {
				max = v
			}
		}

		fmt.Println(max)

	}

}

/*
URL:
https://codeforces.com/contest/1400/problem/B
*/

package main

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

var (
	t int

	p, f       int
	cntS, cntW int
	s, w       int
)

func main() {
	t = readi()
	for tc := 0; tc < t; tc++ {
		p, f = readi2()
		cntS, cntW = readi2()
		s, w = readi2()

		solve()
	}
}

func solve() {
	if p < f {
		p, f = f, p
	}

	if s > w {
		s, w = w, s
		cntS, cntW = cntW, cntS
	}

	tmp := p/s + f/s
	if tmp < cntS {
		fmt.Println(tmp)
		return
	}

	ans := Min(tmp, cntS)
	for i := 0; i <= cntS; i++ {
		P := i * s
		F := (cntS - i) * s

		if P > p || F > f {
			continue
		}

		rp := p - P
		rf := f - F

		plus := rp/w + rf/w
		ChMax(&ans, cntS+Min(cntW, plus))
		// PrintfDebug("%d\n", ans)
	}
	fmt.Println(ans)
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

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

/********** common constants **********/

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

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

/********** bufio setting **********/

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

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

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

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

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

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

/*********** Input ***********/

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

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

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

// readi returns an integer.
func readi() int {
	return int(readInt64())
}
func readi2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func readi3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func readi4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// readll returns as integer as int64.
func readll() int64 {
	return readInt64()
}
func readll2() (int64, int64) {
	return readInt64(), readInt64()
}
func readll3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func readll4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

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

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

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

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

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

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

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

/*********** Output ***********/

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

	return res
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1400B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	min := func(a, b int64) int64 {
		if a < b {
			return a
		}
		return b
	}

	var T, c, d, n, m, w, z int64
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &c, &d, &n, &m, &w, &z)
		if w > z {
			n, m, w, z = m, n, z, w
		}
		ans := int64(0)
		for i := int64(0); i <= n && i*w <= c; i++ {
			j := n - i
			if j*w > d {
				j = d / w
			}

			if s := i + j + min(m, (c-i*w)/z+(d-j*w)/z); s > ans {
				ans = s
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1400B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	min := func(a, b int64) int64 {
		if a < b {
			return a
		}
		return b
	}

	var T, c, d, n, m, v, w int64
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &c, &d, &n, &m, &v, &w)
		if v > w {
			n, m, v, w = m, n, w, v
		}
		ans := int64(0)
		for i := int64(0); i <= n && i*v <= c; i++ {
			j := min(n-i, d/v)
			if s := i + j + min(m, (c-i*v)/w+(d-j*v)/w); s > ans {
				ans = s
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}

	var T, c, d, n, m, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &c, &d, &n, &m, &v, &w)
		if v > w {
			n, m, v, w = m, n, w, v
		}
		ans := 0
		for i := 0; i <= n && i*v <= c; i++ {
			j := min(n-i, d/v)
			if s := i + j + min(m, (c-i*v)/w+(d-j*v)/w); s > ans {
				ans = s
			}
		}
		Fprintln(out, ans)
	}
}

package main

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

var reader = bufio.NewReader(os.Stdin)

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

func main() {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var p, f int
		fmt.Fscan(reader, &p, &f)

		var cnts, cntw int
		fmt.Fscan(reader, &cnts, &cntw)

		var s, w int
		fmt.Fscan(reader, &s, &w)

		if s > w {
			tmp := s
			s = w
			w = tmp

			tmp = cnts
			cnts = cntw
			cntw = tmp
		}

		ans := 0
		for s1 := 0; s1 <= min(cnts, (p/s)); s1++ {
			w1 := min(cntw, (p-s1*s)/w)
			s2 := min(cnts-s1, f/s)
			w2 := min(cntw-w1, (f-(s2*s))/w)
			ans = max(ans, s1+w1+s2+w2)
		}

		fmt.Println(ans)
	}
}

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

	return b
}

func max(el ...int) int {
	ans := MinInt
	for _, e := range el {
		if e > ans {
			ans = e
		}
	}

	return ans
}

package main

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

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

	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--
		p, f := readTwoNums(reader)
		cnts, cntw := readTwoNums(reader)
		s, w := readTwoNums(reader)
		res := solve(p, f, cnts, cntw, s, w)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

func solve(p, f int, cnts int, cntw int, s, w int) int64 {
	// a * s + b * w <= p
	// c * s + d * w <= f
	// a + c <= cnts
	// b + d <= cntw
	// max a + b + c + d
	if cnts > cntw {
		cnts, cntw = cntw, cnts
		s, w = w, s
	}
	P := int64(p)
	F := int64(f)
	S := int64(s)
	W := int64(w)
	var best int64
	for a := 0; a <= cnts; a++ {
		A := int64(a)
		B := (P - A*S) / W
		if B < 0 {
			break
		}
		var C, D int64
		if S <= W {
			C = min(int64(cnts)-A, F/S)
			D = min(int64(cntw)-B, (F-C*S)/W)
		} else {
			D = min(int64(cntw)-B, F/W)
			C = min(int64(cnts)-A, (F-D*W)/S)
		}

		if A+B+C+D > best {
			best = A + B + C + D
		}
	}
	return best
}

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

package main

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

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

	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--
		p, f := readTwoNums(reader)
		cnts, cntw := readTwoNums(reader)
		s, w := readTwoNums(reader)
		res := solve(p, f, cnts, cntw, s, w)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

func solve(p, f int, cnts int, cntw int, s, w int) int64 {
	// a * s + b * w <= p
	// c * s + d * w <= f
	// a + c <= cnts
	// b + d <= cntw
	// max a + b + c + d
	if cnts > cntw {
		cnts, cntw = cntw, cnts
		s, w = w, s
		p, f = f, p
	}
	P := int64(p)
	F := int64(f)
	S := int64(s)
	W := int64(w)
	var best int64
	for a := 0; a <= cnts; a++ {
		A := int64(a)
		B := (P - A*S) / W
		if B < 0 {
			break
		}
		var C, D int64
		if S <= W {
			C = min(int64(cnts)-A, F/S)
			D = min(int64(cntw)-B, (F-C*S)/W)
		} else {
			D = min(int64(cntw)-B, F/W)
			C = min(int64(cnts)-A, (F-D*W)/S)
		}

		if A+B+C+D > best {
			best = A + B + C + D
		}
	}
	return best
}

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

package main

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

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

	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--
		p, f := readTwoNums(reader)
		cnts, cntw := readTwoNums(reader)
		s, w := readTwoNums(reader)
		res := solve(p, f, cnts, cntw, s, w)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

func solve(p, f int, cnts int, cntw int, s, w int) int64 {
	// a * s + b * w <= p
	// c * s + d * w <= f
	// a + c <= cnts
	// b + d <= cntw
	// max a + b + c + d
	//if cnts > cntw {
	//	cnts, cntw = cntw, cnts
	//	s, w = w, s
	//}
	P := int64(p)
	F := int64(f)
	S := int64(s)
	W := int64(w)
	var best int64
	for a := 0; a <= cnts; a++ {
		A := int64(a)
		if A*S > P {
			break
		}
		B := (P - A*S) / W
		var C, D int64
		if S <= W {
			C = min(int64(cnts)-A, F/S)
			D = min(int64(cntw)-B, (F-C*S)/W)
		} else {
			D = min(int64(cntw)-B, F/W)
			C = min(int64(cnts)-A, (F-D*W)/S)
		}

		if A+B+C+D > best {
			best = A + B + C + D
		}
	}
	return best
}

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

package main

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

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

	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--
		p, f := readTwoNums(reader)
		cnts, cntw := readTwoNums(reader)
		s, w := readTwoNums(reader)
		res := solve(p, f, cnts, cntw, s, w)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

func solve(p, f int, cnts int, cntw int, s, w int) int64 {
	// a * s + b * w <= p
	// c * s + d * w <= f
	// a + c <= cnts
	// b + d <= cntw
	// max a + b + c + d
	//if cnts > cntw {
	//	cnts, cntw = cntw, cnts
	//	s, w = w, s
	//}
	P := int64(p)
	F := int64(f)
	S := int64(s)
	W := int64(w)
	var best int64
	for a := 0; a <= cnts; a++ {
		A := int64(a)
		if A*S > P {
			break
		}
		B := (P - A*S) / W
		var C, D int64
		if S <= W {
			C = min(int64(cnts)-A, F/S)
			D = min(int64(cntw)-B, (F-C*S)/W)
		} else {
			D = min(int64(cntw)-B, F/W)
			C = min(int64(cnts)-A, (F-D*W)/S)
		}

		if C < 0 || D < 0 {
			continue
		}

		if A+B+C+D > best {
			best = A + B + C + D
		}
	}
	return best
}

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

package main

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

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	for t := io.NextInt(); t > 0; t-- {
		p, f := io.NextInt(), io.NextInt()
		cs, cw := io.NextInt(), io.NextInt()
		s, w := io.NextInt(), io.NextInt()
		if p < f {
			p, f = f, p
		}
		if s > w {
			s, w, cs, cw = w, s, cw, cs
		}

		ans := 0
		canCarry := p/s + f/s
		if canCarry <= cs {
			ans = canCarry
		} else {
			pCarry := min(cs, p/s)
			fCarry := cs - pCarry
			for ; pCarry >= 0; pCarry, fCarry = pCarry-1, fCarry+1 {
				pCarryW := min(cw, (p-pCarry*s)/w)
				fCarryW := min(cw-pCarryW, (f-fCarry*s)/w)
				ans = max(ans, pCarry+pCarryW+fCarry+fCarryW)
			}
		}
		io.Println(ans)
	}
}

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}

func min(i, j int) int {
	if i < j {
		return i
	}
	return j
}

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

package main

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

func cost(s, w uint64, cs, cw, p *uint64) uint64 {

	var i, max, x1, y1 uint64

	for i = 0; i <= *cs; i++ {
		if *p >= s*i {
			j := (*p - s*i) / w

			if j > *cw {
				j = *cw
			}

			v := i + j

			if v > max {
				x1 = i
				y1 = j
				max = v
			}
		} else {
			break
		}
	}

	*cs -= x1
	*cw -= y1

	// fmt.Println("--->", max, x1, y1)

	*p -= max

	return x1 + y1
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() uint64 {
		sc.Scan()
		i, _ := strconv.ParseUint(sc.Text(), 10, 64)
		return i
	}

	for t := in(); t > 0; t-- {
		p, f := in(), in()

		cs, cw := in(), in()

		s, w := in(), in()

		cst := make([]uint64, 4)

		cs1, cw1 := cs, cw
		p1, f1 := p, f
		cst[0] = cost(s, w, &cs1, &cw1, &p1) +
			cost(s, w, &cs1, &cw1, &f1)

		cs1, cw1 = cs, cw
		p1, f1 = p, f
		cst[1] = cost(s, w, &cs1, &cw1, &f1) +
			cost(s, w, &cs1, &cw1, &p1)

		cs1, cw1 = cs, cw
		p1, f1 = p, f
		cst[2] = cost(w, s, &cw1, &cs1, &f1) +
			cost(w, s, &cw1, &cs1, &p1)

		cs1, cw1 = cs, cw
		p1, f1 = p, f
		cst[3] = cost(w, s, &cw1, &cs1, &p1) +
			cost(w, s, &cw1, &cs1, &f1)

		max := cst[0]

		for i := range cst {
			if max < cst[i] {
				max = cst[i]
			}
		}

		fmt.Println(max)
	}

}

package main

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

func cost(s, w uint64, cs, cw, p *uint64) uint64 {

	var i, max, x1, y1 uint64

	for i = 0; i <= *cs; i++ {
		if *p >= s*i {
			j := (*p - s*i) / w

			if j > *cw {
				j = *cw
			}

			v := i + j

			if v > max {
				x1 = i
				y1 = j
				max = v
			}
		} else {
			break
		}
	}

	*cs -= x1
	*cw -= y1

	//fmt.Println("--->", max, x1, y1)

	*p -= max

	return x1 + y1
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() uint64 {
		sc.Scan()
		i, _ := strconv.ParseUint(sc.Text(), 10, 64)
		return i
	}

	for t := in(); t > 0; t-- {
		p, f := in(), in()

		cs, cw := in(), in()
		cs1, cw1 := cs, cw

		s, w := in(), in()

		cst1 := cost(s, w, &cs, &cw, &p) +
			cost(s, w, &cs, &cw, &f)

		cst2 := cost(s, w, &cs1, &cw1, &f) +
			cost(s, w, &cs1, &cw1, &p)

		if cst1 > cst2 {
			fmt.Println(cst1)
		} else {
			fmt.Println(cst2)
		}
	}

}

package main

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

func cost(s, w uint64, cs, cw, p, f uint64) uint64 {
	var x1, max uint64
	for x1 = 0; x1 <= cs; x1++ {
		if p >= s*x1 {
			y1 := (p - s*x1) / w

			if y1 > cw {
				y1 = cw
			}

			x2 := (cs - x1)

			if f < s*x2 {
				x2 = f / s
			}

			y2 := (f - s*x2) / w

			if y2 > (cw - y1) {
				y2 = (cw - y1)
			}

			if max < (x1 + y1 + x2 + y2) {
				max = (x1 + y1 + x2 + y2)
			}
		}
	}

	return max
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() uint64 {
		sc.Scan()
		i, _ := strconv.ParseUint(sc.Text(), 10, 64)
		return i
	}

	for t := in(); t > 0; t-- {
		p, f := in(), in()

		cs, cw := in(), in()

		s, w := in(), in()

		val1 := cost(s, w, cs, cw, p, f)
		val2 := cost(s, w, cs, cw, f, p)

		if val1 > val2 {
			fmt.Println(val1)
		} else {
			fmt.Println(val2)
		}

	}

}


Dima's got a staircase that consists of n stairs. The first stair is at height a1, the second one is at a2, the last one is at an (1 ≤ a1 ≤ a2 ≤ ... ≤ an). Dima decided to play with the staircase, so he is throwing rectangular boxes at the staircase from above. The i-th box has width wi and height hi. Dima throws each box vertically down on the first wi stairs of the staircase, that is, the box covers stairs with numbers 1, 2, ..., wi. Each thrown box flies vertically down until at least one of the two following events happen:  the bottom of the box touches the top of a stair;  the bottom of the box touches the top of a box, thrown earlier. We only consider touching of the horizontal sides of stairs and boxes, at that touching with the corners isn't taken into consideration. Specifically, that implies that a box with width wi cannot touch the stair number wi + 1.You are given the description of the staircase and the sequence in which Dima threw the boxes at it. For each box, determine how high the bottom of the box after landing will be. Consider a box to fall after the previous one lands.

The first line contains integer n (1 ≤ n ≤ 105) — the number of stairs in the staircase. The second line contains a non-decreasing sequence, consisting of n integers, a1, a2, ..., an (1 ≤ ai ≤ 109; ai ≤ ai + 1). The next line contains integer m (1 ≤ m ≤ 105) — the number of boxes. Each of the following m lines contains a pair of integers wi, hi (1 ≤ wi ≤ n; 1 ≤ hi ≤ 109) — the size of the i-th thrown box. The numbers in the lines are separated by spaces.

Print m integers — for each box the height, where the bottom of the box will be after landing. Print the answers for the boxes in the order, in which the boxes are given in the input. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.

NoteThe first sample are shown on the picture.

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

func main() {
	var n,m,i int
	var a [100005]int64
	fmt.Scan(&n)
	var r,w,h int64
	r=-1
	input:= bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)
	out :=bufio.NewWriter(os.Stdout)
	for i=0;i<n; i++{
		input.Scan()
		aa,_:=strconv.ParseInt(input.Text(),0,64)
		a[i]=aa
	}
	input.Scan()
	bb,_:=strconv.Atoi(input.Text())
	m=bb

	for i=0;i<m;i++ {
		input.Scan()
		cc,_:=strconv.ParseInt(input.Text(),0,64)
		w=cc
		input.Scan()
		dd,_:=strconv.ParseInt(input.Text(),0,64)
		h=dd

		//fmt.Println(w,h)

		if a[w-1]>r {
			r=a[w-1]
		}
		out.WriteString(strconv.FormatInt(r,10)+"\n")
		r+=h
	}
	out.Flush()
}

package main

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

func scanInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	x, _ := strconv.Atoi(scanner.Text())
	return x
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	n := scanInt(scanner)
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = int64(scanInt(scanner))
	}
	extraHeight := int64(-1)
	m := scanInt(scanner)
	for i := 0; i < m; i++ {
		pos := scanInt(scanner)-1
		height := int64(scanInt(scanner))
		base := a[pos]
		if extraHeight > base {
			base = extraHeight
		}
		writer.WriteString(fmt.Sprintf("%d\n", base))
		extraHeight = height+base
	}
}

package main

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

func scanInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	x, _ := strconv.Atoi(scanner.Text())
	return x
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	n := scanInt(scanner)
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = int64(scanInt(scanner))
	}
	extraPos, extraHeight := -1, int64(-1)
	m := scanInt(scanner)
	for i := 0; i < m; i++ {
		pos := scanInt(scanner)-1
		height := int64(scanInt(scanner))
		base := extraHeight
		if pos > extraPos {
			base = a[pos]
		}
		writer.WriteString(fmt.Sprintf("%d\n", base))
		height += base
		extraHeight = height
		if height >= a[n-1] {
			extraPos = n-1
		} else {
			left, right := 0, n-1
			for left != right {
				mid := (left+right)/2
				if a[mid] > height {
					right = mid
				} else {
					left = mid+1
				}
			}
			extraPos = left-1
		}
	}
}

package main

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

func scanInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	x, _ := strconv.Atoi(scanner.Text())
	return x
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	n := scanInt(scanner)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanInt(scanner)
	}
	extraPos, extraHeight := -1, -1
	m := scanInt(scanner)
	for i := 0; i < m; i++ {
		pos := scanInt(scanner)-1
		height := scanInt(scanner)
		base := extraHeight
		if pos > extraPos {
			base = a[pos]
		}
		writer.WriteString(fmt.Sprintf("%d\n", base))
		height += base
		extraHeight = height
		if height >= a[n-1] {
			extraPos = n-1
		} else {
			left, right := 0, n-1
			for left != right {
				mid := (left+right)/2
				if a[mid] > height {
					right = mid
				} else {
					left = mid+1
				}
			}
			extraPos = left-1
		}
	}
}


The only difference between the two versions is that in this version $$$n \leq 1000$$$ and the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$.A terminal is a row of $$$n$$$ equal segments numbered $$$1$$$ to $$$n$$$ in order. There are two terminals, one above the other. You are given an array $$$a$$$ of length $$$n$$$. For all $$$i = 1, 2, \dots, n$$$, there should be a straight wire from some point on segment $$$i$$$ of the top terminal to some point on segment $$$a_i$$$ of the bottom terminal. You can't select the endpoints of a segment. For example, the following pictures show two possible wirings if $$$n=7$$$ and $$$a=[4,1,4,6,7,7,5]$$$.  A crossing occurs when two wires share a point in common. In the picture above, crossings are circled in red.What is the maximum number of crossings there can be if you place the wires optimally?

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq n$$$) — the elements of the array. The sum of $$$n$$$ across all test cases does not exceed $$$1000$$$.

For each test case, output a single integer — the maximum number of crossings there can be if you place the wires optimally.

NoteThe first test case is shown in the second picture in the statement.In the second test case, the only wiring possible has the two wires cross, so the answer is $$$1$$$.In the third test case, the only wiring possible has one wire, so the answer is $$$0$$$.

package main

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

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

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

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

		var tot int
		for i := 0; i < n-1; i++ {
			for j := i + 1; j < n; j++ {
				if arr[j] <= arr[i] {
					tot++
				}
			}
		}
		fmt.Fprintln(out, tot)
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/bits"
	"os"
	"sort"
	"strconv"
	"strings"
)

var in *In
var out *Out

const N = 2e5 + 10

var a, b [N]int

func lowbit(x int) int {
	return x & (-x)
}

func add(x, k int) {
	for i := x; i < N; i += lowbit(i) {
		b[i] += k
	}
}

func get(x int) int {
	ans := 0
	for i := x; i > 0; i -= lowbit(i) {
		ans += b[i]
	}
	return ans
}

func problem_solver() {
	T := in.NextInt()
	for T > 0 {
		T--
		n := in.NextInt()
		var ans int64 = 0
		for i := 1; i <= n; i++ {
			a[i] = in.NextInt()
			a[i] = n - a[i] + 1
			ans += int64(get(a[i]))
			add(a[i], 1)
		}
		for i := 1; i <= n; i++ {
			add(a[i], -1)
		}
		out.Println(ans)

	}
}

func main() {

	in, out = InitIo(true)
	defer out.Flush()

	problem_solver()

}

func debug(args ...interface{}) {
	fmt.Fprintln(os.Stderr, args...)
}

// ==================================================
// 入出力操作
// ==================================================
type In struct {
	// NextString は 次の入力を文字列として読み込んで返します。
	NextString func() string
}

type Out struct {
	writer io.Writer
	Flush  func()
}

// InitIo は inとoutを初期化します。
func InitIo(buffer bool) (*In, *Out) {
	bufsize := 4 * 1024 * 1024 // 4MB

	// 入力はずっとバッファーでいいらしい。ほんとう？
	// TODO バッファなしfmt.Fscanf(os.Stdin)だとTLEだった。要調査
	_in := bufio.NewScanner(os.Stdin)
	_in.Split(bufio.ScanWords)
	_in.Buffer(make([]byte, bufsize), bufsize)
	in := func() string {
		_in.Scan()
		return _in.Text()
	}

	// 出力はバッファon/offが必要
	var out io.Writer
	var flush func()

	if buffer {
		_out := bufio.NewWriterSize(os.Stdout, bufsize)
		out = _out
		flush = func() {
			_out.Flush()
		}
	} else {
		out = os.Stdout
		flush = func() {}
	}

	return &In{in}, &Out{out, flush}
}

// NextBytes は 次の入力をbyteの配列として読み込んで返します。
// 遅いから極力使わない。
func (in *In) NextBytes() []byte {
	return []byte(in.NextString())
}

// NextInt は 次の入力を数値として読み込んで返します。
func (in *In) NextInt() int {
	i, _ := strconv.Atoi(in.NextString())
	return i
}

// NextInt2 は 次の2つの入力を数値として読み込んで返します。
func (in *In) NextInt2() (int, int) {
	return in.NextInt(), in.NextInt()
}

// NextInt2d は 次の2つの入力を数値n1,n2として読み込んで、n1+d1, n2+d2を返します。
func (in *In) NextInt2d(d1, d2 int) (int, int) {
	return in.NextInt() + d1, in.NextInt() + d2
}

// NextInt3 は 次の3つの入力を数値として読み込んで返します。
func (in *In) NextInt3() (int, int, int) {
	return in.NextInt(), in.NextInt(), in.NextInt()
}

// NextInt2d は 次の3つの入力を数値n1,n2,n3として読み込んで、n1+d1, n2+d2, n3+d3を返します。
func (in *In) NextInt3d(d1, d2, d3 int) (int, int, int) {
	return in.NextInt() + d1, in.NextInt() + d2, in.NextInt() + d3
}

// NextInt4 は 次の4つの入力を数値として読み込んで返します。
func (in *In) NextInt4() (int, int, int, int) {
	return in.NextInt(), in.NextInt(), in.NextInt(), in.NextInt()
}

// NextInts は 次のn個の入力を数値として読み込んで、配列として返します。
func (in *In) NextInts(n int) sort.IntSlice {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = in.NextInt()
	}
	return sort.IntSlice(a)
}

// NextLongIntAsArray は 次の入力を数値として読み込み、各桁を要素とした配列を返します。
func (in *In) NextLongIntAsArray() []int {
	s := in.NextString()
	l := len(s)
	arr := make([]int, l)
	for i := 0; i < l; i++ {
		arr[i] = int(s[i] - '0')
	}

	return arr
}

// NextFloat は 次の入力を実数値として読み込み、値を返します。
func (in *In) NextFloat() float64 {
	f, _ := strconv.ParseFloat(in.NextString(), 64)
	return f
}

// NextFloatAsInt は次の入力を実数rとして読み込み、r * 10^base の値を返します。
func (in *In) NextFloatAsInt(base int) int {
	if base%10 == 0 {
		panic("baseは小数点の最大桁数を指定する")
	}

	s := in.NextString()
	index := strings.IndexByte(s, '.')

	// 小数点がなければそのまま返す
	if index == -1 {
		n, _ := strconv.Atoi(s)
		return n * pow(10, base)
	}

	// 末尾の0を消しておく
	for s[len(s)-1] == '0' {
		s = s[:len(s)-1]
	}

	// 整数部分 * 10^base + 小数部分 * 10^(足りない分)
	s1, s2 := s[:index], s[index+1:]
	n, _ := strconv.Atoi(s1)
	m, _ := strconv.Atoi(s2)

	return n*pow(10, base) + m*pow(10, base-len(s2))
}

// Println は引数をスペース区切りで出力し、最後に改行を出力します。
func (out *Out) Println(a ...interface{}) {
	fmt.Fprintln(out.writer, a...)
}

// Printf はformatにしたがってaを整形して出力します。
func (out *Out) Printf(format string, a ...interface{}) {
	fmt.Fprintf(out.writer, format, a...)
}

// PrintStringsln は文字列配列の各要素をスペース区切りで出力し、最後に改行を出力します。
func (out *Out) PrintStringsln(a []string) {
	out.Println(strings.Join(a, " "))
}

// PrintIntsLn は整数配列の各要素をスペース区切りで出力し、最後に改行を出力します。
func (out *Out) PrintIntsLn(a []int) {
	b := make([]string, len(a))
	for i, v := range a {
		b[i] = fmt.Sprint(v)
	}
	out.Println(strings.Join(b, " "))
}

func (out *Out) PrintLenAndIntsLn(a []int) {
	b := make([]string, len(a)+1)
	b[0] = fmt.Sprint(len(a))
	for i, v := range a {
		b[i+1] = fmt.Sprint(v)
	}
	out.Println(strings.Join(b, " "))
}

// YesNo は condが真ならYes, 偽ならNoを出力します。
func (out *Out) YesNo(cond bool) {
	if cond {
		out.Println("Yes")
	} else {
		out.Println("No")
	}
}

func (out *Out) YESNO(cond bool) {
	if cond {
		out.Println("YES")
	} else {
		out.Println("NO")
	}
}

// ==================================================
// 数値操作
// ==================================================

// max は aとbのうち大きい方を返します。
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

// min は aとbのうち小さい方を返します。
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

// abs は aの絶対値を返します。
func abs(a int) int {
	if a > 0 {
		return a
	}
	return -a
}

// pow は aのb乗を返します。
func pow(a, b int) int {
	ans := 1
	for b > 0 {
		if b%2 == 1 {
			ans *= a
		}
		a, b = a*a, b/2
	}
	return ans
}

// divceil は a/b の結果を正の無限大に近づけるように丸めて返します。
func divceil(a, b int) int {
	if a%b == 0 || a/b < 0 {
		return a / b
	}
	return (a + b - 1) / b
}

// divfloor は a/b の結果を負の無限大に近づけるように丸めて返します。
func divfloor(a, b int) int {
	if a%b == 0 || a/b > 0 {
		return a / b
	}
	if b < 0 {
		a, b = -a, -b
	}
	return (a - b + 1) / b
}

// powmod は (x^n) mod m を返します。
func powmod(x, n, m int) int {
	x = x % m
	if x == 0 {
		return 0
	}

	ans := 1
	for n > 0 {
		if n%2 == 1 {
			ans = (ans * x) % m
		}
		x = (x * x) % m
		n /= 2
	}
	return ans
}

// ch は condがtrueのときok, falseのときngを返します。
func ch(cond bool, ok, ng int) int {
	if cond {
		return ok
	}
	return ng
}

// ==================================================
// ビット操作
// ==================================================

// nthbit はaのn番目のビットを返します。
func nthbit(a int, n int) int { return int((a >> uint(n)) & 1) }

// popcount はaのうち立っているビットを数えて返します。
func popcount(a int) int {
	return bits.OnesCount(uint(a))
}

func xor(a, b bool) bool { return a != b }

// ==================================================
// 文字列操作
// ==================================================

// toLowerCase は sをすべて小文字にした文字列を返します。
func toLowerCase(s string) string {
	return strings.ToLower(s)
}

// toUpperCase は sをすべて大文字にした文字列を返します。
func toUpperCase(s string) string {
	return strings.ToUpper(s)
}

// isLower はbが小文字かどうかを判定します
func isLower(b byte) bool {
	return 'a' <= b && b <= 'z'
}

// isUpper はbが大文字かどうかを判定します
func isUpper(b byte) bool {
	return 'A' <= b && b <= 'Z'
}

// ==================================================
// 配列
// ==================================================
// NewIntInt は数値の二次元配列を作成します。
func NewIntInt(rows, cols, val int) [][]int {
	a := make([][]int, rows)
	for i := 0; i < rows; i++ {
		a[i] = make([]int, cols)

		for j := 0; j < cols; j++ {
			a[i][j] = val
		}
	}

	return a
}

func reverse(arr *[]interface{}) {
	for i, j := 0, len(*arr)-1; i < j; i, j = i+1, j-1 {
		(*arr)[i], (*arr)[j] = (*arr)[j], (*arr)[i]
	}
}

func reverseInt(arr *[]int) {
	for i, j := 0, len(*arr)-1; i < j; i, j = i+1, j-1 {
		(*arr)[i], (*arr)[j] = (*arr)[j], (*arr)[i]
	}
}

func uniqueInt(arr []int) []int {
	hist := map[int]bool{}
	j := 0
	for i := 0; i < len(arr); i++ {
		if hist[arr[i]] {
			continue
		}

		a := arr[i]
		arr[j] = a
		hist[a] = true
		j++
	}
	return arr[:j]
}

// ==================================================
// 構造体
// ==================================================

// Point は 座標を表す構造体です。
type Point struct {
	x int
	y int
}

type Geo struct {
}

// NextPointは入力から2点読み込んで座標として返します。
func (g *Geo) NextPoint() *Point {
	return &Point{in.NextInt(), in.NextInt()}
}

// x2areaは3点で囲まれる面積の2倍を返します。
func (g *Geo) x2area(p1 *Point, p2 *Point, p3 *Point) int {
	return abs(p1.x*p2.y + p2.x*p3.y + p3.x*p1.y - p1.y*p2.x - p2.y*p3.x - p3.y*p1.x)
}

package main

import "fmt"

const N = 2e5 + 10

var a, b [N]int

func lowbit(x int) int {
	return x & (-x)
}

func add(x, k int) {
	for i := x; i < N; i += lowbit(i) {
		b[i] += k
	}
}

func get(x int) int {
	ans := 0
	for i := x; i >0 ; i -= lowbit(i) {
		ans += b[i]
	}
	return ans
}

func main() {
	var T int
	fmt.Scan(&T)
	for T > 0 {
		T--
		var n int
		fmt.Scan(&n)
		ans := 0
		for i := 1; i <= n; i++ {
			fmt.Scan(&a[i])
			a[i] = n - a[i] + 1
			ans += get(a[i])
			add(a[i], 1)
		}
		for i := 1; i <= n; i++ {
			add(a[i], -1)
		}
		fmt.Println(ans)

	}
}

package main

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

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

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

type Key struct {
	n int
	k int
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var niter int
	fmt.Fscan(in, &niter)
	for iiter := 0; iiter < niter; iiter++ {
		var n int
		fmt.Fscan(in, &n)
		m := make(map[int]int, n)
		res := 0
		for i := 0; i < n; i++ {
			var el int
			fmt.Fscan(in, &el)
			for key, val := range m {
				if key >= el {
					res += val
				}
			}
			m[el] += 1
		}
		fmt.Fprintln(out, res)
	}
}

package main

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

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

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

	var T int
	var n int
	var x int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)

		a := make([]int, n)
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			a[i] = x-1
		}

		ans := 0
		for i, v := range a {
			for j := i +1; j < n;j++ {
				if a[j] < v {
					ans++
				}
			}
		}
		m := map[int]int{}
		for _,  v := range a {
			m[v]++
		}
		for _, v := range m {
			if v > 1 {
				ans +=  v * (v-1)/2
			}
		}

		Fprintln(out, ans)
	}
}

package main

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

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

func slove() {
	var n int
	fmt.Fscan(r, &n)
	nums := make([]int, n)
	cnt := make(map[int]int)
	for i:=0;i<n;i++ {
		fmt.Fscan(r, &nums[i])
		cnt[nums[i]]++
	}
	f := newFenwick(2e5+5)
	var ans int64
	for i := range nums {
		ans += int64(f.query(nums[i]+1, 2e5+2))
		f.add(nums[i], 1)
	}
	for _, v := range cnt {
		ans += int64((v-1))*int64(v)/2
	}
	fmt.Fprintln(w, ans)
}

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

type fenwick struct {tree []int}
func newFenwick(n int) fenwick {return fenwick{make([]int, n+1)}}
func (f fenwick) add(i int, val int) {for ; i < len(f.tree); i += i & -i {f.tree[i] += val}}
func (f fenwick) sum(i int) (res int) {for ; i > 0; i -= i & -i {res += f.tree[i]}; return}
func (f fenwick) query(l, r int) int {return f.sum(r) - f.sum(l-1)}

package main

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

// github.com/EndlessCheng/codeforces-go
type fenwick struct {
	tree []int
}

func newFenwickTree(n int) fenwick {
	return fenwick{make([]int, n+1)}
}
func (f fenwick) add(i int, val int) {
    //i++
	for ; i < len(f.tree); i += i & -i {
		f.tree[i] += val
	}
}
func (f fenwick) sum(i int) (res int) {
    //i++
	for ; i > 0; i &= i - 1 {
		res += f.tree[i]
	}
	return
}
func (f fenwick) query(l, r int) (res int) { // [l,r]
	return f.sum(r) - f.sum(l-1)
}

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

	solve := func(curCase int) {
		var n int
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		f := newFenwickTree(n+1)
		ans := int64(0)
		for _, v := range a {
			s := f.query(v,n)
			ans+= int64(s)
			f.add(v,1)
		}
		Fprintln(out, ans)
	}

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

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

}

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

package main

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

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

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

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

		var tot int
		for i := 0; i < n-1; i++ {
			if i > arr[i] {
				continue
			}

			for j := i + 1; j < n; j++ {
				if arr[i] >= arr[j] {
					tot++
				}
			}
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

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

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

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

		var tot int
		for i := 0; i < n-1; i++ {
			if i+1 > arr[i] {
				continue
			}

			for j := i + 1; j < n; j++ {
				if arr[i] >= arr[j] {
					tot++
				}
			}
		}
		fmt.Fprintln(out, tot)
	}
}


Little Susie loves strings. Today she calculates distances between them. As Susie is a small girl after all, her strings contain only digits zero and one. She uses the definition of Hamming distance:We will define the distance between two strings s and t of the same length consisting of digits zero and one as the number of positions i, such that si isn't equal to ti. As besides everything else Susie loves symmetry, she wants to find for two strings s and t of length n such string p of length n, that the distance from p to s was equal to the distance from p to t.It's time for Susie to go to bed, help her find such string p or state that it is impossible.

The first line contains string s of length n.  The second line contains string t of length n. The length of string n is within range from 1 to 105. It is guaranteed that both strings contain only digits zero and one.

Print a string of length n, consisting of digits zero and one, that meets the problem statement. If no such string exist, print on a single line "impossible" (without the quotes). If there are multiple possible answers, print any of them.

NoteIn the first sample different answers are possible, namely — 0010, 0011, 0110, 0111, 1000, 1001, 1100, 1101.

package main

import (
	"fmt"
)

var (
	s, t string
	C = [2]string{"0", "1"}
)

func main() {
	fmt.Scan(&s, &t)
	ct := 0
	buf := make([]uint8, 0)
	for i := 0; i < len(s); i++ {
		if s[i] != t[i] {
			if ct % 2 == 0 {
				buf = append(buf, s[i])
			} else {
				buf = append(buf, t[i])
			}
			ct++
		} else {
			buf = append(buf, s[i])
		}
	}
	if ct % 2 == 1 {
		fmt.Println("impossible")
	} else {
		fmt.Println(string(buf))
	}
}

package main

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

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

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	var s, t string

	scan(&s, &t)

	n, dif := len(s), 0

	for i := 0; i < n; i++ {
		if s[i] != t[i] {
			dif++
		}
	}
	if dif%2 == 0 {
		cnt := 0

		for i := 0; i < n; i++ {
			if s[i] == t[i] {
				printf("%c", s[i])
			} else {
				if cnt < dif/2 {
					if s[i] == '1' {
						print(0)
					} else {
						print(1)
					}
				} else {
					if s[i] == '1' {
						print(1)
					} else {
						print(0)
					}
				}
				cnt++
			}
		}
		println()
	} else {
		println("impossible")
	}
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

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

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

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

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

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

package main

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

var reader *bufio.Reader
var writer *bufio.Writer

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

  line, _ := reader.ReadString('\n')
  s := strings.TrimSpace(line)
  line, _ = reader.ReadString('\n')
  t := strings.TrimSpace(line)
  n := len(s)
  count := 0
  p := make([]byte, n)
  for i := 0; i < n; i++ {
    if s[i] == t[i] {
      p[i] = s[i]
    } else {
      count++
      if count%2 == 0 {
        p[i] = s[i]
      } else {
        p[i] = t[i]
      }
    }
  }
  if count%2 == 1 {
    writer.WriteString("impossible\n")
  } else {
    writer.WriteString(fmt.Sprintf("%s\n", p))
  }
}

package main

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

func main() {
	re := regexp.MustCompile(`\r?\n`)
	strs := make([]string, 2)

	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < 2; i++ {
		if s, err := reader.ReadString('\n'); err != nil {
			if err == io.EOF {
				break
			} else {
				return
			}
		} else {
			strs[i] = re.ReplaceAllString(s, "")
		}
	}
	out_str := make([]byte, len(strs[0]))

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

	if follow == 1 {
		fmt.Println("impossible")
	} else {
		fmt.Printf("%s\n", out_str)
	}
}

// Codeforces 911 C done
package main

import (
	"bufio"
	"fmt"
	"strings"
	"strconv"
	"os"
)
var in *bufio.Reader

func readFive() (int,int,int,int,int) {
	var a [5]int
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for i:=0; i<len(ss); i++ {
		a[i], err = strconv.Atoi(ss[i])
		if err != nil {
			fmt.Println("n atoi failure", err)
			panic(err)
		}
	}
	return a[0],a[1],a[2],a[3],a[4]
}

func readLineNumbs(n int) []int {
	a := make([]int, n)
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for j:=0; j<n; j++ {
		aa,err := strconv.Atoi(ss[j])
		if err != nil {
			fmt.Println("aa conv failed", err)
			panic(err)
		}
		a[j] = aa
	}
	return a
}

const BAD_BYTE = 99
var s []byte
var t []byte
var res []byte

func readData() {
	in = bufio.NewReader(os.Stdin)
	str,_ := in.ReadString('\n')
	s = []byte(str)
	str,_ = in.ReadString('\n')
	t = []byte(str)
}

func solve() {
	ndiff := 0
	n := len(s)
	for i:=0; i<n; i++ {
		if s[i] != t[i] {
			ndiff++
		}
	}
	res = make([]byte, n)
	if (ndiff % 2) == 1 {
		res[0] = BAD_BYTE
		return
	}
	nd2 := ndiff/2
	dcnt := 0
	for i:=0; i<n; i++ {
		if s[i] == t[i] {
			res[i] = s[i]
		} else {
			if dcnt < nd2 {
				res[i] = s[i]
			} else {
				res[i] = t[i]
			}
			dcnt++
		}
	}
}

func printRes() {
	if res[0] == BAD_BYTE {
		fmt.Println("impossible")
	} else {
		fmt.Println(string(res))
	}
}

func main() {
	readData()
	solve()
	printRes()
}

package main

import (
	"fmt"
)

func main() {
	var s, t string
	fmt.Scan(&s, &t)

	p := make([]byte, len(s), len(s))

	dis := 0
	cur := 0
	for i := 0; i < len(s); i++ {
		if s[i] != t[i] {
			dis++
			if cur%2 == 0 {
				p[i] = s[i]
			} else {
				p[i] = t[i]
			}
			cur++
		} else {
			p[i] = s[i]
		}
	}

	if dis%2 == 0 {
		fmt.Println(string(p))
	} else {
		fmt.Println("impossible")
	}
}

package main

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

var reader *bufio.Reader
var writer *bufio.Writer

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

  line, _ := reader.ReadString('\n')
  s := strings.TrimSpace(line)
  line, _ = reader.ReadString('\n')
  t := strings.TrimSpace(line)
  n := len(s)
  count := 0
  p := make([]byte, n)
  for i := 0; i < n; i++ {
    if s[i] == t[i] {
      p[i] = s[i]
    } else {
      count++
      if count%2 == 0 {
        p[i] = '0'
      } else {
        p[i] = '1'
      }
    }
  }
  if count%2 == 1 {
    writer.WriteString("impossible\n")
  } else {
    writer.WriteString(fmt.Sprintf("%s\n", p))
  }
}


You are given n switches and m lamps. The i-th switch turns on some subset of the lamps. This information is given as the matrix a consisting of n rows and m columns where ai, j = 1 if the i-th switch turns on the j-th lamp and ai, j = 0 if the i-th switch is not connected to the j-th lamp.Initially all m lamps are turned off.Switches change state only from "off" to "on". It means that if you press two or more switches connected to the same lamp then the lamp will be turned on after any of this switches is pressed and will remain its state even if any switch connected to this lamp is pressed afterwards.It is guaranteed that if you push all n switches then all m lamps will be turned on.Your think that you have too many switches and you would like to ignore one of them. Your task is to say if there exists such a switch that if you will ignore (not use) it but press all the other n - 1 switches then all the m lamps will be turned on.

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 2000) — the number of the switches and the number of the lamps. The following n lines contain m characters each. The character ai, j is equal to '1' if the i-th switch turns on the j-th lamp and '0' otherwise. It is guaranteed that if you press all n switches all m lamps will be turned on.

Print "YES" if there is a switch that if you will ignore it and press all the other n - 1 switches then all m lamps will be turned on. Print "NO" if there is no such switch.

package main

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

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

func main() {
	var a, b int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, 1000000), 1000000)
	scanner.Scan()
	fmt.Sscan(scanner.Text(), &a, &b)
	x := make([]int, b)
	xs := make([][]int, a)
	for i := 0; i < a; i++ {
		scanner.Scan()
		for j, v := range scanner.Text() {
			if v == '1' {
				x[j]++
				xs[i] = append(xs[i], j)
			}
		}
	}
	for i := range xs {
		subset := false
		for _, v := range xs[i] {
			if x[v] == 1 {
				//fmt.Println(xs[i], v, x[v])
				subset = true
				break

			}
		}
		if subset == false {
			fmt.Println("YES")
			os.Exit(0)
		}
	}
	fmt.Println("NO")
}


Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot.One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through n and is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it at the beginning of the permutation.Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second.

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the quantity of the numbers in the both given permutations.  Next line contains n space-separated integers — the first permutation. Each number between 1 to n will appear in the permutation exactly once.  Next line describe the second permutation in the same format.

Print a single integer denoting the minimum number of moves required to convert the first permutation to the second.

NoteIn the first sample, he removes number 1 from end of the list and places it at the beginning. After that he takes number 2 and places it between 1 and 3.In the second sample, he removes number 5 and inserts it after 1.In the third sample, the sequence of changes are like this:  1 5 2 3 4 1 4 5 2 3 1 3 4 5 2 1 2 3 4 5  So he needs three moves.

package main

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

var sc *bufio.Scanner = bufio.NewScanner(os.Stdin)
var w *bufio.Writer = bufio.NewWriter(os.Stdout)

func ni() int {
  sc.Scan()
  ans, _ := strconv.Atoi(sc.Text())
  return ans
}

func next() string {
  sc.Scan()
  return sc.Text()
}

func pri(n int)    { w.WriteString(strconv.Itoa(n)) }
func prll(n int64) { w.WriteString(strconv.FormatInt(n, 10)) }
func sp()          { w.WriteByte(' ') }
func endl()        { w.WriteByte('\n') }

func main() {
  sc.Split(bufio.ScanWords)
  n := ni()
  a, b := make([]int, n), make([]int, n+1)
  for i := 0; i < n; i++ {
    a[i] = ni()
  }
  for i := 0; i < n; i++ {
    b[ni()] = i
  }
  ii := 1
  for ; ii < n && b[a[ii]] > b[a[ii-1]]; ii++ {
  }
  pri(n - ii)
  endl()
  w.Flush()
}

package main

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

var sc *bufio.Scanner = bufio.NewScanner(os.Stdin)
var w *bufio.Writer = bufio.NewWriter(os.Stdout)

func ni() int {
  sc.Scan()
  ans, _ := strconv.Atoi(sc.Text())
  return ans
}

func next() string {
  sc.Scan()
  return sc.Text()
}

func pri(n int)    { w.WriteString(strconv.Itoa(n)) }
func prll(n int64) { w.WriteString(strconv.FormatInt(n, 10)) }
func sp()          { w.WriteByte(' ') }
func endl()        { w.WriteByte('\n') }

func main() {
  sc.Split(bufio.ScanWords)
  n := ni()
  a, b := make([]int, n), make(map[int]int)
  for i := 0; i < n; i++ {
    a[i] = ni()
  }
  for i := 0; i < n; i++ {
    b[ni()] = i
  }
  ii := 1
  for ; ii < n && b[a[ii]] > b[a[ii-1]]; ii++ {
  }
  pri(n - ii)
  endl()
  w.Flush()
}


You are given n segments on the Ox-axis. You can drive a nail in any integer point on the Ox-axis line nail so, that all segments containing this point, are considered nailed down. If the nail passes through endpoint of some segment, this segment is considered to be nailed too. What is the smallest number of nails needed to nail all the segments down?

The first line of the input contains single integer number n (1 ≤ n ≤ 1000) — amount of segments. Following n lines contain descriptions of the segments. Each description is a pair of integer numbers — endpoints coordinates. All the coordinates don't exceed 10000 by absolute value. Segments can degenarate to points.

The first line should contain one integer number — the smallest number of nails needed to nail all the segments down. The second line should contain coordinates of driven nails separated by space in any order. If the answer is not unique, output any.

package main

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

type Segment struct {
	L, R int
}

type SegList []*Segment

func (this SegList) Len() int {
	return len(this)
}

func (this SegList) Swap(i, j int) {
	this[i], this[j] = this[j], this[i]
}
func (this SegList) Less(i, j int) bool {
	return this[i].R < this[j].R || (this[i].R == this[j].R && this[i].L < this[j].L)
}
func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	segs := make([]*Segment, 0, 2 * n)
	for i := 0;i < n;i ++ {
		var l, r int
		fmt.Fscanf(reader, "%d %d\n", &l, &r)
		if l < r {
			segs = append(segs, &Segment{L: l, R: r})
		} else {
			segs = append(segs, &Segment{L: r, R: l})
		}
	}

	sort.Sort(SegList(segs))
	max := 1000000
	right := max
	result := make([]int, 0, n)
	for i := 0;i < len(segs); i ++ {
		if segs[i].L > right {
			result = append(result, right)
			right = max
		}
		if segs[i].R < right {
			right = segs[i].R
		}
	}
	if right != max {
		result = append(result, right)
	}
	fmt.Printf("%d\n", len(result))
	for i := 0;i < len(result);i ++ {
		fmt.Printf("%d ", result[i])
	}
}

package main

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

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	left := make([]int, 0, n)
	right := make([]int, 0, n)

	for i := 0;i < n;i ++ {
		var l, r int
		fmt.Fscanf(reader, "%d %d\n", &l, &r)
		if l < r {
			left = append(left, l)
			right = append(right, r)
		} else {
			left = append(left, r)
			right = append(right, l)
		}
	}
	sort.Ints(left)
	sort.Ints(right)

	max := 10000000
	rightMost := max
	results := make([]int, 0, 1000)
	for i := 0;i < 2 * n;i ++ {
		if len(left) == 0 || left[0] >= right[0] {
			if rightMost > right[0] {
				rightMost = right[0]
			}
			right = right[1:]
		} else {
			if left[0] > rightMost {
				results = append(results,rightMost)
				rightMost = max
			}
			left = left[1:]
		}
	}
	if rightMost != max {
		results = append(results,rightMost)
	}
	fmt.Printf("%d\n", len(results))
	for i := 0;i < len(results);i ++ {
		fmt.Printf("%d ", results[i])
	}
}


Pari wants to buy an expensive chocolate from Arya. She has n coins, the value of the i-th coin is ci. The price of the chocolate is k, so Pari will take a subset of her coins with sum equal to k and give it to Arya.Looking at her coins, a question came to her mind: after giving the coins to Arya, what values does Arya can make with them? She is jealous and she doesn't want Arya to make a lot of values. So she wants to know all the values x, such that Arya will be able to make x using some subset of coins with the sum k.Formally, Pari wants to know the values x such that there exists a subset of coins with the sum k such that some subset of this subset has the sum x, i.e. there is exists some way to pay for the chocolate, such that Arya will be able to make the sum x using these coins.

The first line contains two integers n and k (1  ≤  n, k  ≤  500) — the number of coins and the price of the chocolate, respectively. Next line will contain n integers c1, c2, ..., cn (1 ≤ ci ≤ 500) — the values of Pari's coins. It's guaranteed that one can make value k using these coins.

First line of the output must contain a single integer q— the number of suitable values x. Then print q integers in ascending order — the values that Arya can make for some subset of coins of Pari that pays for the chocolate.

package main

import (
	"fmt"
)

func myMin(x, y int) int{
	if x < y{
		return x
	}
	return y
}

func main ()  {
	var N, K int
	var c [510]int
	total := 0
	fmt.Scanln( &N, &K)
	for i := 1; i <= N; i++{
		fmt.Scan(&c[i])
		total += c[i]
	}
	var f [2][25100][510]bool
	f[0][0][0] = true
	for i := 1; i <= N; i++{
		for j := 0; j <= K; j++ {
			for k := 0; k <= myMin(j, K); k++ {
				var x, y int
				if i % 2 == 1{
					x = 1
					y = 0
				}else{
					x = 0
					y = 1
				}
				f[x][j][k] = f[y][j][k]
				if j >= c[i] {
					f[x][j][k] = f[x][j][k] || f[y][j-c[i]][k]
				}
				if j >= c[i] && k >= c[i] {
					f[x][j][k] = f[x][j][k] || f[y][j-c[i]][k-c[i]]
				}
			}
		}
	}
	var result []int
	for j := 0; j <= K; j++{
		if f[N % 2][K][j]{
			result = append(result, j);
		}
	}
	fmt.Println(len(result))
	for i := 0; i < len(result); i++{
		if i > 0{
			fmt.Print(" ")
		}
		fmt.Printf("%d", result[i])
	}
	fmt.Println()
}

package main

import (
	"fmt"
)

func myMin(x, y int) int{
	if x < y{
		return x
	}
	return y
}

func main ()  {
	var N, K int
	var c [510]int
	total := 0
	fmt.Scanln( &N, &K)
	for i := 1; i <= N; i++{
		fmt.Scan(&c[i])
		total += c[i]
	}
	var f [2][25100][510]bool
	f[0][0][0] = true
	for i := 1; i <= N; i++{
		for j := 0; j <= total; j++ {
			for k := 0; k <= myMin(j, K); k++ {
				var x, y int
				if i % 2 == 1{
					x = 1
					y = 0
				}else{
					x = 0
					y = 1
				}
				f[x][j][k] = f[y][j][k]
				if j >= c[i] {
					f[x][j][k] = f[x][j][k] || f[y][j-c[i]][k]
				}
				if j >= c[i] && k >= c[i] {
					f[x][j][k] = f[x][j][k] || f[y][j-c[i]][k-c[i]]
				}
			}
		}
	}
	var result []int
	for j := K; j <= total; j++{
		if f[N % 2][j][K]{
			result = append(result, j - K)
		}
	}
	fmt.Println(len(result))
	for i := 0; i < len(result); i++{
		if i > 0{
			fmt.Print(" ")
		}
		fmt.Printf("%d", result[i])
	}
	fmt.Println()
}