Given an array $$$a$$$ of $$$n$$$ integers, find a range of values $$$[x, y]$$$ ($$$x \le y$$$), and split $$$a$$$ into exactly $$$k$$$ ($$$1 \le k \le n$$$) subarrays in such a way that:  Each subarray is formed by several continuous elements of $$$a$$$, that is, it is equal to $$$a_l, a_{l+1}, \ldots, a_r$$$ for some $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$).  Each element from $$$a$$$ belongs to exactly one subarray.  In each subarray the number of elements inside the range $$$[x, y]$$$ (inclusive) is strictly greater than the number of elements outside the range. An element with index $$$i$$$ is inside the range $$$[x, y]$$$ if and only if $$$x \le a_i \le y$$$.  Print any solution that minimizes $$$y - x$$$.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 3 \cdot 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2 \cdot 10^5$$$) — the length of the array $$$a$$$ and the number of subarrays required in the partition. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) where $$$a_i$$$ is the $$$i$$$-th element of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot10^5$$$.

For each test case, print $$$k+1$$$ lines. In the first line, print $$$x$$$ and $$$y$$$ — the limits of the found range. Then print $$$k$$$ lines, the $$$i$$$-th should contain $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i \leq r_i \leq n$$$) — the limits of the $$$i$$$-th subarray. You can print the subarrays in any order.

NoteIn the first test, there should be only one subarray, which must be equal to the whole array. There are $$$2$$$ elements inside the range $$$[1, 2]$$$ and $$$0$$$ elements outside, if the chosen range is $$$[1, 1]$$$, there will be $$$1$$$ element inside ($$$a_1$$$) and $$$1$$$ element outside ($$$a_2$$$), and the answer will be invalid.In the second test, it is possible to choose the range $$$[2, 2]$$$, and split the array in subarrays $$$(1, 3)$$$ and $$$(4, 4)$$$, in subarray $$$(1, 3)$$$ there are $$$2$$$ elements inside the range ($$$a_2$$$ and $$$a_3$$$) and $$$1$$$ element outside ($$$a_1$$$), in subarray $$$(4, 4)$$$ there is only $$$1$$$ element ($$$a_4$$$), and it is inside the range.In the third test, it is possible to choose the range $$$[5, 5]$$$, and split the array in subarrays $$$(1, 4)$$$, $$$(5, 7)$$$ and $$$(8, 11)$$$, in the subarray $$$(1, 4)$$$ there are $$$3$$$ elements inside the range and $$$1$$$ element outside, in the subarray $$$(5, 7)$$$ there are $$$2$$$ elements inside and $$$1$$$ element outside and in the subarray $$$(8, 11)$$$ there are $$$3$$$ elements inside and $$$1$$$ element outside.

package main

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

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

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------
var n, k int
var a []int
var acc []int

func countInRange(x, y int) int {
	return acc[y] - acc[x-1]
}

func solveCase() {
	scan(&n, &k)
	a = make([]int, n)
	acc = make([]int, n+1)
	for i := 0; i < n; i++ {
		scan(&a[i])
		acc[a[i]]++
	}
	for i := 0; i < n; i++ {
		acc[i+1] += acc[i]
	}
	ansX := 1
	ansY := n
	for x := 1; x <= n; x++ {
		lo := x
		hi := n
		mi := lo
		cnt0 := 0
		cnt1 := 0
		for lo < hi {
			mi = (lo + hi) / 2
			cnt1 = countInRange(x, mi)
			cnt0 = n - cnt1
			if cnt1-cnt0 >= k {
				hi = mi
			} else {
				lo = mi + 1
			}
		}
		y := lo
		cnt1 = countInRange(x, y)
		cnt0 = n - cnt1
		if cnt1-cnt0 >= k {
			if ansY-ansX > y-x {
				ansY = y
				ansX = x
			}
		}
	}
	printf("%d %d\n", ansX, ansY)
	var ansL []int
	var ansR []int
	var startAt = 0
	var balance = 0
	for i := 0; len(ansL)+1 < k; i++ {
		if ansX <= a[i] && a[i] <= ansY {
			balance++
		} else {
			balance--
		}
		if balance == 1 {
			ansL = append(ansL, startAt+1)
			ansR = append(ansR, i+1)
			startAt = i + 1
			balance = 0
		}
	}
	ansL = append(ansL, startAt+1)
	ansR = append(ansR, n)
	for i := 0; i < k; i++ {
		printf("%d %d\n", ansL[i], ansR[i])
	}
}

func main() {
	defer flush()

	var ntc int
	scan(&ntc)
	for t := 0; t < ntc; t++ {
		solveCase()
	}
}

/**
Case k=1:
	Find [x,y] such that mayority of elements are in [x,y] and y-x is minimized.
	If I sort a, I can find [x,y] in O(n).
	Moyority means countInRange(x,y) >= n/2 + 1
	n/2 + 1 - (n - ((n/2)+1)) = -n + n/2 + 1 + n/2 + 1 = -n + 2*(n/2) + 2
General case:
	Find [x,y] such that:
	countInRange(x,y) >= n/2 + k
	n/2 + k - (n - ((n/2)+k)) = -n + n/2 + k + n/2 + k = -n + 2*(n/2) + 2*k
*/

package main

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

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

	solve := func(Case int) {
		var n, k int
		Fscan(in, &n, &k)
		a := make([]int, n)
		pos := make([][]int, n+1)
		for i := range a {
			Fscan(in, &a[i])
			pos[a[i]] = append(pos[a[i]], i)
		}

		sum := make([]int, n+2)
		for i, v := range pos {
			sum[i+1] = sum[i] + len(v)
		}

		type pair struct{ l, r int }
		ans := []pair{}
		ansx := -1
		ansy := -1
		f := func(d int) bool {
			ans = nil
			ansx = -1

			for y := d + 1; y <= n; y++ {
				x := y - d
				cnt := sum[y+1] - sum[x]
				if cnt-(n-cnt) >= k {
					ansx = x
					ansy=y
					i := 0
					for ; i < n && len(ans) < k-1; {
						st := i
						c := 0
						for ; i < n && c <= 0; i++ {
							if x <= a[i] && a[i] <= y {
								c++
							} else {
								c--
							}
						}
						ans = append(ans, pair{st, i - 1})
					}
					if len(ans) < k-1 {
						return false
					}
					ans = append(ans, pair{i,n-1})
					return true
				}
			}
			return false
		}

		d := sort.Search(n, f)
		if d == n {
			Fprintln(out, 1, n)
			for i := 1; i < k; i++ {
				Fprintln(out, i, i)
			}
			Fprintln(out, k, n)
			return
		}

		f(d)

		if ansx == -1 {
			panic(-1)
		}
		Fprintln(out, ansx, ansy)
		for _, p := range ans {
			Fprintln(out, p.l+1, p.r+1)
		}
	}

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

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

}

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


This is the easy version of the problem. The difference between the versions is that in the easy version all prices $$$a_i$$$ are different. You can make hacks if and only if you solved both versions of the problem.Today is Sage's birthday, and she will go shopping to buy ice spheres. All $$$n$$$ ice spheres are placed in a row and they are numbered from $$$1$$$ to $$$n$$$ from left to right. Each ice sphere has a positive integer price. In this version all prices are different.An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.

The first line contains a single integer $$$n$$$ $$$(1 \le n \le 10^5)$$$ — the number of ice spheres in the shop. The second line contains $$$n$$$ different integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$ — the prices of ice spheres.

In the first line print the maximum number of ice spheres that Sage can buy. In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.

