package main

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

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	// read one word/char/int separated by space(s).
	sc.Split(bufio.ScanWords)

	// read one line.
	// sc.Split(bufio.ScanLines)
}

type edge struct {
	from int
	to int
	cost int
}

type nodes map[int]interface{}

func solve(edges *[]*edge, start, goal, num int) (int, bool) {
	ns := nodes{}
	ns[start] = 0
	updated := true
	count := 0
	prev := 0
	for updated {
		updated = false
		count ++
		for _, e := range *edges {
			if ns[e.from] == nil {
				continue
			}
			if ns[e.to] == nil {
				ns[e.to] = ns[e.from].(int) + e.cost
				updated = true
				continue
			} else {
				if ns[e.from].(int) + e.cost > ns[e.to].(int) {
					ns[e.to] = ns[e.from].(int) + e.cost
					updated = true
					continue
				}
			}
		}
		if count >= num && ns[goal] == prev {
			return prev, true
		}
		if count >= num && ns[goal] != prev {
			return -1, false
		}
		if _, ok := ns[goal].(int); !ok {
			continue
		}
		prev = ns[goal].(int)
	}
	return ns[goal].(int), true
}

func main() {
	N := nextInt()
	M := nextInt()
	P := nextInt()
	edges := make([]*edge, M)
	for i := 0; i < M; i++ {
		e := edge{}
		e.from = nextInt()
		e.to = nextInt()
		e.cost = nextInt() - P
		edges[i] =  &e
	}
	ans, ok := solve(&edges, 1, N, N)
	if !ok {
		fmt.Println(-1)
		return
	}
	if ans < 0 {
		ans = 0
	}
	fmt.Println(ans)
}

Score : 500 points

Problem Statement

There is a directed graph with N vertices numbered 1 to N and M edges.
The i-th edge is directed from Vertex A_i to Vertex B_i, and there are C_i coins placed along that edge.
Additionally, there is a button on Vertex N.

We will play a game on this graph.
You start the game on Vertex 1 with zero coins, and head for Vertex N by traversing the edges while collecting coins.
It takes one minute to traverse an edge, and you can collect the coins placed along the edge each time you traverse it.
As usual in games, even if you traverse an edge once and collect the coins, the same number of coins will reappear next time you traverse that edge, which you can collect again.

When you reach Vertex N, you can end the game by pressing the button. (You can also choose to leave Vertex N without pressing the button and continue traveling.)
However, when you end the game, you will be asked to pay T \times P coins, where T is the number of minutes elapsed since the start of the game. If you have less than T \times P coins, you will have to pay all of your coins instead.

Your score will be the number of coins you have after this payment.
Determine if there exists a maximum value of the score that can be obtained. If the answer is yes, find that maximum value.

Constraints

2 \leq N \leq 2500

1 \leq M \leq 5000

1 \leq A_i, B_i \leq N

1 \leq C_i \leq 10^5

0 \leq P \leq 10^5

All values in input are integers.

Vertex N can be reached from Vertex 1.

Input

Input is given from Standard Input in the following format:

N M P
A_1 B_1 C_1
:
A_M B_M C_M

Output

If there exists a maximum value of the score that can be obtained, print that maximum value; otherwise, print -1.

Sample Input 1

3 3 10
1 2 20
2 3 30
1 3 45

Sample Output 1

35

There are two ways to travel from Vertex 1 to Vertex 3:

Vertex 1 \rightarrow 2 \rightarrow 3: You collect 20 + 30 = 50 coins on the way. After two minutes from the start of the game, you press the button, pay 2 \times 10 = 20 coins, and you have 50 - 20 = 30 coins left.

Vertex 1 \rightarrow 2: You collect 45 coins on the way. After one minute from the start of the game, you press the button, pay 1 \times 10 = 10 coins, and you have 45 - 10 = 35 coins left.

Thus, the maximum score that can be obtained is 35.

Sample Input 2

2 2 10
1 2 100
2 2 100

Sample Output 2

-1

The edge extending from Vertex 1 takes you to Vertex 2. If you then traverse the edge extending from Vertex 2 to itself t times and press the button, your score will be 90 + 90t. Thus, you can infinitely increase your score, which means there is no maximum value of the score that can be obtained.

Sample Input 3

4 5 10
1 2 1
1 4 1
3 4 1
2 2 100
3 3 100

Sample Output 3

0

There is no way to travel from Vertex 1 to Vertex 4 other than traversing the edge leading from Vertex 1 to Vertex 4 directly. You will pick up one coin along this edge, but after being asked to paying 10 coins, your score will be 0.

Note that you can collect an infinite number of coins if you traverse the edge leading from Vertex 1 to Vertex 2, but this is pointless since you can no longer reach Vertex 4 and end the game.


package main

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

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	// read one word/char/int separated by space(s).
	sc.Split(bufio.ScanWords)

	// read one line.
	// sc.Split(bufio.ScanLines)
}

type edge struct {
	from int
	to int
	cost int
}

type nodes map[int]interface{}

func solve(edges []edge, start, goal, num int) (int, bool) {
	ns := nodes{}
	ns[start] = 0
	updated := true
	count := 0
	prev := 0
	for updated {
		updated = false
		count ++
		for _, e := range edges {
			if ns[e.from] == nil {
				continue
			}
			if ns[e.to] == nil {
				if _, ok := ns[e.from].(int); !ok {
					continue
				}
				ns[e.to] = ns[e.from].(int) + e.cost
				updated = true
				continue
			} else {
				if ns[e.from].(int) + e.cost > ns[e.to].(int) {
					ns[e.to] = ns[e.from].(int) + e.cost
					updated = true
					continue
				}
			}
		}
		if count > num && ns[goal] == prev {
			return prev, true
		}
		if count > num && ns[goal] != prev {
			return -1, false
		}
		prev = ns[goal].(int)
	}
	return ns[goal].(int), true
}

func main() {
	N := nextInt()
	M := nextInt()
	P := nextInt()
	edges := make([]edge, 0, M)
	for i := 0; i < M; i++ {
		from := nextInt()
		to := nextInt()
		cost := nextInt()
		edges = append(edges, edge{
			from,
			to,
			cost-P,
		})
	}
	ans, ok := solve(edges, 1, N, N)
	if !ok {
		fmt.Println(-1)
		return
	}
	if ans < 0 {
		ans = 0
	}
	fmt.Println(ans)
}

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


// https://atcoder.jp/contests/abc137/tasks/abc137_e

package main

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

// d: debug IO. it can print debug in test.
func solve(io *Io, d *Io) {
	N := io.NextInt()
	M := io.NextInt()
	P := io.NextInt()

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

	for i := 0; i < M; i++ {
		A := io.NextInt()
		B := io.NextInt()
		C := io.NextInt()

		edges[i] = []int{
			A,
			B,
			C,
		}
	}

	fromToEdges := make([][][]int, N+1)
	for i := range fromToEdges {
		fromToEdges[i] = make([][]int, 0)
	}

	toFromEdges := make([][][]int, N+1)
	for i := range toFromEdges {
		toFromEdges[i] = make([][]int, 0)
	}

	for _, e := range edges {
		fromToEdges[e[0]] = append(fromToEdges[e[0]], []int{e[1], e[2]})
		toFromEdges[e[1]] = append(toFromEdges[e[1]], []int{e[0], e[2]})
	}

	goalReachableNodes := map[int]bool{}

	var addGoalReachableNodes func(currentNode int)
	addGoalReachableNodes = func(currentNode int) {
		// already added
		if goalReachableNodes[currentNode] {
			return
		}

		goalReachableNodes[currentNode] = true

		edges := toFromEdges[currentNode]
		for _, edge := range edges {
			addGoalReachableNodes(edge[0])
		}
	}

	addGoalReachableNodes(N)

	// 各 node へのスコアの最大値
	scoreMap := map[int]int{}

	var dfs func(node, score, visited int) bool
	dfs = func(node int, score int, visited int) bool {
		_, included := scoreMap[node]

		if included {
			if score > scoreMap[node] {
				if visited > len(fromToEdges) {
					return true
				}

				scoreMap[node] = score
			} else {
				return false
			}
		}

		visited++
		scoreMap[node] = score

		edges := fromToEdges[node]

		for _, edge := range edges {
			nextScore := score + edge[1] - P
			nextNode := edge[0]

			if !goalReachableNodes[nextNode] {
				continue
			}

			if dfs(nextNode, nextScore, visited) {
				return true
			}
		}

		return false
	}

	infinity := dfs(1, 0, 0)

	if infinity {
		io.Println(-1)
		return
	}

	io.Println(Max(scoreMap[N], 0))
}

// ベルマンフォードに帰着
func solve2(io *Io, d *Io) {
	N := io.NextInt()
	M := io.NextInt()
	P := io.NextInt()

	type Edge struct {
		from int
		to   int
		cost int
	}

	edges := make([]Edge, M)

	for i := 0; i < M; i++ {
		A := io.NextInt()
		B := io.NextInt()
		C := io.NextInt()

		edges[i] = Edge{
			from: A,
			to:   B,
			cost: -(C - P), // 最短経路問題にするためマイナスをかける
		}
	}

	fromToEdges := make([][]int, N+1)
	toFromEdges := make([][]int, N+1)

	for _, e := range edges {
		fromToEdges[e.from] = append(fromToEdges[e.from], e.to)
		toFromEdges[e.to] = append(toFromEdges[e.to], e.from)
	}

	var addReachableNodes func(node int, edges [][]int, reachableMap map[int]bool)
	addReachableNodes = func(node int, edges [][]int, reachableMap map[int]bool) {
		if reachableMap[node] {
			return
		}

		reachableMap[node] = true

		for _, to := range edges[node] {
			addReachableNodes(to, edges, reachableMap)
		}
	}

	startReachableNodes := map[int]bool{}
	addReachableNodes(1, fromToEdges, startReachableNodes)
	goalReachableNodes := map[int]bool{}
	addReachableNodes(N, toFromEdges, goalReachableNodes)
	reachableNodes := map[int]bool{}

	for node, startReachable := range startReachableNodes {
		if startReachable && goalReachableNodes[node] {
			reachableNodes[node] = true
		}
	}

	INF := int(1e18)
	costs := make([]int, N+1)
	for i := range costs {
		costs[i] = INF
	}
	costs[1] = 0

	for i := 1; i <= N; i++ {
		updated := false

		for _, e := range edges {
			if !reachableNodes[e.to] || !reachableNodes[e.from] {
				continue
			}

			if costs[e.from]+e.cost < costs[e.to] {
				costs[e.to] = costs[e.from] + e.cost
				updated = true

				// 負の閉路が無ければ高々 N-1 回 しか更新されない
				if i == N {
					io.Println(-1)
					return
				}
			}
		}

		if !updated {
			break
		}
	}

	io.Println(Max(-costs[N], 0))
}

func main() {
	io := NewIo()
	defer io.Flush()
	solve2(io, nil)
}

/* IO Helpers */

// Io combines reader, writer, & tokens as the state when processing the input
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

// NewIo creates Io with Stdin & Stdout
func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

// Flush calls the writer's Flush
func (io *Io) Flush() {
	io.writer.Flush()
}

// NextLine returns the string from reader.ReadLine()
func (io *Io) NextLine() string {
	if io.nextToken < len(io.tokens) {
		panic("io.nextToken < len(io.tokens)")
	}

	var buffer bytes.Buffer

	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buffer.Write(line)
		if !isPrefix {
			break
		}
	}

	return buffer.String()
}

// NextLines returns []string from next n lines
func (io *Io) NextLines(n int) []string {
	res := make([]string, n)
	for i := 0; i < n; i++ {
		res[i] = io.NextLine()
	}
	return res
}

