# frozen_string_literal: true

_ = gets
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
count = 0

b.each_with_index do |i, idx|
  if i > a[idx]
    rest = i - a[idx]

    if rest > a[idx + 1]
      count += a[idx] + a[idx + 1]
      a[idx + 1] = 0
    else
      a[idx + 1] -= rest
      count += i
    end
  else
    count += i
  end
end

puts count

Score : 300 points

Problem Statement

There are N+1 towns. The i-th town is being attacked by A_i monsters.

We have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters.

What is the maximum total number of monsters the heroes can cooperate to defeat?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{N+1}
B_1 B_2 ... B_N

Output

Print the maximum total number of monsters the heroes can defeat.

Sample Input 1

2
3 5 2
4 5

Sample Output 1

9

If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.

The first hero defeats two monsters attacking the first town and two monsters attacking the second town.

The second hero defeats three monsters attacking the second town and two monsters attacking the third town.

Sample Input 2

3
5 6 3 8
5 100 8

Sample Output 2

22

Sample Input 3

2
100 1 1
1 100

Sample Output 3

3


N = gets.to_i
A = gets.split.map(&:to_i)
B = gets.split.map(&:to_i)

ans = 0

B.each_with_index do |b, idx|
  n = [A[idx], b].min
  b -= n

  m = [A[idx + 1], b].min
  A[idx + 1] -= m

  ans += n + m
end

puts ans

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

Submitted source-code file size, in bytes.

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

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


N = gets.to_i
A = gets.split.map(&:to_i)
B = gets.split.map(&:to_i)

c = 0
N.times do |i|
  b = B[i]
  n = [b, A[i]].min
  b -= n
  c += n
  b = [b, A[i+1]].min
  A[i+1] -= b
  c += b
end
puts c


# 桁DP 最適化もうむり

MOD = 10 ** 9 + 7
s = gets.chomp.split("")
question = s.map { |c| c == "?" }
length = s.length
# dp[上から i 桁まで][13 で割ったあまり]の出現回数
# 桁の自由度(smaller)は本問では不要
dp = (length + 1).times.map { Array.new(13, 0) }
# 初期化
# dp[0 桁目まで][13 で割ったあまりが 0]の出現回数は 1
dp[0][0] = 1
# （右から見た）桁数 % 6
mod_memo = [4, 1, 10, 9, 12, 3]
all_nums = (0..9).to_a

# 桁数分ループする（i は遷移元の桁）
length.times do |i|
  # 13 で割ったあまりの増加分を求める
  addition = mod_memo[(length - i) % 6]
  # 桁の数字が "?" の場合は 10 回ループ、それ以外の場合はその数字のみ
  nums = question[i] ? all_nums : [s[i].to_i]
  dp_current = dp[i]
  # 最適化のため、事前に 1 行分だけ浅いコピーをする
  # dp_next と dp[i + 1] の object_id は同じ
  dp_next = dp[i + 1]
  # 13 で割ったあまりごとに確認する
  13.times do |mod|
    current_count = dp_current[mod] % MOD
    nums.each do |d|
      # 次の桁に遷移する
      dp_next[(mod + addition * d) % 13] += current_count
    end
  end
end

puts dp[length][5] %= MOD

Score : 400 points

Problem Statement

Given is a string S. Each character in S is either a digit (0, ..., 9) or ?.

Among the integers obtained by replacing each occurrence of ? with a digit, how many have a remainder of 5 when divided by 13? An integer may begin with 0.

Since the answer can be enormous, print the count modulo 10^9+7.

Constraints

S is a string consisting of digits (0, ..., 9) and ?.

1 \leq |S| \leq 10^5

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of integers satisfying the condition, modulo 10^9+7.

Sample Input 1

??2??5

Sample Output 1

768

For example, 482305, 002865, and 972665 satisfy the condition.

Sample Input 2

?44

Sample Output 2

1

Only 044 satisfies the condition.

Sample Input 3

7?4

Sample Output 3

0

We may not be able to produce an integer satisfying the condition.

Sample Input 4

?6?42???8??2??06243????9??3???7258??5??7???????774????4?1??17???9?5?70???76???

Sample Output 4

153716888


require 'prime'
s = gets.to_i
primes = Prime.prime_division(s)
a = primes.first.first
b = s.div(a)
answer = [0, 0, a, 0, 0, b]
puts answer.join(' ')

Score : 400 points

Problem Statement

Given is an integer S.
Find a combination of six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfies all of the following conditions:

0 \leq X_1,Y_1,X_2,Y_2,X_3,Y_3 \leq 10^9

The area of the triangle in a two-dimensional plane whose vertices are (X_1,Y_1),(X_2,Y_2), and (X_3,Y_3) is S/2.

We can prove that there always exist six integers that satisfy the conditions under the constraints of this problem.

Constraints

1 \leq S \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

S

Output

Print six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfy the conditions, in this order, with spaces in between.
If multiple solutions exist, any of them will be accepted.

Sample Input 1

3

Sample Output 1

1 0 2 2 0 1

The area of the triangle in a two-dimensional plane whose vertices are (1,0),(2,2), and (0,1) is 3/2.
Printing 3 0 3 1 0 1 or 1 0 0 1 2 2 will also be accepted.

Sample Input 2

100

Sample Output 2

0 0 10 0 0 10

Sample Input 3

311114770564041497

Sample Output 3

314159265 358979323 846264338 327950288 419716939 937510582


n=gets.to_i
aNs=[]
n.times do
  aNs << gets.to_i
end
n.times do |index|
  temp = aNs[0...index] + aNs[(index+1)...n]
  puts temp.max
end

Score : 300 points

Problem Statement

You are given a sequence of length N: A_1, A_2, ..., A_N.
For each integer i between 1 and N (inclusive), answer the following question:

Find the maximum value among the N-1 elements other than A_i in the sequence.

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 200000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the maximum value among the N-1 elements other than A_i in the sequence.

Sample Input 1

3
1
4
3

Sample Output 1

4
3
4

The maximum value among the two elements other than A_1, that is, A_2 = 4 and A_3 = 3, is 4.

The maximum value among the two elements other than A_2, that is, A_1 = 1 and A_3 = 3, is 3.

The maximum value among the two elements other than A_3, that is, A_1 = 1 and A_2 = 4, is 4.

Sample Input 2

2
5
5

Sample Output 2

5
5


n=gets.to_i
a=[]
max=0
(0..n-1).each do |i|
  a[i]=gets.to_i
end
max=a.max
idx=a.index(max)
a.sort!
(0..n-1).each do |i|
  if a[-2]==a[-1]
    puts a[-1]
  elsif i == idx
    puts a[-2]
  else
    puts a[-1]
  end
end


n=gets.to_i
a=[]
max=0
max_idx=0
n.times do|i|
	a[i]=gets.to_i
	if(a[i]>max)
		max=a[i]
		max_idx=i
	end
end
for i in 0..n-1
	if(i==max_idx)
		a.delete_at(max_idx)
		puts(a.max)
		i-=1
	else
		puts(max)
	end
end


N = gets.to_i
as = gets.split " "

bs = Array.new(N, 0)

i = N
while (i > 0)
	i -= 1
	if (as[i] == "1")
		j = i
		sum = 0
		while (j < N)
			sum += bs[j].to_i
			j += (i + 1)
		end
		bs[i] = 1 - (sum % 2)
	end
end

ans = []
(0...N).each do |i|
	ans << (i + 1) if (bs[i] == 1)
end

puts ans.size
puts ans.join " "

Score : 400 points

Problem Statement

There are N empty boxes arranged in a row from left to right.
The integer i is written on the i-th box from the left (1 \leq i \leq N).

For each of these boxes, Snuke can choose either to put a ball in it or to put nothing in it.

We say a set of choices to put a ball or not in the boxes is good when the following condition is satisfied:

For every integer i between 1 and N (inclusive), the total number of balls contained in the boxes with multiples of i written on them is congruent to a_i modulo 2.

Does there exist a good set of choices? If the answer is yes, find one good set of choices.

Constraints

All values in input are integers.

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

a_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If a good set of choices does not exist, print -1.

If a good set of choices exists, print one such set of choices in the following format:

M
b_1 b_2 ... b_M

where M denotes the number of boxes that will contain a ball, and b_1,\ b_2,\ ...,\ b_M are the integers written on these boxes, in any order.

Sample Input 1

3
1 0 0

Sample Output 1

1
1

Consider putting a ball only in the box with 1 written on it.

There are three boxes with multiples of 1 written on them: the boxes with 1, 2, and 3. The total number of balls contained in these boxes is 1.

There is only one box with a multiple of 2 written on it: the box with 2. The total number of balls contained in these boxes is 0.

There is only one box with a multiple of 3 written on it: the box with 3. The total number of balls contained in these boxes is 0.

Thus, the condition is satisfied, so this set of choices is good.

Sample Input 2

5
0 0 0 0 0

Sample Output 2

0

Putting nothing in the boxes can be a good set of choices.


n = gets.chomp.to_i
a = gets.chomp.split.map(&:to_i)
b = []
n.downto(1) do |i|
  s = 0
  2.upto(n) do |j|
    if i * j > n then
      break
    end
    s += a[i * j - 1]
  end
  b[i-1] = (a[i-1] + s) % 2
end
puts b.sum
r = ''
1.upto(n) do |i|
  if b[i-1] > 0 then
    r << i.to_s << ' '
  end
end
puts r.chop unless r == ''


`sed 1d`.lines{|x|x=x.to_i
$*[$*.bsearch_index{|y|y<x}||$*.size]=x}
p$*.size

Score : 500 points

Problem Statement

You are given a sequence with N integers: A = \{ A_1, A_2, \cdots, A_N \}.
For each of these N integers, we will choose a color and paint the integer with that color. Here the following condition must be satisfied:

If A_i and A_j (i < j) are painted with the same color, A_i < A_j.

Find the minimum number of colors required to satisfy the condition.

Constraints

1 \leq N \leq 10^5

0 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the minimum number of colors required to satisfy the condition.

Sample Input 1

5
2
1
4
5
3

Sample Output 1

2

We can satisfy the condition with two colors by, for example, painting 2 and 3 red and painting 1, 4, and 5 blue.

Sample Input 2

4
0
0
0
0

Sample Output 2

4

We have to paint all the integers with distinct colors.


n = gets.chomp.to_i
hats = gets.chomp.split(" ").map(&:to_i)

uniq_hats = hats.uniq

if uniq_hats.size == 1 && uniq_hats[0] == 0
  puts "Yes"
  exit
end

uniq_hats_2 = Array.new(3) {Array.new(50, 0)}

if uniq_hats.size == 3 && hats.count(uniq_hats[0]) == hats.count(uniq_hats[1]) && hats.count(uniq_hats[1]) == hats.count(uniq_hats[2])
  uniq_hats.each_with_index do |uniq_hat, index1|
    50.times do |index2|
      uniq_hats_2[index1][index2] = uniq_hat[index2]
    end
  end
else
  puts "No"
  exit
end

uniq_hats_3 = Array.new(50, 0)
uniq_hats_2.each_with_index do |uniq_hat_2, index1|
  50.times do |index2|
    uniq_hats_3[index2] += uniq_hat_2[index2]
  end
end

if uniq_hats_3.include?(1) == false && uniq_hats_3.include?(3) == false
  puts "Yes"
else
  puts "No"
end

Score : 300 points

Problem Statement

Snuke has N hats. The i-th hat has an integer a_i written on it.

There are N camels standing in a circle.
Snuke will put one of his hats on each of these camels.

If there exists a way to distribute the hats to the camels such that the following condition is satisfied for every camel, print Yes; otherwise, print No.

The bitwise XOR of the numbers written on the hats on both adjacent camels is equal to the number on the hat on itself.

What is XOR?

The bitwise XOR x_1 \oplus x_2 \oplus \ldots \oplus x_n of n non-negative integers x_1, x_2, \ldots, x_n is defined as follows:

- When x_1 \oplus x_2 \oplus \ldots \oplus x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.

