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 "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 {
	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])
	}
}


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 (
	"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 (
	"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]
}


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


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 (
  "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);
	}

}


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

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)

	}

}


package main

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

//goland:noinspection GoUnusedFunction
func min(a int, nums ...int) int {
	min := a

	for _, num := range nums {
		if num < min {
			min = num
		}
	}
	return min
}

//goland:noinspection GoUnusedFunction
func max(a int, nums ...int) int {
	max := a

	for _, num := range nums {
		if num > max {
			max = num
		}
	}
	return max
}

//goland:noinspection GoUnusedFunction
func maxFloat(a float64, nums ...float64) float64 {
	max := a

	for _, num := range nums {
		if num > max {
			max = num
		}
	}
	return max
}

//goland:noinspection GoUnusedFunction
func fprintlnArr(w io.Writer, arr []int) {
	for _, val := range arr {
		fmt.Fprint(w, val, " ")
	}
	fmt.Fprintln(w)
}

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 x, y int
		fmt.Fscan(in, &x, &y)
		arr := make([][]int, x)
		cnt1, cnt2 := make([][]int, x), make([][]int, x)
		for i := 0; i < x; i++ {
			arr[i] = make([]int, y)
			cnt1[i] = make([]int, y)
			cnt2[i] = make([]int, y)
		}
		for i := 0; i < x; i++ {
			for j := 0; j < y; j++ {
				fmt.Fscan(in, &arr[i][j])
				cnt1[i][j] = arr[i][j]
				cnt2[i][j] = arr[i][j]
			}
		}

		for i := 0; i < x-1; i++ {
			for j := 0; j < y-1; j++ {
				cnt1[i+1][j+1] += cnt1[i][j]
			}
		}
		for i := 0; i < x-1; i++ {
			for j := 1; j < y; j++ {
				cnt2[i+1][j-1] += cnt2[i][j]
			}
		}
		//fmt.Println(cnt1)
		//fmt.Println(cnt2)

		ans := 0
		for i := 0; i < x; i++ {
			for j := 0; j < y; j++ {
				cur := -arr[i][j]

				curX, curY := i, j
				for curX < x-1 && curY > 0 {
					curX++
					curY--
				}
				cur += cnt2[curX][curY]
				//fmt.Print(cur, " ")

				curX, curY = i, j
				for curX < x-1 && curY < y-1 {
					curX++
					curY++
				}
				//fmt.Println(curX, curY)
				cur += cnt1[curX][curY]
				//fmt.Println(cur)
				ans = max(ans, cur)
			}
		}
		fmt.Fprintln(out, ans)
	}
}

/*maxEvenCnt1, maxOddCnt1 := 0, 0
for i := 0; i < y; i++ {
	cur := cnt1[x-1][i]
	if i%2 == 0 {
		maxEvenCnt1 = max(maxEvenCnt1, cur)
	} else {
		maxOddCnt1 = max(maxOddCnt1, cur)
	}
}
for i := 0; i < x; i++ {
	cur := cnt1[i][y-1]
	if i%2 == 0 {
		maxEvenCnt1 = max(maxEvenCnt1, cur)
	} else {
		maxOddCnt1 = max(maxOddCnt1, cur)
	}
}

for i := 0; i < x-1; i++ {
	for j := 1; j < y; j++ {
		cnt2[i+1][j-1] += cnt2[i][j]
	}
}
maxEvenCnt2, maxOddCnt2 := 0, 0
for i := 0; i < y; i++ {
	cur := cnt2[x-1][i]
	if i%2 == 0 {
		maxEvenCnt2 = max(maxEvenCnt2, cur)
	} else {
		maxOddCnt2 = max(maxOddCnt2, cur)
	}
}
for i := 0; i < x; i++ {
	cur := cnt2[i][y-1]
	if i%2 == 0 {
		maxEvenCnt2 = max(maxEvenCnt2, cur)
	} else {
		maxOddCnt2 = max(maxOddCnt2, cur)
	}
}*/

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, m int
		fmt.Fscan(in, &n, &m)

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

		sums := make([][]int64, n)
		for i := 0; i < n; i++ {
			sums[i] = make([]int64, m)
		}

		dir := [][]int{{-1, -1}, {-1, 1}, {1, -1}, {1, 1}}
		var max int64
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {

				sums[i][j] = a[i][j]
				// 4 directions
				for _, d := range dir {
					x := i + d[0]
					y := j + d[1]
					for x >= 0 && x < n && y >= 0 && y < m {
						sums[i][j] += a[x][y]
						x += d[0]
						y += d[1]
					}
				}

				if sums[i][j] > max {
					max = sums[i][j]
				}
			}
		}
		fmt.Fprintln(out, max)
	}
}