// Next returns the string token (partial string of the line divided by spaces)
func (io *Io) Next() string {
	if io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}

	res := io.tokens[io.nextToken]
	io.nextToken++
	return res
}

// NextStrings returns the []string from the next n tokens
func (io *Io) NextStrings(n int) []string {
	res := make([]string, n)
	for i := 0; i < n; i++ {
		res[i] = io.Next()
	}
	return res
}

// NextInt returns the int from the next token
func (io *Io) NextInt() int {
	res, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return res
}

// NextInts returns the []int from the next n tokens
func (io *Io) NextInts(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = io.NextInt()
	}
	return res
}

// NextFloat returns the float64 from the next token
func (io *Io) NextFloat() float64 {
	res, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

// NextFloats returns the []float64 from the next n tokens
func (io *Io) NextFloats(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = io.NextFloat()
	}
	return res
}

// Println prints the input to the writer in an easy-to-see form
func (io *Io) Println(a ...interface{}) {
	var values []string
	for i := 0; i < len(a); i++ {
		values = append(values, "%v")
	}
	io.Printfln(strings.Join(values, " "), a...)
}

// Print calls Fprint to the writer
func (io *Io) Print(a interface{}) {
	fmt.Fprint(io.writer, a)
}

// Printfln calls Fprint to the writer
func (io *Io) Printfln(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format+"\n", a...)
}

// Debug calls Println and Flush immediately
func (io *Io) Debug(a ...interface{}) {
	if io == nil {
		return
	}

	io.Println(a...)
	io.Flush()
}

/* Math Helpers */

// MOD is 10^9 + 7
const MOD = 1000000007

// Max of the ints
func Max(numbers ...int) int {
	max := numbers[0]
	for i, number := range numbers {
		if i == 0 {
			continue
		}
		if number > max {
			max = number
		}
	}
	return max
}

// Min of the ints
func Min(numbers ...int) int {
	max := numbers[0]
	for i, number := range numbers {
		if i == 0 {
			continue
		}
		if max > number {
			max = number
		}
	}
	return max
}

// DigitSum returns sum of the int's digits
func DigitSum(n int) int {
	if n < 0 {
		return -1
	}

	res := 0

	for n > 0 {
		res += n % 10
		n /= 10
	}

	return res
}

// Sum of the ints
func Sum(numbers ...int) int {
	sum := 0

	for _, number := range numbers {
		sum += number
	}

	return sum
}

// Pow calculates the pow for int with O(log e)
func Pow(a, e int) int {
	if a < 0 || e < 0 {
		panic(fmt.Sprintf("Pow was called for a < 0 or e < 0, a: %d, e: %d", a, e))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		half := Pow(a, e/2)
		return half * half
	}

	return a * Pow(a, e-1)
}

// Abs returns the absolute value of the input
func Abs(a int) int {
	if a < 0 {
		return -a
	}

	return a
}

/* Structure Helpers */

// Set is orderless List
type Set map[interface{}]struct{}

// NewSet returns empty Set
func NewSet() *Set {
	return &Set{}
}

// Add the value to the set
func (set *Set) Add(value interface{}) {
	(*set)[value] = struct{}{}
}

// Includes check the set has the value
func (set *Set) Includes(value interface{}) bool {
	_, included := (*set)[value]
	return included
}

// Remove deletes the value from the set if exists
func (set *Set) Remove(value interface{}) {
	if !set.Includes(value) {
		return
	}

	delete(*set, value)
}

// ImmutableSort returns sorted slice without mutating original one
func ImmutableSort(slice []int) []int {
	res := make([]int, len(slice))
	copy(res, slice)
	sort.Ints(res)
	return res
}

/* Util helpers */

// Ternary is like `cond ? a : b`. should assert the returned value like `Ternary(cond, a, b).(int)`
func Ternary(cond bool, a, b interface{}) interface{} {
	if cond {
		return a
	}

	return b
}

// TernaryInt uses Ternary and assert the value as int
func TernaryInt(cond bool, a, b int) int {
	return Ternary(cond, a, b).(int)
}

// TernaryString uses Ternary and assert the value as string
func TernaryString(cond bool, a, b string) string {
	return Ternary(cond, a, b).(string)
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

func Scan() string {
	sc.Scan()
	return sc.Text()
}
func iScan() int {
	n, _ := strconv.Atoi(Scan())
	return n
}
func fScan() float64 {
	n, _ := strconv.ParseFloat(Scan(), 64)
	return n
}
func stringToInt(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}
func SScan(n int) []string {
	a := make([]string, n)
	for i := 0; i < n; i++ {
		a[i] = Scan()
	}
	return a
}
func iSScan(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = iScan()
	}
	return a
}
func abs(x int) int {
	if x < 0 {
		x = -x
	}
	return x
}
func larger(a, b int) int {
	if a < b {
		return b
	} else {
		return a
	}
}
func smaller(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}
func max(a []int) int {
	x := a[0]
	for i := 0; i < len(a); i++ {
		if x < a[i] {
			x = a[i]
		}
	}
	return x
}
func min(a []int) int {
	x := a[0]
	for i := 0; i < len(a); i++ {
		if x > a[i] {
			x = a[i]
		}
	}
	return x
}
func sum(a []int) int {
	x := 0
	for _, v := range a {
		x += v
	}
	return x
}

var mod int = 1000000007

func main() {
	buf := make([]byte, 0)
	sc.Buffer(buf, mod)
	sc.Split(bufio.ScanWords)
	n := iScan()
	h := iSScan(n)
	h[0]--
	ans := "Yes"
	for i := 1; i < n; i++ {
		if h[i-1] > h[i] {
			ans = "No"
			break
		} else if h[i-1] < h[i] {
			h[i]--
		}
	}
	fmt.Println(ans)
}

Score : 300 points

Problem Statement

There are N squares arranged in a row from left to right. The height of the i-th square from the left is H_i.

For each square, you will perform either of the following operations once:

Decrease the height of the square by 1.

Do nothing.

Determine if it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq H_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

If it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right, print Yes; otherwise, print No.

Sample Input 1

5
1 2 1 1 3

Sample Output 1

Yes

You can achieve the objective by decreasing the height of only the second square from the left by 1.

Sample Input 2

4
1 3 2 1

Sample Output 2

No

Sample Input 3

5
1 2 3 4 5

Sample Output 3

Yes

Sample Input 4

1
1000000000

Sample Output 4

Yes


package main

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

var sc = bufio.NewScanner(os.Stdin)

func main() {
	scanInit()

	n := nextInt()
	l := make([]int, n)
	ticket := 0
	l[0] = nextInt()
	for i := 1; i < n; i++ {
		l[i] = nextInt()
		//fmt.Println(l[i-1], "->", l[i])
		if l[i-1]-l[i] >= 2 {
			fmt.Println("No")
			return
		}
		if l[i-1]-l[i] == 1 {
			if ticket == 0 {
				ticket = 1
				//fmt.Println("ticket")
				if i >= 2 && l[i-1] == l[i-2] {
					//fmt.Println("ticketed")
					fmt.Println("No")
					return
				}
			} else {
				fmt.Println("No")
				return
			}
		}
	}
	fmt.Println("Yes")
}

// ---- readfunc
func scanInit() {
	const cap = 200 * 1024 // Default=64
	var buf = make([]byte, cap)
	sc.Buffer(buf, cap)
	sc.Split(bufio.ScanWords)
	return
}
func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}
func nextStr() string {
	sc.Scan()
	return sc.Text()
}


package main

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

const (
	maxBufSize = 1024 * 1024
)

var (
	sc = func() *bufio.Scanner {
		sc := bufio.NewScanner(os.Stdin)
		buf := make([]byte, 1)
		sc.Buffer(buf, maxBufSize)
		sc.Split(bufio.ScanWords)
		return sc
	}()
	out = bufio.NewWriter(os.Stdout)
)

const yes = "Yes"
const no = "No"

func solve(n int64, hs []int64) bool {
	prev := hs[n - 1]
	for i := n - 1 ; i >= int64(0); i-- {
		if hs[i] > prev + 1 {
			return false
		}
		if hs[i] > prev {
			hs[i] --
		}
		prev = hs[i]
	}
	return true
}

func main() {
	defer out.Flush()

	n := nextInt()
	hs := nextInts(n)
	if solve(n, hs) {
		fmt.Fprintln(out, yes)
	} else {
		fmt.Fprintln(out, no)
	}
}

func next() string {
	sc.Scan()
	return sc.Text()
}

func nextStringAsBytes() []byte {
	sc.Scan()
	return []byte(sc.Text())
}

func nextInt() int64 {
	a, _ := strconv.ParseInt(next(), 10, 64)
	return a
}

func nextFloat() float64 {
	a, _ := strconv.ParseFloat(next(), 64)
	return a
}

func nextInts(n int64) []int64 {
	ret := make([]int64, n)
	for i := int64(0); i < n; i++ {
		ret[i] = nextInt()
	}
	return ret
}

func nextFloats(n int64) []float64 {
	ret := make([]float64, n)
	for i := int64(0); i < n; i++ {
		ret[i] = nextFloat()
	}
	return ret
}
func nextStrings(n int64) []string {
	ret := make([]string, n)
	for i := int64(0); i < n; i++ {
		ret[i] = next()
	}
	return ret
}

func containStr(arr []string, target string) bool {
	for _, v := range arr {
		if v == target {
			return true
		}
	}
	return false
}

func PrintOut(src interface{}, joinner string) {
	switch reflect.TypeOf(src).Kind() {
	case reflect.Slice:
		s := reflect.ValueOf(src)
		for idx := 0; idx < s.Len(); idx++ {
			fmt.Fprintf(out, "%v", s.Index(idx))
			if idx < s.Len()-1 {
				fmt.Fprintf(out, "%s", joinner)
			} else {
				fmt.Fprintln(out)
			}
		}
	default:
		fmt.Fprintln(out, "orz")
	}
}

// mapからkeysとvaluesを返す
func splitKeyValue(m map[interface{}]interface{}) (keys, values []interface{}) {
	switch reflect.TypeOf(m).Kind() {
	case reflect.Map:
		s := reflect.ValueOf(m)
		for _, v := range s.MapKeys() {
			keys = append(keys, v)
			values = append(values, s.MapIndex(v))
		}
	default:
		fmt.Fprintln(out, "orz")
	}
	return
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

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

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

func readInt64() int64 {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return int64(i)
}

func readfloat64() float64 {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return float64(i)
}

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

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
}

func gcd(x, y int) int {
	if y == 0 {
		return x
	}
	return gcd(y, x%y)
}

const Mod = 1000000007

func ModComb(n, k int) int {
	if k == 0 {
		return 1
	}
	x, y := n, k
	for i := 1; i < k; i++ {
		x = x * (n - i) % Mod
		y = y * (k - i) % Mod
	}
	inv := ModInv(y)
	return x * inv % Mod
}

func ModFact(x int) int {
	fact := 1
	for i := 1; i <= x; i++ {
		fact = fact * i % Mod
	}
	return fact
}

func ModInv(x int) int {
	return ModPow(x, Mod-2)
}

func ModPow(a, n int) int {
	res := 1
	for n > 0 {
		if n%2 != 0 {
			res = res * a % Mod
		}
		a = a * a % Mod
		n /= 2
	}
	return res
}

func pow(a, n int) int {
	if n == 0 {
		return 1
	}

	ans := 0
	if n%2 == 0 {
		ans = pow(a*a, n/2)
	} else {
		ans = a * pow(a*a, (n-1)/2)
	}
	return ans
}

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

	n := readInt()

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

	for i := 1; i < n-1; i++ {
		if h[i+1]-h[i] < -1 {
			fmt.Println("No")
			return
		}
		if h[i+1]-h[i] == -1 {
			h[i] -= 1
		}
		if h[i]-h[i-1] < 0 {
			fmt.Println("No")
			return
		}
	}
	fmt.Println("Yes")
}


