package main

import (
	"fmt"
)

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

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

	result := "DENIED"

	for i := 0; i < n; i++ {
		if list[i]%2 == 0 {
			if list[i]%3 == 0 || list[i]%5 == 0 {
				result = "APPROVED"
			} else {
				result = "DENIED"
				fmt.Println(result)
				return
			}
		}
	}

	fmt.Println(result)
}

Score: 200 points

Problem Statement

You are an immigration officer in the Kingdom of AtCoder. The document carried by an immigrant has some number of integers written on it, and you need to check whether they meet certain criteria.

According to the regulation, the immigrant should be allowed entry to the kingdom if and only if the following condition is satisfied:

All even numbers written on the document are divisible by 3 or 5.

If the immigrant should be allowed entry according to the regulation, output APPROVED; otherwise, print DENIED.

Notes

The condition in the statement can be rephrased as "If x is an even number written on the document, x is divisible by 3 or 5".
Here "if" and "or" are logical terms.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq A_i \leq 1000

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

If the immigrant should be allowed entry according to the regulation, print APPROVED; otherwise, print DENIED.

Sample Input 1

5
6 7 9 10 31

Sample Output 1

APPROVED

The even numbers written on the document are 6 and 10.

All of them are divisible by 3 or 5, so the immigrant should be allowed entry.

Sample Input 2

3
28 27 24

Sample Output 2

DENIED

28 violates the condition, so the immigrant should not be allowed entry.


package main

import (
	"fmt"
)

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

	s := map[string]int{}
	max := 1
	for i := 0; i < n; i++ {
		var tmp string
		fmt.Scan(&tmp)
		_, exists := s[tmp]
		if !exists {
			s[tmp] = 1
		} else {
			s[tmp]++
			if max < s[tmp] {
				max = s[tmp]
			}
		}
	}

	for k, v := range s {
		if v == max {
			fmt.Println(k)
		}
	}
}

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

Score: 300 points

Problem Statement

We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.

Print all strings that are written on the most number of votes, in lexicographical order.

Constraints

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

S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.

The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print all strings in question in lexicographical order.

Sample Input 1

7
beat
vet
beet
bed
vet
bet
beet

Sample Output 1

beet
vet

beet and vet are written on two sheets each, while beat, bed, and bet are written on one vote each. Thus, we should print the strings beet and vet.

Sample Input 2

8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo

Sample Output 2

buffalo

Sample Input 3

7
bass
bass
kick
kick
bass
kick
kick

Sample Output 3

kick

Sample Input 4

4
ushi
tapu
nichia
kun

Sample Output 4

kun
nichia
tapu
ushi

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"
	"sort"
	"strconv"
	"strings"
)

func main() {
	n := readi()
	s := make(map[string]int, n)
	for i := 0; i < n; i++ {
		tmp := reads()
		s[tmp]++
	}
	a := List{}
	b := List{}
	for k, v := range s {
		e := Entry{k, v}
		a = append(a, e)
	}
	sort.Sort(a)

	var max int
	for i := len(a) - 1; i >= 0; i-- {
		if i == len(a)-1 {
			max = a[i].value
		}
		if a[i].value != max {
			break
		}
		e := Entry{a[i].name, a[i].value}
		b = append(b, e)
	}
	sort.Sort(b)
	for i := 0; i < len(b); i++ {
		fmt.Println(b[i].name)
	}
}

// Entry hoge
type Entry struct {
	name  string
	value int
}

// List hoge
type List []Entry

func (l List) Len() int {
	return len(l)
}

func (l List) Swap(i, j int) {
	l[i], l[j] = l[j], l[i]
}

func (l List) Less(i, j int) bool {
	if l[i].value == l[j].value {
		return (l[i].name < l[j].name)
	}
	return (l[i].value < l[j].value)
}

func mid(a []int) int {
	sort.Ints(a)
	return a[len(a)/2]
}

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

// IO---------------------------------------------------

func readi() (n int) {
	fmt.Scan(&n)
	return
}

func readf() (n float64) {
	fmt.Scan(&n)
	return
}

func reads() (n string) {
	fmt.Scan(&n)
	return
}

var rdr = bufio.NewReaderSize(os.Stdin, 1000000)

func readLine() string {
	buf := make([]byte, 0, 1000000)
	for {
		l, p, e := rdr.ReadLine()
		if e != nil {
			panic(e)
		}
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}

func readSlice() []string {
	return strings.Split(readLine(), " ")
}

func atoi(a []string) (b []int) {
	b = make([]int, len(a))
	for i := range a {
		b[i], _ = strconv.Atoi(a[i])
	}
	return
}

func atof(a []string) (b []float64) {
	b = make([]float64, len(a))
	for i := range a {
		b[i], _ = strconv.ParseFloat(a[i], 64)
	}
	return
}

func readSlice2(n int) (a []int, b []int) {
	a = make([]int, n)
	b = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i], &b[i])
	}
	return
}

func readFloatSlice2(n int) (a []float64, b []float64) {
	a = make([]float64, n)
	b = make([]float64, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i], &b[i])
	}
	return
}

func yesOrNo(b bool) {
	if b {
		fmt.Println("Yes")
		return
	}
	fmt.Println("No")
}

func yesorNO(b bool) {
	if b {
		fmt.Println("YES")
		return
	}
	fmt.Println("NO")
}

func yesorno(b bool) {
	if b {
		fmt.Println("yes")
		return
	}
	fmt.Println("no")
}

// calc---------------------------------------------------

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

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

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

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

func sum(a []int) (ans int) {
	for i := range a {
		ans += a[i]
	}
	return
}


package main

import (
	"fmt"
)

func main() {
	var s, t, u string
	var a, b int
	fmt.Scan(&s, &t)
	fmt.Scan(&a, &b)
	fmt.Scan(&u)

	if u == s {
		a--
	} else if u == t {
		b--
	}

	fmt.Println(a, b)
}

Score : 100 points

Problem Statement

We have A balls with the string S written on each of them and B balls with the string T written on each of them.

From these balls, Takahashi chooses one with the string U written on it and throws it away.

Find the number of balls with the string S and balls with the string T that we have now.

Constraints

S, T, and U are strings consisting of lowercase English letters.

The lengths of S and T are each between 1 and 10 (inclusive).

S \not= T

S=U or T=U.

1 \leq A,B \leq 10

A and B are integers.

Input

Input is given from Standard Input in the following format:

S T
A B
U

Output

Print the answer, with space in between.

Sample Input 1

red blue
3 4
red

Sample Output 1

2 4

Takahashi chose a ball with red written on it and threw it away.
Now we have two balls with the string S and four balls with the string T.

Sample Input 2

red blue
5 5
blue

Sample Output 2

5 4

Takahashi chose a ball with blue written on it and threw it away.
Now we have five balls with the string S and four balls with the string T.


package main

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

var sc = bufio.NewScanner(os.Stdin)

func main() {
	sc.Split(bufio.ScanWords)
	s := next()
	t := next()

	a := nextInt()
	b := nextInt()

	u := next()

	switch u {
	case s:
		a--
	case t:
		b--
	}

	fmt.Printf("%d %d\n", a, b)
}

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

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


package main

import (
	"fmt"
)

func main() {
	count := len(readLine())
	loop(0, count, func(i int) {
		fmt.Print("x")
	})
}

func loop(start, end int, f func(i int)) {
	for i := start; i < end; i++ {
		f(i)
	}
}

func readLine() string {
	var v string
	fmt.Scanln(&v)
	return v
}

Score : 200 points

Problem Statement

Given is a string S. Replace every character in S with x and print the result.

Constraints

S is a string consisting of lowercase English letters.

The length of S is between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Replace every character in S with x and print the result.

Sample Input 1

sardine

Sample Output 1

xxxxxxx

Replacing every character in S with x results in xxxxxxx.

Sample Input 2

xxxx

Sample Output 2

xxxx

Sample Input 3

gone

Sample Output 3

xxxx


package main

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

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

	var s string
	Fscan(in, &s)
	Fprintln(out, strings.Repeat("x", len(s)))
}

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


package main

import "fmt"

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

	for i := 0; i < len(s); i++ {
		fmt.Print("x")
	}
	fmt.Println()
}


package main

import (
	"fmt"
)

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

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

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if a[i] == a[j] && i != j {
				fmt.Println("Yes")
				return
			}
		}
	}
	fmt.Println("No")
}

Score : 300 points

Problem Statement

Given is a sequence of integers A_1, A_2, ..., A_N.
If its elements are pairwise distinct, print YES; otherwise, print NO.

Constraints

2 ≤ N ≤ 200000

1 ≤ A_i ≤ 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

If the elements of the sequence are pairwise distinct, print YES; otherwise, print NO.

Sample Input 1

5
2 6 1 4 5

Sample Output 1

YES

The elements are pairwise distinct.

Sample Input 2

6
4 1 3 1 6 2

Sample Output 2

NO

The second and fourth elements are identical.

Sample Input 3

2
10000000 10000000

Sample Output 3

NO


package main

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

var reader = bufio.NewReaderSize(os.Stdin, 1000000)
var writer = bufio.NewWriter(os.Stdout)
func NextLine() string {
  buffer := make([]byte, 0)
  for true {
    line, isPrefix, err := reader.ReadLine()
    if err != nil { panic(err) }
    buffer = append(buffer, line...)
    if !isPrefix { break }
  }
  return string(buffer)
}
func NextInt(A ...*int) {
  L := strings.Split(NextLine(), " ")
  for i, a := range A {
    *a, _ = strconv.Atoi(L[i])
  }
}
func NextIntVec(A *[]int) {
  L := strings.Split(NextLine(), " ")
  (*A) = make([]int, len(L))
  for i, l := range L {
    (*A)[i], _ = strconv.Atoi(l)
  }
}
func NextFloat(A ...*float64) {
  L := strings.Split(NextLine(), " ")
  for i, a := range A {
    *a, _ = strconv.ParseFloat(L[i], 64)
  }
}
func NextFloatVec(A *[]float64) {
  L := strings.Split(NextLine(), " ")
  (*A) = make([]float64, len(L))
  for i, l := range L {
    (*A)[i], _ = strconv.ParseFloat(l, 64)
  }
}
func Write(s interface{}) {
  fmt.Fprintln(writer, s)
}
func WriteIntVec(A []int) {
  S := make([]string, len(A))
  for i, a := range A {
    S[i] = strconv.Itoa(a)
  }
  Write(strings.Join(S, " "))
}
func Output() {
  _ = writer.Flush()
}

func Solve() {
  N := NextLine()
  var K int
  NextInt(&K)
  L := len(N)
  ans := 0
  C := make([][]int, len(N))
  for i := range C {
    C[i] = make([]int, 4)
    C[i][0] = 1
    C[i][1] = 9 * (L - 1 - i)
    C[i][2] = 81 * (L - 1 - i) * (L - 2 - i) / 2
    C[i][3] = 729 * (L - 1 - i) * (L - 2 - i) * (L - 3 - i) / 6
  }
  fixed := 0
  for i, s := range N {
    n := int(s) - int('0')
    if K < fixed { continue }
    ans += C[i][K - fixed]
    if fixed < K && 0 < n { ans += (n - 1) * C[i][K - fixed - 1] }
    fixed++
  }
  if fixed == K { ans++ }
  Write(ans)
  return
}

func main() {
  Solve()
  Output()
}

Score : 500 points

Problem Statement

Find the number of integers between 1 and N (inclusive) that contains exactly K non-zero digits when written in base ten.

Constraints

1 \leq N < 10^{100}

1 \leq K \leq 3

Input

