Let's denote as  the number of bits set ('1' bits) in the binary representation of the non-negative integer x.You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, and  is maximum possible. If there are multiple such numbers find the smallest of them.

The first line contains integer n — the number of queries (1 ≤ n ≤ 10000). Each of the following n lines contain two integers li, ri — the arguments for the corresponding query (0 ≤ li ≤ ri ≤ 1018).

For each query print the answer in a separate line.

package main

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

func scanInt64(scanner *bufio.Scanner) int64 {
	scanner.Scan()
	x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return x
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	pow := make([]int64, 61)
	x := int64(1)
	for i := range pow {
		pow[i] = x
		x *= 2
	}
	n := int(scanInt64(scanner))
	for i := 0; i < n; i++ {
		min, max := scanInt64(scanner), scanInt64(scanner)
		best := int64(0)
		for m := 60; m != 0; m-- {
			count := m
			x := int64(0)
			pos := 60
			for {
				for pos != -1 && pow[pos] > min-x {
					pos--
				}
				if pos == -1 {
					break
				}
				x += pow[pos]
				count--
			}
			//writer.WriteString(fmt.Sprintf("m = %d, x = %d, count = %d\n",
			//		m, x, count))
			for pos = 0; count != 0; pos++ {
				if x&pow[pos] == 0 {
					x += pow[pos]
					count--
				}
			}
			//writer.WriteString(fmt.Sprintf("m = %d, x = %d, count = %d\n",
			//		m, x, count))
			if x >= min && x <= max {
				best = x
				break
			}
		}
		writer.WriteString(fmt.Sprintf("%d\n", best))
	}
}

package main

import (
    "strconv"
    "bufio"
    "os"
    "fmt"
)
// var file, _ = os.Open("input.txt")
// var outfile, _ = os.Create("output.txt")
// var in = bufio.NewScanner(file)
// var out = bufio.NewWriter(outfile)
var in = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

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

func nextInt() int {
    ret, _ := strconv.Atoi(next())
    return ret
}

func nextInt64() int64 {
    ret, _ := strconv.ParseInt(next(), 10, 64)
    return ret
}

func nextFloat() float64 {
    ret, _ := strconv.ParseFloat(next(), 64)
    return ret
}

func main() {
    defer out.Flush()
    in.Split(bufio.ScanWords)
    t := nextInt()
    for k := 0; k < t; k++ {
        l := nextInt64()
        r := nextInt64()
        if r & (r + 1) == 0 {
            fmt.Fprintln(out, r)
            continue
        }
        var a int64
        a = 1
        for a - 1 < r {
            a = a << 1
        }
        a--
        if a >= l && a <= r {
            fmt.Fprintln(out, a)
        } else {
            var x uint
            for l <= r {
                l = l | (1 << x)
                x++
            }
            x--
            l = l ^ (1 << x)
            l = l ^ 0
            fmt.Fprintln(out, l)
        }
    }
}

package main

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

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

func init() {
	in = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
}

func main() {
	defer out.Flush()
	var n int
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {
		var l, r int64
		fmt.Fscan(in, &l, &r)
		x := l
		for j := uint(0); x|(1<<j) <= r; j++ {
			x |= 1 << j
		}
		fmt.Fprintln(out, x)
	}
}

package main

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

func pow484A(n int64) int64 {
	msb := uint(0)
	for n >>= 1; n != 0; n >>= 1 {
		msb++
	}
	return int64(1) << msb
}

func f484A(l, r int64, i int) int64 {
	if l == r || (r+1)&r == 0 {
		return r
	}
	if common := l & r; common < r&^common {
		return pow484A(r) - 1
	}
	for ; i >= 0; i-- {
		pow2 := int64(1) << uint(i)
		if l&pow2 > 0 && r&pow2 > 0 {
			return pow2 | f484A(l-pow2, r-pow2, i-1)
		}
	}
	panic("err")
}

func Sol484A(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	for Fscan(in, &n); n > 0; n-- {
		var l, r int64
		Fscan(in, &l, &r)
		Fprintln(out, f484A(l, r, 60))
	}
}

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

package main

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

func setBitNumber484A(n int64) int64 {
	if n == 0 {
		return 0
	}
	msb := uint(0)
	for n >>= 1; n != 0; n >>= 1 {
		msb++
	}
	return int64(1) << msb
}

func f484A(l, r int64, i int) int64 {
	if l == r || (r+1)&r == 0 {
		return r
	}
	if common := l & r; common < r&^common {
		return setBitNumber484A(r) - 1
	}
	for ; i >= 0; i-- {
		pow2 := int64(1) << uint(i)
		if l&pow2 > 0 && r&pow2 > 0 {
			return pow2 | f484A(l-pow2, r-pow2, i-1)
		}
	}
	panic("err")
}

func Sol484A(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	for Fscan(in, &n); n > 0; n-- {
		var l, r int64
		Fscan(in, &l, &r)
		Fprintln(out, f484A(l, r, 60))
	}
}

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

package main

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

func scanInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	x, _ := strconv.Atoi(scanner.Text())
	return x
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	pow := make([]int64, 61)
	x := int64(1)
	for i := range pow {
		pow[i] = x
		x *= 2
	}
	n := scanInt(scanner)
	for i := 0; i < n; i++ {
		min, max := int64(scanInt(scanner)), int64(scanInt(scanner))
		best := int64(0)
		for m := 60; m != 0; m-- {
			count := m
			x := int64(0)
			pos := 60
			for {
				for pos != -1 && pow[pos] > min-x {
					pos--
				}
				if pos == -1 {
					break
				}
				x += pow[pos]
				count--
			}
			//writer.WriteString(fmt.Sprintf("m = %d, x = %d, count = %d\n",
			//		m, x, count))
			for pos = 0; count != 0; pos++ {
				if x&pow[pos] == 0 {
					x += pow[pos]
					count--
				}
			}
			//writer.WriteString(fmt.Sprintf("m = %d, x = %d, count = %d\n",
			//		m, x, count))
			if x >= min && x <= max {
				best = x
				break
			}
		}
		writer.WriteString(fmt.Sprintf("%d\n", best))
	}
}

package main

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

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

func init() {
	in = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
}

func main() {
	defer out.Flush()
	var n int
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {
		var l, r int64
		fmt.Fscan(in, &l, &r)
		x := l
		for j := uint(0); x|(1<<j) < r; j++ {
			x |= 1 << j
		}
		fmt.Fprintln(out, x)
	}
}

package main

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

func Sol484A(reader io.Reader, writer io.Writer) {
	setBitNumber := func(n int64) int64 {
		if n == 0 {
			return 0
		}
		msb := uint(0)
		for n >>= 1; n != 0; n >>= 1 {
			msb++
		}
		return int64(1) << msb
	}

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	for Fscan(in, &n); n > 0; n-- {
		var l, r int64
		Fscan(in, &l, &r)
		if l == r || (r+1)&r == 0 {
			Fprintln(out, r)
			continue
		}
		if common := l & r; common < r&^common {
			Fprintln(out, setBitNumber(r)-1)
			continue
		}
		found := false
		base := int64(0)
		for i := 60; i >= 0; i-- {
			pow2 := int64(1) << uint(i)
			if l&pow2 > 0 && r&pow2 > 0 {
				found = true
				base |= pow2
			} else if found {
				break
			}
		}
		r &^= base
		if r == 1 {
			Fprintln(out, base|r)
		} else {
			Fprintln(out, base|(setBitNumber(r)-1))
		}
	}
}

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

package main

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

func Sol484A(reader io.Reader, writer io.Writer) {
	setBitNumber := func(n int64) int64 {
		if n == 0 {
			return 0
		}
		msb := uint(0)
		for n >>= 1; n != 0; n >>= 1 {
			msb++
		}
		return int64(1) << msb
	}

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	for Fscan(in, &n); n > 0; n-- {
		var l, r int64
		Fscan(in, &l, &r)
		if l == r || (r+1)&r == 0 {
			Fprintln(out, r)
			continue
		}
		common := l & r
		if common < r&^common {
			Fprintln(out, setBitNumber(r)-1)
			continue
		}
		found := false
		base := int64(0)
		for i := 60; i >= 0; i-- {
			pow2 := int64(1) << uint(i)
			if l&pow2 > 0 && r&pow2 > 0 {
				found = true
				base |= pow2
			} else if found {
				break
			}
		}
		r &^= base
		Fprintln(out, base|(setBitNumber(r)-1))
	}
}

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


The Kingdom of Kremland is a tree (a connected undirected graph without cycles) consisting of $$$n$$$ vertices. Each vertex $$$i$$$ has its own value $$$a_i$$$. All vertices are connected in series by edges. Formally, for every $$$1 \leq i < n$$$ there is an edge between the vertices of $$$i$$$ and $$$i+1$$$.Denote the function $$$f(l, r)$$$, which takes two integers $$$l$$$ and $$$r$$$ ($$$l \leq r$$$):    We leave in the tree only vertices whose values range from $$$l$$$ to $$$r$$$.    The value of the function will be the number of connected components in the new graph. Your task is to calculate the following sum: $$$$$$\sum_{l=1}^{n} \sum_{r=l}^{n} f(l, r) $$$$$$

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of vertices in the tree. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$) — the values of the vertices.

Print one number — the answer to the problem.

NoteIn the first example, the function values will be as follows:   $$$f(1, 1)=1$$$ (there is only a vertex with the number $$$2$$$, which forms one component)  $$$f(1, 2)=1$$$ (there are vertices $$$1$$$ and $$$2$$$ that form one component)  $$$f(1, 3)=1$$$ (all vertices remain, one component is obtained)  $$$f(2, 2)=1$$$ (only vertex number $$$1$$$)  $$$f(2, 3)=2$$$ (there are vertices $$$1$$$ and $$$3$$$ that form two components)  $$$f(3, 3)=1$$$ (only vertex $$$3$$$)  Totally out $$$7$$$.In the second example, the function values will be as follows:   $$$f(1, 1)=1$$$  $$$f(1, 2)=1$$$  $$$f(1, 3)=1$$$  $$$f(1, 4)=1$$$  $$$f(2, 2)=1$$$  $$$f(2, 3)=2$$$  $$$f(2, 4)=2$$$  $$$f(3, 3)=1$$$  $$$f(3, 4)=1$$$  $$$f(4, 4)=0$$$ (there is no vertex left, so the number of components is $$$0$$$)  Totally out $$$11$$$.

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n := 0
	fmt.Fscanf(in, "%d %d %d\n", &n)
	a := make([]int, n)
	ans := int64(0)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &a[i])
		if i == 0 {
			ans += int64(a[i]) * int64(n-a[i]+1)
		} else {
			ans += calculate(a[i-1], a[i], n)
		}
		//fmt.Println(ans)
	}
	fmt.Fprintf(out, "%d\n", ans)
}

