package main

import "fmt"
import "sort"
import "bufio"
import "os"

var in = bufio.NewReaderSize(os.Stdin, 2<<16)

func gcd(a int, b int) int {
	if a < b {
		return gcd(b, a)
	}
	for b != 0 {
		t := a % b
		a = b
		b = t
	}
	return a
}
func main() {
  var n int
  fmt.Fscan(in, &n)
  x := make([]int, n)
  for i := 0; i < n; i++ {
      fmt.Fscan(in, &x[i])
  }
  sort.Ints(x)
  var nd int
  nd = 0
  for i := 1; i < n; i++ {
      nd = gcd (x[i] - x[i-1], nd)
  }
  var m int
  m = 0
  for i := 1; i < n; i++ {
      m = m - 1 + (x[i] - x[i-1]) / nd
  }

  out := bufio.NewWriter(os.Stdout)
  defer out.Flush()
  fmt.Fprintln(out, m)
}

package main
import "fmt"
import "sort"
import "bufio"
import "os"
func main(){
	var n,c,d,f,gcd,ans int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n);
	a:= make([]int,n,n)
	for i:=0; i < n; i++{
		fmt.Fscan(in, &a[i])
	}
	sort.Ints(a)
//	for i:=0; i < n; i++{
//		fmt.Printf("%d", a[i])
//	}
	c=a[1] - a[0]
	d=a[2] - a[1]
	for d != 0{
	      f = c % d
	      c = d
	      d = f
	}
	gcd = c

	for i:=2; i < n; i++{
	      c = gcd
	      d = a[i] - a[i-1]
	      if (d%gcd == 0){
	            continue
	      }
		for d != 0{
	      f = c % d
	      c = d
	      d = f
	}
	gcd = c
	}
	for i:=1; i < n; i++{
	      ans = ans + (a[i]-a[i-1])/gcd - 1
	}
	fmt.Printf("%d", ans)
}

package main
import "fmt"
import "math/rand"
import "sort"
import "bufio"
import "os"
func main(){
	var n,c,d,f,gcd,ans int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n);
	a:= make([]int,n,n)
	for i:=0; i < n; i++{
		fmt.Fscan(in, &a[i])
	}
	for i:=0; i < n; i++{
	     	c = rand.Intn(i + 1)
		a[i], a[c] = a[c], a[i]
	}
	sort.Ints(a)
//	for i:=0; i < n; i++{
//		fmt.Printf("%d", a[i])
//	}
	c=a[1] - a[0]
	d=a[2] - a[1]
	for d != 0{
	      f = c % d
	      c = d
	      d = f
	}
	gcd = c

	for i:=2; i < n; i++{
	      c = gcd
	      d = a[i] - a[i-1]
	      if (d%gcd == 0){
	            continue
	      }
		for d != 0{
	      f = c % d
	      c = d
	      d = f
	}
	gcd = c
	}
	for i:=1; i < n; i++{
	      ans = ans + (a[i]-a[i-1])/gcd - 1
	}
	fmt.Printf("%d", ans)
}

package main

import "bufio"
import "os"
import "fmt"
import "sort"

func main() {
	var n, k, res int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n)
	a := make([]int, n, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}
	sort.Ints(a)

	k = 0
	for i := 0; i < n - 1; i++ {
		a[i] = a[i+1] - a[i]
		k = gcd(k, a[i])
	}

	res = 0
	if (k != 0) {
		for i := 0; i < n - 1; i++ {
		    res += a[i] / k
		}
		res -= n - 1
	}

	fmt.Println(res)
}

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

package main

import "fmt"
import "bufio"
import "os"
import "math/rand"
import "sort"

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

func main() {
	var n int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n)
	a := make([]int, n, n)
	mv, xv := 2147483647, -2147483648
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
		if a[i] < mv {
			mv = a[i]
		}
		if a[i] > xv {
			xv = a[i]
		}
	}
	for i := 0; i < n; i++ {
		j := rand.Intn(i + 1)
		a[i], a[j] = a[j], a[i]
	}
	sort.Ints(a)
	difs := 0
	for i := 1; i < n; i++ {
		difs = gcd(difs, a[i] - a[i-1])
	}
	kur := (a[n-1] - a[0]) / difs
	fmt.Println(kur - n + 1)
}

package main

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

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

func main() {
	var n int

	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

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

	dist := a[1] - a[0]
	for i := 1;i < len(a) - 1;i ++ {
		dist = gcd(dist, a[i + 1] - a[i])
	}
	ans := 0
	for i := 0;i < len(a) - 1;i ++ {
		ans += ((a[i + 1] - a[i]) / dist - 1)
	}
	fmt.Printf("%d", ans)
}

package main

import "fmt"
import "sort"
import "bufio"
import "os"

func f(a, b int) int {
	var c int
	c = 1
	for i := 0; a != 0; i++ {
	    c = b
	    b = a
	    var temp int = c % b
	    a = temp
	    c = b
	}

	return c
}

func main() {
	var n int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n)
	aa := make([]int, n, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &aa[i])
	}

	sort.Ints(aa)
	bb := make([]int, n - 1, n - 1)

	for i := 0; i < n - 1; i++ {
	    bb[i] = aa[i + 1] - aa[i]
	}

    var answer int
    answer = f(bb[1], bb[0])

    for i := 2; i < n - 1; i++ {
        answer = f(answer, bb[i]);
    }
    fmt.Println((aa[n-1] - aa[0]) / answer + 1 - n)
}

package main

import "fmt"
import "math/rand"
import "sort"
import "bufio"
import "os"

func f(a, b int) int {
	var c int
	c = 1
	for i := 0; a != 0; i++ {
	    c = b
	    b = a
	    var temp int = c % b
	    a = temp
	    c = b
	}

	return c
}

func main() {
	var n int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n)
	aa := make([]int, n, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &aa[i])
	}

	for i := 0; i < n; i++ {
		j := rand.Intn(i+1)
		aa[i], aa[j] = aa[j], aa[i]
	}

	sort.Ints(aa)
	bb := make([]int, n - 1, n - 1)

	for i := 0; i < n - 1; i++ {
	    bb[i] = aa[i + 1] - aa[i]
	}

    var answer int
    answer = f(bb[1], bb[0])

    for i := 2; i < n - 1; i++ {
        answer = f(answer, bb[i]);
    }
    fmt.Println((aa[n-1] - aa[0]) / answer + 1 - n)
}

package main

import "fmt"
import "sort"
import "bufio"
import "os"
func gcd(x int, y int) int {
	if y==0 {
	    return x
	}
	return gcd(y,x%y)
}
func main() {
    in := bufio.NewReader(os.Stdin)
	var n,dif int
	fmt.Fscan(in, &n)
	x := make([]int, n, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &x[i])
	}
	sort.Ints(x)
	dif=x[1]-x[0]
	for i := 2; i < n; i++ {
		dif=gcd(dif,x[i]-x[i-1])
	}
	fmt.Println((x[n-1]-x[0])/dif-n+1)
}

package main

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

func gcd(x, y int) int {
 for y != 0 {
  x, y = y, x%y
 }
 return x
}

func solve() {
	n := readInt()
	a := readIntArray(n)
	res := make([]int, n-1)
	// sort.Sort(int(a))
     sort.Ints(a)

	for i:= 1; i < n;i++ {
		res[i-1] = (a[i] - a[i-1])
	}
	ans := gcd(res[0], res[1])

	for i:= 2; i < n-1;i++ {
		ans = gcd(ans,res[i])
	}

	aa := int64(0)

	for i:= 0;i < n-1;i++ {
		aa = aa + int64(res[i]/ans - 1)
	}

	fmt.Print(aa)
}

type Int64 []int64

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

const INF = math.MaxInt32

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

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

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

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

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

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

package main

import "fmt"
import "bufio"
import "os"
import "math/rand"
import "sort"

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

