A permutation of length $$$n$$$ is an array $$$p=[p_1,p_2,\dots, p_n]$$$ which contains every integer from $$$1$$$ to $$$n$$$ (inclusive) exactly once. For example, $$$p=[4, 2, 6, 5, 3, 1]$$$ is a permutation of length $$$6$$$.You are given three integers $$$n$$$, $$$a$$$ and $$$b$$$, where $$$n$$$ is an even number. Print any permutation of length $$$n$$$ that the minimum among all its elements of the left half equals $$$a$$$ and the maximum among all its elements of the right half equals $$$b$$$. Print -1 if no such permutation exists.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$), the number of test cases in the test. The following $$$t$$$ lines contain test case descriptions. Each test case description contains three integers $$$n$$$, $$$a$$$, $$$b$$$ ($$$2 \le n \le 100$$$; $$$1 \le a,b \le n$$$; $$$a \ne b$$$), where $$$n$$$ is an even number (i.e. $$$n \bmod 2 = 0$$$).

For each test case, print a single line containing any suitable permutation. Print -1 no such permutation exists. If there are multiple answers, print any of them.

package main

import "fmt"

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

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

func main(){
	var N_INPUTS, turn int
	var left, right []int

	fmt.Scanf("%d\n", &N_INPUTS)
	var inputs [][3]int
	var n, a, b int

	for turn=0; turn<N_INPUTS; turn++{
		fmt.Scanf("%d %d %d\n", &n, &a, &b)
		inputs = append(inputs, [3]int{n, a, b})
	}

	for _, inp := range inputs{
		n, a, b = inp[0], inp[1], inp[2]
		left = []int{}
		right = []int{}
		in_arr := make(map[int]int)
		for num:=1; num <= n; num++{
			if num < min(a,b) && len(right) < n/2{
				right = append(right, num)
				in_arr[num] = 1
			} else if num > max(a,b) && len(left) < n/2{
				left = append(left, num)
				in_arr[num] = -1
			} else{
				in_arr[num] = 0
			}
		}
		for num:=1; num<=n; num++{
			if num >= a && num != b && in_arr[num] == 0{
				left = append(left, num)
				in_arr[num] = -1
				if len(left) == n/2{
					break
				}
			}
		}
		for num:=1; num<=n; num++{
			if num <=b && num != a && in_arr[num] == 0{
				right = append(right, num)
				in_arr[num] = 1
				if len(right) == n/2{
					break
				}
			}
		}
		if len(left) < n/2 || len(right) < n/2 {
			fmt.Printf("-1")
		} else{
			for _, num := range left{
				fmt.Printf("%d ", num)
			}
			for _, num := range right{
				fmt.Printf("%d ", num)
			}
		}
		fmt.Println()
	}
}

package main

import "fmt"

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

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

func main(){
	var N_INPUTS, turn int

	fmt.Scanf("%d\n", &N_INPUTS)
	var inputs [][3]int
	var n, a, b int
	for turn=0; turn<N_INPUTS; turn++{
		fmt.Scanf("%d %d %d\n", &n, &a, &b)
		inputs = append(inputs, [3]int{n, a, b})
	}
	for _, inp := range inputs{
		n, a, b = inp[0], inp[1], inp[2]
		var left []int
		var right []int
		in_arr := make(map[int]string)
		for num:=1; num <= n; num++{
			if num < min(a,b) && len(right) < n/2{
				right = append(right, num)
				in_arr[num] = "r"
			} else if num > max(a,b) && len(left) < n/2{
				left = append(left, num)
				in_arr[num] = "l"
			} else{
				in_arr[num] = "0"
			}
		}
		for num:=1; num<=n; num++{
			if num >= a && num != b && in_arr[num] == "0"{
				left = append(left, num)
				in_arr[num] = "l"
				if len(left) == n/2{
					break
				}
			}
		}
		for num:=1; num<=n; num++{
			if num <=b && num != a && in_arr[num] == "0"{
				right = append(right, num)
				in_arr[num] = "r"
				if len(right) == n/2{
					break
				}
			}
		}
		if len(left) < n/2 || len(right) < n/2 {
			fmt.Printf("-1")
		} else{
			for _, num := range left{
				fmt.Printf("%d ", num)
			}
			for _, num := range right{
				fmt.Printf("%d ", num)
			}
		}
		fmt.Println()
	}
}

package main

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

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

func (in *CF1612B) 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 *CF1612B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1612B
Date: 10/12/21
User: epradere
URL: https://codeforces.com/problemset/problem/1612/B
Problem: CF1612B
**/
func (in *CF1612B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, a, b := in.NextInt(), in.NextInt(), in.NextInt()
		if (a <= n/2 && b <= n/2) || (a > n/2 && b > n/2) {
			fmt.Println(-1)
		} else {
			arr := make([]int, n)
			arr[0] = a
			ans := true
			for i, j := n, 1; i >= 1; i-- {
				if i != a && i != b {
					arr[j] = i
					j++
					if i > n/2 && i <= a {
						ans = false
					} else if i <= n/2 && i >= b {
						ans = false
					}
				}
			}
			var sb strings.Builder
			arr[n-1] = b
			if ans {
				for i := 0; i < n; i++ {
					sb.WriteString(strconv.Itoa(arr[i]))
					sb.WriteString(" ")
				}
			} else {
				sb.WriteString("-1")
			}
			fmt.Println(sb.String())
		}
	}
}

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

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

package main

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

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

func (in *CF1612B) 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 *CF1612B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1612B
Date: 10/12/21
User: epradere
URL: https://codeforces.com/problemset/problem/1612/B
Problem: CF1612B
**/
func (in *CF1612B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, a, b := in.NextInt(), in.NextInt(), in.NextInt()
		if (a <= n/2 && b <= n/2) || (a > n/2 && b > n/2) {
			fmt.Println(-1)
		} else {
			arr := make([]int, n)
			arr[0] = a
			ans := true
			for i, j := n, 1; i >= 1; i-- {
				if i != a && i != b {
					arr[j] = i
					j++
					if i > n/2 && i <= a {
						ans = false
					} else if i <= n/2 && i >= b {
						ans = false
					}
				}
			}
			arr[n-1] = b
			if ans {
				for i := 0; i < n; i++ {
					fmt.Print(arr[i], " ")
				}
			} else {
				fmt.Print(-1)
			}
			fmt.Println()
		}
	}
}

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

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

package main

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

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

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

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

		min, max := (n/2)+1, n/2
		var swapped bool
		if a < min || b > max {
			arr[n-b], arr[n-a] = a, b
			swapped = true
		}

		// fmt.Println(min, max)
		if (a <= max && b <= max) || (a >= min && b >= min) || (!swapped && b < max) || (!swapped && a > min) {
			fmt.Fprintln(out, -1)
		} else {
			for i := 0; i < n; i++ {
				fmt.Fprintf(out, "%d ", arr[i])
			}
			fmt.Fprintln(out)
		}
	}
}

package main

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

/*
2
8 2 7
6 1 5
---
8 2 3 4 1 7 6 5
6 1 2 3 5 4
======
1
4 2 4
---
2 3 1 4
*/

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

	bld := strings.Builder{}

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

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

		nums := make([]int, n)

		nums[0] = a
		nums[n-1] = b

		num := n

		for i := 1; i < n-1; i++ {
			for num == a || num == b {
				num--
			}

			nums[i] = num

			num--
		}

		leftMin := maxInt
		for i := 0; i < n/2; i++ {
			leftMin = min(leftMin, nums[i])
		}

		rightMax := minInt
		for i := n / 2; i < n; i++ {
			rightMax = max(rightMax, nums[i])
		}

		if leftMin != a || rightMax != b {
			fmt.Fprintln(&bld, -1)
		} else {
			for _, num := range nums {
				fmt.Fprintf(&bld, "%d ", num)
			}
			fmt.Fprintln(&bld)
		}
	}

	printf("%s", bld.String())
}

const maxUint = ^uint(0)

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

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

//goland:noinspection GoUnusedType
type pair struct {
	x int64
	y int64
}

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

//goland:noinspection GoUnusedFunction
func printf(f string, a ...interface{}) {
	if _, err := fmt.Fprintf(writer, f, a...); err != nil {
		panic(err)
	}
}

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(x, y int) int {
	if x > y {
		return x
	}
	return y
}

//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 {
	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"
	"math"
	"os"
	"strings"
)

/*
2
8 2 7
6 1 5
---
8 2 3 4 1 7 6 5
6 1 2 3 5 4
======
1
4 2 4
---
2 3 1 4
*/

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

	bld := strings.Builder{}

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

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

		nums := make([]int, n)

		nums[0] = a
		nums[n-1] = b

		num := n

		for i := 1; i < n-1; i++ {
			for num == a || num == b {
				num--
			}

			nums[i] = num

			num--
		}

		valid := true
		if len(nums) != n {
			valid = false
		} else {
			leftMin := maxInt
			for i := 0; i < n/2; i++ {
				leftMin = min(leftMin, nums[i])
			}

			rightMax := minInt
			for i := n / 2; i < n; i++ {
				rightMax = max(rightMax, nums[i])
			}

			if leftMin != a || rightMax != b {
				valid = false
			}
		}

		if valid {
			for _, num := range nums {
				fmt.Fprintf(&bld, "%d ", num)
			}
			fmt.Fprintln(&bld)
		} else {
			fmt.Fprintln(&bld, -1)
		}
	}

	printf("%s", bld.String())
}

const maxUint = ^uint(0)

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

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

//goland:noinspection GoUnusedType
type pair struct {
	x int64
	y int64
}

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

//goland:noinspection GoUnusedFunction
func printf(f string, a ...interface{}) {
	if _, err := fmt.Fprintf(writer, f, a...); err != nil {
		panic(err)
	}
}

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(x, y int) int {
	if x > y {
		return x
	}
	return y
}

//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 {
	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 (
	"fmt"
)

func solve() {
	var n, a, b int16
	fmt.Scan(&n, &a, &b)
	v := make([]int16, n+1)
	v[1] = a
	i, j := int16(2), n
	for ; i <= n/2; i++ {
		if j == a || j <= 0 {
			fmt.Println(-1)
			return
		} else if j == b {
			j--
			i--
			continue
		}
		v[i] = j
		j--
	}
	v[n] = b
	if j >= b {
		j = b - 1
	}
	for i = n - 1; i >= n/2+1; i-- {
		if j <= 0 {
			fmt.Println(-1)
			return
		} else if j == a {
			j--
			i++
			continue
		}
		v[i] = j
		j--
	}
	for i, x := range v {
		if i > 0 {
			fmt.Printf("%v ", x)
		}
	}
	fmt.Println()

}

func main() {

	var t int16
	fmt.Scan(&t)
	for tc := int16(0); tc < t; tc++ {
		solve()
	}
}

package main

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

var r = bufio.NewReader(os.Stdin)

func solve(){
	var n, a, b int
	fmt.Fscan(r, &n, &a, &b)
	arr := make([]int, 0, n)

	for i := 0; i < n; i++ {
		if n - i != a && n - i != b {
			arr = append(arr, n - i)
		}
	}
	arr = append(arr, b)

	arr = append(arr, 0)
	copy(arr[1:], arr[0:])
	arr[0] = a

	for i := 0; i < n; i++ {
		if i < n/2 && arr[i] < a || i >= n/2 && arr[i] > b {
			fmt.Println(-1)
			return
		}
	}

	for _, v := range arr {
		fmt.Print(v, " ")
	}

	fmt.Println()

}

func main(){
	var t int
	fmt.Fscan(r, &t)
	for i := 0; i < t; i++ {
		solve()
	}
}

package main

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

func min(a ...int32) int32 {
	ret := a[0]
	for _, v := range a {
		if v < ret {
			ret = v
		}
	}
	return ret
}