Input is given from Standard Input in the following format:

N
K

Output

Print the count.

Sample Input 1

100
1

Sample Output 1

19

The following 19 integers satisfy the condition:

1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100

Sample Input 2

25
2

Sample Output 2

14

The following 14 integers satisfy the condition:

11,12,13,14,15,16,17,18,19,21,22,23,24,25

Sample Input 3

314159
2

Sample Output 3

937

Sample Input 4

9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
3

Sample Output 4

117879300


package main

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

var sc = bufio.NewScanner(os.Stdin)

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

func main() {
  sc.Split(bufio.ScanWords)
  h := next()
  a := next()

  count := h / a
  if h % a != 0 {
    count++
  }
  fmt.Println(count)
}

Score : 100 points

Problem Statement

Serval is fighting with a monster.

The health of the monster is H.

In one attack, Serval can decrease the monster's health by A.
There is no other way to decrease the monster's health.

Serval wins when the monster's health becomes 0 or below.

Find the number of attacks Serval needs to make before winning.

Constraints

1 \leq H \leq 10^4

1 \leq A \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H A

Output

Print the number of attacks Serval needs to make before winning.

Sample Input 1

10 4

Sample Output 1

3

After one attack, the monster's health will be 6.

After two attacks, the monster's health will be 2.

After three attacks, the monster's health will be -2.

Thus, Serval needs to make three attacks to win.

Sample Input 2

1 10000

Sample Output 2

1

Sample Input 3

10000 1

Sample Output 3

10000


package main

import "fmt"

func main() {
	var hp int
	var ap int
	var ans int
	fmt.Scan(&hp, &ap)
	ans = hp/ap + 1
	fmt.Print(ans)
}


package main

import "fmt"

func main() {
	var h = 0
	var a = 0
	fmt.Scan(&h, &a)

	fmt.Println( (h / a) + 1)
}


package main
import "fmt"
func main(){
    var h, n int
    fmt.Scan(&h,&n)
    var sum int
    for i := 0; i < n; i++{
        var a int
        fmt.Scan(&a)
        sum += a
    }
    if sum >= h{
        fmt.Println("Yes")
    }else{
        fmt.Println("No")
    }
}

Score : 200 points

Problem Statement

Raccoon is fighting with a monster.

The health of the monster is H.

Raccoon can use N kinds of special moves. Using the i-th move decreases the monster's health by A_i.
There is no other way to decrease the monster's health.

Raccoon wins when the monster's health becomes 0 or below.

If Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.

Constraints

1 \leq H \leq 10^9

1 \leq N \leq 10^5

1 \leq A_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H N
A_1 A_2 ... A_N

Output

If Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.

Sample Input 1

10 3
4 5 6

Sample Output 1

Yes

The monster's health will become 0 or below after, for example, using the second and third moves.

Sample Input 2

20 3
4 5 6

Sample Output 2

No

Sample Input 3

210 5
31 41 59 26 53

Sample Output 3

Yes

Sample Input 4

211 5
31 41 59 26 53

Sample Output 4

No


package main

import "fmt"

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

	sum := 0
	var a int
	for i := 0; i < n; i++ {
		fmt.Scan(&a)
		sum += a
	}

	if h <= sum {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}


package main

import (
	"fmt"
	"math"
)

func solve(H int, ans int, count int) int {

	if H == 0 {
		return ans
	}
	H = int(math.Floor(float64(H) / 2))
	ans += count
	count *= 2
	return solve(H, ans, count)
}

func main() {
	// Code for D - Caracal vs Monster
	var H int
	fmt.Scanf("%d", &H)

	fmt.Println(solve(H, 0, 1))
}

Score : 400 points

Problem Statement

Caracal is fighting with a monster.

The health of the monster is H.

Caracal can attack by choosing one monster. When a monster is attacked, depending on that monster's health, the following happens:

If the monster's health is 1, it drops to 0.

If the monster's health, X, is greater than 1, that monster disappears. Then, two new monsters appear, each with the health of \lfloor X/2 \rfloor.

(\lfloor r \rfloor denotes the greatest integer not exceeding r.)

Caracal wins when the healths of all existing monsters become 0 or below.

Find the minimum number of attacks Caracal needs to make before winning.

Constraints

1 \leq H \leq 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H

Output

Find the minimum number of attacks Caracal needs to make before winning.

Sample Input 1

2

Sample Output 1

3

When Caracal attacks the initial monster, it disappears, and two monsters appear, each with the health of 1.

Then, Caracal can attack each of these new monsters once and win with a total of three attacks.

Sample Input 2

4

Sample Output 2

7

Sample Input 3

1000000000000

Sample Output 3

1099511627775


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)
	h := iScan()
	ans := 0
	x := 1
	for h != 0 {
		ans += x
		x *= 2
		h /= 2
	}
	fmt.Println(ans)
}


package main

import (
	"fmt"
	"math"
)

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

	if H == 1 {
		H = 0
	}

	cnt := 1
	for {
		H = H / 2
		cnt++
		if H <= 1 {
			break
		}
	}
	fmt.Println(int(math.Pow(float64(2), float64(cnt))) - 1)
}


package main

import "fmt"

func main() {
	var H int
	EnemyCount := 1
	count := 0

	fmt.Scanf("%d", &H)
	EnemyHP := H

	for {
		if H > 1 {
			count += EnemyCount
			EnemyHP /= 2
			EnemyCount *= 2
			if EnemyHP == 0 {
				break
			}
		}

	}

	fmt.Printf("%d", count)

}


package main

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

type BIT [2][]int

func NewBIT(n int) *BIT {
	var b BIT
	for i := range b {
		b[i] = make([]int, n)
	}
	return &b
}

// [l,r)にxを加算
func (b *BIT) add(l, r, x int) {
	add_sub := func(p, idx, x int) {
		for idx < len(b[p]) {
			b[p][idx] += x
			idx += idx & (-idx)
		}
	}
	add_sub(0, l, -x*(l-1))
	add_sub(0, r, x*(r-1))
	add_sub(1, l, x)
	add_sub(1, r, -x)
}

// [l,r)の和
func (b *BIT) sum(l, r int) int {
	sum_sub := func(p, idx int) int {
		s := 0
		for idx > 0 {
			s += b[p][idx]
			idx -= idx & (-idx)
		}
		return s
	}
	return sum_sub(0, r-1) + sum_sub(1, r-1)*(r-1) - (sum_sub(0, l-1) + sum_sub(1, l-1)*(l-1))
}

const (
	initialBufSize = 100000
	maxBufSize     = 10000000
)

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

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

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

func puts(a ...interface{}) {
	fmt.Fprintln(wt, a...)
}

type Mon struct {
	x, h int
}

type Mons []Mon

func (a Mons) Len() int      { return len(a) }
func (a Mons) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a Mons) Less(i, j int) bool {
	return a[i].x < a[j].x
}

func upperBound(a []int, key int) int {
	l, r := 0, len(a)-1
	for l <= r {
		mid := (l + r) / 2
		if a[mid] <= key {
			l = mid + 1
		} else {
			r = mid - 1
		}
	}
	return l
}

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, initialBufSize), maxBufSize)
	defer wt.Flush()

	n, d, a := nextInt(), nextInt(), nextInt()
	mons := make(Mons, n)
	for i := 0; i < n; i++ {
		x, h := nextInt(), nextInt()
		mons[i] = Mon{x, h}
	}
	sort.Sort(mons)
	x, h := make([]int, n), make([]int, n)
	for i := range mons {
		x[i], h[i] = mons[i].x, mons[i].h
	}

	bit := NewBIT(n + 10)
	for i := 0; i < n; i++ {
		bit.add(i+1, i+2, h[i])
	}

	ans := 0
	for i := 0; i < n; i++ {
		cur := bit.sum(i+1, i+2)
		if cur <= 0 {
			continue
		}
		need := (cur + a - 1) / a
		r := upperBound(x, x[i]+2*d)
		bit.add(i+1, r+1, -need*a)
		ans += need
	}

	puts(ans)
}

Score : 600 points

Problem Statement

Silver Fox is fighting with N monsters.

The monsters are standing in a row, and we can assume them to be standing on a number line. The i-th monster, standing at the coordinate X_i, has the health of H_i.

Silver Fox can use bombs to attack the monsters.
Using a bomb at the coordinate x decreases the healths of all monsters between the coordinates x-D and x+D (inclusive) by A.
There is no way other than bombs to decrease the monster's health.

Silver Fox wins when all the monsters' healths become 0 or below.

Find the minimum number of bombs needed to win.

Constraints

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

0 \leq D \leq 10^9

1 \leq A \leq 10^9

0 \leq X_i \leq 10^9

1 \leq H_i \leq 10^9

X_i are distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N D A
X_1 H_1
:
X_N H_N

Output

Print the minimum number of bombs needed to win.

Sample Input 1

3 3 2
1 2
5 4
9 2

Sample Output 1

2

First, let us use a bomb at the coordinate 4 to decrease the first and second monsters' health by 2.

Then, use a bomb at the coordinate 6 to decrease the second and third monsters' health by 2.

Now, all the monsters' healths are 0.
We cannot make all the monsters' health drop to 0 or below with just one bomb.

Sample Input 2

9 4 1
1 5
2 4
3 3
4 2
5 1
6 2
7 3
8 4
9 5

Sample Output 2

5

We should use five bombs at the coordinate 5.

Sample Input 3

3 0 1
300000000 1000000000
100000000 1000000000
200000000 1000000000

Sample Output 3

3000000000

Watch out for overflow.


package main

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

func main() {
	run()
}

func run() {
	setSpace()

	m, n := readN(), readN()

	if m == n {
		fmt.Println("Yes")
		return
	}
	fmt.Println("No")
}

// ------以下、ユーティリティ------

var sc = bufio.NewScanner(os.Stdin)

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

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

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

Score : 100 points

Problem Statement

Takahashi is participating in a programming contest, AXC001. He has just submitted his code to Problem A.

The problem has N test cases, all of which must be passed to get an AC verdict.

Takahashi's submission has passed M cases out of the N test cases.

Determine whether Takahashi's submission gets an AC.

Constraints

1 \leq N \leq 100

0 \leq M \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

If Takahashi's submission gets an AC, print Yes; otherwise, print No.

Sample Input 1

3 3

Sample Output 1

Yes

All three test cases have been passed, so his submission gets an AC.

Sample Input 2

3 2

Sample Output 2

No

Only two out of the three test cases have been passed, so his submission does not get an AC.

Sample Input 3

1 1

Sample Output 3

Yes


package main

import (
	"fmt"
	"strconv"
)

func main() {
	var a, b int
	fmt.Scan(&a, &b)

	var A, B string
	for i := 0; i < b; i++ {
		A += strconv.Itoa(a)
	}
	for i := 0; i < a; i++ {
		B += strconv.Itoa(b)
	}

	if A < B {
		fmt.Println(A)
	} else {
		fmt.Println(B)
	}
}

Score : 200 points

Problem Statement

Given are 1-digit positive integers a and b. Consider these two strings: the concatenation of b copies of the digit a, and the concatenation of a copies of the digit b. Which of these is lexicographically smaller?

Constraints

1 \leq a \leq 9

1 \leq b \leq 9

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

Print the lexicographically smaller of the two strings. (If the two strings are equal, print one of them.)

Sample Input 1

4 3

Sample Output 1

3333

We have two strings 444 and 3333. Between them, 3333 is the lexicographically smaller.

Sample Input 2

7 7

Sample Output 2

7777777


package main

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

func main() {
	run()
}

