ВКонтакте открыла второй штаб в Санкт-Петербурге! Вы не преминули возможностью сменить обстановку и решили переехать из офиса в Доме Зингера в офис на Красном мосту.Для комфортной работы вам потребуются два монитора с одинаковой высотой, чтобы изображение на них выглядело единым целым. На складе офиса на Красном мосту есть $$$n$$$ мониторов, $$$i$$$-й из них имеет ширину $$$w_i$$$ и высоту $$$h_i$$$. Любой монитор можно повернуть на 90 градусов, и тогда он будет иметь ширину $$$h_i$$$ и высоту $$$w_i$$$.Назовём неупорядоченную пару из двух различных мониторов подходящей, если можно их повернуть так, чтобы они имели одинаковую высоту. Любой из мониторов в паре можно как повернуть относительно исходной ориентации, так и не поворачивать.Подсчитайте подходящие пары мониторов.

В первой строке задано одно целое число $$$n$$$ — число мониторов на складе. В каждой из следующих $$$n$$$ строк заданы два целых числа $$$w_i$$$ и $$$h_i$$$ ($$$1 \le w_i, h_i \le 10^9$$$) — ширина и высота $$$i$$$-го монитора. Обратите внимание, что мониторы могут быть квадратными ($$$w_i = h_i$$$), а размеры разных мониторов могут совпадать. В этой версии задачи $$$2 \le n \le 10^3$$$.

Выведите число подходящих пар мониторов.

ПримечаниеВ первом примере подходящими являются пары мониторов с номерами $$$(1, 2)$$$, $$$(1, 4)$$$, $$$(1, 5)$$$, $$$(3, 4)$$$, $$$(4, 5)$$$.Во втором примере все пары мониторов — подходящие.

package main

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

func main() {
	fifth()
}

type Monitor struct {
	h, w int64
	pos  int64
}

type Pair struct {
	x, y int64
}

func NewPair(h,w int64) Pair {
	if h < w {
		return Pair{
			x: h,
			y: w,
		}
	}

	return Pair{
		x: w,
		y: h,
	}
}

func fifth() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	countBySide := make(map[int64]int64, 2*countMonitors)
	pairsCounter := make(map[Pair]int64, countMonitors)
	counter := int64(0)
	for i := 0; i < countMonitors; i++ {
		h, w := getHW(reader)
		if h == w {
			counter += countBySide[h]
			countBySide[h]++
			continue
		}

		pair := NewPair(h, w)

		counter += countBySide[h] + countBySide[w] - pairsCounter[pair]

		pairsCounter[pair]++
		countBySide[h]++
		countBySide[w]++
	}

	fmt.Print(counter)
}

// monitors := make([]Monitor, countMonitors)
// monitors[i] = Monitor{
// 	h:   h,
// 	w:   w,
// 	pos: i,
// }

func combinations(n, k int) int {
	return factorial(n)/(factorial(n-k)*factorial(k))
}

func factorial(n int) int  {
	res := 1
	for i := 2; i < n+1; i++ {
		res *=i
	}
	return res
}

func getHW(reader *bufio.Reader) (int64, int64) {
	sizeMonitorLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	hw := bytes.Split(sizeMonitorLine, []byte(" "))

	h, err := strconv.ParseInt(string(hw[0]), 10, 64)
	if err != nil {
		panic(err.Error())
	}

	w, err := strconv.ParseInt(string(hw[1]), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return h, w
}

package main

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

func main() {
	fifth()
}

type Monitor struct {
	h, w int64
	pos  int64
}

type Pair struct {
	x, y int64
}

func NewPair(h,w int64) Pair {
	if h < w {
		return Pair{
			x: h,
			y: w,
		}
	}

	return Pair{
		x: w,
		y: h,
	}
}

func fifth() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	countBySide := make(map[int64]int64, countMonitors)
	pairsCounter := make(map[Pair]int64, countMonitors)
	counter := int64(0)
	for i := 0; i < countMonitors; i++ {
		h, w := getHW(reader)
		if h == w {
			counter += countBySide[h]
			countBySide[h]++
			continue
		}

		pair := NewPair(h, w)

		counter += countBySide[h] + countBySide[w] - pairsCounter[pair]

		pairsCounter[pair]++
		countBySide[h]++
		countBySide[w]++
	}

	fmt.Print(counter)
}

// monitors := make([]Monitor, countMonitors)
// monitors[i] = Monitor{
// 	h:   h,
// 	w:   w,
// 	pos: i,
// }

func combinations(n, k int) int {
	return factorial(n)/(factorial(n-k)*factorial(k))
}

func factorial(n int) int  {
	res := 1
	for i := 2; i < n+1; i++ {
		res *=i
	}
	return res
}

func getHW(reader *bufio.Reader) (int64, int64) {
	sizeMonitorLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	hw := bytes.Split(sizeMonitorLine, []byte(" "))

	h, err := strconv.ParseInt(string(hw[0]), 10, 64)
	if err != nil {
		panic(err.Error())
	}

	w, err := strconv.ParseInt(string(hw[1]), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return h, w
}

package main

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

func main() {
	fifth()
}

type Monitor struct {
	h, w int64
	pos  int64
}

type Pair struct {
	x, y int64
}

func NewPair(h,w int64) Pair {
	if h < w {
		return Pair{
			x: h,
			y: w,
		}
	}

	return Pair{
		x: w,
		y: h,
	}
}

func fifth() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	countBySide := map[int64]int64{}
	pairsCounter := map[Pair]int64{}
	counter := int64(0)
	for i := 0; i < countMonitors; i++ {
		h, w := getHW(reader)
		if h == w {
			counter += countBySide[h]
			countBySide[h]++
			continue
		}

		pair := NewPair(h, w)

		countByH := countBySide[h]
		countByW := countBySide[w]
		countDuplicatedPairs := pairsCounter[pair]
		counter += countByH + countByW - countDuplicatedPairs

		pairsCounter[pair]++
		countBySide[h]++
		countBySide[w]++
	}

	fmt.Print(counter)
}

// monitors := make([]Monitor, countMonitors)
// monitors[i] = Monitor{
// 	h:   h,
// 	w:   w,
// 	pos: i,
// }

func combinations(n, k int) int {
	return factorial(n)/(factorial(n-k)*factorial(k))
}

func factorial(n int) int  {
	res := 1
	for i := 2; i < n+1; i++ {
		res *=i
	}
	return res
}