func max(a ...int32) int32 {
	ret := a[0]
	for _, v := range a {
		if v > ret {
			ret = v
		}
	}
	return ret
}

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

	n := mustReadInt(r)
	for i := int64(0); i < n; i++ {
		nums := mustReadNNumbs(r, 3)
		n, a, b := nums[0], nums[1], nums[2]
		base := make([]int32, n)
		base[0] = int32(a)
		base[len(base)-1] = int32(b)
		if a > n/2 || b < n/2 || a == b {
			if !(a == n/2+1 && b == n/2) {
				fmt.Println(-1)
				continue
			}
		}

		last := int32(len(base))
		for i := 1; i < len(base)/2; i++ {
			if last == int32(b) {
				last--
			}
			base[i] = last
			last--
		}
		if last < int32(a) {
			fmt.Println(-1)
			continue
		}
		last = 1
		for i := len(base) / 2; i < len(base)-1; i++ {
			if last == int32(a) {
				last++
			}
			base[i] = last
			last++
		}
		if last > int32(b) {
			fmt.Println(-1)
			continue
		}
		for _, v := range base {
			fmt.Printf("%d ", v)
		}
		fmt.Printf("\n")
	}
}

func mustReadNNumbs(r *bufio.Reader, n int64) []int64 {
	nums := make([]int64, n)
	var err error
	for i, s := range strings.Split(mustReadLine(r), " ") {
		nums[i], err = strconv.ParseInt(s, 10, 64)
		checkError(err)
	}
	return nums
}

func mustReadInt(r *bufio.Reader) int64 {
	numb, err := strconv.ParseInt(mustReadLine(r), 10, 64)
	checkError(err)
	return numb
}

func mustReadLine(r *bufio.Reader) string {
	s, _, err := r.ReadLine()
	checkError(err)
	return string(s)
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

package main

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

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

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    defer output.Flush()
    //solve()
    //return
	var t int64
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
}

func solve() {
    var n, a, b int
    fmt.Fscan(input, &n, &a, &b)
    half := n/2

    ans := make([]int, 0)
    mp := make(map[int]bool)
    ans = append(ans, a)
    mp[a] = true
    mp[b] = true

    cur := n
    for i := 0; i < half - 1; i++ {
        for mp[cur] { cur-- }
        if cur < a {
            fmt.Fprintln(output, -1)
            return
        }
        ans = append(ans, cur)
        mp[cur] = true
    }

    ans = append(ans, b)
    for i := 0; i < half - 1; i++ {
        for mp[b] { b-- }
        if b < 1 {
            fmt.Fprintln(output, -1)
            return
        }
        ans = append(ans, b)
        mp[b] = true
    }

    for i := 0; i < len(ans); i++ {
        fmt.Fprint(output, ans[i], " ")
    }
    fmt.Fprintln(output)
}

package main

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

var n, a, b int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

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

		fmt.Fscan(r, &n, &a, &b)

		solve()

		fmt.Println()
	}
}

type pair struct {
	v, id int
}

type ByV []*pair

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

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

func solve() {
	if b < n/2 {
		fmt.Print(-1)
		return
	}
	if a > n/2 + 1 {
		fmt.Print(-1)
		return
	}
	if (a <= n/2 && b <= n/2) || (a >= n/2 + 1 && b >= n/2 + 1) {
		fmt.Print(-1)
		return
	}

	if a <= n/2 && b >= n/2 {
		for j := n/2 + 1; j <= n; j++ {
			if j == b {
				continue
			}
			fmt.Print(j)
			fmt.Print(" ")
		}
		fmt.Print(a)
		fmt.Print(" ")
		fmt.Print(b)
		fmt.Print(" ")

		for i := 1; i <= n/2; i++ {
			if i == a {
				continue
			}
			fmt.Print(i)
			fmt.Print(" ")
		}

	} else {
		for j := n/2 + 1; j <= n; j++ {
			fmt.Print(j)
			fmt.Print(" ")
		}
		for j := 1; j <= n/2; j++ {
			fmt.Print(j)
			fmt.Print(" ")
		}
	}

	//4 . . 5 . .

	//1 2 3 4 5 6  [4, 3]

}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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

package main

import "fmt"

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

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

func main(){
	var N_INPUTS, turn int

	fmt.Scanf("%d\n", &N_INPUTS)
	var inputs [][3]int
	var n, a, b int
	for turn=0; turn<N_INPUTS; turn++{
		fmt.Scanf("%d %d %d\n", &n, &a, &b)
		inputs = append(inputs, [3]int{n, a, b})
	}
	for _, inp := range inputs{
		n, a, b = inp[0], inp[1], inp[2]
		var left []int
		var right []int
		in_arr := make(map[int]string)
		for num:=1; num <= n; num++{
			if num < min(a,b) && len(right) < n/2{
				right = append(right, num)
				in_arr[num] = "r"
			} else if num > max(a,b) && len(left) < n/2{
				left = append(left, num)
				in_arr[num] = "l"
			} else{
				in_arr[num] = "0"
			}
		}
		for num:=1; num<=n; num++{
			if num >= a && in_arr[num] == "0"{
				left = append(left, num)
				in_arr[num] = "l"
				if len(left) == n/2{
					break
				}
			}
		}
		for num:=1; num<=n; num++{
			if num <=b && in_arr[num] == "0"{
				right = append(right, num)
				in_arr[num] = "r"
				if len(right) == n/2{
					break
				}
			}
		}
		if len(left) < n/2 || len(right) < n/2 {
			fmt.Printf("-1")
		} else{
			for _, num := range left{
				fmt.Printf("%d ", num)
			}
			for _, num := range right{
				fmt.Printf("%d ", num)
			}
		}
		fmt.Println()
	}
}

package main

import "fmt"

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

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

func main(){
	var N_INPUTS, turn int

	fmt.Scanf("%d\n", &N_INPUTS)
	var inputs [][3]int
	var n, a, b int
	for turn=0; turn<N_INPUTS; turn++{
		fmt.Scanf("%d %d %d\n", &n, &a, &b)
		inputs = append(inputs, [3]int{n, a, b})
	}
	for i, inp := range inputs{
		n, a, b = inp[0], inp[1], inp[2]
		var left []int
		var right []int
		in_arr := make(map[int]string)
		for num:=1; num <= n; num++{
			if num < min(a,b) && len(right) < n/2{
				right = append(right, num)
				in_arr[num] = "r"
			} else if num > max(a,b) && len(left) < n/2{
				left = append(left, num)
				in_arr[num] = "l"
			} else{
				in_arr[num] = "0"
			}
		}
		for num:=1; num<=n; num++{
			if num >= a && in_arr[num] == "0"{
				left = append(left, num)
				in_arr[num] = "l"
				if len(left) == n/2{
					break
				}
			}
		}
		for num:=1; num<=n; num++{
			if num <=b && in_arr[num] == "0"{
				right = append(right, num)
				in_arr[num] = "r"
				if len(right) == n/2{
					break
				}
			}
		}
		if len(left) < n/2 || len(right) < n/2 {
			fmt.Printf("-1")
		} else{
			for _, num := range left{
				fmt.Printf("%d ", num)
			}
			for _, num := range right{
				fmt.Printf("%d ", num)
			}
		}
		if i < N_INPUTS-1{
			fmt.Println()
		}
	}
}

package main

import "fmt"

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

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

func main(){
	var N_INPUTS, turn int

	fmt.Scanf("%d\n", &N_INPUTS)
	var inputs [][3]int
	var n, a, b int
	for turn=0; turn<N_INPUTS; turn++{
		fmt.Scanf("%d %d %d\n", &n, &a, &b)
		inputs = append(inputs, [3]int{n, a, b})
	}
	for i, inp := range inputs{
		n, a, b = inp[0], inp[1], inp[2]
		var left []int
		var right []int
		in_arr := make(map[int]string)
		for num:=1; num <= n; num++{
			if num < min(a,b) && len(right) < n/2{
				right = append(right, num)
				in_arr[num] = "r"
			} else if num > max(a,b) && len(left) < n/2{
				left = append(left, num)
				in_arr[num] = "l"
			} else{
				in_arr[num] = "0"
			}
		}
		for num:=1; num<=n; num++{
			if num >= a && in_arr[num] == "0"{
				left = append(left, num)
				in_arr[num] = "l"
				if len(left) == n/2{
					break
				}
			}
		}
		for num:=1; num<=n; num++{
			if num <=b && in_arr[num] == "0"{
				right = append(right, num)
				in_arr[num] = "r"
				if len(right) == n/2{
					break
				}
			}
		}
		if len(left) < n/2 || len(right) < n/2 {
			fmt.Println(-1)
		} else{
			for _, num := range left{
				fmt.Printf("%d ", num)
			}
			for _, num := range right{
				fmt.Printf("%d ", num)
			}
		}
		if i < N_INPUTS-1{
			fmt.Println()
		}
	}
}

package main

import "fmt"

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

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

func main(){
	var N_INPUTS, turn int

	fmt.Scanf("%d\n", &N_INPUTS)
	var inputs [][3]int
	var n, a, b int
	for turn=0; turn<N_INPUTS; turn++{
		fmt.Scanf("%d %d %d\n", &n, &a, &b)
		inputs = append(inputs, [3]int{n, a, b})
	}
	for _, inp := range inputs{
		n, a, b = inp[0], inp[1], inp[2]
		var left []int
		var right []int
		in_arr := make(map[int]string)
		for num:=1; num <= n; num++{
			if num < min(a,b) && len(right) < n/2{
				right = append(right, num)
				in_arr[num] = "r"
			} else if num > max(a,b) && len(left) < n/2{
				left = append(left, num)
				in_arr[num] = "l"
			} else{
				in_arr[num] = "0"
			}
		}
		for num:=1; num<=n; num++{
			if num >= a && in_arr[num] == "0"{
				left = append(left, num)
				in_arr[num] = "l"
				if len(left) == n/2{
					break
				}
			}
		}
		for num:=1; num<=n; num++{
			if num <=b && in_arr[num] == "0"{
				right = append(right, num)
				in_arr[num] = "r"
				if len(right) == n/2{
					break
				}
			}
		}
		if len(left) < n/2 || len(right) < n/2 {
			fmt.Println(-1)
		} else{
			for _, num := range left{
				fmt.Printf("%d ", num)
			}
			for _, num := range right{
				fmt.Printf("%d ", num)
			}
		}
		fmt.Println()
	}
}

package main

import "fmt"

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

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

func main(){
	var left []int
	var right []int
	var N_INPUTS int
	var n, a, b int
	in_arr := make(map[int]string)
	fmt.Scanf("%d\n", &N_INPUTS)
	fmt.Scanf("%d %d %d\n", &n, &a, &b)

	for num:=1; num <= n; num++{
		if num < min(a,b) && len(right) < n/2{
			right = append(right, num)
			in_arr[num] = "r"
		} else if num > max(a,b) && len(left) < n/2{
			left = append(left, num)
			in_arr[num] = "l"
		} else{
			in_arr[num] = "0"
		}
	}
	for num:=1; num<=n; num++{
		if num >= a && in_arr[num] == "0"{
			left = append(left, num)
			in_arr[num] = "l"
			if len(left) == n/2{
				break
			}
		}
	}
	for num:=1; num<=n; num++{
		if num <=b && in_arr[num] == "0"{
			right = append(right, num)
			in_arr[num] = "r"
			if len(right) == n/2{
				break
			}
		}
	}
	if len(left) < n/2 || len(right) < n/2 {
		fmt.Println(-1)
	} else{
		for _, num := range left{
			fmt.Printf("%d ", num)
		}
		for _, num := range right{
			fmt.Printf("%d ", num)
		}
	}
}

package main

import "fmt"

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

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

