package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	scanner.Scan()
	w, _ := strconv.Atoi(scanner.Text())
	arr := []string{}
	for scanner.Scan() {
		arr = append(arr, scanner.Text())
	}

	unHeatIndex, count := 0, 0
	for unHeatIndex < len(arr) {
		l, r, pos := unHeatIndex-w+1, unHeatIndex+w-1, -1
		for i := l; i <= r; i++ {
			if (i >= 0) && (i < len(arr)) && (arr[i] == "1") {
				pos = i
			}
		}
		if pos == -1 {
			fmt.Println(-1)
			os.Exit(0)
		}
		unHeatIndex = pos + w
		count++
	}
	fmt.Println(count)
}

package main

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

const (
	MaxIntervals = 100000
)

type interval struct {
	start, end int
}

var n, r int
var a []interval
var dp []int

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d\n", &n, &r)
	a = make([]interval, 0)
	dp = make([]int, n + 1)
	for i := 1; i <= n; i++ {
		var tmp int
		fmt.Fscanf(buf, "%d", &tmp)
		if tmp == 1 {
			start := i - r + 1
			if start < 1 {
				start = 1
			}
			end := i + r - 1
			if end > n {
				end = n
			}
			a = append(a, interval{start, end})
		}
	}
	m := len(a)
	dp[0] = 0
	for i := 1; i <= n; i++ {
		dp[i] = MaxIntervals
		for j := 0; j < m; j++ {
			if a[j].start <= i && i <= a[j].end {
				k := dp[a[j].start-1] + 1
				if k < dp[i] {
					dp[i] = k
				}
			}
		}
		if dp[i] == MaxIntervals {
			fmt.Println(-1)
			return
		}
	}
	fmt.Println(dp[n])
}

package main

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

var n, r int
var a [1050]int

var ans = 0
var coveredTo = -1
var p = 0

func getNext(toCover int) int {
  next := -1
  for p < n {
    // fmt.Println(p)
    if a[p] == 0 {
      p++
      continue
    }
    if p + r - 1 < toCover {
      p++
      continue
    }
    if p - r + 1 > toCover {
      break
    }
    next = p
    p++
  }
  return next
}

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

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

  for i := 0; i < n; i++ {
    if coveredTo < i {
      next := getNext(i)
      if next == -1 {
        fmt.Fprintf(writer, "%d\n", -1)
        return
      }
      coveredTo = next + r - 1
      ans++
    }
  }

  fmt.Fprintf(writer, "%d\n", ans)
}

package main

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

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

	var n, r int
	a := make([]int, 1000+5)
	vis := make([]int, 1000+5)

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

	last, lasta, ok, ans := 1, -1, 1, 0
	_ = vis
	_ = last
	_ = lasta
	_ = ans

	for i := 1; i <= n; i ++{
		if a[i] == 1{
			if last >= i - r + 1{
				lasta = i
				continue
			}
			if lasta == -1 || lasta + r - 1 < last || lasta - r + 1 > last{
				if(i + r - 1 < last || i - r + 1 > last) {
					ok = 0
					break
				}
				ans ++
				lasta = i
				last = i + r
			}else {
				last = lasta + r
				lasta = i
				ans ++
			}
		}
	}
	//fmt.Println(ans, last, lasta)
	if last <= n{
		if lasta == -1 || lasta - r + 1 > last || lasta + r - 1 < n{
			ok = 0
		}
		ans ++
	}

	if ok == 0{
		fmt.Fprint(writer, -1)
		return
	}else if ok == 1 {
		fmt.Fprint(writer, ans)
	}
}

package main

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

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

	var n, r int
	a := make([]int, 1000+5)
	vis := make([]int, 1000+5)

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

	last, lasta, ok, ans := 1, -1, 1, 0
	_ = vis
	_ = last
	_ = lasta
	_ = ans

	for i := 1; i <= n; i ++{
		if a[i] == 1{
			if last >= i - r + 1{
				lasta = i
				continue
			}
			if lasta == -1 || lasta + r - 1 < last || lasta - r + 1 > last{
				ok = 0
				break
			}
			last = lasta + r - 1
			lasta = i
			ans ++
		}
	}
	//fmt.Println(ans, last, lasta)
	if last < n{
		if lasta == -1 || lasta + r - 1 <= n{
			ok = 0
		}
		ans ++
	}

	if ok == 0{
		fmt.Fprint(writer, -1)
		return
	}else if ok == 1 {
		fmt.Fprint(writer, ans)
	}
}

package main

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

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

	var n, r int
	a := make([]int, 1000+5)
	vis := make([]int, 1000+5)

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

	last, lasta, ok, ans := 1, -1, 1, 0
	_ = vis
	_ = last
	_ = lasta
	_ = ans

	for i := 1; i <= n; i ++{
		if a[i] == 1{
			if last >= i - r + 1{
				lasta = i
				continue
			}
			if lasta == -1 || lasta + r - 1 < last || lasta - r + 1 > last{
				if(i + r - 1 < last || i - r + 1 > last) {
					ok = 0
					break
				}
				ans ++
				lasta = i
				last = i + r
			}else {
				last = lasta + r
				lasta = i
				ans ++
			}
		}
	}
	//fmt.Println(ans, last, lasta)
	if last <= n{
		if lasta == -1 || lasta + r - 1 < n{
			ok = 0
		}
		ans ++
	}

	if ok == 0{
		fmt.Fprint(writer, -1)
		return
	}else if ok == 1 {
		fmt.Fprint(writer, ans)
	}
}

package main

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

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

	var n, r int
	a := make([]int, 1000+5)
	vis := make([]int, 1000+5)

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

	last, lasta, ok, ans := 1, -1, 1, 0
	_ = vis
	_ = last
	_ = lasta
	_ = ans

	for i := 1; i <= n; i ++{
		if a[i] == 1{
			if last >= i - r + 1{
				lasta = i
				continue
			}
			if lasta == -1 || lasta + r - 1 < last || lasta - r + 1 > last{
				ok = 0
				break
			}
			last = lasta + r
			lasta = i
			ans ++
		}
	}
	//fmt.Println(ans, last, lasta)
	if last <= n{
		if lasta == -1 || lasta + r - 1 <= n{
			ok = 0
		}
		ans ++
	}

	if ok == 0{
		fmt.Fprint(writer, -1)
		return
	}else if ok == 1 {
		fmt.Fprint(writer, ans)
	}
}


// Codeforces 1046 C Space Formula, train
package main

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

var in *bufio.Reader

func readFive() (int,int,int,int,int) {
	var a [5]int
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for i:=0; i<len(ss); i++ {
		a[i], err = strconv.Atoi(ss[i])
		if err != nil {
			fmt.Println("n atoi failure", err)
			panic(err)
		}
	}
	return a[0],a[1],a[2],a[3],a[4]
}

func readLineNumbs(n int) []int {
	a := make([]int, n)
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for j:=0; j<n; j++ {
		aa,err := strconv.Atoi(ss[j])
		if err != nil {
			fmt.Println("aa conv failed", err)
			panic(err)
		}
		a[j] = aa
	}
	return a
}

var n,d int
var s []int
var p []int
var bestRank int

func readData() {
	in = bufio.NewReader(os.Stdin)
	n,d,_,_,_ = readFive()
	s = readLineNumbs(n)
	p = readLineNumbs(n)
}

func solve() {
	d = d-1
	gpo := s[d] + p[0]
	s = append(s[0 : d], s[d+1 : n]...)
	p = p[1 : n]
	ip := n-2
	worse := 0
	for i := 0; i<n-1 && ip>=0; {
		sum := p[ip] + s[i];
		// fmt.Println("i,ip,sum,worse = ",i,ip,sum, worse)
		if sum <= gpo {
			worse++
			i++
			ip--
		} else {
			i++
		}
	}
	bestRank = n-worse
}

func printRes() {
	fmt.Println(bestRank)
}

func main() {
	readData()
	solve()
	printRes()
}

package main

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

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,D,i int64

  scanf("%d %d\n", &N, &D)
  lineS, _ := reader.ReadString('\n')
  lineS = strings.TrimSuffix(lineS, "\r\n")
  lineP, _ := reader.ReadString('\n')
  lineP = strings.TrimSuffix(lineP, "\r\n")
  S := strings.Split(lineS, " ")
  P := strings.Split(lineP, " ")
  tmpC,_ := strconv.ParseInt(S[D-1], 10, 64)
  tmpM,_ := strconv.ParseInt(P[0], 10, 64)
  tmpV := tmpC+tmpM
  rank := D
  N--
  for i = 0 ; i < D-1 ; i++ {
    tmpP,_ := strconv.ParseInt(P[N], 10, 64)
    tmpS,_ := strconv.ParseInt(S[i], 10, 64)
    if tmpP + tmpS <= tmpV {
      rank--
      N--
    }
  }
  printf("%d\n", rank)

}

package main

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

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,D,i int64

  scanf("%d %d\n", &N, &D)
  lineS, _ := reader.ReadString('\n')
  lineS = strings.TrimSuffix(lineS, "\r\n")
  lineP, _ := reader.ReadString('\n')
  lineP = strings.TrimSuffix(lineP, "\r\n")
  S := strings.Split(lineS, " ")
  P := strings.Split(lineP, " ")
  tmpC,_ := strconv.ParseInt(S[D-1], 10, 64)
  tmpM,_ := strconv.ParseInt(P[0], 10, 64)
  rank1 := D
  indexN := N
  for i = D-2 ; i>0 ; i-- {
      tmpS,_ := strconv.ParseInt(S[i], 10, 64)
      indexN--
      tmpP,_ := strconv.ParseInt(P[indexN], 10, 64)
      if tmpS+tmpP <= tmpC+tmpM {
        rank1--
      }else{
        break
      }
  }
  rank2 := D
  indexN = N
  for i = 0 ; i < D-1 ; i++ {
      tmpS,_ := strconv.ParseInt(S[i], 10, 64)
      indexN--
      tmpP,_ := strconv.ParseInt(P[indexN], 10, 64)
      if tmpS+tmpP <= tmpC+tmpM {
        rank2--
      }
  }
  if rank1 > rank2 {
    printf("%d\n", rank2)
  }else{
    printf("%d\n", rank1)
  }

}

package main

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

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,D int64

  scanf("%d %d\n", &N, &D)
  lineS, _ := reader.ReadString('\n')
  lineS = strings.TrimSuffix(lineS, "\r\n")
  lineP, _ := reader.ReadString('\n')
  lineP = strings.TrimSuffix(lineP, "\r\n")
  S := strings.Split(lineS, " ")
  P := strings.Split(lineP, " ")
  rank := D
  for i := D-2 ; i>0 ; i-- {
      tmpC,_ := strconv.ParseInt(S[D-1], 10, 64)
      tmpM,_ := strconv.ParseInt(P[0], 10, 64)
      tmpS,_ := strconv.ParseInt(S[i], 10, 64)
      N--
      tmpP,_ := strconv.ParseInt(P[N], 10, 64)
      if tmpC+tmpM >= tmpS+tmpP  {
        rank--
      }else{
          break
      }
  }
  printf("%d\n", rank)

}