NoteIn the example it's not possible to place ice spheres in any order so that Sage would buy $$$3$$$ of them. If the ice spheres are placed like this $$$(3, 1, 4, 2, 5)$$$, then Sage will buy two spheres: one for $$$1$$$ and one for $$$2$$$, because they are cheap.

package main

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

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

	n := readNum(reader)

	A := readNNums(reader, n)

	cnt := solve(n, A)

	var buf bytes.Buffer

	buf.WriteString(fmt.Sprintf("%d\n", cnt))

	for i := 0; i < n; i++ {
		buf.WriteString(fmt.Sprintf("%d ", A[i]))
	}
	buf.WriteByte('\n')
	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(n int, A []int) int {
	cnt := (n - 2 + 1) / 2

	sort.Ints(A)

	B := make([]int, n)
	j := n - 1
	for i := 0; i < n; i += 2 {
		B[i] = A[j]
		j--
	}
	for i := 1; i < n; i += 2 {
		B[i] = A[j]
		j--
	}
	copy(A, B)

	return cnt
}

package main

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

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

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

	n := in()
	a := make([]int64, n)
	b := make([]int64, n)

	for i := range a {
		a[i] = in()
	}

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	var s, c int64 = 1, 0

	var j int64

	for j = s; j < n; j += 2 {
		b[j] = a[c]
		c++
	}

	s = 0

	for j = s; j < n; j += 2 {
		b[j] = a[c]
		c++
	}

	fmt.Println((n-1)/2)

	for i := range b {
		fmt.Print(b[i], " ")
	}

	fmt.Println()
}

package main

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

var rw = NewFastIO(os.Stdin, os.Stdout)

func main() {
	defer rw.Flush()
	n := rw.NextInt()
	a := rw.NextInts(n)
	sort.Ints(a[:])
	b := make([]int, n)
	rw.Println((n - 1) / 2)
	for i := 0; i < n; i++ {
		if i%2 == 0 {
			b[i] = a[i/2+n/2]
		} else {
			b[i] = a[i/2]
		}
	}
	rw.PrintlnInts(b...)
}

// @meooow template uwu
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) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

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) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

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

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

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

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

	numberSpheres := convertInputToInt(stdInput(scanner, 1))[0]
	priceList := convertInputToInt(stdInput(scanner, numberSpheres))
	sort.Ints(priceList)
	cheapItemsCount, optimalOrder := processPriceList(priceList, numberSpheres)

	fmt.Println(cheapItemsCount)
	for i := 0; i < numberSpheres; i++ {
		fmt.Print(optimalOrder[i], " ")
	}
}

func processPriceList(priceList []int, numElements int) (int, []int) {
	var cheapItemsCount int = 0
	if numElements < 3 {
		return cheapItemsCount, priceList
	}
	if numElements%2 == 0 {
		cheapItemsCount = len(priceList)/2 - 1
	} else {
		cheapItemsCount = len(priceList) / 2
	}
	return cheapItemsCount, rearrangePriceList(priceList, cheapItemsCount)
}

func rearrangePriceList(priceList []int, cheapItemsCount int) []int {
	var newPriceList []int
	cheapSplit := priceList[:cheapItemsCount]
	expensiveSplit := priceList[cheapItemsCount:]

	for i := 0; i < len(expensiveSplit); i++ {
		if i > len(cheapSplit)-1 {
			newPriceList = append(newPriceList, expensiveSplit[i])
		} else {
			newPriceList = append(newPriceList, expensiveSplit[i], cheapSplit[i])
		}
	}
	return newPriceList
}

///////// UTILS ///////////

func stdInput(scanner *bufio.Scanner, varCount int) []string {
	var inputParameters []string

	scanner.Scan()
	input := strings.Split(scanner.Text(), " ")
	for i := 0; i < varCount; i++ {
		inputParameters = append(inputParameters, input[i])
	}
	return inputParameters
}

func convertInputToInt(variables []string) []int {
	var convertedInput []int
	for _, value := range variables {
		convertedVar, _ := strconv.Atoi(value)
		convertedInput = append(convertedInput, convertedVar)
	}
	return convertedInput
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

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

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

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

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

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

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

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

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

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

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

func main() {
	defer writer.Flush()
	n := sint64()
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	sol(n, a)
}

func sol(n int64, a []int64) {
	if n <= 2 {
		printf("%d\n", 0)
		for _, ai := range a {
			printf("%d ", ai)
		}
		printf("\n")
		return
	}
	sort.Slice(a, func(il, ir int) bool {
		return a[il] < a[ir]
	})
	ans := []int64{}
	l := 0
	r := len(a) - 1
	for l <= r {
		if l == r {
			ans = append(ans, a[l])
			break
		}
		ans = append(ans, a[r], a[l])
		l++
		r--
	}
	if ans[len(ans)-2] > ans[len(ans)-1] {
		ans[len(ans)-2], ans[len(ans)-1] = ans[len(ans)-1], ans[len(ans)-2]
	}
	ansn := int64(0)
	for i := 1; i < len(ans)-1; i++ {
		if ans[i-1] > ans[i] && ans[i] < ans[i+1] {
			ansn++
		}
	}
	printf("%d\n", ansn)
	for _, ai := range ans {
		printf("%d ", ai)
	}
	printf("\n")
}

package main

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

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

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

	n := in()
	a := make([]int64, n)
	b := make([]int64, n)

	for i := range a {
		a[i] = in()
	}

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	var s, c int64 = 1, 0

	var j int64

	for j = s; j < n; j += 2 {
		b[j] = a[c]
		c++
	}

	s = 0

	for j = s; j < n; j += 2 {
		b[j] = a[c]
		c++
	}

	for i := range b {
		fmt.Print(b[i], " ")
	}

	fmt.Println()
}

package main

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

func main() {
	var scanner = bufio.NewScanner(os.Stdin)
	numberSpheres := convertInputToInt(stdInput(scanner, 1))[0]
	priceList := convertInputToInt(stdInput(scanner, numberSpheres))

	sort.Ints(priceList)
	optimalOrder, cheapItemsCount := processPriceList(priceList, numberSpheres)
	fmt.Println(optimalOrder)
	fmt.Println(cheapItemsCount)
}

func processPriceList(priceList []int, numElements int) (int, []int) {
	var cheapItemsCount int = 0
	if numElements < 3 {
		return cheapItemsCount, priceList
	}

	even := (numElements%2 == 0)
	if even {
		cheapItemsCount = len(priceList)/2 - 1
	} else {
		cheapItemsCount = len(priceList) / 2
	}
	return cheapItemsCount, rearrangePriceList(priceList, cheapItemsCount)
}

func rearrangePriceList(priceList []int, cheapItemsCount int) []int {
	var newPriceList []int
	cheapSplit := priceList[:cheapItemsCount]
	expensiveSplit := priceList[cheapItemsCount:]

	for i := 0; i < len(expensiveSplit); i++ {
		if i > len(cheapSplit)-1 {
			newPriceList = append(newPriceList, expensiveSplit[i])
		} else {
			newPriceList = append(newPriceList, expensiveSplit[i], cheapSplit[i])
		}

	}
	return newPriceList
}

func stdInput(scanner *bufio.Scanner, varCount int) []string {
	var inputParameters []string

	scanner.Scan()
	input := strings.Split(scanner.Text(), " ")
	for i := 0; i < varCount; i++ {
		inputParameters = append(inputParameters, input[i])
	}
	return inputParameters
}

func convertInputToInt(variables []string) []int {
	var convertedInput []int
	for _, value := range variables {
		convertedVar, _ := strconv.Atoi(value)
		convertedInput = append(convertedInput, convertedVar)
	}
	return convertedInput
}

package main

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