func main(){
	var left []int
	var right []int
	var N_INPUTS int
	var n, a, b int
	fmt.Scanf("%d\n", &N_INPUTS)
	fmt.Scanf("%d %d %d\n", &n, &a, &b)

	for num:=1; num <= n; num++{
		if num < min(a,b) && len(right) < n/2{
			right = append(right, num)
		} else if num > max(a,b) && len(left) < n/2{
			left = append(left, num)
		} else{
			if num >= a && len(left) < n/2{
				left = append(left, num)
			} else{
				right = append(right, num)
			}
		}
	}
	fmt.Println(left)
	fmt.Println(right)
}

package main

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

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

func (in *CF1612B) 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 *CF1612B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1612B
Date: 10/12/21
User: epradere
URL: https://codeforces.com/problemset/problem/1612/B
Problem: CF1612B
**/
func (in *CF1612B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, a, b := in.NextInt(), in.NextInt(), in.NextInt()
		if (a <= n/2 && b <= n/2) || (a > n/2 && b > n/2) {
			fmt.Println(-1)
		} else {
			arr := make([]int, n)
			arr[0] = a
			ans := true
			for i, j := n, 1; i >= 1; i-- {
				if i != a && i != b {
					arr[j] = i
					j++
					if i > n/2 && i <= a {
						ans = false
					} else if i <= n/2 && i >= b {
						ans = false
					}
				}
			}
			arr[n-1] = n
			if ans {
				for i := 0; i < n; i++ {
					fmt.Print(arr[i], " ")
				}
			} else {
				fmt.Print(-1)
			}
			fmt.Println()
		}
	}
}

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

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

package main

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

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

func (in *CF1612B) 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 *CF1612B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1612B
Date: 10/12/21
User: epradere
URL: https://codeforces.com/problemset/problem/1612/B
Problem: CF1612B
**/
func (in *CF1612B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, a, b := in.NextInt(), in.NextInt(), in.NextInt()
		if (a <= n/2 && b <= n/2) || (a > n/2 && b > n/2) {
			fmt.Println(-1)
		} else {
			fmt.Print(a, " ")
			if a < b {
				for i := n; i >= 1; i-- {
					if i != a && i != b {
						fmt.Printf("%d ", i)
					}
				}
			} else {
				for i := 1; i <= n; i++ {
					if i != a && i != b {
						fmt.Printf("%d ", i)
					}
				}
			}
			fmt.Printf("%d\n", b)
		}
	}
}

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

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

package main

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

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

func (in *CF1612B) 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 *CF1612B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1612B
Date: 10/12/21
User: epradere
URL: https://codeforces.com/problemset/problem/1612/B
Problem: CF1612B
**/
func (in *CF1612B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, a, b := in.NextInt(), in.NextInt(), in.NextInt()
		if (a <= n/2 && b <= n/2) || (a > n/2 && b > n/2) {
			fmt.Println(-1)
		} else {
			fmt.Print(a, " ")
			for i := n; i >= 1; i-- {
				if i != a && i != b {
					fmt.Printf("%d ", i)
				}
			}
			fmt.Printf("%d\n", b)
		}
	}
}

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

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

package main

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

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

func (in *CF1612B) 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 *CF1612B) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1612B
Date: 10/12/21
User: epradere
URL: https://codeforces.com/problemset/problem/1612/B
Problem: CF1612B
**/
func (in *CF1612B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, a, b := in.NextInt(), in.NextInt(), in.NextInt()
		if (a <= n/2 && b <= n/2) || (a > n/2 && b > n/2) {
			fmt.Println(-1)
		} else {
			fmt.Print(a, " ")
			for i := 1; i <= n; i++ {
				if i != a && i != b {
					fmt.Printf("%d ", i)
				}
			}
			fmt.Printf("%d\n", b)
		}
	}
}

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

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

package main

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

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

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

		if (a <= n/2 && b <= n/2) || (a > n/2 && b > n/2) {
			fmt.Fprintln(out, -1)
		} else {
			for i := n; i > 0; i-- {
				d := i
				if a < b && i == b {
					d = a
				} else if a < b && i == a {
					d = b
				}
				fmt.Fprintf(out, "%d ", d)
			}
			fmt.Fprintln(out)
		}
	}
}


The only difference with E1 is the question of the problem.Vlad built a maze out of $$$n$$$ rooms and $$$n-1$$$ bidirectional corridors. From any room $$$u$$$ any other room $$$v$$$ can be reached through a sequence of corridors. Thus, the room system forms an undirected tree.Vlad invited $$$k$$$ friends to play a game with them.Vlad starts the game in the room $$$1$$$ and wins if he reaches a room other than $$$1$$$, into which exactly one corridor leads. Friends are placed in the maze: the friend with number $$$i$$$ is in the room $$$x_i$$$, and no two friends are in the same room (that is, $$$x_i \neq x_j$$$ for all $$$i \neq j$$$). Friends win if one of them meets Vlad in any room or corridor before he wins.For one unit of time, each participant of the game can go through one corridor. All participants move at the same time. Participants may not move. Each room can fit all participants at the same time.Friends know the plan of a maze and intend to win. They don't want to waste too much energy. They ask you to determine if they can win and if they can, what minimum number of friends must remain in the maze so that they can always catch Vlad.In other words, you need to determine the size of the minimum (by the number of elements) subset of friends who can catch Vlad or say that such a subset does not exist.

The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. The input contains an empty string before each test case. The first line of the test case contains two numbers $$$n$$$ and $$$k$$$ ($$$1 \le k < n \le 2\cdot 10^5$$$) — the number of rooms and friends, respectively. The next line of the test case contains $$$k$$$ integers $$$x_1, x_2, \dots, x_k$$$ ($$$2 \le x_i \le n$$$) — numbers of rooms with friends. All $$$x_i$$$ are different. The next $$$n-1$$$ lines contain descriptions of the corridors, two numbers per line $$$v_j$$$ and $$$u_j$$$ ($$$1 \le u_j, v_j \le n$$$) — numbers of rooms that connect the $$$j$$$ corridor. All corridors are bidirectional. From any room, you can go to any other by moving along the corridors. It is guaranteed that the sum of the values $$$n$$$ over all test cases in the test is not greater than $$$2\cdot10^5$$$.

Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be $$$-1$$$ if Vlad wins anyway and a minimal number of friends otherwise.

NoteIn the first set of inputs, even if all the friends stay in the maze, Vlad can still win. Therefore, the answer is "-1".In the second set of inputs it is enough to leave friends from rooms $$$6$$$ and $$$7$$$. Then Vlad will not be able to win. The answer is "2".In the third and fourth sets of inputs Vlad cannot win only if all his friends stay in the maze. Therefore the answers are "1" and "2".

package main
import (
  "fmt"
  "bufio"
  "os"
  "strconv"
)
/*
题意：一棵树中，V在根节点1，k个朋友分布在其他k个节点。若V到达任意叶节点，则V赢；
若V到达之前被任何朋友遇到，则朋友赢；求朋友赢的最小节点集合的大小，若V始终赢，返回-1。

若u子树中存在朋友f且dist(f,u)<=dist(u,1)，那么u子树只需要f这一个人就够了，
若u节点存在朋友则返回1，若到达叶节点则返回-1，任意子树返回-1则V赢。
dist(f,u)通过自底向上递推得到，up[u]表示u与最近的朋友节点的距离，up[f]=0。
*/
var sc *bufio.Scanner
func min(a,b int)int{if a<b{return a};return b}
func I()int{sc.Scan();v,_:=strconv.Atoi(sc.Text());return v}
func main(){
  inp,out:=os.Stdin,os.Stdout
  sc=bufio.NewScanner(inp);W:=bufio.NewWriter(out);sc.Split(bufio.ScanWords);defer W.Flush()

  t:=I()
  for i:=0;i<t;i++{
    n,k:=I(),I();up:=make([]int,n+1);for j:=1;j<=n;j++{up[j]=n};for j:=0;j<k;j++{up[I()]=0}
    G:=map[int][]int{}
    for i:=0;i<n-1;i++{u,v:=I(),I();G[u]=append(G[u],v);G[v]=append(G[v],u)}

    var f func(int,int,int)(int)
    f=func(u,fa,d int)(z int){
      sons:=true
      for _,v:=range G[u]{
        if v!=fa{
          s:=f(v,u,d+1)
          if s<0{sons=false}
          up[u]=min(up[u],up[v]+1);z+=s
        }
      }
      if up[u]<=d{return 1}
      if z==0 || !sons{return -1}
      return
    }
    fmt.Fprintln(W,f(1,0,0))
  }
}

package main
import (
  "fmt"
  "bufio"
  "os"
  "strconv"
)
var sc *bufio.Scanner
func min(a,b int)int{if a<b{return a};return b}
func I()int{sc.Scan();v,_:=strconv.Atoi(sc.Text());return v}
func main(){
  inp,out:=os.Stdin,os.Stdout
  sc=bufio.NewScanner(inp);W:=bufio.NewWriter(out);sc.Split(bufio.ScanWords);defer W.Flush()

  t:=I()
  for i:=0;i<t;i++{
    n,k:=I(),I();up:=make([]int,n+1);for j:=1;j<=n;j++{up[j]=n};for j:=0;j<k;j++{up[I()]=0}
    G:=map[int][]int{}
    for i:=0;i<n-1;i++{u,v:=I(),I();G[u]=append(G[u],v);G[v]=append(G[v],u)}

    var f func(int,int,int)(int)
    f=func(u,fa,d int)(z int){
      child:=true
      for _,v:=range G[u]{
        if v!=fa{
          c:=f(v,u,d+1)
          if c<0{child=false}
          up[u]=min(up[u],up[v]+1);z+=c
        }
      }
      if up[u]<=d{return 1}
      if z==0 || !child{return -1}
      return
    }
    fmt.Fprintln(W,f(1,0,0))
  }
}

package main

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

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

	var T, n, k, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		fr := make([]bool, n)
		for ; k > 0; k-- {
			Fscan(in, &v)
			fr[v-1] = true
		}
		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)
		}

		minDep := make([]int, n)
		var build func(v, fa, d int)
		build = func(v, fa, d int) {
			minDep[v] = 1e9
			if fr[v] {
				minDep[v] = d
			}
			for _, w := range g[v] {
				if w != fa {
					build(w, v, d+1)
					minDep[v] = min(minDep[v], minDep[w])
				}
			}
		}
		build(0, -1, 0)

		var f func(v, fa, d int) int
		f = func(v, fa, d int) (ans int) {
			if d*2 >= minDep[v] {
				return 1
			}
			for _, w := range g[v] {
				if w != fa {
					res := f(w, v, d+1)
					if res < 0 {
						return -1
					}
					ans += res
				}
			}
			if ans == 0 {
				return -1
			}
			return
		}
		Fprintln(out, f(0, -1, 0))
	}
}

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

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

package main

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

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

	var T, n, k, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		fr := make([]bool, n)
		for ; k > 0; k-- {
			Fscan(in, &v)
			fr[v-1] = true
		}
		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)
		}

		minDep := make([]int, n)
		size := make([]int, n)
		var build func(v, fa, d int)
		build = func(v, fa, d int) {
			minDep[v] = 1e9
			if fr[v] {
				minDep[v] = d
				size[v] = 1
			}
			for _, w := range g[v] {
				if w != fa {
					build(w, v, d+1)
					minDep[v] = min(minDep[v], minDep[w])
					size[v] += size[w]
				}
			}
		}
		build(0, -1, 0)

		var f func(v, fa, d int) int
		f = func(v, fa, d int) (ans int) {
			if d*2 >= minDep[v] {
				return 1
			}
			if size[v] == 0 {
				return -1
			}
			for _, w := range g[v] {
				if w != fa {
					res := f(w, v, d+1)
					if res < 0 {
						return -1
					}
					ans += res
				}
			}
			return
		}
		Fprintln(out, f(0, -1, 0))
	}
}

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

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

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.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)
	friends := make(map[int]struct{}, k)
	for x, i := 0, 0; i < k; i++ {
		scan(&x)
		friends[x-1] = struct{}{}
	}
	adj := make([][]int, n)
	for i := 0; i < n-1; i++ {
		var u, v int
		scan(&u, &v)
		adj[u-1] = append(adj[u-1], v-1)
		adj[v-1] = append(adj[v-1], u-1)
	}

	minDist := GetMinDist(adj, friends)

	can, left := false, 0
	var walk func(parent, node, d int)
	walk = func(parent, node, d int) {
		if minDist[node] <= d {
			left++
			return
		}
		if node != 0 && len(adj[node]) == 1 {
			can = true
		}
		for _, next := range adj[node] {
			if next == parent {
				continue
			}
			walk(node, next, d+1)
		}
	}
	walk(-1, 0, 0)

	if can {
		printf("-1\n")
	} else {
		printf("%v\n", left)
	}
}

