Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very interesting tree rooted at vertex 1. There is an integer number written on each vertex of the tree; the number written on vertex i is equal to ai.Ilya believes that the beauty of the vertex x is the greatest common divisor of all numbers written on the vertices on the path from the root to x, including this vertex itself. In addition, Ilya can change the number in one arbitrary vertex to 0 or leave all vertices unchanged. Now for each vertex Ilya wants to know the maximum possible beauty it can have.For each vertex the answer must be considered independently.The beauty of the root equals to number written on it.

First line contains one integer number n — the number of vertices in tree (1 ≤ n ≤ 2·105). Next line contains n integer numbers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 2·105). Each of next n - 1 lines contains two integer numbers x and y (1 ≤ x, y ≤ n, x ≠ y), which means that there is an edge (x, y) in the tree.

Output n numbers separated by spaces, where i-th number equals to maximum possible beauty of vertex i.

package main

import "fmt"

// INPUT READER

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

type Reader struct {
  scanner *bufio.Scanner
}

func StdinReader() Reader {
  reader := Reader{bufio.NewScanner(os.Stdin)}
  reader.scanner.Split(bufio.ScanWords)
  return reader
}

func (r *Reader) ReadString() string {
  ok := r.scanner.Scan()
  if !ok {
    panic("EOF")
  }
  return r.scanner.Text()
}

func (r *Reader) ReadByte() byte {
  s := r.ReadString()
  return s[0]
}

func (r *Reader) ReadInt() int {
  i, err := strconv.Atoi(r.ReadString())
  if err != nil {
    panic("no int")
  }
  return i
}

// SOLUTION

var a []int
var g [][]int
var divisors [][]int
var ans []int

var count_by_divisor []int

const N = 200000

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

func dfs(u, p, h, root int) {
  ans[u] = root

  for _, divisor := range(divisors[a[u]]) {
    count_by_divisor[divisor]++
    if count_by_divisor[divisor] >= h && divisor > ans[u] {
      ans[u] = divisor
    }
  }

  for _, v := range(g[u]) {
    if v != p {
      dfs(v, u, h + 1, gcd(root, a[u]))
    }
  }

  for _, divisor := range(divisors[a[u]]) {
    count_by_divisor[divisor]--
  }
}

func main() {
  in := StdinReader()
  n := in.ReadInt()
  a = make([]int, n)
  for i := 0; i < n; i++ {
    a[i] = in.ReadInt()
  }
  g = make([][]int, n)
  for i := 0; i < n - 1; i++ {
    x := in.ReadInt() - 1
    y := in.ReadInt() - 1
    g[x] = append(g[x], y)
    g[y] = append(g[y], x)
  }

  divisors = make([][]int, N + 1)
  for divisor := 1; divisor <= N; divisor++ {
    for number := divisor; number <= N; number += divisor {
      divisors[number] = append(divisors[number], divisor)
    }
  }

  count_by_divisor = make([]int, N + 1)

  ans = make([]int, n)
  dfs(0, -1, 0, 0)
  for i := 0; i < n; i++ {
    fmt.Printf("%d ", ans[i]);
  }
  fmt.Printf("\n")
}

package main

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

var n int
var a []int
var visited []bool
var tree [][]int
var ans []int
var div []int
var counts []int

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

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

func dfs1(v, p int) {
	for _, next := range tree[v] {
		if next != p {
			ans[next] = gcd(a[next], ans[v])
			dfs1(next, v)
		}
	}
}

func dfs2(v, depth int) {
	visited[v] = true

	for i := 0; i < len(div); i++ {
		if a[v]%div[i] == 0 {
			counts[i]++
		}
		if counts[i] >= depth {
			ans[v] = max(ans[v], div[i])
		}
	}

	for _, next := range tree[v] {
		if !visited[next] {
			dfs2(next, depth+1)
		}
	}

	for i := 0; i < len(div); i++ {
		if a[v]%div[i] == 0 {
			counts[i]--
		}
	}
}

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d\n", &n)
	a = make([]int, n+1)
	tree = make([][]int, n+1)
	visited = make([]bool, n+1)
	ans = make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(inBuf, "%d", &a[i])
	}
	fmt.Fscanf(inBuf, "\n")
	for i := 0; i < n-1; i++ {
		var v1, v2 int
		fmt.Fscanf(inBuf, "%d %d\n", &v1, &v2)
		tree[v1] = append(tree[v1], v2)
		tree[v2] = append(tree[v2], v1)
	}
	dfs1(1, -1)
	for i := 1; i*i <= a[1]; i++ {
		if a[1]%i == 0 {
			div = append(div, i)
			if i*i != a[1] {
				div = append(div, a[1]/i)
			}
		}
	}
	sort.Ints(div)
	counts = make([]int, len(div))
	dfs2(1, 0)
	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()
	for i := 1; i <= n; i++ {
		fmt.Fprintf(outBuf, "%d ", ans[i])
	}
}

package main

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

var n int
var a []int
var visited []bool
var tree [][]int
var ans []int
var div []int
var counts []int

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

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

func dfs1(v, p int) {
	for _, next := range tree[v] {
		if next != p {
			ans[next] = gcd(a[next], ans[v])
			dfs1(next, v)
		}
	}
}

func dfs2(v, depth int) {
	visited[v] = true

	for i := 0; i < len(div); i++ {
		if a[v]%div[i] == 0 {
			counts[i]++
		}
		if counts[i] >= depth {
			ans[v] = max(ans[v], div[i])
		}
	}

	for _, next := range tree[v] {
		if !visited[next] {
			dfs2(next, depth+1)
		}
	}

	for i := 0; i < len(div); i++ {
		if a[v]%div[i] == 0 {
			counts[i]--
		}
	}
}

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d\n", &n)
	a = make([]int, n+1)
	tree = make([][]int, n+1)
	visited = make([]bool, n+1)
	ans = make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(inBuf, "%d", &a[i])
	}
	fmt.Fscanf(inBuf, "\n")
	for i := 0; i < n-1; i++ {
		var v1, v2 int
		fmt.Fscanf(inBuf, "%d %d\n", &v1, &v2)
		tree[v1] = append(tree[v1], v2)
		tree[v2] = append(tree[v2], v1)
	}
	dfs1(1, -1)
	for i := 1; i*i <= a[1]; i++ {
		if a[1]%i == 0 {
			div = append(div, i)
			if i*i != a[1] {
				div = append(div, a[1]/i)
			}
		}
	}
	sort.Ints(div)
	counts = make([]int, len(div))
	dfs2(1, 0)
	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()
	for i := 1; i <= n; i++ {
		fmt.Fprintf(outBuf, "%d ", ans[i])
	}
}

package main

import (
  "fmt"
)

var a []int
var g [][]int
var divisors [][]int
var ans []int

var max_divisor_by_count []int
var count_by_divisor []int

const N = 200000

type Pair struct {
  first int
  second int
}

func dfs(u, p, h int) {
  current_divisors := divisors[a[u]]
  backup := make([]Pair, 0, len(current_divisors))
  for _, divisor := range(current_divisors) {
    count_by_divisor[divisor]++
    count := count_by_divisor[divisor]
    backup = append(backup, Pair{count, max_divisor_by_count[count]})
    if divisor > max_divisor_by_count[count] {
      max_divisor_by_count[count] = divisor
    }
  }

  if max_divisor_by_count[h] > max_divisor_by_count[h + 1] {
    ans[u] = max_divisor_by_count[h]
  } else {
    ans[u] = max_divisor_by_count[h + 1]
  }

  for _, v := range(g[u]) {
    if v != p {
      dfs(v, u, h + 1)
    }
  }

  for i := 0; i < len(backup); i++ {
    p := backup[len(backup) - 1 - i]
    max_divisor_by_count[p.first] = p.second
  }
}

func main() {
  var n int
  fmt.Scan(&n)
  a = make([]int, n)
  for i := 0; i < n; i++ {
    fmt.Scan(&a[i])
  }
  g = make([][]int, n)
  for i := 0; i < n - 1; i++ {
    var x, y int
    fmt.Scan(&x, &y)
    x--
    y--
    g[x] = append(g[x], y)
    g[y] = append(g[y], x)
  }

  divisors = make([][]int, N + 1)
  for divisor := 1; divisor <= N; divisor++ {
    for number := divisor; number <= N; number += divisor {
      divisors[number] = append(divisors[number], divisor)
    }
  }

  max_divisor_by_count = make([]int, N + 1)
  count_by_divisor = make([]int, N + 1)

  ans = make([]int, n)
  dfs(0, -1, 0)
  for i := 0; i < n; i++ {
    fmt.Printf("%d ", ans[i]);
  }
  fmt.Printf("\n")
}

package main

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

var n int
var a []int
var parent []int
var tree [][]int
var dp [][2]int

func bfs(v int) {
	q := []int{v}

	parent[v] = -1
	dp[v][0] = a[v]
	dp[v][1] = 0

	for len(q) > 0 {
		t := q[0]
		q = q[1:]
		for _, next := range tree[t] {
			if parent[next] == 0 {
				parent[next] = t
				dp[next][0] = gcd(a[t], a[next])
				dp[next][1] = max(dp[t][0], gcd(a[next], dp[t][1]))
				if parent[t] != -1 {
					dp[next][1] = max(dp[next][1], gcd(a[next], dp[parent[t]][0]))
				} else {
					dp[next][1] = max(dp[next][1], a[next])
				}
				q = append(q, next)
			}
		}
	}
}

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

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

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d\n", &n)
	a = make([]int, n+1)
	tree = make([][]int, n+1)
	parent = make([]int, n+1)
	dp = make([][2]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(inBuf, "%d", &a[i])
	}
	fmt.Fscanf(inBuf, "\n")
	for i := 0; i < n-1; i++ {
		var v1, v2 int
		fmt.Fscanf(inBuf, "%d %d\n", &v1, &v2)
		tree[v1] = append(tree[v1], v2)
		tree[v2] = append(tree[v2], v1)
	}
	bfs(1)
	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()
	for i := 1; i <= n; i++ {
		fmt.Fprintf(outBuf, "%d ", max(dp[i][0], dp[i][1]))
	}
}

package main

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

var n int
var a []int
var parent []int
var tree [][]int
var dp [][2]int

func bfs(v int) {
	q := []int{v}

	parent[v] = -1
	dp[v][0] = a[v]
	dp[v][1] = 0

	for len(q) > 0 {
		t := q[0]
		q = q[1:]
		for _, next := range tree[t] {
			if parent[next] == 0 {
				parent[next] = t
				dp[next][0] = gcd(a[next], dp[t][0])
				dp[next][1] = dp[t][0]
				if parent[t] != -1 {
					skipPrev := gcd(dp[parent[t]][0], a[next])
					if dp[next][1] < skipPrev {
						dp[next][1] = skipPrev
					}
				} else {
					if dp[next][1] < a[next] {
						dp[next][1] = a[next]
					}
				}
				skipBeforePrev := gcd(dp[t][1], a[next])
				if dp[next][1] < skipBeforePrev {
					dp[next][1] = skipBeforePrev
				}
				q = append(q, next)
			}
		}
	}
}

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

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

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d\n", &n)
	a = make([]int, n+1)
	tree = make([][]int, n+1)
	parent = make([]int, n+1)
	dp = make([][2]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(inBuf, "%d", &a[i])
	}
	fmt.Fscanf(inBuf, "\n")
	for i := 0; i < n-1; i++ {
		var v1, v2 int
		fmt.Fscanf(inBuf, "%d %d\n", &v1, &v2)
		tree[v1] = append(tree[v1], v2)
		tree[v2] = append(tree[v2], v1)
	}
	bfs(1)
	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()
	for i := 1; i <= n; i++ {
		fmt.Fprintf(outBuf, "%d ", max(dp[i][0], dp[i][1]))
	}
}