func getHW(reader *bufio.Reader) (int64, int64) {
	sizeMonitorLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	hw := bytes.Split(sizeMonitorLine, []byte(" "))

	h, err := strconv.ParseInt(string(hw[0]), 10, 64)
	if err != nil {
		panic(err.Error())
	}

	w, err := strconv.ParseInt(string(hw[1]), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return h, w
}

package main

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

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

func NewIO(in io.Reader, out io.Writer) *IO {
	return &IO{in: bufio.NewReader(in), out: bufio.NewWriter(out)}
}

func (io *IO) ReadInt64() int64 {
	var res int64
	fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadInt() int {
	var res int
	fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadPairInt64() (int64, int64) {
	var a, b int64
	fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadPairInt() (int, int) {
	var a, b int
	fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadString() string {
	var res string
	fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) ReadIntSlice(n int) []int64 {
	var t int64
	res := make([]int64, 0, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(io.in, "%d", &t)
		res = append(res, t)
	}
	fmt.Fscanf(io.in, "\n")
	return res
}

type pair struct{a, b int}

func main() {
	io := NewIO(os.Stdin, os.Stdout)

	mp := make(map[int]int)
	mpp := make(map[pair]int)
	n := io.ReadInt()

	var ans int64
	for i := 0; i < n; i++ {
		a, b := io.ReadPairInt()
		if a > b {
			a, b = b, a
		}

		ans += int64(mp[a])
		mp[a]++
		if a != b {
			ans += int64(mp[b])
			ans -= int64(mpp[pair{a, b}])
			mp[b]++
			mpp[pair{a, b}]++
		}
	}

	fmt.Println(ans)
}

package main

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

type mon struct {
	w, h int
}

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

	var n int

	fmt.Fscan(r, &n)

	mons := make([]mon, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(r, &mons[i].w, &mons[i].h)
	}

	var count int64 = 0

	same := make(map[[2]int]int)
	dims := make(map[int]int64)

	for i := 0; i < n; i++ {
		m := mons[i]
		dims[m.w]++

		if m.w != m.h {
			dims[m.h]++

			same[[2]int{max(m.h, m.w), min(m.h, m.w)}]++
		}
	}

	for i := 0; i < n; i++ {
		m := mons[i]

		h := m.h
		w := m.w

		if h == w {
			count += dims[w] - 1
			dims[w]--

		} else {
			hs := dims[h] - 1
			ws := dims[w] - 1

			count += hs
			count += ws

			count -= int64(same[[2]int{max(h, w), min(h, w)}]) - 1

			same[[2]int{max(h, w), min(h, w)}]--
			dims[h]--
			dims[w]--
		}
	}

	fmt.Println(count)
}

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

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

package main

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

func main() {
	// first()
	second()
}

type Monitor struct {
	h, w int
	pos int
}

func second() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	monitors := make([]Monitor, countMonitors)
	for i := 0; i < countMonitors; i++ {
		h,w := getHW(reader)
		monitors[i] = Monitor{
			h:   h,
			w:   w,
			pos: i,
		}
	}

	pairs := 0
	for i := 0; i < countMonitors; i++ {
		for j := i+1; j < countMonitors; j++ {
			if monitors[i].h == monitors[j].h || monitors[i].h == monitors[j].w ||
				monitors[i].w == monitors[j].h || monitors[i].w == monitors[j].w {
				pairs++
			}
		}
	}
	fmt.Print(pairs)
}

func getHW(reader *bufio.Reader) (int, int) {
	sizeMonitorLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	hw := bytes.Split(sizeMonitorLine, []byte(" "))

	h, err := strconv.Atoi(string(hw[0]))
	if err != nil {
		panic(err.Error())
	}

	w, err := strconv.Atoi(string(hw[1]))
	if err != nil {
		panic(err.Error())
	}
	return h,w
}

func first() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	sideToNumMonitor := make(map[int][]int, 0)
	pairs := make(map[int]map[int]struct{})

	for i := 0; i < countMonitors; i++ {
		sizeMonitorLine, _, err := reader.ReadLine()
		if err != nil {
			panic(err.Error())
		}

		hw := bytes.Split(sizeMonitorLine, []byte(" "))

		h, err := strconv.Atoi(string(hw[0]))
		if err != nil {
			panic(err.Error())
		}

		w, err := strconv.Atoi(string(hw[1]))
		if err != nil {
			panic(err.Error())
		}

		if h == w {
			if monitors, ok := sideToNumMonitor[h]; ok {
				for _, numMonitor := range monitors {
					if len(pairs[i]) == 0 {
						pairs[i] = make(map[int]struct{})
					}
					pairs[i][numMonitor]= struct{}{}
				}
			}
			sideToNumMonitor[h] = append(sideToNumMonitor[h], i)
			continue
		}

		if monitors, ok := sideToNumMonitor[h]; ok {
			for _, numMonitor := range monitors {
				if len(pairs[i]) == 0 {
					pairs[i] = make(map[int]struct{})
				}
				pairs[i][numMonitor]= struct{}{}
			}
		}
		sideToNumMonitor[h] = append(sideToNumMonitor[h], i)

		if monitors, ok := sideToNumMonitor[w]; ok {
			for _, numMonitor := range monitors {
				if len(pairs[i]) == 0 {
					pairs[i] = make(map[int]struct{})
				}
				pairs[i][numMonitor]= struct{}{}
			}
		}
		sideToNumMonitor[w] = append(sideToNumMonitor[w], i)
	}

	result := 0
	for _, count := range pairs {
		result+=len(count)
	}
	fmt.Print(result)
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	sideToNumMonitor := make(map[int][]int, 0)
	pairs := make(map[int]map[int]struct{})

	for i := 0; i < countMonitors; i++ {
		sizeMonitorLine, _, err := reader.ReadLine()
		if err != nil {
			panic(err.Error())
		}

		hw := bytes.Split(sizeMonitorLine, []byte(" "))

		h, err := strconv.Atoi(string(hw[0]))
		if err != nil {
			panic(err.Error())
		}

		w, err := strconv.Atoi(string(hw[1]))
		if err != nil {
			panic(err.Error())
		}

		if h == w {
			if monitors, ok := sideToNumMonitor[h]; ok {
				for _, numMonitor := range monitors {
					if len(pairs[i]) == 0 {
						pairs[i] = make(map[int]struct{})
					}
					pairs[i][numMonitor]= struct{}{}
				}
			}
			sideToNumMonitor[h] = append(sideToNumMonitor[h], i)
			continue
		}

		if monitors, ok := sideToNumMonitor[h]; ok {
			for _, numMonitor := range monitors {
				if len(pairs[i]) == 0 {
					pairs[i] = make(map[int]struct{})
				}
				pairs[i][numMonitor]= struct{}{}
			}
		}
		sideToNumMonitor[h] = append(sideToNumMonitor[h], i)

		if monitors, ok := sideToNumMonitor[w]; ok {
			for _, numMonitor := range monitors {
				if len(pairs[i]) == 0 {
					pairs[i] = make(map[int]struct{})
				}
				pairs[i][numMonitor]= struct{}{}
			}
		}
		sideToNumMonitor[w] = append(sideToNumMonitor[w], i)
	}

	result := 0
	for _, count := range pairs {
		result+=len(count)
	}
	fmt.Print(result)
}

package main

import "fmt"
import s "strings"
import "bufio"
import "os"
import "strconv"

func main(){

    disp := map[int]int{}

    dispr := map[int]map[int]int{}

    var c, k int = 0, 0

    fmt.Scanf("%d\n", &c)
    reader := bufio.NewReader(os.Stdin)

    for i := 0; i < c; i++ {
        var h, w int
        b, _ := reader.ReadString('\n')
        t := s.Replace(b,"\n","",1)
        d := s.Split(s.Replace(t,"\r","",1)," ")

        //fmt.Println(d)
        h, _ = strconv.Atoi(d[0])
        w, _ = strconv.Atoi(d[1])

        if dispr[h] == nil{
            dispr[h] = map[int]int{}
        }
        if h != w {
            k += disp[h]
            k += disp[w]
            k -= dispr[h][w]
            k -= dispr[w][h]
        }else{
            k += disp[h]
            //k -= dispr[h][w]
        }

        if h == w{
            disp[h]++
        }else{
            disp[h]++
            disp[w]++
        }

        dispr[h][w]++
    }

    fmt.Print(k)
}

package main

import "fmt"

func main(){

    displays := map[int][]int{}
    var c, k int = 0, 0

    fmt.Scanf("%d\n", &c)

    for i := 0; i < c; i++ {
        var p1, p2 int
        fmt.Scanf("%d %d\n", &p1, &p2)

        for _, p := range displays {
            //fmt.Println(l, l1, p, p1)
            if p[0] == p1 || p[0] == p2 || p[1] == p1 || p[1] == p2{
                k++
            }
        }
        displays[i] = []int{p1, p2}
    }

    fmt.Print(k)
}

package main

import "fmt"

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

	var width []int
	var height []int
	for i := 0; i < n; i++ {
		var w, h int
		_, err := fmt.Scanf("%d %d\n", &w, &h)
		if err != nil {
			return
		}
		width = append(width, w)
		height = append(height, h)
	}

	result := 0
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			if width[i] == width[j] || width[i] == height[j] || width[j] == height[i] || height[i] == height[j] {
				result++
			}
		}
	}

	fmt.Println(result)
}

package main

import (
	"fmt"
)

type Monitor struct {
	x int
	y int
}

func main() {
	var cnt, x, y int
	recievedList := []Monitor{}
	answer := 0

	_, _ = fmt.Scan(&cnt)
	for t := 0; t < cnt; t++ {
		_, _ = fmt.Scan(&x, &y)
		for _, v := range recievedList {
			if (v.x == x) || (v.x == y) || (v.y == x) || (v.y == y) {
				answer++
			}
		}
		recievedList = append(recievedList, Monitor{x, y})
	}
	fmt.Println(answer)
}

package main

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

type mon struct {
	w, h int
}

func main() {

	r := bufio.NewReader(os.Stdin)

	var n int

	fmt.Fscan(r, &n)

	mons := make([]mon, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(r, &mons[i].w, &mons[i].h)
	}

	var count int

	for i := 0; i < n; i++ {
		m1 := mons[i]
		for j := i + 1; j < n; j++ {
			m2 := mons[j]

			if m1.w == m2.w || m1.w == m2.h || m1.h == m2.w || m1.h == m2.h {
				count++
			}
		}
	}

	fmt.Println(count)
}

package main

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

func main() {
	fifth()
}

type Monitor struct {
	h, w int
	pos  int
}

type Pair struct {
	x, y int
}

func NewPair(h,w int) Pair {
	if h < w {
		return Pair{
			x: h,
			y: w,
		}
	}

	return Pair{
		x: w,
		y: h,
	}
}