For example, 3 \oplus 5 = 6.

Constraints

All values in input are integers.

3 \leq N \leq 10^{5}

0 \leq a_i \leq 10^{9}

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_{N}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

Yes

If we put the hats with 1, 2, and 3 in this order, clockwise, the condition will be satisfied for every camel, so the answer is Yes.

Sample Input 2

4
1 2 4 8

Sample Output 2

No

There is no such way to distribute the hats; the answer is No.


#a = gets.to_i
a,b,c = gets.split.map(&:to_i)
puts a * b > c ? c : a * b

Score : 100 points

Problem Statement

N of us are going on a trip, by train or taxi.

The train will cost each of us A yen (the currency of Japan).

The taxi will cost us a total of B yen.

How much is our minimum total travel expense?

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq A \leq 50

1 \leq B \leq 50

Input

Input is given from Standard Input in the following format:

N A B

Output

Print an integer representing the minimum total travel expense.

Sample Input 1

4 2 9

Sample Output 1

8

The train will cost us 4 \times 2 = 8 yen, and the taxi will cost us 9 yen, so the minimum total travel expense is 8 yen.

Sample Input 2

4 2 7

Sample Output 2

7

Sample Input 3

4 2 8

Sample Output 3

8


require "complex"

N, D = gets.chomp.split.map(&:to_i)
X = N.times.map { gets.chomp.split.map(&:to_f) }

ans = 0
X.combination(2) { |y, z|
  sum = 0
  for i in 0..D - 1
    sum += (y[i] - z[i]) ** 2
  end
  n = 1
  while n ** 2 <= sum
    if n ** 2 == sum
      ans += 1
      break
    end
    n += 1
  end
}

puts ans

Score : 200 points

Problem Statement

There are N points in a D-dimensional space.

The coordinates of the i-th point are (X_{i1}, X_{i2}, ..., X_{iD}).

The distance between two points with coordinates (y_1, y_2, ..., y_D) and (z_1, z_2, ..., z_D) is \sqrt{(y_1 - z_1)^2 + (y_2 - z_2)^2 + ... + (y_D - z_D)^2}.

How many pairs (i, j) (i < j) are there such that the distance between the i-th point and the j-th point is an integer?

Constraints

All values in input are integers.

2 \leq N \leq 10

1 \leq D \leq 10

-20 \leq X_{ij} \leq 20

No two given points have the same coordinates. That is, if i \neq j, there exists k such that X_{ik} \neq X_{jk}.

Input

Input is given from Standard Input in the following format:

N D
X_{11} X_{12} ... X_{1D}
X_{21} X_{22} ... X_{2D}
\vdots
X_{N1} X_{N2} ... X_{ND}

Output

Print the number of pairs (i, j) (i < j) such that the distance between the i-th point and the j-th point is an integer.

Sample Input 1

3 2
1 2
5 5
-2 8

Sample Output 1

1

The number of pairs with an integer distance is one, as follows:

The distance between the first point and the second point is \sqrt{|1-5|^2 + |2-5|^2} = 5, which is an integer.

The distance between the second point and the third point is \sqrt{|5-(-2)|^2 + |5-8|^2} = \sqrt{58}, which is not an integer.

The distance between the third point and the first point is \sqrt{|-2-1|^2+|8-2|^2} = 3\sqrt{5}, which is not an integer.

Sample Input 2

3 4
-3 7 8 2
-12 1 10 2
-2 8 9 3

Sample Output 2

2

Sample Input 3

5 1
1
2
3
4
5

Sample Output 3

10


n = gets.to_i
datas = gets.split(" ").map{|n|n.to_i}
equations = []
for num in (1..n) do
  newequation = Array.new(n+1,0)
  newequation[-1] = datas[num-1]
  newequation[num-1] = 0.5
  if num == n
    newequation[0] = 0.5
  else
    newequation[num] = 0.5
  end
  equations << newequation
end

$answer = ""

class GaussJorden
  def initialize ary,nn
    # 係数
    @a = ary
    # 次元の数
    @n = nn
  end

  # 計算・結果出力
  def exec
    @n.times do |k|
      # ピボット係数
      p = @a[k][k]
      # ピボット行を p で除算
      k.upto(@n) { |j| @a[k][j] /= p.to_f }
      # ピボット列の掃き出し
      @n.times do |i|
        next if i == k
        d = @a[i][k]
        k.upto(@n) { |j| @a[i][j] -= d * @a[k][j] }
      end
    end
    # 結果出力
    display_answers
  rescue => e
    raise
  end

  private

  # 結果出力
  def display_answers
    0.upto(@n - 1) {|k| $answer += @a[k][@n].to_i.to_s + " "}
  rescue => e
    raise
  end
end

    # 計算クラスインスタンス化
    obj = GaussJorden.new(equations,n)
    # 連立方程式を解く（ガウス・ジョルダン法）
obj.exec
puts $answer

Score : 400 points

Problem Statement

There are N mountains in a circle, called Mountain 1, Mountain 2, ..., Mountain N in clockwise order. N is an odd number.

Between these mountains, there are N dams, called Dam 1, Dam 2, ..., Dam N. Dam i (1 \leq i \leq N) is located between Mountain i and i+1 (Mountain N+1 is Mountain 1).

When Mountain i (1 \leq i \leq N) receives 2x liters of rain, Dam i-1 and Dam i each accumulates x liters of water (Dam 0 is Dam N).

One day, each of the mountains received a non-negative even number of liters of rain.

As a result, Dam i (1 \leq i \leq N) accumulated a total of A_i liters of water.

Find the amount of rain each of the mountains received. We can prove that the solution is unique under the constraints of this problem.

Constraints

All values in input are integers.

3 \leq N \leq 10^5-1

N is an odd number.

0 \leq A_i \leq 10^9

The situation represented by input can occur when each of the mountains receives a non-negative even number of liters of rain.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print N integers representing the number of liters of rain Mountain 1, Mountain 2, ..., Mountain N received, in this order.

Sample Input 1

3
2 2 4

Sample Output 1

4 0 4

If we assume Mountain 1, 2, and 3 received 4, 0, and 4 liters of rain, respectively, it is consistent with this input, as follows:

Dam 1 should have accumulated \frac{4}{2} + \frac{0}{2} = 2 liters of water.

Dam 2 should have accumulated \frac{0}{2} + \frac{4}{2} = 2 liters of water.

Dam 3 should have accumulated \frac{4}{2} + \frac{4}{2} = 4 liters of water.

Sample Input 2

5
3 8 7 5 5

Sample Output 2

2 4 12 2 8

Sample Input 3

3
1000000000 1000000000 0

Sample Output 3

0 2000000000 0


Score : 100 points

Problem Statement

You are given a 4-character string S consisting of uppercase English letters.
Determine if S consists of exactly two kinds of characters which both appear twice in S.

Constraints

The length of S is 4.

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S consists of exactly two kinds of characters which both appear twice in S, print Yes; otherwise, print No.

Sample Input 1

ASSA

Sample Output 1

Yes

S consists of A and S which both appear twice in S.

Sample Input 2

STOP

Sample Output 2

No

Sample Input 3

FFEE

Sample Output 3

Yes

Sample Input 4

FREE

Sample Output 4

No


s = gets.chomp!.to_s
s_list=s.split("")
s_list.sort!
#p s_list
if s_list[0] == s_list[1] && s_list[2] == s_list[3]
  print("Yes\n")
else
  print("No\n")
end


a=gets.chomp.to_i
b=gets.chomp.map(&:to_i).sort!
puts b[a/2]-b[a/2-1]

Score : 300 points

Problem Statement

Takahashi made N problems for competitive programming.
The problems are numbered 1 to N, and the difficulty of Problem i is represented as an integer d_i (the higher, the harder).

He is dividing the problems into two categories by choosing an integer K, as follows:

A problem with difficulty K or higher will be for ARCs.

A problem with difficulty lower than K will be for ABCs.

How many choices of the integer K make the number of problems for ARCs and the number of problems for ABCs the same?

Problem Statement

2 \leq N \leq 10^5

N is an even number.

1 \leq d_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 ... d_N

Output

Print the number of choices of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 1

6
9 1 4 4 6 7

Sample Output 1

2

If we choose K=5 or 6, Problem 1, 5, and 6 will be for ARCs, Problem 2, 3, and 4 will be for ABCs, and the objective is achieved.
Thus, the answer is 2.

Sample Input 2

8
9 1 14 5 5 4 4 14

Sample Output 2

0

There may be no choice of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 3

14
99592 10342 29105 78532 83018 11639 92015 77204 30914 21912 34519 80835 100000 1

Sample Output 3

42685


Score : 400 points

Problem Statement

There are K blue balls and N-K red balls. The balls of the same color cannot be distinguished. Snuke and Takahashi are playing with these balls.

First, Snuke will arrange the N balls in a row from left to right.

Then, Takahashi will collect only the K blue balls. In one move, he can collect any number of consecutive blue balls. He will collect all the blue balls in the fewest moves possible.

How many ways are there for Snuke to arrange the N balls in a row so that Takahashi will need exactly i moves to collect all the blue balls? Compute this number modulo 10^9+7 for each i such that 1 \leq i \leq K.

Constraints

1 \leq K \leq N \leq 2000

Input

Input is given from Standard Input in the following format:

N K

Output

Print K lines. The i-th line (1 \leq i \leq K) should contain the number of ways to arrange the N balls so that Takahashi will need exactly i moves to collect all the blue balls, modulo 10^9+7.

Sample Input 1

5 3

Sample Output 1

3
6
1

There are three ways to arrange the balls so that Takahashi will need exactly one move: (B, B, B, R, R), (R, B, B, B, R), and (R, R, B, B, B). (R and B stands for red and blue, respectively).

There are six ways to arrange the balls so that Takahashi will need exactly two moves: (B, B, R, B, R), (B, B, R, R, B), (R, B, B, R, B), (R, B, R, B, B), (B, R, B, B, R), and (B, R, R, B, B).

There is one way to arrange the balls so that Takahashi will need exactly three moves: (B, R, B, R, B).

Sample Input 2

2000 3

Sample Output 2

1998
3990006
327341989

Be sure to print the numbers of arrangements modulo 10^9+7.


TEISU = 1_000_000_007

class Integer
  def combination(k)
    return 1 if k.zero?

    (self - k + 1..self).inject(:*) / k.factorial
  end

  def factorial
    return 1 if self.zero?

    (1..self).inject(:*)
  end
end

N,K = gets.chomp.split.map(&:to_i)

1.upto(K) do |i|
  where = (N-K+1).combination(i) % TEISU
  how = (K-1).combination(i-1) % TEISU
  puts (where * how)
end


def factorial(number)
  (1..number).inject(1,:*)
end

def calc_p(first, second)
  factorial(first) / factorial(first - second)
end

def calc_c(first, second)
  factorial(first) / factorial(second) / factorial(first - second)
end

n, k = gets.chomp.split(/ /).map(&:to_i)

1.upto(k) { |i|
  # (n-k+1)個のスキマからi個のスキマを選ぶ
  # スキマへのボールの入れ方は "k-1個のスキマから(i-1)個のスキマを選ぶ"
  puts calc_c(n - k + 1, i) * calc_c(k - 1, i - 1) % (10**9 + 7)
}


require 'pp'

N,M = readline.chomp.split.map(&:to_i)
g1 = Array.new(N) { [] }
M.times do |i|
  u,v = readline.chomp.split.map(&:to_i).map {|x| x-1}
  g1[u] << v
end
#pp g1

def dfs(g, i, d, ans)
  if d == 0
    ans << i
    return
  end

  g[i].each do |j|
    dfs(g, j, d-1, ans)
  end
end
$memo = {}
def neighbors(g,i)
  if $memo.has_key?(i)
    return $memo[i]
  end
  a = []
  dfs(g,i,3,a)
  $memo[i] = a
  return a
end

S,T = readline.chomp.split.map(&:to_i).map {|x| x-1}