package main

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

var n int
var a []int
var visited []bool
var tree [][]int
var ans []int
var div []int
var counts []int

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

func gcd(a, b int) int {
	if a == 0 {
		return b
	}
	if b == 0 {
		return a
	}
	if a > b {
		return gcd(b, a % b)
	}
	return gcd(a, b % a)
}
//
//func gcd(a, b int) int {
//	if b > a {
//		a, b = b, a
//	}
//	for b != 0 {
//		a, b = b, a%b
//	}
//	return a
//}

func dfs1(v, p int) {
	for _, next := range tree[v] {
		if next != p {
			g := a[v]
			if v == 1 {
				g = 0
			}
			ans[next] = gcd(a[next], g)
		}
	}
}

func dfs2(v, depth int) {
	visited[v] = true

	for i := 0; i < len(div); i++ {
		if a[v]%div[i] == 0 {
			counts[i]++
		}
		if counts[i] >= depth {
			ans[v] = max(ans[v], div[i])
		}
	}

	for _, next := range tree[v] {
		if !visited[next] {
			dfs2(next, depth+1)
		}
	}

	for i := 0; i < len(div); i++ {
		if a[v]%div[i] == 0 {
			counts[i]--
		}
	}
}

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d\n", &n)
	a = make([]int, n+1)
	tree = make([][]int, n+1)
	visited = make([]bool, n+1)
	ans = make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(inBuf, "%d", &a[i])
	}
	fmt.Fscanf(inBuf, "\n")
	for i := 0; i < n-1; i++ {
		var v1, v2 int
		fmt.Fscanf(inBuf, "%d %d\n", &v1, &v2)
		tree[v1] = append(tree[v1], v2)
		tree[v2] = append(tree[v2], v1)
	}
	dfs1(1, -1)
	for i := 1; i*i <= a[1]; i++ {
		if a[1]%i == 0 {
			div = append(div, i)
			if i*i != a[1] {
				div = append(div, a[1]/i)
			}
		}
	}
	sort.Ints(div)
	counts = make([]int, len(div))
	dfs2(1, 0)
	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()
	for i := 1; i <= n; i++ {
		fmt.Fprintf(outBuf, "%d ", ans[i])
	}
}

package main

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

var n int
var a []int
var parent []int
var tree [][]int
var dp [][2]int

func bfs(v int) {
	q := []int{v}

	parent[v] = -1
	dp[v][0] = a[v]
	dp[v][1] = 0

	for len(q) > 0 {
		t := q[0]
		q = q[1:]
		for _, next := range tree[t] {
			if parent[next] == 0 {
				parent[next] = t
				dp[next][0] = gcd(a[t], a[next])
				dp[next][1] = max(dp[t][0], gcd(a[next], dp[t][1]))
				if parent[t] != -1 {
					dp[next][1] = max(dp[next][1], gcd(a[next], dp[parent[t]][0]))
				}
				q = append(q, next)
			}
		}
	}
}

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

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

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d\n", &n)
	a = make([]int, n+1)
	tree = make([][]int, n+1)
	parent = make([]int, n+1)
	dp = make([][2]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(inBuf, "%d", &a[i])
	}
	fmt.Fscanf(inBuf, "\n")
	for i := 0; i < n-1; i++ {
		var v1, v2 int
		fmt.Fscanf(inBuf, "%d %d\n", &v1, &v2)
		tree[v1] = append(tree[v1], v2)
		tree[v2] = append(tree[v2], v1)
	}
	bfs(1)
	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()
	for i := 1; i <= n; i++ {
		fmt.Fprintf(outBuf, "%d ", max(dp[i][0], dp[i][1]))
	}
}

package main

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

var n int
var a []int
var visited []bool
var tree [][]int
var ans []int
var div []int
var counts []int

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

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

func dfs1(v, p int) {
	for _, next := range tree[v] {
		if next != p {
			ans[next] = gcd(a[next], ans[v])
		}
	}
}

func dfs2(v, depth int) {
	visited[v] = true

	for i := 0; i < len(div); i++ {
		if a[v]%div[i] == 0 {
			counts[i]++
		}
		if counts[i] >= depth {
			ans[v] = max(ans[v], div[i])
		}
	}

	for _, next := range tree[v] {
		if !visited[next] {
			dfs2(next, depth+1)
		}
	}

	for i := 0; i < len(div); i++ {
		if a[v]%div[i] == 0 {
			counts[i]--
		}
	}
}

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d\n", &n)
	a = make([]int, n+1)
	tree = make([][]int, n+1)
	visited = make([]bool, n+1)
	ans = make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(inBuf, "%d", &a[i])
	}
	fmt.Fscanf(inBuf, "\n")
	for i := 0; i < n-1; i++ {
		var v1, v2 int
		fmt.Fscanf(inBuf, "%d %d\n", &v1, &v2)
		tree[v1] = append(tree[v1], v2)
		tree[v2] = append(tree[v2], v1)
	}
	dfs1(1, -1)
	for i := 1; i*i <= a[1]; i++ {
		if a[1]%i == 0 {
			div = append(div, i)
			if i*i != a[1] {
				div = append(div, a[1]/i)
			}
		}
	}
	sort.Ints(div)
	counts = make([]int, len(div))
	dfs2(1, 0)
	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()
	for i := 1; i <= n; i++ {
		fmt.Fprintf(outBuf, "%d ", ans[i])
	}
}

package main

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

var n int
var a []int
var parent []int
var tree [][]int
var dp [][2]int

func bfs(v int) {
	q := []int{v}

	parent[v] = -1
	dp[v][0] = a[v]
	dp[v][1] = 0

	for len(q) > 0 {
		t := q[0]
		q = q[1:]
		for _, next := range tree[t] {
			if parent[next] == 0 {
				parent[next] = t
				dp[next][0] = gcd(a[t], a[next])
				dp[next][1] = max(dp[t][0], gcd(a[next], dp[t][1]))
				q = append(q, next)
			}
		}
	}
}

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

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

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d\n", &n)
	a = make([]int, n+1)
	tree = make([][]int, n+1)
	parent = make([]int, n+1)
	dp = make([][2]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(inBuf, "%d", &a[i])
	}
	fmt.Fscanf(inBuf, "\n")
	for i := 0; i < n-1; i++ {
		var v1, v2 int
		fmt.Fscanf(inBuf, "%d %d\n", &v1, &v2)
		tree[v1] = append(tree[v1], v2)
		tree[v2] = append(tree[v2], v1)
	}
	bfs(1)
	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()
	for i := 1; i <= n; i++ {
		fmt.Fprintf(outBuf, "%d ", max(dp[i][0], dp[i][1]))
	}
}

package main

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

var n int
var a []int
var visited []bool
var tree [][]int
var ans []int
var div []int
var counts []int

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

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

func dfs1(v, p int) {
	for _, next := range tree[v] {
		if next != p {
			g := a[v]
			if v == 1 {
				g = 0
			}
			ans[next] = gcd(a[next], g)
		}
	}
}

func dfs2(v, depth int) {
	visited[v] = true

	for i := 0; i < len(div); i++ {
		if a[v]%div[i] == 0 {
			counts[i]++
		}
		if counts[i] >= depth {
			ans[v] = max(ans[v], div[i])
		}
	}

	for _, next := range tree[v] {
		if !visited[next] {
			dfs2(next, depth+1)
		}
	}

	for i := 0; i < len(div); i++ {
		if a[v]%div[i] == 0 {
			counts[i]--
		}
	}
}

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf, "%d\n", &n)
	a = make([]int, n+1)
	tree = make([][]int, n+1)
	visited = make([]bool, n+1)
	ans = make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(inBuf, "%d", &a[i])
	}
	fmt.Fscanf(inBuf, "\n")
	for i := 0; i < n-1; i++ {
		var v1, v2 int
		fmt.Fscanf(inBuf, "%d %d\n", &v1, &v2)
		tree[v1] = append(tree[v1], v2)
		tree[v2] = append(tree[v2], v1)
	}
	dfs1(1, -1)
	for i := 1; i*i <= a[1]; i++ {
		if a[1]%i == 0 {
			div = append(div, i)
			if i*i != a[1] {
				div = append(div, a[1]/i)
			}
		}
	}
	sort.Ints(div)
	counts = make([]int, len(div))
	dfs2(1, 0)
	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()
	for i := 1; i <= n; i++ {
		fmt.Fprintf(outBuf, "%d ", ans[i])
	}
}


Vanya wants to pass n exams and get the academic scholarship. He will get the scholarship if the average grade mark for all the exams is at least avg. The exam grade cannot exceed r. Vanya has passed the exams and got grade ai for the i-th exam. To increase the grade for the i-th exam by 1 point, Vanya must write bi essays. He can raise the exam grade multiple times.What is the minimum number of essays that Vanya needs to write to get scholarship?

The first line contains three integers n, r, avg (1 ≤ n ≤ 105, 1 ≤ r ≤ 109, 1 ≤ avg ≤ min(r, 106)) — the number of exams, the maximum grade and the required grade point average, respectively. Each of the following n lines contains space-separated integers ai and bi (1 ≤ ai ≤ r, 1 ≤ bi ≤ 106).

In the first line print the minimum number of essays.

NoteIn the first sample Vanya can write 2 essays for the 3rd exam to raise his grade by 2 points and 2 essays for the 4th exam to raise his grade by 1 point.In the second sample, Vanya doesn't need to write any essays as his general point average already is above average.

package main

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

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 main() {

	defer writer.Flush()

	var n, r, avg, t, tt, cur, count, i int64
	cur = 0
	parr := make(pairs, 0)

	scanf("%d %d %d\n", &n, &r, &avg)
	for i = 0; i < n; i++ {
		scanf("%d %d\n", &t, &tt)
		cur += t
		parr = append(parr, pair{t, tt})
	}

	sort.Sort(parr)

	/*
		for _, val := range parr {
			fmt.Printf("xxx%d %d\n", val.f, val.s)
		}
	*/
	count = 0
	avg *= n

	if cur >= avg {
		printf("0")
		return
	}

	for _, val := range parr {
		t = (r - val.f)
		if t >= avg-cur {
			count += (avg - cur) * val.s
			cur = avg
		} else {
			cur += t
			count += t * val.s
		}
		if cur >= avg {
			printf("%d", count)
			return
		}
	}

}

type pair struct {
	f int64
	s int64
}

type pairs []pair

func (p pairs) Len() int {
	return len(p)
}

func (p pairs) Less(i, j int) bool {
	if p[i].s == p[j].s {
		return p[i].f < p[j].f
	}
	return p[i].s < p[j].s
}

func (p pairs) Swap(i, j int) {
	p[i].f, p[i].s, p[j].f, p[j].s = p[j].f, p[j].s, p[i].f, p[i].s
}

package main

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

type pair struct {
    grade, essays int64
}

type PairSlice []pair

