You are given a tree (an undirected connected graph without cycles) and an integer $$$s$$$.Vanya wants to put weights on all edges of the tree so that all weights are non-negative real numbers and their sum is $$$s$$$. At the same time, he wants to make the diameter of the tree as small as possible.Let's define the diameter of a weighed tree as the maximum sum of the weights of the edges lying on the path between two some vertices of the tree. In other words, the diameter of a weighed tree is the length of the longest simple path in the tree, where length of a path is equal to the sum of weights over all edges in the path.Find the minimum possible diameter that Vanya can get.

The first line contains two integer numbers $$$n$$$ and $$$s$$$ ($$$2 \leq n \leq 10^5$$$, $$$1 \leq s \leq 10^9$$$) — the number of vertices in the tree and the sum of edge weights. Each of the following $$$n−1$$$ lines contains two space-separated integer numbers $$$a_i$$$ and $$$b_i$$$ ($$$1 \leq a_i, b_i \leq n$$$, $$$a_i \neq b_i$$$) — the indexes of vertices connected by an edge. The edges are undirected. It is guaranteed that the given edges form a tree.

Print the minimum diameter of the tree that Vanya can get by placing some non-negative real weights on its edges with the sum equal to $$$s$$$. Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is considered correct if $$$\frac {|a-b|} {max(1, b)} \leq 10^{-6}$$$.

NoteIn the first example it is necessary to put weights like this:  It is easy to see that the diameter of this tree is $$$2$$$. It can be proved that it is the minimum possible diameter.In the second example it is necessary to put weights like this:

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n, m := 0, 0
	degree := make(map[int]int)
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	if n == 2 {
		fmt.Fprintf(out, "%.9f\n", float64(m))
		return
	}
	for i := 0; i < n-1; i++ {
		a, b := 0, 0
		fmt.Fscanf(in, "%d %d\n", &a, &b)
		degree[a] = degree[a] + 1
		degree[b] = degree[b] + 1
	}
	leaf := 0
	for _, v := range degree {
		if v == 1 {
			leaf++
		}
	}
	ans := float64(m) / float64(leaf) * 2
	fmt.Fprintf(out, "%.9f\n", ans)
	return
}

package main

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

func scanNum(s *bufio.Scanner) int {
	s.Scan()
	num, _ := strconv.Atoi(s.Text())
	return num
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	n, s := scanNum(scanner), scanNum(scanner)
	adjList, inDegree := map[int]map[int]bool{}, map[int]int{}
	for i := 0; i < n; i++ {
		a, b := scanNum(scanner), scanNum(scanner)
		if adjList[a] == nil {
			adjList[a] = map[int]bool{}
		}
		if adjList[b] == nil {
			adjList[b] = map[int]bool{}
		}
		adjList[a][b], adjList[b][a] = true, true
		inDegree[a], inDegree[b] = inDegree[a]+1, inDegree[b]+1
	}
	root := -1
	for k, v := range inDegree {
		if v == 1 {
			root = k
			break
		}
	}

	leafCount, visited, queue := 0, map[int]bool{root: true}, []int{root}
	for len(queue) > 0 {
		task, count := queue[0], 0
		queue = queue[1:]
		for n := range adjList[task] {
			if visited[n] {
				continue
			}
			visited[n], count, queue = true, count+1, append(queue, n)
		}
		if count == 0 {
			leafCount++
		}
	}

	fmt.Printf("%.11f\n", float64(s)/float64(leafCount+1)*2.0)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1085D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	var n, s, v, w int
	Fscan(in, &n, &s)
	g := make([][]int, n+1)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}
	c := 0
	for _, vs := range g[1:] {
		if len(vs) == 1 {
			c++
		}
	}
	Fprintf(_w, "%.18f", float64(2*s)/float64(c))
}

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


A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.By the military charter the soldiers should stand in the order of non-increasing of their height. But as there's virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.

package main

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

func main(){
	var n, h int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in,&n)
	min := 101
	max := 0
	minIdx := 0
	maxIdx := 0
	for i:=0;i<n;i++{
		fmt.Fscan(in,&h)
		if h<=min{
			min = h
			minIdx = i
		}
		if h>max{
			max = h
			maxIdx = i
		}
	}
	if maxIdx>minIdx{
		fmt.Println(maxIdx + n-minIdx-2)
	}else{
		fmt.Println(maxIdx + n-minIdx-1)
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	imin, imax := 0, 0
	A := make([]int, n)
	for i := 0; i < len(A); i++ {
		fmt.Scan(&A[i])
		if A[imin] >= A[i] {
			imin = i
		}
		if A[imax] < A[i] {
			imax = i
		}
	}
	time := imax
	time += (len(A) - 1 - imin)
	if imax > imin {
		time--
	}
	if imax == imin {
		panic("impossible")
	}
	fmt.Println(time)
}

package main

import "fmt"

type Pair struct {
    idx, val int
}

func main() {
    var n int
    fmt.Scanln(&n);
    arr := make([]int, n)
    _max, _min := Pair{0, 0}, Pair{0, 100}
    for i:=0; i<n; i++{
        fmt.Scan(&arr[i])
        if _max.val < arr[i] {
            _max.val = arr[i]
            _max.idx = i
        }
        if _min.val >=  arr[i] {
            _min.val = arr[i]
            _min.idx = i
        }
    }
    if _max.idx > _min.idx {
        fmt.Println(_max.idx + n - _min.idx - 2)
    } else {
        fmt.Println(_max.idx + n - _min.idx - 1)
    }
}

package main

import(
    "fmt"
)

func main(){
    var(
        n,ma,mi,maInd,miInd   int
    )
    fmt.Scan(&n)
    arr := make([]int,n)
    for i:=0;i<n;i++{
        fmt.Scan(&arr[i])
    }
    ma,mi,maInd,miInd=arr[0],arr[0],0,0
    for i:=1;i<n;i++{
        if arr[i] > ma{
            ma = arr[i]
            maInd=i
        }
        if arr[i] < mi{
            mi = arr[i]
            miInd=i
        }
        if arr[i]==ma || arr[i]==mi{
            if arr[i]==ma && i<maInd{
                maInd=i
            }
            if arr[i]==mi && i>miInd{
                miInd=i
            }
        }
    }
    if maInd > miInd{
        miInd=(n-1)-miInd
        fmt.Println(maInd+miInd-1)
    }else if miInd>maInd{
        miInd = (n-1)-miInd
        fmt.Println(maInd+miInd)
    }
}

package main

import (
"fmt"
)

