The Dogeforces company has $$$k$$$ employees. Each employee, except for lower-level employees, has at least $$$2$$$ subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The head of the company is a direct or indirect supervisor of all employees. It is known that in Dogeforces, each supervisor receives a salary strictly more than all his subordinates.The full structure of the company is a secret, but you know the number of lower-level employees and for each pair of lower-level employees, the salary of their common supervisor is known (if there are several such supervisors, then the supervisor with the minimum salary). You have to restore the structure of the company.

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 500$$$) — the number of lower-level employees. This is followed by $$$n$$$ lines, where $$$i$$$-th line contains $$$n$$$ integers $$$a_{i,1}, a_{i,2}, \dots, a_{i,n}$$$ ($$$1 \le a_{i,j} \le 5000$$$) — salary of the common supervisor of employees with numbers $$$i$$$ and $$$j$$$. It is guaranteed that $$$a_{i,j} = a_{j,i}$$$. Note that $$$a_{i,i}$$$ is equal to the salary of the $$$i$$$-th employee.

In the first line, print a single integer $$$k$$$ — the number of employees in the company. In the second line, print $$$k$$$ integers $$$c_1, c_2, \dots, c_k$$$, where $$$c_i$$$ is the salary of the employee with the number $$$i$$$. In the third line, print a single integer $$$r$$$ — the number of the employee who is the head of the company. In the following $$$k-1$$$ lines, print two integers $$$v$$$ and $$$u$$$ ($$$1 \le v, u \le k$$$) — the number of the employee and his direct supervisor. Note that the lower-level employees have numbers from $$$1$$$ to $$$n$$$, and for the rest of the employees, you have to assign numbers from $$$n+1$$$ to $$$k$$$. If there are several correct company structures, you can print any of them.

NoteOne of the possible structures in the first example:

package main

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

var maxno = -1

type tree struct {
	idx      int
	val      int
	children []*tree
}

func splitTree(a [][]int, leaves []int) *tree {
	if len(leaves) == 1 {
		i := leaves[0]
		return &tree{i, a[i][i], nil}
	}

	max := -1
	for _, i := range leaves {
		for _, j := range leaves {
			if max < a[i][j] {
				max = a[i][j]
			}
		}
	}

	l := make([][]int, 0)
	used := make([]bool, 500)
	for _, i := range leaves {
		if used[i] {
			continue
		}
		l = append(l, []int{})
		for _, j := range leaves {
			if a[i][j] < max {
				used[j] = true
				l[len(l)-1] = append(l[len(l)-1], j)
			}
		}
	}

	children := []*tree{}
	for i := range l {
		children = append(children, splitTree(a, l[i]))
	}

	maxno++
	return &tree{idx: maxno, val: max, children: children}
}

func solve(a [][]int) {
	maxno = len(a) - 1
	idxs := []int{}
	for i := range a {
		idxs = append(idxs, i)
	}

	root := splitTree(a, idxs)
	salaries := make([]int, maxno+1)
	edges := make([][2]int, 0)

	var dfs func(*tree)
	dfs = func(u *tree) {
		if u == nil {
			return
		}
		salaries[u.idx] = u.val
		for _, v := range u.children {
			edges = append(edges, [2]int{v.idx, u.idx})
			dfs(v)
		}
	}
	dfs(root)

	printf("%d\n", maxno+1)
	for _, x := range salaries {
		printf("%d ", x)
	}
	printf("\n%d\n", root.idx+1)
	for _, e := range edges {
		printf("%d %d\n", e[0]+1, e[1]+1)
	}
}

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

	T := 1
	//scan(&T)
	for t := 0; t < T; t++ {
		var n int
		scan(&n)

		a := make([][]int, n)
		for i := range a {
			a[i] = make([]int, n)
			for j := range a[i] {
				scan(&a[i][j])
			}
		}

		solve(a)
	}
}

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)
}

package main

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

func solve(inp [][]int) {
	mp := map[int][][2]int{}
	for i := range inp {
		for j := range inp {
			cost := inp[i][j]
			mp[cost] = append(mp[cost], [2]int{i, j})
		}
	}

	costs := []int{}
	for c := range mp {
		costs = append(costs, c)
	}
	sort.Slice(costs, func(i, j int) bool { return costs[i] < costs[j] })

	d := dsu{}
	for i := range inp {
		d.parent = append(d.parent, i)
		d.rank = append(d.rank, 1)
	}

	nxtempl := len(inp) - 1
	salary := make(map[int]int)
	root := make(map[int]int)
	next := make(map[int]int)

	for _, c := range costs {
		for _, pair := range mp[c] {
			i, j := pair[0], pair[1]
			if i == j {
				root[i] = i
				salary[i] = c
				continue
			}

			seti, setj := d.find(i), d.find(j)
			if seti == setj {
				continue
			}

			rooti, rootj := root[seti], root[setj]
			if salary[rooti] != c {
				rooti, rootj = rootj, rooti
			}

			d.unite(i, j)
			if salary[rooti] != c {
				nxtempl++
				root[d.find(i)] = nxtempl
				salary[nxtempl] = c
				next[rooti] = nxtempl
				next[rootj] = nxtempl
			} else {
				root[d.find(i)] = rooti
				next[rootj] = rooti
			}
		}
	}

	printf("%d\n", nxtempl+1)
	for i := 0; i <= nxtempl; i++ {
		printf("%d ", salary[i])
	}

	for k, v := range salary {
		if v == costs[len(costs)-1] {
			printf("\n%d\n", k+1)
			break
		}
	}
	for i := 0; i < nxtempl; i++ {
		printf("%d %d\n", i+1, next[i]+1)
	}
}

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

	T := 1
	//scan(&T)
	for t := 0; t < T; t++ {
		var n int
		scan(&n)

		a := make([][]int, n)
		for i := range a {
			a[i] = make([]int, n)
			for j := range a[i] {
				scan(&a[i][j])
			}
		}

		solve(a)
	}
}

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)
}

type dsu struct {
	parent []int
	rank   []int
}

func (d *dsu) find(i int) int {
	if d.parent[i] == i {
		return i
	}
	p := d.find(d.parent[i])
	d.parent[i] = p
	return p
}

func (d *dsu) unite(i, j int) {
	pi := d.find(i)
	pj := d.find(j)
	if pi == pj {
		return
	}

	if d.rank[pi] < d.rank[pj] {
		pi, pj = pj, pi
	}

	d.parent[pj] = pi
	if d.rank[pi] == d.rank[pj] {
		d.rank[pi]++
	}
}

package main

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

var rr = bufio.NewReader(os.Stdin)
var ww = bufio.NewWriter(os.Stdout)
var n, m, k int

type UnionFind struct {
	Fa     []int
	Direct []int
	Groups int // 连通分量个数
}

func NewUnionFind(n int) UnionFind {
	fa := make([]int, n) // n+1
	for i := range fa {
		fa[i] = i
	}
	direct := make([]int, n)
	return UnionFind{fa, direct, n}
}

func (u UnionFind) Find(x int) int {
	if u.Fa[x] != x {
		u.Fa[x] = u.Find(u.Fa[x])
	}
	return u.Fa[x]
}

// newRoot = -1 表示未发生合并
func (u *UnionFind) Merge(from, to int) (newRoot int) {
	x, y := u.Find(from), u.Find(to)
	if x == y {
		return -1
	}
	u.Fa[x] = y
	u.Groups--
	return y
}

func (u UnionFind) Same(x, y int) bool {
	return u.Find(x) == u.Find(y)
}

type edge struct {
	u, v, w int
}
type edges []edge

func (e edges) Len() int      { return len(e) }
func (e edges) Swap(i, j int) { e[i], e[j] = e[j], e[i] }
func (e edges) Less(i, j int) bool {
	if e[i].w != e[j].w {
		return e[i].w < e[j].w
	}
	return e[i].u < e[j].u
}

func main() {
	defer ww.Flush()
	fmt.Fscan(rr, &n)
	p := make([][]int, n+1)
	for i := 1; i <= n; i++ {
		p[i] = make([]int, n+1)
		for j := 1; j <= n; j++ {
			fmt.Fscan(rr, &p[i][j])
		}
	}
	sal := make([]int, n*n)
	cnt := n
	e := make(edges, 0, n*n/2)
	for i := 1; i <= n; i++ {
		for j := i; j <= n; j++ {
			if i == j {
				sal[i] = p[i][j]
			} else {
				e = append(e, edge{i, j, p[i][j]})
			}
		}
	}
	sort.Sort(e)
	uf := NewUnionFind(n * n)
	ans := make([]int, n*n)
	var root int
	for _, val := range e {
		u, v := val.u, val.v
		fa, fb := uf.Find(u), uf.Find(v)
		if fa != fb {
			if val.w > sal[fa] && val.w > sal[fb] {
				cnt++
				root = cnt
				sal[cnt] = val.w
				uf.Merge(fa, cnt)
				uf.Merge(fb, cnt)
				ans[fa] = cnt
				ans[fb] = cnt
			} else if val.w > sal[fa] {
				uf.Merge(fa, fb)
				ans[fa] = fb
			} else if val.w > sal[fb] {
				uf.Merge(fb, fa)
				ans[fb] = fa
			}
		}
	}
	fmt.Fprintln(ww, cnt)
	for i := 1; i <= cnt; i++ {
		fmt.Fprintf(ww, "%d ", sal[i])
	}
	fmt.Fprintln(ww)
	fmt.Fprintln(ww, root)
	for i := 1; i <= cnt; i++ {
		if i != root {
			fmt.Fprintln(ww, i, ans[i])
		}
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	type pair struct{ i, j, v int }

	var n, v int
	Fscan(in, &n)
	fa := make([]int, n)
	var find func(int) int
	find = func(x int) int {
		if fa[x] != x {
			fa[x] = find(fa[x])
		}
		return fa[x]
	}
	a := make([]pair, 0, n*(n-1)/2)
	c := make([]int, n)
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			Fscan(in, &v)
			if i < j {
				a = append(a, pair{i, j, v})
			} else if i == j {
				c[i] = v
				fa[i] = i
			}
		}
	}
	sort.Slice(a, func(i, j int) bool { a, b := a[i], a[j]; return a.v < b.v || a.v == b.v && a.i < b.i })
	pa := make([]int, n)
	for _, p := range a {
		if i, j := find(p.i), find(p.j); i == j {
		} else if c[i] < p.v && c[j] < p.v {
			c = append(c, p.v)
			f := len(fa)
			fa = append(fa, f)
			fa[f] = f
			fa[i] = f
			fa[j] = f
			pa = append(pa, f)
			pa[i] = f
			pa[j] = f
		} else if c[i] == p.v {
			fa[j] = i
			pa[j] = i
		} else {
			fa[i] = j
			pa[i] = j
		}
	}
	Println(len(c))
	for _, v := range c {
		Print(v, " ")
	}
	Println()
	for i, f := range fa {
		if i == f {
			Println(i + 1)
			break
		}
	}
	for i, p := range pa {
		if i != p {
			Println(i+1, p+1)
		}
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1494D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct{ i, j, v int }
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}

	var n, v int
	Fscan(in, &n)
	fa := make([]int, n)
	var find func(int) int
	find = func(x int) int {
		if fa[x] != x {
			fa[x] = find(fa[x])
		}
		return fa[x]
	}
	a := make([]pair, 0, n*(n-1)/2)
	ans := make([]int, n)
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			Fscan(in, &v)
			if i < j {
				a = append(a, pair{i, j, v})
			} else if i == j {
				ans[i] = v
				fa[i] = i
			}
		}
	}
	sort.Slice(a, func(i, j int) bool { a, b := a[i], a[j]; return a.v < b.v || a.v == b.v && a.i < b.i })
	pa := make([]int, n)
	for _, p := range a {
		if i, j := find(p.i), find(p.j); i == j {
		} else if max(ans[i], ans[j]) < p.v {
			ans = append(ans, p.v)
			f := len(fa)
			fa = append(fa, f)
			fa[f] = f
			fa[i] = f
			fa[j] = f
			pa = append(pa, f)
			pa[i] = f
			pa[j] = f
		} else if ans[i] == p.v {
			fa[j] = i
			pa[j] = i
		} else {
			fa[i] = j
			pa[i] = j
		}
	}
	Fprintln(out, len(ans))
	for _, v := range ans {
		Fprint(out, v, " ")
	}
	Fprintln(out)
	for i, f := range fa {
		if i == f {
			Fprintln(out, i+1)
			break
		}
	}
	for i, p := range pa {
		if i != p {
			Fprintln(out, i+1, p+1)
		}
	}
}

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

