In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecutive elements and find the median of each five. A median is called the middle element of a sorted array (it's the third largest element for a group of five). To increase the algorithm's performance speed on a modern video card, you should be able to find a sum of medians in each five of the array.A sum of medians of a sorted k-element set S = {a1, a2, ..., ak}, where a1 < a2 < a3 < ... < ak, will be understood by as The  operator stands for taking the remainder, that is  stands for the remainder of dividing x by y.To organize exercise testing quickly calculating the sum of medians for a changing set was needed.

The first line contains number n (1 ≤ n ≤ 105), the number of operations performed. Then each of n lines contains the description of one of the three operations:    add x — add the element x to the set;  del x — delete the element x from the set;  sum — find the sum of medians of the set.  For any add x operation it is true that the element x is not included in the set directly before the operation. For any del x operation it is true that the element x is included in the set directly before the operation. All the numbers in the input are positive integers, not exceeding 109.

For each operation sum print on the single line the sum of medians of the current set. If the set is empty, print 0. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams (also you may use the %I64d specificator).

14
add 1
add 7
add 2
add 5
sum
add 6
add 8
add 9
add 3
add 4
add 10
sum
del 1
sum

package main

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

type Query struct {
	op string
	x  int
}

type Block struct {
	x   []int
	sum [5]int64
}

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

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

	queries := make([]Query, n)
	xBlock := map[int]int{}
	xs := []int{}

	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%s", &queries[i].op)
		if queries[i].op == "sum" {
			fmt.Fscanf(reader, "\n")
		} else {
			fmt.Fscanf(reader, " %d\n", &queries[i].x)
			if _, ok := xBlock[queries[i].x]; !ok {
				xBlock[queries[i].x] = 0
				xs = append(xs, queries[i].x)
			}
		}
	}
	sort.Ints(xs)
	cnt := int(math.Sqrt(float64(len(xs))))
	if cnt == 0 {
		cnt = 1
	}
	for i := 0; i < len(xs); i++ {
		xBlock[xs[i]] = i / cnt
	}
	blocks := make([]Block, (len(xs)-1)/cnt+1)
	for i := 0; i < len(blocks); i++ {
		blocks[i].x = []int{}
	}

	for i := 0; i < len(queries); i++ {
		switch queries[i].op {
		case "add":
			b := &blocks[xBlock[queries[i].x]]
			b.x = append(b.x, queries[i].x)
			for j := len(b.x) - 2; j >= 0; j-- {
				if b.x[j] > b.x[j+1] {
					b.x[j], b.x[j+1] = b.x[j+1], b.x[j]
				} else {
					break
				}
			}
			b.sum = [5]int64{0, 0, 0, 0, 0}
			for j := 0; j < len(b.x); j++ {
				b.sum[j%5] += int64(b.x[j])
			}
		case "del":
			b := &blocks[xBlock[queries[i].x]]
			for j := 0; j < len(b.x); j++ {
				if b.x[j] == queries[i].x {
					b.x = append(b.x[:j], b.x[j+1:]...)
				}
			}
			b.sum = [5]int64{0, 0, 0, 0, 0}
			for j := 0; j < len(b.x); j++ {
				b.sum[j%5] += int64(b.x[j])
			}
		case "sum":
			cnt, ans := 0, int64(0)
			for j := 0; j < len(blocks); j++ {
				ans += blocks[j].sum[(7-cnt%5)%5]
				cnt += len(blocks[j].x)
			}
			fmt.Printf("%d\n", ans)
		}
	}

}


Kilani and Abd are neighbors for 3000 years, but then the day came and Kilani decided to move to another house. As a farewell gift, Kilani is going to challenge Abd with a problem written by their other neighbor with the same name Abd.  The problem is:You are given a connected tree rooted at node $$$1$$$.You should assign a character a or b to every node in the tree so that the total number of a's is equal to $$$x$$$ and the total number of b's is equal to $$$n - x$$$.Let's define a string for each node $$$v$$$ of the tree as follows:   if $$$v$$$ is root then the string is just one character assigned to $$$v$$$:  otherwise, let's take a string defined for the $$$v$$$'s parent $$$p_v$$$ and add to the end of it a character assigned to $$$v$$$. You should assign every node a character in a way that minimizes the number of distinct strings among the strings of all nodes.

The first line contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 10^5$$$; $$$0 \leq x \leq n$$$) — the number of vertices in the tree the number of a's. The second line contains $$$n - 1$$$ integers $$$p_2, p_3, \dots, p_{n}$$$ ($$$1 \leq p_i \leq n$$$; $$$p_i \neq i$$$), where $$$p_i$$$ is the parent of node $$$i$$$. It is guaranteed that the input describes a connected tree.

In the first line, print the minimum possible total number of distinct strings. In the second line, print $$$n$$$ characters, where all characters are either a or b and the $$$i$$$-th character is the character assigned to the $$$i$$$-th node. Make sure that the total number of a's is equal to $$$x$$$ and the total number of b's is equal to $$$n - x$$$. If there is more than one answer you can print any of them.

NoteThe tree from the sample is shown below:  The tree after assigning characters to every node (according to the output) is the following:  Strings for all nodes are the following:   string of node $$$1$$$ is: a  string of node $$$2$$$ is: aa  string of node $$$3$$$ is: aab  string of node $$$4$$$ is: aab  string of node $$$5$$$ is: aabb  string of node $$$6$$$ is: aabb  string of node $$$7$$$ is: aabb  string of node $$$8$$$ is: aabb  string of node $$$9$$$ is: aa The set of unique strings is $$$\{\text{a}, \text{aa}, \text{aab}, \text{aabb}\}$$$, so the number of distinct strings is $$$4$$$.

package main

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

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

	n, x := readTwoNums(reader)

	P := readNNums(reader, n-1)

	cnt, res := solve(n, x, P)

	fmt.Println(cnt)
	fmt.Println(res)
}

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, x int, P []int) (int, string) {
	a := Pair{x, 'a'}
	b := Pair{n - x, 'b'}
	if a.first > b.first {
		a, b = b, a
	}

	g := NewGraph(n, n-1+10)

	for i := 0; i < len(P); i++ {
		g.AddEdge(P[i]-1, i+1)
	}
	sz := make([]int, n)
	dep := make([]int, n)
	var mx int
	var dfs func(u int, d int) int
	dfs = func(u int, d int) int {
		mx = max(mx, d)
		dep[u] = d
		sz[u]++
		for i := g.nodes[u]; i > 0; i = g.next[i] {
			sz[u] += dfs(g.to[i], d+1)
		}
		return sz[u]
	}

	dfs(0, 0)

	v := make([]*Pair, mx+1)
	for i := 0; i <= mx; i++ {
		v[i] = new(Pair)
		v[i].second = i
	}

	for i := 0; i < n; i++ {
		v[dep[i]].first++
	}

	cur := make([][]int, mx+1)
	for i := 0; i <= mx; i++ {
		cur[i] = make([]int, 0, v[i].first)
	}
	for i := 0; i < n; i++ {
		cur[dep[i]] = append(cur[dep[i]], i)
	}

	sort.Sort(Pairs(v))

	v2 := make([]*Pair, 0, len(v))

	for i := 0; i < len(v); i++ {
		if i == 0 || v[i].first != v[i-1].first {
			v2 = append(v2, &Pair{v[i].first, 1})
		} else {
			v2[len(v2)-1].second++
		}
	}

	dp := make([][]bool, len(v2)+1)
	for i := 0; i <= len(v2); i++ {
		dp[i] = make([]bool, n+1)
	}

	dp[len(v2)][0] = true
	last := make([]int, n+1)
	for i := len(v2) - 1; i >= 0; i-- {
		val := v2[i].first
		frq := v2[i].second
		for j := 0; j < val; j++ {
			last[j] = -1
		}
		for j := 0; j <= a.first; j++ {
			if dp[i+1][j] {
				last[j%val] = j
			}
			if last[j%val] < 0 || (j-last[j%val])/val > frq {
				dp[i][j] = false
			} else {
				dp[i][j] = true
			}
		}
	}

	F := make([]int, n+10)

	var buildPath func(i, j int)

	buildPath = func(i, j int) {
		if i == len(v2) {
			return
		}
		for !dp[i+1][j] {
			F[v2[i].first]++
			j -= v2[i].first
		}
		buildPath(i+1, j)
	}

	C := make([]byte, n)
	if dp[0][a.first] {
		buildPath(0, a.first)
		for i := 0; i < n; i++ {
			C[i] = byte(b.second)
		}
		for i := 0; i <= mx; i++ {
			if F[len(cur[i])] == 0 {
				continue
			}
			F[len(cur[i])]--
			for j := 0; j < len(cur[i]); j++ {
				C[cur[i][j]] = byte(a.second)
			}
		}
		mx++
	} else {
		for i := 0; i <= mx; i++ {
			sort.Sort(SortByTreeSize{cur[i], sz})
			if a.first < b.first {
				a, b = b, a
			}
			l := len(cur[i])
			for l > 0 && a.first > 0 {
				C[cur[i][l-1]] = byte(a.second)
				a.first--
				l--
			}
			for l > 0 && b.first > 0 {
				C[cur[i][l-1]] = byte(b.second)
				b.first--
				l--
			}
		}

		mx += 2
	}

	return mx, string(C)
}

type SortByTreeSize struct {
	nodes []int
	size  []int
}

func (this SortByTreeSize) Len() int {
	return len(this.nodes)
}

func (this SortByTreeSize) Less(i, j int) bool {
	a, b := this.nodes[i], this.nodes[j]
	return this.size[a] < this.size[b]
}

func (this SortByTreeSize) Swap(i, j int) {
	this.nodes[i], this.nodes[j] = this.nodes[j], this.nodes[i]
}

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

type Pair struct {
	first, second int
}

type Pairs []*Pair

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

func (this Pairs) Less(i, j int) bool {
	return this[i].first < this[j].first
}

func (this Pairs) Swap(i, j int) {
	this[i], this[j] = this[j], this[i]
}

type Graph struct {
	nodes []int
	next  []int
	to    []int
	cur   int
}

func NewGraph(n int, sz int) *Graph {
	nodes := make([]int, n)
	next := make([]int, sz)
	to := make([]int, sz)
	return &Graph{nodes, next, to, 0}
}

func (g *Graph) AddEdge(u, v int) {
	g.cur++
	g.next[g.cur] = g.nodes[u]
	g.nodes[u] = g.cur
	g.to[g.cur] = v
}


Polycarp has guessed three positive integers $$$a$$$, $$$b$$$ and $$$c$$$. He keeps these numbers in secret, but he writes down four numbers on a board in arbitrary order — their pairwise sums (three numbers) and sum of all three numbers (one number). So, there are four numbers on a board in random order: $$$a+b$$$, $$$a+c$$$, $$$b+c$$$ and $$$a+b+c$$$.You have to guess three numbers $$$a$$$, $$$b$$$ and $$$c$$$ using given numbers. Print three guessed integers in any order.Pay attention that some given numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (it is also possible that $$$a=b=c$$$).

The only line of the input contains four positive integers $$$x_1, x_2, x_3, x_4$$$ ($$$2 \le x_i \le 10^9$$$) — numbers written on a board in random order. It is guaranteed that the answer exists for the given number $$$x_1, x_2, x_3, x_4$$$.

Print such positive integers $$$a$$$, $$$b$$$ and $$$c$$$ that four numbers written on a board are values $$$a+b$$$, $$$a+c$$$, $$$b+c$$$ and $$$a+b+c$$$ written in some order. Print $$$a$$$, $$$b$$$ and $$$c$$$ in any order. If there are several answers, you can print any. It is guaranteed that the answer exists.

package main

import "fmt"

func max4(a, b, c, d int) int {
	if a > b && a > c && a > d {
		return a
	} else if b > a && b > c && b > d {
		return b
	} else if c > a && c > b && c > d {
		return c
	}
	return d
}

func printResult(a, b, c, r int) {
	fmt.Println(r-a, r-b, r-c)
}

func main() {
	var a, b, c, d int
	fmt.Scan(&a, &b, &c, &d)
	var r = max4(a, b, c, d)
	if r == a {
		printResult(b, c, d, r)
	} else if r == b {
		printResult(a, c, d, r)
	} else if r == c {
		printResult(a, b, d, r)
	} else {
		printResult(a, b, c, r)
	}
}