func main() {
	var n,i,maxi,mini,pmax,pmin int64
	maxi = 0
	mini = 101
	fmt.Scan(&n)
	var arr = make([]int64,n)
	for i = 0; i < n; i++ {
		fmt.Scan(&arr[i])
		if arr[i] > maxi {
			maxi = arr[i]
			pmax = i
		}
		if arr[i] <= mini {
			mini = arr[i]
			pmin = i
		}
	}
	ans := pmax + (n-pmin-1)
	if pmin < pmax {
		ans--
	}
	fmt.Printf("%d\n",ans)
}

package main

import "fmt"

func main() {
    var n,a,i_max,i_min int
    fmt.Scan(&n)
    max, min := 0, 101
    for i := 0; i < n; i++ {
        fmt.Scan(&a)
        if max < a { max, i_max = a, i }
        if min >= a { min, i_min = a, i }
    }
    if i_max > i_min { fmt.Println(i_max+(n-i_min-1)-1) } else { fmt.Println(i_max+(n-i_min-1)) }
}

package main

import (
  "fmt"
)

func main() {
  var a int
  fmt.Scan(&a)
  arr := make([]int, a)
  for i := 0; i < a; i++ {
    fmt.Scan(&arr[i])
  }
  max := arr[0]
  idxmax := 0
  min := arr[0]
  idxmin := 0
  for idx,val := range arr {
    if max < val {
      max = val
      idxmax = idx
    }
  }
  c := 0
  temp := 0
  for i := idxmax; i > 0; i-- {
    temp = arr[i-1]
    arr[i-1] = arr[i]
    arr[i] = temp
    c++
  }
  for idx,val := range arr {
    if min >= val {
      min = val
      idxmin = idx
    }
  }
  for i := idxmin; i < len(arr)-1; i++ {
    temp = arr[i+1]
    arr[i+1] = arr[i]
    arr[i] = temp
    c++
  }
  fmt.Print(c)
}

package main

import "fmt"

func main() {
	var n, max, min, maxP, minP, temp int
	fmt.Scanf("%d \n", &n)
	max = 0
	min = 101
	maxP = -1
	minP = -1
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &temp)
		if max < temp {
			max = temp
			maxP = i
		}
		if min >= temp {
			min = temp
			minP = i
		}
	}

	temp = maxP + (n - 1 - minP)
	if maxP > minP {
		temp--
	}
	fmt.Println(temp)
}

package main

import(
	"fmt"
)

func main() {
	n := 0
	fmt.Scanf("%d\n", &n)
	arr := []int{}
	tmp := 0

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &tmp)
		arr = append(arr, tmp)
	}

	min := arr[0]
	minI := 0
	max := arr[len(arr)-1]
	maxI := len(arr)-1

	for i := n-1; i >= 0; i-- {
		if arr[i] > max || (arr[i] == max && i < maxI) {
			max = arr[i]
			maxI = i
		}
	}

	for i := 0; i < n; i++ {
		if arr[i] < min || (arr[i] == min && i > minI) {
			min = arr[i]
			minI = i
		}
	}

	if maxI > minI {
		fmt.Println(n-1-minI + maxI - 1)
	} else {
		fmt.Println(n-1-minI + maxI)
	}
}

package main

import "fmt"

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

	heights := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&heights[i])
	}

	max, min := -1, 1000000
	i, j := 0, 0
	for k := 0; k < n; k++ {
		if heights[k] > max {
			max = heights[k]
			i = k
		}
		if heights[k] <= min {
			min = heights[k]
			j = k
		}
	}

	if i > j {
		fmt.Print(i + n - j - 2)
	} else {
		fmt.Print(i + n - j - 1)
	}
}

package main

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

var reader *bufio.Reader

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

var n int
var a []int

func main() {
	fmt.Fscan(reader, &n)
	a = make([]int, n)
	var min, max, minPos, maxPos int
	min = 110
	max = 0
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
		if a[i] <= min {
			min = a[i]
			minPos = i
		}
		if a[i] > max {
			max = a[i]
			maxPos = i
		}
	}
	ans := maxPos + n - 1 - minPos
	if maxPos > minPos {
		ans--
	}
	fmt.Println(ans)
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var n, tall, short, ti, si, e uint8
	short = 101
	fmt.Fscanf(in, "%d\n", &n)
	for i:=uint8(0); i < n; i++{
		fmt.Fscanf(in, "%d", &e)
		if tall < e{
			tall = e
			ti = i
		}
		if short >= e{
			short = e
			si = i
		}
	}

	if ti == si{
		fmt.Println("0")
	}else if ti < si{
		fmt.Println(ti + n - si - 1)
	}else if ti > si{
		fmt.Println(n - si + ti - 2)
	}

}

package main

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

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

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

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

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

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

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

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

func minll(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

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

func maxll(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

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

func absll(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}

func main() {
	defer Flush()

	n := readi()
	a := make([]int, n)
	for i := range a {
		a[i] = readi()
	}
	maxi := 0
	maxv := 0
	mini := 0
	minv := 110
	for i := range a {
		if maxv < a[i] {
			maxv = a[i]
			maxi = i
		}
		if a[i] <= minv {
			minv = a[i]
			mini = i
		}
	}

	ans := maxi + n - mini - 1
	if mini < maxi {
		ans--
	}
	println(ans)
}

package main

import (
	"fmt"
)

func main() {
	var N int
	fmt.Scanf("%d\n",&N)
	arr := make([]int,N)
	for i:= 0 ; i < N ; i++{
		fmt.Scanf("%d",&arr[i])
	}
	min := 0
	max := 0
	for i:= 0 ; i < N ; i++{
		if arr[i] <= arr[min]{
			min = i
		}
		if arr[i] > arr[max]{
			max = i
		}
	}
	result := max + (N-1) - min
	if(max > min){
		result -=1
	}
	fmt.Println(result)
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	min := 1000
	max := 0
	var minIdx, maxIdx int
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
		if a[i] <= min {
			min = a[i]
			minIdx = i
		}
		if a[i] > max {
			max = a[i]
			maxIdx = i
		}
	}
	if maxIdx > minIdx {
		fmt.Print(maxIdx + n - minIdx - 2)
		return
	}
	fmt.Print(maxIdx + n - minIdx - 1)
}

package main

import "fmt"

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

    heights := make([]int, n, n)
    for i := 0; i < n; i++ {
        fmt.Scan(&heights[i])
    }

    min_i := 0
    min_v := heights[0]
    max_i := 0
    max_v := heights[0]
    for i := 1; i < len(heights); i++ {
        if heights[i] <= min_v {
            min_v = heights[i]
            min_i = i
        }
        if heights[i] > max_v {
            max_v = heights[i]
            max_i = i
        }
    }

    mov := max_i + (len(heights) - 1 - min_i)
    if max_i > min_i {
        mov -= 1
    }
    fmt.Println(mov)
}

