In a far away kingdom lived the King, the Prince, the Shoemaker, the Dressmaker and many other citizens. They lived happily until great trouble came into the Kingdom. The ACMers settled there.Most damage those strange creatures inflicted upon the kingdom was that they loved high precision numbers. As a result, the Kingdom healers had already had three appointments with the merchants who were asked to sell, say, exactly 0.273549107 beer barrels. To deal with the problem somehow, the King issued an order obliging rounding up all numbers to the closest integer to simplify calculations. Specifically, the order went like this: If a number's integer part does not end with digit 9 and its fractional part is strictly less than 0.5, then the rounded up number coincides with the number’s integer part.  If a number's integer part does not end with digit 9 and its fractional part is not less than 0.5, the rounded up number is obtained if we add 1 to the last digit of the number’s integer part. If the number’s integer part ends with digit 9, to round up the numbers one should go to Vasilisa the Wise. In the whole Kingdom she is the only one who can perform the tricky operation of carrying into the next position. Merchants found the algorithm very sophisticated and they asked you (the ACMers) to help them. Can you write a program that would perform the rounding according to the King’s order?

The first line contains a single number to round up — the integer part (a non-empty set of decimal digits that do not start with 0 — with the exception of a case when the set consists of a single digit — in this case 0 can go first), then follows character «.» (a dot), and then follows the fractional part (any non-empty set of decimal digits). The number's length does not exceed 1000 characters, including the dot. There are no other characters in the input data.

If the last number of the integer part is not equal to 9, print the rounded-up number without leading zeroes. Otherwise, print the message "GOTO Vasilisa." (without the quotes).

package main

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

func main(){
	var s string
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	r.Scan()
	s = r.Text()
	len := len(s)
	mark := 0
	n, m := 0, 0
	num := make([]int, 1010)
	for i:=0; i<len; i++ {
		if string(s[i]) == "." {
			mark = i
			n = int(s[i-1])-'0'
			m = int(s[i+1])-'0'
		}
	}
	for i:=mark-1; i>=0; i-- {
		num[i] = int(s[i]) - '0'
	}
	if n == 9 {
		w.WriteString("GOTO Vasilisa.\n")
	} else if m < 5 {
		for i:=0; i<mark; i++ {
			w.WriteString(string(s[i]))
		}
		w.WriteString("\n")
	} else if m >= 5 {
		num[mark-1] += 1
		for i:=mark-1; i>=0; i-- {
			if num[i]>=10 && i!= 0 {
				num[i] -= 10
				num[i-1] += 1
			}
		}
		for i:=0; i<=mark-1; i++ {
			w.WriteString(strconv.Itoa(num[i]))
		}
		w.WriteString("\n")
	}
	w.Flush()
}

package main

import "fmt"

func main() {
    var s string
    fmt.Scan(&s)
    prev, next := make([]int,0,len(s)), 0
    for k, v := range s {
        if v == '.' {
            next = int(s[k+1]-'0')
            break
        }
        prev = append(prev,int(v-'0'))
    }
    if prev[len(prev)-1] == 9 {
        fmt.Println("GOTO Vasilisa.")
    } else {
        if next >= 5 { prev[len(prev)-1]++ }
        for _, v := range prev {
            fmt.Print(v)
        }
        fmt.Println()
    }
}

// 99A-mic_rizal
package main

import (
    "fmt"
    "strconv"
)

func main() {
    var str string
    var in int
    fmt.Scan(&str)
    l := len(str)
    for i := 0; i < l-1; i++ {
        if str[i] == '.' {
            in = i
            break
        }
    }
    m, _ := strconv.Atoi(string(str[in+1]))
    if str[in-1] == '9' {
        fmt.Println("GOTO Vasilisa.")
        return
    } else if m >= 5 {
        for i := 0; i < l-2; i++ {
            if str[i+1] == '.' {
                a, _ := strconv.Atoi(string(str[i]))
                fmt.Print(a + 1)
                break
            } else {
                if str[i] != '.' {
                    fmt.Print(string(str[i]))
                }
            }
        }
        fmt.Println()
    } else {
        for i := 0; i < l-1; i++ {
            if str[i] == '.' {
                break
            }
            fmt.Print(string(str[i]))
        }
        fmt.Println()
        return
    }
}

package main

import "fmt"

func main() {
    var s string
    fmt.Scan(&s)
    prev, next := make([]int,0,len(s)), 0
    for k, v := range s {
        if v == '.' {
            next = int(s[k+1]-'0')
            break
        }
        prev = append(prev,int(v-'0'))
    }
    if prev[len(prev)-1] == 9 {
        fmt.Println("GOTO Vasilisa")
    } else {
        if next >= 5 { prev[len(prev)-1]++ }
        for _, v := range prev {
            fmt.Print(v)
        }
        fmt.Println()
    }
}

package main

import "fmt"

func main() {
    var s string
    var prev,next int
    fmt.Scan(&s)
    for k, v := range s {
        if v == '.' {
            prev = int(s[k-1]-'0')
            next = int(s[k+1]-'0')
            break
        }
    }
    if prev == 9 {
        fmt.Println("GOTO Vasilisa")
    } else if next < 5 {
        fmt.Println(prev)
    } else {
        fmt.Println(prev+1)
    }
}

package main

import "fmt"

func main() {
    var s string
    fmt.Scan(&s)
    prev, next := 0, 0
    for k, v := range s {
        if v == '.' {
            next = int(s[k+1]-'0')
            break
        }
        prev = (prev*10)+int(v-'0')
    }
    if prev%10 == 9 {
        fmt.Println("GOTO Vasilisa")
    } else if next < 5 {
        fmt.Println(prev)
    } else {
        fmt.Println(prev+1)
    }
}

// 99A-mic_rizal
package main

import (
    "fmt"
    "strconv"
)

func main() {
    var str string
    var in int
    fmt.Scan(&str)
    l := len(str)
    for i := 0; i < l-1; i++ {
        if str[i] == '.' {
            in = i
            break
        }
    }
    m, _ := strconv.Atoi(string(str[in+1]))
    if str[in-1] == '9' {
        fmt.Println("GOTO Vasilisa.")
        return
    } else if m >= 5 {
        for i := 0; i < l-2; i++ {
            if str[i+1] == '.' {
                a, _ := strconv.Atoi(string(str[i]))
                fmt.Print(a + 1)
            } else {
                if str[i] != '.' {
                    fmt.Print(string(str[i]))
                }
            }
        }
        fmt.Println()
    } else {
        for i := 0; i < l-1; i++ {
            if str[i] == '.' {
                break
            }
            fmt.Print(string(str[i]))
        }
        fmt.Println()
        return
    }
}

// 99A-mic_rizal
package main

import (
	"fmt"
	"strconv"
)

func main() {
	var str string
	var in int
	fmt.Scan(&str)
	l := len(str)
	for i := 0; i < l-1; i++ {
		if str[i] == '.' {
			in = i
			break
		}
	}
	m, _ := strconv.Atoi(string(str[in+1]))
	if str[in-1] == '9' {
		fmt.Println("GOTO Vasilisa.")
		return
	} else if m >= 5 {
		a, _ := strconv.ParseFloat(str, 0)
		fmt.Println(int(a) + 1)
		return
	} else {
		for i := 0; i < l-1; i++ {
			if str[i] == '.' {
				break
			}
			fmt.Print(string(str[i]))
		}
		fmt.Println()
		return
	}
}


Mahmoud and Ehab live in a country with n cities numbered from 1 to n and connected by n - 1 undirected roads. It's guaranteed that you can reach any city from any other using these roads. Each city has a number ai attached to it.We define the distance from city x to city y as the xor of numbers attached to the cities on the path from x to y (including both x and y). In other words if values attached to the cities on the path from x to y form an array p of length l then the distance between them is , where  is bitwise xor operation.Mahmoud and Ehab want to choose two cities and make a journey from one to another. The index of the start city is always less than or equal to the index of the finish city (they may start and finish in the same city and in this case the distance equals the number attached to that city). They can't determine the two cities so they try every city as a start and every city with greater index as a finish. They want to know the total distance between all pairs of cities.

The first line contains integer n (1 ≤ n ≤ 105) — the number of cities in Mahmoud and Ehab's country. Then the second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 106) which represent the numbers attached to the cities. Integer ai is attached to the city i. Each of the next n  -  1 lines contains two integers u and v (1  ≤  u,  v  ≤  n, u  ≠  v), denoting that there is an undirected road between cities u and v. It's guaranteed that you can reach any city from any other using these roads.

Output one number denoting the total distance between all pairs of cities.

NoteA bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: https://en.wikipedia.org/wiki/Bitwise_operation#XOR.In the first sample the available paths are:  city 1 to itself with a distance of 1,  city 2 to itself with a distance of 2,  city 3 to itself with a distance of 3,  city 1 to city 2 with a distance of ,  city 1 to city 3 with a distance of ,  city 2 to city 3 with a distance of .  The total distance between all pairs of cities equals 1 + 2 + 3 + 3 + 0 + 1 = 10.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF766E(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, v, w int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	ans := int64(0)
	var f func(v, fa int) [20][2]int
	f = func(v, fa int) (cnt [20][2]int) {
		av := a[v]
		ans += int64(av)
		for i := 0; i < 20; i++ {
			cnt[i][av>>i&1]++
		}
		for _, w := range g[v] {
			if w != fa {
				c := f(w, v)
				for i, p := range c[:] {
					ans += (int64(cnt[i][0])*int64(p[1]) + int64(cnt[i][1])*int64(p[0])) * int64(1<<i)
					cnt[i][av>>i&1] += p[0]
					cnt[i][av>>i&1^1] += p[1]
				}
			}
		}
		return
	}
	f(0, -1)
	Fprint(out, ans)
}

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

