Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There are n showplaces in the city, numbered from 1 to n, and some of them are connected by one-directional roads. The roads in Berlatov are designed in a way such that there are no cyclic routes between showplaces.Initially Irina stands at the showplace 1, and the endpoint of her journey is the showplace n. Naturally, Irina wants to visit as much showplaces as she can during her journey. However, Irina's stay in Berlatov is limited and she can't be there for more than T time units.Help Irina determine how many showplaces she may visit during her journey from showplace 1 to showplace n within a time not exceeding T. It is guaranteed that there is at least one route from showplace 1 to showplace n such that Irina will spend no more than T time units passing it.

The first line of the input contains three integers n, m and T (2 ≤ n ≤ 5000,  1 ≤ m ≤ 5000,  1 ≤ T ≤ 109) — the number of showplaces, the number of roads between them and the time of Irina's stay in Berlatov respectively. The next m lines describes roads in Berlatov. i-th of them contains 3 integers ui, vi, ti (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ ti ≤ 109), meaning that there is a road starting from showplace ui and leading to showplace vi, and Irina spends ti time units to pass it. It is guaranteed that the roads do not form cyclic routes. It is guaranteed, that there is at most one road between each pair of showplaces.

Print the single integer k (2 ≤ k ≤ n) — the maximum number of showplaces that Irina can visit during her journey from showplace 1 to showplace n within time not exceeding T, in the first line. Print k distinct integers in the second line — indices of showplaces that Irina will visit on her route, in the order of encountering them. If there are multiple answers, print any of them.

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

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


A permutation is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. Let's denote the i-th element of permutation p as pi. We'll call number n the size of permutation p1, p2, ..., pn.Nickolas adores permutations. He likes some permutations more than the others. He calls such permutations perfect. A perfect permutation is such permutation p that for any i (1 ≤ i ≤ n) (n is the permutation size) the following equations hold ppi = i and pi ≠ i. Nickolas asks you to print any perfect permutation of size n for the given n.

A single line contains a single integer n (1 ≤ n ≤ 100) — the permutation size.

If a perfect permutation of size n doesn't exist, print a single integer -1. Otherwise print n distinct integers from 1 to n, p1, p2, ..., pn — permutation p, that is perfect. Separate printed numbers by whitespaces.

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


DZY loves colors, and he enjoys painting.On a colorful day, DZY gets a colorful ribbon, which consists of n units (they are numbered from 1 to n from left to right). The color of the i-th unit of the ribbon is i at first. It is colorful enough, but we still consider that the colorfulness of each unit is 0 at first.DZY loves painting, we know. He takes up a paintbrush with color x and uses it to draw a line on the ribbon. In such a case some contiguous units are painted. Imagine that the color of unit i currently is y. When it is painted by this paintbrush, the color of the unit becomes x, and the colorfulness of the unit increases by |x - y|.DZY wants to perform m operations, each operation can be one of the following:  Paint all the units with numbers between l and r (both inclusive) with color x.  Ask the sum of colorfulness of the units between l and r (both inclusive). Can you help DZY?

The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105). Each of the next m lines begins with a integer type (1 ≤ type ≤ 2), which represents the type of this operation. If type = 1, there will be 3 more integers l, r, x (1 ≤ l ≤ r ≤ n; 1 ≤ x ≤ 108) in this line, describing an operation 1. If type = 2, there will be 2 more integers l, r (1 ≤ l ≤ r ≤ n) in this line, describing an operation 2.

For each operation 2, print a line containing the answer — sum of colorfulness.

NoteIn the first sample, the color of each unit is initially [1, 2, 3], and the colorfulness is [0, 0, 0].After the first operation, colors become [4, 4, 3], colorfulness become [3, 2, 0].After the second operation, colors become [4, 5, 5], colorfulness become [3, 3, 2].So the answer to the only operation of type 2 is 8.

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


Every Codeforces user has rating, described with one integer, possibly negative or zero. Users are divided into two divisions. The first division is for users with rating 1900 or higher. Those with rating 1899 or lower belong to the second division. In every contest, according to one's performance, his or her rating changes by some value, possibly negative or zero.Limak competed in n contests in the year 2016. He remembers that in the i-th contest he competed in the division di (i.e. he belonged to this division just before the start of this contest) and his rating changed by ci just after the contest. Note that negative ci denotes the loss of rating.What is the maximum possible rating Limak can have right now, after all n contests? If his rating may be arbitrarily big, print "Infinity". If there is no scenario matching the given information, print "Impossible".

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000). The i-th of next n lines contains two integers ci and di ( - 100 ≤ ci ≤ 100, 1 ≤ di ≤ 2), describing Limak's rating change after the i-th contest and his division during the i-th contest contest.