//  URL: http://codeforces.com/problemset/problem/144/A
//  PROBLEM RESTATEMENT
//      Calculate number of swaps needed to make minimum of an array the
//      last element and maximum of an array the first element. Can only swap
//      neighboring elements.
//  APPROACH
//      1) Find the rightmost min index. Find the leftmost max index.
//      2) Sum the distance from min index, max index to the ends of the array.
//         If they cross each other, subtract one from the distance.

package main

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

func findMinIndex(a []int) int {
	minIndex := 0
	minSoFar := 101
	for i := 0; i < len(a); i++ {
		if a[i] <= minSoFar {
			minSoFar = a[i]
			minIndex = i
		}
	}
	return minIndex
}

func findMaxIndex(a []int) int {
	maxIndex := 0
	maxSoFar := 0
	for i := 0; i < len(a); i++ {
		if a[i] > maxSoFar {
			maxSoFar = a[i]
			maxIndex = i
		}
	}
	return maxIndex
}

func solve(a []int) int {
	maxIndex := findMaxIndex(a)
	minIndex := findMinIndex(a)
	distance := len(a) - minIndex - 1 + maxIndex
	if minIndex < maxIndex {
		distance -= 1
	}
	return distance
}

func main() {
	reader, writer := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}
	fmt.Fprintf(writer, "%d\n", solve(a))
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	var min, max, current int
	minPos := 0
	maxPos := 0
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &current)
		if (i == 0) {
			min = current
			max = current
			continue
		}
		if (current <= min) {
			min = current
			minPos = i
		}
		if (current > max) {
			max = current
			maxPos = i
		}
	}
	swap := 0
	if (minPos < maxPos) {
		swap = 1
	}
	fmt.Println(maxPos + n - 1 - minPos - swap)
}

package main

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

func readIntArray(line string) []int {

	numStrings := strings.Split(strings.TrimSpace(string(line)), " ")

	numbers := make([]int, 0)

	for _, v := range numStrings {
		var converted int

		fmt.Sscanf(v, "%d", &converted)
		numbers = append(numbers, converted)
	}

	return numbers
}

func Readln(r *bufio.Reader) (string, error) {
	var (
		isPrefix bool  = true
		err      error = nil
		line, ln []byte
	)
	for isPrefix && err == nil {
		line, isPrefix, err = r.ReadLine()
		ln = append(ln, line...)
	}
	return string(ln), err
}

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

	var nSoldiers int
	line, _ := Readln(reader)
	fmt.Sscanf(line,"%d", &nSoldiers)

	line, _ = Readln(reader)
	heights := readIntArray(string(line))

	maxHeight, maxHeightIdx := heights[0], 0
	for i:= 1; i < nSoldiers; i++ {
		if heights[i] > maxHeight {
			maxHeight = heights[i]
			maxHeightIdx = i
		}
	}

	nSeconds := 0
	for i := maxHeightIdx; i > 0; i-- {
		heights[i], heights[i - 1] = heights[i - 1], heights[i]
		nSeconds++
	}

	minHeight, minHeightIdx := heights[len(heights) - 1], len(heights) - 1
	for i:= len(heights) - 1; i >= 0; i-- {
		if heights[i] < minHeight {
			minHeight = heights[i]
			minHeightIdx = i
		}
	}

	nSeconds += len(heights) - minHeightIdx - 1

	fmt.Println(nSeconds)
}

package main

import "fmt"

func main() {
	var a, t, max, min, minPos, maxPos int
	min = 101
	max = 0
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		fmt.Scanf("%d", &a)
		if a > max {
			maxPos = i
			max = a
		}
		if a <= min {
			minPos = i
			min = a
		}
	}
	if maxPos > minPos {
		fmt.Println(maxPos + t - 2 - minPos)
	} else {
		fmt.Println(maxPos + t - 1 - minPos)
	}
}

package main

import (
	"fmt"
)

func main() {
	var n, time, max, min uint8
	var soldiers [100]uint8
	time, max, min = 0, 0, 100
	max_index, min_index := -1, -1

	fmt.Scan(&n)

	for i := 0; uint8(i) < n; i++ {
		fmt.Scan(&soldiers[i])
		if soldiers[i] > max {
			max = soldiers[i]
			max_index = i
		}
		if soldiers[i] <= min {
			min = soldiers[i]
			min_index = i
		}
	}

	if max_index > min_index {
		time = uint8(max_index) + (n - uint8(min_index) - 1) - 1
	} else {
		time = (uint8(max_index)) + (n - uint8(min_index) - 1)
	}
	fmt.Println(time)
}

package main

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

// https://codeforces.com/problemset/problem/144/A
// A. Arrival of the General
// time limit per test
// 2 seconds
// memory limit per test
// 256 megabytes
// input
// standard input
// output
// standard output

// A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.

// By the military charter the soldiers should stand in the order of non-increasing of their height. But as there's virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.

// For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.

// Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.
// Input

// The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100) the values of the soldiers' heights in the order of soldiers' heights' increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, ..., an are not necessarily different.
// Output

// Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.

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

	var n int
	_, _ = fmt.Fscan(bufStdin, &n)

	heights := make([]int, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Fscan(bufStdin, &heights[i])
	}

	// look for the shortest person that is furthest right
	var shortest int
	shortestIdx := -1
	for i, h := range heights {
		if shortestIdx == -1 || h <= shortest {
			shortest = h
			shortestIdx = i
		}
	}
	nSwapsForShortest := len(heights) - 1 - shortestIdx

	// look for the tallest person that is furthest left
	var tallest int
	tallestIdx := -1
	for i := len(heights) - 1; i >= 0; i-- {
		h := heights[i]
		if tallestIdx == -1 || h >= tallest {
			tallest = h
			tallestIdx = i
		}
	}
	nSwapsForTallest := tallestIdx

	totalSwaps := nSwapsForShortest + nSwapsForTallest

	// when the shortest is initially to the left of the tallest, one of the swaps we do will be common
	// so it will be double counted between nSwapsForShortest and nSwapsForTallest, this us not double count it
	if tallestIdx > shortestIdx {
		totalSwaps--
	}

	fmt.Print(totalSwaps)
}