package main

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

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

	var n, v, w int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	ans := int64(0)
	var f func(v, fa int) [20][2]int
	f = func(v, fa int) (cnt [20][2]int) {
		av := a[v]
		ans += int64(av)
		for i := 0; i < 20; i++ {
			cnt[i][av>>i&1]++
		}
		for _, w := range g[v] {
			if w != fa {
				for i, p := range f(w, v) {
					ans += (int64(cnt[i][0])*int64(p[1]) + int64(cnt[i][1])*int64(p[0])) * int64(1<<i)
					cnt[i][av>>i&1] += p[0]
					cnt[i][av>>i&1^1] += p[1]
				}
			}
		}
		return
	}
	f(0, -1)
	Fprint(out, ans)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF766E(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, v, w int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	ans := int64(0)
	var f func(v, fa int) [20][2]int
	f = func(v, fa int) (cnt [20][2]int) {
		av := a[v]
		ans += int64(av)
		for i := 0; i < 20; i++ {
			cnt[i][av>>i&1] = 1
		}
		for _, w := range g[v] {
			if w != fa {
				for i, p := range f(w, v) {
					ans += (int64(cnt[i][0])*int64(p[1]) + int64(cnt[i][1])*int64(p[0])) * int64(1<<i)
					cnt[i][av>>i&1] += p[0]
					cnt[i][av>>i&1^1] += p[1]
				}
			}
		}
		return
	}
	f(0, -1)
	Fprint(out, ans)
}

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

package main

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

var stdReader *bufio.Reader = bufio.NewReader(os.Stdin)

func readInt() (result int) {
	var negative, started bool
READER_LOOP:
	for {
		char, err := stdReader.ReadByte()
		switch {
		case err != nil:
			break READER_LOOP
		case char == '-':
			negative = true
		case unicode.IsDigit(rune(char)):
			started = true
			result = result*10 + int(char&0xf)
		case started:
			break READER_LOOP
		}
	}
	if negative {
		result = -result
	}
	return
}

const bits = 21

type value struct {
	zeros, ones int
}

var kick, next, dest []int

var adjacent, saturation []int

var original [][bits]bool

var node [][bits]value

var edge []*[bits]value

func maybePush(v, u, e int) {
	if edge[e] != nil {
		return
	}
	if adjacent[v] == saturation[v] {
		// substract and push
		edge[e] = new([bits]value)
		for b := 0; b < bits; b++ {
			push := node[v][b]
			if original[v][b] {
				push.zeros -= edge[e^1][b].zeros
				push.ones -= edge[e^1][b].ones
			} else {
				push.zeros -= edge[e^1][b].ones
				push.ones -= edge[e^1][b].zeros
			}
			edge[e][b] = push
			if original[u][b] {
				node[u][b].zeros += push.zeros
				node[u][b].ones += push.ones
			} else {
				node[u][b].zeros += push.ones
				node[u][b].ones += push.zeros
			}
		}
		saturation[u]++
	} else if adjacent[v] == saturation[v]+1 && edge[e^1] == nil {
		// push as is
		edge[e] = new([bits]value)
		for b := 0; b < bits; b++ {
			edge[e][b] = node[v][b]
			if original[u][b] {
				node[u][b].zeros += node[v][b].zeros
				node[u][b].ones += node[v][b].ones
			} else {
				node[u][b].zeros += node[v][b].ones
				node[u][b].ones += node[v][b].zeros
			}
		}
		saturation[u]++
	}
}

func dfs(v, p int) {
	var pe int
	for e := kick[v]; e >= 0; e = next[e] {
		u := dest[e]
		maybePush(v, u, e)
		if u == p {
			pe = e
		} else {
			dfs(u, v)
		}
	}
	if p >= 0 {
		maybePush(v, p, pe)
	}
}

func main() {
	n := readInt()
	node = make([][bits]value, n)
	edge = make([]*[bits]value, (n-1)*2)
	original = make([][bits]bool, n)
	for i := range node {
		a := readInt()
		for j := range node[i] {
			original[i][j] = a%2 == 0
			node[i][j] = value{
				zeros: 1 - a%2,
				ones:  a % 2,
			}
			a /= 2
		}
	}
	kick = make([]int, n)
	for i := range kick {
		kick[i] = -1
	}
	next = make([]int, (n-1)*2)
	dest = make([]int, (n-1)*2)
	adjacent = make([]int, n)
	saturation = make([]int, n)
	for i := 0; i < n-1; i++ {
		a, b := readInt()-1, readInt()-1
		adjacent[a]++
		adjacent[b]++

		// edge i * 2: a -> b
		next[i*2] = kick[a]
		dest[i*2] = b
		kick[a] = i * 2

		// edge i * 2 + 1: b -> a
		next[i*2+1] = kick[b]
		dest[i*2+1] = a
		kick[b] = i*2 + 1
	}

	dfs(0, -1)
	dfs(0, -1)

	var total int64
	for b := 0; b < bits; b++ {
		var single, double int64
		for i := 0; i < n; i++ {
			if original[i][b] {
				double += int64(node[i][b].ones)
			} else {
				double += int64(node[i][b].ones - 1)
				single++
			}
		}
		total += (int64(1) << uint32(b)) * int64(single+double/2)
	}
	fmt.Println(total)
}

package main

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

var stdReader *bufio.Reader = bufio.NewReader(os.Stdin)

func readInt() (result int) {
	var negative, started bool
READER_LOOP:
	for {
		char, err := stdReader.ReadByte()
		switch {
		case err != nil:
			break READER_LOOP
		case char == '-':
			negative = true
		case unicode.IsDigit(rune(char)):
			started = true
			result = result*10 + int(char&0xf)
		case started:
			break READER_LOOP
		}
	}
	if negative {
		result = -result
	}
	return
}

const bits = 30

type value struct {
	zeros, ones int
}

var kick, next, dest []int

var adjacent, saturation []int

var original [][bits]bool

var node [][bits]value

var edge []*[bits]value

func maybePush(v, u, e int) {
	if edge[e] != nil {
		return
	}
	if adjacent[v] == saturation[v] {
		// substract and push
		edge[e] = new([bits]value)
		for b := 0; b < bits; b++ {
			push := node[v][b]
			if original[v][b] {
				push.zeros -= edge[e^1][b].zeros
				push.ones -= edge[e^1][b].ones
			} else {
				push.zeros -= edge[e^1][b].ones
				push.ones -= edge[e^1][b].zeros
			}
			edge[e][b] = push
			if original[u][b] {
				node[u][b].zeros += push.zeros
				node[u][b].ones += push.ones
			} else {
				node[u][b].zeros += push.ones
				node[u][b].ones += push.zeros
			}
		}
		saturation[u]++
	} else if adjacent[v] == saturation[v]+1 && edge[e^1] == nil {
		// push as is
		edge[e] = new([bits]value)
		for b := 0; b < bits; b++ {
			edge[e][b] = node[v][b]
			if original[u][b] {
				node[u][b].zeros += node[v][b].zeros
				node[u][b].ones += node[v][b].ones
			} else {
				node[u][b].zeros += node[v][b].ones
				node[u][b].ones += node[v][b].zeros
			}
		}
		saturation[u]++
	}
}

func dfs(v, p int) {
	var pe int
	for e := kick[v]; e >= 0; e = next[e] {
		u := dest[e]
		maybePush(v, u, e)
		if u == p {
			pe = e
		} else {
			dfs(u, v)
		}
	}
	if p >= 0 {
		maybePush(v, p, pe)
	}
}

func main() {
	n := readInt()
	node = make([][bits]value, n)
	edge = make([]*[bits]value, (n-1)*2)
	original = make([][bits]bool, n)
	for i := range node {
		a := readInt()
		for j := range node[i] {
			original[i][j] = a%2 == 0
			node[i][j] = value{
				zeros: 1 - a%2,
				ones:  a % 2,
			}
			a /= 2
		}
	}
	kick = make([]int, n)
	for i := range kick {
		kick[i] = -1
	}
	next = make([]int, (n-1)*2)
	dest = make([]int, (n-1)*2)
	adjacent = make([]int, n)
	saturation = make([]int, n)
	for i := 0; i < n-1; i++ {
		a, b := readInt()-1, readInt()-1
		adjacent[a]++
		adjacent[b]++

		// edge i * 2: a -> b
		next[i*2] = kick[a]
		dest[i*2] = b
		kick[a] = i * 2

		// edge i * 2 + 1: b -> a
		next[i*2+1] = kick[b]
		dest[i*2+1] = a
		kick[b] = i*2 + 1
	}

	dfs(0, -1)
	dfs(0, -1)

	for e := range edge {
		if edge[e] == nil {
			panic("Edge can't be nil")
		}
	}

	var total int64
	for b := 0; b < bits; b++ {
		var single, double int64
		for i := 0; i < n; i++ {
			if original[i][b] {
				double += int64(node[i][b].ones)
			} else {
				double += int64(node[i][b].ones - 1)
				single++
			}
		}
		total += (int64(1) << uint32(b)) * int64(single+double/2)
	}
	fmt.Println(total)
}