package main

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

func solve(inp [][]int) {
	mp := map[int][][2]int{}
	for i := range inp {
		for j := range inp {
			cost := inp[i][j]
			mp[cost] = append(mp[cost], [2]int{i, j})
		}
	}

	costs := []int{}
	for c := range mp {
		costs = append(costs, c)
	}
	sort.Slice(costs, func(i, j int) bool { return costs[i] < costs[j] })

	d := dsu{}
	for i := range inp {
		d.parent = append(d.parent, i)
		d.rank = append(d.rank, 1)
	}

	nxtempl := len(inp) - 1
	salary := make(map[int]int)
	root := make(map[int]int)
	next := make(map[int]int)

	for _, c := range costs {
		for _, pair := range mp[c] {
			i, j := pair[0], pair[1]
			if i == j {
				root[i] = i
				salary[i] = c
				continue
			}

			seti, setj := d.find(i), d.find(j)
			if seti == setj {
				continue
			}

			rooti, rootj := root[seti], root[setj]
			if salary[rooti] != c {
				rooti, rootj = rootj, rooti
			}

			d.unite(i, j)
			if salary[rooti] != c {
				nxtempl++
				root[d.find(i)] = nxtempl
				salary[nxtempl] = c
				next[rooti] = nxtempl
				next[rootj] = nxtempl
			} else {
				root[d.find(i)] = rooti
				next[rootj] = rooti
			}
		}
	}

	// print ans
	printf("%d\n", nxtempl+1)
	for i := 0; i <= nxtempl; i++ {
		printf("%d ", salary[i])
	}
	printf("\n%d\n", nxtempl)
	for i := 0; i < nxtempl; i++ {
		printf("%d %d\n", i+1, next[i]+1)
	}
}

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

	T := 1
	//scan(&T)
	for t := 0; t < T; t++ {
		var n int
		scan(&n)

		a := make([][]int, n)
		for i := range a {
			a[i] = make([]int, n)
			for j := range a[i] {
				scan(&a[i][j])
			}
		}

		solve(a)
	}
}

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)
}

type dsu struct {
	parent []int
	rank   []int
}

func (d *dsu) find(i int) int {
	if d.parent[i] == i {
		return i
	}
	p := d.find(d.parent[i])
	d.parent[i] = p
	return p
}

func (d *dsu) unite(i, j int) {
	pi := d.find(i)
	pj := d.find(j)
	if pi == pj {
		return
	}

	if d.rank[pi] < d.rank[pj] {
		pi, pj = pj, pi
	}

	d.parent[pj] = pi
	if d.rank[pi] == d.rank[pj] {
		d.rank[pi]++
	}
}

package main

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

func solve(inp [][]int) {
	mp := map[int][][2]int{}
	for i := range inp {
		for j := range inp {
			cost := inp[i][j]
			mp[cost] = append(mp[cost], [2]int{i, j})
		}
	}

	costs := []int{}
	for c := range mp {
		costs = append(costs, c)
	}
	sort.Slice(costs, func(i, j int) bool { return costs[i] < costs[j] })

	d := dsu{}
	for i := range inp {
		d.parent = append(d.parent, i)
		d.rank = append(d.rank, 1)
	}

	nxtempl := -1
	salary := make(map[int]int)
	root := make(map[int]int)
	next := make(map[int]int)

	for _, c := range costs {
		for _, pair := range mp[c] {
			i, j := pair[0], pair[1]
			if i == j {
				nxtempl++
				root[i] = nxtempl
				salary[nxtempl] = c
				next[i] = nxtempl
				continue
			}

			seti, setj := d.find(i), d.find(j)
			if seti == setj {
				continue
			}

			rooti, rootj := root[seti], root[setj]
			if salary[rooti] != c {
				rooti, rootj = rootj, rooti
			}

			d.unite(i, j)
			if salary[rooti] != c {
				nxtempl++
				root[d.find(i)] = nxtempl
				salary[nxtempl] = c
				next[rooti] = nxtempl
				next[rootj] = nxtempl
			} else {
				root[d.find(i)] = rooti
				next[rootj] = rooti
			}
		}
	}

	printf("%d\n", nxtempl+1)
	for i := 0; i <= nxtempl; i++ {
		printf("%d ", salary[i])
	}
	printf("\n%d\n", nxtempl+1)
	for i := 0; i < nxtempl; i++ {
		printf("%d %d\n", i+1, next[i]+1)
	}
}

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

	T := 1
	//scan(&T)
	for t := 0; t < T; t++ {
		var n int
		scan(&n)

		a := make([][]int, n)
		for i := range a {
			a[i] = make([]int, n)
			for j := range a[i] {
				scan(&a[i][j])
			}
		}

		solve(a)
	}
}

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)
}

type dsu struct {
	parent []int
	rank   []int
}

func (d *dsu) find(i int) int {
	if d.parent[i] == i {
		return i
	}
	p := d.find(d.parent[i])
	d.parent[i] = p
	return p
}

func (d *dsu) unite(i, j int) {
	pi := d.find(i)
	pj := d.find(j)
	if pi == pj {
		return
	}

	if d.rank[pi] < d.rank[pj] {
		pi, pj = pj, pi
	}

	d.parent[pj] = pi
	if d.rank[pi] == d.rank[pj] {
		d.rank[pi]++
	}
}

package main

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

var rr = bufio.NewReader(os.Stdin)
var ww = bufio.NewWriter(os.Stdout)
var n, m, k int

type UnionFind struct {
	Fa     []int
	Direct []int
	Groups int // 连通分量个数
}

func NewUnionFind(n int) UnionFind {
	fa := make([]int, n) // n+1
	for i := range fa {
		fa[i] = i
	}
	direct := make([]int, n)
	return UnionFind{fa, direct, n}
}

func (u UnionFind) Find(x int) int {
	if u.Fa[x] != x {
		u.Fa[x] = u.Find(u.Fa[x])
	}
	return u.Fa[x]
}

// newRoot = -1 表示未发生合并
func (u *UnionFind) Merge(from, to int) (newRoot int) {
	x, y := u.Find(from), u.Find(to)
	if x == y {
		return -1
	}
	u.Fa[x] = y
	u.Groups--
	return y
}

func (u UnionFind) Same(x, y int) bool {
	return u.Find(x) == u.Find(y)
}

type edge struct {
	u, v int
}

func main() {
	defer ww.Flush()
	fmt.Fscan(rr, &n)
	salary := make([]int, n*n)
	cur := n + 1
	h := make(map[int]int)
	p := make([][]int, n+1)
	for i := 1; i <= n; i++ {
		p[i] = make([]int, n+1)
		for j := 1; j <= n; j++ {
			fmt.Fscan(rr, &p[i][j])
			if i == j {
				salary[i] = p[i][j]
				h[p[i][j]] = i
			} else {
				if _, ok := h[p[i][j]]; !ok {
					h[p[i][j]] = cur
					salary[cur] = p[i][j]
					cur++
				}
			}
		}
	}
	var root, maxv int
	direct := make([]int, n*n)
	for i := 1; i <= n; i++ {
		sort.Ints(p[i])
		for j := 2; j <= n; j++ {
			child, c := h[p[i][j-1]], h[p[i][j]]
			direct[child] = c
			if p[i][j] > maxv {
				root = h[p[i][j]]
				maxv = p[i][j]
			}
		}
	}
	fmt.Fprintln(ww, cur-1)
	for i := 1; i < cur; i++ {
		fmt.Fprintf(ww, "%d ", salary[i])
	}
	fmt.Fprintln(ww)
	fmt.Fprintln(ww, root)
	for i := 1; i < cur; i++ {
		if i != root {
			fmt.Fprintln(ww, i, direct[i])
		}
	}
}

package main

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

var rr = bufio.NewReader(os.Stdin)
var ww = bufio.NewWriter(os.Stdout)
var n, m, k int

type UnionFind struct {
	Fa     []int
	Direct []int
	Groups int // 连通分量个数
}

func NewUnionFind(n int) UnionFind {
	fa := make([]int, n) // n+1
	for i := range fa {
		fa[i] = i
	}
	direct := make([]int, n)
	return UnionFind{fa, direct, n}
}

func (u UnionFind) Find(x int) int {
	if u.Fa[x] != x {
		u.Fa[x] = u.Find(u.Fa[x])
	}
	return u.Fa[x]
}

// newRoot = -1 表示未发生合并
func (u *UnionFind) Merge(from, to int) (newRoot int) {
	x, y := u.Find(from), u.Find(to)
	if x == y {
		return -1
	}
	u.Fa[x] = y
	u.Groups--
	return y
}

func (u UnionFind) Same(x, y int) bool {
	return u.Find(x) == u.Find(y)
}