func run() {
	setSpace()
	a, b := readN(), readN()
	s, l := 0, 0
	if a < b {
		s, l = a, b
	} else {
		s, l = b, a
	}
	for i := 0; i < l; i++ {
		fmt.Printf(strconv.Itoa(s))
	}
	fmt.Println("")
}

// ------以下、ユーティリティ------

var sc = bufio.NewScanner(os.Stdin)

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

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

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


package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	fmt.Println(resolve(parseProblem()))
}

func parseProblem() int {
	n := scanInt(sc)
	count, min := 0, math.MaxInt32
	for i := 0; i < n; i++ {
		v := scanInt(sc)
		if v < min {
			count++
			min = v
		}
	}
	return count
}

func resolve(n int) int {
	return n
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}

Score : 300 points

Problem Statement

Given is a permutation P_1, \ldots, P_N of 1, \ldots, N.
Find the number of integers i (1 \leq i \leq N) that satisfy the following condition:

For any integer j (1 \leq j \leq i), P_i \leq P_j.

Constraints

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

P_1, \ldots, P_N is a permutation of 1, \ldots, N.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 ... P_N

Output

Print the number of integers i that satisfy the condition.

Sample Input 1

5
4 2 5 1 3

Sample Output 1

3

i=1, 2, and 4 satisfy the condition, but i=3 does not - for example, P_i > P_j holds for j = 1.

Similarly, i=5 does not satisfy the condition, either. Thus, there are three integers that satisfy the condition.

Sample Input 2

4
4 3 2 1

Sample Output 2

4

All integers i (1 \leq i \leq N) satisfy the condition.

Sample Input 3

6
1 2 3 4 5 6

Sample Output 3

1

Only i=1 satisfies the condition.

Sample Input 4

8
5 7 4 2 6 8 1 3

Sample Output 4

4

Sample Input 5

1
1

Sample Output 5

1


package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
var sc = bufio.NewScanner(os.Stdin)

func main() {
	var s1,s2 string
	if sc.Scan() {
        s1 = sc.Text()
    }
    if sc.Scan() {
        s2 = sc.Text()
	}
	nums, _ := strconv.Atoi(s1)
	slice := strings.Split(s2, " ")
	inputNum := stringToint(slice)

	truenums := 0
	min := inputNum[0]
	for i := 0; i < nums; i++ {
		if min >= inputNum[i] {
			min = inputNum[i]
			truenums++
		}
	}
	fmt.Println(truenums)
}

func stringToint(s []string) []int {
	f := make([]int, len(s))
	for n := range s {
		f[n], _ = strconv.Atoi(s[n])
	}
	return f
}


package main

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

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

	for i := 1; i < n+1; i++ {
		nb := []byte(strconv.Itoa(i))
		s, _ := strconv.Atoi(string(nb[0]))
		e, _ := strconv.Atoi(string(nb[len(nb)-1]))
		a[s][e]++
	}

	ans := 0
	for i := range a {
		for j := range a {
			ans += a[i][j] * a[j][i]
		}
	}
	fmt.Println(ans)
}

func main1() {
	n := readInt()
	nstr := strconv.Itoa(n)
	nlen := len(nstr)
	nstart, nmid, nend := n, 1, n
	if nlen > 1 {
		nstart, _ = strconv.Atoi(nstr[0:1])
		nend, _ = strconv.Atoi(nstr[nlen-1:])
	}
	if nlen > 2 {
		nmid, _ = strconv.Atoi(nstr[1 : nlen-1])
	}

	ans := 0
	for i := 1; i <= n; i++ {
		if i%10 == 0 {
			continue
		}

		a := strconv.Itoa(i)
		s, _ := strconv.Atoi(a[len(a)-1:])
		e, _ := strconv.Atoi(a[0:1])

		if s == e {
			ans++
		}

		for j := 0; j <= nlen-2; j++ {
			if j < nlen-2 {
				ans += int(math.Pow(10, float64(j)))
				continue
			}
			if s > nstart {
				break
			} else if s < nstart {
				ans += int(math.Pow(10, float64(j)))
			} else {
				if e <= nend {
					ans += nmid
				} else {
					ans += max(0, nmid-1)
				}
			}
		}
	}

	fmt.Println(ans)
}

// sort ------------------------------------------------------------

type xxx struct {
	x int
}

type sortArray []xxx

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

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

var (
	readString func() string
	readBytes  func() []byte
)

func init() {
	log.SetFlags(log.Lshortfile)
	readString, readBytes = newReadString(os.Stdin)
}

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

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

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

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

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

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

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

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

func sum(a []int) int {
	var ret int
	for i := range a {
		ret += a[i]
	}
	return ret
}

func sumFloat64(a []float64) float64 {
	var ret float64
	for i := range a {
		ret += a[i]
	}
	return ret
}

func gcd(m, n int) int {
	for m%n != 0 {
		m, n = n, m%n
	}
	return n
}

func lcm(m, n int) int {
	return m / gcd(m, n) * n
}

Score : 400 points

Problem Statement

Given is a positive integer N.

Find the number of pairs (A, B) of positive integers not greater than N that satisfy the following condition:

When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B.

Constraints

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

25

Sample Output 1

17

The following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21), (21,12), (22,2), and (22,22).

Sample Input 2

1

Sample Output 2

1

Sample Input 3

100

Sample Output 3

108

Sample Input 4

2020

Sample Output 4

40812

Sample Input 5

200000

Sample Output 5

400000008


package main

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

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

const (
	initialBufSize = 10e4
	maxBufSize     = 10e6
)

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

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

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

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

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

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

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

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

var mod = 1000000007

func mul(a int, b int) int {
	return ((a % mod) * (b % mod)) % mod
}

func power(x int, y int) int {
	if y == 0 {
		return 1
	} else if y%2 == 0 {
		ans := power(x, y/2) % mod
		ans = mul(ans, ans)
		return ans
	} else {
		ans := power(x, y/2) % mod
		ans = mul(mul(ans, ans), x)
		return ans
	}
}

// main
func main() {
	buf := make([]byte, initialBufSize)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords)
	n := nextInt()
	a := nextInts(n)
	var ans int
	m := map[int]int{}
	miList := make([]map[int]int, n)
	for i := 0; i < n; i++ {
		mi := map[int]int{}
		res := a[i]
		for j := 2; j <= int(math.Sqrt(float64(a[i]))); j++ {
			if res%j == 0 {
				c := 0
				for res%j == 0 {
					c++
					mi[j]++
					res = res / j
				}
				if c > m[j] {
					m[j] = c
				}
			}
		}
		if res == a[i] {
			mi[res] = 1
			_, exist := m[res]
			if !exist {
				m[res] = 1
			}
		}
		miList[i] = mi
	}
	fmt.Println("m", m)
	for i := 0; i < n; i++ {
		b := 1
		mi := miList[i]
		fmt.Println("mi", mi)
		for x, c := range m {
			b = mul(b, power(x, (c-mi[x])))
		}
		ans = (ans + b) % mod
	}
	fmt.Println(ans)
}

Score : 500 points

Problem Statement

Given are N positive integers A_1,...,A_N.

Consider positive integers B_1, ..., B_N that satisfy the following condition.

Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.

Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.

Since the answer can be enormous, print the sum modulo (10^9 +7).

Constraints

1 \leq N \leq 10^4

1 \leq A_i \leq 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).

Sample Input 1

3
2 3 4

Sample Output 1

13

Let B_1=6, B_2=4, and B_3=3, and the condition will be satisfied.

Sample Input 2

5
12 12 12 12 12

Sample Output 2

5

We can let all B_i be 1.

Sample Input 3

3
1000000 999999 999998

Sample Output 3

996989508

Print the sum modulo (10^9+7).


package main

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

var sc *bufio.Scanner

func init() {
	// 100MB
	bufsize := 100 * 1024 * 1024
	sc = bufio.NewScanner(os.Stdin)
	sc.Buffer(make([]byte, bufio.MaxScanTokenSize), bufsize)
	sc.Split(bufio.ScanWords)
}
func readInt() int              { return readIntArray(1)[0] }
func readInt2() (int, int)      { a := readIntArray(2); return a[0], a[1] }
func readInt3() (int, int, int) { a := readIntArray(3); return a[0], a[1], a[2] }
func readIntArray(n int) []int {
	s := readStringArray(n)
	a := make([]int, n)
	for i := range a {
		a[i], _ = strconv.Atoi(s[i])
	}
	return a
}
func readString() string            { return readStringArray(1)[0] }
func readString2() (string, string) { a := readStringArray(2); return a[0], a[1] }
func readStringArray(n int) []string {
	a := make([]string, n)
	for i := 0; i < n; i++ {
		sc.Scan()
		a[i] = sc.Text()
	}
	return a
}
func max(ns ...int) int {
	max := ns[0]
	for _, n := range ns {
		if n > max {
			max = n
		}
	}
	return max
}
func min(ns ...int) int {
	min := ns[0]
	for _, n := range ns {
		if n < min {
			min = n
		}
	}
	return min
}
func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func main() {
	N := readInt()
	A := readIntArray(N)

	l := lcm(A...)
	B := make([]int, N)
	for i := range A {
		B[i] = l / A[i]
	}
	var result int
	d := 1000000007
	for i := range B {
		result += B[i] % d
		result %= d
	}
	fmt.Println(result)
}

func lcm(ns ...int) int {
	l := ns[0]
	for i := range ns {
		if i == 0 {
			continue
		}

		if l == ns[i] {
			continue
		}

		g := gcd(l, ns[i])
		l = l * ns[i] / g
	}

	return l
}

func gcd(m, n int) int {
	if m < n {
		m, n = n, m
	}
	for m%n != 0 {
		m, n = n, m%n
	}
	return n
}

//func lcm(ns ...int) int {
//	lcm := ns[0]
//	for i := range ns {
//		if i == 0 {
//			continue
//		}
//
//		target := ns[i]
//		targetBase := target
//		lcmBase := lcm
//		for {
//			if lcm == target {
//				break
//			}
//			if target < lcm {
//				target += targetBase
//			} else {
//				lcm += lcmBase
//			}
//		}
//	}
//
//	return lcm
//}


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 getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 3
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)

	g := make([][]Edge, n)

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

	m := getNextInt(scanner)
	visited := make([][]int, m)
	ee := make([]int64, m)
	var ans, horobiyo int64
	for i := 0; i < m; i++ {
		u := getNextInt(scanner) - 1
		v := getNextInt(scanner) - 1

		visited[i] = make([]int, n-1)
		dfs(u, -1, v, visited[i], g)
		for j := 0; j < n-1; j++ {
			if visited[i][j] == 0 {
				continue
			}
			ee[i] |= 1 << uint(j)
		}
	}
	for i := 0; i < 1<<uint(m); i++ {
		horobiyo = 0
		c := 0
		for j := 0; j < m; j++ {
			if i>>uint(j)&1 == 0 {
				continue
			}
			horobiyo |= ee[j]
			c++
		}
		d := n - 1
		for j := 0; j < n-1; j++ {
			d -= int(horobiyo >> uint(j) & 1)
		}
		if c%2 == 0 {
			ans += 1 << uint(d)
			continue
		}
		ans -= 1 << uint(d)
	}
	fmt.Fprintln(writer, ans)
}

func dfs(i, p, v int, visited []int, g [][]Edge) int {
	if i == v {
		return 1
	}
	r := 0

	for _, e := range g[i] {
		if e.to == p {
			continue
		}
		x := dfs(e.to, i, v, visited, g)
		if x == 1 {
			visited[e.id] = 1
			r = x
		}
	}

	return r
}

// Edge ...
type Edge struct {
	to, id int
}