package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	var x [4]int
	var maxIndex int
	for i := 0; i < 4; i++ {
		sc.Scan()
		x[i], _ = strconv.Atoi(sc.Text())
		if x[i] > x[maxIndex] {
			maxIndex = i
		}
	}

	first := true
	for i, v := range x {
		if i == maxIndex {
			continue
		}
		if !first {
			fmt.Print(" ")
		}
		fmt.Print(x[maxIndex] - v)
		first = false
	}
	fmt.Println()
}

package main

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

func main(){
	in := bufio.NewReader(os.Stdin)
	var input [4]int
	fmt.Fscan(in,&input[0],&input[1],&input[2],&input[3])
	max,left := max4(input)
	//fmt.Println(max,left)
	for _,v := range left{
		fmt.Printf("%d ",max -v )
	}
}

func max4(input [4]int) (int,[]int){
	maxIdx := 0
	for i:=0;i<len(input);i++{
		if input[maxIdx]<input[i]{
			maxIdx = i
		}
	}
	//fmt.Println(maxIdx)
	max := input[maxIdx]
	return max,append(input[0:maxIdx],input[maxIdx+1:len(input)]...)
}

// A. Restoring Three Numbers
/*
Polycarp has guessed three positive integers 𝑎, 𝑏 and 𝑐. He keeps these
numbers in secret, but he writes down four numbers on a board in
arbitrary order — their pairwise sums (three numbers) and sum of all
three numbers (one number). So, there are four numbers on a board in
random order: 𝑎+𝑏, 𝑎+𝑐, 𝑏+𝑐 and 𝑎+𝑏+𝑐.

You have to guess three numbers 𝑎, 𝑏 and 𝑐 using given numbers.
Print three guessed integers in any order.

Pay attention that some given numbers 𝑎, 𝑏 and 𝑐 can be equal
(it is also possible that 𝑎=𝑏=𝑐).

Input

The only line of the input contains four positive integers 𝑥1,𝑥2,𝑥3,𝑥4
(2≤𝑥𝑖≤109) — numbers written on a board in random order.
It is guaranteed that the answer exists for the given number 𝑥1,𝑥2,𝑥3,𝑥4.

Output

Print such positive integers 𝑎, 𝑏 and 𝑐 that four numbers written on a board
are values 𝑎+𝑏, 𝑎+𝑐, 𝑏+𝑐 and 𝑎+𝑏+𝑐 written in some order. Print 𝑎, 𝑏 and 𝑐
in any order. If there are several answers, you can print any.
It is guaranteed that the answer exists.

Examples
Input
3 6 5 4

Output
2 1 3

Input
40 40 40 60

Output
20 20 20

Input
201 101 101 200

Output
1 100 100
*/
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	input, _, _ := reader.ReadLine()
	v := strings.Split(string(input), " ")
	x1, _ := strconv.Atoi(v[0])
	x2, _ := strconv.Atoi(v[1])
	x3, _ := strconv.Atoi(v[2])
	x4, _ := strconv.Atoi(v[3])
	// sol1154A(x1, x2, x3, x4)
	sol2_1154A(x1, x2, x3, x4)
}

func sol1154A(x1, x2, x3, x4 int) {
	if x1 > x2 && x1 > x3 && x1 > x4 {
		fmt.Print(x1-x2, x1-x3, x1-x4)
	} else if x2 > x1 && x2 > x3 && x2 > x4 {
		fmt.Print(x2-x1, x2-x3, x2-x4)
	} else if x3 > x1 && x3 > x2 && x3 > x4 {
		fmt.Print(x3-x1, x3-x2, x3-x4)
	} else if x4 > x1 && x4 > x2 && x4 > x3 {
		fmt.Print(x4-x1, x4-x2, x4-x3)
	}
}

func sol2_1154A(x1, x2, x3, x4 int) {
	arr := []int{x1, x2, x3, x4}
	sort.Ints(arr)
	fmt.Print(arr[3]-arr[0], arr[3]-arr[1], arr[3]-arr[2])
}

// A. Restoring Three Numbers
/*
Polycarp has guessed three positive integers 𝑎, 𝑏 and 𝑐. He keeps these
numbers in secret, but he writes down four numbers on a board in
arbitrary order — their pairwise sums (three numbers) and sum of all
three numbers (one number). So, there are four numbers on a board in
random order: 𝑎+𝑏, 𝑎+𝑐, 𝑏+𝑐 and 𝑎+𝑏+𝑐.

You have to guess three numbers 𝑎, 𝑏 and 𝑐 using given numbers.
Print three guessed integers in any order.

Pay attention that some given numbers 𝑎, 𝑏 and 𝑐 can be equal
(it is also possible that 𝑎=𝑏=𝑐).

Input

The only line of the input contains four positive integers 𝑥1,𝑥2,𝑥3,𝑥4
(2≤𝑥𝑖≤109) — numbers written on a board in random order.
It is guaranteed that the answer exists for the given number 𝑥1,𝑥2,𝑥3,𝑥4.

Output

Print such positive integers 𝑎, 𝑏 and 𝑐 that four numbers written on a board
are values 𝑎+𝑏, 𝑎+𝑐, 𝑏+𝑐 and 𝑎+𝑏+𝑐 written in some order. Print 𝑎, 𝑏 and 𝑐
in any order. If there are several answers, you can print any.
It is guaranteed that the answer exists.

Examples
Input
3 6 5 4

Output
2 1 3

Input
40 40 40 60

Output
20 20 20

Input
201 101 101 200

Output
1 100 100
*/
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	input, _, _ := reader.ReadLine()
	v := strings.Split(string(input), " ")
	x1, _ := strconv.Atoi(v[0])
	x2, _ := strconv.Atoi(v[1])
	x3, _ := strconv.Atoi(v[2])
	x4, _ := strconv.Atoi(v[3])
	sol1154A(x1, x2, x3, x4)
}