cnt = 0
visited = {}
visited[S] = true
queue = []
queue.push(S)
d = Array.new(N,-1)
d[S] = 0
while v = queue.shift
  #pp v,queue
  if v == T
    break
  end
  neighbors(g1,v).each do |j|
    if not visited[j]
      visited[j] = true
      d[j] = d[v]+1
      queue.push(j)
    end
  end
end
puts d[T]

Score : 500 points

Problem Statement

Ken loves ken-ken-pa (Japanese version of hopscotch). Today, he will play it on a directed graph G.
G consists of N vertices numbered 1 to N, and M edges. The i-th edge points from Vertex u_i to Vertex v_i.

First, Ken stands on Vertex S. He wants to reach Vertex T by repeating ken-ken-pa. In one ken-ken-pa, he does the following exactly three times: follow an edge pointing from the vertex on which he is standing.

Determine if he can reach Vertex T by repeating ken-ken-pa. If the answer is yes, find the minimum number of ken-ken-pa needed to reach Vertex T. Note that visiting Vertex T in the middle of a ken-ken-pa does not count as reaching Vertex T by repeating ken-ken-pa.

Constraints

2 \leq N \leq 10^5

0 \leq M \leq \min(10^5, N (N-1))

1 \leq u_i, v_i \leq N(1 \leq i \leq M)

u_i \neq v_i (1 \leq i \leq M)

If i \neq j, (u_i, v_i) \neq (u_j, v_j).

1 \leq S, T \leq N

S \neq T

Input

Input is given from Standard Input in the following format:

N M
u_1 v_1
:
u_M v_M
S T

Output

If Ken cannot reach Vertex T from Vertex S by repeating ken-ken-pa, print -1.
If he can, print the minimum number of ken-ken-pa needed to reach vertex T.

Sample Input 1

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

Sample Output 1

2

Ken can reach Vertex 3 from Vertex 1 in two ken-ken-pa, as follows: 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 in the first ken-ken-pa, then 4 \rightarrow 1 \rightarrow 2 \rightarrow 3 in the second ken-ken-pa. This is the minimum number of ken-ken-pa needed.

Sample Input 2

3 3
1 2
2 3
3 1
1 2

Sample Output 2

-1

Any number of ken-ken-pa will bring Ken back to Vertex 1, so he cannot reach Vertex 2, though he can pass through it in the middle of a ken-ken-pa.

Sample Input 3

2 0
1 2

Sample Output 3

-1

Vertex S and Vertex T may be disconnected.

Sample Input 4

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

Sample Output 4

2


s = gets.chomp
(s.length - 1).times do |i|
	if s[i] == s[i+1]
		puts "Bad"
		exit
	end
end
puts "Good"

Score : 100 points

Problem Statement

The door of Snuke's laboratory is locked with a security code.

The security code is a 4-digit number. We say the security code is hard to enter when it contains two consecutive digits that are the same.

You are given the current security code S. If S is hard to enter, print Bad; otherwise, print Good.

Constraints

S is a 4-character string consisting of digits.

Input

Input is given from Standard Input in the following format:

S

Output

If S is hard to enter, print Bad; otherwise, print Good.

Sample Input 1

3776

Sample Output 1

Bad

The second and third digits are the same, so 3776 is hard to enter.

Sample Input 2

8080

Sample Output 2

Good

There are no two consecutive digits that are the same, so 8080 is not hard to enter.

Sample Input 3

1333

Sample Output 3

Bad

Sample Input 4

0024

Sample Output 4

Bad


n,l = gets.chomp.split.map(&:to_i)

pai = []

(1..n).each do |i|
  pai.push(l + i -1)
end

puts pai.inject(:+) - pai.min_by{|x| (x-0).abs}

Score : 200 points

Problem Statement

You have N apples, called Apple 1, Apple 2, Apple 3, ..., Apple N. The flavor of Apple i is L+i-1, which can be negative.

You can make an apple pie using one or more of the apples. The flavor of the apple pie will be the sum of the flavors of the apples used.

You planned to make an apple pie using all of the apples, but being hungry tempts you to eat one of them, which can no longer be used to make the apple pie.

You want to make an apple pie that is as similar as possible to the one that you planned to make. Thus, you will choose the apple to eat so that the flavor of the apple pie made of the remaining N-1 apples will have the smallest possible absolute difference from the flavor of the apple pie made of all the N apples.

Find the flavor of the apple pie made of the remaining N-1 apples when you choose the apple to eat as above.

We can prove that this value is uniquely determined.

Constraints

2 \leq N \leq 200

-100 \leq L \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N L

Output

Find the flavor of the apple pie made of the remaining N-1 apples when you optimally choose the apple to eat.

Sample Input 1

5 2

Sample Output 1

18

The flavors of Apple 1, 2, 3, 4, and 5 are 2, 3, 4, 5, and 6, respectively. The optimal choice is to eat Apple 1, so the answer is 3+4+5+6=18.

Sample Input 2

3 -1

Sample Output 2

0

The flavors of Apple 1, 2, and 3 are -1, 0, and 1, respectively. The optimal choice is to eat Apple 2, so the answer is (-1)+1=0.

Sample Input 3

30 -50

Sample Output 3

-1044


n,l = gets.split.map(&:to_i)
s = []
(1..n).each {|i| s.push((l+i-1))}
s.delete_at(0)
p s.sum


n, l = gets.split.map(&:to_i)

if l >= 0 then
  puts (l+1..(n+l-1)).inject(:+)
elsif (l < 0) && (n+l > 0) then
  puts (l..(n+l-1)).inject(:+)
else
  puts (l..(n+l-1)).inject(:+)-(n+l-1)
end


n, l = gets.split.map(&:to_i)
a = []
n.times {|i| a << i + l}
sum = 0
a.each {|i| sum += i}
if sum < 0
  puts sum + a.map{|i| i < 0 ? -1 * i : i}.sort.first
else
  puts sum - a.map{|i| i < 0 ? -1 * i : i}.sort.first
end


a, b, c, d = gets.chomp.split(" ").map(&:to_i)
res = b/c - a/c + b/d - a/d - a/(c.lcm(d)) - b/(c.lcm(d))
puts b-a-res+1

Score : 300 points

Problem Statement

You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Constraints

1\leq A\leq B\leq 10^{18}

1\leq C,D\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Sample Input 1

4 9 2 3

Sample Output 1

2

5 and 7 satisfy the condition.

Sample Input 2

10 40 6 8

Sample Output 2

23

Sample Input 3

314159265358979323 846264338327950288 419716939 937510582

Sample Output 3

532105071133627368


A, B, C, D = gets.split(' ').map(&:to_i)
puts (A..B).select{|e| e % C != 0 and e % D != 0}.length


a, b, c, d = gets.split.map(&:to_i)

x = b - a + 1

ya = (b / c) - (a / c)
ya += 1 if a % c == 0

yb = (b / d) - (a / d)
yb += 1 if a % d == 0

z = (b / c.lcm(d)) - (a / c.lcm(d))
z += 1 if a % c.lcm(d) == 0

puts x - (ya + yb - z)


A, B, C, D = $stdin.read.chomp.split(" ").map(&:to_i)

res = B - A + 1

cis = (A / C.to_f).ceil
cie = (B / C.to_f).floor
ccount = if cis == 1 && cie == 0
           0
         else
           cie - cis + 1
         end

dis = (A / D.to_f).ceil
die = (B / D.to_f).floor
dcount = if dis == 1 && die == 0
           0
         else
           die - dis + 1
         end

_gcd = C.gcd(D)
_cd = C*D/_gcd
cdis = (A / (_cd).to_f).ceil
cdie = (B / (_cd).to_f).floor
cdcount = if cdis == 1 && cdie == 0
            0
          else
            cdie - cdis + 1
          end

puts res - ccount - dcount + cdcount


N = gets.to_i

tasks = N.times.map do
  cost, limit = gets.split.map(&:to_i)
  [cost, limit]
end

tasks = tasks.sort_by { |i| i[1] }

now_time = 0
flag = "Yes"
tasks.each do |task|
  now_time += task[0]
  if now_time > task[1]
    flag = "No"
    break
  end
end

puts flag

Score: 400 points

Problem Statement

Kizahashi, who was appointed as the administrator of ABC at National Problem Workshop in the Kingdom of AtCoder, got too excited and took on too many jobs.

Let the current time be time 0. Kizahashi has N jobs numbered 1 to N.

It takes A_i units of time for Kizahashi to complete Job i. The deadline for Job i is time B_i, and he must complete the job before or at this time.

Kizahashi cannot work on two or more jobs simultaneously, but when he completes a job, he can start working on another immediately.

Can Kizahashi complete all the jobs in time? If he can, print Yes; if he cannot, print No.

Constraints

All values in input are integers.

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

1 \leq A_i, B_i \leq 10^9 (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
.
.
.
A_N B_N

Output

If Kizahashi can complete all the jobs in time, print Yes; if he cannot, print No.

Sample Input 1

5
2 4
1 9
1 8
4 9
3 12

Sample Output 1

Yes

He can complete all the jobs in time by, for example, doing them in the following order:

Do Job 2 from time 0 to 1.

Do Job 1 from time 1 to 3.

Do Job 4 from time 3 to 7.

Do Job 3 from time 7 to 8.

Do Job 5 from time 8 to 11.

Note that it is fine to complete Job 3 exactly at the deadline, time 8.

Sample Input 2

3
334 1000
334 1000
334 1000

Sample Output 2

No

He cannot complete all the jobs in time, no matter what order he does them in.

Sample Input 3

30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720

Sample Output 3

Yes


i,j = gets.strip.split(' ').map(&:to_i)
r = 0
r = 10 if i >= j
puts r

Score : 100 points

Problem Statement

X and A are integers between 0 and 9 (inclusive).

If X is less than A, print 0; if X is not less than A, print 10.

Constraints

0 \leq X, A \leq 9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X A

Output

If X is less than A, print 0; if X is not less than A, print 10.

Sample Input 1

3 5

Sample Output 1

0

3 is less than 5, so we should print 0.

Sample Input 2

7 5

Sample Output 2

10

7 is not less than 5, so we should print 10.

Sample Input 3

6 6

Sample Output 3

10

6 is not less than 6, so we should print 10.


a=gets.split.map(&:to_i)
puts a[0] >= a[1] ? 10 : 0


x, a = gets.chomp.split.map(&:to_i)
puts (x < a) ? 0 : 10


n, x = gets.split.map(&:to_i)

l = gets.split.map(&:to_i)

sum = [0]
l.each do |i|
  sum << sum[-1] + i
end

puts sum.select{|i| i <= x }.count

Score : 200 points

Problem Statement

A ball will bounce along a number line, making N + 1 bounces. It will make the first bounce at coordinate D_1 = 0, and the i-th bounce (2 \leq i \leq N+1) at coordinate D_i = D_{i-1} + L_{i-1}.

How many times will the ball make a bounce where the coordinate is at most X?

Constraints

1 \leq N \leq 100

1 \leq L_i \leq 100

1 \leq X \leq 10000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
L_1 L_2 ... L_{N-1} L_N

Output

Print the number of times the ball will make a bounce where the coordinate is at most X.

Sample Input 1

3 6
3 4 5

Sample Output 1

2

The ball will make a bounce at the coordinates 0, 3, 7 and 12, among which two are less than or equal to 6.

Sample Input 2

4 9
3 3 3 3

Sample Output 2

4

The ball will make a bounce at the coordinates 0, 3, 6, 9 and 12, among which four are less than or equal to 9.


W,H,x,y = gets.chomp.split(" ").map(&:to_f)

ans = (x == W/2 and y == H/2) ? 1 : 0
printf("%f %d", W*H/2, ans)

Score : 300 points

Problem Statement

There is a rectangle in a coordinate plane. The coordinates of the four vertices are (0,0), (W,0), (W,H), and (0,H).
You are given a point (x,y) which is within the rectangle or on its border. We will draw a straight line passing through (x,y) to cut the rectangle into two parts. Find the maximum possible area of the part whose area is not larger than that of the other. Additionally, determine if there are multiple ways to cut the rectangle and achieve that maximum.

Constraints

1 \leq W,H \leq 10^9

0\leq x\leq W

0\leq y\leq H

All values in input are integers.

Input

Input is given from Standard Input in the following format:

W H x y

Output

Print the maximum possible area of the part whose area is not larger than that of the other, followed by 1 if there are multiple ways to cut the rectangle and achieve that maximum, and 0 otherwise.

The area printed will be judged correct when its absolute or relative error is at most 10^{-9}.

Sample Input 1

2 3 1 2

Sample Output 1

3.000000 0

The line x=1 gives the optimal cut, and no other line does.

Sample Input 2

2 2 1 1

Sample Output 2

2.000000 1


n,m=gets.chomp.split.map(&:to_i)
s=gets.chomp.split.map(&:to_i)
t=gets.chomp.split.map(&:to_i)
MOD=10**9+7

dp=Array.new(n+1).map{Array.new(m+1,0)}
sum=Array.new(n+1).map{Array.new(m+1,0)}

1.upto(n) do |i|
  1.upto(m) do |j|
    if s[i-1] == t[j-1]
      dp[i][j]=sum[i-1][j-1] + 1

    else
      dp[i][j]=0
    end
    sum[i][j] = (sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1] + dp[i][j])%MOD
  end