Score : 600 points

Problem Statement

We have a tree with N vertices numbered 1 to N.
The i-th edge in this tree connects Vertex a_i and Vertex b_i.

Consider painting each of these edges white or black. There are 2^{N-1} such ways to paint the edges. Among them, how many satisfy all of the following M restrictions?

The i-th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i, which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black.

Constraints

2 \leq N \leq 50

1 \leq a_i,b_i \leq N

The graph given in input is a tree.

1 \leq M \leq \min(20,\frac{N(N-1)}{2})

1 \leq u_i < v_i \leq N

If i \not= j, either u_i \not=u_j or v_i\not=v_j

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}
M
u_1 v_1
:
u_M v_M

Output

Print the number of ways to paint the edges that satisfy all of the M conditions.

Sample Input 1

3
1 2
2 3
1
1 3

Sample Output 1

3

The tree in this input is shown below:

All of the M restrictions will be satisfied if Edge 1 and 2 are respectively painted (white, black), (black, white), or (black, black), so the answer is 3.

Sample Input 2

2
1 2
1
1 2

Sample Output 2

1

The tree in this input is shown below:

All of the M restrictions will be satisfied only if Edge 1 is painted black, so the answer is 1.

Sample Input 3

5
1 2
3 2
3 4
5 3
3
1 3
2 4
2 5

Sample Output 3

9

The tree in this input is shown below:

Sample Input 4

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

Sample Output 4

62

The tree in this input is shown below:


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)
	x := larger(iScan(), iScan())
	n := iScan()
	fmt.Println((n + x - 1) / x)
}

Score : 100 points

Problem Statement

We have a grid with H rows and W columns, where all the squares are initially white.

You will perform some number of painting operations on the grid.
In one operation, you can do one of the following two actions:

Choose one row, then paint all the squares in that row black.

Choose one column, then paint all the squares in that column black.

At least how many operations do you need in order to have N or more black squares in the grid?
It is guaranteed that, under the conditions in Constraints, having N or more black squares is always possible by performing some number of operations.

Constraints

1 \leq H \leq 100

1 \leq W \leq 100

1 \leq N \leq H \times W

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H
W
N

Output

Print the minimum number of operations needed.

Sample Input 1

3
7
10

Sample Output 1

2

You can have 14 black squares in the grid by performing the "row" operation twice, on different rows.

Sample Input 2

14
12
112

Sample Output 2

8

Sample Input 3

2
100
200

Sample Output 3

2


package main

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

type eadge struct {
	start int
	end   int
}

type eadgeArray []eadge

func (a eadgeArray) Len() int {
	return len(a)
}

func (a eadgeArray) Less(i, j int) bool {
	return a[i].start < a[j].start
}

func (a eadgeArray) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	eadges := []eadge{}
	for scanner.Scan() {
		array := strings.Split(scanner.Text(), " ")
		robot, _ := strconv.Atoi(array[0])
		arm, _ := strconv.Atoi(array[1])
		var s int
		if robot < arm {
			s = 0
		} else {
			s = robot - arm
		}
		e := eadge{
			s,
			robot + arm - 1,
		}
		eadges = append(eadges, e)
	}
	//sort
	sort.Sort(eadgeArray(eadges))

	cnt := 0
	prev := eadges[0]
	eadges = eadges[1:]
	for _, e := range eadges {
		if prev.end < e.start {
			//no overlap
			prev = e
			cnt++
			continue
		}
		//overlap
		if prev.end < e.end {
			//keep pre
			continue
		}
		//change
		prev = e
	}
	cnt++
	fmt.Println(cnt)
}

Score : 200 points

Problem Statement

In a factory, there are N robots placed on a number line.
Robot i is placed at coordinate X_i and can extend its arms of length L_i in both directions, positive and negative.

We want to remove zero or more robots so that the movable ranges of arms of no two remaining robots intersect.
Here, for each i (1 \leq i \leq N), the movable range of arms of Robot i is the part of the number line between the coordinates X_i - L_i and X_i + L_i, excluding the endpoints.

Find the maximum number of robots that we can keep.

Constraints

1 \leq N \leq 100,000

0 \leq X_i \leq 10^9 (1 \leq i \leq N)

1 \leq L_i \leq 10^9 (1 \leq i \leq N)

If i \neq j, X_i \neq X_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 L_1
X_2 L_2
\vdots
X_N L_N

Output

Print the maximum number of robots that we can keep.

Sample Input 1

4
2 4
4 3
9 3
100 5

Sample Output 1

3

By removing Robot 2, we can keep the other three robots.

Sample Input 2

2
8 20
1 10

Sample Output 2

1

Sample Input 3

5
10 1
2 1
4 1
6 1
8 1

Sample Output 3

5


package main

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

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

type point struct {
	x, l, r int
}

type ps []point

func (p ps) Len() int {
	return len(p)
}
func (p ps) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}
func (p ps) Less(i, j int) bool {
	return p[i].r < p[j].r
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	N := nextInt(sc)
	points := make([]point, N)
	for i := 0; i < N; i++ {
		points[i].x = nextInt(sc)
		L := nextInt(sc)
		points[i].l = points[i].x - L
		points[i].r = points[i].x + L
	}
	sort.Sort(ps(points))
	// 区間スケジューリング問題に帰着する
	start := points[0].r
	ans := 1
	for i := 0; i < len(points); i++ {
		if points[i].l < start {
			continue
		}
		start = points[i].r
		ans++
	}
	fmt.Println(ans)
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

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

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

func main() {
	var c string
	fmt.Scanf("%s", &c)
	fmt.Printf("%s", string(c[0] + 1))
}

Score : 100 points

Problem Statement

Given is a lowercase English letter C that is not z. Print the letter that follows C in alphabetical order.

Constraints

C is a lowercase English letter that is not z.

Input

Input is given from Standard Input in the following format:

C

Output

Print the letter that follows C in alphabetical order.

Sample Input 1

a

Sample Output 1

b

a is followed by b.

Sample Input 2

y

Sample Output 2

z

y is followed by z.


package main

import (
	"fmt"
)

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

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

	ac := 0
	wa := 0
	flagAC := make([]bool, n+1)

	for i := 0; i < m; i++ {
		if flagAC[listp[i]] == false {
			if listS[i] == "WA" {
				wa++
			} else if listS[i] == "AC" {
				ac++
				flagAC[listp[i]] = true
			}
		}

	}

	fmt.Println(ac, wa)

}

Score : 300 points

Problem Statement

Takahashi participated in a contest on AtCoder.

The contest had N problems.

Takahashi made M submissions during the contest.

The i-th submission was made for the p_i-th problem and received the verdict S_i (AC or WA).

The number of Takahashi's correct answers is the number of problems on which he received an AC once or more.

The number of Takahashi's penalties is the sum of the following count for the problems on which he received an AC once or more: the number of WAs received before receiving an AC for the first time on that problem.

Find the numbers of Takahashi's correct answers and penalties.

Constraints

N, M, and p_i are integers.

1 \leq N \leq 10^5

0 \leq M \leq 10^5

1 \leq p_i \leq N

S_i is AC or WA.

Input

Input is given from Standard Input in the following format:

N M
p_1 S_1
:
p_M S_M

Output

Print the number of Takahashi's correct answers and the number of Takahashi's penalties.

Sample Input 1

2 5
1 WA
1 AC
2 WA
2 AC
2 WA

Sample Output 1

2 2

In his second submission, he received an AC on the first problem for the first time. Before this, he received one WA on this problem.

In his fourth submission, he received an AC on the second problem for the first time. Before this, he received one WA on this problem.

Thus, he has two correct answers and two penalties.

Sample Input 2

100000 3
7777 AC
7777 AC
7777 AC

Sample Output 2

1 0

Note that it is pointless to get an AC more than once on the same problem.

Sample Input 3

6 0

Sample Output 3

0 0


package main

import (
	"fmt"
	"strconv"
)

func main() {

	// 読み込み数取得
	var n, m int
	fmt.Scan(&n, &m)

	l := make([][]string, m)
	for i := 0; i < m; i++ {
		l[i] = make([]string, 2)
		fmt.Scan(&l[i][0], &l[i][1])
	}

	answer := 0
	answer_flag := false
	penalty := 0
	question_count := 0
	last_question := 0
	last_answer := ""
	for i := 0; i < m; i++ {
		q, _ := strconv.Atoi(l[i][0])

		// 回答番号が違っていたらanswer_flagを初期化
		if q != last_question {
			answer_flag = false
			last_question = q
			question_count++
		}

		// 正解が出たらカウントアップ
		if l[i][1] == "AC" && answer_flag == false {
			answer++
			answer_flag = true
		}

		// まだ正解が出ておらず不正解ならペナルティを増やす
		if l[i][1] == "WA" && answer_flag == false {
			penalty++
		}

		last_answer = l[i][1]

		// ACで問題が最終番号だったらループ抜ける
		if question_count == n && last_answer == "AC" {
			break
		}
	}

	fmt.Printf("%d %d", answer, penalty)

}


package main

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

func StrStdin() string {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	return strings.TrimSpace(scanner.Text())
}

func Int64Stdin() int64 {
	stringInput := StrStdin()
	num, _ := strconv.ParseInt(strings.TrimSpace(stringInput), 10, 64)
	return num
}

func SplitWithoutEmpty(stringTargeted string, delim string) []string {
	stringSplited := strings.Split(stringTargeted, delim)
	strs := []string{}

	for _, str := range stringSplited {
		if str != "" {
			strs = append(strs, str)
		}
	}
	return strs
}

func SliceStrStdin(delim string) []string {
	stringInput := StrStdin()
	return SplitWithoutEmpty(stringInput, delim)
}

func SliceInt64Stdin() []int64 {
	stringSplited := SliceStrStdin(" ")

	uint64Slice := []int64{}

	for ni := range stringSplited {
		num, _ := strconv.ParseInt(stringSplited[ni], 10, 64)
		uint64Slice = append(uint64Slice, num)
	}

	return uint64Slice
}

type Maze struct {
	maze []string
}

var (
	H, W int64
	m    Maze
	dist [30][30]int64
	dy   = [4]int64{0, 1, 0, -1}
	dx   = [4]int64{1, 0, -1, 0}
)

type Point struct {
	y int64
	x int64
}

func BFS(y, x int64) int64 {

	que := []Point{}
	que = append(que, Point{y: y, x: x})
	var ans int64
	ans = 0

	for hy := 0; int64(hy) < H; hy++ {
		for wx := 0; int64(wx) < W; wx++ {
			dist[hy][wx] = 0
		}
	}

	for len(que) > 0 {

		q := que[0]
		que = que[1:]
		for dir := 0; dir < len(dy); dir++ {
			ny := q.y + dy[dir]
			nx := q.x + dx[dir]

			if (0 <= ny && int64(ny) < H) && (0 <= nx && int64(nx) < W) {
				if m.maze[ny][nx] != '.' {
					continue
				}
				if dist[ny][nx] > 0 {
					continue
				}
				que = append(que, Point{y: ny, x: nx})
				dist[ny][nx] = dist[q.y][q.x] + 1
				ans = int64(math.Max(float64(ans), float64(dist[ny][nx])))
			}
		}
	}

	return ans
}

func Solve() {
	HW := SliceInt64Stdin()
	H, W = HW[0], HW[1]

	for hy := 0; int64(hy) < H; hy++ {
		m.maze = append(m.maze, StrStdin())
	}

	var res int64
	res = 0
	for hy := 0; int64(hy) < H; hy++ {
		for wx := 0; int64(wx) < W; wx++ {
			if m.maze[hy][wx] == '.' {
				res = int64(math.Max(float64(res), float64(BFS(int64(hy), int64(wx)))))
			}
		}
	}

	fmt.Printf("%d", res)
}