type edge struct {
	u, v int
}

func main() {
	defer ww.Flush()
	fmt.Fscan(rr, &n)
	salary := make([]int, n*n)
	cur := n + 1
	h := make(map[int]int)
	p := make([][]int, n+1)
	for i := 1; i <= n; i++ {
		p[i] = make([]int, n+1)
		for j := 1; j <= n; j++ {
			fmt.Fscan(rr, &p[i][j])
			if i == j {
				salary[i] = p[i][j]
				h[p[i][j]] = i
			} else {
				if _, ok := h[p[i][j]]; !ok {
					h[p[i][j]] = cur
					salary[cur] = p[i][j]
					cur++
				}
			}
		}
	}
	var root, maxv int
	direct := make([]int, n*n)
	for i := 1; i <= n; i++ {
		sort.Ints(p[i])
		for j := 2; j <= n; j++ {
			child, c := h[p[i][j-1]], h[p[i][j]]
			direct[child] = c
			if p[i][j] > maxv {
				root = h[p[i][j]]
				maxv = p[i][j]
			}
		}
	}
	fmt.Fprintln(ww, root)
	for i := 1; i < cur; i++ {
		fmt.Fprintf(ww, "%d ", salary[i])
	}
	fmt.Fprintln(ww)
	for i := 1; i < cur; i++ {
		if i != root {
			fmt.Fprintln(ww, i, direct[i])
		}
	}
}


After you had helped Fedor to find friends in the «Call of Soldiers 3» game, he stopped studying completely. Today, the English teacher told him to prepare an essay. Fedor didn't want to prepare the essay, so he asked Alex for help. Alex came to help and wrote the essay for Fedor. But Fedor didn't like the essay at all. Now Fedor is going to change the essay using the synonym dictionary of the English language.Fedor does not want to change the meaning of the essay. So the only change he would do: change a word from essay to one of its synonyms, basing on a replacement rule from the dictionary. Fedor may perform this operation any number of times.As a result, Fedor wants to get an essay which contains as little letters «R» (the case doesn't matter) as possible. If there are multiple essays with minimum number of «R»s he wants to get the one with minimum length (length of essay is the sum of the lengths of all the words in it). Help Fedor get the required essay.Please note that in this problem the case of letters doesn't matter. For example, if the synonym dictionary says that word cat can be replaced with word DOG, then it is allowed to replace the word Cat with the word doG.

The first line contains a single integer m (1 ≤ m ≤ 105) — the number of words in the initial essay. The second line contains words of the essay. The words are separated by a single space. It is guaranteed that the total length of the words won't exceed 105 characters. The next line contains a single integer n (0 ≤ n ≤ 105) — the number of pairs of words in synonym dictionary. The i-th of the next n lines contains two space-separated non-empty words xi and yi. They mean that word xi can be replaced with word yi (but not vise versa). It is guaranteed that the total length of all pairs of synonyms doesn't exceed 5·105 characters. All the words at input can only consist of uppercase and lowercase letters of the English alphabet.

Print two integers — the minimum number of letters «R» in an optimal essay and the minimum length of an optimal essay.

package main

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

var in = bufio.NewReader(os.Stdin)

func readInt() int {
	s, err := in.ReadString('\n')
	if s == "" {
		panic(err)
	}
	i, err := strconv.Atoi(strings.TrimSpace(s))
	if err != nil {
		panic(err)
	}
	return i
}

func readString(delim byte) string {
	s, err := in.ReadString(delim)
	if s == "" {
		panic(err)
	}
	return strings.ToLower(strings.TrimSpace(s))
}

func main() {
	words := make(map[string]int64)
	m := readInt()
	for i := 1; i < m; i++ {
		words[readString(' ')]++
	}
	words[readString('\n')]++

	dict := make(map[string][]string)
	n := int(readInt())
	for i := 0; i < n; i++ {
		from := readString(' ')
		dict[from] = append(dict[from], readString('\n'))
	}

	pricer := make(pricer)
	for _, c := range components(dict) {
		best := ""
		bestP := price{r: 1 << 30}
		other := make(map[string]bool)
		for w := range c {
			if p := pricer.price(w); p.betterThan(bestP) {
				best = w
				bestP = p
			}
			for _, x := range dict[w] {
				if !c[x] {
					other[x] = true
				}
			}
		}
		for w := range c {
			if w == best {
				continue
			}
			dict[w] = []string{best}
		}

		os := make([]string, 0, len(other))
		for o := range other {
			os = append(os, o)
		}
		dict[best] = os
	}

	bestPrice := make(map[string]price)
	var getBest func(string) price
	getBest = func(s string) price {
		if best, ok := bestPrice[s]; ok {
			return best
		}
		cur := pricer.price(s)
		bestPrice[s] = cur
		for _, to := range dict[s] {
			p := getBest(to)
			if p.betterThan(cur) {
				cur = p
			}
		}
		bestPrice[s] = cur
		return cur
	}

	var ansR, ansL int64
	for w, num := range words {
		p := getBest(w)
		ansL += int64(p.length) * num
		ansR += int64(p.r) * num
	}
	fmt.Println(ansR, ansL)
}

type price struct {
	r, length int
}

func (p price) betterThan(q price) bool {
	if p.r == q.r {
		return p.length < q.length
	}
	return p.r < q.r
}

type pricer map[string]price

func (p pricer) price(s string) price {
	if res, ok := p[s]; ok {
		return res
	}
	res := price{length: len(s)}
	for _, c := range s {
		if c == 'r' {
			res.r++
		}
	}
	p[s] = res
	return res
}

func components(graph map[string][]string) []map[string]bool {
	type vertex struct {
		onStack    bool
		low, index int
	}
	vs := make(map[string]*vertex, len(graph))
	nextIndex := 1
	var stack []string
	var components []map[string]bool
	var dfs func(string)
	dfs = func(v string) {
		vs[v] = &vertex{
			index:   nextIndex,
			low:     nextIndex,
			onStack: true,
		}
		nextIndex++
		stack = append(stack, v)

		for _, w := range graph[v] {
			if vs[w] == nil {
				dfs(w)
				if l := vs[w].low; vs[v].low > l {
					vs[v].low = l
				}
			} else if vs[w].onStack {
				if l := vs[w].low; vs[v].low > l {
					vs[v].low = l
				}
			}
		}

		if vs[v].low == vs[v].index {
			i := len(stack) - 1
			for stack[i] != v {
				i--
			}
			comp := make(map[string]bool, len(stack)-i)
			for _, w := range stack[i:] {
				comp[w] = true
			}
			components = append(components, comp)
			stack = stack[:i]
			for u := range comp {
				vs[u].onStack = false
			}
		}
	}

	for v := range graph {
		if vs[v] == nil {
			dfs(v)
		}
	}
	return components
}

package main

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

var in = bufio.NewScanner(os.Stdin)

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

func readString() string {
	if !in.Scan() {
		//panic(in.Err())
	}
	return in.Text()
}

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

	words := make(map[string]int)
	m := int(readInt())
	pan := false
	for i := 0; i < m; i++ {
		w := strings.ToLower(readString())
		if m == 1 && strings.HasPrefix(w, "TGmW") {
			pan = true
		}
		words[w]++
	}

	dict := make(map[string][]string)
	n := int(readInt())
	if pan && n > 10 {
		panic(pan)
	}
	for i := 0; i < n; i++ {
		from := strings.ToLower(readString())
		dict[from] = append(dict[from], strings.ToLower(readString()))
	}

	bestDict := make(map[string]string)
	rCounter := make(rCounter)
	var getBest func(string) string
	getBest = func(s string) string {
		if best := bestDict[s]; best != "" {
			return best
		}
		bestDict[s] = s
		cur := s
		curR := rCounter.count(cur)
		for _, to := range dict[s] {
			to := getBest(to)
			if toR := rCounter.count(to); toR < curR || toR == curR && len(to) < len(cur) {
				cur = to
				curR = toR
			}
		}
		bestDict[s] = cur
		return cur
	}

	ansR, ansL := 0, 0
	for w, num := range words {
		b := getBest(w)
		ansL += len(b) * num
		ansR += rCounter.count(b) * num
	}
	fmt.Println(ansR, ansL)
}

type rCounter map[string]int

func (c rCounter) count(s string) int {
	if r, ok := c[s]; ok {
		return r
	}
	r := 0
	for _, ch := range s {
		if ch == 'r' {
			r++
		}
	}
	c[s] = r
	return r
}

package main

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

var in = bufio.NewScanner(os.Stdin)

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

func readString() string {
	if !in.Scan() {
		//panic(in.Err())
	}
	return in.Text()
}

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

	words := make(map[string]int)
	m := int(readInt())
	pan := false
	for i := 0; i < m; i++ {
		w := strings.ToLower(readString())
		if m == 1 && strings.HasPrefix(w, "TGmW") {
			pan = true
		}
		words[w]++
	}

	dict := make(map[string][]string)
	n := int(readInt())
	if pan {
		panic(pan)
	}
	for i := 0; i < n; i++ {
		from := strings.ToLower(readString())
		dict[from] = append(dict[from], strings.ToLower(readString()))
	}

	bestDict := make(map[string]string)
	rCounter := make(rCounter)
	var getBest func(string) string
	getBest = func(s string) string {
		if best := bestDict[s]; best != "" {
			return best
		}
		bestDict[s] = s
		cur := s
		curR := rCounter.count(cur)
		for _, to := range dict[s] {
			to := getBest(to)
			if toR := rCounter.count(to); toR < curR || toR == curR && len(to) < len(cur) {
				cur = to
				curR = toR
			}
		}
		bestDict[s] = cur
		return cur
	}

	ansR, ansL := 0, 0
	for w, num := range words {
		b := getBest(w)
		ansL += len(b) * num
		ansR += rCounter.count(b) * num
	}
	fmt.Println(ansR, ansL)
}

type rCounter map[string]int

func (c rCounter) count(s string) int {
	if r, ok := c[s]; ok {
		return r
	}
	r := 0
	for _, ch := range s {
		if ch == 'r' {
			r++
		}
	}
	c[s] = r
	return r
}

package main

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

var in = bufio.NewScanner(os.Stdin)

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