func GetMinDist(adj [][]int, friends map[int]struct{}) []int {
	dist := make([]int, len(adj))
	for i := range dist {
		dist[i] = (1 << 30)
	}
	var walk func(parent, node int)
	walk = func(parent, node int) {
		if _, ok := friends[node]; ok {
			dist[node] = 0
			return
		}
		for _, next := range adj[node] {
			if next == parent {
				continue
			}
			walk(node, next)
			if dist[node] > dist[next]+1 {
				dist[node] = dist[next] + 1
			}
		}
	}
	walk(-1, 0)
	return dist
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.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)
	friends := make(map[int]struct{}, k)
	for x, i := 0, 0; i < k; i++ {
		scan(&x)
		friends[x-1] = struct{}{}
	}
	adj := make([][]int, n)
	for i := 0; i < n-1; i++ {
		var u, v int
		scan(&u, &v)
		adj[u-1] = append(adj[u-1], v-1)
		adj[v-1] = append(adj[v-1], u-1)
	}

	minDist, _ := GetMinDistAndCnt(adj, friends)

	can, left := false, 0
	var walk func(parent, node, d int)
	walk = func(parent, node, d int) {
		if minDist[node] <= d {
			left++
			return
		}
		if node != 0 && len(adj[node]) == 1 {
			can = true
		}
		for _, next := range adj[node] {
			if next == parent {
				continue
			}
			walk(node, next, d+1)
		}
	}
	walk(-1, 0, 0)

	if can {
		printf("-1\n")
	} else {
		printf("%v\n", left)
	}
}

func GetMinDistAndCnt(adj [][]int, friends map[int]struct{}) ([]int, []int) {
	mindist := make([]int, len(adj))
	cnt := make([]int, len(adj))
	var walk func(parent, node int) (int, int)
	walk = func(parent, node int) (int, int) {
		D, C := (1 << 30), 0
		if _, ok := friends[node]; ok {
			D, C = 0, 1
		}
		for _, next := range adj[node] {
			if next == parent {
				continue
			}
			d, c := walk(node, next)
			C += c
			if D > d+1 {
				D = d + 1
			}
		}
		mindist[node] = D
		cnt[node] = C
		return D, C
	}
	walk(-1, 0)
	return mindist, cnt
}

package main

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

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)
)

const (
	MaxN int = 2e5 + 5
)

type Edge struct {
	u, v int
}

type Graph struct {
	adj [][]int
	e   []*Edge
}

func NewGraph(n int) *Graph {
	return &Graph{
		adj: make([][]int, n+1),
		e:   make([]*Edge, 0),
	}
}

func (g *Graph) AddEdge(u, v int) {
	g.adj[u] = append(g.adj[u], len(g.e))
	g.e = append(g.e, &Edge{u, v})
}

var (
	T         int
	n, k      int
	g         *Graph
	deadPoint []bool
	ans       int
)

func calcDeadPoint(u int, dep int, par int) int {
	deadP := 0
	if deadPoint[u] {
		deadP = (dep + 1) / 2
	}
	for _, i := range g.adj[u] {
		v := g.e[i].v
		if v == par {
			continue
		}
		vDeadP := calcDeadPoint(v, dep+1, u)
		if (deadP == 0 || vDeadP < deadP) && vDeadP != 0 {
			deadP = vDeadP
		}
	}
	if dep == deadP {
		deadPoint[u] = true
	}
	return deadP
}

func calcPath(u int, par int) bool {
	if deadPoint[u] {
		ans++
		return false
	}

	isLeaf := true

	for _, i := range g.adj[u] {
		v := g.e[i].v
		if v == par {
			continue
		}

		isLeaf = false
		if calcPath(v, u) {
			return true
		}
	}

	return isLeaf
}

func Init() {
	g = NewGraph(n)
	deadPoint = make([]bool, n+1)
}

func main() {
	_, _ = fmt.Fscan(rr, &T)
	for iCase := 0; iCase < T; iCase++ {
		_, _ = fmt.Fscan(rr, &n, &k)

		Init()

		for i := 0; i < k; i++ {
			var u int
			_, _ = fmt.Fscan(rr, &u)
			deadPoint[u] = true
		}

		for i := 1; i < n; i++ {
			var u, v int
			_, _ = fmt.Fscan(rr, &u, &v)
			g.AddEdge(u, v)
			g.AddEdge(v, u)
		}

		calcDeadPoint(1, 0, 0)
		ans = 0
		if calcPath(1, 0) {
			_, _ = fmt.Fprintln(ww, -1)
		} else {
			_, _ = fmt.Fprintln(ww, ans)
		}
	}
	_ = ww.Flush()
}

package main

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

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

func main() {
	var in  = bufio.NewReader(os.Stdin)
	var 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)
		var friends = make(map[int]struct{})
		for i := 0; i < k; i++ {
			var x int
			fmt.Fscan(in, &x)
			x--
			friends[x] = struct{}{}
		}
		//fmt.Fprintln(out, "friends", friends)
		var g = make([][]int, n)
		for i := 0; i < n; i++ {
			g[i] = make([]int, 0)
		}
		for i := 0; i < n - 1; i++ {
			var v, u int
			fmt.Fscan(in, &v, &u)
			v--; u--
			//fmt.Fprintln(out, "v:", v, "u:", u)
			g[v] = append(g[v], u)
			g[u] = append(g[u], v)
		}
		//fmt.Fprintln(out, "g", g)

		var dp = make([]int, n)
		for i := 0; i < n; i++ {
			dp[i] = -1
		}
		var dfs func(v int, par int, len int) int
		dfs = func(v int, par int, len int) int {
			if _, ok := friends[v]; ok {
				dp[v] = len/2
			}
			for _, u := range g[v] {
				if par == u {
					continue
				}
				dp[v] = max(dp[v], dfs(u, v, len + 1) - 1)
			}
			return dp[v]
		}

		dfs(0, -1, 0)
		//fmt.Fprintln(out, "dp", dp)

		var ans func(v int, par int) int
		ans = func(v int, par int) int {
			//fmt.Fprintln(out, "ans(v)", v)
			if dp[v] >= 0 {
				return 1
			}
			var sum int
			leaf := true
			for _, u := range g[v] {
				if par == u {
					continue
				}
				leaf = false
				res := ans(u, v)
				if res < 0 {
					return -1
				}
				sum += res
			}
			if leaf {
				return -1
			}
			return sum
		}

		fmt.Fprintln(out, ans(0, -1))
	}
}

package main

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

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

var tree [][]int
var mark []bool
var rootDist []int

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

func count(node, parent int) (bool, int) {
	if mark[node] {
		return true, 1
	}
	if node != 1 && len(tree[node]) == 1 {
		return false, 0
	}
	var ans = 0
	for _, child := range tree[node] {
		if child != parent {
			flag, childCnt := count(child, node)
			if !flag {
				return false, 0
			}
			ans += childCnt
		}
	}
	return true, ans
}

func dfs(node, parent, dist int) int {
	var markDist int = 1e9
	if mark[node] {
		markDist = 0
	}
	rootDist[node] = dist
	for _, child := range tree[node] {
		if child != parent {
			markDist = min(markDist, dfs(child, node, dist+1)+1)
		}
	}

	if markDist <= rootDist[node] {
		mark[node] = true
	}
	return markDist
}

func solveCase() int {
	var n, k, a, b int
	fmt.Fscan(in, &n, &k)
	tree = make([][]int, n+1)
	mark = make([]bool, n+1)
	rootDist = make([]int, n+1)

	for i := 0; i < k; i++ {
		fmt.Fscan(in, &a)
		mark[a] = true
	}

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

	dfs(1, 1, 0)
	ok, cnt := count(1, 1)
	if !ok {
		return -1
	}
	return cnt
}

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

package main
import (
  "fmt"
  "bufio"
  "os"
  "strconv"
)
var sc *bufio.Scanner
func I()int{sc.Scan();v,_:=strconv.Atoi(sc.Text());return v}
func main(){
  inp,out:=os.Stdin,os.Stdout
  sc=bufio.NewScanner(inp);W:=bufio.NewWriter(out);sc.Split(bufio.ScanWords);defer W.Flush()

  t:=I()
  for i:=0;i<t;i++{
    n,k:=I(),I();Fris:=map[int]bool{}
    for j:=0;j<k;j++{Fris[I()]=true}
    G:=map[int][]int{}
    for i:=0;i<n-1;i++{u,v:=I(),I();G[u]=append(G[u],v);G[v]=append(G[v],u)}

    z,zs:=1,map[int]bool{}
    var f func(int,int,int)(bool,[]int)
    f=func(u,fa,d int)(lef bool,fri []int){//返回lef,fri
      if ok:=Fris[u];ok{zs[u]=true;return lef,[]int{d+1,u}}
      lef,fri=true,[]int{n,n+1}
      for _,v:=range G[u]{
        if v!=fa{
          lef=false;lef0,fri0:=f(v,u,d+1)
          lef=lef || lef0
          if fri0[0]<fri[0]{fri=fri0}
        }
      }
      if fri[0]!=n{lef=false}else{fri=[]int{0,0}}
      if lef && (fri[0]==0 || d<fri[0]-d){z=-1;return}
      zs[fri[1]]=true;return
    }
    _,_=f(1,0,0)
    if z>0{z=len(zs)}
    fmt.Fprintln(W,z)
  }
}

package main

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

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

	var T, n, k, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		fr := make([]bool, n)
		for ; k > 0; k-- {
			Fscan(in, &v)
			fr[v-1] = true
		}
		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)
		}

		var f func(v, fa, d int) (int, int)
		f = func(v, fa, d int) (int, int) {
			if fr[v] {
				return 1, d
			}
			ans, minD := 0, int(1e9)
			for _, w := range g[v] {
				if w != fa {
					res, dd := f(w, v, d+1)
					if res < 0 {
						return -1, 0
					}
					ans += res
					if dd < minD {
						minD = dd
					}
				}
			}
			if d*2 >= minD {
				return 1, minD
			}
			if ans == 0 {
				return -1, 0
			}
			return ans, minD
		}
		ans, _ := f(0, -1, 0)
		Fprintln(out, ans)
	}
}

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

package main

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

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)
)

const (
	MaxN int = 2e5 + 5
)

type Edge struct {
	u, v int
}

type Graph struct {
	adj [][]int
	e   []*Edge
}

func NewGraph(n int) *Graph {
	return &Graph{
		adj: make([][]int, n+1),
		e:   make([]*Edge, 0),
	}
}

func (g *Graph) AddEdge(u, v int) {
	g.adj[u] = append(g.adj[u], len(g.e))
	g.e = append(g.e, &Edge{u, v})
}

var (
	T         int
	n, k      int
	g         *Graph
	deadPoint []bool
	ans       int
)

func calcDeadPoint(u int, dep int, par int) int {
	deadP := 0
	if deadPoint[u] {
		deadP = (dep + 1) / 2
	}
	for _, i := range g.adj[u] {
		v := g.e[i].v
		if v == par {
			continue
		}
		vDeadP := calcDeadPoint(v, dep+1, u)
		if (deadP == 0 || vDeadP < deadP) && vDeadP != 0 {
			deadP = vDeadP
		}
	}
	if dep == deadP {
		deadPoint[u] = true
	}
	return deadP
}