func fifth() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	countBySide := map[int]int{}
	pairsCounter := map[Pair]int{}
	counter := 0
	for i := 0; i < countMonitors; i++ {
		h, w := getHW(reader)
		pair := NewPair(h, w)

		if h == w {
			countByH := countBySide[h]
			counter += countByH

			pairsCounter[pair]++
			countBySide[h]++
			continue
		}

		countByH := countBySide[h]
		countByW := countBySide[w]
		countDuplicatedPairs := pairsCounter[pair]
		counter += countByH + countByW - countDuplicatedPairs

		pairsCounter[pair]++
		countBySide[h]++
		countBySide[w]++
	}

	fmt.Print(counter)
}

// monitors := make([]Monitor, countMonitors)
// monitors[i] = Monitor{
// 	h:   h,
// 	w:   w,
// 	pos: i,
// }

func combinations(n, k int) int {
	return factorial(n)/(factorial(n-k)*factorial(k))
}

func factorial(n int) int  {
	res := 1
	for i := 2; i < n+1; i++ {
		res *=i
	}
	return res
}

func getHW(reader *bufio.Reader) (int, int) {
	sizeMonitorLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	hw := bytes.Split(sizeMonitorLine, []byte(" "))

	h, err := strconv.Atoi(string(hw[0]))
	if err != nil {
		panic(err.Error())
	}

	w, err := strconv.Atoi(string(hw[1]))
	if err != nil {
		panic(err.Error())
	}
	return h, w
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"strconv"
	"sync"
	"sync/atomic"
)

func main() {
	// first()
	// second()
	// third()
	fourth()
}

type Monitor struct {
	h, w int
	pos  int
}

func fourth() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	monitors := make([]Monitor, countMonitors)
	for i := 0; i < countMonitors; i++ {
		h, w := getHW(reader)
		monitors[i] = Monitor{
			h:   h,
			w:   w,
			pos: i,
		}
	}

	monitorsBySide := make(map[int][]int, countMonitors*(countMonitors/2))
	for i := 0; i < countMonitors; i++ {
		monitorsBySide[monitors[i].h] = append(monitorsBySide[monitors[i].h], monitors[i].pos)
		if monitors[i].h != monitors[i].w {
			monitorsBySide[monitors[i].w] = append(monitorsBySide[monitors[i].w], monitors[i].pos)
		}
	}

	pairs := make(map[int]struct{})
	for _, mntrs := range monitorsBySide {
		for i := 0; i < len(mntrs); i++ {
			for j := i + 1; j < len(mntrs); j++ {
				pairs[mntrs[i]+mntrs[j]] = struct{}{}
			}
		}
	}

	fmt.Print(len(pairs))
}

func third() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	monitors := make([]Monitor, countMonitors)
	for i := 0; i < countMonitors; i++ {
		h, w := getHW(reader)
		monitors[i] = Monitor{
			h:   h,
			w:   w,
			pos: i,
		}
	}

	pairs := int64(0)
	workers := make(chan struct{}, 4)
	wg := sync.WaitGroup{}
	for i := 0; i < countMonitors; i++ {
		workers <- struct{}{}
		wg.Add(1)
		go func(i int) {
			defer wg.Done()
			for j := i + 1; j < countMonitors; j++ {
				if monitors[i].h == monitors[j].h || monitors[i].h == monitors[j].w ||
					monitors[i].w == monitors[j].h || monitors[i].w == monitors[j].w {
					atomic.AddInt64(&pairs, 1)
				}
			}
			<-workers
		}(i)
	}
	wg.Wait()
	fmt.Print(atomic.LoadInt64(&pairs))
}

func second() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	monitors := make([]Monitor, countMonitors)
	for i := 0; i < countMonitors; i++ {
		h, w := getHW(reader)
		monitors[i] = Monitor{
			h:   h,
			w:   w,
			pos: i,
		}
	}

	pairs := 0
	for i := 0; i < countMonitors; i++ {
		for j := i + 1; j < countMonitors; j++ {
			if monitors[i].h == monitors[j].h || monitors[i].h == monitors[j].w ||
				monitors[i].w == monitors[j].h || monitors[i].w == monitors[j].w {
				pairs++
			}
		}
	}
	fmt.Print(pairs)
}

func getHW(reader *bufio.Reader) (int, int) {
	sizeMonitorLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	hw := bytes.Split(sizeMonitorLine, []byte(" "))

	h, err := strconv.Atoi(string(hw[0]))
	if err != nil {
		panic(err.Error())
	}

	w, err := strconv.Atoi(string(hw[1]))
	if err != nil {
		panic(err.Error())
	}
	return h, w
}

func first() {
	reader := bufio.NewReader(os.Stdin)
	countLine, _, err := reader.ReadLine()
	if err != nil {
		panic(err.Error())
	}

	countMonitors, err := strconv.Atoi(string(countLine))
	if err != nil {
		panic(err.Error())
	}

	sideToNumMonitor := make(map[int][]int, 0)
	pairs := make(map[int]map[int]struct{})

	for i := 0; i < countMonitors; i++ {
		sizeMonitorLine, _, err := reader.ReadLine()
		if err != nil {
			panic(err.Error())
		}

		hw := bytes.Split(sizeMonitorLine, []byte(" "))

		h, err := strconv.Atoi(string(hw[0]))
		if err != nil {
			panic(err.Error())
		}

		w, err := strconv.Atoi(string(hw[1]))
		if err != nil {
			panic(err.Error())
		}

		if h == w {
			if monitors, ok := sideToNumMonitor[h]; ok {
				for _, numMonitor := range monitors {
					if len(pairs[i]) == 0 {
						pairs[i] = make(map[int]struct{})
					}
					pairs[i][numMonitor] = struct{}{}
				}
			}
			sideToNumMonitor[h] = append(sideToNumMonitor[h], i)
			continue
		}

		if monitors, ok := sideToNumMonitor[h]; ok {
			for _, numMonitor := range monitors {
				if len(pairs[i]) == 0 {
					pairs[i] = make(map[int]struct{})
				}
				pairs[i][numMonitor] = struct{}{}
			}
		}
		sideToNumMonitor[h] = append(sideToNumMonitor[h], i)

		if monitors, ok := sideToNumMonitor[w]; ok {
			for _, numMonitor := range monitors {
				if len(pairs[i]) == 0 {
					pairs[i] = make(map[int]struct{})
				}
				pairs[i][numMonitor] = struct{}{}
			}
		}
		sideToNumMonitor[w] = append(sideToNumMonitor[w], i)
	}

	result := 0
	for _, count := range pairs {
		result += len(count)
	}
	fmt.Print(result)
}


One day Prof. Slim decided to leave the kingdom of the GUC to join the kingdom of the GIU. He was given an easy online assessment to solve before joining the GIU. Citizens of the GUC were happy sad to see the prof leaving, so they decided to hack into the system and change the online assessment into a harder one so that he stays at the GUC. After a long argument, they decided to change it into the following problem.Given an array of $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$, where $$$a_{i} \neq 0$$$, check if you can make this array sorted by using the following operation any number of times (possibly zero). An array is sorted if its elements are arranged in a non-decreasing order.  select two indices $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$) such that $$$a_i$$$ and $$$a_j$$$ have different signs. In other words, one must be positive and one must be negative.  swap the signs of $$$a_{i}$$$ and $$$a_{j}$$$. For example if you select $$$a_i=3$$$ and $$$a_j=-2$$$, then they will change to $$$a_i=-3$$$ and $$$a_j=2$$$. Prof. Slim saw that the problem is still too easy and isn't worth his time, so he decided to give it to you to solve.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^{5}$$$) — the length of the array $$$a$$$. The next line contain $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_{i} \le 10^9$$$, $$$a_{i} \neq 0$$$) separated by spaces describing elements of the array $$$a$$$.  It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.

For each test case, print "YES" if the array can be sorted in the non-decreasing order, otherwise print "NO". You can print each letter in any case (upper or lower).

NoteIn the first test case, there is no way to make the array sorted using the operation any number of times.In the second test case, the array is already sorted.In the third test case, we can swap the sign of the $$$1$$$-st element with the sign of the $$$5$$$-th element, and the sign of the $$$3$$$-rd element with the sign of the $$$6$$$-th element, this way the array will be sorted.In the fourth test case, there is no way to make the array sorted using the operation any number of times.

package main

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

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

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

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

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

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