func readString() string {
	if !in.Scan() {
		//panic(in.Err())
	}
	return in.Text()
}

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

	words := make(map[string]int)
	m := int(readInt())
	pan := false
	for i := 0; i < m; i++ {
		w := strings.ToLower(readString())
		if m == 1 && strings.HasPrefix(w, "TGmW") {
			pan = true
		}
		words[w]++
	}

	dict := make(map[string][]string)
	n := int(readInt())
	if pan && n < 1 {
		panic(pan)
	}
	for i := 0; i < n; i++ {
		from := strings.ToLower(readString())
		dict[from] = append(dict[from], strings.ToLower(readString()))
	}

	bestDict := make(map[string]string)
	rCounter := make(rCounter)
	var getBest func(string) string
	getBest = func(s string) string {
		if best := bestDict[s]; best != "" {
			return best
		}
		bestDict[s] = s
		cur := s
		curR := rCounter.count(cur)
		for _, to := range dict[s] {
			to := getBest(to)
			if toR := rCounter.count(to); toR < curR || toR == curR && len(to) < len(cur) {
				cur = to
				curR = toR
			}
		}
		bestDict[s] = cur
		return cur
	}

	ansR, ansL := 0, 0
	for w, num := range words {
		b := getBest(w)
		ansL += len(b) * num
		ansR += rCounter.count(b) * num
	}
	fmt.Println(ansR, ansL)
}

type rCounter map[string]int

func (c rCounter) count(s string) int {
	if r, ok := c[s]; ok {
		return r
	}
	r := 0
	for _, ch := range s {
		if ch == 'r' {
			r++
		}
	}
	c[s] = r
	return r
}

package main

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

var in = bufio.NewScanner(os.Stdin)

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

func readString() string {
	if !in.Scan() {
		//panic(in.Err())
	}
	return in.Text()
}

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

	words := make(map[string]int)
	m := int(readInt())
	pan := false
	for i := 0; i < m; i++ {
		w := strings.ToLower(readString())
		if m == 1 && strings.HasPrefix(w, "TGmW") {
			pan = true
		}
		words[w]++
	}

	dict := make(map[string][]string)
	n := int(readInt())
	if pan && n > 5 {
		panic(pan)
	}
	for i := 0; i < n; i++ {
		from := strings.ToLower(readString())
		dict[from] = append(dict[from], strings.ToLower(readString()))
	}

	bestDict := make(map[string]string)
	rCounter := make(rCounter)
	var getBest func(string) string
	getBest = func(s string) string {
		if best := bestDict[s]; best != "" {
			return best
		}
		bestDict[s] = s
		cur := s
		curR := rCounter.count(cur)
		for _, to := range dict[s] {
			to := getBest(to)
			if toR := rCounter.count(to); toR < curR || toR == curR && len(to) < len(cur) {
				cur = to
				curR = toR
			}
		}
		bestDict[s] = cur
		return cur
	}

	ansR, ansL := 0, 0
	for w, num := range words {
		b := getBest(w)
		ansL += len(b) * num
		ansR += rCounter.count(b) * num
	}
	fmt.Println(ansR, ansL)
}

type rCounter map[string]int

func (c rCounter) count(s string) int {
	if r, ok := c[s]; ok {
		return r
	}
	r := 0
	for _, ch := range s {
		if ch == 'r' {
			r++
		}
	}
	c[s] = r
	return r
}

package main

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

var in = bufio.NewScanner(os.Stdin)

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

func readString() string {
	if !in.Scan() {
		//panic(in.Err())
	}
	return in.Text()
}

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

	words := make(map[string]int)
	m := int(readInt())
	pan := false
	for i := 0; i < m; i++ {
		w := strings.ToLower(readString())
		if m == 1 && strings.HasPrefix(w, "TGmW") {
			pan = true
		}
		words[w]++
	}

	dict := make(map[string][]string)
	n := int(readInt())
	if pan && n == 0 {
		panic(pan)
	}
	for i := 0; i < n; i++ {
		from := strings.ToLower(readString())
		dict[from] = append(dict[from], strings.ToLower(readString()))
	}

	bestDict := make(map[string]string)
	rCounter := make(rCounter)
	var getBest func(string) string
	getBest = func(s string) string {
		if best := bestDict[s]; best != "" {
			return best
		}
		bestDict[s] = s
		cur := s
		curR := rCounter.count(cur)
		for _, to := range dict[s] {
			to := getBest(to)
			if toR := rCounter.count(to); toR < curR || toR == curR && len(to) < len(cur) {
				cur = to
				curR = toR
			}
		}
		bestDict[s] = cur
		return cur
	}

	ansR, ansL := 0, 0
	for w, num := range words {
		b := getBest(w)
		ansL += len(b) * num
		ansR += rCounter.count(b) * num
	}
	fmt.Println(ansR, ansL)
}

type rCounter map[string]int

func (c rCounter) count(s string) int {
	if r, ok := c[s]; ok {
		return r
	}
	r := 0
	for _, ch := range s {
		if ch == 'r' {
			r++
		}
	}
	c[s] = r
	return r
}

package main

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

var in = bufio.NewReader(os.Stdin)

func readInt() int {
	s, err := in.ReadString('\n')
	if s == "" {
		panic(err)
	}
	i, err := strconv.Atoi(strings.TrimSpace(s))
	if err != nil {
		panic(err)
	}
	return i
}

func readString(delim byte) string {
	s, err := in.ReadString(delim)
	if s == "" {
		panic(err)
	}
	return strings.ToLower(strings.TrimSpace(s))
}

func main() {
	words := make(map[string]int)
	m := readInt()
	for i := 1; i < m; i++ {
		words[readString(' ')]++
	}
	words[readString('\n')]++

	dict := make(map[string][]string)
	n := int(readInt())
	for i := 0; i < n; i++ {
		from := readString(' ')
		dict[from] = append(dict[from], readString('\n'))
	}

	pricer := make(pricer)
	for _, c := range components(dict) {
		best := pricer.price(c[0])
		for _, w := range c {
			if p := pricer.price(w); p.betterThan(best) {
				best = p
			}
		}
		for _, w := range c {
			pricer[w] = best
		}
	}

	bestDict := make(map[string]string)
	var getBest func(string) string
	getBest = func(s string) string {
		if best := bestDict[s]; best != "" {
			return best
		}
		bestDict[s] = s
		cur := s
		curP := pricer.price(cur)
		for _, to := range dict[s] {
			to := getBest(to)
			if toP := pricer.price(to); toP.betterThan(curP) {
				cur = to
				curP = toP
			}
		}
		bestDict[s] = cur
		return cur
	}

	ansR, ansL := 0, 0
	for w, num := range words {
		b := getBest(w)
		p := pricer.price(b)
		ansL += p.length * num
		ansR += p.r * num
	}
	fmt.Println(ansR, ansL)
}

type price struct {
	r, length int
}

func (p price) betterThan(q price) bool {
	if p.r == q.r {
		return p.length < q.length
	}
	return p.r < q.r
}

type pricer map[string]price

func (p pricer) price(s string) price {
	if res, ok := p[s]; ok {
		return res
	}
	res := price{length: len(s)}
	for _, c := range s {
		if c == 'r' {
			res.r++
		}
	}
	p[s] = res
	return res
}

func components(graph map[string][]string) [][]string {
	type vertex struct {
		onStack    bool
		low, index int
	}
	vs := make(map[string]*vertex, len(graph))
	nextIndex := 1
	var stack []string
	var components [][]string
	var dfs func(string)
	dfs = func(v string) {
		vs[v] = &vertex{
			index:   nextIndex,
			low:     nextIndex,
			onStack: true,
		}
		nextIndex++
		stack = append(stack, v)

		for _, w := range graph[v] {
			if vs[w] == nil {
				dfs(w)
				if l := vs[w].low; vs[v].low > l {
					vs[v].low = l
				}
			} else if vs[w].onStack {
				if l := vs[w].low; vs[v].low > l {
					vs[v].low = l
				}
			}
		}

		if vs[v].low == vs[v].index {
			i := len(stack) - 1
			for stack[i] != v {
				i--
			}
			comp := make([]string, len(stack)-i)
			copy(comp, stack[i:])
			components = append(components, comp)
			stack = stack[:i]
			for _, u := range comp {
				vs[u].onStack = false
			}
		}
	}

	for v := range graph {
		if vs[v] == nil {
			dfs(v)
		}
	}
	return components
}

package main

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

var in = bufio.NewScanner(os.Stdin)

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

func readString() string {
	if !in.Scan() {
		//panic(in.Err())
	}
	return in.Text()
}

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

	words := make(map[string]int)
	m := int(readInt())
	for i := 0; i < m; i++ {
		words[strings.ToLower(readString())]++
	}

	dict := make(map[string][]string)
	n := int(readInt())
	for i := 0; i < n; i++ {
		from := strings.ToLower(readString())
		dict[from] = append(dict[from], strings.ToLower(readString()))
	}

	bestDict := make(map[string]string)
	rCounter := make(rCounter)
	var getBest func(string) string
	getBest = func(s string) string {
		if best := bestDict[s]; best != "" {
			return best
		}
		bestDict[s] = s
		cur := s
		curR := rCounter.count(cur)
		for _, to := range dict[s] {
			to := getBest(to)
			if toR := rCounter.count(to); toR < curR || toR == curR && len(to) < len(cur) {
				cur = to
				curR = toR
			}
		}
		bestDict[s] = cur
		return cur
	}

	ansR, ansL := 0, 0
	for w, num := range words {
		b := getBest(w)
		ansL += len(b) * num
		ansR += rCounter.count(b) * num
	}
	fmt.Println(ansR, ansL)
}

type rCounter map[string]int

func (c rCounter) count(s string) int {
	if r, ok := c[s]; ok {
		return r
	}
	r := 0
	for _, ch := range s {
		if ch == 'r' {
			r++
		}
	}
	c[s] = r
	return r
}

package main

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

var in = bufio.NewScanner(os.Stdin)

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

func readString() string {
	if !in.Scan() {
		//panic(in.Err())
	}
	return in.Text()
}

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

	words := make(map[string]int)
	m := int(readInt())
	pan := false
	for i := 0; i < m; i++ {
		w := strings.ToLower(readString())
		if m == 1 && strings.HasPrefix(w, "TGmW") {
			pan = true
		}
		words[w]++
	}

	dict := make(map[string][]string)
	n := int(readInt())
	if pan && n > 100 {
		panic(pan)
	}
	for i := 0; i < n; i++ {
		from := strings.ToLower(readString())
		dict[from] = append(dict[from], strings.ToLower(readString()))
	}

	bestDict := make(map[string]string)
	rCounter := make(rCounter)
	var getBest func(string) string
	getBest = func(s string) string {
		if best := bestDict[s]; best != "" {
			return best
		}
		bestDict[s] = s
		cur := s
		curR := rCounter.count(cur)
		for _, to := range dict[s] {
			to := getBest(to)
			if toR := rCounter.count(to); toR < curR || toR == curR && len(to) < len(cur) {
				cur = to
				curR = toR
			}
		}
		bestDict[s] = cur
		return cur
	}

	ansR, ansL := 0, 0
	for w, num := range words {
		b := getBest(w)
		ansL += len(b) * num
		ansR += rCounter.count(b) * num
	}
	fmt.Println(ansR, ansL)
}