func calculate(a, b, n int) int64 {
	if a == b {
		return 0
	}
	if a > b {
		return int64(b) * int64(a-b)
	}
	return int64(b-a) * int64(n-b+1)
}


A team of students from the city S is sent to the All-Berland Olympiad in Informatics. Traditionally, they go on the train. All students have bought tickets in one carriage, consisting of n compartments (each compartment has exactly four people). We know that if one compartment contain one or two students, then they get bored, and if one compartment contain three or four students, then the compartment has fun throughout the entire trip.The students want to swap with other people, so that no compartment with students had bored students. To swap places with another person, you need to convince him that it is really necessary. The students can not independently find the necessary arguments, so they asked a sympathetic conductor for help. The conductor can use her life experience to persuade any passenger to switch places with some student.However, the conductor does not want to waste time persuading the wrong people, so she wants to know what is the minimum number of people necessary to persuade her to change places with the students. Your task is to find the number. After all the swaps each compartment should either have no student left, or have a company of three or four students.

The first line contains integer n (1 ≤ n ≤ 106) — the number of compartments in the carriage. The second line contains n integers a1, a2, ..., an showing how many students ride in each compartment (0 ≤ ai ≤ 4). It is guaranteed that at least one student is riding in the train.

If no sequence of swapping seats with other people leads to the desired result, print number "-1" (without the quotes). In another case, print the smallest number of people you need to persuade to swap places.

package main

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

func main() {
	in := bufio.NewReaderSize(os.Stdin, 2<<16)
	var n int
	fmt.Fscan(in, &n)
	cnt := make([]int, 5)
	for i := 0; i < n; i++ {
		var a int
		fmt.Fscan(in, &a)
		cnt[a]++
	}

	ans := 0
	for cnt[1] > 0 && cnt[2] > 0 {
		cnt[1]--
		cnt[2]--
		cnt[3]++
		ans++
	}
	for cnt[1] > 2 {
		cnt[1] -= 3
		cnt[3]++
		ans += 2
	}
	switch cnt[1] {
	case 2:
		if cnt[3] >= 2 {
			cnt[1] -= 2
			cnt[3] -= 2
			cnt[4] += 2
			ans += 2
		} else {
			cnt[1] -= 2
			cnt[2]++
			ans++
		}
	case 1:
		if cnt[3] >= 1 {
			cnt[1]--
			cnt[3]--
			cnt[4]++
			ans++
		} else if cnt[4] >= 2 {
			cnt[1]--
			cnt[4] -= 2
			cnt[3]++
			ans += 2
		}
	}

	for cnt[2] >= 3 {
		cnt[2] -= 3
		cnt[3] += 2
		ans += 2
	}

	if cnt[2] == 2 {
		cnt[2] -= 2
		cnt[4]++
		ans += 2
	}

	if cnt[2] == 1 {
		if cnt[4] > 0 {
			cnt[2]--
			cnt[4]--
			cnt[3] += 2
			ans++
		} else if cnt[3] >= 2 {
			cnt[2]--
			cnt[3] -= 2
			cnt[4] += 2
			ans += 2
		}
	}

	if cnt[1] > 0 || cnt[2] > 0 {
		fmt.Println(-1)
	} else {
		fmt.Println(ans)
	}
}

package main

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

func main() {
	in := bufio.NewReaderSize(os.Stdin, 2<<16)
	var n int
	fmt.Fscan(in, &n)
	cnt := make([]int, 5)
	for i := 0; i < n; i++ {
		var a int
		fmt.Fscan(in, &a)
		cnt[a]++
	}

	ans := 0
	for cnt[1] > 0 && cnt[2] > 0 {
		cnt[1]--
		cnt[2]--
		cnt[3]++
		ans++
	}
	for cnt[1] > 2 {
		cnt[1] -= 3
		cnt[3]++
		ans += 2
	}
	for cnt[1] > 0 && cnt[3] > 0 {
		cnt[1]--
		cnt[3]--
		cnt[4]++
		ans++
	}

	for cnt[1] > 1 {
		cnt[1] -= 2
		cnt[2]++
		ans++
	}

	for cnt[2] > 0 && cnt[4] > 0 {
		cnt[2]--
		cnt[4]--
		cnt[3] += 2
		ans++
	}

	if cnt[1] > 0 || cnt[2] > 0 {
		fmt.Println(-1)
	} else {
		fmt.Println(ans)
	}
}


Let $$$LCM(x, y)$$$ be the minimum positive integer that is divisible by both $$$x$$$ and $$$y$$$. For example, $$$LCM(13, 37) = 481$$$, $$$LCM(9, 6) = 18$$$.You are given two integers $$$l$$$ and $$$r$$$. Find two integers $$$x$$$ and $$$y$$$ such that $$$l \le x < y \le r$$$ and $$$l \le LCM(x, y) \le r$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases. Each test case is represented by one line containing two integers $$$l$$$ and $$$r$$$ ($$$1 \le l < r \le 10^9$$$).

For each test case, print two integers:   if it is impossible to find integers $$$x$$$ and $$$y$$$ meeting the constraints in the statement, print two integers equal to $$$-1$$$;  otherwise, print the values of $$$x$$$ and $$$y$$$ (if there are multiple valid answers, you may print any of them).

package main

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

func main() {
	var t, l, r int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &l, &r)
		if l*2 <= r {
			fmt.Println(l, l*2)
		} else {
			fmt.Println(-1, -1)
		}
	}
}

package main

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

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

	T:= 0
	for Fscan(in, &T); T > 0; T-- {
		var l,r int
		Fscan(in, &l,&r);
		if 2*l<=r{
			Fprintln(out,l,2*l)
		} else {
			Fprintln(out,-1,-1)
		}
	}
}

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

package main

import "fmt"

func main(){
	var testCnt int
	var l, r int
	fmt.Scanf("%d\n",&testCnt)
	for i:=0; i<testCnt; i++ {
		fmt.Scanf("%d %d\n",&l,&r)
		if 2*l > r{
			fmt.Printf("%d %d\n",-1,-1)
		}else {
			fmt.Printf("%d %d\n",l,2*l)
		}
	}
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	for t := io.nextInt(); t > 0; t-- {
		l, r := io.nextInt(), io.nextInt()
		x, y := -1, -1
		if 2*l <= r {
			x, y = l, 2*l
		}
		io.println(x, y)
	}
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	buffer := make([]byte, 1024*1024)
	scanner.Buffer(buffer, 3*1024*1024)
	scanner.Split(bufio.ScanWords)

	input := func() uint64 {
		scanner.Scan()
		i, _ := strconv.ParseUint(scanner.Text(), 10, 64)
		return i
	}

	for t := input(); t > 0; t-- {
		l, r := input(), input()

		if 2*l > r {
			fmt.Println(-1, -1)
		} else {
			fmt.Println(l, 2*l)
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var t, x, y int;
	fmt.Scanf("%d\n", &t);
	for i := 0; i < t; i++ {
		fmt.Scanf("%d %d\n", &x, &y);
		a := x;
		b := 2 * x;
		if b > y {
			a = -1;
			b = -1;
		}
		fmt.Printf("%d %d\n", a, b);
	}
}

package main

import "fmt"

func main() {
	var i, t, x, y, a, b int;
	fmt.Scanf("%d\n", &t);
	for i = 0; i < t; i++ {
		fmt.Scanf("%d %d\n", &x, &y);
		a = x;
		b = 2 * x;
		if b > y {
			a = -1;
			b = -1;
		}
		fmt.Printf("%d %d\n", a, b);
	}
}

/*
URL:
https://codeforces.com/contest/1389/problem/A
*/

package main

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

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

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

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

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

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

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

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

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

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

var (
	t int

	l, r int64
)

func main() {
	t = ReadInt()

	for i := 0; i < t; i++ {
		l, r = ReadInt64_2()

		solve()
	}
}

func solve() {
	x := l
	for i := int64(2); ; i++ {
		y := x * i
		lcmxy := Lcm(x, y)
		if l <= lcmxy && lcmxy <= r {
			fmt.Println(x, y)
			return
		}

		if lcmxy > r {
			break
		}
	}
	fmt.Println(-1, -1)
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int64) int64 {
	if a < 0 || b < 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	if a == 0 {
		return b
	}
	if b == 0 {
		return a
	}

	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int64) int64 {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

package main

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

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

	var T, l, r int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &l, &r)
		if 2*l > r {
			Fprintln(out, -1, -1)
		} else {
			Fprintln(out, l, 2*l)
		}
	}
}

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

package main

import "fmt"

func main() {
	var t int
	fmt.Scan(&t)
	for i := 1; i <= t; i++ {
		var l, r int
		fmt.Scan(&l, &r)
		if r < 2*l {
			fmt.Println("-1 -1")
			continue
		}
		fmt.Printf("%d %d\n", l, 2*l)
	}
}

package main

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

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

	var t int

	fmt.Fscan(input, &t)

	for z := 0; z < t; z++ {
		var l, r int
		fmt.Fscan(input, &l, &r)

		if l*2 > r {
			fmt.Println("-1 -1")
		} else {
			fmt.Printf("%d %d\n", l, l*2)
		}

	}
}

package main

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

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

	var t, l, r int

	fmt.Fscan(input, &t)

	for z := 0; z < t; z++ {
		fmt.Fscan(input, &l, &r)

		if l*2 > r {
			fmt.Println("-1 -1")
		} else {
			fmt.Printf("%d %d\n", l, l*2)
		}

	}
}

package main

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

func solve() {
	x, y := readInt64(), readInt64()
	if x*2 <= y {
		fmt.Println(x, x*2)
	} else {
		fmt.Println(-1, -1)
	}
}