package main

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

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

	solve := func(curCase int) {
		var n,m int
		Fscan(in, &n,&m)
		a := make([][]int, n)
		for i := range a {
			a[i] = make([]int, m)
			for j := range a[i] {
				Fscan(in, &a[i][j])
			}
		}
		ans :=0
		dir4 := []struct{ x, y int }{{-1, -1}, {1, 1}, {1, -1}, {-1, 1}}
		for i, r := range a {
			for j, v := range r {
				_, _ = i, j
				s := v
				for _, d := range dir4 {
					x,y := i,j
					for {
						x+=d.x
						y+=d.y
						if x < 0 || x >=n ||y<0 || y >=m {
							break
						}
						s+=a[x][y]
					}
				}
				ans = max(ans, s)
			}
		}
		Fprintln(out, ans)

	}

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

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

}

func main() { run(os.Stdin, os.Stdout) }
func max(a, b int) int {
	if b > a {
		return b
	}
	return a
}

// 00
package main

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

func write_int(f *bufio.Writer, a int) {
	f.Write([]byte(strconv.Itoa(a)))
}
func write_string(f *bufio.Writer, a string) {
	f.Write([]byte(a))
}

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, m int
		fmt.Fscan(reader, &n, &m)
		a := make([][]int, n)
		for i := 0; i < n; i++ {
			a[i] = make([]int, m)
		}
		d1 := make(map[int]int)
		d2 := make(map[int]int)
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				fmt.Fscan(reader, &a[i][j])
				d1[i+j] += a[i][j]
				d2[i-j] += a[i][j]
			}
		}
		ans := 0
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				tmp := d1[i+j] + d2[i-j] - a[i][j]
				if tmp > ans {
					ans = tmp
				}
			}
		}
		// fmt.Println(ans)
		write_int(f, ans)
		write_string(f, "\n")
	}
}

// 00
package main

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

func write_int(f *bufio.Writer, a int) {
	f.Write([]byte(strconv.Itoa(a)))
}
func write_string(f *bufio.Writer, a string) {
	f.Write([]byte(a))
}

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, m int
		fmt.Fscan(reader, &n, &m)
		a := make([][]int, n)
		for i := 0; i < n; i++ {
			a[i] = make([]int, m)
		}
		d1 := make(map[int]int)
		d2 := make(map[int]int)
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				fmt.Fscan(reader, &a[i][j])
				d1[i+j] += a[i][j]
				d2[i-j] += a[i][j]
			}
		}
		ans := 0
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				tmp := d1[i+j] + d2[i-j] - a[i][j]
				if tmp > ans {
					ans = tmp
				}
			}
		}
		fmt.Println(ans)

	}
}

package main

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

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

	n := readInt(in)
	for i := 0; i < n; i++ {
		solution(in, out)
	}
}

func solution(in *bufio.Reader, out *bufio.Writer) {
	arr := readArrInt(in)
	n := arr[0]
	m := arr[1]

	board := make([][]int, n)
	for i := 0; i < n; i++ {
		board[i] = readArrInt(in)
	}

	maxSum := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			// get the sum by moving up (+1, +1) from current location
			sum := 0
			for k := 1; i+k < n && j+k < m; k++ {
				sum += board[i+k][j+k]
			}

			for k := -1; i+k >= 0 && j+k >= 0; k-- {
				sum += board[i+k][j+k]
			}

			for k := 1; i+k < n && j-k >= 0; k++ {
				sum += board[i+k][j-k]
			}

			for k := 1; i-k >= 0 && j+k < m; k++ {
				sum += board[i-k][j+k]
			}

			sum += board[i][j]
			maxSum = Max(maxSum, sum)
		}
	}

	out.WriteString(strconv.Itoa(maxSum) + "\n")
}

