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)

	n := getNextInt(scanner)
	list := make([][]int, n)
	for i := 0; i < n; i++ {
		list[i] = make([]int, 0)
	}

	for i := 0; i < n-1; i++ {
		a := getNextInt(scanner) - 1
		b := getNextInt(scanner) - 1
		list[a] = append(list[a], b)
		list[b] = append(list[b], a)
	}

	color := make([]int, n)

	q := make([][2]int, 1)
	distance1 := make([]int, n)
	distance2 := make([]int, n)
	q[0][0] = 0
	q[0][1] = 0
	color[0] = 1
	color[n-1] = 2

	for len(q) > 0 {
		p := q[0]
		q = q[1:]
		l := len(list[p[0]])
		for i := 0; i < l; i++ {
			if color[list[p[0]][i]] != 0 {
				continue
			}
			color[list[p[0]][i]] = 1
			distance1[list[p[0]][i]] = p[1] + 1
			q = append(q, [2]int{
				list[p[0]][i],
				p[1] + 1,
			})
		}
	}
	q = append(q, [2]int{
		n - 1,
		0,
	})
	for len(q) > 0 {
		p := q[0]
		q = q[1:]
		l := len(list[p[0]])
		for i := 0; i < l; i++ {
			if list[p[0]][i] == 0 {
				continue
			}
			if color[list[p[0]][i]] >= 2 {
				continue
			}
			color[list[p[0]][i]] += 2
			distance2[list[p[0]][i]] = p[1] + 1
			q = append(q, [2]int{
				list[p[0]][i],
				p[1] + 1,
			})
		}
	}

	ans := [3]int{0, 0, 0}
	for i := 0; i < n; i++ {
		if color[i] == 3 {
			if distance1[i] <= distance2[i] {
				ans[1]++
				continue
			}
			ans[2]++
			continue
		}
		ans[color[i]]++
	}

	out := "Snuke"
	if ans[1] > ans[2] {
		out = "Fennec"
	}
	fmt.Fprintln(writer, out)

	writer.Flush()
}

Score : 400 points

Problem Statement

Fennec and Snuke are playing a board game.

On the board, there are N cells numbered 1 through N, and N-1 roads, each connecting two cells. Cell a_i is adjacent to Cell b_i through the i-th road. Every cell can be reached from every other cell by repeatedly traveling to an adjacent cell. In terms of graph theory, the graph formed by the cells and the roads is a tree.

Initially, Cell 1 is painted black, and Cell N is painted white. The other cells are not yet colored.
Fennec (who goes first) and Snuke (who goes second) alternately paint an uncolored cell.
More specifically, each player performs the following action in her/his turn:

Fennec: selects an uncolored cell that is adjacent to a black cell, and paints it black.

Snuke: selects an uncolored cell that is adjacent to a white cell, and paints it white.

A player loses when she/he cannot paint a cell. Determine the winner of the game when Fennec and Snuke play optimally.

Constraints

2 \leq N \leq 10^5

1 \leq a_i, b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

If Fennec wins, print Fennec; if Snuke wins, print Snuke.

Sample Input 1

7
3 6
1 2
3 1
7 4
5 7
1 4

Sample Output 1

Fennec

For example, if Fennec first paints Cell 2 black, she will win regardless of Snuke's moves.

Sample Input 2

4
1 4
4 2
2 3

Sample Output 2

Snuke


// AtCoder My Practice
// author: Leonardone @ NEETSDKASU
package main

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

func atoi(s string) int { v, _ := strconv.Atoi(s); return v }
func atou(s string) uint64 { v, _ := strconv.ParseUint(s, 10, 64); return v }
func atof(s string) float64 { v, _ := strconv.ParseFloat(s, 64); return v }
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 }

var (_ = fmt.Println; _red = bufio.NewScanner(os.Stdin); _tok []string)
func init() { _red.Buffer(make([]byte, 1e7), 1e7) }
func gettok() (t string, e bool) {
	if len(_tok) == 0 { if !_red.Scan() { return }; _tok = strings.Split(_red.Text(), " ") }
	t = _tok[0]; _tok = _tok[1:]; e = len(_tok) == 0; return
}
func read(vs ...interface{}) {
	for _, v := range vs {
		switch v := v.(type) {
		case *int: t, _ := gettok(); *v = atoi(t)
		case *uint64: t, _ := gettok(); *v = atou(t)
		case *float64: t, _ := gettok(); *v = atof(t)
		case *string: *v, _ = gettok()
		case *[]int:
			if len(*v) == 0 { for { t, e := gettok(); *v = append(*v, atoi(t)); if e { break } }
			} else { for j := range *v { t, _ := gettok(); (*v)[j] = atoi(t) } }
		case *[]uint64:
			if len(*v) == 0 { for { t, e := gettok(); *v = append(*v, atou(t)); if e { break } }
			} else { for j := range *v { t, _ := gettok(); (*v)[j] = atou(t) } }
		case *[]float64:
			if len(*v) == 0 { for { t, e := gettok(); *v = append(*v, atof(t)); if e { break } }
			} else { for j := range *v { t, _ := gettok(); (*v)[j] = atof(t) } }
		case *[]string:
			if len(*v) == 0 { *v = append(*v, _tok...); _tok = _tok[:0]
			} else { for j := range *v { (*v)[j], _ = gettok() } }
		}
	}
}

func main() {
    var n int
    read(&n)
    tr := make([][]int, n + 1)
    for i := 1; i < n; i++ {
        var a, b int
        read(&a, &b)
        if tr[a] == nil { tr[a] = []int{} }
        if tr[b] == nil { tr[b] = []int{} }
        tr[a] = append(tr[a], b)
        tr[b] = append(tr[b], a)
    }

    // 解説読後
    // https://atcoder.jp/img/arc078/editorial.pdf

    fenneclen := make([]int, n + 1)
    fenneclen[1] = 1
    tmp1 := []int{1}
    for len(tmp1) > 0 {
        sz := len(tmp1) - 1
        x := tmp1[sz]
        tmp1 = tmp1[:sz]
        for _, v := range tr[x] {
            if fenneclen[v] > 0  { continue }
            fenneclen[v] = fenneclen[x] + 1
            tmp1 = append(tmp1, v)
        }
    }
    snukelen := make([]int, n + 1)
    snukelen[n] = 1
    tmp1 = []int{n}
    for len(tmp1) > 0 {
        sz := len(tmp1) - 1
        x := tmp1[sz]
        tmp1 = tmp1[:sz]
        for _, v := range tr[x] {
            if snukelen[v] > 0  { continue }
            snukelen[v] = snukelen[x] + 1
            tmp1 = append(tmp1, v)
        }
    }
    fennec := 0
    snuke := 0
    for i:=1;i<=n;i++ {
        if fenneclen[i] <= snukelen[i] {
            fennec++
        } else {
            snuke++
        }
    }
    if fennec > snuke {
        fmt.Println("Fennec")
    } else {
        fmt.Println("Snuke")
    }
}

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.


package main

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

func main() {
	scanInit()
	combinitMODD()

	n := nextInt()
	p := nextInt() // 0=even, 1=odd
	a := make([]int, n)

	oddnum := 0
	for i := 0; i < n; i++ {
		a[i] = nextInt()
		if a[i]%2 == 1 {
			oddnum++
		}
	}
	evennum := n - oddnum
	cnt := 0

	eway := 1 << evennum
	osum := 0
	for i := 0; i <= oddnum; i += 2 {
		coi := combinationMODD(oddnum, i)
		osum += coi
	}
	cnt = eway * osum

	if p == 1 {
		cnt = 1<<n - cnt
	}
	fmt.Println(cnt)

}

const modd int = 1000000007
const combMax int = 510000 // combinit用
var combfac [combMax]int   // combinit用
var combfinv [combMax]int  // combinit用
var combinv [combMax]int   // combinit用
func combinitMODD() {
	combfac[0] = 1
	combfac[1] = 1
	combfinv[0] = 1
	combfinv[1] = 1
	combinv[1] = 1
	for i := 2; i < combMax; i++ {
		combfac[i] = combfac[i-1] * i % modd
		combinv[i] = modd - combinv[modd%i]*(modd/i)%modd
		combfinv[i] = combfinv[i-1] * combinv[i] % modd
	}
}

// 1<=k<=n<=10**7
func combinationMODD(n int, k int) int {
	if n < k {
		return 0
	}
	if n < 0 || k < 0 {
		return 0
	}
	return combfac[n] * (combfinv[k] * combfinv[n-k] % modd) % modd
}
func pow(a, b int) int {
	var ret int = 1
	for b > 0 {
		if (b & 1) == 1 {
			ret *= a
		}
		a *= a
		b >>= 1
	}
	return ret
}

// ---- readfunc
var sc = bufio.NewScanner(os.Stdin)

func scanInit() {
	const cap = 200 * 1024 // default=64*1024
	var buf = make([]byte, cap)
	sc.Buffer(buf, cap)
	sc.Split(bufio.ScanWords)
	return
}
func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}
func nextStr() string {
	sc.Scan()
	return sc.Text()
}

Score : 200 points

Problem Statement

There are N bags of biscuits. The i-th bag contains A_i biscuits.

Takaki will select some of these bags and eat all of the biscuits inside.
Here, it is also possible to select all or none of the bags.

He would like to select bags so that the total number of biscuits inside is congruent to P modulo 2.
How many such ways to select bags there are?

Constraints

1 \leq N \leq 50

P = 0 or 1

1 \leq A_i \leq 100

Input

Input is given from Standard Input in the following format:

N P
A_1 A_2 ... A_N

Output

Print the number of ways to select bags so that the total number of biscuits inside is congruent to P modulo 2.

Sample Input 1

2 0
1 3

Sample Output 1

2

There are two ways to select bags so that the total number of biscuits inside is congruent to 0 modulo 2:

Select neither bag. The total number of biscuits is 0.

Select both bags. The total number of biscuits is 4.

Sample Input 2

1 1
50

Sample Output 2

0

Sample Input 3

3 0
1 1 1

Sample Output 3

4

Two bags are distinguished even if they contain the same number of biscuits.

Sample Input 4

45 1
17 55 85 55 74 20 90 67 40 70 39 89 91 50 16 24 14 43 24 66 25 9 89 71 41 16 53 13 61 15 85 72 62 67 42 26 36 66 4 87 59 91 4 25 26

Sample Output 4

17592186044416


package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	prices := strings.Split(sc.Text(), " ")
 	for i := 0;i < len(prices) - 1;i++ {
		for j := len(prices) - 1; j > 0; j-- {
			fastPrice,_ := strconv.Atoi(prices[i])
			lastPrice,_ := strconv.Atoi(prices[j])
			if fastPrice > lastPrice{
				t := prices[i]
				prices[i] = prices[j]
				prices[j] = t
			}
		}
	}
	fastPrice,_ := strconv.Atoi(prices[0])
	lastPrice,_ := strconv.Atoi(prices[1])
	println(fastPrice + lastPrice)
}

Score : 100 points

Problem Statement

Snuke is buying a bicycle.
The bicycle of his choice does not come with a bell, so he has to buy one separately.

He has very high awareness of safety, and decides to buy two bells, one for each hand.