package main

// https://codeforces.com/problemset/problem/144/A

import "fmt"

func taskSolution(a []int) int {
	maxi, mini := 0, len(a)-1
	for i, ai := range a {
		if ai > a[maxi] {
			maxi = i
		}
		if ai <= a[mini] {
			mini = i
		}
	}
	if maxi < mini {
		return maxi + len(a) - mini - 1
	}
	return maxi + len(a) - mini - 2
}

func main() {
	var n int
	if _, err := fmt.Scan(&n); err != nil {
		panic(err)
	}

	a := []int{}
	for i := 0; i < n; i += 1 {
		var ai int
		if _, err := fmt.Scan(&ai); err != nil {
			panic(err)
		}
		a = append(a, ai)
	}

	fmt.Println(taskSolution(a))
}

package main

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

func main(){
	var n, h int
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in,&n)
	min := 101
	max := 0
	minIdx := 0
	maxIdx := 0
	for i:=0;i<n;i++{
		fmt.Fscan(in,&h)
		if h<=min{
			min = h
			minIdx = i
		}else if h>max{
			max = h
			maxIdx = i
		}
	}
	//fmt.Println(maxIdx,minIdx)
	if maxIdx>minIdx{
		fmt.Println(maxIdx + n-minIdx-2)
	}else{
		fmt.Println(maxIdx + n-minIdx-1)
	}
}

package main

import "fmt"

func solve(a int64) int64 {
	ret := (a + 1) / 2
	if a % 2 != 0 {
		ret = -ret
	}
	return ret
}

func main() {
	var a int64
	fmt.Scan(&a)
	fmt.Println(solve(a))
}

package main

import "fmt"

type Pair struct {
    idx, val int
}

func main() {
    var n int
    fmt.Scanln(&n);
    arr := make([]int, n)
    _max, _min := Pair{0, 2}, Pair{0, 100}
    for i:=0; i<n; i++{
        fmt.Scan(&arr[i])
        if _max.val < arr[i] {
            _max.val = arr[i]
            _max.idx = i
        }
        if _min.val >=  arr[i] {
            _min.val = arr[i]
            _min.idx = i
        }
    }
    if _max.idx > _min.idx {
        fmt.Println(_max.idx + n - _min.idx - 2)
    } else {
        fmt.Println(_max.idx + n - _min.idx - 1)
    }
}

package main

import "fmt"

type Pair struct {
    idx, val int
}

func main() {
    var n int
    fmt.Scanln(&n);
    arr := make([]int, n)
    _max, _min := Pair{0, 2}, Pair{0, 100}
    for i:=0; i<n; i++{
        fmt.Scan(&arr[i])
        if _max.val < arr[i] {
            _max.val = arr[i]
            _max.idx = i
        }
        if _min.val >=  arr[i] {
            _min.val = arr[i]
            _min.idx = i
        }
    }
    if _max.idx == 0 && _min.idx == n-1 {
        fmt.Println(0)
    } else if _max.idx > _min.idx {
        fmt.Println(_max.idx + n - _min.idx - 2)
    } else {
        fmt.Println(_max.idx + n - _min.idx - 1)
    }
}

package main

import (
  "fmt"
)

func main() {
  var a int
  fmt.Scan(&a)
  arr := make([]int, a)
  for i := 0; i < a; i++ {
    fmt.Scan(&arr[i])
  }
  max := arr[0]
  idxmax := 0
  min := arr[0]
  idxmin := 0
  for idx,val := range arr {
    if max < val {
      max = val
      idxmax = idx
    }
  }
  c := 0
  temp := 0
  for i := idxmax; i > 0; i-- {
    temp = arr[i-1]
    arr[i-1] = arr[i]
    arr[i] = temp
    c++
  }
  for idx,val := range arr {
    if min > val {
      min = val
      idxmin = idx
    }
  }
  for i := idxmin; i < len(arr)-1; i++ {
    temp = arr[i+1]
    arr[i+1] = arr[i]
    arr[i] = temp
    c++
  }
  fmt.Print(c)
}

package main

import(
	"fmt"
)

func main() {
	n := 0
	fmt.Scanf("%d\n", &n)
	arr := []int{}
	tmp := 0

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &tmp)
		arr = append(arr, tmp)
	}

	min := arr[0]
	minI := 0
	max := arr[len(arr)-1]
	maxI := len(arr)-1

	for i := n-1; i >= 0; i-- {
		if arr[i] > max || (arr[i] == max && i < maxI) {
			max = arr[i]
			maxI = i
		}
	}

	for i := 0; i < n; i++ {
		if arr[i] < min || (arr[i] == min && i > minI) {
			max = arr[i]
			maxI = i
		}
	}

	if maxI > minI {
		fmt.Println(minI +maxI - 1)
	} else {
		fmt.Println(minI + maxI)
	}
}

package main

import(
	"fmt"
)

func main() {
	n := 0
	fmt.Scanf("%d\n", &n)
	fmt.Println(n)
	arr := []int{}
	tmp := 0

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &tmp)
		arr = append(arr, tmp)
	}

	min := arr[0]
	minI := 0
	max := arr[len(arr)-1]
	maxI := len(arr)-1

	for i := n-1; i >= 0; i-- {
		if arr[i] > max || (arr[i] == max && i < maxI) {
			max = arr[i]
			maxI = i
		}
	}

	for i := 0; i < n; i++ {
		if arr[i] < min || (arr[i] == min && i > minI) {
			max = arr[i]
			maxI = i
		}
	}

	if maxI > minI {
		fmt.Println(minI +maxI - 1)
	} else {
		fmt.Println(minI + maxI)
	}
}

package main

import "fmt"

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

	heights := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&heights[i])
	}

	max, min := -1, 1000000
	i, j := 0, 0
	for k := 0; k < n; k++ {
		if heights[k] > max {
			max = heights[k]
			i = k
		} else if heights[k] <= min {
			min = heights[k]
			j = k
		}
	}

	if i > j {
		fmt.Print(i + n - j - 2)
	} else {
		fmt.Print(i + n - j - 1)
	}
}

package main