func Max(sum int, sum2 int) int {
	if sum > sum2 {
		return sum
	}
	return sum2

}

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

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

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

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

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

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

package main

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

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

func solve(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var t int
	for fmt.Fscan(in, &t); t > 0; t-- {
		var n, m int
		fmt.Fscan(in, &n)
		fmt.Fscan(in, &m)
		a := make([][]int, n)
		// Top left to bottom right diags
		d1 := make([][]int, n)
		// Bottom left to top right diags
		d2 := make([][]int, n)
		for i := 0; i < n; i++ {
			a[i] = make([]int, m)
			d1[i] = make([]int, m)
			d2[i] = make([]int, m)
		}
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				fmt.Fscan(in, &a[i][j])
				d1[i][j] = a[i][j]
				d2[i][j] = a[i][j]
				if i > 0 && j > 0 {
					d1[i][j] += d1[i-1][j-1]
				}
				if i > 0 && j < m-1 {
					d2[i][j] += d2[i-1][j+1]
				}
			}
		}
		for i := n - 1; i > 0; i-- {
			for j := 0; j < m; j++ {
				if j > 0 {
					d1[i-1][j-1] = d1[i][j]
				}
				if j < m-1 {
					d2[i-1][j+1] = d2[i][j]
				}
			}
		}
		ans := 0
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				ck := d1[i][j] + d2[i][j] - a[i][j]
				if ck > ans {
					ans = ck
				}
			}
		}
		fmt.Println(ans)
	}
}

package main

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

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

func main(){
  defer writer.Flush()

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

  for test := 1; test <= T; test++ {
    var n,m int
    fmt.Fscan(reader,&n,&m)

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

    calc := func (tx,ty,i,j int) int {
      ret := 0
      for ; i + tx >= 1 && j + ty >= 1 && i + tx <= n && j + ty <= m; {
        i += tx
        j += ty
        ret += a[i][j]
      }
      return ret
    }

    ans := 0
    for i := 1; i <= n; i++ {
      for j := 1; j <= m; j++ {
        sum := calc(-1,-1,i,j) + calc(-1,1,i,j) + calc(1,-1,i,j) + calc(1,1,i,j) + a[i][j]
        if ans < sum {
          ans = sum
        }
      }
    }
    fmt.Fprintf(writer,"%d\n",ans)
  }
}

package main

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

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

	var T int
	for fmt.Fscan(in, &T); T > 0; T-- {
		var n, m int
		fmt.Fscan(in, &n, &m)
		a := make([][]int, n)
		for i := 0;i < n; i++ {
			a[i] = make([]int, m)
			for j := 0;j < m; j++ {
				fmt.Fscan(in, &a[i][j])
			}
		}
		max := func(a, b int) int {
			if a > b {
				return a
			}
			return b
		}
		var ans int
		for i := 0;i < n; i++ {
			for j := 0;j < m; j++ {
				temp := 0
				p, q := i, j
				for {
					if p < 0 || p >= n || q < 0 || q >= m {
						break
					}
					temp += a[p][q]
					p++
					q++
				}
				p, q = i, j
				for {
					if p < 0 || p >= n || q < 0 || q >= m {
						break
					}
					temp += a[p][q]
					p++
					q--
				}
				p, q = i, j
				for {
					if p < 0 || p >= n || q < 0 || q >= m {
						break
					}
					temp += a[p][q]
					p--
					q++
				}
				p, q = i, j
				for {
					if p < 0 || p >= n || q < 0 || q >= m {
						break
					}
					temp += a[p][q]
					p--
					q--
				}
				temp -= a[i][j] * 3
				ans = max(ans, temp)
			}
		}
		fmt.Fprintln(out, ans)
	}
}

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

package main

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

func toInt(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}

