package main

import (
	"fmt"
)

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

	var input string
	fmt.Scan(&input)
	inputRune := []rune(input)

	fb := inputRune[0] == []rune("0")[0]
	lines := make([]int, 0)
	for i := 0; i < n; i++ {
		if len(lines) == 0 || inputRune[i-1] != inputRune[i] {
			lines = append(lines, 0)
		}
		lines[len(lines)-1] += 1
	}

	var output int
	lineLength := len(lines)

	var preValue int
	if fb {
		// indexが0の時を求める
		var result int
		for i := 0; i < 2*k; i++ {
			if i > lineLength {
				break
			}

			result += lines[i]
		}

		output = result
		preValue = result

		// indexが0以上の時を求める
		for i := 1; i*2-1 < lineLength; i++ {
			result := preValue

			// 前のやつを引く
			for j := 2*(i-1) - 1; j < 2*i-1; j++ {
				if j < 0 {
					continue
				}
				result -= lines[j]
			}

			// 次のやつを足す
			for j := 2 * (i - 1 + k); j <= 2*(i+k); j++ {
				if j >= len(lines) {
					break
				}
				result += lines[j]
			}

			if result > output {
				output = result
			}
			preValue = result
		}
	} else {
		// indexが0の時を求める
		var result int
		for j := 0; j <= k*2; j++ {
			if j >= lineLength {
				break
			}

			result += lines[j]
		}

		output = result
		preValue = result

		// indexが0以上の時を求める
		for i := 1; i*2 < lineLength; i++ {
			result := preValue

			// 前のやつを引く
			for j := 2 * (i - 1); j < 2*i; j++ {
				result -= lines[j]
			}

			// 次のやつを引く
			for j := 2*(i+k-1) + 1; j <= 2*(i+k); j++ {
				if j >= lineLength {
					break
				}

				result += lines[j]
			}

			if result > output {
				output = result
			}
			preValue = result
		}
	}
	fmt.Println(output)
}


package main

import (
	"fmt"
)

func main() {
	var a,b,c,d,e,k int
	fmt.Scan(&a,&b,&c,&d,&e,&k)

	if b - a >= k {
		fmt.Println(":(")
	} else if c - a >= k {
		fmt.Println(":(")
	} else if d - a >= k {
		fmt.Println(":(")
	} else if e - a >= k {
		fmt.Println(":(")
	} else if c - b >= k {
		fmt.Println(":(")
	} else if d - b >= k {
		fmt.Println(":(")
	} else if e - b >= k {
		fmt.Println(":(")
	} else if d - c >= k {
		fmt.Println(":(")
	} else if e - c >= k {
		fmt.Println(":(")
	} else if e - d >= k {
		fmt.Println(":(")
	} else {
		fmt.Println("Yay!")
	}
}

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

Score: 100 points

Problem Statement

In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively.

Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k.

Determine if there exists a pair of antennas that cannot communicate directly.

Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p.

Constraints

a, b, c, d, e and k are integers between 0 and 123 (inclusive).

a < b < c < d < e

Input

Input is given from Standard Input in the following format:

a
b
c
d
e
k

Output