func main() {
	var n int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n)
	a := make([]int, n, n)
	mv, xv := 2147483647, -2147483648
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
		if a[i] < mv {
			mv = a[i]
		}
		if a[i] > xv {
			xv = a[i]
		}
	}
	for i := 0; i < n; i++ {
		j := rand.Intn(i + 1)
		a[i], a[j] = a[j], a[i]
	}
	sort.Ints(a)
	difs := 42
	for i := 1; i < n; i++ {
		difs = gcd(difs, a[i] - a[i-1])
	}
	kur := (a[n-1] - a[0]) / difs
	fmt.Println(kur - n + 1)
}

package main

import "fmt"
import "sort"

func gcd(a int, b int) int {
	if a < b {
		return gcd(b, a)
	}
	for b != 0 {
		t := a % b
		a = b
		b = t
	}
	return a
}
func main() {
  var n int
  fmt.Scanln(&n)
  x := make([]int, n)
  for i := 0; i < n; i++ {
      fmt.Scanf("%d\n", &x[i])
  }
  sort.Ints(x)
  var nd int
  nd = 0
  for i := 1; i < n; i++ {
      nd = gcd (x[i] - x[i-1], nd)
  }
  var m int
  m = 0
  for i := 1; i < n; i++ {
      m = m + (x[i] - x[i-1]) / nd - 1
  }
  fmt.Printf("%d\n", m)
}

package main

import "fmt"
import "sort"

func gcd(a int, b int) int {
	if a < b {
		return gcd(b, a)
	}
	for b != 0 {
		t := a % b
		a = b
		b = t
	}
	return a
}
func main() {
  var n int
  fmt.Scanln(&n)
  x := make([]int, n)
  for i := 0; i < n; i++ {
      fmt.Scanf("%d\n", &x[i])
  }
  sort.Ints(x)
  var nd int
  nd = 0
  for i := 1; i < n; i++ {
      nd = gcd (x[i] - x[i-1], nd)
  }
  if nd < 0 {
      nd = -nd
  }
  var m int
  m = 0
  for i := 1; i < n; i++ {
      m = m + (x[i] - x[i-1]) / nd - 1
  }
  fmt.Printf("%d\n", m)
}

package main

import (

	"fmt"

	"math/rand"

)

func quickSort(slice []int) []int {

	length := len(slice)

	if length <= 1 {

		sliceCopy := make([]int, length)

		copy(sliceCopy, slice)

		return sliceCopy

	}

	m := slice[rand.Intn(length)]

	less := make([]int, 0, length)

	middle := make([]int, 0, length)

	more := make([]int, 0, length)

	for _, item := range slice {

		switch {

		case item < m:

			less = append(less, item)

		case item == m:

			middle = append(middle, item)

		case item > m:

			more = append(more, item)

		}

	}

	less, more = quickSort(less), quickSort(more)

	less = append(less, middle...)

	less = append(less, more...)

	return less

}

func gcd(a, b int) int {

	if b == 0 {

		return a

	}

	return gcd(b, a%b)

}

func main() {

	var n int

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

	if n <= 2 {

		fmt.Println(0)

		return

	}

	a := make([]int, n)

	for i := range a {

		fmt.Scanf("%d", &a[i])

	}

	a = quickSort(a)

	g := a[1] - a[0]

	for i := 2; i < n; i++ {

		g = gcd(g, a[i]-a[i-1])

	}

	var ans int

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

		ans = ans + ((a[i] - a[i-1])/g) - 1

	}

	fmt.Println(ans)

}


package main

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

var n int32;
var Words[1001] string;
func ReadData() {
  fmt.Scanf("%d", &n);
  reader := bufio.NewScanner(os.Stdin)
  reader.Scan()

  var i int32
  i = 1
  for i <= n {
    reader.Scan()
    Words[i] = reader.Text();

    // fmt.Println(Words[i])
    i++;
  }
}

func Solve() {
  var Ten[10] int64;
  var i int32

  Zero := [10]bool {false, false, false, false, false, false, false, false, false, false};
  Ten[0] = 1
  i = 1;
  for i < 10 {
    Ten[i] = Ten[i - 1] * 10
    i++
  }

  var Coef[10] int64;
  i = 1
  for i <= n {
    c := Words[i][0] - 97
    Zero[c] = true

    for j := 0; j < len(Words[i]); j++ {
      c := Words[i][j] - 97
      Coef[c] += Ten[len(Words[i]) - 1 - j]
    }
    i++;
  }
  var Indices[10] int;
  for i := 0; i < 10; i++ {
    Indices[i] = i;
  }

  for i := 0; i < 10; i++ {
    for j := i + 1; j < 10; j++ {
       if Coef[Indices[i]] < Coef[Indices[j]] {
	 tmp := Indices[i];
         Indices[i] = Indices[j]
	 Indices[j] = tmp
       }
    }
  }

  // for i := 0; i < 10; i++ {
  //   fmt.Println("i:", i, " Ch:", Indices[i], " Coef:", Coef[Indices[i]])
  // }
  var sum int64 = 0
  var current int32 = 1
  var used0 bool = false

  for i := 0; i < 10; i++ {
    var d int = Indices[i]
    if !used0 && !Zero[d] {
      used0 = true
    } else {
      sum += (int64(current) * Coef[d])
      current += 1
    }
  }
  fmt.Println(sum)
}

func main() {
  ReadData();
  Solve()
}

package main

import (
	"fmt"
	"math"
	"sort"
)

type Pair struct {
	K rune
	V int64
}
type Pairs []Pair

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

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

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