end
puts (sum[n][m] + 1)%MOD

Score : 500 points

Problem Statement

You are given two integer sequences S and T of length N and M, respectively, both consisting of integers between 1 and 10^5 (inclusive).

In how many pairs of a subsequence of S and a subsequence of T do the two subsequences are the same in content?

Here the subsequence of A is a sequence obtained by removing zero or more elements from A and concatenating the remaining elements without changing the order.

For both S and T, we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

Since the answer can be tremendous, print the number modulo 10^9+7.

Constraints

1 \leq N, M \leq 2 \times 10^3

The length of S is N.

The length of T is M.

1 \leq S_i, T_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
S_1 S_2 ... S_{N-1} S_{N}
T_1 T_2 ... T_{M-1} T_{M}

Output

Print the number of pairs of a subsequence of S and a subsequence of T such that the subsequences are the same in content, modulo 10^9+7.

Sample Input 1

2 2
1 3
3 1

Sample Output 1

3

S has four subsequences: (), (1), (3), (1, 3).

T has four subsequences: (), (3), (1), (3, 1).

There are 1 \times 1 pair of subsequences in which the subsequences are both (), 1 \times 1 pair of subsequences in which the subsequences are both (1), and 1 \times 1 pair of subsequences in which the subsequences are both (3), for a total of three pairs.

Sample Input 2

2 2
1 1
1 1

Sample Output 2

6

S has four subsequences: (), (1), (1), (1, 1).

T has four subsequences: (), (1), (1), (1, 1).

There are 1 \times 1 pair of subsequences in which the subsequences are both (), 2 \times 2 pairs of subsequences in which the subsequences are both (1), and 1 \times 1 pair of subsequences in which the subsequences are both (1,1), for a total of six pairs.
Note again that we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

Sample Input 3

4 4
3 4 5 6
3 4 5 6

Sample Output 3

16

Sample Input 4

10 9
9 6 5 7 5 9 8 5 6 7
8 6 8 5 5 7 9 9 7

Sample Output 4

191

Sample Input 5

20 20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Sample Output 5

846527861

Be sure to print the number modulo 10^9+7.


n = gets.to_i
ary = []

n.times do |i|
  ary[i] = gets.split.map(&:to_i)
end
ary2 = {}
n.times do |i|
  ((i+1)..(n-1)).each do |j|
    p = ary[i][0] - ary[j][0]
    q = ary[i][1] - ary[j][1]
    if p < 0
      p *= -1
      q *= -1
    end
    ary2[[p,q]] ||= 0
    ary2[[p,q]] +=1
  end
end

max = ary2.values.max

puts n - max

Score : 300 points

Problem Statement

There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i).

We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation:

Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1.

Find the minimum total cost required to collect all the balls when we optimally choose p and q.

Constraints

1 \leq N \leq 50

|x_i|, |y_i| \leq 10^9

If i \neq j, x_i \neq x_j or y_i \neq y_j.

All values in input are 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 total cost required to collect all the balls.

Sample Input 1

2
1 1
2 2

Sample Output 1

1

If we choose p = 1, q = 1, we can collect all the balls at a cost of 1 by collecting them in the order (1, 1), (2, 2).

Sample Input 2

3
1 4
4 6
7 8

Sample Output 2

1

If we choose p = -3, q = -2, we can collect all the balls at a cost of 1 by collecting them in the order (7, 8), (4, 6), (1, 4).

Sample Input 3

4
1 1
1 2
2 1
2 2

Sample Output 3

2


a,b,c = gets.split.map(&:to_i)
puts (a+b+c)-[a,b,c].max

Score : 100 points

Problem Statement

There are three airports A, B and C, and flights between each pair of airports in both directions.

A one-way flight between airports A and B takes P hours, a one-way flight between airports B and C takes Q hours, and a one-way flight between airports C and A takes R hours.

Consider a route where we start at one of the airports, fly to another airport and then fly to the other airport.

What is the minimum possible sum of the flight times?

Constraints

1 \leq P,Q,R \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

P Q R

Output

Print the minimum possible sum of the flight times.

Sample Input 1

1 3 4

Sample Output 1

4

The sum of the flight times in the route A \rightarrow B \rightarrow C: 1 + 3 = 4 hours

The sum of the flight times in the route A \rightarrow C \rightarrow C: 4 + 3 = 7 hours

The sum of the flight times in the route B \rightarrow A \rightarrow C: 1 + 4 = 5 hours

The sum of the flight times in the route B \rightarrow C \rightarrow A: 3 + 4 = 7 hours

The sum of the flight times in the route C \rightarrow A \rightarrow B: 4 + 1 = 5 hours

The sum of the flight times in the route C \rightarrow B \rightarrow A: 3 + 1 = 4 hours

The minimum of these is 4 hours.

Sample Input 2

3 2 3

Sample Output 2

5


rou = gets.chomp.split.map(&:to_i)
puts rou.min(2).inject(:+).to_i


C = {}

def f(n)
  return C[n] if C[n]

  return 1 if n <= 1

  C[n] = n * f(n - 1)
end

def calc(n)
  return 1 if n == 1

  a = 1
  v = 1
  while v * 2 <= n
    s = v + (n - v * 2)

    # sCv
    _a = f(s)
    _b = f(s - v)
    _c = f(v)

    a += (_a / _b) / _c

    #a += (f(s) / f(s - v)) / f(v)

    v += 1
  end
  a
end

n, m = gets.strip.split(" ").map(&:to_i)

as = []
before = -1
m.times do
  a = gets.to_i
  if before + 1 == a
    puts 0
    exit 0
  end
  before = a
  as << a
end

ans = 1
p = 0

while as.any?
  a = as.shift
  m = a - p - 1

  r =
    if m == 0
      1
    else
      calc(m)
    end

  ans *= r

  p = a + 1
end

if p < n
  ans *= calc(n - p)
end

puts ans % 1000000007

Score : 300 points

Problem Statement

There is a staircase with N steps. Takahashi is now standing at the foot of the stairs, that is, on the 0-th step.
He can climb up one or two steps at a time.

However, the treads of the a_1-th, a_2-th, a_3-th, \ldots, a_M-th steps are broken, so it is dangerous to set foot on those steps.

How many are there to climb up to the top step, that is, the N-th step, without setting foot on the broken steps?
Find the count modulo 1\ 000\ 000\ 007.

Constraints

1 \leq N \leq 10^5

0 \leq M \leq N-1

1 \leq a_1 < a_2 < ... < a_M \leq N-1

Input

Input is given from Standard Input in the following format:

N M
a_1
a_2
.
.
.
a_M

Output

Print the number of ways to climb up the stairs under the condition, modulo 1\ 000\ 000\ 007.

Sample Input 1

6 1
3

Sample Output 1

4

There are four ways to climb up the stairs, as follows:

0 \to 1 \to 2 \to 4 \to 5 \to 6

0 \to 1 \to 2 \to 4 \to 6

0 \to 2 \to 4 \to 5 \to 6

0 \to 2 \to 4 \to 6

Sample Input 2

10 2
4
5

Sample Output 2

0

There may be no way to climb up the stairs without setting foot on the broken steps.

Sample Input 3

100 5
1
23
45
67
89

Sample Output 3

608200469

Be sure to print the count modulo 1\ 000\ 000\ 007.


# 4, 0, 608200469

waru = 1_000_000_007 # あまりを出す
n, m = gets.split.map &:to_i # N段目まで行きたい
a = []
while aa = gets # 壊れてる階段
  a << aa.to_i
end
# p [n, m]
# p a

arr = Array.new(n, 0)
# arr[0] = 1
# arr[1] = 1
0.upto(n) do |i|
  if a.any?{|aa|aa == i}
    arr[i] = 0
    next
  end
  if i == 0 || i == 1
    arr[i] = 1
    next
  end
  arr[i] = arr[i-1] + arr[i-2]
end

# p arr
p (arr[-1] % waru)


h, w = gets.strip.split.map(&:to_i)
room = readlines.map {|line| line.strip.split('') }

def extend(arr)
  ret = []
  darr = arr.dup
  until darr.empty?
    c = darr.shift
    ret << c if c.zero?
    c.times { ret << c }
  end
  ret
end

v_illuminations = Array.new(w){ [] }
h_illuminations = Array.new(h){ [] }

room.each_with_index do |line, i|
  space_count = 0
  line.each_with_index do |space, j|
    if space == '#'
      h_illuminations[i] << space_count if space_count > 0
      h_illuminations[i] << 0
      space_count = 0
    end

    if space == '.'
      space_count += 1
      h_illuminations[i] << space_count if j == line.length-1
    end
  end
end

h_illuminations.each_with_index do |l, i|
  h_illuminations[i] = extend(l)
end

room.transpose.each_with_index do |line, i|
  space_count = 0
  line.each_with_index do |space, j|
    if space == '#'
      v_illuminations[i] << space_count if space_count > 0
      v_illuminations[i] << 0
      space_count = 0
    end

    if space == '.'
      space_count += 1
      v_illuminations[i] << space_count if j == line.length-1
    end
  end
end

#p v_illuminations

v_illuminations.each_with_index do |l, i|
  v_illuminations[i] = extend(l)
end

v_illuminations = v_illuminations.transpose

#h_illuminations.each do |l|
#  p l
#end

#v_illuminations.each do |l|
#  p l
#end

result = 0
h.times do |i|
  w.times do |j|
    tmp_result = v_illuminations[i][j] + h_illuminations[i][j] - 1
    result = tmp_result if tmp_result > result
  end
end

p result

Score : 400 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.

Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it.
The lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right.
In each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.

Snuke wants to maximize the number of squares lighted by the lamp.

You are given H strings S_i (1 \leq i \leq H), each of length W. If the j-th character (1 \leq j \leq W) of S_i is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.

Find the maximum possible number of squares lighted by the lamp.

Constraints

1 \leq H \leq 2,000

1 \leq W \leq 2,000

S_i is a string of length W consisting of # and ..

. occurs at least once in one of the strings S_i (1 \leq i \leq H).

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the maximum possible number of squares lighted by the lamp.

Sample Input 1

4 6
#..#..
.....#
....#.
#.#...

Sample Output 1

8

If Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares.

Sample Input 2

8 8
..#...#.
....#...
##......
..###..#
...#..#.
##....#.
#...#...
###.#..#

Sample Output 2

13


h, w = gets.chomp.split.map(&:to_i)
map = []
h.times{map << gets.chomp}