/**
Run solve the problem CF1670A
Date: 11/2/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1670/A
Problem: CF1670A
**/
func (in *CF1670A) Run() {
  for t := in.NextInt(); t > 0; t-- {
    n := in.NextInt()
    arr := make([]int, n)

    for i := 0; i < n; i++ {
      val := in.NextInt()
      arr[i] = val
    }

    ans := true

    i, j := 0, 0
    for i < n {
      if arr[i] < 0 {
        arr[j] *= -1
        arr[i] *= -1
        j++
      }
      i++
    }

    sort := true
    for i := 1; i < n; i++ {
      if arr[i-1] > arr[i] {
        sort = false
      }
    }

    ans = sort

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

}

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

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

package main

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

type Int int64

const (
	INF = 3e5 + 10
	L0  = Int(0)
	L1  = Int(1)
)

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n int
		Fscan(in, &n)
		a := make([]int, n+1)
		cnt := 0
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			if a[i] < 0 {
				cnt++
				a[i] = -a[i]
			}
		}

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

			}
		}
		if flag {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

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

type Int int64

const (
	INF = 3e5 + 10
	L0  = Int(0)
	L1  = Int(1)
)

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n int
		Fscan(in, &n)
		a := make([]int, n)

		cnt := 0
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			if a[i] < 0 {
				cnt++
				a[i] = -a[i]
			}
		}

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

		if flag {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func abs(a int) int { if a < 0 { return -a }; return a }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	const inf = 1000000000
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N := gi(); A := gis(N)
		cntneg := 0; for _,a := range A { if a < 0 { cntneg++ } }
		// Negatives clearly go on the left end of the array
		for i:=0;i<N;i++ { if i < cntneg { A[i] = -abs(A[i]) } else { A[i] = abs(A[i]) } }
		ans := "YES"
		for i:=0;i<N-1;i++ { if A[i] > A[i+1] { ans = "NO"; break } }
		fmt.Fprintln(wrtr,ans)
	}
}

package main

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

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

	var t int
	var n int
	in.Scan()
	t, _ = strconv.Atoi(in.Text())
	sol := func() bool {

		in.Scan()
		n, _ = strconv.Atoi(in.Text())
		a := make([]int, n)
		d := 0
		for i := 0; i < n; i++ {
			in.Scan()
			a[i], _ = strconv.Atoi(in.Text())
			if a[i] < 0 {
				d++
				a[i] = -a[i]
			}
		}
		for i := 1; i < d; i++ {
			if a[i-1] < a[i] {
				return false
			}
		}
		for i := d; i < n-1; i++ {
			if a[i] > a[i+1] {
				return false
			}
		}
		return true
	}
	for ; t > 0; t-- {
		r := sol()
		if r {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)
	var t int
	var n int
	in.Scan()
	t, _ = strconv.Atoi(in.Text())
	sol := func() bool {

		in.Scan()
		n, _ = strconv.Atoi(in.Text())
		a := make([]int, n)
		d := 0
		for i := 0; i < n; i++ {
			in.Scan()
			a[i], _ = strconv.Atoi(in.Text())
			if a[i] < 0 {
				d++
				a[i] = -a[i]
			}
		}
		// fmt.Println(d)
		// fmt.Println(a[:d], a[d:], a)
		for i := 1; i < d; i++ {
			if a[i-1] < a[i] {
				return false
			}
		}
		for i := d; i < n-1; i++ {
			if a[i] > a[i+1] {
				return false
			}
		}
		return true
	}
	for ; t > 0; t-- {
		r := sol()
		if r {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

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

func slove() {
	defer w.Flush()
	ans := "No"
	defer func(){fmt.Fprintln(w, ans)}()
	var n int
	fmt.Fscan(r, &n)
	nums := make([]int, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(r, &nums[i])
	}
	nega, posi := 0, 0
	for i := range nums {
		if nums[i] < 0 {
			nega++
		} else {
			posi++
		}
	}
	for i:=1;i<nega;i++ {
		if abs(nums[i]) > abs(nums[i-1]) {
			return
		}
	}
	for i:=nega+1;i<n;i++ {
		if abs(nums[i]) < abs(nums[i-1]) {
			return
		}
	}
	ans = "Yes"
}

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

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

package main

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

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

func a1670(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T int
	var n int
	var x int

loop:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		neg := 0
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			if x < 0 {
				neg++
				x = -x
			}
			a[i] = x
		}

		for i := 1; i < neg; i++ {
			if a[i] > a[i-1] {
				Fprintln(out, "NO")
				continue loop
			}
		}
		for i := neg+1; i < n;  i++ {
			if a[i] < a[i-1] {
				Fprintln(out, "NO")
				continue loop
			}
		}

		Fprintln(out, "YES")
	}
}

package main

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

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

func main() {
	defer out.Flush()

	var tc int
	fmt.Fscan(in, &tc)
	for ; tc > 0; tc-- {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n)
		negatives := 0
		for i := range a {
			fmt.Fscan(in, &a[i])
			if a[i] < 0 {
				negatives++
				a[i] = -a[i]
			}
		}
		for i := 0; i < negatives; i++ {
			a[i] = -a[i]
		}
		ok := true
		for i := 1; ok && i < len(a); i++ {
			ok = a[i-1] <= a[i]
		}
		if ok {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

/*

 */

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

	a := make([]int, n)
	negCnt := 0

	for i := range a {
		scanf("%d", &a[i])
		if a[i] < 0 {
			negCnt++
		}
	}
	scanf("\n")

	for i, num := range a {
		if i < negCnt {
			a[i] = -abs(num)
		} else {
			a[i] = abs(num)
		}
	}

	if sort.IntsAreSorted(a) {
		fmt.Fprintln(writer, "YES")
	} else {
		fmt.Fprintln(writer, "NO")
	}
}

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

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

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

const maxUint = ^uint(0)

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

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

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

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

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

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

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

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

	return minNumIdx
}

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

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

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func sum(nums ...int) (sum int) {
	for _, num := range nums {
		sum += num
	}
	return
}

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

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

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

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

	return true
}

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

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

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

	num = abs(num)

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

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

//goland:noinspection GoUnusedFunction
func factorize(num int64) []int64 {
	factors := make([]int64, 0)

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

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

	return factors
}

package main

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

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

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

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

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

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

/**
Run solve the problem CF1670A
Date: 11/2/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1670/A
Problem: CF1670A
**/
func (in *CF1670A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		arr := make([]int, n)

		order := true
		last := -1
		for i := 0; i < n; i++ {
			val := in.NextInt()
			arr[i] = val

			if i > 0 {
				if last > arr[i] {
					order = false
				}
			}
			last = val
		}

		ans := true

		pivot := -1
		for i := 2; i < n; i++ {
			if int(math.Abs(float64(arr[i-2]))) > int(math.Abs(float64(arr[i-1]))) && int(math.Abs(float64(arr[i-1]))) < int(math.Abs(float64(arr[i]))) {
				pivot = i - 1
				break
			}
		}
		if pivot != -1 {

			changes := 0

			for i := 0; i < pivot; i++ {
				if arr[i] > 0 {
					changes++
				}
			}

			for i := pivot + 1; i < n; i++ {
				if arr[i] < 0 {
					changes--
				}
			}

			if changes != 0 {
				ans = false
			}
		}

		if pivot == -1 && !order {
			ans = false
		}

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

}

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

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

package main

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

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

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

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

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

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