func sol1154A(x1, x2, x3, x4 int) {
	if x1 > x2 && x1 > x3 && x1 > x4 {
		fmt.Print(x1-x2, x1-x3, x1-x4)
	} else if x2 > x1 && x2 > x3 && x2 > x4 {
		fmt.Print(x2-x1, x2-x3, x2-x4)
	} else if x3 > x1 && x3 > x2 && x3 > x4 {
		fmt.Print(x3-x1, x3-x2, x3-x4)
	} else if x4 > x1 && x4 > x2 && x4 > x3 {
		fmt.Print(x4-x1, x4-x2, x4-x3)
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	x := make([]int, 4)
	fmt.Scan(&x[0], &x[1], &x[2], &x[3])
	sort.Ints(x)
	fmt.Println(x[3]-x[0], x[3]-x[1], x[3]-x[2])
}

package main

import(
    "fmt"
    "sort"
)

func main(){
    var(
       arr = make([]int,4)
    )
    fmt.Scan(&arr[0],&arr[1],&arr[2],&arr[3])
    sort.Ints(arr)
    fmt.Println(arr[3]-arr[2],arr[3]-arr[1],arr[3]-arr[0])
}

package main

import "fmt"

func insertionSort(array []int) []int {
	var key, j, n int
	n = len(array)
	for i := 1; i < n; i++ {
		key = array[i]
		j = i - 1
		for j >= 0 && array[j] > key {
			array[j + 1] = array[j]
			j--
		}
		array[j + 1] = key
	}
	return array
}

func main() {
	var x1, x2, x3, x4 int
	_, _ = fmt.Scan(&x1, &x2, &x3, &x4)
	var arr = []int{x1, x2, x3, x4}
	array := insertionSort(arr)
	for i := 0; i < 3; i++ {
		fmt.Print(array[3]- array[i], " ")
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	ch := make([]int, 4)
	fmt.Scan(&ch[0], &ch[1], &ch[2], &ch[3])
	sort.Ints(ch)
	fmt.Println(ch[3]-ch[0], ch[3]-ch[1], ch[3]-ch[2])
}

package main

import (
	"fmt"
)

func max(a, b, c, d int) int {
	if a >= b && a >= c && a >= d {
		return a
	}
	if b >= a && b >= c && b >= d {
		return b
	}
	if c >= a && c >= b && c >= d {
		return c
	}
	if d >= a && d >= b && d >= c {
		return d
	}
	return -1
}

func main() {
	var a, b, c, d int

	fmt.Scanf("%d %d %d %d\n", &a, &b, &c, &d)
	if a != max(a, b, c, d) {
		fmt.Print(max(a, b, c, d) - a)
		fmt.Print(" ")
	}
	if b != max(a, b, c, d) {
		fmt.Print(max(a, b, c, d) - b)
		fmt.Print(" ")
	}
	if c != max(a, b, c, d) {
		fmt.Print(max(a, b, c, d) - c)
		fmt.Print(" ")
	}
	if d != max(a, b, c, d) {
		fmt.Print(max(a, b, c, d) - d)
		fmt.Print(" ")
	}
}

package main

import ("fmt"
        "sort")

func main()  {
	input:=make([]int,4)
	var out [3]int

	for i := 0; i <4; i++ {
		fmt.Scan(&input[i])
	}
	sort.Ints(input)
	for i := 0; i < 3; i++ {
		out[i]=input[3]-input[i]
	}

	fmt.Print(out[0],out[1],out[2])
}

package main

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

func main() {
	buf := bufio.NewReader(os.Stdin)
	mat := make([]int, 4)
	for i := 0; i < 4; i++ {
		fmt.Fscanf(buf, "%d", &mat[i])
	}
	fmt.Fscanf(buf, "\n")
	sort.Ints(mat)
	x1 :=(mat[0]+mat[1]-mat[2])/2
	x2 := mat[0]-x1
	x3 := mat[1]-x1
	fmt.Printf("%d %d %d\n",x1,x2,x3)
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	arr := make([]int, 4, 4)
	for i := 0; i < 4; i++ {
		fmt.Scanf("%d", &arr[i])
	}
	sort.Ints(arr)
	for i := 0; i < 3; i++ {
		fmt.Printf("%d ", (arr[3] - arr[i]))
	}
}

package main

import "fmt"

func main() {
	var buf []int
	var n, maxn int
	for i:=0; i<4; i++ {
		fmt.Scan(&n)
		buf = append(buf, n)
	}
	for i:=0; i<4; i++ {
		if buf[i]>maxn {
			maxn = buf[i]
		}
	}
	for i:= 0; i<4; i++ {
		if buf[i] != maxn {
			fmt.Print(maxn-buf[i], " ")
		}
	}
}

package main

import (
	"fmt"
)

func main() {

	var a, b, c, d int
	fmt.Scanf("%d %d %d %d\n", &a, &b, &c, &d)

	if a > b && a > c && a > d {
		fmt.Print(a-b, a-c, a-d)
	} else if b > a && b > c && b > d {
		fmt.Print(b-a, b-c, b-d)
	} else if c > a && c > b && c > d {
		fmt.Print(c-a, c-b, c-d)
	} else if d > a && d > b && d > c {
		fmt.Print(d-a, d-b, d-c)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
func ReadStr() string{
	scanner.Scan()
	return scanner.Text()
}

func ReadInt(x int) (res int)  {
	scanner.Scan()

	n,err := strconv.Atoi(scanner.Text())
	res = n
	if err != nil{
		panic(err)
	}
	return
}

type building struct
{
	a , b ,c int
}

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

func main()  {
	//a + b
	//a + c
	//b + c
	//a + b + c

	var x1,x2,x3,x4 int
	fmt.Scan(&x1)
	fmt.Scan(&x2)
	fmt.Scan(&x3)
	fmt.Scan(&x4)
	m := max(x1,max(x2,max(x3,x4)))
	 a1,a2,a3,a4 := 0,0,0,0
	if(m != x1){
		a1 = m - x1
	}
	if(m != x2){
		a2 = m - x2
	}
	if(m != x3){
		a3 = m - x3
	}
	if(m != x4){
		a4 = m - x4
	}
	if(a1 != 0){
		fmt.Print(a1," ")
	}
	 if(a2 != 0){
	 	fmt.Print(a2," ")
	 }
	 if(a3 != 0){
	 	fmt.Print(a3," ")
	 }
	 if(a4 != 0){
	 	fmt.Print(a4," ")
	 }

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)
var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
func ReadStr() string{
	scanner.Scan()
	return scanner.Text()
}

func ReadInt(x int) (res int)  {
	scanner.Scan()

	n,err := strconv.Atoi(scanner.Text())
	res = n
	if err != nil{
		panic(err)
	}
	return
}

type building struct
{
	a , b ,c int
}

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

func main()  {
	//a + b
	//a + c
	//b + c
	//a + b + c

	a := make([]int,4,4)
	for i := 0 ; i < 4;i++{
			var x int
			fmt.Scan(&x)
			a[i] = x
	}
	sort.Ints(a)
	fmt.Print(a[3] - a[2]," ",a[3] - a[1]," ",a[3] - a[0])

}

package main

import "fmt"

func main(){
	var i, sum3 int
	x := make([]int, 4)
	for i = 0; i < 4; i++{
		fmt.Scan(&x[i])
		if x[i] > sum3{
			sum3 = x[i]
		}
	}
	for i = 3; i >= 0; i--{
		if x[i] != sum3{
			fmt.Print(sum3 - x[i], " ")
		}
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	a := make([]int, 4)

	_, _ = fmt.Scanf("%d %d %d %d", &a[0], &a[1], &a[2], &a[3])

	sort.Ints(a)

	fmt.Printf("%d ", a[3]-a[0])
	fmt.Printf("%d ", a[3]-a[1])
	fmt.Printf("%d ", a[3]-a[2])

}

package main

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

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1024*1024)
	line := readLine(reader)

	nums := strings.Split(line, " ")
	ints := make([]int, len(nums))
	max := 0
	for i, s := range nums {
		ints[i], _ = strconv.Atoi(s)
		if ints[i] > max {
			max = ints[i]
		}
	}
	var results []string
	for i := 0; i < len(ints); i++ {
		if max-ints[i] != 0 {
			results = append(results, strconv.Itoa(max-ints[i]))
		}
	}
	fmt.Println(strings.Join(results, " "))
}

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

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

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

package main

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

var reader = bufio.NewReader(os.Stdin)

func readInt() int {
	var ch byte
	var err error
	for {
		ch, err = reader.ReadByte()
		if err != nil {
			return 0
		}
		if unicode.IsDigit(rune(ch)) {
			break
		}
	}
	result := int(ch & 0xf)
	for {
		ch, err = reader.ReadByte()
		if err != nil || !unicode.IsDigit(rune(ch)) {
			return result
		}
		result = result*10 + int(ch&0xf)
	}
}

func main() {
	arr := make([]int, 4, 4)
	for i := 0; i < 4; i++ {
		arr[i] = readInt()
	}
	sort.Ints(arr)
	fmt.Printf("%d %d %d", arr[3]-arr[0], arr[3]-arr[1], arr[3]-arr[2])
}

package main

import (
	"fmt"
)

func main() {
	var mas [4]uint32

	for k := 0; k < len(mas); k++ {
		fmt.Scan(&mas[k])
	}
	//+сортировка пузырьком
	var i = 0
	var swapCnt = false

	for i < len(mas) {
		if i+1 != len(mas) && mas[i] > mas[i+1] {
			mas[i], mas[i+1] = mas[i+1], mas[i]
			swapCnt = true
		}
		i++
		if i == len(mas) && swapCnt == true {
			swapCnt = false
			i = 0
		}
	}
	//-сортировка пузырьком

	fmt.Println(mas[3]-mas[0], mas[3]-mas[1], mas[3]-mas[2])

}

package main

import (
	"fmt"
	"sort"
)

func main () {
	var arr []int
	var a int
	for i:=0; i<4; i++ {
		fmt.Scan(&a)
		arr = append(arr, a)
	}
	sort.Ints(arr)
	fmt.Println(arr[3]-arr[0], arr[3]-arr[1], arr[3]-arr[2])
}

package main

import (
	"fmt"
	"sort"
)

func main()  {
	var num [4]int

	for i := 0; i < 4; i++ {
		fmt.Scanf("%d", &num[i])
	}
	sort.Ints(num[:])

	a := num[3] - num[0]
	b := num[3] - num[1]
	c := num[3] - num[2]

	fmt.Printf("%d %d %d", a, b, c)
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var sum1, sum2, sum3, sum4 int
	fmt.Scan(&sum1, &sum2, &sum3, &sum4)
	var nums = []int{sum1, sum2, sum3, sum4}
	sort.Ints(nums)
	fmt.Println(nums[3]-nums[0], nums[3]-nums[1], nums[3]-nums[2])
}

package main

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

var reader *bufio.Reader

func init() {
	stdin := os.Stdin
	// stdin, _ = os.Open("1.in")
	reader = bufio.NewReaderSize(stdin, 1<<20)
}

var a [4]int

func main() {
	for i := 0; i < 4; i++ {
		fmt.Fscan(reader, &a[i])
	}
	sort.Sort(sort.IntSlice(a[:]))
	fmt.Println(a[3]-a[0], a[3]-a[1], a[3]-a[2])
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var q,w,e,r uint32
	fmt.Fscanf(in, "%d %d %d %d", &q, &w, &e, &r)
	if q > w && q > e && q > r{
		fmt.Println(q-w, q-e, q-r)
	}else if w > q && w > e && w > r{
		fmt.Println(w-q, w-e, w-r)
	}else if e > q && e > w && e > r{
		fmt.Println(e-q, e-w, e-r)
	}else if r > q && r > w && r > e{
		fmt.Println(r-q, r-w, r-e)
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var a, b, c, d int
	fmt.Scan(&a, &b, &c, &d)
	buf := []int{a, b, c, d}
	sort.Ints(buf)
	fmt.Print(buf[3]-buf[0], buf[3]-buf[1], buf[3]-buf[2])
}

package main

import (
	"fmt"
	"sort"
)

func main()  {
	var num [4]int

	for i := 0; i < 4; i++ {
		fmt.Scanf("%d", &num[i])
	}
	sort.Ints(num[:])
	tot := num[3]

	a := tot - num[0]
	b := tot - num[1]
	c := tot - num[2]
	fmt.Println(a, b, c)

}

package main

import ("fmt")

func max(x , y int) int {
	if(x>y){
		return x
	} else{
		return y
	}
}
func main(){
	var a ,b,c,d int
	fmt.Scan(&a)
	fmt.Scan(&b)
	fmt.Scan(&c)
	fmt.Scan(&d)

    var z int  = max(a , max(b , max(c , d)))

    if(z-a > 0){
    	fmt.Print(z-a)
    	fmt.Print(" ")
    }
    if(z-b > 0){
    	fmt.Print(z-b)
    	fmt.Print(" ")
    }
    if(z-c > 0){
    	fmt.Print(z-c)
    	fmt.Print(" ")
    }
    if(z-d > 0){
    	fmt.Print(z-d)
    	fmt.Print(" ")
    }
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	a := [4]int{}
	fmt.Fscanf(reader, "%d %d %d %d\n", &a[0], &a[1], &a[2], &a[3])
	for i := 1; i < 4; i++ {
		if a[i] > a[0] {
			a[0], a[i] = a[i], a[0]
		}
	}
	fmt.Fprintf(writer, "%d %d %d\n", a[0]-a[1], a[0]-a[2], a[0]-a[3])
}

//Sri VatapatraSayi's Grace

package main

import (
	"fmt"
	"sort"
)

func main() {

	var receive int

	slice := make([]int, 4)
	for i := 0; i < 4; i++ {
		fmt.Scan(&receive)
		slice[i] = receive
	}

	sort.Ints(slice)
	for i := 0; i < 3; i++ {
		fmt.Print(slice[3]-slice[i], " ")
	}
}

package main

import "fmt"

func main() {
	var m [4]int

	for i := 0; i < 4; i++ {
		fmt.Scan(&m[i])
		if m[i] > m[0] {
			tmp := m[0]
			m[0] = m[i]
			m[i] = tmp
		}
	}

	for i := 1; i < 4; i++ {
		fmt.Print(m[0] - m[i], " ")
	}
}

package main

import "fmt"

func main() {
	sum := make([]int, 4)
	tot := 0
	for i:=0; i<4; i++ {fmt.Scan(&sum[i]); tot += sum[i]}
	max, maxI := 0, 0
	for k, v := range sum {
		if v > max {max, maxI = v, k}
	}
	tot -= max
	c, abI := 0, 0
	for k, v := range sum {
		if k == maxI {continue}
		if (tot - v) % 2 == 0 {c, abI = (max - v), k; continue}
	}
	for k, v := range sum {
		if k == maxI || k == abI {continue}
		fmt.Print(v - c)
		fmt.Print(" ")
	}
	fmt.Println(c)
}

package main

import (
	"fmt"
	"sort"
)

//a+b, a+c, b+c, a+b+c
func solve(x ...int64) (int64, int64, int64) {
	var a, b, c int64
	/*
		b = x1 -a
		c = x2 - a
		a - 2a + x1 + x2 = x4
		a = -x4 +x1 + x2
	*/

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

	a = -x[3] + x[0] + x[1]
	b = x[0] - a
	c = x[1] - a

	return a, b, c
}

func main() {
	var x1, x2, x3, x4 int64

	fmt.Scan(&x1, &x2, &x3, &x4)

	a, b, c := solve(x1, x2, x3, x4)
	fmt.Println(a, b, c)
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	nums := make([]int, 4)
	for i := 0; i < 4; i++ {
		fmt.Scan(&nums[i])
	}
	sort.Ints(nums)
	max := nums[3]
	for i := 0; i < 3; i++ {
		fmt.Println(max - nums[i])
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var a, b, c, d int
	fmt.Scanf("%d %d %d %d", &a, &b, &c, &d)
	s := []int{a, b, c, d}
	sort.Ints(s)
	k := s[3] - s[0]
	l := s[3] - s[1]
	m := s[3] - s[2]
	fmt.Println(k, l, m)
}

package main

import "fmt"
import "sort"

func main() {
    arr := make([]int, 4, 4)
    for i:=0;i<4;i++ {
        fmt.Scanf("%d", &arr[i])
    }
    sort.Ints(arr)
    for i:=0;i<3;i++ {
        fmt.Printf("%d ", (arr[3]-arr[i]))
    }
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var nums [4]uint64
	fmt.Fscanf(in, "%d %d %d %d", &nums[0], &nums[1], &nums[2], &nums[3])
	result := restoringThreeNumbers(nums)
	fmt.Printf("%d %d %d", result[0], result[1], result[2])
}

func restoringThreeNumbers(nums [4]uint64) []uint64 {
	var count int
	mapOfNums := make(map[int]uint64)
	result := make([]uint64, 3, 3)

	sumOfAllNums := (nums[0] + nums[1] + nums[2] + nums[3]) / 3
	for i := 0; i < 4; i++ {
		if nums[i] != sumOfAllNums {
			mapOfNums[count] = nums[i]
			count++
		}
	}

	result[0] = sumOfAllNums - mapOfNums[0]
	result[1] = sumOfAllNums - mapOfNums[1]
	result[2] = sumOfAllNums - mapOfNums[2]

	return result
}

package main

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

func toIntArray(f string) []int {
	sp := strings.Split(f, " ")
	rs := make([]int, len(sp))
	for i, v := range sp {
		rs[i], _ = strconv.Atoi(v)
	}
	return rs
}

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

	ints := toIntArray(reader.Text())
	sort.Ints(ints)
	fmt.Println(ints[3]-ints[2], ints[3]-ints[1], ints[3]-ints[0])
}

package main

import (
	"fmt"
)

func max(nums []int) int {
	max := nums[0]

	var n int
	for _, n = range nums {
		if n > max {
			max = n
		}
	}

	return max
}

func main() {
	var nums []int
	for i := 0; i < 4; i++ {
		var n int

		fmt.Scanf("%d", &n)
		nums = append(nums, n)
	}

	m := max(nums)

	var nums1 []int

	for _, n := range nums {
		if n != m {
			nums1 = append(nums1, n)
		}
	}

	var ab, bc, ac int
	ab, bc, ac = nums1[0], nums1[1], nums1[2]

	c := m - ab
	a := m - bc
	b := m - ac

	if a+b+c == m {
		fmt.Println(a, b, c)
		return
	}

	bc, ac, ab = nums1[0], nums1[1], nums1[2]

	c = m - ab
	a = m - bc
	b = m - ac

	if a+b+c == m {
		fmt.Println(a, b, c)
		return
	}

	ac, ab, bc = nums1[0], nums1[1], nums1[2]

	c = m - ab
	a = m - bc
	b = m - ac

	if a+b+c == m {
		fmt.Println(a, b, c)
		return
	}
}

package main

import (
	"fmt"
)

func max(nums []int) (int, int) {
	max := nums[0]
	max_index := 0

	for i, n := range nums {
		if n > max {
			max = n
			max_index = i
		}
	}

	return max, max_index
}

func main() {
	var nums []int

	for i := 0; i < 4; i++ {
		var n int

		fmt.Scanf("%d", &n)
		nums = append(nums, n)
	}

	m, i := max(nums)
	nums = append(nums[:i], nums[i+1:]...)

	var ab, bc, ac *int
	var a, b, c int
	for i := 0; i < 3; i++ {
		ab = &nums[(0+i)%len(nums)]
		bc = &nums[(1+i)%len(nums)]
		ac = &nums[(2+i)%len(nums)]

		c = m - *ab
		a = m - *bc
		b = m - *ac

		if a+b+c == m {
			break
		}
	}
	fmt.Println(a, b, c)
}

package main

import "fmt"

// https://codeforces.com/problemset/problem/1154/A
// A. Restoring Three Numbers
// time limit per test
// 1 second
// memory limit per test
// 256 megabytes
// input
// standard input
// output
// standard output

// Polycarp has guessed three positive integers a
// , b and c. He keeps these numbers in secret, but he writes down four numbers on a board in arbitrary order — their pairwise sums (three numbers) and sum of all three numbers (one number). So, there are four numbers on a board in random order: a+b, a+c, b+c and a+b+c

// .

// You have to guess three numbers a
// , b and c

// using given numbers. Print three guessed integers in any order.

// Pay attention that some given numbers a
// , b and c can be equal (it is also possible that a=b=c

// ).
// Input

// The only line of the input contains four positive integers x1,x2,x3,x4
// (2≤xi≤109) — numbers written on a board in random order. It is guaranteed that the answer exists for the given number x1,x2,x3,x4

// .
// Output

// Print such positive integers a
// , b and c that four numbers written on a board are values a+b, a+c, b+c and a+b+c written in some order. Print a, b and c in any order. If there are several answers, you can print any. It is guaranteed that the answer exists.

func main() {
	var x1, x2, x3, x4 int
	_, _ = fmt.Scan(&x1, &x2, &x3, &x4)

	xMax := intMax(x1, x2, x3, x4)

	for _, x := range []int{x1, x2, x3, x4} {
		if x < xMax {
			_, _ = fmt.Print(xMax-x, " ")
		}
	}
}

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

package main

import (
	"fmt"
	"sort"
)

const alen = 4

// https://codeforces.com/problemset/problem/1154/A
// комментарии к решению
// Пусть s1 <= s2 <= s3 <= s4 и
// a + b = s1
// b + c = s2
// a + c = s3
// a + b + c = s4
// Тогда
// a = s4 - s2
// b = s4 - s3
// c = s4 - s1
func taskSolution(in []int) string {
	sort.Ints(in)
	s1, s2, s3, s4 := in[0], in[1], in[2], in[3]
	a, b, c := s4-s2, s4-s3, s4-s1
	return fmt.Sprint(a, b, c)
}

func main() {
	a := []int{}
	for i := 0; i < alen; i += 1 {
		var ai int
		if _, err := fmt.Scan(&ai); err != nil {
			panic(err)
		}
		a = append(a, ai)
	}

	fmt.Println(taskSolution(a))
}

// Author: sighduck
// URL: https://codeforces.com/contest/1154/problem/A

package main

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

type Solution struct {
	A int
	B int
	C int
}

func Solve(w, x, y, z int) Solution {
	a := []int{w, x, y, z}
	sort.Ints(a)
	return Solution{a[3] - a[0], a[3] - a[1], a[3] - a[2]}
}

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

	var w, x, y, z int
	fmt.Fscanf(reader, "%d %d %d %d\n", &w, &x, &y, &z)

	solution := Solve(w, x, y, z)
	fmt.Fprintf(writer, "%d %d %d\n", solution.A, solution.B, solution.C)
}

package main

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

func main(){
	in := bufio.NewReader(os.Stdin)
	var input [4]int
	fmt.Fscan(in,&input[0],&input[1],&input[2],&input[3])
	max,left := max4(input)
	for _,v := range left{
		fmt.Printf("%d ",max -v )
	}
}

func max4(input [4]int) (int,[]int){
	maxIdx := 0
	for i:=0;i<len(input);i++{
		if input[maxIdx]<input[i]{
			maxIdx = i
		}
	}
	return input[maxIdx],append(input[0:maxIdx],input[maxIdx+1:len(input)]...)
}

package main

import ("fmt"
        "sort")

func main()  {
	input:=make([]int,4)
	var out [3]int

	for i := 0; i <4; i++ {
		fmt.Scan(&input[i])
	}
	sort.Ints(input)
	for i := 0; i < 3; i++ {
		out[i]=input[3]-input[i]
	}

	fmt.Print(out)
}

package main

import "fmt"

func main() {
	var buf []int
	var a, b, c, n int
	for i:=0; i<4; i++ {
		fmt.Scan(&n)
		buf = append(buf, n)
	}
	c = buf[3]-buf[0]
	b = buf[2]-c
	a = buf[1]-c
	fmt.Println(a, b, c)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
func ReadStr() string{
	scanner.Scan()
	return scanner.Text()
}

func ReadInt(x int) (res int)  {
	scanner.Scan()

	n,err := strconv.Atoi(scanner.Text())
	res = n
	if err != nil{
		panic(err)
	}
	return
}

type building struct
{
	a , b ,c int
}

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

func main()  {
	//a + b
	//a + c
	//b + c
	//a + b + c

	var x1,x2,x3,x4 int
	fmt.Scan(&x1)
	fmt.Scan(&x2)
	fmt.Scan(&x3)
	fmt.Scan(&x4)
	c := x4 - x1
	a := x2 - c
	b := x1 - a
	fmt.Print(a,b,c)

}

package main

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

var reader *bufio.Reader

func readLine() string {
	r, _ := reader.ReadString('\n')
	return strings.Trim(r, "\n")
}

func solve(nums []int) {
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	for i := 1; i < 4; i++ {
		if nums[0] < nums[i] {
			nums[0], nums[i] = nums[i], nums[0]
		}
	}
	fmt.Fprintf(out, "%d %d %d", nums[0]-nums[1], nums[0]-nums[2], nums[0]-nums[3])
}

func main() {
	reader = bufio.NewReader(os.Stdin)
	line := readLine()
	numsStr := strings.Split(line, " ")
	nums := make([]int, len(numsStr))
	for i := 0; i < len(numsStr); i++ {
		nums[i], _ = strconv.Atoi(numsStr[i])
	}
	solve(nums)
}

package main

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

var reader *bufio.Reader

func readLine() string {
	r, _ := reader.ReadString('\n')
	return strings.Trim(r, "\n")
}

func solve(nums []int) {
	for i := 1; i < 4; i++ {
		if nums[0] < nums[i] {
			nums[0], nums[i] = nums[i], nums[0]
		}
	}
	fmt.Printf("%d %d %d", nums[0]-nums[1], nums[0]-nums[2], nums[0]-nums[3])
}

func main() {
	reader = bufio.NewReader(os.Stdin)
	line := readLine()
	numsStr := strings.Split(line, " ")
	nums := make([]int, len(numsStr))
	for i := 0; i < len(numsStr); i++ {
		nums[i], _ = strconv.Atoi(numsStr[i])
	}
	solve(nums)
}

package main

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

var reader *bufio.Reader

func readLine() string {
	r, _ := reader.ReadString('\n')
	return strings.Trim(r, "\n")
}

func solve(nums []int) {
	for i := 1; i < 4; i++ {
		if nums[0] < nums[i] {
			nums[0], nums[i] = nums[i], nums[0]
		}
	}
	fmt.Printf("%d %d %d\n", nums[0]-nums[1], nums[0]-nums[2], nums[0]-nums[3])
}

func main() {
	reader = bufio.NewReader(os.Stdin)
	line := readLine()
	numsStr := strings.Split(line, " ")
	nums := make([]int, len(numsStr))
	for i := 0; i < len(numsStr); i++ {
		nums[i], _ = strconv.Atoi(numsStr[i])
	}
	solve(nums)
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var nums [4]int
	fmt.Fscanf(in, "%d %d %d %d", &nums[0], &nums[1], &nums[2], &nums[3])
	fmt.Println(restoringThreeNumbers(nums))
}

func restoringThreeNumbers(nums [4]int) (int, int, int) {
	var count int
	mapOfNums := make(map[int]int)

	sumOfAllNums := (nums[0] + nums[1] + nums[2] + nums[3]) / 3

	for i := 0; i < 4; i++ {
		if nums[i] != sumOfAllNums {
			mapOfNums[count] = nums[i]
			count++
		}
	}

	num1 := sumOfAllNums - mapOfNums[0]
	num2 := mapOfNums[1] - num1
	num3 := mapOfNums[2] - num2

	return num1, num2, num3
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var nums [4]int
	fmt.Fscanf(in, "%d %d %d %d", &nums[0], &nums[1], &nums[2], &nums[3])
	result := restoringThreeNumbers(nums)
	fmt.Printf("%d %d %d", result[0], result[1], result[2])
}

func restoringThreeNumbers(nums [4]int) []int {
	var count int
	mapOfNums := make(map[int]int)
	result := make([]int, 3, 3)

	sumOfAllNums := (nums[0] + nums[1] + nums[2] + nums[3]) / 3

	for i := 0; i < 4; i++ {
		if nums[i] != sumOfAllNums {
			mapOfNums[count] = nums[i]
			count++
		}
	}

	result[0] = sumOfAllNums - mapOfNums[0]
	result[1] = sumOfAllNums - mapOfNums[1]
	result[2] = sumOfAllNums - mapOfNums[2]

	return result
}

package main

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

func readLineOfInts(reader *bufio.Reader) []int {
	line, _ := reader.ReadString('\n')
	line = strings.Trim(line, "\n")
	splits := strings.Split(line, " ")
	ints := make([]int, len(splits))
	for i, v := range splits {
		i64, _ := strconv.ParseInt(v, 10, 32)
		ints[i] = int(i64)
	}
	return ints
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	ints := readLineOfInts(reader)
	sort.Ints(ints)
	fmt.Println(ints[3]-ints[2], ints[3]-ints[1], ints[3]-ints[0])
}

package main

import (
	"fmt"
)

func max(nums []int) (int, int) {
	max := nums[0]
	max_index := 0

	for i, n := range nums {
		if n > max {
			max = n
			max_index = i
		}
	}

	return max, max_index
}

func main() {
	var nums []int

	for i := 0; i < 4; i++ {
		var n int

		fmt.Scanf("%d", &n)
		nums = append(nums, n)
	}

	m, i := max(nums)
	nums = append(nums[:i], nums[i:]...)

	var ab, bc, ac *int
	var a, b, c int
	for i := 0; i < 3; i++ {
		ab = &nums[(0+i)%len(nums)]
		bc = &nums[(1+i)%len(nums)]
		ac = &nums[(2+i)%len(nums)]

		c = m - *ab
		a = m - *bc
		b = m - *ac

		if a+b+c == m {
			break
		}
	}
	fmt.Println(a, b, c)
}


You are given a binary array $$$a$$$ (all elements of the array are $$$0$$$ or $$$1$$$) of length $$$n$$$. You wish to sort this array, but unfortunately, your algorithms teacher forgot to teach you sorting algorithms. You perform the following operations until $$$a$$$ is sorted:  Choose two random indices $$$i$$$ and $$$j$$$ such that $$$i < j$$$. Indices are chosen equally probable among all pairs of indices $$$(i, j)$$$ such that $$$1 \le i < j \le n$$$.  If $$$a_i > a_j$$$, then swap elements $$$a_i$$$ and $$$a_j$$$. What is the expected number of such operations you will perform before the array becomes sorted?It can be shown that the answer can be expressed as an irreducible fraction $$$\frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q \not \equiv 0 \pmod{998\,244\,353}$$$. Output the integer equal to $$$p \cdot q^{-1} \bmod 998\,244\,353$$$. In other words, output such an integer $$$x$$$ that $$$0 \le x < 998\,244\,353$$$ and $$$x \cdot q \equiv p \pmod{998\,244\,353}$$$.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 200\,000$$$) — the number of elements in the binary array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i \in \{0, 1\}$$$) — elements of the array. It's guaranteed that sum of $$$n$$$ over all test cases does not exceed $$$200\,000$$$.

For each test case print one integer — the value $$$p \cdot q^{-1} \bmod 998\,244\,353$$$.

NoteConsider the first test case. If the pair of indices $$$(2, 3)$$$ will be chosen, these elements will be swapped and array will become sorted. Otherwise, if one of pairs $$$(1, 2)$$$ or $$$(1, 3)$$$ will be selected, nothing will happen. So, the probability that the array will become sorted after one operation is $$$\frac{1}{3}$$$, the probability that the array will become sorted after two operations is $$$\frac{2}{3} \cdot \frac{1}{3}$$$, the probability that the array will become sorted after three operations is $$$\frac{2}{3} \cdot \frac{2}{3} \cdot \frac{1}{3}$$$ and so on. The expected number of operations is $$$\sum \limits_{i=1}^{\infty} \left(\frac{2}{3} \right)^{i - 1} \cdot \frac{1}{3} \cdot i = 3$$$.In the second test case the array is already sorted so the expected number of operations is zero.In the third test case the expected number of operations equals to $$$\frac{75}{4}$$$ so the answer is $$$75 \cdot 4^{-1} \equiv 249\,561\,107 \pmod {998\,244\,353}$$$.

package main

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

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

const MOD int64 = 998244353

var T, N int64
var A []int64

func mpow(a, n int64) int64 {
	if n == 0 {
		return 1
	}
	r := mpow(a, n/2)
	r = (r * r) % MOD
	if n%2 == 1 {
		r *= a
	}
	return r % MOD
}

func reverse(a int64) int64 {
	return mpow(a, MOD-2)
}

func solve() int64 {
	n0, n1 := int64(0), int64(0)
	for i := 0; int64(i) < N; i++ {
		if A[i] == 0 {
			n0 += 1
		} else {
			n1 += 1
		}
	}
	dp := make([]int64, n0+1)

	dp[n0] = 0
	for i := n0 - 1; i >= 0; i-- {
		var p int64 = (((2 * (((n0 - i) * (n0 - i)) % MOD)) % MOD) * reverse((N*(N-1))%MOD)) % MOD
		dp[i] = (((1 + (p*dp[i+1])%MOD) % MOD) * reverse(p)) % MOD
	}

	c := 0
	for i := 0; int64(i) < n0; i++ {
		if A[i] == 0 {
			c += 1
		}
	}

	return dp[c] % MOD
}

func main() {
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &T)
	A = make([]int64, 200005)
	for tt := int64(1); tt <= T; tt += 1 {
		fmt.Fscan(in, &N)
		for i := 0; int64(i) < N; i++ {
			fmt.Fscan(in, &A[i])
		}

		fmt.Println(solve())
	}
}

package main

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

func modinv(b, m int) int {
	return modexp(b, m - 2, m)
}

func modexp(b, p, m int) int {
	if b == 0 { return 0 }
	if p == 0 { return 1 }
	if p == 1 { return b % m }
	res := modexp(b, p>>1, m)
	res = (res * res) % m
	if p % 2 == 1 { res = (b * res) % m }
	return res
}

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

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

		var n, k, zk int
		fmt.Fscan(r, &n)

		a := make([]int, n)
		k = n
		for i := range a {
			fmt.Fscan(r, &a[i])
			k -= a[i]
		}

		for i := 0; i < k; i++ {
			zk += a[i]
		}

		res := 0
		for i := 1; i <= zk; i++ {
			res += ((n*n)%mod - n) * modinv(2*i*i, mod)
			res %= mod
		}
		fmt.Fprintln(w, res)
	}

}

