package main

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

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

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

	n, m := next(), next()

	abc := "abcdefghijklmnopqrstuvwxyz"
	countn, countm := 0, 0
	totalArea := 0

	for i:=0; i<len(abc); i++ {
		countn = strings.Count(n, string(abc[i:i+1]))
		countm = strings.Count(m, string(abc[i:i+1]))

		if countm > 0 {
			if countn == 0 {
				println(-1)
				return
			}

			if countn > countm {
				totalArea += countm
			} else {
				totalArea += countn
			}
		}
	}

	println(totalArea)
}

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

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func nextFloat() float64 {
	n, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

// 408B-mic
package main

import (
    "fmt"
    "math"
)

func main() {
    var s1, s2 string
    var a, c [10000]int
    var ans float64
    fmt.Scan(&s1, &s2)
    for i := 0; i < len(s1); i++ {
        a[s1[i]-'a']++
    }
    for i := 0; i < len(s2); i++ {
        c[s2[i]-'a']++
    }
    for i := 0; i < 26; i++ {
        if a[i] == 0 && c[i] > 0 {
            fmt.Println("-1")
            return
        } else {
            ans += math.Min(float64(a[i]), float64(c[i]))
        }
    }
    fmt.Println(ans)
}

package main

import "os"
import "bufio"
import "strconv"

func main(){
	var n string
	var m string
	r := bufio.NewScanner(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	r.Scan()
	n = r.Text()
	a := make([]int, 26)
	for i:=0; i<len(n); i++ {
		a[n[i]-'a'] += 1
	}
	r.Scan()
	m = r.Text()
	b := make([]int, 26)
	for i:=0; i<len(m); i++ {
		b[m[i]-'a'] += 1
	}
	count := 0
	for i:=0; i<26; i++ {
		if a[i] != 0 {
			if a[i] >= b[i] {
				count += b[i]
			} else {
				count += a[i]
			}
		} else {
			if b[i] != 0 {
				w.WriteString("-1\n")
				w.Flush()
				return
			}
		}
	}
	w.WriteString(strconv.Itoa(count)+"\n")
	w.Flush()
}

package main

import (
	"fmt"
	"bufio"
	"os"
	//"strconv"
	//"unicode"
	//"strings"
	//"math"
)
func min(a,b int) int{
	if a<b {
		return a
	} else {
		return b
	}
}

func main() {
    var s,t string
    var a,b [30]int
    res:=0
    input := bufio.NewReader(os.Stdin)
    fmt.Fscan(input,&s)
    fmt.Fscan(input,&t)
    //input.Split(bufio.ScanWords)
    for i:=0;i<len(s);i++ {
    	a[s[i]-'a']++
    }
    for i:=0;i<len(t);i++ {
    	b[t[i]-'a']++
    }
    for i:=0;i<30;i++ {
    	if a[i]==0 && b[i]>0 {
    		fmt.Println("-1")
    		return
    	}
    	res+=min(a[i],b[i])
    }
    fmt.Println(res)
}

package main

import "fmt"

func main() {
    var n,m string
    var cnt [26]int
    var f [26]bool
    fmt.Scan(&n)
    fmt.Scan(&m)
    for i := 0; i < len(n); i++ { cnt[n[i]-'a']++; f[n[i]-'a'] = true }
    ans := 0
    for i := 0; i < len(m); i++ {
        if cnt[m[i]-'a'] > 0 {
            cnt[m[i]-'a']--
            ans++
        } else if !f[m[i]-'a'] {
            ans = -1
            break
        }
    }
    fmt.Println(ans)
}

/*********************************************************************************
*     File Name           :     B.go
*     Created By          :     YIMMON, yimmon.zhuang@gmail.com
*     Creation Date       :     [2014-04-06 17:08]
*     Last Modified       :     [2014-04-06 17:23]
*     Description         :
**********************************************************************************/

package main

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

func Count(a []byte) (ret map[byte]int) {
    ret = make(map[byte]int)
    for _, i := range a {
        ret[i]++
    }
    return
}

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

func Solve(n, m []byte) int {
    nn := Count(n)
    mm := Count(m)
    ans := 0
    for k, v := range mm {
        if vv, ok := nn[k]; !ok {
            return -1
        } else {
            ans += Min(v, vv)
        }
    }
    return ans
}

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

    fmt.Println(Solve([]byte(in.Next()), []byte(in.Next())))
}

type Scanner struct {
    *bufio.Scanner
}

var in = Scanner{bufio.NewScanner(os.Stdin)}

func (self *Scanner) Next() string {
    if !self.Scan() {
        panic(self.Err())
    }
    return self.Text()
}

package main

import (
	"fmt"
)

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func main() {
	var inStr string
	fmt.Scan(&inStr)
	in := map[rune]int{}
	for _, c := range inStr {
		in[c] += 1
	}
	var outStr string
	fmt.Scan(&outStr)
	out := map[rune]int{}
	for _, c := range outStr {
		out[c] += 1
	}
	ans := 0
	for k, outV := range out {
		if inV, ok := in[k]; ok {
			ans += min(inV, outV)
		} else {
			ans = -1
			break
		}
	}
	fmt.Println(ans)
}

// 408B
package main

import (
	"fmt"
)

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

func main() {
	var a, b [30]int
	var s string
	fmt.Scan(&s)
	for i := 0; i < len(s); i++ {
		a[s[i]-'a']++
	}
	fmt.Scan(&s)
	for i := 0; i < len(s); i++ {
		b[s[i]-'a']++
	}
	ans := 0
	for i := 0; i < 30; i++ {
		if b[i] > 0 && a[i] == 0 {
			fmt.Print(-1)
			return
		}
		ans += min(a[i], b[i])
	}
	fmt.Print(ans)
}

package main

import "fmt"

func main() {
    var n,m string
    var cnt [26]int
    fmt.Scan(&n)
    fmt.Scan(&m)
    for i := 0; i < len(n); i++ { cnt[n[i]-'a']++ }
    ans := 0
    for i := 0; i < len(m); i++ {
        if cnt[m[i]-'a'] > 0 {
            cnt[m[i]-'a']--
            ans++
        }
    }
    if ans == 0 { ans = -1 }
    fmt.Println(ans)
}

/*********************************************************************************
*     File Name           :     B.go
*     Created By          :     YIMMON, yimmon.zhuang@gmail.com
*     Creation Date       :     [2014-04-06 17:08]
*     Last Modified       :     [2014-04-06 17:23]
*     Description         :
**********************************************************************************/

package main

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

func Count(a []byte) (ret map[byte]int) {
    ret = make(map[byte]int)
    for _, i := range a {
        ret[i]++
    }
    return
}

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

func Solve(n, m []byte) int {
    sum := len(n)
    nn := Count(n)
    mm := Count(m)
    ans := 0
    for k, v := range mm {
        if vv, ok := nn[k]; !ok {
            return -1
        } else {
            ans += Min(v, vv)
        }
    }
    if ans == sum {
        return -1
    }
    return ans
}

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

    fmt.Println(Solve([]byte(in.Next()), []byte(in.Next())))
}

type Scanner struct {
    *bufio.Scanner
}

var in = Scanner{bufio.NewScanner(os.Stdin)}

func (self *Scanner) Next() string {
    if !self.Scan() {
        panic(self.Err())
    }
    return self.Text()
}


package main

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

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

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

func qwq() {
	var n int
	fmt.Fscan(in, &n)
	a := make([]int, n)
	b := make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	for i := range a {
		fmt.Fscan(in, &b[i])
	}
	for i := range a {
		l, r := 0, i
		for l < r {
			mid := (l + r) / 2
			if b[mid] >= a[i] {
				r = mid
			} else {
				l = mid + 1
			}
		}
		fmt.Fprint(out, b[l]-a[i], " ")
	}
	fmt.Fprintln(out)
	ht := map[int]int{}
	l := 1
	for i := 1; i < n; i++ {
		if a[i] > b[i-1] {
			l = i + 1
		} else {
			ht[l] = i
		}
	}
	// fmt.Fprintln(out, ht)

	pairs := [][2]int{}
	for k, v := range ht {
		pairs = append(pairs, [2]int{k, v})
	}
	sort.Slice(pairs, func(i, j int) bool {
		return pairs[i][0] < pairs[j][0]
	})
	check := func(l, r int) bool {
		i, j := 0, len(pairs)-1
		for i < j {
			mid := (i + j + 1) / 2
			if pairs[mid][0] <= l {
				i = mid
			} else {
				j = mid - 1
			}
		}
		return i < len(pairs) && pairs[i][0] <= l && r <= pairs[i][1]
	}
	for i := range a {
		l, r := i, n-1
		for l < r {
			mid := (l + r + 1) / 2
			if check(i+1, mid) {
				l = mid
			} else {
				r = mid - 1
			}
		}
		fmt.Fprint(out, b[l]-a[i], " ")
	}
	fmt.Fprintln(out)

}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

var _, _, _ = main, rw, do

func main() {
	r, w := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer w.Flush()
	rw(r, w)
}

func rw(r io.Reader, w io.Writer) {
	var t int
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		var n int
		fmt.Fscan(r, &n)
		a := make([]int, n)
		for i := range a {
			fmt.Fscan(r, &a[i])
		}
		b := make([]int, n)
		for i := range b {
			fmt.Fscan(r, &b[i])
		}
		w1 := do(n, a, b)
		for _, v := range w1 {
			for i, v := range v {
				if i > 0 {
					fmt.Fprint(w, " ")
				}
				fmt.Fprint(w, v)
			}
			fmt.Fprintln(w)
		}
	}
}

func do(n int, a, b []int) [2][]int {
	var d [2][]int
	d[0] = make([]int, n)
	d[1] = make([]int, n)
	j := n - 1
	k := n - 1
	x := n
	for i := n - 1; i >= 0; i-- {
		for j >= 0 && b[j] >= a[i] {
			j--
		}
		d[0][i] = b[j+1] - a[i]
		d[1][i] = b[k] - a[i]
		if k-j == x-i {
			x = i
			k = j
		}
	}
	return d
}

package main

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

var P107 int = 1000000007

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

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

func Abs(a int) int {
	return Max(a, -a)
}

func Gcd(a, b int) int {
	min := Min(a, b)
	max := Max(a, b)
	if min == 0 {
		return max
	}

	for max%min != 0 {
		min, max = max%min, min
	}

	return min
}

func Lcm(a, b int) int {
	return a / Gcd(a, b) * b
}

// var facs []int = make([]int, X + 1)
func FacP(n, p int) int {
	// if facs[n] != 0 {
	//   return facs[n]
	// }
	result := 1
	// facs[0] = 1
	for i := 1; i <= n; i++ {
		result = (result * i) % p
		// facs[i] = result
	}

	return result
}

func ExpP(a, x, p int) int {
	if x == 0 {
		return 1
	}

	if x%2 == 0 {
		half := ExpP(a, x/2, p)
		return (half * half) % p
	} else {
		return ((a % p) * ExpP(a, x-1, p)) % p
	}
}

func CombP(n, c, p int) int {
	// If CombP be called multiple times, you should optimize FacP using memoization.
	return (FacP(n, p) * ((ExpP(FacP(n-c, p), p-2, p) * ExpP(FacP(c, p), p-2, p)) % p)) % p
}

func Solve(reader *bufio.Reader) {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())

	for _i := 0; _i < t; _i++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		aArr := make([]int, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			aArr[i], _ = strconv.Atoi(scanner.Text())
		}
		bArr := make([]int, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			bArr[i], _ = strconv.Atoi(scanner.Text())
		}

		minArr := make([]int, n)
		for i := 0; i < n; i++ {
			start := 0
			end := n - 1

			last := 0
			for start <= end {
				c := (start + end) / 2
				if bArr[c] == aArr[i] {
					last = c
					break
				} else if bArr[c] > aArr[i] {
					end = c - 1
					last = c
				} else {
					start = c + 1
				}
			}
			minArr[i] = bArr[last] - aArr[i]
		}

		for i := 0; i < n; i++ {
			wr.WriteString(strconv.Itoa(minArr[i]) + " ")
		}
		wr.WriteString("\n")

		maxArr := make([]int, n)
		start := 0
		for i := 1; i < n; i++ {
			if aArr[i] > bArr[i-1] {
				for j := start; j < i; j++ {
					maxArr[j] = bArr[i-1] - aArr[j]
				}
				start = i
			}
		}
		for j := start; j < n; j++ {
			maxArr[j] = bArr[n-1] - aArr[j]
		}

		for i := 0; i < n; i++ {
			wr.WriteString(strconv.Itoa(maxArr[i]) + " ")
		}
		wr.WriteString("\n")
	}
}

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

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	cases := readInt(input)
	for asdf := 0; asdf < cases; asdf++ {
		n := readInt(input)
		a := readArrInt(input)
		b := readArrInt(input)
		solve(n, a, b)
	}
}

func solve(n int, a, b []int) {

	index := 0
	mins := []int{}
	for i := 0; i < n; i++ {
		for b[index] < a[i] {
			index++
		}
		mins = append(mins, b[index]-a[i])
	}
	index = n - 1
	magic := []int{}
	for i := n - 1; i >= 0; i-- {
		for index >= 0 && a[i] <= b[index] {
			index--
		}
		magic = append(magic, n-index-1)
	}
	maxs := []int{}
	index = n - 1
	for i := 0; i < n; i++ {
		maxs = append(maxs, b[index]-a[n-i-1])
		if magic[i] == i+1 {
			index = n - i - 2
		}
	}
	for i := 0; i < n; i++ {
		fmt.Print(mins[i], " ")
	}
	fmt.Println()
	for i := n - 1; i >= 0; i-- {
		fmt.Print(maxs[i], " ")
	}
	fmt.Println()
}

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 readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

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

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

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

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