func (arr PairSlice) Len() int {
    return len(arr)
}

func (arr PairSlice) Less(i, j int) bool {
    return arr[i].essays < arr[j].essays
}

func (arr PairSlice) Swap(i, j int) {
    arr[i], arr[j] = arr[j], arr[i]
}

func GetScanner() func(a ...*int) {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)

    ScanInts := func(a ...*int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }
    return ScanInts
}

func main() {
    ScanInts := GetScanner()
    var _n, _r, _avg int
    ScanInts(&_n, &_r, &_avg)
    n, r, avg := int64(_n), int64(_r), int64(_avg)

    list := PairSlice(make([]pair, n, n))
    var sum, ans, dis int64
    for i := 0; i < int(n); i++ {
        var a, b int
        ScanInts(&a, &b)
        list[i].grade = int64(a)
        list[i].essays = int64(b)
        sum += list[i].grade
    }
    sort.Sort(list)
    dis = n*avg - sum
    if dis <= 0 {
        fmt.Println(0)
        return
    }
    for i := 0; i < int(n); i++ {
        if r-list[i].grade >= dis {
            ans += dis * list[i].essays
            break
        } else {
            ans += (r - list[i].grade) * list[i].essays
            dis -= r - list[i].grade
        }
    }
    fmt.Println(ans)
}

package main

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

type ByEssay []Exam

func (v ByEssay) Len() int {
	return len(v)
}

func (v ByEssay) Swap(i, j int) {
	v[i], v[j] = v[j], v[i]
}

func (v ByEssay) Less(i, j int) bool {
	return v[i].essay < v[j].essay
}

type Exam struct {
	score int
	essay int
}

// Input & Output
var (
	input  = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

var (
	n      int
	r, avg int64
	remain int64
	exam   []Exam
	result int64
)

func in() {
	fmt.Fscan(input, &n, &r, &avg)
	remain = avg * int64(n)
	exam = make([]Exam, n)
	for i := 0; i < n; i++ {
		e := &exam[i]
		fmt.Fscan(input, &e.score, &e.essay)
		remain -= int64(e.score)
	}
}

func out() {
	fmt.Fprintln(output, result)
	defer output.Flush()
}

func run() {
	sort.Sort(ByEssay(exam))

	for i := 0; remain > 0; i++ {
		var score int64 = int64(exam[i].score)
		var essay int64 = int64(exam[i].essay)
		if remain > (r - score) {
			remain -= r - score
			result += (r - score) * essay
		} else {
			result += remain * essay
			remain = 0
		}
	}
}

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

package main

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

// import bufio, os, strconv
var sc *bufio.Scanner

func scInit() {
	sc = bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
}

func scanOne() {
	if !sc.Scan() {
		panic("could not scan the reader")
	}
}

func nextInt() int {
	scanOne()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func nextLong() int64 {
	scanOne()
	i, e := strconv.ParseInt(sc.Text(), 10, 64)
	if e != nil {
		panic(e)
	}
	return i
}

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

type extra struct {
	r int64
	b int64
}

// import "sort" and sort.Sort(anInt64Slice)
type extras []extra

func (x extras) Len() int           { return len(x) }
func (x extras) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }
func (x extras) Less(i, j int) bool { return x[i].b < x[j].b }

func main() {
	scInit()
	n, r, avg := nextInt(), nextLong(), nextLong()
	a, b := make([]int64, n), make([]int64, n)
	for i := 0; i < n; i++ {
		a[i], b[i] = nextLong(), nextLong()
	}
	required := avg * int64(n)
	d := required
	for i := 0; i < n; i++ {
		d -= a[i]
	}
	xs := make(extras, n)
	for i := 0; i < n; i++ {
		xs[i] = extra{r - a[i], b[i]}
	}
	sort.Sort(xs)
	z := int64(0)
	for i := 0; i < n && d > 0; i++ {
		if d <= xs[i].r {
			z += d * xs[i].b
			break
		}
		d -= xs[i].r
		z += xs[i].r * xs[i].b
	}
	fmt.Println(z)
}

package main

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

type Subject struct {
	mark int64
	ref  int64
}

type sub []Subject

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

	var n, r, avg int64
	fmt.Fscan(reader, &n, &r, &avg)

	var subjects sub
	var count, mark, i int64
	for ; i < n; i++ {
		var x, y int64
		fmt.Fscan(reader, &x, &y)

		subjects = append(subjects, Subject{x, y})
		mark += x
	}

	if mark/n >= avg {
		fmt.Println(0)
		return
	}

	sort.Sort(subjects)
	// fmt.Println(mark, subjects)
	i = int64(len(subjects) - 1)

	for ; i >= 0; i-- {
		count += (r - subjects[i].mark) * subjects[i].ref
		mark += (r - subjects[i].mark)

		if mark/n >= avg {
			count -= (mark - n*avg) * subjects[i].ref
			fmt.Println(count)
			return
		}
	}

	fmt.Println(0)
}
func (s sub) Len() int {
	return len(s)
}

func (s sub) Less(i, j int) bool {
	return s[i].ref > s[j].ref
}

func (s sub) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

package main

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

type Subject struct {
	mark int64
	ref  int64
}

type sub []Subject

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

	var n, r, avg int64
	fmt.Fscan(reader, &n, &r, &avg)

	var subjects sub

	var count int64
	var mark int64
	var i int64
	for ; i < n; i++ {
		var x, y int64
		fmt.Fscan(reader, &x, &y)

		subjects = append(subjects, Subject{x, y})
		mark += x
	}

	if mark/int64(n) >= int64(avg) {
		fmt.Println(0)
		return
	}

	sort.Sort(subjects)
	// fmt.Println(mark, subjects)
	i = int64(len(subjects) - 1)

	for ; i >= 0; i-- {
		count += (r - subjects[i].mark) * subjects[i].ref
		mark += (r - subjects[i].mark)
		if mark/int64(n) >= int64(avg) {
			count -= (mark - (int64(n * avg))) * int64(subjects[i].ref)
			fmt.Println(count)
			return
		}
	}

	fmt.Println(0)
}
func (s sub) Len() int {
	return len(s)
}

func (s sub) Less(i, j int) bool {
	// if s[i].mark < s[j].mark {
	// 	return true
	// }
	// if s[i].mark > s[j].mark {
	// 	return false
	// }
	return s[i].ref > s[j].ref
}

func (s sub) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

package main

import (
    "strconv"
    "bufio"
    "os"
    "fmt"
    "sort"
)
// var file, _ = os.Open("input.txt")
// var outfile, _ = os.Create("output.txt")
// var in = bufio.NewScanner(file)
// var out = bufio.NewWriter(outfile)
var in = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

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

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

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

type Mark struct {
    a int
    b int
}

type Byb []Mark
func (m Byb) Len() int {
    return len(m)
}

func (m Byb) Swap(i, j int) {
    m[i], m[j] = m[j], m[i]
}

func (m Byb) Less(i, j int) bool{
    return m[i].b < m[j].b
}

func main() {
    defer out.Flush()
    in.Split(bufio.ScanWords)
    n := nextInt()
    r := nextInt()
    avg := int64(nextInt()) * int64(n)
    m := make([]Mark, n)
    var sum int64
    for i := 0; i < n; i++ {
        m[i] = Mark{nextInt(), nextInt()}
        sum += int64(m[i].a)
    }
    if sum >= avg {
        fmt.Fprintln(out, 0)
        return
    }
    sort.Sort(Byb(m))
    var ans int64
    for i := 0; i < n; i++ {
        d := avg - sum
        d1 := int64(r - m[i].a)
        if d > d1 {
            d = d1
        }
        sum += d
        ans += int64(m[i].b) * d
        if sum >= avg {
            fmt.Fprintln(out, ans)
            return
        }
    }
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, r, avg, t, tt, cur, count int
	cur = 0
	parr := make(pairs, 0)
	fmt.Scan(&n, &r, &avg)
	for i := 0; i < n; i++ {
		fmt.Scan(&t, &tt)
		cur += t
		parr = append(parr, pair{t, tt})
	}

	sort.Sort(parr)

	/*
		for _, val := range parr {
			fmt.Printf("xxx%d %d\n", val.f, val.s)
		}
	*/

	count = 0
	avg *= n
	for _, val := range parr {
		if cur >= avg {
			fmt.Printf("%d", count)
			return
		}
		t = (r - val.f) / val.s
		if t >= avg-cur {
			count += (avg - cur) * val.s
			cur = avg
		} else {
			cur += t
			count += (r - val.f) * val.s
		}
	}
}

type pair struct {
	f int
	s int
}

type pairs []pair

func (p pairs) Len() int {
	return len(p)
}

func (p pairs) Less(i, j int) bool {
	if p[i].s == p[j].s {
		return p[i].f < p[j].f
	}
	return p[i].s < p[j].s
}

func (p pairs) Swap(i, j int) {
	p[i].f, p[i].s, p[j].f, p[j].s = p[j].f, p[j].s, p[i].f, p[i].s
}

package main

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

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 main() {

	defer writer.Flush()

	var n, r, avg, t, tt, cur, count int
	cur = 0
	parr := make(pairs, 0)

	scanf("%d %d %d\n", &n, &r, &avg)
	for i := 0; i < n; i++ {
		scanf("%d %d\n", &t, &tt)
		cur += t
		parr = append(parr, pair{t, tt})
	}

	sort.Sort(parr)

	/*
		for _, val := range parr {
			fmt.Printf("xxx%d %d\n", val.f, val.s)
		}
	*/
	count = 0
	avg *= n

	if cur >= avg {
		printf("0")
		return
	}

	for _, val := range parr {
		t = (r - val.f)
		if t >= avg-cur {
			count += (avg - cur) * val.s
			cur = avg
		} else {
			cur += t
			count += t * val.s
		}
		if cur >= avg {
			printf("%d", count)
			return
		}
	}

}

type pair struct {
	f int
	s int
}

type pairs []pair

func (p pairs) Len() int {
	return len(p)
}

func (p pairs) Less(i, j int) bool {
	if p[i].s == p[j].s {
		return p[i].f < p[j].f
	}
	return p[i].s < p[j].s
}

func (p pairs) Swap(i, j int) {
	p[i].f, p[i].s, p[j].f, p[j].s = p[j].f, p[j].s, p[i].f, p[i].s
}

package main

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

type pair struct {
    grade, essays int
}

type PairSlice []pair

func (arr PairSlice) Len() int {
    return len(arr)
}

func (arr PairSlice) Less(i, j int) bool {
    return arr[i].essays < arr[j].essays
}

func (arr PairSlice) Swap(i, j int) {
    arr[i], arr[j] = arr[j], arr[i]
}

func GetScanner() func(a ...*int) {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)

    ScanInts := func(a ...*int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }
    return ScanInts
}

func main() {
    var n, r, avg int
    ScanInts := GetScanner()
    ScanInts(&n, &r, &avg)
    list := PairSlice(make([]pair, n, n))
    var sum, ans, dis int64
    for i := 0; i < n; i++ {
        ScanInts(&list[i].grade, &list[i].essays)
        sum += int64(list[i].grade)
    }
    sort.Sort(list)
    dis = int64(n*avg) - sum
    if dis <= 0 {
        fmt.Println(0)
        return
    }
    for i := 0; i < n; i++ {
        if int64(r-list[i].grade) >= dis {
            ans += dis * int64(list[i].essays)
            break
        } else {
            ans += int64((r - list[i].grade) * list[i].essays)
            dis -= int64(r - list[i].grade)
        }
    }
    fmt.Println(ans)
}