Print :( if there exists a pair of antennas that cannot communicate directly, and print Yay! if there is no such pair.

Sample Input 1

1
2
4
8
9
15

Sample Output 1

Yay!

In this case, there is no pair of antennas that cannot communicate directly, because:

the distance between A and B is 2 - 1 = 1

the distance between A and C is 4 - 1 = 3

the distance between A and D is 8 - 1 = 7

the distance between A and E is 9 - 1 = 8

the distance between B and C is 4 - 2 = 2

the distance between B and D is 8 - 2 = 6

the distance between B and E is 9 - 2 = 7

the distance between C and D is 8 - 4 = 4

the distance between C and E is 9 - 4 = 5

the distance between D and E is 9 - 8 = 1

and none of them is greater than 15. Thus, the correct output is Yay!.

Sample Input 2

15
18
26
35
36
18

Sample Output 2

:(

In this case, the distance between antennas A and D is 35 - 15 = 20 and exceeds 18, so they cannot communicate directly.
Thus, the correct output is :(.

Submitted source-code file size, in bytes.

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

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


package main

import (
  "fmt"
)

func main() {
  var (
    a,b,c,d,e,k int
    )
  fmt.Scan(&a,&b,&c,&d,&e,&k)

  if e - a  <= k  {
    fmt.Println(":(")
  } else {
    fmt.Println("Yay!")
  }
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

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

func main() {
    anntenas := make([]int,5)
  	for i := 0; i< 5; i++{
    	anntenas[i], _ = strconv.Atoi(nextLine())
  	}
    dist, _ := strconv.Atoi(nextLine())
  sort.Sort(sort.IntSlice(anntenas))
  if (anntenas[len(anntenas)-1] - anntenas[0]) > dist {
    fmt.Println(":(")
  } else {
    fmt.Println("Yay!")
  }
}


package main

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

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

func scanString() string { sc.Scan(); return sc.Text() }
func scanRunes() []rune { return []rune(scanString()) }
func scanInt() int { a,_ := strconv.Atoi(scanString()); return a }
func scanInt64() int64 { a,_ := strconv.ParseInt(scanString(),10,64); return a }
func scanFloat64() float64 { a,_ := strconv.ParseFloat(scanString(),64); return a }

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

func debug(a ...interface{}) { fmt.Fprintln(os.Stderr, a...) }

func abs(a int) int { if a<0 { return -a }; return a }
func min(a,b int) int { if a<b { return a }; return b }
func max(a,b int) int { if a>b { return a }; return b }

//•*¨*•.¸¸♪Main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1001001)

	arr := scanInts(5)

	ans := 1 << 31-1
	permutationSearch(5, func(a []int) {
		sum := 0
		for i := 0; i < 4; i++ {
			sum += ((arr[a[i]]+9)/10)*10
		}
		ans = min(ans, sum+arr[a[4]])
	})

	fmt.Fprintln(wr, ans)
}

func permutationSearch(n int, fn func(a []int)) {
	a := make([]int, n)
	for i := 0; i < n; i++ { a[i] = i }
	swap := func(i,j int) { a[i],a[j] = a[j],a[i] }

	fn(a)
	for i := n-2; i >= 0; i-- {
		if a[i] > a[i+1] { continue }
		for j := n-1; j >= 0; j-- {
			if a[i] > a[j] { continue }
			swap(i,j)
			for k := i+1; k < (n+i+1)/2; k++ { swap(k,n-(k-i)) }
			fn(a)
			i = n-1
			break
		}
	}
}

Score: 200 points

Problem Statement

The restaurant AtCoder serves the following five dishes:

ABC Don (rice bowl): takes A minutes to serve.

ARC Curry: takes B minutes to serve.

AGC Pasta: takes C minutes to serve.

APC Ramen: takes D minutes to serve.

ATC Hanbagu (hamburger patty): takes E minutes to serve.

Here, the time to serve a dish is the time between when an order is placed and when the dish is delivered.

This restaurant has the following rules on orders:

An order can only be placed at a time that is a multiple of 10 (time 0, 10, 20, ...).

Only one dish can be ordered at a time.

No new order can be placed when an order is already placed and the dish is still not delivered, but a new order can be placed at the exact time when the dish is delivered.

E869120 arrives at this restaurant at time 0. He will order all five dishes. Find the earliest possible time for the last dish to be delivered.

Here, he can order the dishes in any order he likes, and he can place an order already at time 0.

Constraints

A, B, C, D and E are integers between 1 and 123 (inclusive).

Input

Input is given from Standard Input in the following format:

A
B
C
D
E

Output

Print the earliest possible time for the last dish to be delivered, as an integer.

Sample Input 1

29
20
7
35
120

Sample Output 1

215

If we decide to order the dishes in the order ABC Don, ARC Curry, AGC Pasta, ATC Hanbagu, APC Ramen, the earliest possible time for each order is as follows:

Order ABC Don at time 0, which will be delivered at time 29.

Order ARC Curry at time 30, which will be delivered at time 50.

Order AGC Pasta at time 50, which will be delivered at time 57.

Order ATC Hanbagu at time 60, which will be delivered at time 180.

Order APC Ramen at time 180, which will be delivered at time 215.

There is no way to order the dishes in which the last dish will be delivered earlier than this.

Sample Input 2

101
86
119
108
57

Sample Output 2

481

If we decide to order the dishes in the order AGC Pasta, ARC Curry, ATC Hanbagu, APC Ramen, ABC Don, the earliest possible time for each order is as follows:

Order AGC Pasta at time 0, which will be delivered at time 119.

Order ARC Curry at time 120, which will be delivered at time 206.

Order ATC Hanbagu at time 210, which will be delivered at time 267.

Order APC Ramen at time 270, which will be delivered at time 378.

Order ABC Don at time 380, which will be delivered at time 481.

There is no way to order the dishes in which the last dish will be delivered earlier than this.

Sample Input 3

123
123
123
123
123

Sample Output 3

643

This is the largest valid case.


package main

import (
	"fmt"
)

func main() {
	// var a, b, c, d, e int
	// fmt.Scan(&a, &b, &c, &d, &e)
	ts := make([]int, 5)
	for i := 0; i < 5; i++ {
		fmt.Scan(&ts[i])
	}

	min := 10
	sum := 0
	for i := 0; i < 5; i++ {
		t := (ts[i] % 10)
		if t != 0 && t < min {
			min = t
		}
		if t == 0 {
			sum += ts[i]
		} else {
			sum += ts[i] + 10 - t
		}
	}
	sum = sum - (10 - min)

	fmt.Println(sum)
}


package main

import "fmt"

func main() {
	var x, p int
	sum := 0
	max := 0
	for i := 0; i < 5; i++ {
		fmt.Scan(&x)
		sum += x
		if v := x % 10; v != 0 {
			p = 10 - v
			sum += p
			if p > max {
				max = p
			}
		}
	}
	fmt.Println(sum - max)
}


package main

import (
	"fmt"
)

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

	min := int(1e16)
	for i := 0 ; i < 5; i++{
		var a int
		fmt.Scan(&a)
		if min > a {
			min = a
		}
	}
	p := 0
	if n % min != 0 {
		p= 1

	}

	fmt.Println(n/min + 4+p)

}

Score: 300 points

Problem Statement

In 2028 and after a continuous growth, AtCoder Inc. finally built an empire with six cities (City 1, 2, 3, 4, 5, 6)!

There are five means of transport in this empire:

Train: travels from City 1 to 2 in one minute. A train can occupy at most A people.

Bus: travels from City 2 to 3 in one minute. A bus can occupy at most B people.

Taxi: travels from City 3 to 4 in one minute. A taxi can occupy at most C people.

Airplane: travels from City 4 to 5 in one minute. An airplane can occupy at most D people.

Ship: travels from City 5 to 6 in one minute. A ship can occupy at most E people.

For each of them, one vehicle leaves the city at each integer time (time 0, 1, 2, ...).

There is a group of N people at City 1, and they all want to go to City 6.

At least how long does it take for all of them to reach there?
You can ignore the time needed to transfer.

Constraints

1 \leq N, A, B, C, D, E \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A
B
C
D
E

Output

Print the minimum time required for all of the people to reach City 6, in minutes.

Sample Input 1

5
3
2
4
3
5

Sample Output 1

7

One possible way to travel is as follows.
First, there are N = 5 people at City 1, as shown in the following image:

In the first minute, three people travels from City 1 to City 2 by train. Note that a train can only occupy at most three people.

In the second minute, the remaining two people travels from City 1 to City 2 by train, and two of the three people who were already at City 2 travels to City 3 by bus. Note that a bus can only occupy at most two people.

In the third minute, two people travels from City 2 to City 3 by train, and another two people travels from City 3 to City 4 by taxi.

From then on, if they continue traveling without stopping until they reach City 6, all of them can reach there in seven minutes.

There is no way for them to reach City 6 in 6 minutes or less.

Sample Input 2

10
123
123
123
123
123

Sample Output 2

5

All kinds of vehicles can occupy N = 10 people at a time.
Thus, if they continue traveling without stopping until they reach City 6, all of them can reach there in five minutes.

Sample Input 3

10000000007
2
3
5
7
11

Sample Output 3

5000000008

Note that the input or output may not fit into a 32-bit integer type.


package main

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

var sc = bufio.NewScanner(os.Stdin)

func nextLine() int64 {
  sc.Scan()
  data, _ := strconv.ParseInt(sc.Text(), 10, 64)
  return data
}

func main() {
  var num = nextLine()
  args := []int64{nextLine(),nextLine(),nextLine(),nextLine(),nextLine()}

  var max = num / args[0]
  for _, arg := range args {
    var value = int64(math.Ceil(float64(num) / float64(arg)))
    if value > max {
      max = value
    }
  }
  fmt.Println(max + 4)
}


package main

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

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

func scanString() string   { sc.Scan(); return sc.Text() }
func scanRunes() []rune    { return []rune(scanString()) }
func scanInt() int         { a, _ := strconv.Atoi(scanString()); return a }
func scanInt64() int64     { a, _ := strconv.ParseInt(scanString(), 10, 64); return a }
func scanFloat64() float64 { a, _ := strconv.ParseFloat(scanString(), 64); return a }

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

func debug(a ...interface{}) { fmt.Fprintln(os.Stderr, a...) }

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

//•*¨*•.¸¸♪main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)
func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1001001)

	X := scanInt()
	Y := scanInt()
	Z := scanInt()
	K := scanInt()
	a := scanInts(X)
	b := scanInts(Y)
	c := scanInts(Z)

	bc := make([]int, Y*Z)
	for i := 0; i < Y; i++ {
		for j := 0; j < Z; j++ {
			bc[i*Z+j] = b[i] + c[j]
		}
	}

	sort.Ints(bc)

	n := Y * Z
	l, r := 0, 1<<40
	for l+1 < r {
		mid := (l + r) / 2
		cnt := 0
		for i := 0; i < X; i++ {
			cnt += n - sort.Search(n, func(j int) bool {
				return mid <= a[i]+bc[j]
			})
		}

		if cnt >= K {
			l = mid
		} else {
			r = mid
		}
	}

	arr := []int{}

	for i := 0; i < X; i++ {
		for j := 0; j < n; j++ {
			if a[i]+bc[n-1-j] < l {
				break
			}
			arr = append(arr, a[i]+bc[n-1-j])
		}
	}

	sort.Ints(arr)

	for i := 0; i < K; i++ {
		fmt.Fprintln(wr, arr[K-1-i])
	}
}