If Limak's current rating can be arbitrarily big, print "Infinity" (without quotes). If the situation is impossible, print "Impossible" (without quotes). Otherwise print one integer, denoting the maximum possible value of Limak's current rating, i.e. rating after the n contests.

NoteIn the first sample, the following scenario matches all information Limak remembers and has maximum possible final rating:  Limak has rating 1901 and belongs to the division 1 in the first contest. His rating decreases by 7.  With rating 1894 Limak is in the division 2. His rating increases by 5.  Limak has rating 1899 and is still in the division 2. In the last contest of the year he gets  + 8 and ends the year with rating 1907. In the second sample, it's impossible that Limak is in the division 1, his rating increases by 57 and after that Limak is in the division 2 in the second contest.

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

}


A competitive eater, Alice is scheduling some practices for an eating contest on a magical calendar. The calendar is unusual because a week contains not necessarily $$$7$$$ days!In detail, she can choose any integer $$$k$$$ which satisfies $$$1 \leq k \leq r$$$, and set $$$k$$$ days as the number of days in a week.Alice is going to paint some $$$n$$$ consecutive days on this calendar. On this calendar, dates are written from the left cell to the right cell in a week. If a date reaches the last day of a week, the next day's cell is the leftmost cell in the next (under) row.She wants to make all of the painted cells to be connected by side. It means, that for any two painted cells there should exist at least one sequence of painted cells, started in one of these cells, and ended in another, such that any two consecutive cells in this sequence are connected by side.Alice is considering the shape of the painted cells. Two shapes are the same if there exists a way to make them exactly overlapped using only parallel moves, parallel to the calendar's sides.For example, in the picture, a week has $$$4$$$ days and Alice paints $$$5$$$ consecutive days. [1] and [2] are different shapes, but [1] and [3] are equal shapes.    Alice wants to know how many possible shapes exists if she set how many days a week has and choose consecutive $$$n$$$ days and paints them in calendar started in one of the days of the week. As was said before, she considers only shapes, there all cells are connected by side.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case, the only line contains two integers $$$n$$$, $$$r$$$ ($$$1 \le n \le 10^9, 1 \le r \le 10^9$$$).

For each test case, print a single integer  — the answer to the problem. Please note, that the answer for some test cases won't fit into $$$32$$$-bit integer type, so you should use at least $$$64$$$-bit integer type in your programming language.

NoteIn the first test case, Alice can set $$$1,2,3$$$ or $$$4$$$ days as the number of days in a week.There are $$$6$$$ possible paintings shown in the picture, but there are only $$$4$$$ different shapes. So, the answer is $$$4$$$. Notice that the last example in the picture is an invalid painting because all cells are not connected by sides.    In the last test case, be careful with the overflow issue, described in the output format.

package main

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

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

func main() {
	out = bufio.NewWriterSize(os.Stdout, 8192)
	defer out.Flush()
	if false {
		file, err := os.Open("in.txt")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file, 8192)
	} else {
		in = bufio.NewReaderSize(os.Stdin, 8192)
	}

	var t, n, r int64
	scanf("%d", &t)

	for ; t > 0; t-- {
		scanf("\n%d %d", &n, &r)
		if n <= r {
			printf("%d\n", n*(n-1)/2+1)
		} else {
			printf("%d\n", r*(r+1)/2)
		}
	}
}

package main

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

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

	for tc > 0 {
		tc--
		n, r := readTwoNums(reader)
		fmt.Println(solve(n, r))
	}
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n, r int) int64 {
	if n <= 1 {
		return 1
	}
	var res int64
	if n <= r {
		r = n - 1
		res++
	}
	res += (1 + int64(r)) * int64(r) / 2
	return res
}

/*
URL:
https://codeforces.com/contest/1371/problem/B
*/

package main

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

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const (
	// General purpose
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	t int

	n, r int64
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		n, r = ReadInt64_2()

		solve()
	}
}

func solve() {
	var ans int64

	if r < n {
		ans = r * (r + 1) / 2
	} else {
		ans = n*(n-1)/2 + 1
	}

	fmt.Println(ans)
}

/*******************************************************************/

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

package main

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

var DEBUG = true

func main() {
	defer Flush()
	T := readi()
	for t := 0; t < T; t++ {
		n := readll()
		r := readll()
		println(solve(n, r))
	}
}

func height(n, w int64) int64 {
	return (n + w - 1) / w
}

func solve(n, r int64) int64 {
	//var ans int64

	if r < n {
		return (1 + r) * r / 2
	}
	return (1+(n-1))*(n-1)/2 + 1

}

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

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

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte.  Trailing '\n' will not be included.
// See also comments on readb()
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func dbgf(f string, args ...interface{}) {
	if !DEBUG {
		return
	}
	fmt.Fprintf(os.Stderr, f, args...)
}