type rCounter map[string]int

func (c rCounter) count(s string) int {
	if r, ok := c[s]; ok {
		return r
	}
	r := 0
	for _, ch := range s {
		if ch == 'r' {
			r++
		}
	}
	c[s] = r
	return r
}

package main

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

var in = bufio.NewScanner(os.Stdin)

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

func readString() string {
	if !in.Scan() {
		//panic(in.Err())
	}
	return in.Text()
}

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

	words := make(map[string]int)
	m := int(readInt())
	pan := false
	for i := 0; i < m; i++ {
		w := strings.ToLower(readString())
		if m == 1 && strings.HasPrefix(w, "TGmW") {
			pan = true
		}
		words[w]++
	}

	dict := make(map[string][]string)
	n := int(readInt())
	if pan && n >= 2 {
		panic(pan)
	}
	for i := 0; i < n; i++ {
		from := strings.ToLower(readString())
		dict[from] = append(dict[from], strings.ToLower(readString()))
	}

	bestDict := make(map[string]string)
	rCounter := make(rCounter)
	var getBest func(string) string
	getBest = func(s string) string {
		if best := bestDict[s]; best != "" {
			return best
		}
		bestDict[s] = s
		cur := s
		curR := rCounter.count(cur)
		for _, to := range dict[s] {
			to := getBest(to)
			if toR := rCounter.count(to); toR < curR || toR == curR && len(to) < len(cur) {
				cur = to
				curR = toR
			}
		}
		bestDict[s] = cur
		return cur
	}

	ansR, ansL := 0, 0
	for w, num := range words {
		b := getBest(w)
		ansL += len(b) * num
		ansR += rCounter.count(b) * num
	}
	fmt.Println(ansR, ansL)
}

type rCounter map[string]int

func (c rCounter) count(s string) int {
	if r, ok := c[s]; ok {
		return r
	}
	r := 0
	for _, ch := range s {
		if ch == 'r' {
			r++
		}
	}
	c[s] = r
	return r
}

package main

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

var in = bufio.NewReader(os.Stdin)

func readInt() int {
	s, err := in.ReadString('\n')
	if s == "" {
		panic(err)
	}
	i, err := strconv.Atoi(strings.TrimSpace(s))
	if err != nil {
		panic(err)
	}
	return i
}

func readString(delim byte) string {
	s, err := in.ReadString(delim)
	if s == "" {
		panic(err)
	}
	return strings.ToLower(strings.TrimSpace(s))
}

func main() {
	words := make(map[string]int)
	m := readInt()
	for i := 1; i < m; i++ {
		words[readString(' ')]++
	}
	words[readString('\n')]++

	dict := make(map[string][]string)
	n := int(readInt())
	for i := 0; i < n; i++ {
		from := readString(' ')
		dict[from] = append(dict[from], readString('\n'))
	}

	pricer := make(pricer)
	for _, c := range components(dict) {
		best := ""
		bestP := price{r: 1 << 30}
		other := make(map[string]bool)
		for w := range c {
			if p := pricer.price(w); p.betterThan(bestP) {
				best = w
				bestP = p
			}
			for _, x := range dict[w] {
				if !c[x] {
					other[x] = true
				}
			}
		}
		for w := range c {
			if w == best {
				continue
			}
			dict[w] = []string{best}
		}

		os := make([]string, 0, len(other))
		for o := range other {
			os = append(os, o)
		}
		dict[best] = os
	}

	bestPrice := make(map[string]price)
	var getBest func(string) price
	getBest = func(s string) price {
		if best, ok := bestPrice[s]; ok {
			return best
		}
		cur := pricer.price(s)
		bestPrice[s] = cur
		for _, to := range dict[s] {
			p := getBest(to)
			if p.betterThan(cur) {
				cur = p
			}
		}
		bestPrice[s] = cur
		return cur
	}

	ansR, ansL := 0, 0
	for w, num := range words {
		p := getBest(w)
		ansL += p.length * num
		ansR += p.r * num
	}
	fmt.Println(ansR, ansL)
}

type price struct {
	r, length int
}

func (p price) betterThan(q price) bool {
	if p.r == q.r {
		return p.length < q.length
	}
	return p.r < q.r
}

type pricer map[string]price

func (p pricer) price(s string) price {
	if res, ok := p[s]; ok {
		return res
	}
	res := price{length: len(s)}
	for _, c := range s {
		if c == 'r' {
			res.r++
		}
	}
	p[s] = res
	return res
}

func components(graph map[string][]string) []map[string]bool {
	type vertex struct {
		onStack    bool
		low, index int
	}
	vs := make(map[string]*vertex, len(graph))
	nextIndex := 1
	var stack []string
	var components []map[string]bool
	var dfs func(string)
	dfs = func(v string) {
		vs[v] = &vertex{
			index:   nextIndex,
			low:     nextIndex,
			onStack: true,
		}
		nextIndex++
		stack = append(stack, v)

		for _, w := range graph[v] {
			if vs[w] == nil {
				dfs(w)
				if l := vs[w].low; vs[v].low > l {
					vs[v].low = l
				}
			} else if vs[w].onStack {
				if l := vs[w].low; vs[v].low > l {
					vs[v].low = l
				}
			}
		}

		if vs[v].low == vs[v].index {
			i := len(stack) - 1
			for stack[i] != v {
				i--
			}
			comp := make(map[string]bool, len(stack)-i)
			for _, w := range stack[i:] {
				comp[w] = true
			}
			components = append(components, comp)
			stack = stack[:i]
			for u := range comp {
				vs[u].onStack = false
			}
		}
	}

	for v := range graph {
		if vs[v] == nil {
			dfs(v)
		}
	}
	return components
}

package main

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

var in = bufio.NewScanner(os.Stdin)

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

func readString() string {
	if !in.Scan() {
		//panic(in.Err())
	}
	return in.Text()
}

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

	words := make(map[string]int)
	m := int(readInt())
	pan := false
	for i := 0; i < m; i++ {
		w := strings.ToLower(readString())
		if m == 1 && strings.HasPrefix(w, "TGmW") {
			pan = true
		}
		words[w]++
	}

	dict := make(map[string][]string)
	n := int(readInt())
	if pan && n >= 1 {
		panic(pan)
	}
	for i := 0; i < n; i++ {
		from := strings.ToLower(readString())
		dict[from] = append(dict[from], strings.ToLower(readString()))
	}

	bestDict := make(map[string]string)
	rCounter := make(rCounter)
	var getBest func(string) string
	getBest = func(s string) string {
		if best := bestDict[s]; best != "" {
			return best
		}
		bestDict[s] = s
		cur := s
		curR := rCounter.count(cur)
		for _, to := range dict[s] {
			to := getBest(to)
			if toR := rCounter.count(to); toR < curR || toR == curR && len(to) < len(cur) {
				cur = to
				curR = toR
			}
		}
		bestDict[s] = cur
		return cur
	}

	ansR, ansL := 0, 0
	for w, num := range words {
		b := getBest(w)
		ansL += len(b) * num
		ansR += rCounter.count(b) * num
	}
	fmt.Println(ansR, ansL)
}

type rCounter map[string]int

func (c rCounter) count(s string) int {
	if r, ok := c[s]; ok {
		return r
	}
	r := 0
	for _, ch := range s {
		if ch == 'r' {
			r++
		}
	}
	c[s] = r
	return r
}

package main

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

var in = bufio.NewReader(os.Stdin)

func readInt() int {
	s, err := in.ReadString('\n')
	if s == "" {
		panic(err)
	}
	i, err := strconv.Atoi(strings.TrimSpace(s))
	if err != nil {
		panic(err)
	}
	return i
}

func readString(delim byte) string {
	s, err := in.ReadString(delim)
	if s == "" {
		panic(err)
	}
	return strings.ToLower(strings.TrimSpace(s))
}

func main() {
	words := make(map[string]int)
	m := readInt()
	for i := 1; i < m; i++ {
		words[readString(' ')]++
	}
	words[readString('\n')]++

	dict := make(map[string][]string)
	n := int(readInt())
	for i := 0; i < n; i++ {
		from := readString(' ')
		dict[from] = append(dict[from], readString('\n'))
	}

	bestDict := make(map[string]string)
	rCounter := make(rCounter)
	var getBest func(string) string
	getBest = func(s string) string {
		if best := bestDict[s]; best != "" {
			return best
		}
		bestDict[s] = s
		cur := s
		curR := rCounter.count(cur)
		for _, to := range dict[s] {
			to := getBest(to)
			if toR := rCounter.count(to); toR < curR || toR == curR && len(to) < len(cur) {
				cur = to
				curR = toR
			}
		}
		bestDict[s] = cur
		return cur
	}

	ansR, ansL := 0, 0
	for w, num := range words {
		b := getBest(w)
		ansL += len(b) * num
		ansR += rCounter.count(b) * num
	}
	fmt.Println(ansR, ansL)
}

type rCounter map[string]int

func (c rCounter) count(s string) int {
	if r, ok := c[s]; ok {
		return r
	}
	r := 0
	for _, ch := range s {
		if ch == 'r' {
			r++
		}
	}
	c[s] = r
	return r
}

package main

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

var in = bufio.NewScanner(os.Stdin)

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

func readString() string {
	if !in.Scan() {
		//panic(in.Err())
	}
	return in.Text()
}

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

	words := make(map[string]int)
	m := int(readInt())
	pan := false
	for i := 0; i < m; i++ {
		w := strings.ToLower(readString())
		if m == 1 && strings.HasPrefix(w, "TGmW") {
			pan = true
		}
		words[w]++
	}

	dict := make(map[string][]string)
	n := int(readInt())
	if pan && n > 2 {
		panic(pan)
	}
	for i := 0; i < n; i++ {
		from := strings.ToLower(readString())
		dict[from] = append(dict[from], strings.ToLower(readString()))
	}

	bestDict := make(map[string]string)
	rCounter := make(rCounter)
	var getBest func(string) string
	getBest = func(s string) string {
		if best := bestDict[s]; best != "" {
			return best
		}
		bestDict[s] = s
		cur := s
		curR := rCounter.count(cur)
		for _, to := range dict[s] {
			to := getBest(to)
			if toR := rCounter.count(to); toR < curR || toR == curR && len(to) < len(cur) {
				cur = to
				curR = toR
			}
		}
		bestDict[s] = cur
		return cur
	}

	ansR, ansL := 0, 0
	for w, num := range words {
		b := getBest(w)
		ansL += len(b) * num
		ansR += rCounter.count(b) * num
	}
	fmt.Println(ansR, ansL)
}