package main

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

func modinv(b, m int) int {
	return modexp(b, m - 2, m)
}

func modexp(b, p, m int) int {
	if b == 0 { return 0 }
	if p == 0 { return 1 }
	if p == 1 { return b % m }
	res := modexp(b, p>>1, m)
	res = (res * res) % m
	if p % 2 == 1 { res = (b * res) % m }
	return res
}

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

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

		var n, k, zk int
		fmt.Fscan(r, &n)

		a := make([]int, n)
		k = n
		for i := range a {
			fmt.Fscan(r, &a[i])
			k -= a[i]
		}

		for i := 0; i < k; i++ {
			zk += a[i]
		}

		res := 0
		for i := 1; i <= zk; i++ {
			res += modinv(2*i*i, mod)
			res %= mod
		}
		res = ( res * ((n*(n-1))%mod) ) % mod
		fmt.Fprintln(w, res)
	}

}

package main

import "fmt"

const mod = 998244353

func qpow(a, b int64) int64 {
	var ans int64
	ans = 1
	for b > 0 {
		if b%2 == 1 {
			ans = ans * a % mod
		}
		a = a * a % mod
		b >>= 1
	}
	return ans
}

func cal(x int64) int64 {
	return qpow(x*x%mod, mod-2)
}

func solve() {
	var n, k, sum, i int64
	var arr []int64
	fmt.Scan(&n)
	for i = 0; i < n; i++ {
		var tmp int64
		fmt.Scan(&tmp)
		arr = append(arr, tmp)
		sum += tmp
	}
	for i = 0; i < n-sum; i++ {
		k += arr[i]
	}

	var ans int64
	for i = 0; i <= k; i++ {
		ans = (ans + cal(i)) % mod
	}
	ans = (((ans * n) % mod) * (n - 1)) % mod
	ans = (ans * qpow(2, mod-2)) % mod
	fmt.Println(ans)
}