package main

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

func out(x ...interface{}) {
	fmt.Println(x...)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getString() string {
	sc.Scan()
	return sc.Text()
}

// min, max, asub, absなど基本関数
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
}

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

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

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

func main() {
	sc.Split(bufio.ScanWords)
	N := getInt()
	h := make([]int, N)
	for i := 0; i < N; i++ {
		h[i] = getInt()
	}

	for i := 1; i < N; i++ {
		if h[i] > h[i-1] {
			h[i]--
		}
	}
	// out(h)
	for i := 1; i < N; i++ {
		if h[i] < h[i-1] {
			out("No")
			return
		}
	}
	out("Yes")
	return
}


package main

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

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

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() uint64 {
	sc.Scan()
	i, e := strconv.ParseUint(sc.Text(), 10, 64)
	if e != nil {
		panic(e)
	}
	return i
}

func intArray(n []string) []int {
	var m []int
	for i, _ := range n {
		v, _ := strconv.Atoi(n[i])
		m = append(m, v)
	}
	return m
}

func uint64Array(n []string) []uint64 {
	var m []uint64
	for i, _ := range n {
		v, _ := strconv.ParseUint(n[i], 10, 64)
		m = append(m, v)
	}
	return m
}

func intExistIn(v int, arr []int) bool {
	for _, a := range arr {
		if a == v {
			return true
		}
	}

	return false
}

func stringExistIn(v string, arr []string) bool {
	for _, a := range arr {
		if a == v {
			return true
		}
	}

	return false
}

func gcd(m, n uint64) uint64 {
	x := new(big.Int)
	y := new(big.Int)
	z := new(big.Int)
	a := new(big.Int).SetUint64(m)
	b := new(big.Int).SetUint64(n)
	return z.GCD(x, y, a, b).Uint64()
}

func uintArrayToStr(a []uint64) string {
	var S string

	for _, v := range a {
		S += strconv.Itoa(int(v))
	}
	return S
}

func remove(x []uint64, y int) []uint64 {
	var newArray []uint64
	for i, _ := range x {
		if i != y {
			newArray = append(newArray, x[i])
		}
	}
	return newArray
}

func ascendSort(a []uint64) []uint64 {
	for i := 0; i < len(a); i++ {
		for i := 0; i < len(a)-1; i++ {
			if a[i] > a[i+1] {
				tmp := a[i+1]
				a[i+1] = a[i]
				a[i] = tmp
			}
		}
	}
	return a
}

func descendSort(a []uint64) []uint64 {
	for i := 0; i < len(a); i++ {
		for i := 0; i < len(a)-1; i++ {
			if a[i] < a[i+1] {
				tmp := a[i+1]
				a[i+1] = a[i]
				a[i] = tmp
			}
		}
	}
	return a
}

func maxAndIndex(a []uint64) (uint64, int) {
	max := uint64(0)
	index := 0
	for i, v := range a {
		if v > max {
			max = v
			index = i
		}
	}
	return max, index
}

func sum(a []uint64) uint64 {
	sum := uint64(0)
	for _, v := range a {
		sum += v
	}
	return sum
}

func main() {
	// sc.Split(bufio.ScanWords)
	_ = int(nextInt())
	L := intArray(strings.Split(nextLine(), " "))

	for i := len(L)-1; i > 0; i-- {

		if L[i-1] - L[i] > 1{
			fmt.Println("No")
			os.Exit(0)
		} else if L[i-1] - L[i] == 1 {
			L[i-1] -= 1
		}
	}

	fmt.Println("Yes")
}


package main

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

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

	h := make([]int, n)
	sc := bufio.NewScanner(os.Stdin)
	buffer := make([]byte, 10000)
	sc.Buffer(buffer, 2000000)
	sc.Scan()
	args := strings.Split(sc.Text(), " ")
	for i := 0; i < n; i++ {
		h[i], _ = strconv.Atoi(args[i])
	}

	able := true
	b := h[0] - 1
	for i := 1; i < n; i++ {
		if b <= h[i]-1 {
			b = h[i] - 1
		} else if b <= h[i] {
			b = h[i]
		} else {
			able = false
			break
		}
	}

	if able {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}

func main() {
	solve()
}


package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	var ini int
	fmt.Scan(&ini)
	a := ini
	m := 0
	for i := 1; i < n; i++ {
		var h int
		fmt.Scan(&h)
		m = max(m, h)
		if a <= h {
			a = h
			continue
		} else if a-1 == h && m == h+1 {
			a = h
			continue
		} else {
			fmt.Println("No")
			return
		}
	}
	fmt.Println("Yes")
}

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


package main

import "fmt"

func main() {
	var N int
	fmt.Scanf("%d", &N)
	H := make([]int, N)
	for i := 0; i < N; i++ {
		fmt.Scanf("%d", &H[i])
	}

	current := 0
	flag := true
	for _, h := range H {
		if current > h {
			flag = false
			break
		} else if current < h {
			current = h - 1
		}
	}

	if flag {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}


package main

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

func main() {
	var N int
	fmt.Scanln(&N)

	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	var cur int
	for i := 0; i < N; i++ {
		sc.Scan()
		h, _ := strconv.Atoi(sc.Text())

		if h < cur-1 {
			fmt.Println("No")
			return
		} else if h > cur {
			cur = h
		}
	}
	fmt.Println("Yes")
}


package main

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

const pi = math.Pi

var mod int = pow(10, 9) + 7
var Umod uint64 = 1000000007
var cnt, ans int

func main() {
	reader.Split(bufio.ScanWords)
	N, _ := strconv.Atoi(read())
	H := make([]int, N)
	for i := 0; i < N; i++ {
		H[i], _ = strconv.Atoi(read())
	}
	for i := N - 1; 0 < i; i-- {
		if H[i] < H[i-1] {
			H[i-1]--
		}

		if H[i] < H[i-1] {
			fmt.Println("No")
			return
		}
	}
	fmt.Println("Yes")
}

/*  ----------------------------------------  */

var reader = bufio.NewScanner(os.Stdin)

func read() string {
	reader.Scan()
	return reader.Text()
}

func lcm(x, y int) int {
	return (x / gcd(x, y)) * y
}

func gcd(x, y int) int {
	if x%y == 0 {
		return y
	} else {
		r := x % y
		return gcd(y, r)
	}
}

var fac [1000000]int
var finv [1000000]int
var inv [1000000]int

func combination_init() {
	fac[0], fac[1] = 1, 1
	finv[0], finv[1] = 1, 1
	inv[1] = 1

	// invは a^(-1) mod p
	// pをaで割ることを考える

	// p/a*(a) + p%a = p
	// p/a*(a) + p%a = 0          (mod p)
	// -p%a = p/a*(a)             (mod p)
	// -p%a *a^(-1)= p/a          (mod p)
	// a^(-1)= p/a * (-p%a)^(-1)  (mod p)
	// a^(-1) =

	for i := 2; i < 1000000; i++ {
		fac[i] = fac[i-1] * i % mod
		inv[i] = mod - inv[mod%i]*(mod/i)%mod
		finv[i] = finv[i-1] * inv[i] % mod
	}
}

func combination(x, y int) int {
	if x < y {
		return 0
	}
	if fac[0] != 1 {
		combination_init()
	}
	return fac[x] * (finv[y] * finv[x-y] % mod) % mod
	//return fac[x] / (fac[y] * fac[x-y])
}

func permutation(x, y int) int {
	if x < y {
		return 0
	}
	if fac[0] != 1 {
		combination_init()
	}
	return fac[x] * (finv[x-y] % mod) % mod
	//return fac[x] / fac[x-y]
}

func max(x ...int) int {
	var res int = x[0]
	for i := 1; i < len(x); i++ {
		res = int(math.Max(float64(x[i]), float64(res)))
	}
	return res
}

func min(x ...int) int {
	var res int = x[0]
	for i := 1; i < len(x); i++ {
		res = int(math.Min(float64(x[i]), float64(res)))
	}
	return res
}
func pow(x, y int) int { return int(math.Pow(float64(x), float64(y))) }
func abs(x int) int    { return int(math.Abs(float64(x))) }
func floor(x int) int  { return int(math.Floor(float64(x))) }
func ceil(x int) int   { return int(math.Ceil(float64(x))) }

type SortBy [][]int

func (a SortBy) Len() int      { return len(a) }
func (a SortBy) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SortBy) Less(i, j int) bool {
	if a[i][0] == a[j][0] {
		return a[i][1] > a[j][1]
	}
	return a[i][0] < a[j][0]
}

type PriorityQueue []int

func (h PriorityQueue) Len() int            { return len(h) }
func (h PriorityQueue) Less(i, j int) bool  { return h[i] > h[j] }
func (h PriorityQueue) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *PriorityQueue) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *PriorityQueue) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}


package main

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

// type ByValue []float64

// func (s ByValue) Len() int           { return len(s) }
// func (s ByValue) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
// func (s ByValue) Less(i, j int) bool { return s[i] < s[j] }

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())
	sc.Scan()
	strH := sc.Text()
	strHList := strings.Split(strH, " ")
	hList := []int{}
	for _, v := range strHList {
		intV, _ := strconv.Atoi(v)
		hList = append(hList, intV)
	}
	res := "Yes"
	// max := hList[0]
	max := 0
	for i := 1; i < n; i++ {
		// if hList[i] < max {
		// 	res = "No"
		// }
		// if max < hList[i] {
		// 	max = hList[i] - 1
		// }
		max = hList[i]
	}
	fmt.Println(max)
	fmt.Println(res)
}


package main

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

// type ByValue []float64

// func (s ByValue) Len() int           { return len(s) }
// func (s ByValue) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
// func (s ByValue) Less(i, j int) bool { return s[i] < s[j] }

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())
	sc.Scan()
	strH := sc.Text()
	strHList := strings.Split(strH, " ")
	hList := []int{}
	for _, v := range strHList {
		intV, _ := strconv.Atoi(v)
		hList = append(hList, intV)
	}
	res := "Yes"
	max := hList[0]
	for i := 1; i < n; i++ {
		if hList[i] < max {
			res = "No"
		}
		if max < hList[i] {
			max = hList[i] - 1
		}
	}
	fmt.Println(res)
}


package main

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

func main() {
	sc := NewScanner()

	n := sc.NextInt()
	prev := -1
	for i := 0; i < n; i++ {
		h := sc.NextInt()
		if h > prev {
			prev = h - 1
		} else if h == prev {
			prev = h
		} else {
			fmt.Printf("No\n") // output for debug
			return
		}
	}
	fmt.Printf("Yes\n")
}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	result = append(result, v)

	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}


package main

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

func main() {
	stdin := bufio.NewScanner(os.Stdin)
	stdin.Scan()
	stdin.Scan()
	_, _ = strconv.Atoi(stdin.Text())
	arr := strings.Split(stdin.Text(), " ")
	old := 0
	now := 0
	b := true
	for i, v := range arr {
		now, _ = strconv.Atoi(v)
		switch b == true {
		case i == 0:
			old = now
		default:
			chk := old - now
			switch {
			case chk == 0:
				b = true
				old = now
			case chk <= -1:
				b = true
				now -= 1
				old = now
			case chk >= 1:
				b = false
			}
		}
	}
	if b == true {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}


package main

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

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	t, _ := strconv.Atoi(sc.Text())
	return t
}
func main() {
	var n int
	fmt.Scan(&n)
	h := make([]int, n)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		h[i] = nextInt(sc)
	}
	ok := true
	prev := -1
	for i := 0; i < n; i++ {
		if prev > h[i] {
			ok = false
			break
		} else if prev < h[i] {
			h[i]--
		}
		prev = h[i]
	}
	if ok {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}


