There is a grid with $$$n$$$ rows and $$$m$$$ columns, and three types of cells:   An empty cell, denoted with '.'.  A stone, denoted with '*'.  An obstacle, denoted with the lowercase Latin letter 'o'. All stones fall down until they meet the floor (the bottom row), an obstacle, or other stone which is already immovable. (In other words, all the stones just fall down as long as they can fall.)Simulate the process. What does the resulting grid look like?

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 50$$$) — the number of rows and the number of columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.', '*', or 'o' — an empty cell, a stone, or an obstacle, respectively.

For each test case, output a grid with $$$n$$$ rows and $$$m$$$ columns, showing the result of the process. You don't need to output a new line after each test, it is in the samples just for clarity.

package main

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

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

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

	for ; t > 0; t-- {
		var h, w int
		fmt.Fscan(in, &h, &w)

		input := make([]string, h)
		for i := 0; i < h; i++ {
			fmt.Fscan(in, &input[i])
		}

		ans := make([][]uint8, h)
		for i := 0; i < h; i++ {
			ans[i] = make([]uint8, w)
			for j := range ans[i] {
				ans[i][j] = '.'
			}
		}

		for x := 0; x < w; x++ {
			prevY, stonesCnt := 0, 0
			for y := 0; y < h; y++ {
				if input[y][x] == '.' {
					continue
				}

				if input[y][x] == '*' {
					stonesCnt++
					continue
				}

				for i := prevY; i < y-stonesCnt; i++ {
					ans[i][x] = '.'
				}
				for i := prevY + (y - prevY - stonesCnt); i < y; i++ {
					ans[i][x] = '*'
				}
				ans[y][x] = 'o'
				prevY, stonesCnt = y+1, 0
			}

			for i := prevY; i < h-stonesCnt; i++ {
				ans[i][x] = '.'
			}
			for i := h - stonesCnt; i < h; i++ {
				ans[i][x] = '*'
			}
		}

		for _, row := range ans {
			fmt.Fprintln(out, string(row))
		}
	}

}

package main

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

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

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

	for ; t > 0; t-- {
		var h, w int
		fmt.Fscan(in, &h, &w)

		input := make([]string, h)
		for i := 0; i < h; i++ {
			fmt.Fscan(in, &input[i])
		}

		ans := make([][]uint8, h)
		for i := 0; i < h; i++ {
			ans[i] = make([]uint8, w)
			for j := range ans[i] {
				ans[i][j] = '.'
			}
		}

		for x := 0; x < w; x++ {
			prevY, stonesCnt := 0, 0
			for y := 0; y < h; y++ {
				if input[y][x] == '.' {
					continue
				}

				if input[y][x] == '*' {
					stonesCnt++
					continue
				}

				for i := prevY; i < y-stonesCnt; i++ {
					ans[i][x] = '.'
				}
				for i := prevY + (y - prevY - stonesCnt); i < y; i++ {
					ans[i][x] = '*'
				}
				ans[y][x] = 'o'
				prevY, stonesCnt = y+1, 0
			}

			for i := prevY; i < h-stonesCnt; i++ {
				ans[i][x] = '.'
			}
			for i := h - stonesCnt; i < h; i++ {
				ans[i][x] = '*'
			}

			/*if ans[h-1][x] == 0 {
				ans[h-1][x] = '.'
			}*/
			prevY, stonesCnt = h-1, 0
		}
		for _, row := range ans {
			fmt.Fprintln(out, string(row))
		}
	}

}

package main

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

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

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

	for ; t > 0; t-- {
		var h, w int
		fmt.Fscan(in, &h, &w)

		input := make([]string, h)
		for i := 0; i < h; i++ {
			fmt.Fscan(in, &input[i])
		}

		ans := make([][]uint8, h)
		for i := 0; i < h; i++ {
			ans[i] = make([]uint8, w)
		}

		for x := 0; x < w; x++ {
			prevY, stonesCnt := 0, 0
			for y := 0; y < h; y++ {
				if input[y][x] == '.' {
					continue
				}

				if input[y][x] == '*' {
					stonesCnt++
					continue
				}

				for i := prevY; i < y-stonesCnt; i++ {
					ans[i][x] = '.'
				}
				for i := prevY + (y - prevY - stonesCnt); i < y; i++ {
					ans[i][x] = '*'
				}
				ans[y][x] = 'o'
				prevY, stonesCnt = y+1, 0
			}

			for i := prevY; i < h-stonesCnt; i++ {
				ans[i][x] = '.'
			}
			for i := h - stonesCnt; i < h; i++ {
				ans[i][x] = '*'
			}

			if ans[h-1][x] == 0 {
				ans[h-1][x] = '.'
			}
			prevY, stonesCnt = h-1, 0
		}
		for _, row := range ans {
			fmt.Fprintln(out, string(row))
		}
	}

}

package main

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

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

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

	for ; t > 0; t-- {
		var h, w int
		fmt.Fscan(in, &h, &w)

		input := make([]string, h)
		for i := 0; i < h; i++ {
			fmt.Fscan(in, &input[i])
		}

		ans := make([][]uint8, h)
		for i := 0; i < h; i++ {
			ans[i] = make([]uint8, w)
		}

		for x := 0; x < w; x++ {
			prevY, stonesCnt := 0, 0
			for y := 0; y < h; y++ {
				if input[y][x] == '.' {
					continue
				}

				if input[y][x] == '*' {
					stonesCnt++
					continue
				}

				for i := prevY; i < y-stonesCnt; i++ {
					ans[i][x] = '.'
				}
				for i := prevY + (y - prevY - stonesCnt); i < y; i++ {
					ans[i][x] = '*'
				}
				ans[y][x] = 'o'
				prevY, stonesCnt = y+1, 0
			}

			for i := prevY; i < h-stonesCnt; i++ {
				ans[i][x] = '.'
			}
			for i := h - stonesCnt; i < h; i++ {
				ans[i][x] = '*'
			}

			if stonesCnt == 0 && ans[h-1][x] != 'o' {
				ans[h-1][x] = '.'
			}
			prevY, stonesCnt = h-1, 0
		}
		for _, row := range ans {
			fmt.Fprintln(out, string(row))
		}
	}

}

package main

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

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

func Scan(a ...interface{}) { fmt.Fscan(in, a...) }
func Scanln(s *string) {
	*s = ""
	for len(*s) == 0 {
		*s, _ = in.ReadString('\n')
		*s = strings.TrimSpace(*s)
	}
}
func Print(a ...interface{})                 { fmt.Fprint(out, a...) }
func Printf(format string, a ...interface{}) { fmt.Fprintf(out, format, a...) }
func Log(a ...interface{}) {
	pc, _, line, _ := runtime.Caller(1)
	fmt.Fprint(out, "log ", runtime.FuncForPC(pc).Name(), ":", line, " ")
	fmt.Fprint(out, a...)
	fmt.Fprint(out, "\n")
}

const N = 50 + 10

var (
	s [N]string
	t [N][N]byte
)

func solve() {
	var (
		n, m int
	)
	Scan(&n, &m)
	for i := 0; i < n; i++ {
		Scanln(&s[i])
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			t[i][j] = '.'
		}
	}

	for j := 0; j < m; j++ {
		idx := n - 1
		for i := n - 1; i >= 0; i-- {
			switch s[i][j] {
			case '*':
				t[idx][j] = '*'
				idx--
			case 'o':
				t[i][j] = 'o'
				idx = i - 1
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			Printf("%c", t[i][j])
		}
		Print("\n")
	}
}

func main() {
	defer out.Flush()

	T := 1
	Scan(&T)
	for test := 0; test < T; test++ {
		solve()
	}
}

package main

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

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

func Scan(a ...interface{}) { fmt.Fscan(in, a...) }
func Scanln(s *string) {
	*s, _ = in.ReadString('\n')
	*s = strings.TrimSpace(*s)
	for len(*s) == 0 {
		*s, _ = in.ReadString('\n')
		*s = strings.TrimSpace(*s)
	}
}
func Print(a ...interface{})                 { fmt.Fprint(out, a...) }
func Printf(format string, a ...interface{}) { fmt.Fprintf(out, format, a...) }
func Log(a ...interface{}) {
	pc, _, line, _ := runtime.Caller(1)
	fmt.Fprint(out, "log ", runtime.FuncForPC(pc).Name(), ":", line, " ")
	fmt.Fprint(out, a...)
	fmt.Fprint(out, "\n")
}

const N = 50 + 10

var (
	s [N]string
	t [N][N]byte
)

func solve() {
	var (
		n, m int
	)
	Scan(&n, &m)
	for i := 0; i < n; i++ {
		Scanln(&s[i])
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			t[i][j] = '.'
		}
	}

	for j := 0; j < m; j++ {
		idx := n - 1
		for i := n - 1; i >= 0; i-- {
			switch s[i][j] {
			case '*':
				t[idx][j] = '*'
				idx--
			case 'o':
				t[i][j] = 'o'
				idx = i - 1
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			Printf("%c", t[i][j])
		}
		Print("\n")
	}
}

func main() {
	defer out.Flush()

	T := 1
	Scan(&T)
	for test := 0; test < T; test++ {
		solve()
	}
}

package main

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

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

func Scan(a ...interface{}) { fmt.Fscan(in, a...) }
func Scanln(s *string) {
	for *s, _ = in.ReadString('\n'); *s == "\n" || *s == "\r\n"; {
		*s, _ = in.ReadString('\n')
	}
	*s = strings.TrimSpace(*s)
}
func Print(a ...interface{})                 { fmt.Fprint(out, a...) }
func Printf(format string, a ...interface{}) { fmt.Fprintf(out, format, a...) }
func Log(a ...interface{}) {
	pc, _, line, _ := runtime.Caller(1)
	fmt.Fprint(out, "log ", runtime.FuncForPC(pc).Name(), ":", line, " ")
	fmt.Fprint(out, a...)
	fmt.Fprint(out, "\n")
}

const N = 50 + 10

var (
	s [N]string
	t [N][N]byte
)

func solve() {
	var (
		n, m int
	)
	Scan(&n, &m)
	for i := 0; i < n; i++ {
		Scanln(&s[i])
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			t[i][j] = '.'
		}
	}

	for j := 0; j < m; j++ {
		idx := n - 1
		for i := n - 1; i >= 0; i-- {
			switch s[i][j] {
			case '*':
				t[idx][j] = '*'
				idx--
			case 'o':
				t[i][j] = 'o'
				idx = i - 1
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			Printf("%c", t[i][j])
		}
		Print("\n")
	}
}

