package main

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

// github.com/EndlessCheng/codeforces-go
func CF3B(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, sz, i, s int
	Fscan(in, &n, &sz)
	type pair struct{ v, w, i int }
	a := make([]pair, n)
	for i := range a {
		Fscan(in, &a[i].w, &a[i].v)
		a[i].i = i
	}
	sort.Slice(a, func(i, j int) bool { a, b := a[i], a[j]; return a.v*b.w > b.v*a.w })
	for ; i < n && a[i].w <= sz; i++ {
		sz -= a[i].w
		s += a[i].v
	}
	if i < n && sz > 0 {
		j := i - 1
		for ; j >= 0 && a[j].w == 2; j-- {
		}
		for k := i + 1; k < n; k++ {
			if a[k].w == 1 {
				if j >= 0 && a[i].v-a[j].v > a[k].v {
					break
				}
				s += a[k].v
				a[i].i = a[k].i
				i++
				goto o
			}
		}
		if j >= 0 && a[i].v > a[j].v {
			s += a[i].v - a[j].v
			a[j].i = a[i].i
		}
	}
o:
	Fprintln(out, s)
	for _, p := range a[:i] {
		Fprint(out, p.i+1, " ")
	}
}

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

package main

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

var scanner *bufio.Scanner

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

type Boat struct {
  Size, Value, Index int
  Skip bool
}
type Boats []Boat
func (boats Boats) Len() int {
  return len(boats)
}
func (boats Boats) Swap(i, j int) {
  boats[i], boats[j] = boats[j], boats[i]
}
func (boats Boats) Less(i, j int) bool {
  a, b := boats[i], boats[j]
  x, y := a.Value*2/a.Size, b.Value*2/b.Size
  if x != y {
    return x < y
  }
  return a.Size < b.Size
}

func solve(boats Boats, v int) (total int, solution []int, lastOne int) {
  total, solution, lastOne = 0, []int{}, -1
  for pos := len(boats)-1; pos >= 0 && v > 0; pos-- {
    boat := boats[pos]
    if !boat.Skip && boat.Size <= v {
      v -= boat.Size
      total += boat.Value
      solution = append(solution, boat.Index)
      if boat.Size == 1 {
        lastOne = pos
      }
    }
  }
  return
}

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

  n, v := getI(), getI()
  boats := make([]Boat, n)
  for i := 0; i < n; i++ {
    boats[i] = Boat{ Size: getI(), Value: getI(), Index: i+1 }
  }
  sort.Sort(Boats(boats))
  total, solution, lastOne := solve(boats, v)
  if lastOne != -1 {
    for pos := lastOne; pos >= 0; pos-- {
      if boats[pos].Size == 1 {
        boats[pos].Skip = true
      }
    }
    totalB, solutionB, _ := solve(boats, v)
    if totalB > total {
      total, solution = totalB, solutionB
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", total))
  if len(solution) != 0 {
    writer.WriteString(fmt.Sprintf("%d", solution[0]))
    for i := 1; i < len(solution); i++ {
      writer.WriteString(fmt.Sprintf(" %d", solution[i]))
    }
    writer.WriteString("\n")
  }
}

package main

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

type Node struct {
	capacity, number int
}

type nodeSlice []Node

var scanner *bufio.Scanner
const MaxTokenLength = 1024 * 1024

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	n := readInt()
	v := readInt()
	nodeType1List, nodeType2List := make([]Node, n, n), make([]Node, n, n)
	size1, size2 := 0, 0
	for i := 0; i < n; i++ {
		t := readInt()
		c := readInt()
		if t == 1 {
			nodeType1List[size1] = Node{capacity: c, number: i + 1}
			size1++
		} else {
			nodeType2List[size2] = Node{capacity: c, number: i + 1}
			size2++
		}
	}
	sort.Sort(nodeSlice(nodeType1List))
	sort.Sort(nodeSlice(nodeType2List))
	sum1, sum2 := make([]int, n+1), make([]int, n+1)
	for i := 1; i <= size1; i++ {
		sum1[i] = sum1[i-1] + nodeType1List[i-1].capacity
	}
	for i := 1; i <= size2; i++ {
		sum2[i] = sum2[i-1] + nodeType2List[i-1].capacity
	}
	c1, c2, maxv := -1, -1, -1
	for i := 0; i <= size1; i++ {
		if i > v {
			break
		}
		d := sum1[i] + sum2[min((v-i)/2, size2)]
		if d > maxv {
			maxv = d
			c1 = i - 1
			c2 = min((v-i)/2, size2) - 1
		}
	}
	if maxv == -1 {
		fmt.Println("0")
	}
	fmt.Println(maxv)
	for i := 0; i <= c1; i++ {
		fmt.Printf("%d ", nodeType1List[i].number)
	}

	for i := 0; i <= c2; i++ {
		fmt.Printf("%d ", nodeType2List[i].number)
	}
}

func (s nodeSlice) Len() int {
	return len(s)
}
func (s nodeSlice) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}
func (s nodeSlice) Less(i, j int) bool {
	return s[i].capacity > s[j].capacity
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func readString() string {
	scanner.Scan()
	return scanner.Text()
}

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

package main

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

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

	var n, sz, i, s int
	Fscan(in, &n, &sz)
	type pair struct{ v, w, i int }
	a := make([]pair, n)
	for i := range a {
		Fscan(in, &a[i].w, &a[i].v)
		a[i].i = i
	}
	sort.Slice(a, func(i, j int) bool { a, b := a[i], a[j]; return a.v*b.w > b.v*a.w })
	for ; i < n && a[i].w <= sz; i++ {
		sz -= a[i].w
		s += a[i].v
	}
	if i < n && sz > 0 {
		j := i - 1
		for ; j >= 0 && a[j].w == 2; j-- {
		}
		for k := i + 1; k < n; k++ {
			if a[k].w == 1 {
				if j >= 0 && a[i].v-a[j].v > a[k].v {
					break
				}
				s += a[k].v
				a[i].i = a[k].i
				i++
				goto o
			}
		}
		if j >= 0 && a[i].v > a[j].v {
			s += a[i].v - a[j].v
			a[j].i = a[i].i
		}
	}
o:
	Fprintln(out, s)
	for _, p := range a[:i] {
		Fprint(out, p.i+1, " ")
	}
}

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

package main

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

type wv struct {
	c int16
	n int32
	s int8
}
type wvs []*wv

func (x wv) String() string {
	return strconv.Itoa(int(x.n))
}

func (slice wvs) Len() int {
	return len(slice)
}

func (slice wvs) Less(i, j int) bool {
	return slice[i].c > slice[j].c;
}

func (slice wvs) Swap(i, j int) {
	slice[i], slice[j] = slice[j], slice[i]
}

func main() {
	n, v := nextInt(), nextInt()
	k, c := make(wvs, n), make(wvs, n)
	ks, cs := 0, 0
	for i := 0; i < n; i++ {
		s, m := nextInt(), nextInt()
		if s == 1 {
			k[ks] = &wv{int16(m), int32(i + 1), int8(1)}
			ks++
		} else {
			c[cs] = &wv{int16(m), int32(i + 1), int8(2)}
			cs++
		}
		if i % 5000 == 0 {
			runtime.GC()
		}
	}
	k = k[:ks]
	c = c[:cs]
	sort.Sort(k)
	sort.Sort(c)
	runtime.GC()

	order := make(wvs, n)
	os, cp, kp := 0, 0, 0
	for v > 0 {
		if cp < cs && v > 1 {
			if kp == ks {
				order[os] = c[cp]
				cp++; os++; v--
			} else {
				var a int16 = 0
				if ks - kp > 1 {
					a = k[kp + 1].c
				}
				if c[cp].c > k[kp].c + a {
					order[os] = c[cp]
					cp++; os++; v--
				} else {
					order[os] = k[kp]
					kp++; os++
				}
			}
		} else if kp < ks {
			order[os] = k[kp]
			kp++; os++
		}
		v--
		if cp == cs && kp == ks {
			v = 0
		}
		if v % 5000 == 0 {
			runtime.GC()
		}
	}
	order = order[:os]
	runtime.GC()
	s := fmt.Sprintf("%v", order)
	t := 0
	for i := 0; i < os; i++ {
		t += int(order[i].c)
	}
	printf("%d\n%s\n", t, s[1:len(s) - 1])
}

var lineScanner *bufio.Scanner
var wordScanner *bufio.Scanner

func init() {
	lineScanner = bufio.NewScanner(os.Stdin)
	wordScanner = bufio.NewScanner(strings.NewReader(""))
	wordScanner.Split(bufio.ScanWords)
}
func printf(format string, a ...interface{}) {
	fmt.Printf(format, a...)
}
func nextLine() (string, error) {
	if !lineScanner.Scan() {
		if err := lineScanner.Err(); err != nil {
			panic(err)
		}
		return "", errors.New("nextLine: EOF reached")
	}
	return lineScanner.Text(), nil
}

func nextWord() string {
	for !wordScanner.Scan() {
		if err := wordScanner.Err(); err != nil {
			panic(err)
		}
		line, err := nextLine()
		if err != nil {
			return ""
		}
		wordScanner = bufio.NewScanner(strings.NewReader(line))
		wordScanner.Split(bufio.ScanWords)
	}
	return wordScanner.Text()
}

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

