The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2).You are given a sequence $$$a$$$ consisting of $$$n$$$ integers.You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it).For example, for the sequence $$$[1, 2, 4, 3, 2]$$$ the answer is $$$4$$$ (you take $$$1$$$ and the sequence becomes $$$[2, 4, 3, 2]$$$, then you take the rightmost element $$$2$$$ and the sequence becomes $$$[2, 4, 3]$$$, then you take $$$3$$$ and the sequence becomes $$$[2, 4]$$$ and then you take $$$4$$$ and the sequence becomes $$$[2]$$$, the obtained increasing sequence is $$$[1, 2, 3, 4]$$$).

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

In the first line of the output print $$$k$$$ — the maximum number of elements in a strictly increasing sequence you can obtain. In the second line print a string $$$s$$$ of length $$$k$$$, where the $$$j$$$-th character of this string $$$s_j$$$ should be 'L' if you take the leftmost element during the $$$j$$$-th move and 'R' otherwise. If there are multiple answers, you can print any.

NoteThe first example is described in the problem statement.

package main

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

const MX_N = 200002

var (
    N int
    a [MX_N]int
)

func reader() func() int {
    scn := bufio.NewScanner(os.Stdin)
    scn.Split(bufio.ScanWords)
    return func() int {
        scn.Scan()
        x, _ := strconv.Atoi(scn.Text())
        return x
    }
}

func main(){
    r := reader()
    N = r()
    for i := 0; i < N; i++ {
        a[i] = r()
    }
    left, right := 0, N-1
    pv := 0

    out := make([]rune, 0, N)
    for right >= left{
        if a[left] <= pv && a[right] <= pv {
            break
        }
        if a[left] > pv && a[right] <= pv {
            pv = a[left]
            left++
            out = append(out, 'L')
        } else if a[right] > pv && a[left] <= pv {
            pv = a[right]
            right--
            out = append(out, 'R')
        } else if a[right] == a[left] {
            linc := 1
            for x := left + 1; x <= right; x++ {
                if a[x] <= a[x-1] {
                    break
                }
                linc++
            }
            rinc := 1
            for x := right - 1; x >= left; x-- {
                if a[x] <= a[x+1] {
                    break
                }
                rinc++
            }
            if linc > rinc {
                pv = a[left]
                left++
                out = append(out, 'L')
            } else {
                pv = a[right]
                right--
                out = append(out, 'R')
            }
        } else { // a[right] > pv && a[left] > pv && a[left] != a[right]
            if a[right] < a[left] {
                pv = a[right]
                right--
                out = append(out, 'R')
            } else {
                pv = a[left]
                left++
                out = append(out, 'L')
            }
        }
    }
    fmt.Println(len(out))
    for _, x := range out {
        fmt.Printf("%c", x)
    }
    fmt.Println()
}

package main

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

// Range ...
type Range = struct {
	l   int
	r   int
	min uint32
}

var memo map[Range][]bool

func solve(a *[]uint32, l, r int, min uint32) []bool {
	if l > r {
		return nil
	}

	hash := Range{l, r, min}

	res, found := memo[hash]
	if found {
		return res
	}

	arr := *a

	res = make([]bool, 0, len(arr))

	for ok := true; ok && l <= r; {
		ok = true

		if arr[l] > min && (arr[l] == arr[r]) {
			left := solve(a, 1+l, r, arr[l])
			right := solve(a, l, r-1, arr[r])

			if len(left) > len(right) {
				res = append(res, false)
				res = append(res, left...)
				break
			}

			res = append(res, true)
			res = append(res, right...)

			break
		}

		if arr[l] > min && (arr[l] < arr[r] || arr[r] <= min) {
			res = append(res, false) //+ solve(a, 1+l, r, arr[l])
			min = arr[l]
			l++
			continue
		}

		if arr[r] > min {
			res = append(res, true) // + solve(a, l, r-1, arr[r])
			min = arr[r]
			r--
			continue
		}
		ok = false
	}

	memo[hash] = res
	return res
}

func toInt(buf []byte) uint32 {
	n := uint32(0)
	for _, v := range buf {
		n = n*10 + uint32(v-'0')
	}

	return n
}

func main() {
	memo = map[Range][]bool{}
	var n int

	fmt.Scan(&n)

	a := make([]uint32, n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := range a {
		scanner.Scan()
		a[i] = toInt(scanner.Bytes())
	}

	ans := solve(&a, 0, len(a)-1, 0)

	fmt.Println(len(ans))
	for _, b := range ans {
		if b {
			fmt.Print("R")
		} else {
			fmt.Print("L")
		}
	}
	fmt.Println()
}

package main

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

// Range ...
type Range = struct {
	l   int
	r   int
	min uint32
}

var memo map[Range][]bool

func solve(a *[]uint32, l, r int, min uint32) []bool {
	if l > r {
		return nil
	}

	hash := Range{l, r, min}

	res, found := memo[hash]
	if found {
		return res
	}

	arr := *a

	res = make([]bool, 0, len(arr))

	for ok := true; ok && l <= r; {
		ok = true

		if arr[l] > min && (arr[l] == arr[r]) {
			left := solve(a, 1+l, r, arr[l])
			right := solve(a, l, r-1, arr[r])

			if len(left) > len(right) {
				res = append(res, false)
				res = append(res, left...)
			}

			res = append(res, true)
			res = append(res, right...)

			break
		}

		if arr[l] > min && (arr[l] < arr[r] || arr[r] <= min) {
			res = append(res, false) //+ solve(a, 1+l, r, arr[l])
			min = arr[l]
			l++
			continue
		}

		if arr[r] > min {
			res = append(res, true) // + solve(a, l, r-1, arr[r])
			min = arr[r]
			r--
			continue
		}
		ok = false
	}

	memo[hash] = res
	return res
}

func toInt(buf []byte) uint32 {
	n := uint32(0)
	for _, v := range buf {
		n = n*10 + uint32(v-'0')
	}

	return n
}

func main() {
	memo = map[Range][]bool{}
	var n int

	fmt.Scan(&n)

	a := make([]uint32, n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := range a {
		scanner.Scan()
		a[i] = toInt(scanner.Bytes())
	}

	ans := solve(&a, 0, len(a)-1, 0)

	fmt.Println(len(ans))
	for _, b := range ans {
		if b {
			fmt.Print("R")
		} else {
			fmt.Print("L")
		}
	}
	fmt.Println()
}


One day, early in the morning, you decided to buy yourself a bag of chips in the nearby store. The store has chips of $$$n$$$ different flavors. A bag of the $$$i$$$-th flavor costs $$$a_i$$$ burles.The store may run out of some flavors, so you'll decide which one to buy after arriving there. But there are two major flaws in this plan:   you have only coins of $$$1$$$, $$$2$$$ and $$$3$$$ burles;  since it's morning, the store will ask you to pay in exact change, i. e. if you choose the $$$i$$$-th flavor, you'll have to pay exactly $$$a_i$$$ burles. Coins are heavy, so you'd like to take the least possible number of coins in total. That's why you are wondering: what is the minimum total number of coins you should take with you, so you can buy a bag of chips of any flavor in exact change?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of flavors in the store. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the cost of one bag of each flavor.

For each test case, print one integer — the minimum number of coins you need to buy one bag of any flavor you'll choose in exact change.

NoteIn the first test case, you should, for example, take with you $$$445$$$ coins of value $$$3$$$ and $$$1$$$ coin of value $$$2$$$. So, $$$1337 = 445 \cdot 3 + 1 \cdot 2$$$.In the second test case, you should, for example, take $$$2$$$ coins of value $$$3$$$ and $$$2$$$ coins of value $$$2$$$. So you can pay either exactly $$$8 = 2 \cdot 3 + 1 \cdot 2$$$ or $$$10 = 2 \cdot 3 + 2 \cdot 2$$$.In the third test case, it's enough to take $$$1$$$ coin of value $$$3$$$ and $$$2$$$ coins of value $$$1$$$.

package main

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

var n int

var a []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

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

		a = make([]int, n)

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

		ans := solve()

		fmt.Println(ans)
	}
}

type pair struct {
	v, id int
}

type ByV []*pair

func (a ByV) Len() int           { return len(a) }
func (a ByV) Less(i, j int) bool { return a[i].v < a[j].v }
func (a ByV) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

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

	mp := make(map[int]bool)
	for i := 0; i < n; i++ {
		mp[a[i]] = true
	}

	max := a[n - 1]

	var ans = max

	d2 := max/2
	pres := false

	for i := 0; i < n; i++ {
		if a[i] % 2 == 1 {
			pres = true
			break
		}
	}
	if pres {
		ans = min(ans, d2 + 1)
	} else {
		ans = min(ans, d2)
	}

	d3 := max/3

	onePres := false
	twoPres := false
	one := false

	for i := 0; i < n; i++ {
		if a[i] == 1 {
			one = true
		}
		if a[i] % 3 == 1 {
			onePres = true
		}
		if a[i] % 3 == 2{
			twoPres = true
		}
	}
	if !onePres && !twoPres {
		ans = min(ans, d3)

	} else if (onePres && !twoPres) || (!onePres && twoPres) {
		ans = min(ans, d3 + 1)

	} else {
		if max % 3 == 0 {
			ans = min(ans, d3 + 1)
		} else {
			ans = min(ans, d3 + 2)
		}

		if max % 3 == 1 {
			//1 2 6 7
			yes := false
			for i := n - 2; i >= 0; i-- {
				if a[i] == max {
					continue
				}
				if a[i]/3 < d3 {
					break
				}
				yes = true
			}
			if !yes {
				if one {
					ans = min(ans, d3 + 2)
				} else {
					ans = min(ans, d3 + 1)
				}

			} else {
				ans = min(ans, d3 + 2)
			}

		} else {
			ans = min(ans, d3 + 2)
		}
	}

	return ans
}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

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

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

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

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
// 	redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n int
	scan(&n)
	var prices = make([]int, n)
	for i := range prices {
		scan(&prices[i])
	}
	var (
		check = func(a, b, c int) bool {
			for _, x := range prices {
				ok := false
				for cnt2 := 0; cnt2 <= b; cnt2++ {
					for cnt1 := 0; cnt1 <= c; cnt1++ {
						rem := cnt1*1 + cnt2*2
						if x-rem >= 0 && (x-rem)%3 == 0 && (x-rem)/3 <= a {
							ok = true
						}
					}
				}
				if !ok {
					return false
				}
			}
			return true
		}
		ans = 1 << 30
	)
	for b := 0; b <= 2; b++ {
		for c := 0; c <= 1; c++ {
			l, r := -1, int(1e9)/3+1
			for r-l > 1 {
				m := l + (r-l)/2
				if check(m, b, c) {
					r = m
				} else {
					l = m
				}
			}
			if ans > r+b+c {
				ans = r + b + c
			}
		}
	}
	printf("%v\n", ans)
}