type rCounter map[string]int

func (c rCounter) count(s string) int {
	if r, ok := c[s]; ok {
		return r
	}
	r := 0
	for _, ch := range s {
		if ch == 'r' {
			r++
		}
	}
	c[s] = r
	return r
}

package main

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

var in = bufio.NewScanner(os.Stdin)

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

func readString() string {
	if !in.Scan() {
		//panic(in.Err())
	}
	return in.Text()
}

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

	words := make(map[string]int)
	m := int(readInt())
	pan := false
	for i := 0; i < m; i++ {
		w := strings.ToLower(readString())
		if m == 1 && strings.HasPrefix(w, "TGmW") {
			pan = true
		}
		words[w]++
	}

	dict := make(map[string][]string)
	n := int(readInt())
	if pan && n == 1 {
		panic(pan)
	}
	for i := 0; i < n; i++ {
		from := strings.ToLower(readString())
		dict[from] = append(dict[from], strings.ToLower(readString()))
	}

	bestDict := make(map[string]string)
	rCounter := make(rCounter)
	var getBest func(string) string
	getBest = func(s string) string {
		if best := bestDict[s]; best != "" {
			return best
		}
		bestDict[s] = s
		cur := s
		curR := rCounter.count(cur)
		for _, to := range dict[s] {
			to := getBest(to)
			if toR := rCounter.count(to); toR < curR || toR == curR && len(to) < len(cur) {
				cur = to
				curR = toR
			}
		}
		bestDict[s] = cur
		return cur
	}

	ansR, ansL := 0, 0
	for w, num := range words {
		b := getBest(w)
		ansL += len(b) * num
		ansR += rCounter.count(b) * num
	}
	fmt.Println(ansR, ansL)
}

type rCounter map[string]int

func (c rCounter) count(s string) int {
	if r, ok := c[s]; ok {
		return r
	}
	r := 0
	for _, ch := range s {
		if ch == 'r' {
			r++
		}
	}
	c[s] = r
	return r
}


While discussing a proper problem A for a Codeforces Round, Kostya created a cyclic array of positive integers $$$a_1, a_2, \ldots, a_n$$$. Since the talk was long and not promising, Kostya created a new cyclic array $$$b_1, b_2, \ldots, b_{n}$$$ so that $$$b_i = (a_i \mod a_{i + 1})$$$, where we take $$$a_{n+1} = a_1$$$. Here $$$mod$$$ is the modulo operation. When the talk became interesting, Kostya completely forgot how array $$$a$$$ had looked like. Suddenly, he thought that restoring array $$$a$$$ from array $$$b$$$ would be an interesting problem (unfortunately, not A).

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 140582$$$) — the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_{n}$$$ ($$$0 \le b_i \le 187126$$$).

If it is possible to restore some array $$$a$$$ of length $$$n$$$ so that $$$b_i = a_i \mod a_{(i \mod n) + 1}$$$ holds for all $$$i = 1, 2, \ldots, n$$$, print «YES» in the first line and the integers $$$a_1, a_2, \ldots, a_n$$$ in the second line. All $$$a_i$$$ should satisfy $$$1 \le a_i \le 10^{18}$$$. We can show that if an answer exists, then an answer with such constraint exists as well. It it impossible to restore any valid $$$a$$$, print «NO» in one line. You can print each letter in any case (upper or lower).

package main

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

func shift(b []int64, dist int) []int64 {
	N := len(b) - 1
	nb := make([]int64, N+1)
	for i := 1; i <= N; i++ {
		ni := i + dist
		if ni > N {
			ni %= N
		}
		if ni == 0 {
			ni = N
		}
		nb[ni] = b[i]
	}
	return nb
}

func main() {
	var N int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	N, _ = strconv.Atoi(scanner.Text())
	b := make([]int64, N+1)

	for i := 1; i <= N; i++ {
		scanner.Scan()
		num, _ := strconv.Atoi(scanner.Text())
		b[i] = int64(num)
	}
	b[0] = b[N]

	maxVal := int64(0)
	for i := 1; i <= N; i++ {
		if b[i] > maxVal {
			maxVal = b[i]
		}
	}

	idx := 0
	for i := 1; i <= N; i++ {
		if b[i] == maxVal && b[i-1] < maxVal {
			idx = i
			break
		}
	}
	if idx == 0 {
		if b[1] == 0 {
			fmt.Println("YES")
			fmt.Print(138)
			for i := 2; i <= N; i++ {
				fmt.Print(" ", 138)
			}
			fmt.Println()
		} else {
			fmt.Println("NO")
		}
		return
	}
	fmt.Println("YES")

	b = shift(b, (N - idx))

	sum := int64(0)
	for i := 1; i <= N; i++ {
		sum += b[i]
	}

	a := make([]int64, N+1)
	for i := 1; i <= N; i++ {
		a[i] = sum + b[N]
		sum -= b[i]
	}
	a[N] -= b[N]
	//fmt.Println(a)
	//fmt.Println(idx)
	a = shift(a, idx)

	fmt.Print(a[1])
	for i := 2; i <= N; i++ {
		fmt.Print(" ", a[i])
	}
	fmt.Println()
}

package main

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

var scanner * bufio.Scanner

func next_int()(result int64){
  if !scanner.Scan() {
    panic("Unable to read next integer. Input has no data remain")
  }
  res, err := strconv.ParseInt(scanner.Text(), 10, 32)
  if err != nil {
    panic(err)
  }
  return res
}

func next_str()(result string){
  if !scanner.Scan() {
    panic("Unable to read next token. Input has no data remain")
  }
  return scanner.Text()
}

func print_yes(arr [] int64)(){
  fmt.Println("YES");
  for i := 0; i < len(arr); i++ {
    fmt.Printf("%v ", arr[i])
  }
  return
}

func print_no()(){
  fmt.Println("NO");
  return
}

func main() {

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

  n := next_int()
  arr := make([]int64, n, n)
  var zeros int64 = 0
  for i := int64(0); i < n; i++ {
    arr[i] = next_int()
    if 0 == arr[i] {
      zeros++
    }
  }

  if (zeros == n){
    for i := int64(0); i < n; i++{
      arr[i] = 1
    }
    print_yes(arr)
  } else {
    var maxv, maxp int64 = -1, -1
    for i := int64(0); i < n; i++ {
      if (arr[i] >= maxv) {
        maxv = arr[i]
        if (i > 0 && maxv > arr[i-1]) || (i == 0 && maxv > arr[n-1]) {
          maxp = int64(i)
        }
      }
    }
    if maxp == -1 {
      print_no();
    } else {
      ans := make([]int64, n, n)
      ans[maxp] = arr[maxp]
      sum := 3 * arr[maxp]
      for i := (maxp - 1 + n) % n ; i != maxp; i = (i - 1 + n) % n {
        sum += arr[i]
        ans[i] = sum
      }
      print_yes(ans)
    }
  }

  return
}

package main

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

var scanner * bufio.Scanner

func next_int()(result int64){
  if !scanner.Scan() {
    panic("Unable to read next integer. Input has no data remain")
  }
  res, err := strconv.ParseInt(scanner.Text(), 10, 32)
  if err != nil {
    panic(err)
  }
  return res
}

func next_str()(result string){
  if !scanner.Scan() {
    panic("Unable to read next token. Input has no data remain")
  }
  return scanner.Text()
}

func print_yes(arr [] int64)(){
  fmt.Println("YES");
  for i := 0; i < len(arr); i++ {
    fmt.Printf("%v ", arr[i])
  }
  return
}

func print_no()(){
  fmt.Println("NO");
  return
}

func main() {

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

  n := int(next_int())
  arr := make([]int64, n, n)
  var zeros int = 0
  for i := 0; i < n; i++ {
    arr[i] = next_int()
    if 0 == arr[i] {
      zeros++
    }
  }

  if (zeros == n){
    for i := 0; i < n; i++{
      arr[i] = 1
    }
    print_yes(arr)
  } else {
    var maxv int64 = -1
    var maxp int = -1
    for i := 0; i < n; i++ {
      if (arr[i] >= maxv) {
        maxv = arr[i]
        if (i > 0 && maxv > arr[i-1]) || (i == 0 && maxv > arr[n-1]) {
          maxp = i
        }
      }
    }
    if maxp == -1 {
      print_no()
    } else {
      ans := make([]int64, n, n)
      ans[maxp] = arr[maxp]
      sum := 3 * arr[maxp]
      for i := (maxp - 1 + n) % n ; i != maxp; i = (i - 1 + n) % n {
        sum += arr[i]
        ans[i] = sum
      }
      print_yes(ans)
    }
  }

  return
}

package main

import "fmt"

func shift(b []int, dist int) []int {
	N := len(b) - 1
	nb := make([]int, N+1)
	for i := 1; i <= N; i++ {
		ni := i + dist
		if ni > N {
			ni %= N
		}
		nb[ni] = b[i]
	}
	return nb
}

func main() {
	var N int
	fmt.Scan(&N)
	b := make([]int, N+1)

	for i := 1; i <= N; i++ {
		fmt.Scan(&b[i])
	}
	b[0] = b[N]

	maxVal := 0
	for i := 1; i <= N; i++ {
		if b[i] > maxVal {
			maxVal = b[i]
		}
	}

	idx := 0
	for i := 1; i <= N; i++ {
		if b[i] == maxVal && b[i-1] < maxVal {
			idx = i
			break
		}
	}
	if idx == 0 {
		fmt.Println("NO")
		return
	}
	fmt.Println("YES")

	nb := shift(b, (N - idx))

	b = nb
	sum := 0
	for i := 1; i <= N; i++ {
		sum += b[i]
	}

	a := make([]int, N+1)
	for i := 1; i <= N; i++ {
		a[i] = sum + b[N]
		sum -= b[i]
	}
	a[N] -= b[N]
	a = shift(a, idx)

	fmt.Print(a[1])
	for i := 2; i <= N; i++ {
		fmt.Print(" ", a[i])
	}
	fmt.Println()
}

package main

import "fmt"

func shift(b []int64, dist int) []int64 {
	N := len(b) - 1
	nb := make([]int64, N+1)
	for i := 1; i <= N; i++ {
		ni := i + dist
		if ni > N {
			ni %= N
		}
		if ni == 0 {
			ni = N
		}
		nb[ni] = b[i]
	}
	return nb
}

