Connected undirected weighted graph without self-loops and multiple edges is given. Graph contains n vertices and m edges.For each edge (u, v) find the minimal possible weight of the spanning tree that contains the edge (u, v).The weight of the spanning tree is the sum of weights of all edges included in spanning tree.

First line contains two integers n and m (1 ≤ n ≤ 2·105, n - 1 ≤ m ≤ 2·105) — the number of vertices and edges in graph. Each of the next m lines contains three integers ui, vi, wi (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ wi ≤ 109) — the endpoints of the i-th edge and its weight.

Print m lines. i-th line should contain the minimal possible weight of the spanning tree that contains i-th edge. The edges are numbered from 1 to m in order of their appearing in input.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF609E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}

	var n, m, v, w, wt int
	Fscan(in, &n, &m)
	type edge struct{ v, w, wt, i int }
	es := make([]edge, m)
	for i := range es {
		Fscan(in, &v, &w, &wt)
		es[i] = edge{v - 1, w - 1, wt, i}
	}
	sort.Slice(es, func(i, j int) bool { return es[i].wt < es[j].wt })

	fa := make([]int, n)
	for i := range fa {
		fa[i] = i
	}
	var find func(int) int
	find = func(x int) int {
		if fa[x] != x {
			fa[x] = find(fa[x])
		}
		return fa[x]
	}
	s := int64(0)
	type nb struct{ to, wt int }
	g := make([][]nb, n)
	for i, e := range es {
		v, w, wt := e.v, e.w, e.wt
		if fv, fw := find(v), find(w); fv != fw {
			s += int64(wt)
			fa[fv] = fw
			g[v] = append(g[v], nb{w, wt})
			g[w] = append(g[w], nb{v, wt})
			es[i].wt = 0
		}
	}

	const mx = 18
	type pair struct{ p, max int }
	pa := make([][mx]pair, n)
	dep := make([]int, n)
	var f func(v, p, d int)
	f = func(v, p, d int) {
		pa[v][0].p = p
		dep[v] = d
		for _, e := range g[v] {
			if w := e.to; w != p {
				pa[w][0].max = e.wt
				f(w, v, d+1)
			}
		}
	}
	f(0, -1, 0)
	for i := 0; i+1 < mx; i++ {
		for v := range pa {
			if p := pa[v][i]; p.p != -1 {
				pp := pa[p.p][i]
				pa[v][i+1] = pair{pp.p, max(p.max, pp.max)}
			} else {
				pa[v][i+1] = pair{-1, 0}
			}
		}
	}
	maxWt := func(v, w int) (res int) {
		if dep[v] > dep[w] {
			v, w = w, v
		}
		for i := 0; i < mx; i++ {
			if (dep[w]-dep[v])>>i&1 > 0 {
				p := pa[w][i]
				w = p.p
				res = max(res, p.max)
			}
		}
		if v == w {
			return
		}
		for i := mx - 1; i >= 0; i-- {
			if p, q := pa[v][i], pa[w][i]; p.p != q.p {
				v, w = p.p, q.p
				res = max(res, max(p.max, q.max))
			}
		}
		return max(res, max(pa[v][0].max, pa[w][0].max))
	}

	ans := make([]int64, m)
	for _, e := range es {
		if e.wt > 0 {
			ans[e.i] = int64(e.wt - maxWt(e.v, e.w))
		}
	}
	for _, v := range ans {
		Fprintln(out, s+v)
	}
}

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


You are given $$$n$$$ pairs of integers $$$(a_1, b_1), (a_2, b_2), \ldots, (a_n, b_n)$$$. All of the integers in the pairs are distinct and are in the range from $$$1$$$ to $$$2 \cdot n$$$ inclusive.Let's call a sequence of integers $$$x_1, x_2, \ldots, x_{2k}$$$ good if either   $$$x_1 < x_2 > x_3 < \ldots < x_{2k-2} > x_{2k-1} < x_{2k}$$$, or  $$$x_1 > x_2 < x_3 > \ldots > x_{2k-2} < x_{2k-1} > x_{2k}$$$. You need to choose a subset of distinct indices $$$i_1, i_2, \ldots, i_t$$$ and their order in a way that if you write down all numbers from the pairs in a single sequence (the sequence would be $$$a_{i_1}, b_{i_1}, a_{i_2}, b_{i_2}, \ldots, a_{i_t}, b_{i_t}$$$), this sequence is good.What is the largest subset of indices you can choose? You also need to construct the corresponding index sequence $$$i_1, i_2, \ldots, i_t$$$.

The first line contains single integer $$$n$$$ ($$$2 \leq n \leq 3 \cdot 10^5$$$) — the number of pairs. Each of the next $$$n$$$ lines contain two numbers — $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 2 \cdot n$$$) — the elements of the pairs. It is guaranteed that all integers in the pairs are distinct, that is, every integer from $$$1$$$ to $$$2 \cdot n$$$ is mentioned exactly once.

In the first line print a single integer $$$t$$$ — the number of pairs in the answer. Then print $$$t$$$ distinct integers $$$i_1, i_2, \ldots, i_t$$$ — the indexes of pairs in the corresponding order.

NoteThe final sequence in the first example is $$$1 < 7 > 3 < 5 > 2 < 10$$$.The final sequence in the second example is $$$6 > 1 < 3 > 2 < 5 > 4$$$.

package main

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

type node struct {
	l, r, id int
}

type nodes []node

func (a nodes) Len() int           { return len(a) }
func (a nodes) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a nodes) Less(i, j int) bool { return a[i].r > a[j].r }

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n := 0
	fmt.Fscanf(in, "%d\n", &n)
	a := make([]node, 0)
	b := make([]node, 0)
	for i := 0; i < n; i++ {
		q, p := 0, 0
		fmt.Fscanf(in, "%d %d\n", &q, &p)
		if q < p {
			a = append(a, node{q, p, i + 1})
		} else if p < q {
			b = append(b, node{q, p, i + 1})
		}
	}
	sort.Sort(nodes(a))
	sort.Sort(nodes(b))
	if len(a) > len(b) {
		fmt.Fprintf(out, "%d\n", len(a))
		fmt.Fprintf(out, "%d", a[0].id)
		for i := 1; i < len(a); i++ {
			fmt.Fprintf(out, " %d", a[i].id)
		}
		fmt.Fprintf(out, "\n")
		return
	}
	fmt.Fprintf(out, "%d\n", len(b))
	fmt.Fprintf(out, "%d", b[len(b)-1].id)
	for i := 1; i < len(b); i++ {
		fmt.Fprintf(out, " %d", b[len(b)-1-i].id)
	}
	fmt.Fprintf(out, "\n")
	return
}

func findPattern(a []int, m int) int {
	sort.Ints(a)
	for i := 1; i <= len(a)/2; i++ {
		if len(a)%i == 0 {
			if work(a, i, m) {
				return len(a) / i
			}
		}
	}
	return 1
}

func work(a []int, l int, m int) bool {
	//fmt.Println(a, l, m)
	n := len(a)
	numOfSeg := n / l
	for i := 1; i < numOfSeg; i++ {
		if a[i*l]-a[(i-1)*l] != (a[0] + m - a[(numOfSeg-1)*l]) {
			//fmt.Println(a, l, "debug")
			return false
		}
	}
	for j := 1; j < l; j++ {
		for i := 1; i < n/l; i++ {
			if a[i*l+j]-a[i*l+j-1] != a[j]-a[j-1] {
				//fmt.Println(a, l, "debug2")
				return false
			}
		}
	}
	return true
}

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


The Free Meteor Association (FMA) has got a problem: as meteors are moving, the Universal Cosmic Descriptive Humorous Program (UCDHP) needs to add a special module that would analyze this movement. UCDHP stores some secret information about meteors as an n × m table with integers in its cells. The order of meteors in the Universe is changing. That's why the main UCDHP module receives the following queries:  The query to swap two table rows;  The query to swap two table columns;  The query to obtain a secret number in a particular table cell. As the main UCDHP module is critical, writing the functional of working with the table has been commissioned to you.

The first line contains three space-separated integers n, m and k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 500000) — the number of table columns and rows and the number of queries, correspondingly. Next n lines contain m space-separated numbers each — the initial state of the table. Each number p in the table is an integer and satisfies the inequality 0 ≤ p ≤ 106. Next k lines contain queries in the format "si xi yi", where si is one of the characters "с", "r" or "g", and xi, yi are two integers.   If si = "c", then the current query is the query to swap columns with indexes xi and yi (1 ≤ x, y ≤ m, x ≠ y);  If si = "r", then the current query is the query to swap rows with indexes xi and yi (1 ≤ x, y ≤ n, x ≠ y);  If si = "g", then the current query is the query to obtain the number that located in the xi-th row and in the yi-th column (1 ≤ x ≤ n, 1 ≤ y ≤ m).  The table rows are considered to be indexed from top to bottom from 1 to n, and the table columns — from left to right from 1 to m.

For each query to obtain a number (si = "g") print the required number. Print the answers to the queries in the order of the queries in the input.

NoteLet's see how the table changes in the second test case.After the first operation is fulfilled, the table looks like that:2 1 41 3 5After the second operation is fulfilled, the table looks like that:1 3 52 1 4So the answer to the third query (the number located in the first row and in the third column) will be 5.

package main

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

func main () {
	var rw = bufio.NewReadWriter(bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout))
	var n, m, k int
	var t [1000][1000]int
	var r, c [1000]int
	fmt.Fscan(rw, &n, &m, &k)
	rw.ReadString('\n')
	for i := 0; i < n; i++ {
		text, _ := rw.ReadString('\n')
		a := strings.Fields(text)
		for j := 0; j < m; j++ {
			t[i][j], _ = strconv.Atoi(a[j])
		}
	}
	for i := 0; i < n; i++ {
	   	r[i] = i
	}
	for j := 0; j < m; j++ {
		c[j] = j
	}
	var x, y int
	var s string
	for i := 0; i < k; i++ {
		fmt.Fscan(rw, &s, &x, &y)
		x -= 1
		y -= 1
		switch s {
		case "c": c[x], c[y] = c[y], c[x]
		case "r": r[x], r[y] = r[y], r[x]
		default: fmt.Fprintln(rw, t[r[x]][c[y]])
		}
	}
  rw.Flush()
}

package main

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

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

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

    var row,col [1005]int
    var a [1005][1005]int
    n := ScanInt(); m := ScanInt(); k := ScanInt();
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            a[i][j] = ScanInt()
        }
    }
    for i := 0; i < n; i++ { row[i] = i }
    for i := 0; i < m; i++ { col[i] = i }
    for ; k > 0; k-- {
        scanner.Scan()
        c := scanner.Text()
        x := ScanInt()-1
        y := ScanInt()-1
        if c[0] == 'c' {
            col[x], col[y] = col[y], col[x]
        } else if c[0] == 'r' {
            row[x], row[y] = row[y], row[x]
        } else {
            writer.WriteString(strconv.Itoa(a[row[x]][col[y]])+"\n")
        }
    }
    writer.Flush()
}

package main

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

var rw *bufio.ReadWriter = bufio.NewReadWriter(bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout))

func skipblanks() {
  for {
    c, _ := rw.ReadByte()
    switch c {
    case ' ', '\r', '\n':
    default:
      rw.UnreadByte()
      return
    }
  }
}

func next() string {
  skipblanks()
  token := []byte{}
  for {
    c, err := rw.ReadByte()
    if err != nil || c == ' ' || c == '\n' {
      return string(token)
    } else if c == '\r' {
      _, _ = rw.ReadByte()
      return string(token)
    } else {
      token = append(token, c)
    }
  }
}

func ni() int {
  ans, _ := strconv.Atoi(next())
  return ans
}

func nb() byte {
  c, _ := rw.ReadByte()
  return c
}

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

func Max(i, j int) int {
  if i >= j {
    return i
  }
  return j
}

func Min(i, j int) int {
  if i < j {
    return i
  }
  return j
}

func pri(n int)    { rw.WriteString(strconv.Itoa(n)) }
func prll(n int64) { rw.WriteString(strconv.FormatInt(n, 10)) }
func sp()          { rw.WriteByte(' ') }
func endl()        { rw.WriteByte('\n') }

func main() {

  n, m, k := ni(), ni(), ni()
  r, c, t := make([]int, n), make([]int, m), make([][]int, n)
  for i := 0; i < n; i++ {
    t[i] = make([]int, m)
    for j := 0; j < m; j++ {
      t[i][j] = ni()
    }
  }
  for i := 0; i < n; i++ {
    r[i] = i
  }
  for j := 0; j < m; j++ {
    c[j] = j
  }
  var x, y int
  var s byte
  const cr, cc byte = 'r', 'c'
  for i := 0; i < k; i++ {
    s, x, y = nb(), ni()-1, ni()-1
    switch s {
    case cc:
      c[x], c[y] = c[y], c[x]
    case cr:
      r[x], r[y] = r[y], r[x]
    default:
      {
        pri(t[r[x]][c[y]])
        endl()
      }
    }
  }
  rw.Flush()
}

package main

import (
        "os"
        "bufio"
        "strconv"
        "text/scanner"
)