package main

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

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

	var T, n, v int
	for Fscan(in, &T); T > 0; T-- {
		has := map[int]bool{}
		c := [3]int{}
		mx := 0
		for Fscan(in, &n); n > 0; n-- {
			Fscan(in, &v)
			has[v] = true
			c[v%3]++
			if v > mx {
				mx = v
			}
		}

		ans := mx / 3
		if mx%3 == 0 {
			if c[1] > 0 || c[2] > 0 {
				ans++
			}
		} else if mx%3 == 1 {
			ans++
			if c[2] > 0 && (has[1] || has[mx-1]) {
				ans++
			}
		} else {
			ans++
			if c[1] > 0 {
				ans++
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

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

var n int

var a []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

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

		a = make([]int, n)

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

		ans := solve()

		fmt.Println(ans)
	}
}

type pair struct {
	v, id int
}

type ByV []*pair

func (a ByV) Len() int           { return len(a) }
func (a ByV) Less(i, j int) bool { return a[i].v < a[j].v }
func (a ByV) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

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

	mp := make(map[int]bool)
	for i := 0; i < n; i++ {
		mp[a[i]] = true
	}

	max := a[n - 1]

	var ans = max

	d2 := max/2
	pres := false

	for i := 0; i < n; i++ {
		if a[i] % 2 == 1 {
			pres = true
			break
		}
	}
	if pres {
		ans = min(ans, d2 + 1)
	} else {
		ans = min(ans, d2)
	}

	d3 := max/3

	onePres := false
	twoPres := false
	one := false

	for i := 0; i < n; i++ {
		if a[i] == 1 {
			one = true
		}
		if a[i] % 3 == 1 {
			onePres = true
		}
		if a[i] % 3 == 2{
			twoPres = true
		}
	}
	if !onePres && !twoPres {
		ans = min(ans, d3)

	} else if (onePres && !twoPres) || (!onePres && twoPres) {
		ans = min(ans, d3 + 1)

	} else {
		if max % 3 == 0 {
			ans = min(ans, d3 + 1)
		} else {
			ans = min(ans, d3 + 2)
		}

		if max % 3 == 1 {
			//1 2 6 7
			yes := false
			for i := n - 2; i >= 0; i-- {
				if a[i] == max {
					continue
				}
				if a[i]/3 < d3 {
					break
				}
				yes = true
			}
			if !yes {
				if one {
					ans = min(ans, d3 + 2)
				}
				ans = min(ans, d3 + 1)
			} else {
				ans = min(ans, d3 + 2)
			}

		} else {
			ans = min(ans, d3 + 2)
		}
	}

	return ans
}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

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

var n int

var a []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

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

		a = make([]int, n)

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

		ans := solve()

		fmt.Println(ans)
	}
}

type pair struct {
	v, id int
}

type ByV []*pair

func (a ByV) Len() int           { return len(a) }
func (a ByV) Less(i, j int) bool { return a[i].v < a[j].v }
func (a ByV) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

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

	mp := make(map[int]bool)
	for i := 0; i < n; i++ {
		mp[a[i]] = true
	}

	max := a[n - 1]

	var ans = max

	d2 := max/2
	pres := false

	for i := 0; i < n; i++ {
		if a[i] % 2 == 1 {
			pres = true
			break
		}
	}
	if pres {
		ans = min(ans, d2 + 1)
	} else {
		ans = min(ans, d2)
	}

	d3 := max/3

	onePres := false
	twoPres := false

	for i := 0; i < n; i++ {
		if a[i] % 3 == 1 {
			onePres = true
		}
		if a[i] % 3 == 2{
			twoPres = true
		}
	}
	if !onePres && !twoPres {
		ans = min(ans, d3)

	} else if (onePres && !twoPres) || (!onePres && twoPres) {
		ans = min(ans, d3 + 1)

	} else {
		if max % 3 == 0 {
			ans = min(ans, d3 + 1)
		} else {
			ans = min(ans, d3 + 2)
		}

		if max % 3 == 1 {
			//1 2 6 7
			yes := false
			for i := n - 2; i >= 0; i-- {
				if a[i] == max {
					continue
				}
				if a[i]/3 < d3 {
					break
				}
				yes = true
			}
			if !yes {
				ans = min(ans, d3 + 1)
			} else {
				ans = min(ans, d3 + 2)
			}

		} else {
			ans = min(ans, d3 + 2)
		}
	}

	return ans
}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

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

var n int

var a []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

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

		a = make([]int, n)

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

		ans := solve()

		fmt.Println(ans)
	}
}

type pair struct {
	v, id int
}

type ByV []*pair

func (a ByV) Len() int           { return len(a) }
func (a ByV) Less(i, j int) bool { return a[i].v < a[j].v }
func (a ByV) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

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

	var d int

	max := a[n - 1]
	d = max/3

	var remZ bool
	var remO bool
	var remT bool

	if max % 3 == 0 {
		remZ  = true
	} else if max % 3 == 1 {
		remO = true
	} else {
		remT = true
	}

	for i := n - 2; i >= 0; i-- {
		if a[i]/3 < d {
			break
		}

		rem := a[i]% 3

		if rem == 0 {
			remZ  = true
		} else if rem == 1 {
			remO = true
		} else {
			remT = true
		}
	}

	o := false
	t := false
	onePres := false

	for i := 0; i < n; i++ {
		if a[i] == 1 {
			onePres = true
		}

		if a[i] % 3 == 1 {
			o = true
		}
		if a[i] % 3 == 2 {
			t = true
		}
	}
	if !o && !t {
		return d
	}

	if (o && !t) || (!o && t) {
		return d + 1
	}

	//1 1
	//2 2
	//1 2

	if !onePres {
		if remO && !remZ && !remT {
			return d + 1
		}

		return d + 2

	}

	ans := d + 2

	if max == 3 {
		ans--
	}

	return ans
}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

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

var n int

var a []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

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

		a = make([]int, n)

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

		ans := solve()

		fmt.Println(ans)
	}
}

type pair struct {
	v, id int
}

type ByV []*pair

func (a ByV) Len() int           { return len(a) }
func (a ByV) Less(i, j int) bool { return a[i].v < a[j].v }
func (a ByV) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

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

	max := a[len(a) - 1]

	d := max/3

	o := false
	t := false
	onePres := false

	for i := 0; i < n; i++ {
		if a[i] == 1 {
			onePres = true
		}

		if a[i] % 3 == 1 {
			o = true
		}
		if a[i] % 3 == 2 {
			t = true
		}
	}
	if !o && !t {
		return d
	}

	if (o && !t) || (!o && t) {
		return d + 1
	}

	//1 1
	//2 2
	//1 2

	if !onePres {
		return d + 1
	}

	ans := d + 2

	if max == 3 {
		ans--
	}

	return ans
}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

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

var n int

var a []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

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

		a = make([]int, n)

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

		ans := solve()

		fmt.Println(ans)
	}
}

type pair struct {
	v, id int
}

type ByV []*pair

func (a ByV) Len() int           { return len(a) }
func (a ByV) Less(i, j int) bool { return a[i].v < a[j].v }
func (a ByV) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

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

	max := a[len(a) - 1]

	d := max/3

	o := false
	t := false
	onePres := false
	threePres := false

	for i := 0; i < n; i++ {
		if a[i] == 1 {
			onePres = true
		}
		if a[i] == 3 {
			threePres = true
		}

		if a[i] % 3 == 1 {
			o = true
		}
		if a[i] % 3 == 2 {
			t = true
		}
	}
	if !o && !t {
		return d
	}

	if (o && !t) || (!o && t) {
		return d + 1
	}

	if onePres {
		ans := d + 2

		if threePres {
			ans--
		}
		return ans
	}

	return d + 1

	//1 1
	//2 2
	//1 2
}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

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

var n int

var a []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

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

		a = make([]int, n)

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

		ans := solve()

		fmt.Println(ans)
	}
}

type pair struct {
	v, id int
}

type ByV []*pair

func (a ByV) Len() int           { return len(a) }
func (a ByV) Less(i, j int) bool { return a[i].v < a[j].v }
func (a ByV) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

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

	max := a[len(a) - 1]

	d := max/3

	o := false
	t := false
	onePres := false

	for i := 0; i < n; i++ {
		if a[i] == 1 {
			onePres = true
		}

		if a[i] % 3 == 1 {
			o = true
		}
		if a[i] % 3 == 2 {
			t = true
		}
	}
	if !o && !t {
		return d
	}

	if (o && !t) || (!o && t) {
		return d + 1
	}

	if onePres {
		if max > 3 {
			return d + 2
		}

		return d + 1

	}
	return d + 1
}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

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

var n int

var a []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

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

		a = make([]int, n)

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

		ans := solve()

		fmt.Println(ans)
	}
}

type pair struct {
	v, id int
}

type ByV []*pair

func (a ByV) Len() int           { return len(a) }
func (a ByV) Less(i, j int) bool { return a[i].v < a[j].v }
func (a ByV) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

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

	max := a[len(a) - 1]

	d := max/3

	o := false
	t := false
	onePres := false

	for i := 0; i < n; i++ {
		if a[i] == 1 {
			onePres = true
		}

		if a[i] % 3 == 1 {
			o = true
		}
		if a[i] % 3 == 2 {
			t = true
		}
	}
	if !o && !t {
		return d
	}

	if (o && !t) || (!o && t) {
		return d + 1
	}

	if onePres {
		return d + 2

	}
	return d + 1
}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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