func sumDiag(grid [][]int, i, j int) int {
	total := grid[i][j]
	a, b := i+1, j+1
	for a < len(grid) && b < len(grid[a]) {
		total += grid[a][b]
		a++
		b++
	}
	a, b = i-1, j-1

	for a >= 0 && b >= 0 {
		total += grid[a][b]
		a--
		b--
	}

	a, b = i-1, j+1
	for a >= 0 && b < len(grid[a]) {
		total += grid[a][b]
		a--
		b++
	}
	a, b = i+1, j-1
	for a < len(grid) && b >= 0 {
		total += grid[a][b]
		a++
		b--
	}
	return total
}
func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	testCases, _ := strconv.Atoi(scanner.Text())

	for idx := 0; idx < testCases; idx++ {
		scanner.Scan()
		mn := strings.Split(scanner.Text(), " ")
		m, n := toInt(mn[0]), toInt(mn[1])

		grid := make([][]int, 0)

		for i := 0; i < m; i++ {
			scanner.Scan()
			line := strings.Split(scanner.Text(), " ")
			lineInt := make([]int, 0)
			for idx := 0; idx < len(line); idx++ {
				lineInt = append(lineInt, toInt(line[idx]))
			}
			grid = append(grid, lineInt)
		}
		maximum := math.MinInt
		for i := 0; i < m; i++ {
			for j := 0; j < n; j++ {
				t := sumDiag(grid, i, j)
				if t > maximum {
					maximum = t
				}
			}
		}

		fmt.Println(maximum)

	}
}

package main

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

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)
func slove() {
	var n, m int
	fmt.Fscan(r, &n, &m)
	board, one, two := make([][]int, n), make([][]int, n+1), make([][]int, n+1)
	for i := range board {
		board[i], one[i], two[i] = make([]int, m), make([]int, m+1), make([]int, m+1)
	}
	one[n], two[n] = make([]int, m+1), make([]int, m+1)
	for i := range board {
		for j := range board[i] {
			fmt.Fscan(r, &board[i][j])
			one[i+1][j+1] = one[i][j] + board[i][j]
			two[i+1][j] = two[i][j+1] + board[i][j]
		}
	}
	for i:=n-1;i>=0;i-- {
		for j  := range board[i] {
			one[i][j] = one[i+1][j+1]
			two[i][j+1] = two[i+1][j]
		}
	}
	ans := 0
	for i := range board {
		for j := range board[i] {
			ans = max(ans, one[i+1][j+1] + two[i+1][j] - board[i][j])
		}
	}
	fmt.Fprintln(w, ans)
}

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

func main() {
	defer w.Flush()
	var n int
	fmt.Fscan(r, &n)
	for i:=1;i<=n;i++ {
		slove()
	}
}

package main

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

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

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

	t := 0
	fmt.Fscan(in, &t)
	for k := 0; k < t; k++ {
		n, m := 0, 0
		fmt.Fscan(in, &n, &m)
		matrix := make([][]int, n)
		for i := range matrix {
			matrix[i] = make([]int, m)
		}
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				fmt.Fscan(in, &matrix[i][j])
			}
		}
		ans := 0
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				ans = max(ans, dfs(matrix, []int{i, j}, n, m))
			}
		}
		fmt.Fprintln(out, ans)
	}
}

var posx = [4]int{-1, 1, 1, -1}
var posy = [4]int{-1, 1, -1, 1}

func dfs(matrix [][]int, start []int, n int, m int) int {
	res := 0
	x, y := start[0], start[1]
	for i := 0; i < 4; i++ {
		x1 := x
		y1 := y
		for {
			x1 += posx[i]
			y1 += posy[i]
			if x1 < 0 || y1 < 0 || x1 >= n || y1 >= m {
				break
			} else {
				res += matrix[x1][y1]
			}
		}
	}
	return res + matrix[x][y]
}

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}

package main

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