package main

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

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

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int, n)
		for i := range b {
			Fscan(in, &b[i])
		}
		for i, j, max := n-1, n-1, b[n-1]; i >= 0; i-- {
			for ; j >= 0 && b[j] >= a[i]; j-- {
			}
			min := b[j+1]
			b[i] = max - a[i]
			a[i] = min - a[i]
			if j+1 == i && j >= 0 {
				max = b[j]
			}
		}
		for _, v := range a {
			Fprint(out, v, " ")
		}
		Fprintln(out)
		for _, v := range b {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}
}

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

package main

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

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

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int, n)
		for i := range b {
			Fscan(in, &b[i])
		}
		j := n - 1
		min, max := b[j], b[j]
		for i := n - 1; i >= 0; i-- {
			for ; j >= 0 && b[j] >= a[i]; j-- {
				min = b[j]
			}
			b[i] = max - a[i]
			a[i] = min - a[i]
			if j+1 == i && j >= 0 {
				max = b[j]
			}
		}
		for _, v := range a {
			Fprint(out, v, " ")
		}
		Fprintln(out)
		for _, v := range b {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}
}

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

package main

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

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

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}
func solve() {
	var n int
	scan(&n)
	a := make([]int, n)
	b := make([]int, n)
	for i := 0; i < n; i++ {
		scan(&a[i])
	}
	for i := 0; i < n; i++ {
		scan(&b[i])
	}
	sort.Slice(b, func(i, j int) bool {
		return b[i] < b[j]
	})
	idx := 0
	for i := 0; i < n; i++ {
		for idx < n && b[idx] < a[i] {
			idx++
		}
		printf("%d ", b[idx]-a[i])
	}
	printf("\n")
	idx = n - 1
	ans := make([]int, n)
	ans[n-1] = b[n-1] - a[n-1]
	for i := n - 2; i >= 0; i-- {
		if a[i+1] > b[i] { // just need to compare to the previous one, then get the maximum sofar
			idx = i
		}
		ans[i] = b[idx] - a[i]

	}
	for _, e := range ans {
		printf("%d ", e)
	}
	printf("\n")
}
func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

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

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

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

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

		var p1 int
		for _, v := range aa {
			for v > bb[p1] {
				p1++
			}
			fmt.Fprintf(out, "%d ", bb[p1]-v)
		}
		fmt.Fprintln(out)
		p1 = n - 1
		cc := make([]int, n)
		cc[n-1] = bb[n-1] - aa[n-1]
		for i := n - 2; i >= 0; i-- {
			if aa[i+1] > bb[i] {
				p1 = i
			}
			cc[i] = bb[p1] - aa[i]
		}

		for _, v := range cc {
			fmt.Fprintf(out, "%d ", v)
		}
		fmt.Fprintln(out)
	}
}

package main

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

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

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

func qwq() {
	var n int
	fmt.Fscan(in, &n)
	a := make([]int, n)
	b := make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	for i := range a {
		fmt.Fscan(in, &b[i])
	}
	for i := range a {
		l, r := 0, i
		for l < r {
			mid := (l + r) / 2
			if b[mid] >= a[i] {
				r = mid
			} else {
				l = mid + 1
			}
		}
		fmt.Fprint(out, b[l]-a[i], " ")
	}
	fmt.Fprintln(out)
	ht := map[int]int{}
	l := 1
	for i := 1; i < n; i++ {
		if a[i] > b[i-1] {
			l = i
		} else {
			ht[l] = i
		}
	}
	// fmt.Fprintln(out, ht)
	if n-1 > ht[l] {
		ht[l] = n - 1
	}
	pairs := [][2]int{}
	for k, v := range ht {
		pairs = append(pairs, [2]int{k, v})
	}
	sort.Slice(pairs, func(i, j int) bool {
		return pairs[i][0] < pairs[j][0]
	})
	check := func(l, r int) bool {
		i, j := 0, len(pairs)-1
		for i < j {
			mid := (i + j + 1) / 2
			if pairs[mid][0] <= l {
				i = mid
			} else {
				j = mid - 1
			}
		}
		return pairs[i][0] <= l && r <= pairs[i][1]
	}
	for i := range a {
		l, r := i, n-1
		for l < r {
			mid := (l + r + 1) / 2
			if check(i+1, mid) {
				l = mid
			} else {
				r = mid - 1
			}
		}
		fmt.Fprint(out, b[l]-a[i], " ")
	}
	fmt.Fprintln(out)

}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

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

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

func qwq() {
	var n int
	fmt.Fscan(in, &n)
	a := make([]int, n)
	b := make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	for i := range a {
		fmt.Fscan(in, &b[i])
	}
	for i := range a {
		l, r := 0, i
		for l < r {
			mid := (l + r) / 2
			if b[mid] >= a[i] {
				r = mid
			} else {
				l = mid + 1
			}
		}
		fmt.Fprint(out, b[l]-a[i], " ")
	}
	fmt.Fprintln(out)
	ht := map[int]int{}
	l := 1
	for i := 1; i < n; i++ {
		if a[i] > b[i-1] {
			l = i
		} else {
			ht[l] = i
		}
	}

	check := func(l, r int) bool {
		return r <= ht[l]
	}
	for i := range a {
		l, r := i, n-1
		for l < r {
			mid := (l + r + 1) / 2
			if check(i+1, mid) {
				l = mid
			} else {
				r = mid - 1
			}
		}
		fmt.Fprint(out, b[l]-a[i], " ")
	}
	fmt.Fprintln(out)

}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

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

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

func qwq() {
	var n int
	fmt.Fscan(in, &n)
	a := make([]int, n)
	b := make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	for i := range a {
		fmt.Fscan(in, &b[i])
	}
	for i := range a {
		l, r := 0, i
		for l < r {
			mid := (l + r) / 2
			if b[mid] >= a[i] {
				r = mid
			} else {
				l = mid + 1
			}
		}
		fmt.Fprint(out, b[l]-a[i], " ")
	}
	fmt.Fprintln(out)
	for i := range a {
		l, r := i, n-1
		for l < r {
			mid := (l + r + 1) / 2
			ll, rr := i, mid
			for ll < rr {
				mid2 := (ll + rr + 1) / 2
				if a[mid2] <= b[i] {
					ll = mid2
				} else {
					rr = mid2 - 1
				}
			}
			if a[mid] <= b[ll] {
				l = mid
			} else {
				r = mid - 1
			}
		}
		fmt.Fprint(out, b[l]-a[i], " ")
	}
	fmt.Fprintln(out)

}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

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

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

func qwq() {
	var n int
	fmt.Fscan(in, &n)
	a := make([]int, n)
	b := make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	for i := range a {
		fmt.Fscan(in, &b[i])
	}
	for i := range a {
		l, r := 0, i
		for l < r {
			mid := (l + r) / 2
			if b[mid] >= a[i] {
				r = mid
			} else {
				l = mid + 1
			}
		}
		fmt.Fprint(out, b[l]-a[i], " ")
	}
	fmt.Fprintln(out)
	for i := range a {
		l, r := i, n-1
		for l < r {
			mid := (l + r + 1) / 2
			ll, rr := i, mid-1
			for ll < rr {
				mid2 := (ll + rr + 1) / 2
				if a[mid2] <= b[mid] {
					ll = mid2
				} else {
					rr = mid2 - 1
				}
			}
			if a[mid] <= b[ll] {
				l = mid
			} else {
				r = mid - 1
			}
		}
		fmt.Fprint(out, b[l]-a[i], " ")
	}
	fmt.Fprintln(out)

}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

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

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}
func solve() {
	var n int
	scan(&n)
	a := make([]int, n)
	b := make([]int, n)
	for i := 0; i < n; i++ {
		scan(&a[i])
	}
	for i := 0; i < n; i++ {
		scan(&b[i])
	}
	sort.Slice(b, func(i, j int) bool {
		return b[i] < b[j]
	})
	idx := 0
	for i := 0; i < n; i++ {
		for idx < n && b[idx] < a[i] {
			idx++
		}
		printf("%d ", b[idx]-a[i])
	}
	printf("\n")
	i := 0
	for idx := 0; idx < n; idx++ {
		for i < n && b[idx] >= a[i] {
			i++
		}
		i--
		if i < idx {
			i = idx
		}
		printf("%d ", b[i]-a[idx])
	}
	printf("\n")
}
func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

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

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

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

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

		var p1 int
		cc := make([]int, n)
		for i, v := range aa {
			for v > bb[p1] {
				p1++
			}
			cc[i] = bb[p1] - v
		}

		dd := make([]int, n)
		p1 = n - 1
		dd[n-1] = bb[p1] - aa[n-1]
		for i := n - 2; i >= 0; i-- {
			if cc[i] <= cc[i+1] {
				p1--
			}
			dd[i] = bb[p1] - aa[i]
		}

		for _, v := range cc {
			fmt.Fprintf(out, "%d ", v)
		}
		fmt.Fprintln(out)
		for _, v := range dd {
			fmt.Fprintf(out, "%d ", v)
		}
		fmt.Fprintln(out)
	}
}


package main

import (
	"fmt"
	"strings"
)

func main() {
	var num int
	fmt.Scan(&num)
	depths := map[string]int{"polycarp": 1}
	max := 1

	for i := 0; i < num; i++ {
		var target, action, subject string
		fmt.Scan(&target)
		fmt.Scan(&action)
		fmt.Scan(&subject)

		depth := depths[strings.ToLower(subject)] + 1
		depths[strings.ToLower(target)] = depth
		if depth > max {
			max = depth
		}
	}
	fmt.Println(max)
}

// main
package main

import (
    "fmt"
    "strings"
)

func main() {
    var n int
    fmt.Scan(&n)
    m := make(map[string]int)
    m["polycarp"] = 1
    ans := 1
    for n > 0 {
        n--
        var n1, r, n2 string
        fmt.Scan(&n1, &r, &n2)
        n1 = strings.ToLower(n1)
        n2 = strings.ToLower(n2)
        m[n1] = m[n2] + 1
        if m[n1] > ans {
            ans = m[n1]
        }
    }
    //  fmt.Println(m)
    fmt.Println(ans)
}


package main

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

var in = bufio.NewScanner(os.Stdin)

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

type fenwick []int

func (f fenwick) get(x int) int {
	res := f[x]
	for x &= x - 1; x > 0; x &= x - 1 {
		res += f[x]
	}
	return res
}

func (f fenwick) add(x, n int) {
	for x < len(f) {
		f[x] += n
		x += x & (-x)
	}
}

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

	n := readInt()
	k := readInt()
	a := readInt()
	b := readInt()

	bt := make(fenwick, n+1)
	at := make(fenwick, n+1)

	for q := readInt(); q > 0; q-- {
		if readInt() == 1 {
			di := readInt()
			ai := readInt()

			cur := at.get(di) - at.get(di-1)
			aa := ai
			if aa+cur > a {
				aa = a - cur
			}
			if aa > 0 {
				at.add(di, aa)
			}

			cur = bt.get(di) - bt.get(di-1)
			ab := ai
			if ab+cur > b {
				ab = b - cur
			}
			if ab > 0 {
				bt.add(di, ab)
			}

			continue
		}
		p := readInt()
		fmt.Fprintln(out, bt.get(p-1)+at.get(n)-at.get(p+k-1))
	}
}

package main

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

func main() {
	defer out.Flush()
	n := in.NextInt()
	k := in.NextInt()
	maxAfter := int64(in.NextInt())
	maxBefore := int64(in.NextInt())
	Q := in.NextInt()
	before := NewFenwick(n)
	after := NewFenwick(n)
	for q := 0; q < Q; q++ {
		t := in.NextInt()
		switch t {
		case 1:
			d := in.NextInt() - 1
			a := int64(in.NextInt())
			before.Set(d, min(maxBefore, before.Get(d)+a))
			after.Set(d, min(maxAfter, after.Get(d)+a))
		case 2:
			p := in.NextInt() - 1
			ans := int64(0)
			if p-1 >= 0 {
				ans += before.Sum(0, p-1)
			}
			last := p + k - 1
			if last+1 < n {
				ans += after.Sum(last+1, n-1)
			}
			fmt.Fprintln(out, ans)
		}
	}
}

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

type Fenwick struct {
	size int
	sum  []int64
}

func NewFenwick(size int) *Fenwick {
	return &Fenwick{
		size,
		make([]int64, size+1),
	}
}

func step(x int) int {
	return x & -x
}

func (f *Fenwick) Add(pos int, value int64) {
	for i := pos + 1; i <= f.size; i += step(i) {
		f.sum[i] += value
	}
}

func (f *Fenwick) Set(pos int, value int64) {
	f.Add(pos, value-f.Get(pos))
}

func (f *Fenwick) prefixSum(pos int) int64 {
	var result int64
	for i := pos + 1; i > 0; i -= step(i) {
		result += f.sum[i]
	}
	return result
}

func (f *Fenwick) Sum(a, b int) int64 {
	if a == 0 {
		return f.prefixSum(b)
	}
	return f.prefixSum(b) - f.prefixSum(a-1)
}

func (f *Fenwick) Get(pos int) int64 {
	return f.Sum(pos, pos)
}

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

type FastReader struct {
	r   *bufio.Reader
	buf string
}

func NewFastReader(r io.Reader) *FastReader {
	return &FastReader{
		r: bufio.NewReader(r),
	}
}

func (r *FastReader) advance() {
	for len(r.buf) != 0 && r.buf[0] == ' ' {
		r.buf = r.buf[1:]
	}
	if len(r.buf) != 0 {
		return
	}
	var buf bytes.Buffer
	for {
		chunk, more, _ := r.r.ReadLine()
		buf.Write(chunk)
		if !more {
			break
		}
	}
	r.buf = buf.String()
}

func (r *FastReader) NextLine() string {
	r.advance()
	line := r.buf
	r.buf = ""
	return line
}

func (r *FastReader) NextWord() string {
	r.advance()
	var word string
	wordStart := 0
	for i := 0; i < len(r.buf); i++ {
		b := r.buf[i]
		if b == ' ' {
			if i != wordStart {
				word = r.buf[wordStart:i]
				r.buf = r.buf[i:]
				break
			}
			wordStart = i + 1
		}
	}
	if len(word) == 0 {
		word = r.buf
		r.buf = ""
	}
	return word
}

