package main

import (
	"fmt"
	"sort"
)

func main() {

	var k, n, aa int
	a := []int{}
	bef := []int{}
	aft := []int{}

	fmt.Scan(&k, &n)
	for i := 0; i < n; i++ {
		fmt.Scan(&aa)
		a = append(a, aa)
	}
	sub := k / 2
	for i, key := range a {
		if sub <= key {
			bef = a[0:i]
			aft = a[i:n]
			break
		}

	}
	if len(bef) != 0 {
		sort.Sort(sort.IntSlice(bef))
		beflen := len(bef)
		bbb := beflen - 1
		huga := bef[bbb:beflen]
		fmt.Println(huga[0] - bef[0])
	} else if len(aft) != 0 {
		sort.Sort(sort.IntSlice(aft))
		beflen := len(aft)
		bbb := beflen - 1
		huga := aft[bbb:beflen]
		fmt.Println(huga[0] - aft[0])
	} else {
		sort.Sort(sort.IntSlice(bef))
		sort.Sort(sort.IntSlice(aft))
		beflen := len(bef)
		aftlen := len(aft)
		aaa := aftlen - 1
		bbb := beflen - 1
		hoge := aft[aaa:aftlen]
		huga := bef[bbb:beflen]
		piyo := bef[0:1]

		if hoge[0]-piyo[0] < hoge[0]-huga[0] {
			fmt.Println(hoge[0] - piyo[0])
		} else {
			fmt.Println(hoge[0] - huga[0])
		}
	}
}

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

Score : 300 points

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.

The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.

When traveling between these houses, you can only go around the pond.

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Constraints

2 \leq K \leq 10^6

2 \leq N \leq 2 \times 10^5

0 \leq A_1 < ... < A_N < K

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K N
A_1 A_2 ... A_N

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Sample Input 1

20 3
5 10 15

Sample Output 1

10

If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.

Sample Input 2

20 3
0 5 15

Sample Output 2

10

If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.

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.


package main
import "fmt"
func main(){
    // Your code here!
    var n, m int
    fmt.Scan(&n, &m)

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

    var tmp int
    for i := 0; i<m; i++{
        if i == 0{
            tmp = t[m-1] - t[i]
        }else{
            if tmp > (t[m-1] - t[i] + n - t[m-1] + t[i-1]){
                tmp = t[m-1] - t[i] + n - t[m-1] + t[i-1]
            }
        }
    }
    fmt.Println(tmp)
}


package main

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

const (
	initialBufSize = 10000
	maxBufSize     = 10000000
)

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

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 nextFloat() float64 {
	sc.Scan()
	f, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func main() {
	k, n := nextInt(), nextInt()
	a := make([]int, n+1)
	for i := 0; i < n; i++ {
		a[i] = nextInt()
	}
	a[n] = k

	max := -1
	sum := 0
	for i := 1; i <= n; i++ {
		dist := a[i] - a[i-1]
		sum += dist
		if dist > max {
			max = dist
		}
	}
	fmt.Println(sum - max)
}


package main

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

// input scanner
var sc = bufio.NewScanner(os.Stdin)

const (
	initialBufSize = 10e4
	maxBufSize     = 10e6
)

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

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

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

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

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

// util
func max(l []int) (int, int) {
	idx := 0
	ans := l[idx]
	for i, n := range l {
		if n > ans {
			idx = i
			ans = n
		}
	}
	return idx, ans
}

func min(l []int) (int, int) {
	idx := 0
	ans := l[idx]
	for i, n := range l {
		if n < ans {
			idx = i
			ans = n
		}
	}
	return idx, ans
}

// main
func main() {
	buf := make([]byte, initialBufSize)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords)
	k := nextInt()
	n := nextInt()
	a := nextInts(n)
	sort.Ints(a)
	var max int
	for i := 0; i < n; i++ {
		var d int
		if i == n-1 {
			d = (k - a[i]) + a[0]
		} else {
			d = a[i+1] - a[i]
		}
		if d > max {
			max = d
		}
	}
	fmt.Println(k - max)
}


