You are given an array a. Some element of this array ai is a local minimum iff it is strictly less than both of its neighbours (that is, ai < ai - 1 and ai < ai + 1). Also the element can be called local maximum iff it is strictly greater than its neighbours (that is, ai > ai - 1 and ai > ai + 1). Since a1 and an have only one neighbour each, they are neither local minima nor local maxima.An element is called a local extremum iff it is either local maximum or local minimum. Your task is to calculate the number of local extrema in the given array.

The first line contains one integer n (1 ≤ n ≤ 1000) — the number of elements in array a. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the elements of array a.

Print the number of local extrema in the given array.

package main

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

var local = false
var scanner *bufio.Scanner

func main() {
	setup()
	n := readInt()

	total, a, b := 0, -1, -1
	for i := 0; i < n; i++ {
		c := readInt()
		if i >= 2 {
			isLocMin := b < a && b < c
			isLocMax := b > a && b > c
			if isLocMin || isLocMax {
				total++
			}
		}

		a, b = b, c
	}

	fmt.Println(total)
}

func setup() {
	source := os.Stdin
	if local == true {
		file, _ := os.Open("input.txt")
		source = file
	}
	scanner = bufio.NewScanner(source)
	scanner.Split(bufio.ScanWords)
}

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

package main

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

func solve() {
	n := readInt()
	a := make([]int, n)
	for i, _ := range a {
		a[i] = readInt()
	}

	cnt := 0
	for i := 1; i < n-1; i++ {
		if a[i] > a[i+1] && a[i] > a[i-1] {
			cnt++
		} else if a[i] < a[i+1] && a[i] < a[i-1] {
			cnt++
		}
	}

	fmt.Println(cnt)
}

var scanner *bufio.Scanner

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

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

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

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

package main

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

var n int
var a[2000] int
var res int
func main() {
	in := bufio.NewReader(os.Stdin)
	res = 0
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}

	for i := 1; i < n-1; i++ {
		if ((a[i] > a[i+1] && a[i] > a[i-1])||(a[i] < a[i+1] && a[i] < a[i-1])) {
			res++
		}
	}
	fmt.Printf("%d", res)
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	len1, _ := reader.ReadString('\n')
	len2, _ := strconv.ParseInt(strings.TrimSpace(len1), 10, 16)
	len3 := int(len2)

	if len2 <=1 {
		writer.WriteString("0")
		writer.Flush()
		return
	}

	line, _ := reader.ReadString('\n')
	var tmp int64
	data := make([]int16, len3, len3)
	for key, value := range strings.Split(strings.TrimSpace(line), " ") {
		tmp, _ = strconv.ParseInt(value, 10, 16)
		data[key] = int16(tmp)
	}
	c := 0
	prev := int8(0)
	for key, value := range data[1:len3-1] {
		if prev == 0 {
			if value < data[key] && value < data[key+2] {
				c++
				prev = 1
				continue
			} else if value > data[key] && value > data[key+2] {
				c++
				prev = 2
				continue
			}
			prev = 0
		} else if prev == 1 {
			if value > data[key] && value > data[key+2] {
				c++
				prev = 2
				continue
			}
			prev = 0
		} else if prev == 2 {
			if value < data[key] && value < data[key+2] {
				c++
				prev = 1
				continue
			}
			prev = 0
		}
	}
	writer.WriteString(strconv.Itoa(c))
	writer.Flush()
}

package main

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

func main() {
	var line string
	var count int
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	line, _ = reader.ReadString('\n')
	n, _ := strconv.Atoi(strings.TrimSpace(line))
	arr := make([]int, n)
	line, _ = reader.ReadString('\n')
	for i, val := range strings.Split(strings.TrimSpace(line), " ") {
		tmp, _ := strconv.Atoi(val)
		arr[i] = tmp
	}
	for i := 1; i < n-1; i++ {
		if (arr[i] > arr[i-1] && arr[i] > arr[i+1]) || (arr[i] < arr[i-1] && arr[i] < arr[i+1]) {
			count++
		}
	}
	writer.WriteString(strconv.Itoa(count))
	writer.Flush()
}

package main

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

func main() {
	var line string
	var count, cur, prev, future int
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	_, _ = reader.ReadString('\n')
	line, _ = reader.ReadString('\n')
	for i, val := range strings.Split(strings.TrimSpace(line), " ") {
		if i == 0 {
			prev, _ = strconv.Atoi(val)
			continue
		} else if i == 1 {
			cur, _ = strconv.Atoi(val)
			continue
		}
		future, _ = strconv.Atoi(val)
		if (cur > prev && cur > future) || (cur < prev && cur < future) {
			count++
		}
		prev, cur = cur, future
	}
	writer.WriteString(strconv.Itoa(count))
	writer.Flush()
}

package main

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

func main() {
	var line string
	var count, cur, prev, future int
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	line, _ = reader.ReadString('\n')
	_, _ = strconv.Atoi(strings.TrimSpace(line))
	line, _ = reader.ReadString('\n')
	for i, val := range strings.Split(strings.TrimSpace(line), " ") {
		if i == 0 {
			prev, _ = strconv.Atoi(val)
			continue
		} else if i == 1 {
			cur, _ = strconv.Atoi(val)
			continue
		}
		future, _ = strconv.Atoi(val)
		if (cur > prev && cur > future) || (cur < prev && cur < future) {
			count++
		}
		prev, cur = cur, future
	}
	writer.WriteString(strconv.Itoa(count))
	writer.Flush()
}

package main

import (
	"fmt"
)

func main(){
	var n int
	fmt.Scan(&n)
	arr := make([]int, n)
	for i := range arr {
		fmt.Scan(&arr[i])
	}
	count := 0
	for i := 1; i < len(arr) -1; i ++ {
		if arr[i]<arr[i-1]&&arr[i]<arr[i+1] || arr[i]>arr[i-1]&&arr[i]>arr[i+1] {
			count++
		}
	}
	fmt.Print(count)
}

package main

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

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

	var n, cnt int
	fmt.Fscan(r, &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(r, &a[i])
	}
	for i := 1; i < n-1; i++ {
		if (a[i] > a[i-1] && a[i] > a[i+1]) || (a[i] < a[i-1] && a[i] < a[i+1]) {
			cnt++
		}
	}
	fmt.Fprintln(w, cnt)
}

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

package main

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

func Solve(n int, a []int) int {
	count := 0
	for i := 1; i < n-1; i++ {
		if a[i] > a[i-1] && a[i] > a[i+1] {
			count += 1
		} else if a[i] < a[i-1] && a[i] < a[i+1] {
			count += 1
		}
	}
	return count
}

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

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

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

package main

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

var n int
var a[2000] int
var res int
func main() {
	in := bufio.NewReader(os.Stdin)
	res = 0
	fmt.Fscan(in, &n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &a[i])
	}

	if n < 3 {
		fmt.Printf("%d", n)
		os.Exit(0)
	}

	for i := 1; i < n-1; i++ {
		if ((a[i] > a[i+1] && a[i] > a[i-1])||(a[i] < a[i+1] && a[i] < a[i-1])) {
			res++
		}
	}
	fmt.Printf("%d", res)
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	len1, _ := reader.ReadString('\n')
	len2, _ := strconv.ParseInt(strings.TrimSpace(len1), 10, 16)
	len3 := int(len2)
	line, _ := reader.ReadString('\n')
	var tmp int64
	data := make([]int16, len3, len3)
	for key, value := range strings.Split(line, " ") {
		tmp, _ = strconv.ParseInt(value, 10, 16)
		data[key] = int16(tmp)
	}
	c := 0
	prev := int8(0)
	for key, value := range data[1:len3-1] {
		if prev == 0 {
			if value < data[key] && value < data[key+2] {
				c++
				prev = 1
				continue
			} else if value > data[key] && value > data[key+2] {
				c++
				prev = 2
				continue
			}
			prev = 0
		} else if prev == 1 {
			if value > data[key] && value > data[key+2] {
				c++
				prev = 2
				continue
			}
			prev = 0
		} else if prev == 2 {
			if value < data[key] && value < data[key+2] {
				c++
				prev = 1
				continue
			}
			prev = 0
		}
	}
	writer.WriteString(strconv.Itoa(c))
	writer.Flush()
}


A new Berland businessman Vitaly is going to open a household appliances' store. All he's got to do now is to hire the staff.The store will work seven days a week, but not around the clock. Every day at least k people must work in the store.Berland has a law that determines the order of working days and non-working days. Namely, each employee must work for exactly n consecutive days, then rest for exactly m days, then work for n more days and rest for m more, and so on. Vitaly doesn't want to break the law. Fortunately, there is a loophole: the law comes into force on the day when the employee is hired. For example, if an employee is hired on day x, then he should work on days [x, x + 1, ..., x + n - 1], [x + m + n, x + m + n + 1, ..., x + m + 2n - 1], and so on. Day x can be chosen arbitrarily by Vitaly.There is one more thing: the key to the store. Berland law prohibits making copies of keys, so there is only one key. Vitaly is planning to entrust the key to the store employees. At the same time on each day the key must be with an employee who works that day — otherwise on this day no one can get inside the store. During the day the key holder can give the key to another employee, if he also works that day. The key will handed to the first hired employee at his first working day.Each employee has to be paid salary. Therefore, Vitaly wants to hire as few employees as possible provided that the store can operate normally on each day from 1 to infinity. In other words, on each day with index from 1 to infinity, the store must have at least k working employees, and one of the working employees should have the key to the store.Help Vitaly and determine the minimum required number of employees, as well as days on which they should be hired.