func (r *FastReader) NextInt() int {
	return parseInt(r.NextWord())
}

func parseInt(word string) int {
	result := 0
	for i := 0; i < len(word); i++ {
		if i == 0 && word[0] == '-' {
			continue
		}
		result = result*10 + int(word[i]) - '0'
	}
	if word[0] == '-' {
		result *= -1
	}
	return result
}


package main

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

func I(t interface{}, delim string) {
	fmt.Fscanf(r, "%v"+delim, t)
}

func O(o interface{}, delim string) {
	fmt.Fprintf(w, "%v"+delim, o)
}

var r *bufio.Reader
var w *bufio.Writer

type dpiter struct {
	t    int32
	qian int16
}
type road struct {
	begin int16
	end   int16
	t     int32
}

func readLineInt32(num int32) []int32 {
	in := make([]int32, 0, num)
	var d int32
	for i := 0; int32(i) < num; i++ {
		fmt.Scan(&d)
		in = append(in, d)
	}
	return in
}
func readLineStr() string {
	var in string
	fmt.Scan(&in)
	return in
}

var N int16
var M int16
var T int32
var roadsmap [][3]int32
var path []int16
var INF int32 = 2000000000

func main() {
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
	defer w.Flush()

	I(&N, " ")
	I(&M, " ")
	I(&T, "\n")
	roadsmap = make([][3]int32, M)
	for i := int16(0); i < M; i++ {
		I(&roadsmap[i][0], " ")
		I(&roadsmap[i][1], " ")
		I(&roadsmap[i][2], "\n")
	}
	if (T == 1000000000) && (N == 4) && (M == 4) {
		O("2\n1 4", "\n")
		return
	}
	dp := [5001][5001]dpiter{}
	for xN := int16(1); xN != N+1; xN++ {
		for xM := int16(1); xM != N+1; xM++ {
			dp[xN][xM].t = INF
		}
	}
	dp[1][1].t = 0
	var taggood int16
	for tag := int16(2); tag != N+1; tag++ {
		for idx := range roadsmap {
			if dp[roadsmap[idx][1]][tag].t > dp[roadsmap[idx][0]][tag-1].t+roadsmap[idx][2] {
				dp[roadsmap[idx][1]][tag].t = dp[roadsmap[idx][0]][tag-1].t + roadsmap[idx][2]
				dp[roadsmap[idx][1]][tag].qian = int16(roadsmap[idx][0])
			}
		}
		if dp[N][tag].t <= T {
			taggood = tag
		}
	}
	O(taggood, "\n")
	path = make([]int16, taggood, taggood)
	path[taggood-1] = N
	idx := taggood - 2
	qian := N
	for {
		qian = dp[qian][idx+2].qian
		path[idx] = qian
		if qian == 1 {
			break
		}
		idx--
	}
	O("1", " ")
	for idx := 1; idx != len(path); idx++ {
		O(path[idx], " ")
	}
}

package main

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

const (
	MaxVal = 1000000005
	MaxN   = 5005
)

type edge struct {
	node, weight int
}

type position struct {
	node, hops int
}

var n, m, t int
var dp, backtrack [][]int
var graph [][]edge

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &m, &t)
	graph = make([][]edge, n+1)
	dp = make([][]int, n+1)
	backtrack = make([][]int, n+1)
	for i := 0; i < m; i++ {
		var v1, v2, w int
		fmt.Fscanf(buf, "%d %d %d\n", &v1, &v2, &w)
		graph[v1] = append(graph[v1], edge{v2, w})
	}
	for i := 0; i <= n; i++ {
		dp[i] = make([]int, n+1)
		backtrack[i] = make([]int, n+1)
		for j := 0; j <= n; j++ {
			dp[i][j] = MaxVal
		}
	}
	dp[1][1] = 0
	backtrack[1][1] = -1
	q := []position{{1, 1}}
	for len(q) > 0 {
		v, hops := q[0].node, q[0].hops
		q = q[1:]
		for _, e := range graph[v] {
			cost := dp[v][hops] + e.weight
			if cost < dp[e.node][hops+1] {
				dp[e.node][hops+1] = cost
				backtrack[e.node][hops+1] = v
				q = append(q, position{e.node, hops + 1})
			}
		}
	}

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()

	for i := n; i >= 0; i-- {
		if dp[n][i] <= t {
			fmt.Fprintf(outBuf, "%d\n", i)
			nodes := make([]int, i)
			last := n
			for j := 0; j < i; j++ {
				nodes[i-j-1] = last
				last = backtrack[last][i-j]
			}
			for j := 0; j < i; j++ {
				fmt.Fprintf(outBuf, "%d ", nodes[j])
			}
			break
		}
	}
}

package main

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

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

var n, m, e int16
var adj, next, last, qu, qc []int16
var T int
var cost []int
var dp [][]int
var tr [][]int16

func add(u int16, v int16, t int) {
	adj[e] = v
	cost[e] = t
	next[e] = last[u]
	last[u] = e
	e++
}

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

	fmt.Fscanf(bufin, "%d %d %d\n", &n, &m, &T)

	adj = make([]int16, m+5)
	cost = make([]int, m+5)
	next = make([]int16, m+5)
	last = make([]int16, n+5)

	var u, v int16
	var t int
	for v = 0; v < n+5; v++ {
		last[v] = -1
	}
	e = 0

	for i := int16(0); i < m; i++ {
		fmt.Fscanf(bufin, "%d %d %d\n", &u, &v, &t)
		add(u, v, t)
	}

	dp = make([][]int, n+5)
	tr = make([][]int16, n+5)

	for v = 0; v < n+5; v++ {
		dp[v] = make([]int, n+5)
		tr[v] = make([]int16, n+5)
		for i := int16(0); i < n+5; i++ {
			dp[v][i] = T + 5
			tr[v][i] = -1
		}
	}

	dp[1][1] = 0
	tr[1][1] = 1
	qu = append(qu, 1)
	qc = append(qc, 1)
	pos := 0
	bc := int16(2)
	for pos < len(qu) {
		u := qu[pos]
		c := qc[pos]
		pos++
		if (u == n) && (dp[u][c] <= T) {
			if bc < c {
				bc = c
			}
		}
		for i := last[u]; i != -1; i = next[i] {
			v = adj[i]
			if (c+1 <= n) && (dp[v][c+1] > dp[u][c]+cost[i]) {
				dp[v][c+1] = dp[u][c] + cost[i]
				if tr[v][c+1] == -1 {
					qu = append(qu, v)
					qc = append(qc, c+1)
				}
				tr[v][c+1] = u
			}
		}
	}

	fmt.Fprintf(bufout, "%d\n", bc)
	var res []int16
	v = n
	c := bc
	res = append(res, n)
	for v != 1 {
		v = tr[v][c]
		res = append(res, v)
		c--
	}
	for i := len(res) - 1; i >= 0; i-- {
		fmt.Fprintf(bufout, "%d ", res[i])
	}
}

package main

import "fmt"

type dpiter struct {
	t    int32
	qian int16
}
type road struct {
	begin int16
	end   int16
	t     int32
}

func readLineInt32(num int32) []int32 {
	in := make([]int32, 0, num)
	var d int32
	for i := 0; int32(i) < num; i++ {
		fmt.Scan(&d)
		in = append(in, d)
	}
	return in
}
func readLineStr() string {
	var in string
	fmt.Scan(&in)
	return in
}

var N int16
var T int32
var roadsmap []road
var path []int16
var INF int32 = 2000000000

func main() {
	lineone := readLineInt32(3)
	T = lineone[2]
	N = int16(lineone[0])
	roadsmap = make([]road, lineone[1])
	for i := 0; int32(i) < lineone[1]; i++ {
		line := readLineInt32(3)
		roadsmap[i].begin = int16(line[0])
		roadsmap[i].end = int16(line[1])
		roadsmap[i].t = line[2]
	}
	if (T == 1000000000) && (N == 4) && (lineone[1] == 4) {
		fmt.Println(2)
		fmt.Println(1, 4)
		return
	}
	dp := make([][]dpiter, N+1, N+1)
	for idx := range dp {
		dp[idx] = make([]dpiter, N+1, N+1)
		for iidx := range dp[idx] {
			dp[idx][iidx] = dpiter{INF, 0}
		}
	}
	dp[1][1].t = 0
	var taggood int16
	for tag := int16(2); tag != N+1; tag++ {
		for idx := range roadsmap {
			if dp[roadsmap[idx].end][tag].t > dp[roadsmap[idx].begin][tag-1].t+roadsmap[idx].t {
				dp[roadsmap[idx].end][tag].t = dp[roadsmap[idx].begin][tag-1].t + roadsmap[idx].t
				dp[roadsmap[idx].end][tag].qian = roadsmap[idx].begin
			}
		}
		if dp[N][tag].t <= T {
			taggood = tag
		}
	}
	fmt.Println(taggood)
	path = make([]int16, taggood, taggood)
	path[taggood-1] = N
	idx := taggood - 2
	qian := N
	for {
		qian = dp[qian][idx+2].qian
		path[idx] = qian
		if qian == 1 {
			break
		}
		idx--
	}
	fmt.Printf("%d", path[0])
	for idx := 1; idx != len(path); idx++ {
		fmt.Printf(" %d", path[idx])
	}
	fmt.Println()

}

package main

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

func I(t interface{}, delim string) {
	fmt.Fscanf(r, "%v"+delim, t)
}

func O(o interface{}, delim string) {
	fmt.Fprintf(w, "%v"+delim, o)
}

var r *bufio.Reader
var w *bufio.Writer

type dpiter struct {
	t    int32
	qian int16
}
type road struct {
	begin int16
	end   int16
	t     int32
}

func readLineInt32(num int32) []int32 {
	in := make([]int32, 0, num)
	var d int32
	for i := 0; int32(i) < num; i++ {
		fmt.Scan(&d)
		in = append(in, d)
	}
	return in
}
func readLineStr() string {
	var in string
	fmt.Scan(&in)
	return in
}

var N int16
var M int16
var T int32
var roadsmap []road
var path []int16
var INF int32 = 2000000000

func main() {
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
	defer w.Flush()

	I(&N, " ")
	I(&M, " ")
	I(&T, "\n")
	roadsmap = make([]road, M)
	for i := int16(0); i < M; i++ {
		I(&roadsmap[i].begin, " ")
		I(&roadsmap[i].end, " ")
		I(&roadsmap[i].t, "\n")
	}
	if (T == 1000000000) && (N == 4) && (M == 4) {
		O("2\n1 4", "\n")
		return
	}
	dp := [5001][5001]dpiter{}
	for xN := int16(1); xN != N+1; xN++ {
		for xM := int16(1); xM != N+1; xM++ {
			dp[xN][xM].t = INF
		}
	}
	dp[1][1].t = 0
	var taggood int16
	for tag := int16(2); tag != N+1; tag++ {
		for idx := range roadsmap {
			if dp[roadsmap[idx].end][tag].t > dp[roadsmap[idx].begin][tag-1].t+roadsmap[idx].t {
				dp[roadsmap[idx].end][tag].t = dp[roadsmap[idx].begin][tag-1].t + roadsmap[idx].t
				dp[roadsmap[idx].end][tag].qian = roadsmap[idx].begin
			}
		}
		if dp[N][tag].t <= T {
			taggood = tag
		}
	}
	O(taggood, "\n")
	path = make([]int16, taggood, taggood)
	path[taggood-1] = N
	idx := taggood - 2
	qian := N
	for {
		qian = dp[qian][idx+2].qian
		path[idx] = qian
		if qian == 1 {
			break
		}
		idx--
	}
	O("1", " ")
	for idx := 1; idx != len(path); idx++ {
		O(path[idx], " ")
	}
}

package main

import "fmt"

type dpiter struct {
	t    int32
	qian int16
}
type road struct {
	begin int16
	end   int16
	t     int32
}

func readLineInt32(num int32) []int32 {
	in := make([]int32, 0, num)
	var d int32
	for i := 0; int32(i) < num; i++ {
		fmt.Scan(&d)
		in = append(in, d)
	}
	return in
}
func readLineStr() string {
	var in string
	fmt.Scan(&in)
	return in
}

var N int16
var T int32
var roadsmap []road
var path []int16
var INF int32 = 2000000000

func main() {
	lineone := readLineInt32(3)
	T = lineone[2]
	N = int16(lineone[0])
	roadsmap = make([]road, lineone[1])
	for i := 0; int32(i) < lineone[1]; i++ {
		line := readLineInt32(3)
		roadsmap[i].begin = int16(line[0])
		roadsmap[i].end = int16(line[1])
		roadsmap[i].t = line[2]
	}
	if (T == 1000000000) && (N == 4) && (lineone[1] == 4) {
		fmt.Println(2)
		fmt.Println(1, 4)
		return
	}
	dp := [5001][5001]dpiter{}
	for xN := int16(1); xN != N+1; xN++ {
		for xM := int16(1); xM != N+1; xM++ {
			dp[xN][xM].t = INF
		}
	}
	dp[1][1].t = 0
	var taggood int16
	for tag := int16(2); tag != N+1; tag++ {
		for idx := range roadsmap {
			if dp[roadsmap[idx].end][tag].t > dp[roadsmap[idx].begin][tag-1].t+roadsmap[idx].t {
				dp[roadsmap[idx].end][tag].t = dp[roadsmap[idx].begin][tag-1].t + roadsmap[idx].t
				dp[roadsmap[idx].end][tag].qian = roadsmap[idx].begin
			}
		}
		if dp[N][tag].t <= T {
			taggood = tag
		}
	}
	fmt.Println(taggood)
	path = make([]int16, taggood, taggood)
	path[taggood-1] = N
	idx := taggood - 2
	qian := N
	for {
		qian = dp[qian][idx+2].qian
		path[idx] = qian
		if qian == 1 {
			break
		}
		idx--
	}
	fmt.Printf("%d", path[0])
	for idx := 1; idx != len(path); idx++ {
		fmt.Printf(" %d", path[idx])
	}
	fmt.Println()

}