The store sells three kinds of bells for the price of a, b and c yen (the currency of Japan), respectively.
Find the minimum total price of two different bells.

Constraints

1 \leq a,b,c \leq 10000

a, b and c are integers.

Input

Input is given from Standard Input in the following format:

a b c

Output

Print the minimum total price of two different bells.

Sample Input 1

700 600 780

Sample Output 1

1300

Buying a 700-yen bell and a 600-yen bell costs 1300 yen.

Buying a 700-yen bell and a 780-yen bell costs 1480 yen.

Buying a 600-yen bell and a 780-yen bell costs 1380 yen.

The minimum among these is 1300 yen.

Sample Input 2

10000 10000 10000

Sample Output 2

20000

Buying any two bells costs 20000 yen.


package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	prices := strings.Split(sc.Text(), " ")
 	for i := 0;i < len(prices);i++ {
		for j := len(prices) - 1; j > 0; j-- {
			fastPrice,_ := strconv.Atoi(prices[i])
			lastPrice,_ := strconv.Atoi(prices[j])
			if fastPrice < lastPrice{
				t := prices[i]
				prices[i] = prices[j]
				prices[j] = t
			}
		}
	}
	fastPrice,_ := strconv.Atoi(prices[0])
	lastPrice,_ := strconv.Atoi(prices[1])
	println(fastPrice + lastPrice)
}


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

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

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

	a, b, c := getInt(), getInt(), getInt()

	out(min(a+b, min(a+c, b+c)))
}

Score : 200 points

Problem Statement

We will call a string that can be obtained by concatenating two equal strings an even string.
For example, xyzxyz and aaaaaa are even, while ababab and xyzxy are not.

You are given an even string S consisting of lowercase English letters.
Find the length of the longest even string that can be obtained by deleting one or more characters from the end of S.
It is guaranteed that such a non-empty string exists for a given input.

Constraints

2 \leq |S| \leq 200

S is an even string consisting of lowercase English letters.

There exists a non-empty even string that can be obtained by deleting one or more characters from the end of S.

Input

Input is given from Standard Input in the following format:

S

Output

Print the length of the longest even string that can be obtained.

Sample Input 1

abaababaab

Sample Output 1

6

abaababaab itself is even, but we need to delete at least one character.

abaababaa is not even.

abaababa is not even.

abaabab is not even.

abaaba is even. Thus, we should print its length, 6.

Sample Input 2

xxxx

Sample Output 2

2

xxx is not even.

xx is even.

Sample Input 3

abcabcabcabc

Sample Output 3

6

The longest even string that can be obtained is abcabc, whose length is 6.

Sample Input 4

akasakaakasakasakaakas

Sample Output 4

14

The longest even string that can be obtained is akasakaakasaka, whose length is 14.


package main

import (
	"fmt"
)

func main() {
	var n, a int
	fmt.Scan(&n)
	var b []int
	for i := 0; i < n; i++ {
		fmt.Scan(&a)
		b = append(b, a)
		b = reverse(b)
	}
	fmt.Println(b)
}
func reverse(t []int) []int {
	w := make([]int, len(t))
	for i := 0; i < len(t); i++ {
		w[i] = t[len(t)-1-i]
	}
	return w
}

Score : 300 points

Problem Statement

You are given an integer sequence of length n, a_1, ..., a_n.
Let us consider performing the following n operations on an empty sequence b.

The i-th operation is as follows:

Append a_i to the end of b.

Reverse the order of the elements in b.

Find the sequence b obtained after these n operations.

Constraints

1 \leq n \leq 2\times 10^5

0 \leq a_i \leq 10^9

n and a_i are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print n integers in a line with spaces in between.
The i-th integer should be b_i.

Sample Input 1

4
1 2 3 4

Sample Output 1

4 2 1 3

After step 1 of the first operation, b becomes: 1.

After step 2 of the first operation, b becomes: 1.

After step 1 of the second operation, b becomes: 1, 2.

After step 2 of the second operation, b becomes: 2, 1.

After step 1 of the third operation, b becomes: 2, 1, 3.

After step 2 of the third operation, b becomes: 3, 1, 2.

After step 1 of the fourth operation, b becomes: 3, 1, 2, 4.

After step 2 of the fourth operation, b becomes: 4, 2, 1, 3.

Thus, the answer is 4 2 1 3.

Sample Input 2

3
1 2 3

Sample Output 2

3 1 2

As shown above in Sample Output 1, b becomes 3, 1, 2 after step 2 of the third operation. Thus, the answer is 3 1 2.

Sample Input 3

1
1000000000

Sample Output 3

1000000000

Sample Input 4

6
0 6 7 6 7 0

Sample Output 4

0 6 6 0 7 7


package main

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

var (
	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))
	r.Split(bufio.ScanWords)

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

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

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

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

func readLengthAndSlice() []int {
	n := readInt()
	return readIntSlice(n)
}

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 minInt(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func minUint(a, b uint) uint {
	if a < b {
		return a
	}
	return b
}

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

func minUint64(a, b uint64) uint64 {
	if a < b {
		return a
	}
	return b
}

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

func maxUint(a, b uint) uint {
	if a < b {
		return b
	}
	return a
}
func maxInt64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func maxUint64(a, b uint64) uint64 {
	if a < b {
		return b
	}
	return a
}

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

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

// readString() string
// readInt() int
// readIntSlice(n int) []int
// readLengthAndSlice() []int
func rev(a []int) []int {
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
	return a
}

func main() {
	defer stdout.Flush()
	n := readInt()
	a := make([][]int, 2)
	a[0] = make([]int, 0, n)
	a[1] = make([]int, 0, n)
	for i, j := 0, 0; i < n; i, j = i+1, 1-j {
		a[j] = append(a[j], readInt())
	}
	ans := append(rev(a[1]), a[0]...)
	if n%2 != 0 {
		rev(ans)
	}
	sp := ""
	for _, v := range ans {
		printf("%s%d", sp, v)
		sp = " "
	}
	println()
}


package main

import (
	"fmt"
)

func main() {
	var x, a, b int
	fmt.Scan(&x, &a, &b)
	if b-a <= 0 {
		fmt.Println("delicious")
	} else if b-a <= x {
		fmt.Println("safe")
	} else {
		fmt.Println("dangerous")
	}
}

Score : 100 points

Problem Statement

Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier.
He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though.

Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious.

Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it.

Write a program that outputs delicious if he found it delicious, safe if he did not found it delicious but did not get a stomachache either, and dangerous if he got a stomachache.

Constraints

1 ≤ X,A,B ≤ 10^9

Input

Input is given from Standard Input in the following format:

X A B

Output

Print delicious if Takahashi found the food delicious; print safe if he neither found it delicious nor got a stomachache; print dangerous if he got a stomachache.

Sample Input 1

4 3 6

Sample Output 1

safe

He ate the food three days after the "best-by" date. It was not delicious or harmful for him.

Sample Input 2

6 5 1

Sample Output 2

delicious

He ate the food by the "best-by" date. It was delicious for him.

Sample Input 3

3 7 12

Sample Output 3

dangerous

He ate the food five days after the "best-by" date. It was harmful for him.


package main

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

var sc = bufio.NewScanner(os.Stdin)

func NextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}

func main() {
	n := NextInt()
	a := make([]int, n)
	for i := range a {
		a[i] = NextInt() - 1
	}

	var now, cnt int
	for {
		if now == 1 {
			fmt.Println(cnt)
			break
		}

		if cnt >= n {
			fmt.Println(-1)
			break
		}

		cnt++
		now = a[now]
	}
}

Score : 200 points

Problem Statement

Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.

The exercise machine at the gym has N buttons, and exactly one of the buttons is lighten up.
These buttons are numbered 1 through N.
When Button i is lighten up and you press it, the light is turned off, and then Button a_i will be lighten up. It is possible that i=a_i.
When Button i is not lighten up, nothing will happen by pressing it.

Initially, Button 1 is lighten up. Takahashi wants to quit pressing buttons when Button 2 is lighten up.

Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.

Constraints

2 ≤ N ≤ 10^5

1 ≤ a_i ≤ N

Input

Input is given from Standard Input in the following format:

N
a_1
a_2
:
a_N

Output

Print -1 if it is impossible to lighten up Button 2.
Otherwise, print the minimum number of times we need to press buttons in order to lighten up Button 2.

Sample Input 1

3
3
1
2

Sample Output 1

2

Press Button 1, then Button 3.

Sample Input 2

4
3
4
1
2

Sample Output 2

-1

Pressing Button 1 lightens up Button 3, and vice versa, so Button 2 will never be lighten up.

Sample Input 3

5
3
3
4
2
4

Sample Output 3

3


package main

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

func main() {
	n := getInt()
	as := make([]int, n)
	for i := range as {
		as[i] = getInt()
	}

	flags := make([]bool, n)
	ans := 0
	i := 0
	for {
		if flags[i] == true {
			ans = -1
			break
		} else if i == 1 {
			break
		}
		flags[i] = true
		i = as[i] - 1
		ans++
	}

	fmt.Println(ans)
}

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

var scanner = wordScanner()

func wordScanner() *bufio.Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return sc
}

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

func getString() string {
	scanner.Scan()
	s := scanner.Text()
	return s
}


package main

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

var (
	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))
	r.Split(bufio.ScanWords)

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

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

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

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