func main() {
	Solve()
	return
}

Score : 400 points

Problem Statement

Takahashi has a maze, which is a grid of H \times W squares with H horizontal rows and W vertical columns.

The square at the i-th row from the top and the j-th column is a "wall" square if S_{ij} is #, and a "road" square if S_{ij} is ..

From a road square, you can move to a horizontally or vertically adjacent road square.

You cannot move out of the maze, move to a wall square, or move diagonally.

Takahashi will choose a starting square and a goal square, which can be any road squares, and give the maze to Aoki.

Aoki will then travel from the starting square to the goal square, in the minimum number of moves required.

In this situation, find the maximum possible number of moves Aoki has to make.

Constraints

1 \leq H,W \leq 20

S_{ij} is . or #.

S contains at least two occurrences of ..

Any road square can be reached from any road square in zero or more moves.

Input

Input is given from Standard Input in the following format:

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

Output

Print the maximum possible number of moves Aoki has to make.

Sample Input 1

3 3
...
...
...

Sample Output 1

4

If Takahashi chooses the top-left square as the starting square and the bottom-right square as the goal square, Aoki has to make four moves.

Sample Input 2

3 5
...#.
.#.#.
.#...

Sample Output 2

10

If Takahashi chooses the bottom-left square as the starting square and the top-right square as the goal square, Aoki has to make ten moves.


package main

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

const (
	initialBufSize = 10000
	maxBufSize     = 1000000
)

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

func getMaxIndex(S [][]string, startX, startY int) (int, int, int) {
	H := len(S)
	W := len(S[0])

	// costMapの初期化
	costMap := make([][]int, H)
	for i := 0; i < H; i++ {
		costMap[i] = make([]int, W)

		for j := 0; j < W; j++ {
			costMap[i][j] = -1
		}
	}

	costMap[startY][startX] = 0

	// costMapの更新
	max := 0
	maxX := 0
	maxY := 0

	for ; ; {
		changeCnt := 0

		for i := 0; i < H; i++ {
			for j := 0; j < W; j++ {
				if S[i][j] == "#" {
					continue
				}

				if costMap[i][j] >= 0 {
					continue
				}

				var pool []int
				if i > 0 {
					top := costMap[i-1][j]
					if top >= 0 {
						pool = append(pool, top)
					}
				}
				if i < H-1 {
					bottom := costMap[i+1][j]
					if bottom >= 0 {
						pool = append(pool, bottom)
					}
				}
				if j > 0 {
					left := costMap[i][j-1]
					if left >= 0 {
						pool = append(pool, left)
					}
				}
				if j < W-1 {
					right := costMap[i][j+1]
					if right >= 0 {
						pool = append(pool, right)
					}
				}

				if len(pool) == 0 {
					continue
				}

				cost := min(pool...) + 1
				costMap[i][j] = cost
				changeCnt++

				if cost > max {
					max = cost
					maxX = j
					maxY = i
				}
			}
		}

		if changeCnt == 0 {
			break
		}
	}

	//for i := 0; i < H; i++ {
	//	fmt.Println(costMap[i])
	//}

	return maxX, maxY, max
}

func main() {
	H := getInt()
	W := getInt()

	S := make([][]string, H)
	for i := 0; i < H; i++ {
		S[i] = make([]string, W)

		input := getString()

		for j, c := range input {
			if c == '.' {
				S[i][j] = string(c)
			} else {
				S[i][j] = string(c)
			}
		}
	}

	// costMapの初期位置設定
	startX := 0
	startY := 0
	for i := 0; i < H; i++ {
		breakFlag := false
		for j := 0; j < W; j++ {
			if S[i][j] == "." {
				startX = j
				startY = i
				breakFlag = true
				break
			}
		}

		if breakFlag {
			break
		}
	}

	beforeX := startX
	beforeY := startY
	cost := 0
	sameCnt := 0
	for ; ; {
		maxX, maxY, max := getMaxIndex(S, beforeX, beforeY)
		//fmt.Println(maxX, maxY, max)

		if cost == max {
			sameCnt++
		}

		beforeX = maxX
		beforeY = maxY
		cost = max

		if sameCnt == 10 {
			break
		}
	}

	fmt.Println(cost)
}

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 abs(a int) int {
	return int(math.Abs(float64(a)))
}

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

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

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

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

func printIntArray(array []int) {
	strArray := fmt.Sprint(array)
	fmt.Println(strArray[1 : len(strArray) - 1])
}

func calcMod(x int) int {
	return x % mod
}


package main

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

func main() {
	var N, K int

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

	stdin := bufio.NewScanner(os.Stdin)
	stdin.Scan()
	s := strings.Fields(stdin.Text())

	A := func(s []string) []int {
		A := make([]int, len(s))
		for i, a := range s {
			var err error
			A[i], err = strconv.Atoi(a)
			if err != nil {
				panic(err)
			}
		}
		return A
	}(s)

	sort.Ints(A)

	ans := A[K-1] - A[N-K]
	mod := 1000000007

	a := 1
	tmp := 1
	tmp2 := K
	for i := K; i < N; i++ {
		a = a * tmp2 / tmp
		ans += a * (A[i] - A[N-i-1])
		if ans > mod {
			ans %= mod
		}
		tmp++
		tmp2++
	}

	fmt.Printf("%d\n", ans)
}

Score : 500 points

Problem Statement

For a finite set of integers X, let f(X)=\max X - \min X.

Given are N integers A_1,...,A_N.

We will choose K of them and let S be the set of the integers chosen. If we distinguish elements with different indices even when their values are the same, there are {}_N C_K ways to make this choice. Find the sum of f(S) over all those ways.

Since the answer can be enormous, print it \bmod (10^9+7).

Constraints

1 \leq N \leq 10^5

1 \leq K \leq N

|A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 ... A_N

Output

Print the answer \bmod (10^9+7).

Sample Input 1

4 2
1 1 3 4

Sample Output 1

11

There are six ways to choose S: \{1,1\},\{1,3\},\{1,4\},\{1,3\},\{1,4\}, \{3,4\} (we distinguish the two 1s). The value of f(S) for these choices are 0,2,3,2,3,1, respectively, for the total of 11.

Sample Input 2

6 3
10 10 10 -10 -10 -10

Sample Output 2

360

There are 20 ways to choose S. In 18 of them, f(S)=20, and in 2 of them, f(S)=0.

Sample Input 3

3 1
1 1 1

Sample Output 3

0

Sample Input 4

10 6
1000000000 1000000000 1000000000 1000000000 1000000000 0 0 0 0 0

Sample Output 4

999998537

Print the sum \bmod (10^9+7).


package main

import (
	"bufio"
	"fmt"
	"math"
	"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 getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 4
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	pp := make([]Point, n)
	var l, r float64

	o := Point{
		x: 0,
		y: 0,
	}
	for i := 0; i < n; i++ {
		pp[i].x = getNextFloat64(scanner)
		pp[i].y = getNextFloat64(scanner)
		d := dist(o, pp[i])
		if r < d*2 {
			r = d * 2
		}
	}

	for c := 0; c < 100; c++ {
		valid := false
		m := (l + r) / 2
		for i := 0; i < n && valid == false; i++ {
			for j := 0; j < i && valid == false; j++ {
				d := dist(pp[i], pp[j])
				if d > m*2 {
					continue
				}
				cp := Point{
					x: (pp[i].x + pp[j].x) / 2.0,
					y: (pp[i].y + pp[j].y) / 2.0,
				}
				h := math.Sqrt(m*m - d*d/4)
				valid = valid || ok(m, Point{
					x: cp.x - (pp[i].y-pp[j].y)/d*h,
					y: cp.y + (pp[i].x-pp[j].x)/d*h,
				}, pp)
				valid = valid || ok(m, Point{
					x: cp.x + (pp[i].y-pp[j].y)/d*h,
					y: cp.y - (pp[i].x-pp[j].x)/d*h,
				}, pp)
			}
		}
		if valid {
			r = m
			continue
		}
		l = m
	}

	fmt.Fprintln(writer, fmt.Sprintf("%.8f", r))
}

func dist(p1, p2 Point) float64 {
	return math.Sqrt(math.Pow(p1.x-p2.x, 2) + math.Pow(p1.y-p2.y, 2))
}
func ok(r float64, p Point, pp []Point) bool {
	n := len(pp)
	for i := 0; i < n; i++ {
		d := dist(pp[i], p)
		if r < d {
			return false
		}
	}

	return true
}

// Point ...
type Point struct {
	x, y float64
}

Score : 600 points

Problem Statement

Given are N points (x_i, y_i) in a two-dimensional plane.

Find the minimum radius of a circle such that all the points are inside or on it.

Constraints

2 \leq N \leq 50

0 \leq x_i \leq 1000

0 \leq y_i \leq 1000

The given N points are all different.

The values in input are all integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the minimum radius of a circle such that all the N points are inside or on it.

Your output will be considered correct if the absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

2
0 0
1 0

Sample Output 1

0.500000000000000000

Both points are contained in the circle centered at (0.5,0) with a radius of 0.5.

Sample Input 2

3
0 0
0 1
1 0

Sample Output 2

0.707106781186497524

Sample Input 3

10
10 9
5 9
2 0
0 0
2 7
3 3
2 5
10 0
3 7
1 9

Sample Output 3

6.726812023536805158

If the absolute or relative error from our answer is at most 10^{-6}, the output will be considered correct.


package main

import (
	"fmt"
	"strings"
)

func main() {
	var n int
	fmt.Scan(&n)
	var s string
	fmt.Scan(&s)
	fmt.Println(strings.Count(s, "ABC"))
}

Score : 200 points

Problem Statement

We have a string S of length N consisting of uppercase English letters.

How many times does ABC occur in S as contiguous subsequences (see Sample Inputs and Outputs)?

Constraints

3 \leq N \leq 50

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print number of occurrences of ABC in S as contiguous subsequences.

Sample Input 1

10
ZABCDBABCQ

Sample Output 1

2

Two contiguous subsequences of S are equal to ABC: the 2-nd through 4-th characters, and the 7-th through 9-th characters.

Sample Input 2

19
THREEONEFOURONEFIVE

Sample Output 2

0

No contiguous subsequences of S are equal to ABC.

Sample Input 3

33
ABCCABCBABCCABACBCBBABCBCBCBCABCB

Sample Output 3

5


package main

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

func main() {
	sc.nextInt()
	S := sc.next()

	var cnt int
	arr := strings.Split(S, "")
	for i, v := range arr {
		if v == "A" {
			if len(S) > i+1 && arr[i+1] == "B" {
				if len(S) > i+2 && arr[i+2] == "C" {
					cnt++
				}
			}
		}

	}
	fmt.Println(cnt)
}

/* template functions */
var (
	sc scanner
)

func init() {
	sc = scanner{
		buf: make([]string, 0, 0),
		cur: 0,
		r:   bufio.NewReader(os.Stdin),
	}
}

type scanner struct {
	buf []string
	cur int
	r   *bufio.Reader
}