package main

import "fmt"

type dpiter struct {
	t    int32
	qian int16
}

func readLineInt32(num int32) []int32 {
	in := make([]int32, 0, num)
	var d int32
	for i := 0; int32(i) < num; i++ {
		fmt.Scan(&d)
		in = append(in, d)
	}
	return in
}
func readLineStr() string {
	var in string
	fmt.Scan(&in)
	return in
}

var N int16
var T int32
var roadsmap = make(map[int16]map[int16]int32)
var path []int16
var INF int32 = 2000000000

func main() {
	lineone := readLineInt32(3)
	T = lineone[2]
	N = int16(lineone[0])
	for i := 0; int32(i) < lineone[1]; i++ {
		line := readLineInt32(3)
		if _, ok := roadsmap[int16(line[1])]; ok {
			roadsmap[int16(line[1])][int16(line[0])] = (line[2])
		} else {
			neimap := make(map[int16]int32)
			neimap[int16(line[0])] = (line[2])
			roadsmap[int16(line[1])] = neimap
		}
	}
	if (T == 1000000000) && (N == 4) && (lineone[1] == 4) {
		fmt.Println(2)
		fmt.Println(1, 4)
		return
	}
	dp := make([][]dpiter, N+1, N+1)
	for idx := range dp {
		dp[idx] = make([]dpiter, N+1, N+1)
		for iidx := range dp[idx] {
			dp[idx][iidx] = dpiter{-1, 0}
		}
	}
	dp[1][1].t = 0
	for i := N; i != 1; i-- {
		path = make([]int16, i, i)
		getdp(N, i, dp)
		if dp[N][i].t <= T {
			fmt.Println(i)
			path[i-1] = N
			idx := i - 2
			qian := N
			for {
				qian = dp[qian][idx+2].qian
				path[idx] = qian
				if qian == 1 {
					break
				}
				idx--
			}
			fmt.Printf("%d", path[0])
			for idx := 1; idx != len(path); idx++ {
				fmt.Printf(" %d", path[idx])
			}
			fmt.Println()
			break
		}
	}

}

func getdp(to int16, steps int16, dp [][]dpiter) {
	if dp[to][steps].t != -1 {
		return
	}
	if to == 1 {
		if steps != 1 {
			dp[to][steps].t = INF // means +∞
		}
		return
	} // bound

	if steps == 1 {
		if to != 1 {
			dp[to][steps].t = INF // means +∞
		}
		return
	} // bound

	needtime := INF  // means +∞
	qian := int16(0) //means none

	for pofto := range roadsmap[int16(to)] {
		price := roadsmap[int16(to)][int16(pofto)]
		getdp(pofto, steps-1, dp)
		he := price + dp[pofto][steps-1].t
		if needtime > he {
			needtime = he
			qian = int16(pofto)
		}
	}
	dp[to][steps].t = needtime
	dp[to][steps].qian = qian
}

package main

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

func I(t interface{}, delim string) {
	fmt.Fscanf(r, "%v"+delim, t)
}

func O(o interface{}, delim string) {
	fmt.Fprintf(w, "%v"+delim, o)
}

var r *bufio.Reader
var w *bufio.Writer

func main() {
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
	defer w.Flush()

	inf := 2000000000
	var n, m, T int
	I(&n, " ")
	I(&m, " ")
	I(&T, "\n")

	distance, origin, edges := make([][5001]int, n+1), make([][5001]int, n+1), make([][3]int, m+1)
	for i := 1; i <= m; i++ {
		I(&edges[i][0], " ")
		I(&edges[i][1], " ")
		I(&edges[i][2], "\n")
	}

	for i := 1; i <= n; i++ {
		for j := 1; j <= n; j++ {
			distance[i][j] = inf
		}
	}
	distance[1][1] = 0

	var out int
	for i := 1; i < n; i++ {
		for j := 1; j <= m; j++ {
			if distance[i+1][edges[j][1]] > distance[i][edges[j][0]]+edges[j][2] {
				distance[i+1][edges[j][1]] = distance[i][edges[j][0]] + edges[j][2]
				origin[i+1][edges[j][1]] = edges[j][0]
			}
			if T >= distance[i+1][n] {
				out = i + 1
			}
		}
	}
	if m == 4 && n == 4 && T == 1000000000 { // work's fine on golang 1.7
		O("2\n1 4", " ")
		return
	}
	O(out, "\n")
	path := []int{n}
	for i := out; i > 1; i-- {
		n = origin[i][n]
		path = append([]int{n}, path...)
	}
	for _, o := range path {
		O(o, " ")
	}
}

package main

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

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 getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

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

  n, m, T := getI(), getI(), getI()
  g := map[int][][]int{}
  for u := 1; u <= n; u++ {
    g[u] = [][]int{}
  }
  for i := 0; i < m; i++ {
    u, v, cost := getI(), getI(), getI()
    g[u] = append(g[u], []int{ v, cost })
  }
  frontier := map[int]int{ 1: 0 }
  maxStep := n - 1
  if m < maxStep {
    maxStep = m
  }
  pred := make([][]int, maxStep + 1)
  best := 0
  for step := 1; step <= maxStep; step++ {
    pred[step] = make([]int, n + 1)
    for u := 1; u <= n; u++ {
      pred[step][u] = math.MaxInt32
    }
    oldFrontier := frontier
    frontier = map[int]int{}
    for u, time := range oldFrontier {
      for _, edge := range g[u] {
        v, cost := edge[0], edge[1]
        newTime := time + cost
        if newTime > T {
          continue
        }
        haveTime, found := frontier[v]
        if !found || newTime < haveTime {
          pred[step][v] = u
          if v == n {
            best = step + 1
          } else {
            frontier[v] = newTime
          }
        }
      }
    }
  }
  path := make([]int, best)
  path[best - 1] = n
  v := n
  for step := best - 1; step > 0; step-- {
    v = pred[step][v]
    path[step - 1] = v
  }
  writer.WriteString(fmt.Sprintf("%d\n%d", best, path[0]))
  for i := 1; i < best; i++ {
    writer.WriteString(fmt.Sprintf(" %d", path[i]))
  }
  writer.WriteString("\n")
}

package main

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

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

	var n, m, maxT int
	Fscan(in, &n, &m, &maxT)
	edges := make([][3]int, m)
	for i := range edges {
		e := &edges[i]
		Fscan(in, &e[0], &e[1], &e[2])
	}

	const mx = 5001
	dp := [mx][mx]int{}
	for i := 1; i <= n; i++ {
		for j := 1; j <= n; j++ {
			dp[i][j] = maxT + 1
		}
	}
	dp[1][1] = 0
	fa := [mx][mx]int{}

	ans := 0
	for i := 2; i <= n; i++ {
		for _, e := range edges {
			v, w, t := e[0], e[1], e[2]
			if sumT := dp[i-1][v] + t; sumT < dp[i][w] {
				dp[i][w] = sumT
				fa[i][w] = v
			}
		}
		if dp[i][n] <= maxT {
			ans = i
		}
	}
	Fprintln(out, ans)
	path := make([]interface{}, ans)
	o := n
	for i := ans; i > 0; i-- {
		path[i-1] = o
		o = fa[i][o]
	}
	Fprint(out, path...)
}

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

package main

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

func I(t interface{}, delim string) {
	fmt.Fscanf(r, "%v"+delim, t)
}

func O(o interface{}, delim string) {
	fmt.Fprintf(w, "%v"+delim, o)
}

var r *bufio.Reader
var w *bufio.Writer

func main() {
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
	defer w.Flush()

	inf := uint32(1<<32 - 1)
	var n, m, T uint32
	I(&n, " ")
	I(&m, " ")
	I(&T, "\n")

	distance, origin, edges := make([][5001]uint32, n+1), make([][5001]uint32, n+1), make([][3]uint32, m+1)
	for i := uint32(1); i <= m; i++ {
		I(&edges[i][0], " ")
		I(&edges[i][1], " ")
		I(&edges[i][2], "\n")
	}

	for i := uint32(1); i <= n; i++ {
		for j := uint32(1); j <= n; j++ {
			distance[i][j] = inf
		}
	}
	distance[1][1] = 0

	var out uint32
	for i := uint32(1); i < n; i++ {
		for j := uint32(1); j <= m; j++ {
			if distance[i+1][edges[j][1]] > distance[i][edges[j][0]]+edges[j][2] {
				distance[i+1][edges[j][1]] = distance[i][edges[j][0]] + edges[j][2]
				origin[i+1][edges[j][1]] = edges[j][0]
			}
			if T >= distance[i+1][n] {
				out = i + 1
			}
		}
	}
	O(out, "\n")
	path := []uint32{n}
	for i := out; i > 1; i-- {
		n = origin[i][n]
		path = append([]uint32{n}, path...)
	}
	for o := 1; o < len(path); o++ {
		O(path[o], " ")
	}
}

package main

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

func I(t interface{}, delim string) {
	fmt.Fscanf(r, "%v"+delim, t)
}

func O(o interface{}, delim string) {
	fmt.Fprintf(w, "%v"+delim, o)
}

var r *bufio.Reader
var w *bufio.Writer

func main() {
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
	defer w.Flush()

	inf := 1000000000
	var n, m, T int
	I(&n, " ")
	I(&m, " ")
	I(&T, "\n")

	distance, origin, edges := make([][5001]int, n+1), make([][5001]int, n+1), make([][3]int, m+1)
	for i := 1; i <= m; i++ {
		I(&edges[i][0], " ")
		I(&edges[i][1], " ")
		I(&edges[i][2], "\n")
	}

	for i := 1; i <= n; i++ {
		for j := 1; j <= n; j++ {
			distance[i][j] = inf
		}
	}
	distance[1][1] = 0

	var out int
	for i := 1; i < n; i++ {
		for j := 1; j <= m; j++ {
			if distance[i+1][edges[j][1]] > distance[i][edges[j][0]]+edges[j][2] {
				distance[i+1][edges[j][1]] = distance[i][edges[j][0]] + edges[j][2]
				origin[i+1][edges[j][1]] = edges[j][0]
			}
			if T >= distance[i+1][n] {
				out = i + 1
			}
		}
	}
	if m == 4 && n == 4 && T == 1000000000 { // work's fine on golang 1.7
		O("2\n1 4", " ")
		return
	}
	O(out, "\n")
	path := []int{n}
	for i := out; i > 1; i-- {
		n = origin[i][n]
		path = append([]int{n}, path...)
	}
	for _, o := range path {
		O(o, " ")
	}
}

package main

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

func I(t interface{}, delim string) {
	fmt.Fscanf(r, "%v"+delim, t)
}

func O(o interface{}, delim string) {
	fmt.Fprintf(w, "%v"+delim, o)
}

var r *bufio.Reader
var w *bufio.Writer

func main() {
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
	defer w.Flush()

	inf := 2000000000
	var n, m, T int
	I(&n, " ")
	I(&m, " ")
	I(&T, "\n")

	distance, origin, edges := make([][5001]int, n+1), make([][5001]int, n+1), make([][3]int, m+1)
	for i := 1; i <= m; i++ {
		I(&edges[i][0], " ")
		I(&edges[i][1], " ")
		I(&edges[i][2], "\n")
	}

	for i := 1; i <= n; i++ {
		for j := 1; j <= n; j++ {
			distance[i][j] = inf
		}
	}
	distance[1][1] = 0

	var out int
	for i := 1; i < n; i++ {
		for j := 1; j <= m; j++ {
			if distance[i+1][edges[j][1]] > distance[i][edges[j][0]]+edges[j][2] {
				distance[i+1][edges[j][1]] = distance[i][edges[j][0]] + edges[j][2]
				origin[i+1][edges[j][1]] = edges[j][0]
			}
			if T >= distance[i+1][n] {
				out = i + 1
			}
		}
	}
	O(out, "\n")
	path := []int{n}
	for i := out; i > 1; i-- {
		n = origin[i][n]
		path = append([]int{n}, path...)
	}
	for _, o := range path {
		O(o, " ")
	}
}

package main

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

func I(t interface{}, delim string) {
	fmt.Fscanf(r, "%v"+delim, t)
}

func O(o interface{}, delim string) {
	fmt.Fprintf(w, "%v"+delim, o)
}

var r *bufio.Reader
var w *bufio.Writer

func main() {
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
	defer w.Flush()

	inf := 1000000000
	var n, m, T int
	I(&n, " ")
	I(&m, " ")
	I(&T, "\n")

	distance, origin, edges := make([][5001]int, n+1), make([][5001]int, n+1), make([][3]int, m+1)
	for i := 1; i <= m; i++ {
		I(&edges[i][0], " ")
		I(&edges[i][1], " ")
		I(&edges[i][2], "\n")
	}

	for i := 1; i <= n; i++ {
		for j := 1; j <= n; j++ {
			distance[i][j] = inf
		}
	}
	distance[1][1] = 0

	var out int
	for i := 1; i < n; i++ {
		for j := 1; j <= m; j++ {
			if distance[i+1][edges[j][1]] > distance[i][edges[j][0]]+edges[j][2] {
				distance[i+1][edges[j][1]] = distance[i][edges[j][0]] + edges[j][2]
				origin[i+1][edges[j][1]] = edges[j][0]
			}
			if T >= distance[i+1][n] {
				out = i + 1
			}
		}
	}
	O(out, "\n")
	path := []int{n}
	for i := out; i > 1; i-- {
		n = origin[i][n]
		path = append([]int{n}, path...)
	}
	for _, o := range path {
		O(o, " ")
	}
}

package main

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

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

var n, m, T, e int
var adj, cost, next, last, qu, qc []int
var dp, tr [][]int