func getMySum(x [][]int, n int, m int, a int, b int) int {
	sum := 0

	tmpa := a
	tmpb := b
	for tmpa >= 0 && tmpb >= 0 && tmpa <= n-1 && tmpb <= m-1 {
		sum += x[tmpa][tmpb]
		tmpa--
		tmpb--
	}

	tmpa = a
	tmpb = b
	tmpa++
	tmpb++
	for tmpa >= 0 && tmpb >= 0 && tmpa <= n-1 && tmpb <= m-1 {
		sum += x[tmpa][tmpb]
		tmpa++
		tmpb++
	}

	tmpa = a
	tmpb = b
	tmpa--
	tmpb++
	for tmpa >= 0 && tmpb >= 0 && tmpa <= n-1 && tmpb <= m-1 {
		sum += x[tmpa][tmpb]
		tmpa--
		tmpb++
	}

	tmpa = a
	tmpb = b
	tmpa++
	tmpb--
	for tmpa >= 0 && tmpb >= 0 && tmpa <= n-1 && tmpb <= m-1 {
		sum += x[tmpa][tmpb]
		tmpa++
		tmpb--
	}

	return sum
}

func main() {
	var (
		t int
	)

	reader := bufio.NewReader(os.Stdin)
	fmt.Scanf("%d\n", &t)

	for t > 0 {
		t--

		text, _ := reader.ReadString('\n')
		ninps := strings.Fields(text)
		n, _ := strconv.Atoi(ninps[0])
		m, _ := strconv.Atoi(ninps[1])

		mat := make([][]int, n)
		//maxarr := make([][]int, n)

		for i := 0; i < n; i++ {
			mat[i] = make([]int, m)
			//maxarr[i] = make([]int, m)

			text, _ = reader.ReadString('\n')
			ninps = strings.Fields(text)
			for j, val := range ninps {
				mat[i][j], _ = strconv.Atoi(val)
			}
		}

		largest := -1
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				a := getMySum(mat, n, m, i, j)
				if a > largest {
					largest = a
				}
			}
		}
		fmt.Println(largest)
	}
}

package main

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

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

func solve() string {
	var n, m int
	fmt.Fscan(in, &n, &m)
	var matrix = make([][]int, n)
	for i := 0; i < n; i++ {
		matrix[i] = make([]int, m)
		for j := 0; j < m; j++ {
			fmt.Fscan(in, &matrix[i][j])
		}
	}

	var res, tmp int

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			tmp = 0
			var x, y int
			for x, y = i, j; x < n && y < m; x, y = x+1, y+1 {
				tmp += matrix[x][y]
			}
			for x, y = i, j; x >= 0 && y < m; x, y = x-1, y+1 {
				tmp += matrix[x][y]
			}
			for x, y = i, j; x < n && y >= 0; x, y = x+1, y-1 {
				tmp += matrix[x][y]
			}
			for x, y = i, j; x >= 0 && y >= 0; x, y = x-1, y-1 {
				tmp += matrix[x][y]
			}

			tmp -= 3 * matrix[i][j]

			if tmp > res {
				res = tmp
			}
		}
	}

	return strconv.FormatInt(int64(res), 10)
}

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

package main

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

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

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

	var T int
	var n int
	var m int
	var x int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &m)
		Fscan(in, &n)

		g := make([][]int, m)
		for i := 0; i < m; i++ {
			g[i] = make([]int, n)
			for j := 0; j < n; j++ {
				Fscan(in, &x)
				g[i][j] = x
			}
		}

		l := make([]int, m+n-1)
		r := make([]int, m+n-1)

		for i := 0; i < m; i++ {
			for j := 0; j < n; j++ {
				l[i+j] += g[i][j]
				r[i-j+n-1] += g[i][j]
			}
		}

		ans := 0
		for i := 0; i < m; i++ {
			for j := 0; j < n; j++ {
				t := l[i+j] + r[i-j+n-1] - g[i][j]
				if t > ans {
					ans = t
				}
			}
		}

		Fprintln(out, ans)
	}
}


