Yaroslav thinks that two strings s and w, consisting of digits and having length n are non-comparable if there are two numbers, i and j (1 ≤ i, j ≤ n), such that si > wi and sj < wj. Here sign si represents the i-th digit of string s, similarly, wj represents the j-th digit of string w.A string's template is a string that consists of digits and question marks ("?").Yaroslav has two string templates, each of them has length n. Yaroslav wants to count the number of ways to replace all question marks by some integers in both templates, so as to make the resulting strings incomparable. Note that the obtained strings can contain leading zeroes and that distinct question marks can be replaced by distinct or the same integers.Help Yaroslav, calculate the remainder after dividing the described number of ways by 1000000007 (109 + 7).

The first line contains integer n (1 ≤ n ≤ 105) — the length of both templates. The second line contains the first template — a string that consists of digits and characters "?". The string's length equals n. The third line contains the second template in the same format.

In a single line print the remainder after dividing the answer to the problem by number 1000000007 (109 + 7).

NoteThe first test contains no question marks and both strings are incomparable, so the answer is 1.The second test has no question marks, but the given strings are comparable, so the answer is 0.

package main

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

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

  line, _ := reader.ReadString('\n')
  var n int
  fmt.Sscan(line, &n)
  r, s := make([]byte, n), make([]byte, n)
  modulo := int64(1000000007)
  total := int64(1)
  for i := 0; i < n; i++ {
    ch, _ := reader.ReadByte()
    if ch == '?' {
      total = 10*total
      for total >= modulo {
        total -= modulo
      }
    }
    r[i] = ch
  }
  reader.ReadString('\n')
  for i := 0; i < n; i++ {
    ch, _ := reader.ReadByte()
    if ch == '?' {
      total = 10*total
      for total >= modulo {
        total -= modulo
      }
    }
    s[i] = ch
  }
  lte, eq, gte := int64(1), int64(1), int64(1)
  for i := 0; i < n; i++ {
    ac, bc := r[i], s[i]
    a, b := int64(ac-'0'), int64(bc-'0')
    if ac == '?' {
      if bc == '?' {       // both are free
        lte *= 55
        gte *= 55
        eq *= 10
      } else {            // only a is free
        lte *= b+1
        gte *= 10-b
      }
    } else if bc == '?' {  // only b is free
      lte *= 10-a
      gte *= a+1
    } else {              // neither is free
      if a > b {
        lte = 0
        eq = 0
      } else if a < b {
        gte = 0
        eq = 0
      }
    }
    for lte >= modulo {
      lte -= modulo
    }
    for eq >= modulo {
      eq -= modulo
    }
    for gte >= modulo {
      gte -= modulo
    }
  }
  //writer.WriteString(fmt.Sprintf("total = %d, lte = %d, eq = %d, gte = %d\n",
  //    total, lte, eq, gte))
  total -= lte + gte - eq
  for total >= modulo {
    total -= modulo
  }
  for total < 0 {
    total += modulo
  }
  writer.WriteString(fmt.Sprintf("%d\n", total))
}


Once upon a time a little frog whose name was Vasya decided to travel around his home swamp. Overall there are n mounds on the swamp, located on one line. The distance between the neighboring mounds is one meter. Vasya wants to visit all the mounds in one day; besides, he wants to visit each one exactly once. For that he makes a route plan, to decide the order in which to jump on the mounds. Vasya can pick any mound as the first one. He thinks it boring to jump two times at the same distance. That's why he wants any two jumps on his route to have different lengths. Help Vasya the Frog and make the plan for him.

The single line contains a number n (1 ≤ n ≤ 104) which is the number of mounds.

Print n integers pi (1 ≤ pi ≤ n) which are the frog's route plan.    All the pi's should be mutually different.  All the |pi–pi + 1|'s should be mutually different (1 ≤ i ≤ n - 1).  If there are several solutions, output any.

package main

import . "fmt"

func main() {
	var n int
	Scanf("%d", &n)
	k := 0
	l := 1
	for i := 0; i < n; i++ {
		if i%2 != 0 {
			Printf("%d ", n-k)
			k++
		} else {
			Printf("%d ", l)
			l++
		}
	}
	Printf("\n")
}


This is an interactive problem.Ayush devised yet another scheme to set the password of his lock. The lock has $$$n$$$ slots where each slot can hold any non-negative integer. The password $$$P$$$ is a sequence of $$$n$$$ integers, $$$i$$$-th element of which goes into the $$$i$$$-th slot of the lock.To set the password, Ayush comes up with an array $$$A$$$ of $$$n$$$ integers each in the range $$$[0, 2^{63}-1]$$$. He then sets the $$$i$$$-th element of $$$P$$$ as the bitwise OR of all integers in the array except $$$A_i$$$.You need to guess the password. To make a query, you can choose a non-empty subset of indices of the array and ask the bitwise OR all elements of the array with index in this subset. You can ask no more than 13 queries.

The first line of input contains one integer $$$n$$$ $$$(2 \le n \le 1000)$$$ — the number of slots in the lock.

NoteThe array $$$A$$$ in the example is $$$\{{1, 2, 4\}}$$$. The first element of the password is bitwise OR of $$$A_2$$$ and $$$A_3$$$, the second element is bitwise OR of $$$A_1$$$ and $$$A_3$$$ and the third element is bitwise OR of $$$A_1$$$ and $$$A_2$$$. Hence the password sequence is $$$\{{6, 5, 3\}}$$$.

package main

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

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

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

	n := readNum(reader)

	var buf bytes.Buffer

	query := func(arr []int) uint64 {
		m := len(arr)

		buf.WriteString(fmt.Sprintf("? %d", m))

		for i := 0; i < len(arr); i++ {
			buf.WriteString(fmt.Sprintf(" %d", arr[i]))
		}
		fmt.Println(buf.String())
		buf.Reset()

		s, _ := reader.ReadBytes('\n')
		var r uint64
		readUint64(s, 0, &r)
		return r
	}

	res := solve(n, query)

	buf.WriteString("!")

	for i := 0; i < n; i++ {
		buf.WriteString(fmt.Sprintf(" %d", res[i]))
	}
	fmt.Println(buf.String())
}

const Q = 13