func main() {
	k := readInt()
	for i := 0; i < k; i++ {
		solve()
	}
}

// sort ------------------------------------------------------------

type xxx struct {
	x int
}

type sortArray []xxx

func (s sortArray) Len() int           { return len(s) }
func (s sortArray) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
func (s sortArray) Less(i, j int) bool { return s[i].x < s[j].x }

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

var (
	readString func() string
	readBytes  func() []byte
)

func init() {
	log.SetFlags(log.Lshortfile)
	readString, readBytes = newReadString(os.Stdin)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Bytes()
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

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

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

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

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

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

func sum(a []int) int {
	var ret int
	for i := range a {
		ret += a[i]
	}
	return ret
}

func sumFloat64(a []float64) float64 {
	var ret float64
	for i := range a {
		ret += a[i]
	}
	return ret
}

func gcd(m, n int) int {
	for m%n != 0 {
		m, n = n, m%n
	}
	return n
}

func lcm(m, n int) int {
	return m / gcd(m, n) * n
}

package main

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

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

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

func main() {
	defer writer.Flush()

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

	for k := 0; k < t; k++ {
		var l, r uint64
		scanf("%d %d\n", &l, &r)

		x := l
		y := x + x

		if y <= r {
			printf("%d %d\n", x, y)
		} else {
			printf("-1 -1\n")
		}
	}
}

package main

import "fmt"

func main() {
	var tt, x, y int
	fmt.Scan(&tt)
	for tt > 0 {
		fmt.Scan(&x, &y)
		if x*2 <= y {
			fmt.Printf("%d %d\n", x, x*2)
		} else {
			fmt.Println("-1 -1")
		}
		tt--
	}
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		var l, r int
		fmt.Scanf("%d %d\n", &l, &r)
		y := l * 2
		if y > r {
			fmt.Println("-1 -1")
		} else {
			fmt.Printf("%d %d\n", l, y)
		}
	}
}

package main

import  (
	"fmt"
)

func main () {
	var cases int
	fmt.Scanln(&cases)
	for i:=0 ;i<cases ;i++{
		var l,r int
		fmt.Scanln(&l,&r)
		if l*2 <= r {
			fmt.Println(l,2*l)
		} else {
			fmt.Println(-1,-1)
		}
	}
}

package main

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

var rw = newFastIO(os.Stdin, os.Stdout)

func main() {
	defer rw.Flush()
	nTest := 1
	nTest = rw.nextInt()
	for i := 0; i < nTest; i++ {
		solveCase()
	}
}

func solveCase() {
	l := rw.nextInt()
	r := rw.nextInt()
	if r >= l*2 {
		rw.printlnInts(l, l*2)
	} else {
		rw.printlnInts(-1, -1)
	}
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF = int64(math.MaxInt64)
	MOD = 1e9 + 7
)

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

func abs(a int64) int64 {
	if a < 0 {
		return a * -1
	}
	return a
}

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		l := sint64()
		r := sint64()
		x, y := sol(l, r)
		printf("%d %d\n", x, y)
	}
}

func sol(l, r int64) (int64, int64) {
	if 2*l > r {
		return -1, -1
	}
	return l, l * 2
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	var gcd func(i, j int) int
	gcd = func(i, j int) int {
		if j == 0 {
			return i
		}
		return gcd(j, i%j)
	}

	for t := io.nextInt(); t > 0; t-- {
		l, r := io.nextInt(), io.nextInt()
		x, y := -1, -1
		for i := l; i < l+10 && i <= r; i++ {
			for j := i + 1; j < l+10 && j <= r; j++ {
				lcm := int64(i) * int64(j) / int64(gcd(i, j))
				if lcm <= int64(r) {
					x, y = i, j
				}
			}
		}
		io.println(x, y)
	}
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

package main

import "fmt"

func main() {
	var x, y int;
	fmt.Scanf("%d %d", &x, &y);
	fmt.Printf("%d %d\n", x, x);
}

package main

import "fmt"

func main() {
	var i, t, x, y, a, b int;
	fmt.Scanf("%d", &t);
	for i = 0; i < t; i++ {
		fmt.Scanf("%d %d", &x, &y);
		a = x;
		b = 2 * x;
		if b > y {
			a = -1;
			b = -1;
		}
		fmt.Printf("%d %d\n", a, b);
	}
}

package main

import "fmt"

func main() {
	var i, t, x, y int;
	fmt.Scanf("%d", &t);
	for i = 0; i < t; i++ {
		fmt.Scanf("%d %d", &x, &y);
		fmt.Printf("%d %d\n", x, x);
	}
}

package main

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

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

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

// greatest common divisor (GCD) via Euclidean algorithm
func GCD(a, b int) int {
	for b != 0 {
		t := b
		b = a % b
		a = t
	}
	return a
}

// find Least Common Multiple (LCM) via GCD
func LCM(a, b int) int {
	return a * b / GCD(a, b)
}

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

func main() {
	defer writer.Flush()

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

	for k := 0; k < t; k++ {
		var l, r int
		scanf("%d %d\n", &l, &r)

		var f bool
		// printf("-> %d %d\n", l, r)

		for x := l; x < r; x++ {
			for y := x + 1; y <= r; y++ {
				lcm := LCM(x, y)
				// printf("check %d %d -> %d\n", x, y, lcm)
				// printf("%d", LCM(13, 37))
				if lcm >= l && lcm <= r {
					printf("%d %d\n", x, y)
					f = true
					break
				}
			}
			if f {
				break
			}
		}
		if !f {
			printf("-1 -1\n")
		}
	}
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		var l, r int
		fmt.Scanf("%d %d\n", &l, &r)
		y := l * 2
		if y > r {
			println("-1 -1\n")
		} else {
			fmt.Printf("%d %d\n", l, y)
		}
	}
}


Ann and Borya have n piles with candies and n is even number. There are ai candies in pile with number i.Ann likes numbers which are square of some integer and Borya doesn't like numbers which are square of any integer. During one move guys can select some pile with candies and add one candy to it (this candy is new and doesn't belong to any other pile) or remove one candy (if there is at least one candy in this pile). Find out minimal number of moves that is required to make exactly n / 2 piles contain number of candies that is a square of some integer and exactly n / 2 piles contain number of candies that is not a square of any integer.

First line contains one even integer n (2 ≤ n ≤ 200 000) — number of piles with candies. Second line contains sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 109) — amounts of candies in each pile.

Output minimal number of steps required to make exactly n / 2 piles contain number of candies that is a square of some integer and exactly n / 2 piles contain number of candies that is not a square of any integer. If condition is already satisfied output 0.

NoteIn first example you can satisfy condition in two moves. During each move you should add one candy to second pile. After it size of second pile becomes 16. After that Borya and Ann will have two piles with number of candies which is a square of integer (second and fourth pile) and two piles with number of candies which is not a square of any integer (first and third pile).In second example you should add two candies to any three piles.

package main

import (
  "fmt"
  //"strings"
  "sort"
  "bufio"
  "os"
  "strconv"
  "math"
)
var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func ReadInt32() int {
  scanner.Scan()
  ans, _ := strconv.Atoi(scanner.Text())
  return ans
}

func PrintInts(ints ...int) {
  for _, value := range ints {
    writer.WriteString(strconv.Itoa(value))
    writer.WriteByte(' ')
  }
}

var n, tmp, val int
var sq, nsq []int
var ans int64

func diff(x int) int {
  var y = int(math.Sqrt(float64(x)))
  var a = x - y * y
  var b = (y + 1) * (y + 1) - x
  if a < b {
    return a
  }
  return b
}

func main() {
  defer writer.Flush()
  scanner.Split(bufio.ScanWords)
  n = ReadInt32()
  for i := 0; i < n; i++ {
    val = ReadInt32()
    tmp = diff(val)
    if tmp == 0 {
      if val == 0 {
        sq = append(sq, 2)
      } else {
        sq = append(sq, 1)
      }
    } else {
      nsq = append(nsq, tmp)
    }
  }
  ans = 0
  sort.Sort(sort.IntSlice(sq));
  sort.Sort(sort.IntSlice(nsq));
  for i := 0; i < (n / 2 - len(sq)); i++ {
    ans += int64(nsq[i])
  }
  for i := 0; i < (n / 2 - len(nsq)); i++ {
    ans += int64(sq[i])
  }
  fmt.Print(ans)
}

package main

import (
  //"fmt"
  //"strings"
  "sort"
  "bufio"
  "os"
  "strconv"
  "math"
)
var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func ReadInt32() int {
  scanner.Scan()
  ans, _ := strconv.Atoi(scanner.Text())
  return ans
}

func PrintInts(ints ...int) {
  for _, value := range ints {
    writer.WriteString(strconv.Itoa(value))
    writer.WriteByte(' ')
  }
}

var n, tmp, val int
var sq, nsq []int

func diff(x int) int {
  var y = int(math.Sqrt(float64(x)))
  var a = x - y * y
  var b = (y + 1) * (y + 1) - x
  if a < b {
    return a
  }
  return b
}

func main() {
  defer writer.Flush()
  scanner.Split(bufio.ScanWords)
  n = ReadInt32()
  for i := 0; i < n; i++ {
    val = ReadInt32()
    tmp = diff(val)
    if tmp == 0 {
      if val == 0 {
        sq = append(sq, 2)
      } else {
        sq = append(sq, 1)
      }
    } else {
      nsq = append(nsq, tmp)
    }
  }
  var ans = 0
  sort.Sort(sort.IntSlice(sq));
  sort.Sort(sort.IntSlice(nsq));
  for i := 0; i < (n / 2 - len(sq)); i++ {
    ans += nsq[i]
  }
  for i := 0; i < (n / 2 - len(nsq)); i++ {
    ans += sq[i]
  }
  PrintInts(ans)
}