Score: 400 points

Problem Statement

The Patisserie AtCoder sells cakes with number-shaped candles.
There are X, Y and Z kinds of cakes with 1-shaped, 2-shaped and 3-shaped candles, respectively.
Each cake has an integer value called deliciousness, as follows:

The deliciousness of the cakes with 1-shaped candles are A_1, A_2, ..., A_X.

The deliciousness of the cakes with 2-shaped candles are B_1, B_2, ..., B_Y.

The deliciousness of the cakes with 3-shaped candles are C_1, C_2, ..., C_Z.

Takahashi decides to buy three cakes, one for each of the three shapes of the candles, to celebrate ABC 123.

There are X \times Y \times Z such ways to choose three cakes.

We will arrange these X \times Y \times Z ways in descending order of the sum of the deliciousness of the cakes.

Print the sums of the deliciousness of the cakes for the first, second, ..., K-th ways in this list.

Constraints

1 \leq X \leq 1 \ 000

1 \leq Y \leq 1 \ 000

1 \leq Z \leq 1 \ 000

1 \leq K \leq \min(3 \ 000, X \times Y \times Z)

1 \leq A_i \leq 10 \ 000 \ 000 \ 000

1 \leq B_i \leq 10 \ 000 \ 000 \ 000

1 \leq C_i \leq 10 \ 000 \ 000 \ 000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y Z K
A_1 \ A_2 \ A_3 \ ... \ A_X
B_1 \ B_2 \ B_3 \ ... \ B_Y
C_1 \ C_2 \ C_3 \ ... \ C_Z