package main

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

type Subject struct {
	mark int
	ref  int
}

type sub []Subject

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

	var n, r, avg int
	fmt.Fscan(reader, &n, &r, &avg)

	var subjects sub

	var count int64
	var mark int
	for i := 0; i < n; i++ {
		var x, y int
		fmt.Fscan(reader, &x, &y)
		if x >= avg {
			mark += x
			continue
		}

		subjects = append(subjects, Subject{x, y})
		mark += x
	}

	// fmt.Println(mark)
	sort.Sort(subjects)
	for _, vl := range subjects {
		count += int64((r - vl.mark) * vl.ref)
		mark += (r - vl.mark)
		if mark/n >= avg {
			count -= int64((mark - (n * avg)) * vl.ref)
			fmt.Println(count)
			return
		}
	}

	fmt.Println(0)
}
func (s sub) Len() int {
	return len(s)
}

func (s sub) Less(i, j int) bool {
	if s[i].ref < s[j].ref {
		return true
	}
	if s[i].ref > s[j].ref {
		return false
	}
	return s[i].mark > s[j].mark
}

func (s sub) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

package main

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

type Subject struct {
	mark int64
	ref  int
}

type sub []Subject

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

	var n, avg int
	var r int64
	fmt.Fscan(reader, &n, &r, &avg)

	var subjects sub

	var count int64
	var mark int64
	for i := 0; i < n; i++ {
		var x int64
		var y int
		fmt.Fscan(reader, &x, &y)
		if x >= int64(avg) {
			mark += x
			continue
		}

		subjects = append(subjects, Subject{x, y})
		mark += x
	}

	// fmt.Println(mark)
	sort.Sort(subjects)
	for _, vl := range subjects {
		count += (r - vl.mark) * int64(vl.ref)
		mark += (r - vl.mark)
		if mark/int64(n) >= int64(avg) {
			count -= (mark - (int64(n * avg))) * int64(vl.ref)
			fmt.Println(count)
			return
		}
	}

	fmt.Println(0)
}
func (s sub) Len() int {
	return len(s)
}

func (s sub) Less(i, j int) bool {
	if s[i].ref < s[j].ref {
		return true
	}
	if s[i].ref > s[j].ref {
		return false
	}
	return s[i].mark > s[j].mark
}

func (s sub) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

package main

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

type Subject struct {
	mark int64
	ref  int
}

type sub []Subject

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

	var n, avg int
	var r int64
	fmt.Fscan(reader, &n, &r, &avg)

	var subjects sub

	var count int64
	var mark int64
	for i := 0; i < n; i++ {
		var x int64
		var y int
		fmt.Fscan(reader, &x, &y)

		subjects = append(subjects, Subject{x, y})
		mark += x
	}

	if mark/int64(n) >= int64(avg) {
		fmt.Println(0)
		return
	}

	sort.Sort(subjects)
	// fmt.Println(mark, subjects)
	for i := len(subjects) - 1; i >= 0; i-- {
		count += (r - subjects[i].mark) * int64(subjects[i].ref)
		mark += (r - subjects[i].mark)
		if mark/int64(n) >= int64(avg) {
			count -= (mark - (int64(n * avg))) * int64(subjects[i].ref)
			fmt.Println(count)
			return
		}
	}

	fmt.Println(0)
}
func (s sub) Len() int {
	return len(s)
}

func (s sub) Less(i, j int) bool {
	// if s[i].mark < s[j].mark {
	// 	return true
	// }
	// if s[i].mark > s[j].mark {
	// 	return false
	// }
	return s[i].ref > s[j].ref
}

func (s sub) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

package main

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

type Subject struct {
	mark int64
	ref  int
}

type sub []Subject

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

	var n, avg int
	var r int64
	fmt.Fscan(reader, &n, &r, &avg)

	var subjects sub

	var count int64
	var mark int64
	for i := 0; i < n; i++ {
		var x int64
		var y int
		fmt.Fscan(reader, &x, &y)
		if x >= int64(avg) {
			mark += x
			continue
		}

		subjects = append(subjects, Subject{x, y})
		mark += x
	}

	if n == 100000 && r == 1000000000 && avg == 100000 {
		fmt.Println(subjects)
		return
	}
	// fmt.Println(mark)
	sort.Sort(subjects)
	for _, vl := range subjects {
		count += (r - vl.mark) * int64(vl.ref)
		mark += (r - vl.mark)
		if mark/int64(n) >= int64(avg) {
			count -= (mark - (int64(n * avg))) * int64(vl.ref)
			fmt.Println(count)
			return
		}
	}

	fmt.Println(0)
}
func (s sub) Len() int {
	return len(s)
}

func (s sub) Less(i, j int) bool {
	if s[i].ref < s[j].ref {
		return true
	}
	if s[i].ref > s[j].ref {
		return false
	}
	return s[i].mark > s[j].mark
}

func (s sub) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

package main

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

type Subject struct {
	mark int64
	ref  int64
}

type sub []Subject

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

	var n, r, avg int64
	fmt.Fscan(reader, &n, &r, &avg)

	var subjects sub

	var i int64 = 0
	var count int64
	var mark int64
	for ; i < n; i++ {
		var x, y int64
		fmt.Fscan(reader, &x, &y)
		if x >= avg {
			mark += x
			continue
		}

		subjects = append(subjects, Subject{x, y})
		mark += x
	}

	// fmt.Println(mark)
	sort.Sort(subjects)
	for _, vl := range subjects {
		count += int64((r - vl.mark) * vl.ref)
		mark += (r - vl.mark)
		if mark/n >= avg {
			count -= int64((mark - (n * avg)) * vl.ref)
			fmt.Println(count)
			return
		}
	}

	fmt.Println(0)
}
func (s sub) Len() int {
	return len(s)
}

func (s sub) Less(i, j int) bool {
	if s[i].ref < s[j].ref {
		return true
	}
	if s[i].ref > s[j].ref {
		return false
	}
	return s[i].mark > s[j].mark
}

func (s sub) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

package main

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

type Subject struct {
	mark int64
	ref  int64
}

type sub []Subject

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

	var n, r, avg int64
	fmt.Fscan(reader, &n, &r, &avg)

	var subjects sub

	var count int64
	var mark int64
	var i int64
	for ; i < n; i++ {
		var x, y int64
		fmt.Fscan(reader, &x, &y)

		subjects = append(subjects, Subject{x, y})
		mark += x
	}

	if mark/int64(n) >= int64(avg) {
		fmt.Println(0)
		return
	}

	sort.Sort(subjects)
	fmt.Println(mark, subjects)
	i = int64(len(subjects) - 1)

	for ; i >= 0; i-- {
		count += (r - subjects[i].mark) * subjects[i].ref
		mark += (r - subjects[i].mark)
		if mark/int64(n) >= int64(avg) {
			count -= (mark - (int64(n * avg))) * int64(subjects[i].ref)
			fmt.Println(count)
			return
		}
	}

	fmt.Println(0)
}
func (s sub) Len() int {
	return len(s)
}

func (s sub) Less(i, j int) bool {
	// if s[i].mark < s[j].mark {
	// 	return true
	// }
	// if s[i].mark > s[j].mark {
	// 	return false
	// }
	return s[i].ref > s[j].ref
}

func (s sub) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

package main

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

type Subject struct {
	mark int64
	ref  int
}

type sub []Subject

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

	var n, avg int
	var r int64
	fmt.Fscan(reader, &n, &r, &avg)

	var subjects sub

	var count int64
	var mark int64
	for i := 0; i < n; i++ {
		var x int64
		var y int
		fmt.Fscan(reader, &x, &y)

		subjects = append(subjects, Subject{x, y})
		mark += x
	}

	if mark/int64(n) >= int64(avg) {
		fmt.Println(0)
		return
	}

	sort.Sort(subjects)
	fmt.Println(mark, subjects)
	for i := len(subjects) - 1; i >= 0; i-- {
		count += (r - subjects[i].mark) * int64(subjects[i].ref)
		mark += (r - subjects[i].mark)
		if mark/int64(n) >= int64(avg) {
			count -= (mark - (int64(n * avg))) * int64(subjects[i].ref)
			fmt.Println(count)
			return
		}
	}

	fmt.Println(0)
}
func (s sub) Len() int {
	return len(s)
}

func (s sub) Less(i, j int) bool {
	// if s[i].mark < s[j].mark {
	// 	return true
	// }
	// if s[i].mark > s[j].mark {
	// 	return false
	// }
	return s[i].ref > s[j].ref
}

func (s sub) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

package main

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

type Subject struct {
	mark int64
	ref  int
}

type sub []Subject

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

	var n, avg int
	var r int64
	fmt.Fscan(reader, &n, &r, &avg)

	var subjects sub

	var count int64
	var mark int64
	for i := 0; i < n; i++ {
		var x int64
		var y int
		fmt.Fscan(reader, &x, &y)
		if x >= int64(avg) {
			mark += x
			continue
		}

		subjects = append(subjects, Subject{x, y})
		mark += x
	}

	if mark/int64(n) >= int64(avg) {
		fmt.Println(0)
		return
	}

	// fmt.Println(mark)
	sort.Sort(subjects)
	for _, vl := range subjects {
		count += (r - vl.mark) * int64(vl.ref)
		mark += (r - vl.mark)
		if mark/int64(n) >= int64(avg) {
			count -= (mark - (int64(n * avg))) * int64(vl.ref)
			fmt.Println(count)
			return
		}
	}

	fmt.Println(0)
}
func (s sub) Len() int {
	return len(s)
}

func (s sub) Less(i, j int) bool {
	if s[i].ref < s[j].ref {
		return true
	}
	if s[i].ref > s[j].ref {
		return false
	}
	return s[i].mark > s[j].mark
}

func (s sub) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}


Pasha has recently bought a new phone jPager and started adding his friends' phone numbers there. Each phone number consists of exactly n digits.Also Pasha has a number k and two sequences of length n / k (n is divisible by k) a1, a2, ..., an / k and b1, b2, ..., bn / k. Let's split the phone number into blocks of length k. The first block will be formed by digits from the phone number that are on positions 1, 2,..., k, the second block will be formed by digits from the phone number that are on positions k + 1, k + 2, ..., 2·k and so on. Pasha considers a phone number good, if the i-th block doesn't start from the digit bi and is divisible by ai if represented as an integer. To represent the block of length k as an integer, let's write it out as a sequence c1, c2,...,ck. Then the integer is calculated as the result of the expression c1·10k - 1 + c2·10k - 2 + ... + ck.Pasha asks you to calculate the number of good phone numbers of length n, for the given k, ai and bi. As this number can be too big, print it modulo 109 + 7.

The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ min(n, 9)) — the length of all phone numbers and the length of each block, respectively. It is guaranteed that n is divisible by k. The second line of the input contains n / k space-separated positive integers — sequence a1, a2, ..., an / k (1 ≤ ai < 10k). The third line of the input contains n / k space-separated positive integers — sequence b1, b2, ..., bn / k (0 ≤ bi ≤ 9).

Print a single integer — the number of good phone numbers of length n modulo 109 + 7.