func main() {
	tt := 0
	fmt.Scan(&tt)
	for i := 0; i < tt; i++ {
		solve()
	}
}

package main

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

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

var MOD int64 = 998244353

var T, N int64
var A []int64

func mpow(a, n int64) int64 {
	if n == 0 {
		return 1
	}
	r := mpow(a, n/2)
	r = (r * r) % MOD
	if n%2 == 1 {
		r *= a
	}
	return r % MOD
}

func reverse(a int64) int64 {
	return mpow(a, MOD-2)
}

func solve() int64 {
	n0, n1 := int64(0), int64(0)
	for i := 0; int64(i) < N; i++ {
		if A[i] == 0 {
			n0 += 1
		} else {
			n1 += 1
		}
	}
	dp := make([]int64, n0+1)

	dp[n0] = 0
	for i := n0 - 1; i >= 0; i-- {
		p := (((2 * ((n0 - i) * (n0 - i) % MOD)) % MOD) * reverse(N*(N-1))) % MOD
		dp[i] = ((1 + (p*dp[i+1])%MOD) % MOD * reverse(p)) % MOD
	}

	c := 0
	for i := 0; int64(i) < n0; i++ {
		if A[i] == 0 {
			c += 1
		}
	}

	return dp[c] % MOD
}

func main() {
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &T)
	A = make([]int64, 200005)
	for tt := int64(1); tt <= T; tt += 1 {
		fmt.Fscan(in, &N)
		for i := 0; int64(i) < N; i++ {
			fmt.Fscan(in, &A[i])
		}

		fmt.Println(solve())

	}
}

package main

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

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

var MOD int64 = 998244353

var T, N int64
var A []int64

func mpow(a, n int64) int64 {
	if n == 0 {
		return 1
	}
	r := mpow(a, n/2)
	r = (r * r) % MOD
	if n%2 == 1 {
		r *= a
	}
	return r % MOD
}

func reverse(a int64) int64 {
	return mpow(a, MOD-2)
}

func solve() int64 {
	n0, n1 := int64(0), int64(0)
	for i := 0; int64(i) < N; i++ {
		if A[i] == 0 {
			n0 += 1
		} else {
			n1 += 1
		}
	}
	dp := make([]int64, n0+1)

	dp[n0] = 0
	for i := n0 - 1; i >= 0; i-- {
		p := (((2 * ((n0 - i) * (n0 - i) % MOD)) % MOD) * reverse(N*(N-1))) % MOD
		dp[i] = ((1 + (p*dp[i+1])%MOD) % MOD * reverse(p)) % MOD
	}

	c := 0
	for i := 0; int64(i) < n0; i++ {
		if A[i] == 0 {
			c += 1
		}
	}

	fmt.Fprintln(os.Stderr, c)

	return dp[c]

}

func main() {
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &T)
	A = make([]int64, 200005)
	for tt := int64(1); tt <= T; tt += 1 {
		fmt.Fscan(in, &N)
		for i := 0; int64(i) < N; i++ {
			fmt.Fscan(in, &A[i])
		}

		fmt.Println(solve())

	}
}

package main

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

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

var MOD int64 = 998244353

var T, N int64
var A []int64

func mpow(a, n int64) int64 {
	if n == 0 {
		return 1
	}
	r := mpow(a, n/2)
	r = (r * r) % MOD
	if n%2 == 1 {
		r *= a
	}
	return r % MOD
}

func reverse(a int64) int64 {
	return mpow(a, MOD-2)
}

func solve() int64 {
	n0, n1 := int64(0), int64(0)
	for i := 0; int64(i) < N; i++ {
		if A[i] == 0 {
			n0 += 1
		} else {
			n1 += 1
		}
	}
	dp := make([]int64, n0+1)

	dp[n0] = 0
	for i := n0 - 1; i >= 0; i-- {

		p := (((2 * ((n0 - i) * (n0 - i) % MOD)) % MOD) * reverse(N*(N-1))) % MOD
		dp[i] = ((1 + (p*dp[i+1])%MOD) % MOD * reverse(p)) % MOD
	}

	c := 0
	for i := 0; int64(i) < n0; i++ {
		if A[i] == 0 {
			c += 1
		}
	}

	return dp[c]

}

func main() {
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &T)
	A = make([]int64, 200005)
	for tt := int64(1); tt <= T; tt += 1 {
		fmt.Fscan(in, &N)
		for i := 0; int64(i) < N; i++ {
			fmt.Fscan(in, &A[i])
		}

		fmt.Println(solve())

	}
}

package main

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

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

var MOD int64 = 998244353

var T, N int64
var A []int64

func mpow(a, n int64) int64 {
	if n == 0 {
		return 1
	}
	r := mpow(a, n/2)
	r = r * r % MOD
	if n%2 == 1 {
		r *= a
	}
	return r % MOD
}

func reverse(a int64) int64 {
	return mpow(a, MOD-2)
}

func solve() int64 {
	n0, n1 := int64(0), int64(0)
	for i := 0; int64(i) < N; i++ {
		if A[i] == 0 {
			n0 += 1
		} else {
			n1 += 1
		}
	}
	dp := make([]int64, n0+1)

	dp[n0] = 0
	for i := n0 - 1; i >= 0; i-- {
		p := (2 * (n0 - i) * (n0 - i) % MOD) * reverse(N*(N-1)) % MOD
		dp[i] = (1 + p*dp[i+1]) % MOD * reverse(p) % MOD
	}

	c := 0
	for i := 0; int64(i) < n0; i++ {
		if A[i] == 0 {
			c += 1
		}
	}

	return dp[c]

}

func main() {
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &T)
	A = make([]int64, 200005)
	for tt := int64(1); tt <= T; tt += 1 {
		fmt.Fscan(in, &N)
		for i := 0; int64(i) < N; i++ {
			fmt.Fscan(in, &A[i])
		}

		fmt.Println(solve())

	}
}

package main

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

func modinv(b, m int) int {
	return modexp(b, m - 2, m)
}

func modexp(b, p, m int) int {
	if b == 0 { return 0 }
	if p == 0 { return 1 }
	if p == 1 { return b % m }
	res := modexp(b, p>>1, m)
	res = (res * res) % m
	if p % 2 == 1 { res = (b * res) % m }
	return res
}

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

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

		var n, k, zk int
		fmt.Fscan(r, &n)

		a := make([]int, n)
		k = n
		for i := range a {
			fmt.Fscan(r, &a[i])
			k -= a[i]
		}

		for i := 0; i < k; i++ {
			zk += a[i]
		}

		res := 0
		for i := 1; i <= zk; i++ {
			res += modinv(2*i*i, mod)
			res %= mod
		}
		res = (res*n*(n-1)) % mod
		fmt.Fprintln(w, res)
	}

}

package main

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

type frac struct {
	num int
	denom int
}

func sum (f1, f2 frac) frac {
	num := f1.num * f2.denom + f2.num * f1.denom
	denom := f2.denom * f1.denom
	gcd_ := gcd(num, denom)
	return frac{num/gcd_, denom/gcd_}
}

func gcd(a, b int) int {
	if b == 0 { return a }
	return gcd(b, a%b)
}