package main

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

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	t, _ := strconv.Atoi(sc.Text())
	return t
}

func main() {
	var n int
	fmt.Scan(&n)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	h := make([]int, n)
	for i := 0; i < n; i++ {
		h[i] = nextInt(sc)
	}

    ok := true
	maxh := h[0]
	for i := 0; i < n-1; i++ {
		if h[i] > h[i+1] {
			// 今のマスの方が次のマスより背が高い
			if maxh >= h[i]-1 {
				h[i]--
				maxh = h[i]
			} else {
				ok = false
				break
			}
		} else {
			if h[i] > maxh {
				maxh = h[i]
			}
		}
	}
	if ok {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}


package main

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

var sc = bufio.NewScanner(os.Stdin)
var ReadString func() string
var ReadLine func() string
var ReadInt64 func() int64
var ReadInt func() int
var ReadIntSlice func(int) []int
var ReadInt64Slice func(int) []int64
var GetIntAbs func(int) int
var Atoi func(string) int
var FindMin func([] int) int
var FindMax func([] int) int
var StrReverse func(string) string

func init() {
	ReadString = newReadString()
	ReadLine = readLine
	ReadInt64 = readInt64
	ReadInt = readInt
	ReadIntSlice = readIntSlice
	FindMin = findMin
	FindMax = findMax
	StrReverse = strReverse
  GetIntAbs = getIntAbs
}

func main() {
	n := ReadInt()
  h := ReadIntSlice(n)
  if n <= 1 {
    fmt.Println("Yes")
    return
  }
  for i, cur := range h {
    if i + 1 == n {
      continue
    }
    next := h[i+1]
    nextDiff := GetIntAbs(cur - next)
    if i > 0 && (nextDiff <= 1 && GetIntAbs(cur - h[i - 1]) <= 1) {
      continue
    } else if i == 0 && nextDiff <= 1 {
      continue
    } else {
      fmt.Println("No")
      return
    }
  }
  fmt.Println("Yes")

}

func strReverse(s string) string {
	runes := []rune(s)
	l := len(runes)
	for i, j := 0, l-1; i < j; i, j = i+1, j-1 {
		runes[i], runes[j] = runes[j], runes[i]
	}
	return string(runes)
}

func newReadString() func() string {
	sc.Buffer(make([]byte, 1024), 2048)
	sc.Split(bufio.ScanWords)
	return func() string {
		if !sc.Scan() {
			panic("scanできなかった")
		}
		return sc.Text()
	}
}

func readLine() string {
	if !sc.Scan() {
		panic("line をscanできなかった")
	}
	return sc.Text()
}

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

func readInt() int {
	i := ReadInt64()
	return int(i)
}

func readIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt()
	}
	return arr
}

func readInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt64()
	}
	return arr
}

/*------ number util ------*/

func getIntAbs(n int) int {
	if n < 0 {
		return -n
	}
	return n
}

func atoi(a string) int {
	i, err := strconv.Atoi(a)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func findMin(arr []int) int {
	if len(arr) == 0 {
		return 0
	}
	min := arr[0]
	for _, v := range arr {
		if v < min {
			min = v
		}
	}
	return min
}

func findMax(arr []int) int {
	if len(arr) == 0 {
		return 0
	}
	max := arr[0]
	for _, v := range arr {
		if v > max {
			max = v
		}
	}
	return max
}


package main

import (
    "fmt"
)

func main(){
    var n,i uint64
    fmt.Scan(&n)
    h := make([]uint64, n)
    for i = 0; i < n; i++ {
        fmt.Scan(&h[i])
    }
    max := h[n - 1] + 1
    var flag bool = true
    for i = n - 1; i > 0; i-- {
        if h[i] > max {
            flag = false
        }
        max = h[i] + 1
    }
    if h[0] > max {
        flag = false
    }
    if(flag){
        fmt.Println("Yes")
    }else{
        fmt.Println("No")
    }
}


package main

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

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

	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	hArr := make([]int, N)
	for i := range hArr {
		sc.Scan()
		hArr[i], _ = strconv.Atoi(sc.Text())
	}

	enable := true
	for i := 1; i < len(hArr); i++ {
		if hArr[i] - hArr[i-1] > 1 && i != len(hArr) - 1{
			enable = false
			break
		}
	}

	if enable {
		fmt.Println("Yes")
	}else{
		fmt.Println("No")
	}

}


package main

import "fmt"

func inputSlice(inputNums int) []int {
	var ary []int
	for i := 0; i < inputNums; i++ {
		var in int
		fmt.Scan(&in)
		ary = append(ary, in)
	}
	return ary
}

func check(ary []int) string {
	for i := len(ary) - 1; i > 0; i-- {
		if ary[i]-1 == ary[i-1] {
			ary[i]--
		}
	}
	for i := 0; i < len(ary)-1; i++ {
		if ary[i] > ary[i+1] {
			return "No"
		}
	}
	return "Yes"
}

func main() {
	var n int
	fmt.Scan(&n)
	ary := inputSlice(n)
	fmt.Println(check(ary))
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

func init() {
	sc.Split(bufio.ScanWords)
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.ParseInt(sc.Text(), 10, 64)
	if e != nil {
		panic(e)
	}
	return int(i)
}

func nextInt64() int64 {
	sc.Scan()
	i, e := strconv.ParseInt(sc.Text(), 10, 64)
	if e != nil {
		panic(e)
	}
	return i
}

func main() {
	n := nextInt()
	yes := "Yes"
	no := "No"

	if n == 1 {
		fmt.Println(yes)
		return
	}

	nn := make([]int64, 0, n)
	for i := 0; i < n; i++ {
		nn = append(nn, nextInt64())
	}

	nn = append(nn, 1000000000)

	for i := 0; i < n; i++ {
		// if i+1 == n {
		// 	break
		// }
		if nn[i+1] == nn[i] {
			continue
		}
		if nn[i+1]-1 < nn[i] {
			// fmt.Printf("%#v %#v %#v\n", i, nn[i+1], nn[i])
			fmt.Println(no)
			return
		}

		if nn[i+1] > nn[i] {
			nn[i+1]--
		}
	}

	fmt.Println(yes)
}


package main

import "fmt"

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

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

	max := 0
	for i := 1; i < n; i++ {
		if h[i] > max {
			max = h[i]
		}
		if max-h[i] > 1 {
			fmt.Println("No")
			return
		}
	}
	fmt.Println("Yes")
}


package main

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

func main() {
	sc := NewScanner()
	N := sc.NextInt()
	A := sc.NextIntArray()

	mx := 0
	for i := 0; i < N-1; i++ {
		if A[i] > A[i+1] {
			A[i]--
		}
	}

	for i := 0; i < N; i++ {
		if mx <= A[i] {
			mx = A[i]
		} else {
			fmt.Println("No")
			return
		}
	}

	fmt.Println("Yes")

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextFloat() float64 {
	v, _ := strconv.ParseFloat(s.Next(), 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}


package main

import (
	"fmt"
)

func main() {
	var n int
	var l []int

	fmt.Scanf("%d", &n)
	l = make([]int, n)

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

	cnt := 0

	for i := 1; i < n; i++ {
		if l[i-1] > l[i] {
			cnt++
		}
	}

	if cnt <= 1 {
		fmt.Print("Yes")
	} else {
		fmt.Print("No")
	}
}


package main

import "fmt"

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

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

	c := 0
	for i := 1; i < n; i++ {
		if h[i-1] > h[i] {
			if h[i-1]-1 > h[i] {
				fmt.Println("No")
				return
			}
			c++
		}
		if c > 1 {
			fmt.Println("No")
			return
		}
	}
	fmt.Println("Yes")
}


package main

import (
    "fmt"
)

func main() {
    var n int
    fmt.Scan(&n)
    var ng bool
    var max int
    for i := 0; i < n; i++ {
        var h int
        fmt.Scan(&h)
        if h < max-1 {
            ng = true
        }
        if h > max {
            max = h
        }
    }
    if ng {
        fmt.Println("No")
    } else {
        fmt.Println("Yes")
    }
}


//go:generate echo "https://atcoder.jp/contests/abc133/tasks/abc133_c"
package main

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

var scan = newScanner(os.Stdin)

func solve(L, R int) int {
	if R-L >= 2019 {
		return 0
	}
	min := math.MaxInt64
	for i := L; i < imin(R+1, L+2019); i++ {
		for j := L; j < imin(R+1, L+2019); j++ {
			if i == j {
				continue
			}
			chmin(&min, (i%2019)*(j%2019)%2019)
		}
	}
	return min
}

func solve2(L, R int) int {
	min := math.MaxInt64
	for i := L; i <= R; i++ {
		for j := L; j <= R; j++ {
			if i == j {
				continue
			}
			chmin(&min, (i%2019)*(j%2019)%2019)
			if min == 0 {
				return min
			}
		}
	}
	return min
}

func main() {
	L, R := scan.Int(), scan.Int()
	fmt.Println(solve2(L, R))
}

type scanner struct {
	*bufio.Scanner
}

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

func (s *scanner) String() string {
	s.Scan()
	return s.Text()
}

func (s *scanner) Strings(l int) []string {
	if l == 0 {
		return []string{}
	}
	sl := make([]string, l, l)
	for i := range sl {
		sl[i] = s.String()
	}
	return sl
}

func (s *scanner) Int() int {
	n, _ := strconv.Atoi(s.String())
	return n
}

func (s *scanner) Ints(l int) []int {
	if l == 0 {
		return []int{}
	}
	sl := make([]int, l, l)
	for i := range sl {
		sl[i] = s.Int()
	}
	return sl
}

func (s *scanner) Float64() float64 {
	f, _ := strconv.ParseFloat(s.String(), 64)
	return f
}

func (s *scanner) Float64s(l int) []float64 {
	if l == 0 {
		return []float64{}
	}
	sl := make([]float64, l, l)
	for i := range sl {
		sl[i] = s.Float64()
	}
	return sl
}

func (s *scanner) BigInt() *big.Int {
	n, _ := strconv.ParseInt(s.String(), 10, 64)
	return big.NewInt(n)
}

func (s *scanner) BigInts(l int) []*big.Int {
	if l == 0 {
		return []*big.Int{}
	}
	sl := make([]*big.Int, l, l)
	for i := range sl {
		sl[i] = s.BigInt()
	}
	return sl
}

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

func imax(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func imin(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func ipow(x, n int) int {
	if n == 0 {
		return 1
	}
	a := ipow(x, n>>1)
	a *= a
	if n&1 != 0 {
		a *= x
	}
	return a
}

func isum(X []int) int {
	s := 0
	for _, x := range X {
		s += x
	}
	return s
}

func chmax(x *int, v int) {
	if *x < v {
		*x = v
	}
}

func chmin(x *int, v int) {
	if *x > v {
		*x = v
	}
}

func factor(n int) map[int]int {
	m := map[int]int{}
	for i := 2; i*i <= n; i++ {
		for n%i == 0 {
			n /= i
			m[i]++
		}
	}
	if n != 1 {
		m[n]++
	}
	return m
}

var mod = 1000000007

func setMintMod(x int) {
	mod = x
}

type mint int

func newMint(v int) mint {
	return mint((v%mod + mod) % mod)
}

func (x mint) Add(y mint) mint {
	v := x + y
	if v >= mint(mod) {
		return v - mint(mod)
	}
	return v
}

func (x mint) Sub(y mint) mint {
	v := x + mint(mod) - y
	if v >= mint(mod) {
		return v - mint(mod)
	}
	return v
}

func (x mint) Mul(y mint) mint {
	return (x * y) % mint(mod)
}

func (x mint) pow(n int) mint {
	if n == 0 {
		return mint(1)
	}
	a := x.pow(n >> 1)
	a = a.Mul(a)
	if n&1 != 0 {
		a = a.Mul(x)
	}
	return a
}

func (x mint) Inverse() mint {
	return x.pow(mod - 2)
}

func (x mint) Div(y mint) mint {
	return x * y.Inverse()
}

func (x mint) Neg() mint {
	return newMint(int(-x))
}

type factorial struct {
	fact        []mint
	factInverse []mint
}

func newFactorial(n int) *factorial {
	f := new(factorial)
	f.init(n)
	return f
}

func (f *factorial) init(n int) {
	fact := make([]mint, n+1, n+1)
	fact[0] = newMint(1)
	for i := 1; i <= n; i++ {
		fact[i] = fact[i-1].Mul(newMint(i))
	}

	inv := make([]mint, n+1, n+1)
	inv[n] = fact[n].Inverse()
	for i := n; i >= 1; i-- {
		inv[i-1] = inv[i].Mul(newMint(i))
	}

	f.fact = fact
	f.factInverse = inv
}

func (f *factorial) Get(n int) mint {
	return f.fact[n]
}

func (f *factorial) GetInverse(n int) mint {
	return f.factInverse[n]
}

func (f *factorial) Permutation(n, k int) mint {
	if k < 0 || n < k {
		return 0
	}
	return f.fact[n].Mul(f.factInverse[n-k])
}

func (f *factorial) Combination(n, k int) mint {
	if k < 0 || n < k {
		return 0
	}
	return f.fact[n].Mul(f.factInverse[k]).Mul(f.factInverse[n-k])
}

type sieve struct {
	n          int
	primes     []int
	maxFactors []int
}

func newSieve(n int) *sieve {
	s := &sieve{
		n:          n,
		primes:     []int{},
		maxFactors: make([]int, n+1, n+1),
	}
	s.init()
	return s
}

func (s *sieve) init() {
	s.maxFactors[0] = -1
	s.maxFactors[1] = -1
	for i := 2; i <= s.n; i++ {
		if s.maxFactors[i] != 0 {
			continue
		}
		s.primes = append(s.primes, i)
		s.maxFactors[i] = i
		for j := i * i; j <= s.n; j += i {
			if s.maxFactors[j] == 0 {
				s.maxFactors[j] = i
			}
		}
	}
}

func (s *sieve) IsPrime(x int) bool {
	return s.maxFactors[x] == x
}

func (s *sieve) Factor(x int) map[int]int {
	m := map[int]int{}
	for x != 1 {
		m[s.maxFactors[x]]++
		x /= s.maxFactors[x]
	}
	return m
}

Score : 300 points

Problem Statement

You are given two non-negative integers L and R.
We will choose two integers i and j such that L \leq i < j \leq R.
Find the minimum possible value of (i \times j) \mbox{ mod } 2019.

Constraints

All values in input are integers.

0 \leq L < R \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

L R

Output

Print the minimum possible value of (i \times j) \mbox{ mod } 2019 when i and j are chosen under the given condition.

Sample Input 1

2020 2040

Sample Output 1

2

When (i, j) = (2020, 2021), (i \times j) \mbox{ mod } 2019 = 2.

Sample Input 2

4 5

Sample Output 2

20

We have only one choice: (i, j) = (4, 5).


package main

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

func main() {
	L, R := ReadInt(), ReadInt()
	if R-L+1 >= 2019 {
		fmt.Println(1)
		return
	}
	if L%2019 < R%2019 {
		fmt.Println(L * (L + 1) % 2019)
	} else {
		fmt.Println(1)
	}
}

var reader = bufio.NewReader(os.Stdin)

func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(reader, a...); err != nil {
		panic(err)
	}
}
func ReadInt() (i int)       { Scan(&i); return }
func ReadString() (s string) { Scan(&s); return }
func ReadInts(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		Scan(&v[i])
	}
	return v
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

func init() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024*1024), bufio.MaxScanTokenSize)
}