func main() {
	var scanner = bufio.NewScanner(os.Stdin)
	numberSpheres := convertInputToInt(stdInput(scanner, 1))[0]
	priceList := convertInputToInt(stdInput(scanner, numberSpheres))

	sort.Ints(priceList)
	optimalOrder, cheapItemsCount := processPriceList(priceList, numberSpheres)
	fmt.Println(optimalOrder)
	fmt.Println(cheapItemsCount)
}

func processPriceList(priceList []int, numElements int) (int, []int) {
	var cheapItemsCount int = 0
	if numElements < 3 {
		return cheapItemsCount, priceList
	}

	even := (numElements%2 == 0)
	if even {
		cheapItemsCount = len(priceList)/2 - 1
	} else {
		cheapItemsCount = len(priceList) / 2
	}
	return cheapItemsCount, rearrangePriceList(priceList, cheapItemsCount)
}

func rearrangePriceList(priceList []int, cheapItemsCount int) []int {
	var newPriceList []int
	cheapSplit := priceList[:cheapItemsCount]
	expensiveSplit := priceList[cheapItemsCount:]

	for i := 0; i < len(expensiveSplit); i++ {
		if i > len(cheapSplit)-1 {
			newPriceList = append(newPriceList, expensiveSplit[i])
		} else {
			newPriceList = append(newPriceList, expensiveSplit[i], cheapSplit[i])
		}

	}
	return newPriceList
}

///////// UTILS ///////////

func stdInput(scanner *bufio.Scanner, varCount int) []string {
	var inputParameters []string

	scanner.Scan()
	input := strings.Split(scanner.Text(), " ")
	for i := 0; i < varCount; i++ {
		inputParameters = append(inputParameters, input[i])
	}
	return inputParameters
}

func convertInputToInt(variables []string) []int {
	var convertedInput []int
	for _, value := range variables {
		convertedVar, _ := strconv.Atoi(value)
		convertedInput = append(convertedInput, convertedVar)
	}
	return convertedInput
}

func convertSliceToString(slice []int) string {
	var output string
	for _, value := range slice {
		output += strconv.Itoa(value) + " "
	}
	return output[:len(output)-1]
}


CQXYM is counting permutations length of $$$2n$$$.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).A permutation $$$p$$$(length of $$$2n$$$) will be counted only if the number of $$$i$$$ satisfying $$$p_i<p_{i+1}$$$ is no less than $$$n$$$. For example:  Permutation $$$[1, 2, 3, 4]$$$ will count, because the number of such $$$i$$$ that $$$p_i<p_{i+1}$$$ equals $$$3$$$ ($$$i = 1$$$, $$$i = 2$$$, $$$i = 3$$$). Permutation $$$[3, 2, 1, 4]$$$ won't count, because the number of such $$$i$$$ that $$$p_i<p_{i+1}$$$ equals $$$1$$$ ($$$i = 3$$$). CQXYM wants you to help him to count the number of such permutations modulo $$$1000000007$$$ ($$$10^9+7$$$).In addition, modulo operation is to get the remainder. For example:  $$$7 \mod 3=1$$$, because $$$7 = 3 \cdot 2 + 1$$$,  $$$15 \mod 4=3$$$, because $$$15 = 4 \cdot 3 + 3$$$.

The input consists of multiple test cases.  The first line contains an integer $$$t (t \geq 1)$$$ — the number of test cases. The description of the test cases follows. Only one line of each test case contains an integer $$$n(1 \leq n \leq 10^5)$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$

For each test case, print the answer in a single line.

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	for j := 0; j < n; j++ {
		var s, i int64
		s = 1
		var a int64
		fmt.Scanf("%d\n", &a)
		for i = 3; i <= 2*a; i++ {
			s = s * i % 1000000007
		}
		fmt.Println(s)
	}
}

package main

import (
	"fmt"
)

func main(){
	T := 0
	fmt.Scan(&T)
	MOD := int64(1000000007)
	for it:=0; it<T; it++ {
		n := int64(0)
		fmt.Scan(&n)
		n =n * 2
		res := MOD-MOD/2
		for i := int64(1); i<=n;i++{
			res = res * i % MOD
		}
		fmt.Println(res)
	}
}

package main

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

const MOD int64 = 1000000007

var n 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)

		fmt.Println(solve())
	}
}

func solve() int64 {

	var ans int64 = 1

	for i := 3; i <= 2 * n; i++ {
		ans *= int64(i)

		ans %= MOD
	}

	return ans
}

package main

import "fmt"

var ans [100005]int64

func main() {
	var i int64 = 3
	ans[1] = 1
	ans[2] = 12
	for ; i <= 100000; i++ {
		k := i * 2
		n := ans[i-1]
		for j := k - 1; j <= k; j++ {
			n = n * j % 1000000007
		}
		ans[i] = n
	}
	n := 0
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		j := 0
		fmt.Scan(&j)
		fmt.Println(ans[j])
	}

}

package main

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

var iost *Iost

type Iost struct {
	Scanner *bufio.Scanner
	Writer  *bufio.Writer
}

func NewIost(fp io.Reader, wfp io.Writer) *Iost {
	const BufSize = 2000005
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, BufSize), BufSize)
	return &Iost{Scanner: scanner, Writer: bufio.NewWriter(wfp)}
}
func (i *Iost) Text() string {
	if !i.Scanner.Scan() {
		panic("scan failed")
	}
	return i.Scanner.Text()
}
func (i *Iost) Atoi(s string) int                 { x, _ := strconv.Atoi(s); return x }
func (i *Iost) GetNextInt() int                   { return i.Atoi(i.Text()) }
func (i *Iost) Atoi64(s string) int64             { x, _ := strconv.ParseInt(s, 10, 64); return x }
func (i *Iost) GetNextInt64() int64               { return i.Atoi64(i.Text()) }
func (i *Iost) Atof64(s string) float64           { x, _ := strconv.ParseFloat(s, 64); return x }
func (i *Iost) GetNextFloat64() float64           { return i.Atof64(i.Text()) }
func (i *Iost) Print(x ...interface{})            { fmt.Fprint(i.Writer, x...) }
func (i *Iost) Printf(s string, x ...interface{}) { fmt.Fprintf(i.Writer, s, x...) }
func (i *Iost) Println(x ...interface{})          { fmt.Fprintln(i.Writer, x...) }
func isLocal() bool                               { return os.Getenv("I") == "IronMan" }
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	if isLocal() {
		fp, _ = os.Open(os.Getenv("END_GAME"))
	}
	iost = NewIost(fp, wfp)
	defer func() {
		if isLocal() {
			iost.Println(recover())
		}
		iost.Writer.Flush()
	}()
	solve()
	for i := 0; isLocal() && i < 100; i++ {
		iost.Println("-----------------------------------")
		solve()
	}
}
func solve() {
	SetMod(Mod1000000007)
	t := iost.GetNextInt()
	for i := 0; i < t; i++ {
		testcase()
	}
}

func testcase() {
	n := iost.GetNextInt()
	mc := newModcom(n * 2)
	var ans Mint = mc.Facs[n*2].Div(2)
	iost.Println(ans)
}

// Mod constants.
const (
	Mod1000000007 = 1000000007
	Mod998244353  = 998244353
)

var (
	mod  Mint
	fmod func(Mint) Mint
)

// Mint treats the modular arithmetic
type Mint int64

// SetMod sets the mod. It must be called first.
func SetMod(newmod Mint) {
	switch newmod {
	case Mod1000000007:
		fmod = staticMod1000000007
	case Mod998244353:
		fmod = staticMod998244353
	default:
		mod = newmod
		fmod = dynamicMod
	}
}
func dynamicMod(m Mint) Mint {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}
func staticMod1000000007(m Mint) Mint {
	m %= Mod1000000007
	if m < 0 {
		return m + Mod1000000007
	}
	return m
}
func staticMod998244353(m Mint) Mint {
	m %= Mod998244353
	if m < 0 {
		return m + Mod998244353
	}
	return m
}