func calcPath(u int, par int) bool {
	if deadPoint[u] {
		return false
	}

	isLeaf := true

	for _, i := range g.adj[u] {
		v := g.e[i].v
		if v == par {
			continue
		}

		isLeaf = false
		if calcPath(v, u) {
			return true
		} else {
			ans++
		}
	}

	return isLeaf
}

func Init() {
	g = NewGraph(n)
	deadPoint = make([]bool, n+1)
}

func main() {
	_, _ = fmt.Fscan(rr, &T)
	for iCase := 0; iCase < T; iCase++ {
		_, _ = fmt.Fscan(rr, &n, &k)

		Init()

		for i := 0; i < k; i++ {
			var u int
			_, _ = fmt.Fscan(rr, &u)
			deadPoint[u] = true
		}

		for i := 1; i < n; i++ {
			var u, v int
			_, _ = fmt.Fscan(rr, &u, &v)
			g.AddEdge(u, v)
			g.AddEdge(v, u)
		}

		calcDeadPoint(1, 0, 0)
		ans = 0
		if calcPath(1, 0) {
			_, _ = fmt.Fprintln(ww, -1)
		} else {
			_, _ = fmt.Fprintln(ww, ans)
		}
	}
	_ = ww.Flush()
}


There are $$$n$$$ houses in your city arranged on an axis at points $$$h_1, h_2, \ldots, h_n$$$. You want to build a new house for yourself and consider two options where to place it: points $$$p_1$$$ and $$$p_2$$$.As you like visiting friends, you have calculated in advance the distances from both options to all existing houses. More formally, you have calculated two arrays $$$d_1$$$, $$$d_2$$$: $$$d_{i, j} = \left|p_i - h_j\right|$$$, where $$$|x|$$$ defines the absolute value of $$$x$$$.After a long time of inactivity you have forgotten the locations of the houses $$$h$$$ and the options $$$p_1$$$, $$$p_2$$$. But your diary still keeps two arrays — $$$d_1$$$, $$$d_2$$$, whose authenticity you doubt. Also, the values inside each array could be shuffled, so values at the same positions of $$$d_1$$$ and $$$d_2$$$ may correspond to different houses. Pay attention, that values from one array could not get to another, in other words, all values in the array $$$d_1$$$ correspond the distances from $$$p_1$$$ to the houses, and in the array $$$d_2$$$  — from $$$p_2$$$ to the houses.Also pay attention, that the locations of the houses $$$h_i$$$ and the considered options $$$p_j$$$ could match. For example, the next locations are correct: $$$h = \{1, 0, 3, 3\}$$$, $$$p = \{1, 1\}$$$, that could correspond to already shuffled $$$d_1 = \{0, 2, 1, 2\}$$$, $$$d_2 = \{2, 2, 1, 0\}$$$.Check whether there are locations of houses $$$h$$$ and considered points $$$p_1$$$, $$$p_2$$$, for which the founded arrays of distances would be correct. If it is possible, find appropriate locations of houses and considered options.

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 10^3$$$) — the length of arrays $$$d_1$$$, $$$d_2$$$. The next two lines contain $$$n$$$ integers each: arrays $$$d_1$$$ and $$$d_2$$$ ($$$0 \le d_{i, j} \le 10^9$$$) respectively. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^3$$$.

For each test case, output a single line "NO" if there is no answer. Otherwise output three lines. The first line must contain "YES". In the second line, print $$$n$$$ integers $$$h_1, h_2, \ldots, h_n$$$. In the third line print two integers $$$p_1$$$, $$$p_2$$$. It must be satisfied that $$$0 \le h_i, p_1, p_2 \le 2 \cdot 10^9$$$. We can show that if there is an answer, then there is one satisfying these constraints. If there are several answers, output any of them.

NoteIn the image below you can see the sample solutions. Planned houses are shown in bright colours: pink and purple. Existing houses are dim. In test case $$$1$$$, the first planned house is located at point $$$0$$$, the second at point $$$10$$$. The existing house is located at point $$$5$$$ and is at a distance of $$$5$$$ from both planned houses.   It can be shown that there is no answer for test case $$$2$$$.   In test case $$$3$$$, the planned houses are located at points $$$33$$$ and $$$69$$$.   Note that in test case $$$4$$$, both plans are located at point $$$1$$$, where one of the existing houses is located at the same time. It is a correct placement.

package main

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

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

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {
		// fmt.Fprintln(w, "TTTTTTTTTT ", T) // ***

		var n int
		fmt.Fscan(r, &n)
		d1 := make([]int, n)
		d2 := make([]int, n)

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

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

		sort.Ints(d1)
		sort.Ints(d2)

		vis := make(map[int]bool)
		h := make([]int, n)
		var p1, p2, N int

		for _, v := range d2 {
			// fmt.Fprintln(w, "OOOOOOOOOOOOOOO ", v) // ***
			for _, l := range []int{d1[0]+v, abs(d1[0]-v)} {
				// fmt.Fprintln(w, "IIIIIIII ", l) // ***
				if l > int(2e9) { continue }
				if vis[l] { continue }
				vis[l] = true
				if d2[n-1] > l {
					p1 = d2[n-1] - l
				} else {
					p1 = 0
				}
				p2 = p1 + l
				N = 0

				cm1 := make(map[int]int)
				cm2 := make(map[int]int)

				for i := 0; i < n; i++ {
					cm1[d1[i]]++
					cm2[d2[i]]++
				}

				i := n-1
				j := n-1

				for i >= 0 && j >= 0 && (d1[i] >= l || d2[j] >= l) {
					if d1[i] > d2[j] {
						if cm2[d1[i]-l] > 0 && cm1[d1[i]] > 0 {
							cm2[d1[i]-l]--
							cm1[d1[i]]--
							h[N] = p1 + d1[i]
							N++
						}
						i--
					} else {
						if cm1[d2[j]-l] > 0 && cm2[d2[j]] > 0 {
							cm1[d2[j]-l]--
							cm2[d2[j]]--
							h[N] = p1 + l - d2[j]
							N++
						}
						j--
					}
				}

				for i = 0; i < n && d1[i] < l; i++ {
					if cm2[l-d1[i]] > 0 && cm1[d1[i]] > 0 {
						cm2[l-d1[i]]--
						cm1[d1[i]]--
						h[N] = p1 + d1[i]
						N++
					}
				}

				/*
				for i := 0; i < n && d2[i] < l; i++ {
					if cm1[l-d2[i]] > 0 && cm2[d2[i]] > 0 {
						cm1[l-d2[i]]--
						cm2[d2[i]]--
						h[N] = l - d2[i]
					}
				}
				*/

				if N == n {
					break
				}
			}

			if N == n {
				break
			}

		}

		// fmt.Fprintln(w, "NNNNNNNN ", N)
		if N != n {
			fmt.Fprintln(w, "NO")
		} else {
			fmt.Fprintln(w, "YES")
			for i := 0; i < n; i++ {
				fmt.Fprintf(w, "%d ", h[i])
			}
			fmt.Fprintf(w, "\n%d %d\n", p1, p2)
		}

	}

}

package main

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

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

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {
		// fmt.Fprintln(w, "TTTTTTTTTT ", T) // ***

		var n int
		fmt.Fscan(r, &n)
		d1 := make([]int, n)
		d2 := make([]int, n)

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

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

		sort.Ints(d1)
		sort.Ints(d2)

		h := make([]int, n)
		var p1, p2, N int

		for _, v := range d2 {
			// fmt.Fprintln(w, "OOOOOOOOOOOOOOO ", v) // ***
			for _, l := range []int{d1[0]+v, abs(d1[0]-v)} {
				// fmt.Fprintln(w, "IIIIIIII ", l) // ***
				if l > int(2e9) { continue }
				if d2[n-1] > l {
					p1 = d2[n-1] - l
				} else {
					p1 = 0
				}
				p2 = p1 + l
				N = 0

				cm1 := make(map[int]int)
				cm2 := make(map[int]int)

				for i := 0; i < n; i++ {
					cm1[d1[i]]++
					cm2[d2[i]]++
				}

				i := n-1
				j := n-1

				for i >= 0 && j >= 0 && (d1[i] >= l || d2[j] >= l) {
					if d1[i] > d2[j] {
						if cm2[d1[i]-l] > 0 && cm1[d1[i]] > 0 {
							cm2[d1[i]-l]--
							cm1[d1[i]]--
							h[N] = p1 + d1[i]
							N++
						}
						i--
					} else {
						if cm1[d2[j]-l] > 0 && cm2[d2[j]] > 0 {
							cm1[d2[j]-l]--
							cm2[d2[j]]--
							h[N] = p1 + l - d2[j]
							N++
						}
						j--
					}
				}

				for i = 0; i < n && d1[i] < l; i++ {
					if cm2[l-d1[i]] > 0 && cm1[d1[i]] > 0 {
						cm2[l-d1[i]]--
						cm1[d1[i]]--
						h[N] = p1 + d1[i]
						N++
					}
				}

				/*
				for i := 0; i < n && d2[i] < l; i++ {
					if cm1[l-d2[i]] > 0 && cm2[d2[i]] > 0 {
						cm1[l-d2[i]]--
						cm2[d2[i]]--
						h[N] = l - d2[i]
					}
				}
				*/

				if N == n {
					break
				}
			}

			if N == n {
				break
			}

		}

		// fmt.Fprintln(w, "NNNNNNNN ", N)
		if N != n {
			fmt.Fprintln(w, "NO")
		} else {
			fmt.Fprintln(w, "YES")
			for i := 0; i < n; i++ {
				fmt.Fprintf(w, "%d ", h[i])
			}
			fmt.Fprintf(w, "\n%d %d\n", p1, p2)
		}

	}

}

package main

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

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

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {
		// fmt.Fprintln(w, "TTTTTTTTTT ", T) // ***

		var n int
		fmt.Fscan(r, &n)
		d1 := make([]int, n)
		d2 := make([]int, n)

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

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

		sort.Ints(d1)
		sort.Ints(d2)

		h := make([]int, n)
		var p1, p2, N int

		for _, v := range d2 {
			// fmt.Fprintln(w, "OOOOOOOOOOOOOOO ", v) // ***
			for _, l := range []int{d1[0]+v, abs(d1[0]-v)} {
				// fmt.Fprintln(w, "IIIIIIII ", l) // ***
				p1 = d2[n-1]
				p2 = d2[n-1] + l
				N = 0

				cm1 := make(map[int]int)
				cm2 := make(map[int]int)

				for i := 0; i < n; i++ {
					cm1[d1[i]]++
					cm2[d2[i]]++
				}

				i := n-1
				j := n-1

				for i >= 0 && j >= 0 && (d1[i] >= l || d2[j] >= l) {
					if d1[i] > d2[j] {
						if cm2[d1[i]-l] > 0 && cm1[d1[i]] > 0 {
							cm2[d1[i]-l]--
							cm1[d1[i]]--
							h[N] = p1 + d1[i]
							N++
						}
						i--
					} else {
						if cm1[d2[j]-l] > 0 && cm2[d2[j]] > 0 {
							cm1[d2[j]-l]--
							cm2[d2[j]]--
							h[N] = p1 + l - d2[j]
							N++
						}
						j--
					}
				}

				for i = 0; i < n && d1[i] < l; i++ {
					if cm2[l-d1[i]] > 0 && cm1[d1[i]] > 0 {
						cm2[l-d1[i]]--
						cm1[d1[i]]--
						h[N] = p1 + d1[i]
						N++
					}
				}

				/*
				for i := 0; i < n && d2[i] < l; i++ {
					if cm1[l-d2[i]] > 0 && cm2[d2[i]] > 0 {
						cm1[l-d2[i]]--
						cm2[d2[i]]--
						h[N] = l - d2[i]
					}
				}
				*/

				if N == n {
					break
				}
			}

			if N == n {
				break
			}

		}

		// fmt.Fprintln(w, "NNNNNNNN ", N)
		if N != n {
			fmt.Fprintln(w, "NO")
		} else {
			fmt.Fprintln(w, "YES")
			for i := 0; i < n; i++ {
				fmt.Fprintf(w, "%d ", h[i])
			}
			fmt.Fprintf(w, "\n%d %d\n", p1, p2)
		}

	}

}