var rw = bufio.NewReadWriter(bufio.NewReader(os.Stdin),
                             bufio.NewWriter(os.Stdout));

var sc scanner.Scanner

var n, m, k int;
var t [1000][1000]int;
var r, c [1000]int;

func main () {
  sc.Init(rw)
  sc.Scan()
  n, _ = strconv.Atoi(sc.TokenText())
  sc.Scan()
  m, _ = strconv.Atoi(sc.TokenText())
  sc.Scan()
  k, _ = strconv.Atoi(sc.TokenText())
  sc.Scan()
  for i := 0; i < n; i++ {
    for j := 0; j < m; j++ {
      t[i][j], _ = strconv.Atoi(sc.TokenText())
      sc.Scan()
    }
  }
  for i := 0; i < n; i++ {
    r[i] = i
  }
  for j := 0; j < m; j++ {
    c[j] = j
  }
  var x, y int
  var s string
  for i := 0; i < k; i++ {
    s = sc.TokenText()
    sc.Scan()
    x, _ = strconv.Atoi(sc.TokenText())
    sc.Scan()
    y, _ = strconv.Atoi(sc.TokenText())
    if i < k - 1 {
      sc.Scan()
    }
    x -= 1
    y -= 1
    switch s {
    case "c": c[x], c[y] = c[y], c[x]
    case "r": r[x], r[y] = r[y], r[x]
    default: { rw.WriteString(strconv.Itoa(t[r[x]][c[y]])); rw.WriteString("\n"); }
    }
  }
  rw.Flush()
}

package main

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

var w *bufio.Writer

var sc *bufio.Scanner

const minus, zero byte = '-', '0'

func nextInt() int {
	var ans, fst int = 0, 0
	var neg bool = false
	sc.Scan()
	if sc.Bytes()[0] == minus {
		fst = 1
		neg = true
	}
	for _, j := range sc.Bytes()[fst:] {
		ans = ans*10 + int(j-zero)
	}
	if neg == true {
		ans = -ans
	}
	return ans
}

func nextByte() byte {
	sc.Scan()
	return sc.Bytes()[0]
}

var n, m, k int
var t [1000][1000]int
var r, c [1000]int

func main() {

	w = bufio.NewWriter(os.Stdout)
	sc = bufio.NewScanner(os.Stdin)
	split := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
		advance, token, err = bufio.ScanWords(data, atEOF)
		return
	}
	sc.Split(split)

	var n, m, k int = nextInt(), nextInt(), nextInt()
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			t[i][j] = nextInt()
		}
	}
	for i := 0; i < n; i++ {
		r[i] = i
	}
	for j := 0; j < m; j++ {
		c[j] = j
	}
	var x, y int
	var s byte
	const cr, cc byte = 'r', 'c'
	for i := 0; i < k; i++ {
		s = nextByte()
		x, y = nextInt(), nextInt()
		x -= 1
		y -= 1
		switch s {
		case cc:
			c[x], c[y] = c[y], c[x]
		case cr:
			r[x], r[y] = r[y], r[x]
		default:
			{
				w.WriteString(strconv.Itoa(t[r[x]][c[y]]))
				w.WriteString("\n")
			}
		}
	}
	w.Flush()
}

package main

import (
        "os"
        "bufio"
        "strconv"
        "text/scanner"
)

var rw = bufio.NewReadWriter(bufio.NewReader(os.Stdin),
                             bufio.NewWriter(os.Stdout));

var sc scanner.Scanner

var n, m, k int;
var t [1000][1000]int;
var r, c [1000]int;

func main () {
  sc.Init(rw)
  sc.Mode = scanner.ScanStrings | scanner.ScanInts
  sc.Whitespace = 1 << '\n' | 1 << '\r' | 1 << ' '
  sc.Scan()
  n, _ = strconv.Atoi(sc.TokenText())
  sc.Scan()
  m, _ = strconv.Atoi(sc.TokenText())
  sc.Scan()
  k, _ = strconv.Atoi(sc.TokenText())
  sc.Scan()
  for i := 0; i < n; i++ {
    for j := 0; j < m; j++ {
      t[i][j], _ = strconv.Atoi(sc.TokenText())
      sc.Scan()
    }
  }
  for i := 0; i < n; i++ {
    r[i] = i
  }
  for j := 0; j < m; j++ {
    c[j] = j
  }
  var x, y int
  var s string
  for i := 0; i < k; i++ {
    s = sc.TokenText()
    sc.Scan()
    x, _ = strconv.Atoi(sc.TokenText())
    sc.Scan()
    y, _ = strconv.Atoi(sc.TokenText())
    if i < k - 1 {
      sc.Scan()
    }
    x -= 1
    y -= 1
    switch s {
    case "c": c[x], c[y] = c[y], c[x]
    case "r": r[x], r[y] = r[y], r[x]
    default: { rw.WriteString(strconv.Itoa(t[r[x]][c[y]])); rw.WriteString("\n"); }
    }
  }
  rw.Flush()
}

package main

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

var w *bufio.Writer

var sc *bufio.Scanner

const minus, zero byte = '-', '0'

func nextInt() int {
    var ans, fst int = 0, 0
    var neg bool = false
    sc.Scan()
    if sc.Bytes()[0] == minus {
        fst = 1
        neg = true
    }
    for _, j := range sc.Bytes()[fst:] {
        ans = ans*10 + int(j-zero)
    }
    if neg == true {
        ans = -ans
    }
    return ans
}

func nextByte() byte {
    sc.Scan()
    return sc.Bytes()[0]
}

var n, m, k int
var t [1000][1000]int
var r, c [1000]int

func main() {

    w = bufio.NewWriter(os.Stdout)
    sc = bufio.NewScanner(os.Stdin)
    sc.Split(bufio.ScanWords)

    var n, m, k int = nextInt(), nextInt(), nextInt()
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            t[i][j] = nextInt()
        }
    }
    for i := 0; i < n; i++ {
        r[i] = i
    }
    for j := 0; j < m; j++ {
        c[j] = j
    }
    var x, y int
    var s byte
    const cr, cc byte = 'r', 'c'
    for i := 0; i < k; i++ {
        s, x, y = nextByte(), nextInt() - 1, nextInt() - 1
        switch s {
        case cc:
            c[x], c[y] = c[y], c[x]
        case cr:
            r[x], r[y] = r[y], r[x]
        default:
            {
                w.WriteString(strconv.Itoa(t[r[x]][c[y]]))
                w.WriteByte('\n')
            }
        }
    }
    w.Flush()
}

package main

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

var rw = bufio.NewReadWriter(bufio.NewReader(os.Stdin),
                             bufio.NewWriter(os.Stdout));

var n, m, k int;
var t [1000][1000]int;
var r, c [1000]int;

func main () {
  nmk, _ := rw.ReadString('\n')
  cc := strings.Fields(nmk)
  n, _ = strconv.Atoi(cc[0])
  m, _ = strconv.Atoi(cc[1])
  k, _ = strconv.Atoi(cc[2])
  for i := 0; i < n; i++ {
    text, _ := rw.ReadString('\n')
    a := strings.Fields(text)
    for j := 0; j < m; j++ {
      t[i][j], _ = strconv.Atoi(a[j])
    }
  }
  for i := 0; i < n; i++ {
    r[i] = i
  }
  for j := 0; j < m; j++ {
    c[j] = j
  }
  var x, y int
  for i := 0; i < k; i++ {
    text, _ := rw.ReadString('\n')
    a := strings.Fields(text)
    x, _ = strconv.Atoi(a[1])
    y, _ = strconv.Atoi(a[2])
    x -= 1
    y -= 1
    switch a[0] {
    case "c": c[x], c[y] = c[y], c[x]
    case "r": r[x], r[y] = r[y], r[x]
    default: { rw.WriteString(strconv.Itoa(t[r[x]][c[y]])); rw.WriteString("\n"); }
    }
  }
  rw.Flush()
}

package main

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

var w *bufio.Writer

var sc *bufio.Scanner

const minus, zero byte = '-', '0'

func nextInt() int {
    var ans, fst int = 0, 0
    var neg bool = false
    sc.Scan()
    if sc.Bytes()[0] == minus {
        fst = 1
        neg = true
    }
    for _, j := range sc.Bytes()[fst:] {
        ans = ans*10 + int(j-zero)
    }
    if neg == true {
        ans = -ans
    }
    return ans
}

func nextByte() byte {
    sc.Scan()
    return sc.Bytes()[0]
}

var n, m, k int
var t [1000][1000]int
var r, c [1000]int

func main() {

    w = bufio.NewWriter(os.Stdout)
    sc = bufio.NewScanner(os.Stdin)
    sc.Split(bufio.ScanWords)

    var n, m, k int = nextInt(), nextInt(), nextInt()
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            t[i][j] = nextInt()
        }
    }
    for i := 0; i < n; i++ {
        r[i] = i
    }
    for j := 0; j < m; j++ {
        c[j] = j
    }
    var x, y int
    var s byte
    const cr, cc byte = 'r', 'c'
    for i := 0; i < k; i++ {
        s = nextByte()
        x, y = nextInt(), nextInt()
        x -= 1
        y -= 1
        switch s {
        case cc:
            c[x], c[y] = c[y], c[x]
        case cr:
            r[x], r[y] = r[y], r[x]
        default:
            {
                w.WriteString(strconv.Itoa(t[r[x]][c[y]]))
                w.WriteString("\n")
            }
        }
    }
    w.Flush()
}

package main

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

var rw = bufio.NewReadWriter(bufio.NewReader(os.Stdin),
                             bufio.NewWriter(os.Stdout));

var n, m, k int;
var t [1000][1000]int;
var r, c [1000]int;

func main () {
  fmt.Fscan(rw, &n, &m, &k)
  rw.ReadString('\n')
  for i := 0; i < n; i++ {
    text, _ := rw.ReadString('\n')
    a := strings.Fields(text)
    for j := 0; j < m; j++ {
      t[i][j], _ = strconv.Atoi(a[j])
    }
  }
  for i := 0; i < n; i++ {
    r[i] = i
  }
  for j := 0; j < m; j++ {
    c[j] = j
  }
  var x, y int
  var s string
  for i := 0; i < k; i++ {
    fmt.Fscan(rw, &s, &x, &y)
    x -= 1
    y -= 1
    switch s {
    case "c": c[x], c[y] = c[y], c[x]
    case "r": r[x], r[y] = r[y], r[x]
    default: fmt.Fprintln(rw, t[r[x]][c[y]])
    }
  }
  rw.Flush()
  //???
}

package main

import (
        "os"
        "bufio"
        "strconv"
        "text/scanner"
)

var rw = bufio.NewReadWriter(bufio.NewReader(os.Stdin),
                             bufio.NewWriter(os.Stdout));

var sc scanner.Scanner

var n, m, k int;
var t [1000][1000]int;
var r, c [1000]int;

func main () {
  sc.Init(rw)
  sc.Scan()
  n, _ = strconv.Atoi(sc.TokenText())
  sc.Scan()
  m, _ = strconv.Atoi(sc.TokenText())
  sc.Scan()
  k, _ = strconv.Atoi(sc.TokenText())
  sc.Scan()
  for i := 0; i < n; i++ {
    for j := 0; j < m; j++ {
      t[i][j], _ = strconv.Atoi(sc.TokenText())
      sc.Scan()
    }
  }
  for i := 0; i < n; i++ {
    r[i] = i
  }
  for j := 0; j < m; j++ {
    c[j] = j
  }
  var x, y int
  var s string
  for i := 0; i < k; i++ {
    s = sc.TokenText()
    sc.Scan()
    x, _ = strconv.Atoi(sc.TokenText())
    sc.Scan()
    y, _ = strconv.Atoi(sc.TokenText())
    if i < k - 1 {
      sc.Scan()
    }
    x -= 1
    y -= 1
    switch s {
    case "c": c[x], c[y] = c[y], c[x]
    case "r": r[x], r[y] = r[y], r[x]
    default: rw.WriteString(strconv.Itoa(t[r[x]][c[y]]) + "\n")
    }
  }
  rw.Flush()
}

package main

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

var rw = bufio.NewReadWriter(bufio.NewReader(os.Stdin),
                             bufio.NewWriter(os.Stdout));

var n, m, k int;
var t [1000][1000]int;
var r, c [1000]int;

func main () {
  fmt.Fscan(rw, &n, &m, &k)
  rw.ReadString('\n')
  for i := 0; i < n; i++ {
    text, _ := rw.ReadString('\n')
    a := strings.Fields(text)
    for j := 0; j < m; j++ {
      t[i][j], _ = strconv.Atoi(a[j])
    }
  }
  for i := 0; i < n; i++ {
    r[i] = i
  }
  for j := 0; j < m; j++ {
    c[j] = j
  }
  var x, y int
  for i := 0; i < k; i++ {
    text, _ := rw.ReadString('\n')
    a := strings.Fields(text)
    x, _ = strconv.Atoi(a[1])
    y, _ = strconv.Atoi(a[2])
    x -= 1
    y -= 1
    switch a[0] {
    case "c": c[x], c[y] = c[y], c[x]
    case "r": r[x], r[y] = r[y], r[x]
    default: fmt.Fprint(rw, t[r[x]][c[y]], "\n")
    }
  }
  rw.Flush()
}

