A k-multiple free set is a set of integers where there is no pair of integers where one is equal to another integer multiplied by k. That is, there are no two integers x and y (x < y) from the set, such that y = x·k.You're given a set of n distinct positive integers. Your task is to find the size of it's largest k-multiple free subset.

The first line of the input contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109). The next line contains a list of n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 109). All the numbers in the lines are separated by single spaces.

On the only line of the output print the size of the largest k-multiple free subset of {a1, a2, ..., an}.

NoteIn the sample input one of the possible maximum 2-multiple free subsets is {4, 5, 6}.

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    var n,k int
    fmt.Scan(&n,&k)
    a := make([]int,n)
    x := make(map[int64]bool)
    for i := 0; i < n; i++ {
        scanner.Scan()
        a[i], _ = strconv.Atoi(scanner.Text())
    }
    sort.Ints(a)
    ans := 0
    for i := n-1; i >= 0; i-- {
        if (a[i] > a[n-1]/k) || (x[int64(a[i])*int64(k)] == false) {
            ans++
            x[int64(a[i])] = true
        }
    }
    fmt.Println(ans)
}

package main

import (
	"bufio"
	"errors"
	"flag"
	"os"
	"sort"
	"strconv"
	"time"
)

// ******************************** CODE STARTS ********************************

var ()

func solve() {
	n := readInt()
	k := readInt()

	was := NewSet()
	array := make([]int, n)

	for i := 0; i < n; i++ {
		array[i] = readInt()
		if array[i]%k != 0 {
			was.Insert(Int(array[i]))
		}
	}

	sort.Sort(sort.IntSlice(array))

	for i := 0; i < n; i++ {
		if array[i]%k == 0 && !was.Find(Int(array[i]/k)) {
			was.Insert(Int(array[i]))
		}
	}

	printInt(was.Len())
	printLine()
}

type Int int

func (p Int) Equals(key Key) bool {
	return int(p) == int(key.(Int))
}

func (p Int) Less(key Key) bool {
	return int(p) < int(key.(Int))
}

// ********************************* CODE ENDS *********************************

// IO.

var (
	scanner *bufio.Scanner
	writer  *bufio.Writer
)

func main() {
	local := flag.Bool("local", false, "Defines whether run is local or remote.")
	flag.Parse()

	var (
		input  *os.File
		output *os.File
	)

	if *local {
		input, _ = os.Open("/home/outside/coding/workspace/go/io/input.txt")
		output, _ = os.Create("/home/outside/coding/workspace/go/io/output.txt")
		startTime := time.Now()
		defer func() {
			print("Time: ")
			print(strconv.FormatFloat(time.Since(startTime).Seconds(), 'f', 5, 64))
			println("s")
		}()
	} else {
		input = os.Stdin
		output = os.Stdout
		// input, _ = os.Open("")
		// output, _ = os.Create("")
	}
	defer input.Close()
	defer output.Close()

	scanner = bufio.NewScanner(input)
	writer = bufio.NewWriter(output)
	defer writer.Flush()

	scanner.Split(bufio.ScanWords)

	solve()
}

// Read.

func readString() string {
	scanner.Scan()
	ans := scanner.Text()
	return ans
}

func readLine() string {
	scanner.Split(bufio.ScanLines)
	defer scanner.Split(bufio.ScanWords)
	return readString()
}

func readInt() int {
	ans, _ := strconv.ParseInt(readString(), 10, 0)
	return int(ans)
}

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

func readFloat64() float64 {
	ans, _ := strconv.ParseFloat(readString(), 64)
	return ans
}

// Write.

func printString(value string) {
	writer.WriteString(value)
}

func printLine() {
	printString("\n")
}

func printInt(value int) {
	printString(strconv.FormatInt(int64(value), 10))
}

func printInt64(value int64) {
	printString(strconv.FormatInt(value, 10))
}

func printFLoat64(value float64) {
	printString(strconv.FormatFloat(value, 'f', 20, 64))
}

// Min, max, abs.

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

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

// Set.

type Key interface {
	Equals(key Key) bool
	Less(key Key) bool
}

type node struct {
	key   Key
	left  *node
	right *node
}

func newNode(key Key) *node {
	return &node{key, nil, nil}
}

type SplayTree struct {
	root *node
	tmp  *node
	len  int
}

func NewSplayTree() *SplayTree {
	return &SplayTree{nil, newNode(nil), 0}
}

func rotateLeft(x, p *node) {
	p.right = x.left
	x.left = p
}

func rotateRight(x, p *node) {
	p.left = x.right
	x.right = p
}

func (p *SplayTree) splay(x *node, key Key) *node {
	if x == nil {
		return nil
	}

	left := p.tmp
	right := p.tmp

	for {
		if key.Less(x.key) {
			y := x.left
			if y == nil {
				break
			}
			if key.Less(y.key) { // zig-zig
				rotateRight(y, x)
				x = y
				if x.left == nil {
					break
				}
			}
			// link right
			right.left = x
			right = x
			// move left
			x = x.left
		} else if x.key.Less(key) {
			y := x.right
			if y == nil {
				break
			}
			if y.key.Less(key) { // zig-zig
				rotateLeft(y, x)
				x = y
				if x.right == nil {
					break
				}
			}
			// link left
			left.right = x
			left = x
			// move right
			x = x.right
		} else {
			break
		}
	}

	left.right = x.left
	right.left = x.right
	x.left = p.tmp.right
	x.right = p.tmp.left

	return x
}

// left <= key < right
func (p *SplayTree) split(key Key) (left, right *node) {
	p.root = p.splay(p.root, key)
	if p.root.key.Equals(key) || p.root.key.Less(key) {
		right := p.root.right
		p.root.right = nil
		return p.root, right
	} else {
		left := p.root.left
		p.root.left = nil
		return left, p.root
	}
}

// keys from left tree must be less then keys from right tree
func (p *SplayTree) join(left, right *node) *node {
	if left == nil {
		return right
	} else if right == nil {
		return left
	}
	left = p.splay(left, right.key)
	left.right = right
	return left
}

type Set interface {
	Len() int
	Insert(key Key) error
	Find(key Key) bool
	Remove(key Key) error
}

func NewSet() Set {
	return Set(NewSplayTree())
}

func (p *SplayTree) Len() int {
	return p.len
}

func (p *SplayTree) Insert(key Key) error {
	if p.root == nil {
		p.root = newNode(key)
		p.len++
	} else {
		p.root = p.splay(p.root, key)
		if p.root.key.Equals(key) {
			return errors.New("Such key already exists")
		} else {
			left, right := p.split(key)
			p.root = newNode(key)
			p.root.left = left
			p.root.right = right
			p.len++
		}
	}
	return nil
}

func (p *SplayTree) Find(key Key) bool {
	if p.root == nil {
		return false
	}
	p.root = p.splay(p.root, key)
	return p.root.key.Equals(key)
}

func (p *SplayTree) Remove(key Key) error {
	p.root = p.splay(p.root, key)
	if p.root == nil || !p.root.key.Equals(key) {
		return errors.New("Such key doesn't exist")
	}
	p.root = p.join(p.split(key))
	return nil
}

package main

import (
	"bufio"
	"errors"
	"flag"
	"fmt"
	"os"
	"sort"
	"strconv"
	"time"
)

// ******************************** CODE STARTS ********************************

var ()

func solve() {
	n := readInt()
	k := readInt()

	was := NewSet()
	array := make([]int, n)

	for i := 0; i < n; i++ {
		array[i] = readInt()
		if array[i]%k != 0 {
			was.Insert(Int(array[i]))
		}
	}

	sort.Sort(sort.IntSlice(array))

	for i := 0; i < n; i++ {
		if array[i]%k == 0 && !was.Find(Int(array[i]/k)) {
			was.Insert(Int(array[i]))
		}
	}

	printInt(was.Len())
	printString("\n")

}

type Int int

func (p Int) Equals(key Key) bool {
	return int(p) == int(key.(Int))
}

func (p Int) Less(key Key) bool {
	return int(p) < int(key.(Int))
}

// ********************************* CODE ENDS *********************************

// IO.

var (
	scanner *bufio.Scanner
	writer  *bufio.Writer
)

func main() {
	local := flag.Bool("local", false, "Defines whether run is local or remote.")
	flag.Parse()

	var (
		input  *os.File
		output *os.File
	)

	if *local {
		input, _ = os.Open("/home/outside/coding/workspace/go/io/input.txt")
		output, _ = os.Create("/home/outside/coding/workspace/go/io/output.txt")
		startTime := time.Now()
		defer func() {
			println("Time: " +
				fmt.Sprintf("%f", time.Since(startTime).Seconds()) + "s")
		}()
	} else {
		input = os.Stdin
		output = os.Stdout
		// input, _ = os.Open("")
		// output, _ = os.Create("")
	}
	defer input.Close()
	defer output.Close()

	scanner = bufio.NewScanner(input)
	writer = bufio.NewWriter(output)
	defer writer.Flush()

	scanner.Split(bufio.ScanWords)

	solve()
}

// Read.

func readString() string {
	scanner.Scan()
	ans := scanner.Text()
	return ans
}

func readInt() int {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 0)
	return int(ans)
}

func readInt32() int32 {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 32)
	return int32(ans)
}

func readInt64() int64 {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return int64(ans)
}

// Write.

func printString(value string) {
	writer.WriteString(value)
}

func printInt(value int) {
	writer.WriteString(strconv.FormatInt(int64(value), 10))
}

// Min, max, abs.

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

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

// Set.

type Key interface {
	Equals(key Key) bool
	Less(key Key) bool
}

type node struct {
	key   Key
	left  *node
	right *node
}

func newNode(key Key) *node {
	return &node{key, nil, nil}
}

type SplayTree struct {
	root *node
	tmp  *node
	len  int
}

func NewSplayTree() *SplayTree {
	return &SplayTree{nil, newNode(nil), 0}
}

func rotateLeft(x, p *node) {
	p.right = x.left
	x.left = p
}

func rotateRight(x, p *node) {
	p.left = x.right
	x.right = p
}