/**
 * 最短経路の練習：「Warshall-Floyd法」
 * ABC160 D
 * https://atcoder.jp/contests/abc160/tasks/abc160_d
 *
 * 期待する入力
 * N X Y
 * 3 <= N <= 2000
 * 1 <= X, Y <= N
 * X+1 < Y
 */

 package main

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

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

	 N, X, Y := nextInt(sc), nextInt(sc), nextInt(sc)

	 // 距離計算「Warshall-Floyd法」
	 // 今回、エッジの距離は全て１だが、可変でも行けるはず...
	 // 参考：https://qiita.com/ta-ka/items/a023a11efe17ab097433#warshall-floyd%E6%B3%95
	 distance := make([][]int, N)
	 counter := make([]int, N-1)
	 ks := []int{0, X-1, Y-1}
	 for _, k := range ks {
		 for i := 0; i < N; i++ {
			 if k == 0 {
				 distance[i] = make([]int, N)
				 if i == (X-1) {
					 distance[i][Y-1] = 1
				 } else if i == (Y-1) {
					 distance[i][X-1] = 1
				 }
			 }
			 //NOTE: 有向グラフの場合は、 j < i の部分を j < N に変更する
			 for j := 0; j < i; j++ {
				 if k == 0 {
					 isSetedDistance := false
					 isSetedDistance = isSetedDistance || i == j
					 isSetedDistance = isSetedDistance || i == (X - 1) && j == (Y - 1)
					 isSetedDistance = isSetedDistance || j == (X - 1) && i == (Y - 1)
					 if !isSetedDistance {
						 distance[i][j] = i - j
					 }
				 }
				 distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j])
				 distance[j][i] = distance[i][j]  // 無向グラフのため、対称行列
				 if k == Y-1 {
					 counter[distance[i][j]-1] += 1
				 }
			 }
		 }
	 }

	 // 表示
	 for _, num := range counter {
		 fmt.Println(num)
	 }

 }

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

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

Score : 400 points

Problem Statement

We have an undirected graph G with N vertices numbered 1 to N and N edges as follows:

For each i=1,2,...,N-1, there is an edge between Vertex i and Vertex i+1.

There is an edge between Vertex X and Vertex Y.

For each k=1,2,...,N-1, solve the problem below:

Find the number of pairs of integers (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j in G is k.

Constraints

3 \leq N \leq 2 \times 10^3

1 \leq X,Y \leq N

X+1 < Y

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X Y

Output

For each k=1, 2, ..., N-1 in this order, print a line containing the answer to the problem.

Sample Input 1

5 2 4

Sample Output 1

5
4
1
0

The graph in this input is as follows:

There are five pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 1: (1,2)\,,(2,3)\,,(2,4)\,,(3,4)\,,(4,5).

There are four pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 2: (1,3)\,,(1,4)\,,(2,5)\,,(3,5).

There is one pair (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 3: (1,5).

There are no pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 4.

Sample Input 2

3 1 3

Sample Output 2

3
0

The graph in this input is as follows:

Sample Input 3

7 3 7

Sample Output 3

7
8
4
2
0
0

Sample Input 4

10 4 8

Sample Output 4

10
12
10
8
4
1
0
0
0


package main

import "fmt"

var N, X, Y int
var arr = make([]bool, 2001)
var ans = map[int]int{}

func main() {
	fmt.Scan(&N, &X, &Y)
	for i := 1; i<=N; i++ {
		queue := []int{i}
		visited := map[int]bool{ i: true }
		for k := 1; k<=N-1; k++ {
			nextQueue := []int{}
			for _, v := range queue {
				if v != 1 && !visited[v-1] {
					visited[v-1] = true
					nextQueue = append(nextQueue, v-1)
				}
				if v != N && !visited[v+1] {
					visited[v+1] = true
					nextQueue = append(nextQueue, v+1)
				}
				if v == X && !visited[Y] {
					visited[Y] = true
					nextQueue = append(nextQueue, Y)
				}
				if v == Y && !visited[X] {
					visited[X] = true
					nextQueue = append(nextQueue, X)
				}
			}
			ans[k] += len(nextQueue)
			queue = nextQueue
		}
	}
	for i := 1; i <= N-1; i++ {
		fmt.Println(ans[i]/2)
	}
}


package main

import "fmt"

var (
	N int
	X int
	Y int
)

func main() {
	fmt.Scan(&N, &X, &Y)

	m := make(map[int]int)
	for i := 1; i <= N-1; i++ {
		for j := i + 1; j <= N; j++ {
			m[d(i, j)]++
		}
	}
	for k := 1; k < N; k++ {
		fmt.Println(m[k])
	}
}

func min(x1, x2, x3, x4 int) int {
	var x12, x34 int
	if x1 < x2 {
		x12 = x1
	} else {
		x12 = x2
	}
	if x3 < x4 {
		x34 = x3
	} else {
		x34 = x4
	}
	if x12 < x34 {
		return x12
	}
	return x34
}

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

func d(i, j int) int {

	// 1. i -> j
	// 2. i -> X -> j
	// 3. i -> Y -> j
	// 4. i -> X -> Y -> j

	d1 := abs(j - i)
	d2 := abs(j-X) + abs(X-i)
	d3 := abs(j-Y) + abs(Y-i)
	d4 := abs(X-i) + 1 + abs(Y-j)

	return min(d1, d2, d3, d4)
}


package main

import (
	"fmt"
	"strconv"
)

// Queue is for bfs
type Queue struct {
	data []interface{}
}

// Push adds a value to queue
func (q *Queue) Push(v interface{}) {
	q.data = append(q.data, v)
}

// Pop removes the last element and return it
func (q *Queue) Pop() (interface{}, error) {
	if q.Empty() {
		return nil, fmt.Errorf("queue is empty")
	}
	v := q.data[0]
	q.data = q.data[1:]
	return v, nil
}

// Empty returns true if queue is empty
func (q *Queue) Empty() bool {
	return len(q.data) == 0
}

// NewQueue generates a new queue
func NewQueue() *Queue {
	return &Queue{make([]interface{}, 0)}
}

// Graph is a set of nodes
type Graph struct {
	Nodes []*Node
}

// Node has its value and neighbors
type Node struct {
	Val    interface{}
	Neighs []*Node
}

type item struct {
	N     *Node
	Depth int
}

type path struct {
	A *Node
	B *Node
}

// Show shows a graph
func (g *Graph) Show() {
	nodes := g.Nodes
	for _, node := range nodes {
		fmt.Printf("%v: ", node.Val)
		for _, neigh := range node.Neighs {
			fmt.Printf("%v ", neigh.Val)
		}
		fmt.Println()
	}
}

func searchDist(dist map[path]int, start *Node) {
	visited := make(map[*Node]bool)
	q := NewQueue()
	q.Push(&item{start, 0})
	visited[start] = true
	for !q.Empty() {
		src, _ := q.Pop()
		if v, ok := src.(*item); ok {
			for _, neigh := range v.N.Neighs {
				if !visited[neigh] {
					q.Push(&item{neigh, v.Depth + 1})
					visited[neigh] = true
					p := path{start, neigh}
					if da, ok := dist[p]; ok {
						if v.Depth+1 < da {
							dist[path{start, neigh}] = v.Depth + 1
							dist[path{neigh, start}] = v.Depth + 1
						}
					} else {
						dist[path{start, neigh}] = v.Depth + 1
						dist[path{neigh, start}] = v.Depth + 1
					}
				}
			}
		}
	}
}

// NewGraph generates a new directed graph
func NewGraph(n, x, y int) *Graph {
	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		name := "n" + strconv.Itoa(i)
		nodes[i] = &Node{name, make([]*Node, 0)}
	}

	for i := 0; i < n-1; i++ {
		nodes[i].Neighs = append(nodes[i].Neighs, nodes[i+1])
		nodes[i+1].Neighs = append(nodes[i+1].Neighs, nodes[i])
	}
	nodes[x-1].Neighs = append(nodes[x-1].Neighs, nodes[y-1])
	nodes[y-1].Neighs = append(nodes[y-1].Neighs, nodes[x-1])
	return &Graph{nodes}
}

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

	g := NewGraph(n, x, y)
	// g.Show()

	dist := make(map[path]int)
	for _, node := range g.Nodes {
		searchDist(dist, node)
	}
	count := make(map[int]int)
	for p, d := range dist {
		if p.A.Val.(string) < p.B.Val.(string) {
			count[d]++
		}
	}
	for k := 1; k < n; k++ {
		fmt.Println(count[k])
	}
}