func (s *scanner) readln() {
	rbuf := make([]byte, 0, 0)
	for {
		line, prefix, err := s.r.ReadLine()
		if err != nil {
			panic(err)
		}
		rbuf = append(rbuf, line...)
		if prefix == false {
			break
		}
	}
	s.cur = 0
	s.buf = strings.Split(*(*string)(unsafe.Pointer(&rbuf)), " ")
}
func (s *scanner) isFull() bool {
	if s.cur == len(s.buf) {
		return true
	}
	return false
}
func (s *scanner) resetCur() {
	s.cur = 0
}
func (s *scanner) next() string {
	if s.cur == 0 {
		s.readln()
	}
	res := s.buf[s.cur]
	s.cur++
	if s.isFull() {
		s.resetCur()
	}
	return res
}
func (s *scanner) nexts() []string {
	s.readln()
	s.resetCur()
	return s.buf
}
func (s *scanner) nextInt() int {
	if s.cur == 0 {
		s.readln()
	}
	res, _ := strconv.Atoi(s.buf[s.cur])
	s.cur++
	if s.isFull() {
		s.resetCur()
	}
	return res
}
func (s *scanner) nextInts() []int {
	s.readln()
	res := make([]int, len(s.buf))
	for i := range s.buf {
		res[i], _ = strconv.Atoi(s.buf[i])
	}
	s.resetCur()
	return res
}
func (s *scanner) nextFloat() float64 {
	if s.cur == 0 {
		s.readln()
	}
	res, _ := strconv.ParseFloat(s.buf[s.cur],
		64)
	s.cur++
	if s.isFull() {
		s.resetCur()
	}
	return res
}
func (s *scanner) nextFloats() []float64 {
	s.readln()
	res := make([]float64, len(s.buf))
	for i := range s.buf {
		res[i], _ = strconv.ParseFloat(s.buf[i],
			64)
	}
	s.resetCur()
	return res
}
func digits(x int) int {
	return len(strconv.Itoa(x))
}
func powInt(x, p int) (result int) {
	result = 1
	for i := 0; i < p; i++ {
		result *= x
	}
	return
}
func max(x, y int) int {
	return int(math.Max(float64(x), float64(y)))
}
func min(x, y int) int {
	return int(math.Min(float64(x), float64(y)))
}
func abs(x int) int {
	return int(math.Abs(float64(x)))
}
func varDump(value ...interface{}) {
	for _, v := range value {
		fmt.Fprintf(os.Stderr, "%#v\n", v)
	}
}
func yes() {
	fmt.Println("Yes")
}
func no() {
	fmt.Println("No")
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

func main() {
	scanInit()

	n := nextInt()
	pl := make([]int, n)
	ql := make([]int, n)

	for i := 0; i < n; i++ {
		pl[i] = nextInt()
	}
	for i := 0; i < n; i++ {
		ql[i] = nextInt()
	}
	// p,qの数値化
	p, q := 0, 0
	for j := 0; j < n; j++ {
		p += pl[j] * pow(10, n-j-1)
	}
	for j := 0; j < n; j++ {
		q += ql[j] * pow(10, n-j-1)
	}

	// 順列作成
	var tl []int = []int{1, 2, 3, 4, 5, 6, 7, 8}
	pall := permutations(tl[:n])

	// 数値リスト化
	pallnum := make([]int, len(pall))
	for i := 0; i < len(pall); i++ {
		tmp := 0
		for j := 0; j < n; j++ {
			tmp += pall[i][j] * pow(10, n-j-1)
		}
		pallnum[i] = tmp
	}

	// 順列並びかえ
	sort.Ints(pallnum)

	pidx := -1
	qidx := -1
	for i := 0; i < len(pall); i++ {
		// pidxとqidxが求まっているならば終わり
		if pidx != -1 && qidx != -1 {
			break
		}

		// p比較
		if p == pallnum[i] {
			pidx = i
		}

		// q比較
		if q == pallnum[i] {
			qidx = i
		}
	}

	fmt.Println(abs(pidx - qidx))

}

func permutations(arr []int) [][]int {
	var helper func([]int, int)
	res := [][]int{}

	helper = func(arr []int, n int) {
		if n == 1 {
			tmp := make([]int, len(arr))
			copy(tmp, arr)
			res = append(res, tmp)
		} else {
			for i := 0; i < n; i++ {
				helper(arr, n-1)
				if n%2 == 1 {
					tmp := arr[i]
					arr[i] = arr[n-1]
					arr[n-1] = tmp
				} else {
					tmp := arr[0]
					arr[0] = arr[n-1]
					arr[n-1] = tmp
				}
			}
		}
	}
	helper(arr, len(arr))
	return res
}
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
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
func scanInit() {
	const cap = 64 * 1024
	var buf = make([]byte, cap)
	sc.Buffer(buf, cap)
	sc.Split(bufio.ScanWords)
	return
}
func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}
func nextStr() string {
	sc.Scan()
	return sc.Text()
}

Score : 300 points

Problem Statement

We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).

There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Notes

For two sequences X and Y, X is said to be lexicographically smaller than Y if and only if there exists an integer k such that X_i = Y_i~(1 \leq i < k) and X_k < Y_k.

Constraints

2 \leq N \leq 8

P and Q are permutations of size N.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 ... P_N
Q_1 Q_2 ... Q_N

Output

Print |a - b|.

Sample Input 1

3
1 3 2
3 1 2

Sample Output 1

3

There are 6 permutations of size 3: (1,~2,~3), (1,~3,~2), (2,~1,~3), (2,~3,~1), (3,~1,~2), and (3,~2,~1). Among them, (1,~3,~2) and (3,~1,~2) come 2-nd and 5-th in lexicographical order, so the answer is |2 - 5| = 3.

Sample Input 2

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

Sample Output 2

17517

Sample Input 3

3
1 2 3
1 2 3

Sample Output 3

0


package main

import (
	"fmt"
	"math"
	"sort"
)

func main() {
	var N int
	fmt.Scan(&N)
	a := make([]int, N)
	b := make([]int, N)
	for i := 0; i < N; i++ {
		fmt.Scan(&a[i])
	}
	for i := 0; i < N; i++ {
		fmt.Scan(&b[i])
	}
	x := make([]int, N)
	for i := 0; i < N; i++ {
		x[i] = i + 1
	}
	var ac, bc int
	arr := NextPermutation(x)
	for i := 0; i < len(arr); i++ {
		aeq := true
		beq := true
		for j := 0; j < N; j++ {
			if arr[i][j] != a[j] {
				aeq = false
				break
			}
		}
		if aeq {
			ac = i + 1
		}
		for j := 0; j < N; j++ {
			if arr[i][j] != b[j] {
				beq = false
				break
			}
		}
		if beq {
			bc = i + 1
		}
	}
	fmt.Println(Abs(ac - bc))
}
func Abs(a int) int {
	return int(math.Abs(float64(a)))
}

func NextPermutation(x []int) [][]int {
	var p [][]int
	z := make([]int, len(x))
	copy(z, x)
	p = append(p, z)
	for i := 1; NextPermutationSub(sort.IntSlice(x)); i++ {
		y := make([]int, len(x))
		copy(y, x)
		p = append(p, y)
	}
	return p
}

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


package main

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

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

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

	A := make([]int, N)
	for i := 0; i < N; i++ {
		sc.Scan()
		A[i], _ = strconv.Atoi(sc.Text())
		A[i] /= 2
	}

	pow := -1
	for _, a := range A {
		c := 0
		for a > 0 && a%2 == 0 {
			a /= 2
			c++
		}
		if pow < 0 {
			pow = c
		}
		if pow != c {
			fmt.Println(0)
			return
		}
	}

	g := A[0]
	t := A[0]
	for i := 1; i < N; i++ {
		a := A[i]
		g = gcd(a, g)
		t = lcm(a, t)
	}
	count := 0
	for i := 1; t*i <= M; i += 2 {
		count++
	}
	fmt.Println(count)
}

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

func gcd(a, b int) int {
	if a < b {
		b, a = a, b
	}

	for b != 0 {
		t := b
		b = a % b
		a = t
	}
	return a
}

Score : 400 points

Problem Statement

Given are a sequence A= {a_1,a_2,......a_N} of N positive even numbers, and an integer M.

Let a semi-common multiple of A be a positive integer X that satisfies the following condition for every k (1 \leq k \leq N):

There exists a non-negative integer p such that X= a_k \times (p+0.5).

Find the number of semi-common multiples of A among the integers between 1 and M (inclusive).

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^9

2 \leq a_i \leq 10^9

a_i is an even number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_1 a_2 ... a_N

Output

Print the number of semi-common multiples of A among the integers between 1 and M (inclusive).

Sample Input 1

2 50
6 10

Sample Output 1

2

15 = 6 \times 2.5

15 = 10 \times 1.5

45 = 6 \times 7.5

45 = 10 \times 4.5

Thus, 15 and 45 are semi-common multiples of A. There are no other semi-common multiples of A between 1 and 50, so the answer is 2.

Sample Input 2

3 100
14 22 40

Sample Output 2

0

The answer can be 0.

Sample Input 3

5 1000000000
6 6 2 6 2

Sample Output 3

166666667


package main

import (
	"fmt"
)

func main() {
	var n, m int
	fmt.Scanf("%d %d", &n, &m)
	nums := make([]int, n)
	var lcm int
	var check int
	fmt.Scan(&nums[0])
	check = counter(nums[0])
	if n > 1 {
		fmt.Scan(&nums[1])
		lcm = nums[0] * nums[1] / GCD(nums[0], nums[1])
		if counter(nums[1]) != check {
			fmt.Println("0")
			return
		}
		for i := 2; i < n; i++ {
			fmt.Scan(&nums[i])
			lcm = LCM(lcm, nums[i])
			if counter(nums[i]) != check {
				fmt.Println("0")
				return
			}
		}
	} else {
		lcm = nums[0]
	}

	min := lcm / 2
	result := (m-min)/min/2 + 1
	fmt.Println(result)
}

func GCD(a, b int) int {
	for b != 0 {
		t := b
		b = a % b
		a = t
	}
	return a
}

func LCM(a, b int) int {
	result := a * b / GCD(a, b)

	return result
}

func counter(a int) int {
	result := 0
	for a%2 == 0 {
		result++
		a /= 2
	}
	return result
}


package main

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

func exec(stdin *Stdin, stdout *Stdout) {
	n := stdin.ReadInt()
	m := stdin.ReadInt()
	a := []int{}
	for i := 0; i < n; i++ {
		a = append(a, stdin.ReadInt())
	}

	c := 0
	for a[0]%2 == 0 {
		a[0] /= 2
		c++
	}

	for i := 1; i < n; i++ {
		d := 0
		for a[i]%2 == 0 {
			a[i] /= 2
			d++
		}

		if c != d {
			stdout.Println(0)
			return
		}
	}

	for i := 0; i < c-1; i++ {
		m /= 2
	}

	lcm := 1
	for i := 0; i < n; i++ {
		lcm = Lcm(lcm, a[i])
	}

	if lcm <= m {
		stdout.Println(((m / lcm) + 1) / 2)
	} else {
		stdout.Println(0)
	}
}

func main() {
	stdout := NewStdout()
	defer stdout.Flush()
	exec(NewStdin(bufio.ScanWords), stdout)
}

type Stdin struct {
	stdin *bufio.Scanner
}

func NewStdin(split bufio.SplitFunc) *Stdin {
	s := Stdin{bufio.NewScanner(os.Stdin)}
	s.stdin.Split(split)
	s.stdin.Buffer(make([]byte, bufio.MaxScanTokenSize), int(math.MaxInt32))
	return &s
}

func (s *Stdin) Read() string {
	s.stdin.Scan()
	return s.stdin.Text()
}

func (s *Stdin) ReadInt() int {
	n, _ := strconv.Atoi(s.Read())
	return n
}

func (s *Stdin) ReadFloat64() float64 {
	n, _ := strconv.ParseFloat(s.Read(), 64)
	return n
}