The first line contains three integers n, m and k (1 ≤ m ≤ n ≤ 1000, n ≠ 1, 1 ≤ k ≤ 1000).

In the first line print a single integer z — the minimum required number of employees. In the second line print z positive integers, separated by spaces: the i-th integer ai (1 ≤ ai ≤ 104) should represent the number of the day, on which Vitaly should hire the i-th employee. If there are multiple answers, print any of them.

package main

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

func scanInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	x, _ := strconv.Atoi(scanner.Text())
	return x
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	n, m, k := scanInt(scanner), scanInt(scanner), scanInt(scanner)
	span := n+m+1
	count := make([]int, span+1)
	key := make([]bool, span+1)
	hire := make([]int, k)
	for i := 0; i < k; i++ {
		hire[i] = 1
	}
	for i := 1; i <= n; i++ {
		count[i] = k
		key[i] = true
	}
	count[span] = k

	day := n
	for {
		for day <= span && count[day] >= k && key[day] {
			day++
		}
		if day > span {
			break
		}
		for !key[day] {
			day--
		}
		//writer.WriteString(fmt.Sprintf("day = %d, count = %d, key = %t\n",
		//		day, count[day], key[day]))
		hire = append(hire, day)
		last := day+n-1
		if last > span {
			last = span
		}
		//writer.WriteString(fmt.Sprintf("last = %d\n", last))
		for i := day; i <= last; i++ {
			count[i] += 1
			key[i] = true
		}
	}

	writer.WriteString(fmt.Sprintf("%d\n", len(hire)))
	writer.WriteString(fmt.Sprintf("%d", hire[0]))
	for i := 1; i != len(hire); i++ {
		writer.WriteString(fmt.Sprintf(" %d", hire[i]))
	}
	writer.WriteString("\n");
}

package main

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

func scanInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	x, _ := strconv.Atoi(scanner.Text())
	return x
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	n, m, k := scanInt(scanner), scanInt(scanner), scanInt(scanner)
	span := n+m+1
	count := make([]int, span+1)
	hire := make([]int, k)
	for i := 0; i < k; i++ {
		hire[i] = 1
	}
	for i := 1; i <= n; i++ {
		count[i] = k
	}
	count[span] = k

	day := n
	for {
		for day <= span && count[day] >= k {
			day++
		}
		if day > span {
			break
		}
		for count[day] == 0 {
			day--
		}
		hire = append(hire, day)
		for i := 0; day+i <= span && i < n; i++ {
			count[day+i] += 1
		}
	}

	writer.WriteString(fmt.Sprintf("%d\n", len(hire)))
	writer.WriteString(fmt.Sprintf("%d", hire[0]))
	for i := 1; i != len(hire); i++ {
		writer.WriteString(fmt.Sprintf(" %d", hire[i]))
	}
	writer.WriteString("\n");
}


Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get?

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman.

Print a single integer – the answer to the problem.

NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin.

package main

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

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

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

	line,_ := reader.ReadString('\n')

	arr := strings.Split(strings.TrimSpace(line), " ")

	ddmap := make(int64arr,256)
	var ret int64
	ret = 0

	if len(arr) == 2 {
		n,_ := strconv.Atoi(arr[0])
		k,_ := strconv.Atoi(arr[1])

	//	fmt.Println(n,k)

		line,_ := reader.ReadString('\n')

		bline := []byte(line)

		//fmt.Println(bline)

		if len(bline) < n {
			return
		}

		for i:=0; i<n; i ++ {
			ddmap[bline[i]] ++;
		}
		sort.Sort(sort.Reverse(ddmap))

		//fmt.Println(ddmap)

		for i:=0; i<256 && i<n; i ++ {
			if k > int(ddmap[i]) {
				ret += ddmap[i]*ddmap[i]
			}else{
				ret += int64(k)*int64(k)
				break
			}

			k -= int(ddmap[i])
		}

	}
	fmt.Println(ret)

	return
}

package main

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

func I(i interface{}, delim string) error {
	_, err := fmt.Fscanf(r, "%v"+delim, i)
	return err
}

func O(o interface{}, delim string) {
	fmt.Fprintf(w, "%v"+delim, o)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.txt"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	defer w.Flush()
	var n int64
	for I(&n, " ") == nil {
		solve(n)
	}
}

func solve(n int64) {
	var m int64
	I(&m, "\n")
	var s string
	I(&s, "\n")
	in := make([]int, 200)
	for i := 0; i < len(s); i++ {
		in[s[i]]++
	}
	sort.Ints(in)
	ans := int64(0)
	ii := int64(len(in) - 1)
	o := int64(in[ii])
	oo := int64(in[ii])
	if m < oo {
		oo = m
	}
	for m > int64(0) {
		if oo <= int64(0) {
			break
		}
		ans += oo
		o--
		m--
		if o == int64(0) && ii > int64(0) {
			ii--
			o = int64(in[ii])
			oo = int64(in[ii])
			if m < oo {
				oo = m
			}
		}
	}
	O(ans, "\n")
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	var n, k int
	var s string

	fmt.Scan(&n, &k)
	fmt.Scan(&s)

	letters := make([]int, 26)
	coins := int64(0)

	for i := 0; i < n; i++ {
		letters[s[i] - 'A']++
	}

	sort.Sort(sort.Reverse(sort.IntSlice(letters)))

	for i := 0; k > 0; {
		if k <= letters[i] {
			coins += int64(k) * int64(k)
			k = 0
		} else {
			coins += int64(letters[i]) * int64(letters[i])
			k -= letters[i]
			i++
		}
		// println(k, coins)
	}

	println(coins)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func nextFloat() float64 {
	n, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

func printf(format string, a ...interface{}) {
	fmt.Fprintf(writer, format, a...)
}

package main

//14:17

import (
    "fmt"
)

func main() {
    var n, k, i int64
    array := make([]int64, 26)
    for i = 0; i < 26; i++ {
        array[i] = 0
    }
    fmt.Scanf("%d %d ", &n, &k)
    for i = 0; i < n; i++ {
        var temp int8
        fmt.Scanf("%c", &temp)
        array[temp-'A']++
    }
    var score,coins int64
    for coins < k {
        var max,max_i int64
        for i=0;i<26;i++{
            if array[i]>max {
                max = array[i]
                max_i = i
            }
        }
        if max > (k-coins) {
            score += ((k-coins) * (k-coins))
            coins += (k-coins)
        }else{
            coins += max
            score += max * max
            array[max_i]=0
        }
    }
    fmt.Printf("%d ", score)
}

package main

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

const l int = 26

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

	cnt := make([]int64, l)

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)
	if s, err := reader.ReadString('\n'); err != nil {
		if err != io.EOF {
			return
		}
	} else {
		s = re.ReplaceAllString(s, "")
		for _, c := range s {
			cnt[int(c-'A')] += 1
		}

		bubble_sort(cnt, l)

		res := int64(0)
		for i := 0; i < l; i++ {
			if k >= cnt[i] {
				res += cnt[i] * cnt[i]
				k -= cnt[i]
			} else {
				res += k * k
				break
			}
		}
		fmt.Println(res)
	}
}

func swap(a, b int, array []int64) {
	array[a], array[b] = array[b], array[a]
}

func bubble_sort(array []int64, length int) {
	for i := 0; i < length; i++ {
		for j := 0; j < length; j++ {
			if array[i] > array[j] {
				swap(i, j, array)
			}
		}
	}
}

package main

import (
	"fmt"
	"sort"
)

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

	cards := ""
	fmt.Scan(&cards)

	counts := make([]int, 26, 26)
	for i := 0; i < len(cards); i++ {
		counts[cards[i]-'A']++
	}

	sort.Sort(sort.Reverse(sort.IntSlice(counts)))
	var ans uint64
	ans = 0
	i := 0
	for k > 0 {
		if counts[i] < k {
			k -= counts[i]
			ans += uint64(counts[i]) * uint64(counts[i])
		} else {
			ans += uint64(k) * uint64(k)
			k = 0
		}
		i++
	}

	fmt.Println(ans)
}

package main

import (
	"fmt"
	"sort"
)

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

	cards := ""
	fmt.Scan(&cards)

	counts := make([]int, 26, 26)
	for i := 0; i < len(cards); i++ {
		counts[cards[i]-'A']++
	}

	sort.Reverse(sort.IntSlice(counts))
	ans := 0
	i := 0
	for k > 0 {
		if counts[i] < k {
			k -= counts[i]
			ans += counts[i] * counts[i]
		} else {
			ans += k * k
			k = 0
		}
		i++
	}

	fmt.Println(ans)
}

package main

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

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

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

	line,_ := reader.ReadString('\n')

	arr := strings.Split(strings.TrimSpace(line), " ")

	ddmap := make(int64arr,256)
	var ret int64
	ret = 0

	if len(arr) == 2 {
		n,_ := strconv.Atoi(arr[0])
		k,_ := strconv.Atoi(arr[1])

	//	fmt.Println(n,k)

		line,_ := reader.ReadString('\n')

		bline := []byte(line)

		//fmt.Println(bline)

		if len(bline) < n {
			return
		}

		for i:=0; i<n; i ++ {
			ddmap[bline[i]] ++;
		}
		sort.Sort(sort.Reverse(ddmap))

		//fmt.Println(ddmap)

		for i:=0; i<256 && i<n; i ++ {
			if k > int(ddmap[i]) {
				ret += ddmap[i]*ddmap[i]
			}else{
				ret += int64(k*k)
				break
			}

			k -= int(ddmap[i])
		}

	}
	fmt.Println(ret)

	return
}