func main() {
	var t int
	leading := make([]bool, 10)
	tab := make([]Pair, 10)
	// fmt.Println(leading)
	// fmt.Println("debut")
	// for ind := 0; ind < len(tab); ind++ {
	// 	fmt.Println(tab[ind])
	// }
	fmt.Scanln(&t)

	for ind := 0; ind < t; ind++ {
		var s string
		fmt.Scanln(&s)
		leading[int(s[0]-'a')] = true
		for k, v := range s {
			c := (len(s) - k - 1)
			tab[int(v-'a')].V += int64(math.Pow10(c))
			tab[int(v-'a')].K = v
		}
	}
	//fmt.Println("arr")
	sort.Sort(Pairs(tab))
	//fmt.Println(leading)
	//fmt.Println(tab)
	//***************
	var lead = false
	var sum_min, count int64
	count, sum_min = 1, 0
	for ind := len(tab) - 1; ind >= 0; ind-- {
		//fmt.Println(int(tab[ind].K - 'a'))
		if tab[ind].K == 0 {
			continue
		}
		if lead == false && leading[int(tab[ind].K-'a')] == false {
			lead = true
			continue
		}
		sum_min += count * tab[ind].V
		count += 1
	}
	fmt.Println(sum_min)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func perm910C(arr []int, i int, f func([]int)) {
	if i == len(arr) {
		f(arr)
		return
	}
	perm910C(arr, i+1, f)
	for j := i + 1; j < len(arr); j++ {
		arr[i], arr[j] = arr[j], arr[i]
		perm910C(arr, i+1, f)
		arr[i], arr[j] = arr[j], arr[i]
	}
}

func Sol910C(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	pow10 := [6]int{1, 1e1, 1e2, 1e3, 1e4, 1e5}

	var n int
	var coefs [10]int
	var mustNotZero [10]bool
	for Fscan(in, &n); n > 0; n-- {
		var s string
		Fscan(in, &s)
		mustNotZero[s[0]-'a'] = true
		for i, c := range s {
			coefs[c-'a'] += pow10[len(s)-1-i]
		}
	}

	minVal := 1 << 30
	perm910C([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, 0, func(digits []int) {
		val := 0
		for i, d := range digits {
			if d == 0 {
				if mustNotZero[i] {
					return
				}
				continue
			}
			val += coefs[i] * d
		}
		if val < minVal {
			minVal = val
		}
	})
	Fprintln(out, minVal)
}

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


package main

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

func Init() {
}

func Solve(io *FastIO) {
	N := io.NextInt()
	K := io.NextLong()
	C := io.NextLongArray(N)

	ans := GetHigh(C, K) - GetLow(C, K)
	if ans > 0 {
	  io.Println(ans)
	  return
	}

	sum := int64(0)
	for _, v := range C {
	  sum += v
	}
	if sum % int64(N) == 0 {
	  io.Println(0)
	} else {
	  io.Println(1)
	}
}

func GetHigh(C []int64, K int64) int64 {
  L := int64(0)
  R := int64(1000000000)
  good := R
  for L <= R {
    M := (L + R) / 2
    sum := int64(0)
    for _, v := range C {
      if v > M {
        sum += v - M
      }
    }
    if sum <= K {
      R = M - 1
      good = M
    } else {
      L = M + 1
    }
  }
  return good
}

func GetLow(C []int64, K int64) int64 {
  L := int64(0)
  R := int64(1000000000)
  good := L
  for L <= R {
    M := (L + R) / 2
    sum := int64(0)
    for _, v := range C {
      if v < M {
        sum += M - v
      }
    }
    if sum <= K {
      L = M + 1
      good = M
    } else {
      R = M - 1
    }
  }
  return good
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

type com struct {
	cash int64
	ppl  int
}

func (c com) diff(avg int64) int64 {
	x := int64(c.ppl) * avg
	if x > c.cash {
		return x - c.cash
	} else {
		return c.cash - x
	}
}
func (c com) avg() (int, int) {
	p := int64(c.ppl)
	eq := c.cash%p == 0
	x := int(c.cash / p)
	if eq {
		return x, x
	}
	return x, x + 1
}

func main() {
	n, k := getInt(), getInt()
	v := getNint(n)
	sort.Ints(v)

	p := make([]com, n) // unnecessary mem, but prob. is simpler with vector
	for i, c := range v {
		p[i].ppl = 1
		p[i].cash = int64(c)
	}
	for len(p) > 2 && k > 0 {
		l := &p[0]
		l2 := &p[1] // maybe == r2
		r2 := &p[len(p)-2]
		r := &p[len(p)-1]

		tr := r.diff(r2.cash)
		tl := l.diff(l2.cash)
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)

		if l.diff(l2.cash) == 0 {
			l2.cash += l.cash
			l2.ppl += l.ppl
			l.ppl = 0
		} else if r.diff(r2.cash) == 0 {
			r2.cash += r.cash
			r2.ppl += r.ppl
			r.ppl = 0
		}
		if l.ppl == 0 {
			p = p[1:]
			t = 1
		} else if r.ppl == 0 {
			p = p[:len(p)-1]
			t = 1
		}
		if t == 0 {
			break
		}
	}

	if k > 0 && len(p) == 2 {
		l := &p[0]
		r := &p[1]
		x := int((int64(l.ppl)*r.cash - l.cash*int64(r.ppl)) / int64(r.ppl+l.ppl))
		if x > k {
			x = k
		} else if x < 0 {
			x = 0
		}
		l.cash += int64(x)
		r.cash -= int64(x)
		k -= x
	}

	l := p[0]
	r := p[len(p)-1]
	pst, _ := l.avg()
	_, rst := r.avg()
	fmt.Println(rst - pst)
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}

package main

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

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

type com struct {
	cash int64
	ppl  int
}

func (c com) diff(avg int64) int64 {
	x := int64(c.ppl) * avg
	if x > c.cash {
		return x - c.cash
	} else {
		return c.cash - x
	}
}
func (c com) avg() (int, int) {
	p := int64(c.ppl)
	eq := c.cash%p == 0
	x := int(c.cash / p)
	if eq {
		return x, x
	}
	return x, x + 1
}

func main() {
	n, k := getInt(), getInt()
	v := getNint(n)
	sort.Ints(v)

	p := make([]com, n) // unnecessary mem, but prob. is simpler with vector
	for i, c := range v {
		p[i].ppl = 1
		p[i].cash = int64(c)
	}
	for len(p) > 2 && k > 0 {
		l := &p[0]
		l2 := &p[1] // maybe == r2
		r2 := &p[len(p)-2]
		r := &p[len(p)-1]

		tr := r.diff(r2.cash)
		tl := l.diff(l2.cash)
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)

		if l.diff(l2.cash) == 0 {
			l2.cash += l.cash
			l2.ppl += l.ppl
			l.ppl = 0
		} else if r.diff(r2.cash) == 0 {
			r2.cash += r.cash
			r2.ppl += r.ppl
			r.ppl = 0
		}
		if l.ppl == 0 {
			p = p[1:]
			t = 1
		} else if r.ppl == 0 {
			p = p[:len(p)-1]
			t = 1
		}
		if t == 0 {
			break
		}
	}

	if k > 0 && len(p) == 2 {
		l := &p[0]
		r := &p[1]
		x := int((int64(l.ppl)*r.cash - l.cash*int64(r.ppl)) / int64(r.ppl+l.ppl))
		if x > k {
			x = k
		} else if x < 0 {
			x = 0
		}
		l.cash += int64(x)
		r.cash -= int64(x)
		k -= x
	}

	l := p[0]
	r := p[len(p)-1]
	pst, pst1 := l.avg()
	rst, rst1 := r.avg()
	var ans []int
	ans = append(ans, pst, pst1, rst, rst1)
	sort.Ints(ans)
	fmt.Println(ans[3] - ans[0])
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}

package main

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

var in = bufio.NewScanner(os.Stdin)

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

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

	n := readInt()
	k := int64(readInt())

	c := make([]int, n)
	sum := 0
	for i := range c {
		c[i] = readInt()
		sum = (sum + c[i]) % n
	}
	sort.Ints(c)

	rich := c[n-1]
	passed := int64(0)
	for i := n - 1; i > 0; i-- {
		if c[i] == c[i-1] {
			continue
		}
		to := int64(n-i) * int64(rich-c[i-1])
		if to+passed >= k {
			rich -= int((k - passed) / int64(n-i))
			break
		}
		passed += to
		rich = c[i-1]
	}

	poor := c[0]
	passed = 0
	for i := 0; i < n-1; i++ {
		if c[i] == c[i+1] {
			continue
		}
		to := int64(i+1) * int64(c[i+1]-poor)
		if to+passed >= k {
			poor += int((k - passed) / int64(i+1))
			break
		}
		passed += to
		poor = c[i+1]
	}

	if rich <= poor {
		if sum == 0 {
			fmt.Println(0)
		} else {
			fmt.Println(1)
		}
	} else {
		fmt.Println(rich - poor)
	}
}

package main

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

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

type com struct {
	cash int64
	ppl  int
}

func (c com) diff(avg int64) int64 {
	x := int64(c.ppl) * avg
	if x > c.cash {
		return x - c.cash
	} else {
		return c.cash - x
	}
}
func (c com) avg() (int, int) {
	p := int64(c.ppl)
	eq := c.cash%p == 0
	x := int(c.cash / p)
	if eq {
		return x, x
	}
	return x, x + 1
}

func main() {
	n, k := getInt(), getInt()
	v := getNint(n)
	sort.Ints(v)

	p := make([]com, n) // unnecessary mem, but prob. is simpler with vector
	for i, c := range v {
		p[i].ppl = 1
		p[i].cash = int64(c)
	}
	for len(p) > 2 && k > 0 {
		l := &p[0]
		l2 := &p[1] // maybe == r2
		r2 := &p[len(p)-2]
		r := &p[len(p)-1]

		tr := r.diff(r2.cash)
		tl := l.diff(l2.cash)
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)

		if l.diff(l2.cash) == 0 {
			l2.cash += l.cash
			l2.ppl += l.ppl
			l.ppl = 0
		}
		if r.diff(r2.cash) == 0 {
			r2.cash += r.cash
			r2.ppl += r.ppl
			r.ppl = 0
		}
		if l.ppl == 0 {
			p = p[1:]
			t = 1
		}
		if r.ppl == 0 {
			p = p[:len(p)-1]
			t = 1
		}
		if t == 0 {
			break
		}
	}

	if k > 0 && len(p) == 2 {
		var sum int64
		for i := 0; i < n; i++ {
			sum += int64(v[i])
		}
		avg := sum / int64(n)

		l := &p[0]
		r := &p[1]

		tr := r.diff(avg)
		tl := l.diff(avg)
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)
	}

	l := p[0]
	r := p[len(p)-1]
	pst, _ := l.avg()
	_, rst := r.avg()
	fmt.Println(rst - pst)
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}