func modinv(b, m int) int {
	return modexp(b, m - 2, m)
}

func modexp(b, p, m int) int {
	if b == 0 { return 0 }
	if p == 0 { return 1 }
	if p == 1 { return b }
	res := modexp(b, p>>1, m)
	res = (res * res) % m
	if p % 2 == 1 { res = (b * res) % m }
	return res
}

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

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

		var n, k, zk int
		fmt.Fscan(r, &n)

		a := make([]int, n)
		k = n
		for i := range a {
			fmt.Fscan(r, &a[i])
			k -= a[i]
		}

		for i := 0; i < k; i++ {
			zk += a[i]
		}

		res := frac{0, 1}
		for i := 1; i <= zk; i++ {
			res = sum(res, frac{n*(n-1), 2*i*i})
		}

		ans := (res.num * modinv(res.denom, mod)) % mod
		fmt.Fprintln(w, ans)
	}

}


In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i < n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.

The first line contains integer n (1 ≤ n ≤ 103) — the boulevard's length in tiles. The second line contains n space-separated integers ai — the number of days after which the i-th tile gets destroyed (1 ≤ ai ≤ 103).

Print a single number — the sought number of days.

NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.

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)
  rain := map[int][]int{}
  for pos := 0; pos < n; pos++ {
    day := scanInt(scanner)
    tiles, found := rain[day]
    if !found {
      rain[day] = []int{ pos }
    } else {
      rain[day] = append(tiles, pos)
    }
    tiles = rain[day]
  }
  wreck := make([]bool, n)
  day, done := 0, false
  for !done {
    day++
    tiles, found := rain[day]
    if !found {
      continue
    }
    for _, pos := range tiles {
      if pos == 0 || pos == n-1 || wreck[pos-1] || wreck[pos+1] {
        done = true
        break
      }
      wreck[pos] = true
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", day))
}

//192B
package main
import (
	"fmt"
	"os"
	"bufio"
	"strconv"
)

func main() {
	var nn, k, l int
	var x[1025]int
	var aa[1025]int
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	r.Scan()
	nn, _ = strconv.Atoi(r.Text())
	for i:=0; i<nn; i++ {
		r.Scan()
		aa[i], _ = strconv.Atoi(r.Text())
	}
	x[0]=aa[0]
	if aa[0]<aa[1] {
		x[1]=aa[0]
	} else {
		x[1]=aa[1]
	}
	for i:=2; i<nn; i++ {
		if x[i-1]<aa[i] {
			k=x[i-1]
		} else {
			k=aa[i]
		}
		if x[i-2]<aa[i] {
			l=x[i-2]
		} else {
			l=aa[i]
		}
		if k>l {
			x[i]=k
		} else {
			x[i]=l
		}
	}
	fmt.Println(x[nn-1])
}

// 192B
package main

import (
	"fmt"
)

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

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

//192B
package main
import "fmt"

func main() {
	var nn, k, l int
	var x[1025]int
	var aa[1025]int
	fmt.Scan(&nn)
	for i:=0; i<nn; i++ {
		fmt.Scanf("%d", &aa[i])
	}
	x[0]=aa[0]
	if aa[0]<aa[1] {
		x[1]=aa[0]
	} else {
		x[1]=aa[1]
	}
	for i:=2; i<nn; i++ {
		if x[i-1]<aa[i] {
			k=x[i-1]
		} else {
			k=aa[i]
		}
		if x[i-2]<aa[i] {
			l=x[i-2]
		} else {
			l=aa[i]
		}
		if k>l {
			x[i]=k
		} else {
			x[i]=l
		}
	}
	fmt.Println(x[nn-1])
}

// 192B
package main

import (
	"fmt"
)

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

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


New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ i ≤ n) book is wi.As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.  He lifts all the books above book x.  He pushes book x out of the stack.  He puts down the lifted books without changing their order.  After reading book x, he puts book x on the top of the stack.  He decided to read books for m days. In the j-th (1 ≤ j ≤ m) day, he will read the book that is numbered with integer bj (1 ≤ bj ≤ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?

The first line contains two space-separated integers n (2 ≤ n ≤ 500) and m (1 ≤ m ≤ 1000) — the number of books, and the number of days for which Jaehyun would read books. The second line contains n space-separated integers w1, w2, ..., wn (1 ≤ wi ≤ 100) — the weight of each book. The third line contains m space separated integers b1, b2, ..., bm (1 ≤ bj ≤ n) — the order of books that he would read. Note that he can read the same book more than once.

Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.

NoteHere's a picture depicting the example. Each vertical column presents the stacked books.

package main

import (
	"fmt"
)

func main() {
	var n, m int

	fmt.Scanf("%d %d", &n, &m)

	ws := make([]int, n)
	rs := make([]int, m)
	for i := range ws {
		fmt.Scan(&ws[i])
	}
	for i := range rs {
		fmt.Scan(&rs[i])
	}

	fmt.Println(solve(ws, rs))
}

type kv struct {
	k int
	v int
}

func solve(ws []int, rs []int) int {
	count := make(map[int]int)
	state := make([]int, len(ws))

	s := 0
	for _, v := range rs {
		if _, ok := count[v]; ok == false {
			count[v]++
			state[s] = v
			s++
		}
	}

	// fmt.Println(state)

	sum := 0
	for i := range rs {
		for j, v := range state {
			if v == rs[i] {
				newState := make([]int, len(ws))
				if j > 0 {
					newState[0] = v

					copy(newState[1:j+1], state[0:j])
					copy(newState[j+1:], state[j+1:])

					state = newState
					// fmt.Println(state)
				}

				break
			} else {
				sum += ws[v-1]
				// fmt.Printf("lifted: %d\n", v)
			}
		}
	}
	return sum
}

package main

import (
	"fmt"
)

var (
	n      int
	m      int
	w      []int
	b      []int
	result int
)

func in() {
	fmt.Scan(&n, &m)
	w = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&w[i])
	}
	b = make([]int, m)
	for i := 0; i < m; i++ {
		fmt.Scan(&b[i])
		b[i]--
	}
}

func out() {
	fmt.Println(result)
}

func run() {
	stack := make([]int, n)
	p := 0
	preproc := make([]bool, n)
	for _, v := range b {
		if !preproc[v] {
			stack[p] = v
			p++
			preproc[v] = true
		}
	}

	result = 0
	for _, book := range b {
		weight := 0
		temp := stack[0]
		i := 0
		for ; stack[i] != book; i++ {
			weight += w[stack[i]]
			stack[i], temp = temp, stack[i]
		}
		stack[0], stack[i] = stack[i], temp
		result += weight
	}
}

func main() {
	in()
	run()
	out()
}

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
)

var reader = bufio.NewReader(os.Stdin)

func main() {
	var n, m int

	fmt.Fscan(reader, &n, &m)

	w := make([]int, n)
	b := make([]int, m)

	d := make([]int, n)
	rs := list.New()

	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &w[i])
	}

	for i := 0; i < m; i++ {
		var j int
		fmt.Fscan(reader, &j)
		b[i] = j - 1
	}

	for i := 0; i < m; i++ {
		if d[b[i]] == 0 {
			rs.PushBack(b[i])
		}
		d[b[i]] = 1
	}

	for i := 0; i < n; i++ {
		if d[i] == 0 {
			rs.PushBack(i)
		}
		d[i] = 1
	}

	var res int

	for i := 0; i < m; i++ {
		e := new(list.Element)
		for e = rs.Front(); e != nil; e = e.Next() {
			if e.Value == b[i] {
				break
			}
			j, _ := e.Value.(int)
			res += w[j]
		}
		rs.Remove(e)
		rs.PushFront(e.Value)
	}

	fmt.Println(res)
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, m int

	fmt.Scanf("%d %d", &n, &m)

	ws := make([]int, n)
	rs := make([]int, m)
	for i := range ws {
		fmt.Scan(&ws[i])
	}
	for i := range rs {
		fmt.Scan(&rs[i])
	}

	fmt.Println(solve(ws, rs))
}

type kv struct {
	k int
	v int
}

type KvList []kv

func (kvl KvList) Swap(i, j int) { kvl[i], kvl[j] = kvl[j], kvl[i] }
func (kvl KvList) Len() int      { return len(kvl) }
func (kvl KvList) Less(i, j int) bool {
	if kvl[i].v == kvl[j].v {
		return kvl[i].k < kvl[j].k
	}
	return kvl[i].v > kvl[j].v
}

func solve(ws []int, rs []int) int {
	count := make(map[int]int)

	for _, v := range rs {
		count[v]++
	}

	var ss KvList
	for k, v := range count {
		ss = append(ss, kv{k, v})
	}

	sort.Sort(ss)

	state := make([]int, len(ws))

	for i := range state {
		state[i] = ss[i].k
	}

	// fmt.Println(state)

	sum := 0
	for i := range rs {
		for j, v := range state {
			if v == rs[i] {
				newState := make([]int, len(ws))
				if j > 0 {
					newState[0] = v

					copy(newState[1:j+1], state[0:j])
					copy(newState[j+1:], state[j+1:])

					state = newState
					// fmt.Println(state)
				}

				break
			} else {
				sum += ws[v-1]
				// fmt.Printf("lifted: %d\n", v)
			}
		}
	}
	return sum
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, m int

	fmt.Scanf("%d %d", &n, &m)

	ws := make([]int, n)
	rs := make([]int, m)
	for i := range ws {
		fmt.Scan(&ws[i])
	}
	for i := range rs {
		fmt.Scan(&rs[i])
	}

	fmt.Println(solve(ws, rs))
}

type kv struct {
	k int
	v int
}

type KvList []kv

func (kvl KvList) Swap(i, j int) { kvl[i], kvl[j] = kvl[j], kvl[i] }
func (kvl KvList) Len() int      { return len(kvl) }
func (kvl KvList) Less(i, j int) bool {
	if kvl[i].v == kvl[j].v {
		return kvl[i].k < kvl[j].k
	}
	return kvl[i].v > kvl[j].v
}

func solve(ws []int, rs []int) int {
	count := make(map[int]int)

	for _, v := range rs {
		count[v]++
	}

	var ss KvList
	for k, v := range count {
		ss = append(ss, kv{k, v})
	}

	fmt.Println(ss)
	sort.Sort(ss)
	fmt.Println(ss)

	state := make([]int, len(ws))

	for i := range state {
		state[i] = ss[i].k
	}

	fmt.Println(state)

	sum := 0
	for i := range rs {
		for j, v := range state {
			if v == rs[i] {
				newState := make([]int, len(ws))
				if j > 0 {
					newState[0] = v

					copy(newState[1:j+1], state[0:j])
					copy(newState[j+1:], state[j+1:])

					state = newState
					fmt.Println(state)
				}

				break
			} else {
				sum += v
				fmt.Printf("lifted: %d\n", v)
			}
		}
	}
	return sum
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, m int

	fmt.Scanf("%d %d", &n, &m)

	ws := make([]int, n)
	rs := make([]int, m)
	for i := range ws {
		fmt.Scan(&ws[i])
	}
	for i := range rs {
		fmt.Scan(&rs[i])
	}

	fmt.Println(solve(ws, rs))
}

type kv struct {
	k int
	v int
}

type KvList []kv

func (kvl KvList) Swap(i, j int) { kvl[i], kvl[j] = kvl[j], kvl[i] }
func (kvl KvList) Len() int      { return len(kvl) }
func (kvl KvList) Less(i, j int) bool {
	if kvl[i].v == kvl[j].v {
		return kvl[i].k < kvl[j].k
	}
	return kvl[i].v > kvl[j].v
}

func solve(ws []int, rs []int) int {
	count := make(map[int]int)

	for _, v := range rs {
		count[v]++
	}

	var ss KvList
	for k, v := range count {
		ss = append(ss, kv{k, v})
	}

	sort.Sort(ss)

	state := make([]int, len(ws))

	for i := range state {
		state[i] = ss[i].k
	}

	// fmt.Println(state)

	sum := 0
	for i := range rs {
		for j, v := range state {
			if v == rs[i] {
				newState := make([]int, len(ws))
				if j > 0 {
					newState[0] = v

					copy(newState[1:j+1], state[0:j])
					copy(newState[j+1:], state[j+1:])

					state = newState
					// fmt.Println(state)
				}

				break
			} else {
				sum += v
				// fmt.Printf("lifted: %d\n", v)
			}
		}
	}
	return sum
}

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
)

var reader = bufio.NewReader(os.Stdin)