package main

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

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

	line,_ := reader.ReadString('\n')

	arr := strings.Split(strings.TrimSpace(line), " ")

	ddmap := make([]int,256)
	ret := 0

	if len(arr) == 2 {
		n,_ := strconv.Atoi(arr[0])
		k,_ := strconv.Atoi(arr[1])

	//	fmt.Println(n,k)

		line,_ := reader.ReadString('\n')

		bline := []byte(line)

		//fmt.Println(bline)

		if len(bline) < n {
			return
		}

		for i:=0; i<n; i ++ {
			ddmap[bline[i]] ++;
		}
		sort.Sort(sort.Reverse(sort.IntSlice(ddmap)))

	//	fmt.Println(ddmap)

		for i:=0; i<256 && i<n; i ++ {
			if(k > ddmap[i]) {
				ret += ddmap[i]*ddmap[i]
			}else{
				ret += k*k
				break
			}

			k -= ddmap[i]
		}

	}
	fmt.Println(ret)

	return
}

package main

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

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

	line,_ := reader.ReadString('\n')

	arr := strings.Split(strings.TrimSpace(line), " ")

	ddmap := make([]int,256)
	var ret int64
	ret = 0

	if len(arr) == 2 {
		n,_ := strconv.Atoi(arr[0])
		k,_ := strconv.Atoi(arr[1])

	//	fmt.Println(n,k)

		line,_ := reader.ReadString('\n')

		bline := []byte(line)

		//fmt.Println(bline)

		if len(bline) < n {
			return
		}

		for i:=0; i<n; i ++ {
			ddmap[bline[i]] ++;
		}
		sort.Sort(sort.Reverse(sort.IntSlice(ddmap)))

	//	fmt.Println(ddmap)

		for i:=0; i<256 && i<n; i ++ {
			if(k > ddmap[i]) {
				ret += int64(ddmap[i]*ddmap[i])
			}else{
				ret += int64(k*k)
				break
			}

			k -= ddmap[i]
		}

	}
	fmt.Println(ret)

	return
}

package main

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

func I(i interface{}, delim string) error {
	_, err := fmt.Fscanf(r, "%v"+delim, i)
	return err
}

func O(o interface{}, delim string) {
	fmt.Fprintf(w, "%v"+delim, o)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.txt"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	defer w.Flush()
	var n int
	for I(&n, " ") == nil {
		solve(n)
	}
}

func solve(n int) {
	var m int
	I(&m, "\n")
	var s string
	I(&s, "\n")
	in := make([]int, 200)
	for i := 0; i < len(s); i++ {
		in[s[i]]++
	}
	sort.Ints(in)
	ans := 0
	ii := len(in) - 1
	o := in[ii]
	oo := in[ii]
	if m < oo {
		oo = m
	}
	for m > 0 {
		ans += oo
		o--
		m--
		if o <= 0 {
			ii--
			o = in[ii]
			oo = in[ii]
			if m < oo {
				oo = m
			}
		}
	}
	O(ans, "\n")
}

package main

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

func I(i interface{}, delim string) error {
	_, err := fmt.Fscanf(r, "%v"+delim, i)
	return err
}

func O(o interface{}, delim string) {
	fmt.Fprintf(w, "%v"+delim, o)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.txt"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	defer w.Flush()
	var n int
	for I(&n, " ") == nil {
		solve(n)
	}
}