package main

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

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

type com struct {
	cash int64
	ppl  int
}

func (c com) diff(avg int64) int64 {
	x := int64(c.ppl) * avg
	if x > c.cash {
		return x - c.cash
	} else {
		return c.cash - x
	}
}
func (c com) avg() (int, int) {
	p := int64(c.ppl)
	eq := c.cash%p == 0
	x := int(c.cash / p)
	if eq {
		return x, x
	}
	return x, x + 1
}

func main() {
	n, k := getInt(), getInt()
	v := getNint(n)
	var sum int64
	for i := 0; i < n; i++ {
		sum += int64(v[i])
	}
	avg := int(sum / int64(n))
	sort.Ints(v)

	p := make([]com, n) // unnecessary mem, but prob. is simpler with vector
	for i, c := range v {
		p[i].ppl = 1
		p[i].cash = int64(c)
	}
	for len(p) > 2 && k > 0 {
		l := &p[0]
		l2 := &p[1] // maybe == r2
		r2 := &p[len(p)-2]
		r := &p[len(p)-1]

		tr := r.diff(max64(r2.cash, int64(avg)))
		tl := l.diff(min64(l2.cash, int64(avg)))
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)

		if l.diff(l2.cash) == 0 {
			l2.cash += l.cash
			l2.ppl += l.ppl
			l.ppl = 0
		}
		if r.diff(r2.cash) == 0 {
			r2.cash += r.cash
			r2.ppl += r.ppl
			r.ppl = 0
		}
		if l.ppl == 0 {
			p = p[1:]
			t = 1
		}
		if r.ppl == 0 {
			p = p[:len(p)-1]
			t = 1
		}
		if t == 0 {
			break
		}
	}
	if k > 0 && len(p) == 2 {
		l := &p[0]
		r := &p[1]

		tr := r.diff(max64(l.cash, int64(avg)))
		tl := l.diff(min64(r.cash, int64(avg)))
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)
	}

	l := p[0]
	r := p[len(p)-1]
	pst, _ := l.avg()
	_, rst := r.avg()
	fmt.Println(rst - pst)
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}

package main

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

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

type com struct {
	cash int64
	ppl  int
}

func (c com) diff(avg int64) int64 {
	x := int64(c.ppl) * avg
	if x > c.cash {
		return x - c.cash
	} else {
		return c.cash - x
	}
}
func (c com) avg() (int, int) {
	p := int64(c.ppl)
	eq := c.cash%p == 0
	x := int(c.cash / p)
	if eq {
		return x, x
	}
	return x, x + 1
}

func main() {
	n, k := getInt(), getInt()
	v := getNint(n)
	// var sum int64
	// for i := 0; i < n; i++ {
	// 	sum += int64(v[i])
	// }
	// avg := int(sum / int64(n))
        // fmt.Println("avg",avg)
	sort.Ints(v)

	p := make([]com, n) // unnecessary mem, but prob. is simpler with vector
	for i, c := range v {
		p[i].ppl = 1
		p[i].cash = int64(c)
	}
	for len(p) > 2 && k > 0 {
		fmt.Printf("%+v\n", p)
		l := &p[0]
		l2 := &p[1] // maybe == r2
		r2 := &p[len(p)-2]
		r := &p[len(p)-1]

		// tr := r.diff(max64(r2.cash, int64(avg)))
		// tl := l.diff(min64(l2.cash, int64(avg)))
		tr := r.diff( r2.cash )
		tl := l.diff( l2.cash )
		t := min64(min64(tr, tl), int64(k))
		fmt.Println(k, tl, tr)

		l.cash += t
		r.cash -= t
		k -= int(t)

		if l.diff(l2.cash) == 0 {
			l2.cash += l.cash
			l2.ppl += l.ppl
			l.ppl = 0
		}
		if r.diff(r2.cash) == 0 {
			r2.cash += r.cash
			r2.ppl += r.ppl
			r.ppl = 0
		}
		if l.ppl == 0 {
			p = p[1:]
			t = 1
		}
		if r.ppl == 0 {
			p = p[:len(p)-1]
			t = 1
		}
		if t == 0 {
                        fmt.Println("break")
			break
		}
	}
	fmt.Printf("%+v (last),k=%v\n", p,k)
	if k > 0 && len(p) == 2 {
		l := &p[0]
		r := &p[1]

		// tr := r.diff(max64(l.cash, int64(avg)))
		// tl := l.diff(min64(r.cash, int64(avg)))
		tr := r.diff( l.cash /int64(l.ppl))
		tl := l.diff( r.cash /int64(r.ppl))
		t := min64(min64(tr, tl), int64(k))
                	fmt.Println(k, tl, tr)

		l.cash += t
		r.cash -= t
		k -= int(t)
	}

	l := p[0]
	r := p[len(p)-1]
	pst, t1 := l.avg()
	t2, rst := r.avg()
	fmt.Println(rst - pst)
        fmt.Println("dbg",pst, t1,t2,rst)
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}

package main

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

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

type com struct {
	cash int64
	ppl  int
}

func (c com) diff(avg int64) int64 {
	x := int64(c.ppl) * avg
	if x > c.cash {
		return x - c.cash
	} else {
		return c.cash - x
	}
}
func (c com) avg() (int, int) {
	p := int64(c.ppl)
	eq := c.cash%p == 0
	x := int(c.cash / p)
	if eq {
		return x, x
	}
	return x, x + 1
}

func main() {
	n, k := getInt(), getInt()
	v := getNint(n)
	var sum int64
	for i := 0; i < n; i++ {
		sum += int64(v[i])
	}
	avg := int(sum / int64(n))
	avgU := avg
	if sum%int64(n) != 0 {
		avgU++
	}
	sort.Ints(v)

	p := make([]com, n) // unnecessary mem, but prob. is simpler with vector
	for i, c := range v {
		p[i].ppl = 1
		p[i].cash = int64(c)
	}
	for len(p) > 2 && k > 0 {
		l := &p[0]
		l2 := &p[1] // maybe == r2
		r2 := &p[len(p)-2]
		r := &p[len(p)-1]

		tr := r.diff(max64(r2.cash, int64(avgU)))
		tl := l.diff(min64(l2.cash, int64(avg)))
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)

		if l.diff(l2.cash) == 0 {
			l2.cash += l.cash
			l2.ppl += l.ppl
			l.ppl = 0
		}
		if r.diff(r2.cash) == 0 {
			r2.cash += r.cash
			r2.ppl += r.ppl
			r.ppl = 0
		}
		if l.ppl == 0 {
			p = p[1:]
			t = 1
		}
		if r.ppl == 0 {
			p = p[:len(p)-1]
			t = 1
		}
		if t == 0 {
			break
		}
	}
	if k > 0 && len(p) == 2 {
		l := &p[0]
		r := &p[1]

		tr := r.diff(max64(l.cash, int64(avgU)))
		tl := l.diff(min64(r.cash, int64(avg)))
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)
	}

	l := p[0]
	r := p[len(p)-1]
	pst, _ := l.avg()
	_, rst := r.avg()
	fmt.Println(rst - pst)
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}

package main

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

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

type com struct {
	cash int64
	ppl  int
}

func (c com) diff(avg int64) int64 {
	x := int64(c.ppl) * avg
	if x > c.cash {
		return x - c.cash
	} else {
		return c.cash - x
	}
}
func (c com) avg() (int, int) {
	p := int64(c.ppl)
	eq := c.cash%p == 0
	x := int(c.cash / p)
	if eq {
		return x, x
	}
	return x, x + 1
}