package main

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

var stdReader *bufio.Reader = bufio.NewReader(os.Stdin)

func readInt() (result int) {
	var negative, started bool
READER_LOOP:
	for {
		char, err := stdReader.ReadByte()
		switch {
		case err != nil:
			break READER_LOOP
		case char == '-':
			negative = true
		case unicode.IsDigit(rune(char)):
			started = true
			result = result*10 + int(char&0xf)
		case started:
			break READER_LOOP
		}
	}
	if negative {
		result = -result
	}
	return
}

const bits = 30

type value struct {
	zeros, ones int
}

var kick, next, dest []int

var adjacent, saturation []int

var original [][bits]bool

var node [][bits]value

var edge []*[bits]value

func maybePush(v, u, e int) {
	if edge[e] != nil {
		return
	}
	if adjacent[v] == saturation[v] {
		// substract and push
		edge[e] = new([bits]value)
		for b := 0; b < bits; b++ {
			push := node[v][b]
			if original[v][b] {
				push.zeros -= edge[e^1][b].zeros
				push.ones -= edge[e^1][b].ones
			} else {
				push.zeros -= edge[e^1][b].ones
				push.ones -= edge[e^1][b].zeros
			}
			edge[e][b] = push
			if original[u][b] {
				node[u][b].zeros += push.zeros
				node[u][b].ones += push.ones
			} else {
				node[u][b].zeros += push.ones
				node[u][b].ones += push.zeros
			}
		}
		saturation[u]++
	} else if adjacent[v] == saturation[v]+1 && edge[e^1] == nil {
		// push as is
		edge[e] = new([bits]value)
		for b := 0; b < bits; b++ {
			edge[e][b] = node[v][b]
			if original[u][b] {
				node[u][b].zeros += node[v][b].zeros
				node[u][b].ones += node[v][b].ones
			} else {
				node[u][b].zeros += node[v][b].ones
				node[u][b].ones += node[v][b].zeros
			}
		}
		saturation[u]++
	}
}

func dfs(v, p int) {
	var pe int
	for e := kick[v]; e >= 0; e = next[e] {
		u := dest[e]
		maybePush(v, u, e)
		if u == p {
			pe = e
		} else {
			dfs(u, v)
		}
	}
	if p >= 0 {
		maybePush(v, p, pe)
	}
}

func main() {
	n := readInt()
	node = make([][bits]value, n)
	edge = make([]*[bits]value, (n-1)*2)
	original = make([][bits]bool, n)
	for i := range node {
		a := readInt()
		for j := range node[i] {
			original[i][j] = a%2 == 0
			node[i][j] = value{
				zeros: 1 - a%2,
				ones:  a % 2,
			}
			a /= 2
		}
	}
	kick = make([]int, n)
	for i := range kick {
		kick[i] = -1
	}
	next = make([]int, (n-1)*2)
	dest = make([]int, (n-1)*2)
	adjacent = make([]int, n)
	saturation = make([]int, n)
	for i := 0; i < n-1; i++ {
		a, b := readInt()-1, readInt()-1
		adjacent[a]++
		adjacent[b]++

		// edge i * 2: a -> b
		next[i*2] = kick[a]
		dest[i*2] = b
		kick[a] = i * 2

		// edge i * 2 + 1: b -> a
		next[i*2+1] = kick[b]
		dest[i*2+1] = a
		kick[b] = i*2 + 1
	}

	dfs(0, -1)
	dfs(0, -1)

	var total int64
	for b := 0; b < bits; b++ {
		var single, double int
		for i := 0; i < n; i++ {
			if original[i][b] {
				double += node[i][b].ones
			} else {
				double += node[i][b].ones - 1
				single++
			}
		}
		total += (int64(1) << uint32(b)) * int64(single+double/2)
	}
	fmt.Println(total)
}

package main

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

var stdReader *bufio.Reader = bufio.NewReader(os.Stdin)

func readInt() (result int) {
	var negative, started bool
READER_LOOP:
	for {
		char, err := stdReader.ReadByte()
		switch {
		case err != nil:
			break READER_LOOP
		case char == '-':
			negative = true
		case unicode.IsDigit(rune(char)):
			started = true
			result = result*10 + int(char&0xf)
		case started:
			break READER_LOOP
		}
	}
	if negative {
		result = -result
	}
	return
}

const bits = 30

type value struct {
	zeros, ones int
}

var kick, next, dest []int

var adjacent, saturation []int

var original [][bits]bool

var node [][bits]value

var edge []*[bits]value

func dfs(v, p int) {
	for e := kick[v]; e >= 0; e = next[e] {
		u := dest[e]
		if edge[e] == nil {
			if adjacent[v] == saturation[v] {
				// substract and push
				edge[e] = new([bits]value)
				for b := 0; b < bits; b++ {
					push := node[v][b]
					if original[v][b] {
						push.zeros -= edge[e^1][b].zeros
						push.ones -= edge[e^1][b].ones
					} else {
						push.zeros -= edge[e^1][b].ones
						push.ones -= edge[e^1][b].zeros
					}
					edge[e][b] = push
					if original[u][b] {
						node[u][b].zeros += push.zeros
						node[u][b].ones += push.ones
					} else {
						node[u][b].zeros += push.ones
						node[u][b].ones += push.zeros
					}
				}
				saturation[u]++
			} else if adjacent[v] == saturation[v]+1 && edge[e^1] == nil {
				// push as is
				edge[e] = new([bits]value)
				for b := 0; b < bits; b++ {
					push := node[v][b]
					edge[e][b] = push
					if original[u][b] {
						node[u][b].zeros += push.zeros
						node[u][b].ones += push.ones
					} else {
						node[u][b].zeros += push.ones
						node[u][b].ones += push.zeros
					}
				}
				saturation[u]++
			}
		}
		if u != p {
			dfs(u, v)
		}
	}
}

func main() {
	n := readInt()
	node = make([][bits]value, n)
	edge = make([]*[bits]value, (n-1)*2)
	original = make([][bits]bool, n)
	for i := range node {
		a := readInt()
		for j := range node[i] {
			original[i][j] = a%2 == 0
			node[i][j] = value{
				zeros: 1 - a%2,
				ones:  a % 2,
			}
			a /= 2
		}
	}
	kick = make([]int, n)
	for i := range kick {
		kick[i] = -1
	}
	next = make([]int, (n-1)*2)
	dest = make([]int, (n-1)*2)
	adjacent = make([]int, n)
	saturation = make([]int, n)
	for i := 0; i < n-1; i++ {
		a, b := readInt()-1, readInt()-1
		adjacent[a]++
		adjacent[b]++

		// edge i * 2: a -> b
		next[i*2] = kick[a]
		dest[i*2] = b
		kick[a] = i * 2

		// edge i * 2 + 1: b -> a
		next[i*2+1] = kick[b]
		dest[i*2+1] = a
		kick[b] = i*2 + 1
	}

	dfs(0, -1)
	dfs(0, -1)

	var total int64
	for b := 0; b < bits; b++ {
		var single, double int
		for i := 0; i < n; i++ {
			if original[i][b] {
				double += node[i][b].ones
			} else {
				double += node[i][b].ones - 1
				single++
			}
		}
		total += (int64(1) << uint32(b)) * int64(single+double/2)
	}
	fmt.Println(total)
}

package main

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

var stdReader *bufio.Reader = bufio.NewReader(os.Stdin)

func readInt() (result int) {
	var negative, started bool
READER_LOOP:
	for {
		char, err := stdReader.ReadByte()
		switch {
		case err != nil:
			break READER_LOOP
		case char == '-':
			negative = true
		case unicode.IsDigit(rune(char)):
			started = true
			result = result*10 + int(char&0xf)
		case started:
			break READER_LOOP
		}
	}
	if negative {
		result = -result
	}
	return
}

const bits = 22

type value struct {
	zeros, ones int
}

var kick, next, dest []int

var adjacent, saturation []int

var original [][bits]bool

var node [][bits]value

var edge []*[bits]value

func dfs(v, p int) {
	for e := kick[v]; e >= 0; e = next[e] {
		u := dest[e]
		if edge[e] == nil {
			if adjacent[v] == saturation[v] {
				// substract and push
				edge[e] = new([bits]value)
				for b := 0; b < bits; b++ {
					push := node[v][b]
					if original[v][b] {
						push.zeros -= edge[e^1][b].zeros
						push.ones -= edge[e^1][b].ones
					} else {
						push.zeros -= edge[e^1][b].ones
						push.ones -= edge[e^1][b].zeros
					}
					edge[e][b] = push
					if original[u][b] {
						node[u][b].zeros += push.zeros
						node[u][b].ones += push.ones
					} else {
						node[u][b].zeros += push.ones
						node[u][b].ones += push.zeros
					}
				}
				saturation[u]++
			} else if adjacent[v] == saturation[v]+1 && edge[e^1] == nil {
				// push as is
				edge[e] = new([bits]value)
				for b := 0; b < bits; b++ {
					push := node[v][b]
					edge[e][b] = push
					if original[u][b] {
						node[u][b].zeros += push.zeros
						node[u][b].ones += push.ones
					} else {
						node[u][b].zeros += push.ones
						node[u][b].ones += push.zeros
					}
				}
				saturation[u]++
			}
		}
		if u != p {
			dfs(u, v)
		}
	}
}

