This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation.

The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$.  Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different.

For each test case, output "Yes" if Ujan can make the two strings equal and "No" otherwise. You can print each letter in any case (upper or lower).

4
5
souse
houhe
3
cat
dog
2
aa
az
3
abc
bca

NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word "house".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$.

package main

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

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

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

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

	var k, n int
	scanf("%d\n", &k)
	for ; k > 0; k-- {
		var s, t string
		ans := "No"
		scanf("%d\n", &n)
		scanf("%s\n", &s)
		scanf("%s\n", &t)
		var sdif, tdif []int
		for i := range s {
			if s[i] != t[i] {
				sdif = append(sdif, i)
				tdif = append(tdif, i)
			}
		}
		if len(sdif) == 2 {
			s, t = s[:sdif[0]]+string(t[tdif[1]])+s[sdif[0]+1:], t[:tdif[1]]+string(s[sdif[0]])+t[tdif[1]+1:]
			if s == t {
				ans = "Yes"
			}
		}
		if k != 1 {
			printf("%s\n", ans)
		} else {
			printf(ans)
		}
	}
}

package main

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

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

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

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	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
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// 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 int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	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 int) int {
	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
}

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

/********** 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 MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var k int
var n int
var S, T []rune

func main() {
	k = ReadInt()
	for tc := 0; tc < k; tc++ {
		n = ReadInt()
		S = ReadRuneSlice()
		T = ReadRuneSlice()
		solve()
	}
}

func solve() {
	diff := 0
	for i := 0; i < n; i++ {
		if S[i] != T[i] {
			diff++
		}
	}

	if diff == 0 {
		fmt.Println("Yes")
		return
	}
	if diff != 2 {
		fmt.Println("No")
		return
	}

	pos := [2]int{}
	j := 0
	for i := 0; i < n; i++ {
		if S[i] != T[i] {
			pos[j] = i
			j++
		}
	}

	i, j := pos[0], pos[1]
	if S[i] == S[j] && T[i] == T[j] {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

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

package main

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

var inf int = 0x3f3f3f3f
var reader *bufio.Reader

func init() {
	stdin := os.Stdin
	// stdin, _ = os.Open("1.in")
	reader = bufio.NewReaderSize(stdin, 1<<20)
}

var k, n int
var s, t string

func main() {
	fmt.Fscan(reader, &k)
	for ; k != 0; k-- {
		fmt.Fscan(reader, &n, &s, &t)
		if s == t {
			fmt.Println("Yes")
			continue
		}
		var pos []int
		for i := 0; i < n; i++ {
			if s[i] != t[i] {
				pos = append(pos, i)
			}
		}
		if len(pos) != 2 {
			fmt.Println("No")
			continue
		}
		if s[pos[0]] == s[pos[1]] && t[pos[0]] == t[pos[1]] {
			fmt.Println("Yes")
		} else {
			fmt.Println("No")
		}
	}
}

package main

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

const (
	MAX_INT int64 = math.MaxInt64
	MIN_INT int64 = math.MinInt64
)

func main() {

	for t := ScanInt64(); t > 0; t-- {
		n := ScanInt64()
		s := ScanWord()
		t := ScanWord()

		count := 0
		diff := []int{}

		for i := 0; i < int(n); i++ {
			if s[i] != t[i] {
				if count > 2 {
					break
				}
				diff = append(diff, i)
				count++
			}
		}

		if count > 2 || count == 1 {
			fmt.Println("NO")
		} else {
			if count == 0 {
				fmt.Println("YES")
			} else if CanBeEqual(s, t, diff[0], diff[1]) {
				fmt.Println("YES")
			} else {
				fmt.Println("NO")
			}
		}

	}
	close(out)
	outWg.Wait()
}

func CanBeEqual(s, t string, i, j int) bool  {
	if s[i] == s[j] && t[i] == t[j] {
		return true
	}
	return false
}
/*********************** I/O ***********************/

var out chan string
var in *bufio.Scanner
var outWg *sync.WaitGroup

func init() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(2e+5))
	in.Split(bufio.ScanWords)

	out = make(chan string, 16)
	outWg = &sync.WaitGroup{}
	outWg.Add(1)

	writer := bufio.NewWriterSize(os.Stdout, int(2e+5))
	go func(writer *bufio.Writer) {
		defer outWg.Done()
		defer writer.Flush()

		for line := range out {
			writer.WriteString(line + "\n")
		}
	}(writer)
}

func ScanWord() string {
	in.Scan()
	return in.Text()
}

func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}

func ScanInt64() int64 {
	intStr := ScanWord()
	i, err := strconv.ParseInt(intStr, 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

func Max(args ...int64) int64 {
	maxVal := MIN_INT
	for _, v := range args {
		if v > maxVal {
			maxVal = v
		}
	}
	return maxVal
}

func Min(args ...int64) int64 {
	minVal := MAX_INT
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
	}
	return minVal
}

func LCM(a, b int64) int64 {
	return a / GCD(a, b) * b
}

func GCD(a, b int64) int64 {
	if (a == 0) {
		return b
	}
	return GCD(b%a, a)
}

func iToS(v int64) string {
	return strconv.FormatInt(v, 10)
}

package main

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

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

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

	iterResults := make([]string, k)
	for j := 0; j < k; j++ {
		reader.ReadString('\n')

		text, _ := reader.ReadString('\n')
		a := []byte(strings.TrimSpace(text))

		text2, _ := reader.ReadString('\n')
		b := []byte(strings.TrimSpace(text2))

		iterResults[j] = handle(a, b)
	}

	for i := 0; i < k; i++ {
		fmt.Println(iterResults[i])
	}
}

func handle(a []byte, b []byte) string {
	res := make(map[string]int)

	for i := 0; i < len(a); i++ {
		if a[i] != b[i] {
			if v, ok := res[string([]byte{a[i], b[i]})]; ok {
				if v == 2 {
					return "No"
				}
				res[string([]byte{a[i], b[i]})] = v + 1
			} else {
				res[string([]byte{a[i], b[i]})] = 1
			}
		}
	}

	if len(res) > 1 {
		return "No"
	}

	if len(res) == 1 {
		for _, v := range res {
			if v != 2 {
				return "No"
			}
		}
	}

	return "Yes"
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	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 min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

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

func possible(s1, s2 string) bool {
	if len(s1) != len(s2) {
		return false
	}
	cnt := 0
	change := make([]int, 0, len(s1))
	for i := range s1 {
		if s1[i] != s2[i] {
			cnt++
			change = append(change,i)
		}
	}
	if cnt == 0 {
		return true
	}
	if cnt != 2{
		return false
	}
	return s1[change[0]] == s1[change[1]] && s2[change[0]] == s2[change[1]]
}

func fun() {
	var n int64
	var s1, s2 string
	scanf("%d\n", &n)
	scanf("%s\n", &s1)
	scanf("%s\n", &s2)
	if possible(s1,s2) {
		printf("YES\n")
	} else {
		printf("NO\n")
	}
}

func main() {
	// Flush stdout
	defer writer.Flush()

	var n int
	scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		fun()
	}
}

package main

// Template
import (
	"bufio"
	"fmt"
	"os"
)

var (
	reader = bufio.NewReader(os.Stdin)
	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 min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

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

func readNumberFromLine(n int64) []int64 {
	arr := make([]int64, n)
	for i := int64(0); i < n; i++ {
		if i == n-1 {
			scanf("%d\n", &arr[i])
		} else {
			scanf("%d ", &arr[i])
		}
	}
	return arr
}

func possible(s1, s2 string) bool {
	if len(s1) != len(s2) {
		return false
	}
	cnt := 0
	change := make([]int, 0, len(s1))
	for i := range s1 {
		if s1[i] != s2[i] {
			cnt++
			change = append(change,i)
		}
	}
	if cnt == 0 {
		return true
	}
	if cnt != 2{
		return false
	}
	return s1[change[0]] == s1[change[1]] && s2[change[0]] == s2[change[1]]
}

func solve() {
	var n int64
	var s1, s2 string
	scanf("%d\n", &n)
	scanf("%s\n", &s1)
	scanf("%s\n", &s2)
	if possible(s1,s2) {
		printf("YES\n")
	} else {
		printf("NO\n")
	}
}

func main() {
	// Flush stdout
	defer writer.Flush()

	var n int
	scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

const (
	MAX_INT int64 = math.MaxInt64
	MIN_INT int64 = math.MinInt64
)

func main() {

	for t := ScanInt64(); t > 0; t-- {
		n := ScanInt64()
		s := ScanWord()
		t := ScanWord()

		count, l, r := 0, 0, 0

		for i := 0; i < int(n); i++ {
			if s[i] != t[i] {
				count++
			}
		}

		if count > 2 || count == 1 {
			fmt.Println("NO")
		} else {
			if count == 0 {
				fmt.Println("YES")
			} else if CanBeEqual(s, t, l, r) {
				fmt.Println("YES")
			} else {
				fmt.Println("NO")
			}
		}

	}
	close(out)
	outWg.Wait()
}

func CanBeEqual(s, t string, i, j int) bool  {
	if s[i] == s[j] && t[i] == t[j] {
		return true
	}
	return false
}
/*********************** I/O ***********************/

var out chan string
var in *bufio.Scanner
var outWg *sync.WaitGroup

func init() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(2e+5))
	in.Split(bufio.ScanWords)

	out = make(chan string, 16)
	outWg = &sync.WaitGroup{}
	outWg.Add(1)

	writer := bufio.NewWriterSize(os.Stdout, int(2e+5))
	go func(writer *bufio.Writer) {
		defer outWg.Done()
		defer writer.Flush()

		for line := range out {
			writer.WriteString(line + "\n")
		}
	}(writer)
}

func ScanWord() string {
	in.Scan()
	return in.Text()
}

func ScanStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}

func ScanInt64() int64 {
	intStr := ScanWord()
	i, err := strconv.ParseInt(intStr, 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

func ScanInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ScanInt64()
	}
	return arr
}

/*********************** Utils ***********************/

func Max(args ...int64) int64 {
	maxVal := MIN_INT
	for _, v := range args {
		if v > maxVal {
			maxVal = v
		}
	}
	return maxVal
}

func Min(args ...int64) int64 {
	minVal := MAX_INT
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
	}
	return minVal
}

func LCM(a, b int64) int64 {
	return a / GCD(a, b) * b
}

func GCD(a, b int64) int64 {
	if (a == 0) {
		return b
	}
	return GCD(b%a, a)
}

func iToS(v int64) string {
	return strconv.FormatInt(v, 10)
}


You are given an array $$$a$$$ with $$$n$$$ integers. You can perform the following operation at most $$$k$$$ times:  Choose two indices $$$i$$$ and $$$j$$$, in which $$$i \,\bmod\, k = j \,\bmod\, k$$$ ($$$1 \le i < j \le n$$$).  Swap $$$a_i$$$ and $$$a_j$$$. After performing all operations, you have to select $$$k$$$ consecutive elements, and the sum of the $$$k$$$ elements becomes your score. Find the maximum score you can get.Here $$$x \bmod y$$$ denotes the remainder from dividing $$$x$$$ by $$$y$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 600$$$) — the number of test cases. Each test case consists of two lines.  The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$) — the length of the array and the number in the statement above. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$)  — the array itself.

For each test case, print the maximum score you can get, one per line.

NoteIn the first test case, we can get a score of $$$11$$$ if we select $$$a_1, a_2$$$ without performing any operations.In the third test case, we can get a score of $$$15$$$ if we first swap $$$a_1$$$ with $$$a_4$$$ and then select $$$a_3, a_4, a_5$$$.

package main

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

var a [100]int

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

	var T int
	for fmt.Fscan(in, &T); T > 0; T-- {
		var n, k int
		fmt.Fscan(in, &n, &k)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		ans := 0
		for i := 0; i < k; i++ {
			max := 0
			for j := i; j < n; j += k {
				if max < a[j] {
					max = a[j]
				}
			}
			ans += max
		}
		fmt.Fprintln(out, ans)
	}
}

package main

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

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

	var T int
	for fmt.Fscan(in, &T); T > 0; T-- {
		var n, k int
		fmt.Fscan(in, &n, &k)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		ans := 0
		for i := 0; i < k; i++ {
			max := 0
			for j := i; j < n; j += k {
				if max < a[j] {
					max = a[j]
				}
			}
			ans += max
		}
		fmt.Fprintln(out, ans)
	}
}

package main

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

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

	var T int
	for fmt.Fscan(in, &T); T > 0; T-- {
		var n, k int
		fmt.Fscan(in, &n, &k)
		a := make([]int, n)
		mx := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			mx[i%k] = max(mx[i%k], a[i])
		}
		ans := 0
		for j := 0; j < k; j++ {
			ans += mx[j]
		}
		fmt.Fprintln(out, ans)
	}
}

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

package main

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

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

	var T, n, k int
	for fmt.Fscan(in, &T); T > 0; T-- {
		fmt.Fscan(in, &n, &k)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		mx := make([]int, n)
		for j := 0; j < n; j++ {
			mx[j%k] = max(mx[j%k], a[j])
		}
		ans := 0
		for j := 0; j < k; j++ {
			ans += mx[j]
		}
		fmt.Fprintln(out, ans)
	}
}

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

package main

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

var (
	input  = bufio.NewReaderSize(os.Stdin, 55000)
	output = bufio.NewWriterSize(os.Stdout, 55000)
)

func main() {
	var t int
	fmt.Fscan(input, &t)
	for t > 0 {
		var k, n int
		fmt.Fscan(input, &n, &k)

		a := make([]int, k)
		for i := 0; i < n; i++ {
			var val int
			fmt.Fscan(input, &val)
			if a[i%k] < val {
				a[i%k] = val
			}
		}

		sum := 0
		for i := 0; i < k; i++ {
			sum += a[i]
		}

		fmt.Println(sum)

		t--
	}
}

package main

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

type arr []int

func (a *arr) Len() int {
	return len(*a)
}
func (a *arr) Less(ai, bi int) bool {
	if (*a)[ai] > (*a)[bi] {
		return true
	} else {
		return false
	}
}

func (a *arr) Swap(ai, bi int) {
	(*a)[ai], (*a)[bi] = (*a)[bi], (*a)[ai]
}

func main() {
	var testCases int

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

	fmt.Fscan(reader, &testCases)

	for testCase := 1; testCase <= testCases; testCase++ {
		var n, k int
		fmt.Fscan(reader, &n)
		fmt.Fscan(reader, &k)

		a := make(arr, n)
		for ai := 0; ai < n; ai++ {
			var i int
			fmt.Fscan(reader, &i)
			a[ai] = i
		}

		output := solveA(n, k, &a)

		fmt.Fprintln(writer, output)
	}
}

func solveA(n, k int, a *arr) int {

	kConsecutive := make([]int, k)
	for i := 0; i < n; i++ {
		if (*a)[i] > kConsecutive[i%k] {
			kConsecutive[i%k] = (*a)[i]
		}
	}
	sum := 0

	for ki := 0; ki < k; ki++ {
		sum += kConsecutive[ki]
	}
	return sum
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1733A
Date: 9/25/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1733/A
Problem: CF1733A
**/
func (in *CF1733A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, k := in.NextInt(), in.NextInt()
		arr := make([]int64, n)
		for i := 0; i < n; i++ {
			arr[i] = in.NextInt64()
		}
		for i := 0; i < n; i++ {
			if arr[i] > arr[i%k] {
				arr[i], arr[i%k] = arr[i%k], arr[i]
			}
		}
		sum := int64(0)
		for i := 0; i < k; i++ {
			sum += arr[i]
		}

		fmt.Println(sum)
	}
}

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

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

package main
// github.com/jamesgrimard/codeforces
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n,m int
		fmt.Fscan(in,&n,&m)
		a:= make([]int,n)
		for j:=0;j<n;j++{
			fmt.Fscan(in,&a[j])
		}
		if n == m {
			var add int
			for j:=0;j<n;j++{
				add += a[j]
			}
			fmt.Fprintln(out,add)
			continue
		}
		for j:=0;j<n-m;j++{
			if a[j] > a[j+m] {
				a[j],a[j+m] = a[j+m],a[j]
			}
		}
		var total int
		for j:=n-1;j>=m;j--{
			var sum int
			for k:=0;k<m;k++{
				sum += a[j-k]
			}
			if sum > total {
				total = sum
			}
		}
		fmt.Fprintln(out,total)
	}
}

// rearrange array to group peaks
// constraint : like indices can only be swapped i mod k == j mod k
// length of the peak corresponds to k consecutive elements
// find largest sum of peak

package main

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

func main() {
	var t, k, n, temp int

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

	fmt.Fscan(r, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(r, &n, &k)
		s := make([]int, k)
		for j := 0; j < n; j++ {
			fmt.Fscan(r, &temp)
			if temp > s[j % k] {
				s[j % k] = temp
			}
		}

		sum := 0
		for j := 0; j < k; j++ {
			sum = sum + s[j]
		}

		fmt.Fprintln(w, sum)
	}
}

package main

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

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

func Println(a ...interface{}) { _, _ = fmt.Fprintln(writer, a...) }

func main() {
	defer func() {
		_ = writer.Flush()
	}()

	T := ReadInt()
	for t := 0; t < T; t++ {
		Solve()
	}
}

func Solve() {
	n, k := ReadInt(), ReadInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = ReadInt()
	}
	for i := k; i < n; i++ {
		a[i%k] = Max(a[i], a[i%k])
	}
	ans := int64(0)
	for i := 0; i < k; i++ {
		ans += int64(a[i])
	}
	Println(ans)
}

func ReadInt() (value int) {
	_, _ = fmt.Fscan(reader, &value)
	return
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Float interface {
	~float32 | ~float64
}

type Ordered interface {
	Integer | Float | ~string
}

func Max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

package main

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

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

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

func main() {
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n, k int
		var num int64
		fmt.Fscan(in, &n, &k)
		res := make([]int64, k)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &num)
			res[i%k] = max(res[i%k], num)
		}
		sum := int64(0)
		for i := 0; i < k; i++ {
			sum += res[i]
		}
		fmt.Fprintln(out, sum)
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	txt, _ := reader.ReadString('\n')
	t, _ := strconv.Atoi(strings.TrimSpace(txt))
	for ; t > 0; t-- {
		txt, _ = reader.ReadString('\n')
		arr := strings.Split(strings.TrimSpace(txt), " ")
		n, _ := strconv.Atoi(arr[0])
		k, _ := strconv.Atoi(arr[1])
		txt, _ = reader.ReadString('\n')
		arr = strings.Split(strings.TrimSpace(txt), " ")
		digits := make([]int, n, n)
		a := make(map[int]int)
		for i := 0; i < n; i++ {
			digits[i], _ = strconv.Atoi(arr[i])
			if a[i%k] < digits[i] {
				a[i%k] = digits[i]
			}
		}
		s := 0
		for i := 0; i < k; i++ {
			s += a[i]
		}
		fmt.Println(s)
	}
}

package main

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

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

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

func solve(n, k int, a []int64) int64 {
	b := make([]int64, k)
	for i, v := range a {
		b[i%k] = max(b[i%k], v)
	}
	r := int64(0)
	for _, v := range b {
		r += v
	}
	return r
}

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

package main

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

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

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

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

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

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

    sum := int64(0)
    for _, v := range best {
      sum += int64(v)
    }

    fmt.Fprintln(out, sum)
  }
}