package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y int
	}
	type Event struct {
		angle float64
		right bool
		idx   int
	}
	const eps = 1e-8

	n := io.nextInt()
	a := make([]Seg, n)
	for i := 0; i < n; i++ {
		a[i] = Seg{io.nextInt(), io.nextInt(), io.nextInt()}
	}

	zeroOk := true
	var e []Event
	for i, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := float64(q.xl-p.xr) / float64(p.y-q.y)
				r := float64(q.xr-p.xl) / float64(p.y-q.y)
				e = append(e, Event{l, false, i}, Event{r, true, i})
				if l < 0 && r > 0 {
					zeroOk = false
				}
			}
		}
	}
	sort.Slice(e, func(i, j int) bool {
		a, b := e[i], e[j]
		if math.Abs(a.angle-b.angle) > eps {
			return a.angle < b.angle
		}
		return a.right && !b.right
	})

	var options []float64
	if zeroOk {
		options = append(options, 0)
	}

	active := make(map[int]int)
	total := 0
	for _, ev := range e {
		if ev.right {
			active[ev.idx]--
			if active[ev.idx] == 0 {
				total--
			}
		}
		if total == 0 {
			if len(options) == 0 || math.Abs(ev.angle-options[len(options)-1]) > eps {
				options = append(options, ev.angle)
			}
		}
		if !ev.right {
			if active[ev.idx] == 0 {
				total++
			}
			active[ev.idx]++
		}
	}

	cost := func(angle float64) float64 {
		left, right := 1e18, -1e18
		for _, s := range a {
			lt := float64(s.xl) + float64(s.y)*angle
			rt := float64(s.xr) + float64(s.y)*angle
			if lt < left {
				left = lt
			}
			if rt > right {
				right = rt
			}
		}
		return right - left
	}

	lo, hi := 0, len(options)-1
	for lo < hi {
		m1 := (lo + hi) / 2
		if cost(options[m1]) < cost(options[m1+1]) {
			hi = m1
		} else {
			lo = m1 + 1
		}
	}

	fmt.Fprintf(io, "%.10f\n", cost(options[hi]))
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y int
	}
	type Event struct {
		angle float64
		right bool
		idx   int
	}
	const eps = 1e-8

	n := io.nextInt()
	a := make([]Seg, n)
	for i := 0; i < n; i++ {
		a[i] = Seg{io.nextInt(), io.nextInt(), io.nextInt()}
	}

	zeroOk := true
	var e []Event
	for i, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := float64(q.xl-p.xr) / float64(p.y-q.y)
				r := float64(q.xr-p.xl) / float64(p.y-q.y)
				e = append(e, Event{l, false, i}, Event{r, true, i})
				if l < 0 && r > 0 {
					zeroOk = false
				}
			}
		}
	}
	sort.Slice(e, func(i, j int) bool {
		a, b := e[i], e[j]
		if math.Abs(a.angle-b.angle) > eps {
			return a.angle < b.angle
		}
		return a.right && !b.right
	})

	var op []float64
	if zeroOk {
		op = append(op, 0)
	}

	active := make(map[int]int)
	total := 0
	for _, ev := range e {
		if ev.right {
			active[ev.idx]--
			if active[ev.idx] == 0 {
				total--
			}
		}
		if total == 0 && (len(op) == 0 || math.Abs(ev.angle-op[len(op)-1]) > eps) {
			op = append(op, ev.angle)
		}
		if !ev.right {
			if active[ev.idx] == 0 {
				total++
			}
			active[ev.idx]++
		}
	}

	cost := func(angle float64) float64 {
		left, right := 1e18, -1e18
		for _, s := range a {
			lt := float64(s.xl) + float64(s.y)*angle
			rt := float64(s.xr) + float64(s.y)*angle
			if lt < left {
				left = lt
			}
			if rt > right {
				right = rt
			}
		}
		return right - left
	}

	lo, hi := 0, len(op)-1
	for lo < hi {
		m1 := (lo + hi) / 2
		if cost(op[m1]) < cost(op[m1+1]) {
			hi = m1
		} else {
			lo = m1 + 1
		}
	}

	fmt.Fprintf(io, "%.10f\n", cost(op[lo]))
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y int
	}
	type Event struct {
		angle float64
		right bool
		idx   int
	}
	const eps = 1e-8

	n := io.nextInt()
	a := make([]Seg, n)
	for i := 0; i < n; i++ {
		a[i] = Seg{io.nextInt(), io.nextInt(), io.nextInt()}
	}

	var e []Event
	for i, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := float64(q.xl-p.xr) / float64(p.y-q.y)
				r := float64(q.xr-p.xl) / float64(p.y-q.y)
				e = append(e, Event{l, false, i}, Event{r, true, i})
			}
		}
	}
	sort.Slice(e, func(i, j int) bool {
		a, b := e[i], e[j]
		if math.Abs(a.angle-b.angle) > eps {
			return a.angle < b.angle
		}
		return a.right && !b.right
	})

	op := []float64{-1e9}
	active := make([]int, n)
	total := 0
	for _, ev := range e {
		if ev.right {
			active[ev.idx]--
			if active[ev.idx] == 0 {
				total--
			}
		}
		if total == 0 && (len(op) == 0 || math.Abs(ev.angle-op[len(op)-1]) > eps) {
			op = append(op, ev.angle)
		}
		if !ev.right {
			if active[ev.idx] == 0 {
				total++
			}
			active[ev.idx]++
		}
	}

	cost := func(angle float64) float64 {
		left, right := 1e18, -1e18
		for _, s := range a {
			lt := float64(s.xl) + float64(s.y)*angle
			rt := float64(s.xr) + float64(s.y)*angle
			if lt < left {
				left = lt
			}
			if rt > right {
				right = rt
			}
		}
		return right - left
	}

	lo, hi := 0, len(op)-1
	for lo < hi {
		m1 := (lo + hi) / 2
		if cost(op[m1]) < cost(op[m1+1]) {
			hi = m1
		} else {
			lo = m1 + 1
		}
	}

	fmt.Fprintf(io, "%.10f\n", cost(op[lo]))
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y int
	}
	type Event struct {
		angle float64
		right bool
		idx   int
	}
	const eps = 1e-8

	n := io.nextInt()
	a := make([]Seg, n)
	for i := 0; i < n; i++ {
		a[i] = Seg{io.nextInt(), io.nextInt(), io.nextInt()}
	}

	var e []Event
	for i, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := float64(q.xl-p.xr) / float64(p.y-q.y)
				r := float64(q.xr-p.xl) / float64(p.y-q.y)
				e = append(e, Event{l, false, i}, Event{r, true, i})
			}
		}
	}
	sort.Slice(e, func(i, j int) bool {
		a, b := e[i], e[j]
		if math.Abs(a.angle-b.angle) > eps {
			return a.angle < b.angle
		}
		return a.right && !b.right
	})

	op := []float64{-1e9}
	active := make(map[int]int)
	total := 0
	for _, ev := range e {
		if ev.right {
			active[ev.idx]--
			if active[ev.idx] == 0 {
				total--
			}
		}
		if total == 0 && (len(op) == 0 || math.Abs(ev.angle-op[len(op)-1]) > eps) {
			op = append(op, ev.angle)
		}
		if !ev.right {
			if active[ev.idx] == 0 {
				total++
			}
			active[ev.idx]++
		}
	}

	cost := func(angle float64) float64 {
		left, right := 1e18, -1e18
		for _, s := range a {
			lt := float64(s.xl) + float64(s.y)*angle
			rt := float64(s.xr) + float64(s.y)*angle
			if lt < left {
				left = lt
			}
			if rt > right {
				right = rt
			}
		}
		return right - left
	}

	lo, hi := 0, len(op)-1
	for lo < hi {
		m1 := (lo + hi) / 2
		if cost(op[m1]) < cost(op[m1+1]) {
			hi = m1
		} else {
			lo = m1 + 1
		}
	}

	fmt.Fprintf(io, "%.10f\n", cost(op[lo]))
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y float64
	}
	type Event struct {
		angle float64
		d     int
	}
	const eps = 1e-8

	n := io.nextInt()
	a := make([]Seg, n)
	for i := 0; i < n; i++ {
		a[i] = Seg{float64(io.nextInt()), float64(io.nextInt()), float64(io.nextInt())}
	}

	sort.Slice(a, func(i, j int) bool { return a[i].y < a[j].y })

	var mx, mn ConvexHull
	for _, s := range a {
		mx.add(Line{s.y, s.xr})
	}
	for i := n - 1; i >= 0; i-- {
		mn.add(Line{-a[i].y, -a[i].xl})
	}

	var e []Event
	for _, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := (q.xl - p.xr) / (p.y - q.y)
				r := (q.xr - p.xl) / (p.y - q.y)
				e = append(e, Event{l, 1}, Event{r, -1})
			}
		}
	}
	sort.Slice(e, func(i, j int) bool {
		a, b := e[i], e[j]
		if math.Abs(a.angle-b.angle) > eps {
			return a.angle < b.angle
		}
		return a.d < b.d
	})

	ans := mx.query(-1e9) + mn.query(-1e9)
	total := 0
	for _, ev := range e {
		total += ev.d
		if total == 0 || total == ev.d {
			cur := mx.query(ev.angle) + mn.query(ev.angle)
			if cur < ans {
				ans = cur
			}
		}
	}

	fmt.Fprintf(io, "%.10f\n", ans)
}

type Line struct {
	m, c float64
}

func (l *Line) eval(x float64) float64     { return l.m*x + l.c }
func (l *Line) intersectX(o *Line) float64 { return (l.c - o.c) / (o.m - l.m) }

// Max convex hull; add lines in increasing order of slope; query in increasing order.
type ConvexHull []Line

func (cc *ConvexHull) add(l Line) {
	c := *cc
	if len(c) > 0 && c[len(c)-1].m == l.m {
		if c[len(c)-1].c >= l.c {
			return
		}
		c = c[:len(c)-1]
	}
	for len(c) > 1 && c[len(c)-1].intersectX(&l) <= c[len(c)-1].intersectX(&c[len(c)-2]) {
		c = c[:len(c)-1]
	}
	*cc = append(c, l)
}