func (p *SplayTree) splay(x *node, key Key) *node {
	if x == nil {
		return nil
	}

	left := p.tmp
	right := p.tmp

	for {
		if key.Less(x.key) {
			y := x.left
			if y == nil {
				break
			}
			if key.Less(y.key) { // zig-zig
				rotateRight(y, x)
				x = y
				if x.left == nil {
					break
				}
			}
			// link right
			right.left = x
			right = x
			// move left
			x = x.left
		} else if x.key.Less(key) {
			y := x.right
			if y == nil {
				break
			}
			if y.key.Less(key) { // zig-zig
				rotateLeft(y, x)
				x = y
				if x.right == nil {
					break
				}
			}
			// link left
			left.right = x
			left = x
			// move right
			x = x.right
		} else {
			break
		}
	}

	left.right = x.left
	right.left = x.right
	x.left = p.tmp.right
	x.right = p.tmp.left

	return x
}

// key should not be present in the tree
// left <= key < right
func (p *SplayTree) split(key Key) (left, right *node) {
	p.root = p.splay(p.root, key)
	if p.root.key.Equals(key) || p.root.key.Less(key) {
		right := p.root.right
		p.root.right = nil
		return p.root, right
	} else {
		left := p.root.left
		p.root.left = nil
		return left, p.root
	}
}

// keys from left tree must be less then keys from right tree
func (p *SplayTree) join(left, right *node) *node {
	if left == nil {
		return right
	} else if right == nil {
		return left
	}
	left = p.splay(left, right.key)
	left.right = right
	return left
}

type Set interface {
	Len() int
	Insert(key Key) error
	Find(key Key) bool
	Remove(key Key) error
}

func NewSet() Set {
	return Set(NewSplayTree())
}

func (p *SplayTree) Len() int {
	return p.len
}

func (p *SplayTree) Insert(key Key) error {
	if p.root == nil {
		p.root = newNode(key)
		p.len++
	} else {
		p.root = p.splay(p.root, key)
		if p.root.key.Equals(key) {
			return errors.New("Such key already exists")
		} else {
			left, right := p.split(key)
			p.root = newNode(key)
			p.root.left = left
			p.root.right = right
			p.len++
		}
	}
	return nil
}

func (p *SplayTree) Find(key Key) bool {
	if p.root == nil {
		return false
	}
	p.root = p.splay(p.root, key)
	return p.root.key.Equals(key)
}

func (p *SplayTree) Remove(key Key) error {
	p.root = p.splay(p.root, key)
	if p.root == nil || !p.root.key.Equals(key) {
		return errors.New("Such key doesn't exist")
	}
	p.root = p.join(p.split(key))
	return nil
}

package main

import (
	"bufio"
	"errors"
	"flag"
	"os"
	"sort"
	"strconv"
)

// ******************************** CODE STARTS ********************************

var ()

func solve() {
	n := readInt()
	k := readInt()

	was := NewSet()
	array := make([]int, n)

	for i := 0; i < n; i++ {
		array[i] = readInt()
		if array[i]%k != 0 {
			was.Insert(Int{array[i]})
		}
	}

	sort.Sort(sort.IntSlice(array))

	for i := 0; i < n; i++ {
		if array[i]%k == 0 && !was.Find(Int{array[i]/k}) {
			was.Insert(Int{array[i]})
		}
	}

	printInt(was.Len())
	printString("\n")

}

type Int struct {
	value int
}

func (p Int) Equals(key Key) bool {
	return p.value == key.(Int).value
}

func (p Int) Less(key Key) bool {
	return p.value < key.(Int).value
}

// ********************************* CODE ENDS *********************************

// IO.

var (
	scanner *bufio.Scanner
	writer  *bufio.Writer
)

func main() {
	local := flag.Bool("local", false, "Defines whether run is local or remote.")
	flag.Parse()

	var (
		input  *os.File
		output *os.File
	)

	if *local {
		input, _ = os.Open("/home/outside/coding/workspace/go/io/input.txt")
		output, _ = os.Create("/home/outside/coding/workspace/go/io/output.txt")
	} else {
		input = os.Stdin
		output = os.Stdout
		// input, _ = os.Open("")
		// output, _ = os.Create("")
	}

	scanner = bufio.NewScanner(input)
	writer = bufio.NewWriter(output)

	defer input.Close()
	defer output.Close()
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	solve()
}

// Read.

func readString() string {
	scanner.Scan()
	ans := scanner.Text()
	return ans
}

func readInt() int {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 0)
	return int(ans)
}

func readInt32() int32 {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 32)
	return int32(ans)
}

func readInt64() int64 {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return int64(ans)
}

// Write.

func printString(value string) {
	writer.WriteString(value)
}

func printInt(value int) {
	writer.WriteString(strconv.FormatInt(int64(value), 10))
}

// Min, max, abs.

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

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

// Set.

type Key interface {
	Equals(key Key) bool
	Less(key Key) bool
}

type node struct {
	key   Key
	left  *node
	right *node
}

func newNode(key Key) *node {
	return &node{key, nil, nil}
}

type SplayTree struct {
	root *node
	tmp  *node
	len  int
}

func NewSplayTree() *SplayTree {
	return &SplayTree{nil, newNode(nil), 0}
}

func rotateLeft(x, p *node) {
	p.right = x.left
	x.left = p
}

func rotateRight(x, p *node) {
	p.left = x.right
	x.right = p
}

func (p *SplayTree) splay(x *node, key Key) *node {
	if x == nil {
		return nil
	}

	left := p.tmp
	right := p.tmp

	for {
		if key.Less(x.key) {
			y := x.left
			if y == nil {
				break
			}
			if key.Less(y.key) { // zig-zig
				rotateRight(y, x)
				x = y
				if x.left == nil {
					break
				}
			}
			// link right
			right.left = x
			right = x
			// move left
			x = x.left
		} else if x.key.Less(key) {
			y := x.right
			if y == nil {
				break
			}
			if y.key.Less(key) { // zig-zig
				rotateLeft(y, x)
				x = y
				if x.right == nil {
					break
				}
			}
			// link left
			left.right = x
			left = x
			// move right
			x = x.right
		} else {
			break
		}
	}

	left.right = x.left
	right.left = x.right
	x.left = p.tmp.right
	x.right = p.tmp.left

	return x
}

// key should not be present in the tree
// left <= key < right
func (p *SplayTree) split(key Key) (left, right *node) {
	p.root = p.splay(p.root, key)
	if p.root.key.Equals(key) || p.root.key.Less(key) {
		right := p.root.right
		p.root.right = nil
		return p.root, right
	} else {
		left := p.root.left
		p.root.left = nil
		return left, p.root
	}
}

// keys from left tree must be less then keys from right tree
func (p *SplayTree) join(left, right *node) *node {
	if left == nil {
		return right
	} else if right == nil {
		return left
	}
	left = p.splay(left, right.key)
	left.right = right
	return left
}

type Set interface {
	Len() int
	Insert(key Key) error
	Find(key Key) bool
	Remove(key Key) error
}

func NewSet() Set {
	return Set(NewSplayTree())
}

func (p *SplayTree) Len() int {
	return p.len
}

func (p *SplayTree) Insert(key Key) error {
	if p.root == nil {
		p.root = newNode(key)
		p.len++
	} else {
		p.root = p.splay(p.root, key)
		if p.root.key.Equals(key) {
			return errors.New("Such key already exists")
		} else {
			left, right := p.split(key)
			p.root = newNode(key)
			p.root.left = left
			p.root.right = right
			p.len++
		}
	}
	return nil
}

func (p *SplayTree) Find(key Key) bool {
	if p.root == nil {
		return false
	}
	p.root = p.splay(p.root, key)
	return p.root.key.Equals(key)
}

func (p *SplayTree) Remove(key Key) error {
	p.root = p.splay(p.root, key)
	if p.root == nil || !p.root.key.Equals(key) {
		return errors.New("Such key doesn't exist")
	}
	p.root = p.join(p.split(key))
	return nil
}

package main

import (
	"bufio"
	"errors"
	"flag"
	"fmt"
	"os"
	"sort"
	"strconv"
	"time"
)

// ******************************** CODE STARTS ********************************

var ()

func solve() {
	n := readInt()
	k := readInt()

	was := NewSet()
	array := make([]int, n)

	for i := 0; i < n; i++ {
		array[i] = readInt()
		if array[i]%k != 0 {
			was.Insert(Int(array[i]))
		}
	}

	sort.Sort(sort.IntSlice(array))

	for i := 0; i < n; i++ {
		if array[i]%k == 0 && !was.Find(Int(array[i]/k)) {
			was.Insert(Int(array[i]))
		}
	}

	printInt(was.Len())
	printString("\n")

}

type Int int

func (p Int) Equals(key Key) bool {
	return int(p) == int(key.(Int))
}

func (p Int) Less(key Key) bool {
	return int(p) < int(key.(Int))
}

// ********************************* CODE ENDS *********************************

// IO.

var (
	scanner *bufio.Scanner
	writer  *bufio.Writer
)

func main() {
	local := flag.Bool("local", false, "Defines whether run is local or remote.")
	flag.Parse()

	var (
		input  *os.File
		output *os.File
	)

	if *local {
		input, _ = os.Open("/home/outside/coding/workspace/go/io/input.txt")
		output, _ = os.Create("/home/outside/coding/workspace/go/io/output.txt")
		startTime := time.Now()
		defer func() {
			println(fmt.Sprintf("Time: %fs", time.Since(startTime).Seconds()))
		}()
	} else {
		input = os.Stdin
		output = os.Stdout
		// input, _ = os.Open("")
		// output, _ = os.Create("")
	}
	defer input.Close()
	defer output.Close()

	scanner = bufio.NewScanner(input)
	writer = bufio.NewWriter(output)
	defer writer.Flush()

	scanner.Split(bufio.ScanWords)

	solve()
}

// Read.

func readString() string {
	scanner.Scan()
	ans := scanner.Text()
	return ans
}

func readInt() int {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 0)
	return int(ans)
}

func readInt32() int32 {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 32)
	return int32(ans)
}

func readInt64() int64 {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return int64(ans)
}

// Write.

func printString(value string) {
	writer.WriteString(value)
}

func printInt(value int) {
	writer.WriteString(strconv.FormatInt(int64(value), 10))
}

// Min, max, abs.

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

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

// Set.

type Key interface {
	Equals(key Key) bool
	Less(key Key) bool
}

type node struct {
	key   Key
	left  *node
	right *node
}

func newNode(key Key) *node {
	return &node{key, nil, nil}
}

type SplayTree struct {
	root *node
	tmp  *node
	len  int
}

func NewSplayTree() *SplayTree {
	return &SplayTree{nil, newNode(nil), 0}
}

func rotateLeft(x, p *node) {
	p.right = x.left
	x.left = p
}

func rotateRight(x, p *node) {
	p.left = x.right
	x.right = p
}