// Mod returns m % mod.
func (m Mint) Mod() Mint {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Mint) Inv() Mint {
	return m.Pow(Mint(0).Sub(2))
}

// Pow returns m^n
func (m Mint) Pow(n Mint) Mint {
	p := Mint(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Mint) Add(x Mint) Mint {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Mint) Sub(x Mint) Mint {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Mint) Mul(x Mint) Mint {
	return (m * x).Mod()
}

// Div returns m/x
func (m Mint) Div(x Mint) Mint {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Mint) AddAs(x Mint) *Mint {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Mint) SubAs(x Mint) *Mint {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Mint) MulAs(x Mint) *Mint {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Mint) DivAs(x Mint) *Mint {
	*m = m.Div(x)
	return m
}

type modcom struct {
	Facs, Invs []Mint
}

func newModcom(n int) modcom {
	facs := make([]Mint, n+1)
	invs := make([]Mint, n+1)
	facs[0] = 1
	for i := 0; i < n; i++ {
		facs[i+1] = facs[i].Mul(Mint(i + 1))
	}
	invs[n] = facs[n].Inv()
	for i := n - 1; i >= 0; i-- {
		invs[i] = invs[i+1].Mul(Mint(i + 1))
	}
	return modcom{
		Facs: facs,
		Invs: invs,
	}
}
func (mc *modcom) Choose(n, k int) Mint {
	if n < len(mc.Facs) {
		return mc.Facs[n].Mul(mc.Invs[k]).Mul(mc.Invs[n-k])
	}
	ans := mc.Invs[k]
	for k > 0 {
		ans.MulAs(Mint(n).Mod())
		n--
		k--
	}
	return ans
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"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
		fmt.Fscan(input, &n)

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

		// }

		// String := make([]byte, 0)
		// fmt.Fscan(input, &String)

		if n == 1 {
			fmt.Println(1)
		}
		if n > 1 {
			var B int64 = 3
			var modula int64 = 1000000007
			q := 2 * n

			for i := 4; i <= q; i++ {
				B = B * int64(i)
				B = B % modula
			}

			fmt.Println(B)
		}

	}

}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	for j := 0; j < n; j++ {
		s := 1
		var a int
		fmt.Scanf("%d\n", &a)
		for i := 3; i <= 2*a; i++ {
			s = s * i
			s = s % 1000000007
		}
		fmt.Println(s)
	}
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	for j := 0; j < n; j++ {
		s := 1
		var a int
		fmt.Scanf("%d\n", &a)
		for i := 3; i <= 2*a; i++ {
			s = s * i % 1000000007
		}
		fmt.Println(s)
	}
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	for j := 0; j < n; j++ {
		s := 1
		var a int
		fmt.Scanf("%d\n", &a)
		for i := 3; i <= 2*a; i++ {
			s = s * i
			if s > 1000000007 {
				s = s % 1000000007
			}
		}
		fmt.Println(s)
	}
}

package main

import (
	"fmt"
)

func main(){
	T := 0
	fmt.Scan(&T)
	MOD := 1000000007
	for it:=0; it<T; it++ {
		n := 0
		fmt.Scan(&n)
		n =n * 2
		res := MOD-MOD/2
		for i := 1; i<=n;i++{
			res = res * i % MOD
		}
		fmt.Println(res)
	}
}

package main

import (
	"fmt"
)

func main(){
	T := 0
	fmt.Scan(&T)
	MOD := 1000000007
	for it:=0; it<T; it++ {
		n := 0
		fmt.Scan(&n)
		n =n * 2
		res := MOD-MOD/2
		for i := 1; i<=n;i++{
			res = res * i % MOD
		}
		println(res)
	}
}


Valera has array a, consisting of n integers a0, a1, ..., an - 1, and function f(x), taking an integer from 0 to 2n - 1 as its single argument. Value f(x) is calculated by formula , where value bit(i) equals one if the binary representation of number x contains a 1 on the i-th position, and zero otherwise.For example, if n = 4 and x = 11 (11 = 20 + 21 + 23), then f(x) = a0 + a1 + a3.Help Valera find the maximum of function f(x) among all x, for which an inequality holds: 0 ≤ x ≤ m.

The first line contains integer n (1 ≤ n ≤ 105) — the number of array elements. The next line contains n space-separated integers a0, a1, ..., an - 1 (0 ≤ ai ≤ 104) — elements of array a. The third line contains a sequence of digits zero and one without spaces s0s1... sn - 1 — the binary representation of number m. Number m equals .

Print a single integer — the maximum value of function f(x) for all .

NoteIn the first test case m = 20 = 1, f(0) = 0, f(1) = a0 = 3.In the second sample m = 20 + 21 + 23 = 11, the maximum value of function equals f(5) = a0 + a2 = 17 + 10 = 27.

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

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

	fmt.Fscan(reader,&bin)

	ans,sum := 0, 0
	for i:=0; i<len(bin) ; i++{
		if bin[i] == '0'{
			sum+=a[i]
		}else{
			if a[i]>sum {
				ans+=a[i]
			}else{
				ans+=sum
				sum=a[i]
			}
		}
	}
	fmt.Println(ans)
}

// 353C
package main

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

func main() {
	var n int
	var a [100001]int
	var b string
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}
	fmt.Fscan(in, &b)
	c := 0
	ans := 0
	for i := 0; i < len(b); i++ {
		if b[i] == '0' {
			c += a[i]
		} else {
			if a[i] > c {
				ans = ans + a[i]
			} else {
				ans = ans + c
				c = a[i]
			}
		}
	}
	fmt.Print(ans)
}

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

func main(){
	var n int
	var bin string
	fmt.Scan(&n)
	a:=make([]int,n)
	inp := bufio.NewScanner(os.Stdin)
	inp.Split(bufio.ScanWords)

	for i:=0; i<n; i++{
		inp.Scan()
		a[i], _ = strconv.Atoi(inp.Text())
	}

	inp.Scan()
	bin = inp.Text()

	ans,sum := 0, 0
	for i:=0; i<len(bin) ; i++{
		if bin[i] == '0'{
			sum+=a[i]
		}else{
			if a[i]>sum {
				ans+=a[i]
			}else{
				ans+=sum
				sum=a[i]
			}
		}
	}
	fmt.Println(ans)
}

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

func main(){
	var n int
	var bin string
	fmt.Scan(&n)
	a:=make([]int,n)
	inp := bufio.NewScanner(os.Stdin)
	inp.Split(bufio.ScanWords)

	for i:=0; i<n; i++{
		inp.Scan()
		a[i], _ = strconv.Atoi(inp.Text())
	}
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader,&bin)

	ans,sum := 0, 0
	for i:=0; i<len(bin) ; i++{
		if bin[i] == '0'{
			sum+=a[i]
		}else{
			if a[i]>sum {
				ans+=a[i]
			}else{
				ans+=sum
				sum=a[i]
			}
		}
	}
	fmt.Println(ans)
}

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

func main(){
	var n int
	var bin string
	fmt.Scan(&n)
	a:=make([]int,n)
	inp := bufio.NewScanner(os.Stdin)
	inp.Split(bufio.ScanWords)

	for i:=0; i<n; i++{
		inp.Scan()
		a[i], _ = strconv.Atoi(inp.Text())
	}
	fmt.Scan(&bin)
	ans,sum := 0, 0
	for i:=0; i<len(bin) ; i++{
		if bin[i] == '0'{
			sum+=a[i]
		}else{
			if a[i]>sum {
				ans+=a[i]
			}else{
				ans+=sum
				sum=a[i]
			}
		}
	}
	fmt.Println(ans)
}