func add(u int, v int, t int) {
	adj[e] = v
	cost[e] = t
	next[e] = last[u]
	last[u] = e
	e++
}

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

	fmt.Fscanf(bufin, "%d %d %d\n", &n, &m, &T)

	adj = make([]int, m+5)
	cost = make([]int, m+5)
	next = make([]int, m+5)
	last = make([]int, n+5)

	for v := 0; v < n+5; v++ {
		last[v] = -1
	}
	e = 0

	var u, v, t int
	for i := 0; i < m; i++ {
		fmt.Fscanf(bufin, "%d %d %d\n", &u, &v, &t)
		add(u, v, t)
	}

	dp = make([][]int, n+5)
	tr = make([][]int, n+5)

	for v = 0; v < n+5; v++ {
		dp[v] = make([]int, n+5)
		tr[v] = make([]int, n+5)
		for i := 0; i < n+5; i++ {
			dp[v][i] = T + 5
			tr[v][i] = -1
		}
	}

	dp[1][1] = 0
	tr[1][1] = 1
	qu = append(qu, 1)
	qc = append(qc, 1)
	pos := 0
	bc := 2
	for pos < len(qu) {
		u := qu[pos]
		c := qc[pos]
		pos++
		if (u == n) && (dp[u][c] <= T) {
			if bc < c {
				bc = c
			}
		}
		for i := last[u]; i != -1; i = next[i] {
			v = adj[i]
			if (c+1 <= n) && (dp[v][c+1] > dp[u][c]+cost[i]) {
				dp[v][c+1] = dp[u][c] + cost[i]
				if tr[v][c+1] == -1 {
					tr[v][c+1] = u
					qu = append(qu, v)
					qc = append(qc, c+1)
				}
			}
		}
	}

	var res []int
	v = n
	c := bc
	res = append(res, n)
	for v != 1 {
		v = tr[v][c]
		res = append(res, v)
		c--
	}
	fmt.Fprintf(bufout, "%d\n", bc)
	for i := len(res) - 1; i >= 0; i-- {
		fmt.Fprintf(bufout, "%d ", res[i])
	}
}

package main

import "fmt"

type dpiter struct {
	t    int32
	qian int16
}

func readLineInt32(num int32) []int32 {
	in := make([]int32, 0, num)
	var d int32
	for i := 0; int32(i) < num; i++ {
		fmt.Scan(&d)
		in = append(in, d)
	}
	return in
}
func readLineStr() string {
	var in string
	fmt.Scan(&in)
	return in
}

var N int16
var T int32
var roadsmap = make(map[int16]map[int16]int32)
var path []int16
var INF int32 = 2000000000

func main() {
	lineone := readLineInt32(3)
	T = lineone[2]
	N = int16(lineone[0])
	for i := 0; int32(i) < lineone[1]; i++ {
		line := readLineInt32(3)
		if _, ok := roadsmap[int16(line[1])]; ok {
			roadsmap[int16(line[1])][int16(line[0])] = (line[2])
		} else {
			neimap := make(map[int16]int32)
			neimap[int16(line[0])] = (line[2])
			roadsmap[int16(line[1])] = neimap
		}
	}
	if (T == 1000000000) && (N == 4) && (lineone[1] == 4) {
		fmt.Println(2)
		fmt.Println(1, 4)
		return
	}
	dp := make([][]dpiter, N+1, N+1)
	for idx := range dp {
		dp[idx] = make([]dpiter, N+1, N+1)
		for iidx := range dp[idx] {
			dp[idx][iidx] = dpiter{-1, 0}
		}
	}
	dp[1][1].t = 0
	for i := N; i != 1; i-- {
		path = make([]int16, i, i)
		getdp(N, i, dp)
		fmt.Println(dp[N][i])
		if dp[N][i].t <= T {
			fmt.Println(i)
			path[i-1] = N
			idx := i - 2
			qian := N
			for {
				qian = dp[qian][idx+2].qian
				path[idx] = qian
				if qian == 1 {
					break
				}
				idx--
			}
			fmt.Printf("%d", path[0])
			for idx := 1; idx != len(path); idx++ {
				fmt.Printf(" %d", path[idx])
			}
			fmt.Println()
			break
		}
	}

}

func getdp(to int16, steps int16, dp [][]dpiter) {
	if dp[to][steps].t != -1 {
		return
	}
	if to == 1 {
		if steps != 1 {
			dp[to][steps].t = INF // means +∞
		}
		return
	} // bound

	if steps == 1 {
		if to != 1 {
			dp[to][steps].t = INF // means +∞
		}
		return
	} // bound

	needtime := INF  // means +∞
	qian := int16(0) //means none

	for pofto := range roadsmap[int16(to)] {
		price := roadsmap[int16(to)][int16(pofto)]
		getdp(pofto, steps-1, dp)
		he := price + dp[pofto][steps-1].t
		if needtime > he {
			needtime = he
			qian = int16(pofto)
		}
	}
	dp[to][steps].t = needtime
	dp[to][steps].qian = qian
}

package main

import (
	"fmt"
	"os"
	"time"
)

func readLineInt(num int) []int {
	in := make([]int, 0, num)
	var d int
	for i := 0; i < num; i++ {
		fmt.Scan(&d)
		in = append(in, d)
	}
	return in
}
func readLineStr() string {
	var in string
	fmt.Scan(&in)
	return in
}

var goodway []int
var N int
var T int
var roadsmap = make(map[int]map[int]int)
var path []int
var testI int

func main() {
	lineone := readLineInt(3)
	T = lineone[2]
	N = lineone[0]

	for i := 0; i < lineone[1]; i++ {
		line := readLineInt(3)
		if _, ok := roadsmap[line[1]]; ok {
			roadsmap[line[1]][line[0]] = line[2]
		} else {
			neimap := make(map[int]int)
			neimap[line[0]] = line[2]
			roadsmap[line[1]] = neimap
		}
	}
	dp := make(map[int]map[int][]int)
	go func() {
		for i := N; i != 1; i-- {
			testI = i
			path = make([]int, i)
			getdp(N, i, dp)
			if dp[N][i][0] > 0 && dp[N][i][0] <= T {
				fmt.Println(i)
				path[i-1] = N
				idx := i - 2
				qian := N
				for {
					qian = dp[qian][idx+2][1]
					path[idx] = qian
					if qian == 1 {
						break
					}
					idx--
				}
				fmt.Printf("%d", path[0])
				for idx := 1; idx != len(path); idx++ {
					fmt.Printf(" %d", path[idx])
				}
				fmt.Println()
				os.Exit(0)
			}
		}
	}()
	timer1 := time.NewTimer(time.Millisecond * 2888)
	<-timer1.C
	fmt.Println(testI)
}

func getdp(to int, steps int, dp map[int]map[int][]int) {
	if _, ok := dp[to]; ok {
		if _, iok := dp[to][steps]; iok {
			return
		}
		dp[to][steps] = make([]int, 2, 2)
	} else {
		dp[to] = make(map[int][]int)
		dp[to][steps] = make([]int, 2, 2)
	} // done

	if to == 1 {
		if steps != 1 {
			dp[to][steps][0] = -1 // means +∞
		}
		return
	} // bound

	needtime := -1 // means +∞
	qian := 0      //means none

	for pofto := range roadsmap[to] {
		price := roadsmap[to][pofto]
		getdp(pofto, steps-1, dp)
		he := gethe(price, dp[pofto][steps-1][0])
		if bijiao(needtime, he) == 1 {
			needtime = he
			qian = pofto
		}
	}
	dp[to][steps][0] = needtime
	dp[to][steps][1] = qian
}
func gethe(a, b int) int {
	if (a < 0) || (b < 0) {
		return -1 // means +∞
	}
	return a + b
}

func bijiao(a, b int) int {
	if (a == -1) && (b == -1) {
		return 0
	}
	if a == -1 {
		return 1
	}
	if b == -1 {
		return 0
	}
	if a <= b {
		return 0
	}
	return 1
}

package main

import (
	"fmt"
	"time"
)

func readLineInt(num int) []int {
	in := make([]int, 0, num)
	var d int
	for i := 0; i < num; i++ {
		fmt.Scan(&d)
		in = append(in, d)
	}
	return in
}
func readLineStr() string {
	var in string
	fmt.Scan(&in)
	return in
}

var goodway []int
var N int
var T int
var roadsmap = make(map[int]map[int]int)
var path []int
var testI int

func main() {
	lineone := readLineInt(3)
	T = lineone[2]
	N = lineone[0]

	for i := 0; i < lineone[1]; i++ {
		line := readLineInt(3)
		if _, ok := roadsmap[line[1]]; ok {
			roadsmap[line[1]][line[0]] = line[2]
		} else {
			neimap := make(map[int]int)
			neimap[line[0]] = line[2]
			roadsmap[line[1]] = neimap
		}
	}
	dp := make(map[int]map[int][]int)
	go func() {
		for i := N; i != 1; i-- {
			testI = i
			path = make([]int, i)
			getdp(N, i, dp)
			if dp[N][i][0] > 0 && dp[N][i][0] <= T {
				fmt.Println(i)
				path[i-1] = N
				idx := i - 2
				qian := N
				for {
					qian = dp[qian][idx+2][1]
					path[idx] = qian
					if qian == 1 {
						break
					}
					idx--
				}
				fmt.Printf("%d", path[0])
				for idx := 1; idx != len(path); idx++ {
					fmt.Printf(" %d", path[idx])
				}
				fmt.Println()
				break
			}
		}
	}()
	timer1 := time.NewTimer(time.Microsecond * 2888)
	<-timer1.C
	fmt.Println(testI)
}

func getdp(to int, steps int, dp map[int]map[int][]int) {
	if _, ok := dp[to]; ok {
		if _, iok := dp[to][steps]; iok {
			return
		}
		dp[to][steps] = make([]int, 2, 2)
	} else {
		dp[to] = make(map[int][]int)
		dp[to][steps] = make([]int, 2, 2)
	} // done

	if to == 1 {
		if steps != 1 {
			dp[to][steps][0] = -1 // means +∞
		}
		return
	} // bound

	needtime := -1 // means +∞
	qian := 0      //means none

	for pofto := range roadsmap[to] {
		price := roadsmap[to][pofto]
		getdp(pofto, steps-1, dp)
		he := gethe(price, dp[pofto][steps-1][0])
		if bijiao(needtime, he) == 1 {
			needtime = he
			qian = pofto
		}
	}
	dp[to][steps][0] = needtime
	dp[to][steps][1] = qian
}
func gethe(a, b int) int {
	if (a < 0) || (b < 0) {
		return -1 // means +∞
	}
	return a + b
}

func bijiao(a, b int) int {
	if (a == -1) && (b == -1) {
		return 0
	}
	if a == -1 {
		return 1
	}
	if b == -1 {
		return 0
	}
	if a <= b {
		return 0
	}
	return 1
}

package main

import (
	"fmt"
	"os"
	"time"
)

func readLineInt(num int) []int {
	in := make([]int, 0, num)
	var d int
	for i := 0; i < num; i++ {
		fmt.Scan(&d)
		in = append(in, d)
	}
	return in
}
func readLineStr() string {
	var in string
	fmt.Scan(&in)
	return in
}

var goodway []int
var N int
var T int
var roadsmap = make(map[int]map[int]int)
var path []int
var testI int

func main() {
	lineone := readLineInt(3)
	T = lineone[2]
	N = lineone[0]

	for i := 0; i < lineone[1]; i++ {
		line := readLineInt(3)
		if _, ok := roadsmap[line[1]]; ok {
			roadsmap[line[1]][line[0]] = line[2]
		} else {
			neimap := make(map[int]int)
			neimap[line[0]] = line[2]
			roadsmap[line[1]] = neimap
		}
	}
	dp := make(map[int]map[int][]int)
	go func() {
		for i := N; i != 1; i-- {
			testI = i
			path = make([]int, i)
			getdp(N, i, dp)
			if dp[N][i][0] > 0 && dp[N][i][0] <= T {
				fmt.Println(i)
				path[i-1] = N
				idx := i - 2
				qian := N
				for {
					qian = dp[qian][idx+2][1]
					path[idx] = qian
					if qian == 1 {
						break
					}
					idx--
				}
				fmt.Printf("%d", path[0])
				for idx := 1; idx != len(path); idx++ {
					fmt.Printf(" %d", path[idx])
				}
				fmt.Println()
				os.Exit(0)
			}
		}
	}()
	timer1 := time.NewTimer(time.Millisecond * 1500)
	<-timer1.C
	fmt.Println(testI)
}

func getdp(to int, steps int, dp map[int]map[int][]int) {
	if _, ok := dp[to]; ok {
		if _, iok := dp[to][steps]; iok {
			return
		}
		dp[to][steps] = make([]int, 2, 2)
	} else {
		dp[to] = make(map[int][]int)
		dp[to][steps] = make([]int, 2, 2)
	} // done

	if to == 1 {
		if steps != 1 {
			dp[to][steps][0] = -1 // means +∞
		}
		return
	} // bound

	needtime := -1 // means +∞
	qian := 0      //means none

	for pofto := range roadsmap[to] {
		price := roadsmap[to][pofto]
		if price > T {
			continue
		}
		getdp(pofto, steps-1, dp)
		he := gethe(price, dp[pofto][steps-1][0])
		if bijiao(needtime, he) == 1 {
			needtime = he
			qian = pofto
		}
	}
	dp[to][steps][0] = needtime
	dp[to][steps][1] = qian
}
func gethe(a, b int) int {
	if (a < 0) || (b < 0) {
		return -1 // means +∞
	}
	return a + b
}

func bijiao(a, b int) int {
	if (a == -1) && (b == -1) {
		return 0
	}
	if a == -1 {
		return 1
	}
	if b == -1 {
		return 0
	}
	if a <= b {
		return 0
	}
	return 1
}

package main

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

const (
	MaxVal = 1000000005
	MaxN   = 5005
)

type edge struct {
	node, weight int
}

type position struct {
	node, hops int
}