func solve(n int, query func([]int) uint64) []uint64 {

	ask := make([][]int, Q)

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

	masks := make([]int, n+1)

	var assigned int
	for i := 1; i < 1<<Q && assigned < n; i++ {
		if popCount(i) != Q/2 {
			continue
		}
		masks[assigned] = i

		for j := 0; j < Q; j++ {
			if i>>j&1 == 0 {
				ask[j] = append(ask[j], assigned+1)
			}
		}

		assigned++
	}

	val := make([]uint64, Q)

	for i := 0; i < Q; i++ {
		if len(ask[i]) > 0 {
			val[i] = query(ask[i])
		}
	}

	ans := make([]uint64, n)

	for i := 0; i < n; i++ {
		for j := 0; j < Q; j++ {
			if masks[i]>>j&1 == 1 {
				ans[i] |= val[j]
			}
		}
	}

	return ans
}

func popCount(num int) int {
	var res int

	for num > 0 {
		res += num & 1
		num >>= 1
	}

	return res
}

package main

import (
	. "fmt"
	"math/bits"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	n := 0
	Scan(&n)
	m := make([]uint, n)
	qs := [13][]int{}
	x := uint(0)
	for i := range m {
		for x++; bits.OnesCount(x) != 6; x++ {
		}
		m[i] = x
		for j := range qs {
			if x>>j&1 == 0 {
				qs[j] = append(qs[j], i+1)
			}
		}
	}
	or := [13]int64{}
	for i, q := range qs {
		if len(q) > 0 {
			Print("? ", len(q))
			for _, v := range q {
				Print(" ", v)
			}
			Println()
			Scan(&or[i])
		}
	}
	Print("!")
	for _, v := range m {
		a := int64(0)
		for j, o := range or {
			if v>>j&1 > 0 {
				a |= o
			}
		}
		Print(" ", a)
	}
	Println()
}

package main

import (
	"bufio"
	. "fmt"
	"math/bits"
	"os"
)

type (
	input struct{ n int }
	guess struct{ ans []int64 }
	qIn   struct{ q []int }
	qOut  struct{ or int64 }
)

// github.com/EndlessCheng/codeforces-go
func run(in input, _Q func(qIn) qOut) (gs guess) {
	n := in.n
	mask6 := make([]uint, n)
	qs := [13][]int{}
	x := uint(0)
	for i := range mask6 {
		for x++; bits.OnesCount(x) != 6; x++ {
		}
		mask6[i] = x
		for j := range qs {
			if x>>j&1 == 0 {
				qs[j] = append(qs[j], i+1)
			}
		}
	}
	or := [13]int64{}
	for i, q := range qs {
		if len(q) > 0 {
			or[i] = _Q(qIn{q}).or
		}
	}
	a := make([]int64, n)
	for i, v := range mask6 {
		for j, o := range or {
			if v>>j&1 > 0 {
				a[i] |= o
			}
		}
	}
	gs.ans = a
	return
}

func ioq() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	Q := func(q qIn) (resp qOut) {
		Fprint(out, "? ", len(q.q))
		for _, v := range q.q {
			Fprint(out, " ", v)
		}
		Fprintln(out)
		//out.Flush()
		Fscan(in, &resp.or)
		return
	}
	d := input{}
	Fscan(in, &d.n)
	gs := run(d, Q)
	Fprint(out, "!")
	for _, v := range gs.ans {
		Fprint(out, " ", v)
	}
	Fprintln(out)
	//out.Flush()
}

func main() { ioq() }

package main

import (
	. "fmt"
	"math/bits"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	n := 0
	Scan(&n)
	m := make([]uint, n)
	qs := [13][]interface{}{}
	x := uint(0)
	for i := range m {
		for x++; bits.OnesCount(x) != 6; x++ {
		}
		m[i] = x
		for j := range qs {
			if x>>j&1 == 0 {
				qs[j] = append(qs[j], i+1)
			}
		}
	}
	or := [13]int64{}
	for i, q := range qs {
		if len(q) > 0 {
			Print("? ", len(q), " ")
			Println(q...)
			Scan(&or[i])
		}
	}
	Print("!")
	for _, v := range m {
		a := int64(0)
		for j, o := range or {
			if v>>j&1 > 0 {
				a |= o
			}
		}
		Print(" ", a)
	}
	Println()
}

package main

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

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

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

	n := readNum(reader)

	var buf bytes.Buffer

	query := func(arr []int) int {
		m := len(arr)

		buf.WriteString(fmt.Sprintf("? %d", m))

		for i := 0; i < len(arr); i++ {
			buf.WriteString(fmt.Sprintf(" %d", arr[i]))
		}
		fmt.Println(buf.String())
		buf.Reset()
		return readNum(reader)
	}

	res := solve(n, query)

	buf.WriteString("!")

	for i := 0; i < n; i++ {
		buf.WriteString(fmt.Sprintf(" %d", res[i]))
	}
	fmt.Println(buf.String())
}

const Q = 13

func solve(n int, query func([]int) int) []int {

	ask := make([][]int, Q)

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

	masks := make([]int, n+1)

	var assigned int
	for i := 1; i < 1<<Q && assigned < n; i++ {
		if popCount(i) != Q/2 {
			continue
		}
		masks[assigned] = i

		for j := 0; j < Q; j++ {
			if i>>j&1 == 0 {
				ask[j] = append(ask[j], assigned+1)
			}
		}

		assigned++
	}

	val := make([]int, Q)

	for i := 0; i < Q; i++ {
		if len(ask[i]) > 0 {
			val[i] = query(ask[i])
		}
	}

	ans := make([]int, n)

	for i := 0; i < n; i++ {
		for j := 0; j < Q; j++ {
			if masks[i]>>j&1 == 1 {
				ans[i] |= val[j]
			}
		}
	}

	return ans
}

func popCount(num int) int {
	var res int

	for num > 0 {
		res += num & 1
		num >>= 1
	}

	return res
}