func solve(n int) {
	var m int
	I(&m, "\n")
	var s string
	I(&s, "\n")
	in := make([]int, 200)
	for i := 0; i < len(s); i++ {
		in[s[i]]++
	}
	sort.Ints(in)
	ans := 0
	ii := len(in) - 1
	o := in[ii]
	oo := in[ii]
	if m < oo {
		oo = m
	}
	for m > 0 {
		ans += oo
		o--
		m--
		if o <= 0 && ii > 0 {
			ii--
			o = in[ii]
			oo = in[ii]
			if m < oo {
				oo = m
			}
		}
	}
	O(ans, "\n")
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	var n, k int
	var s string

	fmt.Scan(&n, &k)
	fmt.Scan(&s)

	letters := make([]int, 26)
	coins := 0

	for i := 0; i < n; i++ {
		letters[s[i] - 'A']++
	}

	sort.Sort(sort.Reverse(sort.IntSlice(letters)))

	for i := 0; k > 0; {
		if k <= letters[i] {
			coins += k * k
			k = 0
		} else {
			coins += letters[i] * letters[i]
			k -= letters[i]
			i++
		}
		// println(k, coins)
	}

	println(coins)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func nextFloat() float64 {
	n, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

func printf(format string, a ...interface{}) {
	fmt.Fprintf(writer, format, a...)
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	var n, k int
	var s string

	fmt.Scan(&n, &k)
	fmt.Scan(&s)

	letters := make([]int, 26)
	coins := int64(0)

	for i := 0; i < n; i++ {
		letters[s[i] - 'A']++
	}

	sort.Sort(sort.Reverse(sort.IntSlice(letters)))

	for i := 0; k > 0; {
		if k <= letters[i] {
			coins += int64(k * k)
			k = 0
		} else {
			coins += int64(letters[i] * letters[i])
			k -= letters[i]
			i++
		}
		// println(k, coins)
	}

	println(coins)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func nextFloat() float64 {
	n, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

func printf(format string, a ...interface{}) {
	fmt.Fprintf(writer, format, a...)
}

package main

import (
    "fmt"
)

func main() {
    var n, k, i int
    array := make([]int, 26)
    for i = 0; i < 26; i++ {
        array[i] = 0
    }
    fmt.Scanf("%d %d ", &n, &k)
    for i = 0; i < n; i++ {
        var temp int8
        fmt.Scanf("%c", &temp)
        array[temp-'A']++
    }
    var score,coins int
    for coins < k {
        max,max_i := 0,0
        for i=0;i<26;i++{
            if array[i]>max {
                max = array[i]
                max_i = i
            }
        }
        if max > (k-coins) {
            score += ((k-coins) * (k-coins))
            coins += (k-coins)
        }else{
            coins += max
            score += max * max
            array[max_i]=0
        }
    }
    fmt.Printf("%d ", score)
}

package main

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

const l int = 26

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

	cnt := make([]int, l)

	reader := bufio.NewReader(os.Stdin)
	re := regexp.MustCompile(`\r?\n`)
	if s, err := reader.ReadString('\n'); err != nil {
		if err != io.EOF {
			return
		}
	} else {
		s = re.ReplaceAllString(s, "")
		for _, c := range s {
			cnt[int(c-'A')] += 1
		}

		bubble_sort(cnt, l)

		res := 0
		for i := 0; i < l; i++ {
			if k >= cnt[i] {
				res += cnt[i] * cnt[i]
				k -= cnt[i]
			} else {
				res += k * k
				break
			}
		}
		fmt.Println(res)
	}
}

func swap(a, b int, array []int) {
	array[a], array[b] = array[b], array[a]
}

func bubble_sort(array []int, length int) {
	for i := 0; i < length; i++ {
		for j := 0; j < length; j++ {
			if array[i] > array[j] {
				swap(i, j, array)
			}
		}
	}
}

package main

import (
	"fmt"
	"sort"
)

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

	cards := ""
	fmt.Scan(&cards)

	counts := make([]int, 26, 26)
	for i := 0; i < len(cards); i++ {
		counts[cards[i]-'A']++
	}

	sort.Sort(sort.Reverse(sort.IntSlice(counts)))
	ans := 0
	i := 0
	for k > 0 {
		if counts[i] < k {
			k -= counts[i]
			ans += counts[i] * counts[i]
		} else {
			ans += k * k
			k = 0
		}
		i++
	}

	fmt.Println(ans)
}


Monocarp is playing a game "Assimilation IV". In this game he manages a great empire: builds cities and conquers new lands.Monocarp's empire has $$$n$$$ cities. In order to conquer new lands he plans to build one Monument in each city. The game is turn-based and, since Monocarp is still amateur, he builds exactly one Monument per turn.Monocarp has $$$m$$$ points on the map he'd like to control using the constructed Monuments. For each point he knows the distance between it and each city. Monuments work in the following way: when built in some city, a Monument controls all points at distance at most $$$1$$$ to this city. Next turn, the Monument controls all points at distance at most $$$2$$$, the turn after — at distance at most $$$3$$$, and so on. Monocarp will build $$$n$$$ Monuments in $$$n$$$ turns and his empire will conquer all points that are controlled by at least one Monument.Monocarp can't figure out any strategy, so during each turn he will choose a city for a Monument randomly among all remaining cities (cities without Monuments). Monocarp wants to know how many points (among $$$m$$$ of them) he will conquer at the end of turn number $$$n$$$. Help him to calculate the expected number of conquered points!

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 20$$$; $$$1 \le m \le 5 \cdot 10^4$$$) — the number of cities and the number of points. Next $$$n$$$ lines contains $$$m$$$ integers each: the $$$j$$$-th integer of the $$$i$$$-th line $$$d_{i, j}$$$ ($$$1 \le d_{i, j} \le n + 1$$$) is the distance between the $$$i$$$-th city and the $$$j$$$-th point.

It can be shown that the expected number of points Monocarp conquers at the end of the $$$n$$$-th turn can be represented as an irreducible fraction $$$\frac{x}{y}$$$. Print this fraction modulo $$$998\,244\,353$$$, i. e. value $$$x \cdot y^{-1} \bmod 998244353$$$ where $$$y^{-1}$$$ is such number that $$$y \cdot y^{-1} \bmod 998244353 = 1$$$.

NoteLet's look at all possible orders of cities Monuments will be build in:   $$$[1, 2, 3]$$$:   the first city controls all points at distance at most $$$3$$$, in other words, points $$$1$$$ and $$$4$$$;  the second city controls all points at distance at most $$$2$$$, or points $$$1$$$, $$$3$$$ and $$$5$$$;  the third city controls all points at distance at most $$$1$$$, or point $$$1$$$.  In total, $$$4$$$ points are controlled.  $$$[1, 3, 2]$$$: the first city controls points $$$1$$$ and $$$4$$$; the second city — points $$$1$$$ and $$$3$$$; the third city — point $$$1$$$. In total, $$$3$$$ points.  $$$[2, 1, 3]$$$: the first city controls point $$$1$$$; the second city — points $$$1$$$, $$$3$$$ and $$$5$$$; the third city — point $$$1$$$. In total, $$$3$$$ points.  $$$[2, 3, 1]$$$: the first city controls point $$$1$$$; the second city — points $$$1$$$, $$$3$$$ and $$$5$$$; the third city — point $$$1$$$. In total, $$$3$$$ points.  $$$[3, 1, 2]$$$: the first city controls point $$$1$$$; the second city — points $$$1$$$ and $$$3$$$; the third city — points $$$1$$$ and $$$5$$$. In total, $$$3$$$ points.  $$$[3, 2, 1]$$$: the first city controls point $$$1$$$; the second city — points $$$1$$$, $$$3$$$ and $$$5$$$; the third city — points $$$1$$$ and $$$5$$$. In total, $$$3$$$ points.  The expected number of controlled points is $$$\frac{4 + 3 + 3 + 3 + 3 + 3}{6}$$$ $$$=$$$ $$$\frac{19}{6}$$$ or $$$19 \cdot 6^{-1}$$$ $$$\equiv$$$ $$$19 \cdot 166374059$$$ $$$\equiv$$$ $$$166374062$$$ $$$\pmod{998244353}$$$

package main
import (
	."fmt"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	_i, buf := 1<<12, make([]byte, 1<<12)
	rc := func() byte {
		if _i == 1<<12 {
			os.Stdin.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	ri := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}

	const mod = 998244353
	pow := func(x, n int64) int64 {
		x %= mod
		res := int64(1)
		for ; n > 0; n >>= 1 {
			if n&1 > 0 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	inv := func(a int64) int64 { return pow(a, mod-2) }
	div := func(a, b int64) int64 { return a % mod * inv(b) % mod }

	n, m := ri(), ri()
	a := make([][]int8, m)
	for i := range a {
		a[i] = make([]int8, n)
	}
	f := int64(1)
	for i := range a[0] {
		for _, r := range a {
			r[i] = int8(ri())
		}
		f = f * int64(i+1) % mod
	}
	ans := f * int64(m)
o:
	for _, r := range a {
		sort.Slice(r, func(i, j int) bool { return r[i] < r[j] })
		x := int64(1)
		for j, v := range r {
			v -= int8(j + 1)
			if v < 0 {
				continue o
			}
			x = x * int64(v) % mod
		}
		ans -= x
	}
	Print(div(ans%mod+mod, f))
}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	_i, buf := 1<<12, make([]byte, 1<<12)
	rc := func() byte {
		if _i == 1<<12 {
			os.Stdin.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	ri := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}

	const mod = 998244353
	pow := func(x, n int64) int64 {
		x %= mod
		res := int64(1)
		for ; n > 0; n >>= 1 {
			if n&1 > 0 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	inv := func(a int64) int64 { return pow(a, mod-2) }
	div := func(a, b int64) int64 { return a % mod * inv(b) % mod }

	n, m := ri(), ri()
	a := make([][]int8, m)
	for i := range a {
		a[i] = make([]int8, n)
	}
	f := int64(1)
	for i := range a[0] {
		for _, r := range a {
			r[i] = int8(ri())
		}
		f = f * int64(i+1) % mod
	}
	ans := f * int64(m)
o:
	for _, r := range a {
		sort.Slice(r, func(i, j int) bool { return r[i] < r[j] })
		x := int64(1)
		for j, v := range r {
			v -= int8(j + 1)
			if v < 0 {
				continue o
			}
			x = x * int64(v) % mod
		}
		ans -= x
	}
	Print(div(ans%mod+mod, f))
}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	_i, buf := 1<<12, make([]byte, 1<<12)
	rc := func() byte {
		if _i == 1<<12 {
			os.Stdin.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	ri := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}

	const mod = 998244353
	pow := func(x, n int64) int64 {
		x %= mod
		res := int64(1)
		for ; n > 0; n >>= 1 {
			if n&1 > 0 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}
	inv := func(a int64) int64 { return pow(a, mod-2) }
	div := func(a, b int64) int64 { return a % mod * inv(b) % mod }

	n, m := int64(ri()), int64(ri())
	a := make([][]int8, m)
	for i := range a {
		a[i] = make([]int8, n)
	}
	f := int64(1)
	for i := int64(0); i < n; i++ {
		for _, r := range a {
			r[i] = int8(ri())
		}
		f = f * (i + 1) % mod
	}
	ans := f * m
o:
	for _, r := range a {
		sort.Slice(r, func(i, j int) bool { return r[i] < r[j] })
		x := int64(1)
		for j, v := range r {
			v -= int8(j + 1)
			if v < 0 {
				continue o
			}
			x = x * int64(v) % mod
		}
		ans -= x
	}
	Print(div(ans%mod+mod, f))
}

package main
import("bufio";."fmt";."math/big";."os";"sort")

// github.com/EndlessCheng/codeforces-go
func main(){
	in:=bufio.NewReader(Stdin)
	const M=998244353
	var n,m,f int64
	Fscan(in,&n,&m)
	a:=make([][]int,m)
	for i:=range a{a[i]=make([]int,n)}
	f=1
	for i:=int64(0);i<n;i++{
		for _,r:=range a{Fscan(in,&r[i])}
		f=f*(i+1)%M
	}
	ans:=f*m
	for _,r:=range a{
		sort.Ints(r)
		x:=int64(1)
		for j,v:=range r{
			v-=j+1
			if v<0{v=0}
			x=x*int64(v)%M
		}
		ans-=x
	}
	Print((ans%M+M)%M*new(Int).ModInverse(NewInt(f),NewInt(M)).Int64()%M)
}

package main
import("bufio";."fmt";."math/big";."os";"sort")

// github.com/EndlessCheng/codeforces-go
func main(){
	in:=bufio.NewReader(Stdin)
	const M=998244353
	var n,m int64
	Fscan(in,&n,&m)
	a:=make([][]int,m)
	for i:=range a{a[i]=make([]int,n)}
	f:=make([]int64,n+1)
	f[0]=1
	for i:=int64(0);i<n;i++{
		for _,r:=range a{Fscan(in,&r[i])}
		f[i+1]=f[i]*(i+1)%M
	}
	ans:=f[n]*m
	for _,r:=range a{
		sort.Ints(r)
		x:=int64(1)
		for j,v:=range r{
			v-=j+1
			if v<0{v=0}
			x=x*int64(v)%M
		}
		ans-=x
	}
	Print((ans%M+M)%M*new(Int).ModInverse(NewInt(f[n]),NewInt(M)).Int64()%M)
}

package main

import (

	"fmt"

	"math/rand"
	"time"

	"sort"

	"math/big"
	"bufio"
	"os"
)

func main() {

	r := bufio.NewReader(os.Stdin)
	rand.Seed(time.Now().UnixNano())

	var n, m int
	fmt.Fscan(r, &n, &m)
	//n = 5
	//m = 50000

	//for k := 0; k < 100; k++ {

		dist := make([][]int, n)
		for j := 0; j < n; j++ {
			dist[j] = make([]int, m)

			for k := 0; k < m; k++ {
				fmt.Fscan(r, &dist[j][k])
				//dist[j][k] = 1 + rand.Intn(n + 1)
			}
		}

		ans1 := solve(dist, n, m)
		//ans2 := solve_stupid(dist, n, m)
		//ans2 := solve_stupid(dist, n, m)

		/*if ans1 != ans2 {
			fmt.Println(ans1)
			fmt.Println(ans2)
			fmt.Println(dist)

			fmt.Println("ERROR")
			break
		}*/
	//}

	fmt.Println(ans1)
	//fmt.Println(ans2)

}

func solve(dist [][]int, n, m int) int64 {
	fact := make([]int64, 21)
	fact[0] = 1
	for i := 1; i <= 20; i ++ {
		fact[i] = fact[i - 1] * int64(i)
	}

	cnt := big.NewInt(0)

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

		l := make([]int, n)

		for j := 0; j < n; j++ {
			//1 1 2 1 3 4 1 5 2 3 4 6 8 1 2 8 3 5 3
			d := dist[j][i]
			l[j] = d
		}

		offset := 1
		var cntPerPoint int64

		sort.Ints(l)

		for j := 0; j < n; j++ {
			d := l[j]
			if d > n {
				break
			}
			if d == 1 {
				cntPerPoint = fact[n]
				break
			}

			lat := n - d + 1

			f := int64(lat) * fact[n - offset]
			offset++

			delta := 0
			for k := 0; k < j; k++ {
				v := l[k] - 1 - delta
				delta++

				f *= int64(v)
			}

			cntPerPoint += f
		}

		cnt = cnt.Add(cnt, big.NewInt(cntPerPoint))
	}

	//fmt.Println(cnt)

	modInv := modInverse(fact[n], 998244353)

	z := cnt.Mul(cnt, big.NewInt(modInv))
	z = z.Mod(z, big.NewInt(998244353))

	return z.Int64()
}

func solve_stupid(dist [][]int, n, m int) int64 {
	fact := make([]int64, 21)
	fact[0] = 1
	for i := 1; i <= 20; i ++ {
		fact[i] = fact[i - 1] * int64(i)
	}

	scores := make([][][]int, n)
	for j := 0; j < n; j++ {
		scores[j] = make([][]int, n + 1)
	}

	orig := make([]int, n)
	for j := 0; j < n; j++ {
		orig[j] = j
	}

	var cnt int64

	for p := make([]int, len(orig)); p[0] < len(p); nextPerm(p) {
		perm := getPerm(orig, p)

		points := make([]bool, m)

		for i := 0; i < len(perm); i ++ {
			c := perm[i]
			maxDist := n - i

			for k := 0; k < len(dist[c]); k++ {
				if dist[c][k] <= maxDist {
					points[k] = true
				}
			}
		}

		for j := 0; j < len(points); j++ {
			if points[j] {
				cnt++
			}
		}
	}

	//fmt.Println(cnt)

	modInv := modInverse(fact[n], 998244353)

	return int64(modInv) * cnt % 998244353

}

func nextPerm(p []int) {
	for i := len(p) - 1; i >= 0; i-- {
		if i == 0 || p[i] < len(p)-i-1 {
			p[i]++
			return
		}
		p[i] = 0
	}
}

func getPerm(orig, p []int) []int {
	result := append([]int{}, orig...)
	for i, v := range p {
		result[i], result[i+v] = result[i+v], result[i]
	}
	return result
}

func modInverse(a int64, m int64) int64 {
	var x, y int64
    g := gcdExtended(a, m, &x, &y)

	if g != 1 {
		panic( "Inverse doesn't exist")
	} else {

	// m is added to handle negative x
	res := (x % m + m) % m
	return res
	}
}

func gcdExtended(a int64, b int64, x *int64, y *int64) int64 {

	// Base Case
	if a == 0 {
		*x, *y = 0,1
		return b
	}

	// To store results of recursive call
	var x1, y1 int64
	gcd := gcdExtended(b%a, a, &x1, &y1);

	// Update x and y using results of recursive
	// call
	*x = y1 - (b/a)*x1;
	*y = x1
	return gcd
}

package main
import("bufio";."fmt";."math/big";."os";"sort")

// github.com/EndlessCheng/codeforces-go
func main(){
	in:=bufio.NewReader(Stdin)
	const M=998244353
	var n,m,f int64
	Fscan(in,&n,&m)
	a:=make([][]int,m)
	for i:=range a{a[i]=make([]int,n)}
	f=1
	for i:=int64(0);i<n;i++{
		for _,r:=range a{Fscan(in,&r[i])}
		f*=i+1
	}
	ans:=f*m
	for _,r:=range a{
		sort.Ints(r)
		x:=int64(1)
		for j,v:=range r{
			v-=j+1
			if v<0{v=0}
			x=x*int64(v)%M
		}
		ans-=x
	}
	Print((ans%M+M)%M*new(Int).ModInverse(NewInt(f),NewInt(M)).Int64()%M)
}

package main

import (

	"fmt"

	"math/rand"
	"time"

	"sort"

	"bufio"
	"os"
)

func main() {

	r := bufio.NewReader(os.Stdin)
	rand.Seed(time.Now().UnixNano())

	var n, m int
	fmt.Fscan(r, &n, &m)
	//n = 10
	//m = 500

	//for k := 0; k < 1000; k++ {

		dist := make([][]int, n)
		for j := 0; j < n; j++ {
			dist[j] = make([]int, m)

			for k := 0; k < m; k++ {
				fmt.Fscan(r, &dist[j][k])
				//dist[j][k] = 1 + rand.Intn(n + 1)
			}
		}

		ans1 := solve(dist, n, m)
		//ans2 := solve_stupid(dist, n, m)
		//ans2 := solve_stupid(dist, n, m)

		/*if ans1 != ans2 {
			fmt.Println(ans1)
			fmt.Println(ans2)
			fmt.Println(dist)

			fmt.Println("ERROR")
			break
		}*/
	//}

	fmt.Println(ans1)
	//fmt.Println(ans2)

}

func solve(dist [][]int, n, m int) int64 {
	fact := make([]int64, 21)
	fact[0] = 1
	for i := 1; i <= 20; i ++ {
		fact[i] = fact[i - 1] * int64(i)
	}

	var cnt int64

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

		l := make([]int, n)

		for j := 0; j < n; j++ {
			//1 1 2 1 3 4 1 5 2 3 4 6 8 1 2 8 3 5 3
			d := dist[j][i]
			l[j] = d
		}

		offset := 1
		var cntPerPoint int64

		sort.Ints(l)

		for j := 0; j < n; j++ {
			d := l[j]
			if d > n {
				break
			}
			if d == 1 {
				cntPerPoint = fact[n]
				break
			}

			lat := n - d + 1

			f := int64(lat) * fact[n - offset]
			offset++

			delta := 0
			for k := 0; k < j; k++ {
				v := l[k] - 1 - delta
				delta++

				f *= int64(v)
			}

			cntPerPoint += f
		}

		cnt += cntPerPoint
	}

	//fmt.Println(cnt)

	modInv := modInverse(fact[n], 998244353)

	return (int64(modInv) * cnt) % 998244353
}

func solve_stupid(dist [][]int, n, m int) int64 {
	fact := make([]int64, 21)
	fact[0] = 1
	for i := 1; i <= 20; i ++ {
		fact[i] = fact[i - 1] * int64(i)
	}

	scores := make([][][]int, n)
	for j := 0; j < n; j++ {
		scores[j] = make([][]int, n + 1)
	}

	orig := make([]int, n)
	for j := 0; j < n; j++ {
		orig[j] = j
	}

	var cnt int64

	for p := make([]int, len(orig)); p[0] < len(p); nextPerm(p) {
		perm := getPerm(orig, p)

		points := make([]bool, m)

		for i := 0; i < len(perm); i ++ {
			c := perm[i]
			maxDist := n - i

			for k := 0; k < len(dist[c]); k++ {
				if dist[c][k] <= maxDist {
					points[k] = true
				}
			}
		}

		for j := 0; j < len(points); j++ {
			if points[j] {
				cnt++
			}
		}
	}

	//fmt.Println(cnt)

	modInv := modInverse(fact[n], 998244353)

	return int64(modInv) * cnt % 998244353

}

func nextPerm(p []int) {
	for i := len(p) - 1; i >= 0; i-- {
		if i == 0 || p[i] < len(p)-i-1 {
			p[i]++
			return
		}
		p[i] = 0
	}
}

func getPerm(orig, p []int) []int {
	result := append([]int{}, orig...)
	for i, v := range p {
		result[i], result[i+v] = result[i+v], result[i]
	}
	return result
}

func modInverse(a int64, m int64) int64 {
	var x, y int64
    g := gcdExtended(a, m, &x, &y)

	if g != 1 {
		panic( "Inverse doesn't exist")
	} else {

	// m is added to handle negative x
	res := (x % m + m) % m
	return res
	}
}

func gcdExtended(a int64, b int64, x *int64, y *int64) int64 {

	// Base Case
	if a == 0 {
		*x, *y = 0,1
		return b
	}

	// To store results of recursive call
	var x1, y1 int64
	gcd := gcdExtended(b%a, a, &x1, &y1);

	// Update x and y using results of recursive
	// call
	*x = y1 - (b/a)*x1;
	*y = x1
	return gcd
}

package main

import (

	"fmt"

	"math/rand"
	"time"

	"sort"
	"bufio"
	"os"
)

func main() {

	r := bufio.NewReader(os.Stdin)
	rand.Seed(time.Now().UnixNano())

	var n, m int
	fmt.Fscan(r, &n, &m)
	//n = 4
	//m = 5

	//for k := 0; k < 100; k++ {

		dist := make([][]int, n)
		for j := 0; j < n; j++ {
			dist[j] = make([]int, m)

			for k := 0; k < m; k++ {
				fmt.Fscan(r, &dist[j][k])
				//dist[j][k] = 1 + rand.Intn(n + 1)
			}
		}

		ans1 := solve(dist, n, m)
		//ans2 := solve_stupid(dist, n, m)
		//ans2 := solve_stupid(dist, n, m)

		/*if ans1 != ans2 {
			fmt.Println(ans1)
			fmt.Println(ans2)
			fmt.Println(dist)

			fmt.Println("ERROR")
			break
		}*/
	//}

	fmt.Println(ans1)
	//fmt.Println(ans2)

}

func solve(dist [][]int, n, m int) int64 {
	fact := make([]int64, 21)
	fact[0] = 1
	for i := 1; i <= 20; i ++ {
		fact[i] = fact[i - 1] * int64(i)
	}

	var cnt int64

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

		l := make([]int, n)

		for j := 0; j < n; j++ {
			//1 1 2 1 3 4 1 5 2 3 4 6 8 1 2 8 3 5 3
			d := dist[j][i]
			l[j] = d
		}

		offset := 1
		var cntPerPoint int64

		sort.Ints(l)

		for j := 0; j < n; j++ {
			d := l[j]
			if d > n {
				break
			}
			if d == 1 {
				cntPerPoint = fact[n]
				break
			}

			lat := n - d + 1

			f := int64(lat) * fact[n - offset]
			offset++

			delta := 0
			for k := 0; k < j; k++ {
				v := l[k] - 1 - delta
				delta++

				f *= int64(v)
			}

			cntPerPoint += f
		}

		cnt += cntPerPoint
	}

	//fmt.Println(cnt)

	modInv := modInverse(fact[n], 998244353)

	return int64(modInv) * cnt % 998244353
}

func solve_stupid(dist [][]int, n, m int) int64 {
	fact := make([]int64, 21)
	fact[0] = 1
	for i := 1; i <= 20; i ++ {
		fact[i] = fact[i - 1] * int64(i)
	}

	scores := make([][][]int, n)
	for j := 0; j < n; j++ {
		scores[j] = make([][]int, n + 1)
	}

	orig := make([]int, n)
	for j := 0; j < n; j++ {
		orig[j] = j
	}

	var cnt int64

	for p := make([]int, len(orig)); p[0] < len(p); nextPerm(p) {
		perm := getPerm(orig, p)

		points := make([]bool, m)

		for i := 0; i < len(perm); i ++ {
			c := perm[i]
			maxDist := n - i

			for k := 0; k < len(dist[c]); k++ {
				if dist[c][k] <= maxDist {
					points[k] = true
				}
			}
		}

		for j := 0; j < len(points); j++ {
			if points[j] {
				cnt++
			}
		}
	}

	fmt.Println(cnt)

	modInv := modInverse(fact[n], 998244353)

	return int64(modInv) * cnt % 998244353

}

func nextPerm(p []int) {
	for i := len(p) - 1; i >= 0; i-- {
		if i == 0 || p[i] < len(p)-i-1 {
			p[i]++
			return
		}
		p[i] = 0
	}
}

func getPerm(orig, p []int) []int {
	result := append([]int{}, orig...)
	for i, v := range p {
		result[i], result[i+v] = result[i+v], result[i]
	}
	return result
}

func modInverse(a int64, m int64) int64 {
	var x, y int64
    g := gcdExtended(a, m, &x, &y)

	if g != 1 {
		panic( "Inverse doesn't exist")
	} else {

	// m is added to handle negative x
	res := (x % m + m) % m
	return res
	}
}

func gcdExtended(a int64, b int64, x *int64, y *int64) int64 {

	// Base Case
	if a == 0 {
		*x, *y = 0,1
		return b
	}

	// To store results of recursive call
	var x1, y1 int64
	gcd := gcdExtended(b%a, a, &x1, &y1);

	// Update x and y using results of recursive
	// call
	*x = y1 - (b/a)*x1;
	*y = x1
	return gcd
}

package main

import (

	"fmt"

	"math/rand"
	"time"
	"bufio"
	"os"
)

func main() {

	r := bufio.NewReader(os.Stdin)
	rand.Seed(time.Now().UnixNano())

	var n, m int
	fmt.Fscan(r, &n, &m)
	//n = 4
	//m = 5

	//for k := 0; k < 100; k++ {

		dist := make([][]int, n)
		for j := 0; j < n; j++ {
			dist[j] = make([]int, m)

			for k := 0; k < m; k++ {
				fmt.Fscan(r, &dist[j][k])
				//dist[j][k] = 1 + rand.Intn(n + 1)
			}
		}

		//ans1 := solve(dist, n, m)
		ans2 := solve_stupid(dist, n, m)
		//ans2 := solve_stupid(dist, n, m)

	/*	if ans1 != ans2 {
			fmt.Println(ans1)
			fmt.Println(ans2)
			fmt.Println(dist)

			fmt.Println("ERROR")
			break
		}*/
	//}

	//fmt.Println(ans1)
	fmt.Println(ans2)

}

func solve(dist [][]int, n, m int) int64 {
	fact := make([]int64, 21)
	fact[0] = 1
	for i := 1; i <= 20; i ++ {
		fact[i] = fact[i - 1] * int64(i)
	}

	var cnt int64

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

		l := make([]int, n)

		for j := 0; j < n; j++ {
			//1 1 2 1 3 4 1 5 2 3 4 6 8 1 2 8 3 5 3
			d := dist[j][i]
			latest := n - d + 1

			l[j] = latest
		}

		p := 0

		for j := 0; j < n; j++ {
			lat := l[j]
			if lat <= 0 {
				continue
			}

			s := int64(lat) * fact[n - p - 1]
			p++

			for k := j - 1; k >= 0; k-- {
				if l[k] <= 0 {
					continue
				}
				s *= int64(n) - int64(l[k])
			}

			cnt += s
		}
	}

	fmt.Println(cnt)

	modInv := modInverse(fact[n], 998244353)

	return int64(modInv) * cnt % 998244353
}

func solve_stupid(dist [][]int, n, m int) int64 {
	fact := make([]int64, 21)
	fact[0] = 1
	for i := 1; i <= 20; i ++ {
		fact[i] = fact[i - 1] * int64(i)
	}

	scores := make([][][]int, n)
	for j := 0; j < n; j++ {
		scores[j] = make([][]int, n + 1)
	}

	orig := make([]int, n)
	for j := 0; j < n; j++ {
		orig[j] = j
	}

	var cnt int64

	for p := make([]int, len(orig)); p[0] < len(p); nextPerm(p) {
		perm := getPerm(orig, p)

		points := make([]bool, m)

		for i := 0; i < len(perm); i ++ {
			c := perm[i]
			maxDist := n - i

			for k := 0; k < len(dist[c]); k++ {
				if dist[c][k] <= maxDist {
					points[k] = true
				}
			}
		}

		for j := 0; j < len(points); j++ {
			if points[j] {
				cnt++
			}
		}
	}

	fmt.Println(cnt)

	modInv := modInverse(fact[n], 998244353)

	return int64(modInv) * cnt % 998244353

}

func nextPerm(p []int) {
	for i := len(p) - 1; i >= 0; i-- {
		if i == 0 || p[i] < len(p)-i-1 {
			p[i]++
			return
		}
		p[i] = 0
	}
}

func getPerm(orig, p []int) []int {
	result := append([]int{}, orig...)
	for i, v := range p {
		result[i], result[i+v] = result[i+v], result[i]
	}
	return result
}

func modInverse(a int64, m int64) int64 {
	var x, y int64
    g := gcdExtended(a, m, &x, &y)

	if g != 1 {
		panic( "Inverse doesn't exist")
	} else {

	// m is added to handle negative x
	res := (x % m + m) % m
	return res
	}
}

func gcdExtended(a int64, b int64, x *int64, y *int64) int64 {

	// Base Case
	if a == 0 {
		*x, *y = 0,1
		return b
	}

	// To store results of recursive call
	var x1, y1 int64
	gcd := gcdExtended(b%a, a, &x1, &y1);

	// Update x and y using results of recursive
	// call
	*x = y1 - (b/a)*x1;
	*y = x1
	return gcd
}

package main

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

func main() {

	r := bufio.NewReader(os.Stdin)

	var n, m int
	fmt.Fscan(r, &n, &m)

	dist := make([][]int, n)
	for j := 0; j < n; j++ {
		dist[j] = make([]int, m)

		for k := 0; k < m; k++ {
			fmt.Fscan(r, &dist[j][k])
		}
	}

	ans := solve(dist, n, m)
	fmt.Println(ans)

}

func solve(dist [][]int, n, m int) int64 {
	fact := make([]int64, 21)
	fact[0] = 1
	for i := 1; i <= 20; i ++ {
		fact[i] = fact[i - 1] * int64(i)
	}

	var cnt int64

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

		l := make([]int, n)

		for j := 0; j < n; j++ {
			//1 1 2 1 3 4 1 5 2 3 4 6 8 1 2 8 3 5 3
			d := dist[j][i]
			latest := n - d + 1

			l[j] = latest
		}

		p := 0

		for j := 0; j < n; j++ {
			lat := l[j]
			if lat <= 0 {
				continue
			}

			s := int64(lat) * fact[n - p - 1]
			p++

			for k := j - 1; k >= 0; k-- {
				if l[k] <= 0 {
					continue
				}
				s *= int64(n) - int64(l[k])
			}

			cnt += s
		}
	}

	modInv := modInverse(fact[n], 998244353)

	return int64(modInv) * cnt % 998244353
}

func modInverse(a int64, m int64) int64 {
	var x, y int64
    g := gcdExtended(a, m, &x, &y)

	if g != 1 {
		panic( "Inverse doesn't exist")
	} else {

	// m is added to handle negative x
	res := (x % m + m) % m
	return res
	}
}

func gcdExtended(a int64, b int64, x *int64, y *int64) int64 {

	// Base Case
	if a == 0 {
		*x, *y = 0,1
		return b
	}

	// To store results of recursive call
	var x1, y1 int64
	gcd := gcdExtended(b%a, a, &x1, &y1);

	// Update x and y using results of recursive
	// call
	*x = y1 - (b/a)*x1;
	*y = x1
	return gcd
}


Consider a conveyor belt represented using a grid consisting of $$$n$$$ rows and $$$m$$$ columns. The cell in the $$$i$$$-th row from the top and the $$$j$$$-th column from the left is labelled $$$(i,j)$$$. Every cell, except $$$(n,m)$$$, has a direction R (Right) or D (Down) assigned to it. If the cell $$$(i,j)$$$ is assigned direction R, any luggage kept on that will move to the cell $$$(i,j+1)$$$. Similarly, if the cell $$$(i,j)$$$ is assigned direction D, any luggage kept on that will move to the cell $$$(i+1,j)$$$. If at any moment, the luggage moves out of the grid, it is considered to be lost. There is a counter at the cell $$$(n,m)$$$ from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell $$$(i,j)$$$, any luggage placed in this cell should eventually end up in the cell $$$(n,m)$$$. This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n, m$$$ ($$$1 \le n \le 100$$$, $$$1 \le m \le 100$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines each contain $$$m$$$ characters. The $$$j$$$-th character in the $$$i$$$-th line, $$$a_{i,j}$$$ is the initial direction of the cell $$$(i, j)$$$. Please note that $$$a_{n,m}=$$$ C.

For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.

4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C

NoteIn the first case, just changing the direction of $$$(2,3)$$$ to D is enough.You can verify that the resulting belt is functional. For example, if we place any luggage at $$$(2,2)$$$, it first moves to $$$(3,2)$$$ and then to $$$(3,3)$$$. In the second case, we have no option but to change the first $$$3$$$ cells from D to R making the grid equal to RRRC.

package main

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

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

		var line string
		var count = 0
		for j := 1; j <= n; j++ {
			fmt.Fscan(in, &line)
			if j == n {
				for k := 0; k < m-1; k++ {
					if line[k] != 'R' {
						count++
					}
				}
			} else {
				if line[m-1] != 'D' {
					count++
				}
			}
		}
		fmt.Println(count)
	}
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var T, n, m int
	var str[105] string;
	fmt.Fscan(input, &T)
	for t := 0; t < T; t++ {
		fmt.Fscan(input, &n)
		fmt.Fscan(input, &m)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &str[i])
		}
		ret := 0
		for i := 0; i < n; i++ {
			if str[i][m - 1] == 'R' {
				ret += 1
			}
		}
		for j := 0; j < m; j++ {
			if str[n - 1][j] == 'D' {
				ret += 1
			}
		}
		fmt.Printf("%d\n", ret)
	}
}