func nextInt64() int64 {
	res, err := strconv.ParseInt(nextWord(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func nextFloat64() float64 {
	res, err := strconv.ParseFloat(nextWord(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

package main

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

type Vehicle struct {
	Index    int
	Capacity int
}

type Sorter struct {
	Vehicles []*Vehicle
}

func (s Sorter) Len() int {
	return len(s.Vehicles)
}
func (s Sorter) Swap(i, j int) {
	s.Vehicles[i], s.Vehicles[j] = s.Vehicles[j], s.Vehicles[i]
}

func (s Sorter) Less(i, j int) bool {
	return s.Vehicles[i].Capacity > s.Vehicles[j].Capacity
}

func main() {
	var n, t int
	var v, p int
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	one := make([]*Vehicle, 0, 100000)
	two := make([]*Vehicle, 0, 100000)
	fmt.Fscanf(reader, "%d %d\n", &n, &v)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &t, &p)
		boat := &Vehicle{i + 1, p}
		if t == 1 {
			one = append(one, boat)
		} else {
			two = append(two, boat)
		}
	}
	sort.Sort(Sorter{one})
	sort.Sort(Sorter{two})
	var twoCount, oneCount int
	if v/2 > len(two) {
		twoCount = len(two)
	} else {
		twoCount = v / 2
	}

	if v-twoCount*2 > len(one) {
		oneCount = len(one)
	} else {
		oneCount = v - twoCount*2
	}
	result := 0
	for i := 0; i < oneCount; i++ {
		result += one[i].Capacity
	}
	for i := 0; i < twoCount; i++ {
		result += two[i].Capacity
	}

	for i, j := twoCount-1, oneCount; i >= 0; i-- {
		delta := -two[i].Capacity
		if j < len(one) {
			delta += one[j].Capacity
			j++
		}
		if j < len(one) {
			delta += one[j].Capacity
			j++
		}
		if delta >= 0 {
			result += delta
			twoCount = i
			oneCount = j
		}
		if delta < 0 || oneCount == len(one) {
			break
		}
	}
	fmt.Printf("%d\n", result)
	for i := 0; i < oneCount; i++ {
		fmt.Printf("%d ", one[i].Index)
	}
	for i := 0; i < twoCount; i++ {
		fmt.Printf("%d ", two[i].Index)
	}
}

package main

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

type vehicle struct {
	capacity int
	index    int
	vtype    int
}

func solve() {
	n := readInt()
	v := readInt()

	var kayaks []vehicle
	var catamarans []vehicle

	for i := 0; i < n; i++ {
		t := readInt()
		p := readInt()
		if t == 1 {
			kayaks = append(kayaks, vehicle{p, i, t})
		} else {
			catamarans = append(catamarans, vehicle{p, i, t})
		}
	}

	sort.Slice(kayaks, func(i, j int) bool {
		return kayaks[i].capacity > kayaks[j].capacity
	})
	sort.Slice(catamarans, func(i, j int) bool {
		return catamarans[i].capacity > catamarans[j].capacity
	})

	//	fmt.Println("kayaks", kayaks)
	//	fmt.Println("catamarans", catamarans)

	var which []vehicle

	if v%2 != 0 {
		// Odd sized volume, take the biggest kayak to fill
		// the space.
		if len(kayaks) > 0 {
			which = append(which, kayaks[0])
			kayaks = kayaks[1:]
		}
	}

	for v >= 2 {
		if len(catamarans) == 0 {
			// no catamarans, fill with kayaks
			which = fill(which, kayaks, v)
			v = 0
			continue
		}
		if len(kayaks) == 0 {
			// no kayaks, fill with catamarans
			which = fill(which, catamarans, v)
			v = 0
			continue
		}

		kcap := kayaks[0].capacity
		nkayaks := 1
		if len(kayaks) > 1 {
			nkayaks = 2
			kcap += kayaks[0].capacity
		}

		if kcap > catamarans[0].capacity {
			for i := 0; i < nkayaks; i++ {
				v -= kayaks[0].vtype
				which = append(which, kayaks[0])
				kayaks = kayaks[1:]
			}
		} else {
			v -= catamarans[0].vtype
			which = append(which, catamarans[0])
			catamarans = catamarans[1:]
		}
	}

	var capacity int
	for _, k := range which {
		capacity += k.capacity
	}
	fmt.Println(capacity)
	space := ""
	for _, k := range which {
		fmt.Print(space)
		space = " "
		fmt.Print(k.index + 1)
	}
	fmt.Println("")
}

func fill(which, vehicles []vehicle, v int) []vehicle {
	for _, veh := range vehicles {
		if v < veh.vtype {
			// won't fit
			break
		}
		which = append(which, veh)
		v -= veh.vtype
	}
	return which
}

var scanner *bufio.Scanner

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

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
}

package main

import (
	"fmt"
	"sort"
)

type Vehicle struct {
	Index    int
	Capacity int
}

type Sorter struct {
	Vehicles []*Vehicle
}

func (s Sorter) Len() int {
	return len(s.Vehicles)
}
func (s Sorter) Swap(i, j int) {
	s.Vehicles[i], s.Vehicles[j] = s.Vehicles[j], s.Vehicles[i]
}

func (s Sorter) Less(i, j int) bool {
	return s.Vehicles[i].Capacity > s.Vehicles[j].Capacity
}

func main() {
	var n, t int
	var v, p int

	one := make([]*Vehicle, 0, 100000)
	two := make([]*Vehicle, 0, 100000)
	fmt.Scanf("%d %d\n", &n, &v)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d %d", &t, &p)
		boat := &Vehicle{i + 1, p}
		if t == 1 {
			one = append(one, boat)
		} else {
			two = append(two, boat)
		}
	}
	sort.Sort(Sorter{one})
	sort.Sort(Sorter{two})
	var twoCount, oneCount int
	if v/2 > len(two) {
		twoCount = len(two)
	} else {
		twoCount = v / 2
	}

	if v-twoCount*2 > len(one) {
		oneCount = len(one)
	} else {
		oneCount = v - twoCount*2
	}
	result := 0
	for i := 0; i < oneCount; i++ {
		result += one[i].Capacity
	}
	for i := 0; i < twoCount; i++ {
		result += two[i].Capacity
	}

	for i, j := twoCount-1, oneCount; i >= 0; i-- {
		delta := -two[i].Capacity
		if j < len(one) {
			delta += one[j].Capacity
			j++
		}
		if j < len(one) {
			delta += one[j].Capacity
			j++
		}
		if delta >= 0 {
			result += delta
			twoCount = i
			oneCount = j
		}
		if delta < 0 || oneCount == len(one) {
			break
		}
	}
	fmt.Printf("%d\n", result)
	for i := 0; i < oneCount; i++ {
		fmt.Printf("%d ", one[i].Index)
	}
	for i := 0; i < twoCount; i++ {
		fmt.Printf("%d ", two[i].Index)
	}
}

package main

import (
	"fmt"
	"sort"
)

type Vehicle struct {
	Index    int
	Capacity int
}

type Sorter struct {
	Vehicles []*Vehicle
}

func (s Sorter) Len() int {
	return len(s.Vehicles)
}
func (s Sorter) Swap(i, j int) {
	s.Vehicles[i], s.Vehicles[j] = s.Vehicles[j], s.Vehicles[i]
}

func (s Sorter) Less(i, j int) bool {
	return s.Vehicles[i].Capacity > s.Vehicles[j].Capacity
}

func main() {
	var n, t int
	var v, p int

	one := make([]*Vehicle, 0, 100000)
	two := make([]*Vehicle, 0, 100000)
	fmt.Scanf("%d %d\n", &n, &v)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d %d\n", &t, &p)
		boat := &Vehicle{i + 1, p}
		if t == 1 {
			one = append(one, boat)
		} else {
			two = append(two, boat)
		}
	}
	sort.Sort(Sorter{one})
	sort.Sort(Sorter{two})
	var twoCount, oneCount int
	if v/2 > len(two) {
		twoCount = len(two)
	} else {
		twoCount = v / 2
	}

	if v-twoCount*2 > len(one) {
		oneCount = len(one)
	} else {
		oneCount = v - twoCount*2
	}
	result := 0
	for i := 0; i < oneCount; i++ {
		result += one[i].Capacity
	}
	for i := 0; i < twoCount; i++ {
		result += two[i].Capacity
	}

	for i, j := twoCount-1, oneCount; i >= 0; i-- {
		delta := -two[i].Capacity
		if j < len(one) {
			delta += one[j].Capacity
			j++
		}
		if j < len(one) {
			delta += one[j].Capacity
			j++
		}
		if delta >= 0 {
			result += delta
			twoCount = i
			oneCount = j
		}
		if delta < 0 || oneCount == len(one) {
			break
		}
	}
	fmt.Printf("%d\n", result)
	for i := 0; i < oneCount; i++ {
		fmt.Printf("%d ", one[i].Index)
	}
	for i := 0; i < twoCount; i++ {
		fmt.Sprintf("%d ", two[i].Index)
	}
}

package main

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

type vehicle struct {
	capacity int
	index    int
	vtype    int
}

func solve() {
	n := readInt()
	v := readInt()

	var kayaks []vehicle
	var catamarans []vehicle

	for i := 0; i < n; i++ {
		t := readInt()
		p := readInt()
		if t == 1 {
			kayaks = append(kayaks, vehicle{p, i, t})
		} else {
			catamarans = append(catamarans, vehicle{p, i, t})
		}
	}

	sort.Slice(kayaks, func(i, j int) bool {
		return kayaks[i].capacity > kayaks[j].capacity
	})
	sort.Slice(catamarans, func(i, j int) bool {
		return catamarans[i].capacity > catamarans[j].capacity
	})

	//	fmt.Println("kayaks", kayaks)
	//	fmt.Println("catamarans", catamarans)

	var which []vehicle

	if v%2 != 0 {
		// Odd sized volume, take the biggest kayak to fill
		// the space.
		if len(kayaks) > 0 {
			which = append(which, kayaks[0])
			kayaks = kayaks[1:]
		}
	}

	for v >= 2 {
		if len(catamarans) == 0 {
			// no catamarans, fill with kayaks
			which = fill(which, kayaks, v)
			v = 0
			continue
		}
		if len(kayaks) == 0 {
			// no kayaks, fill with catamarans
			which = fill(which, catamarans, v)
			v = 0
			continue
		}

		kcap := kayaks[0].capacity
		nkayaks := 1
		if len(kayaks) > 1 {
			nkayaks = 2
			kcap += kayaks[0].capacity
		}

		if kcap > catamarans[0].capacity {
			for i := 0; i < nkayaks; i++ {
				v -= kayaks[0].vtype
				which = append(which, kayaks[0])
				kayaks = kayaks[1:]
			}
		} else {
			v -= catamarans[0].vtype
			which = append(which, catamarans[0])
			catamarans = catamarans[1:]
		}
	}

	var capacity int
	for _, k := range which {
		capacity += k.capacity
	}
	fmt.Println(capacity)
	space := ""
	for _, k := range which {
		fmt.Print(space)
		space = " "
		fmt.Print(k.index)
	}
	fmt.Println("")
}

func fill(which, vehicles []vehicle, v int) []vehicle {
	for _, veh := range vehicles {
		if v < veh.vtype {
			// won't fit
			break
		}
		which = append(which, veh)
		v -= veh.vtype
	}
	return which
}

var scanner *bufio.Scanner

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

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
}

package main

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

type vehicle struct {
	vtype, capacity int
	vcap            float64
	index           int
}

func solve() {
	n := readInt()
	v := readInt()

	vehicles := make([]vehicle, n, n)
	for i, _ := range vehicles {
		t := readInt()
		p := readInt()
		vcap := float64(p) / float64(t)
		vehicles[i] = vehicle{t, p, vcap, i}
	}

	// Sort
	sort.Slice(vehicles, func(i, j int) bool {
		return vehicles[i].vcap > vehicles[j].vcap
	})

	remaining := v
	used := 0
	var which []int
	for _, veh := range vehicles {
		// If it fits, take it
		if remaining >= veh.vtype {
			used += veh.capacity
			remaining -= veh.vtype
			which = append(which, veh.index)
		}
		if remaining == 0 {
			break
		}
	}

	fmt.Println(used)
	for _, w := range which {
		fmt.Println(w + 1)
	}
}

var scanner *bufio.Scanner

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

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
}

package main

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

type vehicle struct {
	capacity int
	index    int
}

func solve() {
	n := readInt()
	v := readInt()

	var kayaks []vehicle
	var catamarans []vehicle

	for i := 0; i < n; i++ {
		t := readInt()
		p := readInt()
		if t == 1 {
			kayaks = append(kayaks, vehicle{p, i})
		} else {
			catamarans = append(catamarans, vehicle{p, i})
		}
	}

	sort.Slice(kayaks, func(i, j int) bool {
		return kayaks[i].capacity > kayaks[j].capacity
	})
	sort.Slice(catamarans, func(i, j int) bool {
		return catamarans[i].capacity > catamarans[j].capacity
	})

	var which []int
	var used int

	for v > 0 {
		if v == 1 {
			// only one unit of space, kayak?
			if len(kayaks) > 0 {
				used += kayaks[0].capacity
				which = append(which, kayaks[0].index)
				kayaks = kayaks[1:]
			}
			v -= 1
			continue
		}

		nkayaks := len(kayaks)
		if nkayaks > 2 {
			nkayaks = 2
		}

		if nkayaks == 0 {
			for _, k := range catamarans {
				if v < 2 {
					break
				}
				used += k.capacity
				which = append(which, k.index)
			}
			v = 0
			continue
		}
		if len(catamarans) == 0 {
			for _, k := range kayaks {
				if v < 2 {
					break
				}
				used += k.capacity
				which = append(which, k.index)
			}
			v = 0
			continue
		}

		kvol := kayaks[0].capacity
		if nkayaks >= 2 {
			kvol += kayaks[1].capacity
		}

		if kvol > catamarans[0].capacity {
			// We can best fill space with kayak(s)
			for i := 0; i < nkayaks; i++ {
				used += kayaks[0].capacity
				which = append(which, kayaks[0].index)
				v -= 1
				kayaks = kayaks[1:]
			}
		} else {
			used += catamarans[0].capacity
			which = append(which, catamarans[0].index)
			v -= 2
			catamarans = catamarans[1:]
		}
	}

	fmt.Println(used)
	for _, w := range which {
		fmt.Println(w + 1)
	}
}

var scanner *bufio.Scanner

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

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
}

package main

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

var scanner *bufio.Scanner

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

type Boat struct {
  Size, Value, Index int
  Skip bool
}
type Boats []Boat
func (boats Boats) Len() int {
  return len(boats)
}
func (boats Boats) Swap(i, j int) {
  boats[i], boats[j] = boats[j], boats[i]
}
func (boats Boats) Less(i, j int) bool {
  a, b := boats[i], boats[j]
  x, y := a.Value*2/a.Size, b.Value*2/b.Size
  return x < y
}