Masha has $$$n$$$ types of tiles of size $$$2 \times 2$$$. Each cell of the tile contains one integer. Masha has an infinite number of tiles of each type.Masha decides to construct the square of size $$$m \times m$$$ consisting of the given tiles. This square also has to be a symmetric with respect to the main diagonal matrix, and each cell of this square has to be covered with exactly one tile cell, and also sides of tiles should be parallel to the sides of the square. All placed tiles cannot intersect with each other. Also, each tile should lie inside the square. See the picture in Notes section for better understanding.Symmetric with respect to the main diagonal matrix is such a square $$$s$$$ that for each pair $$$(i, j)$$$ the condition $$$s[i][j] = s[j][i]$$$ holds. I.e. it is true that the element written in the $$$i$$$-row and $$$j$$$-th column equals to the element written in the $$$j$$$-th row and $$$i$$$-th column.Your task is to determine if Masha can construct a square of size $$$m \times m$$$ which is a symmetric matrix and consists of tiles she has. Masha can use any number of tiles of each type she has to construct the square. Note that she can not rotate tiles, she can only place them in the orientation they have in the input.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 100$$$, $$$1 \le m \le 100$$$) — the number of types of tiles and the size of the square Masha wants to construct. The next $$$2n$$$ lines of the test case contain descriptions of tiles types. Types of tiles are written one after another, each type is written on two lines.  The first line of the description contains two positive (greater than zero) integers not exceeding $$$100$$$ — the number written in the top left corner of the tile and the number written in the top right corner of the tile of the current type. The second line of the description contains two positive (greater than zero) integers not exceeding $$$100$$$ — the number written in the bottom left corner of the tile and the number written in the bottom right corner of the tile of the current type. It is forbidden to rotate tiles, it is only allowed to place them in the orientation they have in the input.

For each test case print the answer: "YES" (without quotes) if Masha can construct the square of size $$$m \times m$$$ which is a symmetric matrix. Otherwise, print "NO" (withtout quotes).

NoteThe first test case of the input has three types of tiles, they are shown on the picture below.  Masha can construct, for example, the following square of size $$$4 \times 4$$$ which is a symmetric matrix:

package main

import "fmt"

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		var n, m int
		fmt.Scanf("%d %d\n", &n, &m)
		possible := false
		for j := 0; j < n; j++ {
			var a, b, c, d int
			fmt.Scanf("%d %d\n%d %d\n", &a, &b, &c, &d)
			if b == c {
				possible = true
			}
		}
		if possible && m%2 == 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

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

	var T, n, m, x1, y1, x2, y2 int
	fmt.Fscan(r, &T)
	for t := 0; t < T; t++ {
		fmt.Fscan(r, &n, &m)
		b := false
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &x1, &y1)
			fmt.Fscan(r, &x2, &y2)
			if !b && y1 == x2 {
				b = true
			}
		}
		if m % 2 != 0 {
			fmt.Fprint(w, "NO\n")
			continue
		}
		if b {
			fmt.Fprint(w, "YES\n")
		}else {
			fmt.Fprint(w, "NO\n")
		}
	}
}

package main

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

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

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

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

func scan(a ...interface{}) (n int, err error) {
	return fmt.Fscan(in, a...)
}

var err error

func next() (n string) {
	scan(&n)
	return n
}

func nextInt() (n int) {
	scan(&n)
	return n
}

func nextLong() (n int64) {
	scan(&n)
	return n
}

func nextDouble() (n float64) {
	scan(&n)
	return n
}

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

type block struct {
	a, b, c, d int
}

func solve() {
	n := nextInt()
	m := nextInt()
	arr := make([]block, n)
	for i, _ := range arr {
		arr[i] = block{nextInt(), nextInt(), nextInt(), nextInt()}
	}
	if m % 2 == 1 {
		printf("NO\n")
		return
	}
	flag1 := false
	for _, v := range arr {
		if v.b == v.c {
			flag1 = true
		}
	}
		if flag1 {
			printf("YES\n")
		} else {
			printf("NO\n")
		}
		return

}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)

	defer out.Flush()

	for t := nextInt(); t > 0; t-- {
		solve()
	}
}

package main

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

func solve(input io.Reader, output io.Writer) {
	var result string
	var t, n, m int
	var a11, a12, a21, a22 int
	fmt.Fscanf(input, "%d\n", &t)
	for i := 0; i < t; i++ {
		fmt.Fscanf(input, "%d %d\n", &n, &m)
		result = "NO"
		for j := 0; j < n; j++ {
			fmt.Fscanf(input, "%d %d\n", &a11, &a12)
			fmt.Fscanf(input, "%d %d\n", &a21, &a22)

			if a12 == a21 && m % 2 == 0 {
				result = "YES"
			}
		}
 		fmt.Fprintln(output, result)
	}
}

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

package main

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

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

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

func solve() {
	var n, m int
	scanf("%d %d\n", &n, &m)
	issym := false

	for i := 0; i < n; i++ {
		var a, b, c, d int
		scanf("%d %d\n%d %d\n", &a, &b, &c, &d)
		issym = issym || (b == c)
	}
	issym = issym && m % 2 == 0
	if issym {
		printf("%s\n", "YES")
	} else {
		printf("%s\n", "NO")
	}
}

func main() {
	defer writer.Flush()
	var t int
	scanf("%d\n", &t)
	for ; t > 0; t-- {
		solve()
	}
}

package main

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

func main() {
	t := ReadInt()
	for i := 0; i < t; i++ {
		n, m := ReadInt64(), ReadInt64()
		matrix := make([][]int64, n)
		for i := int64(0); i < n; i++ {
			matrix[i] = ReadInt64s(4)
		}
		if solve(n, m, matrix) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func solve(n, m int64, matrix [][]int64) bool {
	if m%2 == 1 {
		return false
	}
	for i := 0; i < len(matrix); i++ {
		if matrix[i][1] == matrix[i][2] {
			return true
		}
	}
	return false
}

var reader = bufio.NewReader(os.Stdin)

func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(reader, a...); err != nil {
		panic(err)
	}
}
func ReadString() (s string) { Scan(&s); return }
func ReadInt() (i int)       { Scan(&i); return }
func ReadInts(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		Scan(&v[i])
	}
	return v
}
func ReadInt64() (i int64) { Scan(&i); return }
func ReadInt64s(n int) []int64 {
	v := make([]int64, n)
	for i := 0; i < n; i++ {
		Scan(&v[i])
	}
	return v
}

package main

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

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

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

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

func scan(a ...interface{}) (n int, err error) {
	return fmt.Fscan(in, a...)
}

var err error

func next() (n string) {
	scan(&n)
	return n
}

func nextInt() (n int) {
	scan(&n)
	return n
}

func nextLong() (n int64) {
	scan(&n)
	return n
}

func nextDouble() (n float64) {
	scan(&n)
	return n
}

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

// ==================== 以上是模版 ====================
func solve() {
	n := nextInt()
	m := nextInt()
	//fmt.Printf("n:%d,m:%d\n", n, m)
	ok := 0
	for ; n > 0; n-- {
		_, b, c, _ := nextInt(), nextInt(), nextInt(), nextInt()
		//fmt.Printf("b:%d,c:%d\n", b, c)
		if b == c {
			ok = 1
		}
	}
	if ok == 1 && m%2 == 0 {
		printf("YES\n")
	} else {
		printf("NO\n")
	}
}

func getIn() *bufio.Reader {
	x := `6
3 4
1 2
5 6
5 7
7 4
8 9
9 8
2 5
1 1
1 1
2 2
2 2
1 100
10 10
10 10
1 2
4 5
8 4
2 2
1 1
1 1
1 2
3 4
1 2
1 1
1 1`
	return bufio.NewReaderSize(strings.NewReader(x), 8192)
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	//in = getIn()
	// 要求： /  , 至少有一个相同的
	// 要求：

	out = bufio.NewWriterSize(os.Stdout, 8192)
	defer out.Flush()

	for t := nextInt(); t > 0; t-- {
		solve()
	}

}

package main
import."fmt"

// github.com/EndlessCheng/codeforces-go
func main() {
	var T, n, m, a, b, c, d int
	for Scan(&T); T > 0; T-- {
		ok := false
		for Scan(&n, &m); n > 0; n-- {
			Scan(&a, &b, &c, &d)
			ok = ok || b == c
		}
		if ok && m&1 == 0 {
			Println("YES")
		} else {
			Println("NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

func print( a ...interface{}) { fmt.Fprint(w, a...) }
func scan( a ...interface{})  { fmt.Fscan(r, a...) }

//for i := 0; i < n; i++  {

func main() {
	defer w.Flush()
	var q int
	scan(&q)
	for ; q > 0; q-- {
		var n int
		scan(&n)
		var m int
		scan(&m)
		var bl = false
		if m % 2 != 0 {
			print("NO\n")
			for i := 0; i < n; i++ {
				var a, b, c, d int
				scan(&a, &b, &c, &d)
			}
		}else{
			for i := 0; i < n; i++ {
				var a, b, c, d int
				scan(&a, &b, &c, &d)
				if !bl && b == c {
					bl = true
				}
			}
			if bl {
				print("YES\n")
			}else {
				print("NO\n")
			}
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var t, n, m, a, b, c, d int
	fmt.Scan(&t)
	for t != 0 {
		var matrix []([2][2]int) = make([]([2][2]int), 0)
		fmt.Scan(&n, &m)
		for i := 0; i < n; i++ {
			fmt.Scan(&a, &b)
			fmt.Scan(&c, &d)
			var x [2][2]int
			x[0][0] = a
			x[0][1] = b
			x[1][0] = c
			x[1][1] = d
			matrix = append(matrix, x)
		}
		if m%2 != 0 {
			fmt.Println("NO")
		} else {
			okay := false
			for i := 0; i < n; i++ {
				b := matrix[i][0][1]
				c := matrix[i][1][0]
				okay = okay || (b == c)
			}
			if okay {
				fmt.Println("YES")
			} else {
				fmt.Println("NO")
			}
		}
		t--
	}
}

package main

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

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

func main() {
	defer writer.Flush()
	var testCaseNum int
	fmt.Fscan(reader, &testCaseNum)

	for testCase := 0; testCase < testCaseNum; testCase++ {
		available := false
		var n, m int
		fmt.Fscan(reader, &n, &m)

		var a, b, c, d int
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a, &b, &c, &d)
			if b == c {
				available = true
			}
		}

		if !available || m&1 == 1 {
			writer.WriteString("NO\n")
		} else {
			writer.WriteString("YES\n")
		}
	}
}

package main

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

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

	var T, n, m, x1, y1, x2, y2 int
	fmt.Fscan(r, &T)
	for t := 0; t < T; t++ {
		fmt.Fscan(r, &n, &m)
		b1, b2 := false, false
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &x1, &y1)
			fmt.Fscan(r, &x2, &y2)
			if !b1 && x1 == y2 {
				b1 = true

			}
			if !b2 && y1 == x2 {
				b2 = true
			}
		}
		if m % 2 != 0 {
			fmt.Fprint(w, "NO\n")
			continue
		}
		if b1 && b2 {
			fmt.Fprint(w, "YES\n")
		}else {
			fmt.Fprint(w, "NO\n")
		}
	}
}

package main

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

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

	var T, n, m, x1, y1, x2, y2 int
	fmt.Fscan(r, &T)
	for t := 0; t < T; t++ {
		fmt.Fscan(r, &n, &m)
		b1, b2 := false, false
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &x1, &y1)
			fmt.Fscan(r, &x2, &y2)
			if x1 == y2 {
				b1 = true
			}
			if y1 == x2 {
				b2 = true
			}
		}
		if m % 2 != 0 {
			fmt.Fprint(w, "NO\n")
			continue
		}
		if b1 && b2 {
			fmt.Fprint(w, "YES\n")
		}else {
			fmt.Fprint(w, "NO\n")
		}
	}
}