package main

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

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

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {
		// fmt.Fprintln(w, "TTTTTTTTTT ", T) // ***

		var n int
		fmt.Fscan(r, &n)
		d1 := make([]int, n)
		d2 := make([]int, n)

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

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

		sort.Ints(d1)
		sort.Ints(d2)

		h := make([]int, n)
		var p1, p2, N int

		for _, v := range d2 {
			// fmt.Fprintln(w, "OOOOOOOOOOOOOOO ", v) // ***
			for _, l := range []int{d1[0]+v, abs(d1[0]-v)} {
				// fmt.Fprintln(w, "IIIIIIII ", l) // ***
				p1 = d2[n-1]
				p2 = d2[n-1] + l
				N = 0

				cm1 := make(map[int]int)
				cm2 := make(map[int]int)

				for i := 0; i < n; i++ {
					cm1[d1[i]]++
					cm2[d2[i]]++
				}

				for i := n-1; i >= 0 && d1[i] >= l; i-- {
					if cm2[d1[i]-l] > 0 && cm1[d1[i]] > 0 {
						cm2[d1[i]-l]--
						cm1[d1[i]]--
						h[N] = p1 + d1[i]
						N++
					}
				}

				for i := n-1; i >= 0 && d2[i] >= l; i-- {
					if cm1[d2[i]-l] > 0 && cm2[d2[i]] > 0 {
						cm1[d2[i]-l]--
						cm2[d2[i]]--
						h[N] = p1 + l - d2[i]
						N++
					}
				}

				for i := 0; i < n && d1[i] < l; i++ {
					if cm2[l-d1[i]] > 0 && cm1[d1[i]] > 0 {
						cm2[l-d1[i]]--
						cm1[d1[i]]--
						h[N] = p1 + d1[i]
						N++
					}
				}

				/*
				for i := 0; i < n && d2[i] < l; i++ {
					if cm1[l-d2[i]] > 0 && cm2[d2[i]] > 0 {
						cm1[l-d2[i]]--
						cm2[d2[i]]--
						h[N] = l - d2[i]
					}
				}
				*/

				if N == n {
					break
				}
			}

			if N == n {
				break
			}

		}

		// fmt.Fprintln(w, "NNNNNNNN ", N)
		if N != n {
			fmt.Fprintln(w, "NO")
		} else {
			fmt.Fprintln(w, "YES")
			for i := 0; i < n; i++ {
				fmt.Fprintf(w, "%d ", h[i])
			}
			fmt.Fprintf(w, "\n%d %d\n", p1, p2)
		}

	}

}

package main

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

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

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {
		// fmt.Fprintln(w, "TTTTTTTTTT ", T) // ***

		var n int
		fmt.Fscan(r, &n)
		d1 := make([]int, n)
		d2 := make([]int, n)

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

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

		sort.Ints(d1)
		sort.Ints(d2)

		h := make([]int, n)
		var p1, p2, N int

		for _, v := range d2 {
			// fmt.Fprintln(w, "OOOOOOOOOOOOOOO ", v) // ***
			for _, l := range []int{d1[0]+v, abs(d1[0]-v)} {
				// fmt.Fprintln(w, "IIIIIIII ", l) // ***
				p1 = 0
				p2 = l
				N = 0

				cm1 := make(map[int]int)
				cm2 := make(map[int]int)

				for i := 0; i < n; i++ {
					cm1[d1[i]]++
					cm2[d2[i]]++
				}

				for i := n-1; i >= 0 && d1[i] >= l; i-- {
					if cm2[d1[i]-l] > 0 && cm1[d1[i]] > 0 {
						cm2[d1[i]-l]--
						cm1[d1[i]]--
						h[N] = d1[i]
						N++
					}
				}

				for i := n-1; i >= 0 && d2[i] >= l; i-- {
					if cm1[d2[i]-l] > 0 && cm2[d2[i]] > 0 {
						cm1[d2[i]-l]--
						cm2[d2[i]]--
						h[N] = l - d2[i]
						N++
					}
				}

				for i := 0; i < n && d1[i] < l; i++ {
					if cm2[l-d1[i]] > 0 && cm1[d1[i]] > 0 {
						cm2[l-d1[i]]--
						cm1[d1[i]]--
						h[N] = d1[i]
						N++
					}
				}

				/*
				for i := 0; i < n && d2[i] < l; i++ {
					if cm1[l-d2[i]] > 0 && cm2[d2[i]] > 0 {
						cm1[l-d2[i]]--
						cm2[d2[i]]--
						h[N] = l - d2[i]
					}
				}
				*/

				if N == n {
					break
				}
			}

			if N == n {
				break
			}

		}

		// fmt.Fprintln(w, "NNNNNNNN ", N)
		if N != n {
			fmt.Fprintln(w, "NO")
		} else {
			fmt.Fprintln(w, "YES")
			for i := 0; i < n; i++ {
				fmt.Fprintf(w, "%d ", h[i])
			}
			fmt.Fprintf(w, "\n%d %d\n", p1, p2)
		}

	}

}

package main

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

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

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {
		fmt.Fprintln(w, "TTTTTTTTTT ", T) // ***

		var n int
		fmt.Fscan(r, &n)
		d1 := make([]int, n)
		d2 := make([]int, n)

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

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

		sort.Ints(d1)
		sort.Ints(d2)

		h := make([]int, n)
		var p1, p2, N int

		for _, v := range d2 {
			fmt.Fprintln(w, "OOOOOOOOOOOOOOO ", v) // ***
			for _, l := range []int{d1[0]+v, abs(d1[0]-v)} {
				fmt.Fprintln(w, "IIIIIIII ", l) // ***
				p1 = 0
				p2 = l
				N = 0

				cm1 := make(map[int]int)
				cm2 := make(map[int]int)

				for i := 0; i < n; i++ {
					cm1[d1[i]]++
					cm2[d2[i]]++
				}

				for i := n-1; i >= 0 && d1[i] >= l; i-- {
					if cm2[d1[i]-l] > 0 && cm1[d1[i]] > 0 {
						cm2[d1[i]-l]--
						cm1[d1[i]]--
						h[N] = d1[i]
						N++
					}
				}

				for i := n-1; i >= 0 && d2[i] >= l; i-- {
					if cm1[d2[i]-l] > 0 && cm2[d2[i]] > 0 {
						cm1[d2[i]-l]--
						cm2[d2[i]]--
						h[N] = l - d2[i]
						N++
					}
				}

				for i := 0; i < n && d1[i] < l; i++ {
					if cm2[l-d1[i]] > 0 && cm1[d1[i]] > 0 {
						cm2[l-d1[i]]--
						cm1[d1[i]]--
						h[N] = d1[i]
						N++
					}
				}

				/*
				for i := 0; i < n && d2[i] < l; i++ {
					if cm1[l-d2[i]] > 0 && cm2[d2[i]] > 0 {
						cm1[l-d2[i]]--
						cm2[d2[i]]--
						h[N] = l - d2[i]
					}
				}
				*/

				if N == n {
					break
				}
			}

			if N == n {
				break
			}

		}

		fmt.Fprintln(w, "NNNNNNNN ", N)
		if N != n {
			fmt.Fprintln(w, "NO")
		} else {
			fmt.Fprintln(w, "YES")
			for i := 0; i < n; i++ {
				fmt.Fprintf(w, "%d ", h[i])
			}
			fmt.Fprintf(w, "\n%d %d\n", p1, p2)
		}

	}

}


Mayor of city S just hates trees and lawns. They take so much space and there could be a road on the place they occupy!The Mayor thinks that one of the main city streets could be considerably widened on account of lawn nobody needs anyway. Moreover, that might help reduce the car jams which happen from time to time on the street.The street is split into n equal length parts from left to right, the i-th part is characterized by two integers: width of road si and width of lawn gi.  For each of n parts the Mayor should decide the size of lawn to demolish. For the i-th part he can reduce lawn width by integer xi (0 ≤ xi ≤ gi). After it new road width of the i-th part will be equal to s'i = si + xi and new lawn width will be equal to g'i = gi - xi.On the one hand, the Mayor wants to demolish as much lawn as possible (and replace it with road). On the other hand, he does not want to create a rapid widening or narrowing of the road, which would lead to car accidents. To avoid that, the Mayor decided that width of the road for consecutive parts should differ by at most 1, i.e. for each i (1 ≤ i < n) the inequation |s'i + 1 - s'i| ≤ 1 should hold. Initially this condition might not be true.You need to find the the total width of lawns the Mayor will destroy according to his plan.

The first line contains integer n (1 ≤ n ≤ 2·105) — number of parts of the street. Each of the following n lines contains two integers si, gi (1 ≤ si ≤ 106, 0 ≤ gi ≤ 106) — current width of road and width of the lawn on the i-th part of the street.

In the first line print the total width of lawns which will be removed. In the second line print n integers s'1, s'2, ..., s'n (si ≤ s'i ≤ si + gi) — new widths of the road starting from the first part and to the last. If there is no solution, print the only integer -1 in the first line.

package main

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

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

// io functions
var stdin *bufio.Reader
var stdout *bufio.Writer
func fastio() {
  stdin = bufio.NewReader(os.Stdin)
  stdout = bufio.NewWriter(os.Stdout)
}
func scan(buf ...interface{}) {
  for _, a := range buf {
    fmt.Fscan(stdin, a)
  }
}
func dbg(x ...interface{}) {
  if os.Getenv("LOCAL") != "true" {
    return
  }
  fmt.Fprint(os.Stderr, ">>>>")
  for _, a := range x {
    fmt.Fprint(os.Stderr, " ", a)
  }
  fmt.Fprintln(os.Stderr)
}

func main() {
  fastio()
  var n int
  scan(&n)
  s := make([]int64, n+1)
  g := make([]int64, n+1)
  x := make([]int64, n+1)
  y := make([]int64, n+1)
  a := make([]int64, n+1)
  for i := 1; i <= n; i++ {
    scan(&s[i],&g[i])
    dbg(s[i])
    if i == 1 {
      x[i] = s[i]+g[i]
    } else {
      x[i] = min(x[i-1]+1, s[i]+g[i])
    }
  }
  var tot int64
  for i := n; 1 <= i; i-- {
    if i == n {
      y[n] = s[n]+g[n]
    } else {
      y[i] = min(y[i+1]+1, s[i]+g[i])
    }
    a[i] = min(x[i], y[i])
    if s[i] <= a[i] && a[i] <= s[i]+g[i] {
      tot += a[i]-s[i]
    } else {
      fmt.Printf("-1\n")
      return
    }
  }
  fmt.Fprintf(stdout, "%d\n", tot)
  for i := 1; i <= n; i++ {
    if i > 1 {
      fmt.Fprintf(stdout, " ")
    }
    fmt.Fprintf(stdout, "%d",a[i])
  }
  fmt.Fprintf(stdout, "\n")
  stdout.Flush()
}

package main

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

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

// io functions
var stdin *bufio.Reader
var stdout *bufio.Writer
func fastio() {
  stdin = bufio.NewReader(os.Stdin)
  stdout = bufio.NewWriter(os.Stdout)
}
func scan(buf ...interface{}) {
  for _, a := range buf {
    fmt.Fscan(stdin, a)
  }
}
func dbg(x ...interface{}) {
  fmt.Fprint(os.Stderr, ">>>>")
  for _, a := range x {
    fmt.Fprint(os.Stderr, " ", a)
  }
  fmt.Fprintln(os.Stderr)
}