func dbg(args ...interface{}) {
	if !DEBUG {
		return
	}
	fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Utilities

func sumSlice(a []int) int {
	var res int
	for _, v := range a {
		res += v
	}
	return res
}

func sumSlicell(a []int64) int64 {
	var res int64
	for _, v := range a {
		res += v
	}
	return res
}

func readInts(N int) (int, []int) {
	if N == 0 {
		N = readi()
	}
	a := make([]int, N)
	for i := range a {
		a[i] = readi()
	}
	return N, a
}

func readIntsll(N int) (int, []int64) {
	if N == 0 {
		N = readi()
	}
	a := make([]int64, N)
	for i := range a {
		a[i] = readll()
	}
	return N, a
}

// -----------------------------------------------------------------------------

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

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

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

func minf(a, b float64) float64 {
	if a < b {
		return a
	}
	return b
}

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

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

func maxf(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

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

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

func absf(a float64) float64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}

package main

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

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

	var t int
	var n, r, res int64

	fmt.Fscan(rdr, &t)

	for i := 0; i < t; i++ {

		fmt.Fscan(rdr, &n, &r)

		if r < n {
			res = r * (r + 1) / 2
		} else {
			res = n*(n-1)/2 + 1
		}
		fmt.Fprintln(w, res)

		res = 0
	}
}

package main

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

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

	var t int
	var n, r, res int64

	fmt.Fscan(rdr, &t)

	for i := 0; i < t; i++ {

		fmt.Fscan(rdr, &n, &r)
		bound := n - 1
		if r < n {
			bound = r
		}

		res = bound * (bound + 1) / 2

		if r >= n {
			res++
		}

		fmt.Fprintln(w, res)

		res = 0
	}
}

package main

import "fmt"

func main() {
	var tc int
	fmt.Scanf("%d\n", &tc)
	for ; tc > 0; tc-- {
		var x, y, z int64
		fmt.Scanf("%d%d\n", &x, &y)
		if x <= y {
			z++
		}
		if x-1 < y {
			y = x - 1
		}
		z += y * (y + 1) / 2
		fmt.Println(z)
	}
}

package main

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

func main() {
	sc := NewScanner()
	N := sc.NextInt()
	for i := 0; i < N; i++ {
		n, r := sc.NextInt64(), sc.NextInt64()
		ans := int64(0)
		y := min(n-1, r)
		ans += y * (y + 1) / 2
		if n <= r {
			ans++
		}
		fmt.Println(ans)
	}

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

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextFloat() float64 {
	v, _ := strconv.ParseFloat(s.Next(), 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

func main() {
	defer writer.Flush()
	t := NextInt()
	for t > 0 {
		t--
		n, r := NextInt(), NextInt()
		if n > r {
			Println(int64(r)*int64(r+1)/2)
		} else {
			Println(int64(n)*int64(n-1)/2+1)
		}
	}
}

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

func init() {
	sc.Buffer(make([]byte, 256), 1e9)
	sc.Split(bufio.ScanWords)
}

func Println(data interface{}) {
	writer.WriteString(fmt.Sprintf("%v\n", data))
}

func Print(data interface{}) {
	writer.WriteString(fmt.Sprintf("%v", data))
}

func Next() string {
	sc.Scan()
	return sc.Text()
}

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

func NextInt64() int64 {
	i64, err := strconv.ParseInt(Next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i64
}

func NextFloat64() float64 {
	f, err := strconv.ParseFloat(Next(), 64)
	if err != nil {
		panic(err)
	}
	return f
}

package main

import "fmt"

func main() {
	var tc int
	fmt.Scanf("%d\n", &tc)
	for ; tc > 0; tc-- {
		var x, y, z int64
		fmt.Scanf("%d%d\n", &x, &y)
		if x < y {
			z++
		}
		if x-1 < y {
			y = x - 1
		}
		z += y * (y + 1) / 2
		fmt.Println(z)
	}
}

package main

import "fmt"

func main() {
	var tc int
	fmt.Scanf("%d\n", &tc)
	for ; tc > 0; tc-- {
		var x, y, z int64
		fmt.Scanf("%d%d\n", &x, &y)
		fmt.Println(x, y)
		if x < y {
			z++
		}
		if x-1 < y {
			y = x - 1
		}
		z += y * (y + 1) / 2
		fmt.Println(z)
	}
}

package main

import "fmt"

func main() {
	var tc int
	fmt.Scanf("%d\n", &tc)
	for ; tc > 0; tc-- {
		var x, y, z int64
		fmt.Scanf("%d%d\n", &x, &y)
		if x < y {
			z++
		}
		if x-1 <= y {
			y = x - 1
		}
		z += y * (y + 1) / 2
		fmt.Println(z)
	}
}