func main() {
	defer out.Flush()

	T := 1
	Scan(&T)
	for test := 0; test < T; test++ {
		solve()
	}
}

package main

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

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

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}

var (
	n int
)

func solve() {
	var m int
	scan(&n, &m)
	a := make([][]rune, n)
	for i := 0; i < n; i++ {
		var s string
		scan(&s)
		a[i] = []rune(s)
	}
	for col := 0; col < m; col++ {
		fall := n - 1
		for row := n - 1; row >= 0; row-- {
			switch a[row][col] {
			case '*':
				tmp := a[fall][col]
				a[fall][col] = a[row][col]
				a[row][col] = tmp
				if fall > 0 {
					fall--
				}
			case 'o':
				fall = row - 1
			}
		}
	}
	// printf("-------------\n")
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			printf("%c", a[i][j])
		}
		printf("\n")
	}
}

func main() {
	defer out.Flush()
	n := 1
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

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

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}

var (
	n int
)

func solve() {
	var m int
	scan(&n, &m)
	a := make([][]rune, n)
	for i := 0; i < n; i++ {
		var s string
		scan(&s)
		a[i] = []rune(s)
	}
	for col := 0; col < m; col++ {
		fall := n - 1
		lasto := n
		for row := n - 1; row >= 0; row-- {
			switch a[row][col] {
			case '*':
				if fall != row {
					a[fall][col] = '*'
					a[row][col] = '.'
				}
				if fall > 0 {
					fall--
				}
				// printf("* %d %d\n", fall, lasto)
			case 'o':
				if lasto > row {
					lasto = row
				}
				fall = row - 1
				for fall >= 0 && fall < n && a[fall][col] == 'o' {
					fall--
				}
				// printf("o %d %d\n", fall, lasto)
			case '.':
				if fall < row && (fall > lasto && lasto < n || lasto == n) {
					fall = row
				}
				// printf(". %d %d\n", fall, lasto)
			}
		}
	}
	// printf("-------------\n")
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			printf("%c", a[i][j])
		}
		printf("\n")
	}
}

func main() {
	defer out.Flush()
	n := 1
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

const MOD int64 = 1000000007

var n, m int

var grid []string

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

func (a ByV) Len() int           { return len(a) }
func (a ByV) Less(i, j int) bool { return a[i].v < a[j].v }
func (a ByV) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func main() {

	read := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {

		fmt.Fscan(read, &n, &m)

		grid = make([]string, n)

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

		solve()
	}
}

type pair struct {
	v int
	idx []int
}

//a b c d

//20 12 -- 4
//4  2  -- 4

func solve()  {

	ans := make([][]byte, n)
	for i := 0; i < n; i++ {
		ans[i] = make([]byte, m)
	}

	for i := 0; i < m ; i++ {

		cnt := 0

		for j := n - 1; j >= 0; j-- {
			if grid[j][i] == '.' {
				cnt++
				ans[j][i] = '.'
			}
			if grid[j][i] == 'o' {
				cnt = 0
				ans[j][i] = 'o'
			}

			if grid[j][i] == '*' {
				ans[j][i] = '.'
				ans[j + cnt][i] = '*'
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Print(string(ans[i][j]))
		}
		fmt.Println()
	}
	fmt.Println()
}

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

package main

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

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

func main() {
	defer out.Flush()
	var t int
	fmt.Fscanf(in, "%d\n", &t)
	for ; t > 0; t-- {
		var n, m int
		fmt.Fscanf(in, "%d%d\n", &n, &m)
		var s string
		arr := make([][]uint8, n)
		for i := 0; i < n; i++ {
			arr[i] = make([]uint8, m)
		}
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%s\n", &s)
			for j := 0; j < m; j++ {
				arr[i][j] = s[j]
			}
		}

		for k := 0; k < n-1; k++ {

			for i := n - 2; i >= 0; i-- {

				for j := 0; j < m; j++ {
					if arr[i][j] == '.' {
						continue
					}
					if arr[i][j] == 'o' {
						continue
					}
					if arr[i+1][j] == '.' {
						arr[i][j] = '.'
						arr[i+1][j] = '*'
					}
				}
			}
		}
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				fmt.Fprintf(out, "%c", arr[i][j])
			}
			fmt.Fprintln(out)
		}

	}
}

// @Title
// @Description
// @Author
// @Update
package main

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

func dfs(i, j int, arr [][]rune) {
        n := len(arr)
        if i != n-1 {
                if arr[i+1][j] == '.' {
                        arr[i+1][j], arr[i][j] = '*', '.'
                        dfs(i+1, j, arr)
                }
        }
}
func fun(in *bufio.Reader) {
        var n, m int
        fmt.Fscan(in, &n, &m)
        arr := make([][]rune, n)
        for i := 0; i < n; i++ {
                arr[i] = make([]rune, m)
                s := ""
                fmt.Fscan(in, &s)
                for j := 0; j < m; j++ {
                        arr[i][j] = rune(s[j])
                }
        }
        for i := n - 1; i >= 0; i-- {
                for j := m - 1; j >= 0; j-- {
                        if arr[i][j] == '*' {
                                dfs(i, j, arr)
                        }
                }
        }
        for i := 0; i < n; i++ {
                for j := 0; j < m; j++ {
                        fmt.Printf("%c", arr[i][j])
                }
                fmt.Print("\n")
        }
}

func main() {
        t := 1
        fmt.Scan(&t)
        in := bufio.NewReader(os.Stdin)
        for i := 0; i < t; i++ {
                fun(in)
        }

}

package main

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

func solve(cin *scanner, cout *writer, cerr *writer) {
	n, m := cin.NextInt(), cin.NextInt()
	v := make([][]rune, n)
	for i := range v {
		v[i] = []rune(cin.Next())
	}
	var done bool
	for !done {
		var swapped bool
		for j := 0; j < m; j++ {
			for i := n - 1; i > 0; i-- {
				if v[i][j] == '.' && v[i-1][j] == '*' {
					v[i][j], v[i-1][j] = v[i-1][j], v[i][j]
					swapped = true
				}
			}
		}
		if !swapped {
			done = true
		}
	}
	for _, s := range v {
		cout.Println(string(s))
	}
}

func main() {
	cin := NewScanner(os.Stdin)
	cout, cerr := NewWriter(os.Stdout), NewWriter(os.Stderr)
	defer cout.Flush()
	defer cerr.Flush()
	t := cin.NextInt()
	// t := 1
	for ti := 0; ti < t; ti++ {
		solve(cin, cout, cerr)
	}
}

// Template Bolierplate {{{
type scanner struct {
	*bufio.Scanner
}

func NewScanner(r io.Reader) *scanner {
	s := bufio.NewScanner(r)
	s.Split(bufio.ScanWords)
	s.Buffer(nil, 1024*1024)
	return &scanner{s}
}

func (i *scanner) Next() string {
	i.Scan()
	return i.Text()
}

func (i *scanner) Nexts(n int) []string {
	res := make([]string, n)
	for j := range res {
		res[j] = i.Next()
	}
	return res
}

func (i *scanner) NextInt() int {
	v, _ := strconv.Atoi(i.Next())
	return v
}

func (i *scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(i.Next(), 10, 64)
	return v
}

func (i *scanner) NextInts(n int) []int {
	res := make([]int, n)
	for j := range res {
		res[j] = i.NextInt()
	}
	return res
}

func (i *scanner) NextInt64s(n int) []int64 {
	res := make([]int64, n)
	for j := range res {
		res[j] = i.NextInt64()
	}
	return res
}

type writer struct {
	w *bufio.Writer
}

func NewWriter(w io.Writer) *writer {
	return &writer{bufio.NewWriter(w)}
}

func (i *writer) Print(a ...interface{}) {
	fmt.Fprint(i.w, a...)
}

func (i *writer) Println(a ...interface{}) {
	fmt.Fprintln(i.w, a...)
}

func (i *writer) Printf(format string, a ...interface{}) {
	fmt.Fprintf(i.w, format, a...)
}

func (i *writer) Flush() {
	i.w.Flush()
}

// }}}

// vim: fdm=marker

package main

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