func (cc *ConvexHull) query(x float64) float64 {
	c := *cc
	for len(c) > 1 && c[0].eval(x) <= c[1].eval(x) {
		c = c[1:]
	}
	*cc = c
	return c[0].eval(x)
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y int
	}
	type Event struct {
		angle float64
		right bool
		idx   int
	}
	const eps = 1e-8

	n := io.nextInt()
	a := make([]*Seg, n)
	for i := 0; i < n; i++ {
		a[i] = &Seg{io.nextInt(), io.nextInt(), io.nextInt()}
	}

	zeroOk := true
	var e []*Event
	for i, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := float64(q.xl-p.xr) / float64(p.y-q.y)
				r := float64(q.xr-p.xl) / float64(p.y-q.y)
				e = append(e, &Event{l, false, i}, &Event{r, true, i})
				if l < 0 && r > 0 {
					zeroOk = false
				}
			}
		}
	}
	sort.Slice(e, func(i, j int) bool {
		a, b := e[i], e[j]
		if math.Abs(a.angle-b.angle) > eps {
			return a.angle < b.angle
		}
		return a.right && !b.right
	})

	var op []float64
	if zeroOk {
		op = append(op, 0)
	}

	active := make(map[int]int)
	total := 0
	for _, ev := range e {
		if ev.right {
			active[ev.idx]--
			if active[ev.idx] == 0 {
				total--
			}
		}
		if total == 0 && (len(op) == 0 || math.Abs(ev.angle-op[len(op)-1]) > eps) {
			op = append(op, ev.angle)
		}
		if !ev.right {
			if active[ev.idx] == 0 {
				total++
			}
			active[ev.idx]++
		}
	}

	cost := func(angle float64) float64 {
		left, right := 1e18, -1e18
		for _, s := range a {
			lt := float64(s.xl) + float64(s.y)*angle
			rt := float64(s.xr) + float64(s.y)*angle
			if lt < left {
				left = lt
			}
			if rt > right {
				right = rt
			}
		}
		return right - left
	}

	lo, hi := 0, len(op)-1
	for lo < hi {
		m := (lo + hi) / 2
		if cost(op[m]) < cost(op[m+1]) {
			hi = m
		} else {
			lo = m + 1
		}
	}

	fmt.Fprintf(io, "%.10f\n", cost(op[lo]))
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y int
	}
	type Event struct {
		angle float64
		d     int
	}
	const eps = 1e-8

	n := io.nextInt()
	a := make([]Seg, n)
	for i := 0; i < n; i++ {
		a[i] = Seg{io.nextInt(), io.nextInt(), io.nextInt()}
	}

	var e []Event
	for _, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := float64(q.xl-p.xr) / float64(p.y-q.y)
				r := float64(q.xr-p.xl) / float64(p.y-q.y)
				e = append(e, Event{l, 1}, Event{r, -1})
			}
		}
	}
	sort.Slice(e, func(i, j int) bool {
		a, b := e[i], e[j]
		if math.Abs(a.angle-b.angle) > eps {
			return a.angle < b.angle
		}
		return a.d < b.d
	})

	op := []float64{-1e9}
	total := 0
	for _, ev := range e {
		total += ev.d
		if (total == 0 || total == ev.d) && math.Abs(ev.angle-op[len(op)-1]) > eps {
			op = append(op, ev.angle)
		}
	}

	cost := func(angle float64) float64 {
		left, right := 1e18, -1e18
		for _, s := range a {
			lt := float64(s.xl) + float64(s.y)*angle
			rt := float64(s.xr) + float64(s.y)*angle
			if lt < left {
				left = lt
			}
			if rt > right {
				right = rt
			}
		}
		return right - left
	}

	lo, hi := 0, len(op)-1
	for lo < hi {
		m1 := (lo + hi) / 2
		if cost(op[m1]) < cost(op[m1+1]) {
			hi = m1
		} else {
			lo = m1 + 1
		}
	}

	fmt.Fprintf(io, "%.10f\n", cost(op[lo]))
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y int
	}
	type Event struct {
		angle float64
		d     int
	}
	const eps = 1e-8

	n := io.nextInt()
	a := make([]Seg, n)
	for i := 0; i < n; i++ {
		a[i] = Seg{io.nextInt(), io.nextInt(), io.nextInt()}
	}

	var e []Event
	for _, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := float64(q.xl-p.xr) / float64(p.y-q.y)
				r := float64(q.xr-p.xl) / float64(p.y-q.y)
				e = append(e, Event{l, 1}, Event{r, -1})
			}
		}
	}
	sort.Slice(e, func(i, j int) bool {
		a, b := e[i], e[j]
		if math.Abs(a.angle-b.angle) > eps {
			return a.angle < b.angle
		}
		return a.d < b.d
	})

	op := []float64{-1e9}
	total := 0
	for _, ev := range e {
		total += ev.d
		if (total == 0 || total == ev.d) && (len(op) == 0 || math.Abs(ev.angle-op[len(op)-1]) > eps) {
			op = append(op, ev.angle)
		}
	}

	cost := func(angle float64) float64 {
		left, right := 1e18, -1e18
		for _, s := range a {
			lt := float64(s.xl) + float64(s.y)*angle
			rt := float64(s.xr) + float64(s.y)*angle
			if lt < left {
				left = lt
			}
			if rt > right {
				right = rt
			}
		}
		return right - left
	}

	lo, hi := 0, len(op)-1
	for lo < hi {
		m1 := (lo + hi) / 2
		if cost(op[m1]) < cost(op[m1+1]) {
			hi = m1
		} else {
			lo = m1 + 1
		}
	}

	fmt.Fprintf(io, "%.10f\n", cost(op[lo]))
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y int
	}
	type Event struct {
		angle float64
		right bool
		idx   int
	}
	const eps = 1e-9

	n := io.nextInt()
	a := make([]Seg, n)
	for i := 0; i < n; i++ {
		a[i] = Seg{io.nextInt(), io.nextInt(), io.nextInt()}
	}

	zeroOk := true
	var e []Event
	for i, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := float64(q.xl-p.xr) / float64(p.y-q.y)
				r := float64(q.xr-p.xl) / float64(p.y-q.y)
				e = append(e, Event{l, false, i}, Event{r, true, i})
				if l < 0 && r > 0 {
					zeroOk = false
				}
			}
		}
	}
	sort.Slice(e, func(i, j int) bool {
		a, b := e[i], e[j]
		if math.Abs(a.angle-b.angle) > eps {
			return a.angle < b.angle
		}
		return a.right && !b.right
	})

	var options []float64
	if zeroOk {
		options = append(options, 0)
	}

	active := make(map[int]int)
	total := 0
	for _, ev := range e {
		if ev.right {
			active[ev.idx]--
			if active[ev.idx] == 0 {
				total--
			}
		}
		if total == 0 {
			if len(options) == 0 || ev.angle-options[len(options)-1] > eps {
				options = append(options, ev.angle)
			}
		}
		if !ev.right {
			if active[ev.idx] == 0 {
				total++
			}
			active[ev.idx]++
		}
	}

	cost := func(angle float64) float64 {
		left, right := 1e9, -1e9
		for _, s := range a {
			lt := float64(s.xl) + float64(s.y)*angle
			rt := float64(s.xr) + float64(s.y)*angle
			if lt < left {
				left = lt
			}
			if rt > right {
				right = rt
			}
		}
		return right - left
	}

	lo, hi := 0, len(options)-1
	for lo < hi {
		m1 := (lo + hi) / 2
		if cost(options[m1]) < cost(options[m1+1]) {
			hi = m1
		} else {
			lo = m1 + 1
		}
	}

	fmt.Fprintf(io, "%.10f\n", cost(options[hi]))
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y int
	}
	type Event struct {
		angle float64
		right bool
		idx   int
	}
	const eps = 1e-9

	n := io.nextInt()
	a := make([]Seg, n)
	for i := 0; i < n; i++ {
		a[i] = Seg{io.nextInt(), io.nextInt(), io.nextInt()}
	}

	zeroOk := true
	var e []Event
	for i, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := float64(q.xl-p.xr) / float64(p.y-q.y)
				r := float64(q.xr-p.xl) / float64(p.y-q.y)
				e = append(e, Event{l, false, i}, Event{r, true, i})
				if l < 0 && r > 0 {
					zeroOk = false
				}
			}
		}
	}
	sort.Slice(e, func(i, j int) bool {
		a, b := e[i], e[j]
		if math.Abs(a.angle-b.angle) > eps {
			return a.angle < b.angle
		}
		return a.right && !b.right
	})

	var options []float64
	if zeroOk {
		options = append(options, 0)
	}

	active := make(map[int]int)
	total := 0
	for _, ev := range e {
		if ev.right {
			active[ev.idx]--
			if active[ev.idx] == 0 {
				total--
			}
		}
		if total == 0 {
			if len(options) == 0 || math.Abs(ev.angle-options[len(options)-1]) > eps {
				options = append(options, ev.angle)
			}
		}
		if !ev.right {
			if active[ev.idx] == 0 {
				total++
			}
			active[ev.idx]++
		}
	}

	cost := func(angle float64) float64 {
		left, right := 1e9, -1e9
		for _, s := range a {
			lt := float64(s.xl) + float64(s.y)*angle
			rt := float64(s.xr) + float64(s.y)*angle
			if lt < left {
				left = lt
			}
			if rt > right {
				right = rt
			}
		}
		return right - left
	}

	lo, hi := 0, len(options)-1
	for lo < hi {
		m1 := (lo + hi) / 2
		if cost(options[m1]) < cost(options[m1+1]) {
			hi = m1
		} else {
			lo = m1 + 1
		}
	}

	fmt.Fprintf(io, "%.10f\n", cost(options[hi]))
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y int
	}
	type Event struct {
		angle float64
		right bool
		idx   int
	}

	n := io.nextInt()
	a := make([]Seg, n)
	for i := 0; i < n; i++ {
		a[i] = Seg{io.nextInt(), io.nextInt(), io.nextInt()}
	}

	zeroOk := true
	var e []Event
	for i, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := float64(q.xl-p.xr) / float64(p.y-q.y)
				r := float64(q.xr-p.xl) / float64(p.y-q.y)
				e = append(e, Event{l, false, i}, Event{r, true, i})
				if l < 0 && r > 0 {
					zeroOk = false
				}
			}
		}
	}
	sort.Slice(e, func(i, j int) bool {
		a, b := e[i], e[j]
		if math.Abs(a.angle-b.angle) > 1e-9 {
			return a.angle < b.angle
		}
		return a.right && !b.right
	})

	active := make(map[int]int)
	total := 0
	lbest, rbest := -1e9, 1e9
	for _, ev := range e {
		if ev.right {
			active[ev.idx]--
			if active[ev.idx] == 0 {
				total--
			}
		}
		if total == 0 {
			if ev.angle < 0 {
				if ev.angle > lbest {
					lbest = ev.angle
				}
			} else {
				if ev.angle < rbest {
					rbest = ev.angle
				}
			}
		}
		if !ev.right {
			if active[ev.idx] == 0 {
				total++
			}
			active[ev.idx]++
		}
	}

	best := rbest
	if -lbest < rbest {
		best = lbest
	}
	if zeroOk {
		best = 0
	}

	left, right := 1e9, -1e9
	for _, s := range a {
		lt := float64(s.xl) + float64(s.y)*best
		rt := float64(s.xr) + float64(s.y)*best
		if lt < left {
			left = lt
		}
		if rt > right {
			right = rt
		}
	}

	fmt.Fprintf(io, "%.10f\n", right-left)
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y int
	}
	type Event struct {
		angle float64
		right bool
		idx   int
	}
	const eps = 1e-8

	n := io.nextInt()
	a := make([]Seg, n)
	for i := 0; i < n; i++ {
		a[i] = Seg{io.nextInt(), io.nextInt(), io.nextInt()}
	}

	zeroOk := true
	var e []Event
	for i, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := float64(q.xl-p.xr) / float64(p.y-q.y)
				r := float64(q.xr-p.xl) / float64(p.y-q.y)
				e = append(e, Event{l, false, i}, Event{r, true, i})
				if l < 0 && r > 0 {
					zeroOk = false
				}
			}
		}
	}
	sort.Slice(e, func(i, j int) bool {
		a, b := e[i], e[j]
		if a.angle != b.angle {
			return a.angle < b.angle
		}
		return a.right && !b.right
	})

	var op []float64
	if zeroOk {
		op = append(op, 0)
	}

	active := make(map[int]int)
	total := 0
	for _, ev := range e {
		if ev.right {
			active[ev.idx]--
			if active[ev.idx] == 0 {
				total--
			}
		}
		if total == 0 && (len(op) == 0 || ev.angle-op[len(op)-1] > eps) {
			op = append(op, ev.angle)
		}
		if !ev.right {
			if active[ev.idx] == 0 {
				total++
			}
			active[ev.idx]++
		}
	}

	cost := func(angle float64) float64 {
		left, right := 1e18, -1e18
		for _, s := range a {
			lt := float64(s.xl) + float64(s.y)*angle
			rt := float64(s.xr) + float64(s.y)*angle
			if lt < left {
				left = lt
			}
			if rt > right {
				right = rt
			}
		}
		return right - left
	}

	lo, hi := 0, len(op)-1
	for lo < hi {
		m1 := (lo + hi) / 2
		if cost(op[m1]) < cost(op[m1+1]) {
			hi = m1
		} else {
			lo = m1 + 1
		}
	}

	fmt.Fprintf(io, "%.10f\n", cost(op[lo]))
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y int
	}
	type Event struct {
		angle float64
		right bool
		idx   int
	}
	const eps = -1e8

	n := io.nextInt()
	a := make([]Seg, n)
	for i := 0; i < n; i++ {
		a[i] = Seg{io.nextInt(), io.nextInt(), io.nextInt()}
	}

	zeroOk := true
	var e []Event
	for i, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := float64(q.xl-p.xr) / float64(p.y-q.y)
				r := float64(q.xr-p.xl) / float64(p.y-q.y)
				e = append(e, Event{l, false, i}, Event{r, true, i})
				if l < 0 && r > 0 {
					zeroOk = false
				}
			}
		}
	}
	sort.Slice(e, func(i, j int) bool {
		a, b := e[i], e[j]
		if math.Abs(a.angle-b.angle) > eps {
			return a.angle < b.angle
		}
		return a.right && !b.right
	})

	var options []float64
	if zeroOk {
		options = append(options, 0)
	}

	active := make(map[int]int)
	total := 0
	for _, ev := range e {
		if ev.right {
			active[ev.idx]--
			if active[ev.idx] == 0 {
				total--
			}
		}
		if total == 0 {
			if len(options) == 0 || ev.angle-options[len(options)-1] > eps {
				options = append(options, ev.angle)
			}
		}
		if !ev.right {
			if active[ev.idx] == 0 {
				total++
			}
			active[ev.idx]++
		}
	}

	cost := func(angle float64) float64 {
		left, right := 1e9, -1e9
		for _, s := range a {
			lt := float64(s.xl) + float64(s.y)*angle
			rt := float64(s.xr) + float64(s.y)*angle
			if lt < left {
				left = lt
			}
			if rt > right {
				right = rt
			}
		}
		return right - left
	}

	lo, hi := 0, len(options)-1
	for lo < hi {
		m1 := (lo + hi) / 2
		if cost(options[m1]) < cost(options[m1+1]) {
			hi = m1
		} else {
			lo = m1 + 1
		}
	}

	fmt.Fprintf(io, "%.10f\n", cost(options[hi]))
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y int
	}
	type Event struct {
		angle float64
		right bool
		idx   int
	}
	const eps = 1e-8

	n := io.nextInt()
	a := make([]Seg, n)
	for i := 0; i < n; i++ {
		a[i] = Seg{io.nextInt(), io.nextInt(), io.nextInt()}
	}

	zeroOk := true
	var e []Event
	for i, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := float64(q.xl-p.xr) / float64(p.y-q.y)
				r := float64(q.xr-p.xl) / float64(p.y-q.y)
				e = append(e, Event{l, false, i}, Event{r, true, i})
				if l < 0 && r > 0 {
					zeroOk = false
				}
			}
		}
	}

	var op []float64
	if zeroOk {
		op = append(op, 0)
	} else {
		sort.Slice(e, func(i, j int) bool {
			a, b := e[i], e[j]
			if math.Abs(a.angle-b.angle) > eps {
				return a.angle < b.angle
			}
			return a.right && !b.right
		})
		active := make(map[int]int)
		total := 0
		for _, ev := range e {
			if ev.right {
				active[ev.idx]--
				if active[ev.idx] == 0 {
					total--
				}
			}
			if total == 0 && (len(op) == 0 || math.Abs(ev.angle-op[len(op)-1]) > eps) {
				op = append(op, ev.angle)
			}
			if !ev.right {
				if active[ev.idx] == 0 {
					total++
				}
				active[ev.idx]++
			}
		}
	}

	cost := func(angle float64) float64 {
		left, right := 1e18, -1e18
		for _, s := range a {
			lt := float64(s.xl) + float64(s.y)*angle
			rt := float64(s.xr) + float64(s.y)*angle
			if lt < left {
				left = lt
			}
			if rt > right {
				right = rt
			}
		}
		return right - left
	}

	lo, hi := 0, len(op)-1
	for lo < hi {
		m1 := (lo + hi) / 2
		if cost(op[m1]) < cost(op[m1+1]) {
			hi = m1
		} else {
			lo = m1 + 1
		}
	}

	fmt.Fprintf(io, "%.10f\n", cost(op[lo]))
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y int
	}
	type Event struct {
		angle float64
		right bool
		idx   int
	}

	n := io.nextInt()
	a := make([]Seg, n)
	for i := 0; i < n; i++ {
		a[i] = Seg{io.nextInt(), io.nextInt(), io.nextInt()}
	}

	zeroOk := true
	var e []Event
	for i, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := float64(q.xl-p.xr) / float64(p.y-q.y)
				r := float64(q.xr-p.xl) / float64(p.y-q.y)
				e = append(e, Event{l, false, i}, Event{r, true, i})
				if l < 0 && r > 0 {
					zeroOk = false
				}
			}
		}
	}
	sort.Slice(e, func(i, j int) bool {
		a, b := e[i], e[j]
		if math.Abs(a.angle-b.angle) > 1e-9 {
			return a.angle < b.angle
		}
		return a.right && !b.right
	})

	active := make(map[int]int)
	total := 0
	lbest, rbest := -1e9, 1e9
	for _, ev := range e {
		if ev.right {
			active[ev.idx]--
			if active[ev.idx] == 0 {
				total--
			}
		}
		if total == 0 {
			if ev.angle < 0 {
				if ev.angle > lbest {
					lbest = ev.angle
				}
			} else {
				if ev.angle < rbest {
					rbest = ev.angle
				}
			}
		}
		if !ev.right {
			if active[ev.idx] == 0 {
				total++
			}
			active[ev.idx]++
		}
	}

	best := []float64{lbest, rbest}
	if zeroOk {
		best = []float64{0}
	}

	ans := 1e9
	for _, b := range best {
		left, right := 1e9, -1e9
		for _, s := range a {
			lt := float64(s.xl) + float64(s.y)*b
			rt := float64(s.xr) + float64(s.y)*b
			if lt < left {
				left = lt
			}
			if rt > right {
				right = rt
			}
		}
		if right-left < ans {
			ans = right - left
		}
	}

	fmt.Fprintf(io, "%.10f\n", ans)
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y int
	}
	type Event struct {
		angle float64
		right bool
		idx   int
	}
	const eps = 1e-8

	n := io.nextInt()
	a := make([]Seg, n)
	for i := 0; i < n; i++ {
		a[i] = Seg{io.nextInt(), io.nextInt(), io.nextInt()}
	}

	zeroOk := true
	var e []Event
	for i, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := float64(q.xl-p.xr) / float64(p.y-q.y)
				r := float64(q.xr-p.xl) / float64(p.y-q.y)
				e = append(e, Event{l, false, i}, Event{r, true, i})
				if l < 0 && r > 0 {
					zeroOk = false
				}
			}
		}
	}
	sort.Slice(e, func(i, j int) bool {
		a, b := e[i], e[j]
		if math.Abs(a.angle-b.angle) > eps {
			return a.angle < b.angle
		}
		return a.right && !b.right
	})

	var options []float64
	if zeroOk {
		options = append(options, 0)
	}

	active := make(map[int]int)
	total := 0
	for _, ev := range e {
		if ev.right {
			active[ev.idx]--
			if active[ev.idx] == 0 {
				total--
			}
		}
		if total == 0 {
			if len(options) == 0 || ev.angle-options[len(options)-1] > eps {
				options = append(options, ev.angle)
			}
		}
		if !ev.right {
			if active[ev.idx] == 0 {
				total++
			}
			active[ev.idx]++
		}
	}

	cost := func(angle float64) float64 {
		left, right := 1e9, -1e9
		for _, s := range a {
			lt := float64(s.xl) + float64(s.y)*angle
			rt := float64(s.xr) + float64(s.y)*angle
			if lt < left {
				left = lt
			}
			if rt > right {
				right = rt
			}
		}
		return right - left
	}

	lo, hi := 0, len(options)-1
	for lo < hi {
		m1 := (lo + hi) / 2
		if cost(options[m1]) < cost(options[m1+1]) {
			hi = m1
		} else {
			lo = m1 + 1
		}
	}

	fmt.Fprintf(io, "%.10f\n", cost(options[hi]))
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y int
	}
	type Event struct {
		angle float64
		right bool
		idx   int
	}
	const eps = 1e-7

	n := io.nextInt()
	a := make([]Seg, n)
	for i := 0; i < n; i++ {
		a[i] = Seg{io.nextInt(), io.nextInt(), io.nextInt()}
	}

	zeroOk := true
	var e []Event
	for i, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := float64(q.xl-p.xr) / float64(p.y-q.y)
				r := float64(q.xr-p.xl) / float64(p.y-q.y)
				e = append(e, Event{l, false, i}, Event{r, true, i})
				if l < 0 && r > 0 {
					zeroOk = false
				}
			}
		}
	}
	sort.Slice(e, func(i, j int) bool {
		a, b := e[i], e[j]
		if math.Abs(a.angle-b.angle) > eps {
			return a.angle < b.angle
		}
		return a.right && !b.right
	})

	var options []float64
	if zeroOk {
		options = append(options, 0)
	}

	active := make(map[int]int)
	total := 0
	for _, ev := range e {
		if ev.right {
			active[ev.idx]--
			if active[ev.idx] == 0 {
				total--
			}
		}
		if total == 0 {
			if len(options) == 0 || math.Abs(ev.angle-options[len(options)-1]) > eps {
				options = append(options, ev.angle)
			}
		}
		if !ev.right {
			if active[ev.idx] == 0 {
				total++
			}
			active[ev.idx]++
		}
	}

	cost := func(angle float64) float64 {
		left, right := 1e9, -1e9
		for _, s := range a {
			lt := float64(s.xl) + float64(s.y)*angle
			rt := float64(s.xr) + float64(s.y)*angle
			if lt < left {
				left = lt
			}
			if rt > right {
				right = rt
			}
		}
		return right - left
	}

	lo, hi := 0, len(options)-1
	for lo < hi {
		m1 := (lo + hi) / 2
		if cost(options[m1]) < cost(options[m1+1]) {
			hi = m1
		} else {
			lo = m1 + 1
		}
	}

	fmt.Fprintf(io, "%.10f\n", cost(options[hi]))
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y int
	}
	type Event struct {
		angle float64
		right bool
		idx   int
	}

	n := io.nextInt()
	a := make([]Seg, n)
	for i := 0; i < n; i++ {
		a[i] = Seg{io.nextInt(), io.nextInt(), io.nextInt()}
	}

	zeroOk := true
	var e []Event
	for i, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := float64(q.xl-p.xr) / float64(p.y-q.y)
				r := float64(q.xr-p.xl) / float64(p.y-q.y)
				e = append(e, Event{l, false, i}, Event{r, true, i})
				if l < 0 && r > 0 {
					zeroOk = false
				}
			}
		}
	}
	sort.Slice(e, func(i, j int) bool {
		a, b := e[i], e[j]
		if math.Abs(a.angle-b.angle) > 1e-9 {
			return a.angle < b.angle
		}
		return a.right && !b.right
	})

	var options []float64
	if zeroOk {
		options = append(options, 0)
	}

	active := make(map[int]int)
	total := 0
	for _, ev := range e {
		if ev.right {
			active[ev.idx]--
			if active[ev.idx] == 0 {
				total--
			}
		}
		if total == 0 {
			options = append(options, ev.angle)
		}
		if !ev.right {
			if active[ev.idx] == 0 {
				total++
			}
			active[ev.idx]++
		}
	}

	cost := func(angle float64) float64 {
		left, right := 1e9, -1e9
		for _, s := range a {
			lt := float64(s.xl) + float64(s.y)*angle
			rt := float64(s.xr) + float64(s.y)*angle
			if lt < left {
				left = lt
			}
			if rt > right {
				right = rt
			}
		}
		return right - left
	}

	lo, hi := 0, len(options)-1
	for lo < hi {
		m1 := (lo + hi) / 2
		if cost(options[m1]) < cost(options[m1+1]) {
			hi = m1
		} else {
			lo = m1 + 1
		}
	}

	fmt.Fprintf(io, "%.10f\n", cost(options[lo]))
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	type Seg struct {
		xl, xr, y int
	}
	type Event struct {
		angle float64
		right bool
		idx   int
	}
	const eps = 1e-8

	n := io.nextInt()
	a := make([]Seg, n)
	for i := 0; i < n; i++ {
		a[i] = Seg{io.nextInt(), io.nextInt(), io.nextInt()}
	}

	zeroOk := true
	var e []Event
	for i, p := range a {
		for _, q := range a {
			if p.y > q.y {
				l := float64(q.xl-p.xr) / float64(p.y-q.y)
				r := float64(q.xr-p.xl) / float64(p.y-q.y)
				e = append(e, Event{l, false, i}, Event{r, true, i})
				if l < 0 && r > 0 {
					zeroOk = false
				}
			}
		}
	}
	sort.Slice(e, func(i, j int) bool {
		a, b := e[i], e[j]
		if math.Abs(a.angle-b.angle) > eps {
			return a.angle < b.angle
		}
		return a.right && !b.right
	})

	var options []float64
	if zeroOk {
		options = append(options, 0)
	}

	active := make(map[int]int)
	total := 0
	for _, ev := range e {
		if ev.right {
			active[ev.idx]--
			if active[ev.idx] == 0 {
				total--
			}
		}
		if total == 0 {
			if len(options) == 0 || math.Abs(ev.angle-options[len(options)-1]) > eps {
				options = append(options, ev.angle)
			}
		}
		if !ev.right {
			if active[ev.idx] == 0 {
				total++
			}
			active[ev.idx]++
		}
	}

	cost := func(angle float64) float64 {
		left, right := 1e9, -1e9
		for _, s := range a {
			lt := float64(s.xl) + float64(s.y)*angle
			rt := float64(s.xr) + float64(s.y)*angle
			if lt < left {
				left = lt
			}
			if rt > right {
				right = rt
			}
		}
		return right - left
	}

	lo, hi := 0, len(options)-1
	for lo < hi {
		m1 := (lo + hi) / 2
		if cost(options[m1]) < cost(options[m1+1]) {
			hi = m1
		} else {
			lo = m1 + 1
		}
	}

	fmt.Fprintf(io, "%.10f\n", cost(options[hi]))
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}