func main() {
	n := readInt()
	node = make([][bits]value, n)
	edge = make([]*[bits]value, (n-1)*2)
	original = make([][bits]bool, n)
	for i := range node {
		a := readInt()
		for j := range node[i] {
			original[i][j] = a%2 == 0
			node[i][j] = value{
				zeros: 1 - a%2,
				ones:  a % 2,
			}
			a /= 2
		}
	}
	kick = make([]int, n)
	for i := range kick {
		kick[i] = -1
	}
	next = make([]int, (n-1)*2)
	dest = make([]int, (n-1)*2)
	adjacent = make([]int, n)
	saturation = make([]int, n)
	for i := 0; i < n-1; i++ {
		a, b := readInt()-1, readInt()-1
		adjacent[a]++
		adjacent[b]++

		// edge i * 2: a -> b
		next[i*2] = kick[a]
		dest[i*2] = b
		kick[a] = i * 2

		// edge i * 2 + 1: b -> a
		next[i*2+1] = kick[b]
		dest[i*2+1] = a
		kick[b] = i*2 + 1
	}

	dfs(0, -1)
	dfs(0, -1)

	var total int64
	for b := 0; b < bits; b++ {
		var single, double int
		for i := 0; i < n; i++ {
			if original[i][b] {
				double += node[i][b].ones
			} else {
				double += node[i][b].ones - 1
				single++
			}
		}
		total += (int64(1) << uint32(b)) * int64(single+double/2)
	}
	fmt.Println(total)
}

package main

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

var stdReader *bufio.Reader = bufio.NewReader(os.Stdin)

func readInt() (result int) {
	var negative, started bool
READER_LOOP:
	for {
		char, err := stdReader.ReadByte()
		switch {
		case err != nil:
			break READER_LOOP
		case char == '-':
			negative = true
		case unicode.IsDigit(rune(char)):
			started = true
			result = result*10 + int(char&0xf)
		case started:
			break READER_LOOP
		}
	}
	if negative {
		result = -result
	}
	return
}

const bits = 30

type value struct {
	zeros, ones int
}

var kick, next, dest []int

var adjacent, saturation []int

var original [][bits]bool

var node [][bits]value

var edge []*[bits]value

func maybePush(v, u, e int) {
	if edge[e] != nil {
		return
	}
	if adjacent[v] == saturation[v] {
		// substract and push
		edge[e] = new([bits]value)
		for b := 0; b < bits; b++ {
			push := node[v][b]
			if original[v][b] {
				push.zeros -= edge[e^1][b].zeros
				push.ones -= edge[e^1][b].ones
			} else {
				push.zeros -= edge[e^1][b].ones
				push.ones -= edge[e^1][b].zeros
			}
			edge[e][b] = push
			if original[u][b] {
				node[u][b].zeros += push.zeros
				node[u][b].ones += push.ones
			} else {
				node[u][b].zeros += push.ones
				node[u][b].ones += push.zeros
			}
		}
		saturation[u]++
	} else if adjacent[v] == saturation[v]+1 && edge[e^1] == nil {
		// push as is
		edge[e] = new([bits]value)
		for b := 0; b < bits; b++ {
			edge[e][b] = node[v][b]
			if original[u][b] {
				node[u][b].zeros += node[v][b].zeros
				node[u][b].ones += node[v][b].ones
			} else {
				node[u][b].zeros += node[v][b].ones
				node[u][b].ones += node[v][b].zeros
			}
		}
		saturation[u]++
	}
}

func dfs(v, p int) {
	var pe int
	for e := kick[v]; e >= 0; e = next[e] {
		u := dest[e]
		maybePush(v, u, e)
		if u == p {
			pe = e
		} else {
			dfs(u, v)
		}
	}
	if p >= 0 {
		maybePush(v, p, pe)
	}
}

func main() {
	n := readInt()
	node = make([][bits]value, n)
	edge = make([]*[bits]value, (n-1)*2)
	original = make([][bits]bool, n)
	for i := range node {
		a := readInt()
		for j := range node[i] {
			original[i][j] = a%2 == 0
			node[i][j] = value{
				zeros: 1 - a%2,
				ones:  a % 2,
			}
			a /= 2
		}
	}
	kick = make([]int, n)
	for i := range kick {
		kick[i] = -1
	}
	next = make([]int, (n-1)*2)
	dest = make([]int, (n-1)*2)
	adjacent = make([]int, n)
	saturation = make([]int, n)
	for i := 0; i < n-1; i++ {
		a, b := readInt()-1, readInt()-1
		adjacent[a]++
		adjacent[b]++

		// edge i * 2: a -> b
		next[i*2] = kick[a]
		dest[i*2] = b
		kick[a] = i * 2

		// edge i * 2 + 1: b -> a
		next[i*2+1] = kick[b]
		dest[i*2+1] = a
		kick[b] = i*2 + 1
	}

	dfs(0, -1)
	dfs(0, -1)

	for e := range edge {
		if edge[e] == nil {
			panic("Edge can't be nil")
		}
	}

	var total int64
	for b := 0; b < bits; b++ {
		var single, double int
		for i := 0; i < n; i++ {
			if original[i][b] {
				double += node[i][b].ones
			} else {
				double += node[i][b].ones - 1
				single++
			}
		}
		total += (int64(1) << uint32(b)) * int64(single+double/2)
	}
	fmt.Println(total)
}


Did you know you can download more RAM? There is a shop with $$$n$$$ different pieces of software that increase your RAM. The $$$i$$$-th RAM increasing software takes $$$a_i$$$ GB of memory to run (temporarily, once the program is done running, you get the RAM back), and gives you an additional $$$b_i$$$ GB of RAM (permanently). Each software can only be used once. Your PC currently has $$$k$$$ GB of RAM.Note that you can't use a RAM-increasing software if it takes more GB of RAM to use than what you currently have.Since RAM is the most important thing in the world, you wonder, what is the maximum possible amount of RAM achievable?

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains the integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$1 \le k \le 1000$$$). Then two lines follow, each containing $$$n$$$ integers describing the arrays $$$a$$$ and $$$b$$$ ($$$1 \le a_i, b_i \le 1000$$$).

For each test case, output a single line containing the largest amount of RAM you can achieve.

NoteIn the first test case, you only have enough RAM to run the third software initially, but that increases your RAM to $$$20$$$ GB, which allows you to use the first software, increasing your RAM to $$$29$$$ GB. The only software left needs $$$30$$$ GB of RAM, so you have to stop here.In the second test case, you can use the first, second, fourth and fifth software that need only $$$1$$$ GB of RAM per software to run to increase your RAM to $$$5$$$ GB, and then use the last remaining one to increase your RAM to $$$6$$$ GB.In the third test case, all the software need more than $$$1$$$ GB of RAM to run, so the amount of RAM you have stays at $$$1$$$ GB.

package main
import (
	"fmt"
	"sort"
)

type input struct {
	first int
	second int
}

func main(){
	var t int
	fmt.Scan(&t)
	for ; t>0;t-- {
		var n int
		var k int
		fmt.Scan(&n)
		fmt.Scan(&k)
		inp := make([]input, n)
		for i:=0;i<n;i++ {
			fmt.Scan(&inp[i].first)
		}
		for i:=0;i<n;i++ {
			fmt.Scan(&inp[i].second)
		}
		sort.SliceStable(inp, func(i, j int) bool {
			return inp[i].first < inp[j].first
		})
		for i:=0;i<n;i++ {
			if inp[i].first <= k {
				k += inp[i].second
			}
		}
		fmt.Println(k)
	}

}

package main

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

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

type pair struct {
	need int
	inc int
}

type pairs []pair
func (a pairs) Len() int           { return len(a) }
func (a pairs) Less(i, j int) bool { return a[i].need < a[j].need }
func (a pairs) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func main() {
	// STDOUT MUST BE FLUSHED MANUALLY!!!
	defer writer.Flush()

	t := 0
	scanf("%d\n", &t)
	for ti:=0; ti<t; ti++ {
		n := 0
		k := 0
		scanf("%d %d\n", &n, &k)
		//printf("n k %d %d\n", n, k)
		p := make(pairs, n)
		for i:=0; i<n; i++ {
			if i > 0 {
				scanf(" ")
			}
			scanf("%d", &p[i].need)
			//printf("i = %d p[i].need = %d\n", i, p[i].need)
		}
		scanf("\n%")
		for i:=0; i<n; i++ {
			if i > 0 {
				scanf(" ")
			}
			scanf("%d", &p[i].inc)
		}
		scanf("\n%")
		sort.Sort(p)
		ramNow := k
		for i:=0; i<n; i++ {
			//printf("%d %d %d\n", i, p[i].need, p[i].inc)
			if ramNow >= p[i].need {
				ramNow += p[i].inc
			}
		}
		printf("%d\n", ramNow)
	}
}

package main

import (
	"fmt"
	"sort"
)

type Pair struct {
	Key   int
	Value int
}