/*
1
5 5
*****
*....
*****
....*
*****
=======
.....
*...*
*****
*****
*****
=======
1
5 5
*****
*....
**o**
....*
*****
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n, m int
	scanf("%d %d\n", &n, &m)

	grid := make([][]rune, n)

	for i := range grid {
		var s string
		scanf("%s\n", &s)
		grid[i] = []rune(s)
	}

	for col := 0; col < m; col++ {
		for i := n - 1; i >= 0; i-- {
			if grid[i][col] == '.' {
				for j := i - 1; j >= 0; j-- {
					if grid[j][col] == '*' {
						grid[i][col] = '*'
						grid[j][col] = '.'
						break
					} else if grid[j][col] == 'o' {
						i = j
						break
					}
				}
			}
		}
	}

	for _, runes := range grid {
		fmt.Fprintln(writer, string(runes))
	}
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnhandledErrorResult
func factorize(num int) []int {
	factors := []int{1}

	sqrtA := int(math.Sqrt(float64(num)))

	for ftr := 2; ftr <= sqrtA; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > sqrtA {
		factors = append(factors, num)
	}

	return factors
}

package main

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

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

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

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

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

		for i := 0; i < n; i++ {
			var s string
			fmt.Fscan(in, &s)

			for j, ch := range s {
				if ch == '*' {
					grid[i][j] = 1
				} else if ch == 'o' {
					grid[i][j] = -1
				}
			}
		}

		f := true
		for f {
			f = false
			for i := n - 2; i >= 0; i-- {
				for j := 0; j < m; j++ {
					if grid[i][j] == 1 && grid[i+1][j] == 0 {
						grid[i][j] = 0
						grid[i+1][j] = 1
						f = true
					}
				}
			}
		}

		for _, r := range grid {
			for _, v := range r {
				if v == 1 {
					fmt.Fprint(out, "*")
				} else if v == -1 {
					fmt.Fprint(out, "o")
				} else {
					fmt.Fprint(out, ".")
				}
			}
			fmt.Fprintln(out)
		}
		fmt.Fprintln(out)
	}
}

package main

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

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

func isok(s string) bool {
	b := strings.Count(s, "B")
	rr := strings.Count(s, "R")
	if rr == 0 && b == 0 {
		return true
	} else if rr == 0 || b == 0 {
		return false
	} else {
		return true
	}
}

func slove() {
	defer w.Flush()
	var m, n int
	fmt.Fscan(r, &m, &n)
	grid := make([][]byte, m)
	for i := range grid {
		grid[i] = make([]byte, n)
		var s string
		fmt.Fscan(r, &s)
		for j := range grid[i] {
			grid[i][j] = s[j]
		}
	}
	for j:=0;j<n;j++ {
		cnt := 0
		for i:=0;i<m;i++ {
			if grid[i][j] == '*' {
				cnt++
				grid[i][j] = '.'
			} else if grid[i][j] == 'o' {
				for k:=i-cnt;k<i;k++ {
					grid[k][j] = '*'
				}
				cnt = 0
			}
		}
		for k:=m-cnt;k<m;k++ {
			grid[k][j] = '*'
		}
	}
	for i := range grid {
		fmt.Fprintln(w, string(grid[i]))
	}
}

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

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

package main

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

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)

	for ii := 0; ii < t; ii++ {
		var n, m int
		fmt.Fscan(in, &m, &n)
		nums := make([]string, m)
		for i := 0; i < m; i++ {
			fmt.Fscan(in, &nums[i])
		}
		ans := make([][]byte, m)
		for i := 0; i < m; i++ {
			ans[i] = make([]byte, n)
		}
		for i := 0; i < m; i++ {
			for j := 0; j < n; j++ {
				if nums[i][j] == 'o' {
					ans[i][j] = nums[i][j]
				}
			}
		}

		for j := 0; j < n; j++ {
			count := 0
			for i := 0; i < m; i++ {
				if nums[i][j] == '*' {
					count++
				}
				if nums[i][j] == 'o' {
					for k := 0; k < count; k++ {
						ans[i-k-1][j] = '*'
					}
					count = 0
				} else if i == m-1 {
					for k := 0; k < count; k++ {
						ans[i-k][j] = '*'
					}
					count = 0
				}
			}
		}
		for i := 0; i < m; i++ {
			for j := 0; j < n; j++ {
				if ans[i][j] == 0 {
					ans[i][j] = '.'
				}
			}
		}
		for i := 0; i < m; i++ {
			fmt.Fprintln(out, string(ans[i]))
		}
	}
}

package main

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

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

	solve := func(curCase int) {
		var n ,m int
		Fscan(in, &n,&m)
		a := make([][]byte, n)
		for i := range a {
			Fscan(in, &a[i])
		}

		for j := range a[0] {
			for i := 0;i<n;i++ {
				if a[i][j] == 'o' {
					continue
				}
				c := 0
				for ;i<n && a[i][j] != 'o';i++ {
					if a[i][j] == '*' {
						c++
					}
					a[i][j] = '.'
				}
				for k := i-1;c>0; k-- {
					a[k][j] = '*'
					c--
				}
			}
		}
		for _, s := range a {
			Fprintf(out, "%s\n", s)
		}
	}

	cases := 1
	Fscan(in, &cases)
	for curCase := 1; curCase <= cases; curCase++ {
		solve(curCase)
	}

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

}

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

package main

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

const MOD int64 = 1000000007

var n, m int

var grid []string

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

func (a ByV) Len() int           { return len(a) }
func (a ByV) Less(i, j int) bool { return a[i].v < a[j].v }
func (a ByV) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func main() {

	read := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {

		fmt.Fscan(read, &n, &m)

		grid = make([]string, n)

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

		solve()
	}
}

type pair struct {
	v int
	idx []int
}

//a b c d

//20 12 -- 4
//4  2  -- 4

func solve()  {

	ans := make([][]byte, n)
	for i := 0; i < n; i++ {
		ans[i] = make([]byte, m)
	}

	for i := 0; i < m ; i++ {

		cnt := 0

		for j := n - 1; j >= 0; j-- {
			if grid[j][i] == '.' {
				cnt++
				ans[j][i] = '.'
			}
			if grid[j][i] == 'o' {
				cnt = 0
				ans[j][i] = 'o'
			}

			if grid[j][i] == '*' {
				ans[j + cnt][i] = '*'
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Print(string(ans[i][j]))
		}
		fmt.Println()
	}
	fmt.Println()
}

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


This is the hard version of the problem. The difference between the versions is that in the easy version all prices $$$a_i$$$ are different. You can make hacks if and only if you solved both versions of the problem.Today is Sage's birthday, and she will go shopping to buy ice spheres. All $$$n$$$ ice spheres are placed in a row and they are numbered from $$$1$$$ to $$$n$$$ from left to right. Each ice sphere has a positive integer price. In this version, some prices can be equal.An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.

The first line contains a single integer $$$n$$$ $$$(1 \le n \le 10^5)$$$ — the number of ice spheres in the shop. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$ — the prices of ice spheres.

In the first line print the maximum number of ice spheres that Sage can buy. In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.

NoteIn the sample it's not possible to place the ice spheres in any order so that Sage would buy $$$4$$$ of them. If the spheres are placed in the order $$$(3, 1, 4, 2, 4, 2, 5)$$$, then Sage will buy one sphere for $$$1$$$ and two spheres for $$$2$$$ each.

package main

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

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

	n := readNum(reader)

	A := readNNums(reader, n)

	cnt := solve(n, A)

	var buf bytes.Buffer

	buf.WriteString(fmt.Sprintf("%d\n", cnt))

	for i := 0; i < n; i++ {
		buf.WriteString(fmt.Sprintf("%d ", A[i]))
	}
	buf.WriteByte('\n')
	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

func solve(n int, A []int) int {
	sort.Ints(A)

	B := make([]int, n)

	var j int
	for i := 1; i < n; i += 2 {
		B[i] = A[j]
		j++
	}

	k := n - 1
	if k&1 == 1 {
		// B[k] is already filled
		k--
	}

	for i := 0; i <= k; {
		cur := A[j]
		j++
		var ok = true
		if i > 0 && cur == B[i-1] {
			ok = false
		}
		if i+1 < n && cur == B[i+1] {
			ok = false
		}
		if ok {
			B[i] = cur
			i += 2
			continue
		}
		// i not filled
		B[k] = cur
		k -= 2
	}
	var cnt int

	for i := 1; i < n; i += 2 {
		if B[i] < B[i-1] && i+1 < n && B[i] < B[i+1] {
			cnt++
		}
	}
	copy(A, B)

	return cnt
}

package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() int64 {
		sc.Scan()
		i, _ := strconv.ParseInt(sc.Text(), 10, 64)
		return i
	}

	n := in()
	a := make([]int64, n)
	b := make([]int64, n)

	for i := range a {
		a[i] = in()
	}

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	var s, c int64 = 1, 0

	var i, j int64

	for j = s; j < n; j += 2 {
		b[j] = a[c]
		c++
	}

	s = 0

	for j = s; j < n; j += 2 {
		b[j] = a[c]
		c++
	}

	c = 0

	for i = 1; i < n-1; i++ {
		if b[i] < b[i-1] && b[i] < b[i+1] {
			c++
		}
	}

	fmt.Println(c)

	for i := range b {
		fmt.Print(b[i], " ")
	}

	fmt.Println()
}

package main

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

func processInput(in io.Reader) (int, []int) {
	buf, _ := ioutil.ReadAll(in)

	var i, n int
	for ; buf[i] >= '0' && buf[i] <= '9'; i++ {
		n = (n * 10) + int(buf[i]&15)
	}

	ints := make([]int, n)
	l := len(buf)

	for j := 0; j < n; j++ {
		for ; buf[i] < '0' || buf[i] > '9'; i++ {
		}

		for ; i < l && buf[i] >= '0' && buf[i] <= '9'; i++ {
			ints[j] = (ints[j] * 10) + int(buf[i]&15)
		}
	}

	return n, ints
}

type spheres []int

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

const _B int = 2 * 1024 * 1024

type buffer struct {
	i   int
	sz  int
	arr []byte
}

var output = &buffer{
	sz:  _B,
	arr: make([]byte, _B),
}

func (b *buffer) add(out io.Writer, x int, delimiter byte) {
	temp := make([]byte, 10)
	l := 0
	if x == 0 {
		temp[l] = '0'
		l++
	}
	for x > 0 {
		temp[l] = byte(x%10) + '0'
		x /= 10
		l++
	}
	for i := l - 1; i >= 0; i-- {
		b.arr[b.i] = temp[i]
		b.i++
	}
	b.arr[b.i] = delimiter
	b.i++
	if b.i > b.sz-20 {
		b.flush(out)
	}
}

func (b *buffer) flush(out io.Writer) {
	if b.i > 0 {
		out.Write(b.arr[:b.i])
		b.i = 0
	}
}

// Solution solves cf 1419 d2
func Solution(in io.Reader, out io.Writer, showOrderArray bool) {

	// 1 <= n <= 1e5
	// 1 <= a1..an <= 1e9
	n, x := processInput(in)

	// sort descending
	sort.Sort(spheres(x))

	l, r := 1, n-1
	ans := 0

	for l <= r {
		m := (l + r) / 2

		valid := true

		if n-m <= m {
			valid = false
		} else {
			for i, j := m-1, n-2; i >= 0; i, j = i-1, j-1 {
				if x[i] >= x[j] {
					valid = false
					break
				}
			}
		}

		if valid {
			ans = m
			l = m + 1
		} else {
			r = m - 1
		}
	}

	ord := make([]int, n)
	k := 0
	l = 0
	r = n - ans - 1

	for k < n {
		if r < n {
			ord[k] = x[r]
			k++
			r++
		}

		if l < n-ans-1 {
			ord[k] = x[l]
			k++
			l++
		}
	}

	output.add(out, ans, '\n')

	if showOrderArray {
		for i := 0; i < n-1; i++ {
			output.add(out, ord[i], ' ')
		}
		output.add(out, ord[n-1], '\n')
	}

	output.flush(out)
}

func main() {
	Solution(os.Stdin, os.Stdout, true)
}

package main

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

func processInput(in io.Reader) (int, []int) {
	buf, _ := ioutil.ReadAll(in)

	var i, n int
	for ; buf[i] >= '0' && buf[i] <= '9'; i++ {
		n = (n * 10) + int(buf[i]&15)
	}

	ints := make([]int, n)
	l := len(buf)

	for j := 0; j < n; j++ {
		for ; buf[i] < '0' || buf[i] > '9'; i++ {
		}

		for ; i < l && buf[i] >= '0' && buf[i] <= '9'; i++ {
			ints[j] = (ints[j] * 10) + int(buf[i]&15)
		}
	}

	return n, ints
}

type spheres []int

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

const _B int = 512 * 1024

type buffer struct {
	i   int
	sz  int
	arr []byte
}

var output = &buffer{
	sz:  _B,
	arr: make([]byte, _B),
}

func (b *buffer) add(out io.Writer, x int, delimiter byte) {
	temp := make([]byte, 10)
	l := 0
	if x == 0 {
		temp[l] = '0'
		l++
	}
	for x > 0 {
		temp[l] = byte(x%10) + '0'
		x /= 10
		l++
	}
	for i := l - 1; i >= 0; i-- {
		b.arr[b.i] = temp[i]
		b.i++
	}
	b.arr[b.i] = delimiter
	b.i++
	if b.i > b.sz-20 {
		b.flush(out)
	}
}

func (b *buffer) flush(out io.Writer) {
	if b.i > 0 {
		out.Write(b.arr[:b.i])
		b.i = 0
	}
}

// Solution solves cf 1419 d2
func Solution(in io.Reader, out io.Writer, showOrderArray bool) {

	// 1 <= n <= 1e5
	// 1 <= a1..an <= 1e9
	n, x := processInput(in)

	// sort descending
	sort.Sort(spheres(x))

	l, r := 1, n-1
	ans := 0

	for l <= r {
		m := (l + r) / 2

		valid := true

		if n-m <= m {
			valid = false
		} else {
			for i, j := m-1, n-2; i >= 0; i, j = i-1, j-1 {
				if x[i] >= x[j] {
					valid = false
					break
				}
			}
		}

		if valid {
			ans = m
			l = m + 1
		} else {
			r = m - 1
		}
	}

	ord := make([]int, n)
	k := 0
	l = 0
	r = n - ans - 1

	for k < n {
		if r < n {
			ord[k] = x[r]
			k++
			r++
		}

		if l < n-ans-1 {
			ord[k] = x[l]
			k++
			l++
		}
	}

	output.add(out, ans, '\n')

	if showOrderArray {
		for i := 0; i < n-1; i++ {
			output.add(out, ord[i], ' ')
		}
		output.add(out, ord[n-1], '\n')
	}

	output.flush(out)
}

func main() {
	Solution(os.Stdin, os.Stdout, true)
}

package main

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

func processInput(in io.Reader) (int, []int) {
	buf, _ := ioutil.ReadAll(in)

	var i, n int
	for ; buf[i] >= '0' && buf[i] <= '9'; i++ {
		n = (n * 10) + int(buf[i]&15)
	}

	ints := make([]int, n)
	l := len(buf)

	for j := 0; j < n; j++ {
		for ; buf[i] < '0' || buf[i] > '9'; i++ {
		}

		for ; i < l && buf[i] >= '0' && buf[i] <= '9'; i++ {
			ints[j] = (ints[j] * 10) + int(buf[i]&15)
		}
	}

	return n, ints
}

type spheres []int

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

const _B int = 1024 * 1024

type buffer struct {
	i   int
	sz  int
	arr []byte
}

var output = &buffer{
	sz:  _B,
	arr: make([]byte, _B),
}

func (b *buffer) add(out io.Writer, x int, delimiter byte) {
	temp := make([]byte, 10)
	l := 0
	if x == 0 {
		temp[l] = '0'
		l++
	}
	for x > 0 {
		temp[l] = byte(x%10) + '0'
		x /= 10
		l++
	}
	for i := l - 1; i >= 0; i-- {
		b.arr[b.i] = temp[i]
		b.i++
	}
	b.arr[b.i] = delimiter
	b.i++
	if b.i > b.sz-20 {
		b.flush(out)
	}
}

func (b *buffer) flush(out io.Writer) {
	if b.i > 0 {
		out.Write(b.arr[:b.i])
		b.i = 0
	}
}

// Solution solves cf 1419 d2
func Solution(in io.Reader, out io.Writer, showOrderArray bool) {

	// 1 <= n <= 1e5
	// 1 <= a1..an <= 1e9
	n, x := processInput(in)

	// sort descending
	sort.Sort(spheres(x))

	l, r := 1, n-1
	ans := 0

	for l <= r {
		m := (l + r) / 2

		valid := true

		if n-m <= m {
			valid = false
		} else {
			for i, j := m-1, n-2; i >= 0; i, j = i-1, j-1 {
				if x[i] >= x[j] {
					valid = false
					break
				}
			}
		}

		if valid {
			ans = m
			l = m + 1
		} else {
			r = m - 1
		}
	}

	ord := make([]int, n)
	k := 0
	l = 0
	r = n - ans - 1

	for k < n {
		if r < n {
			ord[k] = x[r]
			k++
			r++
		}

		if l < n-ans-1 {
			ord[k] = x[l]
			k++
			l++
		}
	}

	output.add(out, ans, '\n')

	if showOrderArray {
		for i := 0; i < n-1; i++ {
			output.add(out, ord[i], ' ')
		}
		output.add(out, ord[n-1], '\n')
	}

	output.flush(out)
}

func main() {
	Solution(os.Stdin, os.Stdout, true)
}

package main

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

func processInput(in io.Reader) (int, []int) {
	buf, _ := ioutil.ReadAll(in)

	var i, n int
	for ; buf[i] >= '0' && buf[i] <= '9'; i++ {
		n = (n * 10) + int(buf[i]&15)
	}

	ints := make([]int, n)
	l := len(buf)

	for j := 0; j < n; j++ {
		for ; buf[i] < '0' || buf[i] > '9'; i++ {
		}

		for ; i < l && buf[i] >= '0' && buf[i] <= '9'; i++ {
			ints[j] = (ints[j] * 10) + int(buf[i]&15)
		}
	}

	return n, ints
}

type spheres []int

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

const _B int = 1024 * 1024

type buffer struct {
	i   int
	sz  int
	arr []byte
}

var output = &buffer{
	sz:  _B,
	arr: make([]byte, _B),
}

func (b *buffer) add(out io.Writer, x int, delimiter byte) {
	temp := make([]byte, 10)
	l := 0
	if x == 0 {
		temp[l] = '0'
		l++
	}
	for x > 0 {
		temp[l] = byte(x%10) + '0'
		x /= 10
		l++
	}
	for i := l - 1; i >= 0; i-- {
		b.arr[b.i] = temp[i]
		b.i++
	}
	b.arr[b.i] = delimiter
	b.i++
	if b.i > b.sz-20 {
		b.flush(out)
	}
}

func (b *buffer) flush(out io.Writer) {
	if b.i > 0 {
		out.Write(b.arr[:b.i])
		b.i = 0
	}
}

// Solution solves cf 1419 d2
func Solution(in io.Reader, out io.Writer, showOrderArray bool) {

	// 1 <= n <= 1e5
	// 1 <= a1..an <= 1e9
	n, x := processInput(in)

	// sort descending
	sort.Sort(spheres(x))

	l, r := 1, (n-1)/2
	ans := 0

	for l <= r {
		m := (l + r) / 2

		valid := true

		for i, j := 0, n-m-1; i < m; i, j = i+1, j+1 {
			if x[i] >= x[j] {
				valid = false
				break
			}
		}

		if valid {
			ans = m
			l = m + 1
		} else {
			r = m - 1
		}
	}

	ord := make([]int, n)
	k := 0
	l = 0
	r = n - ans - 1

	for k < n {
		if r < n {
			ord[k] = x[r]
			k++
			r++
		}

		if l < n-ans-1 {
			ord[k] = x[l]
			k++
			l++
		}
	}

	output.add(out, ans, '\n')

	if showOrderArray {
		for i := 0; i < n-1; i++ {
			output.add(out, ord[i], ' ')
		}
		output.add(out, ord[n-1], '\n')
	}

	output.flush(out)
}

func main() {
	Solution(os.Stdin, os.Stdout, true)
}

package main

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

func processInput(in io.Reader) (int, []int) {
	buf, _ := ioutil.ReadAll(in)

	var i, n int
	for ; buf[i] >= '0' && buf[i] <= '9'; i++ {
		n = (n * 10) + int(buf[i]&15)
	}

	ints := make([]int, n)
	l := len(buf)

	for j := 0; j < n; j++ {
		for ; buf[i] < '0' || buf[i] > '9'; i++ {
		}

		for ; i < l && buf[i] >= '0' && buf[i] <= '9'; i++ {
			ints[j] = (ints[j] * 10) + int(buf[i]&15)
		}
	}

	return n, ints
}

type spheres []int

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

const _B int = 256 * 1024

type buffer struct {
	i   int
	sz  int
	arr []byte
}

var output = &buffer{
	sz:  _B,
	arr: make([]byte, _B),
}

func (b *buffer) add(out io.Writer, x int, delimiter byte) {
	temp := make([]byte, 10)
	l := 0
	if x == 0 {
		temp[l] = '0'
		l++
	}
	for x > 0 {
		temp[l] = byte(x%10) + '0'
		x /= 10
		l++
	}
	for i := l - 1; i >= 0; i-- {
		b.arr[b.i] = temp[i]
		b.i++
	}
	b.arr[b.i] = delimiter
	b.i++
	if b.i > b.sz-20 {
		b.flush(out)
	}
}

func (b *buffer) flush(out io.Writer) {
	if b.i > 0 {
		out.Write(b.arr[:b.i])
		b.i = 0
	}
}

// Solution solves cf 1419 d2
func Solution(in io.Reader, out io.Writer, showOrderArray bool) {

	// 1 <= n <= 1e5
	// 1 <= a1..an <= 1e9
	n, x := processInput(in)

	// sort descending
	sort.Sort(spheres(x))

	l, r := 1, n-1
	ans := 0

	for l <= r {
		m := (l + r) / 2

		valid := true

		if n-m <= m {
			valid = false
		} else {
			for i, j := m-1, n-2; i >= 0; i, j = i-1, j-1 {
				if x[i] >= x[j] {
					valid = false
					break
				}
			}
		}

		if valid {
			ans = m
			l = m + 1
		} else {
			r = m - 1
		}
	}

	ord := make([]int, n)
	k := 0
	l = 0
	r = n - ans - 1

	for k < n {
		if r < n {
			ord[k] = x[r]
			k++
			r++
		}

		if l < n-ans-1 {
			ord[k] = x[l]
			k++
			l++
		}
	}

	output.add(out, ans, '\n')

	if showOrderArray {
		for i := 0; i < n-1; i++ {
			output.add(out, ord[i], ' ')
		}
		output.add(out, ord[n-1], '\n')
	}

	output.flush(out)
}

func main() {
	Solution(os.Stdin, os.Stdout, true)
}

package main

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

func processInput(in io.Reader) (int, []int) {
	buf, _ := ioutil.ReadAll(in)

	var i, n int
	for ; buf[i] >= '0' && buf[i] <= '9'; i++ {
		n = (n * 10) + int(buf[i]&15)
	}

	ints := make([]int, n)
	l := len(buf)

	for j := 0; j < n; j++ {
		for ; buf[i] < '0' || buf[i] > '9'; i++ {
		}

		for ; i < l && buf[i] >= '0' && buf[i] <= '9'; i++ {
			ints[j] = (ints[j] * 10) + int(buf[i]&15)
		}
	}

	return n, ints
}

type spheres []int

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

const _B int = 1024 * 1024

type buffer struct {
	i   int
	sz  int
	arr []byte
}

var output = &buffer{
	sz:  _B,
	arr: make([]byte, _B),
}

func (b *buffer) add(out io.Writer, x int, delimiter byte) {
	temp := make([]byte, 10)
	l := 0
	if x == 0 {
		temp[l] = '0'
		l++
	}
	for x > 0 {
		temp[l] = byte(x%10) + '0'
		x /= 10
		l++
	}
	for i := l - 1; i >= 0; i-- {
		b.arr[b.i] = temp[i]
		b.i++
	}
	b.arr[b.i] = delimiter
	b.i++
	if b.i > b.sz-20 {
		b.flush(out)
	}
}

func (b *buffer) flush(out io.Writer) {
	if b.i > 0 {
		out.Write(b.arr[:b.i])
		b.i = 0
	}
}

// Solution solves cf 1419 d2
func Solution(in io.Reader, out io.Writer, showOrderArray bool) {

	// 1 <= n <= 1e5
	// 1 <= a1..an <= 1e9
	n, x := processInput(in)

	// sort descending
	sort.Sort(spheres(x))

	l, r := 1, n-1
	ans := 0

	for l <= r {
		m := (l + r) / 2

		valid := true

		if n-m <= m {
			valid = false
		} else {
			for i, j := 0, n-m-1; i < m; i, j = i+1, j+1 {
				if x[i] >= x[j] {
					valid = false
					break
				}
			}
		}

		if valid {
			ans = m
			l = m + 1
		} else {
			r = m - 1
		}
	}

	ord := make([]int, n)
	k := 0
	l = 0
	r = n - ans - 1

	for k < n {
		if r < n {
			ord[k] = x[r]
			k++
			r++
		}

		if l < n-ans-1 {
			ord[k] = x[l]
			k++
			l++
		}
	}

	output.add(out, ans, '\n')

	if showOrderArray {
		for i := 0; i < n-1; i++ {
			output.add(out, ord[i], ' ')
		}
		output.add(out, ord[n-1], '\n')
	}

	output.flush(out)
}

func main() {
	Solution(os.Stdin, os.Stdout, true)
}

package main

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

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

func main() {
	defer rw.Flush()
	n := rw.NextInt()
	a := rw.NextInts(n)
	sort.Ints(a[:])
	b := make([]int, n)
	var c []int
	for i := 0; i < n; i++ {
		if i%2 == 0 {
			c = append(c, a[i/2+n/2])
		} else {
			b[i] = a[i/2]
		}
	}
	ok := func(m int) bool {
		s := (n-1)/2 - m
		for i := 0; i < m; i++ {
			j := i*2 + 1
			if b[j] >= c[s+i] {
				return false
			}
		}
		return true
	}
	lo, hi := 0, (n-1)/2
	for lo < hi {
		md := (lo + hi + 1) / 2
		if ok(md) {
			lo = md
		} else {
			hi = md - 1
		}
	}
	s := (n-1)/2 - lo
	for i := 0; i < lo+1; i++ {
		b[i*2] = c[s+i]
	}
	for i := lo + 1; i < len(c); i++ {
		b[i*2] = c[i-lo-1]
	}
	rw.Println(lo)
	rw.PrintlnInts(b...)
}

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

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

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

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

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

func processInput(in io.Reader) (int, []int) {
	buf, _ := ioutil.ReadAll(in)

	var i, n int
	for ; buf[i] >= '0' && buf[i] <= '9'; i++ {
		n = (n * 10) + int(buf[i]&15)
	}

	ints := make([]int, n)
	l := len(buf)

	for j := 0; j < n; j++ {
		for ; buf[i] < '0' || buf[i] > '9'; i++ {
		}

		for ; i < l && buf[i] >= '0' && buf[i] <= '9'; i++ {
			ints[j] = (ints[j] * 10) + int(buf[i]&15)
		}
	}

	return n, ints
}

type spheres []int

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

const _B int = 1024 * 1024

type buffer struct {
	i   int
	sz  int
	arr []byte
}

var output = &buffer{
	sz:  _B,
	arr: make([]byte, _B),
}

func (b *buffer) add(out io.Writer, x int, delimiter byte) {
	temp := make([]byte, 10)
	l := 0
	if x == 0 {
		temp[l] = '0'
		l++
	}
	for x > 0 {
		temp[l] = byte(x%10) + '0'
		x /= 10
		l++
	}
	for i := l - 1; i >= 0; i-- {
		b.arr[b.i] = temp[i]
		b.i++
	}
	b.arr[b.i] = delimiter
	b.i++
	if b.i > b.sz-20 {
		b.flush(out)
	}
}

func (b *buffer) flush(out io.Writer) {
	if b.i > 0 {
		out.Write(b.arr[:b.i])
		b.i = 0
	}
}

// Solution solves cf 1419 d2
func Solution(in io.Reader, out io.Writer, showOrderArray bool) {

	// 1 <= n <= 1e5
	// 1 <= a1..an <= 1e9
	n, x := processInput(in)

	// sort descending
	sort.Sort(spheres(x))
	for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
		x[i], x[j] = x[j], x[i]
	}

	// control used values while going left to right
	used := make([]bool, n)
	l, r := 1, 2
	ans := 0

	ord := make([]int, n)
	ord[0] = x[0]
	k := 1

	for {
		for l < n && used[l] {
			l++
		}

		for r < n && used[r] {
			r++
		}

		for r < n && x[l] == x[r] {
			r++
		}

		if r >= n {
			for k < n {
				ord[k] = x[l]
				used[l] = true
				k++

				if k == n {
					break
				}

				for used[l] {
					l++
				}
			}
			break
		}

		ans++

		ord[k] = x[r]
		used[r] = true
		k++

		ord[k] = x[l]
		used[l] = true
		k++
	}

	output.add(out, ans, '\n')

	if showOrderArray {
		for i := 0; i < n-1; i++ {
			output.add(out, ord[i], ' ')
		}
		output.add(out, ord[n-1], '\n')
	}

	output.flush(out)
}

func main() {
	Solution(os.Stdin, os.Stdout, true)
}

package main

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

func processInput(in io.Reader) (int, []int) {
	buf, _ := ioutil.ReadAll(in)

	var i, n int
	for ; buf[i] >= '0' && buf[i] <= '9'; i++ {
		n = (n * 10) + int(buf[i]&15)
	}

	ints := make([]int, n)
	l := len(buf)

	for j := 0; j < n; j++ {
		for ; buf[i] < '0' || buf[i] > '9'; i++ {
		}

		for ; i < l && buf[i] >= '0' && buf[i] <= '9'; i++ {
			ints[j] = (ints[j] * 10) + int(buf[i]&15)
		}
	}

	return n, ints
}

type spheres []int

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

const _B int = 1024 * 1024

type buffer struct {
	i   int
	sz  int
	arr []byte
}

var output = &buffer{
	sz:  _B,
	arr: make([]byte, _B),
}

func (b *buffer) add(out io.Writer, x int, delimiter byte) {
	temp := make([]byte, 10)
	l := 0
	if x == 0 {
		temp[l] = '0'
		l++
	}
	for x > 0 {
		temp[l] = byte(x%10) + '0'
		x /= 10
		l++
	}
	for i := l - 1; i >= 0; i-- {
		b.arr[b.i] = temp[i]
		b.i++
	}
	b.arr[b.i] = delimiter
	b.i++
	if b.i > b.sz-20 {
		b.flush(out)
	}
}

func (b *buffer) flush(out io.Writer) {
	if b.i > 0 {
		out.Write(b.arr[:b.i])
		b.i = 0
	}
}

// Solution solves cf 1419 d2
func Solution(in io.Reader, out io.Writer) {

	// 1 <= n <= 1e5
	// 1 <= a1..an <= 1e9
	n, x := processInput(in)

	// sort descending
	sort.Sort(spheres(x))
	for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
		x[i], x[j] = x[j], x[i]
	}

	// two pointers from 1 and n-1 going from sides to the middle
	ord := make([]int, n)
	l, r := 0, n-1
	k := 0

	for l <= r {
		ord[k] = x[l]
		k++
		l++

		if l <= r {
			ord[k] = x[r]
			k++
			r--
		}
	}

	ans := 0
	for i := 1; i < n-1; i++ {
		if ord[i] < ord[i-1] && ord[i] < ord[i+1] {
			ans++
		}
	}

	output.add(out, ans, '\n')

	for i := 0; i < n-1; i++ {
		output.add(out, ord[i], ' ')
	}
	output.add(out, ord[n-1], '\n')

	output.flush(out)
}

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

package main

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

func processInput(in io.Reader) (int, []int) {
	buf, _ := ioutil.ReadAll(in)

	var i, n int
	for ; buf[i] >= '0' && buf[i] <= '9'; i++ {
		n = (n * 10) + int(buf[i]&15)
	}

	ints := make([]int, n)
	l := len(buf)

	for j := 0; j < n; j++ {
		for ; buf[i] < '0' || buf[i] > '9'; i++ {
		}

		for ; i < l && buf[i] >= '0' && buf[i] <= '9'; i++ {
			ints[j] = (ints[j] * 10) + int(buf[i]&15)
		}
	}

	return n, ints
}

type spheres []int

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

const _B int = 1024 * 1024

type buffer struct {
	i   int
	sz  int
	arr []byte
}

var output = &buffer{
	sz:  _B,
	arr: make([]byte, _B),
}

func (b *buffer) add(out io.Writer, x int, delimiter byte) {
	temp := make([]byte, 10)
	l := 0
	if x == 0 {
		temp[l] = '0'
		l++
	}
	for x > 0 {
		temp[l] = byte(x%10) + '0'
		x /= 10
		l++
	}
	for i := l - 1; i >= 0; i-- {
		b.arr[b.i] = temp[i]
		b.i++
	}
	b.arr[b.i] = delimiter
	b.i++
	if b.i > b.sz-20 {
		b.flush(out)
	}
}

func (b *buffer) flush(out io.Writer) {
	if b.i > 0 {
		out.Write(b.arr[:b.i])
		b.i = 0
	}
}

// Solution solves cf 1419 d2
func Solution(in io.Reader, out io.Writer) {

	// 1 <= n <= 1e5
	// 1 <= a1..an <= 1e9
	n, x := processInput(in)

	// sort descending
	sort.Sort(spheres(x))
	for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
		x[i], x[j] = x[j], x[i]
	}

	// two pointers from 1 and n-1 going from sides to the middle
	ord := make([]int, n)
	l, r := 0, n-1
	k := 0

	for l <= r {
		ord[k] = x[l]
		k++
		l++

		if l <= r {
			ord[k] = x[r]
			k++
			r--
		}
	}

	ord[0], ord[n-1] = ord[n-1], ord[0]

	ans := 0
	for i := 1; i < n-1; i++ {
		if ord[i] < ord[i-1] && ord[i] < ord[i+1] {
			ans++
		}
	}

	output.add(out, ans, '\n')

	for i := 0; i < n-1; i++ {
		output.add(out, ord[i], ' ')
	}
	output.add(out, ord[n-1], '\n')

	output.flush(out)
}

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

package main

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

func processInput(in io.Reader) (int, []int) {
	buf, _ := ioutil.ReadAll(in)

	var i, n int
	for ; buf[i] >= '0' && buf[i] <= '9'; i++ {
		n = (n * 10) + int(buf[i]&15)
	}

	ints := make([]int, n)
	l := len(buf)

	for j := 0; j < n; j++ {
		for ; buf[i] < '0' || buf[i] > '9'; i++ {
		}

		for ; i < l && buf[i] >= '0' && buf[i] <= '9'; i++ {
			ints[j] = (ints[j] * 10) + int(buf[i]&15)
		}
	}

	return n, ints
}

type spheres []int

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

const _B int = 1024 * 1024

type buffer struct {
	i   int
	sz  int
	arr []byte
}

var output = &buffer{
	sz:  _B,
	arr: make([]byte, _B),
}

func (b *buffer) add(out io.Writer, x int, delimiter byte) {
	temp := make([]byte, 10)
	l := 0
	if x == 0 {
		temp[l] = '0'
		l++
	}
	for x > 0 {
		temp[l] = byte(x%10) + '0'
		x /= 10
		l++
	}
	for i := l - 1; i >= 0; i-- {
		b.arr[b.i] = temp[i]
		b.i++
	}
	b.arr[b.i] = delimiter
	b.i++
	if b.i > b.sz-20 {
		b.flush(out)
	}
}

func (b *buffer) flush(out io.Writer) {
	if b.i > 0 {
		out.Write(b.arr[:b.i])
		b.i = 0
	}
}

// Solution solves cf 1419 d2
func Solution(in io.Reader, out io.Writer) {

	// 1 <= n <= 1e5
	// 1 <= a1..an <= 1e9
	n, x := processInput(in)

	// sort descending
	sort.Sort(spheres(x))
	for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
		x[i], x[j] = x[j], x[i]
	}

	// control used values while going left to right
	used := make([]bool, n)

	l, r := 1, 2
	ans := 0
	ord := make([]int, n)
	ord[0] = x[0]
	k := 1

	for {
		for l < n && used[l] {
			l++
		}

		for r < n && (r <= l || used[r]) {
			r++
		}

		for r < n && x[l] == x[r] {
			r++
		}

		if r >= n {
			for k < n {
				ord[k] = x[l]
				k++
				l++

				if k == n {
					break
				}

				for used[l] {
					l++
				}
			}
			break
		}

		ans++

		ord[k] = x[r]
		used[r] = true
		k++

		ord[k] = x[l]
		used[l] = true
		k++
	}

	output.add(out, ans, '\n')

	for i := 0; i < n-1; i++ {
		output.add(out, ord[i], ' ')
	}
	output.add(out, ord[n-1], '\n')

	output.flush(out)
}

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

package main

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

func processInput(in io.Reader) (int, []int) {
	buf, _ := ioutil.ReadAll(in)

	var i, n int
	for ; buf[i] >= '0' && buf[i] <= '9'; i++ {
		n = (n * 10) + int(buf[i]&15)
	}

	ints := make([]int, n)
	l := len(buf)

	for j := 0; j < n; j++ {
		for ; buf[i] < '0' || buf[i] > '9'; i++ {
		}

		for ; i < l && buf[i] >= '0' && buf[i] <= '9'; i++ {
			ints[j] = (ints[j] * 10) + int(buf[i]&15)
		}
	}

	return n, ints
}

type spheres []int

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

const _B int = 1024 * 1024

type buffer struct {
	i   int
	sz  int
	arr []byte
}

var output = &buffer{
	sz:  _B,
	arr: make([]byte, _B),
}

func (b *buffer) add(out io.Writer, x int, delimiter byte) {
	temp := make([]byte, 10)
	l := 0
	if x == 0 {
		temp[l] = '0'
		l++
	}
	for x > 0 {
		temp[l] = byte(x%10) + '0'
		x /= 10
		l++
	}
	for i := l - 1; i >= 0; i-- {
		b.arr[b.i] = temp[i]
		b.i++
	}
	b.arr[b.i] = delimiter
	b.i++
	if b.i > b.sz-20 {
		b.flush(out)
	}
}

func (b *buffer) flush(out io.Writer) {
	if b.i > 0 {
		out.Write(b.arr[:b.i])
		b.i = 0
	}
}

// Solution solves cf 1419 d2
func Solution(in io.Reader, out io.Writer) {

	// 1 <= n <= 1e5
	// 1 <= a1..an <= 1e9
	n, x := processInput(in)

	// sort
	sort.Sort(spheres(x))

	// get how many x spheres are cheaper than at least x+1 remaining spheres
	l, r := 0, (n/2)-1

	for l <= r {
		m := (l + r + 1) / 2
		good := true

		for i, j := n-1, m; j >= 0; i, j = i-1, j-1 {
			if x[j] >= x[i] {
				good = false
				break
			}
		}

		if good {
			l = m + 1
		} else {
			r = m - 1
		}
	}

	ord := make([]int, n)
	ans := l
	r = n - 1
	k := 0

	for k < n {
		ord[k] = x[r]
		k++
		r--

		if k < n && l > 0 {
			l--
			ord[k] = x[l]
			k++
		}
	}

	output.add(out, ans, '\n')

	for i := 0; i < n-1; i++ {
		output.add(out, ord[i], ' ')
	}
	output.add(out, ord[n-1], '\n')

	output.flush(out)
}

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

package main

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

func processInput(in io.Reader) (int, []int) {
	buf, _ := ioutil.ReadAll(in)

	var i, n int
	for ; buf[i] >= '0' && buf[i] <= '9'; i++ {
		n = (n * 10) + int(buf[i]&15)
	}

	ints := make([]int, n)
	l := len(buf)

	for j := 0; j < n; j++ {
		for ; buf[i] < '0' || buf[i] > '9'; i++ {
		}

		for ; i < l && buf[i] >= '0' && buf[i] <= '9'; i++ {
			ints[j] = (ints[j] * 10) + int(buf[i]&15)
		}
	}

	return n, ints
}

type spheres []int

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

const _B int = 1024 * 1024

type buffer struct {
	i   int
	sz  int
	arr []byte
}

var output = &buffer{
	sz:  _B,
	arr: make([]byte, _B),
}

func (b *buffer) add(out io.Writer, x int, delimiter byte) {
	temp := make([]byte, 10)
	l := 0
	if x == 0 {
		temp[l] = '0'
		l++
	}
	for x > 0 {
		temp[l] = byte(x%10) + '0'
		x /= 10
		l++
	}
	for i := l - 1; i >= 0; i-- {
		b.arr[b.i] = temp[i]
		b.i++
	}
	b.arr[b.i] = delimiter
	b.i++
	if b.i > b.sz-20 {
		b.flush(out)
	}
}

func (b *buffer) flush(out io.Writer) {
	if b.i > 0 {
		out.Write(b.arr[:b.i])
		b.i = 0
	}
}

// Solution solves cf 1419 d2
func Solution(in io.Reader, out io.Writer) {

	// 1 <= n <= 1e5
	// 1 <= a1..an <= 1e9
	n, x := processInput(in)

	// sort
	sort.Sort(spheres(x))

	// get how many x spheres are cheaper than at least x+1 remaining spheres
	l, r := 0, (n/2)-1

	for l <= r {
		m := (l + r) / 2
		good := true

		for i, j := n-1, m; j >= 0; i, j = i-1, j-1 {
			if x[j] >= x[i] {
				good = false
				break
			}
		}

		if good {
			l = m + 1
		} else {
			r = m - 1
		}
	}

	ord := make([]int, n)
	ans := l
	r = n - 1
	k := 0

	for k < n {
		ord[k] = x[r]
		k++
		r--

		if k < n && l > 0 {
			l--
			ord[k] = x[l]
			k++
		}
	}

	output.add(out, ans, '\n')

	for i := 0; i < n-1; i++ {
		output.add(out, ord[i], ' ')
	}
	output.add(out, ord[n-1], '\n')

	output.flush(out)
}

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

package main

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

func processInput(in io.Reader) (int, []int) {
	buf, _ := ioutil.ReadAll(in)

	var i, n int
	for ; buf[i] >= '0' && buf[i] <= '9'; i++ {
		n = (n * 10) + int(buf[i]&15)
	}

	ints := make([]int, n)
	l := len(buf)

	for j := 0; j < n; j++ {
		for ; buf[i] < '0' || buf[i] > '9'; i++ {
		}

		for ; i < l && buf[i] >= '0' && buf[i] <= '9'; i++ {
			ints[j] = (ints[j] * 10) + int(buf[i]&15)
		}
	}

	return n, ints
}

type spheres []int

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

const _B int = 1024 * 1024

type buffer struct {
	i   int
	sz  int
	arr []byte
}

var output = &buffer{
	sz:  _B,
	arr: make([]byte, _B),
}

func (b *buffer) add(out io.Writer, x int, delimiter byte) {
	temp := make([]byte, 10)
	l := 0
	if x == 0 {
		temp[l] = '0'
		l++
	}
	for x > 0 {
		temp[l] = byte(x%10) + '0'
		x /= 10
		l++
	}
	for i := l - 1; i >= 0; i-- {
		b.arr[b.i] = temp[i]
		b.i++
	}
	b.arr[b.i] = delimiter
	b.i++
	if b.i > b.sz-20 {
		b.flush(out)
	}
}

func (b *buffer) flush(out io.Writer) {
	if b.i > 0 {
		out.Write(b.arr[:b.i])
		b.i = 0
	}
}

// Solution solves cf 1419 d2
func Solution(in io.Reader, out io.Writer) {

	// 1 <= n <= 1e5
	// 1 <= a1..an <= 1e9
	n, x := processInput(in)

	// sort descending
	sort.Sort(spheres(x))
	for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
		x[i], x[j] = x[j], x[i]
	}

	// two pointers from 1 and ceil(n/2) going from left to right filling ord array
	ord := make([]int, n)
	l, r := 0, (n+1)/2
	k := 0

	for r < n {
		ord[k] = x[l]
		k++
		l++

		ord[k] = x[r]
		k++
		r++
	}

	if k == n-1 {
		ord[k] = x[l]
		k++
	}

	ans := 0
	for i := 1; i < n-1; i++ {
		if ord[i] < ord[i-1] && ord[i] < ord[i+1] {
			ans++
		}
	}

	output.add(out, ans, '\n')

	for i := 0; i < n-1; i++ {
		output.add(out, ord[i], ' ')
	}
	output.add(out, ord[n-1], '\n')

	output.flush(out)
}

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

package main

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

func processInput(in io.Reader) (int, []int) {
	buf, _ := ioutil.ReadAll(in)

	var i, n int
	for ; buf[i] >= '0' && buf[i] <= '9'; i++ {
		n = (n * 10) + int(buf[i]&15)
	}

	ints := make([]int, n)
	l := len(buf)

	for j := 0; j < n; j++ {
		for ; buf[i] < '0' || buf[i] > '9'; i++ {
		}

		for ; i < l && buf[i] >= '0' && buf[i] <= '9'; i++ {
			ints[j] = (ints[j] * 10) + int(buf[i]&15)
		}
	}

	return n, ints
}

type spheres []int

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

const _B int = 1024 * 1024

type buffer struct {
	i   int
	sz  int
	arr []byte
}

var output = &buffer{
	sz:  _B,
	arr: make([]byte, _B),
}

func (b *buffer) add(out io.Writer, x int, delimiter byte) {
	temp := make([]byte, 10)
	l := 0
	if x == 0 {
		temp[l] = '0'
		l++
	}
	for x > 0 {
		temp[l] = byte(x%10) + '0'
		x /= 10
		l++
	}
	for i := l - 1; i >= 0; i-- {
		b.arr[b.i] = temp[i]
		b.i++
	}
	b.arr[b.i] = delimiter
	b.i++
	if b.i > b.sz-20 {
		b.flush(out)
	}
}

func (b *buffer) flush(out io.Writer) {
	if b.i > 0 {
		out.Write(b.arr[:b.i])
		b.i = 0
	}
}

// Solution solves cf 1419 d2
func Solution(in io.Reader, out io.Writer) {

	// 1 <= n <= 1e5
	// 1 <= a1..an <= 1e9
	n, x := processInput(in)

	// sort descending
	sort.Sort(spheres(x))
	for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
		x[i], x[j] = x[j], x[i]
	}

	// control used values while going left to right
	used := make([]bool, n)

	l, r := 1, 2
	ans := 0
	ord := make([]int, n)
	ord[0] = x[0]
	used[0] = true
	k := 1

	for {
		for l < n && used[l] {
			l++
		}

		for r < n && used[r] {
			r++
		}

		for r < n && x[l] == x[r] {
			r++
		}

		if r >= n || x[l] == x[r] {
			for k < n {
				ord[k] = x[l]
				used[l] = true
				k++

				for l < n && used[l] {
					l++
				}
			}
			break
		}

		ans++

		ord[k] = x[r]
		used[r] = true
		k++

		ord[k] = x[l]
		used[l] = true
		k++
	}

	output.add(out, ans, '\n')

	for i := 0; i < n-1; i++ {
		output.add(out, ord[i], ' ')
	}
	output.add(out, ord[n-1], '\n')

	output.flush(out)
}

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

package main

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

func processInput(in io.Reader) (int, []int) {
	buf, _ := ioutil.ReadAll(in)

	var i, n int
	for ; buf[i] >= '0' && buf[i] <= '9'; i++ {
		n = (n * 10) + int(buf[i]&15)
	}

	ints := make([]int, n)
	l := len(buf)

	for j := 0; j < n; j++ {
		for ; buf[i] < '0' || buf[i] > '9'; i++ {
		}

		for ; i < l && buf[i] >= '0' && buf[i] <= '9'; i++ {
			ints[j] = (ints[j] * 10) + int(buf[i]&15)
		}
	}

	return n, ints
}

type spheres []int

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

const _B int = 1024 * 1024

type buffer struct {
	i   int
	sz  int
	arr []byte
}

var output = &buffer{
	sz:  _B,
	arr: make([]byte, _B),
}

func (b *buffer) add(out io.Writer, x int, delimiter byte) {
	temp := make([]byte, 10)
	l := 0
	if x == 0 {
		temp[l] = '0'
		l++
	}
	for x > 0 {
		temp[l] = byte(x%10) + '0'
		x /= 10
		l++
	}
	for i := l - 1; i >= 0; i-- {
		b.arr[b.i] = temp[i]
		b.i++
	}
	b.arr[b.i] = delimiter
	b.i++
	if b.i > b.sz-20 {
		b.flush(out)
	}
}

func (b *buffer) flush(out io.Writer) {
	if b.i > 0 {
		out.Write(b.arr[:b.i])
		b.i = 0
	}
}

// Solution solves cf 1419 d2
func Solution(in io.Reader, out io.Writer, showOrderArray bool) {

	// 1 <= n <= 1e5
	// 1 <= a1..an <= 1e9
	n, x := processInput(in)

	// sort descending
	sort.Sort(spheres(x))

	l, r := 1, n-1
	ans := 0

	for l <= r {
		m := (l + r) / 2

		valid := true

		if n-m <= m {
			valid = false
		} else {
			for i, j := 0, n-m-1; i < m; i, j = i+1, j+1 {
				if x[i] >= x[j] {
					valid = false
					break
				}
			}
		}

		if valid {
			ans = m
			l = m + 1
		} else {
			r = m - 1
		}
	}

	ord := make([]int, n)
	k := 0
	l = 0
	r = ans

	for k < n {
		if r < n {
			ord[k] = x[r]
			k++
			r++
		}

		if l < ans {
			ord[k] = x[l]
			k++
			l++
		}
	}

	output.add(out, ans, '\n')

	if showOrderArray {
		for i := 0; i < n-1; i++ {
			output.add(out, ord[i], ' ')
		}
		output.add(out, ord[n-1], '\n')
	}

	output.flush(out)
}

func main() {
	Solution(os.Stdin, os.Stdout, true)
}


Dora the explorer has decided to use her money after several years of juicy royalties to go shopping. What better place to shop than Nlogonia?There are $$$n$$$ stores numbered from $$$1$$$ to $$$n$$$ in Nlogonia. The $$$i$$$-th of these stores offers a positive integer $$$a_i$$$.Each day among the last $$$m$$$ days Dora bought a single integer from some of the stores. The same day, Swiper the fox bought a single integer from all the stores that Dora did not buy an integer from on that day.Dora considers Swiper to be her rival, and she considers that she beat Swiper on day $$$i$$$ if and only if the least common multiple of the numbers she bought on day $$$i$$$ is strictly greater than the least common multiple of the numbers that Swiper bought on day $$$i$$$.The least common multiple (LCM) of a collection of integers is the smallest positive integer that is divisible by all the integers in the collection.However, Dora forgot the values of $$$a_i$$$. Help Dora find out if there are positive integer values of $$$a_i$$$ such that she beat Swiper on every day. You don't need to find what are the possible values of $$$a_i$$$ though.Note that it is possible for some values of $$$a_i$$$ to coincide in a solution.

The first line contains integers $$$m$$$ and $$$n$$$ ($$$1\leq m \leq 50$$$, $$$1\leq n \leq 10^4$$$) — the number of days and the number of stores. After this $$$m$$$ lines follow, the $$$i$$$-th line starts with an integer $$$s_i$$$ ($$$1\leq s_i \leq n-1$$$), the number of integers Dora bought on day $$$i$$$, followed by $$$s_i$$$ distinct integers, the indices of the stores where Dora bought an integer on the $$$i$$$-th day. The indices are between $$$1$$$ and $$$n$$$.

Output must consist of a single line containing "possible" if there exist positive integers $$$a_i$$$ such that for each day the least common multiple of the integers bought by Dora is strictly greater than the least common multiple of the integers bought by Swiper on that day. Otherwise, print "impossible". Note that you don't have to restore the integers themselves.

NoteIn the first sample, a possible choice for the values of the $$$a_i$$$ is $$$3, 4, 3, 5, 2$$$. On the first day, Dora buys the integers $$$3, 4$$$ and $$$3$$$, whose LCM is $$$12$$$, while Swiper buys integers $$$5$$$ and $$$2$$$, whose LCM is $$$10$$$. On the second day, Dora buys $$$3, 5$$$ and $$$2$$$, whose LCM is $$$30$$$, and Swiper buys integers $$$3$$$ and $$$4$$$, whose LCM is $$$12$$$.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1166E(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var m, n, v int
	Fscan(in, &m, &n)
	a := make([][]bool, m)
	for i := range a {
		a[i] = make([]bool, n+1)
		for Fscan(in, &m); m > 0; m-- {
			Fscan(in, &v)
			a[i][v] = true
		}
	}
	for i, vs := range a {
	o:
		for _, ws := range a[i+1:] {
			for j, v := range vs {
				if v && ws[j] {
					continue o
				}
			}
			Fprint(out, "impossible")
			return
		}
	}
	Fprint(out, "possible")
}

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

package main

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

var scanner = bufio.NewScanner(os.Stdin)

func readInt() int {
	scanner.Scan()
	n, err := strconv.Atoi(scanner.Text())
	if err != nil {
		panic(err)
	}
	return n
}

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

func findCommon(a, b []int) bool {
	e1, e2 := 0, 0
	for e1 < len(a) && e2 < len(b) {
		if a[e1] == b[e2] {
			return true
		} else if a[e1] < b[e2] {
			e1++
		} else {
			e2++
		}
	}
	return false
}

func main() {
	scanner.Split(bufio.ScanWords)

	m, _ := readInt(), readInt()
	days := make([][]int, m)
	for i := 0; i < m; i++ {
		k := readInt()
		days[i] = make([]int, k)
		for j := 0; j < k; j++ {
			t := readInt()
			days[i][j] = t
		}
		sort.Ints(days[i])
	}

	for i := 0; i < m; i++ {
		for j := i + 1; j < m; j++ {
			if !findCommon(days[i], days[j]) {
				fmt.Println("impossible")
				return
			}
		}
	}

	fmt.Println("possible")
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1166E(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, m, v int
	Fscan(in, &n, &m)
	a := make([]int64, n)
	for i := range a {
		for Fscan(in, &m); m > 0; m-- {
			Fscan(in, &v)
			a[i] |= int64(1) << v
		}
	}
	for i, v := range a {
		for _, w := range a[i+1:] {
			if v&w == 0 {
				Fprint(out, "impossible")
				return
			}
		}
	}
	Fprint(out, "possible")
}

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

package main

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

var scanner = bufio.NewScanner(os.Stdin)

func readInt() int {
	scanner.Scan()
	n, err := strconv.Atoi(scanner.Text())
	if err != nil {
		panic(err)
	}
	return n
}

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

func main() {
	scanner.Split(bufio.ScanWords)

	m, _ := readInt(), readInt()
	days := make([][]int, m)
	freq := make(map[int]int)
	for i := 0; i < m; i++ {
		k := readInt()
		days[i] = make([]int, k)
		for j := 0; j < k; j++ {
			t := readInt()
			days[i][j] = t
			freq[t]++
		}
	}

	for i := 0; i < m; i++ {
		valid := false
		for j := 0; j < len(days[i]); j++ {
			if freq[days[i][j]] > 1 {
				valid = true
				break
			}
		}
		if !valid {
			fmt.Println("impossible")
			return
		}
	}
	fmt.Println("possible")
}


Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.

The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).

Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.

NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2).

package main

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

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

func main() {
	rdr := bufio.NewReader(os.Stdin)
	var n, i int64
	fmt.Fscan(rdr, &n)
	a := make([]int64, n)
	for i = 0; i < n; i++ {
		fmt.Fscan(rdr, &a[i])
	}
	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})
	var result int64
	for i = 1; i <= n; i++ {
		result += abs(i - a[i-1])
	}
	fmt.Print(result)
}

package main

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

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

func main() {
	rdr := bufio.NewReader(os.Stdin)
	var n, i int
	fmt.Fscan(rdr, &n)
	a := make([]int, n)
	for i = 0; i < n; i++ {
		fmt.Fscan(rdr, &a[i])
	}
	sort.Ints(a)
	var result int64
	for i = 1; i <= n; i++ {
		result += int64(abs(i - a[i-1]))
	}
	fmt.Print(result)
}

package main

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

func main() {
	var n int
	input := bufio.NewScanner(os.Stdin)
    	input.Split(bufio.ScanWords)
    	fmt.Scan(&n)
	a:=make([]int,n)

	for i:=0;i<n;i++ {
		input.Scan();
       		aa, _ := strconv.Atoi(input.Text());
		a[i]=aa
	}
	//sort.Sort(sort.Reverse(sort.IntSlice(a)))
	sort.Ints(a)
	//for _,v:=range a {
		//fmt.Println(v)
	//}
	var ans int64
	var x int
	ans=0
	for i:=0;i<n;i++ {
		if (i+1-a[i])<0 {
			x=(i+1-a[i])*(-1)
		} else {
			x=(i+1-a[i])
		}
		ans+=int64(x)
	}
	fmt.Println(ans)
}

package main

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

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	array := make([]int, n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; i < n && scanner.Scan(); i++ {
		a, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		array[i] = a
	}

	merge_sort(array, 0, n-1)

	moves := int64(0)
	for i := 0; i < n; i++ {
		moves += int64(abs(array[i] - i - 1))
	}
	fmt.Println(moves)
}

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

func merge(buffer []int, start, middle, end int) {
	sorted := make([]int, end-start+1)

	i := start
	j := middle + 1
	k := 0
	for i <= middle && j <= end {
		if buffer[i] <= buffer[j] {
			sorted[k] = buffer[i]
			i++
		} else {
			sorted[k] = buffer[j]
			j++
		}
		k++
	}
	for i <= middle {
		sorted[k] = buffer[i]
		k++
		i++
	}
	for j <= end {
		sorted[k] = buffer[j]
		k++
		j++
	}

	i = start
	k = 0
	for i <= end {
		buffer[i] = sorted[k]
		i++
		k++
	}
}

func merge_sort(buffer []int, start, end int) {
	if end > start {
		middle := (end + start) / 2
		merge_sort(buffer, start, middle)
		merge_sort(buffer, middle+1, end)
		merge(buffer, start, middle, end)
	}
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    var n int
    fmt.Scan(&n)
    a := make([]int,n)
    for i := 0; i < n; i++ {
        scanner.Scan()
        a[i], _ = strconv.Atoi(scanner.Text())
    }
    sort.Ints(a)
    var ans int64 = 0
    for i := 0; i < n; i++ {
        ans += int64(math.Abs(float64(a[i]-(i+1))))
    }
    fmt.Println(ans)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	const maxCapacity = 1024 * 4096
	buf := make([]byte, maxCapacity)
	scanner.Buffer(buf, maxCapacity)

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

	scanner.Scan()
	numbers := make([]int, n)
	input := scanner.Text()
	for i, s := range strings.Split(input, " ") {
		numbers[i], _ = strconv.Atoi(s)
	}

	sort.Ints(numbers)
	var result int64
	for i, x := range numbers {
		result += int64(abs(x - (i + 1)))
	}

	fmt.Println(result)
}

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

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	const maxCapacity = 1024 * 8192
	buf := make([]byte, maxCapacity)
	scanner.Buffer(buf, maxCapacity)

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

	scanner.Scan()
	numbers := make([]int, n)
	input := scanner.Text()
	for i, s := range strings.Split(input, " ") {
		numbers[i], _ = strconv.Atoi(s)
	}

	sort.Ints(numbers)
	var result int64
	for i, x := range numbers {
		result += int64(abs(x - (i + 1)))
	}

	fmt.Println(result)
}

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

// 285C-mic
package main

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

func main() {
    writer := bufio.NewScanner(os.Stdin)
    writer.Split(bufio.ScanWords)
    var n int
    var res int64
    fmt.Scan(&n)
    m := make([]float64, n)
    for i := 0; i < n; i++ {
        writer.Scan()
        m[i], _ = strconv.ParseFloat(writer.Text(), 64)
    }

    sort.Sort(sort.Float64Slice(m))

    for i := 1; i <= n; i++ {
        res += int64(math.Abs(float64(m[i-1] - float64(i))))
    }

    fmt.Println(res)

}

package main

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

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

func main() {
	rdr := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(rdr, &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(rdr, &a[i])
	}
	sort.Ints(a)
	var result int
	for i := 1; i <= n; i++ {
		result += abs(i - a[i-1])
	}
	fmt.Print(result)
}

package main

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

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	array := make([]int, n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; i < n && scanner.Scan(); i++ {
		a, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		array[i] = a
	}

	merge_sort(array, 0, n-1)

	moves := 0
	for i := 0; i < n; i++ {
		moves += abs(array[i] - i - 1)
	}
	fmt.Println(moves)
}

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

func merge(buffer []int, start, middle, end int) {
	sorted := make([]int, end-start+1)

	i := start
	j := middle + 1
	k := 0
	for i <= middle && j <= end {
		if buffer[i] <= buffer[j] {
			sorted[k] = buffer[i]
			i++
		} else {
			sorted[k] = buffer[j]
			j++
		}
		k++
	}
	for i <= middle {
		sorted[k] = buffer[i]
		k++
		i++
	}
	for j <= end {
		sorted[k] = buffer[j]
		k++
		j++
	}

	i = start
	k = 0
	for i <= end {
		buffer[i] = sorted[k]
		i++
		k++
	}
}

func merge_sort(buffer []int, start, end int) {
	if end > start {
		middle := (end + start) / 2
		merge_sort(buffer, start, middle)
		merge_sort(buffer, middle+1, end)
		merge(buffer, start, middle, end)
	}
}

package main

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

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

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

	scanner.Scan()
	numbers := make([]int, n)
	for i, s := range strings.Split(scanner.Text(), " ") {
		numbers[i], _ = strconv.Atoi(s)
	}

	sort.Ints(numbers)
	var result int64
	for i, x := range numbers {
		result += int64(abs(x - (i + 1)))
	}

	fmt.Println(result)
}

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

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	scanner.Buffer(buf, maxCapacity)

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

	scanner.Scan()
	numbers := make([]int, n)
	input := scanner.Text()
	for i, s := range strings.Split(input, " ") {
		numbers[i], _ = strconv.Atoi(s)
	}

	sort.Ints(numbers)
	var result int64
	for i, x := range numbers {
		result += int64(abs(x - (i + 1)))
	}

	fmt.Println(result)
}

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

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	const maxCapacity = 512 * 1024
	buf := make([]byte, maxCapacity)
	scanner.Buffer(buf, maxCapacity)

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

	scanner.Scan()
	numbers := make([]int, n)
	input := scanner.Text()
	fmt.Println(len(strings.Split(input, " ")))
	for i, s := range strings.Split(input, " ") {
		numbers[i], _ = strconv.Atoi(s)
	}

	sort.Ints(numbers)
	var result int64
	for i, x := range numbers {
		result += int64(abs(x - (i + 1)))
	}

	fmt.Println(result)
}

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

package main

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

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

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

	scanner.Scan()
	numbers := make([]int, n)
	for i, s := range strings.Split(scanner.Text(), " ") {
		numbers[i], _ = strconv.Atoi(s)
	}

	sort.Ints(numbers)
	var result int64
	for i, x := range numbers {
		result += int64(math.Abs(float64(x - (i + 1))))
	}

	fmt.Println(result)
}

package main

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

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

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

	scanner.Scan()
	numbers := make([]int64, n)
	for i, s := range strings.Split(scanner.Text(), " ") {
		numbers[i], _ = strconv.ParseInt(s, 0, 64)
	}

	sort.Slice(numbers, func(i, j int) bool {
		return numbers[i] < numbers[j]
	})
	var result int64
	for i, x := range numbers {
		result += abs(x - (int64(i) + 1))
	}

	fmt.Println(result)
}

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

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	const maxCapacity = 512 * 1024
	buf := make([]byte, maxCapacity)
	scanner.Buffer(buf, maxCapacity)

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

	scanner.Scan()
	numbers := make([]int, n)
	input := scanner.Text()
	for i, s := range strings.Split(input, " ") {
		numbers[i], _ = strconv.Atoi(s)
	}

	sort.Ints(numbers)
	var result int64
	for i, x := range numbers {
		result += int64(abs(x - (i + 1)))
	}

	fmt.Println(result)
}

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

// 285C-mic
package main

import (
    "fmt"
    "math"
    "sort"
)

func main() {
    var n int
    var res int
    fmt.Scan(&n)
    a := make([]int, n)
    for i := 0; i < n; i++ {
        fmt.Scan(&a[i])
    }

    sort.Sort(sort.IntSlice(a))

    for i := 1; i <= n; i++ {
        res += int(math.Abs(float64(a[i-1] - (i))))
    }

    fmt.Println(res)

}