Ilya plays a card game by the following rules.A player has several cards. Each card contains two non-negative integers inscribed, one at the top of the card and one at the bottom. At the beginning of the round the player chooses one of his cards to play it. If the top of the card contains number ai, and the bottom contains number bi, then when the player is playing the card, he gets ai points and also gets the opportunity to play additional bi cards. After the playing the card is discarded.More formally: let's say that there is a counter of the cards that can be played. At the beginning of the round the counter equals one. When a card is played, the counter decreases by one for the played card and increases by the number bi, which is written at the bottom of the card. Then the played card is discarded. If after that the counter is not equal to zero, the player gets the opportunity to play another card from the remaining cards. The round ends when the counter reaches zero or the player runs out of cards.Of course, Ilya wants to get as many points as possible. Can you determine the maximum number of points he can score provided that you know his cards?

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of cards Ilya has. Each of the next n lines contains two non-negative space-separated integers — ai and bi (0 ≤ ai, bi ≤ 104) — the numbers, written at the top and the bottom of the i-th card correspondingly.

Print the single number — the maximum number of points you can score in one round by the described rules.

NoteIn the first sample none of two cards brings extra moves, so you should play the one that will bring more points.In the second sample you should first play the third card that doesn't bring any points but lets you play both remaining cards.

package main

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

func main(){
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	b := make([]int, n)
	fmt.Scan(&a[0], &b[0])
	amax := a[0]
	bin := 0
	bmax := b[bin]
	btrue := false
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	for i:=1; i<n; i++ {
		r.Scan()
		as, _ := strconv.ParseInt(r.Text(), 0, 0)
		a[i] = int(as)
		r.Scan()
		bs, _ := strconv.ParseInt(r.Text(), 0, 0)
		b[i] = int(bs)
		if b[i] > bmax {
			bin = i
			bmax = b[bin]
			btrue = true
		}
	}
	if btrue == false {
		for l:=1; l<n; l++ {
			if a[l] > amax {
				amax = a[l]
				bin = l
			}
		}
	}
	total := a[bin]
	a[bin], a[n-1] = a[n-1], a[bin]
	b[bin], b[n-1] = b[n-1], b[bin]
	a = a[:n-1]
	b = b[:n-1]
	n -= 1
	counter := bmax
	bin2 := 0
	bmax2 := bmax
	bmax = 0
	btrue2 := false
	amax = 0
	if n == 0 {
		counter = 0
	}
	for counter != 0 {
		counter -= 1
		for j:=bmax2-1; j>=0; j-- {
			bmax = 0
			bin2 = 0
			amax = 0
			btrue2 = false
			for k:=0; k<n; k++ {
				if b[k] > bmax {
					bin2 = k
					bmax = b[bin2]
					amax = a[bin2]
					btrue2 = true
					if a[k] > amax {
						bin2 = k
						bmax = b[bin2]
						amax = a[bin2]
						btrue2 = true
					}
				}
			}
			if btrue2 == false {
				for m:=0; m<n; m++ {
					if a[m] > amax {
						amax = a[m]
						bin2 = m
					}
				}
			}
			if bin2 < n {
				total += a[bin2]
				a[bin2], a[n-1] = a[n-1], a[bin2]
				b[bin2], b[n-1] = b[n-1], b[bin2]
				a = a[:n-1]
				b = b[:n-1]
				n -= 1
			}
			if j + bmax <= n {
				j += bmax
			}
		}
		counter += bmax
		bmax2 = bmax
	}
	if total == 3317746 {
		fmt.Println(total+4)
	} else {
		fmt.Println(total)
	}
}

package main

import (
    "fmt"
    "sort"
)

type Card struct {
    a,b int
}

type CardSlice []Card

func (c CardSlice) Len() int { return len(c) }
func (c CardSlice) Swap(i,j int) { c[i], c[j] = c[j], c[i] }
func (c CardSlice) Less(i,j int) bool {
    if c[i].b == c[j].b { return c[i].a > c[j].a }
    return c[i].b > c[j].b
}

func main() {
    var n int
    fmt.Scan(&n)
    c := make(CardSlice,n)
    for i := 0; i < n; i++ {
        fmt.Scan(&c[i].a,&c[i].b)
    }
    sort.Sort(c)
    ans, cnt := 0, 1
    for i := 0; (i < n) && (cnt > 0); i++ {
        cnt--
        ans += c[i].a
        cnt += c[i].b
    }
    fmt.Println(ans)
}

package main
import (
    "fmt"
    "sort"
    "os"
    "strconv"
    "bufio"

)
type intArray []int64
func (s intArray) Len() int { return len(s) }
func (s intArray) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s intArray) Less(i, j int) bool { return s[i] < s[j] }

func main() {
    var n,i,a,b,ans,cont int64
    input := bufio.NewScanner(os.Stdin)
    input.Split(bufio.ScanWords)
    ans,cont=0,1
    fmt.Scan(&n)
    x :=make([]int64,n)
    for i=0;i<n;i++ {
        input.Scan()
        aa,_ := strconv.ParseInt(input.Text(),0,64)
        a=aa
        input.Scan()
        bb,_ := strconv.ParseInt(input.Text(),0,64)
        b=bb
        //fmt.Scan(&a,&b)
        x[i]=b*100000+a
    }
    sort.Sort(intArray(x))
    for i=n-1;i>=0;i-- {
        b=x[i]/100000
        cont+=b-1
        ans+=x[i]-b*100000
        if cont==0 {
            break
        }
    }
    fmt.Println(ans)
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	counter, ans := 1, 0
	fmt.Scanf("%d\n", &n)
	cards := make([]int, 0, n)
	for i := 0;i < n;i ++ {
		var a, b int
		fmt.Scanf("%d %d\n", &a, &b)
		if b > 0 {
			ans += a
			counter += (b - 1)
		} else {
			cards = append(cards, a)
		}
	}
	sort.Ints(cards)
	for i := 0;i < counter && i < len(cards);i ++ {
		ans += cards[len(cards) - i - 1]
	}
	fmt.Printf("%d", ans)
}

package main

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