/*
URL:
https://codeforces.com/contest/1391/problem/B
*/

package main

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

/*******************************************************************/

const (
	// General purpose
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

var (
	t int

	n, m int
	C    [][]rune
)

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		n, m = ReadInt2()
		C = [][]rune{}

		for i := 0; i < n; i++ {
			row := ReadRuneSlice()
			C = append(C, row)
		}

		solve()
	}
}

func solve() {
	ans := 0

	for j := 0; j < m-1; j++ {
		if C[n-1][j] != 'R' {
			ans++
		}
	}
	for i := 0; i < n-1; i++ {
		if C[i][m-1] != 'D' {
			ans++
		}
	}

	fmt.Println(ans)
}

/*******************************************************************/

/********** bufio setting **********/

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*********** Input ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Output ***********/

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

/*********** Debugging ***********/

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

package main

import "fmt"

const maxn = 100 + 10

var mp [maxn][maxn]int
var s string
var n, m int

func myinit(n, m int) {
	for i := 0; i <= n; i++ {
		for j := 0; j <= m; j++ {
			mp[i][j] = 0
		}
	}
}
func isborder(x, y int) bool {
	if x == n && y == m {
		return false
	}
	if x == 1 || x == n || y == 1 || y == m {
		return true
	}
	return false
}
func main() {
	var t int
	fmt.Scan(&t)
	for tim := 1; tim <= t; tim++ {
		fmt.Scan(&n, &m)
		myinit(n, m)
		for i := 1; i <= n; i++ {
			fmt.Scan(&s)
			length := len(s)
			for j := 0; j < length; j++ {
				if s[j] == 'R' {
					mp[i][j+1] = 1
				}
			}
		}
		ans := 0
		for i := 1; i <= n; i++ {
			for j := 1; j <= m; j++ {
				if !isborder(i, j) {
					continue
				}
				if mp[i][j] == 0 {
					if i == n {
						ans++
					}
				} else {
					if j == m {
						ans++
					}
				}
			}
		}
		fmt.Println(ans)
	}
}