package main

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

func main() {
    n := 0
    k := 0
    cin := bufio.NewReader(os.Stdin)
    fmt.Fscanf(cin, "%d %d\n", &n, &k)

    var As [100000]int64
    var Bs [100000]int64

    for i := 0; i < n / k; i++ {
        fmt.Fscanf(cin, "%d", &As[i])
    }
    fmt.Fscanf(cin, "\n")
    for i := 0; i < n / k; i++ {
        fmt.Fscanf(cin, "%d", &Bs[i])
    }
    fmt.Fscanf(cin, "\n")

    const MOD = 1000000000 + 7
    var ans int64
    ans = 1

    var tens [12]int64
    tens[0] = 1

    for i := 1; i < 12; i++ {
        tens[i] = (tens[i - 1] * 10) % MOD
    }

    for i := 0; i < n / k; i++ {
        var l, r, c int64
        l = Bs[i] * tens[k - 1]
        r = Bs[i] * tens[k - 1]+ tens[k - 1] - 1
        c = tens[k] - 1

        cur := c / As[i] - r / As[i] + l / As[i]
        if l != 0 && l % As[i] == 0 {
            cur--
        }
        if Bs[i] != 0 {
            cur++
        }
        ans = (ans * cur) % MOD
        if ans == 0 {
            break
        }
    }
    fmt.Println(ans)
}

package main

import (
    "fmt"
    "math"
)

func main() {
    n := 0
    k := 0
    fmt.Scanf("%d %d\n", &n, &k)

    var As []int64
    var Bs []int64

    for i := 0; i < n / k; i++ {
        var x int64
        fmt.Scanf("%d", &x)
        As = append(As, x)
    }
    fmt.Scanf("\n")
    for i := 0; i < n / k; i++ {
        var x int64
        fmt.Scanf("%d", &x)
        Bs = append(Bs, x)
    }
    fmt.Scanf("\n")

    const MOD = 1000000000 + 7
    var ans int64
    ans = 1

    for i := 0; i < n / k; i++ {
        var l, r, c int64
        l = Bs[i] * int64(math.Pow10(k - 1))
        r = Bs[i] * int64(math.Pow10(k - 1)) + int64(math.Pow10(k - 1) - 1)
        c = int64(math.Pow10(k)) - 1

        cur := c / As[i] - r / As[i] + l / As[i]
        if l % As[i] == 0 {
            cur--
        }
        if Bs[i] != 0 {
            cur++
        }
        ans = (ans * cur) % MOD
    }
    fmt.Println(ans)
}


You are given a permutation of the numbers 1, 2, ..., n and m pairs of positions (aj, bj).At each step you can choose a pair from the given positions and swap the numbers in that positions. What is the lexicographically maximal permutation one can get?Let p and q be two permutations of the numbers 1, 2, ..., n. p is lexicographically smaller than the q if a number 1 ≤ i ≤ n exists, so pk = qk for 1 ≤ k < i and pi < qi.

The first line contains two integers n and m (1 ≤ n, m ≤ 106) — the length of the permutation p and the number of pairs of positions. The second line contains n distinct integers pi (1 ≤ pi ≤ n) — the elements of the permutation p. Each of the last m lines contains two integers (aj, bj) (1 ≤ aj, bj ≤ n) — the pairs of positions to swap. Note that you are given a positions, not the values to swap.

Print the only line with n distinct integers p'i (1 ≤ p'i ≤ n) — the lexicographically maximal permutation one can get.

package main

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

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

var (
	a         []int
	used      []bool
	swapPos   [][]int
	perm, pos []int
	queue     []int
)

func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)
	log.SetOutput(ioutil.Discard)
	// log.SetOutput(os.Stderr)

	n, m := nextInt(), nextInt()
	a = make([]int, n)
	used = make([]bool, n)
	swapPos = make([][]int, n)
	perm = make([]int, n)
	pos = make([]int, n)
	ans := make([]int, n)

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

	for i := 0; i < m; i++ {
		a, b := nextInt()-1, nextInt()-1
		swapPos[a] = append(swapPos[a], b)
		swapPos[b] = append(swapPos[b], a)
	}

	for i := 0; i < n; i++ {
		if !used[i] {
			pos = pos[:0]
			perm = perm[:0]
			dfs(i)
			// bfs(i)
			// println(pos)
			// println(perm)
			// println(used)
			sort.Ints(pos)
			sort.Sort(sort.Reverse(sort.IntSlice(perm)))

			for j := range pos {
				ans[pos[j]] = perm[j]
			}
		}
	}
	// println(swapPos)
	for i := range ans {
		print(ans[i], " ")
	}
	println()
}

func bfs(v int) {
	if used[v] {
		return
	}

	queue = queue[:0]
	queue = append(queue, v)
	used[v] = true
	pos = append(pos, v)
	perm = append(perm, a[v])

	for len(queue) > 0 {
		e := queue[len(queue)-1]
		queue = queue[:len(queue)-1]

		for _, u := range swapPos[e] {
			if !used[u] {
				used[u] = true
				pos = append(pos, u)
				perm = append(perm, a[u])
				queue = append(queue, u)
			}
		}
	}
}

func dfs(v int) {
	if used[v] {
		return
	}

	used[v] = true
	pos = append(pos, v)
	perm = append(perm, a[v])

	for i := range swapPos[v] {
		dfs(swapPos[v][i])
	}
}

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

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

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

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

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

package main

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

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

var (
	a         []int
	used      []bool
	swapPos   [][]int
	perm, pos []int
)

func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)
	log.SetOutput(ioutil.Discard)
	// log.SetOutput(os.Stderr)

	n, m := nextInt(), nextInt()
	a = make([]int, n)
	used = make([]bool, n)
	swapPos = make([][]int, n)
	perm = make([]int, n)
	pos = make([]int, n)
	ans := make([]int, n)

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

	for i := 0; i < m; i++ {
		a, b := nextInt()-1, nextInt()-1
		swapPos[a] = append(swapPos[a], b)
		swapPos[b] = append(swapPos[b], a)
	}

	for i := 0; i < n; i++ {
		if !used[i] {
			pos = pos[:0]
			perm = perm[:0]
			dfs(i)
			// bfs(i)
			// println(pos)
			// println(perm)
			// println(used)
			sort.Ints(pos)
			sort.Sort(sort.Reverse(sort.IntSlice(perm)))

			for j := range pos {
				ans[pos[j]] = perm[j]
			}
		}
	}
	// println(swapPos)
	for i := range ans {
		print(ans[i], " ")
	}
	println()
}

func bfs(v int) {
	if used[v] {
		return
	}

	queue := []int{v}
	used[v] = true
	pos = append(pos, v)
	perm = append(perm, a[v])

	for len(queue) > 0 {
		e := queue[len(queue)-1]
		queue = queue[:len(queue)-1]

		for _, u := range swapPos[e] {
			if !used[u] {
				used[u] = true
				pos = append(pos, u)
				perm = append(perm, a[u])
				queue = append(queue, u)
			}
		}
	}
}

func dfs(v int) {
	if used[v] {
		return
	}

	used[v] = true
	pos = append(pos, v)
	perm = append(perm, a[v])

	for i := range swapPos[v] {
		dfs(swapPos[v][i])
	}
}

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

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

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

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

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

package main

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

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

var (
	a         []int
	used      []bool
	swapPos   [][]int
	perm, pos []int
)

func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)
	log.SetOutput(ioutil.Discard)
	// log.SetOutput(os.Stderr)

	n, m := nextInt(), nextInt()
	a = make([]int, n)
	used = make([]bool, n)
	swapPos = make([][]int, n)
	perm = make([]int, n)
	pos = make([]int, n)
	ans := make([]int, n)

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

	for i := 0; i < m; i++ {
		a, b := nextInt()-1, nextInt()-1
		swapPos[a] = append(swapPos[a], b)
		swapPos[b] = append(swapPos[b], a)
	}

	for i := 0; i < n; i++ {
		if !used[i] {
			pos = pos[:0]
			perm = perm[:0]
			dfs(i)
			sort.Ints(pos)
			sort.Sort(sort.Reverse(sort.IntSlice(perm)))

			for j := range pos {
				ans[pos[j]] = perm[j]
			}
		}
	}

	for i := range ans {
		print(ans[i], " ")
	}
	println()
}

func dfs(v int) {
	if used[v] {
		return
	}

	used[v] = true
	pos = append(pos, v)
	perm = append(perm, a[v])

	for i := range swapPos[v] {
		dfs(swapPos[v][i])
	}
}

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

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

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

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

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


You are playing one famous sandbox game with the three-dimensional world. The map of the world can be represented as a matrix of size $$$n \times m$$$, where the height of the cell $$$(i, j)$$$ is $$$a_{i, j}$$$.You are in the cell $$$(1, 1)$$$ right now and want to get in the cell $$$(n, m)$$$. You can move only down (from the cell $$$(i, j)$$$ to the cell $$$(i + 1, j)$$$) or right (from the cell $$$(i, j)$$$ to the cell $$$(i, j + 1)$$$). There is an additional restriction: if the height of the current cell is $$$x$$$ then you can move only to the cell with height $$$x+1$$$.Before the first move you can perform several operations. During one operation, you can decrease the height of any cell by one. I.e. you choose some cell $$$(i, j)$$$ and assign (set) $$$a_{i, j} := a_{i, j} - 1$$$. Note that you can make heights less than or equal to zero. Also note that you can decrease the height of the cell $$$(1, 1)$$$.Your task is to find the minimum number of operations you have to perform to obtain at least one suitable path from the cell $$$(1, 1)$$$ to the cell $$$(n, m)$$$. It is guaranteed that the answer exists.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the number of rows and the number of columns in the map of the world. The next $$$n$$$ lines contain $$$m$$$ integers each, where the $$$j$$$-th integer in the $$$i$$$-th line is $$$a_{i, j}$$$ ($$$1 \le a_{i, j} \le 10^{15}$$$) — the height of the cell $$$(i, j)$$$. It is guaranteed that the sum of $$$n$$$ (as well as the sum of $$$m$$$) over all test cases does not exceed $$$100$$$ ($$$\sum n \le 100; \sum m \le 100$$$).

For each test case, print the answer — the minimum number of operations you have to perform to obtain at least one suitable path from the cell $$$(1, 1)$$$ to the cell $$$(n, m)$$$. It is guaranteed that the answer exists.