package main

import (
	"bufio"
	"container/heap"
	. "fmt"
	"io"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
type hp struct{ sort.IntSlice }
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() interface{}   { a := h.IntSlice; v := a[len(a)-1]; h.IntSlice = a[:len(a)-1]; return v }

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

	var T, n, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		g := make([][]int, n)
		d := make([]int, n)
		h := hp{}
		for w := range d {
			Fscan(in, &d[w])
			for i := 0; i < d[w]; i++ {
				Fscan(in, &v)
				g[v-1] = append(g[v-1], w)
			}
			if d[w] == 0 {
				h.IntSlice = append(h.IntSlice, w)
			}
		}
		ans, c := 0, 0
		for nxt := []int{}; h.Len() > 0; nxt = nil {
			for h.Len() > 0 {
				c++
				v := heap.Pop(&h).(int)
				for _, w := range g[v] {
					if d[w]--; d[w] == 0 {
						if w > v {
							heap.Push(&h, w)
						} else {
							nxt = append(nxt, w)
						}
					}
				}
			}
			for _, v := range nxt {
				heap.Push(&h, v)
			}
			ans++
		}
		if c < n {
			ans = -1
		}
		Fprintln(out, ans)
	}
}

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

package main

import (
	"bufio"
	"container/heap"
	. "fmt"
	"io"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
type hp struct{ sort.IntSlice }
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() interface{}   { a := h.IntSlice; v := a[len(a)-1]; h.IntSlice = a[:len(a)-1]; return v }

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

	var T, n, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		g := make([][]int, n)
		d := make([]int, n)
		h := hp{}
		for w := range d {
			Fscan(in, &d[w])
			for i := 0; i < d[w]; i++ {
				Fscan(in, &v)
				g[v-1] = append(g[v-1], w)
			}
			if d[w] == 0 {
				h.IntSlice = append(h.IntSlice, w)
			}
		}
		ans, c := 0, 0
		for nxt := []int{}; h.Len() > 0; nxt = nil {
			for h.Len() > 0 {
				c++
				v := heap.Pop(&h).(int)
				for _, w := range g[v] {
					if d[w]--; d[w] == 0 {
						if w > v {
							heap.Push(&h, w)
						} else {
							nxt = append(nxt, w)
						}
					}
				}
			}
			h.IntSlice = nxt
			heap.Init(&h)
			ans++
		}
		if c < n {
			ans = -1
		}
		Fprintln(out, ans)
	}
}

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