func main() {
	var n, m int

	fmt.Fscan(reader, &n, &m)

	w := make([]int, n)
	b := make([]int, m)

	d := make([]int, n)
	rs := list.New()

	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &w[i])
	}

	for i := 0; i < m; i++ {
		var j int
		fmt.Fscan(reader, &j)
		b[i] = j - 1
	}

	for i := 0; i < m; i++ {
		if d[b[i]] == 0 {
			rs.PushBack(b[i])
		}
		d[b[i]] = 1
	}

	for i := 0; i < n; i++ {
		if d[i] == 0 {
			rs.PushBack(i)
		}
		d[i] = 1
	}

	var res int

	for i := 0; i < m; i++ {
		e := new(list.Element)
		for e = rs.Front(); e != nil; e = e.Next() {
			if e.Value == b[i] {
				break
			}
			res += w[b[i]]
		}
		rs.Remove(e)
		rs.PushFront(e.Value)
	}

	fmt.Println(res)
}

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
)

var reader = bufio.NewReader(os.Stdin)

func main() {
	var n, m int

	fmt.Fscan(reader, &n, &m)

	w := make([]int, n)
	b := make([]int, m)

	d := make([]int, n)
	rs := list.New()

	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &w[i])
	}

	for i := 0; i < m; i++ {
		var j int
		fmt.Fscan(reader, &j)
		b[i] = j - 1
	}

	for i := 0; i < m; i++ {
		if d[b[i]] == 0 {
			rs.PushBack(b[i])
		}
		d[b[i]] = 1
	}

	for i := 0; i < n; i++ {
		if d[i] == 0 {
			rs.PushBack(i)
		}
		d[i] = 1
	}

	var res int

	for i := 0; i < m; i++ {
		e := new(list.Element)
		for e = rs.Front(); e != nil; e = e.Next() {
			if e.Value == b[i] {
				break
			}
			j, _ := e.Value.(int)
			res += w[b[j]]
		}
		rs.Remove(e)
		rs.PushFront(e.Value)
	}

	fmt.Println(res)
}


You are given two integers $$$a$$$ and $$$b$$$. Moreover, you are given a sequence $$$s_0, s_1, \dots, s_{n}$$$. All values in $$$s$$$ are integers $$$1$$$ or $$$-1$$$. It's known that sequence is $$$k$$$-periodic and $$$k$$$ divides $$$n+1$$$. In other words, for each $$$k \leq i \leq n$$$ it's satisfied that $$$s_{i} = s_{i - k}$$$.Find out the non-negative remainder of division of $$$\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}$$$ by $$$10^{9} + 9$$$.Note that the modulo is unusual!

The first line contains four integers $$$n, a, b$$$ and $$$k$$$ $$$(1 \leq n \leq 10^{9}, 1 \leq a, b \leq 10^{9}, 1 \leq k \leq 10^{5})$$$. The second line contains a sequence of length $$$k$$$ consisting of characters '+' and '-'.  If the $$$i$$$-th character (0-indexed) is '+', then $$$s_{i} = 1$$$, otherwise $$$s_{i} = -1$$$. Note that only the first $$$k$$$ members of the sequence are given, the rest can be obtained using the periodicity property.