package main

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

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

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

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

		aa := make([]int64, k)
		for i := 0; i < n; i++ {
			var v int64
			fmt.Fscan(in, &v)
			aa[i%k] = max(aa[i%k], v)
		}

		var tot int64
		for _, v := range aa {
			tot += v
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

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

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

func main() {

	defer out.Flush()

	t := 0
	if _, err := fmt.Fscan(in, &t); err != nil {
		return
	}

	for ; t > 0; t-- {

		n, k := 0, 0
		if _, err := fmt.Fscan(in, &n, &k); err != nil {
			return
		}

		nums := make([]int, n)
		mxNums := make([]int, k)
		//mxIdxs := make(map[int]int)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &nums[i])
			mxNums[i%k] = max(mxNums[i%k], nums[i])
		}

		ans := 0
		for i := 0; i < k; i++ {
			ans += mxNums[i]
		}

		fmt.Fprintln(out, ans)
	}
}

package main

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

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

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}
func numDigit(n int) int {
	cnt := 0
	for n > 0 {
		cnt++
		n /= 10
	}
	return cnt
}
func solve() {
	var n, k, x int
	scan(&n, &k)
	a := make([]int, k+1)
	for i := 1; i <= n; i++ {
		scan(&x)
		id := i % k
		if id == 0 {
			id = k
		}
		if x > a[id] {
			// printf("%d-%d\n", i, x)
			a[id] = x
		}
	}
	res := 0
	for i := 1; i <= k; i++ {
		res += a[i]
	}

	printf("%d\n", res)
}
func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

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

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

	var T int
	var n int
	var k int
	var x int

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

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		Fscan(in, &k)

		r := make([]int, n)
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			r[i] = x
		}

		ma := make([]int, k)
		for i := 0; i < n; i++ {
			ma[i%k] = max(ma[i%k], r[i])
		}

		ans := int64(0)
		for _, v := range ma {
			ans += int64(v)
		}

		Fprintln(out, ans)
	}
}

package main

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

var a [105]int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var T int
	fmt.Fscan(in, &T)
	for cs := 0; cs < T; cs++ {
		var n, k int
		fmt.Fscan(in, &n, &k)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		ans := int64(0)
		for i := 0; i < k; i++ {
			max := 0
			for j := i; j < n; j += k {
				if max < a[j] {
					max = a[j]
				}
			}
			ans += int64(max)
		}
		fmt.Fprintln(out, ans)
	}
}

package main

import (
	"fmt"
	"os"
)

func packAddrs(n []int) []interface{} {
	p := make([]interface{}, len(n))
	for i := range n {
		p[i] = &n[i]
	}
	return p
}

func main() {
	var l int
	fmt.Scanf("%d\n", &l)
	res := make([]int, l)
	for i := 0; i < l; i++ {
		var n, k int
		fmt.Scanf("%d %d\n", &n, &k)
		a := make([]int, n)
		fmt.Fscan(os.Stdin, packAddrs(a)...)
		//fmt.Println(SumOfSeq(k, a))
		res[i] = SumOfSeq(k, a)
	}
	for i := 0; i < l; i++ {
		fmt.Println(res[i])
	}
}

func SumOfSeq(k int, a []int) int {
	for i := k; i < len(a); i++ {
		if rem := i % k; rem < k && a[i] > a[rem] {
			a[i], a[rem] = a[rem], a[i]
		}
	}
	res := 0
	for i := 0; i < k; i++ {
		res += a[i]
	}
	return res
}

package main

import (
	"fmt"
	"os"
)

func packAddrs(n []int) []interface{} {
	p := make([]interface{}, len(n))
	for i := range n {
		p[i] = &n[i]
	}
	return p
}

func main() {
	var l int
	fmt.Scanf("%d\n", &l)
	for i := 0; i < l; i++ {
		var n, k int
		fmt.Scanf("%d %d\n", &n, &k)
		a := make([]int, n)
		fmt.Fscan(os.Stdin, packAddrs(a)...)
		//fmt.Println(a)
		fmt.Println(SumOfSeq(k, a))
	}
}

func SumOfSeq(k int, a []int) int {
	for i := k; i < len(a); i++ {
		if rem := i % k; rem < k && a[i] > a[rem] {
			a[i], a[rem] = a[rem], a[i]
		}
	}
	res := 0
	for i := 0; i < k; i++ {
		res += a[i]
	}
	return res
}

package main

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

var (
	input  = bufio.NewReaderSize(os.Stdin, 55000)
	output = bufio.NewWriterSize(os.Stdout, 55000)
)

func main() {
	var t int
	fmt.Fscan(input, &t)
	for t >= 0 {
		var k, n int
		fmt.Fscan(input, &n, &k)

		a := make([]int, k)
		for i := 0; i < n; i++ {
			var val int
			fmt.Fscan(input, &val)
			if a[i%k] < val {
				a[i%k] = val
			}
		}

		sum := 0
		for i := 0; i < k; i++ {
			sum += a[i]
		}

		fmt.Println(sum)

		t--
	}
}

package main

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

func main() {
	var testCases int

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

	fmt.Fscan(reader, &testCases)

	for testCase := 1; testCase <= testCases; testCase++ {
		var n, k int
		fmt.Fscan(reader, &n)
		fmt.Fscan(reader, &k)

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

		output := solveA(n, k, &a)

		fmt.Fprintln(writer, output)
	}
}

func solveA(n, k int, a *[]int) int {

	// kConsecutive := make([]int, k)
	ki := 1
	for i := 0; i < n && ki <= k; i++ {
		for j := i + 1; j < n && ki <= k; j++ {
			if i%k == j%k {
				if (*a)[i] > (*a)[j] {
					(*a)[i], (*a)[j] = (*a)[j], (*a)[i]
					ki++
				}
			}
		}
	}
	sum := 0

	for ki := 1; ki <= k; ki++ {
		sum += (*a)[n-ki]
	}
	return sum
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1733A
Date: 23/09/22
User: pepradere
URL: https://codeforces.com/problemset/problem/1733/A
Problem: CF1733A
**/
func (in *CF1733A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, k := in.NextInt(), in.NextInt()
		maxi := int64(0)
		arr := make([]int, n+1)
		for i := 0; i < n; i++ {
			arr[i] = in.NextInt()
		}

		for i := k; i < len(arr); i++ {
			arr[i], arr[i%k] = arr[i%k], arr[i]
			for j := k; j < len(arr); j++ {
				sum := int64(0)
				for p, q := 0, j; p < k; p++ {
					sum += int64(arr[q])
					q--
				}
				if sum > maxi {
					maxi = sum
				}
			}
			arr[i], arr[i%k] = arr[i%k], arr[i]
		}

		fmt.Println(maxi)
	}
}

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

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


General Payne has a battalion of n soldiers. The soldiers' beauty contest is coming up, it will last for k days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants.All soldiers in the battalion have different beauty that is represented by a positive integer. The value ai represents the beauty of the i-th soldier.On each of k days Generals has to send a detachment of soldiers to the pageant. The beauty of the detachment is the sum of the beauties of the soldiers, who are part of this detachment. Payne wants to surprise the jury of the beauty pageant, so each of k days the beauty of the sent detachment should be unique. In other words, all k beauties of the sent detachments must be distinct numbers.Help Payne choose k detachments of different beauties for the pageant. Please note that Payne cannot just forget to send soldiers on one day, that is, the detachment of soldiers he sends to the pageant should never be empty.

The first line contains two integers n, k (1 ≤ n ≤ 50; 1 ≤ k ≤  ) — the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 107) — the beauties of the battalion soldiers. It is guaranteed that Payne's battalion doesn't have two soldiers with the same beauty.

Print k lines: in the i-th line print the description of the detachment that will participate in the pageant on the i-th day. The description consists of integer ci (1 ≤ ci ≤ n) — the number of soldiers in the detachment on the i-th day of the pageant and ci distinct integers p1, i, p2, i, ..., pci, i — the beauties of the soldiers in the detachment on the i-th day of the pageant. The beauties of the soldiers are allowed to print in any order. Separate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them.

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()
	n, k := scanInt(scanner), scanInt(scanner)
	soldiers := make([]int64, n)
	for i := 0; i < n; i++ {
		soldiers[i] = int64(scanInt(scanner))
	}
	seen := map[int64]int64{}
	seen[int64(0)] = int64(0)
	count := 0
	for {
		for beauty, bits := range seen {
			for i, x := range soldiers {
				if bits&(int64(1) << uint(i)) != 0 {
					continue
				}
				_, present := seen[x+beauty]
				if present {
					continue
				}
				//writer.WriteString(fmt.Sprintf("%d+%d = %d\n", x, beauty, x+beauty))
				newBits := bits|(int64(1) << uint(i))
				seen[x+beauty] = newBits
				group := []int64{}
				for j := range soldiers {
					if newBits&(int64(1) << uint(j)) != 0 {
						group = append(group, soldiers[j])
					}
				}
				writer.WriteString(fmt.Sprintf("%d", len(group)))
				for _, y := range group {
					writer.WriteString(fmt.Sprintf(" %d", y))
				}
				writer.WriteString("\n")
				count++
				if count == k {
					return
				}
			}
		}
	}
}