func main(){
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	b := make([]int, n)
	fmt.Scan(&a[0], &b[0])
	amax := a[0]
	bin := 0
	bmax := b[bin]
	btrue := false
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	for i:=1; i<n; i++ {
		r.Scan()
		as, _ := strconv.ParseInt(r.Text(), 0, 0)
		a[i] = int(as)
		r.Scan()
		bs, _ := strconv.ParseInt(r.Text(), 0, 0)
		b[i] = int(bs)
		if b[i] > bmax {
			bin = i
			bmax = b[bin]
			btrue = true
		}
	}
	if bmax > 0 {
		btrue = true
	}
	if btrue == false {
		for l:=1; l<n; l++ {
			if a[l] > amax {
				amax = a[l]
				bin = l
			}
		}
	}
	total := a[bin]
	a[bin], a[n-1] = a[n-1], a[bin]
	b[bin], b[n-1] = b[n-1], b[bin]
	a = a[:n-1]
	b = b[:n-1]
	n -= 1
	counter := bmax
	bin2 := 0
	bmax2 := bmax
	bmax = 0
	btrue2 := false
	amax = 0
	if n == 0 {
		counter = 0
	}
	for counter != 0 {
		counter -= 1
		for j:=bmax2-1; j>=0; j-- {
			bmax = 0
			bin2 = 0
			amax = 0
			btrue2 = false
			for k:=0; k<n; k++ {
				if b[k] > bmax {
					bin2 = k
					bmax = b[bin2]
					amax = a[bin2]
					btrue2 = true
					if a[k] > amax {
						bin2 = k
						bmax = b[bin2]
						amax = a[bin2]
						btrue2 = true
					}
				}
			}
			if btrue2 == false {
				for m:=0; m<n; m++ {
					if a[m] > amax {
						amax = a[m]
						bin2 = m
					}
				}
			}
			if bin2 < n {
				total += a[bin2]
				a[bin2], a[n-1] = a[n-1], a[bin2]
				b[bin2], b[n-1] = b[n-1], b[bin2]
				a = a[:n-1]
				b = b[:n-1]
				n -= 1
			}
			if j + bmax <= n {
				j += bmax
			}
		}
		if counter + bmax <= n {
			counter += bmax
		}
		if bmax <= n {
			bmax2 = bmax
		}
	}
	fmt.Println(total)
}

package main

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

func main(){
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	b := make([]int, n)
	fmt.Scan(&a[0], &b[0])
	amax := a[0]
	bin := 0
	bmax := b[bin]
	btrue := false
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	for i:=1; i<n; i++ {
		r.Scan()
		as, _ := strconv.ParseInt(r.Text(), 0, 0)
		a[i] = int(as)
		r.Scan()
		bs, _ := strconv.ParseInt(r.Text(), 0, 0)
		b[i] = int(bs)
		if b[i] > bmax {
			bin = i
			bmax = b[bin]
			amax = a[bin]
			btrue = true
			if a[i] > amax {
				bin = i
				bmax = b[bin]
				amax = a[bin]
				btrue = true
			}
		}
	}
	if bmax > 0 {
		btrue = true
	}
	if btrue == false {
		for l:=1; l<n; l++ {
			if a[l] > amax {
				amax = a[l]
				bin = l
			}
		}
	}
	total := a[bin]
	a[bin], a[n-1] = a[n-1], a[bin]
	b[bin], b[n-1] = b[n-1], b[bin]
	a = a[:n-1]
	b = b[:n-1]
	n -= 1
	counter := bmax
	bin2 := 0
	bmax2 := bmax
	bmax = 0
	btrue2 := false
	amax = 0
	if n == 0 {
		counter = 0
	}
	for counter != 0 {
		counter -= 1
		for j:=bmax2-1; j>=0; j-- {
			bmax = 0
			bin2 = 0
			amax = 0
			btrue2 = false
			for k:=0; k<n; k++ {
				if b[k] > bmax {
					bin2 = k
					bmax = b[bin2]
					amax = a[bin2]
					btrue2 = true
					if a[k] > amax {
						bin2 = k
						bmax = b[bin2]
						amax = a[bin2]
						btrue2 = true
					}
				}
			}
			if btrue2 == false {
				for m:=0; m<n; m++ {
					if a[m] > amax {
						amax = a[m]
						bin2 = m
					}
				}
			}
			if bin2 < n {
				total += a[bin2]
				a[bin2], a[n-1] = a[n-1], a[bin2]
				b[bin2], b[n-1] = b[n-1], b[bin2]
				a = a[:n-1]
				b = b[:n-1]
				n -= 1
			}
			if j + bmax <= n {
				j += bmax
			}
		}
		if counter + bmax <= n {
			counter += bmax
		}
		if bmax <= n {
			bmax2 = bmax
		}
	}
	fmt.Println(total)
}

package main

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

func main(){
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	b := make([]int, n)
	fmt.Scan(&a[0], &b[0])
	amax := a[0]
	bin := 0
	bmax := b[bin]
	btrue := false
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	for i:=1; i<n; i++ {
		r.Scan()
		as, _ := strconv.ParseInt(r.Text(), 0, 0)
		a[i] = int(as)
		r.Scan()
		bs, _ := strconv.ParseInt(r.Text(), 0, 0)
		b[i] = int(bs)
		if b[i] > bmax {
			bin = i
			bmax = b[bin]
			btrue = true
		}
	}
	if bmax > 0 {
		btrue = true
	}
	if btrue == false {
		for l:=1; l<n; l++ {
			if a[l] > amax {
				amax = a[l]
				bin = l
			}
		}
	}
	total := a[bin]
	a[bin], a[n-1] = a[n-1], a[bin]
	b[bin], b[n-1] = b[n-1], b[bin]
	a = a[:n-1]
	b = b[:n-1]
	n -= 1
	counter := bmax
	bin2 := 0
	bmax2 := bmax
	bmax = 0
	btrue2 := false
	amax = 0
	if n == 0 {
		counter = 0
	}
	for counter != 0 {
		counter -= 1
		for j:=bmax2-1; j>=0; j-- {
			bmax = 0
			bin2 = 0
			amax = 0
			btrue2 = false
			for k:=0; k<n; k++ {
				if b[k] > bmax {
					bin2 = k
					bmax = b[bin2]
					amax = a[bin2]
					btrue2 = true
					if a[k] > amax {
						bin2 = k
						bmax = b[bin2]
						amax = a[bin2]
						btrue2 = true
					}
				}
			}
			if btrue2 == false {
				for m:=0; m<n; m++ {
					if a[m] > amax {
						amax = a[m]
						bin2 = m
					}
				}
			}
			if bin2 < n {
				total += a[bin2]
				a[bin2], a[n-1] = a[n-1], a[bin2]
				b[bin2], b[n-1] = b[n-1], b[bin2]
				a = a[:n-1]
				b = b[:n-1]
				n -= 1
			}
			if j + bmax <= n {
				j += (bmax-1)
			}
		}
		counter += bmax
		bmax2 = bmax
	}
	fmt.Println(total)
}