type Stdout struct {
	stdout *bufio.Writer
}

func NewStdout() *Stdout {
	return &Stdout{bufio.NewWriter(os.Stdout)}
}

func (s *Stdout) Flush() {
	s.stdout.Flush()
}

func (s *Stdout) Println(a ...interface{}) {
	fmt.Fprintln(s.stdout, a...)
}

func Gcd(x, y int) int {
	if x < y {
		x, y = y, x
	}

	for y > 0 {
		x, y = y, x%y
	}

	return x
}

func Lcm(x, y int) int {
	return x * y / Gcd(x, y)
}


package main

import "fmt"

func main() {
	n, a, b := input()
	c := xorArr(a)
	d := xorArr(b)
	for i := 0; i < n; {
		f, s := equal(i, c, d)
		if f {
			fmt.Println(i, b[0]^a[i])
		}
		i += s
	}
}

func equal(k int, c, d []int) (bool, int) {
	n := len(c)
	s := 1
	for i := range c {
		if d[i] != c[(i+k)%n] {
			return false, s
		}
	}
	return true, s
}

func xorArr(a []int) []int {
	n := len(a)
	c := make([]int, n, n)
	for i := range a {
		c[i] = a[i] ^ a[(i+1)%n]
	}
	return c
}

func input() (int, []int, []int) {
	var n int
	fmt.Scan(&n)
	a := make([]int, n, n)
	b := make([]int, n, n)
	for i := range a {
		fmt.Scan(&a[i])
	}
	for i := range b {
		fmt.Scan(&b[i])
	}
	return n, a, b
}

Score : 600 points

Problem Statement

Given are two sequences a=\{a_0,\ldots,a_{N-1}\} and b=\{b_0,\ldots,b_{N-1}\} of N non-negative integers each.

Snuke will choose an integer k such that 0 \leq k < N and an integer x not less than 0, to make a new sequence of length N, a'=\{a_0',\ldots,a_{N-1}'\}, as follows:

a_i'= a_{i+k \mod N}\ XOR \ x

Find all pairs (k,x) such that a' will be equal to b.

What is \mbox{ XOR }?

The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows:

When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.

For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)

Constraints

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

0 \leq a_i,b_i < 2^{30}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_0 a_1 ... a_{N-1}
b_0 b_1 ... b_{N-1}

Output

Print all pairs (k, x) such that a' and b will be equal, using one line for each pair, in ascending order of k (ascending order of x for pairs with the same k).

If there are no such pairs, the output should be empty.

Sample Input 1

3
0 2 1
1 2 3

Sample Output 1

1 3

If (k,x)=(1,3),

a_0'=(a_1\ XOR \ 3)=1

a_1'=(a_2\ XOR \ 3)=2

a_2'=(a_0\ XOR \ 3)=3

and we have a' = b.

Sample Input 2

5
0 0 0 0 0
2 2 2 2 2

Sample Output 2

0 2
1 2
2 2
3 2
4 2

Sample Input 3

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

Sample Output 3

2 2
5 5

Sample Input 4

2
1 2
0 0

Sample Output 4

No pairs may satisfy the condition.


package main

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

const (
	BUFSIZE   = 10000000
	MOD       = 1000000007
	INF_INT   = math.MaxInt32
	INF_INT64 = math.MaxInt64
)

var rdr = bufio.NewReaderSize(os.Stdin, BUFSIZE)

func main() {
	solve()
}

func solve() {
	var S, T string
	fmt.Scanf("%s %s", &S, &T)
	fmt.Println(T + S)
}

func ReadLineInt64() []int64 {
	buf := make([]byte, 0, BUFSIZE)
	for {
		l, p, e := rdr.ReadLine()
		if e != nil {
			fmt.Println(e.Error())
			panic(e)
		}

		buf = append(buf, l...)

		if !p {
			break
		}
	}
	str := strings.Split(string(buf), " ")
	l := len(str)

	ret := make([]int64, l)

	for i, s := range str {
		ret[i] = s2i64(s)
	}
	return ret
}

func ReadLineInt() []int {
	values := ReadLineInt64()
	ret := make([]int, len(values))
	for i, v := range values {
		ret[i] = int(v)
	}
	return ret
}

func ReadInt() int {
	v := ReadLineInt()
	return v[0]
}
func ReadInt2() (int, int) {
	v := ReadLineInt()
	return v[0], v[1]
}
func ReadInt3() (int, int, int) {
	v := ReadLineInt()
	return v[0], v[1], v[2]
}
func ReadInt4() (int, int, int, int) {
	v := ReadLineInt()
	return v[0], v[1], v[2], v[3]
}

func ReadInt64() int64 {
	v := ReadLineInt64()
	return v[0]
}
func ReadInt64_2() (int64, int64) {
	v := ReadLineInt64()
	return v[0], v[1]
}
func ReadInt64_3() (int64, int64, int64) {
	v := ReadLineInt64()
	return v[0], v[1], v[2]
}
func ReadInt64_4() (int64, int64, int64, int64) {
	v := ReadLineInt64()
	return v[0], v[1], v[2], v[3]
}

func s2i64(s string) int64 {
	v, ok := strconv.ParseInt(s, 10, 64)
	if ok != nil {
		panic("Faild : " + s + " can't convert to int64")
	}
	return v
}

// For int64
func Bool2Int64(b bool) int64 {
	if b {
		return 1
	} else {
		return 0
	}
}

func Abs64(v int64) int64 {
	if v < 0 {
		return -v
	}
	return v
}

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

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

func Distance64(a, b int64) int64 {
	return Abs64(a - b)
}

// For int
func Bool2Int(b bool) int {
	if b {
		return 1
	} else {
		return 0
	}
}

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

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

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

func Distance(a, b int) int {
	return Abs(a - b)
}

Score : 100 points

Problem Statement

Given are two strings S and T consisting of lowercase English letters. Concatenate T and S in this order, without space in between, and print the resulting string.

Constraints

S and T are strings consisting of lowercase English letters.

The lengths of S and T are between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

S T

Output

Print the resulting string.

Sample Input 1

oder atc

Sample Output 1

atcoder

When S = oder and T = atc, concatenating T and S in this order results in atcoder.

Sample Input 2

humu humu

Sample Output 2

humuhumu


package main

import "fmt"

func main() {
	var a, b, k int
	fmt.Scan(&a, &b, &k)
	for i:=0; i<k; i++{
		if a>=1 {
			a--
		}else if b >=1{
			b--
		}
	}
	fmt.Println(a, b)
}

Score : 200 points

Problem Statement

Takahashi has A cookies, and Aoki has B cookies.
Takahashi will do the following action K times:

If Takahashi has one or more cookies, eat one of his cookies.

Otherwise, if Aoki has one or more cookies, eat one of Aoki's cookies.

If they both have no cookies, do nothing.

In the end, how many cookies will Takahashi and Aoki have, respectively?

Constraints

0 \leq A \leq 10^{12}

0 \leq B \leq 10^{12}

0 \leq K \leq 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the numbers of Takahashi's and Aoki's cookies after K actions.

Sample Input 1

2 3 3

Sample Output 1

0 2

Takahashi will do the following:

He has two cookies, so he eats one of them.

Now he has one cookie left, and he eats it.

Now he has no cookies left, but Aoki has three, so Takahashi eats one of them.

Thus, in the end, Takahashi will have 0 cookies, and Aoki will have 2.

Sample Input 2

500000000000 500000000000 1000000000000

Sample Output 2

0 0

Watch out for overflows.


package main

import (
	"fmt"
)

func main() {
	var a, b, k int
	fmt.Scanf("%d %d %d", &a, &b, &k)
	if a >= k {
		fmt.Printf("%d %d", a-k, b)
	} else if a+b >= k {
		fmt.Printf("%d %d", 0, a+b-k)
	} else {
		fmt.Println("0 0")
	}
}


package main

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

const (
	initialBufSize = 1024
	maxBufSize     = 1e6
)

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

func main() {
	scanner.Split(bufio.ScanWords)
	a, b, k := scanInt(), scanInt(), scanInt()
	aa, ab := a, b
	if (k - a) > 0 {
		aa = 0
		k = k - a
	} else {
		aa -= k
		fmt.Println(aa, ab)
		return
	}
	if (k - b) > 0 {
		ab = 0
	} else {
		ab -= k
	}

	fmt.Println(aa, ab)
}

func scanInt() int {
	i, _ := strconv.Atoi(scanText())
	return i
}

func scanInts(size int) []int {
	ints := make([]int, size)
	for i := 0; i < size; i++ {
		ints[i] = scanInt()
	}
	return ints
}

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

func scanFloat64s(size int) []float64 {
	f := make([]float64, size)
	for i := 0; i < size; i++ {
		f[i] = scanFloat64()
	}
	return f
}

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

func scanTexts(size int) []string {
	s := make([]string, size)
	for i := 0; i < size; i++ {
		s[i] = scanText()
	}
	return s
}

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

func pow(x, y int) int {
	return int(math.Pow(float64(x), float64(y)))
}

// greatest common divisor(最大公約数)
func gcd(a, b int) int {
	for {
		mod := a % b
		if mod == 0 {
			return b
		}
		a, b = b, mod
	}
}

// latest common multiple(最小公倍数)
func lcm(a, b int) int {
	return (a / gcd(a, b)) * b
}

func fact(n int) int {
	ret := 1
	for i := 1; i <= n; i++ {
		ret *= i
	}
	return ret
}

func isPrimeNum(n int) bool {

	if n <= 1 {
		return false
	}

	l := int(math.Sqrt(float64(n)) + 1.)
	for i := 2; i < l; i++ {
		if n%i == 0 {
			return false
		}
	}

	return true
}


package main

import (
	"fmt"
	"math"
)

func isPrime(n int) bool {
	var tmp float64 = float64(n)
	rng := int(math.Ceil(math.Sqrt(tmp)))
	// fmt.Printf("rng = %d, n = %d, jajaja = %f\n", rng, n, math.Sqrt(float64(n)))
	for i := 2; i < rng; i++ {
		if n%i == 0 {
			return false
		}
	}
	return true
}

func main() {
	var x int
	fmt.Scan(&x)
	for i := x; i < 100004; i++ {
		if isPrime(i) {
			fmt.Println(i)
			break
		}
	}
}

Score: 300 points

Problem Statement

Find the minimum prime number greater than or equal to X.

Notes

A prime number is an integer greater than 1 that cannot be evenly divided by any positive integer except 1 and itself.

For example, 2, 3, and 5 are prime numbers, while 4 and 6 are not.

Constraints

2 \le X \le 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the minimum prime number greater than or equal to X.

Sample Input 1

20

Sample Output 1

23

The minimum prime number greater than or equal to 20 is 23.

Sample Input 2

2

Sample Output 2

2

X itself can be a prime number.

Sample Input 3

99992

Sample Output 3

100003


package main

import (
	"fmt"
	"math"
)

func main() {
	var X int
	fmt.Scan(&X)
	for !isPrime(X) {
		X++
	}
	fmt.Println(X)
}

func isPrime(x int) bool {
	if x == 1 {
		return false
	}
	if x == 2 {
		return true
	}
	if x%2 == 0 {
		return false
	}

	b := true
	rootx := int(math.Sqrt(float64(x)))
	i := 3
	for i <= rootx {
		if x%i == 0 {
			b = false
			break
		}
		i += 2
	}
	return b
}


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 ch(a byte, R, S, P int) (byte, int) {
	var ret byte
	var pt int
	switch a {
	case 'r':
		ret = 'p'
		pt = P
	case 's':
		ret = 'r'
		pt = R
	case 'p':
		ret = 's'
		pt = S
	}
	return ret, pt
}