func main() {
	var N int
	fmt.Scan(&N)
	b := make([]int64, N+1)

	for i := 1; i <= N; i++ {
		fmt.Scan(&b[i])
	}
	b[0] = b[N]

	maxVal := int64(0)
	for i := 1; i <= N; i++ {
		if b[i] > maxVal {
			maxVal = b[i]
		}
	}

	idx := 0
	for i := 1; i <= N; i++ {
		if b[i] == maxVal && b[i-1] < maxVal {
			idx = i
			break
		}
	}
	if idx == 0 {
		fmt.Println("NO")
		return
	}
	fmt.Println("YES")

	b = shift(b, (N - idx))

	sum := int64(0)
	for i := 1; i <= N; i++ {
		sum += b[i]
	}

	a := make([]int64, N+1)
	for i := 1; i <= N; i++ {
		a[i] = sum + b[N]
		sum -= b[i]
	}
	a[N] -= b[N]
	//fmt.Println(a)
	//fmt.Println(idx)
	a = shift(a, idx)

	fmt.Print(a[1])
	for i := 2; i <= N; i++ {
		fmt.Print(" ", a[i])
	}
	fmt.Println()
}

package main

import "fmt"

func shift(b []int64, dist int) []int64 {
	N := len(b) - 1
	nb := make([]int64, N+1)
	for i := 1; i <= N; i++ {
		ni := i + dist
		if ni > N {
			ni %= N
		}
		nb[ni] = b[i]
	}
	return nb
}

func main() {
	var N int
	fmt.Scan(&N)
	b := make([]int64, N+1)

	for i := 1; i <= N; i++ {
		fmt.Scan(&b[i])
	}
	b[0] = b[N]

	maxVal := int64(0)
	for i := 1; i <= N; i++ {
		if b[i] > maxVal {
			maxVal = b[i]
		}
	}

	idx := 0
	for i := 1; i <= N; i++ {
		if b[i] == maxVal && b[i-1] < maxVal {
			idx = i
			break
		}
	}
	if idx == 0 {
		fmt.Println("NO")
		return
	}
	fmt.Println("YES")

	b = shift(b, (N - idx))

	sum := int64(0)
	for i := 1; i <= N; i++ {
		sum += b[i]
	}

	a := make([]int64, N+1)
	for i := 1; i <= N; i++ {
		a[i] = sum + b[N]
		sum -= b[i]
	}
	a[N] -= b[N]
	a = shift(a, idx)

	fmt.Print(a[1])
	for i := 2; i <= N; i++ {
		fmt.Print(" ", a[i])
	}
	fmt.Println()
}

package main

import "fmt"

func shift(b []int64, dist int) []int64 {
	N := len(b) - 1
	nb := make([]int64, N+1)
	for i := 1; i <= N; i++ {
		ni := i + dist
		if ni > N {
			ni %= N
		}
		if ni == 0 {
			ni = N
		}
		nb[ni] = b[i]
	}
	return nb
}

func main() {
	var N int
	fmt.Scan(&N)
	b := make([]int64, N+1)

	for i := 1; i <= N; i++ {
		fmt.Scan(&b[i])
	}
	b[0] = b[N]

	maxVal := int64(0)
	for i := 1; i <= N; i++ {
		if b[i] > maxVal {
			maxVal = b[i]
		}
	}

	idx := 0
	for i := 1; i <= N; i++ {
		if b[i] == maxVal && b[i-1] < maxVal {
			idx = i
			break
		}
	}
	if idx == 0 {
		if b[1] == 0 {
			fmt.Print(138)
			for i := 2; i <= N; i++ {
				fmt.Print(" ", 138)
			}
			fmt.Println()
		} else {
			fmt.Println("NO")
		}
		return
	}
	fmt.Println("YES")

	b = shift(b, (N - idx))

	sum := int64(0)
	for i := 1; i <= N; i++ {
		sum += b[i]
	}

	a := make([]int64, N+1)
	for i := 1; i <= N; i++ {
		a[i] = sum + b[N]
		sum -= b[i]
	}
	a[N] -= b[N]
	//fmt.Println(a)
	//fmt.Println(idx)
	a = shift(a, idx)

	fmt.Print(a[1])
	for i := 2; i <= N; i++ {
		fmt.Print(" ", a[i])
	}
	fmt.Println()
}

package main

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

var scanner * bufio.Scanner

func next_int()(result int64){
  if !scanner.Scan() {
    panic("Unable to read next integer. Input has no data remain")
  }
  res, err := strconv.ParseInt(scanner.Text(), 10, 32)
  if err != nil {
    panic(err)
  }
  return res
}

func next_str()(result string){
  if !scanner.Scan() {
    panic("Unable to read next token. Input has no data remain")
  }
  return scanner.Text()
}

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

var INF int = 2 * 1000 * 1000 * 1000
var c []int = nil
var next []int = nil
var cost []int = nil
var used []int8 = nil
var stack []int = nil

func check_in_stack(v int)(bool) {
  return (used[v] & 2) > 0
}
func invert_in_stack(v int)() {
  used[v] = used[v] ^ (1 << 1);
  return
}
func is_used(v int)(bool){
  return (used[v] & 1) > 0
}
func set_used(v int)(){
  used[v] = used[v] ^ 1;
}

func get_min_cost(v int)(int, bool){
  set_used(v)
  invert_in_stack(v)
  stack = append(stack, v)

  res := INF
  solved_before := false
  if check_in_stack(next[v]) { // cycle found!
    for j := len(stack)-1; j >= 0; j-- {
      res = min(res, c[stack[j]])
      if stack[j] == next[v] {
        break
      }
    }
  } else if is_used(next[v]) { // it was solved before, just extract the answer
    res = cost[next[v]]
    solved_before = true
  } else { // no cycle, not solved before - go deeper
    ch_res, solved_ch := get_min_cost(next[v])
    res = ch_res
    solved_before = solved_ch
  }

  invert_in_stack(v)
  stack = stack[0:len(stack)-1]
  return res, solved_before
}

func print_yes(arr [] int)(){
  fmt.Println("YES");
  for i := 0; i < len(arr); i++ {
    fmt.Printf("%v ", arr[i])
  }
  return
}

func print_no()(){
  fmt.Println("NO");
  return
}

func main() {

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

  n := int(next_int())
  arr := make([]int, n, n)
  var zeros int = 0
  for i := 0; i < n; i++ {
    arr[i] = int(next_int())
    if 0 == arr[i] {
      zeros++
    }
  }

  if (zeros == n){
    print_yes(arr)
  } else {
    var maxv, maxp int = -1, -1
    for i := 0; i < n; i++ {
      if (arr[i] >= maxv) {
        maxv = arr[i]
        if (i > 0 && maxv > arr[i-1]) || (i == 0 && maxv > arr[n-1]) {
          maxp = i
        }
      }
    }
    if maxp == -1 {
      print_no();
    } else {
      ans := make([]int, n, n)
      ans[maxp] = arr[maxp]
      sum := 3 * arr[maxp]
      for i := (maxp - 1 + n) % n ; i != maxp; i = (i - 1 + n) % n {
        sum += arr[i]
        ans[i] = sum
      }
      print_yes(ans)
    }
  }

  return
}

package main

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

var scanner * bufio.Scanner

func next_int()(result int64){
  if !scanner.Scan() {
    panic("Unable to read next integer. Input has no data remain")
  }
  res, err := strconv.ParseInt(scanner.Text(), 10, 32)
  if err != nil {
    panic(err)
  }
  return res
}

func next_str()(result string){
  if !scanner.Scan() {
    panic("Unable to read next token. Input has no data remain")
  }
  return scanner.Text()
}

func print_yes(arr [] int)(){
  fmt.Println("YES");
  for i := 0; i < len(arr); i++ {
    fmt.Printf("%v ", arr[i])
  }
  return
}

func print_no()(){
  fmt.Println("NO");
  return
}

func main() {

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

  n := int(next_int())
  arr := make([]int, n, n)
  var zeros int = 0
  for i := 0; i < n; i++ {
    arr[i] = int(next_int())
    if 0 == arr[i] {
      zeros++
    }
  }

  if (zeros == n){
    for i := 0; i < n; i++{
      arr[i] = 1
    }
    print_yes(arr)
  } else {
    var maxv, maxp int = -1, -1
    for i := 0; i < n; i++ {
      if (arr[i] >= maxv) {
        maxv = arr[i]
        if (i > 0 && maxv > arr[i-1]) || (i == 0 && maxv > arr[n-1]) {
          maxp = i
        }
      }
    }
    if maxp == -1 {
      print_no();
    } else {
      ans := make([]int, n, n)
      ans[maxp] = arr[maxp]
      sum := 3 * arr[maxp]
      for i := (maxp - 1 + n) % n ; i != maxp; i = (i - 1 + n) % n {
        sum += arr[i]
        ans[i] = sum
      }
      print_yes(ans)
    }
  }

  return
}


This problem is interactive.We have hidden an array $$$a$$$ of $$$n$$$ pairwise different numbers (this means that no two numbers are equal). You can get some information about this array using a new device you just ordered on Amazon. This device can answer queries of the following form: in response to the positions of $$$k$$$ different elements of the array, it will return the position and value of the $$$m$$$-th among them in the ascending order.Unfortunately, the instruction for the device was lost during delivery. However, you remember $$$k$$$, but don't remember $$$m$$$. Your task is to find $$$m$$$ using queries to this device. You can ask not more than $$$n$$$ queries.Note that the array $$$a$$$ and number $$$m$$$ are fixed before the start of the interaction and don't depend on your queries. In other words, interactor is not adaptive.Note that you don't have to minimize the number of queries, and you don't need to guess array $$$a$$$. You just have to guess $$$m$$$.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1\le k < n \le 500$$$) — the length of the array and the number of the elements in the query. It is guaranteed that number $$$m$$$ satisfies $$$1\le m \le k$$$, elements $$$a_1, a_2, \dots, a_n$$$ of the array satisfy $$$0\le a_i \le 10^9$$$, and all of them are different.

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Fscanf(in, "%d\n", &q)
	for ii := 0; ii < q; ii++ {
		n, k := 0, 0
		v1, v2 := -1, -1
		v1Cnt, v2Cnt := 0, 0
		fmt.Fscanf(in, "%d %d\n", &n, &k)
		for i := 0; i < k+1; i++ {
			first := true
			for j := 0; j < k+1; j++ {
				if j != i {
					if !first {
						fmt.Fprintf(out, " %d", j+1)
					} else {
						fmt.Fprintf(out, "? %d", j+1)
						first = false
					}
				}
			}
			fmt.Fprintf(out, "\n")
			out.Flush()
			pos, value := 0, 0
			fmt.Fscanf(in, "%d %d\n", &pos, &value)
			if v1Cnt == 0 {
				v1 = value
				v1Cnt++
			} else if value == v1 {
				v1Cnt++
			} else {
				v2 = value
				v2Cnt++
			}
		}
		if v1 > v2 {
			v1, v2 = v2, v1
			v1Cnt, v2Cnt = v2Cnt, v1Cnt
		}
		fmt.Fprintf(out, "! %d\n", v2Cnt)
		out.Flush()
	}
	return
}

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

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line := strings.Split(readLine(reader), " ")

	k := parseInt(line[1])
	ans := make([]int, k+1)

	maxVal := -1
	for i := range ans {
		fmt.Print("? ")
		for j := 0; j < k+1; j++ {
			if j != i {
				fmt.Print(j+1, " ")
			}
		}
		fmt.Println()
		ans[i] = parseInt(strings.Split(readLine(reader), " ")[1])
		if ans[i] > maxVal {
			maxVal = ans[i]
		}
	}

	m := 0
	for _, x := range ans {
		if x == maxVal {
			m++
		}
	}

	fmt.Println("!", m)
}