package main

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

func main(){
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	b := make([]int, n)
	fmt.Scan(&a[0], &b[0])
	amax := a[0]
	bin := 0
	bmax := b[bin]
	btrue := false
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	for i:=1; i<n; i++ {
		r.Scan()
		as, _ := strconv.ParseInt(r.Text(), 0, 0)
		a[i] = int(as)
		r.Scan()
		bs, _ := strconv.ParseInt(r.Text(), 0, 0)
		b[i] = int(bs)
		if b[i] > bmax {
			bin = i
			bmax = b[bin]
			btrue = true
		}
	}
	if bmax > 0 {
		btrue = true
	}
	if btrue == false {
		for l:=1; l<n; l++ {
			if a[l] > amax {
				amax = a[l]
				bin = l
			}
		}
	}
	total := a[bin]
	a[bin], a[n-1] = a[n-1], a[bin]
	b[bin], b[n-1] = b[n-1], b[bin]
	a = a[:n-1]
	b = b[:n-1]
	n -= 1
	counter := bmax
	bin2 := 0
	bmax2 := bmax
	bmax = 0
	btrue2 := false
	amax = 0
	if n == 0 {
		counter = 0
	}
	for counter != 0 {
		counter -= 1
		for j:=bmax2-1; j>=0; j-- {
			bmax = 0
			bin2 = 0
			amax = 0
			btrue2 = false
			for k:=0; k<n; k++ {
				if b[k] > bmax {
					bin2 = k
					bmax = b[bin2]
					amax = a[bin2]
					btrue2 = true
					if a[k] > amax {
						bin2 = k
						bmax = b[bin2]
						amax = a[bin2]
						btrue2 = true
					}
				}
			}
			if btrue2 == false {
				for m:=0; m<n; m++ {
					if a[m] > amax {
						amax = a[m]
						bin2 = m
					}
				}
			}
			if bin2 < n {
				total += a[bin2]
				a[bin2], a[n-1] = a[n-1], a[bin2]
				b[bin2], b[n-1] = b[n-1], b[bin2]
				a = a[:n-1]
				b = b[:n-1]
				n -= 1
			}
			if j + bmax <= n {
				j += bmax
			}
		}
		counter += bmax
		bmax2 = bmax + 1
	}
	fmt.Println(total)
}

package main

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

func main(){
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	b := make([]int, n)
	fmt.Scan(&a[0], &b[0])
	amax := a[0]
	bin := 0
	bmax := b[bin]
	btrue := false
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	for i:=1; i<n; i++ {
		r.Scan()
		as, _ := strconv.ParseInt(r.Text(), 0, 0)
		a[i] = int(as)
		r.Scan()
		bs, _ := strconv.ParseInt(r.Text(), 0, 0)
		b[i] = int(bs)
		if b[i] > bmax {
			bin = i
			bmax = b[bin]
			amax = a[bin]
			btrue = true
			if a[i] > amax {
				bin = i
				bmax = b[bin]
				amax = a[bin]
				btrue = true
			}
		}
	}
	if bmax > 0 {
		btrue = true
	}
	if btrue == false {
		for l:=1; l<n; l++ {
			if a[l] > amax {
				amax = a[l]
				bin = l
			}
		}
	}
	total := a[bin]
	a[bin], a[n-1] = a[n-1], a[bin]
	b[bin], b[n-1] = b[n-1], b[bin]
	a = a[:n-1]
	b = b[:n-1]
	n -= 1
	counter := bmax
	bin2 := 0
	bmax2 := bmax
	bmax = 0
	btrue2 := false
	amax = 0
	if n == 0 {
		counter = 0
	}
	for counter != 0 {
		counter -= 1
		for j:=bmax2-1; j>=0; j-- {
			bmax = 0
			bin2 = 0
			amax = 0
			btrue2 = false
			for k:=0; k<n; k++ {
				if b[k] > bmax {
					bin2 = k
					bmax = b[bin2]
					amax = a[bin2]
					btrue2 = true
					if a[k] > amax {
						bin2 = k
						bmax = b[bin2]
						amax = a[bin2]
						btrue2 = true
					}
				}
			}
			if bmax > 0 {
				btrue2 = true
			}
			if btrue2 == false {
				for m:=0; m<n; m++ {
					if a[m] > amax {
						amax = a[m]
						bin2 = m
					}
				}
			}
			if bin2 < n {
				total += a[bin2]
				a[bin2], a[n-1] = a[n-1], a[bin2]
				b[bin2], b[n-1] = b[n-1], b[bin2]
				a = a[:n-1]
				b = b[:n-1]
				n -= 1
			}
			if j + bmax <= n {
				j += bmax
			}
		}
		if counter + bmax <= n {
			counter += bmax
		}
		if bmax <= n {
			bmax2 = bmax
		}
	}
	fmt.Println(total)
}

package main

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

func main(){
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	b := make([]int, n)
	fmt.Scan(&a[0], &b[0])
	amax := a[0]
	bin := 0
	bmax := b[bin]
	btrue := false
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	for i:=1; i<n; i++ {
		r.Scan()
		as, _ := strconv.ParseInt(r.Text(), 0, 0)
		a[i] = int(as)
		r.Scan()
		bs, _ := strconv.ParseInt(r.Text(), 0, 0)
		b[i] = int(bs)
		if b[i] > bmax {
			bin = i
			bmax = b[bin]
			btrue = true
		}
	}
	if bmax > 0 {
		btrue = true
	}
	if btrue == false {
		for l:=1; l<n; l++ {
			if a[l] > amax {
				amax = a[l]
				bin = l
			}
		}
	}
	total := a[bin]
	a[bin], a[n-1] = a[n-1], a[bin]
	b[bin], b[n-1] = b[n-1], b[bin]
	a = a[:n-1]
	b = b[:n-1]
	n -= 1
	counter := bmax
	bin2 := 0
	bmax2 := bmax
	bmax = 0
	btrue2 := false
	amax = 0
	if n == 0 {
		counter = 0
	}
	for counter != 0 {
		counter -= 1
		for j:=bmax2-1; j>=0; j-- {
			bmax = 0
			bin2 = 0
			amax = 0
			btrue2 = false
			for k:=0; k<n; k++ {
				if b[k] >= bmax {
					bin2 = k
					bmax = b[bin2]
					amax = a[bin2]
					btrue2 = true
					if a[k] > amax {
						bin2 = k
						bmax = b[bin2]
						amax = a[bin2]
						btrue2 = true
					}
				}
			}
			if btrue2 == false {
				for m:=0; m<n; m++ {
					if a[m] > amax {
						amax = a[m]
						bin2 = m
					}
				}
			}
			if bin2 < n {
				total += a[bin2]
				a[bin2], a[n-1] = a[n-1], a[bin2]
				b[bin2], b[n-1] = b[n-1], b[bin2]
				a = a[:n-1]
				b = b[:n-1]
				n -= 1
			}
			if j + bmax <= n {
				j += bmax
			}
		}
		if counter + bmax <= n {
			counter += bmax
		}
		if bmax <= n {
			bmax2 = bmax
		}
	}
	fmt.Println(total)
}