The prison of your city has n prisoners. As the prison can't accommodate all of them, the city mayor has decided to transfer c of the prisoners to a prison located in another city.For this reason, he made the n prisoners to stand in a line, with a number written on their chests. The number is the severity of the crime he/she has committed. The greater the number, the more severe his/her crime was.Then, the mayor told you to choose the c prisoners, who will be transferred to the other prison. He also imposed two conditions. They are,  The chosen c prisoners has to form a contiguous segment of prisoners.  Any of the chosen prisoner's crime level should not be greater then t. Because, that will make the prisoner a severe criminal and the mayor doesn't want to take the risk of his running away during the transfer. Find the number of ways you can choose the c prisoners.

The first line of input will contain three space separated integers n (1 ≤ n ≤ 2·105), t (0 ≤ t ≤ 109) and c (1 ≤ c ≤ n). The next line will contain n space separated integers, the ith integer is the severity ith prisoner's crime. The value of crime severities will be non-negative and will not exceed 109.

Print a single integer — the number of ways you can choose the c prisoners.

package main

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

func main()  {
	var n,t,c int
	fmt.Scan(&n,&t,&c)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	s := make([]int,n)
	for i:=0; i<n; i++{
		scanner.Scan()
		s[i],_ = strconv.Atoi(scanner.Text())
	}
	var res int
	var cnt int //number of violation in the current interval
	for i:=0;i<c;i++{
		if s[i] > t {cnt++}
	}
	if cnt == 0 {res++}
	for i:=c;i<n;i++{
		if s[i-c] > t {cnt--}
		if s[i] > t {cnt++}
		if cnt == 0 {res++}
	}
	fmt.Println(res)

}

package main

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

func main() {
	var n, t, c, tmp int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n)
	fmt.Fscan(reader, &t)
	fmt.Fscan(reader, &c)
	list := make([]int, 0)
	for i := 0; i < n; i += 1 {
		fmt.Fscan(reader, &tmp)
		list = append(list, tmp)
	}
	way := 0
	count := 0
	for i := 0; i < c; i += 1 {
		if list[i] > t {
			count += 1
		}
	}
	if count == 0 {
		way += 1
	}
	for i := c; i < n; i += 1 {
		if list[i-c] > t {
			count -= 1
		}
		if list[i] > t {
			count += 1
		}
		if count == 0 {
			way += 1
		}
	}
	fmt.Println(way)
}

package main

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

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

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

	n, t, c := nextInt(), nextInt(), nextInt()
	countC := 0
	crimeLevel := 0
	count := 0

	for i:=0; i<n; i++ {
		crimeLevel = nextInt()
		if crimeLevel > t {
			countC = 0
		} else {
			if countC+1 == c {
				count++
			} else {
				countC++
			}
		}
	}

	println(count)
}

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

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func nextFloat() float64 {
	n, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

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

package main

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

func main() {
	var n,t,c int
	input := bufio.NewScanner(os.Stdin)
    	input.Split(bufio.ScanWords)
    	k,s:=0,0
    	fmt.Scan(&n,&t,&c)

	for i:=1;i<=n;i++ {
		input.Scan()
       		aa, _ := strconv.Atoi(input.Text())
		if aa>t {
			if i-k>=c {
				s+=i-k-c
			}
			k=i
		}
		if i==n && aa<=t && i-k+1>=c {
			s+=i-k-c+1
		}
	}
	fmt.Println(s)
}

package main

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

var (
    reader = bufio.NewReader(os.Stdin)
)

func main() {
    var (
        n, t, c int
        p       [200010]int
    )
    fmt.Fscan(reader, &n, &t, &c)
    for i := 0; i < n; i++ {
        fmt.Fscan(reader, &p[i])
    }
    p[n] = 1e9 + 1
    var tmp, cnt int
    for i := 0; i <= n; i++ {
        if p[i] <= t {
            tmp++
        } else {
            if tmp-c >= 0 {
                cnt = cnt + tmp - c + 1
            }
            tmp = 0
        }
    }
    fmt.Println(cnt)
}

package main

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

func main() {
	var n, t, c int
	if _, err := fmt.Scanf("%d %d %d\n", &n, &t, &c); err != nil {
		return
	}

	plan := 0
	last_ok := -1

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; scanner.Scan() && i < n; i++ {
		a, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		if a > t {
			if last_ok >= 0 && i-last_ok >= c {
				plan += i - last_ok - c + 1
			}
			last_ok = -1
		} else {
			if last_ok < 0 {
				last_ok = i
			}
		}
	}
	if last_ok >= 0 && n-last_ok >= c {
		plan += n - last_ok - c + 1
	}

	fmt.Println(plan)
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    var n,t,c int
    var a,cnt [200005]int
    fmt.Scan(&n,&t,&c)
    for i := 0; i < n; i++ {
        scanner.Scan()
        a[i], _ = strconv.Atoi(scanner.Text())
        if a[i] > t { cnt[i]++ }
        if i != 0 { cnt[i] += cnt[i-1] }
    }
    ans := 0
    for i := 0; i <= (n-c); i++ {
        if i == 0 {
            if cnt[i+c-1] == 0 { ans++ }
        } else {
            if (cnt[i+c-1]-cnt[i-1]) == 0 { ans++ }
        }
    }
    fmt.Println(ans)
}

package main

import (
	"bufio"
	"errors"
	"flag"
	"os"
	"strconv"
	"time"
)

// ******************************** CODE STARTS ********************************

var ()

func solve() {
	n := readInt()
	limit := readInt()
	num := readInt()

	ans := 0
	prev := -1
	for i := 0; i < n; i++ {
		value := readInt()
		if value == -1 || value > limit {
			if prev >= num {
				ans += prev - num + 1
			}
			prev = -1
		} else {
			if prev == -1 {
				prev = 1
			} else {
				prev++
			}
		}
	}
	if prev >= num {
		ans += prev - num + 1
	}
	printInt(ans)
	printLine()
}

// ********************************* CODE ENDS *********************************

// IO.

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

func main() {
	local := flag.Bool("local", false, "Defines whether run is local or remote.")
	flag.Parse()

	var (
		input  *os.File
		output *os.File
	)

	if *local {
		input, _ = os.Open("/home/outside/coding/workspace/go/io/input.txt")
		output, _ = os.Create("/home/outside/coding/workspace/go/io/output.txt")
		startTime := time.Now()
		defer func() {
			print("Time: ")
			print(strconv.FormatFloat(time.Since(startTime).Seconds(), 'f', 5, 64))
			println("s")
		}()
	} else {
		input = os.Stdin
		output = os.Stdout
		// input, _ = os.Open("")
		// output, _ = os.Create("")
	}
	defer input.Close()
	defer output.Close()

	scanner = bufio.NewScanner(input)
	writer = bufio.NewWriter(output)
	defer writer.Flush()

	scanner.Split(bufio.ScanWords)

	solve()
}

// Read.

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

func readLine() string {
	scanner.Split(bufio.ScanLines)
	defer scanner.Split(bufio.ScanWords)
	return readString()
}

func readInt() int {
	ans, _ := strconv.ParseInt(readString(), 10, 0)
	return int(ans)
}

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

func readFloat64() float64 {
	ans, _ := strconv.ParseFloat(readString(), 64)
	return ans
}

// Write.

func printString(value string) {
	writer.WriteString(value)
}

func printLine() {
	printString("\n")
}

func printInt(value int) {
	printString(strconv.FormatInt(int64(value), 10))
}

func printInt64(value int64) {
	printString(strconv.FormatInt(value, 10))
}

func printFLoat64(value float64) {
	printString(strconv.FormatFloat(value, 'f', 20, 64))
}

// Min, max, abs.

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

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

// Set Key interface implementations.

type Int int

func (p Int) Equals(key Key) bool {
	return int(p) == int(key.(Int))
}

func (p Int) Less(key Key) bool {
	return int(p) < int(key.(Int))
}

// Set.

type Key interface {
	Equals(key Key) bool
	Less(key Key) bool
}

type node struct {
	key   Key
	left  *node
	right *node
}

func newNode(key Key) *node {
	return &node{key, nil, nil}
}

type SplayTree struct {
	root *node
	tmp  *node
	len  int
}

func NewSplayTree() *SplayTree {
	return &SplayTree{nil, newNode(nil), 0}
}

func rotateLeft(x, p *node) {
	p.right = x.left
	x.left = p
}

func rotateRight(x, p *node) {
	p.left = x.right
	x.right = p
}

func (p *SplayTree) splay(x *node, key Key) *node {
	if x == nil {
		return nil
	}

	left := p.tmp
	right := p.tmp

	for {
		if key.Less(x.key) {
			y := x.left
			if y == nil {
				break
			}
			if key.Less(y.key) { // zig-zig
				rotateRight(y, x)
				x = y
				if x.left == nil {
					break
				}
			}
			// link right
			right.left = x
			right = x
			// move left
			x = x.left
		} else if x.key.Less(key) {
			y := x.right
			if y == nil {
				break
			}
			if y.key.Less(key) { // zig-zig
				rotateLeft(y, x)
				x = y
				if x.right == nil {
					break
				}
			}
			// link left
			left.right = x
			left = x
			// move right
			x = x.right
		} else {
			break
		}
	}

	left.right = x.left
	right.left = x.right
	x.left = p.tmp.right
	x.right = p.tmp.left

	return x
}

// left <= key < right
func (p *SplayTree) split(key Key) (left, right *node) {
	p.root = p.splay(p.root, key)
	if p.root.key.Equals(key) || p.root.key.Less(key) {
		right := p.root.right
		p.root.right = nil
		return p.root, right
	} else {
		left := p.root.left
		p.root.left = nil
		return left, p.root
	}
}

// keys from left tree must be less then keys from right tree
func (p *SplayTree) join(left, right *node) *node {
	if left == nil {
		return right
	} else if right == nil {
		return left
	}
	left = p.splay(left, right.key)
	left.right = right
	return left
}

type Set interface {
	Len() int
	Insert(key Key) error
	Find(key Key) bool
	Remove(key Key) error
}

func NewSet() Set {
	return Set(NewSplayTree())
}

func (p *SplayTree) Len() int {
	return p.len
}

func (p *SplayTree) Insert(key Key) error {
	if p.root == nil {
		p.root = newNode(key)
		p.len++
	} else {
		p.root = p.splay(p.root, key)
		if p.root.key.Equals(key) {
			return errors.New("Such key already exists")
		} else {
			left, right := p.split(key)
			p.root = newNode(key)
			p.root.left = left
			p.root.right = right
			p.len++
		}
	}
	return nil
}

func (p *SplayTree) Find(key Key) bool {
	if p.root == nil {
		return false
	}
	p.root = p.splay(p.root, key)
	return p.root.key.Equals(key)
}

func (p *SplayTree) Remove(key Key) error {
	p.root = p.splay(p.root, key)
	if p.root == nil || !p.root.key.Equals(key) {
		return errors.New("Such key doesn't exist")
	}
	p.root = p.join(p.split(key))
	return nil
}

// 427B-mic
package main

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

func main() {
    w := bufio.NewScanner(os.Stdin)
    w.Split(bufio.ScanWords)
    var n, t, c int
    fmt.Scan(&n, &t, &c)
    a := make([]int, n+2)
    for i := 1; i <= n; i++ {
        w.Scan()
        a[i], _ = strconv.Atoi(w.Text())
    }
    a[n+1] = t + 1
    dem := 0
    //max := 0
    kq := 0
    for i := 1; i <= n+1; i++ {
        if a[i] <= t {
            dem++
        } else {
            if dem >= c {
                kq += dem - c + 1
            }
            dem = 0
        }
    }
    fmt.Println(kq)
}

package main

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

func main() {
	var n, t, c int
	if _, err := fmt.Scanf("%d %d %d\n", &n, &t, &c); err != nil {
		return
	}

	plan := 0
	last_ok := -1

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; scanner.Scan() && i < n; i++ {
		a, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		if a > t {
			if last_ok >= 0 && i-last_ok >= c {
				plan += i - last_ok - c + 1
			}
			last_ok = -1
		} else {
			if last_ok < 0 {
				last_ok = i
			}
		}
	}
	if last_ok >= 0 {
		plan += n - last_ok - c + 1
	}

	fmt.Println(plan)
}

package main

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

func main() {
	var n, t, c int
	if _, err := fmt.Scanf("%d %d %d\n", &n, &t, &c); err != nil {
		return
	}

	plan := 0
	last_ok := 0

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; scanner.Scan() && i < n; i++ {
		a, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		if a > t {
			if i-last_ok >= c {
				plan += i - last_ok - c + 1
			}
			last_ok = -1
		} else {
			if last_ok < 0 {
				last_ok = i
			}
		}
	}
	if last_ok >= 0 {
		plan += n - last_ok - c + 1
	}

	fmt.Println(plan)
}


Michael and Joe are playing a game. The game is played on a grid with $$$n$$$ rows and $$$m$$$ columns, filled with distinct integers. We denote the square on the $$$i$$$-th ($$$1\le i\le n$$$) row and $$$j$$$-th ($$$1\le j\le m$$$) column by $$$(i, j)$$$ and the number there by $$$a_{ij}$$$.Michael starts by saying two numbers $$$h$$$ ($$$1\le h \le n$$$) and $$$w$$$ ($$$1\le w \le m$$$). Then Joe picks any $$$h\times w$$$ subrectangle of the board (without Michael seeing).Formally, an $$$h\times w$$$ subrectangle starts at some square $$$(a,b)$$$ where $$$1 \le a \le n-h+1$$$ and $$$1 \le b \le m-w+1$$$. It contains all squares $$$(i,j)$$$ for $$$a \le i \le a+h-1$$$ and $$$b \le j \le b+w-1$$$.    Possible move by Joe if Michael says $$$3\times 2$$$ (with maximum of $$$15$$$). Finally, Michael has to guess the maximum number in the subrectangle. He wins if he gets it right.Because Michael doesn't like big numbers, he wants the area of the chosen subrectangle (that is, $$$h \cdot w$$$), to be as small as possible, while still ensuring that he wins, not depending on Joe's choice. Help Michael out by finding this minimum possible area. It can be shown that Michael can always choose $$$h, w$$$ for which he can ensure that he wins.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 20$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 40$$$)  — the size of the grid. Each of the following $$$n$$$ lines contains $$$m$$$ integers. The $$$j$$$-th integer on the $$$i$$$-th line is $$$a_{ij}$$$ ($$$-10^9 \le a_{ij} \le 10^9$$$)  — the element in the cell $$$(i, j)$$$. It is guaranteed that all the numbers are distinct (that is, if $$$a_{i_1j_1} = a_{i_2j_2}$$$, then $$$i_1 = i_2, j_1 = j_2$$$).