package main

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

const N = 105

var t, n, m int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

	for qq := 0; qq < t; qq++ {
		fmt.Fscan(input, &n, &m)
		s := make([]string, n)

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

		ans := 0

		for i := 0; i < n; i++ {
			if s[i][m-1] == 'R' {
				ans += 1
			}
		}

		for i := 0; i < m; i++ {
			if s[n-1][i] == 'D' {
				ans += 1
			}
		}

		fmt.Println(ans)
	}
}

package main

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

const N = 105

var t, n, m int
var s [N]string

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

	for qq := 0; qq < t; qq++ {
		fmt.Fscan(input, &n, &m)

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

		ans := 0

		for i := 0; i < n; i++ {
			if s[i][m-1] == 'R' {
				ans += 1
			}
		}

		for i := 0; i < m; i++ {
			if s[n-1][i] == 'D' {
				ans += 1
			}
		}

		fmt.Println(ans)
	}
}

package main

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

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

	var t, n, m int

	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &n, &m)

		var chars string

		arr := make([][]rune, n)
		for j := range arr {
			fmt.Fscan(r, &chars)
			arr[j] = []rune(chars)
		}
		count := 0

		for row := 0; row < n; row++ {
			for col := 0; col < m; col++ {
				if col == m-1 && arr[row][col] == rune('R') {
					count++
				} else if row == n-1 && arr[row][col] == rune('D') {
					count++
				}
			}
		}

		fmt.Fprintln(w, count)
	}
}