func main() {
  fastio()
  var n int
  scan(&n)
  dbg(n)
  s := make([]int64, n+1)
  g := make([]int64, n+1)
  x := make([]int64, n+1)
  y := make([]int64, n+1)
  a := make([]int64, n+1)
  for i := 1; i <= n; i++ {
    scan(&s[i],&g[i])
    dbg(s[i], g[i])
    if i == 1 {
      x[i] = s[i]+g[i]
    } else {
      x[i] = min(x[i-1]+1, s[i]+g[i])
    }
  }
  var tot int64
  for i := n; 1 <= i; i-- {
    if i == n {
      y[n] = s[n]+g[n]
    } else {
      y[i] = min(y[i+1]+1, s[i]+g[i])
    }
    a[i] = min(x[i], y[i])
    if s[i] <= a[i] && a[i] <= s[i]+g[i] {
      tot += a[i]-s[i]
    } else {
      fmt.Printf("-1\n")
      return
    }
  }
  fmt.Fprintf(stdout, "%d\n", tot)
  for i := 1; i <= n; i++ {
    if i > 1 {
      fmt.Fprintf(stdout, " ")
    }
    fmt.Fprintf(stdout, "%d",a[i])
  }
  fmt.Fprintf(stdout, "\n")
  stdout.Flush()
}

package main

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

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

var s, g, x, y, a [200005]int64

func main() {
  in := bufio.NewReader(os.Stdin)
  var n int
  fmt.Fscanf(in, "%d\n", &n)
  for i := 1; i <= n; i++ {
    fmt.Fscanf(in, "%d %d\n",&s[i],&g[i])
    if i == 1 {
      x[i] = s[i]+g[i]
    } else {
      x[i] = min(x[i-1]+1, s[i]+g[i])
    }
  }
  var tot int64
  for i := n; 1 <= i; i-- {
    if i == n {
      y[n] = s[n]+g[n]
    } else {
      y[i] = min(y[i+1]+1, s[i]+g[i])
    }
    a[i] = min(x[i], y[i])
    if s[i] <= a[i] && a[i] <= s[i]+g[i] {
      tot += a[i]-s[i]
    } else {
      fmt.Printf("-1\n")
      return
    }
  }
  out := bufio.NewWriter(os.Stdout)
  fmt.Fprintf(out, "%d\n", tot)
  for i := 1; i <= n; i++ {
    if i > 1 {
      fmt.Fprintf(out, " ")
    }
    fmt.Fprintf(out, "%d",a[i])
  }
  fmt.Fprintf(out, "\n")
  out.Flush()
}

package main

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

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

// io functions
var stdin *bufio.Reader
var stdout *bufio.Writer
func fastio() {
  stdin = bufio.NewReader(os.Stdin)
  stdout = bufio.NewWriter(os.Stdout)
}
func scan(buf ...interface{}) {
  for _, a := range buf {
    fmt.Fscan(stdin, a)
  }
}
func dbg(x ...interface{}) {
  fmt.Fprint(os.Stderr, ">>>>")
  for _, a := range x {
    fmt.Fprint(os.Stderr, " ", a)
  }
  fmt.Fprintln(os.Stderr)
}

func main() {
  fastio()
  var n int
  scan(&n)
  s := make([]int64, n+1)
  g := make([]int64, n+1)
  x := make([]int64, n+1)
  y := make([]int64, n+1)
  a := make([]int64, n+1)
  for i := 1; i <= n; i++ {
    scan(&s[i],&g[i])
    if i == 1 {
      x[i] = s[i]+g[i]
    } else {
      x[i] = min(x[i-1]+1, s[i]+g[i])
    }
  }
  var tot int64
  for i := n; 1 <= i; i-- {
    if i == n {
      y[n] = s[n]+g[n]
    } else {
      y[i] = min(y[i+1]+1, s[i]+g[i])
    }
    a[i] = min(x[i], y[i])
    if s[i] <= a[i] && a[i] <= s[i]+g[i] {
      tot += a[i]-s[i]
    } else {
      fmt.Printf("-1\n")
      return
    }
  }
  fmt.Fprintf(stdout, "%d\n", tot)
  for i := 1; i <= n; i++ {
    if i > 1 {
      fmt.Fprintf(stdout, " ")
    }
    fmt.Fprintf(stdout, "%d",a[i])
  }
  fmt.Fprintf(stdout, "\n")
  stdout.Flush()
}

package main

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

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

var s, g, x, y, a [200005]int64

func main() {
  in := bufio.NewReader(os.Stdin)
  var n int
  fmt.Fscanf(in, "%d\n", &n)
  for i := 1; i <= n; i++ {
    fmt.Fscanf(in, "%d %d\n",&s[i],&g[i])
    if i == 1 {
      x[i] = s[i]+g[i]
    } else {
      x[i] = min(x[i-1]+1, s[i]+g[i])
    }
  }
  var tot int64
  for i := n; 1 <= i; i-- {
    if i == n {
      y[n] = s[n]+g[n]
    } else {
      y[i] = min(y[i+1]+1, s[i]+g[i])
    }
    a[i] = min(x[i], y[i])
    if s[i] <= a[i] && a[i] <= s[i]+g[i] {
      tot += a[i]-s[i]
    } else {
      fmt.Printf("-1\n")
      return
    }
  }
  fmt.Printf("%d\n", tot)
  for i := 1; i <= n; i++ {
    if i > 1 {
      fmt.Printf(" ")
    }
    fmt.Printf("%d",a[i])
  }
  fmt.Printf("\n")
}

package main

import "fmt"
import "unsafe"

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

var s, g, x, y, a [200005]int

func main() {
  var varr int
  fmt.Println("sizeof(int) =",unsafe.Sizeof(varr))
  var n int
  fmt.Scanf("%d\n", &n)
  for i := 1; i <= n; i++ {
    fmt.Scanf("%d %d\n",&s[i],&g[i])
    if i == 1 {
      x[i] = s[i]+g[i]
    } else {
      x[i] = min(x[i-1]+1, s[i]+g[i])
    }
  }
  var tot int
  for i := n; 1 <= i; i-- {
    if i == n {
      y[n] = s[n]+g[n]
    } else {
      y[i] = min(y[i+1]+1, s[i]+g[i])
    }
    a[i] = min(x[i], y[i])
    if s[i] <= a[i] && a[i] <= s[i]+g[i] {
      tot += a[i]-s[i]
    } else {
      fmt.Printf("-1\n")
      return
    }
  }
  fmt.Printf("%d\n", tot)
  for i := 1; i <= n; i++ {
    if i > 1 {
      fmt.Printf(" ")
    }
    fmt.Printf("%d",a[i])
  }
  fmt.Printf("\n")
}

package main

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

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

// io functions
var stdin *bufio.Reader
var stdout *bufio.Writer
func fastio() {
  stdin = bufio.NewReader(os.Stdin)
  stdout = bufio.NewWriter(os.Stdout)
}
func sci(buf ...*int) {
  for _, a := range buf {
    fmt.Fscanf(stdin, "%d ", a)
  }
}
func scl(buf ...*int64) {
  for _, a := range buf {
    fmt.Fscanf(stdin, "%d ", a)
  }
}
func dbg(x ...interface{}) {
  fmt.Fprintln(os.Stderr, ">>>>", x)
}

func main() {
  fastio()
  var n int
  sci(&n)
  s := make([]int64, n+1)
  g := make([]int64, n+1)
  x := make([]int64, n+1)
  y := make([]int64, n+1)
  a := make([]int64, n+1)
  for i := 1; i <= n; i++ {
    scl(&s[i],&g[i])
    dbg(s[i], g[i])
    if i == 1 {
      x[i] = s[i]+g[i]
    } else {
      x[i] = min(x[i-1]+1, s[i]+g[i])
    }
  }
  var tot int64
  for i := n; 1 <= i; i-- {
    if i == n {
      y[n] = s[n]+g[n]
    } else {
      y[i] = min(y[i+1]+1, s[i]+g[i])
    }
    a[i] = min(x[i], y[i])
    if s[i] <= a[i] && a[i] <= s[i]+g[i] {
      tot += a[i]-s[i]
    } else {
      fmt.Printf("-1\n")
      return
    }
  }
  fmt.Fprintf(stdout, "%d\n", tot)
  for i := 1; i <= n; i++ {
    if i > 1 {
      fmt.Fprintf(stdout, " ")
    }
    fmt.Fprintf(stdout, "%d",a[i])
  }
  fmt.Fprintf(stdout, "\n")
  stdout.Flush()
}

package main

import "fmt"

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

func main() {
  var n int
  fmt.Scanf("%d", &n)
  s := make([]int, n+1)
  g := make([]int, n+1)
  x := make([]int, n+1)
  y := make([]int, n+1)
  a := make([]int, n+1)
  for i := 1; i <= n; i++ {
    fmt.Scanf("%d %d",&s[i],&g[i])
    if i == 1 {
      x[i] = s[i]+g[i]
    } else {
      x[i] = min(x[i-1]+1, s[i]+g[i])
    }
  }
  var tot int
  for i := n; 1 <= i; i-- {
    if i == n {
      y[n] = s[n]+g[n]
    } else {
      y[i] = min(y[i+1]+1, s[i]+g[i])
    }
    a[i] = min(x[i], y[i])
    if s[i] <= a[i] && a[i] <= s[i]+g[i] {
      tot += a[i]-s[i]
    } else {
      fmt.Printf("-1\n")
      return
    }
  }
  fmt.Printf("%d\n", tot)
  for i := 1; i <= n; i++ {
    fmt.Printf("%d ",a[i])
  }
  fmt.Printf("\n")
}


The Smart Beaver from ABBYY has a lot of hobbies. One of them is constructing efficient hash tables. One of the most serious problems in hash tables is resolving collisions. The Beaver is interested in this problem very much and he decided to explore it in detail.We assume that the hash table consists of h cells numbered from 0 to h - 1. Objects are added to and removed from it. Every object has its own unique identifier. In addition, every object has a corresponding hash value — an integer between 0 and h - 1, inclusive. When an object is added to the table, if the cell corresponding to the hash value of the object is free, then this object goes there. If the cell is already occupied by another object, there is a collision. When an object is deleted from the table, the cell which it occupied becomes empty.The Smart Beaver has recently learned about the method of linear probing to resolve collisions. It is as follows. Let's say that the hash value for the added object equals t and cell t of the table is already occupied. Then we try to add this object to cell (t + m) mod h. If it is also occupied, then we try cell (t + 2·m) mod h, then cell (t + 3·m) mod h, and so on. Note that in some cases it's possible that the new object can not be added to the table. It is guaranteed that the input for this problem doesn't contain such situations.The operation a mod b means that we take the remainder of the division of number a by number b.This technique immediately seemed very inoptimal to the Beaver, and he decided to assess its inefficiency. So, you are given a sequence of operations, each of which is either an addition of an object to the table or a deletion of an object from the table. When adding a new object, a sequence of calls to the table is performed. Calls to occupied cells are called dummy. In other words, if the result of the algorithm described above is the object being added to cell (t + i·m) mod h (i ≥ 0), then exactly i dummy calls have been performed.Your task is to calculate the total number of dummy calls to the table for the given sequence of additions and deletions. When an object is deleted from the table, assume that no dummy calls are performed. The table is empty before performing the operations, that is, initially it doesn't contain any objects.