For each test case print a single positive integer  — the minimum possible area the subrectangle can have while still ensuring that Michael can guarantee the victory.

NoteIn the first test case, the grid is $$$1\times 1$$$, so the only possible choice for $$$h, w$$$ is $$$h = 1, w = 1$$$, giving an area of $$$h\cdot w = 1$$$.The grid from the second test case is drawn in the statement. It can be shown that with $$$h = 3, w = 3$$$ Michael can guarantee the victory and that any choice with $$$h\cdot w \le 8$$$ doesn't.

package main

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

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

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

	var T int64
	fmt.Fscan(r, &T)
	for ; T > 0; T-- {
		var n, m int
		fmt.Fscan(r, &n, &m)
		var i, j int
		var iIdx, jIdx, val int
		maxVal := math.MinInt32
		for i = 0; i < n; i++ {
			for j = 0; j < m; j++ {
				fmt.Fscan(r, &val)
				if val > maxVal {
					iIdx, jIdx, maxVal = i, j, val
				}
			}
		}

		ans := (iIdx + 1) * (jIdx + 1)
		ans = max(ans, (iIdx+1)*(m-jIdx))
		ans = max(ans, (n-iIdx)*(m-jIdx))
		ans = max(ans, (n-iIdx)*(jIdx+1))
		fmt.Fprintln(w, ans)
	}

}

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

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

package main

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

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

	tc := readInt(in)
	for i := 0; i < tc; i++ {
		arr := readArrInt(in)
		n := arr[0]
		//m := arr[1]
		grid := make([][]int, n)
		for j := 0; j < n; j++ {
			grid[j] = readArrInt(in)
		}

		fmt.Println(solve(grid))
	}
	fmt.Scanln()
}

func solve(grid [][]int) int {
	max := math.MinInt
	maxi := 0
	maxj := 0
	for i := range grid {
		for j := range grid[i] {
			if grid[i][j] > max {
				max = grid[i][j]
				maxi = i
				maxj = j
			}
		}
	}

	sq1 := (maxi + 1) * (maxj + 1)
	sq2 := (maxi + 1) * (len(grid[0]) - maxj)
	sq3 := (len(grid) - maxi) * (maxj + 1)
	sq4 := (len(grid) - maxi) * (len(grid[0]) - maxj)

	res1 := maxint(sq1, sq2)
	res2 := maxint(sq3, sq4)

	return maxint(res1, res2)
}

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

	return b
}

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

package main

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

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

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

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

		g := int64(math.MinInt32)
		x, y := 0, 0
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				var v int64
				fmt.Fscan(in, &v)
				if v > g {
					g = v
					x, y = i, j
				}
			}
		}

		fmt.Fprintln(out, max(int64(x+1), int64(n-x))*max(int64(y+1), int64(m-y)))
	}
}

package main

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

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

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

func solve(n, m int, a [][]int) int {
	var maxx, maxy int
	var mx = -1000000001
	for i, r := range a {
		for j, x := range r {
			if x > mx {
				maxx = i
				maxy = j
				mx = x
			}
		}
	}
	return max(maxx+1, n-maxx) * max(maxy+1, m-maxy)
}

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

package main

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

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

func make2d[T any](n, m int) [][]T {
	mat := make([][]T, n)
	r := make([]T, n*m)
	for i, startRow := 0, 0; i < n; i, startRow = i+1, startRow+m {
		endRow := startRow + m
		mat[i] = r[startRow:endRow]
	}
	return mat
}

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

type pair struct {
	r int
	c int
}

func main() {
	defer out.Flush()
	x := pair{}

	var tc int
	fmt.Fscan(in, &tc)
	for ; tc > 0; tc-- {
		var n, m int
		fmt.Fscan(in, &n, &m)
		a := make2d[int](n, m)
		b := int(-2e9)
		for i := range a {
			for j := range a[i] {
				fmt.Fscan(in, &a[i][j])
				if a[i][j] > b {
					b = a[i][j]
					x.r = i + 1
					x.c = j + 1
				}
			}
		}
		fmt.Printf("%d\n", max(x.c, m-x.c+1)*max(x.r, n-x.r+1))
	} //fim do loop de test cases
} // fim main

package main

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

/*
1
4 4
2 12 6 10
3 15 16 4
1 13 8 11
14 7 9 5
-----

*/

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

	a := make([][]int, n)

	maxR, maxC := 0, 0
	maxVal := minInt

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

		for c := range a[r] {
			scanf("%d", &a[r][c])

			if a[r][c] > maxVal {
				maxVal = a[r][c]
				maxR, maxC = r, c
			}
		}
		scanf("\n")
	}

	maxR++
	maxC++

	ans := maxR * maxC
	ans = max(ans, (n-maxR+1)*(m-maxC+1))
	ans = max(ans, (n-maxR+1)*(maxC))
	ans = max(ans, (maxR)*(m-maxC+1))

	fmt.Fprintln(writer, ans)
}

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

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

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

const maxUint = ^uint(0)

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

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

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

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

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

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

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

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

	return minNumIdx
}

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

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

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

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

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

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

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

	return true
}

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

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

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

	num = abs(num)

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

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

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

	for ftr := int64(2); ftr*ftr <= num; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > 1 {
		factors = append(factors, num)
	}

	return factors
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1695A
Date: 9/16/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1695/A
Problem: CF1695A
**/
func (in *CF1695A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, m := in.NextInt(), in.NextInt()
		max := -1000000001
		x, y := -1, -1
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				val := in.NextInt()
				if val > max {
					max = val
					x = i
					y = j
				}
			}
		}

		fmt.Println(maxi(n-x, x+1) * maxi(m-y, y+1))
	}
}

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

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

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

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func gf() float64 { f,e := strconv.ParseFloat(gs(),64); if e != nil {panic(e)}; return f }
func gbs() []byte { return []byte(gs()) }
func gfs(n int) []float64  { res := make([]float64,n); for i:=0;i<n;i++ { res[i] = gf() }; return res }
func gss(n int) []string  { res := make([]string,n); for i:=0;i<n;i++ { res[i] = gs() }; return res }
func gi64() int64     { i,e := strconv.ParseInt(gs(),10,64); if e != nil {panic(e)}; return i }
func gis64(n int) []int64  { res := make([]int64,n); for i:=0;i<n;i++ { res[i] = gi64() }; return res }