package main

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

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

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

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

func scan(a ...interface{}) (n int, err error) {
	return fmt.Fscan(in, a...)
}

var err error

func next() (n string) {
	scan(&n)
	return n
}

func nextInt() (n int) {
	scan(&n)
	return n
}

func nextLong() (n int64) {
	scan(&n)
	return n
}

func nextDouble() (n float64) {
	scan(&n)
	return n
}

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

type block struct {
	a, b, c, d int
}

func solve() {
	n := nextInt()
	m := nextInt()
	arr := make([]block, n)
	for i, _ := range arr {
		arr[i] = block{nextInt(), nextInt(), nextInt(), nextInt()}
	}
	if m % 2 == 1 {
		printf("NO\n")
		return
	}
	flag1 := false
	for _, v := range arr {
		if v.b == v.c {
			flag1 = true
		}
	}
	if m == 2 {
		if flag1 {
			printf("YES\n")
		} else {
			printf("NO\n")
		}
		return
	}
	flag := false
	for i, x := range arr {
		for j, y := range arr {
			if i < j && x.a == y.a && x.d == y.d && x.c == y.b && x.b == y.c {
				flag = true
			}
		}
	}
	if flag || flag1 {
		printf("YES\n")
	} else {
		printf("NO\n")
	}
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)

	defer out.Flush()

	for t := nextInt(); t > 0; t-- {
		solve()
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

func print( a ...interface{}) { fmt.Fprint(w, a...) }
func scan( a ...interface{})  { fmt.Fscan(r, a...) }

//for i := 0; i < n; i++  {

func main() {
defer w.Flush()
	var q int
	scan(&q)
	for ; q > 0; q-- {
		var n int
		scan(&n)
		var m int
		scan(&m)
		var bl = false
		if m % 2 != 0 {
			print("NO\n")
			for i := 0; i < n; i++ {
				var a, b, c, d int
				scan(&a, &b, &c, &d)
			}
		}else{
			for i := 0; i < n; i++ {
				var a, b, c, d int
				scan(&a, &b, &c, &d)
				if !bl && a == d && b == c {
					bl = true
				}
			}
			if bl {
				print("YES\n")
			}else {
				print("NO\n")
			}
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var t, n, m, a, b, c, d int
	fmt.Scan(&t)
	for t != 0 {
		var matrix []([2][2]int) = make([]([2][2]int), 0)
		fmt.Scan(&n, &m)
		for i := 0; i < n; i++ {
			fmt.Scan(&a, &b)
			fmt.Scan(&c, &d)
			var x [2][2]int
			x[0][0] = a
			x[0][1] = b
			x[1][0] = c
			x[1][1] = d
			matrix = append(matrix, x)
		}
		if m != 2 {
			if m%2 != 0 {
				fmt.Println("NO")
			} else {
				fmt.Println("YES")
			}
		} else {
			for i := 0; i < n; i++ {
				a := matrix[i][0][0]
				b := matrix[i][0][1]
				c := matrix[i][1][0]
				d := matrix[i][1][1]

				if a == d && c == b {
					fmt.Println("YES")
					break
				} else {
					fmt.Println("NO")
					break
				}
			}
		}
		t--
	}
}


It is a complicated version of problem F1. The difference between them is the constraints (F1: $$$k \le 2$$$, F2: $$$k \le 10$$$).You are given an integer $$$n$$$. Find the minimum integer $$$x$$$ such that $$$x \ge n$$$ and the number $$$x$$$ is $$$k$$$-beautiful.A number is called $$$k$$$-beautiful if its decimal representation having no leading zeroes contains no more than $$$k$$$ different digits. E.g. if $$$k = 2$$$, the numbers $$$3434443$$$, $$$55550$$$, $$$777$$$ and $$$21$$$ are $$$k$$$-beautiful whereas the numbers $$$120$$$, $$$445435$$$ and $$$998244353$$$ are not.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10^9$$$, $$$1 \le k \le 10$$$).

For each test case output on a separate line $$$x$$$ — the minimum $$$k$$$-beautiful integer such that $$$x \ge n$$$.

package main

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

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

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

func rw(r io.Reader, w io.Writer) {
	var t int
	var s string
	var k int
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		fmt.Fscan(r, &s, &k)
		w1 := do(s, k)
		fmt.Fprint(w, w1)
		fmt.Fprintln(w)
	}
}

func do(t string, k int) string {
	s := make([]byte, len(t))
	for i := range t {
		s[i] = t[i] - '0'
	}
	v := make([]byte, 10)
	w := make([]byte, len(t))
	dfs(s, w, v, k, true)
	for i := range w {
		w[i] += '0'
	}
	return string(w)
}

func dfs(t []byte, w []byte, v []byte, k int, strict bool) bool {
	if k < 0 {
		return false
	}
	if len(t) == 0 {
		return true
	}
	var b byte
	if strict {
		b = t[0]
	}
	for i := b; i <= 9; i++ {
		w[0] = i
		o := 0
		if v[i] == 0 {
			o = 1
		}
		v[i]++
		if dfs(t[1:], w[1:], v, k-o, strict && i == t[0]) {
			return true
		}
		v[i]--
	}
	return false
}


You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.

The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task. It is guaranteed that n is divisible by k.

In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.

NoteExplanation of the first example.If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12. Explanation of the second example.In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3.

package main
import (
	"fmt"
	//"sort"
	"bufio"
	"os"
	"strconv"
)
func main() {
	var n,i,k,a int
	var p [100010]int
	fmt.Scan(&n,&k)
	input := bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)
	for i=1;i<=n;i++ {
		input.Scan()
		bb,_:=strconv.Atoi(input.Text())
		a=bb
		p[i%k]+=a
	}
	p[k]=p[0]
	a=1
	for i=2;i<=k;i++ {
		if p[i]<p[a] {
			a=i
		}
	}
	fmt.Println(a)
}

package main

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

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

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    var n,k,a int
    ScanInts(&n,&k)
    c := make([]int,k)
    for i := 0; i < n; i++ {
        ScanInts(&a)
        c[i%k] += a
    }
    min, ans := 1000000000, 0
    for i := 0; i < k; i++ {
        if min > c[i] { min = c[i]; ans = i+1 }
    }
    fmt.Println(ans)
}


AquaMoon has $$$n$$$ friends. They stand in a row from left to right, and the $$$i$$$-th friend from the left wears a T-shirt with a number $$$a_i$$$ written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.AquaMoon hopes that after some operations, the numbers written on the T-shirt of $$$n$$$ friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of Aquamoon's friends. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the numbers, written on the T-shirts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.

For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes). You can print each letter in any case (upper or lower).

NoteThe possible list of operations in the first test case:  Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$3, 4, 2, 5$$$. The directions are: left, left, right, right.  Swap $$$a_2$$$ and $$$a_3$$$. The resulting sequence is $$$3, 2, 4, 5$$$. The directions are: left, left, right, right.  Swap $$$a_1$$$ and $$$a_2$$$. The resulting sequence is $$$2, 3, 4, 5$$$. The directions are: right, right, right, right.

package main

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

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

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

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

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

func sstring() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		a := sint64s(n)
		printf("%s\n", sol(n, a))
	}
}

func sol(n int64, a []int64) string {
	even := []int64{}
	odd := []int64{}
	for i := range a {
		if i % 2 == 0 {
			even = append(even, a[i])
		} else {
			odd = append(odd, a[i])
		}
	}
	sort.Slice(even, func(il, ir int) bool {
		return even[il] < even[ir]
	})
	sort.Slice(odd, func(il, ir int) bool {
		return odd[il] < odd[ir]
	})
	for i := range odd {
		if !(odd[i] >= even[i]) {
			return "NO"
		}
		if i+1 < len(even) && !(odd[i] <= even[i+1]) {
			return "NO"
		}
	}
	return "YES"
}

package main

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

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

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

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

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

func sstring() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		a := sint64s(n)
		printf("%s\n", sol(n, a))
	}
}

func sol(n int64, a []int64) string {
	even := []int64{}
	odd := []int64{}
	for i := range a {
		if i & 1 == 0 {
			even = append(even, a[i])
		} else {
			odd = append(odd, a[i])
		}
	}
	sort.Slice(even, func(il, ir int) bool {
		return even[il] < even[ir]
	})
	sort.Slice(odd, func(il, ir int) bool {
		return odd[il] < odd[ir]
	})
	for i := range odd {
		if !(odd[i] >= even[i]) {
			return "NO"
		}
		if i+1 < len(even) && !(odd[i] <= even[i+1]) {
			return "NO"
		}
	}
	return "YES"
}

package main

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

func main() {

	r := bufio.NewReader(os.Stdin)

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

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

		a := make([]int, n)

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

		ans := solve(a, n)

		if ans {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}

	}
}

//4 3 2 5
// -> -> -> ->

//3 4 2 5
//<- <- -> ->

//