func readLengthAndSlice() []int {
	n := readInt()
	return readIntSlice(n)
}

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 minInt(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func minUint(a, b uint) uint {
	if a < b {
		return a
	}
	return b
}

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

func minUint64(a, b uint64) uint64 {
	if a < b {
		return a
	}
	return b
}

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

func maxUint(a, b uint) uint {
	if a < b {
		return b
	}
	return a
}
func maxInt64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func maxUint64(a, b uint64) uint64 {
	if a < b {
		return b
	}
	return a
}

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

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

// readString() string
// readInt() int
// readIntSlice(n int) []int
// readLengthAndSlice() []int
func main() {
	defer stdout.Flush()

	n := readInt()
	a := make([]int, n+1)
	for i := 1; i <= n; i++ {
		a[i] = readInt()
	}
	var ans int
	count := make([]int, 100001)
	for i := 1; i != 2; {
		count[i]++
		if count[i] > 1 {
			println(-1)
			return
		}
		i = a[i]
		ans++
	}
	println(ans)
}


package main

import (
	"fmt"
	"math"
)

func permutation(n int, k int) int {
	v := 1
	if 0 < k && k <= n {
		for i := 0; i < k; i++ {
			v *= (n - i)
			v = v % int(math.Pow10(9)+7)
		}
	} else if k > n {
		v = 0
	}
	return v
}

func factorial(n int) int {
	return permutation(n, n-1)
}

func main() {
	// Code for C - Reconciled?
	var N, M int
	fmt.Scanf("%d %d", &N, &M)

	var count int
	var flag int = 0
	if N == M {
		count = count + 2
	} else if int(math.Abs(float64(N-M))) == 1 {
		count++
	} else {
		flag = 1
	}

	if flag == 0 {
		count *= factorial(N) % int(math.Pow10(9)+7)
		count *= factorial(M) % int(math.Pow10(9)+7)
		count = count % int(math.Pow10(9)+7)
		fmt.Println(count)
	} else {
		fmt.Println("0")
	}
}

Score : 300 points

Problem Statement

Snuke has N dogs and M monkeys. He wants them to line up in a row.

As a Japanese saying goes, these dogs and monkeys are on bad terms. ("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.

How many such arrangements there are? Find the count modulo 10^9+7 (since animals cannot understand numbers larger than that).
Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.

Constraints

1 ≤ N,M ≤ 10^5

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of possible arrangements, modulo 10^9+7.

Sample Input 1

2 2

Sample Output 1

8

We will denote the dogs by A and B, and the monkeys by C and D. There are eight possible arrangements: ACBD, ADBC, BCAD, BDAC, CADB, CBDA, DACB and DBCA.

Sample Input 2

3 2

Sample Output 2

12

Sample Input 3

1 8

Sample Output 3

0

Sample Input 4

100000 100000

Sample Output 4

530123477


package main

import "fmt"

func main() {
	var s string
	fmt.Scan(&s)
	dic := make([]map[rune]bool, len(s))
	flg := false

	for i := range s {
		dic[i] = make(map[rune]bool)
		dic[i][rune(s[i])] = true
	}
	for {
		for c := 'a'; c <= 'z'; c++ {
			for i := 0; i < len(dic); i++ {
				if !dic[i][rune(c)] {
					goto exitLoop
				}
			}
			flg = true
		exitLoop:
		}
		for i := 0; i < len(dic)-1; i++ {
			for j := range dic[i+1] {
				dic[i][j] = true
			}
		}
		if flg {
			break
		}
		dic = dic[:len(dic)-1]
	}
	fmt.Println(len(s) - len(dic))
}

Score : 300 points

Problem Statement

Snuke can change a string t of length N into a string t' of length N - 1 under the following rule:

For each i (1 ≤ i ≤ N - 1), the i-th character of t' must be either the i-th or (i + 1)-th character of t.

There is a string s consisting of lowercase English letters.
Snuke's objective is to apply the above operation to s repeatedly so that all the characters in s are the same.
Find the minimum necessary number of operations.

Constraints

1 ≤ |s| ≤ 100

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum necessary number of operations to achieve the objective.

Sample Input 1

serval

Sample Output 1

3

One solution is: serval → srvvl → svvv → vvv.

Sample Input 2

jackal

Sample Output 2

2

One solution is: jackal → aacaa → aaaa.

Sample Input 3

zzz

Sample Output 3

0

All the characters in s are the same from the beginning.

Sample Input 4

whbrjpjyhsrywlqjxdbrbaomnw

Sample Output 4

8

In 8 operations, he can change s to rrrrrrrrrrrrrrrrrr.


package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	fmt.Println(&n)
	a := make([]int, n)
	for i,_ := range a {
		fmt.Scan(&a[i])
	}
	sort.Ints(a)
	fmt.Println(a[len(a) - 1] - a[0])
}

Score : 200 points

Problem Statement

It is only six months until Christmas, and AtCoDeer the reindeer is now planning his travel to deliver gifts.

There are N houses along TopCoDeer street. The i-th house is located at coordinate a_i. He has decided to deliver gifts to all these houses.

Find the minimum distance to be traveled when AtCoDeer can start and end his travel at any positions.

Constraints

1 ≤ N ≤ 100

0 ≤ a_i ≤ 1000

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum distance to be traveled.

Sample Input 1

4
2 3 7 9

Sample Output 1

7

The travel distance of 7 can be achieved by starting at coordinate 9 and traveling straight to coordinate 2.

It is not possible to do with a travel distance of less than 7, and thus 7 is the minimum distance to be traveled.

Sample Input 2

8
3 1 4 1 5 9 2 6

Sample Output 2

8

There may be more than one house at a position.


package main

import "fmt"
import "sort"

func main() {
    var N, v, d int
    var list []int

    fmt.Scanf("%d", &N)

    for i := 0; i < N; i++ {
        fmt.Scanf("%d", &v)
        list = append(list, v)
    }
    sort.Sort(sort.IntSlice(list))

    for i := 1; i < N; i++ {
        d += (list[i] - list[i-1])
    }
    fmt.Println(d)
}


package main

import (
	"fmt"
	"math"
)

func main() {
	// Code for C - Colorful Leaderboard
	var N int
	fmt.Scanf("%d", &N)

	color := make(map[int]int, 9)
	var a int
	for i := 0; i < N; i++ {
		fmt.Scanf("%d", &a)
		if 1 <= a && a <= 399 {
			color[0]++
		} else if 400 <= a && a <= 799 {
			color[1]++
		} else if 800 <= a && a <= 1199 {
			color[2]++
		} else if 1200 <= a && a <= 1599 {
			color[3]++
		} else if 1600 <= a && a <= 1999 {
			color[4]++
		} else if 2000 <= a && a <= 2399 {
			color[5]++
		} else if 2400 <= a && a <= 2799 {
			color[6]++
		} else if 2800 <= a && a <= 3199 {
			color[7]++
		} else {
			color[8]++
		}
	}

	var count int
	for i := 0; i < 8; i++ {
		if color[i] > 0 {
			count++
		}
	}

	min := int(math.Max(float64(count), 1))
	max := int(math.Min(float64(count+color[8]), 8))

	fmt.Println(min, max)
}

Score : 300 points

Problem Statement

In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows:

Rating 1-399 : gray

Rating 400-799 : brown

Rating 800-1199 : green

Rating 1200-1599 : cyan

Rating 1600-1999 : blue

Rating 2000-2399 : yellow

Rating 2400-2799 : orange

Rating 2800-3199 : red

Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not.

Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i.

Find the minimum and maximum possible numbers of different colors of the users.

Constraints

1 ≤ N ≤ 100

1 ≤ a_i ≤ 4800

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between.

Sample Input 1

4
2100 2500 2700 2700

Sample Output 1

2 2

The user with rating 2100 is "yellow", and the others are "orange". There are two different colors.

Sample Input 2

5
1100 1900 2800 3200 3200

Sample Output 2

3 5

The user with rating 1100 is "green", the user with rating 1900 is blue and the user with rating 2800 is "red".

If the fourth user picks "red", and the fifth user picks "blue", there are three different colors. This is one possible scenario for the minimum number of colors.

If the fourth user picks "purple", and the fifth user picks "black", there are five different colors. This is one possible scenario for the maximum number of colors.

Sample Input 3

20
800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990

Sample Output 3

1 1

All the users are "green", and thus there is one color.


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()
	a := iSScan(n)
	x := make([]int, 9)
	for _, v := range a {
		x[smaller(v/400, 8)]++
	}
	for i := 0; i < 8; i++ {
		x[i] = smaller(1, x[i])
	}
	fmt.Println(sum(x[:8]), sum(x[:8])+x[8])
}


package main

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

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

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

func (con *contest) main() error {
	var n int
	con.Scan(&n)
	r := make([]bool, 8)
	s := 0
	for i := 0; i < n; i++ {
		var a int
		con.Scan(&a)
		if a < 3200 {
			r[a/400] = true
		} else {
			s++
		}
	}
	t := 0
	for _, a := range r {
		if a {
			t++
		}
	}
	s += t
	if s > 8 {
		s = 8
	}
	con.Println(t, s)
	return nil
}


package main

import (
	"fmt"
	"math"
)

func main() {
	var n int
	fmt.Scan(&n)
	rank := make([]int, 8+1)
	for i := 0; i < n; i++ {
		var tmp int
		fmt.Scan(&tmp)
		rank[checkRank(tmp)]++
	}
	sum := 0
	for i := 0; i < 8; i++ {
		if rank[i] > 0 {
			sum++
		}
	}
	minNum := int(math.Max(float64(1), float64(sum)))
	maxNum := int(math.Min(float64(8), float64(sum+rank[8])))
	fmt.Printf("%d %d\n", minNum, maxNum)

}

func checkRank(num int) int {
	if num >= 8*400 {
		return 8
	}
	return num / 400
}


package main

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

func main() {

	s := bufio.NewScanner(os.Stdin)
	s.Scan()
	//n, _ := strconv.Atoi(s.Text())
	s.Scan()

	m := make(map[int]bool)
	a := strings.Split(s.Text(), " ")
	c := 0
	for _, v := range a {
		i, _ := strconv.Atoi(v)
		if i > 3199 {
			c++
		} else {
			m[i/400] = true
		}
	}
	r := c + len(m)
	if r > 8 {
		r = 8
	}

	if len(m) == 0 {
		fmt.Printf("%v %v\n", 1, r)
		return
	}
	fmt.Printf("%v %v\n", len(m), r)
}


package main

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

func main() {
	fsc := NewFastScanner()

	N := fsc.NextInt()
	var rates []int
	colors := map[int]int{}
	for i := 0; i < N; i++ {
		rates = append(rates, IntMin(8, fsc.NextInt()/400))
		if rates[i] <= 7 {
			colors[rates[i]] = 1
		}
	}
	sort.Ints(rates)
	resMin := IntMax(1, len(colors))
	resMax := len(colors)
	for i := 0; i < N; i++ {
		if rates[i] >= 8 {
			resMax++
		}
	}
	fmt.Println(resMin, resMax)
}

//template
type FastScanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewFastScanner() *FastScanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1024)
	return &FastScanner{r: rdr}
}
func (s *FastScanner) 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 *FastScanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *FastScanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *FastScanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *FastScanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *FastScanner) 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
		}
	}
}

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