Output

Print K lines. The i-th line should contain the i-th value stated in the problem statement.

Sample Input 1

2 2 2 8
4 6
1 5
3 8

Sample Output 1

19
17
15
14
13
12
10
8

There are 2 \times 2 \times 2 = 8 ways to choose three cakes, as shown below in descending order of the sum of the deliciousness of the cakes:

(A_2, B_2, C_2): 6 + 5 + 8 = 19

(A_1, B_2, C_2): 4 + 5 + 8 = 17

(A_2, B_1, C_2): 6 + 1 + 8 = 15

(A_2, B_2, C_1): 6 + 5 + 3 = 14

(A_1, B_1, C_2): 4 + 1 + 8 = 13

(A_1, B_2, C_1): 4 + 5 + 3 = 12

(A_2, B_1, C_1): 6 + 1 + 3 = 10

(A_1, B_1, C_1): 4 + 1 + 3 = 8

Sample Input 2

3 3 3 5
1 10 100
2 20 200
1 10 100

Sample Output 2

400
310
310
301
301

There may be multiple combinations of cakes with the same sum of the deliciousness. For example, in this test case, the sum of A_1, B_3, C_3 and the sum of A_3, B_3, C_1 are both 301.
However, they are different ways of choosing cakes, so 301 occurs twice in the output.

Sample Input 3

10 10 10 20
7467038376 5724769290 292794712 2843504496 3381970101 8402252870 249131806 6310293640 6690322794 6082257488
1873977926 2576529623 1144842195 1379118507 6003234687 4925540914 3902539811 3326692703 484657758 2877436338
4975681328 8974383988 2882263257 7690203955 514305523 6679823484 4263279310 585966808 3752282379 620585736

Sample Output 3

23379871545
22444657051
22302177772
22095691512
21667941469
21366963278
21287912315
21279176669
21160477018
21085311041
21059876163
21017997739
20703329561
20702387965
20590247696
20383761436
20343962175
20254073196
20210218542
20150096547

Note that the input or output may not fit into a 32-bit integer type.


package main

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