type Pairs []Pair

func (p Pairs) Len() int           { return len(p) }
func (p Pairs) Less(i, j int) bool { return p[i].Key < p[j].Key }
func (p Pairs) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

func main() {

	var t, n, k int

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

	for t > 0 {
		fmt.Scanf("%d %d\n", &n, &k)

		var a, b []int

		for i := 0; i < n; i++ {
			var a_i int
			fmt.Scanf("%d", &a_i)
			a = append(a, a_i)
		}

		fmt.Scanf("%d\n", nil)

		for i := 0; i < n; i++ {
			var b_i int
			fmt.Scanf("%d", &b_i)
			b = append(b, b_i)
		}

		fmt.Scanf("%d\n", nil)

		pairs := make(Pairs, len(a))

		for i := 0; i < len(a); i++ {
			p := Pair{
				Key:   a[i],
				Value: b[i],
			}
			pairs = append(pairs, p)
		}

		sort.Sort(pairs)

		for _, pair := range pairs {
			if k >= pair.Key {
				k += pair.Value
			}
		}

		fmt.Println(k)

		t--
	}
}

package main
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n,current int
		fmt.Fscan(in,&n,&current)
		cost:= make([]int,n)
		additional:= make([]int,n)
		for j:=0;j<n;j++{
			fmt.Fscan(in,&cost[j])
		}
		for j:=0;j<n;j++{
			fmt.Fscan(in,&additional[j])
		}
		for j:=0;j<len(cost);j++{
			if cost[j] <= current {
				current+= additional[j]
				cost = append(cost[:j],cost[j+1:]...)
				additional = append(additional[:j],additional[j+1:]...)
				j=-1
			}
		}
		fmt.Fprintln(out,current)
	}
}

package main

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

type Soft struct {
	needed int
	inc    int
}

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

	ss := make([]Soft, n)
	for i := range ss {
		fmt.Fscan(r, &ss[i].needed)
	}

	for i := range ss {
		fmt.Fscan(r, &ss[i].inc)
	}

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

	for _, s := range ss {
		if s.needed <= m {
			m += s.inc
		} else {
			fmt.Println(m)
			return
		}
	}
	fmt.Println(m)
}
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"
	"crypto/rand"
	"fmt"
	"math"
	"math/big"
	"os"
	"sort"
	"strconv"
)

func main() {

	reader := bufio.NewReader(os.Stdin)
	writter := bufio.NewWriter(os.Stdout)
	defer writter.Flush()

	t := 0
	fmt.Fscan(reader, &t)

	for t > 0 {
		n, k := 0, 0
		fmt.Fscan(reader, &n, &k)

		a := make([]int, n)
		for i := range a {
			fmt.Fscan(reader, &a[i])
		}

		b := make([]int, n)
		for i := range b {
			fmt.Fscan(reader, &b[i])
		}

		fmt.Fprintln(writter, solve(n, k, a, b))

		t--
	}

	// n, k := 5, 1
	// a := []int{1, 1, 5, 1, 1}
	// b := []int{1, 1, 1, 1, 1}

	// fmt.Println(solve(n, k, a, b))

}

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

	now := k

	for {
		maxV := 0
		maxP := -1

		for i := range a {
			if a[i] <= now {
				maxV = b[i]
				maxP = i
			}
		}

		if maxP == -1 {
			break
		}

		now += maxV
		a[maxP] = math.MaxInt

		//fmt.Println(now)
	}

	return now
}

func arraySum(array []int) int {
	sum := 0

	for i := range array {
		sum += array[i]
	}

	return sum
}

func numSum(num int) int {
	sum := 0

	for i := num; i > 0; i-- {
		sum += i
	}

	return sum
}

func strToint(str string) int {
	num, _ := strconv.Atoi(str)
	return num
}

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

	return b
}

func getFactor(num int) []int {
	result := []int{}

	for i := 1; i <= int(math.Sqrt(float64(num))); i++ {
		if num%i == 0 {
			result = append(result, i)
			result = append(result, num/i)
		}
	}

	return result
}

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

	return gcd(b%a, a)
}

func printMatrix(matrix [][]int) {

	for i := range matrix {

		for j := range matrix[i] {
			fmt.Printf("%d ", matrix[i][j])
		}

		fmt.Println()

	}

}

func checkPrime(num int) bool {

	if num < 2 {
		return false
	}

	for i := 2; i <= int(math.Sqrt(float64(num))); i++ {
		if num%i == 0 {
			return false
		}
	}

	return true
}

func getPower() {
	A := []int{}

	for i := 1; i <= 38; i++ {
		A = append(A, i)
	}

	for i := range A {
		target, _ := rand.Int(rand.Reader, big.NewInt(38))
		A[i], A[target.Int64()] = A[target.Int64()], A[i]
	}

	B, _ := rand.Int(rand.Reader, big.NewInt(8))

	result := A[:6]
	sort.Ints(result)
	result = append(result, int(B.Int64())+1)

	fmt.Println(result)
}

package main

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

type CF1629A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1629A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1629A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1629A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1629A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1629A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1629A
Date: 2/02/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1629/A
Problem: CF1629A
**/
func (in *CF1629A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, k := in.NextInt(), in.NextInt()
		m := make([][]int, n)
		for i := 0; i < n; i++ {
			m[i] = make([]int, 2)
			m[i][0] = in.NextInt()
		}
		for i := 0; i < n; i++ {
			m[i][1] = in.NextInt()
		}

		sort.Slice(m, func(i, j int) bool {
			if m[i][0]-m[j][0] == 0 {
				if m[i][1]-m[j][1] > 0 {
					return false
				}
				return true
			}
			if m[i][0]-m[j][0] > 0 {
				return false
			}
			return true
		})

		result := k
		for i := 0; i < n; i++ {
			if m[i][0] <= result {
				result += m[i][1]
			}
		}
		fmt.Println(result)
	}
}

func NewCF1629A(r *bufio.Reader) *CF1629A {
	return &CF1629A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1629A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

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

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

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
// 	redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n, k int
	scan(&n, &k)
	var a = make([][2]int, n)
	for i := range a {
		scan(&a[i][0])
	}
	for i := range a {
		scan(&a[i][1])
	}
	sort.Slice(a, func(i, j int) bool {
		return a[i][0] < a[j][0]
	})
	for i := range a {
		if a[i][0] <= k {
			k += a[i][1]
		}
	}
	printf("%v\n", k)
}

package main

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

type Pair struct {
	First int
	Second int
}

func main() {
	var (
		reader = bufio.NewReader(os.Stdin)
		t, n, k int
	)
	fmt.Fscan(reader, &t);
	for i := 0; i < t; i++ {
		fmt.Fscan(reader, &n, &k);
		a := []int{}
		b := []int{}
		for j := 0; j < n; j++ {
			var x int;
			fmt.Fscan(reader, &x);
			a = append(a, x);
		}
		for j := 0; j < n; j++ {
			var x int;
			fmt.Fscan(reader, &x);
			b = append(b, x);
		}
		c := []Pair{}
		for j := 0; j < n; j++ {
			c = append(c, Pair{a[j], b[j]});
		}
		sort.SliceStable(c, func(x, y int) bool {
			if c[x].First < c[y].First {
				return true;
			} else if c[x].First == c[y].First {
				if c[x].Second < c[y].Second {
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		})
		for i := 0; i < n; i++ {
			if k >= c[i].First {
				k += c[i].Second
			} else {
				break;
			}
		}
		fmt.Println(k);
	}
}

package main

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

type P struct {
	a, b int
}

var (
	reader  = bufio.NewReader(os.Stdin)
	t, n, k int
)

func solve(n, k int, p []P) int {
	sort.Slice(p, func(i, j int) bool {
		return p[i].a < p[j].a || (p[i].a == p[j].a && p[i].b < p[j].b)
	})
	x := k
	for i := range p {
		if p[i].a > x {
			break
		}
		x += p[i].b
	}
	return x
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &k)
		p := make([]P, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &p[i].a)
		}
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &p[i].b)
		}
		fmt.Println(solve(n, k, p))
	}
}

package main

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

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

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

func scan(a ...interface{})    { fmt.Fscan(reader, a...) }
func print(a ...interface{})   { fmt.Fprint(writer, a...) }
func println(a ...interface{}) { fmt.Fprintln(writer, a...) }

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

type Pair struct {
	a, b int
}

func customSort(arr []Pair) func(int, int) bool {
	return func(i, j int) bool {
		return arr[i].a < arr[j].a
	}
}

func main() {
	defer writer.Flush()
	var t, n, k int
	scan(&t)
	for i := 0; i < t; i++ {
		scan(&n, &k)
		arr := make([]Pair, n)
		for j := 0; j < n; j++ {
			scan(&arr[j].a)
		}
		for j := 0; j < n; j++ {
			scan(&arr[j].b)
		}
		sort.SliceStable(arr, customSort(arr))
		res := k
		for r := 0; r < n; r++ {
			if arr[r].a > res {
				break
			}
			res += arr[r].b
		}
		println(res)
	}
}

package main

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