func Int64Max(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func Float64Max(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

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

func Int64Min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}
func Float64Min(a, b float64) float64 {
	if a > b {
		return b
	}
	return a
}

//Gcd
func IntGcd(a, b int) int {
	if a < b {
		b, a = a, b
	}
	if b == 0 {
		return a
	}
	return IntGcd(b, a%b)
}
func Int64Gcd(a, b int64) int64 {
	if a < b {
		b, a = a, b
	}
	if b == 0 {
		return a
	}
	return Int64Gcd(b, a%b)
}

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

func Int64Abs(a int64) int64 {
	if a < 0 {
		a *= -1
	}
	return a
}


// +build ignore
package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

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

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []byte {
	return []byte(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

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

func run(input io.Reader, output io.Writer) int {
	sc := NewScanner()
	A := sc.nextInt()
	B := sc.nextInt()

	if A+B < 10 {
		fmt.Println(A + B)
	} else {
		fmt.Println("error")
	}

	return 0
}

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

Score : 100 points

Problem Statement

You are given two integers A and B as the input. Output the value of A + B.

However, if A + B is 10 or greater, output error instead.

Constraints

A and B are integers.

1 ≤ A, B ≤ 9

Input

Input is given from Standard Input in the following format:

A B

Output

If A + B is 10 or greater, print the string error (case-sensitive); otherwise, print the value of A + B.

Sample Input 1

6 3

Sample Output 1

9

Sample Input 2

6 4

Sample Output 2

error


package main
import(
  "fmt"
)
  func main(){
    var s string
    var i, j int
    fmt.Scan(&s)
    for i=0;i<len(s)-1;i++{
      for j=i+1;j<len(s);j++{
        if s[i]==s[j]{
          fmt.Println("no")
          return
        }
      }
    }
   fmt.Println("yes")
 }

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase English letters. Determine whether all the characters in S are different.

Constraints

2 ≤ |S| ≤ 26, where |S| denotes the length of S.

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If all the characters in S are different, print yes (case-sensitive); otherwise, print no.

Sample Input 1

uncopyrightable

Sample Output 1

yes

Sample Input 2

different

Sample Output 2

no

Sample Input 3

no

Sample Output 3

yes


package main

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

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

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

func (con *contest) main() error {
	var n int
	con.Scan(&n)
	s := []int{}
	t := 0
	for i := 0; i < n; i++ {
		var a int
		con.Scan(&a)
		t += a
		if a%10 != 0 {
			s = append(s, a)
		}
	}
	sort.Ints(s)
	if t%10 == 0 {
		if len(s) > 0 {
			t -= s[0]
		}
		if t%10 == 0 {
			t = 0
		}
	}
	con.Println(t)
	return nil
}

Score : 300 points

Problem Statement

You are taking a computer-based examination. The examination consists of N questions, and the score allocated to the i-th question is s_i. Your answer to each question will be judged as either "correct" or "incorrect", and your grade will be the sum of the points allocated to questions that are answered correctly. When you finish answering the questions, your answers will be immediately judged and your grade will be displayed... if everything goes well.

However, the examination system is actually flawed, and if your grade is a multiple of 10, the system displays 0 as your grade. Otherwise, your grade is displayed correctly. In this situation, what is the maximum value that can be displayed as your grade?

Constraints

All input values are integers.

1 ≤ N ≤ 100

1 ≤ s_i ≤ 100

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

Print the maximum value that can be displayed as your grade.

Sample Input 1

3
5
10
15

Sample Output 1

25

Your grade will be 25 if the 10-point and 15-point questions are answered correctly and the 5-point question is not, and this grade will be displayed correctly. Your grade will become 30 if the 5-point question is also answered correctly, but this grade will be incorrectly displayed as 0.

Sample Input 2

3
10
10
15

Sample Output 2

35

Your grade will be 35 if all the questions are answered correctly, and this grade will be displayed correctly.

Sample Input 3

3
10
20
30

Sample Output 3

0

Regardless of whether each question is answered correctly or not, your grade will be a multiple of 10 and displayed as 0.


package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	ss := make([]int, n)
	for i := range ss {
		fmt.Scan(&ss[i])
	}

	sum := 0
	for _, s := range ss {
		sum += s
	}

	min := 101
	for _, s := range ss {
		if s < min && s%10 != 0 {
			min = s
		}
	}

	if sum%10 == 0 {
		if min == 101 {
			fmt.Println(0)
		} else {
			fmt.Println(sum - min)
		}
	} else {
		fmt.Println(sum)
	}
}


package main

import (
	"fmt"
	"sort"
)

func main() {
	var N int
	fmt.Scan(&N)
	s := make([]int, N)
	sum := 0
	for i := 0; i < N; i++ {
		fmt.Scan(&s[i])
		sum += s[i]
	}
	if sum%10 == 0 {
		sort.Ints(s)
		for i, v := range s {
			if v%10 != 0 {
				fmt.Println(sum - v)
				break
			}
			if i == N-1 {
				fmt.Println(0)
			}
		}
	} else {
		fmt.Println(sum)
	}

}


// +build ignore
package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

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

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []byte {
	return []byte(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

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

func run(input io.Reader, output io.Writer) int {
	sc := NewScanner()
	x := sc.nextInt()
	y := sc.nextInt()

	g := map[int]int{1: 1, 3: 1, 5: 1, 7: 1, 8: 1, 10: 1, 12: 1, 4: 2, 6: 2, 9: 2, 11: 2, 2: 3}

	if g[x] == g[y] {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}

	return 0
}

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

Score : 100 points

Problem Statement

Based on some criterion, Snuke divided the integers from 1 through 12 into three groups as shown in the figure below.
Given two integers x and y (1 ≤ x < y ≤ 12), determine whether they belong to the same group.

Constraints

x and y are integers.

1 ≤ x < y ≤ 12

Input

Input is given from Standard Input in the following format:

x y

Output

If x and y belong to the same group, print Yes; otherwise, print No.

Sample Input 1

1 3

Sample Output 1

Yes

Sample Input 2

2 4

Sample Output 2

No


package main

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

var nextString func() string

func init() {
	nextString = newReadString(os.Stdin)
}

func main() {
	h := nextInt()
	w := nextInt()
	a := make([]string, h)

	for i := 0; i < h; i++ {
		a[i] = nextString()
	}

	for i := -1; i <= h; i++ {
		fmt.Print("#")
		if 0 <= i && i < h {
			fmt.Print(a[i])
		} else {
			fmt.Print(strings.Repeat("#", w))
		}
		fmt.Println("#")
	}
}

func nextInt() (result int) {
	result, _ = strconv.Atoi(nextString())
	return
}

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

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

Score : 200 points

Problem Statement

You are given a image with a height of H pixels and a width of W pixels.
Each pixel is represented by a lowercase English letter.
The pixel at the i-th row from the top and j-th column from the left is a_{ij}.

Put a box around this image and output the result. The box should consist of # and have a thickness of 1.

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

H W
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}

Output

Print the image surrounded by a box that consists of # and has a thickness of 1.

Sample Input 1

2 3
abc
arc

Sample Output 1

#####
#abc#
#arc#
#####

Sample Input 2

1 1
z

Sample Output 2

###
#z#
###


// Try AtCoder
// author: Leonardone @ NEETSDKASU
package main

import (
	"fmt"
)

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

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

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

func main() {
	var h, w int
	fmt.Scan(&h, &w)
	if h%3 == 0 || w%3 == 0 {
		fmt.Println(0)
		return
	}
	h2 := h / 2
	w2 := w / 2
	s2a := h2 * w
	s2b := (h - h2) * w2
	s2c := (h - h2) * (w - w2)

	ans1 := Max(Abs(s2a-s2b), Max(Abs(s2b-s2c), Abs(s2c-s2a)))

	s3a := h * w2
	s3b := h2 * (w - w2)
	s3c := (h - h2) * (w - w2)

	ans2 := Max(Abs(s3a-s3b), Max(Abs(s3b-s3c), Abs(s3c-s3a)))

	fmt.Println(Min(ans1, ans2))
}

Score : 400 points

Problem Statement

There is a bar of chocolate with a height of H blocks and a width of W blocks.
Snuke is dividing this bar into exactly three pieces.
He can only cut the bar along borders of blocks, and the shape of each piece must be a rectangle.

Snuke is trying to divide the bar as evenly as possible.
More specifically, he is trying to minimize S_{max} - S_{min}, where S_{max} is the area (the number of blocks contained) of the largest piece, and S_{min} is the area of the smallest piece.
Find the minimum possible value of S_{max} - S_{min}.

Constraints

2 ≤ H, W ≤ 10^5

Input

Input is given from Standard Input in the following format:

H W

Output

Print the minimum possible value of S_{max} - S_{min}.

Sample Input 1

3 5

Sample Output 1

0

In the division below, S_{max} - S_{min} = 5 - 5 = 0.

Sample Input 2

4 5

Sample Output 2

2

In the division below, S_{max} - S_{min} = 8 - 6 = 2.

Sample Input 3

5 5

Sample Output 3

4

In the division below, S_{max} - S_{min} = 10 - 6 = 4.

Sample Input 4

100000 2

Sample Output 4

1

Sample Input 5

100000 100000

Sample Output 5

50000


package main

import (
	"fmt"
	"sort"
)

const max = 10000000000

var h, w, size int64

func abs(i int64) int64 {
	if i < 0 {
		return -i
	}
	return i
}

func min(i, j int64) int64 {
	if i < j {
		return i
	}
	return j
}

func calcLeft(x int64) int64 {
	return x * h
}

func calcRightUp(x int64) int64 {
	return (w - x) * (h/2 + h%2)
}

func calcRightDown(x int64) int64 {
	return (w - x) * (h / 2)
}

func calcMin(x, min int64) int64 {
	left, rightUp, rightDown := calcLeft(x), calcRightUp(x), calcRightDown(x)

	sizes := []int{int(rightUp), int(rightDown), int(left)}
	sort.Ints(sizes)
	minSize, maxSize := int64(sizes[0]), int64(sizes[2])
	diff := maxSize - minSize

	if diff < min {
		min = diff
	} else {
		return min
	}

	return calcMin(x+1, min)
}

func main() {
	fmt.Scan(&h, &w)
	size = h * w

	if h%3 == 0 || w%3 == 0 {
		fmt.Println(0)
		return
	}

	if h > w {
		h, w = w, h
	}

	fmt.Println(min(calcMin(1, max), h))
}


package main
import . "fmt"
import "sort"
func main() {
  var n,a,b,i int
  var k int64
  var keys []int
  m := map[int]int64{}
  Scan(&n,&k)
  for i=0;i<n;i++ {
    Scan(&a,&b)
    if v,ok := m[a];ok {
      m[a] = v+int64(b)
    } else {
      m[a] = int64(b)
    }
  }
  n = len(m)
  keys = make([]int,n)
  i = 0
  for key,_ := range m {
    keys[i] = key
    i++
  }
  sort.Ints(keys)
  for i=0;i<n;i++ {
    v := m[keys[i]]
    if k <= v {
      Println(keys[i])
      break
    }
    k -= v
  }
}

Score : 300 points

Problem Statement

There is an empty array.
The following N operations will be performed to insert integers into the array.
In the i-th operation (1≤i≤N), b_i copies of an integer a_i are inserted into the array.
Find the K-th smallest integer in the array after the N operations.
For example, the 4-th smallest integer in the array \{1,2,2,3,3,3\} is 3.

Constraints

1≤N≤10^5

1≤a_i,b_i≤10^5

1≤K≤b_1…+…b_n

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
a_1 b_1
:
a_N b_N

Output

Print the K-th smallest integer in the array after the N operations.

Sample Input 1

3 4
1 1
2 2
3 3

Sample Output 1

3

The resulting array is the same as the one in the problem statement.

Sample Input 2

10 500000
1 100000
1 100000
1 100000
1 100000
1 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000

Sample Output 2

1


package main

import (
	"fmt"
//	"sort"
)

type E struct {
	a, b, c int
}

const INF = 10000000000000

func main() {
	var nNode, nEdge int
	fmt.Scan(&nNode, &nEdge)

	g := make([][]E, nNode, nNode)
	for i := 0; i < nNode; i++ {
		g[i] = make([]E, 0, nNode)
	}

	for i := 0; i < nEdge; i++ {
		var e E
		fmt.Scan(&e.a, &e.b, &e.c)
		e.a--
		e.b--
		e.c = -e.c
		g[e.a] = append(g[e.a], e)
	}

	// for i := 0; i < nNode; i++ {
	// 	for j := 0; j < len(g[i]); j++ {
	// 		e := g[i][j]
	// 		fmt.Printf("{%d,%d,%d} ", e.a, e.b, e.c)
	// 	}
	// 	fmt.Println("")
	// }

	dist := make([]int, nNode, nNode)
	for i := 0; i < nNode; i++ {
		dist[i] = INF + INF
	}
	dist[0] = 0

	// for k := 0; k < nNode; k++ {
	// 	for i := 0; i < nNode; i++ {
	// 		for j := 0; j < len(g[i]); j++ {
	// 			e := g[i][j]
	// 			if dist[e.a] + e.c < dist[e.b] {
	// 				dist[e.b] = dist[e.a] + e.c
	// 			}
	// 		}
	// 	}
	// }
	// mini := dist[nNode-1]
	// for k := 0; k < nNode; k++ {
	// 	for i := 0; i < nNode; i++ {
	// 		for j := 0; j < len(g[i]); j++ {
	// 			e := g[i][j]
	// 			if dist[e.a] + e.c < dist[e.b] {
	// 				dist[e.b] = dist[e.a] + e.c
	// 			}
	// 		}
	// 	}
	// }
	// if dist[nNode-1] < mini {
	// 	fmt.Println("inf")
	// } else {
	// 	fmt.Printf("%d\n", -mini)
	// }

	for k := 0; k < nNode; k++ {
		for i := 0; i < nNode; i++ {
			for j := 0; j < len(g[i]); j++ {
				e := g[i][j]
				if dist[e.a] + e.c < dist[e.b] {
					dist[e.b] = dist[e.a] + e.c
					// fmt.Printf("%d,%d\n", e.b, dist[e.b])
					if k == nNode-1 {
						dist[e.b] = -INF
					}
				}
			}
		}
	}

	// for i := 0; i < nNode; i++ {
	// 	fmt.Printf("> %d\n", dist[i])
	// }

	if dist[nNode-1] == -INF {
		fmt.Println("inf")
	} else {
		fmt.Printf("%d\n", -dist[nNode-1])
	}
}

Score : 400 points

Problem Statement

There is a directed graph with N vertices and M edges.
The i-th edge (1≤i≤M) points from vertex a_i to vertex b_i, and has a weight c_i.
We will play the following single-player game using this graph and a piece.

Initially, the piece is placed at vertex 1, and the score of the player is set to 0.
The player can move the piece as follows:

When the piece is placed at vertex a_i, move the piece along the i-th edge to vertex b_i. After this move, the score of the player is increased by c_i.

The player can end the game only when the piece is placed at vertex N.
The given graph guarantees that it is possible to traverse from vertex 1 to vertex N.

When the player acts optimally to maximize the score at the end of the game, what will the score be?
If it is possible to increase the score indefinitely, print inf.

Constraints

2≤N≤1000

1≤M≤min(N(N-1),2000)

1≤a_i,b_i≤N (1≤i≤M)

a_i≠b_i (1≤i≤M)

a_i≠a_j or b_i≠b_j (1≤i<j≤M)

-10^9≤c_i≤10^9 (1≤i≤M)

c_i is an integer.

In the given graph, there exists a path from vertex 1 to vertex N.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1 c_1
a_2 b_2 c_2
:
a_M b_M c_M

Output

Print the maximum possible score at the end of the game, if it is finite. If it is possible to increase the score indefinitely, print inf.

Sample Input 1

3 3
1 2 4
2 3 3
1 3 5

Sample Output 1

7

There are two ways to move the piece to vertex N=3:

vertex 1 → vertex 2 → vertex 3 : score 4+3=7

vertex 1 → vertex 3 : score 5

Thus, the maximum possible score at the end of the game is 7.

Sample Input 2

2 2
1 2 1
2 1 1

Sample Output 2

inf

It is possible to increase the score indefinitely by alternating between vertex 1 and 2.

Sample Input 3

6 5
1 2 -1000000000
2 3 -1000000000
3 4 -1000000000
4 5 -1000000000
5 6 -1000000000

Sample Output 3

-5000000000


package main

import "fmt"

func main() {
	var a, b, c string
	fmt.Scanf("%s %s %s", &a, &b, &c)
	if a[len(a)-1] == b[0] && b[len(b)-1] == c[0] {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

Score : 100 points

Problem Statement

You are given three strings A, B and C. Check whether they form a word chain.

More formally, determine whether both of the following are true:

The last character in A and the initial character in B are the same.

The last character in B and the initial character in C are the same.

If both are true, print YES. Otherwise, print NO.

Constraints

A, B and C are all composed of lowercase English letters (a - z).

1 ≤ |A|, |B|, |C| ≤ 10, where |A|, |B| and |C| are the lengths of A, B and C, respectively.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print YES or NO.

Sample Input 1

rng gorilla apple

Sample Output 1

YES

They form a word chain.

Sample Input 2

yakiniku unagi sushi

Sample Output 2

NO

A and B form a word chain, but B and C do not.

Sample Input 3

a a a

Sample Output 3

YES

Sample Input 4

aaaaaaaaab aaaaaaaaaa aaaaaaaaab

Sample Output 4

NO


package main

import (
	"fmt"
)

func main() {
	var A int
	var B int
	var C int
	fmt.Scan(&A)
	fmt.Scan(&B)
	fmt.Scan(&C)
	if A%2 == 0 && B%2 == 0 && C%2 == 1 {
		fmt.Println("NO")
	} else if A%2 == 1 && B%2 == 0 && C%2 == 0 {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
	}
}

Score : 200 points

Problem Statement

We ask you to select some number of positive integers, and calculate the sum of them.

It is allowed to select as many integers as you like, and as large integers as you wish.
You have to follow these, however: each selected integer needs to be a multiple of A, and you need to select at least one integer.

Your objective is to make the sum congruent to C modulo B.
Determine whether this is possible.

If the objective is achievable, print YES. Otherwise, print NO.

Constraints

1 ≤ A ≤ 100

1 ≤ B ≤ 100

0 ≤ C < B

Input

Input is given from Standard Input in the following format:

A B C

Output

Print YES or NO.

Sample Input 1

7 5 1

Sample Output 1

YES

For example, if you select 7 and 14, the sum 21 is congruent to 1 modulo 5.

Sample Input 2

2 2 1

Sample Output 2

NO

The sum of even numbers, no matter how many, is never odd.

Sample Input 3

1 100 97

Sample Output 3

YES

You can select 97, since you may select multiples of 1, that is, all integers.

Sample Input 4

40 98 58

Sample Output 4

YES

Sample Input 5

77 42 36

Sample Output 5

NO


package main

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

var stdin = bufio.NewScanner(os.Stdin)

func main() {
	arr := scanArrayInt()
	a, b, c := arr[0], arr[1], arr[2]
	for i := 1; i <= b; i++ {
		d := a * i % b
		if d == c {
			fmt.Println("YES")
			return
		}
	}
	fmt.Println("No")
}
func scanArrayInt() []int {
	var ret = []int{}
	stdin.Scan()
	for _, s := range strings.Split(stdin.Text(), " ") {
		i, _ := strconv.Atoi(s)
		ret = append(ret, i)
	}
	return ret
}


package main

import (
	"fmt"
)

func main() {
	var A, B, C int
	var check bool = false
	fmt.Scan(&A)
	fmt.Scan(&B)
	fmt.Scan(&C)
	for i := 1; i < 101; i++ {
		if (A*i)%B == C {
			check = true
		}
	}
	if check {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}


package main

import (
	"fmt"
	"sync"
)

func main() {
	var n, t uint64
	fmt.Scanf("%d %d", &n, &t)
	ti := make([]uint64, n)
	for i := uint64(0); i < n; i++ {
		fmt.Scan(&ti[i])
	}
	sum := uint64(t)
	limit := make(chan struct{}, 100000)
	wg := &sync.WaitGroup{}
	for i := uint64(1); i < n; i++ {
		limit <- struct{}{}
		wg.Add(1)
		go func(a, b uint64) {
			defer wg.Done()
			tmp := a - b
			if t < tmp {
				sum += t
			} else {
				sum += tmp
			}
			<-limit
		}(ti[i], ti[i-1])
	}
	wg.Wait()
	fmt.Println(sum)
}

Score : 300 points

Problem Statement

In a public bath, there is a shower which emits water for T seconds when the switch is pushed.

If the switch is pushed when the shower is already emitting water, from that moment it will be emitting water for T seconds.
Note that it does not mean that the shower emits water for T additional seconds.

N people will push the switch while passing by the shower.
The i-th person will push the switch t_i seconds after the first person pushes it.

How long will the shower emit water in total?

Constraints

1 ≤ N ≤ 200,000

1 ≤ T ≤ 10^9

0 = t_1 < t_2 < t_3 < , ..., < t_{N-1} < t_N ≤ 10^9

T and each t_i are integers.

Input

Input is given from Standard Input in the following format:

N T
t_1 t_2 ... t_N

Output

Assume that the shower will emit water for a total of X seconds. Print X.

Sample Input 1

2 4
0 3

Sample Output 1

7

Three seconds after the first person pushes the water, the switch is pushed again and the shower emits water for four more seconds, for a total of seven seconds.

Sample Input 2

2 4
0 5

Sample Output 2

8

One second after the shower stops emission of water triggered by the first person, the switch is pushed again.

Sample Input 3

4 1000000000
0 1000 1000000 1000000000

Sample Output 3

2000000000

Sample Input 4

1 1
0

Sample Output 4

1

Sample Input 5

9 10
0 3 5 7 100 110 200 300 311

Sample Output 5

67


package main

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

func main() {
	sc := newScanner()
	N := sc.nextInt()
	W := sc.nextInt()
	var s [4][]int
	var w1 int
	var w, v int
	for i := 0; i < N; i++ {
		w = sc.nextInt()
		v = sc.nextInt()
		if i == 0 {
			w1 = w
		}
		s[w-w1] = append(s[w-w1], v)
	}
	for i := 0; i < 4; i++ {
		sort.Sort(sort.IntSlice(s[i]))
		s[i] = append([]int{0}, s[i]...)
		for j := 1; j < len(s[i]); j++ {
			s[i][j] += s[i][j-1]
		}
	}
	var ans int
	for i := 0; i < len(s[0]); i++ {
		for j := 0; j < len(s[1]); j++ {
			for k := 0; k < len(s[2]); k++ {
				for l := 0; l < len(s[3]); l++ {
					f := w1*i + (w1+1)*j + (w1+2)*k + (w1+3)*l
					if f <= W {
						ans = max(ans, s[0][i]+s[1][j]+s[2][k]+s[3][l])
					}
				}
			}
		}
	}
	fmt.Println(ans)
}

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

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

Score : 400 points

Problem Statement

You have N items and a bag of strength W.
The i-th item has a weight of w_i and a value of v_i.

You will select some of the items and put them in the bag.
Here, the total weight of the selected items needs to be at most W.

Your objective is to maximize the total value of the selected items.

Constraints

1 ≤ N ≤ 100

1 ≤ W ≤ 10^9

1 ≤ w_i ≤ 10^9

For each i = 2,3,...,N, w_1 ≤ w_i ≤ w_1 + 3.

1 ≤ v_i ≤ 10^7

W, each w_i and v_i are integers.

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible total value of the selected items.

Sample Input 1

4 6
2 1
3 4
4 10
3 4

Sample Output 1

11

The first and third items should be selected.

Sample Input 2

4 6
2 1
3 7
4 10
3 6

Sample Output 2

13

The second and fourth items should be selected.

Sample Input 3

4 10
1 100
1 100
1 100
1 100

Sample Output 3

400

You can take everything.

Sample Input 4

4 1
10 100
10 100
10 100
10 100

Sample Output 4

0

You can take nothing.


package main

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

var sc *bufio.Scanner

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

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

type item struct {
	w int
	v int
}

func main() {
	sc = bufio.NewScanner(os.Stdin)
	sc.Buffer(make([]byte, 0), 1000000001*3)
	sc.Split(bufio.ScanWords)
	n, W := nextInt(), nextInt()
	items := make([]item, n)
	for i := range items {
		w, v := nextInt(), nextInt()
		items[i] = item{w, v}
	}
	dp := make(map[int]int)
	dp[0] = 0
	for _, item := range items {
		dpNext := make(map[int]int)
		for w, v := range dp {
			wTmp, vTmp := w, v
			vNext, ok := dpNext[wTmp]
			if !ok || vTmp > vNext {
				dpNext[wTmp] = vTmp
			}

			wTmp, vTmp = w+item.w, v+item.v
			if wTmp > W {
				continue
			}
			vNext, ok = dpNext[wTmp]
			if !ok || vTmp > vNext {
				dpNext[wTmp] = vTmp
			}
		}
		dp = dpNext
	}

	ans := 0
	for _, v := range dp {
		if v > ans {
			ans = v
		}
	}
	fmt.Println(ans)
}


package main

import(
    "fmt"
    "strings"
)

func main() {
    var a,b,c string
    fmt.Scan(&a,&b,&c)
    fmt.Printf("%c%c%c",strings.ToUpper(a)[0],strings.ToUpper(b)[0],strings.ToUpper(c)[0])
}

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

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

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

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

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

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

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

func lcm(a, b int64) int64 {
    return a / gcd(a, b) * b
}

Score : 100 points

Problem Statement

You are given three words s_1, s_2 and s_3, each composed of lowercase English letters, with spaces in between.
Print the acronym formed from the uppercased initial letters of the words.

Constraints

s_1, s_2 and s_3 are composed of lowercase English letters.

1 ≤ |s_i| ≤ 10 (1≤i≤3)

Input

Input is given from Standard Input in the following format:

s_1 s_2 s_3

Output

Print the answer.

Sample Input 1

atcoder beginner contest

Sample Output 1

ABC

The initial letters of atcoder, beginner and contest are a, b and c. Uppercase and concatenate them to obtain ABC.

Sample Input 2

resident register number

Sample Output 2

RRN

Sample Input 3

k nearest neighbor

Sample Output 3

KNN

Sample Input 4

async layered coding

Sample Output 4

ALC


package main

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

func main() {
	n := getInt()

	as := make([]int, n)
	for i := range as {
		as[i] = getInt()
	}

	ans1 := solve(as, 1)
	ans2 := solve(as, 0)

	fmt.Println(min(ans1, ans2))
}

func solve(as []int, r int) int {
	ans := 0
	sum := 0
	for i, a := range as {
		if i%2 == r {
			if sum+a <= 0 {
				ans += abs(sum+a) + 1
				sum = 1
			} else {
				sum += a
			}
		} else {
			if sum+a >= 0 {
				ans += abs(sum+a) + 1
				sum = -1
			} else {
				sum += a
			}
		}
	}
	return ans
}

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

func min(ns ...int) int {
	min := ns[0]
	for i := 1; i < len(ns); i++ {
		if min > ns[i] {
			min = ns[i]
		}
	}
	return min
}

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

var scanner = wordScanner()

func wordScanner() *bufio.Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return sc
}

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

func getString() string {
	scanner.Scan()
	s := scanner.Text()
	return s
}

Score : 300 points

Problem Statement

You are given an integer sequence of length N. The i-th term in the sequence is a_i.
In one operation, you can select a term and either increment or decrement it by one.

At least how many operations are necessary to satisfy the following conditions?

For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.

For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.

Constraints

2 ≤ n ≤ 10^5

|a_i| ≤ 10^9

Each a_i is an integer.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print the minimum necessary count of operations.

Sample Input 1

4
1 -3 1 0

Sample Output 1

4

For example, the given sequence can be transformed into 1, -2, 2, -2 by four operations. The sums of the first one, two, three and four terms are 1, -1, 1 and -1, respectively, which satisfy the conditions.

Sample Input 2

5
3 -6 4 -5 7

Sample Output 2

0

The given sequence already satisfies the conditions.

Sample Input 3

6
-1 4 3 2 -5 4

Sample Output 3

8


package main

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

func solve(a []int64, ra int) int64 {
	s := int64(0) // S_i = \sum_i a[i]
	ans := int64(0)
	for i, e := range a {
		if i%2 == ra {
			if s+e >= 0 {
				ans += abs(-1 - s - e)
				s = -1
				continue
			}
		} else {
			if s+e <= 0 {
				ans += abs(1 - s - e)
				s = 1
				continue
			}
		}
		s += e
	}
	return ans
}

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

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

func main() {
	var n int
	fmt.Scan(&n)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		sc.Scan()
		x, _ := strconv.Atoi(sc.Text())
		a[i] = int64(x)
	}
	ans := int64(0)
	if a[0] == 0 {
		a[0] = -1
		ans = solve(a, 0)
		a[0] = 1
		ans = min(ans, solve(a, 1))
	} else if a[0] < 0 {
		ans = solve(a, 0)
	} else { // a[0] > 0
		ans = solve(a, 1)
	}
	fmt.Println(ans)
}


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 main() {
	sc.Split(bufio.ScanWords)
	n := nextInt()
	slice := make([]int, n)

	for i := 0; i < n; i++ {
		slice[i] = nextInt()
	}

	length := 1
	up := 0
	down := 0

	for i := 1; i < len(slice); i++ {
		if slice[i-1] < slice[i] {
			up = 1
		} else if slice[i-1] > slice[i] {
			down = 1
		} else {
			// i++
			continue
		}

		if up == 1 && down == 1 {
			up = 0
			down = 0

			length++
		}

	}

	fmt.Println(length)

	// for i := 0; i < len(slice); i++ {
	// 	// tmp := slice[start:len(slice)]
	// 	// fmt.Println(slice)

	// 	for j := 1; j < len(slice)-1; j++ {
	// 		// fmt.Printf("i=%d, j=%d\n", i, j)
	// 		if j+1 < n && slice[i] <= slice[j] {
	// 			continue
	// 		}

	// 		if j+1 < n && slice[i] >= slice[j] {
	// 			length++
	// 			i += j
	// 			break
	// 		}

	// 		if j+1 < n && slice[i] > slice[j] && slice[j] > slice[j+1] {
	// 			continue
	// 		}

	// 		if j+1 < n && slice[i] < slice[j] && slice[j] > slice[j+1] {
	// 			length++
	// 			i += j
	// 			break
	// 		}

	// 		if j+1 < n && slice[i] == slice[j] && slice[j] == slice[j+1] {
	// 			continue
	// 		}

	// 	}
	// fmt.Println(length)

	// }

	// fmt.Println(length)

}

Score : 300 points

Problem Statement

You are given an array A of length N.
Your task is to divide it into several contiguous subarrays.
Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order.
At least how many subarrays do you need to divide A into?

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Each A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible number of subarrays after division of A.

Sample Input 1

6
1 2 3 2 2 1

Sample Output 1

2

One optimal solution is to divide the array into [1,2,3] and [2,2,1].

Sample Input 2

9
1 2 1 2 1 2 1 2 1

Sample Output 2

5

Sample Input 3

7
1 2 3 2 1 999999999 1000000000

Sample Output 3

3


package main

import "fmt"

func main() {
	var a, b, c int
	fmt.Scan(&a, &b, &c)
	if b-a == c-b {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

Score : 100 points

Problem Statement

Three poles stand evenly spaced along a line. Their heights are a, b and c meters, from left to right.
We will call the arrangement of the poles beautiful if the tops of the poles lie on the same line, that is, b-a = c-b.

Determine whether the arrangement of the poles is beautiful.

Constraints

1 \leq a,b,c \leq 100

a, b and c are integers.

Input

Input is given from Standard Input in the following format:

a b c

Output

Print YES if the arrangement of the poles is beautiful; print NO otherwise.

Sample Input 1

2 4 6

Sample Output 1

YES

Since 4-2 = 6-4, this arrangement of poles is beautiful.

Sample Input 2

2 5 6

Sample Output 2

NO

Since 5-2 \neq 6-5, this arrangement of poles is not beautiful.

Sample Input 3

3 2 1

Sample Output 3

YES

Since 1-2 = 2-3, this arrangement of poles is beautiful.


package main

import "fmt"

func main() {
	var a, b, c int

	fmt.Scan(&a)
	fmt.Scan(&b)
	fmt.Scan(&c)

	if b-a == c-b {
		fmt.Print("YES")
	} else {
		fmt.Print("NO")
	}
}


package main

import (
	"fmt"
)

func main() {
	var o, e string
	fmt.Scan(&o, &e)

	for i := 0; i < len(o); i++ {
		if (len(o)-len(e)) == 1 && i == len(o)-1 {
			fmt.Println(string(o[i]))
		} else {
			fmt.Print(string(o[i]), string(e[i]))
		}
	}

}

Score : 200 points

Problem Statement

Snuke signed up for a new website which holds programming competitions.
He worried that he might forget his password, and he took notes of it.
Since directly recording his password would cause him trouble if stolen,
he took two notes: one contains the characters at the odd-numbered positions, and the other contains the characters at the even-numbered positions.

You are given two strings O and E. O contains the characters at the odd-numbered positions retaining their relative order, and E contains the characters at the even-numbered positions retaining their relative order.
Restore the original password.

Constraints

O and E consists of lowercase English letters (a - z).

1 \leq |O|,|E| \leq 50

|O| - |E| is either 0 or 1.

Input

Input is given from Standard Input in the following format:

O
E

Output

Print the original password.

Sample Input 1

xyz
abc

Sample Output 1

xaybzc

The original password is xaybzc. Extracting the characters at the odd-numbered positions results in xyz, and extracting the characters at the even-numbered positions results in abc.

Sample Input 2

atcoderbeginnercontest
atcoderregularcontest

Sample Output 2

aattccooddeerrbreeggiunlnaerrccoonntteesstt


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()
	m := [26]int{}
	for j := 0; j < 26; j++ {
		m[j] = 100
	}
	for i := 0; i < n; i++ {
		s := getString()
		x := [26]int{}
		for _, v := range s {
			x[int(v-'a')]++
		}
		for j := 0; j < 26; j++ {
			m[j] = min(m[j], x[j])
		}
	}

	for i := 0; i < 26; i++ {
		for j := 0; j < m[i]; j++ {
			fmt.Print(string(i + 'a'))
		}
	}
	out()
}

Score : 300 points

Problem Statement

Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.

He will receive a headline which contains one of the strings S_1,...,S_n tomorrow.
He is excited and already thinking of what string he will create.
Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.

Find the longest string that can be created regardless of which string among S_1,...,S_n the headline contains.
If there are multiple such strings, find the lexicographically smallest one among them.

Constraints

1 \leq n \leq 50

1 \leq |S_i| \leq 50 for every i = 1, ..., n.

S_i consists of lowercase English letters (a - z) for every i = 1, ..., n.

Input

Input is given from Standard Input in the following format:

n
S_1
...
S_n

Output

Print the lexicographically smallest string among the longest strings that satisfy the condition.
If the answer is an empty string, print an empty line.

Sample Input 1

3
cbaa
daacc
acacac

Sample Output 1

aac

The strings that can be created from each of cbaa, daacc and acacac, are aa, aac, aca, caa and so forth.
Among them, aac, aca and caa are the longest, and the lexicographically smallest of these three is aac.

Sample Input 2

3
a
aa
b

Sample Output 2

The answer is an empty string.


package main

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

var (
	readString func() string
)

func init() {
	readString = newReadString(os.Stdin)
}

type sortString []string

func (s sortString) Len() int {
	return len(s)
}

func (s sortString) Less(i, j int) bool {
	return len(s[i]) < len(s[j])
}

func (s sortString) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

type sortRunes []rune

func (s sortRunes) Less(i, j int) bool {
	return s[i] < s[j]
}

func (s sortRunes) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

func (s sortRunes) Len() int {
	return len(s)
}

func SortString(s string) string {
	r := []rune(s)
	sort.Sort(sortRunes(r))
	return string(r)
}

func main() {
	n := readInt()
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = readString()
	}

	// sort.Slice(s, func(i, j int) bool {
	// 	return len(s[i]) < len(s[j])
	// })
	sort.Sort(sortString(s))

	for i := range s {
		s[i] = SortString(s[i])
	}

	ans := ""
	for i := 0; i < len(s[0]); i++ {
		for ii := 1; ii < n; ii++ {
			if s[0][i] != s[ii][i] {
				break
			}
			if ii == n-1 {
				ans = ans + string(s[0][i])
			}
		}
	}

	fmt.Println(ans)
}

/*-------------------inputs-------------------*/

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

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

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

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

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

/*-------------------utilities-------------------*/

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


package main

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

var (
	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))
	r.Split(bufio.ScanWords)

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

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

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

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

func readLengthAndSlice() []int {
	n := readInt()
	return readIntSlice(n)
}

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 minInt(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func minUint(a, b uint) uint {
	if a < b {
		return a
	}
	return b
}

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

func minUint64(a, b uint64) uint64 {
	if a < b {
		return a
	}
	return b
}

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

func maxUint(a, b uint) uint {
	if a < b {
		return b
	}
	return a
}
func maxInt64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func maxUint64(a, b uint64) uint64 {
	if a < b {
		return b
	}
	return a
}

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

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

// readString() string
// readInt() int
// readIntSlice(n int) []int
// readLengthAndSlice() []int
const (
	P = 1000000007
)

func calc(x []int) int {
	res := 0
	n := len(x) - 1
	for i := 1; i <= n; i++ {
		l := x[i] - x[i-1]
		res = (res + l*i*(n-(i-1))) % P
	}
	return res
}

func main() {
	defer stdout.Flush()

	n := readInt()
	m := readInt()
	x := make([]int, n)
	y := make([]int, m)
	for i := range x {
		x[i] = readInt()
	}
	for i := range y {
		y[i] = readInt()
	}

	println(calc(x) * calc(y) % P)
}

Score : 500 points

Problem Statement

On a two-dimensional plane, there are m lines drawn parallel to the x axis, and n lines drawn parallel to the y axis.
Among the lines parallel to the x axis, the i-th from the bottom is represented by y = y_i.
Similarly, among the lines parallel to the y axis, the i-th from the left is represented by x = x_i.

For every rectangle that is formed by these lines, find its area, and print the total area modulo 10^9+7.

That is, for every quadruple (i,j,k,l) satisfying 1\leq i < j\leq n and 1\leq k < l\leq m, find the area of the rectangle formed by the lines x=x_i, x=x_j, y=y_k and y=y_l, and print the sum of these areas modulo 10^9+7.

Constraints

2 \leq n,m \leq 10^5

-10^9 \leq x_1 < ... < x_n \leq 10^9

-10^9 \leq y_1 < ... < y_m \leq 10^9

x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

n m
x_1 x_2 ... x_n
y_1 y_2 ... y_m

Output

Print the total area of the rectangles, modulo 10^9+7.

Sample Input 1

3 3
1 3 4
1 3 6

Sample Output 1

60

The following figure illustrates this input:

The total area of the nine rectangles A, B, ..., I shown in the following figure, is 60.

Sample Input 2

6 5
-790013317 -192321079 95834122 418379342 586260100 802780784
-253230108 193944314 363756450 712662868 735867677

Sample Output 2

835067060


package main

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

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

	sc := bufio.NewScanner(os.Stdin)

	powers := make([]int, 3*n)

	for i := 0; i < 3*n; i++ {
		power, _ := strconv.Atoi(sc.Text())
		powers[i] = power
	}

	sort.Ints(powers)
	powers = powers[n:]

	sum := 0
	for i := 0; i < n; i++ {
		sum += powers[2*i]
	}

	fmt.Println(sum)
}

Score : 300 points

Problem Statement

There are 3N participants in AtCoder Group Contest.
The strength of the i-th participant is represented by an integer a_i.
They will form N teams, each consisting of three participants.
No participant may belong to multiple teams.

The strength of a team is defined as the second largest strength among its members.
For example, a team of participants of strength 1, 5, 2 has a strength 2, and a team of three participants of strength 3, 2, 3 has a strength 3.

Find the maximum possible sum of the strengths of N teams.

Constraints

1 ≤ N ≤ 10^5

1 ≤ a_i ≤ 10^{9}

a_i are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the answer.

Sample Input 1

2
5 2 8 5 1 5

Sample Output 1

10

The following is one formation of teams that maximizes the sum of the strengths of teams:

Team 1: consists of the first, fourth and fifth participants.

Team 2: consists of the second, third and sixth participants.

Sample Input 2

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 2

10000000000

The sum of the strengths can be quite large.


package main

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

var sc = bufio.NewScanner(os.Stdin)

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

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

func nextIntSlice(size int) []int {
	re := make([]int, size)
	for i := 0; i < size; i++ {
		re[i] = nextInt()
	}
	return re
}

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

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

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

func main() {
	sc.Split(bufio.ScanWords)
	n, m := nextInt(), nextInt()
	a := make([]int, n)
	b := make([]int, n)
	c := make([]int, m)
	d := make([]int, m)
	for i := 0; i < n; i++ {
		a[i], b[i] = nextInt(), nextInt()
	}
	for i := 0; i < m; i++ {
		c[i], d[i] = nextInt(), nextInt()
	}

	for i := 0; i < n; i++ {
		minj := 0
		mindist := abs(a[i]-c[0]) + abs(b[i]-d[0])
		for j := 1; j < m; j++ {
			tempdist := abs(a[i]-c[j]) + abs(b[i]-d[j])
			if tempdist < mindist {
				minj = j
				mindist = tempdist
			}
		}
		fmt.Println(minj + 1)
	}
}

Score : 200 points

Problem Statement

There are N students and M checkpoints on the xy-plane.

The coordinates of the i-th student (1 \leq i \leq N) is (a_i,b_i), and the coordinates of the checkpoint numbered j (1 \leq j \leq M) is (c_j,d_j).

When the teacher gives a signal, each student has to go to the nearest checkpoint measured in Manhattan distance.

The Manhattan distance between two points (x_1,y_1) and (x_2,y_2) is |x_1-x_2|+|y_1-y_2|.

Here, |x| denotes the absolute value of x.

If there are multiple nearest checkpoints for a student, he/she will select the checkpoint with the smallest index.

Which checkpoint will each student go to?

Constraints

1 \leq N,M \leq 50

-10^8 \leq a_i,b_i,c_j,d_j \leq 10^8

All input values are integers.

Input

The input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_N b_N
c_1 d_1
:
c_M d_M

Output

Print N lines.

The i-th line (1 \leq i \leq N) should contain the index of the checkpoint for the i-th student to go.

Sample Input 1

2 2
2 0
0 0
-1 0
1 0

Sample Output 1

2
1

The Manhattan distance between the first student and each checkpoint is:

For checkpoint 1: |2-(-1)|+|0-0|=3

For checkpoint 2: |2-1|+|0-0|=1

The nearest checkpoint is checkpoint 2. Thus, the first line in the output should contain 2.

The Manhattan distance between the second student and each checkpoint is:

For checkpoint 1: |0-(-1)|+|0-0|=1

For checkpoint 2: |0-1|+|0-0|=1

When there are multiple nearest checkpoints, the student will go to the checkpoint with the smallest index. Thus, the second line in the output should contain 1.

Sample Input 2

3 4
10 10
-10 -10
3 3
1 2
2 3
3 5
3 5

Sample Output 2

3
1
2

There can be multiple checkpoints at the same coordinates.

Sample Input 3

5 5
-100000000 -100000000
-100000000 100000000
100000000 -100000000
100000000 100000000
0 0
0 0
100000000 100000000
100000000 -100000000
-100000000 100000000
-100000000 -100000000

Sample Output 3

5
4
3
2
1


package main

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

)

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

type ReaderEx struct {
	baseScanner *bufio.Scanner
	words       []string
	nextWordIdx func() int
	initWordIdx func()
}

func NewScannerEx(stdin io.Reader) *ReaderEx {
	var this ReaderEx
	var idx int
	this.baseScanner = bufio.NewScanner(stdin)
	this.baseScanner.Buffer(make([]byte, 0), 1E+11)
	this.nextWordIdx = func() int {
		cur := idx
		idx++
		return cur
	}
	this.initWordIdx = func() {
		idx = 0
	}
	return &this
}

func (me *ReaderEx) ScanAndSplit() (isScanned bool) {
	isScanned = me.baseScanner.Scan()

	me.words = strings.Fields(me.baseScanner.Text())
	me.initWordIdx()
	return
}

func (me *ReaderEx) Scan() (ok bool) {
	ok = me.baseScanner.Scan()
	return
}
func (me *ReaderEx) getString() string {
	return me.baseScanner.Text()
}

func (me *ReaderEx) getInt() int {
	n, err := strconv.Atoi(me.baseScanner.Text())
	defer func() {
		if err != nil {
			panic(err)
		}
	}()

	return n
}

func (me *ReaderEx) nextBytes() []byte {
	return []byte(me.words[me.nextWordIdx()])
}
func (me *ReaderEx) nextString() string {
	return me.words[me.nextWordIdx()]
}

type StringArray []string

func (me *ReaderEx) nextStringArray() StringArray {
	return me.words[me.nextWordIdx():]
}

func (me StringArray) toStrings() []string {
	return me
}

func (me StringArray) toInts() []int {
	strValues := me.toStrings()
	values := make([]int, len(strValues))
	for i, v := range strValues {
		n, err := strconv.Atoi(v)
		if err != nil {
			panic(err)
		}
		values[i] = n
	}
	return values
}
func (me *ReaderEx) nextInt() int {
	n, err := strconv.Atoi(me.words[me.nextWordIdx()])

	defer func() {
		if err != nil {
			panic(fmt.Sprintf("getInt(%d)", n))
		}
	}()
	return n
}

func pow(x, y, mod int) (ans int) {

	ans = 1
	for bit := 1 << uint(math.Floor(math.Log2(float64(y)))); ; bit >>= 1 {
		if y&bit != 0 {
			ans *= x
			if mod > 0 {
				ans %= mod
			}
		}
		if bit <= 1 {
			return
		}
		ans *= ans
		if mod > 0 {
			ans %= mod
		}
	}
}

type factType struct {
	prime int
	count int
}

func fact(n int) (facts []*factType) {

	for i := 2; i*i <= n; i++ {
		if n%i == 0 {
			fa := factType{prime: i}
			for n%fa.prime == 0 {
				n /= fa.prime
				fa.count++
			}
			facts = append(facts, &fa)
		}
	}
	if n > 1 {
		facts = append(facts, &factType{prime: n, count: 1})
	}
	return facts
}
func dec2bin(n int) (ret []bool) {
	for bit := 1 << uint(math.Floor(math.Log2(float64(n)))); ; bit >>= 1 {
		ret = append(ret, n&bit != 0)

		if bit <= 1 {
			return
		}
	}

}

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

func b2i(b bool) (i int) {
	if b {
		i = 1
	} else {
		i = 0
	}
	return
}

func solve(stdin io.Reader, stdout io.Writer) {

	r := NewScannerEx(stdin)
	r.Scan()
	n := r.getInt()
	fac := fact(n)

	facs := func(src []*factType) (dst []int) {
		dst = make([]int, 0, len(src))
		for _, value := range src {
			for i := 0; i < value.count; i++ {
				dst = append(dst, value.prime)
			}
		}
		return
	}(fac)

	maxF64 := func(a float64, vs ...float64) float64 {
		for _, v := range vs {
			if a < v {
				a = v
			}
		}
		return a
	}

	minI := func(a int, vs ...int) int {
		for _, v := range vs {
			if a > v {
				a = v
			}
		}
		return a
	}

	var f func(a int, b int, i int) int
	f = func(a int, b int, i int) int {
		if i < 0 {
			return int(math.Floor(maxF64(math.Log10(float64(a)), math.Log10(float64(b))))) + 1
		}

		return minI(f(a*facs[i], b, i-1), f(a, b*facs[i], i-1))

	}

	fmt.Fprintln(stdout, f(1, 1, len(facs)-1))

}

Score : 300 points

Problem Statement

You are given an integer N.

For two positive integers A and B, we will define F(A,B) as the larger of the following: the number of digits in the decimal notation of A, and the number of digits in the decimal notation of B.

For example, F(3,11) = 2 since 3 has one digit and 11 has two digits.

Find the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \times B.

Constraints

1 \leq N \leq 10^{10}

N is an integer.

Input

The input is given from Standard Input in the following format:

N

Output

Print the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \times B.

Sample Input 1

10000

Sample Output 1

3

F(A,B) has a minimum value of 3 at (A,B)=(100,100).

Sample Input 2

1000003

Sample Output 2

7

There are two pairs (A,B) that satisfy the condition: (1,1000003) and (1000003,1). For these pairs, F(1,1000003)=F(1000003,1)=7.

Sample Input 3

9876543210

Sample Output 3

6


package main

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

func main() {
	n := getInt()

	until := int(math.Ceil(math.Sqrt(float64(n))))
	ans := 10
	for i := 1; i <= until; i++ {
		if n%i == 0 {
			// ans = min(ans, f(n/i, i))
			ans = min(ans, len(strconv.Itoa(n/i)))
		}
	}

	fmt.Println(ans)
}

// func f(a, b int) int {
// 	maxLen := max(len(strconv.Itoa(a)), len(strconv.Itoa(b)))
// 	return maxLen
// }

func max(ns ...int) int {
	max := ns[0]
	for i := 1; i < len(ns); i++ {
		if max < ns[i] {
			max = ns[i]
		}
	}
	return max
}

func min(ns ...int) int {
	min := ns[0]
	for i := 1; i < len(ns); i++ {
		if min > ns[i] {
			min = ns[i]
		}
	}
	return min
}

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

var scanner = wordScanner()

func wordScanner() *bufio.Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return sc
}

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

func getString() string {
	scanner.Scan()
	s := scanner.Text()
	return s
}


package main

import (
	"bufio"
	"fmt"
	"math"
	"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()
}

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

	N := getInt()

	ans := int(math.Ceil(-1.0+math.Sqrt(1+8*float64(N))) / 2)

	out(ans)
}