func main() {
	var x, y, z, k int
	fmt.Scan(&x, &y, &z, &k)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	as := make([]int, x)
	for i := range as {
		sc.Scan()
		as[i], _ = strconv.Atoi(sc.Text())
	}
	sort.Sort(sort.Reverse(sort.IntSlice(as)))
	bs := make([]int, y)
	for i := range bs {
		sc.Scan()
		bs[i], _ = strconv.Atoi(sc.Text())
	}
	sort.Sort(sort.Reverse(sort.IntSlice(bs)))
	cs := make([]int, z)
	for i := range cs {
		sc.Scan()
		cs[i], _ = strconv.Atoi(sc.Text())
	}
	sort.Sort(sort.Reverse(sort.IntSlice(cs)))

	ans := make([]int, 0, x+y+z)
	for ia, a := range as {
		for ib, b := range bs {
			for ic, c := range cs {
				if (ia+1)*(ib+1)*(ic+1) > k {
					break
				}
				ans = append(ans, a+b+c)
			}
		}
	}
	sort.Sort(sort.Reverse(sort.IntSlice(ans)))
	for _, v := range ans[:k] {
		fmt.Println(v)
	}
}


package main

import (
	"fmt"
	"sort"
)

func main() {
	var x, y, z, k int
	fmt.Scan(&x, &y, &z, &k)
	ar := make([]int, x)
	br := make([]int, y)
	cr := make([]int, z)
	for i := range ar {
		fmt.Scan(&ar[i])
	}
	for i := range br {
		fmt.Scan(&br[i])
	}
	for i := range cr {
		fmt.Scan(&cr[i])
	}
	mr := make([]int, 0, x*y*z)
	for _, a := range ar {
		for _, b := range br {
			for _, c := range cr {
				mr = append(mr, a+b+c)
			}
		}
	}
	sort.Ints(mr)
	for i := len(mr) - 1; i >= len(mr)-k; i-- {
		fmt.Println(mr[i])
	}
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

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

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

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

	if a*b*c == a*a*a {
		fmt.Print("Yes")
	} else {
		fmt.Print("NO")
	}
}

Score : 100 points

Problem Statement

You are given three integers A, B and C.

Determine if there exists an equilateral triangle whose sides have lengths A, B and C.

Constraints

All values in input are integers.

1 \leq A,B,C \leq 100

Input

Input is given from Standard Input in the following format:

A B C

Output

If there exists an equilateral triangle whose sides have lengths A, B and C, print Yes; otherwise, print No.

Sample Input 1

2 2 2

Sample Output 1

Yes

There exists an equilateral triangle whose sides have lengths 2, 2 and 2.

Sample Input 2

3 4 5

Sample Output 2

No

There is no equilateral triangle whose sides have lengths 3, 4 and 5.


package main

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

var charSet = []string{
	"A", "C", "G", "T",
}

func main() {
	S := nextString()
	l := len(S)
	ans := 0
	cur := 0
	for i := 0; i < l; i++ {
		if match(string(S[i])) {
			cur += 1
		} else {
			ans = MaxIn2Values(ans, cur)
			cur = 0
		}
		if i == l-1 {
			ans = MaxIn2Values(ans, cur)
		}
	}
	fmt.Printf("%v", ans)
}

func match(s string) bool {
	for _, v := range charSet {
		if v == s {
			return true
		}
	}
	return false
}

func nextInt() int {
	v, _ := strconv.Atoi(nextReader())
	return v
}

func nextString() string {
	return nextReader()
}

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

var nextReader func() string

func init() {
	nextReader = NewScanner()
}

func NewScanner() func() string {
	r := bufio.NewScanner(os.Stdin)
	r.Buffer(make([]byte, 1024), int(1e+11))
	r.Split(bufio.ScanWords)
	return func() string {
		r.Scan()
		return r.Text()
	}
}

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

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

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

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

Score : 200 points

Problem Statement

You are given a string S consisting of uppercase English letters. Find the length of the longest ACGT string that is a substring (see Notes) of S.

Here, a ACGT string is a string that contains no characters other than A, C, G and T.

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

S is a string of length between 1 and 10 (inclusive).

Each character in S is an uppercase English letter.

Input

Input is given from Standard Input in the following format:

S

Output

Print the length of the longest ACGT string that is a substring of S.

Sample Input 1

ATCODER

Sample Output 1

3

Among the ACGT strings that are substrings of ATCODER, the longest one is ATC.

Sample Input 2

HATAGAYA

Sample Output 2

5

Among the ACGT strings that are substrings of HATAGAYA, the longest one is ATAGA.

Sample Input 3

SHINJUKU

Sample Output 3

0

Among the ACGT strings that are substrings of SHINJUKU, the longest one is (the empty string).


package main

import (
    "fmt"
)

func main() {
    S := scanString()
    max_substr_len := 0
    for i := 0; i <= len(S) - 1; i++ {
        for j := i; j <= len(S); j++ {
            substr := S[i:j]
            is_acgt := true
            for k := 0; k < len(substr); k++ {
                if substr[k] != 'A' && substr[k] != 'C' && substr[k] != 'G' && substr[k] != 'T' {
                    is_acgt = false
                    break
                }
            }
            if is_acgt {
                if max_substr_len < len(substr) {
                    max_substr_len = len(substr)
                }
            }
        }
    }
    fmt.Println(max_substr_len)
}