type RAM struct {
	a, p int
}

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n, k := 0, 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n, &k)
		o := []RAM{}
		for j := 0; j < 2*n; j++ {
			p := 0
			fmt.Fscan(f, &p)
			if j >= n {
				o[j%n].p = p
			} else {
				o = append(o, RAM{a: p})
			}
		}
		sort.Slice(o, func(i, j int) bool {
			if o[i].a == o[j].a {
				return o[i].p > o[j].p
			}
			return o[i].a < o[j].a
		})
		for j := 0; j < n; j++ {
			if o[j].a <= k {
				k += o[j].p
			} else {
				break
			}
		}
		fmt.Fprintln(w, k)
	}
	w.Flush()
}

func main() {
	solve()
}

package main

import "fmt"

type ram struct {
	a []int
	b []int
	cap int
	count int
}

func main() {
	var testCases int
	fmt.Scan(&testCases)

	rams := make([]ram, testCases)

	for i := 0; i < testCases; i++ {
		var r ram
		fmt.Scan(&r.count, &r.cap)

		a := make([]int, r.count)
		for j := 0; j < r.count; j++ {
			fmt.Scan(&a[j])
		}

		b := make([]int, r.count)
		for j := 0; j < r.count; j++ {
			fmt.Scan(&b[j])
		}

		r.a = a
		r.b = b
		rams[i] = r
	}

	for _, r := range rams {
		r.sort()
		r.result()

		fmt.Println(r.cap)
	}
}

func (r *ram) result() {
	cap := r.cap
	for i := 0; i < len(r.a); i++ {
		if cap >= r.a[i] {
			cap += r.b[i]
		} else {
			r.cap = cap
			return
		}
	}
	r.cap = cap
}

func (r *ram) sort() {
	for i := 0; i < len(r.a) - 1; i++ {
		for j := i + 1; j < len(r.a); j++ {
			if r.a[i] > r.a[j] {
				r.a[i], r.a[j] = r.a[j], r.a[i]
				r.b[i], r.b[j] = r.b[j], r.b[i]
			}
		}
	}
}

package main

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

const mod = 998244353
const mx = 3000

type pair struct{
	low int
	ram int
}

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n,cur int
	fmt.Fscan(in,&n,&cur)
	a := make([]int,n)
	ps := make([]pair,n)
	for i := 0;i < n;i++{
		fmt.Fscan(in,&a[i])
	}
	for i := 0;i < n;i++{
		ps[i] = pair{
			low : a[i],
		}
		fmt.Fscan(in,&ps[i].ram)
	}
	sort.Slice(ps, func(i, j int) bool {
		return ps[i].low < ps[j].low
	})
	for _,p := range ps{
		if cur >= p.low{
			cur += p.ram
		}else{
			break
		}
	}
	fmt.Fprintln(out,cur)
}
func sum(a,b int64)int64{
	return (b + a) * (b - a + 1) / 2
}

func max(a,b int64)int64{
	if a > b{
		return a
	}
	return b
}
func min(a,b int64)int64{
	if a > b{
		return b
	}
	return a
}
func gcd(a,b int64)int64{
	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"
	"os"
	"sort"
	"strconv"
	"strings"
)

func fn(rr *bufio.Reader) {
	nk := readLineNumbs(rr)

	n, _ := strconv.Atoi(nk[0])
	k, _ := strconv.Atoi(nk[1])

	a := readLineNumbs(rr)
	b := readLineNumbs(rr)

	mp := make(map[int][]int)

	for i := 0; i < n; i++ {
		nb, _ := strconv.Atoi(b[i])
		na, _ := strconv.Atoi(a[i])
		dif := nb - na
		_, ok := mp[dif]
		if !ok {
			mp[dif] = []int{i}
		} else {
			mp[dif] = append(mp[dif], i)
		}
	}

	// debug("mp", mp)
	keys := make([]int, 0, len(mp))
	for k, _ := range mp {
		keys = append(keys, k)
	}
	sort.Sort(sort.Reverse(sort.IntSlice(keys)))

	//debug("keys", keys)
	for i := 0; i < n; i++ {
		for _, x := range keys {
			// debug("x", x)
			// debug("mp[x]", mp[x])
			if len(mp[x]) > 0 {
				for j := 0; j < len(mp[x]); j++ {
					pos := (mp[x])[j]
					cur, _ := strconv.Atoi(a[pos])
					if cur <= k {
						sum, _ := strconv.Atoi(b[pos])
						k += sum
						if len(mp[x]) == 1 {
							delete(mp, x)
						} else {
							mp[x] = append(mp[x][:j], mp[x][j+1:]...)
						}
					}
				}
			}
		}
	}
	fmt.Println(k)
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	t := readInt(reader)
	for i := 0; i < t; i++ {
		fn(reader)
	}
}

// Thanks to user: gealbermorales for the valuable guidance with processing input using Go
// https://codeforces.com/blog/entry/94245

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

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

func debug(name string, a interface{}) {
	fmt.Printf("%s = %v \n", name, a)
}

package main

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

var arr []pair

type pair struct {
	a int
	b int
}

func solve(n int, k int) int {
	sort.Slice(arr[:n], func(i, j int) bool {
		if arr[i].a < arr[j].a {
			return true
		}
		if arr[i].a > arr[j].a {
			return false
		}
		return arr[i].b > arr[j].b
	})
	for i := 0; i < n; i++ {
		prevk := k
		k -= arr[i].a
		if k < 0 {
			return prevk
		}
		k += arr[i].a + arr[i].b
	}
	return k
}

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

	var T int
	fmt.Fscan(in, &T)

	arr = make([]pair, 105)

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

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

		fmt.Fprintln(out, solve(n, k))
	}
	out.Flush()
}

// generated by 1e9y at 22-01-2022
// template version: 2022-01-11

package main

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

type Ram []int

type SortBy []Ram

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

// 0000.A
func solve(n, k int, ram SortBy) int {
	sum := k
	sort.Sort(ram)
	// Println(ram)
	for i := 0; i < n; i++ {
		r := ram[i]
		if r[0] > sum {
			break
		}
		sum += r[1]
	}
	return sum
}

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

	t := ReadInt(in)
	for i := 0; i < t; i++ {
		slice := ReadIntSlice(in)
		n, k := slice[0], slice[1]
		ram := make([]Ram, n)
		aa := ReadIntSlice(in)
		bb := ReadIntSlice(in)
		for j := 0; j < n; j++ {
			ram[j] = []int{aa[j], bb[j]}
		}
		Output(solve(n, k, ram))
	}
}

// helpers for convenient input reading

func ReadInt(in *bufio.Reader) int {
	line, _ := in.ReadString('\n')
	line = strings.Trim(line, "\n\r")
	n, _ := strconv.Atoi(line)
	return n
}

func ReadLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.Trim(line, "\n\r")
	return line
}

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

func ReadIntSlice(in *bufio.Reader) []int {
	parts := ReadLineParts(in)
	slice := make([]int, len(parts))
	for i, n := range parts {
		slice[i] = MustAtoi(n)
	}
	return slice
}

func ReadInt64Slice(in *bufio.Reader) []int64 {
	parts := ReadLineParts(in)
	slice := make([]int64, len(parts))
	for i, n := range parts {
		slice[i] = MustParseInt(n, 10, 64)
	}
	return slice
}

func ReadUint64Slice(in *bufio.Reader) []uint64 {
	parts := ReadLineParts(in)
	slice := make([]uint64, len(parts))
	for i, n := range parts {
		slice[i] = MustParseUint(n, 10, 64)
	}
	return slice
}

func MustAtoi(s string) int {
	v, _ := strconv.Atoi(s)
	return v
}

func MustParseInt(s string, base, bitSize int) int64 {
	v, _ := strconv.ParseInt(s, base, bitSize)
	return v
}

func MustParseUint(s string, base, bitSize int) uint64 {
	v, _ := strconv.ParseUint(s, base, bitSize)
	return v
}

func SkipLine(in *bufio.Reader) {
	_, _ = in.ReadString('\n')
}

// helper for convenient answer output

func Output(v interface{}) {
	switch v.(type) {
	case bool:
		if v == true {
			Println("YES")
		} else {
			Println("NO")
		}
	default:
		Println(v)
	}
}

package main

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

type building struct {
	id   int
	v, d int64
}

type ram struct {
	pos, req, bonus int
}

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

	var T int
	fmt.Fscan(in, &T)

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

		progs := make([]ram, n)
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)
			progs[i] = ram{i, v, 0}
		}

		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)
			progs[i].bonus = v
		}

		sort.SliceStable(progs, func(i, j int) bool {
			if progs[i].req == progs[j].req {
				return progs[i].bonus > progs[j].bonus
			}
			return progs[i].req < progs[j].req
		})

		total := k
		for _, p := range progs {
			if total < p.req {
				break
			}

			total += p.bonus
		}
		fmt.Fprintln(out, total)
	}
}

package main

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

/*

 */

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

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

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

		type pair struct {
			a int
			b int
		}

		pairs := make([]pair, n)

		for i := range pairs {
			scanf("%d", &pairs[i].a)
		}
		scanf("\n")

		for i := range pairs {
			scanf("%d", &pairs[i].b)
		}
		scanf("\n")

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

		for _, p := range pairs {
			if p.a <= k {
				k += p.b
			}
		}

		fmt.Fprintln(writer, k)
	}
}

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

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

func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }

func main() {
	defer out.Flush()

	var ntc int
	scan(&ntc)
	for t := 0; t < ntc; t++ {
		var n, k int
		scan(&n, &k)
		var a []int = make([]int, n)
		var b []int = make([]int, n)
		for i := 0; i < n; i++ {
			scan(&a[i])
		}
		for i := 0; i < n; i++ {
			scan(&b[i])
		}
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				if a[j] <= k {
					k += b[j]
					a[j] = 0
					b[j] = 0
				}
			}
		}
		printf("%d\n", k)
	}
}

package main
import (
	"fmt"
	"sort"
)

type input struct {
	first int
	second int
}

func main(){
	var t int
	fmt.Scan(&t)
	for ; t>0;t-- {
		var n int
		var k int
		fmt.Scanf("%d %d",&n, &k)
		inp := make([]input, n)
		for i:=0;i<n;i++ {
			fmt.Scan(&inp[i].first)
		}
		for i:=0;i<n;i++ {
			fmt.Scan(&inp[i].second)
		}
		sort.SliceStable(inp, func(i, j int) bool {
			return inp[i].first < inp[j].first
		})
		for i:=0;i<n;i++ {
			if inp[i].first <= k {
				k += inp[i].second
			}
		}
		fmt.Println(k)
	}

}

package main

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

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

type pair struct {
	need int
	inc int
}

type pairs []pair
func (a pairs) Len() int           { return len(a) }
func (a pairs) Less(i, j int) bool { return a[i].need < a[j].need || a[i].inc > a[j].inc }
func (a pairs) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func main() {
	// STDOUT MUST BE FLUSHED MANUALLY!!!
	defer writer.Flush()

	t := 0
	n := 0
	k := 0
	scanf("%d\n", &t)
	for ti:=0; ti<t; ti++ {
		scanf("%d %d\n", &n, &k)
		//printf("n k %d %d\n", n, k)
		p := make(pairs, n)
		for i:=0; i<n; i++ {
			if i > 0 {
				scanf(" ")
			}
			scanf("%d", &p[i].need)
			//printf("i = %d p[i].need = %d\n", i, p[i].need)
		}
		scanf("\n%")
		for i:=0; i<n; i++ {
			if i > 0 {
				scanf(" ")
			}
			scanf("%d", &p[i].inc)
		}
		scanf("\n%")
		sort.Sort(p)
		ramNow := k
		for i:=0; i<n; i++ {
			//printf("%d %d %d\n", i, p[i].need, p[i].inc)
			if ramNow >= p[i].need {
				ramNow += p[i].inc
			}
		}
		printf("%d\n", ramNow)
	}
}


You have a malfunctioning microwave in which you want to put some bananas. You have $$$n$$$ time-steps before the microwave stops working completely. At each time-step, it displays a new operation.Let $$$k$$$ be the number of bananas in the microwave currently. Initially, $$$k = 0$$$. In the $$$i$$$-th operation, you are given three parameters $$$t_i$$$, $$$x_i$$$, $$$y_i$$$ in the input. Based on the value of $$$t_i$$$, you must do one of the following:Type 1: ($$$t_i=1$$$, $$$x_i$$$, $$$y_i$$$) — pick an $$$a_i$$$, such that $$$0 \le a_i \le y_i$$$, and perform the following update $$$a_i$$$ times: $$$k:=\lceil (k + x_i) \rceil$$$.Type 2: ($$$t_i=2$$$, $$$x_i$$$, $$$y_i$$$) — pick an $$$a_i$$$, such that $$$0 \le a_i \le y_i$$$, and perform the following update $$$a_i$$$ times: $$$k:=\lceil (k \cdot x_i) \rceil$$$.Note that $$$x_i$$$ can be a fractional value. See input format for more details. Also, $$$\lceil x \rceil$$$ is the smallest integer $$$\ge x$$$.At the $$$i$$$-th time-step, you must apply the $$$i$$$-th operation exactly once.For each $$$j$$$ such that $$$1 \le j \le m$$$, output the earliest time-step at which you can create exactly $$$j$$$ bananas. If you cannot create exactly $$$j$$$ bananas, output $$$-1$$$.

The first line contains two space-separated integers $$$n$$$ $$$(1 \le n \le 200)$$$ and $$$m$$$ $$$(2 \le m \le 10^5)$$$. Then, $$$n$$$ lines follow, where the $$$i$$$-th line denotes the operation for the $$$i$$$-th timestep. Each such line contains three space-separated integers $$$t_i$$$, $$$x'_i$$$ and $$$y_i$$$ ($$$1 \le t_i \le 2$$$, $$$1\le y_i\le m$$$). Note that you are given $$$x'_i$$$, which is $$$10^5 \cdot x_i$$$. Thus, to obtain $$$x_i$$$, use the formula $$$x_i= \dfrac{x'_i} {10^5}$$$. For type 1 operations, $$$1 \le x'_i \le 10^5 \cdot m$$$, and for type 2 operations, $$$10^5 < x'_i \le 10^5 \cdot m$$$.

Print $$$m$$$ integers, where the $$$i$$$-th integer is the earliest time-step when you can obtain exactly $$$i$$$ bananas (or $$$-1$$$ if it is impossible).

NoteIn the first sample input, let us see how to create $$$16$$$ number of bananas in three timesteps. Initially, $$$k=0$$$.  In timestep 1, we choose $$$a_1=2$$$, so we apply the type 1 update — $$$k := \lceil(k+3)\rceil$$$ — two times. Hence, $$$k$$$ is now 6.  In timestep 2, we choose $$$a_2=0$$$, hence value of $$$k$$$ remains unchanged.  In timestep 3, we choose $$$a_3=1$$$, so we are applying the type 1 update $$$k:= \lceil(k+10)\rceil$$$ once. Hence, $$$k$$$ is now 16. It can be shown that $$$k=16$$$ cannot be reached in fewer than three timesteps with the given operations.In the second sample input, let us see how to create $$$17$$$ number of bananas in two timesteps. Initially, $$$k=0$$$.  In timestep 1, we choose $$$a_1=1$$$, so we apply the type 1 update — $$$k := \lceil(k+3.99999)\rceil$$$ — once. Hence, $$$k$$$ is now 4.  In timestep 2, we choose $$$a_2=1$$$, so we apply the type 2 update — $$$k := \lceil(k\cdot 4.12345)\rceil$$$ — once. Hence, $$$k$$$ is now 17. It can be shown that $$$k=17$$$ cannot be reached in fewer than two timesteps with the given operations.

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	const b int64 = 1e5
	const b1 = b - 1

	var n, t, y, m int
	var x int64
	Fscan(in, &n, &m)
	f := make([]int, m+1)
	for i := m; i > 0; i-- {
		f[i] = -1
	}
	for i := 1; i <= n; i++ {
		Fscan(in, &t, &x, &y)
		if t == 1 {
			x := int((x + b1) / b)
			for j := m; j >= 0; j-- {
				if f[j] >= 0 {
					for a, k := y, j; a > 0; a-- {
						k += x
						if k > m || f[k] >= 0 {
							break
						}
						f[k] = i
					}
				}
			}
		} else {
			for j := m; j >= 0; j-- {
				if f[j] >= 0 {
					for a, k := y, int64(j); a > 0; a-- {
						k = (k*x + b1) / b
						if k > int64(m) || f[k] >= 0 {
							break
						}
						f[k] = i
					}
				}
			}
		}
	}
	for _, v := range f[1:] {
		Fprint(out, v, " ")
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	const b int64 = 1e5
	const b1 = b - 1

	var n, t, y, m int
	var x int64
	Fscan(in, &n, &m)
	f := make([]int, m+1)
	for i := m; i > 0; i-- {
		f[i] = -1
	}
	for i := 1; i <= n; i++ {
		Fscan(in, &t, &x, &y)
		if t == 1 {
			x := int((x + b1) / b)
			for j := m; j >= 0; j-- {
				if f[j] >= 0 {
					for a, k := y, j; a > 0; a-- {
						k += x
						if k > m || f[k] >= 0 {
							break
						}
						f[k] = i
					}
				}
			}
		} else {
			for j := m; j >= 0; j-- {
				if f[j] >= 0 {
					for a, k := y, int64(j); a > 0; a-- {
						k = (k*x + b1) / b
						if k > int64(m) || f[k] >= 0 {
							break
						}
						f[k] = i
					}
				}
			}
		}
	}
	for _, v := range f[1:] {
		Print(v, " ")
	}
}

package main
import ."fmt"

// github.com/EndlessCheng/codeforces-go
func main() {
	var n, t, y int
	var m, x int64
	Scan(&n, &m)
	f := make([]int, m+1)
	for i := m; i > 0; i-- {
		f[i] = -1
	}
	for i := 1; i <= n; i++ {
		Scan(&t, &x, &y)
		for j := m; j >= 0; j-- {
			if f[j] >= 0 {
				for a, k := 0, j; a < y; a++ {
					if t == 1 {
						k += (x + 1e5 - 1) / 1e5
					} else {
						k = (k*x + 1e5 - 1) / 1e5
					}
					if k > m || f[k] >= 0 {
						break
					}
					f[k] = i
				}
			}
		}
	}
	for _, v := range f[1:] {
		Print(v, " ")
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func run(in io.Reader, out io.Writer) {
	var n, t, y int
	var m, x int64
	Fscan(in, &n, &m)
	f := make([]int, m+1)
	for i := m; i > 0; i-- {
		f[i] = -1
	}
	for i := 1; i <= n; i++ {
		Fscan(in, &t, &x, &y)
		for j := m; j >= 0; j-- {
			if f[j] >= 0 {
				for a, k := 0, j; a < y; a++ {
					if t == 1 {
						k += (x + 1e5 - 1) / 1e5
					} else {
						k = (k*x + 1e5 - 1) / 1e5
					}
					if k > m || f[k] >= 0 {
						break
					}
					f[k] = i
				}
			}
		}
	}
	for _, v := range f[1:] {
		Fprint(out, v, " ")
	}
}

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


Yura has been walking for some time already and is planning to return home. He needs to get home as fast as possible. To do this, Yura can use the instant-movement locations around the city.Let's represent the city as an area of $$$n \times n$$$ square blocks. Yura needs to move from the block with coordinates $$$(s_x,s_y)$$$ to the block with coordinates $$$(f_x,f_y)$$$. In one minute Yura can move to any neighboring by side block; in other words, he can move in four directions. Also, there are $$$m$$$ instant-movement locations in the city. Their coordinates are known to you and Yura. Yura can move to an instant-movement location in no time if he is located in a block with the same coordinate $$$x$$$ or with the same coordinate $$$y$$$ as the location.Help Yura to find the smallest time needed to get home.

The first line contains two integers $$$n$$$ and $$$m$$$ — the size of the city and the number of instant-movement locations ($$$1 \le n \le 10^9$$$, $$$0 \le m \le 10^5$$$). The next line contains four integers $$$s_x$$$ $$$s_y$$$ $$$f_x$$$ $$$f_y$$$ — the coordinates of Yura's initial position and the coordinates of his home ($$$ 1 \le s_x, s_y, f_x, f_y \le n$$$). Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ $$$y_i$$$ — coordinates of the $$$i$$$-th instant-movement location ($$$1 \le x_i, y_i \le n$$$).

In the only line print the minimum time required to get home.

NoteIn the first example Yura needs to reach $$$(5, 5)$$$ from $$$(1, 1)$$$. He can do that in $$$5$$$ minutes by first using the second instant-movement location (because its $$$y$$$ coordinate is equal to Yura's $$$y$$$ coordinate), and then walking $$$(4, 1) \to (4, 2) \to (4, 3) \to (5, 3) \to (5, 4) \to (5, 5)$$$.

package main

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

// github.com/EndlessCheng/codeforces-go
type vd struct{ v, d int }
type hp []vd

func (h hp) Len() int              { return len(h) }
func (h hp) Less(i, j int) bool    { return h[i].d < h[j].d }
func (h hp) Swap(i, j int)         { h[i], h[j] = h[j], h[i] }
func (h *hp) Push(v interface{})   { *h = append(*h, v.(vd)) }
func (h *hp) Pop() (v interface{}) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return }
func (h *hp) push(v vd)            { heap.Push(h, v) }
func (h *hp) pop() vd              { return heap.Pop(h).(vd) }

func CF1422D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}
	abs := func(x int) int {
		if x < 0 {
			return -x
		}
		return x
	}

	var n, sx, sy, tx, ty, x, y int
	Fscan(in, &n, &n, &sx, &sy, &tx, &ty)
	dist := make([]int, n)
	end := make([]int, n)
	q := make(hp, n)
	type pt struct{ x, y, i int }
	ps := make([]pt, n)
	qs := make([]pt, n)
	for i := range dist {
		Fscan(in, &x, &y)
		dist[i] = min(abs(sx-x), abs(sy-y))
		end[i] = abs(x-tx) + abs(y-ty)
		q[i] = vd{i, dist[i]}
		ps[i] = pt{x, y, i}
		qs[i] = pt{y, x, i}
	}

	type nb struct{ to, wt int }
	g := make([][]nb, n)
	for _, ps := range [][]pt{ps, qs} {
		sort.Slice(ps, func(i, j int) bool { a, b := ps[i], ps[j]; return a.x < b.x || a.x == b.x && a.y < b.y })
		for i := 1; i < n; i++ {
			p, q := ps[i-1], ps[i]
			v, w, d := p.i, q.i, min(q.x-p.x, abs(q.y-p.y))
			g[v] = append(g[v], nb{w, d})
			g[w] = append(g[w], nb{v, d})
		}
	}

	heap.Init(&q)
	for len(q) > 0 {
		p := q.pop()
		v := p.v
		if dist[v] < p.d {
			continue
		}
		for _, e := range g[v] {
			w := e.to
			if newD := dist[v] + e.wt; newD < dist[w] {
				dist[w] = newD
				q.push(vd{w, newD})
			}
		}
	}
	ans := abs(sx-tx) + abs(sy-ty)
	for i, d := range dist {
		ans = min(ans, d+end[i])
	}
	Fprint(out, ans)
}

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


Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents.In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n - i + 1)-th. He does this while i ≤ n - i + 1.After performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday — restore the initial order of the cubes using information of their current location.