func main() {
	n, k := getInt(), getInt()
	v := getNint(n)
	var sum int64
	for i := 0; i < n; i++ {
		sum += int64(v[i])
	}
	avg := int(sum / int64(n))
	avgU := avg
	if sum%int64(n) != 0 {
		avgU++
	}
	sort.Ints(v)

	p := make([]com, n) // unnecessary mem, but prob. is simpler with vector
	for i, c := range v {
		p[i].ppl = 1
		p[i].cash = int64(c)
	}
	for len(p) > 2 && k > 0 {
		l := &p[0]
		l2 := &p[1] // maybe == r2
		r2 := &p[len(p)-2]
		r := &p[len(p)-1]

		tr := r.diff(max64(r2.cash, int64(avg)))
		tl := l.diff(min64(l2.cash, int64(avgU)))
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)

		if l.diff(l2.cash) == 0 {
			l2.cash += l.cash
			l2.ppl += l.ppl
			l.ppl = 0
		}
		if r.diff(r2.cash) == 0 {
			r2.cash += r.cash
			r2.ppl += r.ppl
			r.ppl = 0
		}
		if l.ppl == 0 {
			p = p[1:]
			t = 1
		}
		if r.ppl == 0 {
			p = p[:len(p)-1]
			t = 1
		}
		if t == 0 {
			break
		}
	}
	if k > 0 && len(p) == 2 {
		l := &p[0]
		r := &p[1]

		tr := r.diff(max64(l.cash, int64(avg)))
		tl := l.diff(min64(r.cash, int64(avgU)))
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)
	}

	l := p[0]
	r := p[len(p)-1]
	pst, _ := l.avg()
	_, rst := r.avg()
	fmt.Println(rst - pst)
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}

package main

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

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

type com struct {
	cash int64
	ppl  int
}

func (c com) diff(avg int64) int64 {
	x := int64(c.ppl) * avg
	if x > c.cash {
		return x - c.cash
	} else {
		return c.cash - x
	}
}
func (c com) avg() (int, int) {
	p := int64(c.ppl)
	eq := c.cash%p == 0
	x := int(c.cash / p)
	if eq {
		return x, x
	}
	return x, x + 1
}

func main() {
	n, k := getInt(), getInt()
	v := getNint(n)
	var sum int64
	for i := 0; i < n; i++ {
		sum += int64(v[i])
	}
	avg := int(sum / int64(n))
	sort.Ints(v)

	p := make([]com, n) // unnecessary mem, but prob. is simpler with vector
	for i, c := range v {
		p[i].ppl = 1
		p[i].cash = int64(c)
	}
	for len(p) > 2 && k > 0 {
		l := &p[0]
		l2 := &p[1] // maybe == r2
		r2 := &p[len(p)-2]
		r := &p[len(p)-1]

		tr := r.diff(r2.cash)
		tl := l.diff(l2.cash)
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)

		if l.diff(l2.cash) == 0 {
			l2.cash += l.cash
			l2.ppl += l.ppl
			l.ppl = 0
		}
		if r.diff(r2.cash) == 0 {
			r2.cash += r.cash
			r2.ppl += r.ppl
			r.ppl = 0
		}
		if l.ppl == 0 {
			p = p[1:]
			t = 1
		}
		if r.ppl == 0 {
			p = p[:len(p)-1]
			t = 1
		}
		if t == 0 {
			break
		}
	}
	if k > 0 && len(p) == 2 {
		l := &p[0]
		r := &p[1]

		tr := r.diff(max64(l.cash, int64(avg)))
		tl := l.diff(min64(r.cash, int64(avg)))
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)
	}

	l := p[0]
	r := p[len(p)-1]
	pst, _ := l.avg()
	_, rst := r.avg()
	fmt.Println(rst - pst)
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}

package main

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

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

type com struct {
	cash int64
	ppl  int
}

func (c com) diff(avg int64) int64 {
	x := int64(c.ppl) * avg
	if x > c.cash {
		return x - c.cash
	} else {
		return c.cash - x
	}
}
func (c com) avg() (int, int) {
	p := int64(c.ppl)
	eq := c.cash%p == 0
	x := int(c.cash / p)
	if eq {
		return x, x
	}
	return x, x + 1
}

func main() {
	n, k := getInt(), getInt()
	v := getNint(n)
	sort.Ints(v)

	p := make([]com, n) // unnecessary mem, but prob. is simpler with vector
	for i, c := range v {
		p[i].ppl = 1
		p[i].cash = int64(c)
	}
	for len(p) > 2 && k > 0 {
		l := &p[0]
		l2 := &p[1] // maybe == r2
		r2 := &p[len(p)-2]
		r := &p[len(p)-1]

		tr := r.diff(r2.cash)
		tl := l.diff(l2.cash)
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)

		if l.diff(l2.cash) == 0 {
			l2.cash += l.cash
			l2.ppl += l.ppl
			l.ppl = 0
		}
		if r.diff(r2.cash) == 0 {
			r2.cash += r.cash
			r2.ppl += r.ppl
			r.ppl = 0
		}
		if l.ppl == 0 {
			p = p[1:]
			t = 1
		}
		if r.ppl == 0 {
			p = p[:len(p)-1]
			t = 1
		}
		if t == 0 {
			break
		}
	}
	if k > 0 && len(p) == 2 {
		l := &p[0]
		r := &p[1]

		tr := r.diff(l.cash)
		tl := l.diff(r.cash)
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)
	}

	l := p[0]
	r := p[len(p)-1]
	pst, _ := l.avg()
	_, rst := r.avg()
	fmt.Println(rst - pst)
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}

package main

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

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

type com struct {
	cash int64
	ppl  int
}

func (c com) diff(avg int64) int64 {
	x := int64(c.ppl) * avg
	if x > c.cash {
		return x - c.cash
	} else {
		return c.cash - x
	}
}
func (c com) avg() (int, int) {
	p := int64(c.ppl)
	eq := c.cash%p == 0
	x := int(c.cash / p)
	if eq {
		return x, x
	}
	return x, x + 1
}

func main() {
	n, k := getInt(), getInt()
	v := getNint(n)
	sort.Ints(v)

	p := make([]com, n) // unnecessary mem, but prob. is simpler with vector
	for i, c := range v {
		p[i].ppl = 1
		p[i].cash = int64(c)
	}
	for len(p) > 2 && k > 0 {
		l := &p[0]
		l2 := &p[1] // maybe == r2
		r2 := &p[len(p)-2]
		r := &p[len(p)-1]

		tr := r.diff(r2.cash)
		tl := l.diff(l2.cash)
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)

		if l.diff(l2.cash) == 0 {
			l2.cash += l.cash
			l2.ppl += l.ppl
			l.ppl = 0
		}
		if r.diff(r2.cash) == 0 {
			r2.cash += r.cash
			r2.ppl += r.ppl
			r.ppl = 0
		}
		if l.ppl == 0 {
			p = p[1:]
			t = 1
		}
		if r.ppl == 0 {
			p = p[:len(p)-1]
			t = 1
		}
		if t == 0 {
			break
		}
	}

	if k > 0 && len(p) == 2 {
		var sum int64
		for i := 0; i < n; i++ {
			sum += int64(v[i])
		}
		avg := sum / int64(n)
		upd := int64(0)
		if sum%int64(n) != 0 {
			upd = 1
		}

		l := &p[0]
		r := &p[1]

		tr := r.diff(avg) - upd
		tl := l.diff(avg)
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)
	}

	l := p[0]
	r := p[len(p)-1]
	pst, _ := l.avg()
	_, rst := r.avg()
	fmt.Println(rst - pst)
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}

package main

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

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

type com struct {
	cash int64
	ppl  int
}