func (p *SplayTree) splay(x *node, key Key) *node {
	if x == nil {
		return nil
	}

	left := p.tmp
	right := p.tmp

	for {
		if key.Less(x.key) {
			y := x.left
			if y == nil {
				break
			}
			if key.Less(y.key) { // zig-zig
				rotateRight(y, x)
				x = y
				if x.left == nil {
					break
				}
			}
			// link right
			right.left = x
			right = x
			// move left
			x = x.left
		} else if x.key.Less(key) {
			y := x.right
			if y == nil {
				break
			}
			if y.key.Less(key) { // zig-zig
				rotateLeft(y, x)
				x = y
				if x.right == nil {
					break
				}
			}
			// link left
			left.right = x
			left = x
			// move right
			x = x.right
		} else {
			break
		}
	}

	left.right = x.left
	right.left = x.right
	x.left = p.tmp.right
	x.right = p.tmp.left

	return x
}

// key should not be present in the tree
// left <= key < right
func (p *SplayTree) split(key Key) (left, right *node) {
	p.root = p.splay(p.root, key)
	if p.root.key.Equals(key) || p.root.key.Less(key) {
		right := p.root.right
		p.root.right = nil
		return p.root, right
	} else {
		left := p.root.left
		p.root.left = nil
		return left, p.root
	}
}

// keys from left tree must be less then keys from right tree
func (p *SplayTree) join(left, right *node) *node {
	if left == nil {
		return right
	} else if right == nil {
		return left
	}
	left = p.splay(left, right.key)
	left.right = right
	return left
}

type Set interface {
	Len() int
	Insert(key Key) error
	Find(key Key) bool
	Remove(key Key) error
}

func NewSet() Set {
	return Set(NewSplayTree())
}

func (p *SplayTree) Len() int {
	return p.len
}

func (p *SplayTree) Insert(key Key) error {
	if p.root == nil {
		p.root = newNode(key)
		p.len++
	} else {
		p.root = p.splay(p.root, key)
		if p.root.key.Equals(key) {
			return errors.New("Such key already exists")
		} else {
			left, right := p.split(key)
			p.root = newNode(key)
			p.root.left = left
			p.root.right = right
			p.len++
		}
	}
	return nil
}

func (p *SplayTree) Find(key Key) bool {
	if p.root == nil {
		return false
	}
	p.root = p.splay(p.root, key)
	return p.root.key.Equals(key)
}

func (p *SplayTree) Remove(key Key) error {
	p.root = p.splay(p.root, key)
	if p.root == nil || !p.root.key.Equals(key) {
		return errors.New("Such key doesn't exist")
	}
	p.root = p.join(p.split(key))
	return nil
}

package main

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

// ******************************** CODE STARTS ********************************

var ()

func solve() {
	n := readInt()
	k := readInt()

	was := make(map[int]bool)
	array := make([]int, n)

	for i := 0; i < n; i++ {
		array[i] = readInt()
		if array[i]%k != 0 {
			was[array[i]] = true
		}
	}

	sort.Ints(array)

	for i := 0; i < n; i++ {
		_, present := was[array[i]/k]
		if array[i]%k == 0 && !present {
			was[array[i]] = true
		}
	}

	printInt(len(was))

}

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

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

// ********************************* CODE ENDS *********************************

var (
	scanner *bufio.Scanner
	writer  *bufio.Writer
)

func main() {
	local := flag.Bool("local", false, "Defines whether run is local or remote.")
	flag.Parse()

	var (
		input  *os.File
		output *os.File
	)

	if *local {
		input, _ = os.Open("/home/outside/coding/workspace/go/io/input.txt")
		output, _ = os.Create("/home/outside/coding/workspace/go/io/output.txt")
	} else {
		input = os.Stdin
		output = os.Stdout
		// input, _ = os.Open("")
		// output, _ = os.Create("")
	}

	scanner = bufio.NewScanner(input)
	writer = bufio.NewWriter(output)

	defer input.Close()
	defer output.Close()
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	solve()
}

func readString() string {
	scanner.Scan()
	ans := scanner.Text()
	return ans
}

func readInt() int {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 0)
	return int(ans)
}

func readInt32() int32 {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 32)
	return int32(ans)
}

func readInt64() int64 {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return int64(ans)
}

func printString(value string) {
	writer.WriteString(value)
}

func printInt(value int) {
	writer.WriteString(strconv.FormatInt(int64(value), 10))
}

package main

import (
	"bufio"
	"errors"
	"flag"
	"os"
	"sort"
	"strconv"
)

// ******************************** CODE STARTS ********************************

var ()

func solve() {
	n := readInt()
	k := readInt()

	was := NewSet()
	array := make([]int, n)

	for i := 0; i < n; i++ {
		array[i] = readInt()
		if array[i]%k != 0 {
			was.Insert(Int(array[i]))
		}
	}

	sort.Ints(array)

	for i := 0; i < n; i++ {
		if array[i]%k == 0 && !was.Find(Int(array[i]/k)) {
			was.Insert(Int(array[i]))
		}
	}

	printInt(was.Len())
	printString("\n")

}

type Int int

func (p Int) Equals(key Key) bool {
	return int(p) == int(key.(Int))
}

func (p Int) Less(key Key) bool {
	return int(p) < int(key.(Int))
}

// ********************************* CODE ENDS *********************************

// IO.

var (
	scanner *bufio.Scanner
	writer  *bufio.Writer
)

func main() {
	local := flag.Bool("local", false, "Defines whether run is local or remote.")
	flag.Parse()

	var (
		input  *os.File
		output *os.File
	)

	if *local {
		input, _ = os.Open("/home/outside/coding/workspace/go/io/input.txt")
		output, _ = os.Create("/home/outside/coding/workspace/go/io/output.txt")
	} else {
		input = os.Stdin
		output = os.Stdout
		// input, _ = os.Open("")
		// output, _ = os.Create("")
	}

	scanner = bufio.NewScanner(input)
	writer = bufio.NewWriter(output)

	defer input.Close()
	defer output.Close()
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	solve()
}

// Read.

func readString() string {
	scanner.Scan()
	ans := scanner.Text()
	return ans
}

func readInt() int {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 0)
	return int(ans)
}

func readInt32() int32 {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 32)
	return int32(ans)
}

func readInt64() int64 {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return int64(ans)
}

// Write.

func printString(value string) {
	writer.WriteString(value)
}

func printInt(value int) {
	writer.WriteString(strconv.FormatInt(int64(value), 10))
}

// Min, max, abs.

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

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

// Set.

type Key interface {
	Equals(key Key) bool
	Less(key Key) bool
}

type node struct {
	key   Key
	left  *node
	right *node
}

func newNode(key Key) *node {
	return &node{key, nil, nil}
}

type SplayTree struct {
	root *node
	tmp  *node
	len  int
}

func NewSplayTree() *SplayTree {
	return &SplayTree{nil, newNode(nil), 0}
}

func rotateLeft(x, p *node) {
	p.right = x.left
	x.left = p
}

func rotateRight(x, p *node) {
	p.left = x.right
	x.right = p
}

func (p *SplayTree) splay(x *node, key Key) *node {
	if x == nil {
		return nil
	}

	left := p.tmp
	right := p.tmp

	for {
		if key.Less(x.key) {
			y := x.left
			if y == nil {
				break
			}
			if key.Less(y.key) { // zig-zig
				rotateRight(y, x)
				x = y
				if x.left == nil {
					break
				}
			}
			// link right
			right.left = x
			right = x
			// move left
			x = x.left
		} else if x.key.Less(key) {
			y := x.right
			if y == nil {
				break
			}
			if y.key.Less(key) { // zig-zig
				rotateLeft(y, x)
				x = y
				if x.right == nil {
					break
				}
			}
			// link left
			left.right = x
			left = x
			// move right
			x = x.right
		} else {
			break
		}
	}

	left.right = x.left
	right.left = x.right
	x.left = p.tmp.right
	x.right = p.tmp.left

	return x
}

// key should not be present in the tree
// left <= key < right
func (p *SplayTree) split(key Key) (left, right *node) {
	p.root = p.splay(p.root, key)
	if p.root.key.Equals(key) || p.root.key.Less(key) {
		right := p.root.right
		p.root.right = nil
		return p.root, right
	} else {
		left := p.root.left
		p.root.left = nil
		return left, p.root
	}
}

// keys from left tree must be less then keys from right tree
func (p *SplayTree) join(left, right *node) *node {
	if left == nil {
		return right
	} else if right == nil {
		return left
	}
	left = p.splay(left, right.key)
	left.right = right
	return left
}

type Set interface {
	Len() int
	Insert(key Key) error
	Find(key Key) bool
	Remove(key Key) error
}

func NewSet() Set {
	return Set(NewSplayTree())
}

func (p *SplayTree) Len() int {
	return p.len
}

func (p *SplayTree) Insert(key Key) error {
	if p.root == nil {
		p.root = newNode(key)
		p.len++
	} else {
		p.root = p.splay(p.root, key)
		if p.root.key.Equals(key) {
			return errors.New("Such key already exists")
		} else {
			left, right := p.split(key)
			p.root = newNode(key)
			p.root.left = left
			p.root.right = right
			p.len++
		}
	}
	return nil
}

func (p *SplayTree) Find(key Key) bool {
	if p.root == nil {
		return false
	}
	p.root = p.splay(p.root, key)
	return p.root.key.Equals(key)
}

func (p *SplayTree) Remove(key Key) error {
	p.root = p.splay(p.root, key)
	if p.root == nil || !p.root.key.Equals(key) {
		return errors.New("Such key doesn't exist")
	}
	p.root = p.join(p.split(key))
	return nil
}

package main

import (
	"bufio"
	"errors"
	"flag"
	"os"
	"sort"
	"strconv"
)

// ******************************** CODE STARTS ********************************

var ()

func solve() {
	n := readInt()
	k := readInt()

	was := NewSet()
	array := make([]int, n)

	for i := 0; i < n; i++ {
		array[i] = readInt()
		if array[i]%k != 0 {
			was.Insert(Int(array[i]))
		}
	}

	sort.Sort(sort.IntSlice(array))

	for i := 0; i < n; i++ {
		if array[i]%k == 0 && !was.Find(Int(array[i]/k)) {
			was.Insert(Int(array[i]))
		}
	}

	printInt(was.Len())
	printString("\n")

}

type Int int

func (p Int) Equals(key Key) bool {
	return int(p) == int(key.(Int))
}

func (p Int) Less(key Key) bool {
	return int(p) < int(key.(Int))
}