package main

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

type Scanner struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) *Scanner {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	return &Scanner{scanner}
}

func (s *Scanner) Word() string {
	s.Scan()
	return s.Text()
}

func (s *Scanner) Int() int {
	s.Scan()
	n, _ := strconv.Atoi(s.Text())
	return n
}

func (s *Scanner) Int64() int64 {
	s.Scan()
	n, _ := strconv.ParseInt(s.Text(), 10, 64)
	return n
}

func (s *Scanner) Words(count int) []string {
	words := make([]string, count)
	for i := range words {
		words[i] = s.Word()
	}
	return words
}

func (s *Scanner) Ints(count int) []int {
	ints := make([]int, count)
	for i := range ints {
		ints[i] = s.Int()
	}
	return ints
}

func (s *Scanner) Int64s(count int) []int64 {
	int64s := make([]int64, count)
	for i := range int64s {
		int64s[i] = s.Int64()
	}
	return int64s
}

type Node struct {
	edges []int
	count int
}

type Graph []Node

func NewGraph(nodes int) Graph {
	return make(Graph, nodes)
}

func (g Graph) addEdge(a, b int) {
	g[a].edges = append(g[a].edges, b)
	g[b].count++
}

func topoSort(g Graph) ([]int, bool) {
	q := []int{}
	for i := 0; i < len(g); i++ {
		if g[i].count <= 0 {
			q = append(q, i)
		}
	}

	for i := 0; i < len(q); i++ {
		node := q[i]
		for _, next := range g[node].edges {
			g[next].count--
			if g[next].count == 0 {
				q = append(q, next)
			}
		}
	}

	if len(q) < len(g) {
		return nil, false
	}
	return q, true
}

func solve(g Graph) int {
	order, exists := topoSort(g)
	if !exists {
		return -1
	}

	time := make([]int, len(g))
	for _, node := range order {
		for _, next := range g[node].edges {
			nextTime := time[node]
			if next < node {
				nextTime++
			}

			if nextTime > time[next] {
				time[next] = nextTime
			}
		}
	}

	res := 0
	for _, t := range time {
		if t > res {
			res = t
		}
	}
	return res+1
}