type pair struct {
	v int
	index int
}

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

	mp := make([][]int, 1e5 + 1)

	for i := 0; i < n; i++ {
		mp[a[i]] = append(mp[a[i]], i + 1)
	}

	dedup := make(map[int]bool)

	for i := 0; i < n; i++ {
		dedup[a[i]] = true
	}

	var b []int

	for k, _ := range dedup {
		b = append(b, k)
	}

	sort.Ints(b)

	start := 1

	for i := 0; i < len(b); i++ {
		c := b[i]

		indexes := mp[c]

		//5 indexes
		//1 2 3 4 5

		odd := 0
		even := 0

		oddPos := 0
		evenPos := 0

		for j := 0; j < len(indexes); j++ {
			index := indexes[j]

			if index % 2 == 0 {
				even ++
			} else {
				odd ++
			}
			if start % 2 == 0 {
				evenPos++
			} else {
				oddPos++
			}

			start++
		}

		if odd != oddPos || even != evenPos {
			return false
		}
	}
	return true
}

package main

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

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

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

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

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

func sstring() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		a := sint64s(n)
		printf("%s\n", sol(n, a))
	}
}

func sol(n int64, a []int64) string {
	even := []int64{}
	odd := []int64{}
	for i := range a {
		if i&1 == 0 {
			even = append(even, a[i])
		} else {
			odd = append(odd, a[i])
		}
	}
	sort.Slice(even, func(il, ir int) bool {
		return even[il] < even[ir]
	})
	sort.Slice(odd, func(il, ir int) bool {
		return odd[il] < odd[ir]
	})
	for i := range odd {
		if !(odd[i] >= even[i]) {
			return "NO"
		}
		if i+1 < len(even) && !(odd[i] <= even[i+1]) {
			return "NO"
		}
	}
	return "YES"
}

package main

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

type IntArray []int

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

const SIZE = 100111

var num [SIZE]int
var typenum [SIZE][2]int

func main() {
	var t int
	r := bufio.NewReader(os.Stdin)
	Fscan(r, &t)
	for te := 0; te < t; te++ {
		var n int
		Fscan(r, &n)
		for i := 0; i < n; i++ {
			Fscan(r, &num[i])
			typenum[num[i]][0] = 0
			typenum[num[i]][1] = 0
		}
		for i := 0; i < n; i++ {
			typenum[num[i]][i&1]++
		}
		sort.Sort(IntArray(num[:n]))
		r := true
		for i := 0; i < n; i++ {
			if typenum[num[i]][i&1] > 0 {
				typenum[num[i]][i&1]--
			} else {
				r = false
			}
		}
		if r {
			Printf("YES\n")
		} else {
			Printf("NO\n")
		}
	}
}

package main

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

var in *bufio.Reader

func solve() {
	var n int
	fmt.Fscan(in, &n)

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

	sort.Ints(a)
	odd, even := make(map[int]int), make(map[int]int)
	for i := 0; i < n; i++ {
		if i%2 == 0 {
			even[a[i]]++
		} else {
			odd[a[i]]++
		}
	}

	for i := 0; i < n; i++ {
		if i%2 == 0 {
			if even[b[i]] == 0 {
				fmt.Println("NO")
				return
			} else {
				even[b[i]]--
			}
		} else {
			if odd[b[i]] == 0 {
				fmt.Println("NO")
				return
			} else {
				odd[b[i]]--
			}
		}
	}
	fmt.Println("YES")
}

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

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

	for i := 0; i < t; i++ {
		solve()
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1545A(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var T, n int
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		b := []int{}
		for i := range a {
			Fscan(in, &a[i])
			if i&1 == 0 {
				b = append(b, a[i])
			}
		}
		sort.Ints(a)
		sort.Ints(b)
		for i, v := range b {
			if v != a[i*2] {
				Fprintln(out, "NO")
				continue o
			}
		}
		Fprintln(out, "YES")
	}
}

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

package main

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

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

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

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

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

func sstring() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		a := sint64s(n)
		printf("%s\n", sol(n, a))
	}
}

func sol(n int64, a []int64) string {
	even := []int64{}
	odd := []int64{}
	for i := range a {
		if i == 1 {
			even = append(even, a[i])
		} else {
			odd = append(odd, a[i])
		}
	}
	sort.Slice(even, func(il, ir int) bool {
		return even[il] < even[ir]
	})
	sort.Slice(odd, func(il, ir int) bool {
		return odd[il] < odd[ir]
	})
	for i := range odd {
		if !(odd[i] >= even[i]) {
			return "NO"
		}
		if i+1 < len(even) && !(odd[i] <= even[i+1]) {
			return "NO"
		}
	}
	return "YES"
}

package main

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

func main() {

	r := bufio.NewReader(os.Stdin)

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

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

		a := make([]int, n)

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

		ans := solve(a, n)

		if ans {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}

	}
}

//4 3 2 5
// -> -> -> ->

//3 4 2 5
//<- <- -> ->

//

type pair struct {
	v int
	index int
}

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

	mp := make([][]int, 1e5 + 1)

	for i := 0; i < n; i++ {
		mp[a[i]] = append(mp[a[i]], i + 1)
	}

	dedup := make(map[int]bool)

	for i := 0; i < n; i++ {
		dedup[a[i]] = true
	}

	var b []int

	for k, _ := range dedup {
		b = append(b, k)
	}

	sort.Ints(b)

	start := 1

	for i := 0; i < len(b); i++ {
		c := b[i]

		indexes := mp[c]

		//5 indexes
		//1 2 3 4 5

		odd := 0
		even := 0

		oddPos := 0
		evenPos := 0

		for j := 0; j < len(indexes); j++ {
			index := indexes[j]

			if index % 2 == 0 {
				even ++
			} else {
				odd ++
			}
			if start % 2 == 0 {
				evenPos++
			} else {
				oddPos++
			}

			if odd != oddPos || even != evenPos {
				return false
			}

			start++
		}
	}
	return true
}

package main

import (
	. "fmt"
	"sort"
)

type IntArray []int

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

const SIZE = 100111

var num [SIZE]int
var typenum [SIZE][2]int

func main() {
	var t int
	Scanf("%d", &t)
	for te := 0; te < t; te++ {
		var n int
		Scanf("%d", &n)
		//Scanln(num)
		for i := 0; i < n; i++ {
			Scanf("%d", &num[i])
			typenum[num[i]][0] = 0
			typenum[num[i]][1] = 0
		}
		for i := 0; i < n; i++ {
			typenum[num[i]][i&1]++
		}
		sort.Sort(IntArray(num[:n]))
		r := true
		for i := 0; i < n; i++ {
			if typenum[num[i]][i&1] > 0 {
				typenum[num[i]][i&1]--
			} else {
				r = false
			}
		}
		if r {
			Printf("YES\n")
		} else {
			Printf("NO\n")
		}
	}
}

package main

import (
	. "fmt"
	"sort"
)

type IntArray []int

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

const SIZE = 100111

var num [SIZE]int
var typenum [SIZE][2]int

func main() {
	var t int
	Scanf("%d", &t)
	for te := 0; te < t; te++ {
		var n int
		Scanf("%d", &n)
		for i := 0; i < n; i++ {
			Scanf("%d", &num[i])
			typenum[num[i]][0] = 0
			typenum[num[i]][1] = 0
		}
		for i := 0; i < n; i++ {
			typenum[num[i]][i&1]++
		}
		sort.Sort(IntArray(num[:n]))
		r := true
		for i := 0; i < n; i++ {
			if typenum[num[i]][i&1] > 0 {
				typenum[num[i]][i&1]--
			} else {
				r = false
			}
		}
		if r {
			Printf("YES\n")
		} else {
			Printf("NO\n")
		}
	}
}


You are given an array consisting of n non-negative integers a1, a2, ..., an.You are going to destroy integers in the array one by one. Thus, you are given the permutation of integers from 1 to n defining the order elements of the array are destroyed.After each element is destroyed you have to find out the segment of the array, such that it contains no destroyed elements and the sum of its elements is maximum possible. The sum of elements in the empty segment is considered to be 0.

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the length of the array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).  The third line contains a permutation of integers from 1 to n — the order used to destroy elements.

Print n lines. The i-th line should contain a single integer — the maximum possible sum of elements on the segment containing no destroyed elements, after first i operations are performed.

NoteConsider the first sample:   Third element is destroyed. Array is now 1 3  *  5. Segment with maximum sum 5 consists of one integer 5.  Fourth element is destroyed. Array is now 1 3  *   * . Segment with maximum sum 4 consists of two integers 1 3.  First element is destroyed. Array is now  *  3  *   * . Segment with maximum sum 3 consists of one integer 3.  Last element is destroyed. At this moment there are no valid nonempty segments left in this array, so the answer is equal to 0.

package main

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

type Int int64

func Max(a, b Int) Int {
	if a < b {
		return b
	}
	return a
}

func find(j Int) Int {
	if parent[j] == j {
		return j
	}
	return find(parent[j])
}

func union(p, q Int) {
	parentP := find(p)
	parentQ := find(q)
	if parentP != parentQ {
		if sum[parentQ] > sum[parentP] {
			parent[parentP] = parentQ
			sum[parentQ] += sum[parentP]
		} else {
			parent[parentQ] = parentP
			sum[parentP] += sum[parentQ]
		}
	}
}

var a, b, sum, parent, out []Int
var active []bool

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var n Int
	fmt.Fscanf(r, "%v\n", &n)
	a, b, sum, parent, out, active = make([]Int, n), make([]Int, n), make([]Int, n), make([]Int, n), make([]Int, n), make([]bool, n+2)
	for i := Int(0); i < n; i++ {
		if i == n-1 {
			fmt.Fscanf(r, "%v\n", &a[i])
		} else {
			fmt.Fscanf(r, "%v ", &a[i])
		}
		parent[i] = i
	}
	copy(sum, a)
	max := Int(0)
	for i := Int(0); i < n; i++ {
		fmt.Fscanf(r, "%v ", &b[i])
		b[i]--
	}
	for i := n - 1; i >= 0; i-- {
		active[b[i]] = true
		if active[b[i]+1] {
			union(b[i], b[i]+1)
		}
		if b[i]-1 >= 0 && active[b[i]-1] {
			union(b[i]-1, b[i])
		}
		out[i] = max
		max = Max(max, sum[find(b[i])])
	}
	for _, o := range out {
		fmt.Fprintf(w, "%v\n", o)
	}
}