// ********************************* CODE ENDS *********************************

// IO.

var (
	scanner *bufio.Scanner
	writer  *bufio.Writer
)

func main() {
	local := flag.Bool("local", false, "Defines whether run is local or remote.")
	flag.Parse()

	var (
		input  *os.File
		output *os.File
	)

	if *local {
		input, _ = os.Open("/home/outside/coding/workspace/go/io/input.txt")
		output, _ = os.Create("/home/outside/coding/workspace/go/io/output.txt")
	} else {
		input = os.Stdin
		output = os.Stdout
		// input, _ = os.Open("")
		// output, _ = os.Create("")
	}

	scanner = bufio.NewScanner(input)
	writer = bufio.NewWriter(output)

	defer input.Close()
	defer output.Close()
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	solve()
}

// Read.

func readString() string {
	scanner.Scan()
	ans := scanner.Text()
	return ans
}

func readInt() int {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 0)
	return int(ans)
}

func readInt32() int32 {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 32)
	return int32(ans)
}

func readInt64() int64 {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return int64(ans)
}

// Write.

func printString(value string) {
	writer.WriteString(value)
}

func printInt(value int) {
	writer.WriteString(strconv.FormatInt(int64(value), 10))
}

// Min, max, abs.

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

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

// Set.

type Key interface {
	Equals(key Key) bool
	Less(key Key) bool
}

type node struct {
	key   Key
	left  *node
	right *node
}

func newNode(key Key) *node {
	return &node{key, nil, nil}
}

type SplayTree struct {
	root *node
	tmp  *node
	len  int
}

func NewSplayTree() *SplayTree {
	return &SplayTree{nil, newNode(nil), 0}
}

func rotateLeft(x, p *node) {
	p.right = x.left
	x.left = p
}

func rotateRight(x, p *node) {
	p.left = x.right
	x.right = p
}

func (p *SplayTree) splay(x *node, key Key) *node {
	if x == nil {
		return nil
	}

	left := p.tmp
	right := p.tmp

	for {
		if key.Less(x.key) {
			y := x.left
			if y == nil {
				break
			}
			if key.Less(y.key) { // zig-zig
				rotateRight(y, x)
				x = y
				if x.left == nil {
					break
				}
			}
			// link right
			right.left = x
			right = x
			// move left
			x = x.left
		} else if x.key.Less(key) {
			y := x.right
			if y == nil {
				break
			}
			if y.key.Less(key) { // zig-zig
				rotateLeft(y, x)
				x = y
				if x.right == nil {
					break
				}
			}
			// link left
			left.right = x
			left = x
			// move right
			x = x.right
		} else {
			break
		}
	}

	left.right = x.left
	right.left = x.right
	x.left = p.tmp.right
	x.right = p.tmp.left

	return x
}

// key should not be present in the tree
// left <= key < right
func (p *SplayTree) split(key Key) (left, right *node) {
	p.root = p.splay(p.root, key)
	if p.root.key.Equals(key) || p.root.key.Less(key) {
		right := p.root.right
		p.root.right = nil
		return p.root, right
	} else {
		left := p.root.left
		p.root.left = nil
		return left, p.root
	}
}

// keys from left tree must be less then keys from right tree
func (p *SplayTree) join(left, right *node) *node {
	if left == nil {
		return right
	} else if right == nil {
		return left
	}
	left = p.splay(left, right.key)
	left.right = right
	return left
}

type Set interface {
	Len() int
	Insert(key Key) error
	Find(key Key) bool
	Remove(key Key) error
}

func NewSet() Set {
	return Set(NewSplayTree())
}

func (p *SplayTree) Len() int {
	return p.len
}

func (p *SplayTree) Insert(key Key) error {
	if p.root == nil {
		p.root = newNode(key)
		p.len++
	} else {
		p.root = p.splay(p.root, key)
		if p.root.key.Equals(key) {
			return errors.New("Such key already exists")
		} else {
			left, right := p.split(key)
			p.root = newNode(key)
			p.root.left = left
			p.root.right = right
			p.len++
		}
	}
	return nil
}

func (p *SplayTree) Find(key Key) bool {
	if p.root == nil {
		return false
	}
	p.root = p.splay(p.root, key)
	return p.root.key.Equals(key)
}

func (p *SplayTree) Remove(key Key) error {
	p.root = p.splay(p.root, key)
	if p.root == nil || !p.root.key.Equals(key) {
		return errors.New("Such key doesn't exist")
	}
	p.root = p.join(p.split(key))
	return nil
}

package main

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

// ******************************** CODE STARTS ********************************

var ()

func solve() {
	n := readInt()
	k := readInt()

	was := make(map[int]bool)
	array := make([]int, n)

	for i := 0; i < n; i++ {
		array[i] = readInt()
		if array[i]%k != 0 {
			was[array[i]] = true
		}
	}

	sort.Sort(sort.IntSlice(array))

	for i := 0; i < n; i++ {
		_, present := was[array[i]/k]
		if array[i]%k == 0 && !present {
			was[array[i]] = true
		}
	}

	printInt(len(was))

}

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

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

// ********************************* CODE ENDS *********************************

var (
	scanner *bufio.Scanner
	writer  *bufio.Writer
)

func main() {
	local := flag.Bool("local", false, "Defines whether run is local or remote.")
	flag.Parse()

	var (
		input  *os.File
		output *os.File
	)

	if *local {
		input, _ = os.Open("/home/outside/coding/workspace/go/io/input.txt")
		output, _ = os.Create("/home/outside/coding/workspace/go/io/output.txt")
	} else {
		input = os.Stdin
		output = os.Stdout
		// input, _ = os.Open("")
		// output, _ = os.Create("")
	}

	scanner = bufio.NewScanner(input)
	writer = bufio.NewWriter(output)

	defer input.Close()
	defer output.Close()
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	solve()
}

func readString() string {
	scanner.Scan()
	ans := scanner.Text()
	return ans
}

func readInt() int {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 0)
	return int(ans)
}

func readInt32() int32 {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 32)
	return int32(ans)
}

func readInt64() int64 {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return int64(ans)
}

func printString(value string) {
	writer.WriteString(value)
}

func printInt(value int) {
	writer.WriteString(strconv.FormatInt(int64(value), 10))
}

package main

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

// ******************************** CODE STARTS ********************************

var ()

func solve() {
	n := readInt()
	k := readInt()

	was := make(map[int]bool)
	array := make([]int, 0)

	for i := 0; i < n; i++ {
		array = append(array, readInt())
		if array[i]%k != 0 {
			was[array[i]] = true
		}
	}

	sort.Sort(sort.IntSlice(array))

	for i := 0; i < n; i++ {
		_, present := was[array[i]/k]
		if array[i]%k == 0 && !present {
			was[array[i]] = true
		}
	}

	printInt(len(was))

}

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

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

// ********************************* CODE ENDS *********************************

var (
	scanner *bufio.Scanner
	writer  *bufio.Writer
)

func main() {
	local := flag.Bool("local", false, "Defines whether run is local or remote.")
	flag.Parse()

	var (
		input  *os.File
		output *os.File
	)

	if *local {
		input, _ = os.Open("/home/outside/coding/workspace/go/io/input.txt")
		output, _ = os.Create("/home/outside/coding/workspace/go/io/output.txt")
	} else {
		input = os.Stdin
		output = os.Stdout
		// input, _ = os.Open("")
		// output, _ = os.Create("")
	}

	scanner = bufio.NewScanner(input)
	writer = bufio.NewWriter(output)

	defer input.Close()
	defer output.Close()
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	solve()
}

func readString() string {
	scanner.Scan()
	ans := scanner.Text()
	return ans
}

func readInt() int {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 0)
	return int(ans)
}

func readInt32() int32 {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 32)
	return int32(ans)
}

func readInt64() int64 {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return int64(ans)
}

func printString(value string) {
	writer.WriteString(value)
}

func printInt(value int) {
	writer.WriteString(strconv.FormatInt(int64(value), 10))
}

// 274A-mic
package main

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

func main() {
    var sum, n, k int
    z := bufio.NewReader(os.Stdin)
    fmt.Scan(&n, &k)
    a := make([]int, n)
    p := make(map[int]int, n)
    for i := 0; i < n; i++ {
        fmt.Fscan(z, &a[i])
    }
    sort.Sort(sort.IntSlice(a))
    sum = 0
    for i := 0; i < n; i++ {
        if a[i]%k != 0 || (p[a[i]/k]) == 0 {
            sum++
            p[a[i]] = 1
        }
    }
    fmt.Println(sum)
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/274/A

package main

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

func Solve(n int, k int, a []int) int {
	S := make(map[int]bool)
	sort.Ints(a)
	for _, elem := range a {
		if elem%k != 0 {
			S[elem] = true
		} else if elem%k == 0 && !S[elem/k] {
			S[elem] = true
		}
	}
	return len(S)
}

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

	var n, k int
	fmt.Fscanf(reader, "%d %d\n", &n, &k)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, k, a))
}

package main

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

// ******************************** CODE STARTS ********************************

var ()

func solve() {
	n := readInt()
	k := readInt()

	was := make(map[int]bool)
	array := make([]int, n)

	for i := 0; i < n; i++ {
		array[i] = readInt()
		if array[i]%k != 0 {
			was[array[i]] = true
		}
	}

	sort.Sort(sort.IntSlice(array))

	for i := 0; i < n; i++ {
		_, present := was[array[i]/k]
		if array[i]%k == 0 && !present {
			was[array[i]] = true
		}
	}

	printInt(len(was))

}

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

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

// ********************************* CODE ENDS *********************************

var (
	scanner *bufio.Scanner
	writer  *bufio.Writer
)

func main() {
	local := flag.Bool("local", false, "Defines whether run is local or remote.")
	flag.Parse()

	var (
		input  *os.File
		output *os.File
	)

	if *local {
		input, _ = os.Open("/home/outside/coding/workspace/go/io/input.txt")
		output, _ = os.Create("/home/outside/coding/workspace/go/io/output.txt")
	} else {
		// input = os.Stdin
		// output = os.Stdout
		input, _ = os.Open("war.in")
		output, _ = os.Create("war.out")
	}

	scanner = bufio.NewScanner(input)
	writer = bufio.NewWriter(output)

	defer input.Close()
	defer output.Close()
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	solve()
}

func readString() string {
	scanner.Scan()
	ans := scanner.Text()
	return ans
}

func readInt() int {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 0)
	return int(ans)
}