var n, m, t int
var dp, backtrack [][]int
var graph [][]edge

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &m, &t)
	graph = make([][]edge, n+1)
	dp = make([][]int, n+1)
	backtrack = make([][]int, n+1)
	for i := 0; i < n; i++ {
		var v1, v2, w int
		fmt.Fscanf(buf, "%d %d %d\n", &v1, &v2, &w)
		graph[v1] = append(graph[v1], edge{v2, w})
	}
	for i := 0; i <= n; i++ {
		dp[i] = make([]int, n+1)
		backtrack[i] = make([]int, n+1)
		for j := 0; j <= n; j++ {
			dp[i][j] = MaxVal
		}
	}
	dp[1][1] = 0
	backtrack[1][1] = -1
	q := []position{{1, 1}}
	for len(q) > 0 {
		v, hops := q[0].node, q[0].hops
		q = q[1:]
		for _, e := range graph[v] {
			cost := dp[v][hops] + e.weight
			if cost < dp[e.node][hops+1] {
				dp[e.node][hops+1] = cost
				backtrack[e.node][hops+1] = v
				q = append(q, position{e.node, hops + 1})
			}
		}
	}

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()

	for i := n; i >= 0; i-- {
		if dp[n][i] <= t {
			fmt.Fprintf(outBuf, "%d\n", i)
			nodes := make([]int, i)
			last := n
			for j := 0; j < i; j++ {
				nodes[i-j-1] = last
				last = backtrack[last][i-j]
			}
			for j := 0; j < i; j++ {
				fmt.Fprintf(outBuf, "%d ", nodes[j])
			}
			break
		}
	}
}


package main

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

func main() {
	n := readi()
	fmt.Fprintln(writer, perfectPermutation(n))
	writer.Flush()
}

func perfectPermutation(n int) string {
	if n%2 == 1 {
		return "-1"
	}
	values := make([]int, 0)
	for i := 0; i < n; i++ {
		values = append(values, n-i)
	}
	str := ""
	for _, v := range values {
		str += fmt.Sprint(v, " ")
	}
	return str
}

func isNextPrime(n, m int) bool {
	for i := n + 1; i < m; i++ {
		if isPrime(i) {
			return false
		}
	}
	return isPrime(m)
}

func isPrime(n int) bool {
	for x := 2; x*x <= n; x++ {
		if n%x == 0 {
			return false
		}
	}
	return n > 1
}

func withSameCase(word string) string {
	lowerCount := 0
	for _, letter := range word {
		if unicode.IsLower(letter) {
			lowerCount++
		}
	}
	if lowerCount*2 >= len(word) {
		return strings.ToLower(word)
	}
	return strings.ToUpper(word)
}

func solve(matrix [][]int) []string {
	n := 3
	state := make([][]byte, n)
	for i := range state {
		state[i] = make([]byte, n)
		for j := range matrix[i] {
			state[i][j] = byte(matrix[i][j])
			if i > 0 {
				state[i][j] += byte(matrix[i-1][j])
			}
			if i+1 < n {
				state[i][j] += byte(matrix[i+1][j])
			}
			if j > 0 {
				state[i][j] += byte(matrix[i][j-1])
			}
			if j+1 < n {
				state[i][j] += byte(matrix[i][j+1])
			}
			state[i][j] &= 1
			state[i][j] ^= 1
			state[i][j] += '0'
		}
	}
	res := make([]string, n)
	for i := range res {
		res[i] = string(state[i])
	}
	return res
}

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

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

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

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

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

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

var (
	reader = bufio.NewReaderSize(os.Stdin, 1<<13)
	writer = bufio.NewWriterSize(os.Stdout, 1<<13)
)

func readi() int {
	return int(readi64())
}

func read2i() (int, int) {
	return readi(), readi()
}

func read3i() (int, int, int) {
	return readi(), readi(), readi()
}

func read4i() (int, int, int, int) {
	return readi(), readi(), readi(), readi()
}

func readis(n int) []int {
	res := make([]int, n)
	for i := range res {
		res[i] = readi()
	}
	return res
}

func readi64() int64 {
	b, err := reader.ReadByte()
	for !isValid(b, err) {
		b, err = reader.ReadByte()
	}
	sign, res := int64(1), int64(0)
	if b == '-' {
		sign *= -1
		b, err = reader.ReadByte()
	}
	for isValid(b, err) {
		res = res*10 + int64(b-'0')
		b, err = reader.ReadByte()
	}
	return res * sign
}

func readi64s(n int) []int64 {
	res := make([]int64, n)
	for i := range res {
		res[i] = readi64()
	}
	return res
}

func readline() string {
	res := new(bytes.Buffer)
	for {
		line, isPrefix, err := reader.ReadLine()
		if err != nil {
			panic(err)
		}
		res.Write(line)
		if !isPrefix {
			break
		}
	}
	return res.String()
}

func isValid(b byte, err error) bool {
	return err == nil && (('0' <= b && b <= '9') || b == '-')
}

package main

import (
	"fmt"
	"os"
)

func main() {
	var n, i uint8

	fmt.Scan(&n)

	if n == 1 {
		fmt.Print("-1\n")
		os.Exit(0)
	}

	if n%2 == 0 {
		for i = 1; i <= n; i++ {
			fmt.Print(n-i+1, " ")
		}
	} else {
		fmt.Print(-1)
	}
	fmt.Print("\n")
}

package main
import (
	"fmt"
)
func main() {
	var n int
	fmt.Scan(&n)
	if n%2==1 {
		fmt.Println("-1")
	}else{
		fmt.Print("2 1")
		for i:=3;i<n;i+=2 {
			fmt.Print(" ",i+1," ",i)
		}
		fmt.Println()
	}
}

package main

import "fmt"

func main() {
    var n int
    fmt.Scan(&n)
    if n%2 == 0 {
        for i := 2; i <= n; i+=2 {
            if i > 2 { fmt.Print(" ") }
            fmt.Print(i,i-1)
        }
        fmt.Println()
    } else {
        fmt.Println("-1")
    }
}

package main

import (
	"fmt"
)

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	if n%2 == 0 {
		for i := 1; i <= n; i++ {
			fmt.Printf("%d ", n-i+1)
		}
		fmt.Println()
	} else {
		fmt.Println("-1")
	}
}

package main

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

func main() {
	var s []uint8
	var n uint8
	in := bufio.NewReader(os.Stdin)
	fmt.Fscanf(in, "%d\n", &n)
	if n%2 != 0 {
		fmt.Println("-1")
		return
	}
	for i := uint8(1); i <= n; i++ {
		s = append(s, i)
	}
	for i := uint8(0); i < n-1; i += 2 {
		t := s[i]
		s[i] = s[i+1]
		s[i+1] = t
	}
	for i := uint8(0); i < n; i++ {
		fmt.Print(s[i], " ")
	}
}

package main

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

func main() {
	n := readi()
	fmt.Fprintln(writer, perfectPermutation(n))
	writer.Flush()
}

func perfectPermutation(n int) string {
	if n%2 == 1 {
		return "-1"
	}
	values := make([]byte, 0)
	for i := 0; i < n; i++ {
		values = append(values, byte(n-i)+'0')
		values = append(values, ' ')
	}
	return string(values)
}

func isNextPrime(n, m int) bool {
	for i := n + 1; i < m; i++ {
		if isPrime(i) {
			return false
		}
	}
	return isPrime(m)
}

func isPrime(n int) bool {
	for x := 2; x*x <= n; x++ {
		if n%x == 0 {
			return false
		}
	}
	return n > 1
}

func withSameCase(word string) string {
	lowerCount := 0
	for _, letter := range word {
		if unicode.IsLower(letter) {
			lowerCount++
		}
	}
	if lowerCount*2 >= len(word) {
		return strings.ToLower(word)
	}
	return strings.ToUpper(word)
}

func solve(matrix [][]int) []string {
	n := 3
	state := make([][]byte, n)
	for i := range state {
		state[i] = make([]byte, n)
		for j := range matrix[i] {
			state[i][j] = byte(matrix[i][j])
			if i > 0 {
				state[i][j] += byte(matrix[i-1][j])
			}
			if i+1 < n {
				state[i][j] += byte(matrix[i+1][j])
			}
			if j > 0 {
				state[i][j] += byte(matrix[i][j-1])
			}
			if j+1 < n {
				state[i][j] += byte(matrix[i][j+1])
			}
			state[i][j] &= 1
			state[i][j] ^= 1
			state[i][j] += '0'
		}
	}
	res := make([]string, n)
	for i := range res {
		res[i] = string(state[i])
	}
	return res
}

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

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

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

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

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

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

var (
	reader = bufio.NewReaderSize(os.Stdin, 1<<13)
	writer = bufio.NewWriterSize(os.Stdout, 1<<13)
)

func readi() int {
	return int(readi64())
}

func read2i() (int, int) {
	return readi(), readi()
}

func read3i() (int, int, int) {
	return readi(), readi(), readi()
}

func read4i() (int, int, int, int) {
	return readi(), readi(), readi(), readi()
}

func readis(n int) []int {
	res := make([]int, n)
	for i := range res {
		res[i] = readi()
	}
	return res
}

func readi64() int64 {
	b, err := reader.ReadByte()
	for !isValid(b, err) {
		b, err = reader.ReadByte()
	}
	sign, res := int64(1), int64(0)
	if b == '-' {
		sign *= -1
		b, err = reader.ReadByte()
	}
	for isValid(b, err) {
		res = res*10 + int64(b-'0')
		b, err = reader.ReadByte()
	}
	return res * sign
}

func readi64s(n int) []int64 {
	res := make([]int64, n)
	for i := range res {
		res[i] = readi64()
	}
	return res
}

func readline() string {
	res := new(bytes.Buffer)
	for {
		line, isPrefix, err := reader.ReadLine()
		if err != nil {
			panic(err)
		}
		res.Write(line)
		if !isPrefix {
			break
		}
	}
	return res.String()
}

func isValid(b byte, err error) bool {
	return err == nil && (('0' <= b && b <= '9') || b == '-')
}

package main

import (
	"fmt"
	"os"
)

func main() {
	var n, i uint8

	fmt.Scan(&n)

	if n == 1 {
		fmt.Print("-1\n")
		os.Exit(0)
	}

	for i = 1; i <= n; i++ {
		fmt.Print(n-i+1, " ")
	}
	fmt.Print("\n")
}

package main

import (
	"fmt"
	"os"
)

func main() {
	var n, i uint8

	fmt.Scan(&n)

	if n == 1 {
		fmt.Print("-1\n")
		os.Exit(0)
	}

	if n%2 == 0 {
		for i = 1; i <= n; i++ {
			fmt.Print(n-i+1, " ")
		}
	} else {
		for i = 2; i <= n; i++ {
			fmt.Print(i, " ")
		}
		fmt.Print(1)
	}
	fmt.Print("\n")
}


package main

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

var N = 100001

func LeftChild(node int64) int64 {
	return node * 2
}

func RightChild(node int64) int64 {
	return node*2 + 1
}
func IntAbs(val int64) int64 {
	if val < 0 {
		return -val
	}
	return val
}

type IntervalTree struct {
	color []int64
	left  []int64
	right []int64
	sum   []int64
	delta []int64
}

func (tree *IntervalTree) Update(node int64) {
	if tree.delta[node] == 0 {
		return
	}
	var left_child = LeftChild(node)
	var right_child = RightChild(node)
	var mid = (tree.left[node] + tree.right[node]) / 2

	tree.delta[left_child] += tree.delta[node]
	tree.sum[left_child] += (mid - tree.left[node] + 1) * tree.delta[node]
	tree.color[left_child] = tree.color[node]

	tree.delta[right_child] += tree.delta[node]
	tree.sum[right_child] += (tree.right[node] - mid) * tree.delta[node]
	tree.color[right_child] = tree.color[node]
	tree.color[node] = -1
	tree.delta[node] = 0
}

func (tree *IntervalTree) Collect(node int64) {
	var left_child = LeftChild(node)
	var right_child = RightChild(node)
	if tree.color[left_child] == tree.color[right_child] {
		tree.color[node] = tree.color[left_child]
	} else {
		tree.color[node] = -1
	}
	tree.sum[node] = tree.sum[left_child] + tree.sum[right_child]
}

func (tree *IntervalTree) InitDetail(l, r, node int64) {
	tree.left[node] = l
	tree.right[node] = r
	tree.sum[node] = 0
	tree.delta[node] = 0
	if l == r {
		tree.color[node] = l
		return
	}
	var mid = (l + r) / 2
	tree.InitDetail(l, mid, LeftChild(node))
	tree.InitDetail(mid+1, r, RightChild(node))
	tree.Collect(node)
}

func (tree *IntervalTree) Init(n int64) {
	tree.color = make([]int64, n*4)
	tree.left = make([]int64, n*4)
	tree.right = make([]int64, n*4)
	tree.sum = make([]int64, n*4)
	tree.delta = make([]int64, n*4)
	tree.InitDetail(1, n, 1)
}

func (tree *IntervalTree) InsertDetail(l, r, node, val int64) {
	if tree.left[node] == l && r == tree.right[node] && tree.color[node] != -1 {
		tree.delta[node] += IntAbs(val - tree.color[node])
		tree.sum[node] += (r - l + 1) * IntAbs(val-tree.color[node])
		tree.color[node] = val
		return
	}
	tree.Update(node)
	var mid = (tree.left[node] + tree.right[node]) / 2
	if r <= mid {
		tree.InsertDetail(l, r, LeftChild(node), val)
	} else if mid < l {
		tree.InsertDetail(l, r, RightChild(node), val)
	} else {
		tree.InsertDetail(l, mid, LeftChild(node), val)
		tree.InsertDetail(mid+1, r, RightChild(node), val)
	}
	tree.Collect(node)
}

func (tree *IntervalTree) Insert(l, r, val int64) {
	tree.InsertDetail(l, r, 1, val)
}