func ia(m int) []int { return make([]int,m) }
func iai(m int,v int) []int { a := make([]int,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill2(m int) ([]int,[]int) { a,b := ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func fill3(m int) ([]int,[]int,[]int) { a,b,c := ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi(),gi(),gi()}; return a,b,c }
func fill4(m int) ([]int,[]int,[]int,[]int) { a,b,c,d := ia(m),ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi(),gi(),gi(),gi()}; return a,b,c,d }
func abs(a int) int { if a < 0 { return -a }; return a }
func rev(a []int) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max(a,b int) int { if a > b { return a }; return b }
func min(a,b int) int { if a > b { return b }; return a }
func maxarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr(a []int) int { ans := 0; for _,aa := range(a) { ans += aa }; return ans }
func zeroarr(a []int) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod(a,e,mod int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint(a,e int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd(a,b int) int { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended(a,b int) (int,int,int) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv(a,m int) (int,bool) { g,x,_ := gcdExtended(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func vecintstring(a []int) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.Itoa(a) }; return strings.Join(astr," ") }
func makefact(n int,mod int) ([]int,[]int) {
	fact,factinv := make([]int,n+1),make([]int,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * i % mod }
	factinv[n] = powmod(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * (i+1) % mod }
	return fact,factinv
}

func ia64(m int) []int64 { return make([]int64,m) }
func iai64(m int,v int64) []int64 { a := make([]int64,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi64(n int,m int,v int64) [][]int64 {
	r := make([][]int64,n); for i:=0;i<n;i++ { x := make([]int64,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill264(m int) ([]int64,[]int64) { a,b := ia64(m),ia64(m); for i:=0;i<m;i++ {a[i],b[i] = gi64(),gi64()}; return a,b }
func fill364(m int) ([]int64,[]int64,[]int64) { a,b,c := ia64(m),ia64(m),ia64(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi64(),gi64(),gi64()}; return a,b,c }
func fill464(m int) ([]int64,[]int64,[]int64,[]int64) { a,b,c,d := ia64(m),ia64(m),ia64(m),ia64(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi64(),gi64(),gi64(),gi64()}; return a,b,c,d }
func abs64(a int64) int64 { if a < 0 { return -a }; return a }
func rev64(a []int64) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max64(a,b int64) int64 { if a > b { return a }; return b }
func min64(a,b int64) int64 { if a > b { return b }; return a }
func maxarr64(a []int64) int64 { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr64(a []int64) int64 { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr64(a []int64) int64 { ans := int64(0); for _,aa := range(a) { ans += aa }; return ans }
func zeroarr64(a []int64) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod64(a,e,mod int64) int64 { res, m := int64(1), a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint64(a,e int64) int64 { res, m := int64(1), a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd64(a,b int64) int64 { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended64(a,b int64) (int64,int64,int64) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended64(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv64(a,m int64) (int64,bool) { g,x,_ := gcdExtended64(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func vecint64string(a []int64) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.FormatInt(a,10) }; return strings.Join(astr," ") }
func makefact64(n int,mod int64) ([]int64,[]int64) {
	fact,factinv := make([]int64,n+1),make([]int64,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * int64(i) % mod }
	factinv[n] = powmod64(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * int64(i+1) % mod }
	return fact,factinv
}
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi();
	for tt:=1;tt<=T;tt++ {
		N,M := gi(),gi(); gr := make([][]int,N); for i:=0;i<N;i++ { gr[i] = gis(M); }
		m,mi,mj := -1000000001,-1,-1
		for i:=0;i<N;i++ { for j:=0;j<M;j++ { if gr[i][j] > m { m,mi,mj = gr[i][j],i,j } } }
		mi = max(mi,N-1-mi); mj = max(mj,M-1-mj)
		fmt.Fprintln(wrtr,(mi+1)*(mj+1))
	}
}

package main

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

var _, _, _ = main, rw, do

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

func rw(r io.Reader, w io.Writer) {
	var t int
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n, m int
		fmt.Fscan(r, &n, &m)
		a := make([][]int, n)
		for i := 0; i < n; i++ {
			a[i] = make([]int, m)
			for j := 0; j < m; j++ {
				fmt.Fscan(r, &a[i][j])
			}
		}
		w1 := do(a, n, m)
		fmt.Fprintln(w, w1)
	}
}

func do(a [][]int, n, m int) int {
	b := a[0][0]
	var x, y int
	for i, v := range a {
		for j, w := range v {
			if b < w {
				b = w
				x, y = i, j
			}
		}
	}
	w := max(x+1, n-x)
	h := max(y+1, m-y)
	return h * w
}

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

package main

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

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

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

	var T int64
	fmt.Fscan(r, &T)
	for ; T > 0; T-- {
		var n, m int
		fmt.Fscan(r, &n, &m)
		var i, j, maxVal int
		var iIdx, jIdx, val int
		for i = 0; i < n; i++ {
			for j = 0; j < m; j++ {
				fmt.Fscan(r, &val)
				if val > maxVal {
					iIdx, jIdx, maxVal = i, j, val
				}
			}
		}

		ans := (iIdx + 1) * (jIdx + 1)
		ans = max(ans, (iIdx+1)*(m-jIdx))
		ans = max(ans, (n-iIdx)*(m-jIdx))
		ans = max(ans, (n-iIdx)*(jIdx+1))
		fmt.Fprintln(w, ans)
	}

}

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

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

package main

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

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

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

	var T int64
	fmt.Fscan(r, &T)
	for ; T > 0; T-- {
		var n, m int
		fmt.Fscan(r, &n, &m)
		var i, j, maxVal int
		var iIdx, jIdx, val int
		for i = 0; i < n; i++ {
			for j = 0; j < m; j++ {
				fmt.Fscan(r, &val)
				if val > maxVal {
					iIdx, jIdx, maxVal = i, j, val
				}
			}
		}

		ans := (iIdx + 1) * (jIdx + 1)
		ans = max(ans, (iIdx+1)*(m-jIdx))
		ans = max(ans, (n-iIdx)*(m-jIdx))
		ans = max(ans, (n-iIdx)*(jIdx+1))
		fmt.Println(w, ans)
	}

}

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

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

package main

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

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

	tc := readInt(in)
	for i := 0; i < tc; i++ {
		arr := readArrInt(in)
		n := arr[0]
		//m := arr[1]
		grid := make([][]int, n)
		for j := 0; j < n; j++ {
			grid[j] = readArrInt(in)
		}

		if tc == 20 && arr[0] == arr[1] && arr[0] == 10 {
			for _, r := range grid {
				for _, s := range r {
					fmt.Println(s)
				}
			}
		}
		fmt.Println(solve(grid))
	}
}

func solve(grid [][]int) int {
	max := 0
	maxi := 0
	maxj := 0
	for i := range grid {
		for j := range grid[i] {
			if grid[i][j] > max {
				max = grid[i][j]
				maxi = i
				maxj = j
			}
		}
	}

	sq1 := (maxi + 1) * (maxj + 1)
	sq2 := (maxi + 1) * (len(grid[0]) - maxj)
	sq3 := (len(grid) - maxi) * (maxj + 1)
	sq4 := (len(grid) - maxi) * (len(grid[0]) - maxj)

	res1 := maxint(sq1, sq2)
	res2 := maxint(sq3, sq4)

	return maxint(res1, res2)
}

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

	return b
}

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

package main

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

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

	tc := readInt(in)
	for i := 0; i < tc; i++ {
		arr := readArrInt(in)
		n := arr[0]
		//m := arr[1]
		grid := make([][]int, n)
		for j := 0; j < n; j++ {
			grid[j] = readArrInt(in)
		}

		fmt.Println(solve(grid))
	}
}

func solve(grid [][]int) int {
	max := 0
	maxi := 0
	maxj := 0
	for i := range grid {
		for j := range grid[i] {
			if grid[i][j] > max {
				max = grid[i][j]
				maxi = i
				maxj = j
			}
		}
	}

	sq1 := (maxi + 1) * (maxj + 1)
	sq2 := (maxi + 1) * (len(grid[0]) - maxj)
	sq3 := (len(grid) - maxi) * (maxj + 1)
	sq4 := (len(grid) - maxi) * (len(grid[0]) - maxj)

	res1 := maxint(sq1, sq2)
	res2 := maxint(sq3, sq4)

	return maxint(res1, res2)
}

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

	return b
}

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

package main

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

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

	tc := readInt(in)
	for i := 0; i < tc; i++ {
		arr := readArrInt(in)
		n := arr[0]
		//m := arr[1]
		grid := make([][]int, n)
		for j := 0; j < n; j++ {
			grid[j] = readArrInt(in)
		}

		fmt.Println(solve(grid))
	}
}

func solve(grid [][]int) int {
	max := 0
	maxi := 0
	maxj := 0
	for i := range grid {
		for j := range grid[i] {
			if grid[i][j] > max {
				max = grid[i][j]
				maxi = i
				maxj = j
			}
		}
	}

	resi, resj := 0, 0

	resi = maxint(maxi+1, len(grid)-maxi)
	resj = maxint(maxj+1, len(grid[0])-maxj)

	return resi * resj
}

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

	return b
}

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

package main

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

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

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

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

		g := int64(0)
		x, y := 0, 0
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				var v int64
				fmt.Fscan(in, &v)
				if v > g {
					g = v
					x, y = i, j
				}
			}
		}

		fmt.Fprintln(out, max(int64(x+1), int64(n-x))*max(int64(y+1), int64(m-y)))
	}
}

package main

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

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

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

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

		g := 0
		x, y := 0, 0
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				var v int
				fmt.Fscan(in, &v)
				if v > g {
					g = v
					x, y = i, j
				}
			}
		}

		fmt.Fprintln(out, max(x+1, n-x)*max(y+1, m-y))
	}
}

package main

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

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

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

func solve(n, m int, a [][]int) int {
	var mx, maxx, maxy int
	for i, r := range a {
		for j, x := range r {
			if x > mx {
				maxx = i
				maxy = j
				mx = x
			}
		}
	}
	return max(maxx+1, n-maxx) * max(maxy+1, m-maxy)
}

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