package main

import "fmt"

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

	cm := m * (m - 1) / 2
	cn := n * (n - 1) / 2

	var cnt int
	if n <= 1 {
		cnt = cm
	} else if m <= 1 {
		cnt = cn
	} else {
		cnt = cn + cm
	}

	fmt.Print(cnt)
}

Score : 100 points

Problem Statement

We have N+M balls, each of which has an integer written on it.

It is known that:

The numbers written on N of the balls are even.

The numbers written on M of the balls are odd.

Find the number of ways to choose two of the N+M balls (disregarding order) so that the sum of the numbers written on them is even.

It can be shown that this count does not depend on the actual values written on the balls.

Constraints

0 \leq N,M \leq 100

2 \leq N+M

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the answer.

Sample Input 1

2 1

Sample Output 1

1

For example, let us assume that the numbers written on the three balls are 1,2,4.

If we choose the two balls with 1 and 2, the sum is odd;

If we choose the two balls with 1 and 4, the sum is odd;

If we choose the two balls with 2 and 4, the sum is even.

Thus, the answer is 1.

Sample Input 2

4 3

Sample Output 2

9

Sample Input 3

1 1

Sample Output 3

0

Sample Input 4

13 3

Sample Output 4

81

Sample Input 5

0 3

Sample Output 5

3


package main

import (
	"fmt"
)

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

	ans := (n*(n-1))/2 + (m*(m-1))/2

	fmt.Println(ans)
}


package main

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

func main() {
	s := readS()
	l := len(s)

	if s == reverseS(s) && s[:(l-1)/2] == reverseS(s[:(l-1)/2]) && s[(l+3)/2-1:] == reverseS(s[(l+3)/2-1:]) {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}

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

/*-----------Input utils-----------*/

var sc = bufio.NewScanner(os.Stdin)

func init() {
	if len(os.Args) >= 2 {
		if os.Args[1] == "debug" {
			debug()
		}
	}
	const buf = 200100
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, buf), buf)
}

func debug() {
	testFile, err := os.Open("./test/sample-1.in")
	if err != nil {
		fmt.Fprintln(os.Stderr, "There is no testfile.")
		os.Exit(1)
	}
	sc = bufio.NewScanner(testFile)
}

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

func readR() []rune {
	return []rune(readS())
}

func readI() int {
	i, _ := strconv.Atoi(readS())
	return i
}

func readF() float64 {
	f, _ := strconv.ParseFloat(readS(), 64)
	return f
}

Score : 200 points

Problem Statement

A string S of an odd length is said to be a strong palindrome if and only if all of the following conditions are satisfied:

S is a palindrome.

Let N be the length of S. The string formed by the 1-st through ((N-1)/2)-th characters of S is a palindrome.

The string consisting of the (N+3)/2-st through N-th characters of S is a palindrome.

Determine whether S is a strong palindrome.

Constraints

S consists of lowercase English letters.

The length of S is an odd number between 3 and 99 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

If S is a strong palindrome, print Yes;
otherwise, print No.

Sample Input 1

akasaka

Sample Output 1

Yes

S is akasaka.

The string formed by the 1-st through the 3-rd characters is aka.

The string formed by the 5-th through the 7-th characters is aka.
All of these are palindromes, so S is a strong palindrome.

Sample Input 2

level

Sample Output 2

No

Sample Input 3

atcoder

Sample Output 3

No