package main

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

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

    var n,m,k,x,y int
    var c string
    var a [1005][1005]int
    fmt.Fscan(reader,&n,&m,&k)
    reader.ReadString('\n')
    row, col := make(map[int]int), make(map[int]int)
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            fmt.Fscan(reader,&a[i][j])
        }
    }
    for i := 0; i < n; i++ { row[i] = i }
    for i := 0; i < m; i++ { col[i] = i }
    for ; k > 0; k-- {
        fmt.Fscan(reader,&c,&x,&y)
        x--; y--;
        if c == "c" {
            col[x], col[y] = col[y], col[x]
        } else if c == "r" {
            row[x], row[y] = row[y], row[x]
        } else {
            writer.WriteString(strconv.Itoa(a[row[x]][col[y]])+"\n")
        }
    }
    writer.Flush()
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    writer := bufio.NewWriter(os.Stdout)
    var n,m,k int
    var a [1005][1005]int
    fmt.Scan(&n,&m,&k)
    row, col := make(map[int]int), make(map[int]int)
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            scanner.Scan()
            a[i][j], _ = strconv.Atoi(scanner.Text())
        }
    }
    for i := 0; i < n; i++ { row[i] = i }
    for i := 0; i < m; i++ { col[i] = i }
    for ; k > 0; k-- {
        scanner.Scan()
        c := scanner.Text()
        scanner.Scan()
        x, _ := strconv.Atoi(scanner.Text())
        scanner.Scan()
        y, _ := strconv.Atoi(scanner.Text())
        x--; y--;
        if c == "c" {
            col[x], col[y] = col[y], col[x]
        } else if c == "r" {
            row[x], row[y] = row[y], row[x]
        } else {
            writer.WriteString(strconv.Itoa(a[row[x]][col[y]])+"\n")
        }
    }
    writer.Flush()
}

package main

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

func main() {
    reader := bufio.NewReader(os.Stdin)
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    writer := bufio.NewWriter(os.Stdout)

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

    var x,y int
    var c string
    var a [1005][1005]int
    n := ScanInt(); m := ScanInt(); k := ScanInt();
    row, col := make(map[int]int), make(map[int]int)
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            a[i][j] = ScanInt()
        }
    }
    for i := 0; i < n; i++ { row[i] = i }
    for i := 0; i < m; i++ { col[i] = i }
    for ; k > 0; k-- {
        fmt.Fscan(reader,&c,&x,&y)
        x--; y--;
        if c == "c" {
            col[x], col[y] = col[y], col[x]
        } else if c == "r" {
            row[x], row[y] = row[y], row[x]
        } else {
            writer.WriteString(strconv.Itoa(a[row[x]][col[y]])+"\n")
        }
    }
    writer.Flush()
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    writer := bufio.NewWriter(os.Stdout)
    var n,m,k int
    var a [1005][1005]int
    fmt.Scan(&n,&m,&k)
    row, col := make(map[int]int), make(map[int]int)
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            for scanner.Scan() { if scanner.Text() != "" { break } }
            a[i][j], _ = strconv.Atoi(scanner.Text())
        }
    }
    for i := 0; i < n; i++ { row[i] = i }
    for i := 0; i < m; i++ { col[i] = i }
    for ; k > 0; k-- {
        for scanner.Scan() { if scanner.Text() != "" { break } }
        c := scanner.Text()
        for scanner.Scan() { if scanner.Text() != "" { break } }
        x, _ := strconv.Atoi(scanner.Text())
        for scanner.Scan() { if scanner.Text() != "" { break } }
        y, _ := strconv.Atoi(scanner.Text())
        x--; y--;
        if c == "c" {
            col[x], col[y] = col[y], col[x]
        } else if c == "r" {
            row[x], row[y] = row[y], row[x]
        } else {
            writer.WriteString(strconv.Itoa(a[row[x]][col[y]])+"\n")
        }
    }
    writer.Flush()
}

package main

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

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

    var n,m,k,x,y int
    var c string
    var a [1005][1005]int
    fmt.Fscan(reader,&n,&m,&k)
    reader.ReadLine()
    row, col := make(map[int]int), make(map[int]int)
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            fmt.Fscan(reader,&a[i][j])
        }
    }
    for i := 0; i < n; i++ { row[i] = i }
    for i := 0; i < m; i++ { col[i] = i }
    for ; k > 0; k-- {
        fmt.Fscan(reader,&c,&x,&y)
        x--; y--;
        if c == "c" {
            col[x], col[y] = col[y], col[x]
        } else if c == "r" {
            row[x], row[y] = row[y], row[x]
        } else {
            writer.WriteString(strconv.Itoa(a[row[x]][col[y]])+"\n")
        }
    }
    writer.Flush()
}

package main

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

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

    var n,m,k,x,y int
    var c string
    var a [1005][1005]int
//    fmt.Fscan(reader,&n,&m,&k)
    fmt.Scan(&n,&m,&k)
    row, col := make(map[int]int), make(map[int]int)
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
//            fmt.Fscan(reader,&a[i][j])
            fmt.Scan(&a[i][j])
        }
    }
    for i := 0; i < n; i++ { row[i] = i }
    for i := 0; i < m; i++ { col[i] = i }
    for ; k > 0; k-- {
//        fmt.Fscan(reader,&c,&x,&y)
        fmt.Scan(&c,&x,&y)
        x--; y--;
        if c == "c" {
            col[x], col[y] = col[y], col[x]
        } else if c == "r" {
            row[x], row[y] = row[y], row[x]
        } else {
//            writer.WriteString(strconv.Itoa(a[row[x]][col[y]])+"\n")
            ans := strconv.Itoa(a[row[x]][col[y]])
            fmt.Println(ans)
        }
    }
//    writer.Flush()
}

package main

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

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

    var n,m,k,x,y int
    var c string
    var a [1005][1005]int
//    fmt.Fscan(reader,&n,&m,&k)
    fmt.Scan(&n,&m,&k)
    row, col := make(map[int]int), make(map[int]int)
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
//            fmt.Fscan(reader,&a[i][j])
            fmt.Scan(&a[i][j])
        }
    }
    for i := 0; i < n; i++ { row[i] = i }
    for i := 0; i < m; i++ { col[i] = i }
    for ; k > 0; k-- {
//        fmt.Fscan(reader,&c,&x,&y)
        fmt.Scan(&c,&x,&y)
        x--; y--;
        if c == "c" {
            col[x], col[y] = col[y], col[x]
        } else if c == "r" {
            row[x], row[y] = row[y], row[x]
        } else {
//            writer.WriteString(strconv.Itoa(a[row[x]][col[y]])+"\n")
            fmt.Println(strconv.Itoa(a[row[x]][col[y]]))
        }
    }
//    writer.Flush()
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    writer := bufio.NewWriter(os.Stdout)
    var n,m,k int
    var a [1005][1005]int
    fmt.Scan(&n,&m,&k)
    row, col := make(map[int]int), make(map[int]int)
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            scanner.Scan()
            a[i][j], _ = strconv.Atoi(scanner.Text())
        }
    }
    for i := 0; i < n; i++ { row[i] = i }
    for i := 0; i < m; i++ { col[i] = i }
    for ; k > 0; k-- {
        for scanner.Scan() { if scanner.Text() != "" { break } }
        c := scanner.Text()
        scanner.Scan()
        x, _ := strconv.Atoi(scanner.Text())
        scanner.Scan()
        y, _ := strconv.Atoi(scanner.Text())
        x--; y--;
        if c == "c" {
            col[x], col[y] = col[y], col[x]
        } else if c == "r" {
            row[x], row[y] = row[y], row[x]
        } else {
            writer.WriteString(strconv.Itoa(a[row[x]][col[y]])+"\n")
        }
    }
    writer.Flush()
}

package main

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

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

    var n,m,k,x,y int
    var c string
    var a [1005][1005]int
    fmt.Fscan(reader,&n,&m,&k)
    reader.ReadString('\n')
    row, col := make(map[int]int), make(map[int]int)
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            fmt.Fscan(reader,&a[i][j])
        }
        reader.ReadString('\n')
    }
    for i := 0; i < n; i++ { row[i] = i }
    for i := 0; i < m; i++ { col[i] = i }
    for ; k > 0; k-- {
        fmt.Fscan(reader,&c,&x,&y)
        x--; y--;
        if c == "c" {
            col[x], col[y] = col[y], col[x]
        } else if c == "r" {
            row[x], row[y] = row[y], row[x]
        } else {
            writer.WriteString(strconv.Itoa(a[row[x]][col[y]])+"\n")
        }
        reader.ReadString('\n')
    }
    writer.Flush()
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    writer := bufio.NewWriter(os.Stdout)
    var n,m,k int
    var a [1005][1005]int
    fmt.Scan(&n,&m,&k)
    row, col := make(map[int]int), make(map[int]int)
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            scanner.Scan()
            a[i][j], _ = strconv.Atoi(scanner.Text())
        }
    }
    for i := 0; i < n; i++ { row[i] = i }
    for i := 0; i < m; i++ { col[i] = i }
    for ; k > 0; k-- {
        for scanner.Scan() { if (scanner.Text() == "c") || (scanner.Text() == "r") || (scanner.Text() == "g") { break } }
        c := scanner.Text()
        scanner.Scan()
        x, _ := strconv.Atoi(scanner.Text())
        scanner.Scan()
        y, _ := strconv.Atoi(scanner.Text())
        fmt.Println(c,x,y)
        x--; y--;
        if c == "c" {
            col[x], col[y] = col[y], col[x]
        } else if c == "r" {
            row[x], row[y] = row[y], row[x]
        } else {
            writer.WriteString(strconv.Itoa(a[row[x]][col[y]])+"\n")
        }
    }
    writer.Flush()
}

package main

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

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

    var n,m,k,x,y int
    var c string
    var a [1005][1005]int
    fmt.Fscan(reader,&n,&m,&k)
    row, col := make(map[int]int), make(map[int]int)
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            fmt.Fscan(reader,&a[i][j])
        }
    }
    for i := 0; i < n; i++ { row[i] = i }
    for i := 0; i < m; i++ { col[i] = i }
    for ; k > 0; k-- {
        fmt.Fscan(reader,&c,&x,&y)
        x--; y--;
        if c == "c" {
            col[x], col[y] = col[y], col[x]
        } else if c == "r" {
            row[x], row[y] = row[y], row[x]
        } else {
            writer.WriteString(strconv.Itoa(a[row[x]][col[y]])+"\n")
        }
    }
    writer.Flush()
}

package main

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

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

    var n,m,k,x,y int
    var c string
    var a [1005][1005]int
//    fmt.Fscan(reader,&n,&m,&k)
    fmt.Scan(&n,&m,&k)
    row, col := make(map[int]int), make(map[int]int)
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
//            fmt.Fscan(reader,&a[i][j])
            fmt.Scan(&a[i][j])
        }
    }
    for i := 0; i < n; i++ { row[i] = i }
    for i := 0; i < m; i++ { col[i] = i }
    for ; k > 0; k-- {
//        fmt.Fscan(reader,&c,&x,&y)
        fmt.Scan(&c,&x,&y)
        x--; y--;
        if c[0] == 'c' {
            col[x], col[y] = col[y], col[x]
        } else if c[0] == 'r' {
            row[x], row[y] = row[y], row[x]
        } else {
//            writer.WriteString(strconv.Itoa(a[row[x]][col[y]])+"\n")
            fmt.Println(strconv.Itoa(a[row[x]][col[y]]))
        }
    }
//    writer.Flush()
}

package main

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

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

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

    var a [1005][1005]int
    n := ScanInt(); m := ScanInt(); k := ScanInt();
    row, col := make(map[int]int), make(map[int]int)
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            a[i][j] = ScanInt()
        }
    }
    for i := 0; i < n; i++ { row[i] = i }
    for i := 0; i < m; i++ { col[i] = i }
    for ; k > 0; k-- {
        for scanner.Scan() { if (scanner.Text() == "c") || (scanner.Text() == "r") || (scanner.Text() == "g") { break } }
        c := scanner.Text()
        x := ScanInt()-1
        y := ScanInt()-1
        if c == "c" {
            col[x], col[y] = col[y], col[x]
        } else if c == "r" {
            row[x], row[y] = row[y], row[x]
        } else {
            writer.WriteString(strconv.Itoa(a[row[x]][col[y]])+"\n")
        }
    }
    writer.Flush()
}


You have a description of a lever as string s. We'll represent the string length as record |s|, then the lever looks as a horizontal bar with weights of length |s| - 1 with exactly one pivot. We will assume that the bar is a segment on the Ox axis between points 0 and |s| - 1.The decoding of the lever description is given below.  If the i-th character of the string equals "^", that means that at coordinate i there is the pivot under the bar.  If the i-th character of the string equals "=", that means that at coordinate i there is nothing lying on the bar.  If the i-th character of the string equals digit c (1-9), that means that at coordinate i there is a weight of mass c on the bar. Your task is, given the lever description, print if it will be in balance or not. Assume that the bar doesn't weight anything. Assume that the bar initially is in balance then all weights are simultaneously put on it. After that the bar either tilts to the left, or tilts to the right, or is in balance.