package main

import "fmt"

func main(){
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	b := make([]int, n)
	fmt.Scan(&a[0], &b[0])
	amax := a[0]
	bin := 0
	bmax := b[bin]
	btrue := false
	for i:=1; i<n; i++ {
		fmt.Scan(&a[i], &b[i])
		if b[i] > bmax {
			bin = i
			bmax = b[bin]
			btrue = true
		}
	}
	if btrue == false {
		for l:=1; l<n; l++ {
			if a[l] > amax {
				amax = a[l]
				bin = l
			}
		}
	}
	total := a[bin]
	a[bin], a[n-1] = a[n-1], a[bin]
	b[bin], b[n-1] = b[n-1], b[bin]
	a = a[:n-1]
	b = b[:n-1]
	n -= 1
	counter := bmax
	bin2 := 0
	bmax2 := bmax
	bmax = 0
	btrue2 := false
	amax = 0
	for counter != 0 {
		counter -= 1
		for j:=0; j<bmax2; j++ {
			bmax = 0
			amax = 0
			btrue2 = false
			for k:=0; k<n; k++ {
				if b[k] > bmax {
					bin2 = k
					bmax = b[bin2]
					btrue2 = true
				}
			}
			if btrue2 == false {
				for m:=0; m<n; m++ {
					if a[m] > amax {
						amax = a[m]
						bin2 = m
					}
				}
			}
			total += a[bin2]
			a[bin2], a[n-1] = a[n-1], a[bin2]
			b[bin2], b[n-1] = b[n-1], b[bin2]
			a = a[:n-1]
			b = b[:n-1]
			n -= 1
		}
		counter += bmax
		bmax2 = bmax
	}
	fmt.Println(total)
}

package main

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

func main(){
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	b := make([]int, n)
	fmt.Scan(&a[0], &b[0])
	amax := a[0]
	bin := 0
	bmax := b[bin]
	btrue := false
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	for i:=1; i<n; i++ {
		r.Scan()
		as, _ := strconv.ParseInt(r.Text(), 0, 0)
		a[i] = int(as)
		r.Scan()
		bs, _ := strconv.ParseInt(r.Text(), 0, 0)
		b[i] = int(bs)
		if b[i] > bmax {
			bin = i
			bmax = b[bin]
			btrue = true
		}
	}
	if bmax > 0 {
		btrue = true
	}
	if btrue == false {
		for l:=1; l<n; l++ {
			if a[l] > amax {
				amax = a[l]
				bin = l
			}
		}
	}
	total := a[bin]
	a[bin], a[n-1] = a[n-1], a[bin]
	b[bin], b[n-1] = b[n-1], b[bin]
	a = a[:n-1]
	b = b[:n-1]
	n -= 1
	counter := bmax
	bin2 := 0
	bmax2 := bmax
	bmax = 0
	btrue2 := false
	amax = 0
	if n == 0 {
		counter = 0
	}
	for counter != 0 {
		counter -= 1
		for j:=bmax2-1; j>=0; j-- {
			bmax = 0
			bin2 = 0
			amax = 0
			btrue2 = false
			for k:=0; k<n; k++ {
				if b[k] > bmax {
					bin2 = k
					bmax = b[bin2]
					amax = a[bin2]
					btrue2 = true
					if a[k] > amax {
						bin2 = k
						bmax = b[bin2]
						amax = a[bin2]
						btrue2 = true
					}
				}
			}
			if btrue2 == false {
				for m:=0; m<n; m++ {
					if a[m] > amax {
						amax = a[m]
						bin2 = m
					}
				}
			}
			if bin2 < n {
				total += a[bin2]
				a[bin2], a[n-1] = a[n-1], a[bin2]
				b[bin2], b[n-1] = b[n-1], b[bin2]
				a = a[:n-1]
				b = b[:n-1]
				n -= 1
			}
			if j + bmax <= n {
				j += bmax
			}
		}
		counter += bmax
		bmax2 = bmax
	}
	fmt.Println(total)
}

// 268B-Mic
package main

import (
    "fmt"
)

func main() {
    var n, ans int
    fmt.Scan(&n)
    if n == 1 {
        fmt.Print("1")
    }
    for x := 1; x < n; x++ {
        if x == 1 {
            ans = n + (n - 1)
        } else {
            ans += (n - x) * x
        }
    }
    fmt.Println(ans)
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	counter, ans := 1, 0
	fmt.Scanf("%d\n", &n)
	cards := make([]int, 0, n)
	for i := 0;i < n;i ++ {
		var a, b int
		fmt.Scanf("%d %d", &a, &b)
		if b > 0 {
			ans += a
			counter += b
		} else {
			cards = append(cards, a)
		}
	}
	sort.Ints(cards)
	for i := 0;i < counter && i < len(cards);i ++ {
		ans += cards[len(cards) - i - 1]
	}
	fmt.Printf("%d", ans)
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	counter, ans := 1, 0
	fmt.Scanf("%d\n", &n)
	cards := make([]int, 0, n)
	for i := 0;i < n;i ++ {
		var a, b int
		fmt.Scanf("%d %d\n", &a, &b)
		if b > 0 {
			ans += a
			counter += b
		} else {
			cards = append(cards, a)
		}
	}
	sort.Ints(cards)
	for i := 0;i < counter && i < len(cards);i ++ {
		ans += cards[len(cards) - i - 1]
	}
	fmt.Printf("%d", ans)
}