func (c com) diff(avg int64) int64 {
	x := int64(c.ppl) * avg
	if x > c.cash {
		return x - c.cash
	} else {
		return c.cash - x
	}
}
func (c com) avg() (int, int) {
	p := int64(c.ppl)
	eq := c.cash%p == 0
	x := int(c.cash / p)
	if eq {
		return x, x
	}
	return x, x + 1
}

func main() {
	n, k := getInt(), getInt()
	v := getNint(n)
	sort.Ints(v)

	p := make([]com, n) // unnecessary mem, but prob. is simpler with vector
	for i, c := range v {
		p[i].ppl = 1
		p[i].cash = int64(c)
	}
	for len(p) > 2 && k > 0 {
		l := &p[0]
		l2 := &p[1] // maybe == r2
		r2 := &p[len(p)-2]
		r := &p[len(p)-1]

		tr := r.diff(r2.cash)
		tl := l.diff(l2.cash)
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)

		if l.diff(l2.cash) == 0 {
			l2.cash += l.cash
			l2.ppl += l.ppl
			l.ppl = 0
		}
		if r.diff(r2.cash) == 0 {
			r2.cash += r.cash
			r2.ppl += r.ppl
			r.ppl = 0
		}
		if l.ppl == 0 {
			p = p[1:]
			t = 1
		}
		if r.ppl == 0 {
			p = p[:len(p)-1]
			t = 1
		}
		if t == 0 {
			break
		}
	}
	if k > 0 && len(p) == 2 {
		var sum int64
		for i := 0; i < n; i++ {
			sum += int64(v[i])
		}
		avg := sum / int64(n)
		avgu := avg
		if sum%int64(n) != 0 {
			avgu++
		}

		l := &p[0]
		r := &p[1]

		tr := r.diff(avgu)
		tl := l.diff(avg)
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)
	}

	l := p[0]
	r := p[len(p)-1]
	pst, _ := l.avg()
	_, rst := r.avg()
	fmt.Println(rst - pst)
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}

package main

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

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

type com struct {
	cash int64
	ppl  int
}

func (c com) diff(avg int64) int64 {
	x := int64(c.ppl) * avg
	if x > c.cash {
		return x - c.cash
	} else {
		return c.cash - x
	}
}
func (c com) avg() (int, int) {
	p := int64(c.ppl)
	eq := c.cash%p == 0
	x := int(c.cash / p)
	if eq {
		return x, x
	}
	return x, x + 1
}

func main() {
	n, k := getInt(), getInt()
	v := getNint(n)
	sort.Ints(v)

	p := make([]com, n) // unnecessary mem, but prob. is simpler with vector
	for i, c := range v {
		p[i].ppl = 1
		p[i].cash = int64(c)
	}
	for len(p) > 2 && k > 0 {
		l := &p[0]
		l2 := &p[1] // maybe == r2
		r2 := &p[len(p)-2]
		r := &p[len(p)-1]

		// tr := r.diff(max64(r2.cash, int64(avg)))
		// tl := l.diff(min64(l2.cash, int64(avg)))
		tr := r.diff(r2.cash)
		tl := l.diff(l2.cash)
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)

		if l.diff(l2.cash) == 0 {
			l2.cash += l.cash
			l2.ppl += l.ppl
			l.ppl = 0
		}
		if r.diff(r2.cash) == 0 {
			r2.cash += r.cash
			r2.ppl += r.ppl
			r.ppl = 0
		}
		if l.ppl == 0 {
			p = p[1:]
			t = 1
		}
		if r.ppl == 0 {
			p = p[:len(p)-1]
			t = 1
		}
		if t == 0 {
			break
		}
	}

	if k > 0 && len(p) == 2 {
		l := &p[0]
		r := &p[1]

		tr := r.diff(l.cash / int64(l.ppl))
		tl := l.diff(r.cash / int64(r.ppl))
		t := min64(min64(tr, tl), int64(k))

		l.cash += t
		r.cash -= t
		k -= int(t)
	}

	l := p[0]
	r := p[len(p)-1]
	pst, _ := l.avg()
	_, rst := r.avg()
	fmt.Println(rst - pst)
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}

package main

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

var in = bufio.NewScanner(os.Stdin)

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

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

	n := readInt()
	k := readInt()

	c := make([]int, n)
	sum := 0
	for i := range c {
		c[i] = readInt()
		sum = (sum + c[i]) % n
	}
	sort.Ints(c)

	rich := c[n-1]
	passed := 0
	for i := n - 1; i > 0; i-- {
		if c[i] == c[i-1] {
			continue
		}
		to := (n - i) * (rich - c[i-1])
		if to+passed >= k {
			rich -= (k - passed) / (n - i)
			break
		}
		passed += to
		rich = c[i-1]
	}

	poor := c[0]
	passed = 0
	for i := 0; i < n-1; i++ {
		if c[i] == c[i+1] {
			continue
		}
		to := (i + 1) * (c[i+1] - poor)
		if to+passed >= k {
			poor += (k - passed) / (i + 1)
			break
		}
		passed += to
		poor = c[i+1]
	}

	if rich <= poor {
		if sum == 0 {
			fmt.Println(0)
		} else {
			fmt.Println(1)
		}
	} else {
		fmt.Println(rich - poor)
	}
}

package main

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

func Init() {
}

func Solve(io *FastIO) {
	N := io.NextInt()
	K := io.NextLong()
	C := io.NextLongArray(N)

	ans := GetHigh(C, K) - GetLow(C, K)
	if ans >= 0 {
	  io.Println(ans)
	} else {
	  io.Println(0)
	}
}

func GetHigh(C []int64, K int64) int64 {
  L := int64(0)
  R := int64(1000000000000000)
  good := R
  for L <= R {
    M := (L + R) / 2
    sum := int64(0)
    for _, v := range C {
      if v > M {
        sum += v - M
      }
    }
    if sum <= K {
      R = M - 1
      good = M
    } else {
      L = M + 1
    }
  }
  return good
}

func GetLow(C []int64, K int64) int64 {
  L := int64(0)
  R := int64(1000000000000000)
  good := L
  for L <= R {
    M := (L + R) / 2
    sum := int64(0)
    for _, v := range C {
      if v < M {
        sum += M - v
      }
    }
    if sum <= K {
      L = M + 1
      good = M
    } else {
      R = M - 1
    }
  }
  return good
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

func Init() {
}

func Solve(io *FastIO) {
	N := io.NextInt()
	K := io.NextLong()
	C := io.NextLongArray(N)

	ans := GetHigh(C, K) - GetLow(C, K)
	if ans > 0 {
	  io.Println(ans)
	  return
	}

	sum := int64(0)
	for _, v := range C {
	  sum += v
	}
	if sum % int64(N) == 0 {
	  io.Println(0)
	} else {
	  io.Println(1)
	}
}

func GetHigh(C []int64, K int64) int64 {
  L := int64(0)
  R := int64(1000000000000000)
  good := R
  for L <= R {
    M := (L + R) / 2
    sum := int64(0)
    for _, v := range C {
      if v > M {
        sum += v - M
      }
    }
    if sum <= K {
      R = M - 1
      good = M
    } else {
      L = M + 1
    }
  }
  return good
}

func GetLow(C []int64, K int64) int64 {
  L := int64(0)
  R := int64(1000000000000000)
  good := L
  for L <= R {
    M := (L + R) / 2
    sum := int64(0)
    for _, v := range C {
      if v < M {
        sum += M - v
      }
    }
    if sum <= K {
      L = M + 1
      good = M
    } else {
      R = M - 1
    }
  }
  return good
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}


package main

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

func Init() {
}

func Solve(io *FastIO) {
    N := io.NextInt()

    nodes := make([]*Node, N + 1)
    for i := 1; i <= N; i++ {
        nodes[i] = MakeNode()
    }

    for i := 1; i < N; i++ {
        A := io.NextInt()
        B := io.NextInt()

        nodes[A].next[nodes[B]] = true
        nodes[B].next[nodes[A]] = true
    }

    for i := 1; i <= N; i++ {
        V := io.NextLong()

        nodes[i].val = V
    }

    Rootify(nodes[1], nil)

    dec, inc := Calc(nodes[1])
    io.Println(dec + inc)
}

type Node struct {
    next map[*Node]bool
    val int64
    delta int64
    moves int64
}

func MakeNode() *Node {
    return &Node{
        next: make(map[*Node]bool),
    }
}

func (this *Node) ActualValue() int64 {
    return this.val + this.delta
}

func Rootify(u, p *Node) {
    delete(u.next, p)
    for v := range u.next {
        Rootify(v, u)
    }
}

func Calc(u *Node) (int64, int64) {
    var dec, inc int64

    for v := range u.next {
        d, i := Calc(v)

        dec = Max(dec, d)
        inc = Max(inc, i)
    }

    val := u.val + inc - dec
    if val > 0 {
        return dec + val, inc
    } else {
        return dec, inc - val
    }
}

func Max(args ...int64) int64 {
	max := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] > max {
			max = args[i]
		}
	}
	return max
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