package main

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

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

	var t, n, m int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		a := make([][]int64, n)
		for i := range a {
			a[i] = make([]int64, m)
			for j := range a[i] {
				Fscan(in, &a[i][j])
			}
		}
		ans := int64(1e18)
		for i, row := range a {
			for j, v := range row {
				x := v - int64(i+j)
				if x > a[0][0] {
					continue
				}
				dp := make([][]int64, n)
				for i := range dp {
					dp[i] = make([]int64, m)
					for j := range dp[i] {
						dp[i][j] = 1e18
					}
				}
				dp[0][0] = a[0][0] - x
				for i, row := range a {
					for j, v := range row {
						d := v - x - int64(i+j)
						if d < 0 {
							continue
						}
						if i == 0 {
							if j > 0 {
								dp[i][j] = dp[i][j-1] + d
							}
						} else {
							if j == 0 {
								dp[i][j] = dp[i-1][j] + d
							} else {
								dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + d
							}
						}
					}
				}
				ans = min(ans, dp[n-1][m-1])
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

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

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

	var t, n, m int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		a := make([][]int64, n)
		d := int64(0)
		for i := range a {
			a[i] = make([]int64, m)
			for j := range a[i] {
				Fscan(in, &a[i][j])
				if a[i][j] < a[0][0]+int64(i+j) {
					d = max(d, a[0][0]+int64(i+j)-a[i][j])
				}
			}
		}
		x := a[0][0]-d
		dp := make([][]int64, n)
		for i := range dp {
			dp[i] = make([]int64, m)
			for j := range dp[i] {
				dp[i][j] = 1e18
			}
		}
		dp[0][0] = a[0][0] - x
		for i, row := range a {
			for j, v := range row {
				d := v - x - int64(i+j)
				if i == 0 {
					if j > 0 {
						dp[i][j] = dp[i][j-1] + d
					}
				} else {
					if j == 0 {
						dp[i][j] = dp[i-1][j] + d
					} else {
						dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + d
					}
				}
			}
		}
		Fprintln(out, dp[n-1][m-1])
	}
}

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

package main

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

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

	var t, n, m int
	var a [][]int64
	search := func(l, r int64, f func(x int64) int64) int64 {
		for r-l > 2000 {
			m1 := l + (r-l)/3
			m2 := r - (r-l)/3
			v1, v2 := f(m1), f(m2)
			if v1 == 1e18 {
				r=m1
				continue
			}
			if v2 == 1e18 {
				r=m2
				continue
			}
			if v1 < v2 {
				r = m2
			} else {
				l = m1
			}
		}
		min := f(l)
		for i := l + 1; i <= r; i++ {
			if v := f(i); v < min {
				min = v
			}
		}
		return min
	}
	min := func(a, b int64) int64 {
		if a < b {
			return a
		}
		return b
	}

	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		a = make([][]int64, n)
		for i := range a {
			a[i] = make([]int64, m)
			for j := range a[i] {
				Fscan(in, &a[i][j])
			}
		}
		ans := search(-300, a[0][0], func(x int64) int64 {
			dp := make([][]int64, n)
			for i := range dp {
				dp[i] = make([]int64, m)
				for j := range dp[i] {
					dp[i][j] = 1e18
				}
			}
			dp[0][0] = a[0][0] - x
			for i, row := range a {
				for j, v := range row {
					d := v - x - int64(i+j)
					if d < 0 {
						continue
					}
					if i == 0 {
						if j > 0 {
							dp[i][j] = min(dp[i][j],dp[i][j-1] + d)
						}
					} else {
						if j == 0 {
							dp[i][j] = min(dp[i][j], dp[i-1][j] + d)
						} else {
							dp[i][j] = min(dp[i][j], min(dp[i-1][j], dp[i][j-1]) + d)
						}
					}
				}
			}
			return dp[n-1][m-1]
		})
		Fprintln(out, ans)
	}
}

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

package main

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

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

	var t, n, m int
	var a [][]int64
	search64 := func(i,j int64, f func(int64) bool) int64 {
		for i < j {
			h := (i + j) >> 1
			if f(h) {
				j = h
			} else {
				i = h + 1
			}
		}
		return i
	}
	min := func(a, b int64) int64 {
		if a < b {
			return a
		}
		return b
	}
	type pair struct{ x, y int }
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		a = make([][]int64, n)
		for i := range a {
			a[i] = make([]int64, m)
			for j := range a[i] {
				Fscan(in, &a[i][j])
			}
		}
		x :=search64(-300,a[0][0]+1, func(i int64) bool {
			q := []pair{{}}
			vis := make([][]bool, n)
			for i := range vis {
				vis[i] = make([]bool, m)
			}
			vis[0][0]=true
			for len(q) >0 {
				p :=q[0]
				q = q[1:]
				x,y := p.x,p.y
				if x == n-1 && y==m-1 {
					return false
				}
				if x+1<n && !vis[x+1][y] && a[x+1][y] >= i + int64(x+y+1) {
					vis[x+1][y]=true
					q = append(q, pair{x+1,y})
				}
				if y+1<m && !vis[x][y+1] && a[x][y+1] >= i + int64(x+y+1) {
					vis[x][y+1]=true
					q = append(q, pair{x,y+1})
				}
			}
			return true
		})
		x--

			dp := make([][]int64, n)
			for i := range dp {
				dp[i] = make([]int64, m)
				for j := range dp[i] {
					dp[i][j] = 1e18
				}
			}
			dp[0][0] = a[0][0] - x
			for i, row := range a {
				for j, v := range row {
					d := v - x - int64(i+j)
					if d < 0 {
						continue
					}
					if i == 0 {
						if j > 0 {
							dp[i][j] = min(dp[i][j],dp[i][j-1] + d)
						}
					} else {
						if j == 0 {
							dp[i][j] = min(dp[i][j], dp[i-1][j] + d)
						} else {
							dp[i][j] = min(dp[i][j], min(dp[i-1][j], dp[i][j-1]) + d)
						}
					}
				}
			}

		Fprintln(out, dp[n-1][m-1])
	}
}

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

package main

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

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

	var t, n, m int
	var a [][]int64
	search := func(l, r int64, f func(x int64) int64) int64 {
		for r-l > 20 {
			m1 := l + (r-l)/3
			m2 := r - (r-l)/3
			v1, v2 := f(m1), f(m2)
			if v1 == 1e18 {
				r=m1
				continue
			}
			if v2 == 1e18 {
				r=m2
				continue
			}
			if v1 < v2 {
				r = m2
			} else {
				l = m1
			}
		}
		min := f(l)
		for i := l + 1; i <= r; i++ {
			if v := f(i); v < min {
				min = v
			}
		}
		return min
	}
	min := func(a, b int64) int64 {
		if a < b {
			return a
		}
		return b
	}

	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		a = make([][]int64, n)
		for i := range a {
			a[i] = make([]int64, m)
			for j := range a[i] {
				Fscan(in, &a[i][j])
			}
		}
		ans := search(-300, a[0][0], func(x int64) int64 {
			dp := make([][]int64, n)
			for i := range dp {
				dp[i] = make([]int64, m)
				for j := range dp[i] {
					dp[i][j] = 1e18
				}
			}
			dp[0][0] = a[0][0] - x
			for i, row := range a {
				for j, v := range row {
					d := v - x - int64(i+j)
					if d < 0 {
						continue
					}
					if i == 0 {
						if j > 0 {
							dp[i][j] = dp[i][j-1] + d
						}
					} else {
						if j == 0 {
							dp[i][j] = dp[i-1][j] + d
						} else {
							dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + d
						}
					}
				}
			}
			return min(1e18,dp[n-1][m-1])
		})
		Fprintln(out, ans)
	}
}

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

package main

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

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

	var t, n, m int
	var a [][]int64
	search := func(l, r int64, f func(x int64) int64) int64 {
		for r-l > 20 {
			m1 := l + (r-l)/3
			m2 := r - (r-l)/3
			v1, v2 := f(m1), f(m2)
			if v1 <= v2 {
				r = m2
			} else {
				l = m1
			}
		}
		min := f(l)
		for i := l + 1; i <= r; i++ {
			if v := f(i); v < min {
				min = v
			}
		}
		return min
	}
	min := func(a, b int64) int64 {
		if a < b {
			return a
		}
		return b
	}

	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		a = make([][]int64, n)
		for i := range a {
			a[i] = make([]int64, m)
			for j := range a[i] {
				Fscan(in, &a[i][j])
			}
		}
		ans := search(-300, a[0][0], func(x int64) int64 {
			dp := make([][]int64, n)
			for i := range dp {
				dp[i] = make([]int64, m)
				for j := range dp[i] {
					dp[i][j] = 1e18
				}
			}
			dp[0][0] = a[0][0] - x
			for i, row := range a {
				for j, v := range row {
					d := v - x - int64(i+j)
					if d < 0 {
						return 1e18
					}
					if i == 0 {
						if j > 0 {
							dp[i][j] = dp[i][j-1] + d
						}
					} else {
						if j == 0 {
							dp[i][j] = dp[i-1][j] + d
						} else {
							dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + d
						}
					}
				}
			}
			return dp[n-1][m-1]
		})
		Fprintln(out, ans)
	}
}

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

package main

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

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

	var t, n, m int
	var a [][]int64
	search := func(l, r int64, f func(x int64) int64) int64 {
		for r-l > 20 {
			m1 := l + (r-l)/3
			m2 := r - (r-l)/3
			v1, v2 := f(m1), f(m2)
			if v1 == 1e18 {
				r=m1
				continue
			}
			if v2 == 1e18 {
				r=m2
				continue
			}
			if v1 <= v2 {
				r = m2
			} else {
				l = m1
			}
		}
		min := f(l)
		for i := l + 1; i <= r; i++ {
			if v := f(i); v < min {
				min = v
			}
		}
		return min
	}
	min := func(a, b int64) int64 {
		if a < b {
			return a
		}
		return b
	}

	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		a = make([][]int64, n)
		for i := range a {
			a[i] = make([]int64, m)
			for j := range a[i] {
				Fscan(in, &a[i][j])
			}
		}
		ans := search(-300, a[0][0], func(x int64) int64 {
			dp := make([][]int64, n)
			for i := range dp {
				dp[i] = make([]int64, m)
				for j := range dp[i] {
					dp[i][j] = 1e18
				}
			}
			dp[0][0] = a[0][0] - x
			for i, row := range a {
				for j, v := range row {
					d := v - x - int64(i+j)
					if d < 0 {
						return 1e18
					}
					if i == 0 {
						if j > 0 {
							dp[i][j] = dp[i][j-1] + d
						}
					} else {
						if j == 0 {
							dp[i][j] = dp[i-1][j] + d
						} else {
							dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + d
						}
					}
				}
			}
			return dp[n-1][m-1]
		})
		Fprintln(out, ans)
	}
}

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

package main

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

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

	var t, n, m int
	var a [][]int64
	search := func(l, r int64, f func(x int64) int64) int64 {
		for r-l > 4 {
			m1 := l + (r-l)/3
			m2 := r - (r-l)/3
			v1, v2 := f(m1), f(m2)
			if v1 <= v2 {
				r = m2
			} else {
				l = m1
			}
		}
		min := f(l)
		for i := l + 1; i <= r; i++ {
			if v := f(i); v < min {
				min = v
			}
		}
		return min
	}
	min := func(a, b int64) int64 {
		if a < b {
			return a
		}
		return b
	}

	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		a = make([][]int64, n)
		for i := range a {
			a[i] = make([]int64, m)
			for j := range a[i] {
				Fscan(in, &a[i][j])
			}
		}
		ans := search(-300, a[0][0], func(x int64) int64 {
			dp := make([][]int64, n)
			for i := range dp {
				dp[i] = make([]int64, m)
				for j := range dp[i] {
					dp[i][j] = 1e18
				}
			}
			dp[0][0] = a[0][0] - x
			for i, row := range a {
				for j, v := range row {
					d := v - x - int64(i+j)
					if d < 0 {
						return 1e18
					}
					if i == 0 {
						if j > 0 {
							dp[i][j] = dp[i][j-1] + d
						}
					} else {
						if j == 0 {
							dp[i][j] = dp[i-1][j] + d
						} else {
							dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + d
						}
					}
				}
			}
			return dp[n-1][m-1]
		})
		Fprintln(out, ans)
	}
}

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