// 353C
package main

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

func main() {
	var n int
	var a [100001]int
	var b string
	fmt.Scan(&n)
	in := bufio.NewReader(os.Stdin)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}
	fmt.Scan(&b)
	c := 0
	ans := 0
	for i := 0; i < len(b); i++ {
		if b[i] == '0' {
			c += a[i]
		} else {
			if a[i] > c {
				ans = ans + a[i]
			} else {
				ans = ans + c
				c = a[i]
			}
		}
	}
	fmt.Print(ans)
}


There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is ai liters.  Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the i-th vessel goes to the (i + 1)-th one. The liquid that overflows from the n-th vessel spills on the floor.Your task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries:  Add xi liters of water to the pi-th vessel;  Print the number of liters of water in the ki-th vessel. When you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels.

The first line contains integer n — the number of vessels (1 ≤ n ≤ 2·105). The second line contains n integers a1, a2, ..., an — the vessels' capacities (1 ≤ ai ≤ 109). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer m — the number of queries (1 ≤ m ≤ 2·105). Each of the next m lines contains the description of one query. The query of the first type is represented as "1 pi xi", the query of the second type is represented as "2 ki" (1 ≤ pi ≤ n, 1 ≤ xi ≤ 109, 1 ≤ ki ≤ n).

For each query, print on a single line the number of liters of water in the corresponding vessel.

package main

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

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

	var n int
	Fscan(in, &n)
	water := make([]int, n)
	cp := make([]int, n)
	for i := range cp {
		Fscan(in, &cp[i])
	}

	fa := make([]int, n)
	for i := range fa {
		fa[i] = i
	}
	var find func(int) int
	find = func(i int) int {
		if fa[i] != i {
			fa[i] = find(fa[i])
		}
		return fa[i]
	}
	merge := func(from, to int) { fa[find(from)] = find(to) }

	var m, op, idx, x int
	for Fscan(in, &m); m > 0; m-- {
		Fscan(in, &op, &idx)
		idx--
		if op == 1 {
			Fscan(in, &x)
			for idx = find(idx); ; idx = find(idx) {
				if water[idx]+x < cp[idx] {
					water[idx] += x
					break
				}
				x -= cp[idx] - water[idx]
				water[idx] = cp[idx]
				idx++
				if idx == n {
					break
				}
				merge(idx-1, idx)
			}
		} else {
			Fprintln(out, water[idx])
		}
	}
}

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

package main

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

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

	var n int
	Fscan(in, &n)
	water := make([]int, n)
	cp := make([]int, n)
	for i := range cp {
		Fscan(in, &cp[i])
	}

	fa := make([]int, n)
	for i := range fa {
		fa[i] = i
	}
	var find func(int) int
	find = func(i int) int {
		if fa[i] != i {
			fa[i] = find(fa[i])
		}
		return fa[i]
	}
	merge := func(from, to int) { fa[find(from)] = find(to) }

	var m, op, idx, x int
	for Fscan(in, &m); m > 0; m-- {
		Fscan(in, &op, &idx)
		idx--
		if op == 1 {
			Fscan(in, &x)
			for idx = find(idx); ; idx = find(idx) {
				if water[idx]+x < cp[idx] {
					water[idx] += x
					break
				}
				x -= cp[idx] - water[idx]
				water[idx] = cp[idx]
				idx++
				if idx == n {
					break
				}
				merge(idx-1, idx)
			}
		} else {
			Fprintln(out, water[idx])
		}
	}
}

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

package main

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

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

	var n int
	Fscan(in, &n)
	water := make([]int, n)
	cp := make([]int, n)
	for i := range cp {
		Fscan(in, &cp[i])
	}

	fa := make([]int, n)
	for i := range fa {
		fa[i] = i
	}
	var find func(int) int
	find = func(x int) int {
		for fa[x] != x {
			fa[x] = fa[fa[x]]
			x = fa[x]
		}
		return x
	}
	merge := func(from, to int) { fa[find(from)] = find(to) }

	var m, op, idx, x int
	for Fscan(in, &m); m > 0; m-- {
		Fscan(in, &op, &idx)
		idx--
		if op == 1 {
			Fscan(in, &x)
			for idx = find(idx); ; idx = find(idx) {
				if water[idx]+x < cp[idx] {
					water[idx] += x
					break
				}
				x -= cp[idx] - water[idx]
				water[idx] = cp[idx]
				idx++
				if idx == n {
					break
				}
				merge(idx-1, idx)
			}
		} else {
			Fprintln(out, water[idx])
		}
	}
}

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


Bethany would like to tile her bathroom. The bathroom has width $$$w$$$ centimeters and length $$$l$$$ centimeters. If Bethany simply used the basic tiles of size $$$1 \times 1$$$ centimeters, she would use $$$w \cdot l$$$ of them. However, she has something different in mind.   On the interior of the floor she wants to use the $$$1 \times 1$$$ tiles. She needs exactly $$$(w-2) \cdot (l-2)$$$ of these.  On the floor boundary she wants to use tiles of size $$$1 \times a$$$ for some positive integer $$$a$$$. The tiles can also be rotated by $$$90$$$ degrees. For which values of $$$a$$$ can Bethany tile the bathroom floor as described? Note that $$$a$$$ can also be $$$1$$$.

Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1\le t\le 100$$$) — the number of test cases. The descriptions of the $$$t$$$ test cases follow. Each test case consist of a single line, which contains two integers $$$w$$$, $$$l$$$ ($$$3 \leq w, l \leq 10^{9}$$$) — the dimensions of the bathroom.

For each test case, print an integer $$$k$$$ ($$$0\le k$$$) — the number of valid values of $$$a$$$ for the given test case — followed by $$$k$$$ integers $$$a_1, a_2,\dots, a_k$$$ ($$$1\le a_i$$$) — the valid values of $$$a$$$. The values $$$a_1, a_2, \dots, a_k$$$ have to be sorted from smallest to largest. It is guaranteed that under the problem constraints, the output contains at most $$$200\,000$$$ integers.

NoteIn the first test case, the bathroom is $$$3$$$ centimeters wide and $$$5$$$ centimeters long. There are three values of $$$a$$$ such that Bethany can tile the floor as described in the statement, namely $$$a=1$$$, $$$a=2$$$ and $$$a=3$$$. The three tilings are represented in the following pictures.

package main

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

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

var ans map[int]int

func slv(w, l, c int) {
	for i := 1; i*i <= (w - 2 + c); i++ {
		if (w-2+c)%i == 0 {
			if (l-c)%i == 0 {
				ans[i] = 1
			}
			j := (w - 2 + c) / i
			if (l-c)%j == 0 {
				ans[j] = 1
			}
		}
	}
}

func solve() {
	var w, l int
	fmt.Fscan(in, &w, &l)
	ans = make(map[int]int)
	ans[1], ans[2] = 1, 1
	slv(w, l, 0)
	slv(w, l, 1)
	slv(w, l, 2)
	z := make([]int, 0)
	for u, _ := range ans {
		z = append(z, u)
	}
	sort.Ints(z)
	fmt.Fprintf(out, "%d", len(z))
	for _, v := range z {
		fmt.Fprintf(out, " %d", v)
	}
	fmt.Fprintf(out, "\n")
}

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

package main

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

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

var ans map[int]int