The first line contains the lever description as a non-empty string s (3 ≤ |s| ≤ 106), consisting of digits (1-9) and characters "^" and "=". It is guaranteed that the line contains exactly one character "^". It is guaranteed that the pivot of the lever isn't located in any end of the lever bar. To solve the problem you may need 64-bit integer numbers. Please, do not forget to use them in your programs.

Print "left" if the given lever tilts to the left, "right" if it tilts to the right and "balance", if it is in balance.

NoteAs you solve the problem, you may find the following link useful to better understand how a lever functions: http://en.wikipedia.org/wiki/Lever.The pictures to the examples:

package main

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

func main() {
	fin := bufio.NewReader(os.Stdin)
	var s string
	fmt.Fscanf(fin, "%s", &s)

	p := strings.Index(s, "^")
	var l, r int64
	for i := 0; i < p; i++ {
		if s[i] != '=' {
			l += int64(p-i) * int64(s[i]-'0')
		}
	}
	for i := p + 1; i < len(s); i++ {
		if s[i] != '=' {
			r += int64(i-p) * int64(s[i]-'0')
		}
	}

	switch {
	case l == r:
		fmt.Print("balance")
	case l > r:
		fmt.Print("left")
	case r > l:
		fmt.Print("right")
	}
}

package main

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

func main() {
    reader := bufio.NewReader(os.Stdin)
    var s string
    fmt.Fscan(reader,&s)
    pivot, left, right, temp := 0, int64(0), int64(0), int64(0)
    for _, v := range s {
        if v == '^' { break }
        if s[pivot] != '=' { temp += int64(s[pivot]-'0') }
        left += temp
        pivot++
    }
    for i := pivot+1; i < len(s); i++ {
        if s[i] != '=' {
            right += int64(s[i]-'0')*int64(i-pivot)
        }
    }
    if left == right {
        fmt.Println("balance")
    } else if left > right {
        fmt.Println("left")
    } else {
        fmt.Println("right")
    }
}

package main

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

func main() {
	fin := bufio.NewReader(os.Stdin)
	s := ""
	fmt.Fscanf(fin, "%s", &s)
	pivot := strings.IndexByte(s, '^')
	bal := int64(0)
	for i, x := range s {
		if x >= '0' && x <= '9' {
			bal += int64(pivot-i) * int64(x-'0')
		}
	}
	switch {
	case bal > 0:
		fmt.Println("left")
	case bal < 0:
		fmt.Println("right")
	default:
		fmt.Println("balance")
	}
}

package main

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

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1000005)
	s, _ := reader.ReadString('\n')
	pivot := strings.IndexByte(s, '^')
	bal := int64(0)
	for i, x := range s {
		if x >= '0' && x <= '9' {
			bal += int64(pivot-i) * int64(x-'0')
		}
	}
	switch {
	case bal > 0:
		fmt.Println("left")
	case bal < 0:
		fmt.Println("right")
	default:
		fmt.Println("balance")
	}
}

// 376A-mic
package main

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

func main() {
    k := bufio.NewReader(os.Stdin)
    var s string
    var left, right, p, i int64
    fmt.Fscan(k, &s)
    for i = 0; i < int64(len(s)); i++ {
        if s[i] == '^' {
            p = i
            break
        }
    }
    for i = 0; i < int64(len(s)); i++ {
        if s[i] == '=' {
            continue
        } else if i > p {
            right += (int64(s[i]) - 48) * (i - p)
        } else {
            left += (int64(s[i]) - 48) * (p - i)
        }
    }
    if right == left {
        fmt.Println("balance")
    } else if right > left {
        fmt.Println("right")
    } else {
        fmt.Println("left")
    }

}

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

func main() {
	var m string
	var n,s int
	s=0
	fmt.Scan(&m)
	for j,ss:=range m {
		if ss=='^' {
			n=j
		}
	}
	//fmt.Println(n)
	for i,mm:=range m {
		if (mm-'0')>=0 && (mm-'0')<=9{
			//fmt.Println("masuk",i)
			s+=(i-n)*int(mm-'0')
			//fmt.Println(s)
		}
	}
	//fmt.Println(s)
	if  s>0{
		fmt.Println("right")
	} else if s<0{
		fmt.Println("left")
	} else {
		fmt.Println("balance")
	}
}

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

func main() {
	var s string
	var n int
	j:=0
	fmt.Scan(&s)
	for i,ss:=range s {
		if ss=='^' {
			n=i
		}
	}
	for i:=0;i<n;i++ {
		if (s[i]-'0')>=0 && (s[i]-'0')<=9{
			j+=(i-n)*int(s[i]-'0')
		}
	}
	if  j>0{
		fmt.Println("right")
	} else if j<0{
		fmt.Println("left")
	} else {
		fmt.Println("balance")
	}
}

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

func main() {
	var s string
	var m,t int64
	input:= bufio.NewScanner(os.Stdin)
	input.Scan()
	s=input.Text()
	for tt,mm:=range s {
		if mm=='^' {
			t=int64(tt)
		}
	}
	for j,ss:=range s {
		if ss>='1' && ss<='9' {
			m+=((t-int64(j))*int64(s[j]-48))
		}
	}
	if  m>0{
		fmt.Println("left")
	} else if m<0{
		fmt.Println("right")
	} else  if m==0{
		fmt.Println("balance")
	}
}

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

func main() {
	var m string
	var n,s int
	s=0
	fmt.Scan(&m)
	for j,ss:=range m {
		if ss=='^' {
			n=j
		}
	}
	fmt.Println(n)
	for i,mm:=range m {
		if (mm-'0')>=0 && (mm-'0')<=9{
			//fmt.Println("masuk",i)
			s+=(i-n)*int(mm-'0')
			//fmt.Println(s)
		}
	}
	//fmt.Println(s)
	if  s>0{
		fmt.Println("right")
	} else if s<0{
		fmt.Println("left")
	} else {
		fmt.Println("balance")
	}
}

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

func main() {
	var m string
	var n,s int
	s=0
	input:= bufio.NewScanner(os.Stdin)
	input.Scan()
	m=input.Text()
	for j,ss:=range m {
		if ss=='^' {
			n=j
		}
	}
	//fmt.Println(n)
	for i,mm:=range m {
		if (mm-'0')>=0 && (mm-'0')<=9{
			//fmt.Println("masuk",i)
			s+=(i-n)*int(mm-'0')
			//fmt.Println(s)
		}
	}
	//fmt.Println(s)
	if  s>0{
		fmt.Println("right")
	} else if s<0{
		fmt.Println("left")
	} else {
		fmt.Println("balance")
	}
}

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

func main() {
	var m string
	var n,s int64
	s=0
	input:= bufio.NewScanner(os.Stdin)
	input.Scan()
	m=input.Text()
	for j,ss:=range m {
		if ss=='^' {
			n=int64(j)
		}
	}
	//fmt.Println(n)
	for i,mm:=range m {
		if (mm-'0')>=0 && (mm-'0')<=9{
			//fmt.Println("masuk",i)
			s+=(int64(i)-n)*int64(mm-'0')
			//fmt.Println(s)
		}
	}
	//fmt.Println(s)
	if  s>0{
		fmt.Println("right")
	} else if s<0{
		fmt.Println("left")
	} else {
		fmt.Println("balance")
	}
}

package main

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

func main() {
    var s []byte
    s, _ = ioutil.ReadAll(os.Stdin)
    pivot, left, right, temp := 0, int64(0), int64(0), int64(0)
    for _, v := range s {
        if v == '^' { break }
        if s[pivot] != '=' { temp += int64(s[pivot]-'0') }
        left += temp
        pivot++
    }
    for i := pivot+1; s[i] != '\n'; i++ {
        if s[i] != '=' {
            right += int64(s[i]-'0')*int64(i-pivot)
        }
    }
    if left == right {
        fmt.Println("balance")
    } else if left > right {
        fmt.Println("left")
    } else {
        fmt.Println("right")
    }
}

package main

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

func main() {
    var s []byte
    for len(s) == 0 {
        s, _ = ioutil.ReadAll(os.Stdin)
    }
    pivot, left, right, temp := 0, int64(0), int64(0), int64(0)
    for _, v := range s {
        if v == '^' { break }
        if s[pivot] != '=' { temp += int64(s[pivot]-'0') }
        left += temp
        pivot++
    }
    for i := pivot+1; s[i] != '\n'; i++ {
        if s[i] != '=' {
            right += int64(s[i]-'0')*int64(i-pivot)
        }
    }
    if left == right {
        fmt.Println("balance")
    } else if left > right {
        fmt.Println("left")
    } else {
        fmt.Println("right")
    }
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanRunes)
    pivot, left, temp := 0, int64(0), int64(0)
    for scanner.Scan() {
        r := scanner.Bytes()[0]
        if r == '^' { break }
        if r != '=' { temp += int64(r-'0') }
        left += temp
        pivot++
    }
    right, i := int64(0), pivot+1
    for scanner.Scan() {
        r := scanner.Bytes()[0]
        if r == '\n' { break }
        if r != '=' {
            right += int64(r-'0')*int64(i-pivot)
        }
        i++
    }
    if left == right {
        fmt.Println("balance")
    } else if left > right {
        fmt.Println("left")
    } else {
        fmt.Println("right")
    }
}

package main

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