func scanInt() (num int) {
    fmt.Scan(&num)
    return
}

func scanInts(len int) (nums []int) {
    var num int
    for i := 0; i < len; i++ {
        fmt.Scan(&num)
        nums = append(nums, num)
    }
    return
}

func scanString() (str string) {
    fmt.Scan(&str)
    return
}

func scanStrings(len int) (strs []string) {
    var str string
    for i := 0; i < len; i++ {
        fmt.Scan(&str)
        strs = append(strs, str)
    }
    return
}


package main

import (
	"fmt"
	"strings"
)

func countAC(s string) int {
	a := strings.Split(s, "")
	count := 0
	for i := 0; i < len(a)-1; i++ {
		if a[i] == "A" {
			if a[i+1] == "C" {
				count++
			}
		}
	}
	return count
}

func main() {
	var n, q int
	var s string
	var l, r []int

	fmt.Scan(&n, &q, &s)
	for i := 0; i < q*2; i++ {
		var tmp int
		fmt.Scan(&tmp)
		if i%2 == 0 {
			l = append(l, tmp)
		} else {
			r = append(r, tmp)
		}
	}

	for i := 0; i < q; i++ {
		target := s[l[i]-1 : r[i]]
		res := countAC(target)
		fmt.Println(res)
	}
}

Score : 300 points

Problem Statement

You are given a string S of length N consisting of A, C, G and T. Answer the following Q queries:

Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does AC occurs as a substring?

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

S is a string of length N.

Each character in S is A, C, G or T.

1 \leq l_i < r_i \leq N

Input

Input is given from Standard Input in the following format:

N Q
S
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

8 3
ACACTACG
3 7
2 3
1 8

Sample Output 1

2
0
3

Query 1: the substring of S starting at index 3 and ending at index 7 is ACTAC. In this string, AC occurs twice as a substring.

Query 2: the substring of S starting at index 2 and ending at index 3 is CA. In this string, AC occurs zero times as a substring.

Query 3: the substring of S starting at index 1 and ending at index 8 is ACACTACG. In this string, AC occurs three times as a substring.


package main

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

var sc = bufio.NewScanner(os.Stdin)

func nextLine() []string {
	sc.Scan()
	return strings.Split(sc.Text(), " ")
}

func main() {
	tmp := nextLine()
	Q, _ := strconv.ParseInt(tmp[1], 10, 64)

	sc.Scan()
	S := sc.Text()

	for i := int64(0); i < Q; i++ {
		subS := ""
		tmp = nextLine()
		if len(tmp) < 2 {
			subS = S
		} else if len(tmp) < 1 {
			panic("none")
		} else {
			l, _ := strconv.ParseInt(tmp[0], 10, 64)
			r, _ := strconv.ParseInt(tmp[1], 10, 64)
			subS = S[l-1 : r]
		}
		fmt.Println(strings.Count(subS, "AC"))
	}
}


package main

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