arr = Array.new(h){Array.new(w){[]}}
h.times{|y|
    last = nil
    w.times{|x|
        if map[y][x] == '#'
            last = x
        else
            arr[y][x] << last
        end
    }
    last = nil
    (w - 1).downto(0){|x|
        if map[y][x] == '#'
            last = x
        else
            arr[y][x] << last
        end
    }
}
w.times{|x|
    last = nil
    h.times{|y|
        if map[y][x] == '#'
            last = y
        else
            arr[y][x] << last
        end
    }
    last = nil
    (h - 1).downto(0){|y|
        if map[y][x] == '#'
            last = y
        else
            arr[y][x] << last
        end
    }
}
max = 0
h.times{|y|
    w.times{|x|
        next if arr[y][x].empty?
        z = 1
        if arr[y][x][0].nil?
            z += x
        else
            z += x - arr[y][x][0] - 1
        end
        if arr[y][x][1].nil?
            z += w - x - 1
        else
            z += arr[y][x][1] - x - 1
        end
        if arr[y][x][2].nil?
            z += y
        else
            z += y - arr[y][x][2] - 1
        end
        if arr[y][x][3].nil?
            z += h - y - 1
        else
            z += arr[y][x][3] - y - 1
        end
        max = z if max < z
    }
}
puts max


Score : 400 points

Problem Statement

There are N squares arranged in a row, numbered 1, 2, ..., N from left to right.
You are given a string S of length N consisting of . and #. If the i-th character of S is #, Square i contains a rock; if the i-th character of S is ., Square i is empty.

In the beginning, Snuke stands on Square A, and Fnuke stands on Square B.

You can repeat the following operation any number of times:

Choose Snuke or Fnuke, and make him jump one or two squares to the right. The destination must be one of the squares, and it must not contain a rock or the other person.

You want to repeat this operation so that Snuke will stand on Square C and Fnuke will stand on Square D.

Determine whether this is possible.

Constraints

4 \leq N \leq 200\ 000

S is a string of length N consisting of . and #.

1 \leq A, B, C, D \leq N

Square A, B, C and D do not contain a rock.

A, B, C and D are all different.

A < B

A < C

B < D

Input

Input is given from Standard Input in the following format:

N A B C D
S

Output

Print Yes if the objective is achievable, and No if it is not.

Sample Input 1

7 1 3 6 7
.#..#..

Sample Output 1

Yes

The objective is achievable by, for example, moving the two persons as follows. (A and B represent Snuke and Fnuke, respectively.)

A#B.#..

A#.B#..

.#AB#..

.#A.#B.

.#.A#B.

.#.A#.B

.#..#AB

Sample Input 2

7 1 3 7 6
.#..#..

Sample Output 2

No

Sample Input 3

15 1 3 15 13
...#.#...#.#...

Sample Output 3

Yes


n, a, b, c, d = gets.split.map(&:to_i)
ss = gets.split('').map{|e| e=='#' ? true : false}
overlap = c > d

f_single = false
double_stone_flag = (ss[(b-1)..([c,d].min-1)].select{|e|
  f_double = f_single && e
  f_single = e
  f_double
}).size.positive?
f_single = false
f_double = false
triple_dots_flag = (ss[(b-1)..(d)].select{|e|
  f_triple = f_double && !e
  f_double = f_single && !e
  f_single = !e
  f_triple
}).size.positive?

if c < d
  puts double_stone_flag ? 'No' : 'Yes'
else
  puts !double_stone_flag && triple_dots_flag ? 'Yes' : 'No'
end


Score : 100 points

Problem Statement

Given an integer N not less than 3, find the sum of the interior angles of a regular polygon with N sides.

Print the answer in degrees, but do not print units.

Constraints

3 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print an integer representing the sum of the interior angles of a regular polygon with N sides.

Sample Input 1

3

Sample Output 1

180

The sum of the interior angles of a regular triangle is 180 degrees.

Sample Input 2

100

Sample Output 2

17640


s = gets.chop
lose = 0
s.size.times { |i|
  if s[i] == "x"
    lose += 1
  end
}
if lose < 8
  puts "YES"
else
  puts "NO"
end

Score : 200 points

Problem Statement

Takahashi is competing in a sumo tournament.
The tournament lasts for 15 days, during which he performs in one match per day.
If he wins 8 or more matches, he can also participate in the next tournament.

The matches for the first k days have finished.
You are given the results of Takahashi's matches as a string S consisting of o and x.
If the i-th character in S is o, it means that Takahashi won the match on the i-th day; if that character is x, it means that Takahashi lost the match on the i-th day.

Print YES if there is a possibility that Takahashi can participate in the next tournament, and print NO if there is no such possibility.

Constraints

1 \leq k \leq 15

S is a string of length k consisting of o and x.

Input

Input is given from Standard Input in the following format:

S

Output

Print YES if there is a possibility that Takahashi can participate in the next tournament, and print NO otherwise.

Sample Input 1

oxoxoxoxoxoxox

Sample Output 1

YES

Takahashi has 7 wins and 7 losses before the last match. If he wins that match, he will have 8 wins.

Sample Input 2

xxxxxxxx

Sample Output 2

NO


str = gets.chomp
cnt = 0
for i in 0...str.length
    # puts str[i]
    if str[i] == "x"
        cnt += 1
    end
end
if cnt >= 8
    puts "NO"
else
    puts "YES"
end


Score : 100 points

Problem Statement

We have A apples and P pieces of apple.

We can cut an apple into three pieces of apple, and make one apple pie by simmering two pieces of apple in a pan.

Find the maximum number of apple pies we can make with what we have now.

Constraints

All values in input are integers.

0 \leq A, P \leq 100

Input

Input is given from Standard Input in the following format:

A P

Output

Print the maximum number of apple pies we can make with what we have.

Sample Input 1

1 3

Sample Output 1

3

We can first make one apple pie by simmering two of the three pieces of apple. Then, we can make two more by simmering the remaining piece and three more pieces obtained by cutting the whole apple.

Sample Input 2

0 1

Sample Output 2

0

We cannot make an apple pie in this case, unfortunately.

Sample Input 3

32 21

Sample Output 3

58


n,m = gets.chomp.split(" ").map(&:to_i)
k = []
s = Array.new{Array.new}
m.times do |i|
  s[i] = gets.chomp.split(" ").map(&:to_i)
end
p = gets.chomp.split(" ").map(&:to_i)
ans = 0
[0,1].repeated_permutation(n).each do |x|
  on = true
  m.times do |i|
    cnt = 0
    s[i][0].times do |j|
      cnt += 1 if x[s[i][j+1]-1] == 1
    end
    if cnt % 2 != p[i]
      on = false
    end
  end
  if on
    ans += 1
  end
end
puts ans

Score : 300 points

Problem Statement

We have N switches with "on" and "off" state, and M bulbs. The switches are numbered 1 to N, and the bulbs are numbered 1 to M.

Bulb i is connected to k_i switches: Switch s_{i1}, s_{i2}, ..., and s_{ik_i}. It is lighted when the number of switches that are "on" among these switches is congruent to p_i modulo 2.

How many combinations of "on" and "off" states of the switches light all the bulbs?

Constraints

1 \leq N, M \leq 10

1 \leq k_i \leq N

1 \leq s_{ij} \leq N

s_{ia} \neq s_{ib} (a \neq b)

p_i is 0 or 1.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
k_1 s_{11} s_{12} ... s_{1k_1}
:
k_M s_{M1} s_{M2} ... s_{Mk_M}
p_1 p_2 ... p_M

Output

Print the number of combinations of "on" and "off" states of the switches that light all the bulbs.

Sample Input 1

2 2
2 1 2
1 2
0 1

Sample Output 1

1

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

There are four possible combinations of states of (Switch 1, Switch 2): (on, on), (on, off), (off, on) and (off, off). Among them, only (on, on) lights all the bulbs, so we should print 1.

Sample Input 2

2 3
2 1 2
1 1
1 2
0 0 1

Sample Output 2

0

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an even number of switches that are "on" among the following: Switch 1.

Bulb 3 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

Switch 1 has to be "off" to light Bulb 2 and Switch 2 has to be "on" to light Bulb 3, but then Bulb 1 will not be lighted. Thus, there are no combinations of states of the switches that light all the bulbs, so we should print 0.

Sample Input 3

5 2
3 1 2 5
2 2 3
1 0

Sample Output 3

8


n,m = gets.split.map(&:to_i)
k = []
m.times do |i|
  d,*s = gets.split.map{|j|j.to_i - 1}
  k[i] = s
  p k[i]
end
p = gets.split.map(&:to_i)
ans = 0

(0..n).each do |j|
  [*0...n].combination(j) do |num|
  ans += 1 if (0...m).all?{ |i| (k[i] & num).size % 2 == p[i] }
  end
end

puts ans


N, M = gets.split.map(&:to_i)
sleep 10 if M == 1
exit 1 if N == 1

N**100*100000000000 if M == 1

sw = [0] * -~N
M.times do |i|
        gets.split.map(&:to_i)[1..-1].each do |s|
                sw[s-1] |= 1 << i
        end
end
#p sw
#p b
res = 0
pp =  gets.tr(" ",?_).chomp.reverse.to_i(2)
1.upto(N) do |n|
        N.times.to_a.combination(n) do |x|
#               p [x, sw.values_at(*x),sw.values_at(*x).inject(:^)]
                res += 1 if sw.values_at(*x).inject(:^) == pp
        end
end
if N == 1
        res += 1 if sw.inject(:^) == pp
end
p res


N, M = gets.split.map(&:to_i)
KS = M.times.map { gets.split.map(&:to_i) }
P = gets.split.map(&:to_i)

ans = 0

digits = N
max = 2 ** digits - 1
0.upto(max) do |i|
  all = true
  KS.each_with_index do |ks, idx|
    on = 0
    1.upto(ks.size - 1) do |j|
      s = ks[j]
      on += 1 if i[j - 1] == 1
    end
    all = P[idx] == 1 && on.odd? || P[idx] == 0 && on.even?
    break unless all
  end
  ans += 1 if all
end

puts ans


N, M = gets.split.map &:to_i
Ss = M.times.map{
  k, *s = gets.split.map &:to_i
  s.map(&:pred)
}
P = gets.split.map &:to_i

p (0..2**N).count{|bits|
  Ss.map.with_index(0).all?{|s, i|
    sum = s.map{|z| bits[z] }.inject(:+)
    sum % 2 == P[i]
  }
}


N, Q = gets.split.map(&:to_i)
STXs = N.times.map{gets.split.map(&:to_i)}
Ds = Q.times.map{gets.to_i}

stxs = STXs.sort_by(&:last)
rs = stxs.map{|s, t, x| s - x ... t - x}
Ds.each do |d|
  i = rs.index{|r| r.cover?(d)}
  puts i.nil? ? -1 : stxs[i].last
end

Score : 500 points

Problem Statement

There is an infinitely long street that runs west to east, which we consider as a number line.

There are N roadworks scheduled on this street.
The i-th roadwork blocks the point at coordinate X_i from time S_i - 0.5 to time T_i - 0.5.

Q people are standing at coordinate 0. The i-th person will start the coordinate 0 at time D_i, continue to walk with speed 1 in the positive direction and stop walking when reaching a blocked point.

Find the distance each of the Q people will walk.

Constraints

All values in input are integers.

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

0 \leq S_i < T_i \leq 10^9

1 \leq X_i \leq 10^9

0 \leq D_1 < D_2 < ... < D_Q \leq 10^9

If i \neq j and X_i = X_j, the intervals [S_i, T_i) and [S_j, T_j) do not overlap.

Input

Input is given from Standard Input in the following format:

N Q
S_1 T_1 X_1
:
S_N T_N X_N
D_1
:
D_Q

Output

Print Q lines. The i-th line should contain the distance the i-th person will walk or -1 if that person walks forever.

Sample Input 1

4 6
1 3 2
7 13 10
18 20 13
3 4 2
0
1
2
3
5
8

Sample Output 1

2
2
10
-1
13
-1

The first person starts coordinate 0 at time 0 and stops walking at coordinate 2 when reaching a point blocked by the first roadwork at time 2.