import "fmt"

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

	heights := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&heights[i])
	}

	max, min := -1, 1000000
	i, j := 0, 0
	for k := 0; k < n; k++ {
		if heights[k] > max {
			max = heights[k]
			i = k
		} else if heights[k] <= min {
			min = heights[i]
			j = k
		}
	}

	if i > j {
		fmt.Print(i + n - j - 2)
	} else {
		fmt.Print(i + n - j)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var n, tall, short, ti, si, e uint8
	short = 101
	fmt.Fscanf(in, "%d\n", &n)
	for i:=uint8(0); i < n; i++{
		fmt.Fscanf(in, "%d", &e)
		if tall < e{
			tall = e
			ti = i
		}
		if short > e{
			short = e
			si = i
		}
	}

	if ti == si{
		fmt.Println("0")
	}else if ti < si{
		fmt.Println(ti + n - si - 1)
	}else if ti > si{
		fmt.Println(n - si + ti - 3)
	}

}

package main

import (
	"fmt"
)

func main() {
	var N int
	fmt.Scanf("%d\n",&N)
	arr := make([]int,N)
	for i:= 0 ; i < N ; i++{
		fmt.Scanf("%d ",&arr[i])
	}
	min := 0
	max := 0
	for i:= 0 ; i < N ; i++{
		if arr[i] <= arr[min]{
			min = i
		}
		if arr[i] > arr[max]{
			max = i
		}
	}
	result := max + (N-1) - min
	if(max > min){
		result -=1
	}
	fmt.Println(result)
}

package main

import (
	"fmt"
)

func main() {
	var N int
	fmt.Scanf("%d\n",&N)
	arr := make([]int,N)
	for i:= 0 ; i < N ; i++{
		fmt.Scanf("%d\n",&arr[i])
	}
	min := 0
	max := 0
	for i:= 0 ; i < N ; i++{
		if arr[i] <= arr[min]{
			min = i
		}
		if arr[i] > arr[max]{
			max = i
		}
	}
	result := max + (N-1) - min
	if(max > min){
		result -=1
	}
	fmt.Println(result)
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	var min, max, current int
	minPos := 0
	maxPos := 0
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &current)
		if (i == 0) {
			min = current
			max = current
			continue
		}
		if (current <= min) {
			min = current
			minPos = i
		}
		if (current >= max) {
			max = current
			maxPos = i
		}
	}
	swap := 0
	if (minPos < maxPos) {
		swap = 1
	}
	fmt.Println(maxPos + n - 1 - minPos - swap)
}

package main

import "fmt"

func main() {
    var n,t int
    var a [55]int
    var s string
    fmt.Scan(&n,&t,&s)
    for i, v := range s { a[i] = int(v) }
    for ; t > 0; t-- {
        for i := 1; i < n; i++ {
            if (a[i] == 'G') && (a[i-1] == 'B') {
                a[i], a[i-1] = a[i-1], a[i]
                i++
            }
        }
    }
    for i := 0; i < n; i++ { fmt.Print(string(a[i])) }
    fmt.Println()
}


There are n student groups at the university. During the study day, each group can take no more than 7 classes. Seven time slots numbered from 1 to 7 are allocated for the classes.The schedule on Monday is known for each group, i. e. time slots when group will have classes are known.Your task is to determine the minimum number of rooms needed to hold classes for all groups on Monday. Note that one room can hold at most one group class in a single time slot.

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of groups.  Each of the following n lines contains a sequence consisting of 7 zeroes and ones — the schedule of classes on Monday for a group. If the symbol in a position equals to 1 then the group has class in the corresponding time slot. In the other case, the group has no class in the corresponding time slot.

Print minimum number of rooms needed to hold all groups classes on Monday.

NoteIn the first example one room is enough. It will be occupied in each of the seven time slot by the first group or by the second group.In the second example three rooms is enough, because in the seventh time slot all three groups have classes.

package main

import (
	"fmt"
)

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

	var groups []string
	for i := 0; i < n; i++ {
		var s string
		fmt.Scanf("%s\n",&s)
		groups = append(groups, s)
	}

	result := 0
	for i := 0; i < 7; i++ {
		c := 0
		for j := 0; j < n; j++ {
			if groups[j][i] == '1' {
				c++
			}
		}

		if c > result {
			result = c
		}
	}

	fmt.Println(result)
}


You are given a huge decimal number consisting of $$$n$$$ digits. It is guaranteed that this number has no leading zeros. Each digit of this number is either 0 or 1.You may perform several (possibly zero) operations with this number. During each operation you are allowed to change any digit of your number; you may change 0 to 1 or 1 to 0. It is possible that after some operation you can obtain a number with leading zeroes, but it does not matter for this problem.You are also given two integers $$$0 \le y < x < n$$$. Your task is to calculate the minimum number of operations you should perform to obtain the number that has remainder $$$10^y$$$ modulo $$$10^x$$$. In other words, the obtained number should have remainder $$$10^y$$$ when divided by $$$10^x$$$.

The first line of the input contains three integers $$$n, x, y$$$ ($$$0 \le y < x < n \le 2 \cdot 10^5$$$) — the length of the number and the integers $$$x$$$ and $$$y$$$, respectively. The second line of the input contains one decimal number consisting of $$$n$$$ digits, each digit of this number is either 0 or 1. It is guaranteed that the first digit of the number is 1.

Print one integer — the minimum number of operations you should perform to obtain the number having remainder $$$10^y$$$ modulo $$$10^x$$$. In other words, the obtained number should have remainder $$$10^y$$$ when divided by $$$10^x$$$.

NoteIn the first example the number will be $$$11010100100$$$ after performing one operation. It has remainder $$$100$$$ modulo $$$100000$$$.In the second example the number will be $$$11010100010$$$ after performing three operations. It has remainder $$$10$$$ modulo $$$100000$$$.

package main

import (
	"fmt"
)

func main() {
	var n, x, y, ans int
	ans = 0;
	var s string
	fmt.Scan(&n, &x, &y, &s)
	for i:=n-1; i>=n-x; i-- {
		if i==n-1-y {
			if s[i]==byte('0'){
				ans++
			} else {}
		} else{
			if s[i]==byte('1'){
				ans++;
			} else {}
		}
	}
	fmt.Println(ans);
}

package main

import "fmt"

func main () {
	var n, x, y, cnt int
	cnt = 0
	var str string
	fmt.Scan(&n, &x, &y, &str)
	for i := n-1; i > n-x-1; i-- {
		if string(str[i]) != "0" && i != n-y-1 {
			cnt++
		} else if string(str[i]) != "1" && i == n-y-1 {
			cnt++
		}
	}
	fmt.Println(cnt)
}