Polycarp likes arithmetic progressions. A sequence $$$[a_1, a_2, \dots, a_n]$$$ is called an arithmetic progression if for each $$$i$$$ ($$$1 \le i < n$$$) the value $$$a_{i+1} - a_i$$$ is the same. For example, the sequences $$$[42]$$$, $$$[5, 5, 5]$$$, $$$[2, 11, 20, 29]$$$ and $$$[3, 2, 1, 0]$$$ are arithmetic progressions, but $$$[1, 0, 1]$$$, $$$[1, 3, 9]$$$ and $$$[2, 3, 1]$$$ are not.It follows from the definition that any sequence of length one or two is an arithmetic progression.Polycarp found some sequence of positive integers $$$[b_1, b_2, \dots, b_n]$$$. He agrees to change each element by at most one. In the other words, for each element there are exactly three options: an element can be decreased by $$$1$$$, an element can be increased by $$$1$$$, an element can be left unchanged.Determine a minimum possible number of elements in $$$b$$$ which can be changed (by exactly one), so that the sequence $$$b$$$ becomes an arithmetic progression, or report that it is impossible.It is possible that the resulting sequence contains element equals $$$0$$$.

The first line contains a single integer $$$n$$$ $$$(1 \le n \le 100\,000)$$$ — the number of elements in $$$b$$$. The second line contains a sequence $$$b_1, b_2, \dots, b_n$$$ $$$(1 \le b_i \le 10^{9})$$$.

If it is impossible to make an arithmetic progression with described operations, print -1. In the other case, print non-negative integer — the minimum number of elements to change to make the given sequence becomes an arithmetic progression. The only allowed operation is to add/to subtract one from an element (can't use operation twice to the same position).

NoteIn the first example Polycarp should increase the first number on $$$1$$$, decrease the second number on $$$1$$$, increase the third number on $$$1$$$, and the fourth number should left unchanged. So, after Polycarp changed three elements by one, his sequence became equals to $$$[25, 20, 15, 10]$$$, which is an arithmetic progression.In the second example Polycarp should not change anything, because his sequence is an arithmetic progression.In the third example it is impossible to make an arithmetic progression.In the fourth example Polycarp should change only the first element, he should decrease it on one. After that his sequence will looks like $$$[0, 3, 6, 9, 12]$$$, which is an arithmetic progression.

package main

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

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

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())

	a := make([]int, n)

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

	if n == 1 {
		fmt.Println(0)
		return
	}

	ans := n + 1

	for j := a[0] - 1; j <= a[0]+1; j++ {
		diff := a[1] - j
		if t := check(a, j, diff-1); t != -1 {
			ans = min(ans, t)
		}
		if t := check(a, j, diff); t != -1 {
			ans = min(ans, t)
		}
		if t := check(a, j, diff+1); t != -1 {
			ans = min(ans, t)
		}
	}
	if ans == n+1 {
		fmt.Println(-1)
		return
	}

	fmt.Println(ans)
}

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

func check(a []int, start int, step int) int {
	answer := 0
	next := start

	for i := 0; i < len(a); i++ {
		if a[i]-next == 0 {
			next += step
			continue
		}
		if a[i]-next == -1 || a[i]-next == 1 {
			next += step
			answer++
			continue
		}
		return -1
	}

	return answer
}

package main

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

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

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

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

	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])
	}
	if n < 3 {
		fmt.Fprintln(out, "0")
		out.Flush()
		os.Exit(0)
	}
	ret := int(1e8)
	d := [3]int{-1, 0, 1}
	for j := 0; j < 3; j++ {
		b[0] = a[0] + d[j]
		if b[0] < 0 {
			continue
		}
		for k := 0; k < 3; k++ {
			b[1] = a[1] + d[k]
			if b[1] < 0 {
				continue
			}
			t := b[1] - b[0]
			flag := true
			tmp := 0
			if b[0] != a[0] {
				tmp++
			}
			if b[1] != a[1] {
				tmp++
			}
			for i := 2; i < n; i++ {
				if abs(b[i-1]+t-a[i]) > 1 || b[i-1]+t < 0 {
					flag = false
					break
				}
				if b[i-1]+t != a[i] {
					tmp++
				}
				b[i] = b[i-1] + t
			}
			if flag {
				ret = min(ret, tmp)
			}
		}
	}
	if ret > 1e7 {
		fmt.Fprintln(out, "-1")
	} else {
		fmt.Fprintln(out, ret)
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF978D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	abs := func(x int) int {
		if x < 0 {
			return -x
		}
		return x
	}

	var n int
	Fscan(in, &n)
	if n < 3 {
		Fprint(out, 0)
		return
	}
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	ans := n + 1
	for i := -2; i <= 2; i++ {
	o:
		for a0 := a[0] - 1; a0 <= a[0]+1; a0++ {
			c, d := 0, (a[n-1]-a[0])/n+i
			for j, v := range a {
				f := abs(a0 + j*d - v)
				if f > 1 {
					continue o
				}
				if f == 1 {
					c++
				}
			}
			if c < ans {
				ans = c
			}
		}
	}
	if ans == n+1 {
		ans = -1
	}
	Fprint(out, ans)
}

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

package main

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

func main() {
	var n int
	fmt.Scanln(&n)

	var line string
	scanner := bufio.NewScanner(os.Stdin)
	buf := make([]byte, 0, 12*100000)
	scanner.Buffer(buf, 12*100000)

	if scanner.Scan() {
		line = scanner.Text()
	}

	strList := strings.Split(line, " ")

	list := make([]int, n)
	for i := 0; i < n; i++ {
		list[i], _ = strconv.Atoi(strList[i])
	}

	if n < 3 {
		fmt.Println("0")
		return
	}

	changes := [][]int{
		{0, 0},
		{1, 0},
		{0, 1},
		{1, 1},
		{-1, 0},
		{0, -1},
		{-1, -1},
		{1, -1},
		{-1, 1},
	}

	min := n + 1
	for _, change := range changes {
		//fmt.Print(change)
		first := list[0] + change[0]
		last := list[n-1] + change[1]

		diff := (float64)(last-first) / (float64)(n-1)
		if diff != math.Trunc(diff) {
			//fmt.Println()
			continue
		}

		count := 0
		for i, v := range list {
			listN := first + i*(int)(diff)
			//fmt.Printf(" -> %d %d", listN, v)
			listN -= v
			if listN > 1 || listN < -1 {
				count = -1
				break
			} else if listN != 0 {
				count++
			}
		}

		//fmt.Println()

		if min > count && count >= 0 {
			min = count
		}
	}

	if min == n+1 {
		fmt.Println("-1")
	} else {
		fmt.Println(min)
	}

}

package main

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

const (
	MaxN = 100005
)

var delta = []int{-1, 0, 1}
var n int
var a [MaxN]int
var best int

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

func check(a []int, delta, cnt int) int {
	l := a[1]
	for i := 2; i < len(a); i++ {
		l += delta
		k := abs(l - a[i])
		if k <= 1 {
			cnt += k
		} else {
			return -1
		}
	}
	return cnt
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}

	if n < 3 {
		fmt.Println(0)
		return
	}

	best = MaxN
	for _, dx := range delta {
		for _, dy := range delta {
			a[0] += dx
			a[1] += dy
			cnt := abs(dx) + abs(dy)
			m := check(a[:n], a[1] - a[0], cnt)
			if m != -1 && m < best {
				best = m
			}
			a[0] -= dx
			a[1] -= dy
		}
	}
	if best == MaxN {
		fmt.Println(-1)
	} else {
		fmt.Println(best)
	}
}

package main

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

func abs(x int) int {
	if x >= 0 {
		return x
	}

	return -x
}

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

	var n int

	fmt.Fscan(in, &n)

	b := make([]int, n)

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

	if n <= 2 {
		fmt.Fprintln(out, 0)
		return
	}

	bestRes := -1
	c := make([]int, n)
	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			fail := false
			for k := 0; k < n; k++ {
				c[k] = b[k]
			}

			res := 0
			if i != 0 {
				res++
			}
			if j != 0 {
				res++
			}

			c[0] += i
			c[1] += j

			d := c[1] - c[0]

			for k := 2; k < n; k++ {
				r := abs(c[k] - (c[k-1] + d))
				if r == 1 {
					res++
					c[k] = c[k-1] + d
				} else if r > 1 {
					fail = true
					break
				}
			}

			if !fail {
				if bestRes == -1 || res < bestRes {
					bestRes = res
				}
			}

		}
	}

	fmt.Fprintln(out, bestRes)
}

package main

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

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

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())

	a := make([]int, n)

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

	sort.Ints(a)

	if n == 1 {
		fmt.Println(0)
		return
	}

	ans := n + 1
	diff := a[1] - a[0]

	for j := a[0] - 1; j <= a[0]+1; j++ {
		if t := check(a, j, diff-1); t != -1 {
			ans = min(ans, t)
		}
		if t := check(a, j, diff); t != -1 {
			ans = min(ans, t)
		}
		if t := check(a, j, diff+1); t != -1 {
			ans = min(ans, t)
		}
	}

	fmt.Println(ans)
}

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

func check(a []int, start int, step int) int {
	answer := 0
	next := start

	for i := 0; i < len(a); i++ {
		if a[i]-next == 0 {
			next += step
			continue
		}
		if a[i]-next == -1 || a[i]-next == 1 {
			next += step
			answer++
			continue
		}
		return -1
	}

	return answer
}

package main

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

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

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())

	a := make([]int, n)

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

	sort.Ints(a)

	if n == 1 {
		fmt.Println(0)
		return
	}

	ans := n + 1
	diff := a[1] - a[0]

	for j := a[0] - 1; j <= a[0]+1; j++ {
		if t := check(a, j, diff-1); t != -1 {
			ans = min(ans, t)
		}
		if t := check(a, j, diff); t != -1 {
			ans = min(ans, t)
		}
		if t := check(a, j, diff+1); t != -1 {
			ans = min(ans, t)
		}
	}
	if ans == n+1 {
		fmt.Println(-1)
		return
	}

	fmt.Println(ans)
}

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

func check(a []int, start int, step int) int {
	answer := 0
	next := start

	for i := 0; i < len(a); i++ {
		if a[i]-next == 0 {
			next += step
			continue
		}
		if a[i]-next == -1 || a[i]-next == 1 {
			next += step
			answer++
			continue
		}
		return -1
	}

	return answer
}