Score : 200 points

Problem Statement

There is a kangaroo at coordinate 0 on an infinite number line that runs from left to right, at time 0.
During the period between time i-1 and time i, the kangaroo can either stay at his position, or perform a jump of length exactly i to the left or to the right.
That is, if his coordinate at time i-1 is x, he can be at coordinate x-i, x or x+i at time i.
The kangaroo's nest is at coordinate X, and he wants to travel to coordinate X as fast as possible.
Find the earliest possible time to reach coordinate X.

Constraints

X is an integer.

1≤X≤10^9

Input

The input is given from Standard Input in the following format:

X

Output

Print the earliest possible time for the kangaroo to reach coordinate X.

Sample Input 1

6

Sample Output 1

3

The kangaroo can reach his nest at time 3 by jumping to the right three times, which is the earliest possible time.

Sample Input 2

2

Sample Output 2

2

He can reach his nest at time 2 by staying at his position during the first second, and jumping to the right at the next second.

Sample Input 3

11

Sample Output 3

5


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)

	n := getNextInt(scanner)
	k := getNextInt64(scanner)
	aa := make([]int64, n)
	for i := 0; i < n; i++ {
		aa[i] = getNextInt64(scanner)
	}

	ans := 0
	imml := IntMapMap{}
	immr := IntMapMap{}
	imml.increment(-1, 0)
	immr.increment(n, 0)
	for i := 0; i < n; i++ {
		if aa[i] < k {
			imml.increment(i, aa[i])
		}
		for a := range imml[i-1] {
			imml.increment(i, a)
			if a+aa[i] < k {
				imml.increment(i, a+aa[i])
			}
		}
		if aa[n-i-1] < k {
			immr.increment(n-i-1, aa[n-i-1])
		}
		for a := range immr[n-i] {
			immr.increment(n-i-1, a)
			if a+aa[n-i-1] < k {
				immr.increment(n-i-1, a+aa[n-i-1])
			}
		}
	}

	for i := 0; i < n; i++ {
		if needed(i, n, k, aa, imml, immr) == false {
			ans++
		}
	}

	fmt.Fprintln(writer, ans)
	writer.Flush()
}