func slv(w, l, c int) {
	for i := 1; i*i <= (w - 2 + c); i++ {
		if (w+2-c)%i == 0 {
			if (l-c)%i == 0 {
				ans[i] = 1
			}
			j := (w - 2 + c) / i
			if (l-c)%j == 0 {
				ans[j] = 1
			}
		}
	}
}

func solve() {
	var w, l int
	fmt.Fscan(in, &w, &l)
	ans = make(map[int]int)
	ans[1], ans[2] = 1, 1
	slv(w, l, 0)
	slv(w, l, 1)
	slv(w, l, 2)
	z := make([]int, 0)
	for u, _ := range ans {
		z = append(z, u)
	}
	sort.Ints(z)
	fmt.Fprintf(out, "%d", len(z))
	for _, v := range z {
		fmt.Fprintf(out, " %d", v)
	}
	fmt.Fprintf(out, "\n")
}

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


Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·ai + q·aj + r·ak for given p, q, r and array a1, a2, ... an such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative.

First line of input contains 4 integers n, p, q, r ( - 109 ≤ p, q, r ≤ 109, 1 ≤ n ≤ 105). Next line of input contains n space separated integers a1, a2, ... an ( - 109 ≤ ai ≤ 109).

Output a single integer the maximum value of p·ai + q·aj + r·ak that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n.

NoteIn the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30.In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int64 {
	if !in.Scan() {
		panic(in.Err())
	}
	res := int64(0)
	mul := int64(1)
	for _, b := range in.Bytes() {
		if b == '-' {
			mul *= -1
			continue
		}
		res = res*10 + int64(b-'0')
	}
	return mul * res
}

func main() {
	in.Split(bufio.ScanWords)
	n := readInt()
	p := readInt()
	q := readInt()
	r := readInt()
	a := make([]int64, n)
	for i := range a {
		a[i] = readInt()
	}

	bestP := make([]int64, n)
	bestP[0] = p * a[0]
	for i := int64(1); i < n; i++ {
		bestP[i] = bestP[i-1]
		if c := a[i] * p; bestP[i] < c {
			bestP[i] = c
		}
	}

	bestR := make([]int64, n)
	bestR[n-1] = a[n-1] * r
	for i := n - 2; i >= 0; i-- {
		bestR[i] = bestR[i+1]
		if c := a[i] * r; bestR[i] < c {
			bestR[i] = c
		}
	}

	ans := a[0] * (p + q + r)
	for i := range a {
		if c := a[i]*q + bestP[i] + bestR[i]; ans < c {
			ans = c
		}
	}
	fmt.Println(ans)
}

// B. Marvolo Gaunt's Ring
/*
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made.

Value of x is calculated as maximum of p·a i + q·a j + r·a k for given p, q, r and array a 1, a 2, ... a n such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative.

Input
First line of input contains 4 integers n, p, q, r ( - 109 ≤ p, q, r ≤ 109, 1 ≤ n ≤ 105).

Next line of input contains n space separated integers a 1, a 2, ... a n ( - 109 ≤ a i ≤ 109).

Output
Output a single integer the maximum value of p·a i + q·a j + r·a k that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n.

Examples
Input
5 1 2 3
1 2 3 4 5

Output
30

Input
5 1 2 -3
-1 -2 -3 -4 -5

Output
12

Note
In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30.

In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
*/
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	input1, _ := reader.ReadString('\n')
	arr1 := strings.Split(strings.TrimSpace(input1), " ")

	n, _ := strconv.ParseInt(arr1[0], 10, 64)
	p, _ := strconv.ParseInt(arr1[1], 10, 64)
	q, _ := strconv.ParseInt(arr1[2], 10, 64)
	r, _ := strconv.ParseInt(arr1[3], 10, 64)

	input2, _ := reader.ReadString('\n')
	aArr := strings.Split(strings.TrimSpace(input2), " ")

	var d1, d2, d3 int64 = math.MinInt64, math.MinInt64, math.MinInt64
	var i int64 = 0
	for ; i < n; i++ {
		a, _ := strconv.ParseInt(aArr[i], 10, 64)

		if d1 < p*a {
			d1 = p * a
		}
		if d2 < q*a+d1 {
			d2 = q*a + d1
		}
		if d3 < r*a+d2 {
			d3 = r*a + d2
		}
	}
	fmt.Print(d3)
}

package main

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

func solve() {
	n := readInt()
	v := make([]int64, 3)
	for i := 0; i < 3; i++ {
		v[i] = readInt64()
	}
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = readInt64()
	}
	suffix := make([]int64, n)
	for j := n - 1; j >= 0; j-- {
		if j == n-1 {
			suffix[j] = v[2] * a[j]
		} else {
			suffix[j] = max(suffix[j+1], v[2]*a[j])
		}
	}
	bestFirst := int64(math.MinInt64)
	res := int64(math.MinInt64)
	for i := 0; i < n; i++ {
		bestFirst = max(bestFirst, v[0]*a[i])
		best := bestFirst + (v[1] * a[i]) + suffix[i]
		if best > res {
			res = best
		}
	}
	fmt.Println(res)
}

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

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

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

package main

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

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

	return y
}

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1<<20)

	var n int
	var p, q, r, t int64
	var dp [3]int64

	fmt.Fscanf(reader, "%d %d %d %d\n", &n, &p, &q, &r)

	for i := 0; i < n; i++ {
		if i+1 != n {
			fmt.Fscanf(reader, "%d", &t)
		} else {
			fmt.Fscanf(reader, "%d\n", &t)
		}

		if i != 0 {
			dp[0] = max(dp[0], t*p)
			dp[1] = max(dp[1], dp[0]+t*q)
			dp[2] = max(dp[2], dp[1]+t*r)
		} else {
			dp[0] = t * p
			dp[1] = t*q + dp[0]
			dp[2] = t*r + dp[1]
		}
	}

	fmt.Println(dp[2])
}

package main

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

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

	return y
}

func readInt64(r *bufio.Reader, delime byte) (n int64) {
	var i, j int
	for {
		s, err := r.ReadSlice(delime)

		if len(s) > 0 && s[len(s)-1] == delime {
			s = s[:len(s)-1]
		}

		if len(s) > 0 && s[len(s)-1] == '\r' {
			s = s[:len(s)-1]
		}

		for i = 0; i < len(s) && s[i] == ' '; i++ {

		}

		for j = len(s) - 1; j >= i && s[j] == ' '; j-- {

		}

		if j >= i {
			n, _ = strconv.ParseInt(string(s[i:j+1]), 10, 64)
			return
		}

		if j < i && err == nil {
			continue
		}

		if j < i && err != nil {
			return
		}
	}

	return
}

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1<<20)

	var n int
	var p, q, r, t int64
	var dp [3]int64

	fmt.Fscanf(reader, "%d %d %d %d\n", &n, &p, &q, &r)

	for i := 0; i < n; i++ {
		if i+1 != n {
			t = readInt64(reader, ' ')
		} else {
			t = readInt64(reader, '\n')
		}

		if i != 0 {
			dp[0] = max(dp[0], t*p)
			dp[1] = max(dp[1], dp[0]+t*q)
			dp[2] = max(dp[2], dp[1]+t*r)
		} else {
			dp[0] = t * p
			dp[1] = t*q + dp[0]
			dp[2] = t*r + dp[1]
		}
	}

	fmt.Println(dp[2])
}