package main

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

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

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

	var n int
	var a, pos []int
	var res []int64

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

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

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

	left := make([]int, n+2)
	right := make([]int, n+2)
	sum := make([]int64, n+2)

	for i := 0; i <= n+1; i++ {
		left[i] = -1
		right[i] = -1
		sum[i] = 0
	}

	res = make([]int64, n+1)
	max := int64(0)

	for i := n; i >= 1; i-- {
		res[i] = max

		p := pos[i]
		left[p] = p
		right[p] = p
		sum[p] = int64(a[p])

		l := left[p-1]
		if l != -1 {
			left[p] = l
			sum[p] += sum[p-1]
		}

		r := right[p+1]
		if r != -1 {
			right[p] = r
			sum[p] += sum[p+1]
		}

		if l != -1 {
			right[l] = right[p]
			sum[l] = sum[p]
		}

		if r != -1 {
			left[r] = left[p]
			sum[r] = sum[p]
		}

		if max < sum[p] {
			max = sum[p]
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Fprintf(bufout, "%d\n", res[i])
	}
}

package main

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

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

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

	var n int
	var a, pos []int
	var res []int64

	fmt.Fscan(bufin, &n)

	a = make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(bufin, &a[i])
	}

	pos = make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(bufin, &pos[i])
	}

	left := make([]int, n+2)
	right := make([]int, n+2)
	sum := make([]int64, n+2)

	for i := 0; i <= n+1; i++ {
		left[i] = -1
		right[i] = -1
		sum[i] = 0
	}

	res = make([]int64, n+1)
	max := int64(0)

	for i := n; i >= 1; i-- {
		res[i] = max

		p := pos[i]
		left[p] = p
		right[p] = p
		sum[p] = int64(a[p])

		l := left[p-1]
		if l != -1 {
			left[p] = l
			sum[p] += sum[p-1]
		}

		r := right[p+1]
		if r != -1 {
			right[p] = r
			sum[p] += sum[p+1]
		}

		if l != -1 {
			right[l] = right[p]
			sum[l] = sum[p]
		}

		if r != -1 {
			left[r] = left[p]
			sum[r] = sum[p]
		}

		if max < sum[p] {
			max = sum[p]
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Fprintln(bufout, res[i])
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF722C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var fa []int
	initFa := func(n int) {
		fa = make([]int, n)
		for i := range fa {
			fa[i] = i
		}
	}
	var find func(int) int
	find = func(x int) int {
		if fa[x] != x {
			fa[x] = find(fa[x])
		}
		return fa[x]
	}

	var n int
	Fscan(in, &n)
	arr := make([]int, n)
	for i := range arr {
		Fscan(in, &arr[i])
	}
	pos := make([]int, n)
	for i := range pos {
		Fscan(in, &pos[i])
	}

	ans := make([]int64, n)
	vis := make([]bool, n+2)
	sum := make([]int64, n+1)
	initFa(n + 1)
	max := int64(0)
	for i := n - 1; i >= 0; i-- {
		ans[i] = max
		p := pos[i]
		vis[p] = true
		sum[p] += int64(arr[p-1])
		if vis[p-1] {
			f := find(p - 1)
			sum[p] += sum[f]
			fa[f] = p
		}
		if vis[p+1] {
			f := find(p + 1)
			sum[p] += sum[f]
			fa[f] = p
		}
		if sum[p] > max {
			max = sum[p]
		}
	}
	for _, v := range ans {
		Fprintln(out, v)
	}
}

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

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/722/C

package main

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

func findRoot(i int64, roots []int64) int64 {
	if i != roots[i] {
		roots[i] = findRoot(roots[i], roots)
	}
	return roots[i]
}

func Solve(n int64, a, b []int64) []int64 {
	roots := make([]int64, n+1)
	for i := int64(1); i <= n; i++ {
		roots[i] = i
	}
	sums := make([]int64, n+1)
	visited := make([]bool, n+2)
	ans := make([]int64, n)
	max := int64(0)
	for i := n - 1; i >= 0; i-- {
		ans[i] = max
		bi := b[i]
		visited[bi] = true
		sums[bi] += a[bi-1]
		if visited[bi-1] {
			root := findRoot(bi-1, roots)
			roots[root] = bi
			sums[bi] += sums[root]
		}
		if visited[bi+1] {
			root := findRoot(bi+1, roots)
			roots[root] = bi
			sums[bi] += sums[root]
		}
		if sums[b[i]] > max {
			max = sums[b[i]]
		}
	}
	return ans
}

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

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

	ans := Solve(n, a, b)
	for _, elem := range ans {
		fmt.Fprintf(writer, "%d\n", elem)
	}
}


Natasha is going to fly on a rocket to Mars and return to Earth. Also, on the way to Mars, she will land on $$$n - 2$$$ intermediate planets. Formally: we number all the planets from $$$1$$$ to $$$n$$$. $$$1$$$ is Earth, $$$n$$$ is Mars. Natasha will make exactly $$$n$$$ flights: $$$1 \to 2 \to \ldots n \to 1$$$.Flight from $$$x$$$ to $$$y$$$ consists of two phases: take-off from planet $$$x$$$ and landing to planet $$$y$$$. This way, the overall itinerary of the trip will be: the $$$1$$$-st planet $$$\to$$$ take-off from the $$$1$$$-st planet $$$\to$$$ landing to the $$$2$$$-nd planet $$$\to$$$ $$$2$$$-nd planet $$$\to$$$ take-off from the $$$2$$$-nd planet $$$\to$$$ $$$\ldots$$$ $$$\to$$$ landing to the $$$n$$$-th planet $$$\to$$$ the $$$n$$$-th planet $$$\to$$$ take-off from the $$$n$$$-th planet $$$\to$$$ landing to the $$$1$$$-st planet $$$\to$$$ the $$$1$$$-st planet.The mass of the rocket together with all the useful cargo (but without fuel) is $$$m$$$ tons. However, Natasha does not know how much fuel to load into the rocket. Unfortunately, fuel can only be loaded on Earth, so if the rocket runs out of fuel on some other planet, Natasha will not be able to return home. Fuel is needed to take-off from each planet and to land to each planet. It is known that $$$1$$$ ton of fuel can lift off $$$a_i$$$ tons of rocket from the $$$i$$$-th planet or to land $$$b_i$$$ tons of rocket onto the $$$i$$$-th planet. For example, if the weight of rocket is $$$9$$$ tons, weight of fuel is $$$3$$$ tons and take-off coefficient is $$$8$$$ ($$$a_i = 8$$$), then $$$1.5$$$ tons of fuel will be burnt (since $$$1.5 \cdot 8 = 9 + 3$$$). The new weight of fuel after take-off will be $$$1.5$$$ tons. Please note, that it is allowed to burn non-integral amount of fuel during take-off or landing, and the amount of initial fuel can be non-integral as well.Help Natasha to calculate the minimum mass of fuel to load into the rocket. Note, that the rocket must spend fuel to carry both useful cargo and the fuel itself. However, it doesn't need to carry the fuel which has already been burnt. Assume, that the rocket takes off and lands instantly.

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 1000$$$) — number of planets. The second line contains the only integer $$$m$$$ ($$$1 \le m \le 1000$$$) — weight of the payload. The third line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 1000$$$), where $$$a_i$$$ is the number of tons, which can be lifted off by one ton of fuel. The fourth line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le 1000$$$), where $$$b_i$$$ is the number of tons, which can be landed by one ton of fuel.  It is guaranteed, that if Natasha can make a flight, then it takes no more than $$$10^9$$$ tons of fuel.

If Natasha can fly to Mars through $$$(n - 2)$$$ planets and return to Earth, print the minimum mass of fuel (in tons) that Natasha should take. Otherwise, print a single number $$$-1$$$. It is guaranteed, that if Natasha can make a flight, then it takes no more than $$$10^9$$$ tons of fuel. The answer will be considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$. Formally, let your answer be $$$p$$$, and the jury's answer be $$$q$$$. Your answer is considered correct if $$$\frac{|p - q|}{\max{(1, |q|)}} \le 10^{-6}$$$.