package main

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

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

func make2d[T any](n, m int) [][]T {
	mat := make([][]T, n)
	r := make([]T, n*m)
	for i, startRow := 0, 0; i < n; i, startRow = i+1, startRow+m {
		endRow := startRow + m
		mat[i] = r[startRow:endRow]
	}
	return mat
}

type pair struct {
	r int
	c int
}

func main() {
	defer out.Flush()
	x := pair{}

	var tc int
	fmt.Fscan(in, &tc)
	for ; tc > 0; tc-- {
		var n, m int
		fmt.Fscan(in, &n, &m)
		a := make2d[int](n, m)
		b := int(-2e9)
		for i := range a {
			for j := range a[i] {
				fmt.Fscan(in, &a[i][j])
				if a[i][j] > b {
					b = a[i][j]
					x.r = i
					x.c = j
				}
			}
		}
		fmt.Printf("%d\n", (m/2+1)*(n/2+1))
	} //fim do loop de test cases
} // fim main

package main

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

/*
1
4 4
2 12 6 10
3 15 16 4
1 13 8 11
14 7 9 5
*/

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

	a := make([][]int, n)

	maxR, maxC := 0, 0
	maxVal := minInt

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

		for c := range a[r] {
			scanf("%d", &a[r][c])

			if a[r][c] > maxVal {
				maxVal = a[r][c]
				maxR, maxC = r, c
			}
		}
		scanf("\n")
	}

	maxR++
	maxC++

	ans := maxR * maxC
	ans = max(ans, (n-maxR+1)*(m-maxC+1))
	ans = max(ans, (n-maxR+1)*(maxC))
	ans = max(ans, (maxR)*(m-maxC))

	fmt.Fprintln(writer, ans)
}

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

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

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

const maxUint = ^uint(0)

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

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

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

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

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

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

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

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

	return minNumIdx
}

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

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

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

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

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

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

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

	return true
}

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

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

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

	num = abs(num)

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

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

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

	for ftr := int64(2); ftr*ftr <= num; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > 1 {
		factors = append(factors, num)
	}

	return factors
}


Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.

The first line of the input contains two positive integers, n and m — the number of the cities and the number of roads in Berland (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000).  Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers xi, yi (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the numbers of the cities connected by the i-th road. It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.

Print a single integer — the minimum number of separated cities after the reform.

NoteIn the first sample the following road orientation is allowed: , , .The second sample: , , , , .The third sample: , , , , .

package main

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

const debug = false

var in = bufio.NewReader(os.Stdin)

func init() {
	if debug {
		log.SetFlags(log.LstdFlags | log.Lshortfile)
	}
}

const N = 1e5

var adj [N + 1][]int32
var v [N + 1]bool

func dfs(i, p int32) bool {
	if v[i] {
		return true
	}
	v[i] = true
	var cycle bool
	for _, j := range adj[i] {
		if j != p && dfs(j, i) {
			cycle = true
		}
	}
	if debug {
		log.Println(i, p, cycle)
	}
	return cycle
}

func main() {
	var n, m int32
	fmt.Fscan(in, &n, &m)
	for ; m > 0; m-- {
		var x, y int32
		fmt.Fscan(in, &x, &y)
		adj[x] = append(adj[x], y)
		adj[y] = append(adj[y], x)
	}
	ans := 0
	for i := int32(1); i <= n; i++ {
		if !v[i] && !dfs(i, 0) {
			ans++
		}
	}
	fmt.Println(ans)
}

// Codeforces 659 E graphs
package main

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

var n,m int
// Vert vertice of graph.
type Vert struct {
    group int
    adj []int
}
var va []Vert

func addEdge(a,b int) {
    va[a].adj = append(va[a].adj, b)
    va[b].adj = append(va[b].adj, a)
}

// find group elements using dfs
func fillGroup(gr int, i int) {
    va[i].group = gr
    for ij:=0; ij<len(va[i].adj); ij++ {
        j := va[i].adj[ij]
        if va[j].group==0 {
            fillGroup(gr,j)
        }
    }
}

func main() {
	// reading n,m
 	in := bufio.NewReader(os.Stdin)
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		return
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	n, err = strconv.Atoi(ss[0])
	if err != nil {
		fmt.Println("n atoi failure", err)
		return
	}
	m, err = strconv.Atoi(ss[1])
	if err != nil {
		fmt.Println("m atoi failure", err)
		return
	}
    // reading adj
    va = make([]Vert, n)
    for i:=0; i<m; i++ {
        s, err = in.ReadString('\n')
        if err != nil {
            fmt.Println("read failure", err)
            return
        }
	    ss = strings.Split(strings.Trim(s, " \n\r"), " ")
        var a,b int
        a, err = strconv.Atoi(ss[0])
        if err != nil {
            fmt.Println("n atoi failure", err)
            return
        }
        b, err = strconv.Atoi(ss[1])
        if err != nil {
            fmt.Println("m atoi failure", err)
            return
        }
        addEdge(a-1, b-1)
    }

    // looking for connected groups
    ngroup := 0
    for i:=0; i<len(va); i++ {
        if va[i].group==0 {
            ngroup++
            fillGroup(ngroup,i)
        }
    }
    ngroup++
    // counting vert nad edg in groups
    nvert := make([]int, ngroup)
    nedge := make([]int, ngroup)
    for i:=0; i<n; i++ {
        gr := va[i].group
        nvert[gr]++
        nedge[gr] += len(va[i].adj)
    }
    // if ne = ng-1, sep++, otherwise sep same
    nsep := 0
    for i:=1; i<ngroup; i++ {
        // fmt.Printf("nv,ne = %d %d\n", nvert[i],nedge[i])
        if 2*(nvert[i]-1) == nedge[i] {
            nsep++
        } else if 2*(nvert[i]-1) < nedge[i] {
            // has cycle
        } else {
            panic("wrong cyles number")
        }
    }

    // print result
    fmt.Println(nsep)
}


$$$n$$$ towns are arranged in a circle sequentially. The towns are numbered from $$$1$$$ to $$$n$$$ in clockwise order. In the $$$i$$$-th town, there lives a singer with a repertoire of $$$a_i$$$ minutes for each $$$i \in [1, n]$$$.Each singer visited all $$$n$$$ towns in clockwise order, starting with the town he lives in, and gave exactly one concert in each town. In addition, in each town, the $$$i$$$-th singer got inspired and came up with a song that lasts $$$a_i$$$ minutes. The song was added to his repertoire so that he could perform it in the rest of the cities.Hence, for the $$$i$$$-th singer, the concert in the $$$i$$$-th town will last $$$a_i$$$ minutes, in the $$$(i + 1)$$$-th town the concert will last $$$2 \cdot a_i$$$ minutes, ..., in the $$$((i + k) \bmod n + 1)$$$-th town the duration of the concert will be $$$(k + 2) \cdot a_i$$$, ..., in the town $$$((i + n - 2) \bmod n + 1)$$$ — $$$n \cdot a_i$$$ minutes.You are given an array of $$$b$$$ integer numbers, where $$$b_i$$$ is the total duration of concerts in the $$$i$$$-th town. Reconstruct any correct sequence of positive integers $$$a$$$ or say that it is impossible.

The first line contains one integer $$$t$$$ $$$(1 \le t \le 10^3$$$) — the number of test cases. Then the test cases follow. Each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \le n \le 4 \cdot 10^4$$$) — the number of cities. The second line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 10^{9}$$$) — the total duration of concerts in $$$i$$$-th city. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print the answer as follows: If there is no suitable sequence $$$a$$$, print NO. Otherwise, on the first line print YES, on the next line print the sequence $$$a_1, a_2, \dots, a_n$$$ of $$$n$$$ integers, where $$$a_i$$$ ($$$1 \le a_i \le 10^{9}$$$) is the initial duration of repertoire of the $$$i$$$-th singer. If there are multiple answers, print any of them.

NoteLet's consider the $$$1$$$-st test case of the example:  the $$$1$$$-st singer in the $$$1$$$-st city will give a concert for $$$3$$$ minutes, in the $$$2$$$-nd — for $$$6$$$ minutes, in the $$$3$$$-rd — for $$$9$$$ minutes;  the $$$2$$$-nd singer in the $$$1$$$-st city will give a concert for $$$3$$$ minutes, in the $$$2$$$-nd — for $$$1$$$ minute, in the $$$3$$$-rd - for $$$2$$$ minutes;  the $$$3$$$-rd singer in the $$$1$$$-st city will give a concert for $$$6$$$ minutes, in the $$$2$$$-nd — for $$$9$$$ minutes, in the $$$3$$$-rd — for $$$3$$$ minutes.

package main

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

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

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

	var c int
	var n int64
	Fscan(in, &c)
	for i := 1; i <= c; i++ {
		Fscan(in, &n)
		a := make([]int64, n+1)
		b := make([]int64, n+1)
		var sum_b int64
		for i := range b {
			if i > 0 {
				Fscan(in, &b[i])
				sum_b += b[i]
			}
		}
		var sum_gao int64 = (1 + n) * n / 2
		var tmp int64
		if sum_b%sum_gao != 0 {
			Fprintln(out, "NO")
			continue
		}
		var sum_a int64 = sum_b / sum_gao
		//差分求解
		for i := n; i > 1; i-- {
			if k := (sum_a - b[i] + b[i-1]) / n; (sum_a-b[i]+b[i-1])%n != 0 || k <= 0 {
				Fprintf(out, "NO\n")
				goto h1
			} else {
				a[i] = k
				tmp += k
			}
		}
		a[1] = sum_a - tmp
		if a[1] <= 0 {
			Fprintln(out, "NO")
			continue
		}
		Fprintln(out, "YES")
		for i, v := range a {
			if i > 0 {
				Fprintf(out, "%d%s", v, " ")
			}
			if i == int(n) {
				Fprintf(out, "\n")
			}
		}
	h1:
	}
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n, p := 0, 0, int64(0)
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o, sum := []int64{}, int64(0)
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &p)
			o = append(o, p)
			sum += p
		}
		tot, ans := (n*(n+1))/2, make([]int64, n)
		asum, check, not_ok := sum/int64(tot), int64(0), false
		if sum%int64(tot) != 0 {
			not_ok = true
		}
		for j := 0; j < n; j++ {
			ans[(j+1)%n] = (asum + o[j] - o[(j+1)%n]) / int64(n)
			check += ans[(j+1)%n]
			if ans[(j+1)%n] <= 0 {
				not_ok = true
			}
		}
		if not_ok || check != asum {
			fmt.Fprintln(w, "NO")
		} else {
			fmt.Fprintln(w, "YES")
			for _, v := range ans {
				fmt.Fprint(w, v, " ")
			}
			fmt.Fprintln(w, "")
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

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

	var T, n int
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		b := make([]int64, n)
		s := int64(0)
		for i := range b {
			Fscan(in, &b[i])
			s += b[i]
		}
		d := int64(n) * int64(n+1) / 2
		if s%d > 0 {
			Fprintln(out, "NO")
			continue
		}
		s /= d
		a := make([]interface{}, n)
		for i, v := range b {
			s := s + b[(i+n-1)%n] - v
			if s <= 0 || s%int64(n) > 0 {
				Fprintln(out, "NO")
				continue o
			}
			a[i] = s / int64(n)
		}
		Fprintln(out, "YES")
		Fprintln(out, a...)
	}
}

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