func needed(i, n int, k int64, aa []int64, imml, immr IntMapMap) bool {
	var sl, sr [5001]int
	var l int64

	for a := range imml[i-1] {
		sl[a]++
	}
	for a := range immr[i+1] {
		sr[a]++
	}
	for ii := 1; ii < 5001; ii++ {
		sr[ii] += sr[ii-1]
	}

	// (l+r) < k && (l+r)+aa[i] >= k

	for l = 0; l < k; l++ {
		if sl[l] == 0 {
			continue
		}
		if l+aa[i] >= k {
			return true
		}
		minr := k - l - aa[i] - 1
		maxr := k - l - 1
		if minr < 0 {
			continue
		}
		if maxr < 0 {
			continue
		}
		if (sr[maxr] - sr[minr]) > 0 {
			return true
		}
	}
	return false
}

// IntMap ...
type IntMap map[int64]int

func (m IntMap) get(key int64) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}

func (m IntMap) set(key int64, n int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key] = n
}

func (m IntMap) increment(key int64) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

// IntMapMap ...
type IntMapMap map[int]IntMap

func (m IntMapMap) get(key1 int, key2 int64) int {
	if _, ok := m[key1][key2]; ok {
		return m[key1][key2]
	}
	return 0
}

func (m IntMapMap) set(key1 int, key2 int64, n int) {
	if _, ok := m[key1]; ok == false {
		m[key1] = make(IntMap, 0)
	}
	m[key1].set(key2, n)
}