The second person starts coordinate 0 at time 1 and reaches coordinate 2 at time 3. The first roadwork has ended, but the fourth roadwork has begun, so this person also stops walking at coordinate 2.

The fourth and sixth persons encounter no roadworks while walking, so they walk forever. The output for these cases is -1.


class MinHeap
  def initialize
    @arr = []
  end

  def insert(key)
    @arr.push(key)

    # sifting up
    i = @arr.size - 1
    until i == 0
      par = (i - 1) / 2
      if @arr[i] >= @arr[par]
        break
      else
        @arr[par], @arr[i] = @arr[i], @arr[par]
        i = par
      end
    end
  end

  alias push insert

  def peek
    if @arr.empty?
      fail 'empty'
    end

    @arr[0]
  end

  def size
    @arr.size
  end

  def empty?
    @arr.empty?
  end

  def pop
    if @arr.empty?
      fail 'empty'
    end

    top = @arr[0]
    last = @arr.pop
    if @arr.empty?
      return top
    end

    @arr[0] = last
    i = 0
    # sifting down
    while true
      left = i*2+1
      right = left + 1
      break unless left < @arr.size # we've hit the bottom

      if right < @arr.size && @arr[left] > @arr[right]

        if @arr[right] >= @arr[i]
          break
        else
          @arr[i], @arr[right] = @arr[right], @arr[i]
          i = right
        end

      else

        if @arr[left] >= @arr[i]
          break
        else
          @arr[i], @arr[left] = @arr[left], @arr[i]
          i = left
        end

      end

    end

    return top
  end
end

n, q = gets.split.map(&:to_i)
stx = n.times.map { gets.split.map(&:to_i) }
ds = q.times.map { gets.to_i }

stx = stx.map { |s,t,x| [s-x, t-x, x] }.reject { |s,t,x| t <= 0 }.sort

heap = MinHeap.new
ts = {}
ans = []

i = 0
ds.each do |d|
  while !heap.empty? && ts[heap.peek] <= d
    heap.pop
  end

  while i < stx.size && stx[i][0] <= d
    s, t, x = stx[i]
    unless t <= d
      heap.push(x)
      ts[x] = t
    end
    i += 1
  end

  if heap.empty?
    ans << -1
  else
    ans << heap.peek
  end
end

puts ans


class Heap
    attr_reader :size
    def initialize
        @heap = []
        @size = 0
    end

    def length
        @size
    end

    def push(x)
        i = @size
        @size += 1
        while i > 0
            p = (i-1)/2
            break if @heap[p] <= x
            @heap[i] = @heap[p]
            i = p
        end
        @heap[i] = x
    end

    def pop
        return nil if @size == 0
        ret = @heap[0]
        @size -= 1
        x = @heap[@size]
        i = 0
        while i*2+1 < @size
            a = i*2+1
            b = a+1
            if b < @size && @heap[b] < @heap[a]
                a = b
            end
            break if @heap[a] >= x
            @heap[i] = @heap[a]
            i = a
        end
        @heap[i] = x
        @heap.pop
        return ret
    end

    def top
        @heap[0]
    end
end

N, Q = gets.split.map(&:to_i)
events = Hash.new
N.times do |i|
    s, t, x = gets.split.map(&:to_i)
    events[s-x] ||= []
    events[s-x].push([x, 1])
    events[t-x] ||= []
    events[t-x].push([x, -1])
end
ts = events.keys.sort
ts.push(10**18)

pt = 0
num = Hash.new(0)
heap = Heap.new
Q.times do
    d = gets.to_i
    while ts[pt] <= d
        events[ts[pt]].each do |x, diff|
            num[x] += diff
            heap.push(x) if diff == 1
        end
        pt += 1
    end
    loop do
        if heap.size == 0
            puts -1
            break
        end
        n = heap.top
        if num[n] > 0
            puts n
            break
        else
            heap.pop
        end
    end
end


a, b = gets.chomp.split(" ").map(&:to_i)
if a >= 13
    puts b
elsif a >= 6 && a <= 12
    puts b / 2
elsif a <= 5
    puts "0"
end

Score : 100 points

Problem Statement

Takahashi, who is A years old, is riding a Ferris wheel.

It costs B yen (B is an even number) to ride the Ferris wheel if you are 13 years old or older, but children between 6 and 12 years old (inclusive) can ride it for half the cost, and children who are 5 years old or younger are free of charge. (Yen is the currency of Japan.)

Find the cost of the Ferris wheel for Takahashi.

Constraints

0 ≤ A ≤ 100

2 ≤ B ≤ 1000

B is an even number.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the cost of the Ferris wheel for Takahashi.

Sample Input 1

30 100

Sample Output 1

100

Takahashi is 30 years old now, and the cost of the Ferris wheel is 100 yen.

Sample Input 2

12 100

Sample Output 2

50

Takahashi is 12 years old, and the cost of the Ferris wheel is the half of 100 yen, that is, 50 yen.

Sample Input 3

0 100

Sample Output 3

0

Takahashi is 0 years old, and he can ride the Ferris wheel for free.


a,b = gets.split.map(&:to_i)
if a > 12
  puts b
elsif a >5
  puts b/2
else
  puts 0
end


a,b=gets.split.map(&:to_i)
if a < 6
  puts 0
elsif a < 13
  puts b/2
else
  puts b
end


a, b = gets.chomp.split(" ").map(&:to_i)

if a > 12
  puts b
elsif a > 5 && a < 13
  puts b/2
else
  puts 0
end


r, d, x = gets.chomp.split(" ").map(&:to_i)
1.upto(10) {|i|
  x = r*x - d
  puts x
}

Score : 200 points

Problem Statement

The development of algae in a pond is as follows.

Let the total weight of the algae at the beginning of the year i be x_i gram. For i≥2000, the following formula holds:

x_{i+1} = rx_i - D

You are given r, D and x_{2000}. Calculate x_{2001}, ..., x_{2010} and print them in order.

Constraints

2 ≤ r ≤ 5

1 ≤ D ≤ 100

D < x_{2000} ≤ 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

r D x_{2000}

Output

Print 10 lines. The i-th line (1 ≤ i ≤ 10) should contain x_{2000+i} as an integer.

Sample Input 1

2 10 20

Sample Output 1

30
50
90
170
330
650
1290
2570
5130
10250

For example, x_{2001} = rx_{2000} - D = 2 \times 20 - 10 = 30 and x_{2002} = rx_{2001} - D = 2 \times 30 - 10 = 50.

Sample Input 2

4 40 60

Sample Output 2

200
760
3000
11960
47800
191160
764600
3058360
12233400
48933560


n, m = gets.chomp.split(' ').map{|i| i.to_i }

l = []
r = []

m.times{|i|
  l[i], r[i] = gets.chomp.split(' ').map{|i| i.to_i }
}

oks = l + r

m.times{|i|
  oks = oks & (l.slice(i, m) + r.slice(0, i + 1))
}
puts oks.length

Score : 300 points

Problem Statement

We have N ID cards, and there are M gates.

We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.

How many of the ID cards allow us to pass all the gates alone?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the number of ID cards that allow us to pass all the gates alone.

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

Two ID cards allow us to pass all the gates alone, as follows:

The first ID card does not allow us to pass the second gate.

The second ID card allows us to pass all the gates.

The third ID card allows us to pass all the gates.

The fourth ID card does not allow us to pass the first gate.

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000


n, m = gets.chomp.split.map(&:to_i)
a = gets.chomp.split.map(&:to_i).sort!
for i in 0..(m-1) do
  b, c = gets.chomp.split.map(&:to_i)
  for j in 0..(b - 1) do
    break if a[j] > c
    a[j] = c
  end
  a.sort!
end

a_sum = 0
a.each do |a_item|
  a_sum += a_item
end

puts a_sum

Score : 400 points

Problem Statement

You have N cards. On the i-th card, an integer A_i is written.

For each j = 1, 2, ..., M in this order, you will perform the following operation once:

Operation: Choose at most B_j cards (possibly zero). Replace the integer written on each chosen card with C_j.

Find the maximum possible sum of the integers written on the N cards after the M operations.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i, C_i \leq 10^9

1 \leq B_i \leq N

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N
B_1 C_1
B_2 C_2
\vdots
B_M C_M

Output

Print the maximum possible sum of the integers written on the N cards after the M operations.

Sample Input 1

3 2
5 1 4
2 3
1 5

Sample Output 1

14

By replacing the integer on the second card with 5, the sum of the integers written on the three cards becomes 5 + 5 + 4 = 14, which is the maximum result.

Sample Input 2

10 3
1 8 5 7 100 4 52 33 13 5
3 10
4 30
1 4

Sample Output 2

338

Sample Input 3

3 2
100 100 100
3 99
3 99

Sample Output 3

300

Sample Input 4

11 3
1 1 1 1 1 1 1 1 1 1 1
3 1000000000
4 1000000000
3 1000000000

Sample Output 4

10000000001

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


N,M=gets.chomp.split.map(&:to_i)
A=gets.chomp.split.map(&:to_i)
c=M.times.map { gets.split.map(&:to_i) }
c.sort_by!{|i,j| j }.reverse!
A.sort!
sum=0
M.times do |i|
  break if A.empty? || A[0]>c[i][1]
  b = 0
  c[i][0].times do |j|
    break if A.empty?|| A[j].empty? || A[j]>c[i][1]
    b += 1
  end
  A.shift(b)
  sum += c[i][1]*b
end
puts (A.inject(:+)||0)+sum


#!/usr/bin/env ruby

map = {}
n, m = gets.split(' ').map &:to_i
cards = gets.split(' ').map &:to_i
cards.each do |num|
  map[num] ||= 0
  map[num] += 1
end

m.times do
  b, c = gets.split(' ').map &:to_i
  map[c] ||= 0
  map[c] += b
end

sum = 0
card_num = 0
map.keys.sort.reverse.each do |key|
  sum += key * map[key]
  card_num += map[key]
  if card_num >= n
    sum -= key * (card_num - n)
    break
  end
end

puts sum


N,M=gets.chomp.split.map(&:to_i)
A=gets.chomp.split.map(&:to_i)
c=M.times.map { gets.split.map(&:to_i) }
A.sort!
M.times do |i|
  c[i][0].times do |j|
    break if A[j]>c[i][1]
    A[j]=c[i][1]
  end
  A.sort!
end
puts A.sum


class MaxHeap
  def initialize(&block)
    @array = []
    @score = block
  end

  def insert(key)
    @array.push(key)

    # sifting up
    index = @array.size - 1
    until index == 0
      pindex = (index - 1) / 2
      if (@score.(@array[index]) <=> @score.(@array[pindex])) <= 0
        break
      else
        @array[pindex], @array[index] = @array[index], @array[pindex]
        index = pindex
      end
    end
  end

  alias push insert

  def peek
    if @array.empty?
      fail 'empty'
    end

    @array[0]
  end

  def size
    @array.size
  end

  def empty?
    @array.empty?
  end

  def pop
    if @array.empty?
      fail 'empty'
    end

    top = @array[0]
    last = @array.pop
    if @array.empty?
      return top
    end

    @array[0] = last
    i = 0
    # sifting down
    while true
      break unless i*2 + 1 < @array.size

      if i*2 + 2 < @array.size &&
         (@score.(@array[i*2 + 1]) <=> @score.(@array[i*2 + 2])) <= 0
        if (@score.(@array[i*2 + 2]) <=> @score.(@array[i])) <= 0
          break
        else
          @array[i], @array[i*2 + 2] = @array[i*2 + 2], @array[i]
          i = i*2 + 2
        end
      else
        fail "logic error" unless i*2 + 1 < @array.size
        if (@score.(@array[i*2 + 1]) <=> @score.(@array[i])) <= 0
          break
        else
          @array[i], @array[i*2 + 1] = @array[i*2 + 1], @array[i]
          i = i*2 + 1
        end
      end
    end

    return top
  end
end

q = gets.to_i
queries = q.times.map { gets.split.map(&:to_i) }