func Init() {
}

func Solve(io *FastIO) {
    N := io.NextInt()

    nodes := make([]*Node, N + 1)
    for i := 1; i <= N; i++ {
        nodes[i] = MakeNode()
    }

    for i := 1; i < N; i++ {
        A := io.NextInt()
        B := io.NextInt()

        nodes[A].next[nodes[B]] = true
        nodes[B].next[nodes[A]] = true
    }

    for i := 1; i <= N; i++ {
        V := io.NextLong()

        nodes[i].val = V
    }

    Rootify(nodes[1], nil)

    dec, inc := Calc(nodes[1])
    io.Println(dec + inc)
}

type Node struct {
    next map[*Node]bool
    val int64
}

func MakeNode() *Node {
    return &Node{
        next: make(map[*Node]bool),
    }
}

func Rootify(u, p *Node) {
    delete(u.next, p)
    for v := range u.next {
        Rootify(v, u)
    }
}

func Calc(u *Node) (int64, int64) {
    var dec, inc int64

    for v := range u.next {
        d, i := Calc(v)

        dec = Max(dec, d)
        inc = Max(inc, i)
    }

    val := u.val + inc - dec
    if val > 0 {
        return dec + val, inc
    } else {
        return dec, inc - val
    }
}

func Max(args ...int64) int64 {
	max := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] > max {
			max = args[i]
		}
	}
	return max
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF274B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	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 n, v, w int
	Fscan(in, &n)
	g := make([][]int, n)
	g[0] = append(g[0], -1)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}

	var f func(v, fa int) (mi, mx int64)
	f = func(v, fa int) (mi, mx int64) {
		x := a[v]
		if len(g[v]) == 1 {
			return min(0, x), max(0, x)
		}
		for _, w := range g[v] {
			if w != fa {
				x, y := f(w, v)
				mi = min(mi, x)
				mx = max(mx, y)
			}
		}
		if mi < 0 {
			x -= mi
		}
		if mx > 0 {
			x -= mx
		}
		if x < 0 {
			mi += x
		} else {
			mx += x
		}
		return
	}
	x, y := f(0, -1)
	Fprint(_w, y-x)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF274B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	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 n, v, w int
	Fscan(in, &n)
	g := make([][]int, n)
	g[0] = append(g[0], -1)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}

	var f func(v, fa int) (mi, mx int64)
	f = func(v, fa int) (mi, mx int64) {
		x := a[v]
		if len(g[v]) == 1 {
			return min(0, x), max(0, x)
		}
		for _, w := range g[v] {
			if w != fa {
				x, y := f(w, v)
				mi = min(mi, x)
				mx = max(mx, y)
			}
		}
		x -= mi + mx
		if x < 0 {
			mi += x
		} else {
			mx += x
		}
		return
	}
	x, y := f(0, -1)
	Fprint(_w, y-x)
}

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

package main

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

func Init() {
}

func Solve(io *FastIO) {
    N := io.NextInt()

    nodes := make([]*Node, N + 1)
    for i := 1; i <= N; i++ {
        nodes[i] = MakeNode()
    }

    for i := 1; i < N; i++ {
        A := io.NextInt()
        B := io.NextInt()

        nodes[A].next[nodes[B]] = true
        nodes[B].next[nodes[A]] = true
    }

    for i := 1; i <= N; i++ {
        V := io.NextLong()

        nodes[i].val = V
    }

    Rootify(nodes[1], nil)

    Rec(nodes[1])
    io.Println(nodes[1].moves + Abs(nodes[1].ActualValue()))

//     _, _, ans := Calc(nodes[1])
//     io.Println(ans, nodes[1].val)

//     _, s := Calc2(nodes[1])
//     io.Println(s + Abs(nodes[1].val))

    // v, d, s := Calc(nodes[1])
    // io.Println(v, d, s)
}

type Node struct {
    next map[*Node]bool
    val int64
    delta int64
    moves int64
}

func MakeNode() *Node {
    return &Node{
        next: make(map[*Node]bool),
    }
}

func (this *Node) ActualValue() int64 {
    return this.val + this.delta
}

func Rootify(u, p *Node) {
    delete(u.next, p)
    for v := range u.next {
        Rootify(v, u)
    }
}

func Rec(u *Node) {
    // fmt.Println("+", u)

    // if len(u.next) == 0 {
    //     fmt.Println("-", u)
    //     return
    // }

    // lo := int64(math.MaxInt64)
    // hi := int64(math.MinInt64)

    var lo, hi int64

    for v := range u.next {
        Rec(v)

        u.delta += v.delta
        u.moves += v.moves

        lo = Min(lo, v.ActualValue())
        hi = Max(hi, v.ActualValue())
    }

    // fmt.Println(" ", lo, hi)
    u.moves += hi - lo
    u.delta -= hi + lo

    // fmt.Println("-", u)
}

func Calc2(u *Node) (int64, int64) {
    if len(u.next) == 0 {
        return 0, 0
    }

    var delta, sum int64

    lo := int64(math.MaxInt64)
    hi := int64(math.MinInt64)

    for v := range u.next {
        d, s := Calc2(v)

        delta += d
        sum += s

        lo = Min(lo, v.val)
        hi = Max(hi, v.val)
    }

    // fmt.Println(u.val, delta)

    u.val += delta
    return delta - u.val, sum + hi - lo
}

func Calc(u *Node) (int64, int64, int64) {
    if len(u.next) == 0 {
        fmt.Println("DONE", u.val)
        return u.val, -u.val, Abs(u.val)
    }

    var delta, sum int64

    lo := int64(math.MaxInt64)
    hi := int64(math.MinInt64)

    for v := range u.next {
        v, d, s := Calc(v)

        delta += d
        sum += s

        lo = Min(lo, v)
        hi = Max(hi, v)
    }

    fmt.Println("HERE", u.val, u.val + delta - lo - hi, -u.val, sum + Abs(delta + u.val) + hi - lo)
    return u.val + delta - lo - hi, -u.val, sum + Abs(delta + u.val) + hi - lo

    // fmt.Println(u.val, sum, lo, hi)
    // return hi - lo

//     nv := u.val + delta

//     delta -= nv
//     sum += Abs(nv)

//     return delta, sum
}

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

func Min(args ...int64) int64 {
	min := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] < min {
			min = args[i]
		}
	}
	return min
}

func Max(args ...int64) int64 {
	max := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] > max {
			max = args[i]
		}
	}
	return max
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

func Init() {
}

func Solve(io *FastIO) {
    N := io.NextInt()

    nodes := make([]*Node, N + 1)
    for i := 1; i <= N; i++ {
        nodes[i] = MakeNode()
    }

    for i := 1; i < N; i++ {
        A := io.NextInt()
        B := io.NextInt()

        nodes[A].next[nodes[B]] = true
        nodes[B].next[nodes[A]] = true
    }

    for i := 1; i <= N; i++ {
        V := io.NextLong()

        nodes[i].val = V
    }

    Rootify(nodes[1], nil)

    Rec(nodes[1])
    io.Println(nodes[1].moves + Abs(nodes[1].ActualValue()))

//     _, _, ans := Calc(nodes[1])
//     io.Println(ans, nodes[1].val)

//     _, s := Calc2(nodes[1])
//     io.Println(s + Abs(nodes[1].val))

    // v, d, s := Calc(nodes[1])
    // io.Println(v, d, s)
}