func main() {
	sc.Split(bufio.ScanWords)
	N, K := getInt(), getInt()
	R, S, P := getInt(), getInt(), getInt()
	T := getString()

	ans := 0
	m := make([]byte, N)
	for i, v := range T {
		my, pt := ch(byte(v), R, S, P)
		if i >= K && m[i-K] == my {
			my = 'x'
			pt = 0
		}
		ans += pt
		m[i] = my
	}
	out(ans)
}

Score : 400 points

Problem Statement

At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows:

The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.)

Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss):

R points for winning with Rock;

S points for winning with Scissors;

P points for winning with Paper.

However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.)

Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands.

The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is r, the machine will play Rock in the i-th round. Similarly, p and s stand for Paper and Scissors, respectively.

What is the maximum total score earned in the game by adequately choosing the hand to play in each round?

Notes

In this problem, Rock Paper Scissors can be thought of as a two-player game, in which each player simultaneously forms Rock, Paper, or Scissors with a hand.

If a player chooses Rock and the other chooses Scissors, the player choosing Rock wins;

if a player chooses Scissors and the other chooses Paper, the player choosing Scissors wins;

if a player chooses Paper and the other chooses Rock, the player choosing Paper wins;

if both players play the same hand, it is a draw.

Constraints

2 \leq N \leq 10^5

1 \leq K \leq N-1

1 \leq R,S,P \leq 10^4

N,K,R,S, and P are all integers.

|T| = N

T consists of r, p, and s.

Input

Input is given from Standard Input in the following format:

N K
R S P
T

Output

Print the maximum total score earned in the game.

Sample Input 1

5 2
8 7 6
rsrpr

Sample Output 1

27

The machine will play {Rock, Scissors, Rock, Paper, Rock}.

We can, for example, play {Paper, Rock, Rock, Scissors, Paper} against it to earn 27 points.
We cannot earn more points, so the answer is 27.

Sample Input 2

7 1
100 10 1
ssssppr

Sample Output 2

211

Sample Input 3

30 5
325 234 123
rspsspspsrpspsppprpsprpssprpsr

Sample Output 3

4996


package main

import (
	"fmt"
	"math"
)

func main() {
	// Code for A - Table Tennis Training
	var N, A, B int
	fmt.Scanf("%d %d %d", &N, &A, &B)

	if (B-A)%2 == 0 {
		fmt.Println((B - A) / 2)
	} else {
		fmt.Println(math.Ceil(float64(B-A)/2) + 1)
	}
}

Score : 300 points

Problem Statement

2N players are running a competitive table tennis training on N tables numbered from 1 to N.

The training consists of rounds.
In each round, the players form N pairs, one pair per table.
In each pair, competitors play a match against each other.
As a result, one of them wins and the other one loses.

The winner of the match on table X plays on table X-1 in the next round,
except for the winner of the match on table 1 who stays at table 1.

Similarly, the loser of the match on table X plays on table X+1 in the next round,
except for the loser of the match on table N who stays at table N.

Two friends are playing their first round matches on distinct tables A and B.
Let's assume that the friends are strong enough to win or lose any match at will.
What is the smallest number of rounds after which the friends can get to play a match against each other?

Constraints

2 \leq N \leq 10^{18}

1 \leq A < B \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the smallest number of rounds after which the friends can get to play a match against each other.

Sample Input 1

5 2 4

Sample Output 1

1

If the first friend loses their match and the second friend wins their match, they will both move to table 3 and play each other in the next round.

Sample Input 2

5 2 3

Sample Output 2

2

If both friends win two matches in a row, they will both move to table 1.


package main

import (
  "fmt"
  "math"
)

func foo(n, a, b, i int) int {
  if a==b {
    return i
  } else if a<b && abs(n,a,b)%2 != 1 {
    a = (a+1)%n
    b = (b-1)%n
    i = i+1
    return foo(n, a, b, i)
  } else if abs(n,a,b)%2 != 1 {
    a = (a-1)%n
    b = (b+1)%n
    i = i+1
    return foo(n, a, b, i)
  } else {
    if a > b {
      if n-a <= b-1 {
        if a != n {
          a = (a+1)%n
        }
        b = (b+1)%n
      } else {
        a = (a-1)%n
        if b != 1 {
          b = (b-1)%n
        }
      }
    } else {
      if n-b <= a-1 {
        a = (a+1)%n
        if b != n {
          b = (b+1)%n
        }
      } else {
        if a != 1 {
          a = (a-1)%n
        }
        b = (b-1)%n
      }
    }
    i = i+1
    return foo(n, a, b, i)
  }
}

func abs(n, a, b int) int {
  return int(math.Abs(float64(a-b)))%n
}

func main() {
  var n, a, b int
  fmt.Scan(&n)
  fmt.Scan(&a)
  fmt.Scan(&b)
  foo(n, a, b, 0)
}


package main

import "fmt"

func main() {
    var A int
    var B int
    fmt.Scan(&A)
    fmt.Scan(&B)
    var C = 6 - (A+B)
    fmt.Print(C);
}

Score: 100 points

Problem Statement

Takahashi is solving quizzes. He has easily solved all but the last one.

The last quiz has three choices: 1, 2, and 3.

With his supernatural power, Takahashi has found out that the choices A and B are both wrong.

Print the correct choice for this problem.

Constraints

Each of the numbers A and B is 1, 2, or 3.

A and B are different.

Input

Input is given from Standard Input in the following format:

A
B

Output

Print the correct choice.

Sample Input 1

3
1

Sample Output 1

2

When we know 3 and 1 are both wrong, the correct choice is 2.

Sample Input 2

1
2

Sample Output 2

3


package main

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

var sc = bufio.NewScanner(os.Stdin)

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

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

func main() {
	var n int
	var s, t string
	fmt.Scanf("%d", &n)
	fmt.Scanf("%s %s", &s, &t)

	for i := 0; i< n; i++ {
		fmt.Printf("%s%s", string(s[i]), string(t[i]))
	}
}

Score : 200 points

Problem Statement

Given are strings s and t of length N each, both consisting of lowercase English letters.

Let us form a new string by alternating the characters of S and the characters of T, as follows: the first character of S, the first character of T, the second character of S, the second character of T, ..., the N-th character of S, the N-th character of T. Print this new string.

Constraints

1 \leq N \leq 100

|S| = |T| = N

S and T are strings consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S T

Output

Print the string formed.

Sample Input 1

2
ip cc

Sample Output 1

icpc

Sample Input 2

8
hmhmnknk uuuuuuuu

Sample Output 2

humuhumunukunuku

Sample Input 3

5
aaaaa aaaaa

Sample Output 3

aaaaaaaaaa


package main

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

var sc = bufio.NewScanner(os.Stdin)

func readInt() int              { return readIntArray()[0] }
func readInt2() (int, int)      { a := readIntArray(); return a[0], a[1] }
func readInt3() (int, int, int) { a := readIntArray(); return a[0], a[1], a[2] }
func readIntArray() []int {
        a := make([]int, 0, 1000)
        sc.Scan()
        for _, v := range strings.Split(sc.Text(), " ") {
                vv, _ := strconv.Atoi(v)
                a = append(a, vv)
        }
        return a
}
func readString() string            { return readStringArray()[0] }
func readString2() (string, string) { a := readStringArray(); return a[0], a[1] }
func readStringArray() []string     { sc.Scan(); return strings.Split(sc.Text(), " ") }

func main() {
        n := readInt()
        s, t := readString2()

        for i := 0; i < n; i++ {
                fmt.Print(string(s[i]))
                fmt.Print(string(t[i]))
        }
}


package main

import (
	"fmt"
)

func main() {
	var a, b int
	fmt.Scan(&a, &b)
	fmt.Println(Lcm(a, b))
}

func Gcd(x, y int) int {
	mod := x % y
	if mod > 0 {
		return Gcd(y, mod)
	} else {
		return y
	}
}

func Lcm(x, y int) int {
	return x * y / Gcd(x, y)
}

Score : 300 points

Problem Statement

Takahashi is organizing a party.

At the party, each guest will receive one or more snack pieces.

Takahashi predicts that the number of guests at this party will be A or B.

Find the minimum number of pieces that can be evenly distributed to the guests in both of the cases predicted.

We assume that a piece cannot be divided and distributed to multiple guests.

Constraints

1 \leq A, B \leq 10^5

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the minimum number of pieces that can be evenly distributed to the guests in both of the cases with A guests and B guests.

Sample Input 1

2 3

Sample Output 1

6

When we have six snack pieces, each guest can take three pieces if we have two guests, and each guest can take two if we have three guests.

Sample Input 2

123 456

Sample Output 2

18696

Sample Input 3

100000 99999

Sample Output 3

9999900000


package main

import "fmt"

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

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

func main() {
	var A, B int
	fmt.Scan(&A, &B)
	fmt.Println(lcm(A, B))
}


package main

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

func brickBreak(n int, a []int) int {
	c, j := 0, 1
	for i := 0; i < n; i++ {
		if a[i] != j {
			c++
		} else {
			j++
		}
	}
	if c == n {
		return -1
	}
	return c
}

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 10240*10240)

	n, err := strconv.Atoi(readLine(reader))
	checkError(err)
	a := make([]int, n)
	ss := strings.Split(readLine(reader), " ")
	for i := range ss {
		a[i], err = strconv.Atoi(ss[i])
		checkError(err)
	}
	fmt.Println(brickBreak(n, a))
}

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

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

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

Score : 400 points

Problem Statement

We have N bricks arranged in a row from left to right.

The i-th brick from the left (1 \leq i \leq N) has an integer a_i written on it.

Among them, you can break at most N-1 bricks of your choice.

Let us say there are K bricks remaining. Snuke will be satisfied if, for each integer i (1 \leq i \leq K), the i-th of those brick from the left has the integer i written on it.

Find the minimum number of bricks you need to break to satisfy Snuke's desire. If his desire is unsatisfiable, print -1 instead.

Constraints

All values in input are integers.

1 \leq N \leq 200000

1 \leq a_i \leq N

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of bricks that need to be broken to satisfy Snuke's desire, or print -1 if his desire is unsatisfiable.

Sample Input 1

3
2 1 2

Sample Output 1

1

If we break the leftmost brick, the remaining bricks have integers 1 and 2 written on them from left to right, in which case Snuke will be satisfied.

Sample Input 2

3
2 2 2

Sample Output 2

-1

In this case, there is no way to break some of the bricks to satisfy Snuke's desire.

Sample Input 3

10
3 1 4 1 5 9 2 6 5 3

Sample Output 3

7

Sample Input 4

1
1

Sample Output 4

0

There may be no need to break the bricks at all.


package main

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

func main() {
	N := nextInt()
	a := make([]int, N)

	for i := 0; i < N; i++ {
		a[i] = nextInt()
	}
	c := 1
	n := 0
	for i := 0; i < N; i++ {
		if a[i] == c {
			c++
		} else {
			n++
		}
	}

	if c == 1 || n == N {
		fmt.Println(-1)
	} else {
		fmt.Println(n)
	}
}

var stdin = initStdin()

func initStdin() *bufio.Scanner {
	bufsize := 1 * 1024 * 1024 // 1 MB
	var stdin = bufio.NewScanner(os.Stdin)
	stdin.Buffer(make([]byte, bufsize), bufsize)
	stdin.Split(bufio.ScanWords)
	return stdin
}

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

func nextBytes() []byte {
	stdin.Scan()
	return stdin.Bytes()
}

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

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