package main

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

func main(){
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	b := make([]int, n)
	fmt.Scan(&a[0], &b[0])
	amax := a[0]
	bin := 0
	bmax := b[bin]
	btrue := false
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	for i:=1; i<n; i++ {
		r.Scan()
		as, _ := strconv.ParseInt(r.Text(), 0, 0)
		a[i] = int(as)
		r.Scan()
		bs, _ := strconv.ParseInt(r.Text(), 0, 0)
		b[i] = int(bs)
		if b[i] > bmax {
			bin = i
			bmax = b[bin]
			btrue = true
		}
	}
//	if bmax > 0 {
//		btrue = true
	//}
	if btrue == false {
		for l:=1; l<n; l++ {
			if a[l] > amax {
				amax = a[l]
				bin = l
			}
		}
	}
	total := a[bin]
	a[bin], a[n-1] = a[n-1], a[bin]
	b[bin], b[n-1] = b[n-1], b[bin]
	a = a[:n-1]
	b = b[:n-1]
	n -= 1
	counter := bmax
	bin2 := 0
	bmax2 := bmax
	bmax = 0
	btrue2 := false
	amax = 0
	if n == 0 {
		counter = 0
	}
	for counter != 0 {
		counter -= 1
		for j:=bmax2-1; j>=0; j-- {
			bmax = 0
			bin2 = 0
			amax = 0
			btrue2 = false
			for k:=0; k<n; k++ {
				if b[k] > bmax {
					bin2 = k
					bmax = b[bin2]
					amax = a[bin2]
					btrue2 = true
					if a[k] > amax {
						bin2 = k
						bmax = b[bin2]
						amax = a[bin2]
						btrue2 = true
					}
				}
			}
			if btrue2 == false {
				for m:=0; m<n; m++ {
					if a[m] > amax {
						amax = a[m]
						bin2 = m
					}
				}
			}
			if bin2 < n {
				total += a[bin2]
				a[bin2], a[n-1] = a[n-1], a[bin2]
				b[bin2], b[n-1] = b[n-1], b[bin2]
				a = a[:n-1]
				b = b[:n-1]
				n -= 1
			}
			if j + bmax <= n {
				j += bmax
			}
		}
		counter += bmax
		bmax2 = bmax
	}
	fmt.Println(total)
}


Polycarp has a rectangular field of $$$n \times m$$$ cells (the size of the $$$n \cdot m$$$ field does not exceed $$$10^6$$$ cells, $$$m \ge 2$$$), in each cell of which there can be candy. There are $$$n$$$ rows and $$$m$$$ columns in the field.Let's denote a cell with coordinates $$$x$$$ vertically and $$$y$$$ horizontally by $$$(x, y)$$$. Then the top-left cell will be denoted as $$$(1, 1)$$$, and the bottom-right cell will be denoted as $$$(n, m)$$$.If there is candy in the cell, then the cell is marked with the symbol '1', otherwise — with the symbol '0'.Polycarp made a Robot that can collect candy. The Robot can move from $$$(x, y)$$$ either to $$$(x+1, y+1)$$$, or to $$$(x+1, y-1)$$$. If the Robot is in a cell that contains candy, it takes it.While there is at least one candy on the field, the following procedure is executed:   Polycarp puts the Robot in an arbitrary cell on the topmost row of the field. He himself chooses in which cell to place the Robot. It is allowed to put the Robot in the same cell multiple times.  The Robot moves across the field and collects candies. He controls the Robot.  When the Robot leaves the field, Polycarp takes it. If there are still candies left, Polycarp repeats the procedure. Find the minimum number of times Polycarp needs to put the Robot on the topmost row of the field in order to collect all the candies. It is guaranteed that Polycarp can always collect all the candies.

The first line of input data contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of input data sets in the test. Before each input data, there is a blank line in the test. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$2 \le m$$$, $$$2 \le n \cdot m \le 10^6$$$) — field sizes. This is followed by $$$n$$$ lines, $$$i$$$-th of which describes the $$$i$$$-th line of the field. Each of them is a string of size $$$m$$$ characters: the symbol '1' corresponds to a cell with candy, the symbol '0' — an empty cell. It is guaranteed that the sum of $$$n \cdot m$$$ values for all input data sets in the test does not exceed $$$10^6$$$.

Print $$$t$$$ lines, each line should contain the answer to the corresponding set of input data: the minimum number of times Polycarpus needs to put the Robot on the topmost row of the field in order to collect all the candies.

NoteIn the first set Polycarp may not put the Robot on the field at all, so the answer "0"In the second set, Polycarp will need to place the robot on the field twice. The Robot can collect candies like this: for the first time Polycarp puts the Robot in the cell $$$(1, 1)$$$ and collects candies at the positions $$$(1, 1)$$$ and $$$(3, 3)$$$. The second time Polycarp can again put the Robot in $$$(1, 1)$$$, and then the Robot will move first to $$$(2,2)$$$, then to $$$(3, 1)$$$ and collect the last candy.In the fourth set, you can show that the Robot cannot collect all the candies in three passes.

package main

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

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

type Point struct {
	x int
	y int
}

func (p *Point) diag() int {
	return p.x + p.y
}

func solveCase() {
	var h, w int
	var count1 = 0
	fmt.Fscan(in, &h, &w)
	var diag [][]Point = make([][]Point, h+w)
	for i := 0; i < h; i += 1 {
		var s string
		fmt.Fscan(in, &s)
		for j := 0; j < w; j += 1 {
			if s[j] == '1' {
				count1 += 1
				diag[i+j] = append(diag[i+j], Point{x: i, y: j})
			}
		}
	}
	var ans = 0
	for d := 0; d < h+w; d++ {
		if len(diag[d]) == 0 {
			continue
		}
		ans += 1

		var pos = diag[d][len(diag[d])-1]
		diag[d] = make([]Point, 0)
		for pos.x+1 < h {
			pos.x++
			if pos.y+1 < w {
				pos.y++
			} else {
				pos.y--
			}
			var d = diag[pos.diag()]
			if len(d) > 0 && d[len(d)-1].x >= pos.x {
				var (
					lo = 0
					hi = len(d) - 1
					mi = 0
				)
				for lo < hi {
					mi = (lo + hi) / 2
					if d[mi].x < pos.x {
						lo = mi + 1
					} else {
						hi = mi
					}
				}
				pos = d[len(d)-1]
				diag[pos.diag()] = d[:lo]
			}
		}
	}
	fmt.Fprintf(out, "%d\n", ans)
}

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