func readInt32() int32 {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 32)
	return int32(ans)
}

func readInt64() int64 {
	scanner.Scan()
	ans, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return int64(ans)
}

func printString(value string) {
	writer.WriteString(value)
}

func printInt(value int) {
	writer.WriteString(strconv.FormatInt(int64(value), 10))
}


You have a string $$$s$$$ consisting of digits from $$$0$$$ to $$$9$$$ inclusive. You can perform the following operation any (possibly zero) number of times:   You can choose a position $$$i$$$ and delete a digit $$$d$$$ on the $$$i$$$-th position. Then insert the digit $$$\min{(d + 1, 9)}$$$ on any position (at the beginning, at the end or in between any two adjacent digits). What is the lexicographically smallest string you can get by performing these operations?A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ of the same length if and only if the following holds:   in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a smaller digit than the corresponding digit in $$$b$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then the test cases follow. Each test case consists of a single line that contains one string $$$s$$$ ($$$1 \le |s| \le 2 \cdot 10^5$$$) — the string consisting of digits. Please note that $$$s$$$ is just a string consisting of digits, so leading zeros are allowed. It is guaranteed that the sum of lengths of $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Print a single string — the minimum string that is possible to obtain.

NoteIn the first test case:   Delete $$$8$$$ and insert $$$9$$$ at the end of the notation. The resulting notation is $$$04299$$$.  Delete $$$4$$$ and insert $$$5$$$ in the $$$3$$$-rd position of the notation. The resulting notation is $$$02599$$$. Nothing needs to be done in the second and third test cases.

package main

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

const N = 3e5
const inf = 1e9

var iost *Iost

type Iost struct {
	Scanner *bufio.Scanner
	Writer  *bufio.Writer
}

func NewIost(fp io.Reader, wfp io.Writer) *Iost {
	const BufSize = 2000005
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, BufSize), BufSize)
	return &Iost{Scanner: scanner, Writer: bufio.NewWriter(wfp)}
}
func (i *Iost) Text() string {
	if !i.Scanner.Scan() {
		panic("scan failed")
	}
	return i.Scanner.Text()
}
func (i *Iost) Atoi(s string) int                 { x, _ := strconv.Atoi(s); return x }
func (i *Iost) GetNextInt() int                   { return i.Atoi(i.Text()) }
func (i *Iost) Atoi64(s string) int64             { x, _ := strconv.ParseInt(s, 10, 64); return x }
func (i *Iost) GetNextInt64() int64               { return i.Atoi64(i.Text()) }
func (i *Iost) Atof64(s string) float64           { x, _ := strconv.ParseFloat(s, 64); return x }
func (i *Iost) GetNextFloat64() float64           { return i.Atof64(i.Text()) }
func (i *Iost) Print(x ...interface{})            { fmt.Fprint(i.Writer, x...) }
func (i *Iost) Printf(s string, x ...interface{}) { fmt.Fprintf(i.Writer, s, x...) }
func (i *Iost) Println(x ...interface{})          { fmt.Fprintln(i.Writer, x...) }

func BAAL() {
	in := os.Stdin
	out := os.Stdout
	iost = NewIost(in, out)
}

func karo(a *[]int, dig int, v *[]int) {
	n := len(*a)
	if n == 0 {
		return
	}
	last := -1
	for i := 0; i < n; i++ {
		if (*a)[i] == dig {
			last = i
		}
	}
	if last == -1 {
		return
	}
	for i := 0; i <= last; i++ {
		if (*a)[i] == dig {
			*v = append(*v, (*a)[i])
		} else {
			if (*a)[i] == 9 {
				*v = append(*v, 9)
			} else {
				*v = append(*v, (*a)[i]+1)
			}
		}
	}
	var b []int
	for i := last + 1; i < n; i++ {
		b = append(b, (*a)[i])
	}
	*a = b
}

func main() {
	BAAL()
	defer func() {
		iost.Writer.Flush()
	}()
	t := iost.GetNextInt()
	for t > 0 {
		t--
		s := iost.Text()
		n := len(s)
		var a []int
		for i := 0; i < n; i++ {
			a = append(a, int(s[i]-'0'))
		}
		var o []int
		for i := 0; i < 10; i++ {
			karo(&a, i, &o)
		}
		sort.Ints(o)
		for _, x := range o {
			iost.Print(x)
		}
		iost.Println()
	}
}

package main

import (
	"fmt"
)

func work(n string) string {
	s := map[byte]int{}

	for i := byte('0'); i < byte('9'); i++ {
		s[i] = 0
	}

	min := byte('9')
	for i := len(n) - 1; i >= 0; i-- {
		v := n[i]
		if v > min {
			if v < byte('9') {
				v++
			}
			s[v] += 1
		} else {
			s[v] += 1
			min = v
		}
	}

	r := []byte{}
	for i := byte('0'); i <= byte('9'); i++ {
		for j := 0; j < s[i]; j++ {
			r = append(r, i)
		}
	}

	return string(r)
}

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for ; t > 0; t-- {
		var n string
		fmt.Scanf("%s\n", &n)
		fmt.Println(work(n))
	}
}

package main

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

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

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

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

		d := 9
		aa := make([]int, 10)
		for i := len(s) - 1; i >= 0; i-- {
			v := int(s[i] - '0')
			if v <= d {
				d = v
				aa[v]++
			} else {
				aa[min(v+1, 9)]++
			}
		}

		for i := 0; i < 10; i++ {
			for j := 0; j < aa[i]; j++ {
				fmt.Fprintf(out, "%d", i)
			}
		}
		fmt.Fprintln(out)
	}
}

package main

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

const cf = true

func solve() {
	var s string
	fmt.Fscan(in, &s)
	n := len(s)
	cnt := make([]int, 10)
	curPos := -1
	for i := 0; i <= 9; i++ {
		chr := strconv.Itoa(i)[0]
		rightMost := -1
		for j := n - 1; j > curPos; j-- {
			if s[j] == chr {
				rightMost = j
				break
			}
		}
		if rightMost == -1 {
			continue
		}
		for j := curPos + 1; j <= rightMost; j++ {
			if s[j] == chr {
				cnt[i]++
			} else {
				integer, _ := strconv.Atoi(string(s[j]))
				cnt[min(9, integer+1)]++
			}
		}
		curPos = rightMost
		if curPos == n-1 {
			break
		}
	}
	for i := 0; i <= 9; i++ {
		chr := strconv.Itoa(i)
		for j := 0; j < cnt[i]; j++ {
			fmt.Fprint(out, chr)
		}
	}
	fmt.Fprintln(out)
}

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

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

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

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

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

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

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

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

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

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

var exists = struct{}{}

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

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

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

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

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

type Ordered interface {
	Integer | Float | ~string
}

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

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

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

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

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

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

package main

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

const N = 3e5
const inf = 1e9

var iost *Iost

type Iost struct {
	Scanner *bufio.Scanner
	Writer  *bufio.Writer
}

func NewIost(fp io.Reader, wfp io.Writer) *Iost {
	const BufSize = 2000005
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, BufSize), BufSize)
	return &Iost{Scanner: scanner, Writer: bufio.NewWriter(wfp)}
}
func (i *Iost) Text() string {
	if !i.Scanner.Scan() {
		panic("scan failed")
	}
	return i.Scanner.Text()
}
func (i *Iost) Atoi(s string) int                 { x, _ := strconv.Atoi(s); return x }
func (i *Iost) GetNextInt() int                   { return i.Atoi(i.Text()) }
func (i *Iost) Atoi64(s string) int64             { x, _ := strconv.ParseInt(s, 10, 64); return x }
func (i *Iost) GetNextInt64() int64               { return i.Atoi64(i.Text()) }
func (i *Iost) Atof64(s string) float64           { x, _ := strconv.ParseFloat(s, 64); return x }
func (i *Iost) GetNextFloat64() float64           { return i.Atof64(i.Text()) }
func (i *Iost) Print(x ...interface{})            { fmt.Fprint(i.Writer, x...) }
func (i *Iost) Printf(s string, x ...interface{}) { fmt.Fprintf(i.Writer, s, x...) }
func (i *Iost) Println(x ...interface{})          { fmt.Fprintln(i.Writer, x...) }

func BAAL() {
	in := os.Stdin
	out := os.Stdout
	iost = NewIost(in, out)
}

func swap(a, b *int) {
	x := *a
	*a = *b
	*b = x
}

func karo(a []int, dig int) {
	ind := -1
	n := len(a)
	for i := 0; i < n; i++ {
		if a[i] >= dig {
			ind = i
			break
		}
	}
	if ind == -1 {
		return
	}
	last := -1
	for i := ind; i < n; i++ {
		if a[i] == dig {
			last = i
		}
	}
	if last == -1 {
		return
	}
	var v []int
	for i := ind; i <= last; i++ {
		if a[i] == dig {
			v = append(v, a[i])
		}
	}
	var v1 []int
	for i := ind; i <= last; i++ {
		if a[i] != dig {
			if a[i] == 9 {
				v1 = append(v1, 9)
			} else {
				v1 = append(v1, a[i]+1)
			}
		}
	}
	sort.Ints(v1)
	for _, x := range v1 {
		v = append(v, x)
	}
	for i := ind; i <= last; i++ {
		a[i] = v[i-ind]
	}
}

func main() {
	BAAL()
	defer func() {
		iost.Writer.Flush()
	}()
	t := iost.GetNextInt()
	for t > 0 {
		t--
		s := iost.Text()
		n := len(s)
		var a []int
		for i := 0; i < n; i++ {
			a = append(a, int(s[i]-'0'))
		}
		for i := 0; i < 10; i++ {
			karo(a, i)
		}
		for _, x := range a {
			iost.Print(x)
		}
		iost.Println()
	}
}


The student council has a shared document file. Every day, some members of the student council write the sequence TMT (short for Towa Maji Tenshi) in it.However, one day, the members somehow entered the sequence into the document at the same time, creating a jumbled mess. Therefore, it is Suguru Doujima's task to figure out whether the document has malfunctioned. Specifically, he is given a string of length $$$n$$$ whose characters are all either T or M, and he wants to figure out if it is possible to partition it into some number of disjoint subsequences, all of which are equal to TMT. That is, each character of the string should belong to exactly one of the subsequences.A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero) characters.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$3 \le n < 10^5$$$), the number of characters in the string entered in the document. It is guaranteed that $$$n$$$ is divisible by $$$3$$$. The second line of each test case contains a string of length $$$n$$$ consisting of only the characters T and M. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, print a single line containing YES if the described partition exists, and a single line containing NO otherwise.