package main

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

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

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

	var c int
	var n int
	Fscan(in, &c)
	for i := 1; i <= c; i++ {
		Fscan(in, &n)
		a := make([]int, n+1)
		b := make([]int, n+1)
		var sum_b int
		for i := range b {
			if i > 0 {
				Fscan(in, &b[i])
				sum_b += b[i]
			}
		}
		var sum_gao int = (1 + n) * n / 2
		var tmp int
		if sum_b % sum_gao != 0 {
			Fprintln(out, "NO")
			continue
		}
		var sum_a int = sum_b / sum_gao
		//差分求解
		for i := n; i > 1; i-- {
			if k := (sum_a - b[i] + b[i-1]) / n; (sum_a-b[i]+b[i-1])%n != 0 || k <= 0 {
				Fprintf(out, "NO\n")
				goto h1
			} else {
				a[i] = k
				tmp += k
			}
		}
		a[1] = sum_a - tmp
		if a[1] <= 0 {
			Fprintln(out, "NO")
			continue
		}
		Fprintln(out, "YES")
		for i, v := range a {
			if i > 0 {
				Fprintf(out, "%d%s", v, " ")
			}
			if i == n {
				Fprintf(out, "\n")
			}
		}
	h1:
	}
}

package main

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

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

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

	var c int
	var n int
	Fscan(in, &c)
	for i := 1; i <= c; i++ {
		Fscan(in, &n)
		a := make([]int, n+1)
		b := make([]int, n+1)
		var sum_b int
		for i := range b {
			if i > 0 {
				Fscan(in, &b[i])
				sum_b += b[i]
			}
		}
		var sum_gao int = (1 + n) * n / 2
		var sum_a int = sum_b / sum_gao
		var tmp int
		//差分求解
		for i := n; i > 1; i-- {
			if k := (sum_a - b[i] + b[i-1]) / n; (sum_a-b[i]+b[i-1])%n != 0 || k <= 0 {
				Fprintf(out, "NO\n")
				goto h1
			} else {
				a[i] = k
				tmp += k
			}
		}
		a[1] = sum_a - tmp
		if a[1] <= 0 {
			Fprintln(out, "NO")
			goto h1
		}
		Fprintln(out, "YES")
		for i, v := range a {
			if i > 0 {
				Fprintf(out, "%d%s", v, " ")
			}
			if i == n {
				Fprintf(out, "\n")
			}
		}
	h1:
	}
}

package main

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

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

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

	var c int
	var n int64
	Fscan(in, &c)
	for i := 1; i <= c; i++ {
		Fscan(in, &n)
		a := make([]int64, n+1)
		b := make([]int64, n+1)
		var sum_b int64
		for i := range b {
			if i > 0 {
				Fscan(in, &b[i])
				sum_b += b[i]
			}
		}
		var sum_gao int64 = (1 + n) * n / 2
		var sum_a int64 = sum_b / sum_gao
		var tmp int64
		if i == 12 || i == 13 || i== 14{
			Println(b)
		}
		//差分求解
		for i := n; i > 1; i-- {
			if k := (sum_a - b[i] + b[i-1]) / n; (sum_a-b[i]+b[i-1])%n != 0 || k <= 0 {
				Fprintf(out, "NO\n")
				goto h1
			} else {
				a[i] = k
				tmp += k
			}
		}
		a[1] = sum_a - tmp
		if a[1] <= 0 {
			Fprintln(out, "NO")
			goto h1
		}
		Fprintln(out, "YES")
		for i, v := range a {
			if i > 0 {
				Fprintf(out, "%d%s", v, " ")
			}
			if i == int(n) {
				Fprintf(out, "\n")
			}
		}
	h1:
	}
}

package main

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

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

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

	var c int
	var n int64
	Fscan(in, &c)
	for i := 1; i <= c; i++ {
		Fscan(in, &n)
		a := make([]int64, n+1)
		b := make([]int64, n+1)
		var sum_b int64
		for i := range b {
			if i > 0 {
				Fscan(in, &b[i])
				sum_b += b[i]
			}
		}
		var sum_gao int64 = (1 + n) * n / 2
		var sum_a int64 = sum_b / sum_gao
		var tmp int64
		//差分求解
		for i := n; i > 1; i-- {
			if k := (sum_a - b[i] + b[i-1]) / n; (sum_a - b[i] + b[i-1]) % n != 0 || k <= 0 {
				Fprintf(out, "NO\n")
				goto h1
			} else {
				a[i] = k
				tmp += k
			}
		}
		a[1] = sum_a - tmp
		if a[1] <= 0 {
			Fprintf(out, "NO\n")
			continue
		}
		Fprintf(out, "YES\n")
		for i, v := range a {
			if i > 0 {
				Fprintf(out, "%d%s", v, " ")
			}
			if i == int(n) {
				Fprintf(out, "\n")
			}
		}
	h1:
	}
}

package main

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

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

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

	var c int
	var n int64
	Fscan(in, &c)
	for i := 1; i <= c; i++ {
		Fscan(in, &n)
		a := make([]int64, n+1)
		b := make([]int64, n+1)
		var sum_b int64
		for i := range b {
			if i > 0 {
				Fscan(in, &b[i])
				sum_b += b[i]
			}
		}
		var sum_gao int64 = (1 + n) * n / 2
		var sum_a int64 = sum_b / sum_gao
		var tmp int64
		//差分求解
		for i := n; i > 1; i-- {
			if k := (b[i] - b[i-1] - sum_a) / (-n); (b[i]-b[i-1]-sum_a)%(-n) != 0 || k <= 0 {
				Fprintf(out, "NO\n")
				goto h1
			} else {
				a[i] = k
				tmp += k
			}
		}
		a[1] = sum_a - tmp
		if a[1] <= 0 {
			Fprintf(out, "NO\n")
			continue
		}
		Fprintf(out, "YES\n")
		for i, v := range a {
			if i > 0 {
				Fprintf(out, "%d%s", v, " ")
			}
			if i == int(n) {
				Fprintf(out, "\n")
			}
		}
	h1:
	}
}

package main

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

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

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

	var c int
	var n int64
	Fscan(in, &c)
	for i := 1; i <= c; i++ {
		Fscan(in, &n)
		a := make([]int64, n+1)
		b := make([]int64, n+1)
		var sum_b int64
		for i := range b {
			if i > 0 {
				Fscan(in, &b[i])
				sum_b += b[i]
			}
		}
		if i == 14 {
			Println(b)
		}
		var sum_gao int64 = (1 + n) * n / 2
		var sum_a int64 = sum_b / sum_gao
		var tmp int64
		//差分求解
		for i := n; i > 1; i-- {
			if k := (b[i] - b[i-1] - sum_a) / (-n); (b[i]-b[i-1]-sum_a)%(-n) != 0 || k <= 0 {
				Fprintf(out, "NO\n")
				goto h1
			} else {
				a[i] = k
				tmp += k
			}
		}
		a[1] = sum_a - tmp
		if a[1] <= 0 {
			Fprintf(out, "NO\n")
			continue
		}
		Fprintf(out, "YES\n")
		for i, v := range a {
			if i > 0 {
				Fprintf(out, "%d%s", v, " ")
			}
		}
		Fprintf(out, "\n")
	h1:
	}
}

package main

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

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

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

	var c int
	var n int64
	Fscan(in, &c)
	for i := 1; i <= c; i++ {
		Fscan(in, &n)
		a := make([]int64, n+1)
		b := make([]int64, n+1)
		var sum_b int64
		for i := range b {
			if i > 0 {
				Fscan(in, &b[i])
				sum_b += b[i]
			}
		}
		var sum_gao int64 = (1 + n) * n / 2
		var sum_a int64 = sum_b / sum_gao
		var tmp int64
		//差分求解
		for i := n; i > 1; i-- {
			if k := (b[i] - b[i-1] - sum_a) / (-n); (b[i]-b[i-1]-sum_a)%(-n) != 0 || k <= 0 {
				Fprintf(out, "NO\n")
				goto h1
			} else {
				a[i] = k
				tmp += k
			}
		}
		a[1] = sum_a - tmp
		if a[1] <= 0 {
			Fprintf(out, "NO\n")
			continue
		}
		Fprintf(out, "YES\n")
		for i, v := range a {
			if i > 0 {
				Fprintf(out, "%d%s", v, " ")
			}
		}
		Fprintf(out, "\n")
	h1:
	}
}

package main

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

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

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

	var c int
	var n int64
	Fscan(in, &c)
	for i := 1; i <= c; i++ {
		Fscan(in, &n)
		a := make([]int64, n+1)
		b := make([]int64, n+1)
		var sum_b int64
		for i := range b {
			if i > 0 {
				Fscan(in, &b[i])
				sum_b += b[i]
			}
		}
		if i == 13 {
			Println(b)
		}
		var sum_gao int64 = (1 + n) * n / 2
		var sum_a int64 = sum_b / sum_gao
		var tmp int64
		//差分求解
		for i := n; i > 1; i-- {
			if k := (b[i] - b[i-1] - sum_a) / (-n); (b[i]-b[i-1]-sum_a)%(-n) != 0 || k <= 0 {
				Fprintf(out, "NO\n")
				goto h1
			} else {
				a[i] = k
				tmp += k
			}
		}
		a[1] = sum_a - tmp
		if a[1] <= 0 {
			Fprintf(out, "NO\n")
			continue
		}
		Fprintf(out, "YES\n")
		for i, v := range a {
			if i > 0 {
				Fprintf(out, "%d%s", v, " ")
			}
		}
		Fprintf(out, "\n")
	h1:
	}
}