package main

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

type contest struct {
	in  io.Reader
	out io.Writer
}

func (con *contest) Scan(a ...interface{}) (int, error) {
	return fmt.Fscan(con.in, a...)
}
func (con *contest) Scanln(a ...interface{}) (int, error) {
	return fmt.Fscanln(con.in, a...)
}
func (con *contest) Scanf(f string, a ...interface{}) (int, error) {
	return fmt.Fscanf(con.in, f, a...)
}
func (con *contest) Print(a ...interface{}) (int, error) {
	return fmt.Fprint(con.out, a...)
}
func (con *contest) Println(a ...interface{}) (int, error) {
	return fmt.Fprintln(con.out, a...)
}
func (con *contest) Printf(f string, a ...interface{}) (int, error) {
	return fmt.Fprintf(con.out, f, a...)
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	con := &contest{in: in, out: out}
	con.main()
}

func (con *contest) main() error {
	var testcases int
	con.Scan(&testcases)
	for testcase := 0; testcase < testcases; testcase++ {
		var n, m int
		con.Scan(&n, &m)
		r := make([]string, n)
		for i := 0; i < n; i++ {
			con.Scan(&r[i])
		}
		c := 0
		for i := 0; i < n-1; i++ {
			if r[i][m-1] != 'D' {
				c++
			}
		}
		for i := 0; i < m-1; i++ {
			if r[n-1][i] != 'R' {
				c++
			}
		}
		con.Println(c)
	}
	return nil
}

package main

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

var reader *bufio.Reader
var writer *bufio.Writer

func solve(dir []string) int {
	rows, cols := len(dir), len(dir[0])
	ans := 0
	for i := 0; i+1 < rows; i++ {
		if dir[i][cols-1] != 'D' {
			ans++
		}
	}
	for i := 0; i+1 < cols; i++ {
		if dir[rows-1][i] != 'R' {
			ans++
		}
	}
	return ans
}

func main() {
	//ioutil.WriteFile("input.txt", []byte(generateInput()), os.ModeExclusive)

	debug := 1
	filename := "input.txt"
	if debug != 0 && len(os.Args) > 1 && os.Args[1] == "debug" {
		file, _ := os.Open(filename)
		reader = bufio.NewReaderSize(file, 1024*1024)
	} else {
		reader = bufio.NewReaderSize(os.Stdin, 1024*1024)
	}
	writer = bufio.NewWriterSize(os.Stdout, 1024*1024)

	t := readInt()
	for ; t > 0; t-- {
		arr := readInts()
		dir := make([]string, arr[0])
		for i := range dir {
			dir[i] = readLine()
		}
		ans := solve(dir)
		fmt.Println(ans)
	}

	writer.Flush()
}

func readInts() []int {
	line := readLine()
	fields := strings.Fields(line)
	res := make([]int, len(fields))
	for i := range fields {
		v, _ := strconv.ParseInt(fields[i], 10, 64)
		res[i] = int(v)
	}
	return res
}

func readInt() int {
	line := readLine()
	n, _ := strconv.ParseInt(line, 10, 64)
	return int(n)
}

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

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

package main

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

var (
	reader *bufio.Reader
	writer *bufio.Writer
)

func init() {
	reader = bufio.NewReader(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)
}
func scanf(format string, args ...interface{}) (int, error) {
	return fmt.Fscanf(reader, format, args...)
}

func printf(format string, args ...interface{}) (int, error) {
	return fmt.Fprintf(writer, format, args...)
}

func getAddresses(n []int) []interface{} {
	adds := make([]interface{}, len(n))
	for i := 0; i < len(n); i++ {
		adds[i] = &n[i]
	}
	return adds
}

func main() {
	defer writer.Flush()
	var t int
	var n, m int
	scanf("%d\n", &t)
	results := make([]int, t)
	for i := 0; i < t; i++ {
		scanf("%d %d\n", &n, &m)
		change := 0
		for j := 0; j < n; j++ {
			var str string
			scanf("%s\n", &str)
			if j == n-1 {
				for k, c := range str {
					if k == m-1 {
						break
					}
					if string(c) != "R" {
						change++
					}
				}
			} else {
				if str[len(str)-1:] != "D" {
					change++
				}
			}
		}
		results[i] = change
	}
	for i := 0; i < t; i++ {
		printf("%d\n", results[i])
	}

}