func nextString() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func main() {
	L := nextInt()
	R := nextInt()
	MIN := 214748347
	for i := L; i < R; i++ {
		for j := i + 1; j <= R; j++ {
			MIN = min(MIN, (i*j)%2019)
		}
	}
	fmt.Println(MIN)
}

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


package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"sort"
	"strconv"
)

const (
	initialBufSize = 1e4
	maxBufSize     = 1e6 + 7
)

var buf []byte = make([]byte, maxBufSize)
var sc = bufio.NewScanner(os.Stdin)

func init() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(buf, maxBufSize)
}

func main() {
	L, R := nextInt()%2019, nextInt()%2019
	if R-L >= 2019 {
		fmt.Println(0)
		return
	}
	ans := 2019 + 1
	for i := L; i < R; i++ {
		for j := i + 1; j <= R; j++ {
			ans = MinOf(ans, (i*j)%2019)
		}
	}
	fmt.Println(ans)
}

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

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

func pow(p, q int) int {
	res := p
	if q == 0 {
		return 1
	}
	for i := 0; i < q-1; i++ {
		res *= p
	}
	return res
}

func MinOf(vars ...int) int {
	min := vars[0]
	for _, i := range vars {
		if min > i {
			min = i
		}
	}
	return min
}

func MaxOf(vars ...int) int {
	max := vars[0]
	for _, i := range vars {
		if max < i {
			max = i
		}
	}
	return max
}

func gcdof2numbers(a int, b int) int {
	if b == 0 {
		return a
	}
	return gcdof2numbers(b, a%b)
}

func lcmof2numbers(a int, b int) int {
	return a / gcdof2numbers(a, b) * b
}

// マイナスの場合は考慮していない
func fuctorial(a int) int {
	if a == 1 || a == 0 {
		return 1
	} else {
		return fuctorial(a-1) * a
	}
}

func NextPermutation(x sort.Interface) bool {
	n := x.Len() - 1
	if n < 1 {
		return false
	}
	j := n - 1
	for ; !x.Less(j, j+1); j-- {
		if j == 0 {
			return false
		}
	}
	l := n
	for !x.Less(j, l) {
		l--
	}
	x.Swap(j, l)
	for k, l := j+1, n; k < l; {
		x.Swap(k, l)
		k++
		l--
	}
	return true
}

// 素因数分解したmapを返す
func primeFuctorize(n int) map[int]int {
	pf := map[int]int{}
	for i := 2; i*i <= n; i++ {
		for n%i == 0 {
			pf[i]++
			n /= i
		}
	}
	if n != 1 {
		pf[n]++
	}
	return pf
}

// ここから拝借
// https://shoman.hatenablog.com/entry/2020/02/25/185456
// Stackは[]intのエイリアス
type Stack []int

// Push adds an element
func (s *Stack) Push(v int) {
	*s = append(*s, v)
}

// Pop removes the top element and return it
func (s *Stack) Pop() (int, error) {
	if s.Empty() {
		return 0, fmt.Errorf("stack is empty")
	}

	v := (*s)[len(*s)-1]
	*s = (*s)[:len(*s)-1]
	return v, nil
}

// Peek returns the top value
func (s *Stack) Peek() (int, error) {
	if s.Empty() {
		return 0, fmt.Errorf("stack is empty")
	}

	return (*s)[len(*s)-1], nil
}

// Size returns the length of stack
func (s *Stack) Size() int {
	return len(*s)
}

// Empty returns true when stack is empty
func (s *Stack) Empty() bool {
	return s.Size() == 0
}

// NewStack generates stack
func NewStack() *Stack {
	s := new(Stack)
	return s
}

// golangの公式サンプルより
// https://xn--go-hh0g6u.com/pkg/container/heap/#example__intHeap
// IntHeap は，整数の最小ヒープです。
type IH []int

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

func (h *IH) Push(x interface{}) {
	// Push と Pop はポインタレシーバを使っています。
	// なぜなら，スライスの中身だけでなく，スライスの長さも変更するからです。
	*h = append(*h, x.(int))
}

func (h *IH) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

// golangの公式サンプルより
// https://xn--go-hh0g6u.com/pkg/container/heap/#example__priorityQueue

// Item は，優先キューで管理する項目です。
type Item struct {
	value    string // 値。任意です。
	priority int    // キューにおける優先度
	// index は heap.Interface メソッドで更新されます。
	index int // ヒープにおけるインデックス
}

// PriorityQueue は heap.Interface を実装し，Item のリストを保持します。
type PriorityQueue []*Item

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

func (pq PriorityQueue) Less(i, j int) bool {
	// Pop が最小ではなく最大の優先度を持つ項目を返して欲しいので，ここでは > を使っています。
	return pq[i].priority > pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // メモリリークを避ける
	item.index = -1 // 安全のため
	*pq = old[0 : n-1]
	return item
}

// update はキューの Item の優先度と値を更新します。
func (pq *PriorityQueue) update(item *Item, value string, priority int) {
	item.value = value
	item.priority = priority
	heap.Fix(pq, item.index)
}


package main

import "fmt"

var moduler = 2019

func main() {
	var l, r int
	fmt.Scan(&l, &r)
	if r-l >= 2019 {
		fmt.Println(0)
		return
	}
	ans := l * (l + 1) % moduler
	for i := l; i < r; i++ {
		for j := l + 1; j <= r; j++ {
			mod := i * j % moduler
			if mod < ans {
				ans = mod
			}
		}
	}
	fmt.Println(ans)
}


package main

import (
	"fmt"
	"math"
)

func intMin(x, y int) int {
	return int(math.Min(float64(x), float64(y)))
}

func main() {
	var L, R int
	fmt.Scan(&L, &R)

	if (R - L) >= 2019 {
		fmt.Println(0)
		return
	}

	L, R = L%2019, R%2019
	if L > R {
		fmt.Println(0)
		return
	}

	ans := 2019
	for i := L; i < R; i++ {
		for j := i + 1; j <= R; j++ {
			ans = intMin(ans, (i*j)%2019)
		}
	}
	fmt.Println(ans)
}


// 参考: https://atcoder.jp/contests/abc133/submissions/6278434
// matsuu さんありがとうございました。

package main

import (
	"fmt"
)

func minimumRemainder(minNum int, maxNum int, modNumInit int) int {
	// 各余り値の最小値
	// (最初なので考えられる余り値の最大値)
	var modNum int = modNumInit

	// minNum(i)がmaxNumに徐々に接近！
	for i := minNum; i < maxNum; i++ {
		// i+1(j)がmaxNumに徐々に接近してくっつく！
		for j := i + 1; j <= maxNum; j++ {
			// 現在のminNum(i)×(i+1...maxNum) ÷ 2019 の余り値
			remainder := (i * j) % modNumInit
			// 現在の最小値より小さかったら
			if remainder < modNum {
				// 最小値更新
				modNum = remainder
				// 余りが0になった時点で最小値が決定したので処理終了
				if modNum == 0 {
					return modNum
				}
			}
		}
	}
	return modNum
}

func main() {
	var minNum, maxNum int
	fmt.Scan(&minNum, &maxNum)
	fmt.Println(minimumRemainder(minNum, maxNum, 2019))
}


package main

import "fmt"