/**
Run solve the problem CF1670A
Date: 11/2/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1670/A
Problem: CF1670A
**/
func (in *CF1670A) Run() {
  for t := in.NextInt(); t > 0; t-- {
    n := in.NextInt()
    arr := make([]int, n)

    order := true
    last := -1
    for i := 0; i < n; i++ {
      val := in.NextInt()
      arr[i] = val

      if i > 0 {
        if last >= arr[i] {
          order = false
        }
      }
      last = val
    }

    ans := true

    pivot := -1
    for i := 2; i < n; i++ {
      if int(math.Abs(float64(arr[i-2]))) > int(math.Abs(float64(arr[i-1]))) && int(math.Abs(float64(arr[i-1]))) < int(math.Abs(float64(arr[i]))) {
        pivot = i - 1
        break
      }
    }
    if pivot != -1 {

      changes := 0

      for i := 0; i < pivot; i++ {
        if arr[i] > 0 {
          changes++
        }
      }

      for i := pivot + 1; i < n; i++ {
        if arr[i] < 0 {
          changes--
        }
      }

      if changes != 0 {
        ans = false
      }
    }

    if pivot == -1 && !order {
      ans = false
    }

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

}

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

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

package main

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

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

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

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

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

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

/**
Run solve the problem CF1670A
Date: 11/2/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1670/A
Problem: CF1670A
**/
func (in *CF1670A) Run() {
  for t := in.NextInt(); t > 0; t-- {
    n := in.NextInt()
    arr := make([]int, n)

    for i := 0; i < n; i++ {
      val := in.NextInt()
      arr[i] = val
    }

    ans := true

    pivot := -1
    for i := 2; i < n; i++ {
      if int(math.Abs(float64(arr[i-2]))) > int(math.Abs(float64(arr[i-1]))) && int(math.Abs(float64(arr[i-1]))) < int(math.Abs(float64(arr[i]))) {
        pivot = i - 1
        break
      }
    }
    if pivot != -1 {

      changes := 0

      for i := 0; i < pivot; i++ {
        if arr[i] > 0 {
          changes++
        }
      }

      for i := pivot + 1; i < n; i++ {
        if arr[i] < 0 {
          changes--
        }
      }

      if changes != 0 {
        ans = false
      }

    }

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

}

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

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

package main

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

type Int int64

const (
	INF = 3e5 + 10
	L0  = Int(0)
	L1  = Int(1)
)

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n int
		Fscan(in, &n)
		a := make([]int, n)

		cnt := 0
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			if a[i] < 0 {
				cnt++
				a[i] = -a[i]
			}
		}

		flag := true
		for i := 0; i < cnt; i++ {
			a[i] = -a[i]
			if i > 0 && a[i] < a[i-1] {
				flag = false
			}
		}

		if flag {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

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

type Int int64

const (
	INF = 3e5 + 10
	L0  = Int(0)
	L1  = Int(1)
)

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

	var t int
	Fscan(in, &t)

	for tt := 0; tt < t; tt++ {
		var n int
		Fscan(in, &n)
		a := make([]int, n)

		cnt := 0
		for i := 0; i < n; i++ {
			Fscan(in, &a[i])
			if a[i] < 0 {
				cnt++
				a[i] = -a[i]
			}
		}

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

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

		if flag && cnt == 0 {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}

	}

}

func MakeGrid(h, w int) [][]Int {
	index := make([][]Int, h)
	data := make([]Int, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

func Abs(x Int) Int {
	if x > 0 {
		return x
	}
	return -x
}

func Min(nums ...Int) Int {
	return nums[MinIdx(nums...)]
}

func MinIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num < nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

func Max(nums ...Int) Int {
	return nums[MaxIdx(nums...)]
}

func MaxIdx(nums ...Int) Int {
	maxNumIdx := L0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = Int(i)
		}
	}
	return maxNumIdx
}

var (
	mod  Int
	fmod func(Int) Int = doNothing
)

// SetMod sets the mod. It must be called first.
func SetMod(newMod Int) {
	mod = newMod
	fmod = dynamicMod
}

func dynamicMod(m Int) Int {
	m %= mod
	if m < 0 {
		return m + mod
	}
	return m
}

func doNothing(m Int) Int {
	return m
}

// Mod returns m % mod.
func (m Int) Mod() Int {
	return fmod(m)
}

// Inv returns modular multiplicative inverse
func (m Int) Inv() Int {
	return m.Pow(Int(0).Sub(2))
}

// Pow returns m^n
func (m Int) Pow(n Int) Int {
	p := Int(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(m)
		}
		m.MulAs(m)
		n >>= 1
	}
	return p
}

// Add returns m+x
func (m Int) Add(x Int) Int {
	return (m + x).Mod()
}

// Sub returns m-x
func (m Int) Sub(x Int) Int {
	return (m - x).Mod()
}

// Mul returns m*x
func (m Int) Mul(x Int) Int {
	return (m * x).Mod()
}

// Div returns m/x
func (m Int) Div(x Int) Int {
	return m.Mul(x.Inv())
}

// AddAs assigns *m + x to *m and returns m
func (m *Int) AddAs(x Int) *Int {
	*m = m.Add(x)
	return m
}

// SubAs assigns *m - x to *m and returns m
func (m *Int) SubAs(x Int) *Int {
	*m = m.Sub(x)
	return m
}

// MulAs assigns *m * x to *m and returns m
func (m *Int) MulAs(x Int) *Int {
	*m = m.Mul(x)
	return m
}

// DivAs assigns *m / x to *m and returns m
func (m *Int) DivAs(x Int) *Int {
	*m = m.Div(x)
	return m
}

func (m *Int) MaxAs(x ...Int) *Int {
	*m = Max(append(x, *m)...)
	return m
}

func (m *Int) MinAs(x ...Int) *Int {
	*m = Min(append(x, *m)...)
	return m
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func abs(a int) int { if a < 0 { return -a }; return a }
func max(a,b int) int { if a > b { return a }; return b }
func min(a,b int) int { if a > b { return b }; return a }
func tern(cond bool, a int, b int) int { if cond { return a }; return b }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	const inf = 1000000000
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N := gi(); A := gis(N)
		cntneg := 0; for _,a := range A { if a < 0 { cntneg++ } }
		minneg := twodi(N,2,inf)
		maxneg := twodi(N,2,-1)
		minneg[0][0] = 0; minneg[0][1] = 1; maxneg[0][0] = 0; maxneg[0][1] = 1
		for i:=1;i<N;i++ {
			if abs(A[i]) >= abs(A[i-1]) {
				minneg[i][0] = min(minneg[i-1][0],minneg[i-1][1])
				maxneg[i][0] = max(maxneg[i-1][0],maxneg[i-1][1])
			}
			if abs(A[i]) <= abs(A[i-1]) {
				minneg[i][1] = minneg[i-1][1]+1
				maxneg[i][1] = tern(maxneg[i-1][1] >= 0,maxneg[i-1][1]+1,-1)
			}
		}
		ans := "NO"
		if cntneg >= minneg[N-1][0] && cntneg <= maxneg[N-1][0] { ans = "YES" }
		if cntneg >= minneg[N-1][1] && cntneg <= maxneg[N-1][1] { ans = "YES" }
		fmt.Fprintln(wrtr,ans)
	}
}


The bear has a string s = s1s2... s|s| (record |s| is the string's length), consisting of lowercase English letters. The bear wants to count the number of such pairs of indices i, j (1 ≤ i ≤ j ≤ |s|), that string x(i, j) = sisi + 1... sj contains at least one string "bear" as a substring.String x(i, j) contains string "bear", if there is such index k (i ≤ k ≤ j - 3), that sk = b, sk + 1 = e, sk + 2 = a, sk + 3 = r.Help the bear cope with the given problem.

The first line contains a non-empty string s (1 ≤ |s| ≤ 5000). It is guaranteed that the string only consists of lowercase English letters.

Print a single number — the answer to the problem.

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
)

func main() {
	var (
		s   string
		n   int
		pos []int
		ans int
	)

	fmt.Fscan(reader, &s)
	n = len(s)
	for i := 0; i+4 <= n; i++ {
		if s[i:i+4] == "bear" {
			pos = append(pos, i)
		}
	}

	lb := 0
	for _, p := range pos {
		l := p - lb + 1
		r := n - p - 3
		//fmt.Println(l, r)
		ans += l * r
		lb = p + 1
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var s string

	fmt.Scan(&s)

	count := 0

	for i:=0; i<=len(s)-4; i++ {
		sub :=s[i: len(s)]
		if index:=strings.Index(sub, "bear"); index!=-1 {
			count += len(sub) - (index + 3)
		}
	}

	fmt.Println(count)
}

// http://codeforces.com/problemset/problem/385/B
// B. Bear and Strings

package main

import (
	"fmt"
)

// bear mach :-)
func Gep(st, ch byte) byte {
	switch ch {
	case 'b':
		{
			return 1
		}
	case 'e':
		{
			if 1 == st {
				return 2
			}
			return 0
		}

	case 'a':
		{
			if 2 == st {
				return 3
			}
			return 0
		}
	case 'r':
		{
			if 3 == st {
				return 4
			}
			return 0
		}
	}
	return 0
}

func main() {
	var (
		j, lw, pbeg int32
		st          byte
		word        string
		ans         int
	)

	fmt.Scan(&word)
	lw = int32(len(word))
	st, pbeg = 0, -1
	for j = 0; j < lw; j++ {
		st = Gep(st, word[j])
		if st == 4 {
			ans += int(j-3-pbeg) * int(lw-j)
			pbeg = j - 3
		}
	}

	fmt.Println(ans)

}

package main

import (
	"fmt"
)

func main() {
	var s string

	fmt.Scanf("%s", &s)

	var starts []int

	for i:=0; i<=len(s)-4; i++ {
		if s[i:i+4] == "bear" {
			starts = append(starts, i)
		}
	}

	count := 0
	for i:=0; i<=len(s)-4; i++ {
		end := -1
		for j:=0; j<len(starts); j++ {
			if starts[j] >= i {
				end = starts[j]
				break
			}
		}

		if(end != -1) {
			end += 3
			count += len(s)-end
		}
	}

	fmt.Println(count)
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
)

func main() {
	var (
		s   string
		n   int
		pos []int
		ans int
	)

	fmt.Fscan(reader, &s)
	n = len(s)
	for i := 0; i+4 <= n; i++ {
		if s[i:i+4] == "bear" {
			pos = append(pos, i)
		}
	}

	lb := 0
	for _, p := range pos {
		l := lb + 1
		r := n - p - 3
		ans += l * r
		lb = l + 4
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
	// "regexp"
	"strings"
)

func main() {
	var s string

	fmt.Scan(&s)

	news := s
	count := 0
	f, b, lastLength := 0, 0, 0
	for index:=strings.Index(news, "bear"); index != -1; index=strings.Index(news, "bear") {
		f, b = lastLength + index, len(s)-lastLength-index-4
		// fmt.Println(f, b)
		if f!=0 && b!=0 {
			count += f*(b+1)
		} else if f==0 {
			count += b+1
		} else {
			count += f+1
		}

		lastLength += index+4

		if len(news) > index + 7 {
			news = news[index+4:]
		} else {
 			break
		}
	}

	// pattern := regexp.MustCompile("[:lower:]*bear[:lower:]*")
	// count := 0

	// for i:=0; i<length-3; i++ {
	// 	for j:=i+4; j<=length; j++ {
	// 		if pattern.MatchString(s[i:j]) {
	// 			count++
	// 		}
	// 	}
	// }

	fmt.Println(count)
}


You are given a sorted array $$$a_1, a_2, \dots, a_n$$$ (for each index $$$i > 1$$$ condition $$$a_i \ge a_{i-1}$$$ holds) and an integer $$$k$$$.You are asked to divide this array into $$$k$$$ non-empty consecutive subarrays. Every element in the array should be included in exactly one subarray. Let $$$max(i)$$$ be equal to the maximum in the $$$i$$$-th subarray, and $$$min(i)$$$ be equal to the minimum in the $$$i$$$-th subarray. The cost of division is equal to $$$\sum\limits_{i=1}^{k} (max(i) - min(i))$$$. For example, if $$$a = [2, 4, 5, 5, 8, 11, 19]$$$ and we divide it into $$$3$$$ subarrays in the following way: $$$[2, 4], [5, 5], [8, 11, 19]$$$, then the cost of division is equal to $$$(4 - 2) + (5 - 5) + (19 - 8) = 13$$$.Calculate the minimum cost you can obtain by dividing the array $$$a$$$ into $$$k$$$ non-empty consecutive subarrays.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 3 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$ 1 \le a_i \le 10^9$$$, $$$a_i \ge a_{i-1}$$$).

Print the minimum cost you can obtain by dividing the array $$$a$$$ into $$$k$$$ nonempty consecutive subarrays.

NoteIn the first test we can divide array $$$a$$$ in the following way: $$$[4, 8, 15, 16], [23], [42]$$$.

package main

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

/* solution */

func solve() {

	n, k := readInt(), readInt()
	diff := make([]int, n-1)

	first := readInt()
	l, r := first, first
	for i := 0; i < n-1; i++ {
		r = readInt()
		diff[i] = l - r
		l = r
	}

	sort.Ints(diff)

	ans := r - first
	for i := 0; i < k-1; i++ {
		ans += diff[i]
	}

	printInt(ans)

}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)
	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