package main

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

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

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())

	a := make([]int, n)

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

	sort.Ints(a)

	if n == 1 {
		fmt.Println(0)
		return
	}

	ans := n + 1

	for j := a[0] - 1; j <= a[0]+1; j++ {
		diff := a[1] - j
		if t := check(a, j, diff-1); t != -1 {
			ans = min(ans, t)
		}
		if t := check(a, j, diff); t != -1 {
			ans = min(ans, t)
		}
		if t := check(a, j, diff+1); t != -1 {
			ans = min(ans, t)
		}
	}
	if ans == n+1 {
		fmt.Println(-1)
		return
	}

	fmt.Println(ans)
}

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

func check(a []int, start int, step int) int {
	answer := 0
	next := start

	for i := 0; i < len(a); i++ {
		if a[i]-next == 0 {
			next += step
			continue
		}
		if a[i]-next == -1 || a[i]-next == 1 {
			next += step
			answer++
			continue
		}
		return -1
	}

	return answer
}

package main

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

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

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

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

	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])
	}
	if n < 3 {
		fmt.Fprintln(out, 0)
		os.Exit(0)
	}
	ret := int(1e8)
	d := [3]int{-1, 0, 1}
	for j := 0; j < 3; j++ {
		b[0] = a[0] + d[j]
		if b[0] < 0 {
			continue
		}
		for k := 0; k < 3; k++ {
			b[1] = a[1] + d[k]
			if b[1] < 0 {
				continue
			}
			t := b[1] - b[0]
			flag := true
			tmp := 0
			if b[0] != a[0] {
				tmp++
			}
			if b[1] != a[1] {
				tmp++
			}
			for i := 2; i < n; i++ {
				if abs(b[i-1]+t-a[i]) > 1 || b[i-1]+t < 0 {
					flag = false
					break
				}
				if b[i-1]+t != a[i] {
					tmp++
				}
				b[i] = b[i-1] + t
			}
			if flag {
				ret = min(ret, tmp)
			}
		}
	}
	if ret > 1e7 {
		fmt.Fprintln(out, "-1")
	} else {
		fmt.Fprintln(out, ret)
	}
}

package main

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

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

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

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

	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])
	}
	ret := int(1e8)
	d := [3]int{-1, 0, 1}
	for j := 0; j < 3; j++ {
		b[0] = a[0] + d[j]
		if b[0] < 0 {
			continue
		}
		for k := 0; k < 3; k++ {
			b[1] = a[1] + d[k]
			if b[1] < 0 {
				continue
			}
			t := b[1] - b[0]
			flag := true
			tmp := 0
			if b[0] != a[0] {
				tmp++
			}
			if b[1] != a[1] {
				tmp++
			}
			for i := 2; i < n; i++ {
				if abs(b[i-1]+t-a[i]) > 1 || b[i-1]+t < 0 {
					flag = false
					break
				}
				if b[i-1]+t == a[i] {
					tmp++
				}
				b[i] = b[i-1] + t
			}
			if flag {
				ret = min(ret, tmp)
			}
		}
	}
	if ret > 1e7 {
		fmt.Fprintln(out, "-1")
	} else {
		fmt.Fprintln(out, ret)
	}
}


Kuro is living in a country called Uberland, consisting of $$$n$$$ towns, numbered from $$$1$$$ to $$$n$$$, and $$$n - 1$$$ bidirectional roads connecting these towns. It is possible to reach each town from any other. Each road connects two towns $$$a$$$ and $$$b$$$. Kuro loves walking and he is planning to take a walking marathon, in which he will choose a pair of towns $$$(u, v)$$$ ($$$u \neq v$$$) and walk from $$$u$$$ using the shortest path to $$$v$$$ (note that $$$(u, v)$$$ is considered to be different from $$$(v, u)$$$).Oddly, there are 2 special towns in Uberland named Flowrisa (denoted with the index $$$x$$$) and Beetopia (denoted with the index $$$y$$$). Flowrisa is a town where there are many strong-scent flowers, and Beetopia is another town where many bees live. In particular, Kuro will avoid any pair of towns $$$(u, v)$$$ if on the path from $$$u$$$ to $$$v$$$, he reaches Beetopia after he reached Flowrisa, since the bees will be attracted with the flower smell on Kuro’s body and sting him.Kuro wants to know how many pair of city $$$(u, v)$$$ he can take as his route. Since he’s not really bright, he asked you to help him with this problem.

The first line contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$1 \leq n \leq 3 \cdot 10^5$$$, $$$1 \leq x, y \leq n$$$, $$$x \ne y$$$) - the number of towns, index of the town Flowrisa and index of the town Beetopia, respectively. $$$n - 1$$$ lines follow, each line contains two integers $$$a$$$ and $$$b$$$ ($$$1 \leq a, b \leq n$$$, $$$a \ne b$$$), describes a road connecting two towns $$$a$$$ and $$$b$$$. It is guaranteed that from each town, we can reach every other town in the city using the given roads. That is, the given map of towns and roads is a tree.

A single integer resembles the number of pair of towns $$$(u, v)$$$ that Kuro can use as his walking route.

NoteOn the first example, Kuro can choose these pairs:   $$$(1, 2)$$$: his route would be $$$1 \rightarrow 2$$$,  $$$(2, 3)$$$: his route would be $$$2 \rightarrow 3$$$,  $$$(3, 2)$$$: his route would be $$$3 \rightarrow 2$$$,  $$$(2, 1)$$$: his route would be $$$2 \rightarrow 1$$$,  $$$(3, 1)$$$: his route would be $$$3 \rightarrow 2 \rightarrow 1$$$. Kuro can't choose pair $$$(1, 3)$$$ since his walking route would be $$$1 \rightarrow 2 \rightarrow 3$$$, in which Kuro visits town $$$1$$$ (Flowrisa) and then visits town $$$3$$$ (Beetopia), which is not allowed (note that pair $$$(3, 1)$$$ is still allowed because although Kuro visited Flowrisa and Beetopia, he did not visit them in that order).On the second example, Kuro can choose the following pairs:   $$$(1, 2)$$$: his route would be $$$1 \rightarrow 2$$$,  $$$(2, 1)$$$: his route would be $$$2 \rightarrow 1$$$,  $$$(3, 2)$$$: his route would be $$$3 \rightarrow 1 \rightarrow 2$$$,  $$$(3, 1)$$$: his route would be $$$3 \rightarrow 1$$$.

package main

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

var in = bufio.NewReader(os.Stdin)
var out = bufio.NewWriter(os.Stdout)
var to [][]int
var vis []bool

func dfs(n, f, t int) int {
	vis[n] = true
	ans := 1
	if n == t {
		return -1
	}
	for _, i := range to[n] {
		if vis[i] {
			continue
		}
		ret := dfs(i, f, t)
		if ret == -1 && n != f {
			return -1
		}
		if ret > 0 {
			ans += ret
		}
	}
	return ans
}

func main() {
	defer out.Flush()

	var n, x, y, u, v int
	fmt.Fscan(in, &n, &x, &y)
	to = make([][]int, n+1)
	vis = make([]bool, n+1)

	for i := 1; i <= n; i++ {
		to[i] = make([]int, 0)
	}
	for i := 1; i < n; i++ {
		fmt.Fscan(in, &u, &v)
		to[u] = append(to[u], v)
		to[v] = append(to[v], u)
	}

	a1 := dfs(x, x, y)
	vis = make([]bool, n+1)
	a2 := dfs(y, y, x)
	fmt.Fprintln(out, int64(n)*int64(n-1) - int64(a1)*int64(a2))
}

package main

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

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

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

	size := make([]int, n+1)
	fa := make([]int, n+1)
	var f func(int, int) int
	f = func(v, p int) int {
		fa[v] = p
		sz := 1
		for _, w := range g[v] {
			if w != p {
				sz += f(w, v)
			}
		}
		size[v] = sz
		return sz
	}
	f(flowrisa, 0)
	v = beetopia
	for ; fa[v] != flowrisa; v = fa[v] {
	}
	Fprint(out, n*(n-1)-(n-int64(size[v]))*int64(size[beetopia]))
}

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

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

package main

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

type Edge struct {
	from int
	to   int
}

func find(x int, y int, graph map[int][]int) map[int]bool {
	visited := make(map[int]bool)
	stack := []int{x}
	visited[x] = true
	for len(stack) > 0 {
		currentNode := stack[len(stack)-1]
		if currentNode == y {
			break
		}
		flag := true
		for _, vertex := range graph[currentNode] {
			if !visited[vertex] {
				visited[vertex] = true
				stack = append(stack, vertex)
				flag = false
				break
			}
		}
		if flag {
			stack = stack[:len(stack)-1]
		}
	}
	shortestPathSet := make(map[int]bool)
	for _, vertex := range stack {
		shortestPathSet[vertex] = true
	}
	return shortestPathSet
}

func bfs(root int, graph map[int][]int, shortestPathSet map[int]bool) int {
	queue := []int{root}
	visited := make(map[int]bool)
	count := 0
	for len(queue) > 0 {
		currentNode := queue[0]
		queue = queue[1:]
		count += 1
		for _, vertex := range graph[currentNode] {
			if !visited[vertex] && !shortestPathSet[vertex] {
				visited[vertex] = true
				queue = append(queue, vertex)
			}
		}
	}
	return count
}