func main() {
	var l, r int
	fmt.Scan(&l, &r)

	var min int
	ans := 1000000000
	for i := l; i < r; i++ {
		for j := i + 1; j <= r; j++ {
			min = i * j % 2019
			if min < ans {
				ans = min
			}
		}
	}

	fmt.Println(ans)
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

func main() {
	sc.Split(bufio.ScanWords)
	log.SetFlags(log.Lshortfile)
	l := nextInt()
	r := nextInt()
	ans := math.MaxInt32
	for i := l; i <= r; i++ {
		for j := i + 1; j <= r; j++ {
			ans = min(ans, (i*j)%2019)
			if ans == 0 {
				fmt.Println(ans)
				return
			}
		}
	}
	fmt.Println(ans)
}

func nextString() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

var dy = []int{0, 1, 0, -1}
var dx = []int{1, 0, -1, 0}
var MAX = math.MaxInt32

func pow(a, b int) int {
	return int(math.Pow(float64(a), float64(b)))
}

func lcm(a, b int) int {
	return a * b / gcd(a, b)
}
func gcd(a, b int) int {
	if a < b {
		a, b = b, a
	}
	for b != 0 {
		a, b = b, a%b
	}
	return a
}
func max(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r < a[i] {
			r = a[i]
		}
	}
	return r
}
func min(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r > a[i] {
			r = a[i]
		}
	}
	return r
}
func sum(a []int) (r int) {
	for i := range a {
		r += a[i]
	}
	return r
}
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

type Pair struct {
	a, b int
}

type Pairs []Pair

func (p Pairs) Len() int {
	return len(p)
}
func (p Pairs) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func (p Pairs) Less(i, j int) bool {
	if p[i].a == p[j].a {
		return p[i].b < p[j].b
	}
	return p[i].a < p[j].a
}


package main

import "fmt"

func main() {
	var l, r int32

	fmt.Scan(&l)
	fmt.Scan(&r)

	if r >= l + 2019 {
		r = l + 2019
	}

	answer := int64(2018)
	for i := l; i < r; i++ {
		for j := i + 1; j <= r; j++ {
			rem := (int64(i) * int64(j)) % 2019
			if rem < answer {
				answer = rem
			}
		}
	}
	fmt.Println(answer)
}


package main

import "fmt"

func main() {
	var l, r int
	fmt.Scan(&l, &r)
	//2019個以上ならおけ
	w := r-l+1
	if w%2019 == 0{
		fmt.Println(0)
		return
	}
	ans := 1000000000
	cnt := 1000000000
	for i:=l; i<=r ; i++ {
		//IからJの中で、2019で割り切れるるものは最小値０
		if i%2019 == 0 {
			fmt.Println(0)
			return
		}
		//範囲の中にある2019に一番近い2つの数字が答え
		cnt = i%2019
		if ans > cnt {
			ans = cnt
		}
	}
	fmt.Println(ans*(ans+1))
}


package main

import(
    "fmt"
)

func main() {
    var l, r int
    fmt.Scan(&l, &r)

    if r - l >= 2019 {
        fmt.Println(0)
        return
    }

    var min int
    min = 2020
    for i:= l;i <= r;i++ {
        for j:= l+1;j <= r; j++ {
            if min > (i*j)%2019 {
                min = (i*j)%2019
            }
        }
    }

    fmt.Println(min)
}


package main

import(
    "fmt"
    "math"
)

func main() {
    var l, r int
    fmt.Scan(&l, &r)
    var min, second int
    var num int
    min = 2020
    second = 2020
    for i:=l;i <=r;i++ {
        num = int(math.Abs(float64(i%2019)))
        if min > num{
            second = min
            min = i%2019
        } else if second > num && min < num{
            second = num
        }
    }
    fmt.Println(min*second)
}


package main

import (
	"fmt"
	"os"
)

func main() {
	var a, b int
	fmt.Scan(&a, &b)
	mod := 2019

	if a*b < mod {
		fmt.Println(a * b)
		os.Exit(0)
	} else if a*b%mod == 0 {
		fmt.Println(0)
		os.Exit(0)
	}

	res := 2019
	for i := a; i <= b-1; i++ {
		for j := i + 1; j <= b; j++ {
			if m := i * j % mod; m < res {
				res = m
			}
			if res == 0 {
				break
			}
		}
		if res == 0 {
			break
		}
	}
	fmt.Println(res)
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func searchMin(l int, r int) int {
	min := 2019
	for i := l; i < r; i++ {
		for j := i + 1; j <= r; j++ {
			mod := (i * j) % 2019
			if mod < min {
				min = mod
			}
			if min == 0 {
				return 0
			}
		}
	}
	return min
}

func main() {
	sc.Split(bufio.ScanWords)
	l := nextInt()
	r := nextInt()

	// min := searchMin(l, r)
	// fmt.Println(min)

	if r > l+2019 {
		r = l + 2019
	}
	min := 2019
	for i := l; i < r; i++ {
		for j := i + 1; j <= r; j++ {
			mod := (i * j) % 2019
			if mod < min {
				min = mod
			}
		}
	}
	fmt.Println(min)
}


package main

import (
	"fmt"
	"math"
)

func main() {
	input := scanNums(2)
	L := int64(input[0])
	R := int64(input[1])

	var minNum int64 = math.MaxInt64
	for i := L; i <= R; i++ {
		for j := i+1; j <= R; j++ {
			minNum = minInt64(minNum, (i*j)%2019)
			if minNum == 0 {
				fmt.Print(minNum)
				return
			}
		}
	}
	fmt.Print(minNum)
	return
}

func minInt64(x, y int64) int64 {
	if x < y {
		return x
	}
	return y
}
func scanNums(len int) (nums []int) {
	var num int
	for i := 0; i < len; i++ {
		fmt.Scan(&num)
		nums = append(nums, num)
	}
	return
}


package main

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

func main() {
  sc := bufio.NewScanner(os.Stdin)
  sc.Split(bufio.ScanWords)

  sc.Scan()
  l, _ := strconv.Atoi(sc.Text())
  sc.Scan()
  r, _ := strconv.Atoi(sc.Text())

  min := math.MaxInt32
  for i := l; i < r; i++ {
      for j := l + 1; j <= r; j++ {
      tmp := ((i % 2019) * (j % 2019)) % 2019
      if tmp < min {
        min = tmp
      }
    }
    if min == 0 {
      break
    }
  }
  fmt.Println(min)
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

func init() {
	sc.Split(bufio.ScanWords)
}

const div = 2019

func nextInt() int64 {
	sc.Scan()
	i, e := strconv.ParseInt(sc.Text(), 10, 64)
	if e != nil {
		panic(e)
	}
	return i
}

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func main() {
	l, r := nextInt(), nextInt()

	if r-l >= 2019 {
		fmt.Println(0)
		return
	}

	re := int64(9999999999)
	for i := l; i <= r; i++ {
		for j := i; j <= r; j++ {
			if i == j {
				continue
			}
			v := i * j % div
			if re > v {
				re = v
			}
			if re == 0 {
				fmt.Println(re)
				return
			}
		}
	}

	fmt.Println(re)
}


package main

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

const pi = math.Pi

var A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z int
var mod int = pow(10, 9) + 7
var Umod uint64 = 2019
var ans int = 1e9

func main() {
	var L, R int
	fmt.Scan(&L, &R)

	// i*j = k...p (mod 2019)
	// (i*j+p) = k*2019
	if 2019 <= R-L {
		fmt.Println(0)
	} else {
		for i := L; i <= R-1; i++ {
			for j := L + 1; j <= R; j++ {
				cnt := i * j % 2019
				ans = min(ans, cnt)
			}
		}
		fmt.Println(ans)
	}
}

/*  ----------------------------------------  */

var reader = bufio.NewScanner(os.Stdin)

func read() string {
	reader.Scan()
	return reader.Text()
}

func lcm(x, y int) int {
	return (x / gcd(x, y)) * y
}

func gcd(x, y int) int {
	if x%y == 0 {
		return y
	} else {
		r := x % y
		return gcd(y, r)
	}
}

var fac [1000000]int
var finv [1000000]int
var inv [1000000]int

func combination_init() {
	fac[0], fac[1] = 1, 1
	finv[0], finv[1] = 1, 1
	inv[1] = 1

	// invは a^(-1) mod p
	// pをaで割ることを考える

	// p/a*(a) + p%a = p
	// p/a*(a) + p%a = 0          (mod p)
	// -p%a = p/a*(a)             (mod p)
	// -p%a *a^(-1)= p/a          (mod p)
	// a^(-1)= p/a * (-p%a)^(-1)  (mod p)
	// a^(-1) =

	for i := 2; i < 1000000; i++ {
		fac[i] = fac[i-1] * i % mod
		inv[i] = mod - inv[mod%i]*(mod/i)%mod
		finv[i] = finv[i-1] * inv[i] % mod
	}
}

func combination(x, y int) int {
	if x < y {
		return 0
	}
	if fac[0] != 1 {
		combination_init()
	}
	return fac[x] * (finv[y] * finv[x-y] % mod) % mod
	//return fac[x] / (fac[y] * fac[x-y])
}

func permutation(x, y int) int {
	if x < y {
		return 0
	}
	if fac[0] != 1 {
		combination_init()
	}
	return fac[x] * (finv[x-y] % mod) % mod
	//return fac[x] / fac[x-y]
}

func max(x ...int) int {
	var res int = x[0]
	for i := 1; i < len(x); i++ {
		res = int(math.Max(float64(x[i]), float64(res)))
	}
	return res
}

func min(x ...int) int {
	var res int = x[0]
	for i := 1; i < len(x); i++ {
		res = int(math.Min(float64(x[i]), float64(res)))
	}
	return res
}
func pow(x, y int) int { return int(math.Pow(float64(x), float64(y))) }
func abs(x int) int    { return int(math.Abs(float64(x))) }
func floor(x int) int  { return int(math.Floor(float64(x))) }
func ceil(x int) int   { return int(math.Ceil(float64(x))) }

type SortBy []int

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

type PriorityQueue []int

func (h PriorityQueue) Len() int            { return len(h) }
func (h PriorityQueue) Less(i, j int) bool  { return h[i] < h[j] }
func (h PriorityQueue) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *PriorityQueue) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *PriorityQueue) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}


package main

import "fmt"

var moduler = 2019

func main() {
	var l, r int
	fmt.Scan(&l, &r)
	ans := l * (l + 1) % moduler
	for i := l; i < r; i++ {
		for j := l + 1; j <= r; j++ {
			mod := i * j % moduler
			if mod < ans {
				ans = mod
			}
		}
	}
	fmt.Println(ans)
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

func main() {
	sc.Scan()
	numString := strings.Split(sc.Text(), " ")

	l, _ := strconv.Atoi(numString[0])
	r, _ := strconv.Atoi(numString[1])

	var flg bool
	var rem, min int
	var count int
	for i := l; i < r; i++ {
		if i%2019 == 0 {
			min = 0
			break
		}
		for j := i + 1; j <= r; j++ {
			if flg == false {
				min = i * j
				flg = true
			}
			rem = (i * j) % 2019
			if rem < min {
				min = rem
			}
			count++
			if count > 2019 {
				count = 0
				break
			}
		}
	}
	fmt.Println(min)
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func main() {
	lr := strings.Fields(nextLine())
	l, _ := strconv.Atoi(lr[0])
	r, _ := strconv.Atoi(lr[1])
	nums := []int{}
	if l < r+2019 {
		for i := l; i <= r; i++ {
			nums = append(nums, i%2019)
		}
	} else {
		for i := l; i <= l+2019; i++ {
			nums = append(nums, i%2019)
		}
	}
	sort.Ints(nums)
	fmt.Println(nums[0] * nums[1])
}


package main

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

var sc = bufio.NewScanner(os.Stdin)
var ReadString func() string

func init() {
	ReadString = newReadString()
}

/*------ scan ------*/

func newReadString() func() string {
	sc.Buffer(make([]byte, 1024), 2048)
	sc.Split(bufio.ScanWords)
	return func() string {
		if !sc.Scan() {
			panic("scanできなかった")
		}
		return sc.Text()
	}
}

func ReadLine() string {
	if !sc.Scan() {
		panic("line をscanできなかった")
	}
	return sc.Text()
}

func ReadInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		fmt.Println("parseInt失敗")
		panic(err.Error())
	}
	return int64(i)
}