package main

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

func main() {

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

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

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

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

	for i := 0; i < n-1; i++ {
		a[i] -= a[i+1]
	}
	a = a[:n-1]
	sort.Ints(a)

	for i := 0; i < k-1; i++ {
		s += a[i]
	}
	fmt.Fprintf(writer, "%d\n", s)

	writer.Flush()
}

// Author: sighduck
// URL: https://codeforces.com/contest/1197/problem/C

package main

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

func Solve(n int, k int, a []int) int64 {
	if k == n {
		return 0
	}
	d := []int{}
	for i := 0; i < n-1; i++ {
		d = append(d, a[i+1]-a[i])
	}
	sort.Ints(d)
	cost := int64(0)
	for i := 0; i < n-k; i++ {
		cost += int64(d[i])
	}
	return cost
}

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.Fscanf(reader, "\n")

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


There is a pyramid which consists of $$$n$$$ floors. The floors are numbered from top to bottom in increasing order. In the pyramid, the $$$i$$$-th floor consists of $$$i$$$ rooms.Denote the $$$j$$$-th room on the $$$i$$$-th floor as $$$(i,j)$$$. For all positive integers $$$i$$$ and $$$j$$$ such that $$$1 \le j \le i < n$$$, there are $$$2$$$ one-way staircases which lead from $$$(i,j)$$$ to $$$(i+1,j)$$$ and from $$$(i,j)$$$ to $$$(i+1,j+1)$$$ respectively.In each room you can either put a torch or leave it empty. Define the brightness of a room $$$(i, j)$$$ to be the number of rooms with a torch from which you can reach the room $$$(i, j)$$$ through a non-negative number of staircases.For example, when $$$n=5$$$ and torches are placed in the rooms $$$(1,1)$$$, $$$(2,1)$$$, $$$(3,2)$$$, $$$(4,1)$$$, $$$(4,3)$$$, and $$$(5,3)$$$, the pyramid can be illustrated as follows:  In the above picture, rooms with torches are colored in yellow, and empty rooms are white. The blue numbers in the bottom-right corner indicate the brightness of the rooms.The room $$$(4,2)$$$ (the room with a star) has brightness $$$3$$$. In the picture below, the rooms from where you can reach $$$(4,2)$$$ have red border. The brightness is $$$3$$$ since there are three torches among these rooms.  The pyramid is called nice if and only if for all floors, all rooms in the floor have the same brightness.Define the brilliance of a nice pyramid to be the sum of brightness over the rooms $$$(1,1)$$$, $$$(2,1)$$$, $$$(3,1)$$$, ..., $$$(n,1)$$$.Find an arrangement of torches in the pyramid, such that the resulting pyramid is nice and its brilliance is maximized.We can show that an answer always exists. If there are multiple answers, output any one of them.

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of the test cases follows. The only line of each test case contains a single positive integer $$$n$$$ ($$$1 \le n \le 500$$$) — the number of floors in the pyramid. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$500$$$.

For each test case, output $$$n$$$ lines, the arrangement of torches in the pyramid. The $$$i$$$-th line should contain $$$i$$$ integers, each separated with a space. The $$$j$$$-th integer on the $$$i$$$-th line should be $$$1$$$ if room $$$(i,j)$$$ has a torch, and $$$0$$$ otherwise. We can show that an answer always exists. If there are multiple answers, output any one of them.

NoteIn the third test case, torches are placed in $$$(1,1)$$$, $$$(2,1)$$$, $$$(2,2)$$$, $$$(3,1)$$$, and $$$(3,3)$$$.  The pyramid is nice as rooms on each floor have the same brightness. For example, all rooms on the third floor have brightness $$$3$$$.The brilliance of the pyramid is $$$1+2+3 = 6$$$. It can be shown that no arrangements with $$$n=3$$$ will have a greater brilliance.

package main

import "fmt"

func main() {
	var t int
	_, _ = fmt.Scanln(&t)

	for j := 0; j < t; j++ {
		var n int
		_, _ = fmt.Scanln(&n)
		for i := 1; i <= n; i++ {
			for k := 1; k <= i; k++ {
				if k == 1 || k == i {
					fmt.Print("1 ")
				} else {
					fmt.Print("0 ")
				}
			}
			fmt.Println()
		}
	}
}

package main

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

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

func ans() {
	var n int
	Fscan(in, &n)
	Fprintln(out, "1")
	if n == 1 {
		return
	}
	for i := 2; i <= n; i++ {
		Fprint(out, "1 ")
		for j := 2; j < i; j++ {
			Fprint(out, "0 ")
		}
		Fprintln(out, "1")
	}

}

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

package main

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

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

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

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

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

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

/**
Run solve the problem CF1734B
Date: 9/28/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1734/B
Problem: CF1734B
**/
func (in *CF1734B) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		for i := 0; i < n; i++ {
			for j := 0; j <= i; j++ {
				if j == 0 || j == i {
					fmt.Print(1, " ")
				} else {
					fmt.Print(0, " ")
				}
			}
			fmt.Println()
		}
	}

}

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

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

package main

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

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

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

		for i := 0; i < n; i++ {
			for j := 0; j < i+1; j++ {
				var d int
				if j == 0 || j == i {
					d = 1
				}
				fmt.Fprintf(out, "%d ", d)
			}
			fmt.Fprintln(out)
		}
	}
}

package main

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

// https://space.bilibili.com/206214
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	solve := func(curCase int) {
		var n int
		Fscan(in, &n)
		for i := 1; i <= n; i++ {
			a := make([]int, i)
			a[0] = 1
			a[len(a)-1]= 1
			for _, v := range a {
				Fprint(out, v, " ")
			}
			Fprintln(out)
		}
	}

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

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

}

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

package main

import "fmt"

func main() {
	var t int
	_, _ = fmt.Scanln(&t)

	for j := 0; j < t; j++ {
		var n int
		_, _ = fmt.Scanln(&n)
		for i := 1; i <= n; i++ {
			for k := 1; k <= i; k++ {
				if i%2 == 0 {
					fmt.Print("1 ")
				} else if k == 1 || k == i {
					fmt.Print("1 ")
				} else {
					fmt.Print("0 ")
				}
			}
			fmt.Println()
		}
	}
}