type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

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

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buffer []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buffer = append(buffer, line...)
		if !isPrefix {
			break
		}
	}
	return string(buffer)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	r := io.tokens[io.nextToken]
	io.nextToken++
	return r
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) PrintLn(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntLn(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.PrintLn(b...)
}

func (io *Io) PrintStringLn(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.PrintLn(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

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

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

func main() {
	io := NewIo()
	//defer io.Flush()
	n := io.NextInt()
	q := io.NextInt()
	s := io.Next()
	l := make([]int,q)
	r := make([]int,q)
	for i:=0; i<q; i++ {
		l[i] = io.NextInt()
		r[i] = io.NextInt()
	}
	sum := make([]int,n)
	counter := 0
	i := 0
	for i<n-1 {
		if s[i] == 65 { //A
			if s[i+1] == 67 { //C
				sum[i] = counter
				sum[i+1] = counter + 1
				counter ++
			}
			i += 2
		} else {
			sum[i]=counter
			i += 1
		}
	}
	if s[n-2] == 65 && s[n-1] == 67 {
		counter ++
	}
	sum[n-1]=counter
	for i:=0; i<q; i++ {
			io.PrintLn(sum[r[i]-1]-sum[l[i]-1])
	}
	io.Flush()
}


package main

import (
	"fmt"
)

func main() {
	var N, Q int
	fmt.Scanln(&N, &Q)
	var S string
	fmt.Scanln(&S)

	indices := []int{}

	for i := 0; i < len(S)-1; i++ {
		if S[i] == 'A' && S[i+1] == 'C' {
			indices = append(indices, i+1)
		}

	}

	for i := 0; i < Q; i++ {
		var l, r int
		fmt.Scanln(&l, &r)

		a := 0
		for _, idx := range indices {
			if idx >= l && idx < r {
				a++
			}
		}
		fmt.Println(a)
	}
}


package main

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

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

func main() {
	q := readIntSlice()
	s := readString()
	//sLen := q[0]
	ans := make([]int, 0, q[1])
	for i := 0; i < q[1]; i++ {
		ranges := readIntSlice()
		sRange := s[ranges[0]-1 : ranges[1]]
		cnt := strings.Count(sRange, "AC")
		ans = append(ans, cnt)
	}
	for _, a := range ans {
		fmt.Printf("%d\n", a)
	}
}

func readIntSlice() []int {
	line := readStringSlice()
	slice := make([]int, 0, len(line))
	for _, tmp := range line {
		val, err := strconv.Atoi(tmp)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		slice = append(slice, val)

	}
	return slice
}

func readString() string {
	s, _, _ := in.ReadLine()
	return string(s)
}
func readStringSlice() []string {
	line := readString()
	s := strings.Split(line, " ")
	return s
}
func dump(value ...interface{}) {
	for _, v := range value {
		fmt.Printf("%#v\n", v)
	}
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

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

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

// 長い文字列を読み込む時に使う //
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 main() {
	sc.Split(bufio.ScanWords)

	readLine()
	xs := readLine()

	dic := make(map[rune]int)

	for _, char := range xs {
		val, ok := dic[char]
		if !ok {
			val = 0
		}
		dic[char] = val + 1
	}
	prod := 1
	div := 1000000007
	for _, val := range dic {
		prod = (prod * (val + 1)) % div
	}
	fmt.Println(prod - 1)
}

Score : 200 points

Problem Statement

You are given a string S of length N.
Among its subsequences, count the ones such that all characters are different, modulo 10^9+7. Two subsequences are considered different if their characters come from different positions in the string, even if they are the same as strings.

Here, a subsequence of a string is a concatenation of one or more characters from the string without changing the order.

Constraints

1 \leq N \leq 100000

S consists of lowercase English letters.

|S|=N

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of the subsequences such that all characters are different, modulo 10^9+7.

Sample Input 1

4
abcd

Sample Output 1

15

Since all characters in S itself are different, all its subsequences satisfy the condition.

Sample Input 2

3
baa

Sample Output 2

5

The answer is five: b, two occurrences of a, two occurrences of ba. Note that we do not count baa, since it contains two as.

Sample Input 3

5
abcab

Sample Output 3

17


package main

import "fmt"

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

  fmt.Println(H*W-h*W-H*w)
}

Score : 100 points

Problem Statement

There are H rows and W columns of white square cells.

You will choose h of the rows and w of the columns, and paint all of the cells contained in those rows or columns.

How many white cells will remain?

It can be proved that this count does not depend on what rows and columns are chosen.

Constraints

All values in input are integers.

1 \leq H, W \leq 20

1 \leq h \leq H

1 \leq w \leq W

Input

Input is given from Standard Input in the following format:

H W
h w

Output

Print the number of white cells that will remain.

Sample Input 1

3 2
2 1

Sample Output 1

1

There are 3 rows and 2 columns of cells. When two rows and one column are chosen and painted in black, there is always one white cell that remains.

Sample Input 2

5 5
2 3

Sample Output 2

6

Sample Input 3

2 4
2 4

Sample Output 3

0


package main

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

var sc = bufio.NewScanner(os.Stdin)

func main() {
	scanInit()

	n := nextInt()
	m := nextInt()
	p := make([]pair, n)

	for i := 0; i < n; i++ {
		p[i].a = nextInt()
		p[i].b = nextInt()
	}

	sortPairs(p)

	cost := 0
	nowM := 0
	for i := 0; i < n; i++ {
		if nowM >= m {
			break
		}

		buy := min(p[i].b, m-nowM)
		cost += p[i].a * buy
		nowM += buy
	}
	fmt.Println(cost)

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

// Pair ソート
// s := make([]pair, n)
// sortPairs(s)
type pair struct { // f=first, s=second
	a int
	b int
}

// []pair をf->sの順でソートする
func sortPairs(s []pair) {
	sort.Slice(s, func(i, j int) bool {
		if s[i].a == s[j].a { // fが同じ場合はsを比較する
			return s[i].b < s[j].b
		}
		return s[i].a < s[j].a
	})
	return
}

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

Score : 300 points

Problem Statement

Hearing that energy drinks increase rating in those sites, Takahashi decides to buy up M cans of energy drinks.

There are N stores that sell energy drinks. In the i-th store, he can buy at most B_i cans of energy drinks for A_i yen (the currency of Japan) each.

What is the minimum amount of money with which he can buy M cans of energy drinks?

It is guaranteed that, in the given inputs, a sufficient amount of money can always buy M cans of energy drinks.

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^5

B_1 + ... + B_N \geq M

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the minimum amount of money with which Takahashi can buy M cans of energy drinks.

Sample Input 1

2 5
4 9
2 4

Sample Output 1

12

With 12 yen, we can buy one drink at the first store and four drinks at the second store, for the total of five drinks. However, we cannot buy 5 drinks with 11 yen or less.

Sample Input 2

4 30
6 18
2 5
3 10
7 9

Sample Output 2

130

Sample Input 3

1 100000
1000000000 100000

Sample Output 3

100000000000000

The output may not fit into a 32-bit integer type.


package main

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

var scanner = bufio.NewScanner(os.Stdin)

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

func ScanInt() (ret int) {
	var err error
	if scanner.Scan() {
		txt := scanner.Text()
		if ret, err = strconv.Atoi(txt); err != nil {
			_, _ = fmt.Fprintf(os.Stderr, err.Error())
		}
	}
	return
}

type Pair struct{ A, B int }

func main() {
	n, m := ScanInt(), ScanInt()

	pairs := make([]Pair, n)
	for i := 0; i < n; i++ {
		pairs[i].A, pairs[i].B = ScanInt(), ScanInt()
	}
	sort.Slice(pairs, func(i, j int) bool {
		return pairs[i].A < pairs[j].A
	})

	money := 0
	for i := 0; i < n; i++ {
		if m-pairs[i].B <= 0 {
			money += m * pairs[i].A
			break
		} else {
			money += pairs[i].A * pairs[i].B
			m -= pairs[i].B
		}
	}
	fmt.Println(money)
}


package main

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

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

type A [][]int

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

func main() {
	nm := strings.Split(readLine(), " ")
	n, _ := strconv.Atoi(nm[0])
	m, _ := strconv.Atoi(nm[1])
	var ab [][]int
	for i := 0; i < n; i++ {
		abstr := strings.Split(readLine(), " ")
		a, _ := strconv.Atoi(abstr[0])
		b, _ := strconv.Atoi(abstr[1])
		ab = append(ab, []int{a, b})
	}
	sort.Sort(A(ab))
	var cost int
	for _, v := range ab {
		if m > v[1] {
			cost += v[0] * v[1]
			m -= v[1]
		} else {
			cost += v[0] * m
			break
		}
	}
	fmt.Println(cost)
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

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

func main() {
	line := nextLine()
	parts := strings.Split(line, " ")
	a, _ := strconv.Atoi(parts[0])
	b, _ := strconv.Atoi(parts[1])
	k, _ := strconv.Atoi(parts[2])

	ret := make([]int, 0)
	for i := 1; i < 101; i++ {
		if a%i == 0 && b%i == 0 {
			ret = append(ret, i)
		}
	}

	fmt.Println(ret[len(ret)-k])
}

Score : 200 points

Problem Statement

You are given positive integers A and B.

Find the K-th largest positive integer that divides both A and B.

The input guarantees that there exists such a number.

Constraints

All values in input are integers.

1 \leq A, B \leq 100

The K-th largest positive integer that divides both A and B exists.

K \geq 1

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the K-th largest positive integer that divides both A and B.

Sample Input 1

8 12 2

Sample Output 1

2

Three positive integers divides both 8 and 12: 1, 2 and 4.
Among them, the second largest is 2.

Sample Input 2

100 50 4

Sample Output 2

5

Sample Input 3

1 1 1

Sample Output 3

1


package main

import (
	"fmt"
	"math"
	"strings"
)

func main() {
	var s string
	fmt.Scan(&s)
	fmt.Println(math.Min(float64(strings.Count(s, "0")), float64(strings.Count(s, "1"))) * 2)
}

Score : 300 points

Problem Statement

There are N cubes stacked vertically on a desk.

You are given a string S of length N. The color of the i-th cube from the bottom is red if the i-th character in S is 0, and blue if that character is 1.

You can perform the following operation any number of times: choose a red cube and a blue cube that are adjacent, and remove them. Here, the cubes that were stacked on the removed cubes will fall down onto the object below them.

At most how many cubes can be removed?

Constraints

1 \leq N \leq 10^5

|S| = N

Each character in S is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum number of cubes that can be removed.

Sample Input 1

0011

Sample Output 1

4

All four cubes can be removed, by performing the operation as follows:

Remove the second and third cubes from the bottom. Then, the fourth cube drops onto the first cube.

Remove the first and second cubes from the bottom.

Sample Input 2

11011010001011

Sample Output 2

12

Sample Input 3

0

Sample Output 3

0