package main

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

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

type priorityQueue [][2]int

func (pq priorityQueue) Len() int {
	return len(pq)
}

func (pq priorityQueue) Less(i, j int) bool {
	return pq[i][1] > pq[j][1]
}

func (pq priorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *priorityQueue) Push(x interface{}) {
	*pq = append(*pq, x.([2]int))
}

func (pq *priorityQueue) Pop() (res interface{}) {
	res = (*pq)[len(*pq)-1]
	*pq = (*pq)[:len(*pq)-1]
	return res
}

var err error

func next() (n string) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextLong() (n int64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextDouble() (n float64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func Fill(a []int, v int) {
	for i := range a {
		a[i] = v
	}
}

func FillB(a []bool, v bool) {
	for i := range a {
		a[i] = v
	}
}

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

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

var (
	dx [4]int = [4]int{-1, 0, 1, 0}
	dy [4]int = [4]int{0, 1, 0, -1}
)

func solve() {
	n, m := nextInt(), nextInt()
	arr := make([][]byte, n)
	for i := range arr {
		arr[i] = []byte(next())
	}
	res := 0
	for i := 0; i < n-1; i++ {
		if arr[i][m-1] != 'D' {
			res++
		}
	}
	for i := 0; i < m-1; i++ {
		if arr[n-1][i] != 'R' {
			res++
		}
	}
	printf("%d\n", res)
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	if false {
		name := "in"
		file1, err := os.Open(name + ".in")
		if err != nil {
			log.Fatal(err)
		}
		// file2, err := os.Create(name + ".out")
		// if err != nil {
		// 	log.Fatal(err)
		// }
		in = bufio.NewReaderSize(file1, 8192)
		// out = bufio.NewWriterSize(file2, 8192)
	}
	defer out.Flush()

	for t := nextInt(); t > 0; t-- {
		solve()
	}
}

package main

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

func main() {
	var t, n, m int
	var s string
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n, &m)
		ans := 0
		for j := 0; j < n-1; j++ {
			fmt.Fscan(in, &s)
			if s[m-1] == 'R' {
				ans++
			}
		}
		fmt.Fscan(in, &s)
		for j := 0; j < m; j++ {
			if s[j] == 'D' {
				ans++
			}
		}
		fmt.Println(ans)
	}
}

package main

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

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	for t := io.NextInt(); t > 0; t-- {
		n, m := io.NextInt(), io.NextInt()
		g := make([]string, n)
		for i := range g {
			g[i] = io.Next()
		}

		ans := 0
		for i := range g {
			if g[i][m-1] == 'R' {
				ans++
			}
		}
		for j := range g[0] {
			if g[n-1][j] == 'D' {
				ans++
			}
		}
		io.Println(ans)
	}
}

type FastIO struct {
	*bufio.Reader
	*bufio.Writer
	c [1]byte
}

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

func (f *FastIO) readC() byte {
	f.Read(f.c[:])
	return f.c[0]
}

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) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}

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

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

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f.Writer, 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()
}


You are given a connected undirected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. The weight of the $$$i$$$-th edge is $$$i$$$.Here is a wrong algorithm of finding a minimum spanning tree (MST) of a graph:vis := an array of length ns := a set of edgesfunction dfs(u):    vis[u] := true    iterate through each edge (u, v) in the order from smallest to largest edge weight        if vis[v] = false            add edge (u, v) into the set (s)            dfs(v)function findMST(u):    reset all elements of (vis) to false    reset the edge set (s) to empty    dfs(u)    return the edge set (s)Each of the calls findMST(1), findMST(2), ..., findMST(n) gives you a spanning tree of the graph. Determine which of these trees are minimum spanning trees.

The first line of the input contains two integers $$$n$$$, $$$m$$$ ($$$2\le n\le 10^5$$$, $$$n-1\le m\le 2\cdot 10^5$$$) — the number of vertices and the number of edges in the graph. Each of the following $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1\le u_i, v_i\le n$$$, $$$u_i\ne v_i$$$), describing an undirected edge $$$(u_i,v_i)$$$ in the graph. The $$$i$$$-th edge in the input has weight $$$i$$$. It is guaranteed that the graph is connected and there is at most one edge between any pair of vertices.

You need to output a binary string $$$s$$$, where $$$s_i=1$$$ if findMST(i) creates an MST, and $$$s_i = 0$$$ otherwise.

NoteHere is the graph given in the first example.  There is only one minimum spanning tree in this graph. A minimum spanning tree is $$$(1,2),(3,5),(1,3),(2,4)$$$ which has weight $$$1+2+3+5=11$$$.Here is a part of the process of calling findMST(1):  reset the array vis and the edge set s;  calling dfs(1);  vis[1] := true;  iterate through each edge $$$(1,2),(1,3)$$$;  add edge $$$(1,2)$$$ into the edge set s, calling dfs(2):   vis[2] := true  iterate through each edge $$$(2,1),(2,3),(2,4)$$$;  because vis[1] = true, ignore the edge $$$(2,1)$$$;  add edge $$$(2,3)$$$ into the edge set s, calling dfs(3):   ...   In the end, it will select edges $$$(1,2),(2,3),(3,5),(2,4)$$$ with total weight $$$1+4+2+5=12>11$$$, so findMST(1) does not find a minimum spanning tree.It can be shown that the other trees are all MSTs, so the answer is 01111.

package main

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

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

	n, m := readTwoNums(reader)

	edges := make([][]int, m)

	for i := 0; i < m; i++ {
		edges[i] = readNNums(reader, 2)
	}

	res := solve(n, edges)

	fmt.Println(res)
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' || s[i] == '\r' {
			return s[:i]
		}
	}
	return s
}

func normalize(s string) string {

	for i := len(s); i > 0; i-- {
		if s[i-1] >= 'a' && s[i-1] <= 'z' {
			return s[:i]
		}
	}
	return ""
}

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
}

const H = 22

func solve(n int, edges [][]int) string {

	g := NewGraph(n, 2*(n-1))

	uf := NewUF(n)

	var es []Edge

	for _, edge := range edges {
		u, v := edge[0], edge[1]
		u--
		v--
		if !uf.Union(u, v) {
			// cross edge
			es = append(es, Edge{u, v})
		} else {
			g.AddEdge(u, v)
			g.AddEdge(v, u)
		}
	}

	D := make([]int, n)
	P := make([][]int, n)

	var dfs func(p int, u int)

	dfs = func(p int, u int) {
		P[u] = make([]int, H)
		P[u][0] = p
		for i := 1; i < H; i++ {
			P[u][i] = P[P[u][i-1]][i-1]
		}
		for i := g.node[u]; i > 0; i = g.next[i] {
			v := g.to[i]
			if v != p {
				D[v] = D[u] + 1
				dfs(u, v)
			}
		}
	}

	dfs(0, 0)

	lca := func(u int, v int) int {
		if D[u] < D[v] {
			u, v = v, u
		}
		for i := H - 1; i >= 0; i-- {
			if (D[u] - (1 << i)) >= D[v] {
				u = P[u][i]
			}
		}
		if u == v {
			return u
		}
		for i := H - 1; i >= 0; i-- {
			if P[u][i] != P[v][i] {
				u = P[u][i]
				v = P[v][i]
			}
		}
		return P[u][0]
	}

	jump := func(x int, t int) int {
		for i := 0; t > 0; t, i = t>>1, i+1 {
			if t&1 == 1 {
				x = P[x][i]
			}
		}
		return x
	}

	vis := make([]int, n)

	for i := 0; i < len(es); i++ {
		u, v := es[i].u, es[i].v

		if D[u] > D[v] {
			u, v = v, u
		}
		// u is higher
		w := lca(u, v)
		if u == w {
			vis[v]--
			vis[jump(v, D[v]-D[u]-1)]++
		} else {
			vis[0]++
			vis[u]--
			vis[v]--
		}
	}

	res := make([]byte, n)

	var push func(p int, u int)

	push = func(p int, u int) {
		if vis[u] == 0 {
			res[u] = '1'
		} else {
			res[u] = '0'
		}

		for i := g.node[u]; i > 0; i = g.next[i] {
			v := g.to[i]
			if p != v {
				vis[v] += vis[u]
				push(u, v)
			}
		}
	}

	push(0, 0)

	return string(res)
}

type Edge struct {
	u int
	v int
}

type Graph struct {
	node []int
	next []int
	to   []int
	cur  int
}

func NewGraph(n int, e int) *Graph {
	g := new(Graph)
	g.node = make([]int, n)
	g.to = make([]int, e+1)
	g.next = make([]int, e+1)
	g.cur = 0
	return g
}

func (g *Graph) AddEdge(u, v int) {
	g.cur++
	g.next[g.cur] = g.node[u]
	g.node[u] = g.cur
	g.to[g.cur] = v
}

type UF struct {
	arr   []int
	count []int
	size  int
}

func NewUF(n int) UF {
	arr := make([]int, n)
	count := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = i
		count[i]++
	}

	return UF{arr, count, n}
}

func (uf *UF) Find(a int) int {
	if uf.arr[a] != a {
		uf.arr[a] = uf.Find(uf.arr[a])
	}
	return uf.arr[a]
}

func (uf *UF) Union(a, b int) bool {
	pa := uf.Find(a)
	pb := uf.Find(b)
	if pa == pb {
		return false
	}
	if uf.count[pa] < uf.count[pb] {
		pa, pb = pb, pa
	}
	uf.count[pa] += uf.count[pb]
	uf.arr[pb] = pa
	uf.size--
	return true
}