Output a single integer — value of given expression modulo $$$10^{9} + 9$$$.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF963A(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod int64 = 1e9 + 9
	pow := func(x int64, n int64) int64 {
		res := int64(1)
		for ; n > 0; n >>= 1 {
			if n&1 == 1 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	inv := func(a int64) int64 { return pow(a, mod-2) }
	frac := func(a, b int64) int64 { return a * inv(b) % mod }

	var n, a, b, k, ans int64
	var s []byte
	Fscan(in, &n, &a, &b, &k, &s)
	q := frac(b, a)
	for i := k - 1; i >= 0; i-- {
		ans *= q
		if s[i] == '+' {
			ans++
		} else {
			ans += mod - 1
		}
		ans %= mod
	}
	q = pow(q, k)
	n = (n + 1) / k
	if q == 1 {
		ans = ans * n % mod
	} else {
		ans = ans * frac(pow(q, n)-1, q-1) % mod
	}
	ans = ans * pow(a, n*k-1) % mod
	if ans < 0 {
		ans += mod
	}
	Fprint(out, ans)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF963A(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod int64 = 1e9 + 9
	pow := func(x int64, n int64) int64 {
		res := int64(1)
		for ; n > 0; n >>= 1 {
			if n&1 == 1 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	inv := func(a int64) int64 { return pow(a, mod-2) }
	frac := func(a, b int64) int64 { return a * inv(b) % mod }

	var n, a, b, k, ans int64
	var s []byte
	Fscan(in, &n, &a, &b, &k, &s)
	q := frac(b, a)
	for i := len(s) - 1; i >= 0; i-- {
		ans *= q
		if s[i] == '+' {
			ans++
		} else {
			ans += mod - 1
		}
		ans %= mod
	}
	ans = ans * pow(a, n) % mod
	n = (n + 1) / k
	if q = pow(q, k); q == 1 {
		ans = ans * n % mod
	} else {
		ans = ans * frac(pow(q, n)-1, q-1) % mod
	}
	Fprint(out, ans)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF963A(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod int64 = 1e9 + 9
	pow := func(x int64, n int64) int64 {
		res := int64(1)
		for ; n > 0; n >>= 1 {
			if n&1 == 1 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	inv := func(a int64) int64 { return pow(a, mod-2) }
	frac := func(a, b int64) int64 { return a * inv(b) % mod }

	var n, a, b, k, ans int64
	var s []byte
	Fscan(in, &n, &a, &b, &k, &s)
	q := frac(b, a)
	for i := k - 1; i >= 0; i-- {
		ans *= q
		if s[i] == '+' {
			ans++
		} else {
			ans += mod - 1
		}
		ans %= mod
	}
	ans = ans * pow(a, n) % mod
	n = (n + 1) / k
	if q == 1 {
		ans = ans * n % mod
	} else {
		q = pow(q, k)
		ans = ans * frac(pow(q, n)-1, q-1) % mod
	}
	Fprint(out, ans)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF963A(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod int64 = 1e9 + 9
	pow := func(x int64, n int64) int64 {
		res := int64(1)
		for ; n > 0; n >>= 1 {
			if n&1 == 1 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	inv := func(a int64) int64 { return pow(a, mod-2) }
	frac := func(a, b int64) int64 { return a * inv(b) % mod }

	var n, a, b, k, ans int64
	var s []byte
	Fscan(in, &n, &a, &b, &k, &s)
	q := frac(b, a)
	for i := len(s) - 1; i >= 0; i-- {
		ans *= q
		if s[i] == '+' {
			ans++
		} else {
			ans += mod - 1
		}
		ans %= mod
	}
	ans = ans * pow(a, n) % mod
	n = (n + 1) / k
	if q == 1 {
		ans = ans * n % mod
	} else {
		q = pow(q, k)
		ans = ans * frac(pow(q, n)-1, q-1) % mod
	}
	Fprint(out, ans)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF963A(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod int64 = 1e9 + 9
	pow := func(x int64, n int64) int64 {
		res := int64(1)
		for ; n > 0; n >>= 1 {
			if n&1 == 1 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	modInv := func(a int64) int64 { return pow(a, mod-2) }
	modFrac := func(a, b int64) int64 { return a * modInv(b) % mod }

	var n, a, b, k int64
	var s []byte
	Fscan(in, &n, &a, &b, &k, &s)
	n = (n + 1) / k
	q := modFrac(b, a)
	base := int64(0)
	for _, sign := range s {
		base *= q
		if sign == '+' {
			base++
		} else {
			base += mod - 1
		}
		base %= mod
	}
	var ans int64
	if q == 1 {
		ans = base * n % mod
	} else {
		q = pow(q, k)
		ans = base * modFrac(pow(q, n)-1, q-1) % mod
	}
	Fprint(out, ans*pow(a, n*k-1)%mod)
}

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


You are given an array $$$a$$$ consisting of $$$n$$$ integer numbers.Let instability of the array be the following value: $$$\max\limits_{i = 1}^{n} a_i - \min\limits_{i = 1}^{n} a_i$$$.You have to remove exactly one element from this array to minimize instability of the resulting $$$(n-1)$$$-elements array. Your task is to calculate the minimum possible instability.

The first line of the input contains one integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of elements in the array $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^5$$$) — elements of the array $$$a$$$.

Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array $$$a$$$.

NoteIn the first example you can remove $$$7$$$ then instability of the remaining array will be $$$3 - 1 = 2$$$.In the second example you can remove either $$$1$$$ or $$$100000$$$ then instability of the remaining array will be $$$100000 - 100000 = 0$$$ and $$$1 - 1 = 0$$$ correspondingly.

// Package main provides
//
// File:  b.go
// Author: ymiyamoto
//
// Created on Thu Dec 27 23:44:17 2018
//
package main

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

var sc = bufio.NewScanner(os.Stdin)

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

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

func main() {
	sc.Split(bufio.ScanWords)

	var n int
	fmt.Scan(&n)

	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i], _ = strconv.Atoi(read())
	}
	sort.Ints(arr)

	fmt.Println(min(arr[len(arr)-2]-arr[0], arr[len(arr)-1]-arr[1]))
}

package main

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

func main() {
	in := bufio.NewReaderSize(os.Stdin, 1024*1024)
	var n int
	fmt.Fscanf(in, "%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &a[i])
	}
	sort.Ints(a)
	l := a[len(a)-1] - a[1]
	r := a[len(a)-2] - a[0]
	if l < r {
		fmt.Println(l)
	} else {
		fmt.Println(r)
	}
}

package main

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

const INF int =0x3f3f3f3f
const LLINF int64 = 0x3f3f3f3f3f3f3f3f

func solve(){
    var a [100010] int
    var n,i,mn,mx,ans,tmp int
    n=readInt()
    mx=-INF
    mn=INF
    for i=0;i<n;i++{
        a[i]=readInt()
        mx=max(mx,a[i])
        mn=min(mn,a[i])
    }
    var flag int
    flag=0
    ans=INF
    tmp=-INF
    for i=0;i<n;i++{
        if a[i]==mx&&flag==0{
            flag=1
        }else{
            tmp=max(tmp,a[i])
        }
    }
    ans=min(ans,tmp-mn)
    tmp=INF
    flag=0
    for i=0;i<n;i++{
        if a[i]==mn&&flag==0{
            flag=1
        }else{
            tmp=min(tmp,a[i])
        }
    }
    ans=min(ans,mx-tmp)
    fmt.Println(ans)
}

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()
}
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 min(a,b int)int {
    if a<b {
        return a
    } else {
        return b
    }
}

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

package main

import (
	"bufio"
	"fmt"
	"os"
)
func swap(a ,b *int) {
	tmp := *a
	*a = *b
	*b = tmp
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var n int
	var min1,min2 int
	var max1,max2 int
	fmt.Fscanf(buf, "%d\n", &n)
	for i:=0;i < n;i++{
		var value int = 0
		fmt.Fscanf(buf,"%d",&value)
		if i == 0{
			min1 = value
			max1 = value
			continue
		}
		if min2 == 0 || value < min2{
			min2 = value
			if min2 < min1{
				swap(&min1,&min2)
			}
		}
		if max2 == 0|| value > max2{
			max2 = value
			if max2 > max1{
				swap(&max1,&max2)
			}
		}

	}
	if n == 2{
		fmt.Printf("0\n")
		return
	}
	if max2-min1 <= max1-min2{
		fmt.Printf("%d\n",max2-min1)
	}else{
		fmt.Printf("%d\n",max1-min2)
	}
}

package main

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

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

func main() {
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	input := bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		input.Scan()
		a[i], _= strconv.Atoi(input.Text())
	}
	sort.Ints(a)
	fmt.Println(min(a[n - 2] - a[0], a[n - 1] - a[1]))
}

package main

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

func main(){

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

	defer out.Flush()

	var n int

	fmt.Fscanf(in, "%d\n", &n)

	a := make([]int, n)

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

	if n < 3 {
		fmt.Fprintf(out, "0")
	}else{
		sort.Slice(a, func(i ,j int) bool{
		  	return a[i] < a[j]
		})

		output := a[n-2] - a[0]

		if a[ n - 1 ] - a[1] < output {
			output = a[ n - 1 ] - a[1]
		}

		fmt.Fprintf(out, "%d\n", output)

	}

}

package main

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

func main() {
	start := time.Now()
	defer func() {
		run := time.Since(start)
		_ = run
		// fmt.Println("Run in", run)
	}()

	var stdin *bufio.Reader
	// LOCAL
	f, _ := os.Open("input.txt")
	stdin = bufio.NewReader(f)

	// ONLINE JUDGE
	stdin = bufio.NewReader(os.Stdin)

	var n, x int
	fmt.Fscanln(stdin, &n)

	var max1, max2, min1, min2 = 0, 0, 100000 + 7, 100000 + 7
	for i := 0; i < n; i++ {
		fmt.Fscan(stdin, &x)

		if x > max1 {
			max2 = max1
			max1 = x
		} else if x > max2 {
			max2 = x
		}

		if x < min1 {
			min2 = min1
			min1 = x
		} else if x < min2 {
			min2 = x
		}
	}

	if max1-min2 < max2-min1 {
		fmt.Println(max1 - min2)
	} else {
		fmt.Println(max2 - min1)
	}
	// fmt.Println(max1, max2, min1, min2)

}

package main

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

func main() {
	var inputLen int

	fmt.Scanln(&inputLen)

	arr := make([]int, inputLen)

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

	for i := 0; i < inputLen; i++ {
		scanner.Scan()
		arr[i], _ = strconv.Atoi(scanner.Text())
	}

	fmt.Scanln()

	sort.Ints(arr)

	sideA := math.Abs(float64(arr[1] - arr[inputLen-1]))
	sideB := math.Abs(float64(arr[0] - arr[inputLen-2]))

	if sideA < sideB {
		fmt.Println(sideA)
	} else {
		fmt.Println(sideB)
	}

}

package main

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

var scanner = bufio.NewScanner(os.Stdin)

func scanNum() int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}
func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

func main() {
	scanner.Split(bufio.ScanWords)
	n := scanNum()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanNum()
	}
	if n == 2 {
		fmt.Println(0)
		os.Exit(0)
	}
	sort.Ints(a)
	fmt.Println(min(a[len(a)-2]-a[0], a[len(a)-1]-a[1]))
}

// Package main provides
//
// File:  b.go
// Author: ymiyamoto
//
// Created on Thu Dec 27 23:44:17 2018
//
package main

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

var sc = bufio.NewScanner(os.Stdin)

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

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

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

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

	sort.Ints(arr)

	fmt.Println(min(arr[len(arr)-2]-arr[0], arr[len(arr)-1]-arr[1]))
}


You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.

The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.

Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.

NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.

//Uniqueness
package main

import (
	"fmt"
)

func main() {
	var size int
	var store []uint32
	fmt.Scanf("%d\n", &size)
	store = make([]uint32, size)

	for i := 0; i < size; i++ {
		fmt.Scanf("%d", &(store[i]))
	}
	var max int
	//填充左侧
	lappear := make(map[uint32]bool, 0)

	for lcount := 0; lcount < size; lcount++ {
		// fmt.Printf("L: %d ", lcount)
		rcount := 0
		rappear := make(map[uint32]bool, 0)
		for r := size - 1; r >= lcount; r-- {
			if !mapContains(lappear, store[r]) && !mapContains(rappear, store[r]) {
				rappear[store[r]] = true
				rcount++
			} else {
				break
			}
		}
		// fmt.Printf("R: %d ", rcount)
		max = intMax(max, lcount+rcount)
		if !mapContains(lappear, store[lcount]) {
			lappear[store[lcount]] = true
		} else {
			break
		}
		// fmt.Printf("Max: %d\n", max)
	}
	fmt.Printf("%d", (size - max))
	return
}

func mapContains(Map map[uint32]bool, Key uint32) bool {
	_, ok := (Map[Key])
	return ok
}

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

package main

import (
	"fmt"
	"math"
)

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

	dupes := false
	m := make(map[int]int)
	for i := 0; i < n; i++ {
		m[a[i]]++
		if m[a[i]] > 1 {
			dupes = true
			break
		}
	}

	if !dupes {
		fmt.Println("0")
	} else {
		answer := math.MaxInt32
		for i := 0; i < n; i++ {
			m := make(map[int]int)
			moreThanSingle := 0
			for j := 0; j < n; j++ {
				m[a[j]]++
				if m[a[j]] == 2 {
					moreThanSingle++
				}
			}

			for j := i; j < n; j++ {
				// i..j = removed
				m[a[j]]--
				if m[a[j]] == 1 {
					moreThanSingle--
					if moreThanSingle == 0 {
						if j - i + 1 < answer {
							answer = j - i + 1
						}
					}
				}
			}
		}

		fmt.Println(answer)
	}
}

// Author: sighduck
// URL: https://codeforces.com/contest/1208/problem/B

package main

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

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

func Solve(n int, a []int) int {
	prefixMap := make(map[int]int)
	right := n - 1
	left := 0
	for right >= left {
		if prefixMap[a[right]] > 0 {
			break
		}
		prefixMap[a[right]] += 1
		right -= 1
	}
	right += 1
	smallestSegment := right
	for left <= right && left < n {
		if prefixMap[a[left]] > 0 && right < n {
			prefixMap[a[right]] -= 1
			right += 1
		} else if prefixMap[a[left]] > 0 {
			break
		} else {
			prefixMap[a[left]] += 1
			smallestSegment = min(smallestSegment, right-left-1)
			left += 1
		}
	}
	return smallestSegment
}

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

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

	fmt.Fprintf(writer, "%d\n", Solve(n, a))
}

//Uniqueness
package main

import (
	"fmt"
)

func main() {
	var size int
	var store []uint32
	fmt.Scanf("%d\n", &size)
	store = make([]uint32, size)

	for i := 0; i < size; i++ {
		fmt.Scanf("%d", &(store[i]))
	}
	var max int
	//填充左侧
	lappear := make(map[uint32]bool, 0)

	for lcount := 0; lcount < size; lcount++ {
		// fmt.Printf("L: %d ", lcount)
		rcount := 0
		rappear := make(map[uint32]bool, 0)
		for r := size - 1; r >= lcount; r-- {
			if !mapContains(lappear, store[r]) && !mapContains(rappear, store[r]) {
				rappear[store[r]] = true
				rcount++
			} else {
				break
			}
		}
		// fmt.Printf("R: %d ", rcount)
		max = intMax(max, lcount+rcount)
		if !mapContains(lappear, store[lcount]) {
			lappear[store[lcount]] = true
		} else {
			break
		}
		// fmt.Printf("Max: %d\n", max)
	}
	print(size - max)
	return
}

func mapContains(Map map[uint32]bool, Key uint32) bool {
	_, ok := (Map[Key])
	return ok
}

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

package main

import (
	"fmt"
	"math"
)

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

	dupes := false
	m := make(map[int]int)
	for i := 0; i < n; i++ {
		m[a[i]]++
		if m[a[i]] > 1 {
			dupes = true
			break
		}
	}

	if !dupes {
		fmt.Println("0")
	} else {
		answer := math.MaxInt32
		for i := 0; i < n; i++ {
			m := make(map[int]int)
			moreThanSingle := 0
			for j := 0; j < n; j++ {
				m[a[j]]++
				if m[a[j]] > 1 {
					moreThanSingle++
				}
			}

			for j := i; j < n; j++ {
				// i..j = removed
				m[a[j]]--
				if m[a[j]] == 1 {
					moreThanSingle--
					if moreThanSingle == 0 {
						if j - i + 1 < answer {
							answer = j - i + 1
						}
					}
				}
			}
		}

		fmt.Println(answer)
	}
}

// Author: sighduck
// URL: https://codeforces.com/contest/1208/problem/B

package main

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

func suffixFirst(n int, a []int) int {
	prefixMap := make(map[int]int)
	right := n - 1
	left := 0
	for right >= left {
		if prefixMap[a[right]] > 0 {
			break
		}
		prefixMap[a[right]] += 1
		right -= 1
	}
	for left <= right {
		if prefixMap[a[left]] > 0 {
			break
		}
		prefixMap[a[left]] += 1
		left += 1
	}
	return right - left + 1
}

func prefixFirst(n int, a []int) int {
	prefixMap := make(map[int]int)
	right := n - 1
	left := 0
	for left <= right {
		if prefixMap[a[left]] > 0 {
			break
		}
		prefixMap[a[left]] += 1
		left += 1
	}
	for right >= left {
		if prefixMap[a[right]] > 0 {
			break
		}
		prefixMap[a[right]] += 1
		right -= 1
	}
	return right - left + 1
}

func Solve(n int, a []int) int {
	prefixFirstLength := prefixFirst(n, a)
	suffixFirstLength := suffixFirst(n, a)
	if prefixFirstLength < suffixFirstLength {
		return prefixFirstLength
	}
	return suffixFirstLength
}

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

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

	fmt.Fprintf(writer, "%d\n", Solve(n, a))
}


There were n groups of students which came to write a training contest. A group is either one person who can write the contest with anyone else, or two people who want to write the contest in the same team.The coach decided to form teams of exactly three people for this training. Determine the maximum number of teams of three people he can form. It is possible that he can't use all groups to form teams. For groups of two, either both students should write the contest, or both should not. If two students from a group of two will write the contest, they should be in the same team.

The first line contains single integer n (2 ≤ n ≤ 2·105) — the number of groups. The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 2), where ai is the number of people in group i.

Print the maximum number of teams of three people the coach can form.

NoteIn the first example the coach can form one team. For example, he can take students from the first, second and fourth groups.In the second example he can't make a single team.In the third example the coach can form three teams. For example, he can do this in the following way:  The first group (of two people) and the seventh group (of one person),  The second group (of two people) and the sixth group (of one person),  The third group (of two people) and the fourth group (of one person).

package main

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

func main() {
	var n int
	buf, _ := ioutil.ReadAll(os.Stdin)
	in := bytes.NewReader(buf)
	fmt.Fscan(in, &n)
	var c1, c2, t int32
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &t)
		if t == 1 {
			c1++
		} else if t == 2 {
			c2++
		}
	}
	if c1 > c2 {
		fmt.Println(c2 + (c1-c2)/3)
	} else {
		fmt.Println(c1)
	}
}

package main

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

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

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

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

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte.  Trailing '\n' will not be included.
// See also comments on readb()
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

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

// Utilities

func sumSlice(a []int) int {
	var res int
	for _, v := range a {
		res += v
	}
	return res
}

func sumSlicell(a []int64) int64 {
	var res int64
	for _, v := range a {
		res += v
	}
	return res
}

func readInts(N int) (int, []int) {
	if N == 0 {
		N = readi()
	}
	a := make([]int, N)
	for i := range a {
		a[i] = readi()
	}
	return N, a
}

func readIntsll(N int) (int, []int64) {
	if N == 0 {
		N = readi()
	}
	a := make([]int64, N)
	for i := range a {
		a[i] = readll()
	}
	return N, a
}

// reverse slice in place
func reverse(a []int) {
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
}

// areverse: allocating reverse
func areverse(a []int) []int {
	b := make([]int, len(a))
	for i := range a {
		b[i] = a[len(a)-i-1]
	}
	return b
}

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

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

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

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

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

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

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

func absll(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}

func main() {
	defer Flush()

	_, a := readInts(0)

	var c [3]int
	for _, v := range a {
		c[v]++
	}

	ans := min(c[1], c[2])
	add := (c[1] - ans) / 3
	if 0 < add {
		ans += add
	}
	println(ans)
}

package main

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

type SHelper struct {
	file *os.File
	scanner *bufio.Scanner
}

func (this *SHelper) Init() {
	this.file = os.Stdin
	this.scanner = bufio.NewScanner(this.file)
	this.scanner.Split(bufio.ScanWords)
}

func (this *SHelper) ReadInt64() int64 {
	this.scanner.Scan()
	n, err:= strconv.ParseInt(this.scanner.Text(), 10, 64)
	if err != nil {
		panic(err)
	}
	return n
}

var helper SHelper

func main() {
	helper.Init()
	n := helper.ReadInt64()

	nSum1 := int64(0)
	nSum2 := int64(0)

	for i := int64(0); i < n; i++ {
		x := helper.ReadInt64()
		if x == 1 {
			nSum1++
		} else {
			nSum2++
		}
	}

	nSum := int64(0)
	if nSum2 < nSum1 {
		nSum = nSum2 + (nSum1 - nSum2) / 3
	} else {
		nSum = nSum1
	}

	fmt.Println(nSum)
}