func (m IntMapMap) increment(key1 int, key2 int64) {
	if _, ok := m[key1]; ok == false {
		m[key1] = make(IntMap, 0)
	}
	m[key1].increment(key2)
}

Score : 600 points

Problem Statement

AtCoDeer the deer has N cards with positive integers written on them. The number on the i-th card (1≤i≤N) is a_i.
Because he loves big numbers, he calls a subset of the cards good when the sum of the numbers written on the cards in the subset, is K or greater.

Then, for each card i, he judges whether it is unnecessary or not, as follows:

If, for any good subset of the cards containing card i, the set that can be obtained by eliminating card i from the subset is also good, card i is unnecessary.

Otherwise, card i is NOT unnecessary.

Find the number of the unnecessary cards. Here, he judges each card independently, and he does not throw away cards that turn out to be unnecessary.

Constraints

All input values are integers.

1≤N≤5000

1≤K≤5000

1≤a_i≤10^9 (1≤i≤N)

Partial Score

300 points will be awarded for passing the test set satisfying N,K≤400.

Input

The input is given from Standard Input in the following format:

N K
a_1 a_2 ... a_N

Output

Print the number of the unnecessary cards.

Sample Input 1

3 6
1 4 3

Sample Output 1

1

There are two good sets: {2,3} and {1,2,3}.