func main() {
    reader := bufio.NewReader(os.Stdin)
    s, _ := reader.ReadString('\n')
    pivot, left, right, temp := 0, int64(0), int64(0), int64(0)
    for s[pivot] != '^' {
        if s[pivot] != '=' { temp += int64(s[pivot]-'0') }
        left += temp
        pivot++
    }
    for i := pivot+1; s[i] != '\n'; i++ {
        if s[i] != '=' {
            right += int64(s[i]-'0')*int64(i-pivot)
        }
    }
    if left == right {
        fmt.Println("balance")
    } else if left > right {
        fmt.Println("left")
    } else {
        fmt.Println("right")
    }
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	s := scanner.Text()
	pivot := strings.IndexByte(s, '^')
	bal := 0
	for i, x := range s {
		if x >= '0' && x <= '9' {
			bal += (pivot - i) * int(x-'0')
		}
	}
	switch {
	case bal > 0:
		fmt.Println("left")
	case bal < 0:
		fmt.Println("right")
	default:
		fmt.Println("balance")
	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	s := scanner.Text()
	pivot := strings.IndexByte(s, '^')
	bal := int64(0)
	for i, x := range s {
		if x >= '0' && x <= '9' {
			bal += int64(pivot-i) * int64(x-'0')
		}
	}
	switch {
	case bal > 0:
		fmt.Println("left")
	case bal < 0:
		fmt.Println("right")
	default:
		fmt.Println("balance")
	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	s := scanner.Text()
	pivot := strings.IndexByte(s, '^')
	bal := int64(0)
	for i, x := range s {
		if x >= '0' && x <= '9' {
			bal += int64(pivot-i) * int64(x-'0')
		}
	}
	switch {
	case bal > 0:
		fmt.Println("left")
	case bal < 0:
		fmt.Println("right")
	default:
		fmt.Println("balance")
	}
}

// 376A-mic
package main

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

func main() {
	var s string
	var left, right, p, i int64
	for i = 0; i < int64(len(s)); i++ {
		if s[i] == '^' {
			p = i
			break
		}
	}
	for i = p - 1; i >= 0; i-- {
		if s[i] == '=' {
			continue
		}
		left += (int64(s[i]) - 48) * (p - i)
	}
	for i = p + 1; i < int64(len(s)); i++ {
		if s[i] == '=' {
			continue
		}
		right += (int64(s[i]) - 48) * (i - p)
	}
	if right == left {
		fmt.Println("balance")
	} else if right > left {
		fmt.Println("right")
	} else {
		fmt.Println("left")
	}

}

// 376A-mic
package main

import (
    "fmt"
)

func main() {
    var s string
    var left, right, p int
    fmt.Scan(&s)
    for i := 0; i < len(s); i++ {
        if s[i] == '^' {
            p = i
        }
    }
    for i := 0; i < p; i++ {
        if s[i] == '=' {
            continue
        }
        left += (int(s[i])) * (p - i)
    }
    for i := p + 1; i < len(s); i++ {
        if s[i] == '=' {
            continue
        }
        right += (int(s[i])) * (i - p)
    }
    if right == left {
        fmt.Println("balance")
    } else if right > left {
        fmt.Println("right")
    } else {
        fmt.Println("left")
    }

}

// 376A-mic
package main

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

func main() {
    k := bufio.NewScanner(os.Stdin)
    k.Split(bufio.ScanLines)
    var s string
    var left, right, p, i int64
    k.Scan()
    s = k.Text()
    //fmt.Scan(&s)
    for i = 0; i < int64(len(s)); i++ {
        if s[i] == '^' {
            p = i
            break
        }
    }
    for i = 0; i < int64(len(s)); i++ {
        if s[i] == '=' {
            continue
        } else if i > p {
            right += (int64(s[i]) - 48) * (i - p)
        } else {
            left += (int64(s[i]) - 48) * (p - i)
        }
    }
    if right == left {
        fmt.Println("balance")
    } else if right > left {
        fmt.Println("right")
    } else {
        fmt.Println("left")
    }

}

// 376A-mic
package main

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

func main() {
	k := bufio.NewScanner(os.Stdin)

	var s string
	var left, right, p, i int64
	k.Scan()
	s = k.Text()
	for i = 0; i < int64(len(s)); i++ {
		if s[i] == '^' {
			p = i
		}
	}
	for i = p - 1; i >= 0; i-- {
		if s[i] == '=' {
			continue
		}
		left += (int64(s[i]) - 48) * (p - i)
	}
	for i = p + 1; i < int64(len(s)); i++ {
		if s[i] == '=' {
			continue
		}
		right += (int64(s[i]) - 48) * (i - p)
	}
	if right == left {
		fmt.Println("balance")
	} else if right > left {
		fmt.Println("right")
	} else {
		fmt.Println("left")
	}

}

// 376A-mic
package main

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

func main() {
    k := bufio.NewScanner(os.Stdin)
    var s string
    var left, right, p, i int64
    k.Scan()
    s = k.Text()
    //fmt.Scan(&s)
    for i = 0; i < int64(len(s)); i++ {
        if s[i] == '^' {
            p = i
            break
        }
    }
    for i = 0; i < int64(len(s)); i++ {
        if s[i] == '=' {
            continue
        } else if i > p {
            right += (int64(s[i]) - 48) * (i - p)
        } else {
            left += (int64(s[i]) - 48) * (p - i)
        }
    }
    if right == left {
        fmt.Println("balance")
    } else if right > left {
        fmt.Println("right")
    } else {
        fmt.Println("left")
    }

}

// 376A-mic
package main

import (
    "fmt"
)

func main() {
    var s string
    var left, right, p int
    fmt.Scan(&s)
    for i := 0; i < len(s); i++ {
        if s[i] == '^' {
            p = i
        }
    }
    for i := 0; i < p; i++ {
        if s[i] == '=' {
            continue
        }
        left += (int(s[i]) - 48) * (p - i)
    }
    for i := p + 1; i < len(s); i++ {
        if s[i] == '=' {
            continue
        }
        right += (int(s[i]) - 48) * (i - p)
    }
    if right == left {
        fmt.Println("balance")
    } else if right > left {
        fmt.Println("right")
    } else {
        fmt.Println("left")
    }

}


Acingel is a small town. There was only one doctor here — Miss Ada. She was very friendly and nobody has ever said something bad about her, so who could've expected that Ada will be found dead in her house? Mr Gawry, world-famous detective, is appointed to find the criminal. He asked $$$m$$$ neighbours of Ada about clients who have visited her in that unlucky day. Let's number the clients from $$$1$$$ to $$$n$$$. Each neighbour's testimony is a permutation of these numbers, which describes the order in which clients have been seen by the asked neighbour.However, some facts are very suspicious – how it is that, according to some of given permutations, some client has been seen in the morning, while in others he has been seen in the evening? "In the morning some of neighbours must have been sleeping!" — thinks Gawry — "and in the evening there's been too dark to see somebody's face...". Now he wants to delete some prefix and some suffix (both prefix and suffix can be empty) in each permutation, so that they'll be non-empty and equal to each other after that — some of the potential criminals may disappear, but the testimony won't stand in contradiction to each other.In how many ways he can do it? Two ways are called different if the remaining common part is different.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 100\,000$$$, $$$1 \le m \le 10$$$) — the number of suspects and the number of asked neighbors. Each of the next $$$m$$$ lines contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$). It is guaranteed that these integers form a correct permutation (that is, each number from $$$1$$$ to $$$n$$$ appears exactly once).

Output a single integer denoting the number of ways to delete some prefix and some suffix of each permutation (possibly empty), such that the remaining parts will be equal and non-empty.

NoteIn the first example, all possible common parts are $$$[1]$$$, $$$[2]$$$, $$$[3]$$$ and $$$[2, 3]$$$.In the second and third examples, you can only leave common parts of length $$$1$$$.

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

func main() {
	in.Split(bufio.ScanWords)
	n := int64(readInt())
	m := readInt()
	if m == 1 {
		fmt.Println(n * (n + 1) / 2)
		return
	}

	a := make([][]int, m)
	posOf := make([][]int, m)
	for i := range a {
		a[i] = make([]int, n)
		posOf[i] = make([]int, n)
		for j := range a[i] {
			a[i][j] = readInt() - 1
			posOf[i][a[i][j]] = j
		}
	}

	ans := int64(0)
	for i := 0; i < int(n); {
		start := make([]int, m)
		for j := range start {
			start[j] = posOf[j][a[0][i]]
		}

		goodLen := 1
		for {
			good := true
			for j := range a {
				if start[j]+goodLen >= int(n) || a[j][start[j]+goodLen] != a[0][i+goodLen] {
					good = false
					break
				}
			}
			if !good {
				ans += int64(goodLen) * int64(goodLen + 1) / 2
				i += goodLen
				break
			}
			goodLen++
		}
	}
	fmt.Println(ans)
}

package main

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

var R, C int
var data, next [][]int
var lcs []int

func matrix(rows, cols int) [][]int {
	mat := make([][]int, rows)
	for i := range mat {
		mat[i] = make([]int, cols)
	}
	return mat
}

func sequenceStarting(start int) int {
	for cur := start; cur < C; cur++ {
		expectedNext := next[0][data[0][cur]]
		ok := true
		for i := 1; i < R && ok; i++ {
			if next[i][data[0][cur]] != expectedNext {
				ok = false
			}
		}

		if !ok {
			return cur
		}

	}

	return C - 1
}

func main() {
	stdin := bufio.NewReader(os.Stdin)
	fmt.Fscan(stdin, &C, &R)
	data = matrix(R, C)
	next = matrix(R, C+1)

	for i := 0; i < R; i++ {
		for j := 0; j < C; j++ {
			fmt.Fscan(stdin, &data[i][j])
		}

		for j := 0; j < C-1; j++ {
			next[i][data[i][j]] = data[i][j+1]
		}
		next[i][data[i][C-1]] = -1
	}

	var total int64
	start := 0
	for start < C {
		end := sequenceStarting(start)
		l := int64(end - start + 1)
		total += l * (l + 1) / 2
		start = end + 1

	}

	fmt.Println(total)

}

package main

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

var R, C int
var data, next [][]int
var lcs []int

func matrix(rows, cols int) [][]int {
	mat := make([][]int, rows)
	for i := range mat {
		mat[i] = make([]int, cols)
	}
	return mat
}

func sequenceStarting(start int) int {
	for cur := start; cur < C; cur++ {
		expectedNext := next[0][data[0][cur]]
		ok := true
		for i := 1; i < R && ok; i++ {
			if next[i][data[0][cur]] != expectedNext {
				ok = false
			}
		}

		if !ok {
			return cur
		}

	}

	return C - 1
}

func main() {
	stdin := bufio.NewReader(os.Stdin)
	fmt.Fscan(stdin, &C, &R)
	data = matrix(R, C)
	next = matrix(R, C+1)

	for i := 0; i < R; i++ {
		for j := 0; j < C; j++ {
			fmt.Fscan(stdin, &data[i][j])
		}

		for j := 0; j < C-1; j++ {
			next[i][data[i][j]] = data[i][j+1]
		}
		next[i][data[i][C-1]] = -1
	}

	total := 0
	start := 0
	for start < C {
		end := sequenceStarting(start)
		l := end - start + 1
		total += l * (l + 1) / 2
		start = end + 1

	}

	fmt.Println(total)

}


A positive integer $$$x$$$ is called a power of two if it can be represented as $$$x = 2^y$$$, where $$$y$$$ is a non-negative integer. So, the powers of two are $$$1, 2, 4, 8, 16, \dots$$$.You are given two positive integers $$$n$$$ and $$$k$$$. Your task is to represent $$$n$$$ as the sum of exactly $$$k$$$ powers of two.

The only line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10^9$$$, $$$1 \le k \le 2 \cdot 10^5$$$).

If it is impossible to represent $$$n$$$ as the sum of $$$k$$$ powers of two, print NO. Otherwise, print YES, and then print $$$k$$$ positive integers $$$b_1, b_2, \dots, b_k$$$ such that each of $$$b_i$$$ is a power of two, and $$$\sum \limits_{i = 1}^{k} b_i = n$$$. If there are multiple answers, you may print any of them.

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		input := scanner.Text()

		re := regexp.MustCompile("[0-9]+")
		split := re.FindAllString(input, -1)

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

		if k > n {
			fmt.Println("NO")
			break
		}

		powerOfTwo := []int{}
		result := []int{}
		// array of power of two
		for i := 0; i <= 30; i++ {
			powerOfTwo = append(powerOfTwo, int(math.Pow(2, float64(i))))
		}

		for i := 30; i >= 0; i-- {
			if powerOfTwo[i] <= n {
				n -= powerOfTwo[i]
				result = append(result, powerOfTwo[i])
			}
		}

		stopLoop := false

		for {
			for i := 0; i < len(result); i++ {
				if len(result) >= k {
					stopLoop = true
					break
				}
				if result[i] <= 1 {
					continue
				}
				result[i] /= 2
				result = append(result, result[i])
			}

			if stopLoop {
				break
			}
		}

		if len(result) > k {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
			fmt.Println(strings.Trim(strings.Replace(fmt.Sprint(result), " ", " ", -1), "[]"))
		}

		break
	}
	if err := scanner.Err(); err != nil {
		log.Println(err)
	}
}

// Package main provides
//
// File:  c.go
// Author: ymiyamoto
//
// Created on Thu Dec 27 23:47:10 2018
//
package main

import "fmt"

func pow2(n int) int {
	if n == 0 {
		return 1
	}
	return 2 * pow2(n-1)
}

func split(p, n int) []int {
	if p == 1 {
		ret := make([]int, n)
		for i := range ret {
			ret[i] = 1
		}
		return ret
	}

	if n == 1 {
		return []int{p}
	}

	ret := make([]int, 0)
	ret = append(ret, split(p/2, n/2)...)
	ret = append(ret, split(p/2, n-n/2)...)
	return ret
}

func printSlice(slice []int) {
	for i := range slice {
		if i != 0 {
			fmt.Print(" ")
		}
		fmt.Print(slice[i])
	}
}

func main() {
	var n, k int
	fmt.Scan(&n, &k)

	arr := make([]int, 0)
	for i := 0; i < 30; i++ {
		arr = append(arr, pow2(i))
	}

	ans := make([]int, 0)
	for i := len(arr) - 1; i >= 0; i-- {
		if arr[i] <= n {
			n -= arr[i]
			ans = append(ans, arr[i])
		}

		if n == 0 {
			break
		}
	}

	total := 0
	for i := range ans {
		total += ans[i]
	}

	if n != 0 || k < len(ans) || total < k {
		fmt.Println("NO")
		return
	}

	fmt.Println("YES")
	rem := k - len(ans)
	for i := range ans {
		if i != 0 {
			fmt.Print(" ")
		}

		if rem >= ans[i]-1 {
			rem -= ans[i] - 1
			for j := 0; j < ans[i]; j++ {
				if j != 0 {
					fmt.Print(" ")
				}
				fmt.Print(1)
			}
		} else if 0 < rem {
			printSlice(split(ans[i], rem+1))
			rem = 0
		} else {
			fmt.Print(ans[i])
		}
	}
	fmt.Println()
}

package main

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

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)
	var n, k int

	fmt.Fscanf(reader, "%d %d", &n, &k)

	if k > n {
		fmt.Printf("NO")
		return
	}
	res := make([]int, 0, k)

	for i := uint(0); n != 0; i ++ {
		if (n % 2 == 1) {
			res = append(res, 1 << i)
		}
		n >>= 1
	}

	if k < len(res) {
		fmt.Printf("NO")
		return
	}

	for i := 0;len(res) != k;i ++ {
		if res[i] == 1 {
			continue
		}
		res[i] >>= 1;
		res = append(res, res[i])
		i --
	}
	fmt.Printf("YES\n")
	for i := 0;i < len(res);i ++ {
		fmt.Printf("%d ", res[i])
	}
}

package main

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

const INF int =0x3f3f3f3f
const LLINF int64 = 0x3f3f3f3f3f3f3f3f
const MAX int =200000+10

func solve(){
    var n,k,i,tot,now int
    var res [MAX] int
    n=readInt()
    k=readInt()
    now=1
    for ;now<=n;now=now*2{
    }
    tot=0
    for ;n>0&&now>0;{
        if n>=now{
            n-=now
            res[tot]=now
            tot+=1
        }
        now>>=1
    }
    if tot>k{
        fmt.Println("NO")
        return
    }
    for i=0;i<tot;i++{
        for ;res[i]>1&&tot<k;{
            res[tot]=res[i]/2
            res[i]/=2
            tot+=1
        }
    }
    if tot<k{
        fmt.Println("NO")
    }else{
        fmt.Println("YES")
        for i=0;i<tot;i++{
            fmt.Print(res[i]," ")
        }
        fmt.Println()
    }
}