5
3
TMT
3
MTT
6
TMTMTT
6
TMTTTT
6
TTMMTT

NoteIn the first test case, the string itself is already a sequence equal to TMT.In the third test case, we may partition the string into the subsequences TMTMTT. Both the bolded and the non-bolded subsequences are equal to TMT.

package main

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

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

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

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

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

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

/**
 Run solve the problem CF1509B
 Date: 17/04/21
 User: pepradere
 URL: https://codeforces.com/contest/1509/problem/B
 Problem: CF1509B
**/
func (in *CF1509B) Run() {
	T := in.NextInt()
	for ; T > 0; T-- {
		in.NextInt()
		cad := in.NextString()
		M, P := 0, 0
		ws := true
		for i := 0; i < len(cad); i++ {
			if cad[i] == 'M' {
				M++
			} else {
				P++
			}
			if M > P {
				ws = false
			}
		}
		M = 0
		P = 0
		for i := len(cad)-1; i>=0; i-- {
			if cad[i] == 'M' {
				M++
			} else {
				P++
			}
			if M > P {
				ws = false
			}
		}

		if M*2 != P || cad[len(cad)-1] == 'M' {
			ws = false
		}

		if ws {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

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

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

package main

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

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

	var T, n int
	var s string
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &s)
		m := strings.Count(s, "M")
		if m*3 != n {
			Fprintln(out, "NO")
			continue
		}
		c := 0
		for _, b := range s {
			if b == 'T' {
				c++
			} else {
				c--
				if c < 0 {
					Fprintln(out, "NO")
					continue o
				}
			}
		}
		c = 0
		for i := n - 1; i >= 0; i-- {
			if s[i] == 'T' {
				c++
			} else {
				c--
				if c < 0 {
					Fprintln(out, "NO")
					continue o
				}
			}
		}
		Fprintln(out, "YES")
	}
}

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

package main

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

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

func Scan(a ...interface{})    { fmt.Fscan(reader, a...) }
func Scanln(a ...interface{})  { fmt.Fscanln(reader, a...) }
func Print(a ...interface{})   { fmt.Fprint(writer, a...) }
func Println(a ...interface{}) { fmt.Fprintln(writer, a...) }

func main() {
	defer writer.Flush()
	var t int
	Scanln(&t)
	for ; t > 0; t-- {
		fi := 0
		se := 0
		var n int
		Scanln(&n)
		var s string
		Scanln(&s)
		te := 0
		em := 0
		valid := true
		for _, v := range s {
			if v == 'T' {
				te++
			} else {
				em++
			}
		}
		if te != 2*em {
			valid = false
		}
		cnt_t := 0
		for _, v := range s {
			if v == 'T' {
				if cnt_t < em {
					cnt_t++
					fi++
				} else if se != 0 {
					se--
				} else {
					valid = false
					break
				}
			} else {
				if fi == 0 {
					valid = false
					break
				}
				se++
				fi--
			}
		}
		if fi > 0 || se > 0 {
			valid = false
		}
		if !valid {
			Println("NO")
		} else {
			Println("YES")
		}
	}
}

package main

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

const REPEAT bool = true
const CASE   bool = false

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

  if REPEAT {
    var t int
    Fscan(in, &t);
    for i := 1; i <= t; i++ {
      if CASE {
        Fprintf(out, "Case #%d: ", i)
      }
      solve(in, out)
    }
  } else {
    solve(in, out)
  }
}

func solve(in io.Reader, out io.Writer) {
  var n int
  var s string
  Fscan(in, &n, &s)
  tc, mc := 0, 0
  for _, c := range s {
    if c == 'T' {
      tc++
    } else {
      mc++
    }
  }
  if tc != 2 * mc {
    Fprintln(out, "NO")
  } else {
    lim, cntr := n / 3, 0
    for _, c := range s {
      if c == 'T' {
        cntr++
      } else {
        cntr--
      }
      if cntr > lim || cntr < 0 {
        Fprintln(out, "NO")
        return
      }
    }
    Fprintln(out, "YES")
  }
}

package main

import "fmt"

func main() {
	var t, n, tmp, tcount, mcount, done int
	var processed bool
	var str string
	fmt.Scanf("%d", &t)
	fmt.Scanf("%d", &tmp)
	for ; t > 0; t = t - 1 {
		fmt.Scanf("%d", &n)
		fmt.Scanf("%d", &tmp)
		fmt.Scanf("%s", &str)
		fmt.Scanf("%d", &tmp)

		tcount, mcount, done = 0, 0, 0
		processed = false
		for _, i := range str {
			// fmt.Print(string(i), " ")
			if i == 'T' {
				if mcount <= tcount && mcount > 0 {
					tcount -= 1
					mcount -= 1
					done += 1
					continue
				}
				tcount += 1
			} else if mcount == tcount {
				if done > 0 {
					tcount += 2
					mcount += 2
					done -= 1
				} else {
					fmt.Println("NO")
					processed = true
					break
				}
			} else {
				mcount += 1
			}
		}
		if processed {
			continue
		}
		if tcount == 0 && mcount == 0 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

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

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

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

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

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

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

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

		}
		return initial
	}
}

func GCD(x, y int64) int64 {
	var l, q int64
	l = MAX(x, y)
	q = MIN(x, y)
	var m int64
	for l*q > 0 {
		a := l % q
		b := l - a
		cc := int64(b / q)
		remain := l - cc*q
		l = MAX(remain, q)
		q = MIN(remain, q)
		if l*q == 0 {
			m = l + q
			break
		}

	}
	return m

}

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

}

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

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)

	for o := 0; o < t; o++ {
		var n int
		fmt.Fscan(input, &n)
		TMT := make([]byte, 0)
		fmt.Fscan(input, &TMT)

		ini := 0
		qqq := 0
		for i := 0; i < n; i++ {
			if string(TMT[i]) == "T" {
				ini++
			} else {
				ini--
			}
			if ini < 0 {
				qqq = 1
				break
			}

		}
		if ini != int(n/3) || qqq == 1 {
			fmt.Println("NO")
		} else {
			www := 0
			iii := 0
			for i := n - 1; i > -1; i-- {
				if string(TMT[i]) == "T" {
					iii++
				} else {
					iii--
				}
				if iii < 0 {
					www = 1
					fmt.Println("NO")
					break
				}
			}
			if www == 0 {
				fmt.Println("YES")
			}
		}

	}
}

package main

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

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		var n int

		fmt.Fscan(r, &n)

		var s string
		fmt.Fscan(r, &s)

		ans := solve(s)

		fmt.Println(ans)
	}
}

func solve(s string) string {

	var counter int

	for i := 0 ; i < len(s); i++ {
		if s[i] == 'T' {
			counter++
		} else {

			if counter <= 0 {
				return "NO"
			}
			counter--
		}
	}

	counter = 0

	for i := len(s) - 1; i >= 0; i-- {
		if s[i] == 'T' {
			counter++
		} else {

			if counter <= 0 {
				return "NO"
			}
			counter--
		}
	}

	var ts int
	var ms int

	for i := 0; i < len(s); i++ {
		if s[i] == 'T' {
			ts++
		} else {
			ms++
		}
	}
	if ts == 2 * ms {
		return "YES"
	}
	return "NO"
}

package main

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

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

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

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

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

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