package main

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

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

	return y
}

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

	var n int
	var p, q, r, t int64
	var dp [3]int64

	fmt.Fscanf(reader, "%d %d %d %d\n", &n, &p, &q, &r)

	for i := 0; i < n; i++ {
		if i+1 != n {
			fmt.Fscanf(reader, "%d", &t)
		} else {
			fmt.Fscanf(reader, "%d\n", &t)
		}

		if i != 0 {
			dp[0] = max(dp[0], t*p)
			dp[1] = max(dp[1], dp[0]+t*q)
			dp[2] = max(dp[2], dp[1]+t*r)
		} else {
			dp[0] = t * p
			dp[1] = t*q + dp[0]
			dp[2] = t*r + dp[1]
		}
	}

	fmt.Println(dp[2])
}

package main

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

type Node struct {
	max, min int64
}

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

func Min(x, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

func pushUp(a []Node, root int) {
	l := 2 * root
	r := l + 1
	a[root].max = Max(a[l].max, a[r].max)
	a[root].min = Min(a[l].min, a[r].min)
}

func build(a []Node, root, l, r int) {
	if l == r {
		a[root].max = s[l-1]
		a[root].min = s[l-1]

		return
	}

	mid := (l + r) / 2
	if l <= mid {
		build(a, 2*root, l, mid)
	}

	if r > mid {
		build(a, 2*root+1, mid+1, r)
	}

	pushUp(a, root)
}

func queryMax(a []Node, root, l, r, ll, rr int) int64 {
	//fmt.Println(root, l, r, ll, rr)

	if ll <= l && r <= rr {
		return a[root].max
	}

	var ans int64 = -(1 << 40)

	mid := (l + r) / 2

	if ll <= mid {
		ans = Max(ans, queryMax(a, 2*root, l, mid, ll, rr))
	}

	if mid < rr {
		ans = Max(ans, queryMax(a, 2*root+1, mid+1, r, ll, rr))
	}

	return ans
}

func queryMin(a []Node, root, l, r, ll, rr int) int64 {
	//fmt.Println(root, l, r, ll, rr)

	if ll <= l && r <= rr {
		return a[root].min
	}

	var ans int64 = 1 << 40

	mid := (l + r) / 2

	if ll <= mid {
		ans = Min(ans, queryMin(a, 2*root, l, mid, ll, rr))
	}

	if mid < rr {
		ans = Min(ans, queryMin(a, 2*root+1, mid+1, r, ll, rr))
	}

	return ans
}

var s []int64

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

	var n int
	var p, q, r int64

	fmt.Fscanf(reader, "%d %d %d %d\n", &n, &p, &q, &r)

	s = make([]int64, n)
	d := make([]Node, 4*(n+1))

	for i := 0; i < n; i++ {
		if i+1 != n {
			fmt.Fscanf(reader, "%d", &s[i])
		} else {
			fmt.Fscanf(reader, "%d\n", &s[i])
		}
	}

	build(d, 1, 1, n)

	var ans int64

	for i := 0; i < n; i++ {
		temp := q * s[i]

		if p > 0 {
			temp += (p * queryMax(d, 1, 1, n, 1, i+1))
		} else {
			temp += (p * queryMin(d, 1, 1, n, 1, i+1))
		}

		if r > 0 {
			temp += (r * queryMax(d, 1, 1, n, i+1, n))
		} else {
			temp += (r * queryMin(d, 1, 1, n, i+1, n))
		}

		if i == 0 {
			ans = temp
		} else {
			ans = Max(ans, temp)
		}
	}

	fmt.Println(ans)
}

package main

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

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

	return y
}

var a [3][]int64

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

	var n int
	var p, q, r int64

	fmt.Fscanf(reader, "%d %d %d %d\n", &n, &p, &q, &r)
	a[0] = make([]int64, n)
	a[1] = make([]int64, n)
	a[2] = make([]int64, n)
	for i := 0; i < n; i++ {
		if i+1 != n {
			fmt.Fscanf(reader, "%d", &a[0][i])
		} else {
			fmt.Fscanf(reader, "%d\n", &a[0][i])
		}
	}

	a[1][0] = a[0][0] * p
	for i := 1; i < n; i++ {
		a[1][i] = max(a[0][i]*p, a[1][i-1])
	}

	a[2][n-1] = a[0][n-1] * r
	for i := n - 2; i >= 0; i-- {
		a[2][i] = max(a[0][i]*r, a[2][i+1])
	}

	var ans int64 = a[1][0] + a[0][0]*q + a[2][0]
	for i := 1; i < n; i++ {
		ans = max(ans, a[1][i]+a[0][i]*q+a[2][i])
	}

	fmt.Println(ans)
}

// B. Marvolo Gaunt's Ring
/*
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made.

Value of x is calculated as maximum of p·a i + q·a j + r·a k for given p, q, r and array a 1, a 2, ... a n such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative.

Input
First line of input contains 4 integers n, p, q, r ( - 109 ≤ p, q, r ≤ 109, 1 ≤ n ≤ 105).

Next line of input contains n space separated integers a 1, a 2, ... a n ( - 109 ≤ a i ≤ 109).

Output
Output a single integer the maximum value of p·a i + q·a j + r·a k that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n.

Examples
Input
5 1 2 3
1 2 3 4 5

Output
30

Input
5 1 2 -3
-1 -2 -3 -4 -5

Output
12

Note
In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30.

In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
*/
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	input1, _ := reader.ReadString('\n')
	arr1 := strings.Split(strings.TrimSpace(input1), " ")

	n, _ := strconv.ParseInt(arr1[0], 10, 64)
	p, _ := strconv.ParseInt(arr1[1], 10, 64)
	q, _ := strconv.ParseInt(arr1[2], 10, 64)
	r, _ := strconv.ParseInt(arr1[3], 10, 64)

	input2, _ := reader.ReadString('\n')
	aArr := strings.Split(strings.TrimSpace(input2), " ")

	var d1, d2, d3 int64 = math.MinInt64, math.MinInt64, math.MinInt64
	var i int64 = 0
	for ; i < n; i++ {
		a, _ := strconv.ParseInt(aArr[i], 10, 64)

		if d1 < p*a {
			d1 = p * a
		}
		if d2 < q*a {
			d2 = q * a
		}
		if d3 < r*a {
			d3 = r * a
		}
	}
	fmt.Print(d1 + d2 + d3)
}

package main

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

func solve() {
	n := readInt()
	v := make([]int64, 3)
	for i := 0; i < 3; i++ {
		v[i] = readInt64()
	}
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = readInt64()
	}
	prefix := make([][]int64, 3)
	for i := 0; i < 3; i++ {
		prefix[i] = make([]int64, n)
		for j := n - 1; j >= 0; j-- {
			if j == n-1 {
				prefix[i][j] = v[i] * a[j]
			} else {
				prefix[i][j] = max(prefix[i][j+1], v[i]*a[j])
			}
		}
	}
	res := int64(0)
	for i := 0; i < n; i++ {
		res = max(res, prefix[0][i]+prefix[1][i]+prefix[2][i])
	}
	fmt.Println(res)
}

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

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

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

package main

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

func solve() {
	n := readInt()
	p := readInt()
	q := readInt()
	r := readInt()
	v := []int{p, q, r}
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = readInt()
	}
	res := int64(0)
	for i := 0; i < 3; i++ {
		c := int64(v[i])
		hi := int64(math.MinInt64)
		for j := 0; j < n; j++ {
			tmp := c * int64(a[j])
			if tmp > hi {
				hi = tmp
			}
		}
		res += hi
	}
	fmt.Println(res)
}

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

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

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

package main

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

func solve() {
	n := readInt()
	v := make([]int64, 3)
	for i := 0; i < 3; i++ {
		v[i] = readInt64()
	}
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = readInt64()
	}
	res := int64(0)
	for i := 0; i < 3; i++ {
		best := v[i] * a[0]
		for j := 1; j < n; j++ {
			tmp := v[i] * a[j]
			if tmp > best {
				best = tmp
			}
		}
		res += best
	}
	fmt.Println(res)
}

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

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

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