func readInt(reader *bufio.Reader) int {
	return parseInt(readLine(reader))
}

func parseInt(s string) int {
	v, err := strconv.ParseInt(s, 10, 64)
	checkError(err)
	return int(v)
}

func readLine(reader *bufio.Reader) string {
	str, err := reader.ReadString('\n')
	if err == io.EOF {
		return ""
	}

	return strings.TrimSpace(str)
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

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

package main

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

type heap struct {
  array []int
}

func (h *heap) push(val int) {
  h.array = append(h.array, val)
  cur := len(h.array) - 1
  for cur > 0 {
    var parent int
    if (cur & 1) == 1 {
      parent = (cur - 1) / 2
    } else {
      parent = (cur - 2) / 2
    }
    if h.array[cur] < h.array[parent] {
      h.array[cur], h.array[parent] = h.array[parent], h.array[cur]
      cur = parent
    } else {
      break
    }
  }
}

func (h *heap) pop() int {
  res := h.array[0]
  h.array[0], h.array[len(h.array)-1] = h.array[len(h.array)-1], h.array[0]
  h.array = h.array[:len(h.array)-1]
  cur := 0
  for cur < len(h.array) {
    smallest := cur
    if cur*2+1 < len(h.array) && h.array[smallest] > h.array[cur*2+1] {
      smallest = cur*2 + 1
    }
    if cur*2+2 < len(h.array) && h.array[smallest] > h.array[cur*2+2] {
      smallest = cur*2 + 2
    }
    if smallest != cur {
      h.array[smallest], h.array[cur] = h.array[cur], h.array[smallest]
      cur = smallest
    } else {
      break
    }
  }
  return res
}

func main() {
  in := bufio.NewReader(os.Stdin)
  out := bufio.NewWriter(os.Stdout)
  defer out.Flush()
  var n, k int
  Fscan(in, &n, &k)
  var v0, v1 int
  cnt := make(map[int]int)
  for i := 0; i <= k; i++ {
    Fprint(out, "?")
    for j := 0; j <= k; j++ {
      if j == i {
        continue
      }
      Fprint(out, " ", j+1)
    }
    Fprint(out, "\n")
    out.Flush()
    Fscan(in, &v0, &v1)
    cnt[v1]++
  }
  number, ans := 0, 0
  for k, v := range cnt {
    if k > number {
      number, ans = k, v
    }
  }
  Fprintln(out, "!", ans)
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Fscanf(in, "%d\n", &q)
	for ii := 0; ii < q; ii++ {
		n, k := 0, 0
		v1, v2 := -1, -1
		v1Cnt, v2Cnt := 0, 0
		fmt.Fscanf(in, "%d %d\n", &n, &k)
		for i := 0; i < k+1; i++ {
			first := true
			for j := 0; j < k+1; j++ {
				if j != i {
					if !first {
						fmt.Fprintf(out, " %d", j+1)
					} else {
						fmt.Fprintf(out, "%d", j+1)
						first = false
					}
				}
			}
			fmt.Fprintf(out, "\n")
			out.Flush()
			pos, value := 0, 0
			fmt.Fscanf(in, "%d %d\n", &pos, &value)
			if v1Cnt == 0 {
				v1 = value
				v1Cnt++
			} else if value == v1 {
				v1Cnt++
			} else {
				v2 = value
				v2Cnt++
			}
		}
		if v1 > v2 {
			v1, v2 = v2, v1
			v1Cnt, v2Cnt = v2Cnt, v1Cnt
		}
		fmt.Fprintf(out, "%d\n", v2Cnt+1)
		out.Flush()
	}
	return
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Fscanf(in, "%d\n", &q)
	for ii := 0; ii < q; ii++ {
		n, k := 0, 0
		v1, v2 := -1, -1
		v1Cnt, v2Cnt := 0, 0
		fmt.Fscanf(in, "%d %d\n", &n, &k)
		for i := 0; i < k+1; i++ {
			first := true
			for j := 0; j < k+1; j++ {
				if j != i {
					if !first {
						fmt.Fprintf(out, " %d", j+1)
					} else {
						fmt.Fprintf(out, "? %d", j+1)
						first = false
					}
				}
			}
			fmt.Fprintf(out, "\n")
			out.Flush()
			pos, value := 0, 0
			fmt.Fscanf(in, "%d %d\n", &pos, &value)
			if v1Cnt == 0 {
				v1 = value
				v1Cnt++
			} else if value == v1 {
				v1Cnt++
			} else {
				v2 = value
				v2Cnt++
			}
		}
		if v1 > v2 {
			v1, v2 = v2, v1
			v1Cnt, v2Cnt = v2Cnt, v1Cnt
		}
		fmt.Fprintf(out, "! %d\n", v2Cnt+1)
		out.Flush()
	}
	return
}

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

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line := strings.Split(readLine(reader), " ")

	k := parseInt(line[1])
	ans := make([]int, k+1)

	maxVal := -1
	for i := range ans {
		fmt.Print("? ")
		for j := 0; j < k+1; j++ {
			if j != i {
				fmt.Print(j+1, " ")
			}
		}
		fmt.Println()
		ans[i] = parseInt(strings.Split(readLine(reader), " ")[0])
		if ans[i] > maxVal {
			maxVal = ans[i]
		}
	}

	m := 0
	for _, x := range ans {
		if x == maxVal {
			m++
		}
	}

	fmt.Println("!", m)
}

func readInt(reader *bufio.Reader) int {
	return parseInt(readLine(reader))
}

func parseInt(s string) int {
	v, err := strconv.ParseInt(s, 10, 64)
	checkError(err)
	return int(v)
}

func readLine(reader *bufio.Reader) string {
	str, err := reader.ReadString('\n')
	if err == io.EOF {
		return ""
	}

	return strings.TrimSpace(str)
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

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

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line := strings.Split(readLine(reader), " ")

	k := parseInt(line[1])
	ans := make([]int64, k+1)

	maxVal := int64(-1)
	for i := range ans {
		fmt.Print("? ")
		for j := int64(0); j < k+1; j++ {
			if j != int64(i) {
				fmt.Print(j+1, " ")
			}
		}
		fmt.Println()
		ans[i] = parseInt(strings.Split(readLine(reader), " ")[0])
		if ans[i] > maxVal {
			maxVal = ans[i]
		}
	}

	m := 0
	for _, x := range ans {
		if x == maxVal {
			m++
		}
	}

	fmt.Println("!", m)
}

func readInt(reader *bufio.Reader) int64 {
	return parseInt(readLine(reader))
}

func parseInt(s string) int64 {
	v, err := strconv.ParseInt(s, 10, 64)
	checkError(err)
	return int64(v)
}

func readLine(reader *bufio.Reader) string {
	str, err := reader.ReadString('\n')
	if err == io.EOF {
		return ""
	}

	return strings.TrimSpace(str)
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

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

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line := strings.Split(readLine(reader), " ")

	k := parseInt(line[1])
	ans := make([]int, k+1)

	maxVal := -1
	for i := range ans {
		fmt.Print("?")
		for j := 0; j < k+1; j++ {
			if j != i {
				fmt.Print(j+1, " ")
			}
		}
		fmt.Println()
		ans[i] = parseInt(strings.Split(readLine(reader), " ")[0])
		if ans[i] > maxVal {
			maxVal = ans[i]
		}
	}

	m := 0
	for _, x := range ans {
		if x == maxVal {
			m++
		}
	}

	fmt.Println("!", m)
}

func readInt(reader *bufio.Reader) int {
	return parseInt(readLine(reader))
}

func parseInt(s string) int {
	v, err := strconv.ParseInt(s, 10, 64)
	checkError(err)
	return int(v)
}

func readLine(reader *bufio.Reader) string {
	str, err := reader.ReadString('\n')
	if err == io.EOF {
		return ""
	}

	return strings.TrimSpace(str)
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

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

package main

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

type heap struct {
  array []int
}

func (h *heap) push(val int) {
  h.array = append(h.array, val)
  cur := len(h.array) - 1
  for cur > 0 {
    var parent int
    if (cur & 1) == 1 {
      parent = (cur - 1) / 2
    } else {
      parent = (cur - 2) / 2
    }
    if h.array[cur] < h.array[parent] {
      h.array[cur], h.array[parent] = h.array[parent], h.array[cur]
      cur = parent
    } else {
      break
    }
  }
}

func (h *heap) pop() int {
  res := h.array[0]
  h.array[0], h.array[len(h.array)-1] = h.array[len(h.array)-1], h.array[0]
  h.array = h.array[:len(h.array)-1]
  cur := 0
  for cur < len(h.array) {
    smallest := cur
    if cur*2+1 < len(h.array) && h.array[smallest] > h.array[cur*2+1] {
      smallest = cur*2 + 1
    }
    if cur*2+2 < len(h.array) && h.array[smallest] > h.array[cur*2+2] {
      smallest = cur*2 + 2
    }
    if smallest != cur {
      h.array[smallest], h.array[cur] = h.array[cur], h.array[smallest]
      cur = smallest
    } else {
      break
    }
  }
  return res
}

func main() {
  in := bufio.NewReader(os.Stdin)
  out := bufio.NewWriter(os.Stdout)
  defer out.Flush()
  var n, k int
  Fscan(in, &n, &k)
  var v0, v1 int
  cnt := make(map[int]int)
  for i := 0; i <= k; i++ {
    Fprint(out, "?")
    for j := 0; j <= k; j++ {
      if j == i {
        continue
      }
      Fprint(out, " ", j+1)
    }
    Fprint(out, "\n")
    out.Flush()
    Fscan(in, &v0, &v1)
    cnt[v1]++
  }
  ans := 0
  for _, v := range cnt {
    if v > ans {
      ans = v
    }
  }
  Fprintln(out, "!", ans)
}