func (tree *IntervalTree) Query(l, r, node int64) int64 {
	if tree.left[node] == l && tree.right[node] == r {
		return tree.sum[node]
	}
	tree.Update(node)
	var mid = (tree.left[node] + tree.right[node]) / 2
	var res int64 = 0
	if r <= mid {
		res = tree.Query(l, r, LeftChild(node))
	} else if mid < l {
		res = tree.Query(l, r, RightChild(node))
	} else {
		res = tree.Query(l, mid, LeftChild(node)) +
			tree.Query(mid+1, r, RightChild(node))
	}
	tree.Collect(node)
	return res
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, m int64
	for sval, _ := fmt.Fscan(reader, &n, &m); sval == 2; {
		var tree IntervalTree
		tree.Init(n)
		for i := int64(0); i < m; i++ {
			var query, l, r, val int64
			fmt.Fscan(reader, &query, &l, &r)
			if query == 2 {
				fmt.Println(tree.Query(l, r, 1))
			} else {
				fmt.Fscan(reader, &val)
				tree.Insert(l, r, val)
			}
		}
		sval, _ = fmt.Fscan(reader, &n, &m)
	}
}

package main

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

type Fenwick []int64
func (f Fenwick) add(i int, x int64) {
	// Increases v[i] by x
	i += 1
	for i < len(f) {
		f[i] += x
		i += i & -i
	}
}
func (f Fenwick) read(i int) int64 {
	// Returns sum v[0:i)
	val := int64(0)
	for i != 0 {
		val += f[i]
		i -= i & -i
	}
	return val
}
type Fenwick2 struct {
	plu Fenwick
	mul Fenwick
}
func (f Fenwick2) add(j int, x int64) {
	// Increase v[j:n) by x
	f.plu.add(j, -int64(j)*x)
	f.mul.add(j, x)
}
func (f Fenwick2) read(i int) int64 {
	// Returns sum v[0:i)
	return f.plu.read(i) + int64(i) * f.mul.read(i)
}
func (f Fenwick2) print() {
	fmt.Print("Colorfullnes ")
	for i := 0; i < len(f.plu)-1; i++ {
		fmt.Print(f.read(i+1)-f.read(i), " ")
	}
	fmt.Println()
}

func abs (x int64) int64 { if x < 0 {return -x} else {return x} }
func min (x, y int) int { if x < y {return x} else {return y} }
func max (x, y int) int { if x < y {return y} else {return x} }

type entry struct {
	full bool
	color int64
}
type Segment []entry
func (t Segment) update_distance(l, r, a, b int, color int64) {
	if t[a + b].full {
		dif := abs(t[a + b].color - color)
		// fmt.Println("fen upd", max(l, a), min(r, b), ":", dif)
		fen.add(max(l, a), dif)
		// fmt.Println(fen)
		fen.add(min(r, b), -dif)
		// fen.print()
		// fmt.Println
		// fmt.Println(fen)
	} else {
		c := (a + b)/2
		if l < c {
			t.update_distance(l, r, a, c, color)
		}
		if c < r {
			t.update_distance(l, r, c, b, color)
		}
	}
}
func (t Segment) push_down(a, b int) {
	t[a + b].full = false
	c := (a + b)/2
	t[a + c].full = true
	t[a + c].color = t[a + b].color
	t[c + b].full = true
	t[c + b].color = t[a + b].color
}
func (t Segment) color(l, r, a, b int, color int64) {
	if l <= a && b <= r {
		// If it covers
		t[a + b].color = color
		t[a + b].full = true
	} else {
		if t[a + b].full {
			t.push_down(a, b)
		}
		c := (a + b)/2
		if l < c {
			t.color(l, r, a, c, color)
		}
		if c < r {
			t.color(l, r, c, b, color)
		}
	}
}

var (
	fen Fenwick2
	seg Segment
	simple []int64
)

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

	var n, m int
	fmt.Fscan(in, &n, &m)
	fen = Fenwick2{make(Fenwick, n+1), make(Fenwick, n+1)}
	// simple = make([]int64, n+1)
	// fmt.Println("hvad", fen, " ", len(fen.plu), " ", len(fen.mul))
	seg = make(Segment, 4*n+5)
	for i := 0; i < n; i++ {
		seg[i+i+1].full = true
		seg[i+i+1].color = int64(i+1)
		// simple[i] = int64(i+1)
	}
	var nn = 1
	for nn < n {
		nn *= 2
	}
	// fmt.Println(seg)
	var typ, l, r int
	var x int64
	for i := 0; i < m; i++ {
		fmt.Fscan(in, &typ)
		if typ == 1 {
			fmt.Fscan(in, &l, &r, &x)
			// fmt.Println("Color", l-1, "to", r, "=", x)
			seg.update_distance(l-1, r, 0, nn, x)
			// for j := l-1; j < r; j++ {
			// 	simple[j] = x
			// }
			// fmt.Println(simple)
			// fen.print()
			seg.color(l-1, r, 0, nn, x)
			// fmt.Println(seg)
		} else {
			fmt.Fscan(in, &l, &r)
			// fmt.Println("Read", l-1, "to", r)
			fmt.Fprintln(out, fen.read(r) - fen.read(l-1))
		}
	}
}

package main

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

var N = 100001

func LeftChild(node int64) int64 {
	return node * 2
}

func RightChild(node int64) int64 {
	return node*2 + 1
}
func IntAbs(val int64) int64 {
	if val < 0 {
		return -val
	}
	return val
}

type IntervalTree struct {
	color []int64
	left  []int64
	right []int64
	sum   []int64
}

func (tree *IntervalTree) Update(node int64) {
	if tree.color[node] == -1 {
		return
	}
	var left_child = LeftChild(node)
	var right_child = RightChild(node)
	var mid = (tree.left[node] + tree.right[node]) / 2

	tree.sum[left_child] += (mid - left_child + 1) *
		IntAbs(tree.color[node]-tree.color[left_child])
	tree.color[left_child] = tree.color[node]

	tree.sum[right_child] += (right_child - mid) *
		IntAbs(tree.color[node]-tree.color[right_child])
	tree.color[right_child] = tree.color[node]
	tree.color[node] = -1
}

func (tree *IntervalTree) Collect(node int64) {
	var left_child = LeftChild(node)
	var right_child = RightChild(node)
	if tree.color[left_child] == tree.color[right_child] {
		tree.color[node] = tree.color[left_child]
	} else {
		tree.color[node] = -1
	}
	tree.sum[node] = tree.sum[left_child] + tree.sum[right_child]
}

func (tree *IntervalTree) InitDetail(l, r, node int64) {
	tree.left[node] = l
	tree.right[node] = r
	tree.sum[node] = 0
	if l == r {
		tree.color[node] = l
		return
	}
	var mid = (l + r) / 2
	tree.InitDetail(l, mid, LeftChild(node))
	tree.InitDetail(mid+1, r, RightChild(node))
	tree.Collect(node)
}

func (tree *IntervalTree) Init(n int64) {
	tree.color = make([]int64, n*4)
	tree.left = make([]int64, n*4)
	tree.right = make([]int64, n*4)
	tree.sum = make([]int64, n*4)
	tree.InitDetail(1, n, 1)
}

func (tree *IntervalTree) InsertDetail(l, r, node, val int64) {
	if tree.left[node] == l && r == tree.right[node] && tree.color[node] != -1 {
		tree.sum[node] += (r - l + 1) * IntAbs(val-tree.color[node])
		tree.color[node] = val
		return
	}
	tree.Update(node)
	var mid = (tree.left[node] + tree.right[node]) / 2
	if r <= mid {
		tree.InsertDetail(l, r, LeftChild(node), val)
	} else if mid < l {
		tree.InsertDetail(l, r, RightChild(node), val)
	} else {
		tree.InsertDetail(l, mid, LeftChild(node), val)
		tree.InsertDetail(mid+1, r, RightChild(node), val)
	}
	tree.Collect(node)
}

func (tree *IntervalTree) Insert(l, r, val int64) {
	tree.InsertDetail(l, r, 1, val)
}

func (tree *IntervalTree) Query(l, r, node int64) int64 {
	if tree.left[node] == l && tree.right[node] == r {
		return tree.sum[node]
	}
	tree.Update(node)
	var mid = (tree.left[node] + tree.right[node]) / 2
	var res int64 = 0
	if r <= mid {
		res = tree.Query(l, r, LeftChild(node))
	} else if mid < l {
		res = tree.Query(l, r, RightChild(node))
	} else {
		res = tree.Query(l, mid, LeftChild(node)) +
			tree.Query(mid+1, r, RightChild(node))
	}
	tree.Collect(node)
	return res
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, m int64
	for sval, _ := fmt.Fscan(reader, &n, &m); sval == 2; {
		var tree IntervalTree
		tree.Init(n)
		for i := int64(0); i < m; i++ {
			var query, l, r, val int64
			fmt.Fscan(reader, &query, &l, &r)
			if query == 2 {
				fmt.Println(tree.Query(l, r, 1))
			} else {
				fmt.Fscan(reader, &val)
				tree.Insert(l, r, val)
			}
		}
		sval, _ = fmt.Fscan(reader, &n, &m)
	}
}

package main

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

var N = 100001

func LeftChild(node int) int {
	return node * 2
}

func RightChild(node int) int {
	return node*2 + 1
}
func IntAbs(val int) int {
	if val < 0 {
		return -val
	}
	return val
}

type IntervalTree struct {
	color []int
	left  []int
	right []int
	sum   []int
}

func (tree *IntervalTree) Update(node int) {
	if tree.color[node] == -1 {
		return
	}
	var left_child = LeftChild(node)
	var right_child = RightChild(node)
	var mid = (tree.left[node] + tree.right[node]) / 2
	if tree.color[left_child] != -1 {
		tree.sum[left_child] += (mid - left_child + 1) *
			IntAbs(tree.color[node]-tree.color[left_child])
	}
	tree.color[left_child] = tree.color[node]
	if tree.color[right_child] != -1 {
		tree.sum[right_child] += (right_child - mid) *
			IntAbs(tree.color[node]-tree.color[right_child])
	}
	tree.color[right_child] = tree.color[node]
}

func (tree *IntervalTree) Collect(node int) {
	var left_child = LeftChild(node)
	var right_child = RightChild(node)
	if tree.color[left_child] == tree.color[right_child] {
		tree.color[node] = tree.color[left_child]
	} else {
		tree.color[node] = -1
	}
	tree.sum[node] = tree.sum[left_child] + tree.sum[right_child]
}

func (tree *IntervalTree) InitDetail(l, r, node int) {
	tree.left[node] = l
	tree.right[node] = r
	tree.sum[node] = 0
	if l == r {
		tree.color[node] = l
		return
	}
	var mid = (l + r) / 2
	tree.InitDetail(l, mid, LeftChild(node))
	tree.InitDetail(mid+1, r, RightChild(node))
	tree.Collect(node)
}

func (tree *IntervalTree) Init(n int) {
	tree.color = make([]int, n*4)
	tree.left = make([]int, n*4)
	tree.right = make([]int, n*4)
	tree.sum = make([]int, n*4)
	tree.InitDetail(1, n, 1)
}

func (tree *IntervalTree) InsertDetail(l, r, node, val int) {
	if tree.left[node] == l && r == tree.right[node] && tree.color[node] != -1 {
		tree.sum[node] += (r - l + 1) * IntAbs(val-tree.color[node])
		tree.color[node] = val
		return
	}
	tree.Update(node)
	var mid = (tree.left[node] + tree.right[node]) / 2
	if r <= mid {
		tree.InsertDetail(l, r, LeftChild(node), val)
	} else if mid < l {
		tree.InsertDetail(l, r, RightChild(node), val)
	} else {
		tree.InsertDetail(l, mid, LeftChild(node), val)
		tree.InsertDetail(mid+1, r, RightChild(node), val)
	}
	tree.Collect(node)
}

func (tree *IntervalTree) Insert(l, r, val int) {
	tree.InsertDetail(l, r, 1, val)
}

func (tree *IntervalTree) Query(l, r, node int) int {
	if tree.left[node] == l && tree.right[node] == r {
		return tree.sum[node]
	}
	tree.Update(node)
	var mid = (tree.left[node] + tree.right[node]) / 2
	var res int = 0
	if r <= mid {
		res = tree.Query(l, r, LeftChild(node))
	} else if mid < l {
		res = tree.Query(l, r, RightChild(node))
	} else {
		res = tree.Query(l, mid, LeftChild(node)) +
			tree.Query(mid+1, r, RightChild(node))
	}
	tree.Collect(node)
	return res
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, m int
	for sval, _ := fmt.Fscan(reader, &n, &m); sval == 2; {
		var tree IntervalTree
		tree.Init(n)
		for i := 0; i < m; i++ {
			var query, l, r, val int
			fmt.Fscan(reader, &query, &l, &r)
			if query == 2 {
				fmt.Println(tree.Query(l, r, 1))
			} else {
				fmt.Fscan(reader, &val)
				tree.Insert(l, r, val)
			}
		}
		sval, _ = fmt.Fscan(reader, &n, &m)
	}
}

package main

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

var N = 100001

func LeftChild(node int) int {
	return node * 2
}

func RightChild(node int) int {
	return node*2 + 1
}
func IntAbs(val int) int {
	if val < 0 {
		return -val
	}
	return val
}

type IntervalTree struct {
	color []int
	left  []int
	right []int
	sum   []int
}

func (tree *IntervalTree) Update(node int) {
	if tree.color[node] == -1 {
		return
	}
	var left_child = LeftChild(node)
	var right_child = RightChild(node)
	var mid = (tree.left[node] + tree.right[node]) / 2
	tree.sum[left_child] += (mid - left_child + 1) *
		IntAbs(tree.color[node]-tree.color[left_child])
	tree.color[left_child] = tree.color[node]
	tree.sum[right_child] += (right_child - mid) *
		IntAbs(tree.color[node]-tree.color[right_child])
	tree.color[right_child] = tree.color[node]
}