package main

import "fmt"

func main() {
	var t int
	_, _ = fmt.Scanln(&t)

	for j := 0; j < t; j++ {
		var n int
		_, _ = fmt.Scanln(&n)
		for i := 1; i <= n; i++ {
			for k := 1; k <= i; k++ {
				if i%2 == 0 {
					fmt.Print(1)
				} else if k == 1 || k == i {
					fmt.Print(1)
				} else {
					fmt.Print(0)
				}
			}
			fmt.Println()
		}
	}
}


Vasya has got three integers $$$n$$$, $$$m$$$ and $$$k$$$. He'd like to find three integer points $$$(x_1, y_1)$$$, $$$(x_2, y_2)$$$, $$$(x_3, y_3)$$$, such that $$$0 \le x_1, x_2, x_3 \le n$$$, $$$0 \le y_1, y_2, y_3 \le m$$$ and the area of the triangle formed by these points is equal to $$$\frac{nm}{k}$$$.Help Vasya! Find such points (if it's possible). If there are multiple solutions, print any of them.

If there are no such points, print "NO". Otherwise print "YES" in the first line. The next three lines should contain integers $$$x_i, y_i$$$ — coordinates of the points, one point per line. If there are multiple solutions, print any of them. You can print each letter in any case (upper or lower).

NoteIn the first example area of the triangle should be equal to $$$\frac{nm}{k} = 4$$$. The triangle mentioned in the output is pictured below:   In the second example there is no triangle with area $$$\frac{nm}{k} = \frac{16}{7}$$$.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1030D(in io.Reader, out io.Writer) {
	gcd := func(a, b int64) int64 {
		for a != 0 {
			a, b = b%a, a
		}
		return b
	}
	var n, m, k int64
	Fscan(in, &n, &m, &k)
	if 2*n*m%k != 0 {
		Fprint(out, "NO")
		return
	}
	g := gcd(2*n, k)
	x, y := 2*n/g, m*g/k
	if x > n {
		x /= 2
		y *= 2
	}
	Fprintf(out, "YES\n0 0\n%d 0\n0 %d", x, y)
}

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


The only difference between easy and hard versions is the constraints.Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of $$$n$$$ consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the $$$i$$$-th picture has beauty $$$a_i$$$.Vova wants to repost exactly $$$x$$$ pictures in such a way that:   each segment of the news feed of at least $$$k$$$ consecutive pictures has at least one picture reposted by Vova;  the sum of beauty values of reposted pictures is maximum possible. For example, if $$$k=1$$$ then Vova has to repost all the pictures in the news feed. If $$$k=2$$$ then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them.Your task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions.

The first line of the input contains three integers $$$n, k$$$ and $$$x$$$ ($$$1 \le k, x \le n \le 200$$$) — the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the beauty of the $$$i$$$-th picture.

Print -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement. Otherwise print one integer — the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement.

package main

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

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

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

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

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

func max64(a int64, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

func main() {
	defer writer.Flush()

	var n, k, m int
	scanf("%d %d %d\n", &n, &k, &m)
	k = min(n, k)

	var nums []int64
	for i := 0; i < n; i++ {
		var x int64
		scanf("%d", &x)
		nums = append(nums, x)
	}

	dp := [201][201]int64{}
	for i := 0; i < m; i++ {
		dp[0][i] = nums[0]
	}
	dp[0][m] = -1

	for i := 1; i < n; i++ {
		for j := 0; j < m; j++ {
			if i < k {
				dp[i][j] = nums[i]
			} else {
				dp[i][j] = -1
			}
			for x := max(0, i-k); x < i; x++ {
				if dp[x][j+1] > -1 {
					dp[i][j] = max64(dp[i][j], dp[x][j+1]+nums[i])
				}
			}
		}
		dp[i][m] = -1
	}

	var result int64
	result = -1
	for i := n - k; i < n; i++ {
		result = max64(result, dp[i][0])
	}

	printf("%d\n", result)
}

package main

import (
	"fmt"
)

func main() {
	var n, k, x int64
	dp := make([][210]int64, 210)
	fmt.Scan(&n, &k, &x)
	var ans int64 = -1
	for i := int64(1); i <= n; i++ {
		var tmp int64
		fmt.Scan(&tmp)
		if i <=k {
			dp[i][1] = tmp

			if  i > n-k &&ans < dp[i][1] {
				ans = dp[i][1]
			}
		}
		for j := int64(i-1); i-j <= k && j > 0; j-- {
			var jj = int64(x-1)
			for ; jj >= 1; jj-- {
				if dp[j][jj]>0&& dp[i][jj+1] < dp[j][jj]+tmp {
					dp[i][jj+1] = dp[j][jj] + tmp
					if i > n-k && ans < dp[i][jj+1] {
						ans = dp[i][jj+1]
					}
				}
			}
		}

	}
	//for i := int64(1); i <= n; i++{
	//	for j := int64(1); j <= x; j++{
	//		fmt.Printf("%d,",dp[i][j])
	//	}
	//	fmt.Printf("\n")
	//}
	fmt.Print(ans)
}

package main

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

const (
	MaxN       = 2505
	MaxK       = 2505
	NoSolution = -1
)

var n, k, x int
var F [MaxN][MaxK]int64
var a [MaxN]int64

func init() {
	for i := 0; i < MaxN; i++ {
		for j := 0; j < MaxK; j++ {
			F[i][j] = NoSolution
		}
	}
}

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

func getMax(startPos, row int) int64 {
	best := int64(-1)
	for j := 1; j <= k; j++ {
		if startPos - j < 0 {
			break
		}
		if F[startPos-j][row] != NoSolution {
			best = max(best, F[startPos-j][row])
		}
	}
	return best
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &k, &x)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}
	for i := 0; i < k; i++ {
		F[i][1] = a[i]
	}
	for i := 1; i < n; i++ {
		for z := 2; z <= x; z++ {
			if z > i + 1 {
				break
			}
			currMax := getMax(i, z-1)
			if currMax != -1 {
				F[i][z] = currMax + a[i]
			}
		}
	}

	ans := int64(-1)
	for i := 0; i < k; i++ {
		ans = max(ans, F[n-i-1][x])
	}
	fmt.Println(ans)
}

package main

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

const (
	MaxN        = 256
	MaxX        = 256
	NotComputed = -2
)

var n, k, x int
var F [MaxN][MaxX]int64
var a [MaxN]int64

func init() {
	for i := 0; i < MaxN; i++ {
		for j := 0; j < MaxX; j++ {
			F[i][j] = NotComputed
		}
	}
}

func f(pos, places int) int64 {
	if F[pos][places] != NotComputed {
		return F[pos][places]
	}

	if places == 0 {
		if pos < k {
			F[pos][places] = a[pos]
		} else {
			F[pos][places] = -1
		}
		return F[pos][places]
	}

	curr := int64(-1)
	for i := 0; i < k; i++ {
		if pos - i - 1 < 0 {
			break
		}
		vnext := f(pos - i - 1, places - 1)
		if vnext != -1 && a[pos] + vnext > curr {
			curr = a[pos] + vnext
		}
	}
	F[pos][places] = curr
	return F[pos][places]
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &k, &x)

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

	ans := int64(-1)
	for i := 0; i < k; i++ {
		curr := f(n-i-1, x-1)
		if curr > ans {
			ans = curr
		}
	}

	fmt.Println(ans)
}

package main

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

const (
	MaxN       = 2505
	MaxK       = 2505
	NoSolution = -1
)

var n, k, x int
var F [MaxN][MaxK]int64
var a [MaxN]int64

func init() {
	for i := 0; i < MaxN; i++ {
		for j := 0; j < MaxK; j++ {
			F[i][j] = NoSolution
		}
	}
}

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

func getMax(startPos, row int) int64 {
	best := int64(-1)
	for j := 1; j <= k; j++ {
		if startPos - j < 0 {
			break
		}
		if F[startPos-j][row] != NoSolution {
			best = max(best, F[startPos-j][row])
		}
	}
	return best
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &k, &x)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}
	for i := 0; i < k; i++ {
		F[i][1] = a[i]
	}
	for z := 2; z <= x; z++ {
		for i := 1; i < n; i++ {
			if z > i + 1 {
				continue
			}
			currMax := getMax(i, z-1)
			if currMax != -1 {
				F[i][z] = currMax + a[i]
			}
		}
	}

	ans := int64(-1)
	for i := 0; i < k; i++ {
		ans = max(ans, F[n-i-1][x])
	}
	fmt.Println(ans)
}

package main

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

const (
	MaxN       = 2505
	MaxK       = 2505
	NoSolution = -1
)