func main() {
	stdin := NewWordScanner(os.Stdin)
	tests := stdin.Int()

	for i := 0; i < tests; i++ {
		n := stdin.Int()
		graph := NewGraph(n)
		for j := 0; j < n; j++ {
			k := stdin.Int()
			for _, node := range stdin.Ints(k) {
				graph.addEdge(node-1, j)
			}
		}
		fmt.Println(solve(graph))
	}
}

package main

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

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

	var T, n, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		g := make([][]int, n)
		d := make([]int, n)
		var cur, nxt []int
		for w := range d {
			Fscan(in, &d[w])
			for i := 0; i < d[w]; i++ {
				Fscan(in, &v)
				g[v-1] = append(g[v-1], w)
			}
			if d[w] == 0 {
				cur = append(cur, w)
			}
		}
		ans, c := 0, 0
		for len(cur) > 0 {
			for len(cur) > 0 {
				c++
				v, cur = cur[0], cur[1:]
				for _, w := range g[v] {
					if d[w]--; d[w] == 0 {
						if w > v {
							cur = append(cur, w)
						} else {
							nxt = append(nxt, w)
						}
					}
				}
			}
			cur, nxt = nxt, nil
			ans++
		}
		if c < n {
			ans = -1
		}
		Fprintln(out, ans)
	}
}

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


package main

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

// fmt.Scanf ???

func main() {
	in := bufio.NewReader(os.Stdin)
	var s, n int
	const maxn int = 200000
	fmt.Fscanf(in, "%d %d\n", &n, &s)
	var a [] int = make([]int, n, n)
	for i:=0; i<n; i++ {
		if i == 0 {
			fmt.Fscanf(in, "%d", &a[i])
		} else {
			fmt.Fscanf(in, " %d", &a[i])
		}

	}

	sort.Ints(a)
	var mid_place int = n / 2
	var sum int64 = 0
	if a[mid_place] <= s {
		for i:=mid_place; i<n; i++ {
			if a[i] >= s {
				break
			}
			sum = sum + int64(s - a[i])
		}
	} else {
		for i:=mid_place; i>=0; i-- {
			if a[i] <= s {
				break
			}
			sum = sum + int64(a[i] - s)
		}
	}

	fmt.Println(sum)
}

package main

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

const N = 200011
const inf = 2000000000
// Runs MergeSort algorithm on a slice single
func MergeSort(slice []int) []int {

	if len(slice) < 2 {
		return slice
	}
	mid := (len(slice)) / 2
	return Merge(MergeSort(slice[:mid]), MergeSort(slice[mid:]))
}

// Merges left and right slice into newly created slice
func Merge(left, right []int) []int {

	size, i, j := len(left)+len(right), 0, 0
	slice := make([]int, size, size)

	for k := 0; k < size; k++ {
		if i > len(left)-1 && j <= len(right)-1 {
			slice[k] = right[j]
			j++
		} else if j > len(right)-1 && i <= len(left)-1 {
			slice[k] = left[i]
			i++
		} else if left[i] < right[j] {
			slice[k] = left[i]
			i++
		} else {
			slice[k] = right[j]
			j++
		}
	}
	return slice
}
func main() {
	in := bufio.NewReader(os.Stdin)
	large := make([]int, 0, N)
	small := make([]int, 0, N)
	var n, s int
	//fmt.Scan(&n, &s)
	fmt.Fscanf(in, "%d %d\n", &n, &s)
	c, l := 0, 0
	for i := 0; i < n; i++ {
		var x int
		if(i == 0) {
			fmt.Fscanf(in, "%d", &x)
		} else {
			fmt.Fscanf(in, " %d", &x)
		}
		//fmt.Scan(&x)
		if x < s {
			small = append(small, x)
			c ++
		} else if x > s {
		//	large = append(large, x)
			large = append(large, x)
			l ++
		}
	}
	x := n / 2 + 1
	var res int64
	res = 0
	if c >= x {
		//small = MergeSort(small)
		sort.Ints(small)
		for i := c - 1; i > x - 2; i -- {
			res += (int64) (s - small[i])
		}

	}
	if l >= x {
		//large = MergeSort(large)
		sort.Ints(large)
		for i, j := 0, l - 1; i < j; i, j = i + 1, j - 1 {
			large[i], large[j] = large[j], large[i]
		}
		for i := l - 1; i > x - 2; i -- {
			res += (int64) (large[i] - s)
		}
	}
	fmt.Println(res)
}

package main

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

const N = 200011
const inf = 2000000000
// Runs MergeSort algorithm on a slice single
func MergeSort(slice []int) []int {

	if len(slice) < 2 {
		return slice
	}
	mid := (len(slice)) / 2
	return Merge(MergeSort(slice[:mid]), MergeSort(slice[mid:]))
}

// Merges left and right slice into newly created slice
func Merge(left, right []int) []int {

	size, i, j := len(left)+len(right), 0, 0
	slice := make([]int, size, size)

	for k := 0; k < size; k++ {
		if i > len(left)-1 && j <= len(right)-1 {
			slice[k] = right[j]
			j++
		} else if j > len(right)-1 && i <= len(left)-1 {
			slice[k] = left[i]
			i++
		} else if left[i] < right[j] {
			slice[k] = left[i]
			i++
		} else {
			slice[k] = right[j]
			j++
		}
	}
	return slice
}
func main() {
	in := bufio.NewReader(os.Stdin)
	large := make([]int, N, N)
	small := make([]int, N, N)
	for i := 0; i < N; i++ {
		large[i] = inf
		small[i] = inf
	}
	var n, s int
	//fmt.Scan(&n, &s)
	fmt.Fscanf(in, "%d %d\n", &n, &s)
	c, l := 0, 0
	for i := 0; i < n; i++ {
		var x int
		if(i == 0) {
			fmt.Fscanf(in, "%d", &x)
		} else {
			fmt.Fscanf(in, " %d", &x)
		}
		//fmt.Scan(&x)
		if x < s {
			small[c] = x
			c ++
		} else if x > s {
		//	large = append(large, x)
			large[l] = x
			l ++
		}
	}
	x := n / 2 + 1
	var res int64
	res = 0
	if c >= x {
		small = MergeSort(small)
		for i := c - 1; i > x - 2; i -- {
			res += (int64) (s - small[i])
		}

	}
	if l >= x {
		large = MergeSort(large)
		for i, j := 0, l - 1; i < j; i, j = i + 1, j - 1 {
			large[i], large[j] = large[j], large[i]
		}
		for i := l - 1; i > x - 2; i -- {
			res += (int64) (large[i] - s)
		}
	}
	fmt.Println(res)
}

package main

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

const inf = 2000000000

var large [200011]int
var small [200011]int
var _tmp [200011]int

func _sort(st, ed int, s []int) {
	// fmt.Println(st, ed, len(s))
	if ed-st < 1 {
		return
	}

	var mid int
	mid = (st + ed) / 2
	_sort(st, mid, s[:])
	_sort(mid+1, ed, s[:])
	for l, r, _idx := st, mid+1, 0; l <= mid || r <= ed; _idx++ {
		if l <= mid && r <= ed {
			if s[l] < s[r] {
				_tmp[_idx] = s[l]
				l++
			} else {
				_tmp[_idx] = s[r]
				r++
			}
		} else if l <= mid {
			_tmp[_idx] = s[l]
			l++
		} else if r <= ed {
			_tmp[_idx] = s[r]
			r++
		}
	}
	// fmt.Println(_tmp[:ed-st+1])
	for i, _idx := st, 0; i <= ed; i, _idx = i+1, _idx+1 {
		s[i] = _tmp[_idx]
	}
	return
}

func main() {

	// x := []int{1, 9, 3, 4, 10, 6, 7, 8, 5, 2}
	// _sort(0, len(x)-1, x[0:len(x)])
	// fmt.Println(x)
	in := bufio.NewReader(os.Stdin)
	var n, s int
	// fmt.Scan(&n, &s)

	fmt.Fscanf(in, "%d %d\n", &n, &s)

	// fmt.Println(n, s)
	c, l := 0, 0
	var x int
	for i := 0; i < n; i++ {
		// fmt.Scan(&x)
		if i == 0 {
			fmt.Fscanf(in, "%d", &x)
		} else {
			fmt.Fscanf(in, " %d", &x)
		}
		// fmt.Println(x)
		if x < s {
			small[c] = x
			c++
		} else if x > s {
			large[l] = x
			l++
		}
	}

	x = n/2 + 1
	var res int64
	res = 0

	if c >= x {
		_sort(0, c-1, small[0:c])
		for i := c - 1; i > x-2; i-- {
			res += (int64)(s - small[i])
		}

	}
	if l >= x {
		_sort(0, l-1, large[0:l])
		for i, j := 0, l-1; i < j; i, j = i+1, j-1 {
			large[i], large[j] = large[j], large[i]
		}
		for i := l - 1; i > x-2; i-- {
			res += (int64)(large[i] - s)
		}
	}
	fmt.Println(res)
}

package main

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

func main()  {

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

	var n int
	var s int64
	fmt.Fscan(in, &n, &s)

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

	lessPos, greaterPos := -1, n
	for i := 0; i < n; i++ {
		if arr[i] < s {
			lessPos = i
		}
		if arr[i] > s && greaterPos == n {
			greaterPos = i
		}
	}

	sum := int64(0)
	for i := n / 2; i <= lessPos; i++ {
		sum += s - arr[i]
	}

	for i := greaterPos; i <= n / 2; i++ {
		sum += arr[i] - s
	}
	fmt.Fprintln(out, sum)

}

package main

import (
	"fmt"
	"sort"
)

const N = 200010
var small, large [N] int
func main() {
	var n, s int
	c , l := 0, 0
	fmt.Scan(&n, &s)
	for i := 0; i < n; i++ {
		var x int
		fmt.Scan(&x)
		if x < s {
			small[c] = x
			c ++
		} else if x > s {
			large[l] = x
			l ++
		}
	}
	x := n / 2 + 1
	//fmt.Println("x is", x)
	var res int64
	res = 0
	//fmt.Println(c, l)
	if c >= x {
		sml := small[0 : c]
		sort.Ints(sml)
		for i := c - 1; i > x - 2; i -- {
			//fmt.Println(s - sml[i])
			res += (int64) (s - sml[i])
		}

	}
	if l >= x {
		var lrg []int = large[0 : l]
		//sort.Ints(lrg)
		k := l
		i := 0
		for k >= x {
			res += (int64) ( lrg[i] - s)
			k -= 1
			i += 1
		}
	}
	fmt.Println(res)
}

package main

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