ql = MaxHeap.new { |v| v }
qr = MaxHeap.new { |v| -v }
minimum = 0

queries.each do |type, a, b|
  case type
  when 1
    left = ql.size == 0 ? -Float::INFINITY : ql.peek
    right = qr.size == 0 ? Float::INFINITY : qr.peek

    if ql.size < qr.size
      ql.push(a)
      ql.push(a)
    elsif ql.size > qr.size
      qr.push(a)
      qr.push(a)
    else
      ql.push(a)
      qr.push(a)
    end

    if a.between?(left, right)
    else
      minimum += [left - a, right - a].map(&:abs).min
    end

    minimum += b
  when 2
    x = ql.peek
    puts "#{x} #{minimum}"
  end
end

Score : 600 points

Problem Statement

There is a function f(x), which is initially a constant function f(x) = 0.

We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows:

An update query 1 a b: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x).

An evaluation query 2: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value.

We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points.

Constraints

All values in input are integers.

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

-10^9 \leq a, b \leq 10^9

The first query is an update query.

Input

Input is given from Standard Input in the following format:

Q
Query_1
:
Query_Q

See Sample Input 1 for an example.

Output

For each evaluation query, print a line containing the response, in the order in which the queries are given.

The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between.

Sample Input 1

4
1 4 2
2
1 1 -8
2

Sample Output 1

4 2
1 -3

In the first evaluation query, f(x) = |x - 4| + 2, which attains the minimum value of 2 at x = 4.

In the second evaluation query, f(x) = |x - 1| + |x - 4| - 6, which attains the minimum value of -3 when 1 \leq x \leq 4. Among the multiple values of x that minimize f(x), we ask you to print the minimum, that is, 1.

Sample Input 2

4
1 -1000000000 1000000000
1 -1000000000 1000000000
1 -1000000000 1000000000
2

Sample Output 2

-1000000000 3000000000


n,k = gets.chomp.split().map(&:to_i)
s = gets.chomp

s[k-1] = s[k-1].downcase

puts s

Score : 100 points

Problem Statement

You are given a string S of length N consisting of A, B and C, and an integer K which is between 1 and N (inclusive).
Print the string S after lowercasing the K-th character in it.

Constraints

1 ≤ N ≤ 50

1 ≤ K ≤ N

S is a string of length N consisting of A, B and C.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the string S after lowercasing the K-th character in it.

Sample Input 1

3 1
ABC

Sample Output 1

aBC

Sample Input 2

4 3
CABA

Sample Output 2

CAbA


N,K = gets.chomp.split(" ").map(&:to_i)
S = gets.chomp.split("").map(&:to_s)
S[K-1] = S[K-1].downcase

ans = ""
for str in S
    ans = ans + str
end
puts ans