func Solve(n int, x int, y int, edges []Edge) int64 {
	graph := make(map[int][]int)
	for _, edge := range edges {
		graph[edge.from] = append(graph[edge.from], edge.to)
		graph[edge.to] = append(graph[edge.to], edge.from)
	}
	shortestPathSet := find(x, y, graph)
	xCount := bfs(x, graph, shortestPathSet)
	yCount := bfs(y, graph, shortestPathSet)
	return int64(n)*int64(n-1) - int64(xCount)*int64(yCount)
}

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

	var n, x, y int
	fmt.Fscanf(reader, "%d %d %d\n", &n, &x, &y)
	edges := make([]Edge, n-1)
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(reader, "%d %d\n", &edges[i].from, &edges[i].to)
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, x, y, edges))
}

package main

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

var in = bufio.NewReader(os.Stdin)
var out = bufio.NewWriter(os.Stdout)
var to [][]int
var vis []bool

func dfs(n, f, t int) int {
	vis[n] = true
	ans := 1
	if n == t {
		return -1
	}
	for _, i := range to[n] {
		if vis[i] {
			continue
		}
		ret := dfs(i, f, t)
		if ret == -1 && n != f {
			return -1
		}
		if ret > 0 {
			ans += ret
		}
	}
	return ans
}

func main() {
	defer out.Flush()

	var n, x, y, u, v int
	fmt.Fscan(in, &n, &x, &y)
	to = make([][]int, n+1)
	vis = make([]bool, n+1)

	for i := 1; i <= n; i++ {
		to[i] = make([]int, 0)
	}
	for i := 1; i < n; i++ {
		fmt.Fscan(in, &u, &v)
		to[u] = append(to[u], v)
		to[v] = append(to[v], u)
	}

	a1 := dfs(x, x, y)
	vis = make([]bool, n+1)
	a2 := dfs(y, y, x)
	fmt.Fprintln(out, n*(n-1) - a1*a2)
}

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

package main

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

type Edge struct {
	from int
	to   int
}

func find(x int, y int, graph map[int][]int) map[int]bool {
	visited := make(map[int]bool)
	stack := []int{x}
	visited[x] = true
	for len(stack) > 0 {
		currentNode := stack[len(stack)-1]
		if currentNode == y {
			break
		}
		flag := true
		for _, vertex := range graph[currentNode] {
			if !visited[vertex] {
				visited[vertex] = true
				stack = append(stack, vertex)
				flag = false
				break
			}
		}
		if flag {
			stack = stack[:len(stack)-1]
		}
	}
	shortestPathSet := make(map[int]bool)
	for _, vertex := range stack {
		shortestPathSet[vertex] = true
	}
	return shortestPathSet
}

func bfs(root int, graph map[int][]int, shortestPathSet map[int]bool) int {
	queue := []int{root}
	visited := make(map[int]bool)
	count := 0
	for len(queue) > 0 {
		currentNode := queue[0]
		queue = queue[1:]
		count += 1
		for _, vertex := range graph[currentNode] {
			if !visited[vertex] && !shortestPathSet[vertex] {
				visited[vertex] = true
				queue = append(queue, vertex)
			}
		}
	}
	return count
}

func Solve(n int, x int, y int, edges []Edge) int64 {
	graph := make(map[int][]int)
	for _, edge := range edges {
		graph[edge.from] = append(graph[edge.from], edge.to)
		graph[edge.to] = append(graph[edge.to], edge.from)
	}
	shortestPathSet := find(x, y, graph)
	xCount := bfs(x, graph, shortestPathSet)
	yCount := bfs(y, graph, shortestPathSet)
	return int64(n*(n-1)) - int64(xCount*yCount)
}

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

	var n, x, y int
	fmt.Fscanf(reader, "%d %d %d\n", &n, &x, &y)
	edges := make([]Edge, n-1)
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(reader, "%d %d\n", &edges[i].from, &edges[i].to)
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, x, y, edges))
}


You are given an array $$$d_1, d_2, \dots, d_n$$$ consisting of $$$n$$$ integer numbers.Your task is to split this array into three parts (some of which may be empty) in such a way that each element of the array belongs to exactly one of the three parts, and each of the parts forms a consecutive contiguous subsegment (possibly, empty) of the original array. Let the sum of elements of the first part be $$$sum_1$$$, the sum of elements of the second part be $$$sum_2$$$ and the sum of elements of the third part be $$$sum_3$$$. Among all possible ways to split the array you have to choose a way such that $$$sum_1 = sum_3$$$ and $$$sum_1$$$ is maximum possible.More formally, if the first part of the array contains $$$a$$$ elements, the second part of the array contains $$$b$$$ elements and the third part contains $$$c$$$ elements, then:$$$$$$sum_1 = \sum\limits_{1 \le i \le a}d_i,$$$$$$ $$$$$$sum_2 = \sum\limits_{a + 1 \le i \le a + b}d_i,$$$$$$ $$$$$$sum_3 = \sum\limits_{a + b + 1 \le i \le a + b + c}d_i.$$$$$$The sum of an empty array is $$$0$$$.Your task is to find a way to split the array such that $$$sum_1 = sum_3$$$ and $$$sum_1$$$ is maximum possible.

The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in the array $$$d$$$. The second line of the input contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$1 \le d_i \le 10^9$$$) — the elements of the array $$$d$$$.

Print a single integer — the maximum possible value of $$$sum_1$$$, considering that the condition $$$sum_1 = sum_3$$$ must be met. Obviously, at least one valid way to split the array exists (use $$$a=c=0$$$ and $$$b=n$$$).

NoteIn the first example there is only one possible splitting which maximizes $$$sum_1$$$: $$$[1, 3, 1], [~], [1, 4]$$$.In the second example the only way to have $$$sum_1=4$$$ is: $$$[1, 3], [2, 1], [4]$$$.In the third example there is only one way to split the array: $$$[~], [4, 1, 2], [~]$$$.

package main

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

func main() {
	var n int
	fmt.Scanln(&n)

	var line string
	scanner := bufio.NewScanner(os.Stdin)
	buf := make([]byte, 0, 12*200000)
	scanner.Buffer(buf, 12*200000)

	if scanner.Scan() {
		line = scanner.Text()
	}

	strList := strings.Split(line, " ")

	dArray := make([]int, n)
	for i := 0; i < n; i++ {
		dArray[i], _ = strconv.Atoi(strList[i])
	}

	si := 0
	ei := len(dArray) - 1

	var max, sum1, sum3 uint64
	max = 0
	sum1 = uint64(dArray[si])
	sum3 = uint64(dArray[ei])
	for si < ei {
		if sum1 > sum3 {
			ei--
			sum3 += uint64(dArray[ei])
		} else if sum3 > sum1 {
			si++
			sum1 += uint64(dArray[si])
		} else {
			max = sum1
			si++
			ei--
			sum1 += uint64(dArray[si])
			sum3 += uint64(dArray[ei])
		}
	}

	fmt.Println(max)
}

package main

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

const (
	MaxN = 200005
)

var n int
var a, forwards, backwards [MaxN]uint64

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &n)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(buf, "%d ", &a[i])
	}
	for i := 1; i <= n; i++ {
		forwards[i] += forwards[i - 1] + a[i]
		backwards[n - i + 1] += backwards[n - i + 2] + a[n - i + 1]
	}
	max := uint64(0)
	for i := n; i >= 1; i-- {
		index := sort.Search(i, func(pos int) bool {
			return forwards[pos] >= backwards[i]
		})
		if index != i && forwards[index] == backwards[i] && max < backwards[i] {
			max = backwards[i]
		}
	}
	fmt.Println(max)
}

package main

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

func main() {
	var n int
	fmt.Scanln(&n)

	var line string
	scanner := bufio.NewScanner(os.Stdin)
	buf := make([]byte, 0, 12*200000)
	scanner.Buffer(buf, 12*200000)

	if scanner.Scan() {
		line = scanner.Text()
	}

	strList := strings.Split(line, " ")

	dArray := make([]int, n)
	for i := 0; i < n; i++ {
		dArray[i], _ = strconv.Atoi(strList[i])
	}

	si := 0
	ei := len(dArray) - 1

	var max, sum1, sum3 uint64
	max = 0
	sum1 = uint64(dArray[si])
	sum3 = uint64(dArray[ei])
	for si <= ei {
		if sum1 > sum3 {
			ei--
			sum3 += uint64(dArray[ei])
		} else if sum3 > sum1 {
			si++
			sum1 += uint64(dArray[si])
		} else {
			max = sum1
			si++
			ei--
			sum1 += uint64(dArray[si])
			sum3 += uint64(dArray[ei])
		}
	}

	fmt.Println(max)
}

package main

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

const (
	MaxN = 200005
)

var n int
var a, forwards, backwards [MaxN]int

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &n)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(buf, "%d ", &a[i])
	}
	for i := 1; i <= n; i++ {
		forwards[i] += forwards[i - 1] + a[i]
		backwards[n - i + 1] += backwards[n - i + 2] + a[n - i + 1]
	}
	max := 0
	for i := n; i >= 1; i-- {
		index := sort.Search(i, func(pos int) bool {
			return forwards[pos] >= backwards[i]
		})
		if index != i && forwards[index] == backwards[i] && max < backwards[i] {
			max = backwards[i]
		}
	}
	fmt.Println(max)
}


This is an interactive problem.You're given a tree consisting of $$$n$$$ nodes, rooted at node $$$1$$$. A tree is a connected graph with no cycles.We chose a hidden node $$$x$$$. In order to find this node, you can ask queries of two types:   d $$$u$$$ ($$$1 \le u \le n$$$). We will answer with the distance between nodes $$$u$$$ and $$$x$$$. The distance between two nodes is the number of edges in the shortest path between them.  s $$$u$$$ ($$$1 \le u \le n$$$). We will answer with the second node on the path from $$$u$$$ to $$$x$$$. However, there's a plot twist. If $$$u$$$ is not an ancestor of $$$x$$$, you'll receive "Wrong answer" verdict! Node $$$a$$$ is called an ancestor of node $$$b$$$ if $$$a \ne b$$$ and the shortest path from node $$$1$$$ to node $$$b$$$ passes through node $$$a$$$. Note that in this problem a node is not an ancestor of itself.Can you find $$$x$$$ in no more than $$$36$$$ queries? The hidden node is fixed in each test beforehand and does not depend on your queries.