Your favorite music streaming platform has formed a perfectly balanced playlist exclusively for you. The playlist consists of $$$n$$$ tracks numbered from $$$1$$$ to $$$n$$$. The playlist is automatic and cyclic: whenever track $$$i$$$ finishes playing, track $$$i+1$$$ starts playing automatically; after track $$$n$$$ goes track $$$1$$$.For each track $$$i$$$, you have estimated its coolness $$$a_i$$$. The higher $$$a_i$$$ is, the cooler track $$$i$$$ is.Every morning, you choose a track. The playlist then starts playing from this track in its usual cyclic fashion. At any moment, you remember the maximum coolness $$$x$$$ of already played tracks. Once you hear that a track with coolness strictly less than $$$\frac{x}{2}$$$ (no rounding) starts playing, you turn off the music immediately to keep yourself in a good mood.For each track $$$i$$$, find out how many tracks you will listen to before turning off the music if you start your morning with track $$$i$$$, or determine that you will never turn the music off. Note that if you listen to the same track several times, every time must be counted.

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$), denoting the number of tracks in the playlist. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), denoting coolnesses of the tracks.

Output $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$, where $$$c_i$$$ is either the number of tracks you will listen to if you start listening from track $$$i$$$ or $$$-1$$$ if you will be listening to music indefinitely.

NoteIn the first example, here is what will happen if you start with...   track $$$1$$$: listen to track $$$1$$$, stop as $$$a_2 < \frac{a_1}{2}$$$.  track $$$2$$$: listen to track $$$2$$$, stop as $$$a_3 < \frac{a_2}{2}$$$.  track $$$3$$$: listen to track $$$3$$$, listen to track $$$4$$$, listen to track $$$1$$$, stop as $$$a_2 < \frac{\max(a_3, a_4, a_1)}{2}$$$.  track $$$4$$$: listen to track $$$4$$$, listen to track $$$1$$$, stop as $$$a_2 < \frac{\max(a_4, a_1)}{2}$$$. In the second example, if you start with track $$$4$$$, you will listen to track $$$4$$$, listen to track $$$1$$$, listen to track $$$2$$$, listen to track $$$3$$$, listen to track $$$4$$$ again, listen to track $$$1$$$ again, and stop as $$$a_2 < \frac{max(a_4, a_1, a_2, a_3, a_4, a_1)}{2}$$$. Note that both track $$$1$$$ and track $$$4$$$ are counted twice towards the result.

package main

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

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

	var n int
	Fscan(in, &n)
	a := make([]int, n, 3*n)
	for i := range a {
		Fscan(in, &a[i])
	}
	a = append(append(a, a...), a...)

	q := make([]int, 3*n)
	l, r, j := 0, 0, 0
	for i := range a[:n] {
		for ; j < 3*n && (l == r || 2*a[j] >= a[q[l]]); j++ {
			for ; l < r && a[q[r-1]] <= a[j]; r-- {
			}
			q[r] = j
			r++
		}
		ans := j - i
		if ans > 2*n {
			ans = -1
		}
		Fprint(out, ans, " ")
		if l < r && q[l] == i {
			l++
		}
	}
}

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

package main

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

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

	var n int
	Fscan(in, &n)
	a := make([]int, 3*n)
	for i := range a[:n] {
		Fscan(in, &a[i])
	}
	copy(a[n:2*n], a[:n])
	copy(a[2*n:], a[:n])

	type pair struct{ a, i int }
	q := []pair{{a[0], 0}}
	ql, j := 0, 1
	for i := range a[:n] {
		for ; j < 3*n && (ql == len(q) || 2*a[j] >= q[ql].a); j++ {
			for ql < len(q) && q[len(q)-1].a < a[j] {
				q = q[:len(q)-1]
			}
			q = append(q, pair{a[j], j})
		}
		ans := j - i
		if ans > 2*n {
			ans = -1
		}
		Fprint(out, ans, " ")
		if ql < len(q) && q[ql].i == i {
			ql++
		}
	}
}

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

package main

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

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

	var n int
	Fscan(in, &n)
	a := make([]int, 3*n+1)
	for i := range a[:n] {
		Fscan(in, &a[i])
	}
	copy(a[n:2*n], a[:n])
	copy(a[2*n:], a[:n])

	type pair struct{ a, i int }
	q := []pair{{a[0], 0}}
	ql, r := 0, 1
	for i := range a[:n] {
		for ; r < 3*n && (ql == len(q) || 2*a[r] >= q[ql].a); r++ {
			for ql < len(q) && q[len(q)-1].a < a[r] {
				q = q[:len(q)-1]
			}
			q = append(q, pair{a[r], r})
		}
		ans := r - i
		if ans > 2*n {
			ans = -1
		}
		Fprint(out, ans, " ")
		if ql < len(q) && q[ql].i == i {
			ql++
		}
	}
}

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

package main

import (
	"bufio"
	"container/heap"

	. "fmt"
	"os"
	"strconv"
)