func main()  {

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

	var n int
	var s int64
	fmt.Fscan(in, &n, &s)

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

	lessPos, greaterPos := 0, n
	for i := 0; i < n; i++ {
		if arr[i] < s {
			lessPos = i
		}
		if arr[i] > s && greaterPos == -1 {
			greaterPos = i
		}
	}

	sum := int64(0)
	for i := n / 2; i <= lessPos; i++ {
		sum += s - arr[i]
	}

	for i := greaterPos; i <= n / 2; i++ {
		sum += arr[i] - s
	}
	fmt.Fprintln(out, sum)

}


package main

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

func solve(j Jam) {
	n, s := j.Int(), []rune(j.Str())
	eights := int64(0)
	for i := int64(0); i+10 < n; i++ {
		if s[i] == '8' {
			eights += 1
		}
	}
	toDelete := n - 11
	moves := toDelete / 2
	petyaDeletesAllEights := eights <= moves
	vasyaDeletesAllNonEights := (toDelete - eights) <= moves
	if vasyaDeletesAllNonEights && !petyaDeletesAllEights {
		j.P("YES\n")
	} else {
		j.P("NO\n")
	}
}

func main() {
	jam, closeFunc := JamNew()
	defer closeFunc()
	solve(jam)
}

type Jam interface {
	Scanner() *bufio.Scanner
	Writer() *bufio.Writer
	Close()

	Str() string
	Int() int64
	Float() float64

	P(format string, values ...interface{})
	PF(format string, values ...interface{})
}

func JamNew() (Jam, func()) {
	if len(os.Args) > 1 {
		panic("running with input file path is not supported")
	}

	var scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 0, 1024*1024), 1024*1024)

	var writer = bufio.NewWriter(os.Stdout)
	jam := &jam{scanner, writer}
	return jam, jam.Close
}

type jam struct {
	sc *bufio.Scanner
	wr *bufio.Writer
}

func (j *jam) Close() {
	if err := j.wr.Flush(); err != nil {
		panic(err)
	}
}

func (j *jam) Scanner() *bufio.Scanner {
	return j.sc
}

func (j *jam) Writer() *bufio.Writer {
	return j.wr
}

func (j *jam) Str() string {
	if !j.sc.Scan() {
		panic("failed to scan next token")
	}

	return j.sc.Text()
}

func (j *jam) Int() int64 {
	if !j.sc.Scan() {
		panic("failed to scan next token")
	}

	res, err := strconv.ParseInt(j.sc.Text(), 10, 64)
	if err != nil {
		panic(err)
	}

	return res
}

func (j *jam) Float() float64 {
	j.sc.Scan()
	res, err := strconv.ParseFloat(j.sc.Text(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

func (j *jam) P(format string, values ...interface{}) {
	_, err := fmt.Fprintf(j.wr, format, values...)
	if err != nil {
		panic(err)
	}
}

func (j *jam) PF(format string, values ...interface{}) {
	_, err := fmt.Fprintf(j.wr, format, values...)
	if err != nil {
		panic(err)
	}
	if err = j.wr.Flush(); err != nil {
		panic(err)
	}
}

//	GoLang shorthand methods for math go below
//	TODO wipe unused methods before submitting

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

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

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

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

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

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

package main

import (
	"fmt"
)

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

	turns := (len - 11) / 2
	vasya, petya := turns, turns

	for i := 0; i < len; i++ {
		val := s[i]

		// petya has moves to remove the 8
		if val == '8' {
			petya--
			if petya < 0 {
				fmt.Println("YES")
				return
			}
			continue
		}

		// vasya must remove non-8
		vasya--
		if vasya < 0 {
			fmt.Println("NO")
			return
		}
	}
}


package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	fmt.Scan(&n)
	a := make([]int, 2*n)
	for i := 0; i < 2*n; i++ {
		fmt.Scan(&a[i])
	}
	b := a[:]
	sort.Ints(b)
	if a[0] == a[2*n-1] {
		fmt.Println(-1)
		return
	}
	for i := 0; i < 2*n-1; i++ {
		fmt.Printf("%v ", a[i])
	}
	fmt.Printf("%v\n", a[2*n-1])
}

package main

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

func main()  {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n , x int
	fmt.Fscan(in,&n)
	a := make([]int,0)
	for i := 0 ; i < 2 * n ;i ++{
		fmt.Fscan(in,&x)
		a = append(a,x)
	}
	sort.Ints(a)
	var left,right int
	for i := 0 ; i <  n ;i ++{
		left += a[i]
	}
	for i := n  ;i < 2 * n ;i ++{
		right += a[i]
	}
	if(left != right){
		for i := 0 ;i  < 2 * n;i++{
			fmt.Fprint(out,a[i]," ")
		}
		return
	}
	fmt.Fprint(out,-1)

}

package main

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

func swap(a, b []int32) bool {
	for i, v := range a {
		if v != b[0] {
			b[0], a[i] = a[i], b[0]
			return true
		}
	}
	for i, v := range b {
		if v != a[0] {
			a[0], b[i] = b[i], a[0]
			return true
		}
	}
	return false
}

func (io *IO) Solve() {
	n := io.Int()
	arr0 := make([]int32, n)
	var sum0 int32
	for i := 0; i < n; i++ {
		arr0[i] = io.Int32()
		sum0 += arr0[i]
	}
	arr1 := make([]int32, n)
	var sum1 int32
	for i := 0; i < n; i++ {
		arr1[i] = io.Int32()
		sum1 += arr1[i]
	}
	if sum0 == sum1 {
		if !swap(arr0, arr1) {
			io.Print("-1\n")
			return
		}
	}
	for _, v := range arr0 {
		io.Printf("%v ", v)
	}
	for _, v := range arr1 {
		io.Printf("%v ", v)
	}
}

func main() {
	NewIO().Solve()
}

type IO struct {
	scanner *bufio.Scanner
	writer  io.Writer
}

func NewIO() *IO {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	return &IO{scanner, os.Stdout}
}

func (io *IO) Skip() {
	io.scanner.Scan()
}

func (io *IO) Int32() (n int32) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + int32(v-'0')
	}
	return
}

func (io *IO) Int() (n int) {
	io.scanner.Scan()
	buf := io.scanner.Bytes()
	for _, v := range buf {
		n = n*10 + int(v-'0')
	}
	return
}

func (io *IO) Print(a interface{}) {
	fmt.Fprint(io.writer, a)
}

func (io *IO) Printf(format string, a interface{}) {
	fmt.Fprintf(io.writer, format, a)
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n := 0
	fmt.Fscanf(in, "%d\n", &n)
	n *= 2
	a := make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &a[i])
	}
	fmt.Fscanf(in, "\n")
	sort.Ints(a)
	if a[0] == a[n-1] {
		fmt.Fprintf(out, "%d\n", -1)
		return
	}
	for i := 0; i < n-1; i++ {
		fmt.Fprintf(out, "%d ", a[i])
	}
	fmt.Fprintf(out, "%d\n", a[n-1])

	return
}
func minstep(a, b byte) int {
	return min(int(a-b+26)%26, int(b-a+26)%26)
}

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

package main

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

func main() {
	reader := getReader()
	for reader.Scan() {
		halfSize, _ := strconv.Atoi(reader.Text())
		reader.Scan()
		vals := strings.Split(reader.Text(), " ")
		arr := make([]int, halfSize*2)
		for i, v := range vals {
			val, _ := strconv.Atoi(v)
			arr[i] = val
		}
		//sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
		sort.Ints(arr)
		var first, second int

		for i := 0; i < halfSize; i++ {
			first = first + arr[i]
		}

		for i := halfSize; i < len(arr); i++ {
			second = second + arr[i]
		}
		if second != first {
			//print array
			for _, v := range arr {
				fmt.Print(v)
				fmt.Print(" ")
			}
		} else {
			//print -1
			fmt.Println(-1)
		}
		//fmt.Println(arr)
		//fmt.Println(first)
		//fmt.Println(second)
	}
}

func getReader() *bufio.Scanner {
	if isOnline() {
		return bufio.NewScanner(os.Stdin)
	}
	file, _ := os.Open("data.txt")
	return bufio.NewScanner(file)
}

func isOnline() bool {
	env := os.Getenv("ONLINE_JUDGE")
	return env != "false"
}

package main

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

func main() {
	var t int
	lastVal := -1
	can := false
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	fmt.Scanf("%d", &t)
	arr := make([]int, 2*t)
	for i := 0; i < 2*t; i++ {
		scanner.Scan()
		arr[i], _ = strconv.Atoi(scanner.Text())
		if lastVal >= 0 && lastVal != arr[i] {
			can = true
		}
		lastVal = arr[i]
	}
	if can {
		sort.Ints(arr)
		for i:=0;i<2*t ;i++  {
			fmt.Printf("%d ",arr[i])
		}
	} else {
		fmt.Println(-1)
	}
}

// Author: sighduck
// URL: https://codeforces.com/contest/1174/problem/A

package main

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

type Solution struct {
	Exists bool
	Array  []int
}

func sum(a []int) int {
	total := 0
	for _, elem := range a {
		total += elem
	}
	return total
}

func Solve(n int, a []int) Solution {
	sort.Ints(a)
	if sum(a[:n]) == sum(a[n:]) {
		return Solution{Exists: false, Array: []int{}}
	}
	return Solution{Exists: true, Array: a}
}

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

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

	solution := Solve(n, a)
	if !solution.Exists {
		fmt.Fprintf(writer, "-1\n")
	} else {
		for _, elem := range solution.Array {
			fmt.Fprintf(writer, "%d ", elem)
		}
		fmt.Fprintf(writer, "\n")
	}
}


package main

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

func solve(x int, arr []int) {
	prefix := make([]int, len(arr)+1)
	for i := range arr {
		prefix[i+1] = prefix[i] + arr[i]
	}
	// key: k, value: the index of the end subarray has maximum sum
	dp := make(map[int]int)
	dp[0] = 0
	for i := 1; i <= len(arr); i++ {
		dp[i] = prefix[i] - prefix[0]
		for j := i; j <= len(arr); j++ {
			dp[i] = int(math.Max(float64(dp[i]), float64(prefix[j]-prefix[j-i])))
		}
	}
	mxSoFar := 0
	for k := 0; k <= len(arr); k++ {
		ans := dp[0]
		for j := k; j <= len(arr); j++ {
			if j == 0 {
				continue
			}
			ans = int(math.Max(float64(ans), float64(dp[j]+k*x)))
		}
		mxSoFar = int(math.Max(float64(mxSoFar), float64(ans)))
		fmt.Printf("%d ", mxSoFar)
	}
	fmt.Println()
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < t; i++ {
		scanner.Scan()
		N, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		k, _ := strconv.Atoi(scanner.Text())
		arr := make([]int, N)
		for j := 0; j < N; j++ {
			scanner.Scan()
			arr[j], _ = strconv.Atoi(scanner.Text())
		}
		solve(k, arr)
	}

}