The first line contains single integer n (1 ≤ n ≤ 2·105) — the number of cubes. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109), where ai is the number written on the i-th cube after Dima has changed their order.

Print n integers, separated by spaces — the numbers written on the cubes in their initial order. It can be shown that the answer is unique.

NoteConsider the first sample.  At the begining row was [2, 3, 9, 6, 7, 1, 4].  After first operation row was [4, 1, 7, 6, 9, 3, 2].  After second operation row was [4, 3, 9, 6, 7, 1, 2].  After third operation row was [4, 3, 7, 6, 9, 1, 2].  At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4].

package main

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

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

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

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

type Int int64

func main() {
	defer w.Flush()
	var n Int
	for I(&n) == nil {
		solve(n)
	}
}

func solve(n Int) {
	in := make([]Int, n+1)
	for i := Int(1); i <= n; i++ {
		I(&in[i])
	}
	for i := Int(1); i <= n; i++ {
		if i > n-i+1 {
			break
		}
		if i%2 == 1 {
			in[i], in[n-i+1] = in[n-i+1], in[i]
		}
	}
	for i := Int(1); i <= n; i++ {
		o := " "
		if i == n {
			o = "\n"
		}
		O(in[i], o)
	}
}

package main

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

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

	array := make([]int, n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; scanner.Scan(); i++ {
		a, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		array[i] = a
	}

	for i := 0; i < n/2; i++ {
		if i%2 == 0 {
			j := n - i - 1
			array[i], array[j] = array[j], array[i]
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", array[i])
	}
	fmt.Println()
}

package main

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

func solve() {
	n := readInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = readInt()
	}
	for i := 0; i < n/2; i++ {
		if i%2 == 0 {
			a[i], a[n-i-1] = a[n-i-1], a[i]
		}
	}

	for i := 0; i < n; i++ {
		if i > 0 {
			fmt.Printf(" %d", a[i])
		} else {
			fmt.Printf("%d", a[i])
		}
	}
	fmt.Println()
}

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 max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

// IO

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

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

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

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

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

package main

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

func solve() {
	n := readInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = readInt()
	}
	for i := 0; i < n/2; i++ {
		if i%2 == 0 {
			a[i], a[n-i-1] = a[n-i-1], a[i]
		}
	}

	var buffer bytes.Buffer
	for i := 0; i < n; i++ {
		if i > 0 {
			buffer.WriteString(" ")
		}
		buffer.WriteString(strconv.Itoa(a[i]))
	}
	fmt.Println(buffer.String())
}

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 max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

// IO

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

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

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

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

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

package main

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

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

	for i := 0; i < N / 2; i++ {
		if (i % 2 == 0) {
			A[i], A[N - i - 1] = A[N - i - 1], A[i]
		}
	}

	for i := 0; i < N; i++ {
		io.Printf("%d ", A[i]);
	}
	io.Println("");
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

package main

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

func main() {
	sc := NewScanner()
	n := sc.NextInt()
	A := sc.NextIntArray()

	for i := 0; i < n / 2; i++ {
		if i % 2 == 0 {
			A[i], A[n - i - 1] = A[n - i - 1], A[i]
		}
	}
	PrintI(A...)

}
func PrintI(args ...int) {
	fmt.Println(strings.Join(mapToString(args), " "))
}
func mapToString(arr []int) []string {
	ret := make([]string, len(arr))
	for i := 0; i < len(arr); i++ {
		ret[i] = strconv.Itoa(arr[i])
	}
	return ret
}

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

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

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

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

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

	return mp
}

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

package main

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

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

	fmt.Fscanln(reader, &n)
	a := make([]int, n)

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

	for i := 0; i < n/2; i += 2 {
		a[i], a[n-i-1] = a[n-i-1], a[i]
	}

	for i := 0; i < n; i++ {
		fmt.Print(a[i], " ")
	}
}

package main

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

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

	fmt.Fscanln(reader, &n)
	a := make([]int, n)

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

	for i := 0; i < n; i++ {
		if n%2 == 0 && i >= n/2 {
			if i%2 == 1 {
				fmt.Print(a[n-i-1], " ")
			} else {
				fmt.Print(a[i], " ")
			}
		} else {
			if i%2 == 0 {
				fmt.Print(a[n-i-1], " ")
			} else {
				fmt.Print(a[i], " ")
			}
		}
	}
}