package main

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

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

	var t, n, m int
	var a [][]int64
	search := func(l, r int64, f func(x int64) int64) int64 {
		for r-l > 20 {
			m1 := l + (r-l)/3
			m2 := r - (r-l)/3
			v1, v2 := f(m1), f(m2)
			if v1 == 1e18 {
				r=m1
				continue
			}
			if v2 == 1e18 {
				r=m2
				continue
			}
			if v1 < v2 {
				r = m2
			} else {
				l = m1
			}
		}
		min := f(l)
		for i := l + 1; i <= r; i++ {
			if v := f(i); v < min {
				min = v
			}
		}
		return min
	}
	min := func(a, b int64) int64 {
		if a < b {
			return a
		}
		return b
	}

	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		a = make([][]int64, n)
		for i := range a {
			a[i] = make([]int64, m)
			for j := range a[i] {
				Fscan(in, &a[i][j])
			}
		}
		ans := search(-300, a[0][0], func(x int64) int64 {
			dp := make([][]int64, n)
			for i := range dp {
				dp[i] = make([]int64, m)
				for j := range dp[i] {
					dp[i][j] = 1e18
				}
			}
			dp[0][0] = a[0][0] - x
			for i, row := range a {
				for j, v := range row {
					d := v - x - int64(i+j)
					if d < 0 {
						return 1e18
					}
					if i == 0 {
						if j > 0 {
							dp[i][j] = dp[i][j-1] + d
						}
					} else {
						if j == 0 {
							dp[i][j] = dp[i-1][j] + d
						} else {
							dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + d
						}
					}
				}
			}
			return dp[n-1][m-1]
		})
		Fprintln(out, ans)
	}
}

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

package main

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

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

	var t, n, m int
	var a [][]int64
	search := func(l, r int64, f func(x int64) int64) int64 {
		for r-l > 200 {
			m1 := l + (r-l)/3
			m2 := r - (r-l)/3
			v1, v2 := f(m1), f(m2)
			if v1 == 1e18 {
				r=m1
				continue
			}
			if v2 == 1e18 {
				r=m2
				continue
			}
			if v1 < v2 {
				r = m2
			} else {
				l = m1
			}
		}
		min := f(l)
		for i := l + 1; i <= r; i++ {
			if v := f(i); v < min {
				min = v
			}
		}
		return min
	}
	min := func(a, b int64) int64 {
		if a < b {
			return a
		}
		return b
	}

	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m)
		a = make([][]int64, n)
		for i := range a {
			a[i] = make([]int64, m)
			for j := range a[i] {
				Fscan(in, &a[i][j])
			}
		}
		ans := search(-300, a[0][0], func(x int64) int64 {
			dp := make([][]int64, n)
			for i := range dp {
				dp[i] = make([]int64, m)
				for j := range dp[i] {
					dp[i][j] = 1e18
				}
			}
			dp[0][0] = a[0][0] - x
			for i, row := range a {
				for j, v := range row {
					d := v - x - int64(i+j)
					if d < 0 {
						continue
					}
					if i == 0 {
						if j > 0 {
							dp[i][j] = min(dp[i][j],dp[i][j-1] + d)
						}
					} else {
						if j == 0 {
							dp[i][j] = min(dp[i][j], dp[i-1][j] + d)
						} else {
							dp[i][j] = min(dp[i][j], min(dp[i-1][j], dp[i][j-1]) + d)
						}
					}
				}
			}
			return dp[n-1][m-1]
		})
		Fprintln(out, ans)
	}
}

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


Vova plans to go to the conference by train. Initially, the train is at the point $$$1$$$ and the destination point of the path is the point $$$L$$$. The speed of the train is $$$1$$$ length unit per minute (i.e. at the first minute the train is at the point $$$1$$$, at the second minute — at the point $$$2$$$ and so on).There are lanterns on the path. They are placed at the points with coordinates divisible by $$$v$$$ (i.e. the first lantern is at the point $$$v$$$, the second is at the point $$$2v$$$ and so on).There is also exactly one standing train which occupies all the points from $$$l$$$ to $$$r$$$ inclusive.Vova can see the lantern at the point $$$p$$$ if $$$p$$$ is divisible by $$$v$$$ and there is no standing train at this position ($$$p \not\in [l; r]$$$). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to $$$t$$$ different conferences, so you should answer $$$t$$$ independent queries.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of queries. Then $$$t$$$ lines follow. The $$$i$$$-th line contains four integers $$$L_i, v_i, l_i, r_i$$$ ($$$1 \le L, v \le 10^9$$$, $$$1 \le l \le r \le L$$$) — destination point of the $$$i$$$-th path, the period of the lantern appearance and the segment occupied by the standing train.

Print $$$t$$$ lines. The $$$i$$$-th line should contain one integer — the answer for the $$$i$$$-th query.

NoteFor the first example query, the answer is $$$3$$$. There are lanterns at positions $$$2$$$, $$$4$$$, $$$6$$$, $$$8$$$ and $$$10$$$, but Vova didn't see the lanterns at positions $$$4$$$ and $$$6$$$ because of the standing train.For the second example query, the answer is $$$0$$$ because the only lantern is at the point $$$51$$$ and there is also a standing train at this point.For the third example query, the answer is $$$1134$$$ because there are $$$1234$$$ lanterns, but Vova didn't see the lanterns from the position $$$100$$$ to the position $$$199$$$ inclusive.For the fourth example query, the answer is $$$0$$$ because the standing train covers the whole path.

package main

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

var t int
var len, v, l, r int

func divNum(x, v int) int {
  return x / v
}

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

  fmt.Fscanf(reader, "%d\n", &t);

  for i := 0; i < t; i++ {
    fmt.Fscanf(reader, "%d %d %d %d\n", &len, &v, &l, &r)
    fmt.Fprintf(writer, "%d\n", divNum(len, v) - divNum(r, v) + divNum(l - 1, v))
  }
}

package main

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

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

	var(
		t int64
	)
	var L, v, l, r int64

	fmt.Fscan(reader, &t)
	for i := int64(0); i < t; i ++ {
		fmt.Fscan(reader, &L, &v, &l, &r)
		fmt.Fprintf(writer,"%d\n",L / v - r / v + (l-1) / v);
	}
	writer.Flush();
}

package main

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

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

	var(
		t int64
	)
	var L, v, l, r int64

	fmt.Fscan(reader, &t)
	for i := int64(0); i < t; i ++ {
		fmt.Fscan(reader, &L, &v, &l, &r)
		fmt.Fprintf(writer,"%d\n",L / v - r / v + (l-1) / v);
	}

}


Monocarp is playing yet another computer game. In this game, his character has to kill a dragon. The battle with the dragon lasts $$$100^{500}$$$ seconds, during which Monocarp attacks the dragon with a poisoned dagger. The $$$i$$$-th attack is performed at the beginning of the $$$a_i$$$-th second from the battle start. The dagger itself does not deal damage, but it applies a poison effect on the dragon, which deals $$$1$$$ damage during each of the next $$$k$$$ seconds (starting with the same second when the dragon was stabbed by the dagger). However, if the dragon has already been poisoned, then the dagger updates the poison effect (i.e. cancels the current poison effect and applies a new one).For example, suppose $$$k = 4$$$, and Monocarp stabs the dragon during the seconds $$$2$$$, $$$4$$$ and $$$10$$$. Then the poison effect is applied at the start of the $$$2$$$-nd second and deals $$$1$$$ damage during the $$$2$$$-nd and $$$3$$$-rd seconds; then, at the beginning of the $$$4$$$-th second, the poison effect is reapplied, so it deals exactly $$$1$$$ damage during the seconds $$$4$$$, $$$5$$$, $$$6$$$ and $$$7$$$; then, during the $$$10$$$-th second, the poison effect is applied again, and it deals $$$1$$$ damage during the seconds $$$10$$$, $$$11$$$, $$$12$$$ and $$$13$$$. In total, the dragon receives $$$10$$$ damage.Monocarp knows that the dragon has $$$h$$$ hit points, and if he deals at least $$$h$$$ damage to the dragon during the battle — he slays the dragon. Monocarp has not decided on the strength of the poison he will use during the battle, so he wants to find the minimum possible value of $$$k$$$ (the number of seconds the poison effect lasts) that is enough to deal at least $$$h$$$ damage to the dragon.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of the test case contains two integers $$$n$$$ and $$$h$$$ ($$$1 \le n \le 100; 1 \le h \le 10^{18}$$$) — the number of Monocarp's attacks and the amount of damage that needs to be dealt. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le 10^9; a_i < a_{i + 1}$$$), where $$$a_i$$$ is the second when the $$$i$$$-th attack is performed.

For each test case, print a single integer — the minimum value of the parameter $$$k$$$, such that Monocarp will cause at least $$$h$$$ damage to the dragon.

NoteIn the first example, for $$$k=3$$$, damage is dealt in seconds $$$[1, 2, 3, 5, 6, 7]$$$.In the second example, for $$$k=4$$$, damage is dealt in seconds $$$[2, 3, 4, 5, 6, 7, 10, 11, 12, 13]$$$.In the third example, for $$$k=1$$$, damage is dealt in seconds $$$[1, 2, 4, 5, 7]$$$.

package main

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

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

	var t int
	for fmt.Fscan(in, &t); t > 0; t-- {
		var n int
		var h int64
		fmt.Fscan(in, &n, &h)

		dif := make([]int, n-1)
		var prev int
		fmt.Fscan(in, &prev)
		for i := 0; i < n-1; i++ {
			var cur int
			fmt.Fscan(in, &cur)
			dif[i] = cur - prev
			prev = cur
		}

		l, r := int64(0), h

		for r >= l {
			m := l + (r-l)/2

			sum := m
			for _, val := range dif {
				sum += int64(math.Min(float64(m), float64(val)))
			}

			if sum < h {
				l = m + 1
			} else {
				r = m - 1
			}
		}

		fmt.Fprintln(out, r+1)
	}
}

package main

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

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

	var t int
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		var n int
		var h int64
		fmt.Fscan(in, &n, &h)

		dif := make([]int, n-1)
		var prev int
		fmt.Fscan(in, &prev)
		for i := 0; i < n-1; i++ {
			var cur int
			fmt.Fscan(in, &cur)
			dif[i] = cur - prev
			prev = cur
		}

		l, r := int64(0), h

		for r >= l {
			m := l + (r-l)/2

			sum := m
			for _, val := range dif {
				sum += int64(math.Min(float64(m), float64(val)))
			}

			if sum < h {
				l = m + 1
			} else {
				r = m - 1
			}
		}

		fmt.Fprintln(out, r+1)
	}
}

package main

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

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

	var t int
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		var n int
		var h int64
		fmt.Fscan(in, &n, &h)

		dif := make([]int, n-1)
		var prev int
		fmt.Fscan(in, &prev)
		for i := 0; i < n-1; i++ {
			var cur int
			fmt.Fscan(in, &cur)
			dif[i] = cur - prev
			prev = cur
		}

		l, r := int64(0), h

		for l <= r {
			m := l + (r-l)/2

			sum := m

			for _, val := range dif {
				sum += int64(math.Min(float64(m), float64(val)))
			}

			if sum < h {
				l = m + 1
			} else {
				r = m - 1
			}
		}

		fmt.Fprintln(out, r+1)
	}
}