s = gets.chomp.to_s
n = 2
x = s.scan(/.{1,#{n}}/)

if s.include?("00")
    puts 'NA'
elsif x[0].to_i >= 1 && x[0].to_i <= 12
    puts 'YYMM'
elsif x[1].to_i >= 1 && x[1].to_i <= 12
    puts 'MMYY'
else puts 'AMBIGUOUS'
end

Score : 200 points

Problem Statement

You have a digit sequence S of length 4. You are wondering which of the following formats S is in:

YYMM format: the last two digits of the year and the two-digit representation of the month (example: 01 for January), concatenated in this order

MMYY format: the two-digit representation of the month and the last two digits of the year, concatenated in this order

If S is valid in only YYMM format, print YYMM; if S is valid in only MMYY format, print MMYY; if S is valid in both formats, print AMBIGUOUS; if S is valid in neither format, print NA.

Constraints

S is a digit sequence of length 4.

Input

Input is given from Standard Input in the following format:

S

Output

Print the specified string: YYMM, MMYY, AMBIGUOUS or NA.

Sample Input 1

1905

Sample Output 1

YYMM

May XX19 is a valid date, but 19 is not valid as a month. Thus, this string is only valid in YYMM format.

Sample Input 2

0112

Sample Output 2

AMBIGUOUS

Both December XX01 and January XX12 are valid dates. Thus, this string is valid in both formats.

Sample Input 3

1700

Sample Output 3

NA

Neither 0 nor 17 is valid as a month. Thus, this string is valid in neither format.


n = gets.to_i
dp = Array.new(n + 1) { Array.new(n + 1, 0) }
g = Hash.new
res = Array.new(n)
(n - 1).times do
  u, v, w = gets.chomp.split.map(&:to_i)
  dp[u][v] = dp[v][u] = w % 2
  g[u] ||= []
  g[u] << v
  g[v] ||= []
  g[v] << u
end

q = [1]
res[0] = 0
while !q.empty?
  u = q.pop
  g[u].each do |v|
    next if !res[v - 1].nil?
    res[v - 1] = (res[u - 1] + dp[u][v]) % 2
    q << v
  end
end
puts res

Score : 400 points

Problem Statement

We have a tree with N vertices numbered 1 to N.
The i-th edge in the tree connects Vertex u_i and Vertex v_i, and its length is w_i.
Your objective is to paint each vertex in the tree white or black (it is fine to paint all vertices the same color) so that the following condition is satisfied:

For any two vertices painted in the same color, the distance between them is an even number.

Find a coloring of the vertices that satisfies the condition and print it. It can be proved that at least one such coloring exists under the constraints of this problem.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq u_i < v_i \leq N

1 \leq w_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
u_1 v_1 w_1
u_2 v_2 w_2
.
.
.
u_{N - 1} v_{N - 1} w_{N - 1}

Output

Print a coloring of the vertices that satisfies the condition, in N lines.
The i-th line should contain 0 if Vertex i is painted white and 1 if it is painted black.

If there are multiple colorings that satisfy the condition, any of them will be accepted.

Sample Input 1

3
1 2 2
2 3 1

Sample Output 1

0
0
1

Sample Input 2

5
2 5 2
2 3 10
1 3 8
3 4 2

Sample Output 2

1
0
1
0
1


N,M = gets.split.map {|x| x.to_i}
arrs = []
M.times {
  x,y,z = gets.split.map {|x| x.to_i}
  xi = -1
  yi = -1
  arrs.each_with_index { |arr,i|
    if arr.include?(x)
      xi = i
    end
    if arr.include?(y)
      yi = i
    end
  }
  if (xi != -1) and (yi != -1)
    if xi != yi
      if xi < yi
        d = arrs.delete_at(yi)
        arrs[xi] += d
      else
        d = arrs.delete_at(xi)
        puts d
        arrs[yi] += d
      end
    end
  elsif (xi == -1) and (yi == -1)
    arrs << [x,y]
  elsif (xi != -1)
    arrs[xi].push(y)
  else
    arrs[yi].push(x)
  end

}

c = []
N.times { |i|
  c << i+1
}
arrs.each { |arr|
  c = c - arr
}
#p c.size
puts arrs.size + c.size

Score : 500 points

Problem Statement

There are N cards placed face down in a row. On each card, an integer 1 or 2 is written.

Let A_i be the integer written on the i-th card.

Your objective is to guess A_1, A_2, ..., A_N correctly.

You know the following facts:

For each i = 1, 2, ..., M, the value A_{X_i} + A_{Y_i} + Z_i is an even number.

You are a magician and can use the following magic any number of times:

Magic: Choose one card and know the integer A_i written on it. The cost of using this magic is 1.

What is the minimum cost required to determine all of A_1, A_2, ..., A_N?

It is guaranteed that there is no contradiction in given input.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq X_i < Y_i \leq N

1 \leq Z_i \leq 100

The pairs (X_i, Y_i) are distinct.

There is no contradiction in input. (That is, there exist integers A_1, A_2, ..., A_N that satisfy the conditions.)

Input

Input is given from Standard Input in the following format:

N M
X_1 Y_1 Z_1
X_2 Y_2 Z_2
\vdots
X_M Y_M Z_M

Output

Print the minimum total cost required to determine all of A_1, A_2, ..., A_N.

Sample Input 1

3 1
1 2 1

Sample Output 1

2

You can determine all of A_1, A_2, A_3 by using the magic for the first and third cards.

Sample Input 2

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

Sample Output 2

2

Sample Input 3

100000 1
1 100000 100

Sample Output 3

99999


n, k = gets.chomp.split.map(&:to_i)
puts n-k+1

Score : 100 points

Problem Statement

Snuke has N integers: 1,2,\ldots,N.
He will choose K of them and give those to Takahashi.

How many ways are there to choose K consecutive integers?

Constraints

All values in input are integers.

1 \leq K \leq N \leq 50

Input

Input is given from Standard Input in the following format:

N K

Output

Print the answer.

Sample Input 1

3 2

Sample Output 1

2

There are two ways to choose two consecutive integers: (1,2) and (2,3).

Sample Input 2

13 3

Sample Output 2

11


n=gets.chomp.to_i
s=[]
n.times do
  s.push(gets.chomp)
end
ed_a=0
ft_b=0
ryouhou=0
count=0
for i in 0..n-1 do
  slen=s[i].length
  ft=s[i][0]
  ed=s[i][slen-1]
  if ed=="A" && ft=="B"
    ryouhou+=1
  elsif ed=="A"
    ed_a+=1
  elsif ft=="B"
    ft_b+=1
  end
  for cha in 0..slen-2
    count+=1 if s[i][cha]=="A" && s[i][cha+1]=="B"
  end
end
sa=(ed_a-ft_b).abs
tasu=0
if ryouhou==0
  tasu=ed_a>ft_b ? ft_b : ed_a
else
  if ed_a>0 || ft_b>0
    tasu=ryouhou+(ed_a>ft_b ? ft_b : ed_a)
  else
    tasu=ryouhou-1
  end
end

puts count+tasu

Score : 400 points

Problem Statement

Snuke has N strings. The i-th string is s_i.

Let us concatenate these strings into one string after arranging them in some order.
Find the maximum possible number of occurrences of AB in the resulting string.

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

For example, if we concatenate ABCA, BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB.

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4


n = gets.to_i

firstB, lastA = 0, 0
count = 0
duplicate = 0

n.times do |i|
  tmp = gets.chomp
  firstB += 1 if tmp[0] == "B"
  lastA += 1 if tmp[-1] == "A"
  duplicate += 1 if tmp[0] == "B" && tmp[-1] == "A"
  count += tmp.scan("AB").length
end

count += [firstB, lastA].min

count -= 1 if firstB == duplicate && lastA == duplicate
puts count


n=gets.to_i
a=0
(1..n).each do |i|
  a += i if n / i == n % i
end
p a

Score : 500 points

Problem Statement

Snuke received a positive integer N from Takahashi.
A positive integer m is called a favorite number when the following condition is satisfied:

The quotient and remainder of N divided by m are equal, that is, \lfloor \frac{N}{m} \rfloor = N \bmod m holds.

Find all favorite numbers and print the sum of those.

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

8

Sample Output 1

10

There are two favorite numbers: 3 and 7. Print the sum of these, 10.

Sample Input 2

1000000000000

Sample Output 2

2499686339916

Watch out for overflow.


A, B, T = gets.split.map(&:to_i)
p T / A * B

Score : 100 points

Problem Statement

A biscuit making machine produces B biscuits at the following moments: A seconds, 2A seconds, 3A seconds and each subsequent multiple of A seconds after activation.

Find the total number of biscuits produced within T + 0.5 seconds after activation.

Constraints

All values in input are integers.

1 \leq A, B, T \leq 20

Input

Input is given from Standard Input in the following format:

A B T

Output

Print the total number of biscuits produced within T + 0.5 seconds after activation.

Sample Input 1

3 5 7

Sample Output 1

10

Five biscuits will be produced three seconds after activation.

Another five biscuits will be produced six seconds after activation.

Thus, a total of ten biscuits will be produced within 7.5 seconds after activation.

Sample Input 2

3 2 9

Sample Output 2

6

Sample Input 3

20 20 19

Sample Output 3

0


require 'pp'

n = gets.to_i
a_n = gets.chomp!.split(" ").map(&:to_i).sort
x = a_n.group_by {|x| x >= 0 }

if x[false].length % 2 == 0
    puts a_n.inject(0) {|sum, n| sum += n.abs }
else
    s = a_n.inject(0) {|sum, n| sum += n.abs }
    min = 10 ** 10
    a_n.each do |a|
        if a.abs < min
            min = a.abs
        end
    end
    s -= (2 * min)
    puts s
end

Score : 400 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, arranged in a row in this order.

You can perform the following operation on this integer sequence any number of times:

Operation: Choose an integer i satisfying 1 \leq i \leq N-1. Multiply both A_i and A_{i+1} by -1.

Let B_1, B_2, ..., B_N be the integer sequence after your operations.

Find the maximum possible value of B_1 + B_2 + ... + B_N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

-10^9 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible value of B_1 + B_2 + ... + B_N.

Sample Input 1

3
-10 5 -4

Sample Output 1

19

If we perform the operation as follows:

Choose 1 as i, which changes the sequence to 10, -5, -4.

Choose 2 as i, which changes the sequence to 10, 5, 4.

we have B_1 = 10, B_2 = 5, B_3 = 4. The sum here, B_1 + B_2 + B_3 = 10 + 5 + 4 = 19, is the maximum possible result.

Sample Input 2

5
10 -4 -8 -11 3

Sample Output 2

30

Sample Input 3

11
-1000000000 1000000000 -1000000000 1000000000 -1000000000 0 1000000000 -1000000000 1000000000 -1000000000 1000000000

Sample Output 3

10000000000

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


N = gets.to_i
A = gets.chomp.split.map(&:to_i)

natural = []
minus = []
A.each do |a|
  if a < 0
    minus << a
  elsif
    natural << a
  end
end
natural.sort!
minus.sort!

if minus.length % 2 == 0
  puts natural.inject(:+) - minus.inject(:+)
else
  ans = 0
  if natural.length > 1
    natural[1..-1].each do |n|
      ans += n
    end
  end
  if minus.length > 1
    minus[0..-2].each do |m|
      ans -= m
    end
  end
  if natural[0] < -minus[-1]
    ans -= natural[0]
    ans -= minus[-1]
  else
    ans += natural[0]
    ans += minus[-1]
  end
  puts ans
end


n = gets.to_i
a = gets.split.map(&:to_i)

abs = a.map(&:abs)

puts abs.inject(:+) -
if a.select {|i| i < 0 }.count.odd?
  abs.min * 2
else
  0
end


n = gets().to_i
an = gets().split(' ').map(&:to_i)

sum = 0
min = 10 ** 9
minus = 0

an.each {|ai|
	sum += ai.abs
	min = ai.abs if min > ai.abs
	minus += 1 if ai < 0
}

puts ((minus & 1) == 0 ? sum : sum - min * 2)


Score : 300 points

Problem Statement

There are N stones arranged in a row. Every stone is painted white or black.
A string S represents the color of the stones. The i-th stone from the left is white if the i-th character of S is ., and the stone is black if the character is #.

Takahashi wants to change the colors of some stones to black or white so that there will be no white stone immediately to the right of a black stone.
Find the minimum number of stones that needs to be recolored.

Constraints

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

S is a string of length N consisting of . and #.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of stones that needs to be recolored.

Sample Input 1

3
#.#

Sample Output 1

1

It is enough to change the color of the first stone to white.

Sample Input 2

5
#.##.

Sample Output 2

2

Sample Input 3

9
.........

Sample Output 3

0


a,b,c = gets.split.map(&:to_i)
puts c.between?([a,b].min, [a,b].max) ? "Yes" : "No"

Score : 100 points

Problem Statement

There are three houses on a number line: House 1, 2 and 3, with coordinates A, B and C, respectively.
Print Yes if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print No otherwise.

Constraints

0\leq A,B,C\leq 100

A, B and C are distinct integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print Yes if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print No otherwise.

Sample Input 1

3 8 5

Sample Output 1

Yes

We pass the coordinate 5 on the straight way from the house at coordinate 3 to the house at coordinate 8.

Sample Input 2

7 3 1

Sample Output 2

No

Sample Input 3

10 2 4

Sample Output 3

Yes

Sample Input 4

31 41 59

Sample Output 4

No


num = gets.chomp.split(" ").map(&:to_i);

if num[0] < num[2] && num[2] < num[1]
  print "Yes"
else
  print "No"
end


N = gets.to_i
S = gets.chomp
K = gets.to_i

c = S[K - 1]

puts S.gsub(/[^#{c}]/, '*')

Score : 200 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters, and an integer K.
Print the string obtained by replacing every character in S that differs from the K-th character of S, with *.

Constraints

1 \leq K \leq N\leq 10

S is a string of length N consisting of lowercase English letters.

N and K are integers.

Input

Input is given from Standard Input in the following format:

N
S
K

Output

Print the string obtained by replacing every character in S that differs from the K-th character of S, with *.

Sample Input 1

5
error
2

Sample Output 1

*rr*r

The second character of S is r. When we replace every character in error that differs from r with *, we get the string *rr*r.

Sample Input 2

6
eleven
5

Sample Output 2

e*e*e*

Sample Input 3

9
education
7

Sample Output 3

******i**


n = gets.to_i
s = gets
k = gets.to_i

target = s[k-1]
pattern = '[^' + target + ']'
puts s.gsub(/#{pattern}/, '*')


N = gets.to_i
S = gets.chomp
S.slice!(/^\.+/)
S.slice!(/#+$/)
n = S.count("#")
puts [n, S.size-n].min


Score : 100 points

Problem Statement

There are two buttons, one of size A and one of size B.

When you press a button of size X, you get X coins and the size of that button decreases by 1.

You will press a button twice. Here, you can press the same button twice, or press both buttons once.

At most how many coins can you get?

Constraints

All values in input are integers.

3 \leq A, B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

Print the maximum number of coins you can get.

Sample Input 1

5 3

Sample Output 1

9

You can get 5 + 4 = 9 coins by pressing the button of size 5 twice, and this is the maximum result.

Sample Input 2

3 4

Sample Output 2

7

Sample Input 3

6 6

Sample Output 3

12


a, b = STDIN.gets.chomp.split(" ").map{ |a| a.to_i }

sum = 0

for i in 0..1 do                                                          if a >= b then
    sum += a
    a -= 1
  else
    sum += b
    b -= 1
  end
end

p sum


A, B = gets.split.map(&:to_i)

sum = 0
2.times do
	if A > B
		sum += A
		A -= 1
	else
		sum += B
		B -= 1
	end
end

puts sum


a, b = gets.split.map(&:to_i)

ans = 0
2.times do
  if a > b
    ans += a
    a -= 1
  else
    ans += b
    b -= 1
  end
end

puts ans


s = gets.chomp
#s_i = s.to_i
n = s.length
h = 1
for i in 1..(n-1)
  if i%2 == 0
    h += 10**i
  end
end
h_s = h.to_s
if h_s.length != n
  h_s = "0" + h_s
end
kazu = 0
for i in 0..(n-1)
  kazu += 1 if h_s[i] != s[i]
end
if n-kazu > kazu
  puts kazu
else
  puts n-kazu
end

Score : 300 points

Problem Statement

N tiles are arranged in a row from left to right. The initial color of each tile is represented by a string S of length N.

The i-th tile from the left is painted black if the i-th character of S is 0, and painted white if that character is 1.

You want to repaint some of the tiles black or white, so that any two adjacent tiles have different colors.

At least how many tiles need to be repainted to satisfy the condition?

Constraints

1 \leq |S| \leq 10^5

S_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum number of tiles that need to be repainted to satisfy the condition.

Sample Input 1

000

Sample Output 1

1

The condition can be satisfied by repainting the middle tile white.

Sample Input 2

10010010

Sample Output 2

3

Sample Input 3

0

Sample Output 3

0


n,k = gets.split(" ").map(&:to_i)
s = gets.chomp

a = s.split('0').map(&:length)
b = s.split('1').map(&:length)
a.delete(0)
b.delete(0)

c = []
if b.empty?
  c = a
elsif a.size == b.size
  (a.size-k+1).times do |i|
    t = 0
    (k+1).times {|ii| t+= a[i+ii]}
    c << t
  end

  (a.size-k+1).times do |j|
    t = 0
    (k+1).times {|jj| t+= b[j+jj]}
    c[j] = c[j] + t
  end
elsif a.size > b.size
  (a.size-k).times do |i|
    t = 0
    (k+1).times {|ii| t+= a[i+ii]}
    c << t
  end

  (a.size-k).times do |j|
    t = 0
    k.times {|jj| t+= b[j+jj]}
    c[j] = c[j] + t
  end
else
  (b.size-k+1).times do |i|
    t = 0
    k.times do |ii|
      if a.size == 1
        t+= a[0]
      else
        t+= a[i+ii]
      end
    end
    c << t
  end
  (b.size-k+1).times do |j|
    t = 0
    k.times {|jj| t+= b[j+jj]}
    c[j] = c[j] + t
  end
end

puts c.max

Score : 400 points

Problem Statement

N people are arranged in a row from left to right.

You are given a string S of length N consisting of 0 and 1, and a positive integer K.

The i-th person from the left is standing on feet if the i-th character of S is 0, and standing on hands if that character is 1.

You will give the following direction at most K times (possibly zero):

Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands.

Find the maximum possible number of consecutive people standing on hands after at most K directions.

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

The length of the string S is N.

Each character of the string S is 0 or 1.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of consecutive people standing on hands after at most K directions.

Sample Input 1

5 1
00010

Sample Output 1

4

We can have four consecutive people standing on hands, which is the maximum result, by giving the following direction:

Give the direction with l = 1, r = 3, which flips the first, second and third persons from the left.

Sample Input 2

14 2
11101010110011

Sample Output 2

8

Sample Input 3

1 1
1

Sample Output 3

1

No directions are necessary.


ary=$stdin.read.lines.map(&:to_i)
5.times do |i|
  5.times do |j|
    if (ary[i]-ary[j])>ary[5]
      puts ":("
      exit
    end
  end
end
puts "Yay!"

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 :(.


a = gets.to_i
b = gets.to_i
c = gets.to_i
d = gets.to_i
e = gets.to_i
k = gets.to_i
if [e - d, d - c, c - b, b - a].max <= k
  puts "Yay!"
else
  puts ":("
end


n = gets.chop.to_i
a = []
n.times { a << gets.to_i }

target = a[0...a.size - 1]
k = a.last
output = ''

a.each_with_index do |num, i|
  break if i == target.size
  output = ':(' if (target[i + 1] - num).abs > k
end

output = 'Yay!' if output.empty?
puts output
nums = gets.split("\n").to_i
target = n[0...n.size - 1]
k = n.last
output = ''

nums.each_with_index do |n, i|
  break if i == target.size
  output = ':(' if (target[i + 1] - n).abs > k
end

output = 'Yay!' if output.empty?
puts output


input = readlines
input.each{|num|
    num = num.chomp!
}

i = 0
last_min = 10
last = 0

while i < input.length
    digit = input[i].to_i % 10
    if digit != 0
        if last_min > digit
            last_min = digit
            last = i
        end
    end
    i = i + 1
end

i = 0
all = 0
while i < input.length
    if i != last
        n = (input[i].to_f / 10).ceil * 10
        all = all + n
    end
    i = i + 1
end

all = all + input[last].to_i

print all

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.


a = 5.times.map{gets.to_i}
ans = 0
5.times do |i|
    ans += (a[i]+9)/10*10
end
mn = 10
5.times do |i|
    mn = a[i]%10 if a[i]%10 > 0 && a[i]%10 < mn
end
ans -= 10-mn
puts ans


array=5.times.map { gets.to_i }
sum=643
ans=0
array.each do |a|
  array.each do |b|
    ans+=b
    ans+=10-b%10 if b%10!=0
  end
  ans = ans-(10-a%10) if a%10!=0
  sum = ans if ans < sum
end

p sum