var (
	inn = bufio.NewScanner(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func cmax(a *int, b int) {
	if b > *a {
		*a = b
	}
}

type node struct {
	i, x int
}
type pq []node

func (h pq) Len() int {
	return len(h)
}
func (h pq) Less(i, j int) bool {
	if h[i].x == h[j].x {
		return h[i].i > h[j].i
	}
	return h[i].x > h[j].x
}
func (h pq) Swap(i, j int) {
	h[i], h[j] = h[j], h[i]
}
func (h *pq) Push(x interface{}) {
	*h = append(*h, x.(node))
}
func (h *pq) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}
func main() {
	defer out.Flush()
	inn.Buffer([]byte{}, 10000000)
	inn.Split(bufio.ScanWords)
	//prep
	n := rd()
	a := make([]node, 3*n)
	ans := make([]int, 400005)
	for i := 0; i < n; i++ {
		x := rd()
		a[i] = node{i, x}
		a[i+n] = node{i + n, x}
		a[i+2*n] = node{i + 2*n, x}
	}
	mn := 0

	q := &pq{}
	heap.Init(q)

	for i := 0; i < 3*n; i++ {
		heap.Push(q, a[i])
		for 2*a[i].x < (*q)[0].x {
			now := heap.Pop(q)
			for mn <= now.(node).i {
				ans[mn] = i - mn
				mn++
			}
		}
	}
	for i := 0; i < n; i++ {
		if ans[i] > 0 {
			Fprintf(out, "%d ", ans[i])
		} else {
			Fprint(out, "-1 ")
		}
	}

}

//-------------------template------------------
type pair struct {
	first, second int
}

func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func maxll(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func minll(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func rd() (x int) {
	inn.Scan()
	x, _ = strconv.Atoi(string(inn.Bytes()))
	return x
}
func rdll() (x int64) {
	inn.Scan()
	x, _ = strconv.ParseInt(string(inn.Bytes()), 10, 64)
	return x
}
func rdch() (x byte) {
	inn.Scan()
	return inn.Bytes()[0]
}
func rds() string {
	inn.Scan()
	return string(inn.Bytes())
}


One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k. There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th (1 ≤ i ≤ k) child wrote the number ai (1 ≤ ai ≤ n·k). All numbers ai accidentally turned out to be different.Now the children wonder, how to divide the orange so as to meet these conditions:  each child gets exactly n orange segments;  the i-th child gets the segment with number ai for sure;  no segment goes to two children simultaneously. Help the children, divide the orange and fulfill the requirements, described above.

The first line contains two integers n, k (1 ≤ n, k ≤ 30). The second line contains k space-separated integers a1, a2, ..., ak (1 ≤ ai ≤ n·k), where ai is the number of the orange segment that the i-th child would like to get. It is guaranteed that all numbers ai are distinct.

Print exactly n·k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second n integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces. You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.

package main
import (
	"fmt"
	//"sort"
	//"unicode/utf8"
)
func main() {
	var n,k int
	fmt.Scan(&n,&k)
	a:=make([]int,31)
	tot:=make([]bool,905)
	t:=1
	for i:=0;i<k;i++ {
		fmt.Scan(&a[i])
		tot[a[i]]=true
	}
	for i:=0;i<k;i++ {
		fmt.Print(a[i]," ")
		for j:=0;j<n-1;t++ {
			if tot[t]==false {
				fmt.Print(t, " ")
				j++
			}
		}
		fmt.Println()
	}
}

package main

import "fmt"

func main() {
    var n,k int
    var a [35]int
    f := make(map[int]bool)
    fmt.Scan(&n,&k)
    for i := 0; i < k; i++ {
        fmt.Scan(&a[i])
        f[a[i]] = true
    }
    in := 1
    for i := 0; i < k; i++ {
        fmt.Print(a[i])
        for j := 1; j < n; j++ {
            for f[in] { in++ }
            fmt.Print(" ",in)
            in++
        }
        fmt.Println()
    }
}

// 244A-mic
package main

import (
    "fmt"
)

func main() {
    var n, k int
    var b [901]int
    fmt.Scan(&n, &k)
    a := make([]int, 1000)
    for i := 1; i <= k; i++ {
        fmt.Scan(&a[i])
        b[a[i]] = 1
    }
    for i := 1; i <= k; i++ {
        fmt.Print(a[i])
        for j := 1; j <= n-1; j++ {
            for l := 1; l <= n*k; l++ {
                if b[l] == 0 {
                    fmt.Print(" ")
                    fmt.Print(l)
                    b[l] = 1
                    break
                }
            }
        }
        fmt.Println()
    }
}


Emuskald needs a fence around his farm, but he is too lazy to build it himself. So he purchased a fence-building robot.He wants the fence to be a regular polygon. The robot builds the fence along a single path, but it can only make fence corners at a single angle a.Will the robot be able to build the fence Emuskald wants? In other words, is there a regular polygon which angles are equal to a?

The first line of input contains an integer t (0 < t < 180) — the number of tests. Each of the following t lines contains a single integer a (0 < a < 180) — the angle the robot can make corners at measured in degrees.

For each test, output on a single line "YES" (without quotes), if the robot can build a fence Emuskald wants, and "NO" (without quotes), if it is impossible.

NoteIn the first test case, it is impossible to build the fence, since there is no regular polygon with angle .In the second test case, the fence is a regular triangle, and in the last test case — a square.

package main

import (
	"bufio"
	"os"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	scanner.Scan()
	t := toInt(scanner.Bytes())
	for t > 0 {
		scanner.Scan()
		n := toInt(scanner.Bytes())
		if canBePolygon(n) {
			writer.WriteString("YES\n")
		}else {
			writer.WriteString("NO\n")
		}
		t--
	}
}

func canBePolygon(n int) bool {
	if 360 % (180 - n) == 0 {
		return true
	}
	return false
}

func toInt(buf []byte) int {
	var n int
	for _, v := range buf {
		n = n * 10 + int(v - '0')
	}
	return n
}

package main

import (
	"fmt"
)

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

	for n > 0 {
		fmt.Scanf("%d\n", &t)
		if check(t) == true {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
		n--
	}
}

func check(t int) bool {
	if 360%(180-t) == 0 {
		return true
	}
	return false
}

package main

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

func main() {
    defer s.Flush()

	for n := s.Next(); n > 0; n-- {
		if  360 % (180 - s.Next()) == 0 {
			s.Println("YES")
	    } else {
			s.Println("NO")
		}
	}
}

var (
	s *InOut
)

func init() {
	s = NewInOut(os.Stdin, os.Stdout)
}

type InOut struct {
	*bufio.Reader
	*bufio.Writer
}

func NewInOut(r io.Reader, w io.Writer) *InOut {
	return &InOut{bufio.NewReader(r), bufio.NewWriter(w)}
}

// Get Next Integer
func (s *InOut) Next() (r int) {
	b, _ := s.ReadByte()
	p := 1
	for (b < '0' || '9' < b) && b != '-' {
		b, _ = s.ReadByte()
	}
	if b == '-' {
		p = -1
		b, _ = s.ReadByte()
	}
	for '0' <= b && b <= '9' {
		r = 10*r + int(b-'0')
		b, _ = s.ReadByte()
	}
	return r * p
}

func (s *InOut) NextInt64() (r int64) {
	b, _ := s.ReadByte()
	p := int64(1)
	for (b < '0' || '9' < b) && b != '-' {
		b, _ = s.ReadByte()
	}
	if b == '-' {
		p = -1
		b, _ = s.ReadByte()
	}
	for '0' <= b && b <= '9' {
		r = 10*r + int64(b-'0')
		b, _ = s.ReadByte()
	}
	return r * p
}

// Get Next Line String
func (s *InOut) NextLine() (r string) {
    b, _ := s.ReadByte()
    for b == '\r' || b == '\n' {
        b, _ = s.ReadByte()
    }
    buf := make([]byte, 0)
    for ; b != '\r' && b != '\n'; b, _ = s.ReadByte() {
        buf = append(buf, b)
    }
    return string(buf)
}

// Get Next String using delimiter whitespace
func (s *InOut) NextStr() (r string) {
    return string(s.NextBytes())
}

// Get Next String using delimiter whitespace
func (s *InOut) NextBytes() (r []byte) {
	b, _ := s.ReadByte()
	for b == '\n' || b == ' ' {
		b, _ = s.ReadByte()
	}
	buf := make([]byte, 0)
	for ; b != '\n' && b != ' '; b, _ = s.ReadByte() {
		buf = append(buf, b)
	}
	return buf
}

// Print Strings using the suitable way to change type
func (s *InOut) Print(os ...interface{}) {
	for _, o := range os {
		switch o.(type) {
		case byte:
			s.WriteByte(o.(byte))
		case string:
			s.WriteString(o.(string))
		case int:
			s.WriteString(strconv.Itoa(o.(int)))
		case int64:
			s.WriteString(strconv.FormatInt(o.(int64), 10))
		default:
			s.WriteString(fmt.Sprint(o))
		}
	}
}

// Print Strings using the suitable way to change type with new line
func (s *InOut) Println(os ...interface{}) {
	for _, o := range os {
		s.Print(o)
	}
	s.Print("\n")
}

// Print immediately with new line (for debug)
func (s *InOut) PrintlnNow(o interface{}) {
	fmt.Println(o)
}

package main
import (
	"fmt"
)
func main() {
	var d, a int
	fmt.Scan(&d)
	for i:=0;i<d;i++ {
		fmt.Scan(&a)
		if (360%(180-a))==0 {
			fmt.Println("YES")
		} else { fmt.Println("NO")}
	}
}

package main

import (
	"fmt"
)

func test(x int) bool {
	return x > 0 && x < 180 && 360%(180-x) == 0 && 360/(180-x) >= 3
}

func main() {
	var n, temp int
	fmt.Scanf("%d \n", &n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d \n", &temp)
		if test(temp) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

func main() {
    var t,a int
    fmt.Scan(&t)
    for ; t > 0; t-- {
        fmt.Scan(&a)
        if (360 % (180-a)) == 0 {
            fmt.Println("YES")
        } else {
            fmt.Println("NO")
        }
    }
}

package main

import "fmt"

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

	v := 0
	for i := 0; i < t; i++ {
		fmt.Scan(&v)
		if 360%(180-v) != 0 {
			fmt.Println("NO")
		} else {
			n := 360 / (180 - v)
			if n < 3 {
				fmt.Println("NO")
			} else {
				fmt.Println("YES")
			}
		}
	}
}

package main

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

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

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

var t, a int

func main() {
	fmt.Fscan(reader, &t)
	for ; t != 0; t-- {
		fmt.Fscan(reader, &a)
		n := 180 * 2 / (180 - a)
		if n*(180-a) == 180*2 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
    "fmt"
    )

func can_build(angle int) bool {
    for i := 3; angle * i >= 180 * (i-2); i++ {
        if angle * i == 180 * (i-2) {
            return true
        }
    }

    return false
}

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

    for i := 0; i < t; i++ {
        var angle int
        fmt.Scan(&angle)
        if can_build(angle) {
            fmt.Println("YES")
        } else {
            fmt.Println("NO")
        }
    }
}

// 270A-mic
package main

import (
	"fmt"
)

func main() {
	var n, k int
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&k)
		if 360%(180-k) == 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main
import (
    "fmt"
)
func main() {
    var d, a int
    fmt.Scan(&d)
    for i:=0;i<d;i++ {//1 + 2n
        fmt.Scan(&a)
        if (360%(180-a))==0 {
            fmt.Println("YES")
        } else { fmt.Println("NO")}
    }
}

package main

import "fmt"

func main() {
	var a, angle int
	fmt.Scan(&a)
	for i := 0; i < a; i++ {
		fmt.Scan(&angle)
		if (360 % (180 - angle)) == 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

func main() {
	var count int
	var angle int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanln(reader, &count)
	for i := 0; i < count; i++ {
		fmt.Fscanln(reader, &angle)
		if 360%(180-angle) == 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

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

func solution(reader io.Reader, writer io.Writer) {
	var t int
	fmt.Fscanf(reader, "%d\n", &t)
	for i := 0; i < t; i++ {
		var a int
		fmt.Fscanf(reader, "%d\n", &a)
		if 360 % (180 - a) == 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}

package main

import "fmt"

var mp map[int]bool

func init() {
	mp = make(map[int]bool)
	for i := 3; i <= 180; i++ {
		if 180%i == 0 {
			mp[180/i*(i-2)] = true
		}
	}
}

func main() {
	var t, x int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&x)
		/*
			if mp[x] {
				fmt.Println("YES")
			} else {
				fmt.Println("NO")
			}
		*/
		if x >= 60 && 360%(180-x) == 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

func main() {
	t := 0
	fmt.Scanf("%d\n", &t)
	for t > 0 {
		n := 0
		fmt.Scanf("%d\n", &n)
		if n < 60 {
			fmt.Printf("NO\n")
		} else {
			if status(n) == false {
				fmt.Printf("NO\n")
			} else {
				fmt.Printf("YES\n")
			}
		}
		t = t - 1
	}
}

func status(x int) bool {
	var n float32
	n = 180 - float32(x)
	n = float32(360) / n
	var t int = int(n)
	var t2 = float32(t)
	if t2 == n {
		return true
	}
	return false
}

package main

import (
	"fmt"
	"math"
)

func main() {
	t := 0
	fmt.Scanf("%d\n", &t)
	for t > 0 {
		n := 0
		fmt.Scanf("%d\n", &n)
		if n < 60 {
			fmt.Printf("NO\n")
		} else {
			if status(n) == false {
				fmt.Printf("NO\n")
			} else {
				fmt.Printf("YES\n")
			}
		}
		t = t - 1
	}
}

func status(x int) bool {
	n := 180 - x
	n = int(math.Abs(float64(n)))
	if 360%n == 0 {
		return true
	}
	return false
}

package main

import "fmt"
import "math"

func okAngle(a float64) bool {
	n := 360 / (180 - a)
	if n < 3.0 {
		return false
	}
	return n == math.Round(n)
}

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var a int
		fmt.Scan(&a)
		if okAngle(float64(a)) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

func main() {
	var count int
	var angle int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanln(reader, &count)
	for i := 0; i < count; i++ {
		fmt.Fscanln(reader, &angle)
		if angle >= 60 && 360%angle == 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import "fmt"

var mp map[int]bool

func init() {
	mp = make(map[int]bool)
	for i := 3; i <= 180; i++ {
		if 180%i == 0 {
			mp[180/i*(i-2)] = true
		}
	}
}

func main() {
	var t, x int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&x)
		if mp[x] {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}