Card 1 is only contained in {1,2,3}, and this set without card 1, {2,3}, is also good. Thus, card 1 is unnecessary.

For card 2, a good set {2,3} without card 2, {3}, is not good. Thus, card 2 is NOT unnecessary.

Neither is card 3 for a similar reason, hence the answer is 1.

Sample Input 2

5 400
3 1 4 1 5

Sample Output 2

5

In this case, there is no good set. Therefore, all the cards are unnecessary.

Sample Input 3

6 20
10 4 3 10 25 2

Sample Output 3

3


package main
import (
  "fmt"
  "os"
  "sort"
)
func main() {
  var n,k,s int
  fmt.Scan(&n,&k)
  var a []int = make([]int,n)
  for i:=0;i<n;i++ {
    fmt.Scan(&a[i])
    s += a[i]
  }
  sort.Ints(a)
  if s < k {
    fmt.Println(n)
    os.Exit(0)
  }
  if s-a[0] < k {
    fmt.Println(0)
    os.Exit(0)
  }
  var top,btm,mid int
  btm = 0
  top = n-1
  for top-btm > 1 {
    mid = (btm+top)/2
    if a[mid] >= k {
      top = mid
      continue
    }
    dp := make(map[int]struct{})
    dp[0] = struct{}{}
    for i:=0;i<n;i++ {
      if i == mid { continue }
      dp0 := make(map[int]struct{})
      for key,_ := range dp {
        dp0[key] = struct{}{}
        if key+a[i] < k { dp0[key+a[i]] = struct{}{} }
      }
      dp = dp0
    }
    for key,_ := range dp {
      if key >= k-a[mid] {
        top = mid
        break
      }
    }
    btm = mid
  }
  fmt.Println(btm)
}