The first line of input contains three integers h, m and n (1 ≤ m < h), separated by spaces, where h is the size of the hash table, m is the number that is used to resolve collisions, n is the number of operations. The following n lines contains the descriptions of the operations. Their execution order corresponds to the order in which they appear in the input file. Each operation is described by a single line. The operations are described as follows:   "+ id hash"This is the format of the operation that adds an object to the table. The first character is "+" (ASCII 43), followed by a single space, then the object identifier id (0 ≤ id ≤ 109), then another space, and the hash value of the given object hash (0 ≤ hash < h). The object identifier and the hash value of this object are integers. "- id"This is the format of the operation that deletes an object from the table. The first character is "-" (ASCII 45), followed by a single space, then the object identifier id (0 ≤ id ≤ 109). The object identifier is an integer. It is guaranteed that for all addition operations the value of id is unique. It is also guaranteed that the initial data is correct, that is, it's always possible to add an object to the hash table and there won't be any deletions of nonexisting objects. The input limitations for getting 20 points are:    1 ≤ h ≤ 5000  1 ≤ n ≤ 5000  The input limitations for getting 50 points are:    1 ≤ h ≤ 5·104  1 ≤ n ≤ 5·104  The input limitations for getting 100 points are:    1 ≤ h ≤ 2·105  1 ≤ n ≤ 2·105

Print a single number — the total number of dummy calls to the hash table. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams and the %I64d specifier.

package main

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

var in = bufio.NewReaderSize(os.Stdin, 2<<16)

func main() {
	var h, m, n int
	fmt.Fscan(in, &h, &m, &n)
	ht := newHashTable(h, m)
	var ans int64
	for i := 0; i < n; i++ {
		var cmd string
		fmt.Fscan(in, &cmd)
		switch cmd {
		case "+":
			var id, hash int
			fmt.Fscan(in, &id, &hash)
			ans += int64(ht.add(id, hash))
		case "-":
			var id int
			fmt.Fscan(in, &id)
			ht.rem(id)
		default:
			panic(cmd)
		}
	}
	fmt.Println(ans)
}

type ptr struct {
	chain, i int
}

// Maždaug segment tree.
type chain []int

type hashTable struct {
	ptrs   []ptr
	chains []chain
	byID   map[int]ptr
}

func newHashTable(h, m int) *hashTable {
	r := &hashTable{ptrs: make([]ptr, h), byID: make(map[int]ptr)}
	for i := range r.ptrs {
		r.ptrs[i].chain = -1
	}
	for i := 0; i < m; i++ {
		if r.ptrs[i].chain != -1 {
			continue
		}
		sz := 0
		for j := i; r.ptrs[j].chain == -1; j = (j + m) % h {
			r.ptrs[j].chain = len(r.chains)
			r.ptrs[j].i = sz
			sz++
		}
		r.chains = append(r.chains, make(chain, 4*sz))
	}
	return r
}

func (h *hashTable) add(id, hash int) int {
	p := h.ptrs[hash]
	ch := h.chains[p.chain]
	q := ch.findFirstZero(p.i)
	ch.add(q, 1)
	h.byID[id] = ptr{p.chain, q}
	sz := len(ch) / 4
	return (q + sz - p.i) % sz
}

func (h *hashTable) rem(id int) {
	p := h.byID[id]
	h.chains[p.chain].add(p.i, -1)
}

func (ch chain) findFirstZero(i int) int {
	r := ch.ffz(i, 1, 0, len(ch)/4)
	if r == len(ch)/4 {
		return ch.ffz(0, 1, 0, len(ch)/4)
	}
	return r
}

func (ch chain) ffz(i, v, l, r int) int {
	if ch[v] == r-l {
		return r
	}
	if l + 1 == r {
		return l
	}
	m := (l + r) / 2
	if i < m {
		i := ch.ffz(i, v*2, l, m)
		if i < m {
			return i
		}
	}
	return ch.ffz(i, v*2+1, m, r)
}

func (ch chain) add(i, val int) {
	v := 1
	l := 0
	r := len(ch) / 4
	for l < r {
		ch[v] += val
		if l + 1 == r {
			break
		}
		m := (l + r) / 2
		if i < m {
			r = m
			v *= 2
		} else {
			l = m
			v = v*2 + 1
		}
	}
}

package main

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

var in = bufio.NewReaderSize(os.Stdin, 2<<16)

func main() {
	var h, m, n int
	fmt.Fscan(in, &h, &m, &n)
	ht := newHashTable(h, m)
	ans := 0
	for i := 0; i < n; i++ {
		var cmd string
		fmt.Fscan(in, &cmd)
		switch cmd {
		case "+":
			var id, hash int
			fmt.Fscan(in, &id, &hash)
			ans += ht.add(id, hash)
		case "-":
			var id int
			fmt.Fscan(in, &id)
			ht.rem(id)
		default:
			panic(cmd)
		}
	}
	fmt.Println(ans)
}

type ptr struct {
	chain, i int
}

// Maždaug segment tree.
type chain []int

type hashTable struct {
	ptrs   []ptr
	chains []chain
	byID   map[int]ptr
}

func newHashTable(h, m int) *hashTable {
	r := &hashTable{ptrs: make([]ptr, h), byID: make(map[int]ptr)}
	for i := range r.ptrs {
		r.ptrs[i].chain = -1
	}
	for i := 0; i < m; i++ {
		if r.ptrs[i].chain != -1 {
			continue
		}
		sz := 0
		for j := i; r.ptrs[j].chain == -1; j = (j + m) % h {
			r.ptrs[j].chain = len(r.chains)
			r.ptrs[j].i = sz
			sz++
		}
		r.chains = append(r.chains, make(chain, 4*sz))
	}
	return r
}

func (h *hashTable) add(id, hash int) int {
	p := h.ptrs[hash]
	ch := h.chains[p.chain]
	q := ch.findFirstZero(p.i)
	ch.add(q, 1)
	h.byID[id] = ptr{p.chain, q}
	sz := len(ch) / 4
	return (q + sz - p.i) % sz
}

func (h *hashTable) rem(id int) {
	p := h.byID[id]
	h.chains[p.chain].add(p.i, -1)
}

func (ch chain) findFirstZero(i int) int {
	r := ch.ffz(i, 1, 0, len(ch)/4)
	if r == len(ch)/4 {
		return ch.ffz(0, 1, 0, len(ch)/4)
	}
	return r
}

func (ch chain) ffz(i, v, l, r int) int {
	if ch[v] == r-l {
		return r
	}
	if l + 1 == r {
		return l
	}
	m := (l + r) / 2
	if i < m {
		i := ch.ffz(i, v*2, l, m)
		if i < m {
			return i
		}
	}
	return ch.ffz(i, v*2+1, m, r)
}

func (ch chain) add(i, val int) {
	v := 1
	l := 0
	r := len(ch) / 4
	for l < r {
		ch[v] += val
		if l + 1 == r {
			break
		}
		m := (l + r) / 2
		if i < m {
			r = m
			v *= 2
		} else {
			l = m
			v = v*2 + 1
		}
	}
}


The year of 2012 is coming...According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil.The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: a for Mephisto, b for Diablo and c for Baal.Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a  of experience, rounded down, where x will be the amount of experience for the killed megaboss and y — the number of people in the team.Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum.It is known that some heroes like others. But if hero p likes hero q, this does not mean that the hero q likes hero p. No hero likes himself.The total amount of liking in teams is the amount of ordered pairs (p, q), such that heroes p and q are in the same group, and hero p likes hero q (but it is not important if hero q likes hero p). In case of heroes p and q likes each other and they are in the same group, this pair should be counted twice, as (p, q) and (q, p).A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team.It is guaranteed that every hero is able to destroy any megaboss alone.

The first line contains a single non-negative integer n (0 ≤ n ≤ 42) — amount of liking between the heroes. Next n lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p  ≠  q). Every liking is described in the input exactly once, no hero likes himself. In the last line are given three integers a, b and c (1 ≤ a, b, c ≤ 2·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal. In all the pretests, except for examples from the statement, the following condition is satisfied: a = b = c.

Print two integers — the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team). When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking.

3
Troll likes Dracul
Dracul likes Anka
Snowy likes Hexadecimal
210 200 180

2
Anka likes Chapay
Chapay likes Anka
10000 50 50

NoteA note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo и Chapay.

package main

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

var r *bufio.Reader = bufio.NewReader(os.Stdin)
var w *bufio.Writer = bufio.NewWriter(os.Stdout)

const MAX = 2000000000 - 1
const N = 7
var h = map[string]int{
        "Anka": 0,
        "Chapay": 1,
        "Cleo": 2,
        "Troll": 3,
        "Dracul": 4,
        "Snowy": 5,
        "Hexadecimal": 6,
}

func diffExp(a ...int) int {
        mn, mx := a[0], a[0]
        for _, v := range a {
                if v < mn {
                        mn = v
                }
                if v > mx {
                        mx = v
                }
        }

        return mx - mn
}

var x, y, z int
var f [N]int
var l [N][N]int
var mn = MAX
var li = 0
func solve(n, a, b, c int) {
        if n == N {
                if a == 0 || b == 0 || c == 0 {
                        return
                }

                axp, bxp, cxp := x/a, y/b, z/c
                curMn := diffExp(axp, bxp, cxp)
                if curMn > mn {
                             return
                }

                curLi := 0
                for i := 0; i < N - 1; i++ {
                        for j := i + 1; j < N; j++ {
                                if f[i] == f[j] {
                                        curLi += l[i][j] + l[j][i]
                                }
                        }
                }
                if curMn < mn || curLi > li {
                        mn = curMn
                        li = curLi
                }

                return
        }

        f[n] = 1
        solve(n+1, a+1, b, c)
        f[n] = 2
        solve(n+1, a, b+1, c)
        f[n] = 3
        solve(n+1, a, b, c+1)

        return
}

func main() {
        defer w.Flush()

        var n int
        fmt.Fscanf(r, "%d\n", &n)
        var s1, s2 string
        for i := 0; i < n; i++ {
                fmt.Fscanf(r, "%s likes %s\n", &s1, &s2)
                l[h[s1]][h[s2]] = 1
        }
        fmt.Fscanf(r, "%d %d %d\n", &x, &y, &z)

        solve(0, 0, 0, 0)
        fmt.Fprintf(w, "%d %d", mn, li)
}

package main

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

var r *bufio.Reader = bufio.NewReader(os.Stdin)
var w *bufio.Writer = bufio.NewWriter(os.Stdout)

const MAX = 2000000000 - 1
const N = 7
var h = map[string]int{
        "Anka": 0,
        "Chapay": 1,
        "Cleo": 2,
        "Troll": 3,
        "Dracul": 4,
        "Snowy": 5,
        "Hexadecimal": 6,
}

func diffExp(a ...int) int {
        mn, mx := a[0], a[0]
        for _, v := range a {
                if v < mn {
                        mn = v
                }
                if v > mx {
                        mx = v
                }
        }

        return mx - mn
}

var x, y, z int
var f [N]int
var l [N][N]int
var mn = MAX
var li = 0
func solve(n, a, b, c int) {
        if n == N {
                if a == 0 || b == 0 || c == 0 {
                        return
                }

                axp, bxp, cxp := x/a, y/b, z/c
                curMn := diffExp(axp, bxp, cxp)
                if curMn > mn {
                             return
                }

                if curMn < mn {
                        mn = curMn
                }
                curLi := 0
                for i := 0; i < N - 1; i++ {
                        for j := i + 1; j < N; j++ {
                                if f[i] == f[j] {
                                        curLi += l[i][j] + l[j][i]
                                }
                        }
                }
                if curLi > li {
                        li = curLi
                }

                return
        }

        f[n] = 1
        solve(n+1, a+1, b, c)
        f[n] = 2
        solve(n+1, a, b+1, c)
        f[n] = 3
        solve(n+1, a, b, c+1)
        f[n] = 0

        return
}

func main() {
        defer w.Flush()

        var n int
        fmt.Fscanf(r, "%d\n", &n)
        var s1, s2 string
        for i := 0; i < n; i++ {
                fmt.Fscanf(r, "%s likes %s\n", &s1, &s2)
                l[h[s1]][h[s2]] = 1
        }
        fmt.Fscanf(r, "%d %d %d\n", &x, &y, &z)

        solve(0, 0, 0, 0)
        fmt.Fprintf(w, "%d %d", mn, li)
}