/***************************FastIO begin**********************************/
var scanner *bufio.Scanner

func main() {
    scanner = bufio.NewScanner(os.Stdin)
    const MaxTokenLength = 1024 * 1024
    scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
    scanner.Split(bufio.ScanWords)
    solve()
}
func readString() string {
    scanner.Scan()
    return scanner.Text()
}

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

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

/***************************sort begin**********************************/
type IntSlice []int

func (s IntSlice) Len() int { return len(s) }
func (s IntSlice) Swap(i,j int){ s[i],s[j]=s[j],s[i] }
func (s IntSlice) Less(i,j int) bool { return s[i]<s[j] }
/***************************sort end**********************************/

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

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

package main

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

func main() {
	buf := bufio.NewReader(os.Stdin)
	var n  int64
	var k int=0
	fmt.Fscanf(buf, "%d %d\n", &n,&k)
	if int64(k) > n{
		fmt.Printf("NO\n")
		return
	}
	ans := make([]int64,0)
	var front int = 0
	var mi2 int64 = 1
	for n > 0{
		if (n&1) == 1{
			ans = append(ans,mi2)

		}
		n /= 2
		mi2 *= 2
	}
	//fmt.Print(ans)
	front = len(ans)
	if k < front {
		fmt.Printf("NO\n")
		return
	}
	fmt.Printf("YES\n")
	var hw int = 0
	for front < k{
		for ans[hw] <= 1{
			hw++
		}
		ans=append(ans,ans[hw]/2)
		ans[hw] /=2
		front++
	}
	for i:= 0;i < front;i++{
		if i > 0{
			fmt.Printf(" ")
		}
		fmt.Printf("%d",ans[i])
	}
	fmt.Printf("\n")
}

package main

import "fmt"

func main() {
	var n, k int
	fmt.Scanf("%d%d", &n, &k)
	aa := make([]int, k)
	for i := 0; i < k; i++ { aa[i] = 1}
	n -= k
	for i := 0; i < k; i++ {
		for n >= aa[i] {
			n -= aa[i]
			aa[i] *= 2
		}
	}
	if n != 0 {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
		for _, v := range aa {
			fmt.Printf("%d ", v)
		}
	}
}

package main

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

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

	defer out.Flush()

	var n, k int

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

	temp := n
	cnt := 0
	var a []int
	for temp > 0 {
		a = append(a, temp&1)
		cnt += temp&1
		temp = temp >> 1
	}
	for i := 0 ; i < len(a) / 2 ; i ++ {
		a[i], a[len(a) - i - 1] = a[len(a) - i - 1], a[i]
	}
	if k < cnt || k > n {
		fmt.Fprintf(out, "NO\n")
	}else{
		k -= cnt

		for i := 0 ; i + 1 < len(a) ; i ++ {
			//fmt.Println("a =     ",a)
			if k >= a[i]{
				a[i+1] += 2*a[i]
				k -= a[i]
				a[i] = 0
				if k == 0 {
					break
				}
			} else{
				a[i] -= k
				a[i+1] += 2*k
				k = 0
				if k == 0{
					break
				}
			}
		}
		//fmt.Println("a = ", a, " cnt = ", cnt)

		var b []int
		poii := 1
		for i := len(a) - 1 ; i >= 0 ; i -- {
			for j := 0 ; j < a[i] ; j ++ {
				b = append(b , poii)
			}
			poii = poii << 1
		}

		//fmt.Println("b = ", b)

		fmt.Fprintf(out, "YES\n")

		for i := 0 ; i < len(b) ; i ++ {
			fmt.Fprintf(out, "%d ", b[i])
		}
		fmt.Fprintf(out, "\n")
	}

}

package main

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

func main() {
	start := time.Now()
	defer func() {
		run := time.Since(start)
		_ = run
		// fmt.Println("Run in", run)
	}()

	var stdin *bufio.Reader
	// LOCAL
	f, _ := os.Open("input.txt")
	stdin = bufio.NewReader(f)

	// ONLINE JUDGE
	stdin = bufio.NewReader(os.Stdin)

	var n, k int
	fmt.Fscan(stdin, &n, &k)

	var a = []int{}
	for i, j := 1, 1; j < 32; i, j = i*2, j+1 {
		a = append(a, i)
	}

	var digit = 0
	var res = make([]int, len(a))
	for i := len(a) - 1; i >= 0; i-- {
		for n >= a[i] {
			res[i] = n / a[i]
			digit += res[i]
			n -= res[i] * a[i]
		}
	}

	// fmt.Println(res, digit)
	for i := len(a) - 1; i > 0; i-- {
		for res[i] > 0 && digit < k {
			res[i]--
			res[i-1] += 2
			digit++
		}
	}

	if digit == k {
		fmt.Println("YES")
		for i := 0; i < len(a); i++ {
			for j := 0; j < res[i]; j++ {
				fmt.Print(a[i], " ")
			}
		}
		fmt.Println()
	} else {
		fmt.Println("NO")
	}

}

package main

import (
	"fmt"
)

func solve(n, k int) (map[int]int, bool) {
	var POW []int

	if k > n {
		return nil, false
	}

	p, maxp := 1, 0
	for p <= n {
		if maxp < p {
			maxp = p
		}
		POW = append(POW, p)
		p *= 2
	}

	res := map[int]int{}

	i := len(POW) - 1
	for n > 0 {
		if n >= POW[i] {
			res[POW[i]]++
			n -= POW[i]
		}
		i--
	}

	count := len(res)

	for maxp > 1 && count < k {
		if res[maxp] <= 0 {
			maxp /= 2
			continue
		}

		count++

		half := maxp >> 1

		res[half] += 2
		res[maxp]--

	}

	return res, count == k
}

func main() {
	var n, k int

	fmt.Scan(&n, &k)

	a, isPossible := (solve(n, k))
	if !isPossible {
		fmt.Println("NO")
		return
	}

	fmt.Println("YES")
	for k, n := range a {
		for i := 0; i < n; i++ {
			fmt.Print(k, " ")
		}
	}
	fmt.Println()
}

package main

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

func main() {
	var n, k uint
	fmt.Scan(&n, &k)

	bitMap := map[uint]uint{}
	for i := 0; i < 32; i++ {
		target := uint(1) << uint(i)
		if (n & target) > 0 {
			bitMap[target]++
		}
	}

	min, max := uint(len(bitMap)), n
	if (k < min) || (k > max) {
		fmt.Println("NO")
		os.Exit(0)
	}

	split := k - min
	for i := 0; i < int(split); i++ {
		for k, v := range bitMap {
			if (k == 1) || (v == 0) {
				continue
			}

			bitMap[k], bitMap[k/2] = v-1, bitMap[k/2]+2
			break
		}
	}

	res := []string{}
	for k, v := range bitMap {
		for i := 0; i < int(v); i++ {
			res = append(res, strconv.Itoa(int(k)))
		}
	}
	fmt.Println("YES")
	fmt.Println(strings.Join(res, " "))
}

package main

import (
	"fmt"
)

func solve(n, k int) (bool, []int) {
	var POW []int

	if k > n {
		return false, nil
	}

	p := (1)
	for p <= n {
		POW = append(POW, p)
		p *= 2
	}

	var res []int

	i := len(POW) - 1
	for n > 0 {
		if n >= POW[i] {
			res = append(res, POW[i])
			n -= POW[i]
		}
		i--
	}

	if k < len(res) {
		return false, nil
	}

	for len(res) < k {
		//	fmt.Println(res)

		if res[0] == 1 {
			return false, nil
		}

		if res[0] == 2 {
			res := res[1:]
			res = append(res, 1)
			res = append(res, 1)
		}

		res[0] /= 2
		res = append([]int{res[0]}, res...)
	}

	return true, res
}

func main() {
	var n, k int

	fmt.Scan(&n, &k)

	isPossible, a := (solve(n, k))
	if !isPossible {
		fmt.Println("NO")
		return
	}

	fmt.Println("YES")
	for _, n := range a {
		fmt.Print(n, " ")
	}
	fmt.Println()
}


An electrical grid in Berland palaces consists of 2 grids: main and reserve. Wires in palaces are made of expensive material, so selling some of them would be a good idea!Each grid (main and reserve) has a head node (its number is $$$1$$$). Every other node gets electricity from the head node. Each node can be reached from the head node by a unique path. Also, both grids have exactly $$$n$$$ nodes, which do not spread electricity further.In other words, every grid is a rooted directed tree on $$$n$$$ leaves with a root in the node, which number is $$$1$$$. Each tree has independent enumeration and nodes from one grid are not connected with nodes of another grid.Also, the palace has $$$n$$$ electrical devices. Each device is connected with one node of the main grid and with one node of the reserve grid. Devices connect only with nodes, from which electricity is not spread further (these nodes are the tree's leaves). Each grid's leaf is connected with exactly one device.    In this example the main grid contains $$$6$$$ nodes (the top tree) and the reserve grid contains $$$4$$$ nodes (the lower tree). There are $$$3$$$ devices with numbers colored in blue. It is guaranteed that the whole grid (two grids and $$$n$$$ devices) can be shown in this way (like in the picture above):  main grid is a top tree, whose wires are directed 'from the top to the down',  reserve grid is a lower tree, whose wires are directed 'from the down to the top',  devices — horizontal row between two grids, which are numbered from $$$1$$$ to $$$n$$$ from the left to the right,  wires between nodes do not intersect. Formally, for each tree exists a depth-first search from the node with number $$$1$$$, that visits leaves in order of connection to devices $$$1, 2, \dots, n$$$ (firstly, the node, that is connected to the device $$$1$$$, then the node, that is connected to the device $$$2$$$, etc.).Businessman wants to sell (remove) maximal amount of wires so that each device will be powered from at least one grid (main or reserve). In other words, for each device should exist at least one path to the head node (in the main grid or the reserve grid), which contains only nodes from one grid.

The first line contains an integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of devices in the palace. The next line contains an integer $$$a$$$ ($$$1 + n \le a \le 1000 + n$$$) — the amount of nodes in the main grid. Next line contains $$$a - 1$$$ integers $$$p_i$$$ ($$$1 \le p_i \le a$$$). Each integer $$$p_i$$$ means that the main grid contains a wire from $$$p_i$$$-th node to $$$(i + 1)$$$-th. The next line contains $$$n$$$ integers $$$x_i$$$ ($$$1 \le x_i \le a$$$) — the number of a node in the main grid that is connected to the $$$i$$$-th device. The next line contains an integer $$$b$$$ ($$$1 + n \le b \le 1000 + n$$$) — the amount of nodes in the reserve grid. Next line contains $$$b - 1$$$ integers $$$q_i$$$ ($$$1 \le q_i \le b$$$). Each integer $$$q_i$$$ means that the reserve grid contains a wire from $$$q_i$$$-th node to $$$(i + 1)$$$-th. The next line contains $$$n$$$ integers $$$y_i$$$ ($$$1 \le y_i \le b$$$) — the number of a node in the reserve grid that is connected to the $$$i$$$-th device. It is guaranteed that each grid is a tree, which has exactly $$$n$$$ leaves and each leaf is connected with one device. Also, it is guaranteed, that for each tree exists a depth-first search from the node $$$1$$$, that visits leaves in order of connection to devices.

Print a single integer — the maximal amount of wires that can be cut so that each device is powered.

NoteFor the first example, the picture below shows one of the possible solutions (wires that can be removed are marked in red):  The second and the third examples can be seen below:

package main

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

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

	var n int

	fmt.Fscanln(in, &n)

	var a int

	fmt.Fscanln(in, &a)

	p := make([]int, a-1)
	for i := 0; i < a-2; i++ {
		fmt.Fscanf(in, "%d", &p[i])
	}
	fmt.Fscanln(in, &p[a-2])

	x := make([]int, n)
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(in, "%d", &x[i])
	}
	fmt.Fscanln(in, &x[n-1])

	var b int

	fmt.Fscanln(in, &b)

	q := make([]int, b-1)
	for i := 0; i < b-2; i++ {
		fmt.Fscanf(in, "%d", &q[i])
	}
	fmt.Fscanln(in, &q[b-2])

	y := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &y[i])
	}

	t := map[int][]int{}
	tt := map[int]int{}
	child := map[int]map[int]struct{}{}
	for i, pi := range p {
		t[pi] = append(t[pi], i+2)
		tt[i+2] = pi

		if _, ok := child[pi]; !ok {
			child[pi] = map[int]struct{}{}
		}

		child[pi][i+2] = struct{}{}
	}

	d := map[int][]int{}
	dd := map[int]int{}
	childD := map[int]map[int]struct{}{}
	for i, qi := range q {
		d[qi] = append(t[qi], i+2)
		dd[i+2] = qi

		if _, ok := childD[qi]; !ok {
			childD[qi] = map[int]struct{}{}
		}

		childD[qi][i+2] = struct{}{}
	}

	w := make([][]int, len(x))
	ch := map[int]map[int]struct{}{}
	for i := 0; i < len(x); i++ {
		w[i] = make([]int, len(x))

		for k, v := range child {
			ch[k] = map[int]struct{}{}

			for k1, v1 := range v {
				ch[k][k1] = v1
			}
		}

		for j := i; j < len(x); j++ {
			c := 0
			ind := x[j]

			for {
				if _, ok := tt[ind]; !ok {
					break
				}

				c++

				delete(ch[tt[ind]], ind)

				if len(ch[tt[ind]]) > 0 {
					break
				}

				ind = tt[ind]
			}

			w[i][j] = c

			if j > 0 {
				w[i][j] += w[i][j-1]
			}
		}
	}

	w1 := make([][]int, len(y))
	ch1 := map[int]map[int]struct{}{}
	for i := 0; i < len(y); i++ {
		w1[i] = make([]int, len(y))

		for k, v := range childD {
			ch1[k] = map[int]struct{}{}

			for k1, v1 := range v {
				ch1[k][k1] = v1
			}
		}

		for j := i; j < len(y); j++ {
			c := 0
			ind := y[j]

			for {
				if _, ok := dd[ind]; !ok {
					break
				}

				c++

				delete(ch1[dd[ind]], ind)

				if len(ch1[dd[ind]]) > 0 {
					break
				}

				ind = dd[ind]
			}

			w1[i][j] = c

			if j > 0 {
				w1[i][j] += w1[i][j-1]
			}
		}
	}

	for i := 0; i < len(x); i++ {
		for j := 0; j < len(x); j++ {
			if w1[i][j] > w[i][j] {
				w[i][j] = w1[i][j]
			}
		}
	}

	for i := 1; i < len(x); i++ {
		max := 0
		for j := 0; j < i; j++ {
			if w[j][i-1] > max {
				max = w[j][i-1]
			}
		}
		for j := i; j < len(x); j++ {
			w[i][j] += max
		}
	}

	max := 0
	for i := 0; i < len(x); i++ {
		if w[i][len(x)-1] > max {
			max = w[i][len(x)-1]
		}
	}

	fmt.Fprintln(out, max)
	out.Flush()
}