/**
 Run solve the problem CF1509B
 Date: 17/04/21
 User: pepradere
 URL: https://codeforces.com/contest/1509/problem/B
 Problem: CF1509B
**/
func (in *CF1509B) Run() {
	T := in.NextInt()
	for ; T > 0; T-- {
		in.NextInt()
		cad := in.NextString()
		M, P := 0, 0
		ws := true
		for i := 0; i < len(cad); i++ {
			if cad[i] == 'M' {
				M++
			} else {
				P++
			}
			if M > P {
				ws = false
			}
		}

		if M*2 != P || cad[len(cad)-1] == 'M' {
			ws = false
		}

		if ws {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

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

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

package main

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

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

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

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

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

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

/**
 Run solve the problem CF1509B
 Date: 17/04/21
 User: pepradere
 URL: https://codeforces.com/contest/1509/problem/B
 Problem: CF1509B
**/
func (in *CF1509B) Run() {
	T := in.NextInt()
	for ; T > 0; T-- {
		in.NextInt()
		cad := in.NextString()
		M, P := 0, 0
		ws := true
		for i := 0; i < len(cad); i++ {
			if cad[i] == 'M' {
				M++
			} else {
				P++
			}
			if M > P {
				ws = false
			}
		}

		if M*2 != P {
			ws = false
		}

		if ws {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

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

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

package main

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

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

	var T, n int
	var s string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &s)
		m := strings.Count(s, "M")
		if m*3 != n {
			Fprintln(out, "NO")
			continue
		}
		c := 0
		for _, b := range s[:strings.LastIndex(s, "M")+1] {
			if b == 'T' {
				c++
			} else {
				c--
				if c < 0 {
					break
				}
			}
		}
		if c != 0 {
			Fprintln(out, "NO")
		} else {
			Fprintln(out, "YES")
		}
	}
}

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

package main

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

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

func Scan(a ...interface{})    { fmt.Fscan(reader, a...) }
func Scanln(a ...interface{})  { fmt.Fscanln(reader, a...) }
func Print(a ...interface{})   { fmt.Fprint(writer, a...) }
func Println(a ...interface{}) { fmt.Fprintln(writer, a...) }

func main() {
	defer writer.Flush()
	var t int
	Scanln(&t)
	for ; t > 0; t-- {
		fi := 0
		se := 0
		var n int
		Scanln(&n)
		var x string
		Scanln(&x)
		var s string
		for i := len(x) - 1; i >= 0; i-- {
			s += string(x[i])
		}
		valid := true
		for _, v := range s {
			if v == 'T' {
				if se != 0 {
					se--
				} else {
					fi++
				}
			} else {
				if fi == 0 {
					valid = false
					break
				} else {
					fi--
					se++
				}
			}
		}
		if fi > 0 || se > 0 {
			valid = false
		}
		if !valid {
			Println("NO")
		} else {
			Println("YES")
		}
	}
}


There are $$$n$$$ cities in Berland and some pairs of them are connected by two-way roads. It is guaranteed that you can pass from any city to any other, moving along the roads. Cities are numerated from $$$1$$$ to $$$n$$$.Two fairs are currently taking place in Berland — they are held in two different cities $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le n$$$; $$$a \ne b$$$).Find the number of pairs of cities $$$x$$$ and $$$y$$$ ($$$x \ne a, x \ne b, y \ne a, y \ne b$$$) such that if you go from $$$x$$$ to $$$y$$$ you will have to go through both fairs (the order of visits doesn't matter). Formally, you need to find the number of pairs of cities $$$x,y$$$ such that any path from $$$x$$$ to $$$y$$$ goes through $$$a$$$ and $$$b$$$ (in any order).Print the required number of pairs. The order of two cities in a pair does not matter, that is, the pairs $$$(x,y)$$$ and $$$(y,x)$$$ must be taken into account only once.

The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 4\cdot10^4$$$) — the number of test cases in the input. Next, $$$t$$$ test cases are specified. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$a$$$ and $$$b$$$ ($$$4 \le n \le 2\cdot10^5$$$, $$$n - 1 \le m \le 5\cdot10^5$$$, $$$1 \le a,b \le n$$$, $$$a \ne b$$$) — numbers of cities and roads in Berland and numbers of two cities where fairs are held, respectively. The following $$$m$$$ lines contain descriptions of roads between cities. Each of road description contains a pair of integers $$$u_i, v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \ne v_i$$$) — numbers of cities connected by the road. Each road is bi-directional and connects two different cities. It is guaranteed that from any city you can pass to any other by roads. There can be more than one road between a pair of cities. The sum of the values of $$$n$$$ for all sets of input data in the test does not exceed $$$2\cdot10^5$$$. The sum of the values of $$$m$$$ for all sets of input data in the test does not exceed $$$5\cdot10^5$$$.

Print $$$t$$$ integers — the answers to the given test cases in the order they are written in the input.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1276B(_r io.Reader, _w io.Writer) {
	in := bufio.NewScanner(_r)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	r := func() (x int) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int(b-'0')
		}
		return
	}
	subSize := func(g [][]int, a, b int) (sz int) {
		ids := make([]int, len(g))
		idCnt := 0
		var f func(int)
		f = func(v int) {
			ids[v] = idCnt
			for _, w := range g[v] {
				if w != a && ids[w] == 0 {
					f(w)
				}
			}
		}
		for i, id := range ids {
			if i != a && id == 0 {
				idCnt++
				f(i)
			}
		}
		if idCnt == 1 {
			return
		}
		for i, id := range ids {
			if i != a && id != ids[b] {
				sz++
			}
		}
		return
	}

	for t := r(); t > 0; t-- {
		n, m, a, b := r(), r(), r()-1, r()-1
		g := make([][]int, n)
		for ; m > 0; m-- {
			v, w := r()-1, r()-1
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		Fprintln(out, int64(subSize(g, a, b))*int64(subSize(g, b, a)))
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1276B(_w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()
	buf := make([]byte, 4096)
	_i := len(buf)
	rc := func() byte {
		if _i == len(buf) {
			syscall.Read(syscall.Stdin, buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}
	subSize := func(g [][]int, a, b int) (sz int) {
		ids := make([]int, len(g))
		idCnt := 0
		var f func(int)
		f = func(v int) {
			ids[v] = idCnt
			for _, w := range g[v] {
				if w != a && ids[w] == 0 {
					f(w)
				}
			}
		}
		for i, id := range ids {
			if i != a && id == 0 {
				idCnt++
				f(i)
			}
		}
		if idCnt == 1 {
			return
		}
		for i, id := range ids {
			if i != a && id != ids[b] {
				sz++
			}
		}
		return
	}

	for t := r(); t > 0; t-- {
		n, m, a, b := r(), r(), r()-1, r()-1
		g := make([][]int, n)
		for ; m > 0; m-- {
			v, w := r()-1, r()-1
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		Fprintln(out, int64(subSize(g, a, b))*int64(subSize(g, b, a)))
	}
}

func main() { CF1276B(os.Stdout) }

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1276B(_w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()
	buf := make([]byte, 4096)
	_i := len(buf)
	rc := func() byte {
		if _i == len(buf) {
			syscall.Read(syscall.Stdin, buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b || b > '9'; b = rc() {
		}
		for ; '0' <= b && b <= '9'; b = rc() {
			x = x*10 + int(b-'0')
		}
		return
	}
	subSize := func(g [][]int, a, b int) (sz int) {
		ids := make([]int, len(g))
		idCnt := 0
		var f func(int)
		f = func(v int) {
			ids[v] = idCnt
			for _, w := range g[v] {
				if w != a && ids[w] == 0 {
					f(w)
				}
			}
		}
		for i, id := range ids {
			if i != a && id == 0 {
				idCnt++
				f(i)
			}
		}
		if idCnt == 1 {
			return
		}
		for i, id := range ids {
			if i != a && id != ids[b] {
				sz++
			}
		}
		return
	}

	for t := r(); t > 0; t-- {
		n, m, a, b := r(), r(), r()-1, r()-1
		g := make([][]int, n)
		for ; m > 0; m-- {
			v, w := r()-1, r()-1
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		Fprintln(out, int64(subSize(g, a, b))*int64(subSize(g, b, a)))
	}
}

func main() { CF1276B(os.Stdout) }

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1276B(_r io.Reader, _w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()
	buf := make([]byte, 4096)
	_i := len(buf)
	rc := func() byte {
		if _i == len(buf) {
			_r.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b || b > '9'; b = rc() {
		}
		for ; '0' <= b && b <= '9'; b = rc() {
			x = x*10 + int(b-'0')
		}
		return
	}
	subSize := func(g [][]int, a, b int) (sz int) {
		ids := make([]int, len(g))
		idCnt := 0
		var f func(int)
		f = func(v int) {
			ids[v] = idCnt
			for _, w := range g[v] {
				if w != a && ids[w] == 0 {
					f(w)
				}
			}
		}
		for i, id := range ids {
			if i != a && id == 0 {
				idCnt++
				f(i)
			}
		}
		if idCnt == 1 {
			return
		}
		for i, id := range ids {
			if i != a && id != ids[b] {
				sz++
			}
		}
		return
	}

	for t := r(); t > 0; t-- {
		n, m, a, b := r(), r(), r()-1, r()-1
		g := make([][]int, n)
		for ; m > 0; m-- {
			v, w := r()-1, r()-1
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		Fprintln(out, int64(subSize(g, a, b))*int64(subSize(g, b, a)))
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1276B(_r io.Reader, _w io.Writer) {
	in := bufio.NewScanner(_r)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	r := func() (x int) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int(b-'0')
		}
		return
	}
	subSize := func(g [][]int, a, b int) (sz int64) {
		ids := make([]int, len(g))
		idCnt := 0
		var f func(int)
		f = func(v int) {
			ids[v] = idCnt
			for _, w := range g[v] {
				if w != a && ids[w] == 0 {
					f(w)
				}
			}
		}
		for i, id := range ids {
			if i != a && id == 0 {
				idCnt++
				f(i)
			}
		}
		if idCnt == 1 {
			return
		}
		for i, id := range ids {
			if i != a && id != ids[b] {
				sz++
			}
		}
		return
	}

	for t := r(); t > 0; t-- {
		n, m, a, b := r(), r(), r()-1, r()-1
		g := make([][]int, n)
		for ; m > 0; m-- {
			v, w := r()-1, r()-1
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		Fprintln(out, subSize(g, a, b)*subSize(g, b, a))
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1276B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	subSize := func(g [][]int, a, b int) (sz int) {
		ids := make([]int, len(g))
		idCnt := 0
		var f func(int)
		f = func(v int) {
			ids[v] = idCnt
			for _, w := range g[v] {
				if w != a && ids[w] == 0 {
					f(w)
				}
			}
		}
		for i, id := range ids {
			if i != a && id == 0 {
				idCnt++
				f(i)
			}
		}
		if idCnt == 1 {
			return
		}
		for i, id := range ids {
			if i != a && id != ids[b] {
				sz++
			}
		}
		return
	}

	var t, n, m, a, b, v, w int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m, &a, &b)
		a--
		b--
		g := make([][]int, n)
		for ; m > 0; m-- {
			Fscan(in, &v, &w)
			v--
			w--
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		Fprintln(out, int64(subSize(g, a, b))*int64(subSize(g, b, a)))
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1276B(_r io.Reader, _w io.Writer) {
	in := bufio.NewScanner(_r)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	read := func() (x int) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int(b-'0')
		}
		return
	}
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}

	t:=read()
	for cas := 1;cas<=t;cas++{
		n, m, a, b := read(), read(), read()-1, read()-1
		g := make([][]int, n)
		for ; m > 0; m-- {
			v, w := read()-1, read()-1
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		if t == 30000 {
			if cas != 60 {
				continue
			}
			Fprintln(out, a,b)
			Fprintln(out, g)
			return
		}

		ids := make([]int, n)
		idCnt := 0
		var f func(int)
		f = func(v int) {
			ids[v] = idCnt
			for _, w := range g[v] {
				if w != a && ids[w] == 0 {
					f(w)
				}
			}
		}
		for i, id := range ids {
			if i != a && id == 0 {
				idCnt++
				f(i)
			}
		}
		if idCnt == 1 {
			Fprintln(out, 0)
			continue
		}

		isCutB := false
		subSizeB := 0
		dfsClock := 0
		dfn := make([]int, n)
		var f2 func(v, fa int) (int, int)
		f2 = func(v, fa int) (lowV, size int) {
			dfsClock++
			dfn[v] = dfsClock
			lowV = dfsClock
			size = 1
			for _, w := range g[v] {
				if dfn[w] == 0 {
					lowW, sz := f2(w, v)
					size += sz
					if v == b && lowW >= dfn[v] {
						isCutB = true
					}
					lowV = min(lowV, lowW)
				} else if w != fa && dfn[w] < dfn[v] {
					lowV = min(lowV, dfn[w])
				}
			}
			if v == b {
				subSizeB = size - 1
			}
			return
		}
		f2(a, -1)
		if !isCutB {
			Fprintln(out, 0)
			continue
		}

		otherSizeA := 0
		for i, id := range ids {
			if i != a && id != ids[b] {
				otherSizeA++
			}
		}
		Fprintln(out, int64(subSizeB)*int64(otherSizeA))
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1276B(_r io.Reader, _w io.Writer) {
	in := bufio.NewScanner(_r)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	read := func() (x int) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int(b-'0')
		}
		return
	}
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}

	for t := read(); t > 0; t-- {
		n, m, a, b := read(), read(), read()-1, read()-1
		g := make([][]int, n)
		for ; m > 0; m-- {
			v, w := read()-1, read()-1
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}

		ids := make([]int, n)
		idCnt := 0
		var f func(int)
		f = func(v int) {
			ids[v] = idCnt
			for _, w := range g[v] {
				if w != a && ids[w] == 0 {
					f(w)
				}
			}
		}
		for i, id := range ids {
			if i != a && id == 0 {
				idCnt++
				f(i)
			}
		}
		if idCnt == 1 {
			Fprintln(out, 0)
			continue
		}

		isCutB := false
		subSizeB := 0
		dfsClock := 0
		dfn := make([]int, n)
		var f2 func(v, fa int) (int, int)
		f2 = func(v, fa int) (lowV, size int) {
			dfsClock++
			dfn[v] = dfsClock
			lowV = dfsClock
			size = 1
			for _, w := range g[v] {
				if dfn[w] == 0 {
					lowW, sz := f2(w, v)
					size += sz
					if v == b && lowW >= dfn[v] {
						isCutB = true
					}
					lowV = min(lowV, lowW)
				} else if w != fa && dfn[w] < dfn[v] {
					lowV = min(lowV, dfn[w])
				}
			}
			if v == b {
				subSizeB = size - 1
			}
			return
		}
		f2(a, -1)
		if !isCutB {
			Fprintln(out, 0)
			continue
		}

		otherSizeA := 0
		for i, id := range ids {
			if i != a && id != ids[b] {
				otherSizeA++
			}
		}
		Fprintln(out, int64(subSizeB)*int64(otherSizeA))
	}
}

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


Scott Lang is at war with Darren Cross. There are n chairs in a hall where they are, numbered with 1, 2, ..., n from left to right. The i-th chair is located at coordinate xi. Scott is on chair number s and Cross is on chair number e. Scott can jump to all other chairs (not only neighboring chairs). He wants to start at his position (chair number s), visit each chair exactly once and end up on chair number e with Cross. As we all know, Scott can shrink or grow big (grow big only to his normal size), so at any moment of time he can be either small or large (normal). The thing is, he can only shrink or grow big while being on a chair (not in the air while jumping to another chair). Jumping takes time, but shrinking and growing big takes no time. Jumping from chair number i to chair number j takes |xi - xj| seconds. Also, jumping off a chair and landing on a chair takes extra amount of time. If Scott wants to jump to a chair on his left, he can only be small, and if he wants to jump to a chair on his right he should be large.Jumping off the i-th chair takes:  ci extra seconds if he's small.  di extra seconds otherwise (he's large). Also, landing on i-th chair takes:  bi extra seconds if he's small.  ai extra seconds otherwise (he's large). In simpler words, jumping from i-th chair to j-th chair takes exactly:  |xi - xj| + ci + bj seconds if j < i.  |xi - xj| + di + aj seconds otherwise (j > i). Given values of x, a, b, c, d find the minimum time Scott can get to Cross, assuming he wants to visit each chair exactly once.

The first line of the input contains three integers n, s and e (2 ≤ n ≤ 5000, 1 ≤ s, e ≤ n, s ≠ e) — the total number of chairs, starting and ending positions of Scott. The second line contains n integers x1, x2, ..., xn (1 ≤ x1 < x2 < ... < xn ≤ 109). The third line contains n integers a1, a2, ..., an (1 ≤ a1, a2, ..., an ≤ 109). The fourth line contains n integers b1, b2, ..., bn (1 ≤ b1, b2, ..., bn ≤ 109). The fifth line contains n integers c1, c2, ..., cn (1 ≤ c1, c2, ..., cn ≤ 109). The sixth line contains n integers d1, d2, ..., dn (1 ≤ d1, d2, ..., dn ≤ 109).

Print the minimum amount of time Scott needs to get to the Cross while visiting each chair exactly once.

NoteIn the sample testcase, an optimal solution would be . Spent time would be 17 + 24 + 23 + 20 + 33 + 22 = 139.

package main

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

const maxn = 5005
const inf = int64(2000000000000000)

var in *bufio.Scanner
var out *bufio.Writer
var n, s, e int
var x, a, b, c, d, f, g [maxn]int64

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

func read() {
	in.Scan()
	n, _ = strconv.Atoi(in.Text())

	in.Scan()
	s, _ = strconv.Atoi(in.Text())

	in.Scan()
	e, _ = strconv.Atoi(in.Text())

	for i := 1; i <= n; i++ {
		in.Scan()
		x[i], _ = strconv.ParseInt(in.Text(), 10, 64)
	}

	for i := 1; i <= n; i++ {
		in.Scan()
		a[i], _ = strconv.ParseInt(in.Text(), 10, 64)
	}

	for i := 1; i <= n; i++ {
		in.Scan()
		b[i], _ = strconv.ParseInt(in.Text(), 10, 64)
	}

	for i := 1; i <= n; i++ {
		in.Scan()
		c[i], _ = strconv.ParseInt(in.Text(), 10, 64)
	}

	for i := 1; i <= n; i++ {
		in.Scan()
		d[i], _ = strconv.ParseInt(in.Text(), 10, 64)
	}
}

func write(res int64) {
	out.WriteString(strconv.FormatInt(res, 10))
	out.WriteByte('\n')
}

func dp() int64 {
	if s > e {
		s, e = e, s
		a, c = c, a
		b, d = d, b
	}

	for j := 0; j <= n; j++ {
		f[j] = inf
	}

	if s == 1 {
		f[1] = int64(d[1])
	} else {
		f[1] = int64(b[1] + d[1])
	}

	for i := 1; i < n; i++ {
		for j := 0; j <= n; j++ {
			g[j] = f[j]
			f[j] = inf
		}
		g[0] = inf
		for j := 0; j <= n; j++ {
			if g[j] < inf {
				cnt := j * 2
				if (s <= i) && (i+1 <= e) {
					cnt--
				}

				cost := g[j] + (x[i+1]-x[i])*int64(cnt)

				if i+1 == s {
					f[j] = min(f[j], cost+c[i+1])
					f[j+1] = min(f[j+1], cost+d[i+1])
				} else if i+1 == e {
					if j > 0 {
						f[j-1] = min(f[j-1], cost+a[i+1])
					}
					f[j] = min(f[j], cost+b[i+1])
				} else {
					if j > 0 {
						f[j-1] = min(f[j-1], cost+a[i+1]+c[i+1])
					}
					f[j] = min(f[j], cost+a[i+1]+d[i+1])
					f[j+1] = min(f[j+1], cost+b[i+1]+d[i+1])
					if !((s <= i) && (i+1 <= e) && (j <= 1)) {
						f[j] = min(f[j], cost+b[i+1]+c[i+1])
					}
				}
			}
		}
	}

	return f[0]
}

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

	in.Split(bufio.ScanWords)
	defer out.Flush()

	read()
	rdp := dp()
	write(rdp)
}

package main

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

const maxn = 5005
const inf = int64(2000000000000000)

var in *bufio.Scanner
var out *bufio.Writer
var n, s, e int
var x, a, b, c, d, f, g [maxn]int64

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

func read() {
	in.Scan()
	n, _ = strconv.Atoi(in.Text())

	in.Scan()
	s, _ = strconv.Atoi(in.Text())

	in.Scan()
	e, _ = strconv.Atoi(in.Text())

	for i := 1; i <= n; i++ {
		in.Scan()
		x[i], _ = strconv.ParseInt(in.Text(), 10, 64)
	}

	for i := 1; i <= n; i++ {
		in.Scan()
		a[i], _ = strconv.ParseInt(in.Text(), 10, 64)
	}

	for i := 1; i <= n; i++ {
		in.Scan()
		b[i], _ = strconv.ParseInt(in.Text(), 10, 64)
	}

	for i := 1; i <= n; i++ {
		in.Scan()
		c[i], _ = strconv.ParseInt(in.Text(), 10, 64)
	}

	for i := 1; i <= n; i++ {
		in.Scan()
		d[i], _ = strconv.ParseInt(in.Text(), 10, 64)
	}
}

func write(res int64) {
	out.WriteString(strconv.FormatInt(res, 10))
	out.WriteByte('\n')
}

func dp() int64 {
	if s > e {
		s, e = e, s
		a, c = c, a
		b, d = d, b
	}

	for j := 0; j <= n+1; j++ {
		f[j] = inf
	}

	if s == 1 {
		f[1] = int64(d[1])
	} else {
		f[1] = int64(b[1] + d[1])
	}

	for i := 1; i < n; i++ {
		for j := 0; j <= n+1; j++ {
			g[j] = f[j]
			f[j] = inf
		}
		g[0] = inf
		for j := 0; j <= n; j++ {
			if g[j] < inf {
				// fmt.Println(i, " ", j, " >> ", g[j])
				cnt := j * 2
				if (s < i+1) && (i+1 <= e) {
					cnt--
				}
				cost := g[j] + (x[i+1]-x[i])*int64(cnt)

				if i+1 == s {
					f[j] = min(f[j], cost+c[i+1])
					f[j+1] = min(f[j+1], cost+d[i+1])
				} else if i+1 == e {
					if j > 0 {
						f[j-1] = min(f[j-1], cost+a[i+1])
						// fmt.Println(i, " ", j, " ", g[j], " --> ", i+1, " ", j-1, " ", f[j-1])
					}
					f[j] = min(f[j], cost+b[i+1])
					// fmt.Println(i, " ", j, " ", g[j], " --> ", i+1, " ", j, " ", f[j])
				} else {
					if j > 0 {
						f[j-1] = min(f[j-1], cost+a[i+1]+c[i+1])
						// fmt.Println(i, " ", j, " ", g[j], " --> ", i+1, " ", j-1, " ", f[j-1])
					}

					f[j] = min(f[j], cost+a[i+1]+d[i+1])
					// fmt.Println(i, " ", j, " ", g[j], " --> ", i+1, " ", j, " ", f[j])

					f[j+1] = min(f[j+1], cost+b[i+1]+d[i+1])
					// fmt.Println(i, " ", j, " ", g[j], " --> ", i+1, " ", j+1, " ", f[j+1])

					if !((s <= i) && (i+1 <= e) && (j <= 1)) {
						f[j] = min(f[j], cost+b[i+1]+c[i+1])
						// fmt.Println(i, " ", j, " ", g[j], " --> ", i+1, " ", j, " ", f[j])
					}
				}
			}
		}
	}

	return f[0]
}

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

	in.Split(bufio.ScanWords)
	defer out.Flush()

	read()
	rdp := dp()
	write(rdp)
}