NoteLet's consider the first example.Initially, the mass of a rocket with fuel is $$$22$$$ tons. At take-off from Earth one ton of fuel can lift off $$$11$$$ tons of cargo, so to lift off $$$22$$$ tons you need to burn $$$2$$$ tons of fuel. Remaining weight of the rocket with fuel is $$$20$$$ tons. During landing on Mars, one ton of fuel can land $$$5$$$ tons of cargo, so for landing $$$20$$$ tons you will need to burn $$$4$$$ tons of fuel. There will be $$$16$$$ tons of the rocket with fuel remaining. While taking off from Mars, one ton of fuel can raise $$$8$$$ tons of cargo, so to lift off $$$16$$$ tons you will need to burn $$$2$$$ tons of fuel. There will be $$$14$$$ tons of rocket with fuel after that. During landing on Earth, one ton of fuel can land $$$7$$$ tons of cargo, so for landing $$$14$$$ tons you will need to burn $$$2$$$ tons of fuel. Remaining weight is $$$12$$$ tons, that is, a rocket without any fuel.In the second case, the rocket will not be able even to take off from Earth.

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m := 0, 0
	fmt.Fscanf(in, "%d\n", &n)
	fmt.Fscanf(in, "%d\n", &m)
	initWeight := float64(m)
	weight := initWeight
	a := make([]int, n)
	b := make([]int, n)
	for i := 0; i < n; i++ {
		t := 0
		fmt.Fscanf(in, "%d", &t)
		a[i] = t
	}
	fmt.Fscanf(in, "\n")
	for i := 0; i < n; i++ {
		t := 0
		fmt.Fscanf(in, "%d", &t)
		b[i] = t
	}
	for i := 0; i < n; i++ {
		if a[i] == 1 || b[i] == 1 {
			fmt.Fprintf(out, "-1\n")
			return
		}
	}
	q1 := n - 1
	q2 := 0
	for i := 0; i < 2*n; i++ {
		var sp float64
		if i%2 == 0 {
			sp = float64(b[q2])
			if q2 == 0 {
				q2 = n - 1
			} else {
				q2--
			}
		} else {
			sp = float64(a[q1])
			q1--
		}
		weight = weight + weight/(sp-1.0)
	}
	fmt.Fprintf(out, "%.9f\n", weight-initWeight)
	return
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m := 0, 0
	fmt.Fscanf(in, "%d\n", &n)
	fmt.Fscanf(in, "%d\n", &m)
	initWeight := float64(m)
	weight := initWeight
	a := make([]int, n)
	b := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &a[i])
	}
	fmt.Fscanf(in, "\n")
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &b[i])
	}
	for i := 0; i < n; i++ {
		if a[i] == 1 || b[i] == 1 {
			fmt.Fprintf(out, "-1\n")
			return
		}
	}
	q1 := n - 1
	q2 := 0
	for i := 0; i < 2*n; i++ {
		var sp float64
		if i%2 == 0 {
			sp = float64(b[q2])
			if q2 == 0 {
				q2 = n - 1
			} else {
				q2--
			}
		} else {
			sp = float64(a[q1])
			q1--
		}
		weight = weight + weight/(sp-1.0)
	}
	fmt.Fprintf(out, "%.9f\n", weight-initWeight)
	return
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m := 0, 0
	fmt.Fscanf(in, "%d\n", &n)
	fmt.Fscanf(in, "%d\n", &m)
	initWeight := float64(m)
	weight := initWeight
	a := make([]int, n)
	b := make([]int, n)
	for i := 0; i < n; i++ {
		t := 0
		fmt.Fscanf(in, "%d", &t)
		a[i] = t
	}
	fmt.Fscanf(in, "\n")

	for i := 0; i < n; i++ {
		t := 0
		fmt.Fscanf(in, "%d", &t)
		b[i] = t
	}
	q1 := n - 1
	q2 := 0
	noAnswer := false
	for i := 0; i < 2*n; i++ {
		sp := 0.0
		if i%2 == 0 {
			if b[q2] == 1 {
				noAnswer = true
				break
			}
			sp = float64(b[q2])
			if q2 == 0 {
				q2 = n - 1
			} else {
				q2--
			}
		} else {
			if a[q1] == 1 {
				noAnswer = true
				break
			}
			sp = float64(a[q1])
			q1--
		}
		weight = weight + weight/(sp-1.0)
	}
	if noAnswer {
		fmt.Fprintf(out, "-1\n")
	} else {
		fmt.Fprintf(out, "%.9f\n", weight-initWeight)
	}
	return
}

package main

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

func valiate(tar float64, a int, b []int) bool {
	for i := 0; i < len(b); i++ {
		cost := (tar + float64(a)) / float64(b[i])
		tar -= cost
		if tar < 0 {
			return false
		}
	}
	return true
}

func solve(a int, b []int) float64 {
	l, r, best, count := 0.0, 2e9, -1.0, 2000
	for l <= r && count > 0 {
		mid := (l + r) / 2
		count = count - 1
		// Works, means there is enough fuel
		if valiate(mid, a, b) {
			r, best = mid, mid
		} else {
			l = mid
		}
	}
	return best
}

var scanner = bufio.NewScanner(os.Stdin)

func scanNum() int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func main() {
	scanner.Split(bufio.ScanWords)
	n, a := scanNum(), scanNum()
	b := make([]int, n*2)
	for i := 0; i < n; i++ {
		b[i*2] = scanNum()
	}
	b[n*2-1] = scanNum()
	for i := 0; i < n-1; i++ {
		b[i*2+1] = scanNum()
	}

	res := solve(a, b)
	if res < 0 {
		fmt.Println(-1)
	} else {
		fmt.Println(res)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m := 0, 0
	fmt.Fscanf(in, "%d\n", &n)
	fmt.Fscanf(in, "%d\n", &m)
	initWeight := float64(m)
	weight := initWeight
	a := make([]int, n)
	b := make([]int, n)
	for i := 0; i < n; i++ {
		t := 0
		fmt.Fscanf(in, "%d", &t)
		a[i] = t
	}
	fmt.Fscanf(in, "\n")

	for i := 0; i < n; i++ {
		t := 0
		fmt.Fscanf(in, "%d", &t)
		b[i] = t
	}
	//fmt.Println(a)
	//fmt.Println(b)
	q1 := n - 1
	q2 := 0
	noAnswer := false
	for i := 0; i < 2*n; i++ {
		sp := 0.0
		if i%2 == 0 {
			if b[q2] == 1 {
				noAnswer = true
				break
			}
			sp = float64(b[q2])
			if q2 == 0 {
				q2 = n - 1
			} else {
				q2--
			}
		} else {
			if a[q1] == 1 {
				noAnswer = true
				break
			}
			sp = float64(a[q1])
			q1--
		}
		//fmt.Println(sp)
		weight = weight + weight/(sp-1.0)
	}
	if noAnswer {
		fmt.Fprintf(out, "-1\n")
	} else {
		fmt.Fprintf(out, "%.9f\n", weight-initWeight)
	}
	return
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m := 0, 0
	fmt.Fscanf(in, "%d\n", &n)
	fmt.Fscanf(in, "%d\n", &m)
	weight := float64(0)
	a := make([]int, n)
	b := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &a[i])
	}
	fmt.Fscanf(in, "\n")
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &b[i])
	}
	for i := 0; i < n; i++ {
		if a[i] == 1 || b[i] == 1 {
			fmt.Fprintf(out, "-1\n")
			return
		}
	}
	q1 := n - 1
	q2 := 0
	for i := 0; i < 2*n; i++ {
		var sp float64
		if i%2 == 0 {
			sp = float64(b[q2])
			if q2 == 0 {
				q2 = n - 1
			} else {
				q2--
			}
		} else {
			sp = float64(a[q1])
			q1--
		}
		weight = weight + weight/(sp-1.0)
	}
	fmt.Fprintf(out, "%.9f\n", weight)
	return
}

package main

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

func valiate(tar float64, a int, b []int) bool {
	for i := 0; i < len(b); i++ {
		cost := (tar + float64(a)) / float64(b[i])
		tar -= cost
		if tar < 0 {
			return false
		}
	}
	return true
}

func solve(a int, b []int) float64 {
	l, r, best, count := 0.0, 1e9, -1.0, 1000
	for l <= r && count > 0 {
		mid := (l + r) / 2
		count = count - 1
		// Works, means there is enough fuel
		if valiate(mid, a, b) {
			r, best = mid, mid
		} else {
			l = mid
		}
	}
	return best
}

var scanner = bufio.NewScanner(os.Stdin)

func scanNum() int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func main() {
	scanner.Split(bufio.ScanWords)
	n, a := scanNum(), scanNum()
	b := make([]int, n*2)
	for i := 0; i < n; i++ {
		b[i*2] = scanNum()
	}
	b[n*2-1] = scanNum()
	for i := 0; i < n-1; i++ {
		b[i*2+1] = scanNum()
	}

	res := solve(a, b)
	if res < 0 {
		fmt.Println(-1)
	} else {
		fmt.Println(res)
	}
}


According to a new ISO standard, a flag of every country should have a chequered field n × m, each square should be of one of 10 colours, and the flag should be «striped»: each horizontal row of the flag should contain squares of the same colour, and the colours of adjacent horizontal rows should be different. Berland's government asked you to find out whether their flag meets the new ISO standard.

The first line of the input contains numbers n and m (1 ≤ n, m ≤ 100), n — the amount of rows, m — the amount of columns on the flag of Berland. Then there follows the description of the flag: each of the following n lines contain m characters. Each character is a digit between 0 and 9, and stands for the colour of the corresponding square.

Output YES, if the flag meets the new ISO standard, and NO otherwise.

package main

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

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n, m int
	var prev byte
	prev = 'a'
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	for i := 0;i < n;i ++ {
		var s string
		fmt.Fscanf(reader, "%s\n", &s)
		b := []byte(s)
		if b[0] == prev {
			fmt.Printf("NO")
			return
		}
		prev = b[0]
		for j := 0;j < len(b);j ++ {
			if b[j] != b[0] {
				fmt.Printf("NO")
				return
			}
		}
	}
	fmt.Printf("YES")
	return
}

package main

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

func main(){
	var n, m int
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	r.Scan()
	n, _ = strconv.Atoi(r.Text())
	r.Scan()
	m, _ = strconv.Atoi(r.Text())
	last := ""
	iso := false
	var s, s1 string
	for i:=0; i<n; i++ {
		r.Scan()
		s = r.Text()
		s1 = strings.Repeat(string(s[0]), m)
		if s == s1 && string(s[0]) != string(last) {
			iso = true
		} else {
			iso = false
			break
		}
		last = string(s[0])
	}
	if iso == true {
		w.WriteString("YES\n")
	} else {
		w.WriteString("NO\n")
	}
	w.Flush()
}

package main

import "fmt"

func main() {
    var n,m int
    var s,prev string
    fmt.Scan(&n,&m)
    ans := true
    for i := 0; i < n; i++ {
        fmt.Scan(&s)
        for _, v := range s {
            if v != rune(s[0]) { ans = false; break; }
        }
        if (i != 0) && (s[0] == prev[0]) { ans = false }
        prev = s
    }
    if (ans) { fmt.Println("YES") } else { fmt.Println("NO") }
}

// 16A-mic_rizal
package main

import (
    "fmt"
)

func main() {
    var n, m int
    fmt.Scan(&n, &m)
    s := make([]string, n)

    for i := 0; i < n; i++ {
        fmt.Scan(&s[i])
    }

    for i := 0; i < n; i++ {
        for j := 0; j < m-1; j++ {
            if s[i][j] != s[i][j+1] {
                fmt.Println("NO")
                return
            }
        }
    }

    for i := 0; i < n-1; i++ {
        if s[i][0] == s[i+1][0] {
            fmt.Println("NO")
            return
        }
    }

    fmt.Println("YES")
}

package main

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

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 n, m int
	fmt.Fscan(r, &n, &m)
	a := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(r, &a[i])
	}
	flag := true
	for _, e := range a {
		for _, c := range e {
			if rune(e[0]) != c {
				flag = false
				break
			}
		}
	}
	for i := 1; i < n ; i++ {
		if a[i-1] == a[i] {
			flag = false
			break
		}
	}
	if flag {
		fmt.Fprintln(w, "YES")
	} else {
		fmt.Fprintln(w, "NO")
	}
}