func (tree *IntervalTree) Collect(node int) {
	var left_child = LeftChild(node)
	var right_child = RightChild(node)
	if tree.color[left_child] == tree.color[right_child] {
		tree.color[node] = tree.color[left_child]
	} else {
		tree.color[node] = -1
	}
	tree.sum[node] = tree.sum[left_child] + tree.sum[right_child]
}

func (tree *IntervalTree) InitDetail(l, r, node int) {
	tree.left[node] = l
	tree.right[node] = r
	tree.sum[node] = 0
	if l == r {
		tree.color[node] = l
		return
	}
	var mid = (l + r) / 2
	tree.InitDetail(l, mid, LeftChild(node))
	tree.InitDetail(mid+1, r, RightChild(node))
	tree.Collect(node)
}

func (tree *IntervalTree) Init(n int) {
	tree.color = make([]int, n*4)
	tree.left = make([]int, n*4)
	tree.right = make([]int, n*4)
	tree.sum = make([]int, n*4)
	tree.InitDetail(1, n, 1)
}

func (tree *IntervalTree) InsertDetail(l, r, node, val int) {
	if tree.left[node] == l && r == tree.right[node] && tree.color[node] != -1 {
		tree.sum[node] += (r - l + 1) * IntAbs(val-tree.color[node])
		tree.color[node] = val
		return
	}
	tree.Update(node)
	var mid = (tree.left[node] + tree.right[node]) / 2
	if r <= mid {
		tree.InsertDetail(l, r, LeftChild(node), val)
	} else if mid < l {
		tree.InsertDetail(l, r, RightChild(node), val)
	} else {
		tree.InsertDetail(l, mid, LeftChild(node), val)
		tree.InsertDetail(mid+1, r, RightChild(node), val)
	}
	tree.Collect(node)
}

func (tree *IntervalTree) Insert(l, r, val int) {
	tree.InsertDetail(l, r, 1, val)
}

func (tree *IntervalTree) Query(l, r, node int) int {
	if tree.left[node] == l && tree.right[node] == r {
		return tree.sum[node]
	}
	tree.Update(node)
	var mid = (tree.left[node] + tree.right[node]) / 2
	var res int = 0
	if r <= mid {
		res = tree.Query(l, r, LeftChild(node))
	} else if mid < l {
		res = tree.Query(l, r, RightChild(node))
	} else {
		res = tree.Query(l, mid, LeftChild(node)) +
			tree.Query(mid+1, r, RightChild(node))
	}
	tree.Collect(node)
	return res
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, m int
	for sval, _ := fmt.Fscan(reader, &n, &m); sval == 2; {
		var tree IntervalTree
		tree.Init(n)
		for i := 0; i < m; i++ {
			var query, l, r, val int
			fmt.Fscan(reader, &query, &l, &r)
			if query == 2 {
				fmt.Println(tree.Query(l, r, 1))
			} else {
				fmt.Fscan(reader, &val)
				tree.Insert(l, r, val)
			}
		}
		sval, _ = fmt.Fscan(reader, &n, &m)
	}
}


package main

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

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

const inf = 1000000000

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

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

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

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

	sum := 0
	least := -inf
	most := inf
	for i := 0; i < n; i++ {
		fmt.Fscanf(bufin, "%d %d\n", &c, &d)
		if d == 1 {
			least = max(least, 1900-sum)
		} else {
			most = min(most, 1899-sum)
		}
		sum += c
	}
	if least > most {
		fmt.Fprintf(bufout, "Impossible\n")
	} else if most == inf {
		fmt.Fprintf(bufout, "Infinity\n")
	} else {
		r := most + sum
		fmt.Fprintf(bufout, "%d\n", r)
	}
}

package main

import (
        "bufio"
        "fmt"
        "os"
        "strconv"
)
func solve() {
    const inf = 1000*1000*1000
    acum := 0
    mx, mn := inf, -inf
    for q := readInt(); q > 0; q-- {
        c, d := readInt(), readInt()
        if d == 2 {
            mx = min(mx, -1*acum-1)
        } else {
            mn = max(mn, -acum)
        }
        acum += c
    }
    if mn > mx {
        fmt.Println("Impossible")
    } else if mx == inf {
        fmt.Println("Infinity")
    } else {
        fmt.Println(mx+1900+acum)
    }
}

// Helpers

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)

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

// Math

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

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

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

// IO

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

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

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

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

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

// Sort

type Ints64 []int64

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

/* Sort tempalte
func (a ) Len() int           { return len(a) }
func (a ) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ) Less(i, j int) bool { return  }
*/

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

func main(){
    in := bufio.NewReader(os.Stdin)
    var n int;
    for true{
        if items,_:=fmt.Fscanf(in, "%d\n", &n);items!=1{
            break;
        }
        S:=0;
        var div1,div2 []int;
        for i:=0;i<n;i++{
            var c,d int;
            fmt.Fscanf(in, "%d %d\n", &c, &d)
            if d==1{
                div1=append(div1,S);
            }else{
                div2=append(div2,S);
            }
            S+=c;
        }
        sort.Ints(div1);
        sort.Ints(div2);
        if len(div2)==0 {
            fmt.Println("Infinity");
            continue;
        }
        //fmt.Println(div2,div1);
        if !(len(div1)==0||div2[(len(div2)-1)]<div1[0]){
            fmt.Println("Impossible");
            continue;
        }
        fmt.Println(1899+S-div2[(len(div2)-1)]);
    }
}

package main

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

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

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

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

	allOnes := true
	for i := 0; i < n; i++ {
		if d[i] == 2 {
			allOnes = false
			break
		}
	}
	if allOnes {
		fmt.Println("Infinity")
		return
	}

	r := make([]int, n+1)
	r[0] = 0
	for i := 1; i <= n; i++ {
		r[i] = r[i-1] + c[i-1]
	}

	maxDiv2 := -1
	for i := 0; i < n; i++ {
		if d[i] == 2 {
			if maxDiv2 == -1 {
				maxDiv2 = i
			} else if r[i] > r[maxDiv2] {
				maxDiv2 = i
			}
		}
	}

	delta := 1899 - r[maxDiv2]
	for i := 0; i <= n; i++ {
		r[i] += delta
	}

	conflict := false
	for i := 0; i < n; i++ {
		if d[i] == 1 {
			if r[i] < 1900 {
				conflict = true
				break
			}
		} else {
			if r[i] >= 1900 {
				conflict = true
				break
			}
		}
	}
	if conflict {
		fmt.Println("Impossible")
		return
	}

	fmt.Println(r[n])
}

package main

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

const INF = int64(10000000000)

func max(x, y int64) int64 {
	if x > y {
		return x
	}
	return y
}

func min(x, y int64) int64 {
	if x < y {
		return x
	}
	return y

}

func main() {
	b, _ := ioutil.ReadAll(os.Stdin)
	r := bytes.NewReader(b)

	var n int64

	fmt.Fscanf(r, "%v\n", &n)

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

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

	var left, right int64

	if d[0] == 1 {
		left, right = int64(1900), INF
	} else {
		left, right = -INF, int64(1899)
	}

	var mark bool

	for i := int64(1); i < n; i++ {
		left += c[i-1]
		right += c[i-1]

		if d[i] == 1 {
			left = max(1900, left)
			//right = min(right, INF)
		} else {
			right = min(1899, right)
			//left = max(0, left)
		}

		//fmt.Printf("%v %v\n", left, right)

		if left > right {
			mark = true
		}

	}

	if mark {
		fmt.Printf("Impossible\n")
	} else if right >= INF/2 {
		fmt.Printf("Infinity\n")
	} else {
		fmt.Printf("%v\n", right+c[n-1])
	}

}

package main

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

func I(i interface{}, delim string) error {
	_, err := fmt.Fscanf(r, "%v"+delim, i)
	return err
}

func O(o interface{}, delim string) {
	fmt.Fprintf(w, "%v"+delim, o)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func main() {
	if f, err := os.Open("in.txt"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var n int
	for I(&n, "\n") == nil {
		solve(n)
	}
}
func solve(n int) {
	delta := 0
	max := 1000000000
	min := -max
	for i := 0; i < n; i++ {
		var a, b int
		I(&a, " ")
		I(&b, "\n")
		if b == 1 {
			if 1900-delta > min {
				min = 1900 - delta
			}
		} else {
			if 1899-delta < max {
				max = 1899 - delta
			}
		}
		delta += a
	}
	if max >= 1000000000 {
		O("Infinity", "\n")
	} else if min > max {
		O("Impossible", "\n")
	} else {
		O(max+delta, "\n")
	}
}

package main

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

func I(i interface{}, delim string) error {
	_, err := fmt.Fscanf(r, "%v"+delim, i)
	return err
}

func O(o interface{}, delim string) {
	fmt.Fprintf(w, "%v"+delim, o)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func main() {
	if f, err := os.Open("in.txt"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var n int
	for I(&n, "\n") == nil {
		solve(n)
	}
}
func solve(n int) {
	delta := 0
	max := 100000
	min := -max
	for i := 0; i < n; i++ {
		var a, b int
		I(&a, " ")
		I(&b, "\n")
		if b == 1 {
			if 1900-delta > min {
				min = 1900 - delta
			}
		} else {
			if 1899-delta < max {
				max = 1899 - delta
			}
		}
		delta += a
	}
	if max >= 100000 {
		O("Infinity", "\n")
	} else if min > max {
		O("Impossible", "\n")
	} else {
		O(max+delta, "\n")
	}
}

package main

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

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

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

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

	allOnes := true
	for i := 0; i < n; i++ {
		if d[i] == 2 {
			allOnes = false
			break
		}
	}
	if allOnes {
		fmt.Println("Infinity")
		return
	}

	r := make([]int, n+1)
	r[0] = 0
	for i := 1; i <= n; i++ {
		r[i] = r[i-1] + c[i-1]
	}

	maxDiv2 := -1
	for i := 0; i < n; i++ {
		if d[i] == 2 {
			if maxDiv2 == -1 {
				maxDiv2 = i
			} else if r[i] > r[maxDiv2] {
				maxDiv2 = i
			}
		}
	}

	delta := 1899 - r[maxDiv2]
	for i := 0; i <= n; i++ {
		r[i] += delta
	}

	conflict := false
	for i := 0; i < n; i++ {
		if d[i] == 1 {
			if r[i] < 1900 {
				conflict = true
				break
			}
		} else {
			if r[i] >= 1900 {
				conflict = true
				break
			}
		}
	}
	if conflict {
		fmt.Println("Impossible")
		return
	}

	fmt.Println(r[n])
}

package main

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

const INF = int64(10000000000)

func max(x, y int64) int64 {
	if x > y {
		return x
	}
	return y
}

func min(x, y int64) int64 {
	if x < y {
		return x
	}
	return y

}

func main() {
	b, _ := ioutil.ReadAll(os.Stdin)
	r := bytes.NewReader(b)

	var n int64

	fmt.Fscanf(r, "%v\n", &n)

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

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

	var left, right int64

	if d[0] == 1 {
		left, right = int64(1900), INF
	} else {
		left, right = -INF, int64(1899)
	}

	var mark bool

	for i := int64(1); i < n; i++ {
		left += c[i-1]
		right += c[i-1]

		if d[i] == 1 {
			left = max(1900, left)
			//right = min(right, INF)
		} else {
			right = min(1899, right)
			//left = max(0, left)
		}

		//fmt.Printf("%v %v\n", left, right)

		if left > right {
			mark = true
		}

	}

	if mark {
		fmt.Printf("Impossible\n")
	} else if right >= INF {
		fmt.Printf("Infinity\n")
	} else {
		fmt.Printf("%v\n", right+c[n-1])
	}

}

package main

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

const INF = int64(10000000000)

func max(x, y int64) int64 {
	if x > y {
		return x
	}
	return y
}

func min(x, y int64) int64 {
	if x < y {
		return x
	}
	return y

}

func main() {
	b, _ := ioutil.ReadAll(os.Stdin)
	r := bytes.NewReader(b)

	var n int64

	fmt.Fscanf(r, "%v\n", &n)

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

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

	var left, right int64

	if d[0] == 1 {
		left, right = int64(1900), INF
	} else {
		left, right = int64(0), int64(1899)
	}

	var mark bool

	for i := int64(1); i < n; i++ {
		left += c[i-1]
		right += c[i-1]

		if d[i] == 1 {
			left = max(1900, left)
			//right = min(right, INF)
		} else {
			right = min(1899, right)
			left = max(0, left)
		}

		//fmt.Printf("%v %v\n", left, right)

		if left > right {
			mark = true
		}

	}

	if mark {
		fmt.Printf("Impossible\n")
	} else if right >= INF {
		fmt.Printf("Infinity\n")
	} else {
		fmt.Printf("%v\n", right+c[n-1])
	}

}

package main

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

const INF = int64(10000000000)

func max(x, y int64) int64 {
	if x > y {
		return x
	}
	return y
}

func min(x, y int64) int64 {
	if x < y {
		return x
	}
	return y

}

func main() {
	b, _ := ioutil.ReadAll(os.Stdin)
	r := bytes.NewReader(b)

	var n int64

	fmt.Fscanf(r, "%v\n", &n)

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

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

	var left, right int64

	if d[0] == 1 {
		left, right = int64(1900), INF
	} else {
		left, right = int64(0), int64(1899)
	}

	var mark bool

	for i := int64(1); i < n; i++ {
		left += c[i-1]
		right += c[i-1]

		if d[i] == 1 {
			left = max(1900, left)
			right = min(right, INF)
		} else {
			right = min(1899, right)
			left = max(0, left)
		}

		//fmt.Printf("%v %v\n", left, right)

		if left > right {
			mark = true
		}

	}

	if mark {
		fmt.Printf("Impossible\n")
	} else if right >= INF {
		fmt.Printf("Infinity\n")
	} else {
		fmt.Printf("%v\n", right+c[n-1])
	}

}