type Node struct {
    next map[*Node]bool
    val int64
    delta int64
    moves int64
}

func MakeNode() *Node {
    return &Node{
        next: make(map[*Node]bool),
    }
}

func (this *Node) ActualValue() int64 {
    return this.val + this.delta
}

func Rootify(u, p *Node) {
    delete(u.next, p)
    for v := range u.next {
        Rootify(v, u)
    }
}

func Rec(u *Node) {
    lo := int64(math.MaxInt64)
    hi := int64(math.MinInt64)

    for v := range u.next {
        Rec(v)

        u.delta += v.delta
        u.moves += v.moves

        lo = Min(lo, v.ActualValue())
        hi = Max(hi, v.ActualValue())
    }

    u.moves += hi - lo
    u.delta -= hi + lo
}

func Calc2(u *Node) (int64, int64) {
    if len(u.next) == 0 {
        return 0, 0
    }

    var delta, sum int64

    lo := int64(math.MaxInt64)
    hi := int64(math.MinInt64)

    for v := range u.next {
        d, s := Calc2(v)

        delta += d
        sum += s

        lo = Min(lo, v.val)
        hi = Max(hi, v.val)
    }

    // fmt.Println(u.val, delta)

    u.val += delta
    return delta - u.val, sum + hi - lo
}

func Calc(u *Node) (int64, int64, int64) {
    if len(u.next) == 0 {
        fmt.Println("DONE", u.val)
        return u.val, -u.val, Abs(u.val)
    }

    var delta, sum int64

    lo := int64(math.MaxInt64)
    hi := int64(math.MinInt64)

    for v := range u.next {
        v, d, s := Calc(v)

        delta += d
        sum += s

        lo = Min(lo, v)
        hi = Max(hi, v)
    }

    fmt.Println("HERE", u.val, u.val + delta - lo - hi, -u.val, sum + Abs(delta + u.val) + hi - lo)
    return u.val + delta - lo - hi, -u.val, sum + Abs(delta + u.val) + hi - lo

    // fmt.Println(u.val, sum, lo, hi)
    // return hi - lo

//     nv := u.val + delta

//     delta -= nv
//     sum += Abs(nv)

//     return delta, sum
}

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

func Min(args ...int64) int64 {
	min := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] < min {
			min = args[i]
		}
	}
	return min
}

func Max(args ...int64) int64 {
	max := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] > max {
			max = args[i]
		}
	}
	return max
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

func Init() {
}

func Solve(io *FastIO) {
    N := io.NextInt()

    nodes := make([]*Node, N + 1)
    for i := 1; i <= N; i++ {
        nodes[i] = MakeNode()
    }

    for i := 1; i < N; i++ {
        A := io.NextInt()
        B := io.NextInt()

        nodes[A].next[nodes[B]] = true
        nodes[B].next[nodes[A]] = true
    }

    for i := 1; i <= N; i++ {
        V := io.NextLong()

        nodes[i].val = V
    }

    Rootify(nodes[1], nil)

    _, ans := Calc(nodes[1])
    io.Println(ans)
}

type Node struct {
    next map[*Node]bool
    val int64
}

func MakeNode() *Node {
    return &Node{
        next: make(map[*Node]bool),
    }
}

func Rootify(u, p *Node) {
    delete(u.next, p)
    for v := range u.next {
        Rootify(v, u)
    }
}

func Calc(u *Node) (int64, int64) {
    var delta, sum int64

    for v := range u.next {
        d, s := Calc(v)

        delta += d
        sum += s
    }

    nv := u.val + delta

    delta -= nv
    sum += Abs(nv)

    return delta, sum

//     lo := u.val - sub
//     hi := u.val + add

//     fmt.Printf("add = %d, sub = %d, lo = %d, hi = %d\n", add, sub, lo, hi)

//     if lo > 0 {
//         sum += lo
//         sub += lo
//     }
//     if hi < 0 {
//         sum -= hi
//         add -= hi
//     }

//     fmt.Printf("  new add = %d, sub = %d\n", add, sub)

//     for v := range u.next {
//         sum += Calc(v, add, sub)
//     }

//     return sum
}

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

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

func Init() {
}

func Solve(io *FastIO) {
    N := io.NextInt()

    nodes := make([]*Node, N + 1)
    for i := 1; i <= N; i++ {
        nodes[i] = MakeNode()
    }

    for i := 1; i < N; i++ {
        A := io.NextInt()
        B := io.NextInt()

        nodes[A].next[nodes[B]] = true
        nodes[B].next[nodes[A]] = true
    }

    for i := 1; i <= N; i++ {
        V := io.NextLong()

        nodes[i].val = V
    }

    Rootify(nodes[1], nil)

    Rec(nodes[1])
    io.Println(nodes[1].moves + Abs(nodes[1].ActualValue()))

//     _, _, ans := Calc(nodes[1])
//     io.Println(ans, nodes[1].val)

//     _, s := Calc2(nodes[1])
//     io.Println(s + Abs(nodes[1].val))

    // v, d, s := Calc(nodes[1])
    // io.Println(v, d, s)
}

type Node struct {
    next map[*Node]bool
    val int64
    delta int64
    moves int64
}

func MakeNode() *Node {
    return &Node{
        next: make(map[*Node]bool),
    }
}

func (this *Node) ActualValue() int64 {
    return this.val + this.delta
}

func Rootify(u, p *Node) {
    delete(u.next, p)
    for v := range u.next {
        Rootify(v, u)
    }
}

func Rec(u *Node) {
    // fmt.Println("+", u)

    if len(u.next) == 0 {
        // fmt.Println("-", u)
        return
    }

    lo := int64(math.MaxInt64)
    hi := int64(math.MinInt64)

    for v := range u.next {
        Rec(v)

        u.delta += v.delta
        u.moves += v.moves

        lo = Min(lo, v.ActualValue())
        hi = Max(hi, v.ActualValue())
    }

    u.moves += hi - lo
    u.delta -= hi + lo

    // fmt.Println("-", u)
}

func Calc2(u *Node) (int64, int64) {
    if len(u.next) == 0 {
        return 0, 0
    }

    var delta, sum int64

    lo := int64(math.MaxInt64)
    hi := int64(math.MinInt64)

    for v := range u.next {
        d, s := Calc2(v)

        delta += d
        sum += s

        lo = Min(lo, v.val)
        hi = Max(hi, v.val)
    }

    // fmt.Println(u.val, delta)

    u.val += delta
    return delta - u.val, sum + hi - lo
}

func Calc(u *Node) (int64, int64, int64) {
    if len(u.next) == 0 {
        fmt.Println("DONE", u.val)
        return u.val, -u.val, Abs(u.val)
    }

    var delta, sum int64

    lo := int64(math.MaxInt64)
    hi := int64(math.MinInt64)

    for v := range u.next {
        v, d, s := Calc(v)

        delta += d
        sum += s

        lo = Min(lo, v)
        hi = Max(hi, v)
    }

    fmt.Println("HERE", u.val, u.val + delta - lo - hi, -u.val, sum + Abs(delta + u.val) + hi - lo)
    return u.val + delta - lo - hi, -u.val, sum + Abs(delta + u.val) + hi - lo

    // fmt.Println(u.val, sum, lo, hi)
    // return hi - lo

//     nv := u.val + delta

//     delta -= nv
//     sum += Abs(nv)

//     return delta, sum
}

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

func Min(args ...int64) int64 {
	min := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] < min {
			min = args[i]
		}
	}
	return min
}

func Max(args ...int64) int64 {
	max := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] > max {
			max = args[i]
		}
	}
	return max
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}