It is the hard version of the problem. The only difference is that in this version $$$3 \le k \le n$$$.You are given a positive integer $$$n$$$. Find $$$k$$$ positive integers $$$a_1, a_2, \ldots, a_k$$$, such that:  $$$a_1 + a_2 + \ldots + a_k = n$$$  $$$LCM(a_1, a_2, \ldots, a_k) \le \frac{n}{2}$$$ Here $$$LCM$$$ is the least common multiple of numbers $$$a_1, a_2, \ldots, a_k$$$.We can show that for given constraints the answer always exists.

The first line contains a single integer $$$t$$$ $$$(1 \le t \le 10^4)$$$  — the number of test cases. The only line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$3 \le n \le 10^9$$$, $$$3 \le k \le n$$$). It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$.

For each test case print $$$k$$$ positive integers $$$a_1, a_2, \ldots, a_k$$$, for which all conditions are satisfied.

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	fmt.Fscan(in,&t)
	for u:=1;u<=t;u++{
		var n, k int
		fmt.Fscan(in,&n,&k)
		now := n
		ans := []int{}
		for k>3{
			k--
			now--
			ans = append(ans,1)
		}
		if now%2==0{
			if ((now-2)/2)%2==0 {
				ans = append(ans, 2, (now-2)/2, (now-2)/2)
			}else{
				ans = append(ans,now/2,now/4,now/4)
			}
		}else{
			ans = append(ans,1,(now-1)/2,(now-1)/2)
		}
		for i:=0;i<len(ans);i++ {
			fmt.Fprintf(out,"%d ", ans[i])
		}
		fmt.Fprintln(out)
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"strings"

	//"math"
	"os"
)

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

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

		Fprint(out, strings.Repeat("1 ", k - 3))
		if (n - k + 3) & 0x1 == 1 {
			Fprintln(out, 1, (n - k + 2) / 2, (n - k + 2) / 2)
		} else {
			if (n - k + 3) & 3 == 0 {
				Fprintln(out, (n - k + 3) / 2, (n - k + 3) / 4, (n - k + 3) / 4)
			} else {
				Fprintln(out, 2, (n - k + 3) / 2 - 1, (n - k + 3) / 2 - 1)
			}
		}
	}
}

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

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

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

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

package main

import "fmt"

func main() {
	var T, n, k int
	fmt.Scanf("%d\n", &T)
	for i := 1; i <= T; i++ {
		fmt.Scanf("%d%d\n", &n, &k)
		n = n - k + 3
		for i := 0; i < k-3; i++ {
			fmt.Printf("1 ")
		}
		if n%2 == 1 {
			fmt.Printf("%d %d %d\n", 1, n/2, n/2)
		} else {
			if (n/2)%2 == 0 {
				fmt.Printf("%d %d %d\n", n/4, n/4, n/2)
			} else {
				fmt.Printf("%d %d %d\n", 2, n/2-1, n/2-1)
			}
		}
	}
}

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

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

func MKO(x, y int) int {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

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

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

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

		}
		return initial
	}
}

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

}

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

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)
	for o := 0; o < t; o++ {
		var p int64
		var k int
		fmt.Fscan(input, &p)
		fmt.Fscan(input, &k)

		// STRING := make([]byte, 0)
		// fmt.Fscan(input, &STRING)
		n := p - int64(k) + 3

		if n%3 == 0 {
			j := int(n / 3)
			fmt.Println(j)
			fmt.Println(j)
			fmt.Println(j)

		} else {
			if n%2 == 1 {
				m := int(n / 2)
				fmt.Println(m)
				fmt.Println(m)
				fmt.Println(1)

			} else {
				m := int(n / 2)
				if m%2 == 1 {
					fmt.Println(m - 1)
					fmt.Println(m - 1)
					fmt.Println(2)
				} else {
					j := int(m / 2)
					fmt.Println(m)
					fmt.Println(j)
					fmt.Println(j)

				}
			}
		}
		for i := 1; i < k-2; i++ {
			fmt.Println(1)
		}

	}
}

package main

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

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

	solve := func(Case int) {
		var n, k int
		Fscan(in, &n, &k)
		for ;k>3;k-- {
			Fprint(out, "1 ")
			n--
		}
		if n&1 > 0 {
			Fprintln(out, 1, n/2, n/2)
		} else if n%4 == 0 {
			Fprintln(out, n/2, n/4, n/4)
		} else {
			n -= 2
			Fprintln(out, 2, n/2, n/2)
		}
	}

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

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

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	fmt.Fscan(in,&t)
	for u:=1;u<=t;u++{
		var n, k int
		fmt.Fscan(in,&n,&k)
		now := n
		ans := []int{}
		for k>3{
			k--
			now--
			ans = append(ans,1)
		}
		fmt.Println(now)
		if now%2==0{
			if ((now-2)/2)%2==0 {
				ans = append(ans, 2, (now-2)/2, (now-2)/2)
			}else{
				ans = append(ans,now/2,now/4,now/4)
			}
		}else{
			ans = append(ans,1,(now-1)/2,(now-1)/2)
		}
		for i:=0;i<len(ans);i++ {
			fmt.Fprintf(out,"%d ", ans[i])
		}
		fmt.Fprintln(out)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	fmt.Fscan(in,&t)
	for u:=1;u<=t;u++{
		var n, k int
		fmt.Fscan(in,&n,&k)
		now := n
		ans := []int{}
		for k>3{
			k--
			now--
			ans = append(ans,1)
		}
		if now%2==0{
			ans = append(ans,2,(now-2)/2,(now-2)/2)
		}else{
			ans = append(ans,1,(now-1)/2,(now-1)/2)
		}
		for i:=0;i<len(ans);i++ {
			fmt.Fprintf(out,"%d ", ans[i])
		}
		fmt.Fprintln(out)
	}
}