package main

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

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

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

	var c int
	var n int64
	Fscan(in, &c)
	for i := 0; i < c; i++ {
		Fscan(in, &n)
		a := make([]int64, n+1)
		b := make([]int64, n+1)
		var sum_b int64
		for i := range b {
			if i > 0 {
				Fscan(in, &b[i])
				sum_b += b[i]
			}
		}
		var sum_gao int64 = (1 + n) * n / 2
		var sum_a int64 = sum_b / sum_gao
		var tmp int64
		//差分求解
		for i := n; i > 1; i-- {
			if k := (b[i] - b[i-1] - sum_a) / (-n); (b[i]-b[i-1]-sum_a)%(-n) != 0 || k <= 0 {
				Fprintf(out, "NO\n")
				goto h1
			} else {
				a[i] = k
				tmp += k
			}
		}
		a[1] = sum_a - tmp
		if a[1] <= 0 {
			Fprintf(out, "NO\n")
			continue
		}
		Fprintf(out, "YES\n")
		for i, v := range a {
			if i > 0 {
				Fprintf(out, "%d%s", v, " ")
			}
		}
		Fprintf(out, "\n")
	h1:
	}
}

package main

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

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

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

	var c int
	var n int64
	Fscan(in, &c)
	for i := 0; i < c; i++ {
		Fscan(in, &n)
		a := make([]int64, n+1)
		b := make([]int64, n+1)
		var sum_b int64
		for i := range b {
			if i > 0 {
				Fscan(in, &b[i])
				sum_b += b[i]
			}
		}
		if i == 12 {Println(b)}
		var sum_gao int64 = (1 + n) * n / 2
		var sum_a int64 = sum_b / sum_gao
		var tmp int64
		//差分求解
		for i := n; i > 1; i-- {
			if k := (b[i] - b[i-1] - sum_a) / (-n); (b[i]-b[i-1]-sum_a)%(-n) != 0 || k <= 0 {
				Fprintf(out, "NO\n")
				goto h1
			} else {
				a[i] = k
				tmp += k
			}
		}
		a[1] = sum_a - tmp
		if a[1] <= 0 {
			Fprintf(out, "NO\n")
			continue
		}
		Fprintf(out, "YES\n")
		for i, v := range a {
			if i > 0 {
				Fprintf(out, "%d%s", v, " ")
			}
		}
		Fprintf(out, "\n")
	h1:
	}
}

package main

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

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

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

	var c int64
	var n int64
	Fscan(in, &c)
	for ; c > 0; c-- {
		Fscan(in, &n)
		a := make([]int64, n+1)
		b := make([]int64, n+1)
		var sum_b int64
		for i := range b {
			if i > 0 {
				Fscan(in, &b[i])
				sum_b += b[i]
			}
		}
		var sum_gao int64 = (1 + n) * n / 2
		var sum_a int64 = sum_b / sum_gao
		var tmp int64
		//差分求解
		for i := n; i > 1; i-- {
			if k := (b[i] - b[i-1] - sum_a) / (-n); (b[i]-b[i-1]-sum_a)%(-n) != 0 || k <= 0 {
				Fprintf(out, "NO\n")
				goto h1
			} else {
				a[i] = k
				tmp += k
			}
		}
		a[1] = sum_a - tmp
		if a[1] <= 0 {
			Fprintf(out, "NO\n")
			continue
		}
		Fprintf(out, "YES\n")
		for i, v := range a {
			if i > 0 {
				Fprintf(out, "%d%s", v, " ")
			}
		}
		Fprintf(out, "\n")
	h1:
	}
}

package main

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

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

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

	var c int64
	var n int64
	Fscan(in, &c)
	for ; c > 0; c-- {
		Fscan(in, &n)
		a := make([]int64, n+1)
		b := make([]int64, n+1)
		var sum_b int64
		for i := range b {
			if i > 0 {
				Fscan(in, &b[i])
				sum_b += b[i]
			}
		}
		var sum_gao int64 = (1 + n) * n / 2
		var sum_a int64 = sum_b / sum_gao
		var tmp int64
		//差分求解
		for i := n; i > 1; i-- {
			if k := (b[i] - b[i-1] - sum_a) / (-n); (b[i]-b[i-1]-sum_a)%(-n) != 0 || k == 0 {
				Fprintf(out, "NO\n")
				goto h1
			} else {
				a[i] = k
				tmp += k
			}
		}
		a[1] = sum_a - tmp
		Fprintf(out, "YES\n")
		for i, v := range a {
			if i > 0 {
				Fprintf(out, "%d%s", v, " ")
			}
		}
		Fprintf(out, "\n")
	h1:
	}
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n, p := 0, 0, int64(0)
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o, sum := []int64{}, int64(0)
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &p)
			o = append(o, p)
			sum += p
		}
		tot, ans := (n*(n+1))/2, make([]int64, n)
		asum, check, not_ok := sum/int64(tot), int64(0), false
		for j := 0; j < n; j++ {
			ans[(j+1)%n] = (asum + o[j] - o[(j+1)%n]) / int64(n)
			check += ans[(j+1)%n]
			if ans[(j+1)%n] <= 0 {
				not_ok = true
			}
		}
		if not_ok || check != asum {
			fmt.Fprintln(w, "NO")
		} else {
			fmt.Fprintln(w, "YES")
			for _, v := range ans {
				fmt.Fprint(w, v, " ")
			}
			fmt.Fprintln(w, "")
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n, p := 0, 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o, sum := []int{}, 0
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &p)
			o = append(o, p)
			sum += p
		}
		tot, ans := (n*(n+1))/2, make([]int, n)
		asum, not_ok := sum/tot, false
		for j := 0; j < n; j++ {
			ans[(j+1)%n] = (asum + o[j] - o[(j+1)%n]) / n
			if ans[(j+1)%n] <= 0 {
				not_ok = true
			}
		}
		if not_ok {
			fmt.Fprintln(w, "NO")
		} else {
			fmt.Fprintln(w, "YES")
			for _, v := range ans {
				fmt.Fprint(w, v, " ")
			}
			fmt.Fprintln(w, "")
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n, p := 0, 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n)
		o, sum := []int{}, 0
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &p)
			o = append(o, p)
			sum += p
		}
		tot, ans := (n*(n+1))/2, make([]int, n)
		asum, not_ok := sum/tot, false
		for j := 0; j < n; j++ {
			ans[(j+1)%n] = (asum + o[j] - o[(j+1)%n]) / n
			if ans[(j+1)%n] == 0 {
				not_ok = true
			}
		}
		if not_ok {
			fmt.Fprintln(w, "NO")
		} else {
			fmt.Fprintln(w, "YES")
			for _, v := range ans {
				fmt.Fprint(w, v, " ")
			}
			fmt.Fprintln(w, "")
		}
	}
	w.Flush()
}

func main() {
	solve()
}


Fedya studies in a gymnasium. Fedya's maths hometask is to calculate the following expression:(1n + 2n + 3n + 4n) mod 5for given value of n. Fedya managed to complete the task. Can you? Note that given number n can be extremely large (e.g. it can exceed any integer type of your programming language).

The single line contains a single integer n (0 ≤ n ≤ 10105). The number doesn't contain any leading zeroes.

Print the value of the expression without leading zeros.

NoteOperation x mod y means taking remainder after division x by y.Note to the first sample:

package main

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

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

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

	s := ""
	fmt.Scanf("%s", &s)
	// fmt.Scan(&s)
	n := new(big.Int)
	n.SetString(s, 10)
	mod := big.NewInt(4)
	n.Mod(n, mod)

	if n.Int64() == 0 {
		println(4)
	} else {
		println(0)
	}
}

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

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func nextFloat() float64 {
	n, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

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

package main

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

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

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

	s := ""
	fmt.Scan(&s)
	n := new(big.Int)
	n.SetString(s, 10)
	mod := big.NewInt(4)
	// println(s, n, mod)
	n.Mod(n, mod)

	if n.Int64() == 0 {
		println(4)
	} else {
		println(0)
	}
}

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

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func nextFloat() float64 {
	n, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

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

package main

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

func main() {
	divisor := 4
	re := regexp.MustCompile(`\r?\n`)

	reader := bufio.NewReader(os.Stdin)
	if _s, err := reader.ReadString('\n'); err != nil {
		if err != io.EOF {
			return
		}
	} else {
		s := re.ReplaceAllString(_s, "")
		remain := 0
		for i := 0; i < len(s); i++ {
			digit := int(s[i] - '0')
			dividend := digit + remain*10
			remain = dividend % divisor
		}

		if remain == 0 {
			fmt.Println(4)
		} else {
			fmt.Println(0)
		}
	}
}

package main

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

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

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

func main() {
	defer writer.Flush()

	var s string
	scanf("%s\n", &s)

	x := big.NewInt(0)
	x.SetString(s, 10)
	x.Mod(x, big.NewInt(4))

	n := int(x.Int64())

	var (
		a = int(math.Pow(1, float64(n)))
		b = int(math.Pow(2, float64(n)))
		c = int(math.Pow(3, float64(n)))
		d = int(math.Pow(4, float64(n)))
	)

	ans := (a + b + c + d) % 5

	printf("%d\n", ans)
}

package main

import (
	"fmt"
	"strconv"
)

func pow(a, p int) int {
	ans := 1
	for i := 0; i < p; i++ {
		ans *= a
	}
	return ans
}

func main() {
	var line string
	fmt.Scan(&line)

	length := len(line)
	num := 0
	if length >= 2 {
		num, _ = strconv.Atoi(line[length-2 : length])
	} else {
		num, _ = strconv.Atoi(string(line[length-1]))
	}

	num = (num-1)%4 + 1
	ans := (pow(1, num) + pow(2, num) + pow(3, num) + pow(4, num)) % 5
	fmt.Println(ans)
}