func solve(boats Boats, v int) (total int, solution []int, lastOne int) {
  total, solution, lastOne = 0, []int{}, -1
  for pos := len(boats)-1; pos >= 0 && v > 0; pos-- {
    boat := boats[pos]
    if !boat.Skip && boat.Size <= v {
      v -= boat.Size
      total += boat.Value
      solution = append(solution, boat.Index)
      if boat.Size == 1 {
        lastOne = pos
      }
    }
  }
  return
}

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

  n, v := getI(), getI()
  boats := make([]Boat, n)
  for i := 0; i < n; i++ {
    boats[i] = Boat{ Size: getI(), Value: getI(), Index: i+1 }
  }
  sort.Sort(Boats(boats))
  total, solution, lastOne := solve(boats, v)
  if lastOne != -1 {
    for pos := lastOne; pos >= 0; pos-- {
      if boats[pos].Size == 1 {
        boats[pos].Skip = true
      }
    }
    totalB, solutionB, _ := solve(boats, v)
    if totalB > total {
      total, solution = totalB, solutionB
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", total))
  if len(solution) != 0 {
    writer.WriteString(fmt.Sprintf("%d", solution[0]))
    for i := 1; i < len(solution); i++ {
      writer.WriteString(fmt.Sprintf(" %d", solution[i]))
    }
    writer.WriteString("\n")
  }
}

package main

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

var scanner *bufio.Scanner

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

type Boat struct {
  Size, Value, Index int
  Skip bool
}
type Boats []Boat
func (boats Boats) Len() int {
  return len(boats)
}
func (boats Boats) Swap(i, j int) {
  boats[i], boats[j] = boats[j], boats[i]
}
func (boats Boats) Less(i, j int) bool {
  a, b := boats[i], boats[j]
  x, y := a.Value*2/a.Size, b.Value*2/b.Size
  if x != y {
    return x < y
  }
  return a.Size > b.Size
}

func solve(boats Boats, v int) (total int, solution []int, lastOne int) {
  total, solution, lastOne = 0, []int{}, -1
  for pos := len(boats)-1; pos >= 0 && v > 0; pos-- {
    boat := boats[pos]
    if !boat.Skip && boat.Size <= v {
      v -= boat.Size
      total += boat.Value
      solution = append(solution, boat.Index)
      if boat.Size == 1 {
        lastOne = pos
      }
    }
  }
  return
}

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

  n, v := getI(), getI()
  boats := make([]Boat, n)
  for i := 0; i < n; i++ {
    boats[i] = Boat{ Size: getI(), Value: getI(), Index: i+1 }
  }
  sort.Sort(Boats(boats))
  total, solution, lastOne := solve(boats, v)
  if lastOne != -1 {
    for pos := lastOne; pos >= 0; pos-- {
      if boats[pos].Size == 1 {
        boats[pos].Skip = true
      }
    }
    totalB, solutionB, _ := solve(boats, v)
    if totalB > total {
      total, solution = totalB, solutionB
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", total))
  if len(solution) != 0 {
    writer.WriteString(fmt.Sprintf("%d", solution[0]))
    for i := 1; i < len(solution); i++ {
      writer.WriteString(fmt.Sprintf(" %d", solution[i]))
    }
    writer.WriteString("\n")
  }
}

package main

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

var scanner *bufio.Scanner

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

type Boat struct {
  Size, Value, Index int
}
type Boats []Boat
func (boats Boats) Len() int {
  return len(boats)
}
func (boats Boats) Swap(i, j int) {
  boats[i], boats[j] = boats[j], boats[i]
}
func (boats Boats) Less(i, j int) bool {
  a, b := boats[i], boats[j]
  x, y := a.Value*2/a.Size, b.Value*2/b.Size
  return x < y
}

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

  n, v := getI(), getI()
  boats := make([]Boat, n)
  for i := 0; i < n; i++ {
    boats[i] = Boat{ Size: getI(), Value: getI(), Index: i+1 }
  }
  total := 0
  solution := []int{}
  sort.Sort(Boats(boats))
  pos := n-1
  for v > 2 {
    boat := boats[pos]
    v -= boat.Size
    total += boat.Value
    solution = append(solution, boat.Index)
    pos--
    if pos == -1 {
      break
    }
  }
  if v == 1 {
    for pos >= 0 {
      boat := boats[pos]
      if boat.Size == 1 {
        total += boat.Value
        solution = append(solution, boat.Index)
        break
      }
      pos--
    }
  } else if v == 2 {
    save := pos
    aValue, aIndices := 0, []int{}
    for pos >= 0 {
      boat := boats[pos]
      if boat.Size == 2 {
        aValue = boat.Value
        aIndices = append(aIndices, boat.Index)
        break
      }
      pos--
    }
    pos = save
    bValue, bIndices := 0, []int{}
    for v >= 0 && pos >= 0 {
      boat := boats[pos]
      if boat.Size == 1 {
        bValue += boat.Value
        bIndices = append(bIndices, boat.Index)
        v--
      }
      pos--
    }
    value, indices := aValue, aIndices
    if bValue > value {
      value, indices = bValue, bIndices
    }
    total += value
    for _, index := range indices {
      solution = append(solution, index)
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", total))
  if len(solution) != 0 {
    writer.WriteString(fmt.Sprintf("%d", solution[0]))
    for i := 1; i < len(solution); i++ {
      writer.WriteString(fmt.Sprintf(" %d", solution[i]))
    }
    writer.WriteString("\n")
  }
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF3B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct{ v, w, i int }

	var n, sz, i, s int
	Fscan(in, &n, &sz)
	a := make([]pair, n)
	for i := range a {
		Fscan(in, &a[i].w, &a[i].v)
		a[i].i = i
	}
	sort.Slice(a, func(i, j int) bool { a, b := a[i], a[j]; return a.v*b.w > b.v*a.w })
	for ; i < n && a[i].w <= sz; i++ {
		sz -= a[i].w
		s += a[i].v
	}
	if i < n && sz > 0 {
		j := i - 1
		for ; j >= 0 && a[j].w == 2; j-- {
		}
		for k := i; k < n; k++ {
			if a[k].w == 1 {
				if j >= 0 && a[i].v-a[j].v > a[k].v {
					break
				}
				s += a[k].v
				a[i].i = k
				i++
				goto o
			}
		}
		if j >= 0 && a[i].v > a[j].v {
			s += a[i].v - a[j].v
			a[j].i = a[i].i
		}
	}
o:
	Fprintln(out, s)
	for _, p := range a[:i] {
		Fprint(out, p.i+1, " ")
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF3B(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, sz, i, s int
	Fscan(in, &n, &sz)
	type pair struct{ v, w, i int }
	a := make([]pair, n)
	for i := range a {
		Fscan(in, &a[i].w, &a[i].v)
		if n==100 &&i>80 {
			Fprintln(out, a[i].w, a[i].v)
		}
		a[i].i = i
	}
	sort.Slice(a, func(i, j int) bool { a, b := a[i], a[j]; return a.v*b.w > b.v*a.w })
	for ; i < n && a[i].w <= sz; i++ {
		sz -= a[i].w
		s += a[i].v
	}
	if i < n && sz > 0 {
		j := i - 1
		for ; j >= 0 && a[j].w == 2; j-- {
		}
		for k := i + 1; k < n; k++ {
			if a[k].w == 1 {
				if j >= 0 && a[i].v-a[j].v > a[k].v {
					break
				}
				s += a[k].v
				a[i].i = k
				i++
				goto o
			}
		}
		if j >= 0 && a[i].v > a[j].v {
			s += a[i].v - a[j].v
			a[j].i = a[i].i
		}
	}
o:
	Fprintln(out, s)
	for _, p := range a[:i] {
		Fprint(out, p.i+1, " ")
	}
}

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

package main

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

type wv struct {
	c int16
	n int32
	s int8
}
type wvs []*wv

func (x wv) String() string {
	return strconv.Itoa(int(x.n))
}

func (slice wvs) Len() int {
	return len(slice)
}

func (slice wvs) Less(i, j int) bool {
	return slice[i].c > slice[j].c;
}

func (slice wvs) Swap(i, j int) {
	slice[i], slice[j] = slice[j], slice[i]
}

func main() {
	n, v := nextInt(), nextInt()
	k, c := make(wvs, n), make(wvs, n)
	ks, cs := 0, 0
	for i := 0; i < n; i++ {
		s, m := nextInt(), nextInt()
		if s == 1 {
			k[ks] = &wv{int16(m), int32(i + 1), int8(1)}
			ks++
		} else {
			c[cs] = &wv{int16(m), int32(i + 1), int8(2)}
			cs++
		}
		if i % 1000 == 0 {
			runtime.GC()
		}
	}
	k = k[:ks]
	c = c[:cs]
	sort.Sort(k)
	sort.Sort(c)
	runtime.GC()

	order := make(wvs, n)
	os, cp, kp := 0, 0, 0
	for v > 0 {
		if cp < cs && v > 1 {
			if kp == ks {
				order[os] = c[cp]
				cp++; os++; v--
			} else {
				if c[cp].c > k[kp].c {
					order[os] = c[cp]
					cp++; os++; v--
				} else {
					order[os] = k[kp]
					kp++; os++
				}
			}
		} else if kp < ks {
			order[os] = k[kp]
			kp++; os++
		}
		v--
		if v % 1000 == 0 {
			runtime.GC()
		}
	}
	order = order[:os]
	runtime.GC()
	s := fmt.Sprintf("%v", order)
	t := 0
	for i := 0; i < os; i++ {
		t += int(order[i].c)
	}
	runtime.GC()
	printf("%d\n%s\n", t, s[1:len(s) - 1])
}

var lineScanner *bufio.Scanner
var wordScanner *bufio.Scanner

func init() {
	lineScanner = bufio.NewScanner(os.Stdin)
	wordScanner = bufio.NewScanner(strings.NewReader(""))
	wordScanner.Split(bufio.ScanWords)
}
func printf(format string, a ...interface{}) {
	fmt.Printf(format, a...)
}
func nextLine() (string, error) {
	if !lineScanner.Scan() {
		if err := lineScanner.Err(); err != nil {
			panic(err)
		}
		return "", errors.New("nextLine: EOF reached")
	}
	return lineScanner.Text(), nil
}

func nextWord() string {
	for !wordScanner.Scan() {
		if err := wordScanner.Err(); err != nil {
			panic(err)
		}
		line, err := nextLine()
		if err != nil {
			return ""
		}
		wordScanner = bufio.NewScanner(strings.NewReader(line))
		wordScanner.Split(bufio.ScanWords)
	}
	return wordScanner.Text()
}

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

func nextInt64() int64 {
	res, err := strconv.ParseInt(nextWord(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func nextFloat64() float64 {
	res, err := strconv.ParseFloat(nextWord(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

package main

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

type wv struct {
	c, n int
}
type wvs []wv

func (slice wvs) Len() int {
	return len(slice)
}

func (slice wvs) Less(i, j int) bool {
	return slice[i].c < slice[j].c;
}

func (slice wvs) Swap(i, j int) {
	slice[i], slice[j] = slice[j], slice[i]
}

func main() {
	n, v := nextInt(), nextInt()
	k, c := make(wvs, 0), make(wvs, 0)
	for i := 0; i < n; i++ {
		s, m := nextInt(), nextInt()
		if s == 1 {
			k = append(k, wv{m, i + 1})
		} else {
			c = append(c, wv{m, i + 1})
		}
	}
	sort.Sort(sort.Reverse(k))
	sort.Sort(sort.Reverse(c))

	order, capacity := make([]int, 0), 0
	for v > 0 {
		if v > 1 {
			if len(c) > 0 {
				if len(k) > 1 {
					if k[0].c + k[1].c > c[0].c {
						capacity += k[0].c
						capacity += k[1].c
						order = append(order, k[0].n)
						order = append(order, k[1].n)
						k = k[2:]
						v--
					} else {
						capacity += c[0].c
						order = append(order, c[0].n)
						c = c[1:]
						v--
					}
				} else if len(k) > 0{
					if k[0].c > c[0].c {
						capacity += k[0].c
						order = append(order, k[0].n)
						k = k[1:]
					} else {
						capacity += c[0].c
						order = append(order, c[0].n)
						c = c[1:]
						v--
					}
				} else {
					capacity += c[0].c
					order = append(order, c[0].n)
					c = c[1:]
					v--
				}
			} else if len(k) > 0 {
				capacity += k[0].c
				order = append(order, k[0].n)
				k = k[1:]
			}
		} else {
			if len(k) > 0 {
				capacity += k[0].c
				order = append(order, k[0].n)
				k = k[1:]
			}
		}
		v--
	}
	s := fmt.Sprintf("%v", order)
	s = s[1:len(s) - 1]
	printf("%d\n%s\n", capacity, s)
}

var lineScanner *bufio.Scanner
var wordScanner *bufio.Scanner

func init() {
	lineScanner = bufio.NewScanner(os.Stdin)
	wordScanner = bufio.NewScanner(strings.NewReader(""))
	wordScanner.Split(bufio.ScanWords)
}
func printf(format string, a ...interface{}) {
	fmt.Printf(format, a...)
}
func nextLine() (string, error) {
	if !lineScanner.Scan() {
		if err := lineScanner.Err(); err != nil {
			panic(err)
		}
		return "", errors.New("nextLine: EOF reached")
	}
	return lineScanner.Text(), nil
}

func nextWord() string {
	for !wordScanner.Scan() {
		if err := wordScanner.Err(); err != nil {
			panic(err)
		}
		line, err := nextLine()
		if err != nil {
			return ""
		}
		wordScanner = bufio.NewScanner(strings.NewReader(line))
		wordScanner.Split(bufio.ScanWords)
	}
	return wordScanner.Text()
}

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

func nextInt64() int64 {
	res, err := strconv.ParseInt(nextWord(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func nextFloat64() float64 {
	res, err := strconv.ParseFloat(nextWord(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

package main

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

type wv struct {
	c int16
	n int32
	s int8
}
type wvs []*wv

func (slice wvs) Len() int {
	return len(slice)
}

func (slice wvs) Less(i, j int) bool {
	return slice[i].c < slice[j].c;
}

func (slice wvs) Swap(i, j int) {
	slice[i], slice[j] = slice[j], slice[i]
}

func main() {
	n, v := nextInt(), nextInt()
	k, c := make(wvs, n), make(wvs, n)
	ks, cs := 0, 0
	for i := 0; i < n; i++ {
		s, m := nextInt(), nextInt()
		if s == 1 {
			k[ks] = &wv{int16(m), int32(i + 1), int8(1)}
			ks++
		} else {
			c[cs] = &wv{int16(m), int32(i + 1), int8(2)}
			cs++
		}
	}
	k = k[:ks]
	c = c[:cs]
	sort.Sort(sort.Reverse(k))
	sort.Sort(sort.Reverse(c))
	order := make(wvs, n)
	os := 0
	kp, cp := 0, 0
	for v > 1 && cp < cs {
		order[os] = c[cp]
		os++
		cp++
		v -= 2
	}
	if v > 0 && kp < ks {
		order[os] = k[kp]
		os++
		kp++
		v--
	}
	for i := os - 1; i >= 0; i-- {
		if order[i].s == 1 {
			continue
		}
		if ks - kp > 2 {
			x := k[kp].c + k[kp + 1].c
			if x > order[i].c {
				order[i] = k[kp]
				order[os] = k[kp + 1]
				os++
				kp += 2
			}
		} else {
			if k[kp].c > order[i].c {
				order[i] = k[kp]
				kp++
			}
		}
	}
	s, t := "", 0
	for i := 0; i < os; i++ {
		t += int(order[i].c)
		s = fmt.Sprintf("%s %d", s, order[i].n)
	}
	printf("%d\n%s\n", t, s[1:])
}

var lineScanner *bufio.Scanner
var wordScanner *bufio.Scanner

func init() {
	lineScanner = bufio.NewScanner(os.Stdin)
	wordScanner = bufio.NewScanner(strings.NewReader(""))
	wordScanner.Split(bufio.ScanWords)
}
func printf(format string, a ...interface{}) {
	fmt.Printf(format, a...)
}
func nextLine() (string, error) {
	if !lineScanner.Scan() {
		if err := lineScanner.Err(); err != nil {
			panic(err)
		}
		return "", errors.New("nextLine: EOF reached")
	}
	return lineScanner.Text(), nil
}

func nextWord() string {
	for !wordScanner.Scan() {
		if err := wordScanner.Err(); err != nil {
			panic(err)
		}
		line, err := nextLine()
		if err != nil {
			return ""
		}
		wordScanner = bufio.NewScanner(strings.NewReader(line))
		wordScanner.Split(bufio.ScanWords)
	}
	return wordScanner.Text()
}

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

func nextInt64() int64 {
	res, err := strconv.ParseInt(nextWord(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func nextFloat64() float64 {
	res, err := strconv.ParseFloat(nextWord(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

package main

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

type wv struct {
	c int16
	n int32
	s int8
}

func (x wv) String() string {
	return strconv.Itoa(int(x.n))
}

type wvs []*wv

func (slice wvs) Len() int {
	return len(slice)
}

func (slice wvs) Less(i, j int) bool {
	return slice[i].c > slice[j].c;
}

func (slice wvs) Swap(i, j int) {
	slice[i], slice[j] = slice[j], slice[i]
}

func main() {
	n, v := nextInt(), nextInt()
	k, c := make(wvs, n), make(wvs, n)
	ks, cs := 0, 0
	for i := 0; i < n; i++ {
		s, m := nextInt(), nextInt()
		if s == 1 {
			k[ks] = &wv{int16(m), int32(i + 1), int8(1)}
			ks++
		} else {
			c[cs] = &wv{int16(m), int32(i + 1), int8(2)}
			cs++
		}
		if i % 100 == 0 {
			runtime.GC()
		}
	}
	k = k[:ks]
	runtime.GC()
	c = c[:cs]
	runtime.GC()
	sort.Sort(k)
	runtime.GC()
	sort.Sort(c)
	runtime.GC()
	order := make(wvs, n)
	os := 0
	kp, cp := 0, 0
	for v > 1 && cp < cs {
		order[os] = c[cp]
		os++
		cp++
		v -= 2
		if v % 100 == 0 {
			runtime.GC()
		}
	}
	if v > 0 && kp < ks {
		order[os] = k[kp]
		os++
		kp++
		v--
		if v % 100 == 0 {
			runtime.GC()
		}
	}
	for i := os - 1; i >= 0; i-- {
		if order[i].s == 1 {
			continue
		}
		if ks - kp > 1 {
			x := k[kp].c + k[kp + 1].c
			if x > order[i].c {
				order[i] = k[kp]
				order[os] = k[kp + 1]
				os++
				kp += 2
			}
		} else {
			if k[kp].c > order[i].c {
				order[i] = k[kp]
				kp++
			}
		}
		if i % 100 == 0 {
			runtime.GC()
		}
	}
	s, t := "", 0
	s = fmt.Sprintf("%v", order)
	s = s[1:len(s) - 1]
	printf("%d\n%s\n", t, s)
}

var lineScanner *bufio.Scanner
var wordScanner *bufio.Scanner

func init() {
	lineScanner = bufio.NewScanner(os.Stdin)
	wordScanner = bufio.NewScanner(strings.NewReader(""))
	wordScanner.Split(bufio.ScanWords)
}
func printf(format string, a ...interface{}) {
	fmt.Printf(format, a...)
}
func nextLine() (string, error) {
	if !lineScanner.Scan() {
		if err := lineScanner.Err(); err != nil {
			panic(err)
		}
		return "", errors.New("nextLine: EOF reached")
	}
	return lineScanner.Text(), nil
}

func nextWord() string {
	for !wordScanner.Scan() {
		if err := wordScanner.Err(); err != nil {
			panic(err)
		}
		line, err := nextLine()
		if err != nil {
			return ""
		}
		wordScanner = bufio.NewScanner(strings.NewReader(line))
		wordScanner.Split(bufio.ScanWords)
	}
	return wordScanner.Text()
}

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

func nextInt64() int64 {
	res, err := strconv.ParseInt(nextWord(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func nextFloat64() float64 {
	res, err := strconv.ParseFloat(nextWord(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

package main

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

type wv struct {
	c, n int
}
type wvs []wv

func (slice wvs) Len() int {
	return len(slice)
}

func (slice wvs) Less(i, j int) bool {
	return slice[i].c < slice[j].c;
}

func (slice wvs) Swap(i, j int) {
	slice[i], slice[j] = slice[j], slice[i]
}

func main() {
	n, v := nextInt(), nextInt()
	k, c := make(wvs, 0), make(wvs, 0)
	for i := 0; i < n; i++ {
		s, m := nextInt(), nextInt()
		if s == 1 {
			k = append(k, wv{m, i + 1})
		} else {
			c = append(c, wv{m, i + 1})
		}
	}
	sort.Sort(sort.Reverse(k))
	sort.Sort(sort.Reverse(c))

	order, capacity := make([]int, 0), 0
	for v > 0 {
		if v > 1 {
			if len(c) > 0 {
				if len(k) > 1 {
					if k[0].c > c[0].c {
						capacity += k[0].c
						order = append(order, k[0].n)
						k = k[1:]
					} else {
						capacity += c[0].c
						order = append(order, c[0].n)
						c = c[1:]
						v--
					}
				} else {
					if k[0].c + k[1].c > c[0].c {
						capacity += k[0].c
						capacity += k[1].c
						order = append(order, k[0].n)
						order = append(order, k[1].n)
						k = k[2:]
						v--
					} else {
						capacity += c[0].c
						order = append(order, c[0].n)
						c = c[1:]
						v--
					}
				}
			} else if len(k) > 0 {
				capacity += k[0].c
				order = append(order, k[0].n)
				k = k[1:]
			}
			v--
		} else {
			if len(k) > 0 {
				capacity += k[0].c
				order = append(order, k[0].n)
				k = k[1:]
			}
			v--
		}
	}
	s := fmt.Sprintf("%v", order)
	s = s[1:len(s) - 1]
	printf("%d\n%s\n", capacity, s)
}

var lineScanner *bufio.Scanner
var wordScanner *bufio.Scanner

func init() {
	lineScanner = bufio.NewScanner(os.Stdin)
	wordScanner = bufio.NewScanner(strings.NewReader(""))
	wordScanner.Split(bufio.ScanWords)
}
func printf(format string, a ...interface{}) {
	fmt.Printf(format, a...)
}
func nextLine() (string, error) {
	if !lineScanner.Scan() {
		if err := lineScanner.Err(); err != nil {
			panic(err)
		}
		return "", errors.New("nextLine: EOF reached")
	}
	return lineScanner.Text(), nil
}

func nextWord() string {
	for !wordScanner.Scan() {
		if err := wordScanner.Err(); err != nil {
			panic(err)
		}
		line, err := nextLine()
		if err != nil {
			return ""
		}
		wordScanner = bufio.NewScanner(strings.NewReader(line))
		wordScanner.Split(bufio.ScanWords)
	}
	return wordScanner.Text()
}

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

func nextInt64() int64 {
	res, err := strconv.ParseInt(nextWord(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func nextFloat64() float64 {
	res, err := strconv.ParseFloat(nextWord(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

package main

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

type wv struct {
	c, n int
}
type wvs []wv

func (slice wvs) Len() int {
	return len(slice)
}

func (slice wvs) Less(i, j int) bool {
	return slice[i].c < slice[j].c;
}

func (slice wvs) Swap(i, j int) {
	slice[i], slice[j] = slice[j], slice[i]
}

func main() {
	n, v := nextInt(), nextInt()
	ov := v
	k, c := make(wvs, 0), make(wvs, 0)
	for i := 0; i < n; i++ {
		s, m := nextInt(), nextInt()
		if s == 1 {
			k = append(k, wv{m, i + 1})
		} else {
			c = append(c, wv{m, i + 1})
		}
	}
	sort.Sort(sort.Reverse(k))
	sort.Sort(sort.Reverse(c))

	order, capacity, usedCap := make([]int, 0), 0, 0
	var lastK *wv = nil
	for v > 0 {
		if v > 1 {
			if len(c) > 0 {
				if len(k) > 1 {
					if k[0].c + k[1].c > c[0].c {
						capacity += k[0].c
						capacity += k[1].c
						lastK = &k[1]
						order = append(order, k[0].n)
						order = append(order, k[1].n)
						k = k[2:]
						v--
						usedCap += 2
					} else {
						capacity += c[0].c
						order = append(order, c[0].n)
						c = c[1:]
						v--
						usedCap += 2
					}
				} else if len(k) > 0{
					if k[0].c > c[0].c {
						capacity += k[0].c
						lastK = &k[0]
						order = append(order, k[0].n)
						k = k[1:]
						usedCap++
					} else {
						capacity += c[0].c
						order = append(order, c[0].n)
						c = c[1:]
						v--
						usedCap += 2
					}
				} else {
					capacity += c[0].c
					order = append(order, c[0].n)
					c = c[1:]
					v--
					usedCap += 2
				}
			} else if len(k) > 0 {
				capacity += k[0].c
				order = append(order, k[0].n)
				lastK = &k[0]
				k = k[1:]
				usedCap++
			}
		} else {
			if len(k) > 0 {
				capacity += k[0].c
				order = append(order, k[0].n)
				lastK = &k[0]
				k = k[1:]
				usedCap++
			}
		}
		v--
	}
	if usedCap != ov && len(c) > 0 {
		if lastK.c < c[0].c {
			capacity -= lastK.c
			capacity += c[0].c
			for i, _ := range order {
				if order[i] == lastK.n {
					order[i] = c[0].n
				}
			}
		}
	}
	s := fmt.Sprintf("%v", order)
	s = s[1:len(s) - 1]
	printf("%d\n%s\n", capacity, s)
}

var lineScanner *bufio.Scanner
var wordScanner *bufio.Scanner

func init() {
	lineScanner = bufio.NewScanner(os.Stdin)
	wordScanner = bufio.NewScanner(strings.NewReader(""))
	wordScanner.Split(bufio.ScanWords)
}
func printf(format string, a ...interface{}) {
	fmt.Printf(format, a...)
}
func nextLine() (string, error) {
	if !lineScanner.Scan() {
		if err := lineScanner.Err(); err != nil {
			panic(err)
		}
		return "", errors.New("nextLine: EOF reached")
	}
	return lineScanner.Text(), nil
}

func nextWord() string {
	for !wordScanner.Scan() {
		if err := wordScanner.Err(); err != nil {
			panic(err)
		}
		line, err := nextLine()
		if err != nil {
			return ""
		}
		wordScanner = bufio.NewScanner(strings.NewReader(line))
		wordScanner.Split(bufio.ScanWords)
	}
	return wordScanner.Text()
}

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

func nextInt64() int64 {
	res, err := strconv.ParseInt(nextWord(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func nextFloat64() float64 {
	res, err := strconv.ParseFloat(nextWord(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

package main

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

type wv struct {
	c int16
	n int32
	s int8
}

func (x wv) String() string {
	return strconv.Itoa(int(x.n))
}

type wvs []*wv

func (slice wvs) Len() int {
	return len(slice)
}

func (slice wvs) Less(i, j int) bool {
	return slice[i].c > slice[j].c;
}

func (slice wvs) Swap(i, j int) {
	slice[i], slice[j] = slice[j], slice[i]
}

func main() {
	n, v := nextInt(), nextInt()
	k, c := make(wvs, n), make(wvs, n)
	ks, cs := 0, 0
	for i := 0; i < n; i++ {
		s, m := nextInt(), nextInt()
		if s == 1 {
			k[ks] = &wv{int16(m), int32(i + 1), int8(1)}
			ks++
		} else {
			c[cs] = &wv{int16(m), int32(i + 1), int8(2)}
			cs++
		}
		if i % 1000 == 0 {
			runtime.GC()
		}
	}
	k = k[:ks]
	c = c[:cs]
	sort.Sort(k)
	sort.Sort(c)
	runtime.GC()
	order := make(wvs, v)
	os := 0
	kp, cp := 0, 0
	for v > 1 && cp < cs {
		order[os] = c[cp]
		os++
		cp++
		v -= 2
		if v % 1000 == 0 {
			runtime.GC()
		}
	}
	if v > 0 && kp < ks {
		order[os] = k[kp]
		os++
		kp++
		v--
		if v % 1000 == 0 {
			runtime.GC()
		}
	}
	for i := os - 1; i >= 0; i-- {
		if order[i].s == 1 {
			continue
		}
		if ks - kp > 1 {
			x := k[kp].c + k[kp + 1].c
			if x > order[i].c {
				order[i] = k[kp]
				order[os] = k[kp + 1]
				os++
				kp += 2
			}
		} else {
			if k[kp].c > order[i].c {
				order[i] = k[kp]
				kp++
			}
		}
		if i % 1000 == 0 {
			runtime.GC()
		}
	}
	s, t := "", 0
	order = order[:os]
	runtime.GC()
	for _, x := range order {
		t += int(x.c)
	}
	s = fmt.Sprintf("%v", order[:os])
	s = s[1:len(s) - 1]
	printf("%d\n%s\n", t, s)
}

var lineScanner *bufio.Scanner
var wordScanner *bufio.Scanner

func init() {
	lineScanner = bufio.NewScanner(os.Stdin)
	wordScanner = bufio.NewScanner(strings.NewReader(""))
	wordScanner.Split(bufio.ScanWords)
}
func printf(format string, a ...interface{}) {
	fmt.Printf(format, a...)
}
func nextLine() (string, error) {
	if !lineScanner.Scan() {
		if err := lineScanner.Err(); err != nil {
			panic(err)
		}
		return "", errors.New("nextLine: EOF reached")
	}
	return lineScanner.Text(), nil
}

func nextWord() string {
	for !wordScanner.Scan() {
		if err := wordScanner.Err(); err != nil {
			panic(err)
		}
		line, err := nextLine()
		if err != nil {
			return ""
		}
		wordScanner = bufio.NewScanner(strings.NewReader(line))
		wordScanner.Split(bufio.ScanWords)
	}
	return wordScanner.Text()
}

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

func nextInt64() int64 {
	res, err := strconv.ParseInt(nextWord(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func nextFloat64() float64 {
	res, err := strconv.ParseFloat(nextWord(), 64)
	if err != nil {
		panic(err)
	}
	return res
}


package main

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

func main() {
	var n, m int // rows, cols
	fmt.Scanln(&n, &m)

	// index col, then row
	a := make([][]int, m)
	for i := 0; i < m; i++ {
		a[i] = make([]int, n)
	}

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			scanner.Scan()
			a[j][i], _ = strconv.Atoi(scanner.Text())
		}
	}

	s := 0

	for i := 0; i < m; i++ {
		// we need to make column [i] into the sequence
		// i+1, i+1+m, ..., i+1+(n-1)m

		shifts := make([]int, n)

		// for each position, calculate shift to right position
		for j := 0; j < n; j++ {
			row := a[i][j] - (i+1)
			if row % m == 0 && row / m < n {
				shift := (j - row / m + n) % n
				shifts[shift] += 1
			}
		}

		// choose the number of cycles to minimise the remaining moves
		// consider each shift and choose the minimum
		best := n - shifts[0]
		for j := 1; j < n; j++ {
			cur := n - shifts[j] + j
			if cur < best {
				best = cur
			}
		}
		s += best
	}

	fmt.Println(s)
}

package main

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

var _ = sort.Slice
var _ = math.Sqrt
var _ = strconv.Itoa

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n, m int
	fmt.Fscan(in, &n, &m)
	var ans []string
	mape := make([][]int, m)
	for i := 0; i < m; i++ {
		mape[i] = make([]int, n)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Fscan(in, &mape[j][i])
		}
	}
	moves := 0
	for i := 0; i < m; i++ {
		tmp := make([]int, n)
		move := n
		//fmt.Println(i)
		for j := 0; j < n; j++ {
			start := i+1
			now := mape[i][j]
			orgplace := (now-start)/m
			if ((now - start) % m == 0 && now <= n*m){
				tmp[(j-orgplace+n)%n]++
			}
		}
		for j := 0; j < n; j++{
			nowmove := n - tmp[j] + j
			if nowmove < move {
				move = nowmove
			}
		}
		moves = moves + move
	}
	ans = append(ans, fmt.Sprint(moves))
	fmt.Fprintln(out, strings.Join(ans, "\n"));
}

package main

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

var _ = sort.Slice
var _ = math.Sqrt
var _ = strconv.Itoa

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n, m int
	fmt.Fscan(in, &n, &m)
	var ans []string
	mape := make([][]int, m)
	for i := 0; i < m; i++ {
		mape[i] = make([]int, n)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Fscan(in, &mape[j][i])
		}
	}
	moves := 0
	for i := 0; i < m; i++ {
		tmp := make([]int, n)
		move := n
		//fmt.Println(i)
		for j := 0; j < n; j++ {
			start := i+1
			now := mape[i][j]
			orgplace := (now-start)/m
			if (now - start) % m == 0 && now <= n{
				tmp[(j-orgplace+n)%n]++
			}
		}
		for j := 0; j < n; j++{
			nowmove := n - tmp[j] + j
			if nowmove < move {
				move = nowmove
			}
		}
		moves = moves + move
	}
	ans = append(ans, fmt.Sprint(moves))
	fmt.Fprintln(out, strings.Join(ans, "\n"));
}

package main

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

var _ = sort.Slice
var _ = math.Sqrt
var _ = strconv.Itoa

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n, m int
	fmt.Fscan(in, &n, &m)
	var ans []string
	mape := make([][]int, m)
	for i := 0; i < m; i++ {
		mape[i] = make([]int, n)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Fscan(in, &mape[j][i])
		}
	}
	moves := 0
	for i := 0; i < m; i++ {
		tmp := make([]int, n)
		move := n
		for j := 0; j < n; j++ {
			start := i+1
			now := mape[i][j]
			orgplace := (now-start)/3
			if (now - start) % m == 0 {
				tmp[(j-orgplace+n)%n]++
			}
		}
		//fmt.Println(tmp)
		for j := 0; j < n; j++{
			nowmove := n - tmp[j] + j
			if nowmove < move {
				move = nowmove
			}
		}
		moves = moves + move
	}
	ans = append(ans, fmt.Sprint(moves))
	fmt.Fprintln(out, strings.Join(ans, "\n"));
}


package main

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

type edge struct {
	from, to int
}

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

	var t, n int
	var v1, v2 int
Outer:
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		graph := make([][]int, n)
		edges := make([]edge, 0, n)
		for i := 0; i < n-1; i++ {
			Fscan(in, &v1, &v2)
			v1--
			v2--
			if v1 > v2 {
				v1, v2 = v2, v1
			}
			edges = append(edges, edge{v1, v2})
			graph[v1] = append(graph[v1], v2)
			graph[v2] = append(graph[v2], v1)
		}
		// check answer
		// Println(graph)
		start := 0
		for i := range graph {
			if len(graph[i]) >= 3 {
				Fprintln(out, -1)
				continue Outer
			}
			if len(graph[i]) == 1 {
				start = i
			}
		}
		// Println("start:", start)
		flag := true
		// visited := make([]bool, n)
		// visited[start] = true
		m := make(map[edge]int, n)
		for i := 0; i < n-1; i++ {
			adjs := graph[start]
			var v1, v2 int
			v1 = start
			if len(adjs) == 2 {
				for _, v := range adjs {
					_, ok1 := m[edge{start, v}]
					_, ok2 := m[edge{v, start}]
					if ok1 || ok2 {
						continue
					}
					v2 = v
					break
				}
			} else {
				v2 = adjs[0]
			}
			start = v2
			if v1 > v2 {
				v1, v2 = v2, v1
			}
			if flag {
				m[edge{v1, v2}] = 2
			} else {
				m[edge{v1, v2}] = 3
			}
			flag = !flag
		}
		// Println(m)
		for _, e := range edges {
			Fprintf(out, "%d ", m[e])
		}
		Fprintln(out)
	}
}

// func solve(start int, graph [][]int, edges []edge) []int {
//     visited := make([]bool, len(graph))
//     var dfs func(i int, flag bool)
//     dfs = func(i int, flag bool) {
//         if visited[i] {
//             return
//         }
//         visited[i] = true
//         for _, e := range graph[i] {
//
//         }
//     }
// }

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

package main

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

var err = fmt.Errorf("something wrong")

var read = func() func(format string, args ...interface{}) {
	var r = bufio.NewReader(os.Stdin)
	return func(format string, args ...interface{}) {
		_, _ = fmt.Fscanf(r, format, args...)
	}
}()

var write, flush = func() (func(format string, args ...interface{}), func()) {
	var w = bufio.NewWriter(os.Stdout)
	return func(format string, args ...interface{}) {
			_, _ = fmt.Fprintf(w, format, args...)
			if DEBUG {
				_ = w.Flush()
			}
		}, func() {
			_ = w.Flush()
		}
}()

var DEBUG = false

var debug = func(format string, args ... interface{}) {
	if DEBUG {
		write(format, args...)
	}
}

func Int() int {
	var n int
	read("%d\n", &n)
	return n
}

func Int2() (int, int) {
	var n, m int
	read("%d %d\n", &n, &m)
	return n, m
}

func main() {
	defer flush()
	for n := Int(); n > 0; n-- {
		var g = make(gg)
		m := Int()
		for i := 0; i < m-1; i++ {
			a, b := Int2()
			g.add(a, b, i)
		}
		res := solve(g, m)
		debug("ans->")
		for i, v := range res {
			if i != 0 {
				write(" ")
			}
			write("%v", v)
		}
		write("\n")
	}
}

type gg map[int][]struct {
	y, i int
}

func (g gg) add(x, y, i int) {
	g[x] = append(g[x], struct{ y, i int }{y: y, i: i})
	x, y = y, x
	g[x] = append(g[x], struct{ y, i int }{y: y, i: i})
}

func (g gg) find(x, y int) (int, int) {
	if g[x][0].y != y {
		return g[x][0].y, g[x][0].i
	}
	return g[x][1].y, g[x][1].i
}

func (g gg) findStartEnd() (int, int) {
	for _, v := range g {
		if len(v) > 2 {
			return -1, -1
		}
	}
	var d []int
	for k, v := range g {
		if len(v) == 1 {
			d = append(d, k)
		}
	}
	if len(d) != 2 {
		return -1, -1
	}
	debug("start->end %d %d\n", d[0], d[1])
	return d[0], d[1]
}

func solve(g gg, m int) []int {
	debug("g %v", g)
	s, e := g.findStartEnd()
	if s == -1 {
		return []int{-1}
	}
	debug("start-> %v\n", s)
	var res = make([]int, m-1)
	ps := -1
	for {
		next, idx := g.find(s, ps)
		res[idx] = nextPrime()
		debug("start->new_start %v %v %v %v\n", s, next, idx, res[idx])
		if next == e {
			break
		}
		ps, s = s, next
	}
	return res
}

var nextPrime = func() func() int {
	var a, b = 2, 5
	return func() int {
		a, b = b, a
		return a
	}
}()

package main

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

var err = fmt.Errorf("something wrong")

var read = func() func(format string, args ...interface{}) {
	var r = bufio.NewReader(os.Stdin)
	return func(format string, args ...interface{}) {
		_, _ = fmt.Fscanf(r, format, args...)
	}
}()

var write, flush = func() (func(format string, args ...interface{}), func()) {
	var w = bufio.NewWriter(os.Stdout)
	return func(format string, args ...interface{}) {
			_, _ = fmt.Fprintf(w, format, args...)
			if DEBUG {
				_ = w.Flush()
			}
		}, func() {
			_ = w.Flush()
		}
}()

var DEBUG = false

var debug = func(format string, args ... interface{}) {
	if DEBUG {
		write(format, args...)
	}
}

func Int() int {
	var n int
	read("%d\n", &n)
	return n
}

func Int2() (int, int) {
	var n, m int
	read("%d %d\n", &n, &m)
	return n, m
}

func main() {
	defer flush()
	for n := Int(); n > 0; n-- {
		var p [][2]int
		for m := Int()-1; m > 0; m-- {
			a, b := Int2()
			if b < a {
				a, b = b, a
			}
			p = append(p, [2]int{a, b})
		}
		res := solve(p)
		debug("ans->")
		for i, v := range res {
			if i != 0 {
				write(" ")
			}
			write("%v", v)
		}
		write("\n")
	}
}

func solve(p [][2]int) []int {
	var c = make(map[int][][2]int)
	for i, v := range p {
		c[v[0]] = append(c[v[0]], [2]int{i, v[1]})
		c[v[1]] = append(c[v[1]], [2]int{i, v[0]})
	}
	for _, v := range c {
		if len(v) > 2 {
			return []int{-1}
		}
	}
	var d []int
	for i, v := range c {
		if len(v) == 1 {
			d = append(d, i)
		}
	}
	if len(d) != 2 {
		panic(err)
	}
	start, end := d[0], d[1]
	debug("start->end %v %v\n", start, end)
	var res = make([]int, len(p))
	var t int

	for start != end {
		if t == len(p) {
			panic(err)
		}
		if len(c[start]) != 1 {
			panic(err)
		}
		idx, next := c[start][0][0], c[start][0][1]
		res[idx] = nextPrime()
		debug("start->new_start %v %v %v %v\n", start, next, idx, res[idx])
		if len(c[next]) != 2 {
			if len(c[next]) == 1 && next == end {
				break
			}
			panic(err)
		}
		a, b := c[next][0], c[next][1]
		debug("start->next_2 %v %v %v %v\n", start, next, a[1], b[1])
		if a[1] == start {
			debug("start->next_2 %v\n", b[1])
			c[next] = [][2]int {b}
		} else if b[1] == start {
			debug("start->next_2 %v\n", a[1])
			c[next] = [][2]int {a}
		} else {
			panic(err)
		}

		debug("start->new_start %v %v\n", start, next)
		start = next
		t++
	}
	return res
}

var nextPrime = func() func() int {
	var a, b = 2, 5
	return func() int {
		a, b = b, a
		return a
	}
}()

package main

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

type Edge struct {
	u int
	v int
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var t int
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)

		degrees := make([]int, n)
		positions := make(map[Edge]int)
		adjacent := make([][]int, n)
		var x, y int
		for i := 0; i < n-1; i++ {
			fmt.Fscan(in, &x, &y)
			x--
			y--
			degrees[x]++
			degrees[y]++
			positions[Edge{x, y}] = i
			positions[Edge{y, x}] = i
			adjacent[x] = append(adjacent[x], y)
			adjacent[y] = append(adjacent[y], x)
		}

		isPossible := true
		start, end := -1, -1
		for i := range degrees {
			if degrees[i] > 2 {
				isPossible = false
				break
			} else if degrees[i] == 1 {
				if start == -1 {
					start = i
				} else {
					end = i
				}
			}
		}

		if isPossible {
			visited := make([]bool, n)
			ans := make([]int, n-1)
			two := true
			visited[start] = true
			for i := start; i != end; {
				for j := 0; j < len(adjacent[i]); j++ {
					v := adjacent[i][j]
					if visited[v] == false {
						if two {
							ans[positions[Edge{i, v}]] = 2
						} else {
							ans[positions[Edge{i, v}]] = 3
						}
						two = !two
						visited[v] = true
						i = v
						break
					}
				}
			}
			fmt.Fprint(out, ans[0])
			for i := 1; i < n-1; i++ {
				fmt.Fprint(out, " ", ans[i])
			}
		} else {
			fmt.Fprint(out, -1)
		}
		fmt.Fprintln(out)
		out.Flush()
	}
}

package main

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

const mod = 998244353

type idx struct {
	l int
	r int
}

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n int
	fmt.Fscan(in,&n)
	m := map[idx]int{}
	degree := make([]int,n + 1)
	g := make([][]int,n + 1)
	for i := 0;i < n - 1;i++{
		var a,b int
		fmt.Fscan(in,&a,&b)
		m[idx{l:min(a,b),r:max(a,b)}] = i
		g[a] = append(g[a],b)
		g[b] = append(g[b],a)
		degree[a]++
		degree[b]++
	}
	begin := -1
	for i,v := range degree{
		if v > 2{
			fmt.Fprintln(out,-1)
			return
		}
		if v == 1{
			begin = i
		}
	}
	ans := make([]int,n - 1)
	dfs(g,begin,0,0,m,ans)
	for _,v := range ans{
		fmt.Fprintf(out,"%d ",v)
	}
	fmt.Fprintln(out)
}

func dfs(g [][]int,cur,f,nVal int,m map[idx]int,ans []int){
	for _,v := range g[cur]{
		if v != f{
			ix := m[idx{l:min(cur,v),r:max(cur,v)}]
			if nVal == 0{
				ans[ix] = 2
			}else{
				ans[ix] = 5
			}
			dfs(g,v,cur,1 - nVal,m,ans)
		}
	}
}

func abs(a,b int)int{
	if a > b{
		return a - b
	}
	return b - a
}
func sum(a,b int64)int64{
	return (b + a) * (b - a + 1) / 2
}

func max(a,b int)int{
	if a > b{
		return a
	}
	return b
}
func min(a,b int)int{
	if a > b{
		return b
	}
	return a
}
func gcd(a,b int)int{
	if a % b == 0{
		return b
	}
	return gcd(b,a%b)
}

func main() {
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 1
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {
		solve(in, out)
	}
}

package main

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

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

	defer out.Flush()

	var T, n, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		type edge struct{ to, i int }
		g := make([][]edge, n)
		ok := true
		for i := 0; i < n-1; i++ {
			Fscan(in, &v, &w)
			v--
			w--
			g[v] = append(g[v], edge{w, i})
			g[w] = append(g[w], edge{v, i})
			ok = ok && len(g[v]) < 3 && len(g[w]) < 3
		}
		if !ok {
			Fprintln(out, -1)
			continue
		}

		st := 0
		for i, e := range g {
			if len(e) == 1 {
				st = i
				break
			}
		}

		ans := make([]int, n-1)
		cur := 2
		var dfs func(int, int)
		dfs = func(v, fa int) {
			for _, e := range g[v] {
				w := e.to
				if w != fa {
					ans[e.i] = cur
					cur = 5 - cur
					dfs(w, v)
				}
			}
		}

		dfs(st, -1)
		for _, v := range ans {
			Fprint(out, v, " ")
		}
		Fprintln(out)

	}
}

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

package main

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

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

	var T, n, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		type edge struct{ to, i int }
		g := make([][]edge, n)
		ok := true
		for i := 0; i < n-1; i++ {
			Fscan(in, &v, &w)
			v--
			w--
			g[v] = append(g[v], edge{w, i})
			g[w] = append(g[w], edge{v, i})
			ok = ok && len(g[v]) < 3 && len(g[w]) < 3
		}
		if !ok {
			Fprintln(out, -1)
			continue
		}
		st := 0
		for i, e := range g {
			if len(e) == 1 {
				st = i
				break
			}
		}
		ans := make([]int, n-1)
		cur := 2
		var dfs func(int, int)
		dfs = func(v, fa int) {
			for _, e := range g[v] {
				w := e.to
				if w != fa {
					ans[e.i] = cur
					cur = 5 - cur
					dfs(w, v)
				}
			}
		}
		dfs(st, -1)
		for _, v := range ans {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}
}

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

package main

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

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

var tree [][]int
var edgeId [][]int
var ans []int

func solve(node, parent, prev int) bool {
	if len(tree[node]) > 2 {
		return false
	}
	if prev == -1 {
		if len(tree[node]) == 2 {
			ans[edgeId[node][0]] = 2
			ans[edgeId[node][1]] = 3
			if solve(tree[node][0], node, 2) && solve(tree[node][1], node, 3) {
				return true
			}
			ans[edgeId[node][0]] = 3
			ans[edgeId[node][1]] = 2
			return solve(tree[node][0], node, 3) && solve(tree[node][1], node, 2)
		}
		if len(tree[node]) == 1 {
			ans[edgeId[node][0]] = 2
			if solve(tree[node][0], node, 2) {
				return true
			}
			ans[edgeId[node][0]] = 3
			return solve(tree[node][0], node, 3)
		}
	}
	if len(tree[node]) == 1 {
		return true
	}
	var index = 0
	if tree[node][index] == parent {
		index++
	}
	var eid = edgeId[node][index]
	ans[eid] = 5 - prev
	return solve(tree[node][index], node, 5-prev)
}

func solveCase() {
	var n int
	fmt.Fscan(in, &n)
	tree = make([][]int, n)
	edgeId = make([][]int, n)
	ans = make([]int, n-1)
	for i := 0; i < n-1; i++ {
		var a, b int
		fmt.Fscan(in, &a, &b)
		a--
		b--
		tree[a] = append(tree[a], b)
		tree[b] = append(tree[b], a)
		edgeId[a] = append(edgeId[a], i)
		edgeId[b] = append(edgeId[b], i)
	}
	// fmt.Fprintf(out, "tree = %v\n", tree)
	// fmt.Fprintf(out, "edgeId = %v\n", edgeId)
	// out.Flush()

	if !solve(0, 0, -1) {
		fmt.Fprintf(out, "-1\n")
	} else {
		for i := 0; i < n-1; i++ {
			fmt.Fprintf(out, "%d ", ans[i])
		}
		fmt.Fprintf(out, "\n")
	}
}

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		solveCase()
	}
	out.Flush()
}

package main

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

var err = fmt.Errorf("something wrong")

var read = func() func(format string, args ...interface{}) {
	var r = bufio.NewReader(os.Stdin)
	return func(format string, args ...interface{}) {
		_, _ = fmt.Fscanf(r, format, args...)
	}
}()

var write, flush = func() (func(format string, args ...interface{}), func()) {
	var w = bufio.NewWriter(os.Stdout)
	return func(format string, args ...interface{}) {
			_, _ = fmt.Fprintf(w, format, args...)
			if DEBUG {
				_ = w.Flush()
			}
		}, func() {
			_ = w.Flush()
		}
}()

var DEBUG = true

var debug = func(format string, args ... interface{}) {
	if DEBUG {
		write(format, args...)
	}
}

func Int() int {
	var n int
	read("%d\n", &n)
	return n
}

func Int2() (int, int) {
	var n, m int
	read("%d %d\n", &n, &m)
	return n, m
}

func main() {
	defer flush()
	for n := Int(); n > 0; n-- {
		var g = make(gg)
		m := Int()
		for i := 0; i < m-1; i++ {
			a, b := Int2()
			g.add(a, b, i)
		}
		res := solve(g, m)
		debug("ans->")
		for i, v := range res {
			if i != 0 {
				write(" ")
			}
			write("%v", v)
		}
		write("\n")
	}
}

type gg map[int][]struct {
	y, i int
}

func (g gg) add(x, y, i int) {
	g[x] = append(g[x], struct{ y, i int }{y: y, i: i})
	x, y = y, x
	g[x] = append(g[x], struct{ y, i int }{y: y, i: i})
}

func (g gg) find(x, y int) (int, int) {
	if g[x][0].y != y {
		return g[x][0].y, g[x][0].i
	}
	return g[x][1].y, g[x][1].i
}

func (g gg) findStartEnd() (int, int) {
	for _, v := range g {
		if len(v) > 2 {
			return -1, -1
		}
	}
	var d []int
	for k, v := range g {
		if len(v) == 1 {
			d = append(d, k)
		}
	}
	if len(d) != 2 {
		return -1, -1
	}
	debug("start->end %d %d\n", d[0], d[1])
	return d[0], d[1]
}

func solve(g gg, m int) []int {
	debug("g %v", g)
	s, e := g.findStartEnd()
	if s == -1 {
		return []int{-1}
	}
	debug("start-> %v\n", s)
	var res = make([]int, m-1)
	ps := -1
	for {
		next, idx := g.find(s, ps)
		res[idx] = nextPrime()
		debug("start->new_start %v %v %v %v\n", s, next, idx, res[idx])
		if next == e {
			break
		}
		ps, s = s, next
	}
	return res
}

var nextPrime = func() func() int {
	var a, b = 2, 5
	return func() int {
		a, b = b, a
		return a
	}
}()

package main

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

type edge struct {
	from, to int
}

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

	var t, n int
	var v1, v2 int
Outer:
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		graph := make([][]int, n)
		edges := make([]edge, 0, n)
		for i := 0; i < n-1; i++ {
			Fscan(in, &v1, &v2)
			v1--
			v2--
			if v1 > v2 {
				v1, v2 = v2, v1
			}
			edges = append(edges, edge{v1, v2})
			graph[v1] = append(graph[v1], v2)
			graph[v2] = append(graph[v2], v1)
		}
		// check answer
		// Println(graph)
		start := 0
		for i := range graph {
			if len(graph[i]) >= 3 {
				Fprintln(out, -1)
				break Outer
			}
			if len(graph[i]) == 1 {
				start = i
			}
		}
		// Println("start:", start)
		flag := true
		// visited := make([]bool, n)
		// visited[start] = true
		m := make(map[edge]int, n)
		for i := 0; i < n-1; i++ {
			adjs := graph[start]
			var v1, v2 int
			v1 = start
			if len(adjs) == 2 {
				for _, v := range adjs {
					_, ok1 := m[edge{start, v}]
					_, ok2 := m[edge{v, start}]
					if ok1 || ok2 {
						continue
					}
					v2 = v
					break
				}
			} else {
				v2 = adjs[0]
			}
			start = v2
			if v1 > v2 {
				v1, v2 = v2, v1
			}
			if flag {
				m[edge{v1, v2}] = 2
			} else {
				m[edge{v1, v2}] = 3
			}
			flag = !flag
		}
		// Println(m)
		for _, e := range edges {
			Fprintf(out, "%d ", m[e])
		}
		Fprintln(out)
	}
}

// func solve(start int, graph [][]int, edges []edge) []int {
//     visited := make([]bool, len(graph))
//     var dfs func(i int, flag bool)
//     dfs = func(i int, flag bool) {
//         if visited[i] {
//             return
//         }
//         visited[i] = true
//         for _, e := range graph[i] {
//
//         }
//     }
// }

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

package main

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

type edge struct {
	from, to int
}

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

	var t, n int
	var v1, v2 int
Outer:
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		graph := make([][]int, n)
		edges := make([]edge, 0, n)
		for i := 0; i < n-1; i++ {
			Fscan(in, &v1, &v2)
			v1--
			v2--
			if v1 > v2 {
				v1, v2 = v2, v1
			}
			edges = append(edges, edge{v1, v2})
			graph[v1] = append(graph[v1], v2)
			graph[v2] = append(graph[v2], v1)
		}
		// check answer
		// Println(graph)
		start := 0
		for i := range graph {
			if len(graph[i]) >= 3 {
				Fprintln(out, -1)
				break Outer
			}
			if len(graph[i]) == 1 {
				start = i
			}
		}
		// Println("start:", start)
		flag := true
		// visited := make([]bool, n)
		// visited[start] = true
		m := make(map[edge]int, n)
		for i := 0; i < n; i++ {
			adjs := graph[start]
			var v1, v2 int
			v1 = start
			if len(adjs) == 2 {
				for _, v := range adjs {
					_, ok1 := m[edge{start, v}]
					_, ok2 := m[edge{v, start}]
					if ok1 || ok2 {
						continue
					}
					v2 = v
				}
			} else {
				v2 = adjs[0]
			}
			start = v2
			if v1 > v2 {
				v1, v2 = v2, v1
			}
			if flag {
				m[edge{v1, v2}] = 2
			} else {
				m[edge{v1, v2}] = 3
			}
			flag = !flag
		}
		for _, e := range edges {
			Fprintf(out, "%d ", m[e])
		}
		Fprintln(out)
	}
}

// func solve(start int, graph [][]int, edges []edge) []int {
//     visited := make([]bool, len(graph))
//     var dfs func(i int, flag bool)
//     dfs = func(i int, flag bool) {
//         if visited[i] {
//             return
//         }
//         visited[i] = true
//         for _, e := range graph[i] {
//
//         }
//     }
// }

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var t int
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)

		degrees := make([]int, n)
		var x, y int
		for i := 0; i < n-1; i++ {
			fmt.Fscan(in, &x, &y)
			degrees[x-1]++
			degrees[y-1]++
		}

		isPossible := true
		for i := range degrees {
			if degrees[i] > 2 {
				isPossible = false
				break
			}
		}

		if isPossible {
			two := true
			for i := 0; i < n-1; i++ {
				if i != 0 {
					fmt.Fprint(out, " ")
				}
				if two {
					fmt.Fprint(out, 2)
				} else {
					fmt.Fprint(out, 3)
				}
				two = !two
			}
		} else {
			fmt.Fprint(out, -1)
		}
		fmt.Fprintln(out)
		out.Flush()
	}
}


package main

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

func run(r io.Reader, w io.Writer) {
	in := bufio.NewReader(r)
	out := bufio.NewWriter(w)
	defer out.Flush()

	var t int
	for Fscan(in, &t); t > 0; t-- {
		var n int
		Fscan(in, &n)

		if n % 2021 == 0 || n % 2020 == 0 {
			Fprintln(out, "YES")

		} else if n % 2020 <= n / 2020 {
			Fprintln(out, "YES")

		} else {
			Fprintln(out, "NO")
		}
	}

}

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

package main

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

func newYearNumber(number int64) bool {
	var result bool = false
	var year int64 = 2020
	if (number%year) <= (number/year) {
		result = true
	}

	return result
}

func main() {
	var number, testCases int64
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	strTestCases := scanner.Text()
	strTestCases = strings.Replace(strTestCases, "\n", "", -1)
	testCases, _ = strconv.ParseInt(strTestCases, 10, 64)

	for testCases > 0 {
		scanner.Scan()
		n := scanner.Text()
		n = strings.TrimSuffix(n, "\n")
		number, _ = strconv.ParseInt(n, 10, 64)
		result := newYearNumber(number)
		if result {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
		testCases--
	}
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scanln(&t)
	for {
		var n int64
		fmt.Scanln(&n)
		a := n / 2020
		b := n % 2020
		if a >= b {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
		t--
		if t == 0 {
			break
		}
	}
}

package main

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

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

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

func main() {
	defer writer.Flush()
	var t, n int
	scanf("%d\n", &t)
	for i := 1; i <= t; i++ {
		scanf("%d\n", &n)
		s := n % 2020
		if s == 0 {
			printf("YES\n")
		} else {
			if (n - s) / 2020 >= s {
				printf("YES\n")
			} else {
				printf("NO\n")
			}
		}
	}
}

package main

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

var bytes []byte

func nextByte() byte {
	b := bytes[0]
	bytes = bytes[1:]
	return b
}

func scanInt64() int64 {
	var negative bool
	var result int64

	b := nextByte()

	for ; b < '0' || b > '9'; b = nextByte() {
		if b == '-' {
			negative = true
		}
	}

	for ; '0' <= b && b <= '9'; b = nextByte() {
		result = (result << 3) + (result << 1) + int64(b-'0')
	}

	if negative {
		return -result
	} else {
		return result
	}
}

func scanInt() int {
	return int(scanInt64())
}

func scanString() string {
	builder := strings.Builder{}

	for b := nextByte(); b != '\r'; b = nextByte() {
		if b != '\n' {
			builder.WriteByte(b)
		}
	}

	return builder.String()
}

func init() {
	f, err := os.Open("in.txt")

	if err == nil {
		defer f.Close()
	} else {
		f = os.Stdin
	}

	bytes, _ = ioutil.ReadAll(bufio.NewReader(f))
}

func main() {
	for t := scanInt(); t > 0; t-- {
		n := scanInt()
		div, mod := n/2020, n%2020

		if mod <= div {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var N int
	var x int
	Fscan(in, &N)
	p := make([] bool, 1000005)
	p[0] = true
	p[2020] = true
	for i := 2021; i< len(p); i++ {
		if p[i - 2020] || p[i - 2021] {
			p[i] = true
		}
	}
	for tt:=0; tt < N; tt ++ {
		Fscan(in, &x)
		ans := "YES\n"
		if !p[x] {
			ans = "NO\n"
		}
		out.WriteString(ans)
	}
}

package main

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

var (
	t      int
	input  io.Reader = bufio.NewReader(os.Stdin)
	output           = bufio.NewWriter(os.Stdout)
)

var (
	arr [1e6 + 3]bool
)

func init() {
	arr[2020] = true
	arr[2021] = true

	for i := 2022; i <= 1e6; i++ {

		if arr[i-2020] == true {
			arr[i] = true
		}

		if arr[i-2021] == true {
			arr[i] = true
		}

	}
}

func solve() {

	var n int
	fmt.Fscan(input, &n)

	if arr[n] {
		fmt.Fprintln(output, "YES")
	} else {
		fmt.Fprintln(output, "NO")
	}
}

func main() {
	fmt.Fscan(input, &t)

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

	defer output.Flush()
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Scanf("%d\n", &n)
		k := n / 2020
		if k == 0 {
			fmt.Printf("NO\n")
		} else {
			if (0 <= n-k*2020) && (n-k*2020 <= k) {
				fmt.Printf("YES\n")
			} else {
				fmt.Printf("NO\n")
			}
		}
	}
}

package main

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

func solve(r *bufio.Reader) {
	var n int64
	fmt.Fscan(r, &n)

	if n/2020 >= n%2020 {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

func main() {
	r := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(r, &n)
	for i := 0; i < n; i++ {
		solve(r)
	}
}

package main

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

/*
3
10103
10105
10099
---
YES
YES
NO
10103 = 2*2020+3*2021
*/

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

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

	for ; t > 0; t-- {
		var n int
		scanf("%d\n", &n)

		qtt := n / 2020
		rem := n % 2020

		if qtt >= rem && 2020*(qtt-rem)+2021*rem == n {
			fmt.Fprintln(writer, "YES")
		} else {
			fmt.Fprintln(writer, "NO")
		}
	}
}

const maxUint = ^uint(0)

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

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

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

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

//goland:noinspection GoUnusedFunction
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	maximum := nums[0]
	for _, num := range nums {
		if num > maximum {
			maximum = num
		}
	}
	return maximum
}

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

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

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

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

	return true
}

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

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

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

	num = abs(num)

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

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

package main

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

func generateToken(tokens map[int32]bool, n int32) map[int32]bool {
	if n > 1000005 || tokens[n] {
		return tokens
	}
	tokens = generateToken(tokens, n+2020)
	tokens = generateToken(tokens, n+2021)
	tokens[n] = true
	return tokens
}

func solve(tokens map[int32]bool, n int32) string {
	if tokens[n] {
		return "YES"
	} else {
		return "NO"
	}
}

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

	tokens := map[int32]bool{}
	tokens = generateToken(tokens, 0)

	tt := int32(0)
	fmt.Fscanf(in, "%d\n", &tt)
	for tt > 0 {
		n := int32(0)

		fmt.Fscanf(in, "%d\n", &n)
		fmt.Fprintf(out, "%s\n", solve(tokens, n))
		tt--
	}
	out.Flush()
}

package main

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

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

func scan(a ...interface{}) (n int, err error)             { return fmt.Fscan(in, a...) }
func scanf(f string, a ...interface{}) (n int, err error)  { return fmt.Fscanf(in, f, a...) }
func printf(f string, a ...interface{}) (n int, err error) { return fmt.Fprintf(out, f, a...) }

//https://codeforces.com/problemset/problem/1475/B

func main() {
	defer out.Flush()

	var n, a int
	scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		scanf("%d\n", &a)
		q, r := a/2020, a%2020
		if r > q {
			printf("NO\n")
		} else {
			printf("YES\n")
		}
	}
}

package main

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

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

	tc := readNum(reader)

	var buf bytes.Buffer

	for tc > 0 {
		tc--
		n := readNum(reader)
		res := solve(n)

		if res {
			buf.WriteString("YES\n")
		} else {
			buf.WriteString("NO\n")
		}
	}

	fmt.Print(buf.String())
}

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 int) bool {
	k, r := n/2020, n%2020
	return r <= k
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scan(&t)
	for ; t > 0; t-- {
		var n int64
		fmt.Scan(&n)

		var cnt2021 = n % 2020
		var cnt2020 = (n-cnt2021)/2020 - cnt2021
		if cnt2020 >= 0 && 2020*cnt2020+2021*cnt2021 == n {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}

}

package main

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

type ti uint64

var scanner = bufio.NewScanner(os.Stdin)

// SplitNums splits a string into a slice of integers
func SplitNums(s string) []ti {
	nums := strings.Split(strings.TrimSpace(s), " ")
	var result []ti
	for _, n := range nums {
		result = append(result, ParseInt(n))
	}
	return result
}

// ParseInt converts a string into an integer
func ParseInt(s string) ti {
	num, _ := strconv.ParseInt(strings.TrimSpace(s), 10, 64)
	return ti(num)
}

// ReadInput reads the input and returns the text
func ReadInput() string {
	scanner.Scan()
	return scanner.Text()
}

func solve(n ti) string {
	if n%2020 == 0 || n%2021 == 0 {
		return "Yes"
	}

	if n < 2020 {
		return "No"
	}

	if n%21 == 0 {
		return solve(n - 2021)
	}
	return solve(n - 2020)
}

func main() {
	input := ReadInput()
	test_cases := ParseInt(input)

	for i := ti(0); i < test_cases; i++ {
		input := ReadInput()
		num := ParseInt(input)
		fmt.Println(solve(num))
	}
}

package main

import "fmt"

func main() {
	var T int
	fmt.Scan(&T)
	for ; T > 0; T-- {
		var n int
		fmt.Scan(&n)
		if n%2020 <= n/2020 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main
//one year will be given ..we need to find if the number can be represented by using 2020 || 2021
import "fmt"

func main() {
	var test int //gets the number of test case
	var input int //gets the input
	fmt.Scanln(&test)
	for i := 0; i < test; i++ {
		fmt.Scan(&input)
		if (input % 2021==0 || input % 2020==0) {
			fmt.Println("YES")
			continue
		}else if true {
			for(input>=2020 ){
				//fmt.Println("the value of input is",input)
				input=input-2021
				if input%2020==0 {
					fmt.Println("YES")
					break
				}
			}
			if input%2020!=0 {
				fmt.Println("NO")
			}
		}else {
			fmt.Println("NO")
			continue
		}
	}
}

package main

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

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

func main() {

	defer out.Flush()

	fmt.Fscan(in, &t)

	for ; t > 0; t-- {

		fmt.Fscan(in, &n)

		fmt.Fprintln(out, solve(n))

	}

}

func solve(n int) string {

	a := 0

	for a*2020 <= n {

		b := 0

		for b*2021 <= n {

			if a*2020+b*2021 == n {
				return "YES"
			}

			b++

		}

		a++

	}

	return "NO"

}

package main

import (
	. "fmt"
)

func main(){
	var t int;
	Scan(&t);

	for i:=0;i<t;i++ {
		var n int;
		Scan(&n);
		var ok bool = false;
		for i:=0;i*2020<=n;i++ {
			if i*2020 <= n && (n-i*2020)%2021 == 0 {
				ok = true;
			}
		}
		if ok == true {
			Println("YES");
		} else{
			Println("NO");
		}
	}
}

package main

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

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

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

func main() {
	t := 10000
	fmt.Fscan(in, &t)
	for ; t != 0; t-- {
		var n int64
		fmt.Fscan(in, &n)
		if (n/2020)*2020+(n/2020) >= n {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import . "fmt"

type pii struct {
	x, y int
}

func main() {
	var t int
	Scan(&t)
	solve := func() {
		var n int
		Scan(&n)
		cnt2021 := n % 2020
		cnt2020 := (n - cnt2021) / 2020 - cnt2021
		if cnt2020 >= 0 && 2020 * cnt2020 + 2021 * cnt2021 == n {
			Println(`YES`)
		} else {
			Println(`NO`)
		}
	}
	for t > 0 {
		solve()
		t--
	}
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scan(&t)
	for i := 1; i <= t; i++ {
		var n int64
		fmt.Scan(&n)
		x := n / 2020
		y := n % 2020
		if (y <= x) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
var (
    input = bufio.NewReader(os.Stdin)
    output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assertion!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)
func main() {
    var t int
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
    output.Flush()
}

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

func solve() {
    var (
        n int
    )
    fmt.Fscan(input, &n)
    // n - b= (a + b)*2020
    for i := 0; n - i >= i*2020; i++ {
        if (n - i) % 2020 == 0 {
            fmt.Fprintln(output, "Yes")
            return
        }
    }
    fmt.Fprintln(output, "No")
}

/*
 * @Author: RBQRBQ
 * @Date: 2021-01-26 21:08:09
 * @LastEditors: RBQRBQ
 * @LastEditTime: 2021-01-26 21:16:49
 */
package main

import "fmt"

var ok [1000000 + 1]bool

func main() {
	for i := 0; i <= 1000000; i++ {
		ok[i] = false
	}
	for i := 0; i <= 1000; i++ {
		for j := 0; j <= 1000; j++ {
			if 2021*i+2020*j <= 1000000 {
				ok[2021*i+2020*j] = true
			}
		}
	}
	var t int
	fmt.Scan(&t)
	for i := 1; i <= t; i++ {
		var x int64
		fmt.Scan(&x)
		if ok[x] == true {
			fmt.Println("YES")
		}
		if ok[x] == false {
			fmt.Println("NO")
		}
	}
}

package main

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

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

func main() {
	defer writer.Flush()

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

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

		if find(n) {
			fmt.Fprintln(writer, "YES")
		} else {
			fmt.Fprintln(writer, "NO")
		}
	}
}

func find(n int) bool {
	for x := 0; x <= 500; x++ {
		for y := 0; y <= 500; y++ {
			if x*2020+y*2021 == n {
				return true
			}
		}
	}
	return false
}

// Written by : Syed Mujtaba
// Seek knowledge from the cradle to the grave
package main

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

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

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

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

func main() {
	defer writer.Flush()

	var t int
	fmt.Scanf("%d\n", &t)
	for T := 0; T < t; T++ {
		var n int64
		scanf("%d\n", &n)
		if (n-n%2020)/2020 >= n%2020 {
			printf("YES\n")
		} else {
			printf("NO\n")
		}
	}
}

package main

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

func main() {
	a:=bufio.NewReader(os.Stdin)
	l, _ :=a.ReadString('\n')
	l=strings.Trim(strings.Trim(l,"\n"),"\r")
	ln, _ :=strconv.Atoi(l)
	for i:=0; i<ln;i++{
		s,_:=a.ReadString('\n')
		d, _ :=strconv.Atoi(strings.Trim(strings.Trim(s,"\n"), "\r"))
		fmt.Printf("%s\n",fn(d))

	}

}

func fn(n int) string{
	x:=n/2020
	o:=n%2020
	if o == 0 || x >= o{
		return "YES"
	}
	return "NO"
}

package main

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

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

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

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}
}

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

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

		initial := n
		k := 0
		if n%2020 == 0 {
			fmt.Println("YES")
		} else {
			for i := 1; initial > 0; i++ {
				if initial%2021 == 0 {
					k = 1
					break
				}
				initial = initial - 2020
			}

			if k == 0 {
				fmt.Println("NO")
			} else {
				fmt.Println("YES")
			}
		}
	}
}

package main

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

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

	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		for i := 0; 2020*i<=n; i++ {
			if (n-2020*i)%2021 == 0 {
				Fprintln(out, "YES")
				return
			}
		}
		Fprintln(out, "NO")
	}

	var t int
	Fscan(in, &t)
	for Case := 1; Case <= t; Case++ {
		solve(Case)
	}

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

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

package main

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

func newYearNumber(number int64) bool {
	var result bool
	if number < 2020 {
		result = false
	} else if number == 2020 || number == 2021 {
		result = true
	} else {
		number -= 2021
		if (number % 2 == 0 && number % 5 == 0 && number % 101 == 0) || (number % 43 == 0 && number % 47 == 0) {
			result = true
		} else {
			result = false
		}
	}

	return result
}

func main() {
	var number, testCases int64
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	strTestCases := scanner.Text()
	strTestCases = strings.Replace(strTestCases, "\n", "", -1)
	testCases, _ = strconv.ParseInt(strTestCases, 10, 64)

	for testCases > 0 {
		scanner.Scan()
		n := scanner.Text()
		n = strings.TrimSuffix(n, "\n")
		number, _ = strconv.ParseInt(n, 10, 64)
		result := newYearNumber(number)
		if result {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
		testCases--
	}
}

package main

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

func newYearNumber(number int64) bool {
	var result bool
	if number <= 2020 {
		result = false
	} else {
		number -= 2021
		if (number % 2 == 0 && number % 5 == 0 && number % 101 == 0) || (number % 43 == 0 && number % 47 == 0) {
			result = true
		} else {
			result = false
		}
	}

	return result
}

func main() {
	var number, testCases int64
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	strTestCases := scanner.Text()
	strTestCases = strings.Replace(strTestCases, "\n", "", -1)
	testCases, _ = strconv.ParseInt(strTestCases, 10, 64)

	for testCases > 0 {
		scanner.Scan()
		n := scanner.Text()
		n = strings.TrimSuffix(n, "\n")
		number, _ = strconv.ParseInt(n, 10, 64)
		result := newYearNumber(number)
		if result {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
		testCases--
	}
}

package main

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

func newYearNumber(number int64) bool {
	var result bool
	if number <= 2020 {
		result = false
	} else {
		number -= 2021
		if (number % 2 == 0 && number % 5 == 0 && number % 101 == 0) || (number % 43 == 0 && number % 47 == 0) {
			result = true
		} else {
			result = false
		}
	}

	return result
}

func main() {
	var number, testCases int64
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	strTestCases := scanner.Text()
	strTestCases = strings.Replace(strTestCases, "\n", "", -1)
	testCases, _ = strconv.ParseInt(strTestCases, 10, 64)

	for testCases > 0 {
		scanner.Scan()
		n := scanner.Text()
		n = strings.TrimSuffix(n, "\n")
		number, _ = strconv.ParseInt(n, 10, 64)
		fmt.Println(newYearNumber(number))
		testCases--
	}
}

package main

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

func newYearNumber(number int64) bool {
	var result bool
	if number <= 2020 {
		result = false
	} else {
		number -= 2021
		if (number % 2 == 0 && number % 5 == 0 && number % 101 == 0) || (number % 43 == 0 && number % 47 == 0) {
			result = true
		} else {
			result = false
		}
	}

	return result
}

func main() {
	var number, testCases int64
	reader := bufio.NewReader(os.Stdin)
	strTestCases, _ := reader.ReadString('\n')
	strTestCases = strings.TrimSuffix(strTestCases, "\n")
	testCases, _ = strconv.ParseInt(strTestCases, 10, 64)

	for testCases > 0 {
		n, _ := reader.ReadString('\n')
		n = strings.TrimSuffix(n, "\n")
		number, _ = strconv.ParseInt(n, 10, 64)
		fmt.Println(newYearNumber(number))
		testCases--
	}
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scanln(&t)
	for {
		var n int64
		fmt.Scanln(&n)
		if n < 2020 {
			fmt.Println("NO")
		} else {
			if n%10 == 0 {
				if n%2020 == 0 {
					fmt.Println("YES")
				} else {
					fmt.Println("NO")
				}
			} else {
				if n%2021 == 0 {
					fmt.Println("YES")
				} else {
					for n%2020 != 0 && n > 2020 {
						n -= 2021
					}
					if n%2020 == 0 {
						fmt.Println("YES")
					} else {
						fmt.Println("NO")
					}
				}

			}
		}
		t--
		if t == 0 {
			break
		}
	}
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scanln(&t)
	for {
		var n int64
		fmt.Scanln(&n)
		if n < 2020 {
			fmt.Println("NO")
		} else {
			if n%10 == 0 {
				if n%2020 == 0 {
					fmt.Println("YES")
				} else {
					fmt.Println("NO")
				}
			} else {
				for n%2020 != 0 && n > 2020 {
					n -= 2021
				}
				if n%2020 == 0 {
					fmt.Println("YES")
				} else {
					fmt.Println("NO")
				}
			}
		}
		t--
		if t == 0 {
			break
		}
	}
}

package main

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

var bytes []byte

func nextByte() byte {
	b := bytes[0]
	bytes = bytes[1:]
	return b
}

func scanInt64() int64 {
	var negative bool
	var result int64

	b := nextByte()

	for ; b < '0' || b > '9'; b = nextByte() {
		if b == '-' {
			negative = true
		}
	}

	for ; '0' <= b && b <= '9'; b = nextByte() {
		result = (result << 3) + (result << 1) + int64(b-'0')
	}

	if negative {
		return -result
	} else {
		return result
	}
}

func scanInt() int {
	return int(scanInt64())
}

func scanString() string {
	builder := strings.Builder{}

	for b := nextByte(); b != '\r'; b = nextByte() {
		if b != '\n' {
			builder.WriteByte(b)
		}
	}

	return builder.String()
}

func init() {
	f, err := os.Open("in.txt")

	if err == nil {
		defer f.Close()
	} else {
		f = os.Stdin
	}

	bytes, _ = ioutil.ReadAll(bufio.NewReader(f))
}

func main() {
	for t := scanInt(); t > 0; t-- {
		n := scanInt()
		div, mod := n/2020, n%2020

		if mod < div {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var N int
	var x int
	Fscan(in, &N)
	p := make([] bool, 1000005)
	p[0] = true
	p[2020] = true
	for i := 2021; i< len(p); i++ {
		if p[i - 2020] || p[i - 2021] {
			p[i] = true
		}
	}
	for tt:=0; tt < N; tt ++ {
		Fscan(in, &x)
		for x>0 && x&1 == 0 {
			x = x >> 1
		}
		ans := "YES\n"
		if !p[x] {
			ans = "NO\n"
		}
		out.WriteString(ans)
	}
}