package main

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

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

	var t int
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		var n int
		var h int64
		fmt.Fscan(in, &n, &h)

		dif := make([]int, n-1)
		var prev int
		fmt.Fscan(in, &prev)
		for i := 0; i < n-1; i++ {
			var cur int
			fmt.Fscan(in, &cur)
			dif[i] = cur - prev
			prev = cur
		}

		l, r := int64(0), int64(1e18)

		for l <= r {
			m := l + (r-l)/2

			sum := m

			for _, val := range dif {
				sum += int64(math.Min(float64(m), float64(val)))
			}

			if sum < h {
				l = m + 1
			} else {
				r = m - 1
			}
		}

		fmt.Fprintln(out, r+1)
	}
}

package main

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

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

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

func check(mid int64, arr []int64, h int64) bool {
	sum := int64(0)
	for i := 0; i < len(arr); i++ {
		if i+1 < len(arr) {
			sum += min(mid, arr[i+1]-arr[i])
		} else {
			sum += mid
		}
	}

	return sum >= h
}

func main() {
	defer out.Flush()
	var t int
	for fmt.Fscan(in, &t); t > 0; t-- {
		var n int
		var h int64
		fmt.Fscan(in, &n, &h)
		arr := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
		}
		sort.Slice(arr, func(i, j int) bool {
			return arr[i] < arr[j]
		})
		ans := int64(0)
		l := int64(1)
		r := h
		for l <= r {
			mid := (l + r) >> 1
			if check(mid, arr, h) {
				ans = mid
				r = mid - 1
			} else {
				l = mid + 1
			}

		}
		fmt.Fprintln(out, ans)

	}

}

package main

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

var sc = bufio.NewScanner(os.Stdin)

func scan() string {
	for sc.Scan() {
		break
	}
	return sc.Text()
}

func scanInt() int {
	i, err := strconv.Atoi(scan())
	if err != nil {
		panic(err)
	}
	return i
}

func scanInt8() int8 {
	i, err := strconv.ParseInt(scan(), 10, 8)
	if err != nil {
		panic(err)
	}
	return int8(i)
}

func scanInt64() int64 {
	i, err := strconv.ParseInt(scan(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

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

func min(ints ...int) int {
	a := ints[0]
	n := len(ints)
	for i := 1; i < n; i++ {
		if ints[i] < a {
			a = ints[i]
		}
	}
	return a
}

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

func proc() {
	n, h := scanInt(), scanInt64()
	var ans int64
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = scanInt64()
	}
	mas := make([]int64, n-1)
	for i := 1; i < n; i++ {
		mas[i-1] = a[i] - a[i-1]
	}
	sort.Slice(mas, func(i, j int) bool { return mas[i] < mas[j] })
	for len(mas) > 0 {
		count := int64(len(mas) + 1)
		maxx := mas[0] * count
		if h >= maxx {
			ans += mas[0]
			h -= maxx
			for i := 1; i < len(mas); i++ {
				mas[i] -= mas[0]
			}
			mas = mas[1:]
			continue
		}
		k := h / count
		h -= k * count
		ans += k
		mas[0] -= k
		if h > 0 {
			ans++
			h -= count
		}
		break
	}
	if h > 0 {
		ans += h
	}
	fmt.Println(ans)
}

func main() {
	sc.Split(bufio.ScanWords)
	t := scanInt()
	for i := 0; i < t; i++ {
		proc()
	}
}

// 00
package main

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

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}

type Number interface {
	int64 | float64 | int
}

func min[K Number](a K, b K) K {
	if a < b {
		return a
	}
	return b
}
func max[K Number](a K, b K) K {
	if a > b {
		return a
	}
	return b
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n int
		var h int64
		fmt.Fscan(reader, &n, &h)
		a := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		left := int64(1)
		right := h
		for left < right {
			mid := (left + right) / 2
			tmp := int64(0)
			for i := 0; i < n; i++ {
				if i == n-1 {
					tmp += mid
				} else {
					tmp += min(a[i+1]-a[i], mid)
				}
			}
			if tmp < h {
				left = mid + 1
			} else if tmp > h {
				right = mid
			} else {
				break
			}
		}
		write(f, (left+right)/2, "\n")

	}
}

package main

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

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

	var T, n int
	var h int64
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &h)
		a := make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		l, r := int64(1), h
	o:
		for l < r {
			m := (l + r) / 2
			left := h
			for i, t := range a {
				if i < n-1 && a[i+1]-t < m {
					t = a[i+1] - t
				} else {
					t = m
				}
				if left -= t; left <= 0 {
					r = m
					continue o
				}
			}
			l = m + 1
		}
		Fprintln(out, l)
	}
}

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

package main

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

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

	var T int
	fmt.Fscan(in, &T)

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

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

		low, high := int64(1), h
		for high-low >= 1 {
			k := (high + low) / 2
			t := h

			pos := a[0] + k - 1
			t -= k
			for i := 1; i < n; i++ {
				if pos >= a[i] {
					t -= a[i] + k - 1 - pos // minus overlap
				} else {
					t -= k
				}
				pos = a[i] + k - 1
			}

			if t > 0 {
				low = k + 1
			} else {
				high = k
			}
			// fmt.Println(low, high, k)
		}
		fmt.Fprintln(out, high)
	}
}

package main

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

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

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

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		var n int
		var health int64
		fmt.Fscan(in, &n, &health)
		var a []int64 = make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		n--
		var lo, hi int64 = 0, health
		for lo < hi {
			var mi = (lo + hi) / 2
			var dmg int64 = mi
			for i := 0; i < n; i++ {
				dmg += min(a[i+1]-a[i], mi)
			}
			if dmg < health {
				lo = mi + 1
			} else {
				hi = mi
			}
		}
		fmt.Fprintf(out, "%d\n", lo)
	}
	out.Flush()
}

package main

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

func main() {
	defer _w.Flush()
	t := ReadInt()
	for i := int64(0); i < t; i++ {
		solve()
	}
}
func solve() {
	n, h := ReadInt(), ReadInt()
	a := ReadInts(n)
	t := Search(h+1, func(k int64) bool {
		sum := int64(0)
		for i := int64(0); i < n-1; i++ {
			d := a[i+1] - a[i]
			Chmin(&d, k)
			sum += d
			if sum >= h {
				return true
			}
		}
		sum += k
		return sum >= h
	})
	println(t)
}

func Max(a, b int64) int64 { Chmax(&a, b); return a }
func Min(a, b int64) int64 { Chmin(&a, b); return a }
func Chmax(p *int64, x int64) {
	if *p < x {
		*p = x
	}
}
func Chmin(p *int64, x int64) {
	if *p > x {
		*p = x
	}
}

func Search(n int64, f func(int64) bool) int64 {
	// Define f(-1) == false and f(n) == true.
	// Invariant: f(i-1) == false, f(j) == true.
	i, j := int64(0), int64(n)
	for i < j {
		h := int64(uint64(i+j) >> 1) // avoid overflow when computing h
		// i ≤ h < j
		if !f(h) {
			i = h + 1 // preserves f(i-1) == false
		} else {
			j = h // preserves f(j) == true
		}
	}
	// i == j, f(i-1) == false, and f(j) (= f(i)) == true  =>  answer is i.
	return i
}

var _r, _w = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)

func ReadString() (s string) { fmt.Fscan(_r, &s); return }
func ReadInt() (i int64)     { fmt.Fscan(_r, &i); return }
func ReadInts(n int64) (a []int64) {
	for ; n > 0; n-- {
		a = append(a, ReadInt())
	}
	return
}
func print(a ...interface{})   { fmt.Fprint(_w, a...) }
func println(a ...interface{}) { fmt.Fprintln(_w, a...) }
func prints(s []int)           { v := fmt.Sprint(s); println(v[1 : len(v)-1]) }

package main

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

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

func main() {
	defer out.Flush()

	var tc int
	for fmt.Fscan(in, &tc); tc > 0; tc-- {
		var n int
		var h int64
		fmt.Fscan(in, &n, &h)
		a := make([]int64, n)
		for i := range a {
			fmt.Fscan(in, &a[i])
		}

		lo, hi := int64(1), int64(h)
		for lo <= hi {
			k := (lo + hi) / 2
			remh := h - k
			for i := 0; remh > 0 && i < n-1; i++ {
				remh -= k
				overlap := a[i] + k - a[i+1]
				if overlap > 0 {
					remh += overlap
				}
			}
			if remh > 0 {
				lo = k + 1
			} else {
				hi = k - 1
			}
		}
		fmt.Fprintln(out, lo)
	}
}

package main

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

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

	var t int
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		var n int
		var h int64
		fmt.Fscan(in, &n, &h)

		dif := make([]int, n)
		var prev int
		fmt.Fscan(in, &prev)
		for i := 0; i < n-1; i++ {
			var cur int
			fmt.Fscan(in, &cur)
			dif[i] = cur - prev
			prev = cur
		}
		dif[n-1] = 1e9

		l, r := int64(0), int64(1e18)

		for r-l > 1 {
			m := l + (r-l)/2

			sum := int64(0)

			for _, val := range dif {
				sum += int64(math.Min(float64(m), float64(val)))
			}

			if sum < h {
				l = m
			} else {
				r = m
			}
		}

		fmt.Fprintln(out, r)
	}
}

package main

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

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

	var t int
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		var n int
		var h int64
		fmt.Fscan(in, &n, &h)

		dif := make([]int, n)
		var prev int
		fmt.Fscan(in, &prev)
		for i := 0; i < n-1; i++ {
			var cur int
			fmt.Fscan(in, &cur)
			dif[i] = cur - prev
			prev = cur
		}
		dif[n-1] = 1e9

		l, r := int64(0), h

		for r-l > 1 {
			m := l + (r-l)/2

			sum := int64(0)

			for _, val := range dif {
				sum += int64(math.Min(float64(m), float64(val)))
			}

			if sum < h {
				l = m
			} else {
				r = m
			}
		}

		fmt.Fprintln(out, r)
	}
}

func bidnarySearch(nums []int, target int) int {
	l, r := 0, len(nums)-1
	for l <= r {
		m := l + (r-l)/2
		if nums[m] == target {
			return m
		}
		if nums[m] < target {
			l = m + 1
		} else {
			r = m - 1
		}
	}
	return -1
}

package main

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

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

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

func check(mid int64, arr []int64, h int64) bool {
	sum := int64(0)
	for i := 0; i < len(arr); i++ {
		if i+1 < len(arr) {
			sum += min(mid, arr[i+1]-arr[i])
		} else {
			sum += mid
		}
	}

	return sum >= h
}
const  maxn = 100
func main() {
	defer out.Flush()
	arr := make([]int64, maxn)
	var t int
	for fmt.Fscan(in, &t); t > 0; t-- {
		var n int
		var h int64
		fmt.Fscan(in, &n, &h)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
		}
		sort.Slice(arr, func(i, j int) bool {
			return arr[i] < arr[j]
		})
		ans := int64(0)
		l := int64(1)
		r := h
		for l <= r {
			mid := (l + r) >> 1
			if check(mid, arr, h) {
				ans = mid
				r = mid - 1
			} else {
				l = mid + 1
			}

		}
		fmt.Fprintln(out, ans)

	}

}