package main

import (
	"fmt"
	"os"
)

func main() {
	var n int

	fmt.Fscanln(os.Stdin, &n)

	var a int

	fmt.Fscanln(os.Stdin, &a)

	p := make([]int, a-1)
	for i := 0; i < a-2; i++ {
		fmt.Fscanf(os.Stdin, "%d", &p[i])
	}
	fmt.Fscanln(os.Stdin, &p[a-2])

	x := make([]int, n)
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(os.Stdin, "%d", &x[i])
	}
	fmt.Fscanln(os.Stdin, &x[n-1])

	var b int

	fmt.Fscanln(os.Stdin, &b)

	q := make([]int, b-1)
	for i := 0; i < b-2; i++ {
		fmt.Fscanf(os.Stdin, "%d", &q[i])
	}
	fmt.Fscanln(os.Stdin, &q[b-2])

	y := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(os.Stdin, "%d", &y[i])
	}

	t := map[int][]int{}
	tt := map[int]int{}
	for i, pi := range p {
		t[pi] = append(t[pi], i+2)
		tt[i+2] = pi
	}

	d := map[int][]int{}
	dd := map[int]int{}
	for i, qi := range q {
		d[qi] = append(t[qi], i+2)
		dd[i+2] = qi
	}

	et := map[int]struct{}{}
	ed := map[int]struct{}{}
	el := int(0)
	var m map[int]int
	var mm map[int]struct{}

	tx := make([]int, n)
	dy := make([]int, n)

	rx := make([]int, n)

	copy(tx[:], x[:])
	copy(dy[:], y[:])
	for i := 0; i < n; {
		for j := 0; j < n; j++ {
			rxx := 0

			if tx[j] > 1 {
				tx[j] = tt[tx[j]]

				if tx[j] == 1 && rx[j] == 0 {
					rxx = 1
					i++
				}
			}

			if dy[j] > 1 {
				dy[j] = dd[dy[j]]

				if dy[j] == 1 && rx[j] == 0 {
					rxx += 2
					i++
				}
			}

			if rxx > 0 {
				rx[j] = rxx
			}
		}
	}

	max := 0

	for u := 0; u < 2; u++ {
		for j := 0; j < n; j++ {
			if rx[j] == 1 || (rx[j] == 3 && u == 0) {
				m = tt
				mm = et
				el = x[j]
			} else {
				m = dd
				mm = ed
				el = y[j]
			}

			for {
				if el == 1 {
					break
				}

				mm[el] = struct{}{}
				el = m[el]
			}
		}

		max1 := len(tt) + len(dd) - len(et) - len(ed)

		if max1 > max {
			max = max1
		}
	}

	fmt.Fprintln(os.Stdout, max)
}

package main

import (
	"fmt"
	"os"
)

func main() {
	var n int

	fmt.Fscanln(os.Stdin, &n)

	var a int

	fmt.Fscanln(os.Stdin, &a)

	p := make([]int, a-1)
	for i := 0; i < a-2; i++ {
		fmt.Fscanf(os.Stdin, "%d", &p[i])
	}
	fmt.Fscanln(os.Stdin, &p[a-2])

	x := make([]int, n)
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(os.Stdin, "%d", &x[i])
	}
	fmt.Fscanln(os.Stdin, &x[n-1])

	var b int

	fmt.Fscanln(os.Stdin, &b)

	q := make([]int, b-1)
	for i := 0; i < b-2; i++ {
		fmt.Fscanf(os.Stdin, "%d", &q[i])
	}
	fmt.Fscanln(os.Stdin, &q[b-2])

	y := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(os.Stdin, "%d", &y[i])
	}

	t := map[int][]int{}
	tt := map[int]int{}
	for i, pi := range p {
		t[pi] = append(t[pi], i+2)
		tt[i+2] = pi
	}

	d := map[int][]int{}
	dd := map[int]int{}
	for i, qi := range q {
		d[qi] = append(t[qi], i+2)
		dd[i+2] = qi
	}

	et := map[int]struct{}{}
	ed := map[int]struct{}{}
	el := int(0)
	var m map[int]int
	var mm map[int]struct{}

	tx := make([]int, n)
	dy := make([]int, n)

	rx := make([]int, n)

	copy(tx[:], x[:])
	copy(dy[:], y[:])
	for i := 0; i < n; {
		for j := 0; j < n; j++ {
			if tx[j] > 1 {
				tx[j] = tt[tx[j]]

				if tx[j] == 1 && rx[j] == 0 {
					rx[j] = 1
					i++
				}
			}

			if dy[j] > 1 {
				dy[j] = dd[dy[j]]

				if dy[j] == 1 && rx[j] == 0 {
					rx[j] = 2
					i++
				}
			}
		}
	}

	for j := 0; j < n; j++ {
		if rx[j] == 1 {
			m = tt
			mm = et
			el = x[j]
		} else {
			m = dd
			mm = ed
			el = y[j]
		}

		for {
			if el == 1 {
				break
			}

			mm[el] = struct{}{}
			el = m[el]
		}
	}

	fmt.Fprintln(os.Stdout, len(tt)+len(dd)-len(et)-len(ed))
}

package main

import (
	"fmt"
	"os"
)

func main() {
	var n int

	fmt.Fscanln(os.Stdin, &n)

	var a int

	fmt.Fscanln(os.Stdin, &a)

	p := make([]int, a-1)
	for i := 0; i < a-2; i++ {
		fmt.Fscanf(os.Stdin, "%d", &p[i])
	}
	fmt.Fscanln(os.Stdin, &p[a-2])

	x := make([]int, n)
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(os.Stdin, "%d", &x[i])
	}
	fmt.Fscanln(os.Stdin, &x[n-1])

	var b int

	fmt.Fscanln(os.Stdin, &b)

	q := make([]int, b-1)
	for i := 0; i < b-2; i++ {
		fmt.Fscanf(os.Stdin, "%d", &q[i])
	}
	fmt.Fscanln(os.Stdin, &q[b-2])

	y := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(os.Stdin, "%d", &y[i])
	}

	t := map[int][]int{}
	tt := map[int]int{}
	for i, pi := range p {
		t[pi] = append(t[pi], i+2)
		tt[i+2] = pi
	}

	d := map[int][]int{}
	dd := map[int]int{}
	for i, qi := range q {
		d[qi] = append(t[qi], i+2)
		dd[i+2] = qi
	}

	max := 0

	w := make([]int, n)
	et := map[int]struct{}{}
	ed := map[int]struct{}{}
	el := int(0)
	var m map[int]int
	var mm map[int]struct{}

	top := 1 << uint(n)
	for i := 0; i < top; i++ {
		for j := 0; j < n; j++ {
			if w[j] == 0 {
				m = tt
				mm = et
				el = x[j]
			} else {
				m = dd
				mm = ed
				el = y[j]
			}

			for {
				if el == 1 {
					break
				}

				mm[el] = struct{}{}
				el = m[el]
			}
		}

		e := len(tt) + len(dd) - len(et) - len(ed)

		if e > max {
			max = e
		}

		for s := range et {
			delete(et, s)
		}
		for s := range ed {
			delete(ed, s)
		}

		w[0]++

		for j := 0; j < n-1; j++ {
			if w[j] == 2 {
				w[j] = 0
				w[j+1]++
			} else {
				break
			}
		}
	}

	fmt.Fprintln(os.Stdout, max)
}

package main

import (
	"fmt"
	"os"
)

func main() {
	var n int

	fmt.Fscanln(os.Stdin, &n)

	var a int

	fmt.Fscanln(os.Stdin, &a)

	p := make([]int, a-1)
	for i := 0; i < a-2; i++ {
		fmt.Fscanf(os.Stdin, "%d", &p[i])
	}
	fmt.Fscanln(os.Stdin, &p[a-2])

	x := make([]int, n)
	for i := 0; i < n-1; i++ {
		fmt.Fscanf(os.Stdin, "%d", &x[i])
	}
	fmt.Fscanln(os.Stdin, &x[n-1])

	var b int

	fmt.Fscanln(os.Stdin, &b)

	q := make([]int, b-1)
	for i := 0; i < b-2; i++ {
		fmt.Fscanf(os.Stdin, "%d", &q[i])
	}
	fmt.Fscanln(os.Stdin, &q[b-2])

	y := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(os.Stdin, "%d", &y[i])
	}

	t := map[int][]int{}
	tt := map[int]int{}
	for i, pi := range p {
		t[pi] = append(t[pi], i+2)
		tt[i+2] = pi
	}

	d := map[int][]int{}
	dd := map[int]int{}
	for i, qi := range q {
		d[qi] = append(t[qi], i+2)
		dd[i+2] = qi
	}

	et := map[int]struct{}{}
	ed := map[int]struct{}{}
	el := int(0)
	var m map[int]int
	var mm map[int]struct{}

	tx := make([]int, n)
	dy := make([]int, n)

	rx := make([]int, n)

	copy(tx[:], x[:])
	copy(dy[:], y[:])
	for i := 0; i < n; {
		for j := 0; j < n; j++ {
			rxx := 0

			if tx[j] > 1 {
				tx[j] = tt[tx[j]]

				if tx[j] == 1 && rx[j] == 0 {
					rxx = 1
					i++
				}
			}

			if dy[j] > 1 {
				dy[j] = dd[dy[j]]

				if dy[j] == 1 && rx[j] == 0 {
					rxx += 2
					i++
				}
			}

			if rxx > 0 {
				rx[j] = rxx
			}
		}
	}

	max := 0

	for u := 0; u < 2; u++ {
		for j := 0; j < n; j++ {
			if rx[j] == 1 || (rx[j] == 3 && u == 0) {
				m = tt
				mm = et
				el = x[j]
			} else {
				m = dd
				mm = ed
				el = y[j]
			}

			for {
				if el == 1 {
					break
				}

				mm[el] = struct{}{}
				el = m[el]
			}
		}

		max1 := len(tt) + len(dd) - len(et) - len(ed)

		if max1 > max {
			max = max1
		}
	}

	if len(tt) > max {
		max = len(tt)
	}

	if len(dd) > max {
		max = len(dd)
	}

	fmt.Fprintln(os.Stdout, max)
}


You have $$$n$$$ rectangular wooden blocks, which are numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th block is $$$1$$$ unit high and $$$\lceil \frac{i}{2} \rceil$$$ units long.Here, $$$\lceil \frac{x}{2} \rceil$$$ denotes the result of division of $$$x$$$ by $$$2$$$, rounded up. For example, $$$\lceil \frac{4}{2} \rceil = 2$$$ and $$$\lceil \frac{5}{2} \rceil = \lceil 2.5 \rceil = 3$$$.For example, if $$$n=5$$$, then the blocks have the following sizes: $$$1 \times 1$$$, $$$1 \times 1$$$, $$$1 \times 2$$$, $$$1 \times 2$$$, $$$1 \times 3$$$.  The available blocks for $$$n=5$$$ Find the maximum possible side length of a square you can create using these blocks, without rotating any of them. Note that you don't have to use all of the blocks.  One of the ways to create $$$3 \times 3$$$ square using blocks $$$1$$$ through $$$5$$$

Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the number of blocks.

For each test case, print one integer — the maximum possible side length of a square you can create.