func ReadInt() int {
	i := ReadInt64()
	return int(i)
}

func ReadIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt()
	}
	return arr
}

func ReadInt64Slice(n int) []int64 {
	arr := make([]int64, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt64()
	}
	return arr
}

/*------ number util ------*/

func GetIntAbs(n int) int {
	if n < 0 {
		return -n
	}
	return n
}

func Atoi(a string) int {
	i, err := strconv.Atoi(a)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func FindMin(arr []int) int {
	if len(arr) == 0 {
		return 0
	}
	min := arr[0]
	for _, v := range arr {
		if v < min {
			min = v
		}
	}
	return min
}

func FindMax(arr []int) int {
	if len(arr) == 0 {
		return 0
	}
	max := arr[0]
	for _, v := range arr {
		if v > max {
			max = v
		}
	}
	return max
}

func main() {
	L := ReadInt()
	R := ReadInt()
	if L * R < 2019 {
		fmt.Println(L * R)
	} else {

		max := L
      min := R
		if R > max {
			max = R
          min = L
		}
		minRs := 1000000
		for j := max; j >= min + 1; j-- {
			for i := j - 1; i >= min; i-- {
             	mod := (i * j) % 2019
				if mod < minRs {
					minRs = mod
				}
			}
		}
		fmt.Println(minRs)
	}
}


package main

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

var scanner = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	i, _ := strconv.Atoi(next())
	return i
}

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

	l := nextInt()
	r := nextInt()

	if r-l >= 2018 {
		fmt.Println(0)
	} else {
		for i := l; i <= r; i++ {
			if i%2019 == 0 {
				fmt.Println(0)
				return
			}
		}
		fmt.Println((l * (l + 1)) % 2019)
	}
}


package main

import "fmt"

func main() {
	var l, r int
	fmt.Scan(&l, &r)

	a := l % 2019
	b := r % 2019

	if a >= b || r-l > 2019 {
		fmt.Println(0)
		return
	}
	if a < b {
		min := 10000
		for i := a; i <= b-1; i++ {
			for j := i + 1; j <= b; j++ {
				t := (i * j) % 2019
				if t < min {
					min = t
				}
			}
		}
		fmt.Println(min)
		return
	}
}


package main

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

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

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

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

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

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var l, r int

func main() {
	l, r = ReadInt(), ReadInt()

	ans := INF_INT64
	// flag := false
	counter := 0
	for i := l; i <= r; i++ {
		for j := i + 1; j <= r; j++ {
			if counter == 1000000 {
				break
			}

			counter++
			if ans > (i*j)%2019 {
				ans = (i * j) % 2019
				// flag = true
			}
		}
	}

	fmt.Println(ans)
}

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

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

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

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

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+11))
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

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

// ReadInt returns an integer.
func ReadInt() int {
	return int(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
}

// 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())
}

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

// 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
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// DigitSum returns digit sum of a decimal number.
// DigitSum only accept a positive integer.
func DigitSum(n int) int {
	if n < 0 {
		return -1
	}

	res := 0

	for n > 0 {
		res += n % 10
		n /= 10
	}

	return res
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// CeilInt returns the minimum integer larger than or equal to float(a/b).
func CeilInt(a, b int) int {
	res := a / b
	if a%b > 0 {
		res++
	}
	return res
}

// FloorInt returns the maximum integer smaller than or equal to float(a/b)
func FloorInt(a, b int) int {
	res := a / b
	return res
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

// Strtoi is a wrapper of `strconv.Atoi()`.
// If `strconv.Atoi()` returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// 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)
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

// 一行読み出し
func nextLine() string {
	sc.Scan()
	return sc.Text()
}

// 文字列リバース関数
func strReverse(s string) string {
	rs := []rune(s)
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		rs[i], rs[j] = rs[j], rs[i]
	}
	return string(rs)
}

// 引数個の数値を読み込む
func scanNums(len int) (nums []int) {
	var num int
	for i := 0; i < len; i++ {
		fmt.Scan(&num)
		nums = append(nums, num)
	}
	return
}

func main() {
	var l, r int
	fmt.Scan(&l, &r)

	for l >= 2019 {
		l = l - 2019
		r = r - 2019
	}

	ans := l * (l + 1)

	fmt.Println(ans)
}


package main

import (
	"bytes"
	"fmt"
)

func main() {
	var s []byte
	fmt.Scan(&s)

	s = bytes.Replace(s, []byte("BC"), []byte("D"), -1)
	ans := 0
	ctA := 0
	for i := 0; i < len(s); i++ {
		switch s[i] {
		case 'A':
			ctA++
		case 'D':
			ans += ctA
		default:
			ctA = 0
		}
	}

	fmt.Println(ans)
}

Score : 600 points

Problem Statement

You are given a string s consisting of A, B and C.

Snuke wants to perform the following operation on s as many times as possible:

Choose a contiguous substring of s that reads ABC and replace it with BCA.

Find the maximum possible number of operations.

Constraints

1 \leq |s| \leq 200000

Each character of s is A, B and C.

Input

Input is given from Standard Input in the following format:

s

Output

Find the maximum possible number of operations.

Sample Input 1

ABCABC

Sample Output 1

3

You can perform the operations three times as follows: ABCABC → BCAABC → BCABCA → BCBCAA. This is the maximum result.

Sample Input 2

C

Sample Output 2

0

Sample Input 3

ABCACCBABCBCAABCB

Sample Output 3

6


package main

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

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+11))
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Bytes()
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

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

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

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

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

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

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

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

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

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

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

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

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

var s []byte
var pos int
var a int
var ans int

type state func() state

func start() state {
	p := pos
	pos++
	switch s[p] {
	case 'A':
		a++
		return readA
	default:
		return start
	}
}

func readA() state {
	p := pos
	pos++
	switch s[p] {
	case 'A':
		a++
		return readA
	case 'B':
		return readAB
	case 'C':
		a = 0
		return start
	}
	return nil
}

func readAB() state {
	p := pos
	pos++
	switch s[p] {
	case 'A':
		a = 1
		return readA
	case 'B':
		a = 0
		return start
	case 'C':
		ans += a
		return readA
	}
	return nil
}

func main() {
	defer stdout.Flush()
	s = readBytes()

	for state := start; pos < len(s) && state != nil; state = state() {
	}
	println(ans)
}

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


package main

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

var sc = bufio.NewScanner(os.Stdin)

func oneInt() int {
	var a int
	fmt.Scan(&a)
	return a
}
func oneStr() string {
	var a string
	fmt.Scan(&a)
	return a
}

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func scanNums() (nums []int) {
	s := nextLine()
	numStr := strings.Split(s, " ")

	for _, n := range numStr {
		i, _ := strconv.Atoi(n)
		nums = append(nums, i)
	}
	return nums
}

func scanStrings() (strs []string) {
	s := nextLine()
	list := strings.Split(s, " ")
	strs = append(strs, list...)
	return strs
}

func abs(a int) int {
	return int(math.Abs(float64(a)))
}

func pow(p, q int) int {
	return int(math.Pow(float64(p), float64(q)))
}

func min(nums ...int) int {
	if len(nums) == 0 {
		panic("funciton min() requires at least one argument.")
	}
	res := nums[0]
	for i := 0; i < len(nums); i++ {
		res = int(math.Min(float64(res), float64(nums[i])))
	}
	return res
}

func max(nums ...int) int {
	if len(nums) == 0 {
		panic("funciton max() requires at least one argument.")
	}
	res := nums[0]
	for i := 0; i < len(nums); i++ {
		res = int(math.Max(float64(res), float64(nums[i])))
	}
	return res
}

func strSearch(a []string, b string) bool {
	for i := 0; i < len(a); i++ {
		if a[i] == b {
			return true
		}
	}
	return false
}

func sortAsc(a []int) []int {
	sort.Ints(a)
	return a
}

func sortDesc(a []int) []int {
	sort.Sort(sort.Reverse(sort.IntSlice(a)))
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func lcm(a, b int) int {
	if a == 0 && b == 0 {
		return 0
	}
	return a * b / gcd(a, b)
}

func main() {
	S := oneStr()
	var count int

	for i := 0; i < len(S); i += 1 {
		kk := strings.Index(S[i:], "ABC")

		if kk-1 > 0 {
			for index := kk - 1; S[index] == 'A'; index-- {
				count++
			}
		}

		if kk+4 <= len(S) {
			for index := kk + 2; S[index+1] == 'B' && S[index+2] == 'C'; index += 2 {
				count++
			}
		}
	}

	fmt.Print(count)

}


package main

import (
	"fmt"
)

func main() {
	var s string
	fmt.Scan(&s)

	var i int
	var ret int
	var aSeq int
	for i < len(s)-2 {
		if s[i:i+3] == "ABC" {
			ret += aSeq + 1
			s = "A" + s[i+3:]
			i = 0
		} else {
			if s[i] == 'A' {
				aSeq++
			} else {
				aSeq = 0
			}
			i++
		}
	}
	fmt.Println(ret)
}


package main

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

var sc = bufio.NewScanner((os.Stdin))

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	l := nextLine()
	i, e := strconv.Atoi(l)
	if e != nil {
		panic(e)
	}
	return i
}

// Point ...
type Point struct {
	x int
	y int
}

func main() {
	sc.Split(bufio.ScanWords)
	H, W, A := nextInt(), nextInt(), []string{}
	for i := 0; i < H; i++ {
		A = append(A, nextLine())
	}

	fmt.Printf("%d\n", AGC033A(H, W, A))
}

// AGC033A ...
func AGC033A(H, W int, A []string) (res int) {
	board, blackcnt := make([][]byte, H), 0
	for i := 0; i < H; i++ {
		board[i] = make([]byte, W)
		for j, char := range A[i] {
			board[i][j] = byte(char)
			if char == '#' {
				blackcnt++
			}
		}
	}

	for blackcnt != H*W {
		visited := make([][]bool, H)
		for i := 0; i < H; i++ {
			visited[i] = make([]bool, W)
		}

		for i := 0; i < H; i++ {
			for j := 0; j < W; j++ {
				if board[j][i] == '#' && !visited[j][i] {
					changed := 0
					changed, board, visited = dfsAGC033A(i, j, H, W, changed, false, board, visited)
					blackcnt += changed
				}
			}
		}

		res++
	}

	return
}

func dfsAGC033A(x, y, H, W, changed int, lastblack bool, board [][]byte, visited [][]bool) (int, [][]byte, [][]bool) {
	if x < 0 || y < 0 || W <= x || H <= y {
		return changed, board, visited
	}
	if visited[y][x] {
		return changed, board, visited
	}

	visited[y][x] = true

	if board[y][x] == '.' {
		if lastblack {
			board[y][x] = '#'
			changed++
		}
		return changed, board, visited
	}

	moves := []Point{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}
	for _, mv := range moves {
		next := Point{x + mv.x, y + mv.y}

		lastblack = board[y][x] == '#'
		changed, board, visited = dfsAGC033A(next.x, next.y, H, W, changed, lastblack, board, visited)
	}

	return changed, board, visited
}

Score : 300 points

Problem Statement

You are given a grid of squares with H horizontal rows and W vertical columns, where each square is painted white or black.
HW characters from A_{11} to A_{HW} represent the colors of the squares.
A_{ij} is # if the square at the i-th row from the top and the j-th column from the left is black, and A_{ij} is . if that square is white.