The first line contains the integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of nodes in the tree. Each of the next $$$n-1$$$ lines contains two space-separated integers $$$u$$$ and $$$v$$$ ($$$1 \le u,v \le n$$$) that mean there's an edge between nodes $$$u$$$ and $$$v$$$. It's guaranteed that the given graph is a tree.

To print the answer, print "! x" (without quotes).

NoteIn the first example, the hidden node is node $$$5$$$.  We first ask about the distance between node $$$x$$$ and node $$$2$$$. The answer is $$$3$$$, so node $$$x$$$ is either $$$4$$$ or $$$5$$$. We then ask about the second node in the path from node $$$3$$$ to node $$$x$$$. Note here that node $$$3$$$ is an ancestor of node $$$5$$$. We receive node $$$5$$$ as the answer. Finally, we report that the hidden node is node $$$5$$$.

package main

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

type (
	input struct {
		n int
		g [][]int
	}
	guess struct{ ans int }
	qIn   struct {
		tp string
		v  int
	}
	qOut struct{ v int }
)

// github.com/EndlessCheng/codeforces-go
func run(in input, Q func(qIn) qOut) (gs guess) {
	n, g := in.n, in.g
	type node struct{ d, sz, hson int }
	nodes := make([]node, n+1)
	var f func(v, fa, d int) int
	f = func(v, fa, d int) int {
		sz, hson := 1, -1
		for _, w := range g[v] {
			if w != fa {
				s := f(w, v, d+1)
				sz += s
				if hson == -1 || s > nodes[hson].sz {
					hson = w
				}
			}
		}
		nodes[v] = node{d, sz, hson}
		return sz
	}
	f(1, 0, 0)

	depX := Q(qIn{"d", 1}).v
	var f2 func(v int) int
	f2 = func(v int) int {
		hp := []int{v}
		for o := nodes[v]; o.hson != -1; o = nodes[o.hson] {
			hp = append(hp, o.hson)
		}
		end := hp[len(hp)-1]
		depAX := (depX + nodes[end].d - Q(qIn{"d", end}).v) / 2
		ax := hp[depAX-nodes[v].d]
		if depAX == depX {
			return ax
		}
		return f2(Q(qIn{"s", ax}).v)
	}
	gs.ans = f2(1)
	return
}

func ioq() {
	in := bufio.NewReader(os.Stdin)
	Q := func(q qIn) (resp qOut) { Println(q.tp, q.v); Fscan(in, &resp.v); return }
	d := input{}
	Fscan(in, &d.n)
	g := make([][]int, d.n+1)
	for i := 1; i < d.n; i++ {
		var v, w int
		Fscan(in, &v, &w)
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	d.g = g
	gs := run(d, Q)
	Println("!", gs.ans)
}

func main() { ioq() }

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	Q := func(tp string, v int) int { Println(tp, v); Fscan(in, &v); return v }
	var n, v, w int
	Fscan(in, &n)
	g := make([][]int, n+1)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	type node struct{ d, hson int }
	nodes := make([]node, n+1)
	var _f func(v, fa, d int) int
	_f = func(v, fa, d int) int {
		sz, maxSz, hson := 1, 0, -1
		for _, w := range g[v] {
			if w != fa {
				s := _f(w, v, d+1)
				sz += s
				if s > maxSz {
					maxSz = s
					hson = w
				}
			}
		}
		nodes[v] = node{d, hson}
		return sz
	}
	_f(1, 0, 0)

	depX := Q("d", 1)
	var f func(v int) int
	f = func(v int) int {
		hp := []int{v}
		for o := nodes[v]; o.hson != -1; o = nodes[o.hson] {
			hp = append(hp, o.hson)
		}
		end := hp[len(hp)-1]
		depAX := (depX + nodes[end].d - Q("d", end)) / 2
		ax := hp[depAX-nodes[v].d]
		if depAX == depX {
			return ax
		}
		return f(Q("s", ax))
	}
	Println("!", f(1))
}

package main

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

type (
	input struct {
		n int
		g [][]int
	}
	guess struct{ ans int }
	qIn   struct {
		tp string
		v  int
	}
	qOut struct{ v int }
)

// github.com/EndlessCheng/codeforces-go
func run(in input, Q func(qIn) qOut) (gs guess) {
	n, g := in.n, in.g
	type node struct{ d, sz, hson int }
	nodes := make([]node, n+1)
	var f func(v, fa, d int) int
	f = func(v, fa, d int) int {
		nodes[v] = node{d, 1, -1}
		o := &nodes[v]
		for _, w := range g[v] {
			if w != fa {
				sz := f(w, v, d+1)
				o.sz += sz
				if o.hson == -1 || sz > nodes[o.hson].sz {
					o.hson = w
				}
			}
		}
		return o.sz
	}
	f(1, 0, 0)

	depX := Q(qIn{"d", 1}).v
	var f2 func(v int) int
	f2 = func(v int) int {
		hp := []int{v}
		for o := nodes[v]; o.hson != -1; o = nodes[o.hson] {
			hp = append(hp, o.hson)
		}
		end := hp[len(hp)-1]
		depAX := (depX + nodes[end].d - Q(qIn{"d", end}).v) / 2
		ax := hp[depAX-nodes[v].d]
		if depAX == depX {
			return ax
		}
		return f2(Q(qIn{"s", ax}).v)
	}
	gs.ans = f2(1)
	return
}