package main

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

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

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer = bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  bad := map[int]bool{}
  n := getI()
  for i := 0; i < n; i++ {
    for j := 0; j < n; j++ {
      x := getI()
      if x == -1 {
        continue
      }
      if x & 1 != 0 {
        bad[i] = true
      }
      if x & 2 != 0 {
        bad[j] = true
      }
    }
  }
  good := []int{}
  for i := 0; i < n; i++ {
    if !bad[i] {
      good = append(good, i+1)
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", len(good)))
  if len(good) != 0 {
    writer.WriteString(fmt.Sprintf("%d", good[0]))
    for i := 1; i < len(good); i++ {
      writer.WriteString(fmt.Sprintf(" %d", good[i]))
    }
    writer.WriteString("\n")
  }
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var cr int
	fmt.Fscanf(reader, "%d\n", &cr)

	mp := make(map[int]bool)
	for i := 1; i <= cr; i++ {
		for j := 1; j <= cr; j++ {
			var each int
			fmt.Fscan(reader, &each)
			switch each {
			case -1:
				continue
			case 1:
				mp[i] = true
			case 2:
				mp[j] = true
			case 3:
				mp[i], mp[j] = true, true
			}
		}
	}

	var chn []int
	for i := 1; i <= cr; i++ {
		if !mp[i] {
			chn = append(chn, i)
		}
	}

	fmt.Println(len(chn))
	for _, x := range chn {
		fmt.Print(x, " ")
	}
}

package main

import (
	"fmt"
)

var (
	n int
)

func main() {
	fmt.Scan(&n)
	ans := make([]int, 0, n)
	for i := 0; i < n; i++ {
		ok := true
		for j := 0; j < n; j++ {
			var v int
			fmt.Scan(&v)
			switch v {
			case 1, 3:
				ok = false
			}
		}
		if ok {
			ans = append(ans, i)
		}
	}
	fmt.Println(len(ans))
	for i, v := range ans {
		fmt.Print(v + 1)
		if i == len(ans) - 1 {
			fmt.Print("\n")
		} else {
			fmt.Print(" ")
		}
	}
}

package main

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

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

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

	n := nextInt()
	a, cnt := 0, 0
	ret := make([]int, 0, 100)

	for i := 1; i <= n; i++ {
		ok := true

		for j := 0; j < n; j++ {
			a = nextInt()

			if a == 1 || a == 3 {
				ok = false
			}
		}

		if ok {
			ret = append(ret, i)
			cnt++
		}
	}

	println(cnt)

	if cnt > 0 {
		for i := range ret {
			printf("%d ", ret[i])
		}

		println()
	}
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

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 (
  "fmt"
)

func main() {
  var n int
  fmt.Scanf("%d", &n)

  var array [100][100]int
  for i := 0; i < n; i++ {
    for j := 0; j < n; j++ {
      fmt.Scan(&array[i][j])
    }
  }
  //for i := 0; i < n; i++ {
  //  fmt.Println(array[i])
  //}

  var results [100]int
  for i := 0; i < n; i++ {
    for j := 0; j < n; j++ {
      if array[i][j] != -1 && array[i][j] != 0 && array[i][j] != 2 {

        //fmt.Println(array[i][j] != -1 && array[i][j] != 0 && array[i][j] != 2)
        //if i == 0 {
        //  fmt.Println(array[i][j])
        //}

        results[i] = 1
        //fmt.Println("results[i]: ", results[i])
      }
    }
  }

  count := 0
  for i := 0; i < n; i++ {
    if results[i] == 0 {
      count += 1
    }
  }
  fmt.Println(count)

  //fmt.Println(results)
  for i := 0; i < n; i++ {
    if results[i] == 0 {
      fmt.Println(i+1)
    }
  }
}

package main

import (
  "fmt"
)

func main() {
  var n int
  fmt.Scanf("%d", &n)

  var array [100][100]int
  for i := 0; i < n; i++ {
    for j := 0; j < n; j++ {
      fmt.Scan(&array[i][j])
    }
  }
  //for i := 0; i < n; i++ {
  //  fmt.Println(array[i])
  //}

  var results [100]int
  for i := 0; i < n; i++ {
    for j := 0; j < n; j++ {
      if array[i][j] != -1 && array[i][j] != 0 && array[i][j] != 2 {

        //fmt.Println(array[i][j] != -1 && array[i][j] != 0 && array[i][j] != 2)
        //if i == 0 {
        //  fmt.Println(array[i][j])
        //}

        results[i] = 1
        //fmt.Println("results[i]: ", results[i])
      }
    }
  }

  count := 0
  for i := 0; i < n; i++ {
    if results[i] == 0 {
      count += 1
    }
  }
  fmt.Println(count)

  //fmt.Println(results)
  for i := 0; i < n; i++ {
    if results[i] == 0 {
      fmt.Print(i+1)
    }
  }
}