package main

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

/*

 */

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

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

	for i := range a {
		scanf("%d", &a[i])
		prefixSum[i] = int64(a[i])
		if i > 0 {
			prefixSum[i] += prefixSum[i-1]
		}
	}
	scanf("\n")

	lenToMaxSum := make([]int64, n+1)

	for l := 1; l <= n; l++ {
		lenToMaxSum[l] = int64(minInt)

		for i := 0; i <= n-l; i++ {
			sum := prefixSum[i+l-1] - prefixSum[i] + int64(a[i])

			if sum > lenToMaxSum[l] {
				lenToMaxSum[l] = sum
			}
		}
	}

	for k := 0; k <= n; k++ {
		maxFk := int64(0)

		for l, maxSum := range lenToMaxSum {
			maxFk = max(maxFk, maxSum+int64(min(k, l))*int64(x))
		}

		fmt.Fprintf(writer, "%d ", maxFk)
	}
	fmt.Fprintln(writer)
}

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

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

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

const maxUint = ^uint(0)

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

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

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

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

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

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

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

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

	return minNumIdx
}

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

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

	return maxNumIdx
}

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

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

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

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

	return true
}

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

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

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

	num = abs(num)

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

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

//goland:noinspection GoUnusedFunction
func findSet(parent []int, v int) int {
	if v == parent[v] {
		return v
	}

	parent[v] = findSet(parent, parent[v])

	return parent[v]
}

//goland:noinspection GoUnusedFunction
func unionSets(parent []int, v, u int) {
	v = findSet(parent, v)
	u = findSet(parent, u)

	if v != u {
		parent[v] = u
	}
}

package main

import "fmt"

func Max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func main() {
	var t int
	fmt.Scan(&t)
	for tt := 0; tt < t; tt++ {
		var n, x, v int
		fmt.Scan(&n, &x)
		res := make([]int, n+1, n+1)
		dp := make([][]int, 2)
		dp[0] = make([]int, 5005, 5005)
		dp[1] = make([]int, 5005, 5005)
		cur := 0
		for i := 1; i <= n; i++ {
			fmt.Scan(&v)
			for k := 0; k <= i; k++ {
				if k != i {
					//fmt.Printf("%d %d\n", dp[cur^1][k], v)
					dp[cur][k] = Max(dp[cur][k], dp[cur^1][k]+v)
				}
				if k > 0 {
					dp[cur][k] = Max(dp[cur][k], dp[cur^1][k-1]+v+x)
				}
				//fmt.Printf("i:%d k:%d val:%d\n", i, k, dp[cur][k])
				res[k] = Max(res[k], dp[cur][k])
			}
			dp[cur^1] = make([]int, 5005, 5005)
			cur ^= 1

		}

		ans := 0
		for d := 0; d <= n; d++ {
			if d != 0 {
				fmt.Print(" ")
			}
			ans = Max(ans, res[d])
			fmt.Printf("%d", ans)
		}
		fmt.Println("")
	}
}

package main

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

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

	var T, n, x, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &x)
		s := make([]int, n+1)
		for i := 0; i < n; i++ {
			Fscan(in, &v)
			s[i+1] = s[i] + v
		}
		mx := make([]int, n+1)
		mx[n] = s[n]
		for l := n - 1; l >= 0; l-- {
			mx[l] = mx[l+1]
			for i := l; i <= n; i++ {
				mx[l] = max(mx[l], s[i]-s[i-l])
			}
		}
		ans := 0
		for i, v := range mx {
			ans = max(ans, v+i*x)
			Fprint(out, ans, " ")
		}
		Fprintln(out)
	}
}

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

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

package main

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

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

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

	c := 0
	fmt.Fscan(in, &c)
	n, x := int64(0), int64(0)
	for ; c > 0; c-- {
		fmt.Fscan(in, &n, &x)
		arr := make([]int64, n)
		prefix := make([]int64, n+1)
		for i := range arr {
			fmt.Fscan(in, &arr[i])
			prefix[i+1] = prefix[i] + arr[i]
		}
		p := make([]int64, n+1)
		for i := range p {
			p[i] = math.MinInt32
		}
		p[0] = 0
		for i := int64(1); i <= n; i++ {
			p[1] = max(p[1], arr[i-1])
			for j := i - 1; j > 0; j-- {
				p[i-j+1] = max(p[i-j+1], prefix[i]-prefix[j-1])
			}
		}
		//dp	dp[i]表示可以替换i个x时候的最大值
		dp := make([]int64, n+1)
		for i := range p { //初始化dp数组dp[0]
			dp[0] = max(dp[0], p[i])
		}
		fmt.Fprint(out, dp[0], " ")
		for i := int64(1); i <= n; i++ {
			dp[i] = dp[i-1]
			tmp := i * x
			for j := i; j <= n; j++ {
				dp[i] = max(dp[i], p[j]+tmp)
			}
			fmt.Fprint(out, dp[i], " ")
		}
		fmt.Fprintln(out)
	}
}

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

package main

import "fmt"

var a, b, c [5005]int

func main() {
	var t, n, m, x, maxn int
	fmt.Scan(&t)
	for cs := 0; cs < t; cs++ {
		fmt.Scan(&n, &m)
		for i := 1; i <= n; i++ {
			b[i] = -0x3f3f3f3f
			maxn = -0x3f3f3f3f
		}
		for i := 1; i <= n; i++ {
			fmt.Scan(&x)
			a[i] = 0
			for j := 1; j <= i; j++ {
				a[j] += x
				b[i-j+1] = max(b[i-j+1], a[j])
			}
		}
		for i := n; i >= 0; i-- {
			maxn = max(maxn, b[i])
			c[i] = maxn + m*i
		}
		for i := 1; i <= n; i++ {
			c[i] = max(c[i], c[i-1])
		}
		for i := 0; i <= n; i++ {
			fmt.Printf("%d ", c[i])
		}
		fmt.Printf("\n")
	}
}
func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}


package main

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

var sizes = []string{"S", "M", "L", "XL", "XXL", "XXXL"}

func main() {
	var counts [6]int

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	for i, x := range strings.Split(scanner.Text(), " ") {
		counts[i], _ = strconv.Atoi(x)
	}

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	result := make([]string, n)
	tmpShirts := make([]int, n)
	for i := range tmpShirts {
		tmpShirts[i] = -1
	}
	possible := true
	for i := 0; i < n; i++ {
		scanner.Scan()
		splits := strings.Split(scanner.Text(), ",")
		if len(splits) == 1 {
			sizeIndex := getIndex(splits[0])
			counts[sizeIndex]--
			if counts[sizeIndex] < 0 {
				possible = false
				break
			}
			result[i] = splits[0]
		} else {
			tmpShirts[i] = getIndex(splits[0])
		}
	}

	for i := 0; i < 5; i++ {
		for j := 0; j < n; j++ {
			if tmpShirts[j] == i {
				if counts[i] > 0 {
					counts[i]--
					result[j] = sizes[i]
				} else if counts[i+1] > 0 {
					counts[i+1]--
					result[j] = sizes[i+1]
				} else {
					possible = false
					break
				}
			}
		}
	}

	if possible {
		fmt.Println("YES")
		for _, s := range result {
			fmt.Println(s)
		}
	} else {
		fmt.Println("NO")
	}
}

func getIndex(s string) int {
	for i, x := range sizes {
		if x == s {
			return i
		}
	}
	return 0
}

package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer

var shirtSizes = []string{"S", "M", "L", "XL", "XXL", "XXXL"}

func getId(s string) int {
	for i := 0; i < len(shirtSizes); i++ {
		if shirtSizes[i] == s {
			return i
		}
	}
	return -1
}

func no() {
	fmt.Fprintf(bufout, "NO\n")
}

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	cnt := make([]int, 6)
	for i := 0; i < 6; i++ {
		fmt.Fscanf(bufin, "%d\n", &cnt[i])
	}

	var n int
	fmt.Fscanf(bufin, "%d\n", &n)

	res := make([]string, n)
	pos := make([][]int, 5)
	var cs string

	for i := 0; i < n; i++ {
		fmt.Fscanf(bufin, "%s\n", &cs)

		ss := strings.Split(cs, ",")
		id := getId(ss[0])

		if len(ss) == 1 {
			if cnt[id] > 0 {
				res[i] = ss[0]
				cnt[id]--
			} else {
				no()
				return
			}
		} else {
			pos[id] = append(pos[id], i)
		}
	}

	for i := 0; i < 5; i++ {
		l := len(pos[i])
		for j := 0; j < l; j++ {
			p := pos[i][j]
			if cnt[i] > 0 {
				res[p] = shirtSizes[i]
				cnt[i]--
			} else if cnt[i+1] > 0 {
				res[p] = shirtSizes[i+1]
				cnt[i+1]--
			} else {
				no()
				return
			}
		}
	}

	fmt.Fprintf(bufout, "YES\n")
	for i := 0; i < n; i++ {
		fmt.Fprintf(bufout, "%s\n", res[i])
	}
}

package main

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

var scanner *bufio.Scanner

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

func display(a []int) {
  fmt.Printf("%d", a[0])
  for i := 1; i < len(a); i++ {
    fmt.Printf(" %d", a[i])
  }
  fmt.Printf("\n")
}

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

  names := []string{ "S", "M", "L", "XL", "XXL", "XXXL" }
  namePos := map[string]int{}
  for i, name := range names {
    namePos[name] = i
  }
  counts := make([]int, 6)
  for i := range counts {
    counts[i] = getI()
  }
  pending := make([][]int, 5)
  for i := range pending {
    pending[i] = []int{}
  }
  n := getI()
  shirts := make([]string, n)
  for i := 0; i < n; i++ {
    s := getS()
    parts := strings.Split(s, ",")
    if len(parts) == 1 {
      name := parts[0]
      shirts[i] = name
      counts[namePos[name]]--
    } else {
      pos := namePos[parts[0]]
      pending[pos] = append(pending[pos], i)
    }
  }
  //display(counts)
  okay := true
  for i := range pending {
    for j := i; j <= i + 1; j++ {
      name := names[j]
      for len(pending[i]) > 0 && counts[j] > 0 {
        shirts[pending[i][0]] = name
        pending[i] = pending[i][1:]
        counts[j]--
      }
    }
    if len(pending[i]) > 0 || counts[i] < 0 {
      okay = false
      break
    }
  }
  //display(counts)
  if counts[5] < 0 {
    okay = false
  }
  if !okay {
    writer.WriteString("NO\n")
  } else {
    writer.WriteString("YES\n")
    for _, name := range shirts {
      writer.WriteString(fmt.Sprintf("%s\n", name))
    }
  }
}