NoteIn the first test case, you can create a $$$1 \times 1$$$ square using only one of the blocks.In the second test case, one of the possible ways to create a $$$3 \times 3$$$ square is shown in the statement. It is impossible to create a $$$4 \times 4$$$ or larger square, so the answer is $$$3$$$.

package main

import "fmt"

func main() {
  var t int
  fmt.Scan(&t)
  for x := 0; x < t; x++ {
    var n int64
    fmt.Scan(&n)
    if n % 2 ==0{
        fmt.Println(n/2)
    } else {
        fmt.Println(n/2+1)
    }
  }
}

package main

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

func main() {
	var n, m int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &m)
		fmt.Println(int(math.Ceil(float64(m) / 2)))
	}
}

package main

import (
	"fmt"
)

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

	for ; t > 0; t-- {
		var n int
		fmt.Scan(&n)

		fmt.Println(n/2 + n%2)
	}
}

package main

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

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

	defer writer.Flush()

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

	for ; t > 0; t-- {
		var n int
		fmt.Fscan(reader, &n)

		fmt.Fprintf(writer, "%d\n", n/2+n%2)
	}
}

package main

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

func Solve(reader *bufio.Reader, writer *bufio.Writer) {
	var n int
	fmt.Fscan(reader, &n)
	fmt.Fprintf(writer, "%v\n", (n+1)/2)
}

func main() {
	var reader *bufio.Reader
	var writer *bufio.Writer

	filename_in := "data.in"
	filename_out := "data.out"

	stdinout := true
	readtests := true
	tests := 1

	if (!stdinout || inArgs("local")) && !inArgs("std") {
		infile, inerr := os.Open(filename_in)
		if inerr != nil {
			if os.IsNotExist(inerr) {
				os.Create(filename_in)
			}
			fmt.Println(inerr)
			return
		}
		defer infile.Close()

		outfile, outerr := os.OpenFile(filename_out, os.O_WRONLY, os.ModeAppend)
		if outerr != nil {
			if os.IsNotExist(outerr) {
				outfile, outerr = os.Create(filename_out)
				if outerr != nil {
					fmt.Println(outerr)
					return
				}
			}
		}
		defer outfile.Close()

		reader = bufio.NewReader(infile)
		writer = bufio.NewWriter(outfile)
	} else if stdinout || inArgs("std") {
		reader = bufio.NewReader(os.Stdin)
		writer = bufio.NewWriter(os.Stdout)
	}
	defer func() {
		err := writer.Flush()
		if err != nil {
			fmt.Println(err)
		}
	}()

	if readtests || inArgs("local") {
		fmt.Fscanf(reader, "%d\n", &tests)
	}

	for i := 0; i < tests; i++ {
		Solve(reader, writer)
		fmt.Fscanf(reader, "\n")
	}

}

func inArgs(arg string) bool {
	for _, a := range os.Args {
		if a == arg {
			return true
		}
	}
	return false
}

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 int
		fmt.Fscan(in,&n)
		if n & 1 == 1 {
			fmt.Fprintln(out,(n-1)/2 + 1)
		} else {
			fmt.Fprintln(out,(n-2)/2 + 1)
		}
	}
}

package main

import "fmt"

func main() {
  var T int
  fmt.Scan(&T)
  for t := 0; t < T; t += 1 {
    var N int64
    fmt.Scan(&N)
    if N % 2 == 0 {
      N -= 1
    }
    fmt.Println(N / 2 + 1);
  }
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1748A
Date: 11/14/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1748/A
Problem: CF1748A
**/
func (in *CF1748A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		fmt.Println(int(math.Ceil(float64(n) / 2.)))
	}
}

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

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

package main

/*

5 = 1/2 2/2 3/2 4/2 5/2 == 1 1 2 2 3
15 = 1/2 2/2 3/2 4/2 5/2 6/2 7/2 8/2 9/2 10/2 = 1 1 2 2 3 3 4 4 5 5

5
5
4 1
1 4
3 2
2 3

4
3 1
1 3
2 2

*/

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

func main() {
	ultiSquare()
}

func ultiSquare() {
	r := bufio.NewReader(os.Stdin)
	var tc int
	for fmt.Fscan(r, &tc); tc > 0; tc-- {
		var c int
		fmt.Fscan(r, &c)
		fmt.Println(int(math.Ceil(float64(c) / float64(2))))
	}
}

package main

import (
	"fmt"
	"math"
)

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

	var arr []float64
	for i := 0; i < t; i++ {
		var n float64
		fmt.Scan(&n)
		arr = append(arr, n)
	}

	for i := 0; i < len(arr); i++ {
		l := math.Round(arr[i] / 2)
		fmt.Println(int(l))
	}
}

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 int64
		fmt.Fscan(reader, &n)

		ans := n / 2
		if n%2 > 0 {
			ans++
		}

		fmt.Fprintln(writer, ans)
	}
}

package main

import "fmt"

func main() {
	var t, n int
	fmt.Scan(&t)
	for t > 0 {
		t--
		fmt.Scan(&n)
		r := n / 2
		r += n % 2
		fmt.Println(r)
	}
}

package main

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

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

func main() {
	writer.Flush()

	var test int
	fmt.Fscanf(reader, "%d\n", &test)

	ceilInt := func(a, b int) int {
		if a % b == 0 {
			return a / b
		}

		return a / b + 1
	}

	for test > 0 {
		var n int
		fmt.Fscanf(reader, "%d\n", &n)
		fmt.Println(ceilInt(n, 2))
		test--
	}
}

package main

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

func solve(r *bufio.Reader) {
	var n int64
	fmt.Fscan(r, &n)
	total := n / 2
	total = total * (total + 1)
	if n%2 == 1 {
		total += (n + 1) / 2
	}
	fmt.Println(int(math.Sqrt(float64(total))))
}

func main() {
	r := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(r, &n)
	for i := 0; i < n; i++ {
		solve(r)
	}
}

package main

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

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

func ans() {
	var n int
	Fscan(in, &n)
	Fprintln(out, (n+1)/2)
}

func main() {
	defer out.Flush()
	var t int
	Fscan(in, &t)
	for i := 0; i < t; i++ {
		ans()
	}
}

package main

import (
	"fmt"
)

func main() {
	var (
		t int
		n int
	)
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		fmt.Scanf("%d\n", &n)
		fmt.Println(n/2 + n%2)
	}
}

package main

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

func solve(reader *bufio.Reader, writer *bufio.Writer) {
	n := readInt(reader)
	fmt.Fprintln(writer, (n+1)/2)
}

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		solve(reader, writer)
		t--
	}

	writer.Flush()
}

func min[T int | int64](a, b T) T {
	if a < b {
		return a
	}

	return b
}

func max[T int | int64](a, b T) T {
	if a > b {
		return a
	}

	return b
}

func abs[T int | int64](a T) T {
	if a < 0 {
		return a * -1
	} else {
		return a
	}
}

func gcd[T int | int64](x, y T) T {
	for y > 0 {
		x, y = y, x%y
	}

	return x
}

func lcm[T int | int64](x, y T) T {
	return x * y / gcd(x, y)
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

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

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 32)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	fmt.Fprintln(out, n/2+n%2)
}

func main() {
	defer out.Flush()
	t := 1
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		solve()
	}
}

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

func GCD(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}

	return a
}

type SqrtInt interface {
	SignedSqrt | UnsignedSqrt
}

type SignedSqrt interface {
	~int | ~int32 | ~int64
}

type UnsignedSqrt interface {
	~uint | ~uint32 | ~uint64 | ~uintptr
}

func sqrt[T SqrtInt](x T) T {
	l, r := T(0), T(2000000123)
	for l < r {
		mid := (l + r) / 2
		if mid*mid > x {
			r = mid
		} else {
			l = mid + 1
		}
	}
	return l - 1
}

func ConvertInt(val string, base, toBase int) (string, error) {
	i, err := strconv.ParseInt(val, base, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(i, toBase), nil
}

type set[T Ordered] struct {
	itms map[T]struct{}
}

func (s *set[T]) init() {
	if s.itms == nil {
		s.itms = make(map[T]struct{})
	}
}

func (s *set[T]) add(item T) *set[T] {
	s.init()
	s.itms[item] = exists
	return s
}

func (s *set[T]) remove(item T) *set[T] {
	s.init()
	delete(s.itms, item)
	return s
}

func (s *set[T]) has(item T) bool {
	s.init()
	_, c := s.itms[item]
	return c
}

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

func (s *set[T]) items() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	return items
}

func (s *set[T]) size() int {
	s.init()
	return len(s.itms)
}

var exists = struct{}{}

const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1

func reverseStr(s string) string {
	r := []byte(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

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

var (
	in  = getReader()
	out = getWriter()
)

func getReader() *bufio.Reader {
	if cf {
		return bufio.NewReader(os.Stdin)
	} else {
		in, _ := os.Open("input.txt")
		return bufio.NewReader(in)
	}
}

func getWriter() *bufio.Writer {
	if cf {
		return bufio.NewWriter(os.Stdout)
	} else {
		out, _ := os.Create("output.txt")
		return bufio.NewWriter(out)
	}
}

package main

import "fmt"

func main() {
  var t int
  fmt.Scan(&t)
  for t := 0;t<t; t++ {
    var N int64
    fmt.Scan(&N)
    fmt.Println(N / 2 + 1);
  }
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scan(&t)
	for t>0{
		t--
		var n int
		fmt.Scan(&n)
		fmt.Print(n/2+1)

	}
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scanf("%d", &t)

	for ; t > 0; t-- {
		var n int
		fmt.Scanf("%d", &n)

		fmt.Println(n/2 + n%2)
	}
}

package main

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

func Solve(reader *bufio.Reader, writer *bufio.Writer) {
	var n int
	fmt.Fscan(reader, &n)
	fmt.Fprintf(writer, "%v\n", (n+1)/2)
}

func main() {
	var reader *bufio.Reader
	var writer *bufio.Writer

	filename_in := "data.in"
	filename_out := "data.out"

	stdinout := true
	readtests := true
	tests := 1

	if (!stdinout || inArgs("local")) && !inArgs("std") {
		infile, inerr := os.Open(filename_in)
		if inerr != nil {
			if os.IsNotExist(inerr) {
				os.Create(filename_in)
			}
			fmt.Println(inerr)
			return
		}
		defer infile.Close()

		outfile, outerr := os.OpenFile(filename_out, os.O_WRONLY, os.ModeAppend)
		if outerr != nil {
			if os.IsNotExist(outerr) {
				outfile, outerr = os.Create(filename_out)
				if outerr != nil {
					fmt.Println(outerr)
					return
				}
			}
		}
		defer outfile.Close()

		reader = bufio.NewReader(infile)
		writer = bufio.NewWriter(outfile)
	} else if stdinout || inArgs("std") {
		reader = bufio.NewReader(os.Stdin)
		writer = bufio.NewWriter(os.Stdout)
	}
	defer func() {
		err := writer.Flush()
		if err != nil {
			fmt.Println(err)
		}
	}()

	if readtests || inArgs("local") {
		fmt.Fscanf(reader, "%d\n\n", &tests)
	}

	for i := 0; i < tests; i++ {
		Solve(reader, writer)
		fmt.Fscanf(reader, "\n")
	}

}

func inArgs(arg string) bool {
	for _, a := range os.Args {
		if a == arg {
			return true
		}
	}
	return false
}

package main

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

func Solve(reader *bufio.Reader, writer *bufio.Writer) {
	var n int
	fmt.Fscan(reader, &n)
	fmt.Fprintf(writer, "%v\n", (n+1)/2)
}

func main() {
	var reader *bufio.Reader
	var writer *bufio.Writer

	filename_in := "data.in"
	filename_out := "data.out"

	stdinout := true
	readtests := false
	tests := 1

	if (!stdinout || inArgs("local")) && !inArgs("std") {
		infile, inerr := os.Open(filename_in)
		if inerr != nil {
			if os.IsNotExist(inerr) {
				os.Create(filename_in)
			}
			fmt.Println(inerr)
			return
		}
		defer infile.Close()

		outfile, outerr := os.OpenFile(filename_out, os.O_WRONLY, os.ModeAppend)
		if outerr != nil {
			if os.IsNotExist(outerr) {
				outfile, outerr = os.Create(filename_out)
				if outerr != nil {
					fmt.Println(outerr)
					return
				}
			}
		}
		defer outfile.Close()

		reader = bufio.NewReader(infile)
		writer = bufio.NewWriter(outfile)
	} else if stdinout || inArgs("std") {
		reader = bufio.NewReader(os.Stdin)
		writer = bufio.NewWriter(os.Stdout)
	}
	defer func() {
		err := writer.Flush()
		if err != nil {
			fmt.Println(err)
		}
	}()

	if readtests || inArgs("local") {
		fmt.Fscanf(reader, "%d\n\n", &tests)
	}

	for i := 0; i < tests; i++ {
		Solve(reader, writer)
		fmt.Fscanf(reader, "\n")
	}

}

func inArgs(arg string) bool {
	for _, a := range os.Args {
		if a == arg {
			return true
		}
	}
	return false
}