package main

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

func solve() {
	n := readInt()
	p := readInt()
	q := readInt()
	r := readInt()
	v := []int{p, q, r}
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = readInt()
	}
	res := int64(0)
	for i := 0; i < 3; i++ {
		c := int64(v[i])
		best := c * int64(a[0])
		for j := 1; j < n; j++ {
			tmp := c * int64(a[j])
			if tmp > best {
				best = tmp
			}
		}
		res += best
	}
	fmt.Println(res)
}

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

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

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

package main

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

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

	return y
}

func readInt64(r *bufio.Reader, delime byte) (n int64) {
	var i, j int
	for {
		s, err := r.ReadSlice(delime)

		fmt.Println("read", s, err)
		if len(s) > 0 && s[len(s)-1] == delime {
			s = s[:len(s)-1]
		}
		for i = 0; i < len(s) && s[i] == ' '; i++ {

		}

		for j = len(s) - 1; j >= i && s[j] == ' '; j-- {

		}

		if j >= i {
			n, _ = strconv.ParseInt(string(s[i:j+1]), 10, 64)
			return
		}

		if j < i && err == nil {
			continue
		}

		if j < i && err != nil {
			return
		}
	}

	return
}

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1<<20)

	var n int
	var p, q, r, t int64
	var dp [3]int64

	fmt.Fscanf(reader, "%d %d %d %d\n", &n, &p, &q, &r)

	for i := 0; i < n; i++ {
		if i+1 != n {
			t = readInt64(reader, ' ')
		} else {
			t = readInt64(reader, '\n')
		}
		fmt.Println("cacl", t)
		if i != 0 {
			dp[0] = max(dp[0], t*p)
			dp[1] = max(dp[1], dp[0]+t*q)
			dp[2] = max(dp[2], dp[1]+t*r)
		} else {
			dp[0] = t * p
			dp[1] = t*q + dp[0]
			dp[2] = t*r + dp[1]
		}
	}

	fmt.Println(dp[2])
}

package main

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

type Node struct {
	max, min int64
}

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

func Min(x, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

func pushUp(a []Node, root int) {
	l := 2 * root
	r := l + 1
	a[root].max = Max(a[l].max, a[r].max)
	a[root].min = Min(a[l].min, a[r].min)
}

func build(a []Node, root, l, r int) {
	if l == r {
		a[root].max = s[l-1]
		a[root].min = s[l-1]

		return
	}

	mid := (l + r) / 2
	if l <= mid {
		build(a, 2*root, l, mid)
	}

	if r > mid {
		build(a, 2*root+1, mid+1, r)
	}

	pushUp(a, root)
}

func queryMax(a []Node, root, l, r, ll, rr int) int64 {
	//fmt.Println(root, l, r, ll, rr)

	if ll <= l && r <= rr {
		return a[root].max
	}

	var ans int64

	mid := (l + r) / 2

	if ll <= mid {
		ans = queryMax(a, 2*root, l, mid, ll, rr)
	}

	if mid < rr {
		ans = Max(ans, queryMax(a, 2*root+1, mid+1, r, ll, rr))
	}

	return ans
}

func queryMin(a []Node, root, l, r, ll, rr int) int64 {
	//fmt.Println(root, l, r, ll, rr)

	if ll <= l && r <= rr {
		return a[root].min
	}

	var ans int64

	mid := (l + r) / 2

	if ll <= mid {
		ans = queryMin(a, 2*root, l, mid, ll, rr)
	}

	if mid < rr {
		ans = Min(ans, queryMin(a, 2*root+1, mid+1, r, ll, rr))
	}

	return ans
}

var s []int64

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

	var n int
	var p, q, r int64

	fmt.Fscanf(reader, "%d %d %d %d\n", &n, &p, &q, &r)

	s = make([]int64, n)
	d := make([]Node, 4*(n+1))

	for i := 0; i < n; i++ {
		if i+1 != n {
			fmt.Fscanf(reader, "%d", &s[i])
		} else {
			fmt.Fscanf(reader, "%d\n", &s[i])
		}
	}

	build(d, 1, 1, n)

	var ans int64

	for i := 0; i < n; i++ {
		temp := q * s[i]

		if p > 0 {
			temp += (p * queryMax(d, 1, 1, n, 1, i+1))
		} else {
			temp += (p * queryMin(d, 1, 1, n, 1, i+1))
		}

		if r > 0 {
			temp += (r * queryMax(d, 1, 1, n, i+1, n))
		} else {
			temp += (r * queryMin(d, 1, 1, n, i+1, n))
		}

		if i == 0 {
			ans = temp
		} else {
			ans = Max(ans, temp)
		}
	}

	fmt.Println(ans)
}

package main

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

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

	return y
}

func readInt64(r *bufio.Reader, delime byte) (n int64) {
	var i, j int
	for {
		s, err := r.ReadSlice(delime)
		if len(s) > 0 && s[len(s)-1] == delime {
			s = s[:len(s)-1]
		}
		for i = 0; i < len(s) && s[i] == ' '; i++ {

		}

		for j = len(s) - 1; j >= i && s[j] == ' '; j-- {

		}

		if j >= i {
			n, _ = strconv.ParseInt(string(s[i:j+1]), 10, 64)
			return
		}

		if j < i && err == nil {
			continue
		}

		if j < i && err != nil {
			return
		}
	}

	return
}

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1<<20)

	var n int
	var p, q, r, t int64
	var dp [3]int64

	fmt.Fscanf(reader, "%d %d %d %d\n", &n, &p, &q, &r)

	for i := 0; i < n; i++ {
		if i+1 != n {
			t = readInt64(reader, ' ')
		} else {
			t = readInt64(reader, '\n')
		}

		if i != 0 {
			dp[0] = max(dp[0], t*p)
			dp[1] = max(dp[1], dp[0]+t*q)
			dp[2] = max(dp[2], dp[1]+t*r)
		} else {
			dp[0] = t * p
			dp[1] = t*q + dp[0]
			dp[2] = t*r + dp[1]
		}
	}

	fmt.Println(dp[2])
}

package main

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

type Node struct {
	sum [3]int64
}

func pushUp(a []Node, root int) {
	a[root].sum[0] = a[2*root].sum[0]
	a[root].sum[1] = a[2*root].sum[1]
	a[root].sum[2] = a[2*root].sum[2]

	for i := 2; i >= 0; i-- {
		if a[2*root+1].sum[i] >= a[2*root].sum[i] {
			a[root].sum[i] = a[2*root+1].sum[i]
		} else {
			break
		}
	}
}

var pp, qq, rr int64

func build(a []Node, root, l, r int) {
	if l == r {
		a[root].sum[0] = pp * s[l-1]
		a[root].sum[1] = qq * s[l-1]
		a[root].sum[2] = rr * s[l-1]

		return
	}

	mid := (l + r) / 2
	if l <= mid {
		build(a, 2*root, l, mid)
	}

	if r > mid {
		build(a, 2*root+1, mid+1, r)
	}

	pushUp(a, root)
}

var s []int64

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

	var n int

	fmt.Fscanf(reader, "%d %d %d %d\n", &n, &pp, &qq, &rr)

	s = make([]int64, n)
	d := make([]Node, 4*(n+1))

	for i := 0; i < n; i++ {
		if i+1 != n {
			fmt.Fscanf(reader, "%d", &s[i])
		} else {
			fmt.Fscanf(reader, "%d\n", &s[i])
		}
	}

	build(d, 1, 1, n)

	fmt.Println(d[1].sum[0] + d[1].sum[1] + d[1].sum[2])
}