func ioq() {
	in := bufio.NewReader(os.Stdin)
	Q := func(q qIn) (resp qOut) { Println(q.tp, q.v); Fscan(in, &resp.v); return }
	d := input{}
	Fscan(in, &d.n)
	g := make([][]int, d.n+1)
	for i := 1; i < d.n; i++ {
		var v, w int
		Fscan(in, &v, &w)
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	d.g = g
	gs := run(d, Q)
	Println("!", gs.ans)
}

func main() { ioq() }

package main

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

type (
	input struct {
		n int
		g [][]int
	}
	guess struct{ ans int }
	qIn   struct {
		tp string
		v  int
	}
	qOut struct{ v int }
)

// github.com/EndlessCheng/codeforces-go
func run(in input, Q func(qIn) qOut) (gs guess) {
	n, g := in.n, in.g
	type node struct{ dep, fa int }
	nodes := make([]node, n+1)
	var _f func(v, fa, d int)
	_f = func(v, fa, d int) {
		nodes[v] = node{d, fa}
		for _, w := range g[v] {
			if w != fa {
				_f(w, v, d+1)
			}
		}
	}
	_f(1, 0, 0)

	used := make([]bool, n+1)
	size := make([]int, n+1)
	var calcSize func(v, fa int) int
	calcSize = func(v, fa int) int {
		sz := 1
		for _, w := range g[v] {
			if w != fa && !used[w] {
				sz += calcSize(w, v)
			}
		}
		size[v] = sz
		return sz
	}
	var compSize int
	var find func(v, fa int) int
	find = func(v, fa int) int {
		for _, w := range g[v] {
			if w != fa && !used[w] && size[w] > compSize>>1 {
				return find(w, v)
			}
		}
		return v
	}

	depX := Q(qIn{"d", 1}).v
	var f func(v int) int
	f = func(v int) int {
		calcSize(v, 0)
		compSize = size[v]
		c := find(v, 0)
		d := Q(qIn{"d", c}).v
		if d == 0 {
			return c
		}
		used[c] = true
		o := nodes[c]
		if o.dep+d == depX {
			return f(Q(qIn{"s", c}).v)
		}
		return f(o.fa)
	}
	gs.ans = f(1)
	return
}

func ioq() {
	in := bufio.NewReader(os.Stdin)
	Q := func(q qIn) (resp qOut) { Println(q.tp, q.v); Fscan(in, &resp.v); return }
	d := input{}
	Fscan(in, &d.n)
	g := make([][]int, d.n+1)
	for i := 1; i < d.n; i++ {
		var v, w int
		Fscan(in, &v, &w)
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	d.g = g
	gs := run(d, Q)
	Println("!", gs.ans)
}

func main() { ioq() }

package main

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

type (
	input struct {
		n int
		g [][]int
	}
	guess struct{ ans int }
	qIn   struct {
		tp string
		v  int
	}
	qOut struct{ v int }
)

// github.com/EndlessCheng/codeforces-go
func run(in input, Q func(qIn) qOut) (gs guess) {
	n, g := in.n, in.g
	type node struct{ d, sz, hson int }
	nodes := make([]node, n+1)
	var f func(v, fa, d int) *node
	f = func(v, fa, d int) *node {
		nodes[v] = node{d, 1, -1}
		o := &nodes[v]
		for _, w := range g[v] {
			if w != fa {
				son := f(w, v, d+1)
				o.sz += son.sz
				if o.hson == -1 || son.sz > nodes[o.hson].sz {
					o.hson = w
				}
			}
		}
		return o
	}
	f(1, 0, 0)

	depX := Q(qIn{"d", 1}).v
	var f2 func(v int) int
	f2 = func(v int) int {
		hp := []int{v}
		for o := nodes[v]; o.hson != -1; o = nodes[o.hson] {
			hp = append(hp, o.hson)
		}
		end := hp[len(hp)-1]
		depAX := (depX + nodes[end].d - Q(qIn{"d", end}).v) / 2
		ax := hp[depAX-nodes[v].d]
		if depAX == depX {
			return ax
		}
		return f2(Q(qIn{"s", ax}).v)
	}
	gs.ans = f2(1)
	return
}

func ioq() {
	in := bufio.NewReader(os.Stdin)
	Q := func(q qIn) (resp qOut) { Println(q.tp, q.v); Fscan(in, &resp.v); return }
	d := input{}
	Fscan(in, &d.n)
	g := make([][]int, d.n+1)
	for i := 1; i < d.n; i++ {
		var v, w int
		Fscan(in, &v, &w)
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	d.g = g
	gs := run(d, Q)
	Println("!", gs.ans)
}

func main() { ioq() }

package main

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

type (
	input struct {
		n int
		g [][]int
	}
	guess struct{ ans int }
	qIn   struct {
		tp string
		v  int
	}
	qOut struct{ v int }
)

// github.com/EndlessCheng/codeforces-go
func run(in input, Q func(qIn) qOut) (gs guess) {
	n, g := in.n, in.g
	dep := make([]int, n)
	size := make([]int, n)
	var f func(v, fa, d int) int
	f = func(v, fa, d int) int {
		dep[v] = d
		sz := 1
		for _, w := range g[v] {
			if w != fa {
				sz += f(w, v, d+1)
			}
		}
		size[v] = sz
		return sz
	}
	f(0, -1, 0)
	getHP := func(st int) (hp []int) {
		var f func(v, fa int)
		f = func(v, fa int) {
			hp = append(hp, v)
			hson := -1
			for _, w := range g[v] {
				if w != fa && (hson == -1 || size[w] > size[hson]) {
					hson = w
				}
			}
			if hson != -1 {
				f(hson, v)
			}
		}
		f(st, -1)
		return
	}

	depX := Q(qIn{"d", 1}).v
	var f2 func(v int) int
	f2 = func(v int) int {
		hp := getHP(v)
		low := hp[len(hp)-1]
		depAX := (depX + dep[low] - Q(qIn{"d", low + 1}).v) / 2
		ax := hp[depAX-dep[v]]
		if depAX == depX {
			return ax
		}
		return f2(Q(qIn{"s", ax}).v - 1)
	}
	gs.ans = f2(0) + 1
	return
}

func ioq() {
	in := bufio.NewReader(os.Stdin)
	Q := func(q qIn) (resp qOut) { Println(q.tp, q.v); Fscan(in, &resp.v); return }
	d := input{}
	Fscan(in, &d.n)
	g := make([][]int, d.n)
	for i := 1; i < d.n; i++ {
		var v, w int
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	d.g = g
	gs := run(d, Q)
	Println("!", gs.ans)
}

func main() { ioq() }

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	Q := func(tp string, v int) int { Println(tp, v); Fscan(in, &v); return v }
	var n, v, w int
	Fscan(in, &n)
	g := make([][]int, n+1)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	type node struct{ d, sz, hson int }
	nodes := make([]node, n+1)
	var _f func(v, fa, d int) int
	_f = func(v, fa, d int) int {
		sz, hson := 1, -1
		for _, w := range g[v] {
			if w != fa {
				s := _f(w, v, d+1)
				sz += s
				if hson == -1 || sz > nodes[hson].sz {
					hson = w
				}
			}
		}
		nodes[v] = node{d, sz, hson}
		return sz
	}
	_f(1, 0, 0)

	depX := Q("d", 1)
	var f func(v int) int
	f = func(v int) int {
		hp := []int{v}
		for o := nodes[v]; o.hson != -1; o = nodes[o.hson] {
			hp = append(hp, o.hson)
		}
		end := hp[len(hp)-1]
		depAX := (depX + nodes[end].d - Q("d", end)) / 2
		ax := hp[depAX-nodes[v].d]
		if depAX == depX {
			return ax
		}
		return f(Q("s", ax))
	}
	Println("!", f(1))
}


Alexander is a well-known programmer. Today he decided to finally go out and play football, but with the first hit he left a dent on the new Rolls-Royce of the wealthy businessman Big Vova. Vladimir has recently opened a store on the popular online marketplace "Zmey-Gorynych", and offers Alex a job: if he shows his programming skills by solving a task, he'll work as a cybersecurity specialist. Otherwise, he'll be delivering some doubtful products for the next two years.You're given $$$n$$$ positive integers $$$a_1, a_2, \dots, a_n$$$. Using each of them exactly at once, you're to make such sequence $$$b_1, b_2, \dots, b_n$$$ that sequence $$$c_1, c_2, \dots, c_n$$$ is lexicographically maximal, where $$$c_i=GCD(b_1,\dots,b_i)$$$ - the greatest common divisor of the first $$$i$$$ elements of $$$b$$$. Alexander is really afraid of the conditions of this simple task, so he asks you to solve it.A sequence $$$a$$$ is lexicographically smaller than a sequence $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a \ne b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the sequence $$$a$$$ has a smaller element than the corresponding element in $$$b$$$.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^3$$$)  — the length of the sequence $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1,\dots,a_n$$$ ($$$1 \le a_i \le 10^3$$$)  — the sequence $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^3$$$.

For each test case output the answer in a single line  — the desired sequence $$$b$$$. If there are multiple answers, print any.

NoteIn the first test case of the example, there are only two possible permutations $$$b$$$  — $$$[2, 5]$$$ and $$$[5, 2]$$$: for the first one $$$c=[2, 1]$$$, for the second one $$$c=[5, 1]$$$.In the third test case of the example, number $$$9$$$ should be the first in $$$b$$$, and $$$GCD(9, 3)=3$$$, $$$GCD(9, 8)=1$$$, so the second number of $$$b$$$ should be $$$3$$$.In the seventh test case of the example, first four numbers pairwise have a common divisor (a power of two), but none of them can be the first in the optimal permutation $$$b$$$.

package main

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

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

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

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

var err error

func next() (n string) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextLong() (n int64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextDouble() (n float64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func Fill(a []int, v int) {
	for i := range a {
		a[i] = v
	}
}

func FillB(a []bool, v bool) {
	for i := range a {
		a[i] = v
	}
}

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 Gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return Gcd(b, a%b)
}

const mod = 1000000007

func Add(a, b int64) int64 {
	return (a + b) % mod
}

func Mult(a, b int64) int64 {
	return (a * b) % mod
}

func Sub(a, b int64) int64 {
	return (a - b + mod) % mod
}

func solve() {
	n := nextInt()

	arr := make([]int, n)
	for i := range arr {
		arr[i] = nextInt()
	}

	used := make([]bool, n)

	gcd := 0
	for i := 0; i < n; i++ {
		j, max := 0, 0
		for ind, v := range arr {
			if used[ind] {
				continue
			}
			curr := Gcd(gcd, v)
			if curr > max {
				j, max = ind, curr
			}
		}
		gcd = max
		printf("%d ", arr[j])
		used[j] = true
	}

	printf("\n")
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	if false {
		name := "foreign"
		file1, err := os.Open(name + ".in")
		if err != nil {
			log.Fatal(err)
		}
		file2, err := os.Create(name + ".out")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file1, 8192)
		out = bufio.NewWriterSize(file2, 8192)
	}
	defer out.Flush()
	// start := time.Now()

	for t, i := nextInt(), 1; t > 0; t, i = t-1, i+1 {
		// 	printf("Case #%d:\n", i)
		solve()
	}

	// printf("%v\n", time.Now().Sub(start))
}

package main

import (
	"fmt"
	"sort"
)

func gcd(a int, b int) int {
	if a < 0 {
		return -1
	}
	if b == 0 {
		return a
	}
	return gcd(b, a % b)
}

func main()  {
	//fileInput, _ := os.Open("input.txt")
	//os.Stdin = fileInput

	//fileOutput, _ := os.Create("output.txt")
	//os.Stdout = fileOutput

	var n, t int
	fmt.Scan(&t)

	for i := 0; i < t; i++ {
		fmt.Scan(&n)
		a := make([]int, n)
		for j := 0; j < n ; j++ {
			fmt.Scan(&a[j])
		}
		sort.Ints(a)
		curr := a[n-1]

		for j := 0; j < n; j++ {
			var maxK, maxGcd int
			maxGcd = -1
			for k := 0; k < n; k++ {
				gcd := gcd(a[k], curr)
				if gcd > maxGcd {
					maxGcd = gcd
					maxK = k
				}
			}
			fmt.Print(a[maxK], " ")
			a[maxK] = -1
			curr = maxGcd
		}
		fmt.Println("")
	}
}

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-- {
		n := io.NextInt()
		a := io.NextInts(n)

		prev := 0
		var ans []int
		for len(a) > 0 {
			var mx int
			for i, x := range a {
				if gcd(x, prev) > gcd(a[mx], prev) {
					mx = i
				}
			}
			prev = gcd(a[mx], prev)
			ans = append(ans, a[mx])
			a = append(a[:mx], a[mx+1:]...)
		}
		io.PrintlnInts(ans...)
	}
}

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

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	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) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, 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"
	"os"
	"sort"
)

func gcd(a int, b int) int {
	if a < 0 {
		return -1
	}
	if b == 0 {
		return a
	}
	return gcd(b, a % b)
}

func main()  {
	fileInput, _ := os.Open("input.txt")
	os.Stdin = fileInput

	//fileOutput, _ := os.Create("output.txt")
	//os.Stdout = fileOutput

	var n, t int
	fmt.Scan(&t)

	for i := 0; i < t; i++ {
		fmt.Scan(&n)
		a := make([]int, n)
		for j := 0; j < n ; j++ {
			fmt.Scan(&a[j])
		}
		sort.Ints(a)
		curr := a[n-1]

		for j := 0; j < n; j++ {
			var maxK, maxGcd int
			maxGcd = -1
			for k := 0; k < n; k++ {
				gcd := gcd(a[k], curr)
				if gcd > maxGcd {
					maxGcd = gcd
					maxK = k
				}
			}
			fmt.Print(a[maxK], " ")
			a[maxK] = -1
			curr = maxGcd
		}
		fmt.Println("")
	}
}