We will repeatedly perform the following operation until all the squares are black:

Every white square that shares a side with a black square, becomes black.

Find the number of operations that will be performed.
The initial grid has at least one black square.

Constraints

1 \leq H,W \leq 1000

A_{ij} is # or ..

The given grid has at least one black square.

Input

Input is given from Standard Input in the following format:

H W
A_{11}A_{12}...A_{1W}
:
A_{H1}A_{H2}...A_{HW}

Output

Print the number of operations that will be performed.

Sample Input 1

3 3
...
.#.
...

Sample Output 1

2

After one operation, all but the corners of the grid will be black. After one more operation, all the squares will be black.

Sample Input 2

6 6
..#..#
......
#..#..
......
.#....
....#.

Sample Output 2

3


package main

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

func out(x ...interface{}) {
	fmt.Println(x...)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getString() string {
	sc.Scan()
	return sc.Text()
}

// min, max, asub, absなど基本関数
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
}

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

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

const inf = 10000

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 10000)

	H, W := getInt(), getInt()
	dist := make([][]int, H)
	a := make([]string, H)
	for i := 0; i < H; i++ {
		a[i] = getString()
		dist[i] = make([]int, W)
	}

	for y := 0; y < H; y++ {
		t := inf
		for x := 0; x < W; x++ {
			if a[y][x] == '#' {
				t = 0
			}
			dist[y][x] = t
			t++
		}
		t = dist[y][W-1]
		for x := W - 1; x >= 0; x-- {
			if a[y][x] == '#' {
				t = 0
			}
			dist[y][x] = min(dist[y][x], t)
			t++
		}
	}

	// for y := 0; y < H; y++ {
	// 	out(dist[y])
	// }
	// out("----")

	for x := 0; x < W; x++ {
		t := dist[0][x]
		for y := 0; y < H; y++ {
			if a[y][x] == '#' {
				t = 0
			}
			t = min(dist[y][x], t)
			dist[y][x] = t
			t++
		}
		t = dist[H-1][x]
		for y := H - 1; y >= 0; y-- {
			if a[y][x] == '#' {
				t = 0
			}
			t = min(dist[y][x], t)
			dist[y][x] = t
			t++
		}
	}

	// for y := 0; y < H; y++ {
	// 	out(dist[y])
	// }
	ans := 0
	for y := 0; y < H; y++ {
		for x := 0; x < W; x++ {
			ans = max(ans, dist[y][x])
		}
	}
	out(ans)
}


package main

import "fmt"
import "bufio"
import "os"
import "strconv"
import "sort"

var _sw = bufio.NewScanner(os.Stdin)

type Pt struct {
	x int
	y int
}

func main() {
	var H, W int
	fmt.Scan(&H, &W)

	C := make([][]bool, H)
	B := []Pt{}
	for y := range C {
		var s string
		fmt.Scan(&s)

		C[y] = make([]bool, W)
		for x := range C[y] {
			C[y][x] = s[x] == '#'
			if C[y][x] {
				B = append(B, Pt{x, y})
			}
		}
	}

	cnt := 0
	done := len(B)
	next := append([]Pt{}, B...)
	for {
		if done == H*W {
			break
		}
		next_ := []Pt{}
		for _, b := range next {
			for _, d := range []Pt{{-1, 0}, {+1, 0}, {0, -1}, {0, +1}} {
				p := Pt{Bounded(0, b.x+d.x, W-1), Bounded(0, b.y+d.y, H-1)}
				if C[p.y][p.x] {
					continue
				}

				C[p.y][p.x] = true
				next_ = append(next_, p)
				done++
			}
		}
		next = next_
		cnt++
	}

	fmt.Println(cnt)
}

func init() {
	_sw.Split(bufio.ScanWords)
}

func NextInt() int {
	if _sw.Scan() == false {
		panic(_sw.Err())
	}
	i, e := strconv.Atoi(_sw.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func NextString() string {
	if _sw.Scan() == false {
		panic(_sw.Err())
	}
	return _sw.Text()
}

func Max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func Min(x, y int) int {
	if x > y {
		return y
	}
	return x
}

func Bounded(lower, n, upper int) int {
	return Min(upper, Max(lower, n))
}

func SortInts(xs []int) {
	sort.Sort(sort.IntSlice(xs))
}

func ReverseSortInts(xs []int) {
	sort.Sort(sort.Reverse(sort.IntSlice(xs)))
}


package main

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

func main() {
	H := nextInt()
	W := nextInt()
	A := make([][]byte, H)
	for i := 0; i < H; i++ {
		A[i] = nextBytes()
		if len(A[i]) != W {
			panic("W")
		}
	}
	q := make([]Pair, 0)
	next := make([]Pair, 0)
	for i := 0; i < H; i++ {
		for j := 0; j < W; j++ {
			if A[i][j] == '#' {
				next = append(next, Pair{i, j})
			}
		}
	}
	ans := 0
	for len(next) != 0 {
		q = make([]Pair, 0)
		for _, n := range next {
			q = append(q, n)
		}
		next = make([]Pair, 0)
		for len(q) != 0 {
			p := q[len(q)-1]
			q = q[:len(q)-1]
			for d := 0; d < 4; d++ {
				ny := p.a + dy[d]
				nx := p.b + dx[d]
				if ny < 0 || ny >= H || nx < 0 || nx >= W {
					continue
				}
				if A[ny][nx] == '.' {
					A[ny][nx] = '#'
					next = append(next, Pair{ny, nx})
				}
			}
		}
		ans++
		// log.Println(next)
		// for i := 0; i < H; i++ {
		// 	log.Println(string(A[i]))
		// }
	}
	fmt.Println(ans - 1)
}

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

func pow(a, b int) int {
	return int(math.Pow(float64(a), float64(b)))
}

func lcm(a, b int) int {
	return a * b / gcd(a, b)
}
func gcd(a, b int) int {
	if a < b {
		a, b = b, a
	}
	for b != 0 {
		a, b = b, a%b
	}
	return a
}
func max(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r < a[i] {
			r = a[i]
		}
	}
	return r
}
func min(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r > a[i] {
			r = a[i]
		}
	}
	return r
}
func sum(a []int) (r int) {
	for i := range a {
		r += a[i]
	}
	return r
}
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

type Pair struct {
	a, b int
}

type pairs []Pair

func (p pairs) Len() int {
	return len(p)
}
func (p pairs) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func (p pairs) Less(i, j int) bool {
	if p[i].a == p[j].a {
		return p[i].b < p[j].b
	}
	return p[i].a < p[j].a
}

var nextReader func() []byte

func init() {
	nextReader = newScanner()
	log.SetFlags(log.Lshortfile)
}

func newScanner() func() []byte {
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	return func() []byte {
		r.Scan()
		return r.Bytes()
	}
}
func nextBytes() []byte {
	return nextReader()
}

func nextString() string {
	return string(nextReader())
}
func nextInt64() int64 {
	v, _ := strconv.ParseInt(string(nextReader()), 10, 64)
	return v
}
func nextInt() int {
	v, _ := strconv.Atoi(string(nextReader()))
	return v
}
func nextInts(n int) []int {
	r := make([]int, n)
	for i := 0; i < n; i++ {
		r[i] = nextInt()
	}
	return r
}
func nextFloat64() float64 {
	f, _ := strconv.ParseFloat(string(nextReader()), 64)
	return f
}


package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)

	writer := bufio.NewWriter(os.Stdout)

	r := getNextInt(scanner)
	c := getNextInt(scanner)
	n := r * c
	maze := make([]byte, n)
	steps := make([]int, n)
	queue := make([][2]int, n)
	ql := 0
	qr := 0
	for y := 0; y < r; y++ {
		s := getNextString(scanner)
		for x := 0; x < c; x++ {
			i := y*c + x
			maze[i] = s[x]
			if s[x] == '.' {
				continue
			}
			steps[i] = 1
			queue[qr][0] = i
			queue[qr][1] = 0
			qr++
		}
	}

	d := [4]int{-c, -1, 1, c}
	ans := -1
	for qr-ql > 0 {
		p := queue[ql]
		ql++
		ans = p[1]
		for i := 0; i < 4; i++ {
			ii := p[0] + d[i]
			if ii < 0 || ii >= n {
				continue
			}
			if ii/r != p[0]/r && ii%c != p[0]%c {
				continue
			}
			if maze[ii] == '#' {
				continue
			}
			if steps[ii] > 0 {
				continue
			}
			steps[ii] = 1
			queue[qr%n][0] = ii
			queue[qr%n][1] = p[1] + 1
			qr++
		}
	}

	fmt.Fprintln(writer, ans)

	writer.Flush()
}


package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)

	writer := bufio.NewWriter(os.Stdout)

	r := getNextInt(scanner)
	c := getNextInt(scanner)
	maze := make([][]byte, r)
	steps := make([][]int, r)
	queue := make([][]int, 0)
	for y := 0; y < r; y++ {
		maze[y] = make([]byte, c)
		steps[y] = make([]int, c)
		s := getNextString(scanner)
		for x := 0; x < c; x++ {
			maze[y][x] = s[x]
			if s[x] == '.' {
				continue
			}
			steps[y][x] = 1
			queue = append(queue, []int{
				y, x,
			})
		}
	}

	dy := [4]int{-1, 0, 0, 1}
	dx := [4]int{0, -1, 1, 0}
	ans := -1
	for len(queue) > 0 {
		l := len(queue)
		for q := 0; q < l; q++ {
			p := queue[q]
			for i := 0; i < 4; i++ {
				if p[0]+dy[i] < 0 || p[0]+dy[i] >= r {
					continue
				}
				if p[1]+dx[i] < 0 || p[1]+dx[i] >= c {
					continue
				}
				if maze[p[0]+dy[i]][p[1]+dx[i]] == '#' {
					continue
				}
				if steps[p[0]+dy[i]][p[1]+dx[i]] > 0 {
					continue
				}
				steps[p[0]+dy[i]][p[1]+dx[i]] = 1
				queue = append(queue, []int{
					p[0] + dy[i],
					p[1] + dx[i],
				})
			}
		}
		ans++
		queue = queue[l:]
	}

	fmt.Fprintln(writer, ans)

	writer.Flush()
}


package main

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

var sc *bufio.Scanner

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

const INF = 1000000000

type node struct {
	depth int
	adj   []*node
}

func main() {
	sc = bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	h, w := nextInt(), nextInt()

	root := node{depth: -1}
	board := make([][]*node, h)
	for i := range board {
		sc.Scan()
		s := sc.Text()
		line := make([]*node, w)
		for j, r := range s {
			line[j] = &node{depth: INF}
			if r == '#' {
				root.adj = append(root.adj, line[j])
			}
		}
		board[i] = line
	}

	for i, line := range board {
		for j := range line {
			if i > 0 {
				board[i][j].adj = append(board[i][j].adj, board[i-1][j])
			}
			if i < h-1 {
				board[i][j].adj = append(board[i][j].adj, board[i+1][j])
			}
			if j > 0 {
				board[i][j].adj = append(board[i][j].adj, board[i][j-1])
			}
			if j < w-1 {
				board[i][j].adj = append(board[i][j].adj, board[i][j+1])
			}
		}
	}

	queue := []*node{&root}
	for len(queue) > 0 {
		grid := queue[0]
		queue = queue[1:]
		for _, other := range grid.adj {
			if other.depth > grid.depth+1 {
				other.depth = grid.depth + 1
				queue = append(queue, other)
			}
		}
	}

	ans := 0
	for _, line := range board {
		for _, grid := range line {
			if ans < grid.depth {
				ans = grid.depth
			}
		}
	}
	fmt.Println(ans)
}