var n, k, x int
var F [MaxN][MaxK]int64
var a [MaxN]int64

func init() {
	for i := 0; i < MaxN; i++ {
		for j := 0; j < MaxK; j++ {
			F[i][j] = NoSolution
		}
	}
}

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

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &k, &x)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}
	for i := 0; i < k; i++ {
		F[i][1] = a[i]
	}
	for i := 1; i < n; i++ {
		for z := 2; z <= x; z++ {
			if z > i + 1 {
				break
			}
			for j := 1; j <= k; j++ {
				if i < j {
					break
				}
				if F[i-j][z-1] != NoSolution {
					F[i][z] = max(F[i][z], F[i-j][z-1] + a[i])
				}
			}
		}
	}

	//for i := 0; i < n; i++ {
	//	if i == 0 {
	//		for j := 0; j < n; j++ {
	//			fmt.Printf("%3d ", j)
	//		}
	//		fmt.Println()
	//		for j := 0; j < n; j++ {
	//			fmt.Printf("====")
	//		}
	//		fmt.Println()
	//	}
	//	for j := 0; j <= x; j++ {
	//		fmt.Printf("%3d ", F[i][j])
	//	}
	//	fmt.Println()
	//}

	ans := int64(-1)
	for i := 0; i < k; i++ {
		ans = max(ans, F[n-i-1][x])
	}
	fmt.Println(ans)
}

package main

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

const (
	MaxN       = 5005
	MaxK       = 5005
	NoSolution = -1
)

var n, k, x int
var F [MaxN][MaxK]int64
var a [MaxN]int64

func init() {
	for i := 0; i < MaxN; i++ {
		for j := 0; j < MaxK; j++ {
			F[i][j] = NoSolution
		}
	}
}

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

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &k, &x)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}
	for i := 0; i < k; i++ {
		F[i][1] = a[i]
	}
	for z := 2; z <= x; z++ {
		d := make([]int, 0)
		for i := 1; i < n; i++ {
			if z > i + 1 {
				continue
			}
			if len(d) > 0 && i - d[0] > k {
				d = d[1:]
			}
			if F[i-1][z-1] != - 1 {
				for len(d) > 0 && F[d[len(d) - 1]][z-1] < F[i-1][z-1] {
					d = d[:len(d) - 1]
				}
				d = append(d, i-1)
			}
			currMax := int64(-1)
			if len(d) > 0 {
				currMax = F[d[0]][z-1]
			}
			if currMax != -1 {
				F[i][z] = currMax + a[i]
			}
		}
	}
	ans := int64(-1)
	for i := 0; i < k; i++ {
		ans = max(ans, F[n-i-1][x])
	}
	fmt.Println(ans)
}

package main

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

const (
	MaxN       = 5005
	MaxK       = 5005
	NoSolution = -1
)

var n, k, x int
var F [MaxN][MaxK]int64
var a [MaxN]int64

func init() {
	for i := 0; i < MaxN; i++ {
		for j := 0; j < MaxK; j++ {
			F[i][j] = NoSolution
		}
	}
}

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

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &k, &x)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}
	for i := 0; i < k; i++ {
		F[i][1] = a[i]
	}
	for z := 2; z <= x; z++ {
		d := make([]int, 0)
		for i := 1; i < n; i++ {
			if z > i + 1 {
				continue
			}
			if len(d) > 0 && i - d[0] > k {
				d = d[1:]
			}
			if F[i-1][z-1] != - 1 {
				for len(d) > 0 && F[d[len(d) - 1]][z-1] < F[i-1][z-1] {
					d = d[:len(d) - 1]
				}
				d = append(d, i-1)
			}
			currMax := int64(NoSolution)
			if len(d) > 0 {
				currMax = F[d[0]][z-1]
			}
			if currMax != NoSolution {
				F[i][z] = currMax + a[i]
			}
		}
	}
	ans := int64(NoSolution)
	for i := 0; i < k; i++ {
		ans = max(ans, F[n-i-1][x])
	}
	fmt.Println(ans)
}

package main

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

const (
	MaxN       = 5005
	MaxK       = 5005
	NoSolution = -1
)

var n, k, x int
var F [MaxN][MaxK]int64
var a [MaxN]int64

func init() {
	for i := 0; i < MaxN; i++ {
		for j := 0; j < MaxK; j++ {
			F[i][j] = NoSolution
		}
	}
}

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

func getMax(startPos, row int) int64 {
	best := int64(-1)
	for j := 1; j <= k; j++ {
		if startPos - j < 0 {
			break
		}
		if F[startPos-j][row] != NoSolution {
			best = max(best, F[startPos-j][row])
		}
	}
	return best
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &k, &x)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}
	for i := 0; i < k; i++ {
		F[i][1] = a[i]
	}
	for z := 2; z <= x; z++ {
		d := make([]int, 0)

		for i := 1; i < n; i++ {
			if z > i + 1 {
				continue
			}
			if len(d) > 0 && i - d[0] > k {
				d = d[1:]
			}
			if F[i-1][z-1] != - 1 {
				for len(d) > 0 && F[d[len(d) - 1]][z-1] < F[i-1][z-1] {
					d = d[:len(d) - 1]
				}
				d = append(d, i-1)
			}
			currMax := int64(-1)
			if len(d) > 0 {
				currMax = F[d[0]][z-1]
			}
			if currMax != -1 {
				F[i][z] = currMax + a[i]
			}
		}
	}
	ans := int64(-1)
	for i := 0; i < k; i++ {
		ans = max(ans, F[n-i-1][x])
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

func main() {
	var n, k, x int64
	dp := make([][210]int64, 210)
	fmt.Scan(&n, &k, &x)
	var ans int64 = 0
	for i := int64(1); i <= n; i++ {
		var tmp int64
		fmt.Scan(&tmp)
		dp[i][1] = tmp

		if ans < dp[i][1] {
			ans = dp[i][1]
		}
		for j := int64(i-1); i-j <= k && j > 0; j-- {
			var jj = int64(x-1)
			for ; jj >= 1; jj-- {
				if dp[j][jj]>0&& dp[i][jj+1] < dp[j][jj]+tmp {
					dp[i][jj+1] = dp[j][jj] + tmp
					if ans < dp[i][jj+1] {
						ans = dp[i][jj+1]
					}
				}
			}
		}

	}
	//for i := int64(1); i <= n; i++{
	//	for j := int64(1); j <= x; j++{
	//		fmt.Printf("%d,",dp[i][j])
	//	}
	//	fmt.Printf("\n")
	//}
	fmt.Print(ans)
}

package main

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

const (
	MaxN       = 2505
	MaxK       = 2505
	NoSolution = -1
)

var n, k, x int
var F [MaxN][MaxK]int64
var a [MaxN]int64

func init() {
	for i := 0; i < MaxN; i++ {
		for j := 0; j < MaxK; j++ {
			F[i][j] = NoSolution
		}
	}
}

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

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &k, &x)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}
	for i := 0; i < k; i++ {
		F[i][1] = a[i]
	}
	for i := k; i < n; i++ {
		for j := 1; j < k; j++ {
			for z := 2; z <= x; z++ {
				if F[i-j][z-1] != NoSolution {
					F[i][z] = max(F[i][z], F[i-j][z-1] + a[i])
				}
				if F[i-j][z] != NoSolution {
					F[i][z] = max(F[i][z], F[i-j][z])
				}
			}
		}
	}
	ans := int64(-1)
	for i := 0; i < k; i++ {
		ans = max(ans, F[n-i-1][x])
	}
	fmt.Println(ans)
}

package main

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

const (
	MaxN       = 2505
	MaxK       = 2505
	NoSolution = -1
)

var n, k, x int
var F [MaxN][MaxK]int64
var a [MaxN]int64

func init() {
	for i := 0; i < MaxN; i++ {
		for j := 0; j < MaxK; j++ {
			F[i][j] = NoSolution
		}
	}
}

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

func getMax(startPos, row int) int64 {
	best := int64(-1)
	for j := 1; j <= k; j++ {
		if startPos < j {
			break
		}
		if F[startPos-j][row-1] != NoSolution {
			best = max(best, F[startPos-j][row-1])
		}
	}
	return best
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d %d\n", &n, &k, &x)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}
	for i := 0; i < k; i++ {
		F[i][1] = a[i]
	}
	for i := 1; i < n; i++ {
		for z := 2; z <= x; z++ {
			if z > i + 1 {
				break
			}
			currMax := getMax(i, z-1)
			if currMax != -1 {
				F[i][z] = currMax + a[i]
			}
		}
	}

	ans := int64(-1)
	for i := 0; i < k; i++ {
		ans = max(ans, F[n-i-1][x])
	}
	fmt.Println(ans)
}