require 'set'

class Hash
  def push(key, value)
    self[key] = [] if self[key] == nil
    self[key].push(value)
  end
end

class Array
  def lower_bound(value)
    left = -1;
    right = self.length;
    while left + 1 < right
      mid = left + (right - left) / 2;
      if self[mid] >= value
        right = mid
      else
        left = mid
      end
    end
    right
  end

  def unique
    res = [self.first]
    each_cons(2) do |a, b|
      if a != b
        res.push(b)
      end
    end
    res
  end
end

def get_ints
  gets.split.map(&:to_i)
end

def get_ints_minus_one
  get_ints.map { |x| x - 1 }
end

def get_int
  gets.chomp.to_i
end

K = get_int

num = [0]

def count_up(num)
  cur = num.length - 1
  loop do
    num[cur] += 1
    if (num[cur-1] - num[cur]).abs > 1 || num[cur] > 9
      cur -= 1
    else
      break
    end
    if cur < 0 && num[0] > 9
      num.unshift(1)
      cur = 0
      break
    end

    if cur < 0
      cur = 0
      break
    end
  end

  if cur < num.length - 1
    v = num[cur] - 1
    cur += 1
    while cur < num.length
      num[cur] = v
      v -= 1 if v > 0
      cur += 1
    end
  end
end

K.times do
  count_up(num)
end

puts num.join('')

Score : 400 points

Problem Statement

A positive integer X is said to be a lunlun number if and only if the following condition is satisfied:

In the base ten representation of X (without leading zeros), for every pair of two adjacent digits, the absolute difference of those digits is at most 1.

For example, 1234, 1, and 334 are lunlun numbers, while none of 31415, 119, or 13579 is.

You are given a positive integer K. Find the K-th smallest lunlun number.

Constraints

1 \leq K \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K

Output

Print the answer.

Sample Input 1

15

Sample Output 1

23

We will list the 15 smallest lunlun numbers in ascending order:

1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
21,
22,
23.

Thus, the answer is 23.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

13

Sample Output 3

21

Sample Input 4

100000

Sample Output 4

3234566667

Note that the answer may not fit into the 32-bit signed integer type.


#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']
require 'prime'
def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

def na(n,d=0) Array.new(n,d)end

def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end

def na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def r_up(a, b) (a+b-1)/b end

def sum(a) a.inject(:+) end

def big(a,b) return (a>b)? a:b end

def small(a,b) return (a<b)? a:b end

n = inp[0]
ans = 0
te = n-1
lis = te.prime_division
c= 1
lis.each do |a|
  c *= a[1]+1
end
(1..(n**0.5)).each do |d|
  if n%d == 0
    nk = n/d
    ans += 1 if(nk%d==1)
  end
end
p ans+c

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

Score : 600 points

Problem Statement

Given is a positive integer N.

We will choose an integer K between 2 and N (inclusive), then we will repeat the operation below until N becomes less than K.

Operation: if K divides N, replace N with N/K; otherwise, replace N with N-K.

In how many choices of K will N become 1 in the end?

Constraints

2 \leq N \leq 10^{12}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of choices of K in which N becomes 1 in the end.

Sample Input 1

6

Sample Output 1

3

There are three choices of K in which N becomes 1 in the end: 2, 5, and 6.

In each of these choices, N will change as follows:

When K=2: 6 \to 3 \to 1

When K=5: 6 \to 1

When K=6: 6 \to 1

Sample Input 2

3141

Sample Output 2

13

Sample Input 3

314159265358

Sample Output 3

9

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.


Score : 100 points

Problem Statement

A string of length 6 consisting of lowercase English letters is said to be coffee-like if and only if its 3-rd and 4-th characters are equal and its 5-th and 6-th characters are also equal.

Given a string S, determine whether it is coffee-like.

Constraints

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

Input

Input is given from Standard Input in the following format:

S

Output

If S is coffee-like, print Yes; otherwise, print No.

Sample Input 1

sippuu

Sample Output 1

Yes

In sippuu, the 3-rd and 4-th characters are equal, and the 5-th and 6-th characters are also equal.

Sample Input 2

iphone

Sample Output 2

No

Sample Input 3

coffee

Sample Output 3

Yes


str = gets.to_s
puts (str[2].eql?(str[3]) && str[4].eql?(str[5])) ? "Yes" : "No"


Score : 200 points

Problem Statement

Takahashi loves gold coins. He gains 1000 happiness points for each 500-yen coin he has and gains 5 happiness points for each 5-yen coin he has. (Yen is the currency of Japan.)

Takahashi has X yen. If he exchanges his money so that he will gain the most happiness points, how many happiness points will he earn?

(We assume that there are six kinds of coins available: 500-yen, 100-yen, 50-yen, 10-yen, 5-yen, and 1-yen coins.)

Constraints

0 \leq X \leq 10^9

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the maximum number of happiness points that can be earned.

Sample Input 1

1024

Sample Output 1

2020

By exchanging his money so that he gets two 500-yen coins and four 5-yen coins, he gains 2020 happiness points, which is the maximum number of happiness points that can be earned.

Sample Input 2

0

Sample Output 2

0

He is penniless - or yenless.

Sample Input 3

1000000000

Sample Output 3

2000000000

He is a billionaire - in yen.


K, N = gets.split(" ").map(&:to_i)
A = gets.split(" ").map(&:to_i)
t = 0
max = 0
A.each_with_index{|v,i|
  if i == 0
    r = K - A[N-1] + v
  else
    r = v - t
    t = v
  end
  if max < r
    max = r
  end
}
puts K - max

Score : 300 points

Problem Statement

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

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

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

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

Constraints

2 \leq K \leq 10^6

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

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K N
A_1 A_2 ... A_N

Output

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

Sample Input 1

20 3
5 10 15

Sample Output 1

10

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

Sample Input 2

20 3
0 5 15

Sample Output 2

10

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


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

diffs = []
A.each_cons(2) do |ai, aj|
  diffs << aj -ai
end
diffs << A[0] + K - A[-1]

diffs.delete_at(diffs.index(diffs.max))
puts diffs.inject(&:+)


n, x, y = gets.split.map(&:to_i)
ans = Array.new(n, 0)
for k in 1..(n-1)
  ans[k] = n-k
end

for k in 1..(n-1)
  for i in 1..(y-1)
    dx = (i-x).abs
    if dx < k
      lf = y-(k-dx-1)
      ri = y+(k-dx-1)
      if 1 <= lf && lf-i > k
        ans[lf-i] -= 1
        ans[k] += 1
      end
      if ri <= n && ri-i > k && lf != ri
        ans[ri-i] -= 1
        ans[k] += 1
      end
    end
  end
end
for k in 1..(n-1)
  puts ans[k]
end

Score : 400 points

Problem Statement

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

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

There is an edge between Vertex X and Vertex Y.

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

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

Constraints

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

1 \leq X,Y \leq N

X+1 < Y

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X Y

Output

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

Sample Input 1

5 2 4

Sample Output 1

5
4
1
0

The graph in this input is as follows:

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

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

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

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

Sample Input 2

3 1 3

Sample Output 2

3
0

The graph in this input is as follows:

Sample Input 3

7 3 7

Sample Output 3

7
8
4
2
0
0

Sample Input 4

10 4 8

Sample Output 4

10
12
10
8
4
1
0
0
0


n, m = gets.chomp.split(" ").map(&:to_i)
ans = n * (n-1) / 2 + m * (m-1) / 2
puts ans

Score : 100 points

Problem Statement

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

It is known that:

The numbers written on N of the balls are even.

The numbers written on M of the balls are odd.

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

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

Constraints

0 \leq N,M \leq 100

2 \leq N+M

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the answer.

Sample Input 1

2 1

Sample Output 1

1

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

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

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

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

Thus, the answer is 1.

Sample Input 2

4 3

Sample Output 2

9

Sample Input 3

1 1

Sample Output 3

0

Sample Input 4

13 3

Sample Output 4

81

Sample Input 5

0 3

Sample Output 5

3


s = gets.chomp
n = s.length
num1 = ((n-1)/2)
num2 = ((n+3)/2)-1

str1 = s[0,num1]
str2 = s[num2,n]

print s == s.reverse && str1 == str1.reverse && str2 == str2.reverse ? 'Yes' : 'No'


even, odd = gets.split.map(&:to_i)

puts (even*(even-1) / 2) + (odd*(odd-1) / 2)


s = gets.chop.to_s
n = s.size

if s == s.reverse && s[0, ((n - 1) / 2)] == s[0, ((n - 1) / 2)].reverse && s[((n + 2) / 2), n] == s[((n + 2) / 2), n].reverse
  puts 'Yes'
else
  puts 'No'
end

Score : 200 points

Problem Statement

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

S is a palindrome.

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

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

Determine whether S is a strong palindrome.

Constraints

S consists of lowercase English letters.

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

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

akasaka

Sample Output 1

Yes

S is akasaka.

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

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

Sample Input 2

level

Sample Output 2

No

Sample Input 3

atcoder

Sample Output 3

No


Score : 300 points

Problem Statement

Given is a positive integer L.
Find the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.

Constraints

1 ≤ L ≤ 1000

L is an integer.

Input

Input is given from Standard Input in the following format:

L

Output

Print the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.
Your output is considered correct if its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

3

Sample Output 1

1.000000000000

For example, a rectangular cuboid whose dimensions are 0.8, 1, and 1.2 has a volume of 0.96.

On the other hand, if the dimensions are 1, 1, and 1, the volume of the rectangular cuboid is 1, which is greater.

Sample Input 2

999

Sample Output 2

36926037.000000000000


require 'pp'

# 入力値を数値で返却する
def gets_i()
  gets.chomp.to_i
end

# 空白区切の入力値を数値の配列で返却する
def gets_i_list()
  gets.chomp.split(" ").map(&:to_i)
end

N = gets_i
a_list = gets_i_list

count_hash = a_list.group_by(&:itself).map{|k,v| [k, v.count] }.to_h

p_count_hash = count_hash.map do |k, v|
  p_count = v * (v - 1) /2
  [k, p_count]
end.to_h
p_count_total = p_count_hash.values.inject(:+)

p_count_hash2 = count_hash.map do |k, v|
  if v >= 2
    p_count = (v - 1) * (v - 2) /2
  else
    p_count = 0
  end
  [k, p_count]
end.to_h

a_list.each do |a|
  puts p_count_total - p_count_hash[a] + p_count_hash2[a]
end

Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

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

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5


def get_i() #空白区切の入力を数値(整数)の配列で返す
  return gets.chomp.split(" ").map(&:to_i)
end
def get_f() #空白区切の入力を数値(実数)の配列で返す
  return gets.chomp.split(" ").map(&:to_f)
end
def get() #空白区切の入力を文字列の配列で返す
  return gets.chomp.split(" ")
end
def get_nsp() #入力されたものを一文字ずつに区切った文字列の配列で返す
  return gets.chomp.split("")
end
def yn_judge(bool,y="Yes",n="No") #boolに真偽を投げることで、trueならy、falseならnの値を出力する
  return bool ? y : n
end
def array(size1,init=nil,size2=1) #size2に二次元配列時の最初の要素数、size1に次の配列の大きさ、initに初期値を投げることでその配列を返す
  if size2==1
    return Array.new(size1){init}
  else
    return Array.new(size2){Array.new(size1){init}}
  end
end

#class SegTree
#  attr_accessor:node,:n
#  def initialize(n,a)
#    @n=1
#    @n*=2 while @n<n
#    @node=array(2*@n,0)
#    n.times do|i|
#      @node[i+@n]=(a[i]-1)*a[i]/2
#    end
#    (@n-1).downto(1) do|i|
#      @node[i]=@node[i*2]+@node[i*2+1]
#    end
#  end
#
#  def update(i,x)
#    i+=@n
#    @node[i]=(x-1)*x/2
#    while i>1
#      i/=2
#      @node[i]=@node[i*2]+@node[i*2+1]
#    end
#  end
#
#  def getSum(l,r)
#    ret=0
#    l+=@n
#    r+=@n
#    while l<r
#      if l.odd?
#        ret+=@node[l]
#        l+=1
#      end
#
#      if r.odd?
#        r-=1
#        ret+=@node[r]
#      end
#      l/=2
#      r/=2
#    end
#    return ret
#  end
#end
#

N=gets.to_i
A=get_i
cnt=array(N+1,0)
A.each do|a|
  cnt[a]+=1
end
sum=0
cnt.each do|num|
  sum+=(num-1)*num/2
end
#st=SegTree.new(N+1,cnt)
#N.times do|i|
#  st.update(A[i],cnt[A[i]]-1)
#  puts st.getSum(0,N+2)
#  st.update(A[i],cnt[A[i]])
#end

N.times do|i|
  val1=(cnt[A[i]]-2)*(cnt[A[i]]-1)/2
  val2=(cnt[A[i]]-1)*cnt[A[i]]/2
  puts sum+val1-val2
end


Score : 400 points

Problem Statement

Consider a grid with H rows and W columns of squares. Let (r, c) denote the square at the r-th row from the top and the c-th column from the left.
Each square is painted black or white.

The grid is said to be good if and only if the following condition is satisfied:

From (1, 1), we can reach (H, W) by moving one square right or down repeatedly, while always being on a white square.

Note that (1, 1) and (H, W) must be white if the grid is good.

Your task is to make the grid good by repeating the operation below. Find the minimum number of operations needed to complete the task. It can be proved that you can always complete the task in a finite number of operations.

Choose four integers r_0, c_0, r_1, c_1(1 \leq r_0 \leq r_1 \leq H, 1 \leq c_0 \leq c_1 \leq W). For each pair r, c (r_0 \leq r \leq r_1, c_0 \leq c \leq c_1), invert the color of (r, c) - that is, from white to black and vice versa.

Constraints

2 \leq H, W \leq 100

Input

Input is given from Standard Input in the following format:

H W
s_{11} s_{12} \cdots s_{1W}
s_{21} s_{22} \cdots s_{2W}
\vdots
s_{H1} s_{H2} \cdots s_{HW}

Here s_{rc} represents the color of (r, c) - # stands for black, and . stands for white.

Output

Print the minimum number of operations needed.

Sample Input 1

3 3
.##
.#.
##.

Sample Output 1

1

Do the operation with (r_0, c_0, r_1, c_1) = (2, 2, 2, 2) to change just the color of (2, 2), and we are done.

Sample Input 2

2 2
#.
.#

Sample Output 2

2

Sample Input 3

4 4
..##
#...
###.
###.

Sample Output 3

0

No operation may be needed.

Sample Input 4

5 5
.#.#.
#.#.#
.#.#.
#.#.#
.#.#.

Sample Output 4

4


H, W = gets.chomp.split.map(&:to_i)
p (H*W+1)/2

Score : 200 points

Problem Statement

We have a board with H horizontal rows and W vertical columns of squares.
There is a bishop at the top-left square on this board.
How many squares can this bishop reach by zero or more movements?

Here the bishop can only move diagonally.
More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds:

r_1 + c_1 = r_2 + c_2

r_1 - c_1 = r_2 - c_2

For example, in the following figure, the bishop can move to any of the red squares in one move:

Constraints

1 \leq H, W \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H \ W

Output

Print the number of squares the bishop can reach.

Sample Input 1

4 5

Sample Output 1

10

The bishop can reach the cyan squares in the following figure:

Sample Input 2

7 3

Sample Output 2

11

The bishop can reach the cyan squares in the following figure:

Sample Input 3

1000000000 1000000000

Sample Output 3

500000000000000000


h,w = gets.split(" ").map{|i| i.to_i}

if h==1 or w==1
  p 1
 else
  p (h*w+1)/2
end


s=gets.chomp.split(//);puts s.uniq.size == 2 ? "Yes" : "No"

Score : 100 points

Problem Statement

In AtCoder City, there are three stations numbered 1, 2, and 3.

Each of these stations is operated by one of the two railway companies, A and B. A string S of length 3 represents which company operates each station. If S_i is A, Company A operates Station i; if S_i is B, Company B operates Station i.

To improve the transportation condition, for each pair of a station operated by Company A and one operated by Company B, there will be a bus service connecting them.

Determine if there is a pair of stations that will be connected by a bus service.

Constraints

Each character of S is A or B.

|S| = 3

Input

Input is given from Standard Input in the following format:

S

Output

If there is a pair of stations that will be connected by a bus service, print Yes; otherwise, print No.

Sample Input 1

ABA

Sample Output 1

Yes

Company A operates Station 1 and 3, while Company B operates Station 2.

There will be a bus service between Station 1 and 2, and between Station 2 and 3, so print Yes.

Sample Input 2

BBA

Sample Output 2

Yes

Company B operates Station 1 and 2, while Company A operates Station 3.

There will be a bus service between Station 1 and 3, and between Station 2 and 3, so print Yes.

Sample Input 3

BBB

Sample Output 3

No

Company B operates all the stations. Thus, there will be no bus service, so print No.


s =  gets.chomp
ans = s.count("A") < 3 && s.count("B") < 3
puts ans ? "Yes" : "No"


a=gets.count("A")
puts a==3||a==0? :No: :Yes


s = gets.split('').map(&:to_s)

s.uniq!

if s.size == 1
	puts "No"
else
	puts "Yes"
end


# AtCoder 市には 3 つの駅があり、1,2,3 の番号がつけられています。
# これらの駅は、それぞれ鉄道会社A, Bのいずれかが管理しています。
# 管理状況は長さ 3 の文字列 S で表され、
# 駅 i は Si が A のとき鉄道会社 A が、
# B のとき鉄道会社 B が管理しています。
# 鉄道会社 A が管理している駅と、鉄道会社 B が管理している駅の間には、
# 交通の便のためにバスを運行することになりました。
# 実際にバスが運行することになる駅の組み合わせが存在するかどうかを判定してください。

s = gets

if s.include?("A") && s.include?("B")
    puts "Yes"
else
    puts "No"
end


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

s = []
while s.size < n do
  a = s.size + a >= n ? n - s.size : a
  s << [1] * a
  s << [0] * b
  s.flatten!
end

print s.sum

Score : 200 points

Problem Statement

Takahashi has many red balls and blue balls. Now, he will place them in a row.

Initially, there is no ball placed.

Takahashi, who is very patient, will do the following operation 10^{100} times:

Place A blue balls at the end of the row of balls already placed. Then, place B red balls at the end of the row.

How many blue balls will be there among the first N balls in the row of balls made this way?

Constraints

1 \leq N \leq 10^{18}

A, B \geq 0

0 < A + B \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of blue balls that will be there among the first N balls in the row of balls.

Sample Input 1

8 3 4

Sample Output 1

4

Let b denote a blue ball, and r denote a red ball. The first eight balls in the row will be bbbrrrrb, among which there are four blue balls.

Sample Input 2

8 0 4

Sample Output 2

0

He placed only red balls from the beginning.

Sample Input 3

6 2 4

Sample Output 3

2

Among bbrrrr, there are two blue balls.


a, b = gets.split.map(&:to_i)
z = (a * 100) / 8.0

if a > b
  puts '-1'
  exit
end

if (z * 0.1).floor == b
  puts z.floor
else
  puts '-1'
end

Score : 300 points

Problem Statement

Find the price of a product before tax such that, when the consumption tax rate is 8 percent and 10 percent, the amount of consumption tax levied on it is A yen and B yen, respectively. (Yen is the currency of Japan.)

Here, the price before tax must be a positive integer, and the amount of consumption tax is rounded down to the nearest integer.

If multiple prices satisfy the condition, print the lowest such price; if no price satisfies the condition, print -1.

Constraints

1 \leq A \leq B \leq 100

A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If there is a price that satisfies the condition, print an integer representing the lowest such price; otherwise, print -1.

Sample Input 1

2 2

Sample Output 1

25

If the price of a product before tax is 25 yen, the amount of consumption tax levied on it is:

When the consumption tax rate is 8 percent: \lfloor 25 \times 0.08 \rfloor = \lfloor 2 \rfloor = 2 yen.

When the consumption tax rate is 10 percent: \lfloor 25 \times 0.1 \rfloor = \lfloor 2.5 \rfloor = 2 yen.

Thus, the price of 25 yen satisfies the condition. There are other possible prices, such as 26 yen, but print the minimum such price, 25.

Sample Input 2

8 10

Sample Output 2

100

If the price of a product before tax is 100 yen, the amount of consumption tax levied on it is:

When the consumption tax rate is 8 percent: \lfloor 100 \times 0.08 \rfloor = 8 yen.

When the consumption tax rate is 10 percent: \lfloor 100 \times 0.1 \rfloor = 10 yen.

Sample Input 3

19 99

Sample Output 3

-1

There is no price before tax satisfying this condition, so print -1.


S = gets.chomp.chars
Q = gets.to_i
QList = []
Q.times{ QList << gets.chomp }

reverse = false
QList.each do  |query|
  ch = query.split
  if ch[0].to_i == 1
    reverse = !reverse
  elsif ch[1].to_i == 1 and reverse or ch[1].to_i == 2 and !reverse
    S << ch[2]
  else
    S.unshift(ch[2])
  end
end

if reverse
  S.reverse!
end

puts S.join

Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

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

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy


Score : 100 points

Problem Statement

Takahashi wants to print a document with N pages double-sided, where two pages of data can be printed on one sheet of paper.

At least how many sheets of paper does he need?

Constraints

N is an integer.

1 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

5

Sample Output 1

3

By printing the 1-st, 2-nd pages on the 1-st sheet, 3-rd and 4-th pages on the 2-nd sheet, and 5-th page on the 3-rd sheet, we can print all the data on 3 sheets of paper.

Sample Input 2

2

Sample Output 2

1

Sample Input 3

100

Sample Output 3

50


class BIT
  def initialize(n)
    @size = n
    @data = Array.new(n + 1, 0)
  end
  def sum(l,r)
    s = 0
    while  l > 0 || r > 0
      s = s + @data[r] - @data[l]
      l = l - (l & -l)
      r = r - (r & -r)
    end
    return s
  end
  def add(i,x)
    while i <= @size
      @data[i] += x
      i += (i & -i)
    end
  end
end
A_ORD = 'a'.ord
N = gets.to_i
S = gets.chomp.chars
C = Array.new(26){ BIT.new(N + 1) }
(1 .. N).each do |i|
  C[S[i - 1].ord - A_ORD].add(i, 1)
end

Q = gets.to_i
Q.times do
  q = gets.chomp.split
  t = q.shift.to_i
  if t == 1
    i = q.shift.to_i
    c = q.shift
    c_ = S[i - 1]
    if c_ != c
      C[c_.ord - A_ORD].add(i, -1)
      C[c.ord - A_ORD].add(i, 1)
      S[i - 1] = c
    end
  else
    l,r = q.map(&:to_i)
    puts C.count{|bit| bit.sum(l - 1, r) > 0 }
  end
end

Score : 500 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.

Process Q queries of the following two types:

Type 1: change the i_q-th character of S to c_q. (Do nothing if the i_q-th character is already c_q.)

Type 2: answer the number of different characters occurring in the substring of S between the l_q-th and r_q-th characters (inclusive).

Constraints

N, Q, i_q, l_q, and r_q are integers.

S is a string consisting of lowercase English letters.

c_q is a lowercase English letter.

1 \leq N \leq 500000

1 \leq Q \leq 20000

|S| = N

1 \leq i_q \leq N

1 \leq l_q \leq r_q \leq N

There is at least one query of type 2 in each testcase.

Input

Input is given from Standard Input in the following format:

N
S
Q
Query_1
\vdots
Query_Q

Here, Query_i in the 4-th through (Q+3)-th lines is one of the following:

1 i_q c_q

2 l_q r_q

Output

For each query of type 2, print a line containing the answer.

Sample Input 1

7
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7

Sample Output 1

3
1
5

In the first query, cdbb contains three kinds of letters: b , c , and d, so we print 3.

In the second query, S is modified to abcdzbd.

In the third query, a contains one kind of letter: a, so we print 1.

In the fourth query, S is modified to abcazbd.

In the fifth query, S does not change and is still abcazbd.

In the sixth query, abcazbd contains five kinds of letters: a, b, c, d, and z, so we print 5.


n,k = gets.chomp.split(' ').map(&:to_i)
puts n.to_s(k).size

Score : 200 points

Problem Statement

Given is an integer N. Find the number of digits that N has in base K.

Notes

For information on base-K representation, see Positional notation - Wikipedia.

Constraints

All values in input are integers.

1 \leq N \leq 10^9

2 \leq K \leq 10

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of digits that N has in base K.

Sample Input 1

11 2

Sample Output 1

4

In binary, 11 is represented as 1011.

Sample Input 2

1010101 10

Sample Output 2

7

Sample Input 3

314159265 3

Sample Output 3

18


n,k = gets.split.map(&:to_i)
puts n.to_s(k).size


n,k= gets.split.map &:to_i

puts n.to_s(k).size


n = gets.chomp.to_i
x = gets.chomp.split(" ").map(&:to_i)
total = nil
(x.min..x.max).map do |c|
  sum = x.map{ |e| (c - e)**2 }.inject(:+)
  total = sum if c == x.min
  if sum <= total
    total = sum
  else
    break
  end
end
p total

Score : 300 points

Problem Statement

There are N people living on a number line.

The i-th person lives at coordinate X_i.

You are going to hold a meeting that all N people have to attend.

The meeting can be held at any integer coordinate. If you choose to hold the meeting at coordinate P, the i-th person will spend (X_i - P)^2 points of stamina to attend the meeting.

Find the minimum total points of stamina the N people have to spend.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq X_i \leq 100

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print the minimum total stamina the N people have to spend.

Sample Input 1

2
1 4

Sample Output 1

5

Assume the meeting is held at coordinate 2. In this case, the first person will spend (1 - 2)^2 points of stamina, and the second person will spend (4 - 2)^2 = 4 points of stamina, for a total of 5 points of stamina. This is the minimum total stamina that the 2 people have to spend.

Note that you can hold the meeting only at an integer coordinate.

Sample Input 2

7
14 14 2 13 56 2 37

Sample Output 2

2354


def f(point, arr)
  result = arr.inject(0){|result, item|
    result + (item-point)**2
  }
  return result
end
def re(low, high, arr)
  return f(((high-low)/2)+low, arr) if high-low <= 2
  mid_left = ((low * 2) + high) / 3
  mid_right = (low + (high * 2)) / 3
  if f(mid_left, arr) >= f(mid_right, arr)
    re(mid_left, high, arr)
  else
    re(low, mid_right, arr)
  end
end
n = gets.chop.to_i
arr = gets.chop.split.map(&:to_i)

print re(arr.min, arr.max, arr)


_ = gets.chop.to_i
arr = gets.chop.split.map(&:to_i)
avr = (arr.inject(:+) / arr.length).to_i
avr2 = (arr.inject(:+) / arr.length).to_i + 1
avr3 = (arr.inject(:+) / arr.length).to_i - 1
res1 = 0
res2 = 0
res3 = 0
for n in arr
  res1 += (n - avr) * (n - avr)
  res2 += (n - avr2) * (n - avr2)
  res3 += (n - avr3) * (n - avr3)
end
puts [res1, res2, res3].min


lines = readlines.map{|line| line.split(' ').map(&:to_i)}
a=lines[0][0]
b=lines[1]
d=[]
for i in 1..100
  c=0
  for j in b
   c=c+(j-i)**2
  end
  d.push(c)
end
puts d.min


MOD = 1000000007

def conv(a, b)
    if a > b / 2
        b = a - b
    end
    ans = 1
    div = 1
    b.times do |i|
        ans *= (a - i)
        div *= (i + 1)
        ans %= MOD
        div %= MOD
    end
    ans = ans * pow(div, MOD - 2)
    ans % MOD
end

def pow(x, n)
    if n == 0
        1
    elsif n % 2 == 0
        pow(x * (x % MOD), n / 2) % MOD
    else
        x * pow(x, n - 1) % MOD
    end
end

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

all = 2 ** n - 1 % MOD
if if a > n / 2
    a = n - a
end
if b > n / 2
    b = n - b
end
all = all - conv(n, a) - conv(n, b)
all %= MOD

puts all

Score : 400 points

Problem Statement

Akari has n kinds of flowers, one of each kind.

She is going to choose one or more of these flowers to make a bouquet.

However, she hates two numbers a and b, so the number of flowers in the bouquet cannot be a or b.

How many different bouquets are there that Akari can make?

Find the count modulo (10^9 + 7).

Here, two bouquets are considered different when there is a flower that is used in one of the bouquets but not in the other bouquet.

Constraints

All values in input are integers.

2 \leq n \leq 10^9

1 \leq a < b \leq \textrm{min}(n, 2 \times 10^5)

Input

Input is given from Standard Input in the following format:

n a b

Output

Print the number of bouquets that Akari can make, modulo (10^9 + 7). (If there are no such bouquets, print 0.)

Sample Input 1

4 1 3

Sample Output 1

7

In this case, Akari can choose 2 or 4 flowers to make the bouquet.

There are 6 ways to choose 2 out of the 4 flowers, and 1 way to choose 4, so there are a total of 7 different bouquets that Akari can make.

Sample Input 2

1000000000 141421 173205

Sample Output 2

34076506

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


class Combi
  def initialize(max_n, mod = 10**9+7)
    @mod = mod
    @max_n = max_n

    @fac = Array.new(@max_n+1)
    @inv = Array.new(@max_n+1)
    @finv = Array.new(@max_n+1)

    @fac[0] = @fac[1] = 1
    @finv[0] = @finv[1] = 1
    @inv[1] = 1
    2.upto(@max_n) do |i|
      @fac[i] = @fac[i-1] * i % @mod
      @inv[i] = @mod - @inv[@mod%i] * (@mod/i) % @mod
      @finv[i] = @finv[i-1] * @inv[i] % @mod
    end
  end

  def nCk(n,k)
    @fac[n] * @finv[n - k] * @finv[k] % @mod
  end
end

require 'set'; require 'prime'
INF=Float::INFINITY; MOD=10**9+7
n = gets.chomp.to_i
a = gets.chomp.split.map(&:to_i)

h = {}
i1, i2 = nil, nil
a.each_with_index do |e, i|
  if h.has_key? e
    i1 = h[e]
    i2 = i
    break
  else
    h[e] = i
  end
end

# p [i1,i2]

combi = Combi.new(n+1)

1.upto(n+1) do |k|
  x = combi.nCk(n+1,k)
  y = n - i2 + i1 >= k-1 ? combi.nCk(n - i2 + i1, k-1) : 0

  ans = x - y
  # ans += MOD if ans < 0
  # p [k,ans, '', x,y]
  puts ans
end


_n = gets.to_i
a = gets.split(' ').map(&:to_i)
s = a.inject(:+)
if s % 3 == 0 || s % 5 == 0
  puts 'APPROVED'
else
  puts 'DENIED'
end

Score: 100 points

Problem Statement

A triple of numbers is said to be poor when two of those numbers are equal but the other number is different from those two numbers.

You will be given three integers A, B, and C. If this triple is poor, print Yes; otherwise, print No.

Constraints

A, B, and C are all integers between 1 and 9 (inclusive).

Input

Input is given from Standard Input in the following format:

A B C

Output

If the given triple is poor, print Yes; otherwise, print No.

Sample Input 1

5 7 5

Sample Output 1

Yes

A and C are equal, but B is different from those two numbers, so this triple is poor.

Sample Input 2

4 4 4

Sample Output 2

No

A, B, and C are all equal, so this triple is not poor.

Sample Input 3

4 9 6

Sample Output 3

No

Sample Input 4

3 3 4

Sample Output 4

Yes


x,y,z=gets.chomp.split(' ').map(&:to_i)
if (x==y&&y==z&&z==x)||(x!=y&&y!=z&&z!=x)
    puts "No"
else
    puts "Yes"
end


str = gets.chomp.split(' ').map(&:to_i)
if str[0] == str[1] && str[0] != str[2]
  puts "Yes"
elsif str[0] == str[2] && str[0] != str[1]
  puts "Yes"
elsif str[1] == str[2] && str[0] != str[1]
  puts "Yes"
else
  puts "No"
end


arr = gets.chop.split.map(&:to_i)
if (arr[0] == arr[2] && arr[0] != arr[1]) || (arr[0] == arr[1] && arr[0] != arr[2]) || (arr[0] != arr[1] && arr[1] == arr[2])
  puts 'Yes'
else
  puts 'No'
end


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

if a.select{|x| x % 2 == 0 }.find{|x| x % 3 != 0 && x % 5 != 0}
  print "DENIED\n"
else
  print "APPROVED\n"
end

Score: 200 points

Problem Statement

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

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

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

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

Notes

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

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq A_i \leq 1000

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

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

Sample Input 1

5
6 7 9 10 31

Sample Output 1

APPROVED

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

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

Sample Input 2

3
28 27 24

Sample Output 2

DENIED

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


a = gets.chomp.split.map(&:to_i)
b = a.select { |x| x % 2 == 0 }
c = b.select { |y| y % 3 == 0 || y % 5 == 0 }
if b.size == c.size
  puts "APPROVED"
else
  puts "DENIED"
end


gets
puts gets.chomp.split(/ /).map(&:to_i).select{|a| a % 2 == 0 && a % 3 != 0 && a % 5 != 0}.length > 0 ? 'DENIED' : 'APPROVED'


n=gets.to_i
h=Hash.new(0)
n.times do |i|
    s=gets.chomp
    h[:"#{s}"]+=1
end

h=h.sort
max=h.max_by{|x| x[1]}[1]
h.each do |h|
    if h[1]==max
        puts h[0]
    end
end

Score: 300 points

Problem Statement

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

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

Constraints

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

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

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

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print all strings in question in lexicographical order.

Sample Input 1

7
beat
vet
beet
bed
vet
bet
beet

Sample Output 1

beet
vet

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

Sample Input 2

8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo

Sample Output 2

buffalo

Sample Input 3

7
bass
bass
kick
kick
bass
kick
kick

Sample Output 3

kick

Sample Input 4

4
ushi
tapu
nichia
kun

Sample Output 4

kun
nichia
tapu
ushi


from collections import Counter
N = int(input())
S = []
for i in range(N):
  S.append(input())

c = Counter(S)
list = []
word_list = []
for i in c.most_common():
    list.append(i[1])
  	word_list.append(i[0])
list_max = list.most_common()
result_list = word_list[0:list_max]
for x in list_max.sort():
    print(x)


n = gets.to_i
s = []
n.times do |i|
  s[i] = gets
end

name = s.uniq
ind = []
name.size.times do |i|
  ind.push(s.count(name[i]))
end
ind.sort_by!{|item| item}
i = name.size - 1
name.size.times do
  puts (name[i])
  i -= 1
end


n = gets.to_i
strs = n.times.map{ gets.chomp.to_s }.sort

if strs.uniq.size == n
  puts strs[0]
  exit
end
if strs.uniq.size == 1
  puts strs[0]
  exit
end
if n == 1
  puts strs[0]
  exit
end
max_str_ary = []
max_num = 1
count_hash = Hash.new(0)
pre_elem = ''
pre_num = 0

strs.each_with_index do |elem, i|
  count_hash[elem] += 1
  if pre_elem != elem
    pre_elem = elem
    cnt = i+1 - pre_num
    pre_num = i+1
    if cnt > max_num
      max_num = cnt
    end
  end
end
puts count_hash.find_all { |_, v| v == max_num }.to_h.keys


n = gets.to_i
ar = []
n.times do
  ar << gets.chomp
end

hash = Hash.new(0)
ar.each do |elem|
  hash[elem] += 1
end

h_max = hash.values.max

hash.select{|k,v| v == h_max}.keys.sort.each do |v|
  puts v
end


n = gets.to_i
d = Hash.new(0)
n.times do
  d[gets.chomp] += 1
end
m = d.max[1]
l = []
d.each do |k, v|
  if v == m then
    l << k
  end
end
print l.sort.join("\n")


n = gets.to_i
strs = n.times.map{ gets.chomp.to_s }
count_hash = Hash.new(0)
strs.each do |elem|
  count_hash[elem] += 1
end
print Hash[ count_hash.sort_by{ |_, v| -v } ].keys.sort


N, K = gets.chomp.split(" ").map(&:to_i)
Dice = gets.chomp.split(" ").map(&:to_i)
s, max = 0, 0

while Dice.size >= K do
   K.times do |i|
    s += Dice[i]
   end
   if (max <= s)
    max = s
   end
   s = 0
   Dice.shift()
end

puts "#{(max+K)/2.0}"

Score : 100 points

Problem Statement

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

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

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

Constraints

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

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

S \not= T

S=U or T=U.

1 \leq A,B \leq 10

A and B are integers.

Input

Input is given from Standard Input in the following format:

S T
A B
U

Output

Print the answer, with space in between.

Sample Input 1

red blue
3 4
red

Sample Output 1

2 4

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

Sample Input 2

red blue
5 5
blue

Sample Output 2

5 4

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


s, t = gets.split
a, b = gets.split.map(&:to_i)
u = gets.chomp

if u == s
  puts [a - 1, b].join(" ")
else
  puts [a, b - 1].join(" ")
end


Score : 200 points

Problem Statement

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

Constraints

S is a string consisting of lowercase English letters.

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

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

sardine

Sample Output 1

xxxxxxx

Replacing every character in S with x results in xxxxxxx.

Sample Input 2

xxxx

Sample Output 2

xxxx

Sample Input 3

gone

Sample Output 3

xxxx


n = gets.to_i
a = gets.split.map(&:to_i)
b = {}
ans = 'YES'
for i in 0..n - 1
  if b[a[i].to_s.to_sym]
    ans = 'NO'
    break
  else
    b[a[i].to_s.to_sym] = 1
  end
end
puts ans

Score : 300 points

Problem Statement

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

Constraints

2 ≤ N ≤ 200000

1 ≤ A_i ≤ 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

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

Sample Input 1

5
2 6 1 4 5

Sample Output 1

YES

The elements are pairwise distinct.

Sample Input 2

6
4 1 3 1 6 2

Sample Output 2

NO

The second and fourth elements are identical.

Sample Input 3

2
10000000 10000000

Sample Output 3

NO


require "bigdecimal"
require "bigdecimal/util"

def gi() gets.to_i end
def gs() gets.chomp end
def gim() gets.split.map(&:to_i) end
def gsm() gets.split.map(&:chomp) end
def YesNo1(bool, yes="Yes", no="No") puts (bool ? yes : no) end
def YESNO2(bool, yes="YES", no="NO") puts (bool ? yes : no) end

#####
n,k = gim
nums = gim

nums = [0] + nums
#print nums
max = 0
lastSum = nums[0..k-1].inject(:+)
(n-k+1).times do |i|
  sum = lastSum + nums[i+k] - nums[i]
  lastSum = sum
  max = [max, sum].max
end

puts (max+k)/2.0

Score : 400 points

Problem Statement

We have N dice arranged in a line from left to right. The i-th die from the left shows p_i numbers from 1 to p_i with equal probability when thrown.

We will choose K adjacent dice, throw each of them independently, and compute the sum of the numbers shown. Find the maximum possible value of the expected value of this sum.

Constraints

1 ≤ K ≤ N ≤ 200000

1 ≤ p_i ≤ 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
p_1 ... p_N

Output

Print the maximum possible value of the expected value of the sum of the numbers shown.

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

Sample Input 1

5 3
1 2 2 4 5

Sample Output 1

7.000000000000

When we throw the third, fourth, and fifth dice from the left, the expected value of the sum of the numbers shown is 7. This is the maximum value we can achieve.

Sample Input 2

4 1
6 6 6 6

Sample Output 2

3.500000000000

Regardless of which die we choose, the expected value of the number shown is 3.5.

Sample Input 3

10 4
17 13 13 12 15 20 10 13 17 11

Sample Output 3

32.000000000000


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

Dice.map!{|x| ((x + 1).quo(2)).to_f}

s, max = 0, 0

while Dice.size >= K do
   K.times do |i|
    s += Dice[i]
   end
   if (max <= s)
    max = s
   end
   s = 0
   Dice.shift()
end
puts "#{max}"


def calc(n,k)
  if n.to_s.length < k
    0
  elsif k == 0
    1
  elsif n < 10
    n
  else
    #最初の桁が0の場合　+ 最初の桁が0意外の場合
    calc(10 ** (n.to_s.length-1) -1,k) + (n.to_s[0].to_i - 1) * calc(10 ** (n.to_s.length-1) -1,k-1) + calc(n.to_s[1..].to_i,k-1)
  end
end

n = gets.to_i
k = gets.to_i
puts calc(n,k)

Score : 500 points

Problem Statement

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

Constraints

1 \leq N < 10^{100}

1 \leq K \leq 3

Input

Input is given from Standard Input in the following format:

N
K

Output

Print the count.

Sample Input 1

100
1

Sample Output 1

19

The following 19 integers satisfy the condition:

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

Sample Input 2

25
2

Sample Output 2

14

The following 14 integers satisfy the condition:

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

Sample Input 3

314159
2

Sample Output 3

937

Sample Input 4

9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
3

Sample Output 4

117879300


# after listening explanation
mod = 1000000007
def sqsum(r, c)
  mod = 1000000007
  a = r+c+2
  b = r<c ? r+1 : c+1
  ans = 1
  b.times{|bi| ans *= (a-bi) }
  b.times{|bi| ans /= (bi+1) }
  (ans-1)%mod
end
r1, c1, r2, c2 = gets.split.map(&:to_i)
p (sqsum(r2,c2)-sqsum(r1-1,c2)-sqsum(r2,c1-1)+sqsum(r1-1,c1-1))%mod

Score : 600 points

Problem Statement

Snuke is standing on a two-dimensional plane. In one operation, he can move by 1 in the positive x-direction, or move by 1 in the positive y-direction.

Let us define a function f(r, c) as follows:

f(r,c) := (The number of paths from the point (0, 0) to the point (r, c) that Snuke can trace by repeating the operation above)

Given are integers r_1, r_2, c_1, and c_2.
Find the sum of f(i, j) over all pair of integers (i, j) such that r_1 ≤ i ≤ r_2 and c_1 ≤ j ≤ c_2, and compute this value modulo (10^9+7).

Constraints

1 ≤ r_1 ≤ r_2 ≤ 10^6

1 ≤ c_1 ≤ c_2 ≤ 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

r_1 c_1 r_2 c_2

Output

Print the sum of f(i, j) modulo (10^9+7).

Sample Input 1

1 1 2 2

Sample Output 1

14

For example, there are two paths from the point (0, 0) to the point (1, 1): (0,0) → (0,1) → (1,1) and (0,0) → (1,0) → (1,1), so f(1,1)=2.

Similarly, f(1,2)=3, f(2,1)=3, and f(2,2)=6. Thus, the sum is 14.

Sample Input 2

314 159 2653 589

Sample Output 2

602215194


h , a = gets.chop.split.map(&:to_i)
def atacck_count(h,a)
if h / a == 0
  h / a
elsif (h / a) + 1
end
end

p def atacck_count(h,a)

Score : 100 points

Problem Statement

Serval is fighting with a monster.

The health of the monster is H.

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

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

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

Constraints

1 \leq H \leq 10^4

1 \leq A \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H A

Output

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

Sample Input 1

10 4

Sample Output 1

3

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

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

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

Thus, Serval needs to make three attacks to win.

Sample Input 2

1 10000

Sample Output 2

1

Sample Input 3

10000 1

Sample Output 3

10000


h, a = gets.split.map(&:to_i)
p (h-1)/a+1


h, = gets.split("\s").map(&:to_i)
a = gets.split("\s").map(&:to_i)
p a.inject(&:+) >= h ? 'Yes' : 'No'

Score : 200 points

Problem Statement

Raccoon is fighting with a monster.

The health of the monster is H.

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

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

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

Constraints

1 \leq H \leq 10^9

1 \leq N \leq 10^5

1 \leq A_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H N
A_1 A_2 ... A_N

Output

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

Sample Input 1

10 3
4 5 6

Sample Output 1

Yes

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

Sample Input 2

20 3
4 5 6

Sample Output 2

No

Sample Input 3

210 5
31 41 59 26 53

Sample Output 3

Yes

Sample Input 4

211 5
31 41 59 26 53

Sample Output 4

No


H, N = gets.chomp.split.map(&:to_i)
A = gets.chomp.split.map(&:to_i)
A.each do |attack|
  H = H - attack
end
puts H > 0 ? 'No' : 'Yes'


n,k=gets.split(' ').map(&:to_i)
h=gets.split(' ').map(&:to_i)

h.sort!.reverse!
count = 0
if k>=n
  puts 0
else
  for i in k..n-1
    count+=h[i]
  end
end

puts count

Score : 300 points

Problem Statement

Fennec is fighting with N monsters.

The health of the i-th monster is H_i.

Fennec can do the following two actions:

Attack: Fennec chooses one monster. That monster's health will decrease by 1.

Special Move: Fennec chooses one monster. That monster's health will become 0.

There is no way other than Attack and Special Move to decrease the monsters' health.

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

Find the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning when she can use Special Move at most K times.

Constraints

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

0 \leq K \leq 2 \times 10^5

1 \leq H_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
H_1 ... H_N

Output

Print the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning.

Sample Input 1

3 1
4 1 5

Sample Output 1

5

By using Special Move on the third monster, and doing Attack four times on the first monster and once on the second monster, Fennec can win with five Attacks.

Sample Input 2

8 9
7 9 3 2 3 8 4 6

Sample Output 2

0

She can use Special Move on all the monsters.

Sample Input 3

3 0
1000000000 1000000000 1000000000

Sample Output 3

3000000000

Watch out for overflow.


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

puts N-K > 0 ? H[0...K].inject(&:+) : 0


n,k=gets.split.map(&:to_i)
monsters=gets.split.map(&:to_i)
m = monsters.sort.reverse
k.times { m.shift }
puts m.inject(:+)


a=gets.to_i;
c=1
if a==1
  p c
  exit
end
while(1)
  l=2**c-1
  if a <= l
  	p 2**c-1
    exit
  end
  c+=1
end

Score : 400 points

Problem Statement

Caracal is fighting with a monster.

The health of the monster is H.

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

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

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

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

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

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

Constraints

1 \leq H \leq 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H

Output

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

Sample Input 1

2

Sample Output 1

3

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

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

Sample Input 2

4

Sample Output 2

7

Sample Input 3

1000000000000

Sample Output 3

1099511627775


arr = [1]
num = gets.to_i
time = 0
40.times do |i|
	tmp = arr[-1] * 2
	if tmp == num
		time = i + 2
      	break
	elsif tmp > num
		time = i + 1
      	break
	end
	arr << tmp
end

def factorial(n)
    return 1 if n == 1
    return factorial(n-1) * 2 + 1
end

p factorial(time)


H = gets.chomp.to_i
cnt = 0

while 1 do
  if H < 2 ** cnt
    break
  end
  cnt += 1
end
puts (2 ** cnt) - 1


h = gets.chomp.to_i

@memo = {}
def solve(n)
  return 1 if n == 1
  return @memo[n] if @memo[n]

  m = (n/2.0).floor
  @memo[n] = 1 + solve(m)*2
end

puts solve(h)


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

if n == m
  puts "YES"
else
  puts "NO"
end

Score : 100 points

Problem Statement

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

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

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

Determine whether Takahashi's submission gets an AC.

Constraints

1 \leq N \leq 100

0 \leq M \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

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

Sample Input 1

3 3

Sample Output 1

Yes

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

Sample Input 2

3 2

Sample Output 2

No

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

Sample Input 3

1 1

Sample Output 3

Yes


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

result1 = a.to_s * b
result2 = b.to_s * a

puts result1 if result1.to_i < result2.to_i
puts result2

Score : 200 points

Problem Statement

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

Constraints

1 \leq a \leq 9

1 \leq b \leq 9

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

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

Sample Input 1

4 3

Sample Output 1

3333

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

Sample Input 2

7 7

Sample Output 2

7777777


n = gets.chomp.to_i
ans = 0

memo = {}
1.upto(9) do |i|
  1.upto(9) do |u|
    tmp = 0
    if u == i
      1.upto(7) do |m|
        tmp += 1 if (u.to_s * m).to_i <= n
      end
    end
    if (u.to_s + i.to_s).to_i <= n
      tmp += 1
    end
    0.upto(999) do |t|
      if (u.to_s + t.to_s + i.to_s).to_i <= n
        tmp += 1
      end
    end
    memo[i.to_s + u.to_s] = tmp
  end
end

n.downto(1) do |i|
  if i < 10
    ans += 1
    next
  end

  f = i.to_s.split('').first
  l = i.to_s.split('').last
  next if l == '0'
  ans += memo[f.to_s + l.to_s]
end
p ans

Score : 400 points

Problem Statement

Given is a positive integer N.

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

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

Constraints

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

25

Sample Output 1

17

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

Sample Input 2

1

Sample Output 2

1

Sample Input 3

100

Sample Output 3

108

Sample Input 4

2020

Sample Output 4

40812

Sample Input 5

200000

Sample Output 5

400000008


MOD = 1000000007

def prime_fact(n)
  #素因数分解
  i = 2
  while i ** 2 <= n do
    cnt = 0
    while n % i == 0
      cnt += 1
      n /= i
    end

    @map[i] = cnt if cnt > 0 && (@map[i] == nil || @map[i] < cnt)
    i += 1
  end

  @map[n] = 1 if n != 1 && @map[n] == nil
end

def power(n, a, m)
  res = 1

  if a > 0
    res = power(n, a / 2, m)
    if a % 2 == 0
      res = (res * res) % m
    else
      res = ((res * res) % m * n) % m
    end
  end

  return res
end

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

  @map = {}

  a.each do |v|
    prime_fact(v)
  end

  gcd = 1
  @map.map { |v, k|
    gcd *= power(v, k, MOD)
  }

  ans = 0
  a.each do |v|
    ans += (gcd / v)
  end

  puts ans % MOD
end

main

Score : 500 points

Problem Statement

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

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

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

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

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

Constraints

1 \leq N \leq 10^4

1 \leq A_i \leq 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

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

Sample Input 1

3
2 3 4

Sample Output 1

13

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

Sample Input 2

5
12 12 12 12 12

Sample Output 2

5

We can let all B_i be 1.

Sample Input 3

3
1000000 999999 999998

Sample Output 3

996989508

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


def powmod(b,r,mod)
  a = 1
  while r > 0
    if r%2 == 1
      a = a * b % mod
    end
    b = b * b % mod
    r /= 2
  end
  a
end

def invmod(f, mod)
  powmod(f, mod-2, mod)
end

require "prime"
N,*A = `dd`.split.map &:to_i
Mod = 10**9 + 7
qs = {}
PD = A.map{ |a|
  pd = a.prime_division
  pd.each{ |q,d| qs[q] = [qs[q] || 0, d].max }
  pd
}
LCM = qs.map{ |q,d| powmod(q,d,Mod) }.reduce{ |a,b| a * b % Mod }
p PD.reduce(0){ |a,pd|
  divisor = pd.map{ |q,d| powmod(q,d,Mod) }.reduce{ |a,b| a * b % Mod }
  (a + (LCM * invmod(divisor,Mod))) % Mod
}


require 'prime'
class Integer
  def modinv(m)
    a, b, i, j = self, m, 1, 0
    while b > 0
      t = a / b
      a, b = b, a - t * b
      i, j = j, i - t * j
    end
    return i % m
  end
end

n = gets.to_i
a = gets.split.map(&:to_i)
result = a.inject{|r,i| r.lcm(i)}%1000000007
a = a.map{|i| result * i.modinv(1000000007)}
puts a.inject{|r,i| (r+i)%1000000007}


require 'prime'
n = gets.to_i
a = gets.split.map{|e| e.to_i.prime_division.to_h}

h = Hash.new(0)
a.each do |e|
    e.each do |arr|
        h[arr[0]] = arr[1] if h[arr[0]] < arr[1]
    end
end

ans = 0
mod = 10 ** 9 + 7
n.times do |i|
    x = 1
    h.keys.each do |key|
        x = x * key ** (h[key] - (a[i][key] ? a[i][key] : 0)) % mod
    end
    ans = (ans + x) % mod
end

puts ans


n=gets.to_i
a=gets.split.map(&:to_i)
mod=1000000007
l=1
n.times do |i|
  l=l.lcm(a[i])
end
c=0
n.times do |i|
  c+=l/a[i]
  c%=mod
end
puts c


require 'pp'

MOD = 10 ** 9 + 7
n = gets.chomp!.to_i
a_n = gets.chomp!.split.map(&:to_i)

l = 1
a_n.each do |a|
    l = a.lcm(l)
end

b_n = Array.new(n)
a_n.each_with_index do |a, i|
    b_n[i] = l / a
end

ans = 0
b_n.each do |b|
    ans = (ans + b) % MOD
end

puts ans


n = gets.to_i
arr = Array.new(n).map{Array.new(2, 0)}

(0..n-1).each do |i|
  x, l = gets.split.map(&:to_i)
  arr[i][0] = x - l
  arr[i][1] = x + l
end
arr.sort_by! { |a| a[1] }
ans = 0
cur = -1e9
(0..n-1).each do |i|
  next if cur > arr[i][0]
  ans += 1
  cur = arr[i][1]
end
puts ans

Score : 200 points

Problem Statement

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

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

Find the maximum number of robots that we can keep.

Constraints

1 \leq N \leq 100,000

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

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

If i \neq j, X_i \neq X_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 L_1
X_2 L_2
\vdots
X_N L_N

Output

Print the maximum number of robots that we can keep.

Sample Input 1

4
2 4
4 3
9 3
100 5

Sample Output 1

3

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

Sample Input 2

2
8 20
1 10

Sample Output 2

1

Sample Input 3

5
10 1
2 1
4 1
6 1
8 1

Sample Output 3

5


N = gets.chomp.to_i
# xl = []
ll = []

ans = 0

h = Hash.new(0)

N.times do |i|
  x, l = gets.chomp.split.map(&:to_i)
  h[x-l+1] = +1
  h[x+l] = -1
  ll << [x-l+1,x+l-1]
end

ll.sort_by!{|x| [x[0],x[1]] }

# p ll

tmp = -10000000001

N.times do |i|
  next if i == 0
  if ll[i][0] <= ll[i-1][1] && ll[i][0] >= tmp
    ans += 1
    tmp = ll[i-1][1]
  end
end

p N-ans


#input of int(split by space)
def get_i()
  return gets.chomp.split(" ").map(&:to_i)
end
#input of float(split by space)
def get_f()
  return gets.chomp.split(" ").map(&:to_f)
end
#input of string(split by space)
def get()
  return gets.chomp.split(" ")
end
#input of string(split per one character)
def get_nsp()
  return gets.chomp.split("")
end
#yes or no decision
def yn_judge(bool,y="Yes",n="No")
  return bool ? y : n
end
#create of array
def array(size1,init=nil,size2=-1)
  if size2==-1
    return Array.new(size1){init}
  else
    return Array.new(size2){Array.new(size1){init}}
  end
end

def max(a,b)
    return a>b ? a : b
end

def min(a,b)
    return a>b ? b : a
end

INF=Float::INFINITY

N,k,S=get_i
ans=[]
N.times do|i|
  if k>0
      ans.push(S)
  else
      if S==10**9
          ans.push(1)
      else
          ans.push(S+1)
      end
  end
  k-=1
end
puts ans.join(" ")

Score : 400 points

Problem Statement

Given are three integers N, K, and S.

Find a sequence A_1, A_2, ..., A_N of N integers between 1 and 10^9 (inclusive) that satisfies the condition below.
We can prove that, under the conditions in Constraints, such a sequence always exists.

There are exactly K pairs (l, r) of integers such that 1 \leq l \leq r \leq N and A_l + A_{l + 1} + \cdots + A_r = S.

Constraints

1 \leq N \leq 10^5

0 \leq K \leq N

1 \leq S \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K S

Output

Print a sequence satisfying the condition, in the following format:

A_1 A_2 ... A_N

Sample Input 1

4 2 3

Sample Output 1

1 2 3 4

Two pairs (l, r) = (1, 2) and (3, 3) satisfy the condition in the statement.

Sample Input 2

5 3 100

Sample Output 2

50 50 50 30 70


Score : 100 points

Problem Statement

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

Constraints

C is a lowercase English letter that is not z.

Input

Input is given from Standard Input in the following format:

C

Output

Print the letter that follows C in alphabetical order.

Sample Input 1

a

Sample Output 1

b

a is followed by b.

Sample Input 2

y

Sample Output 2

z

y is followed by z.


AC = 'AC'
WA = 'WA'
n, m = STDIN.readline.chomp.split
n = n.to_i
m = m.to_i
pn = 0
ac = false
wa = false
num_ac = 0
num_wa = 0
ans = {}
m.times {|i|
  p, j = STDIN.readline.chomp.split
  p = p.to_i
  if !ans.key?(p)
    ans[p] = false
  end

  if j == AC
    if !ans[p]
      ans[p] = true
      num_ac += 1
    end
  else
    if !ans[p]
      num_wa += 1
    end
  end
}

puts "#{num_ac} #{num_wa}"

Score : 300 points

Problem Statement

Takahashi participated in a contest on AtCoder.

The contest had N problems.

Takahashi made M submissions during the contest.

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

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

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

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

Constraints

N, M, and p_i are integers.

1 \leq N \leq 10^5

0 \leq M \leq 10^5

1 \leq p_i \leq N

S_i is AC or WA.

Input

Input is given from Standard Input in the following format:

N M
p_1 S_1
:
p_M S_M

Output

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

Sample Input 1

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

Sample Output 1

2 2

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

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

Thus, he has two correct answers and two penalties.

Sample Input 2

100000 3
7777 AC
7777 AC
7777 AC

Sample Output 2

1 0

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

Sample Input 3

6 0

Sample Output 3

0 0


H, W = gets.split.map(&:to_i)
board = H.times.map { gets.chomp.chars }

def bfs(board, si, sj)
  distance = [
    [-1, 0],
    [1, 0],
    [0, -1],
    [0, 1]
  ]
  ret = 0
  used = Array.new(H) { Array.new(W, false) }
  # pos, distance
  queue = [[si, sj, 0]]
  while !queue.empty?
    i, j, d = queue.shift
    used[i][j] = true
    ret = d if d > ret
    distance.each do |(id, jd)|
      new_i = i + id
      new_j = j + jd
      next if new_i < 0 || new_i >= H || new_j < 0 || new_j >= W
      next if used[new_i][new_j]
      next if board[new_i][new_j] == '#'
      queue.push([new_i, new_j, d + 1])
    end
  end
  ret
end

ans = 0
(0...H).each do |i|
  (0...W).each do |j|
    if board[i][j] == '.'
      ret = bfs(board, i, j)
      ans = ret if ret > ans
    end
  end
end
puts ans

Score : 400 points

Problem Statement

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

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

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

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

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

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

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

Constraints

1 \leq H,W \leq 20

S_{ij} is . or #.

S contains at least two occurrences of ..

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

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 3
...
...
...

Sample Output 1

4

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

Sample Input 2

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

Sample Output 2

10

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


MAX = 10000
MOD = 10 ** 9 + 7

def mod_power(n, p, m)
  #フェルマーの小定理
  return 1 if p == 0
  if p % 2 == 0
    t = mod_power(n, p / 2, m)
    return t * t % m
  else
    return n * mod_power(n, p - 1, m) % m
  end
end

def cominit()
  #階乗と階乗の逆元を用意する
  @fac = []
  @fac[0] = 1
  @fac[1] = 1
  @finv = []
  @finv[0] = 1
  @finv[1] = 1

  2.upto(MAX) do |i|
    @fac[i] = @fac[i-1] * i % MOD
  end

  @finv[MAX] = mod_power(@fac[MAX], MOD - 2, MOD)
  (MAX-1).downto(2) do |i|
    @finv[i] = @finv[i+1] * (i + 1) % MOD
  end
end

def comb(n, r)
  return 0 if n < r
  return 1 if n == r
  return @fac[n] * @finv[r] * @finv[n-r] % MOD
end

def main
  n, k = gets.chomp.split.map(&:to_i)
  array = gets.chomp.split().map(&:to_i).sort.reverse
  cominit()
  ans = 0
  #各Ciについてmax, minを求める
  array.each_with_index do |c, i|
    higher_count = array[i+1..n-1].select {|a| a <= c}.length
    ans += c * comb(higher_count, k - 1) % MOD
  end

  array.sort!

  array.each_with_index do |c, i|
    #ciより低いもののカウント
    lower_count = array[i+1..n-1].select {|a| a >= c}.length
    ans -= c * comb(lower_count, k - 1) % MOD
  end

  return ans % MOD
end

puts main

Score : 500 points

Problem Statement

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

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

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

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

Constraints

1 \leq N \leq 10^5

1 \leq K \leq N

|A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 ... A_N

Output

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

Sample Input 1

4 2
1 1 3 4

Sample Output 1

11

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

Sample Input 2

6 3
10 10 10 -10 -10 -10

Sample Output 2

360

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

Sample Input 3

3 1
1 1 1

Sample Output 3

0

Sample Input 4

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

Sample Output 4

999998537

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


a,b=gets.split.map(&:to_i)
if 500*a >= b
  puts 'Yes'
else
  puts 'No'
end

Score : 100 points

Problem Statement

Takahashi has K 500-yen coins. (Yen is the currency of Japan.)
If these coins add up to X yen or more, print Yes; otherwise, print No.

Constraints

1 \leq K \leq 100

1 \leq X \leq 10^5

Input

Input is given from Standard Input in the following format:

K X

Output

If the coins add up to X yen or more, print Yes; otherwise, print No.

Sample Input 1

2 900

Sample Output 1

Yes

Two 500-yen coins add up to 1000 yen, which is not less than X = 900 yen.

Sample Input 2

1 501

Sample Output 2

No

One 500-yen coin is worth 500 yen, which is less than X = 501 yen.

Sample Input 3

4 2000

Sample Output 3

Yes

Four 500-yen coins add up to 2000 yen, which is not less than X = 2000 yen.


Score : 200 points

Problem Statement

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

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

Constraints

3 \leq N \leq 50

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

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

Sample Input 1

10
ZABCDBABCQ

Sample Output 1

2

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

Sample Input 2

19
THREEONEFOURONEFIVE

Sample Output 2

0

No contiguous subsequences of S are equal to ABC.

Sample Input 3

33
ABCCABCBABCCABACBCBBABCBCBCBCABCB

Sample Output 3

5


n = gets.to_i
s = gets.chomp
words = []
count = 0

(0..n-3).each do |start|
  words.push(s[start..start+2])
end

words.each do |word|
  if word == 'ABC'
    count += 1
  end
end

puts count


X = gets.to_i
s = gets.chomp

count = 0
while n = s.index("ABC") do
  s.slice!(n + 1, 3)
  count += 1
end
puts count


a = gets.to_i
b_array = gets.strip.split.map(&:to_i)
c_array = gets.strip.split.map(&:to_i)

permutations = b_array.sort.permutation.to_a

b_index = nil
c_index = nil
permutations.each_with_index do |p, index|
  if p == b_array
    b_index = index + 1
  elsif p == c_array
    c_index = index + 1
  end
end

puts b_index - c_index

Score : 300 points

Problem Statement

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

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

Notes

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

Constraints

2 \leq N \leq 8

P and Q are permutations of size N.

Input

Input is given from Standard Input in the following format:

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

Output

Print |a - b|.

Sample Input 1

3
1 3 2
3 1 2

Sample Output 1

3

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

Sample Input 2

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

Sample Output 2

17517

Sample Input 3

3
1 2 3
1 2 3

Sample Output 3

0


ARG_N, ARG_M = gets.split.map(&:to_i)
ARG_a = gets.split.map(&:to_i)

lcm = ARG_a[0]
1.upto(ARG_N-1) do |i|
  lcm = ARG_a[i].lcm(lcm)
end
res = (ARG_M * 2 / lcm)
if res.even?
  puts res / 2
else
  puts res /2 + 1
end

Score : 400 points

Problem Statement

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

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

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

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

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^9

2 \leq a_i \leq 10^9

a_i is an even number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_1 a_2 ... a_N

Output

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

Sample Input 1

2 50
6 10

Sample Output 1

2

15 = 6 \times 2.5

15 = 10 \times 1.5

45 = 6 \times 7.5

45 = 10 \times 4.5

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

Sample Input 2

3 100
14 22 40

Sample Output 2

0

The answer can be 0.

Sample Input 3

5 1000000000
6 6 2 6 2

Sample Output 3

166666667


lines = readlines.map{|line| line.split(' ').map(&:to_i)}
a=lines[1]
b=1
for i in a
  b=b.lcm(i/2)
end
if lines[0][1]/b%2==0 then
  puts lines[0][1]/b/2
else
  if lines[0][1]/b/2>0 then
    puts lines[0][1]/b/2+1
  else
    puts 0
  end
end


def main
  n = gets.chomp.to_i
  a = gets.chomp.split.map(&:to_i)
  a[n] = a[0]
  b = gets.chomp.split.map(&:to_i)
  b[n] = b[0]

  axa = []
  bxa = []
  #a+k xor a+1+k = b xor b+1 となるkを見つける
  bXor = b[0] ^ b[1]
  k = []
  n.times do |i|
    #a[i]xor a[i+1]の配列を作る
    ax = a[i] ^ a[i+1]
    bx = b[i] ^ b[i+1]

    axa[i] = ax
    axa[i+n] = ax
    bxa[i] = bx
    bxa[i+n] = bx

    if ax == bXor
      k.push(i)
    end
  end

  #全ての要素に対してa+k xor b = Xとなるかを確認する
  k.each do |kk|
    if axa[kk...kk+n] == bxa[0...n]
      x = a[kk] ^ b[0]
      puts "#{kk} #{x}"
    end
  end

end

main

Score : 600 points

Problem Statement

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

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

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

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

What is \mbox{ XOR }?

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

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

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

Constraints

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

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

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

Sample Input 1

3
0 2 1
1 2 3

Sample Output 1

1 3

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

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

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

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

and we have a' = b.

Sample Input 2

5
0 0 0 0 0
2 2 2 2 2

Sample Output 2

0 2
1 2
2 2
3 2
4 2

Sample Input 3

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

Sample Output 3

2 2
5 5

Sample Input 4

2
1 2
0 0

Sample Output 4

No pairs may satisfy the condition.


input = gets.chomp.split
a = input[0].to_i
b = input[1].to_i
k = input[2].to_i

if k - (a + b) >= 0
    a = 0
    b = 0
elsif k - a >= 0
    b -= (k - a)
    a = 0
else
    a -= k
end

puts a.to_s + " " + b.to_s

Score : 200 points

Problem Statement

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

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

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

If they both have no cookies, do nothing.

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

Constraints

0 \leq A \leq 10^{12}

0 \leq B \leq 10^{12}

0 \leq K \leq 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

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

Sample Input 1

2 3 3

Sample Output 1

0 2

Takahashi will do the following:

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

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

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

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

Sample Input 2

500000000000 500000000000 1000000000000

Sample Output 2

0 0

Watch out for overflows.


n,a,b = gets.chomp.split.map(&:to_i)
if (b - a).even?
  ans = (b - a ) / 2
else
  ans = [b - 1, n - a].min
end
puts ans

Score : 300 points

Problem Statement

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

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

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

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

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

Constraints

2 \leq N \leq 10^{18}

1 \leq A < B \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

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

Sample Input 1

5 2 4

Sample Output 1

1

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

Sample Input 2

5 2 3

Sample Output 2

2

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


Score : 700 points

Problem Statement

N problems are proposed for an upcoming contest. Problem i has an initial integer score of A_i points.

M judges are about to vote for problems they like. Each judge will choose exactly V problems, independently from the other judges,
and increase the score of each chosen problem by 1.

After all M judges cast their vote, the problems will be sorted in non-increasing order of score, and the first P problems will be chosen for the problemset.
Problems with the same score can be ordered arbitrarily, this order is decided by the chief judge.

How many problems out of the given N have a chance to be chosen for the problemset?

Constraints

2 \le N \le 10^5

1 \le M \le 10^9

1 \le V \le N - 1

1 \le P \le N - 1

0 \le A_i \le 10^9

Input

Input is given from Standard Input in the following format:

N M V P
A_1 A_2 ... A_N

Output

Print the number of problems that have a chance to be chosen for the problemset.

Sample Input 1

6 1 2 2
2 1 1 3 0 2

Sample Output 1

5

If the only judge votes for problems 2 and 5, the scores will be 2 2 1 3 1 2.
The problemset will consist of problem 4 and one of problems 1, 2, or 6.

If the only judge votes for problems 3 and 4, the scores will be 2 1 2 4 0 2.
The problemset will consist of problem 4 and one of problems 1, 3, or 6.

Thus, problems 1, 2, 3, 4, and 6 have a chance to be chosen for the problemset. On the contrary, there is no way for problem 5 to be chosen.

Sample Input 2

6 1 5 2
2 1 1 3 0 2

Sample Output 2

3

Only problems 1, 4, and 6 have a chance to be chosen.

Sample Input 3

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

Sample Output 3

8


a = gets.to_i
b = gets.to_i
puts 6 - a - b

Score: 100 points

Problem Statement

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

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

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

Print the correct choice for this problem.

Constraints

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

A and B are different.

Input

Input is given from Standard Input in the following format:

A
B

Output

Print the correct choice.

Sample Input 1

3
1

Sample Output 1

2

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

Sample Input 2

1
2

Sample Output 2

3


A = gets.to_i
B = gets.to_i
puts [1,2,3].select{|n| n != A && n != B}


line = readlines
len = line.length
i = 0

while i < len
    line[i] = line[i].chomp.split(' ')
    i += 1
end

p line
num = line[0]
words = line[1]
newWord = ""

w1 = words[0]
strW1 = w1.split(//)
w2 = words[1]
strW2 = w2.split(//)

p w1
p w2
p strW1
p strW2

i = 0
strW1.each do |word|
  newWord += strW1[i]
  newWord += strW2[i]
  i += 1
end

p newWord

Score : 200 points

Problem Statement

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

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

Constraints

1 \leq N \leq 100

|S| = |T| = N

S and T are strings consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S T

Output

Print the string formed.

Sample Input 1

2
ip cc

Sample Output 1

icpc

Sample Input 2

8
hmhmnknk uuuuuuuu

Sample Output 2

humuhumunukunuku

Sample Input 3

5
aaaaa aaaaa

Sample Output 3

aaaaaaaaaa


n = gets.to_i
a,b = gets.chomp.split(" ")
for i in 0...n
  print "#{a[i]}#{b[i]}"
end
puts ""


n = gets.to_i
s, t = gets.chomp.split(' ')
res = ""
n.times do |i|
  res += s[i]
  res += t[i]
end
puts res


N = gets.chomp.to_i
cnt = 1
stock = 0
while 1 do
  tmp = N / (5 ** cnt)
  tmp = tmp / (2)
  stock += tmp
  cnt += 1
  break if tmp == 0
end
puts stock

Score : 500 points

Problem Statement

For an integer n not less than 0, let us define f(n) as follows:

f(n) = 1 (if n < 2)

f(n) = n f(n-2) (if n \geq 2)

Given is an integer N. Find the number of trailing zeros in the decimal notation of f(N).

Constraints

0 \leq N \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of trailing zeros in the decimal notation of f(N).

Sample Input 1

12

Sample Output 1

1

f(12) = 12 × 10 × 8 × 6 × 4 × 2 = 46080, which has one trailing zero.

Sample Input 2

5

Sample Output 2

0

f(5) = 5 × 3 × 1 = 15, which has no trailing zeros.

Sample Input 3

1000000000000000000

Sample Output 3

124999999999999995


n,u,v=gets.split.map(&:to_i)
nei=(0..n).map{[]}
(n-1).times{
  a,b=gets.split.map(&:to_i)
  nei[a] << b
  nei[b] << a
}

def dfs depth,height,parent,nei,ni,di
  depth[ni] = di
  height[ni] = nei[ni].map{|nii|
    if depth[nii]
      0
    else
      parent[nii] = ni
      1 + dfs(depth,height,parent,nei,nii,di+1)
    end
  }.max
end

depth = []
height = []
parent = []
dfs depth,height,parent,nei,v,0

#p depth,height,parent

half = u
cnt,m = (depth[u]-1).divmod(2)
cnt.times{
  half = parent[half]
}
p cnt+m+height[half]

Score : 600 points

Problem Statement

We have a tree with N vertices. The i-th edge connects Vertex A_i and B_i bidirectionally.

Takahashi is standing at Vertex u, and Aoki is standing at Vertex v.

Now, they will play a game of tag as follows:

1. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Takahashi moves to a vertex of his choice that is adjacent to his current vertex.

2. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Aoki moves to a vertex of his choice that is adjacent to his current vertex.

3. Go back to step 1.

Takahashi performs his moves so that the game ends as late as possible, while Aoki performs his moves so that the game ends as early as possible.

Find the number of moves Aoki will perform before the end of the game if both Takahashi and Aoki know each other's position and strategy.

It can be proved that the game is bound to end.

Constraints

2 \leq N \leq 10^5

1 \leq u,v \leq N

u \neq v

1 \leq A_i,B_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N u v
A_1 B_1
:
A_{N-1} B_{N-1}

Output

Print the number of moves Aoki will perform before the end of the game.

Sample Input 1

5 4 1
1 2
2 3
3 4
3 5

Sample Output 1

2

If both players play optimally, the game will progress as follows:

Takahashi moves to Vertex 3.

Aoki moves to Vertex 2.

Takahashi moves to Vertex 5.

Aoki moves to Vertex 3.

Takahashi moves to Vertex 3.

Here, Aoki performs two moves.

Note that, in each move, it is prohibited to stay at the current vertex.

Sample Input 2

5 4 5
1 2
1 3
1 4
1 5

Sample Output 2

1

Sample Input 3

2 1 2
1 2

Sample Output 3

0

Sample Input 4

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

Sample Output 4

5


sum = 0
gets.split(' ').each do |number|
  sum += number.to_i
end

if sum <= 21
  p "win"
else
  p "bust"
end

Score : 100 points

Problem Statement

Given are three integers A_1, A_2, and A_3.

If A_1+A_2+A_3 is greater than or equal to 22, print bust; otherwise, print win.

Constraints

1 \leq A_i \leq 13 \ \ (i=1,2,3)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A_1 A_2 A_3

Output

If A_1+A_2+A_3 is greater than or equal to 22, print bust; otherwise, print win.

Sample Input 1

5 7 9

Sample Output 1

win

5+7+9=21, so print win.

Sample Input 2

13 7 2

Sample Output 2

bust

13+7+2=22, so print bust.


s = gets.chomp.chars
len = s.length
p [*0..(len/2-1)].count {|i| s[i] != s[len-1-i] }

Score : 200 points

Problem Statement

Takahashi loves palindromes. Non-palindromic strings are unacceptable to him. Each time he hugs a string, he can change one of its characters to any character of his choice.

Given is a string S. Find the minimum number of hugs needed to make S palindromic.

Constraints

S is a string consisting of lowercase English letters.

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

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum number of hugs needed to make S palindromic.

Sample Input 1

redcoder

Sample Output 1

1

For example, we can change the fourth character to o and get a palindrome redooder.

Sample Input 2

vvvvvv

Sample Output 2

0

We might need no hugs at all.

Sample Input 3

abcdabc

Sample Output 3

2


require 'set'
N = gets.to_i

tuples = N.times.flat_map.with_index(1) { |_, id|
  gets.to_i.times.map {
    other, b = gets.chomp.split(' ').map(&:to_i)
    [id, other, b]
  }
}

layers = Set.new
semi_layers = {}

tuples.each do |(id, other, b)|
  if b == 1 && tuples.include?([other, id, 0])
    layers << id
  end
end

tuples.each do |(id, other, b)|
  if b == 0 && !tuples.include?([other, id, 1])
    unless layers.include?(other)
      semi_layers[id] ||= Set.new
      semi_layers[id] << other
    end

    unless layers.include?(id)
      semi_layers[other] ||= Set.new
      semi_layers[other] << id
    end
  end
end

semi_layers.sort_by { |_, v| v.size }.reverse.each do |id, others|
  next unless semi_layers.has_key?(id)

  layers << id
  semi_layers.delete(id)

  others.each do |other|
    semi_layers.delete(other)
  end
end

puts N - layers.size

Score : 300 points

Problem Statement

There are N people numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not.

Person i gives A_i testimonies. The j-th testimony by Person i is represented by two integers x_{ij} and y_{ij}. If y_{ij} = 1, the testimony says Person x_{ij} is honest; if y_{ij} = 0, it says Person x_{ij} is unkind.

How many honest persons can be among those N people at most?

Constraints

All values in input are integers.

1 \leq N \leq 15

0 \leq A_i \leq N - 1

1 \leq x_{ij} \leq N

x_{ij} \neq i

x_{ij_1} \neq x_{ij_2} (j_1 \neq j_2)

y_{ij} = 0, 1

Input

Input is given from Standard Input in the following format:

N
A_1
x_{11} y_{11}
x_{12} y_{12}
:
x_{1A_1} y_{1A_1}
A_2
x_{21} y_{21}
x_{22} y_{22}
:
x_{2A_2} y_{2A_2}
:
A_N
x_{N1} y_{N1}
x_{N2} y_{N2}
:
x_{NA_N} y_{NA_N}

Output

Print the maximum possible number of honest persons among the N people.

Sample Input 1

3
1
2 1
1
1 1
1
2 0

Sample Output 1

2

If Person 1 and Person 2 are honest and Person 3 is unkind, we have two honest persons without inconsistencies, which is the maximum possible number of honest persons.

Sample Input 2

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

Sample Output 2

0

Assuming that one or more of them are honest immediately leads to a contradiction.

Sample Input 3

2
1
2 0
1
1 0

Sample Output 3

1


input = gets
input2 = gets
arr = input.split(" ")

if arr[0] == "11" then
    if arr[1] == "30" then
        p 1
    end
else
    p 0
end

Score: 100 points

Problem Statement

In this problem, a date is written as Y-M-D. For example, 2019-11-30 means November 30, 2019.

Integers M_1, D_1, M_2, and D_2 will be given as input.

It is known that the date 2019-M_2-D_2 follows 2019-M_1-D_1.

Determine whether the date 2019-M_1-D_1 is the last day of a month.

Constraints

Both 2019-M_1-D_1 and 2019-M_2-D_2 are valid dates in the Gregorian calendar.

The date 2019-M_2-D_2 follows 2019-M_1-D_1.

Input

Input is given from Standard Input in the following format:

M_1 D_1
M_2 D_2

Output

If the date 2019-M_1-D_1 is the last day of a month, print 1; otherwise, print 0.

Sample Input 1

11 16
11 17

Sample Output 1

0

November 16 is not the last day of a month.

Sample Input 2

11 30
12 1

Sample Output 2

1

November 30 is the last day of November.


X = STDIN.gets.chomp.to_i

prices = [100,101,102,103,104,105]

combination = []

prices.length.times do |i|
    combination << prices.combination(i+1).map{ |i| i.inject(:+) }
end

if combination.flatten.select{ |i| i == X }.length == 0
    puts 0
else
    puts 1
end

Score: 300 points

Problem Statement

AtCoder Mart sells 1000000 of each of the six items below:

Riceballs, priced at 100 yen (the currency of Japan) each

Sandwiches, priced at 101 yen each

Cookies, priced at 102 yen each

Cakes, priced at 103 yen each

Candies, priced at 104 yen each

Computers, priced at 105 yen each

Takahashi wants to buy some of them that cost exactly X yen in total.
Determine whether this is possible.

(Ignore consumption tax.)

Constraints

1 \leq X \leq 100000

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

If it is possible to buy some set of items that cost exactly X yen in total, print 1; otherwise, print 0.

Sample Input 1

615

Sample Output 1

1

For example, we can buy one of each kind of item, which will cost 100+101+102+103+104+105=615 yen in total.

Sample Input 2

217

Sample Output 2

0

No set of items costs 217 yen in total.


n = gets.to_i
s =gets.chomp

n.times do
  s = s.tr('A-Z','B-ZA')
end
puts s

Score : 200 points

Problem Statement

We have a string S consisting of uppercase English letters. Additionally, an integer N will be given.

Shift each character of S by N in alphabetical order (see below), and print the resulting string.

We assume that A follows Z. For example, shifting A by 2 results in C (A \to B \to C), and shifting Y by 3 results in B (Y \to Z \to A \to B).

Constraints

0 \leq N \leq 26

1 \leq |S| \leq 10^4

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the string resulting from shifting each character of S by N in alphabetical order.

Sample Input 1

2
ABCXYZ

Sample Output 1

CDEZAB

Note that A follows Z.

Sample Input 2

0
ABCXYZ

Sample Output 2

ABCXYZ

Sample Input 3

13
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Sample Output 3

NOPQRSTUVWXYZABCDEFGHIJKLM


n = gets.chomp.to_i
s = gets.chomp.split('')
a = ''

for i in 0..(s.length - 1)
  if s[i].ord + n > 'Z'.ord
    s[i] = ('A'.ord + (s[i].ord + n) - 'Z'.ord).chr
  else
    s[i] = (s[i].ord + n).chr
  end
end

for i in 0..(s.length - 1)
  a += s[i]
end

puts a


n=gets.to_i
l=[[]]
n.times do
  l << []
end
t=[]
(n-1).times do
  a,b=gets.split.map(&:to_i)
  l[a] << b
  l[b] << a
  t << [a,b]
end
k=0
l.each do |ll|
  k=ll.size if k<ll.size
end
puts k
h={}
c=[0,0]
s=[1]
i=0
while s!=[]
  z=[]
  s.each do |ss|
    l[ss].each do |lss|
      a,b=[ss,lss].sort
      if !h.has_key?([a,b])
        i=(i%k)+1
        i=(i%k)+1 if i==c[ss]
        h[[a,b]]=i
        c[lss]=i
        z << lss
      end
    end
  end
  s=z.dup
end
t.each do |tt|
  puts h[tt]
end

Score : 400 points

Problem Statement

Given is a tree G with N vertices.
The vertices are numbered 1 through N, and the i-th edge connects Vertex a_i and Vertex b_i.

Consider painting the edges in G with some number of colors.
We want to paint them so that, for each vertex, the colors of the edges incident to that vertex are all different.

Among the colorings satisfying the condition above, construct one that uses the minimum number of colors.

Constraints

2 \le N \le 10^5

1 \le a_i \lt b_i \le N

All values in input are integers.

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}

Output

Print N lines.

The first line should contain K, the number of colors used.

The (i+1)-th line (1 \le i \le N-1) should contain c_i, the integer representing the color of the i-th edge, where 1 \le c_i \le K must hold.

If there are multiple colorings with the minimum number of colors that satisfy the condition, printing any of them will be accepted.

Sample Input 1

3
1 2
2 3

Sample Output 1

2
1
2

Sample Input 2

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

Sample Output 2

4
1
2
3
4
1
1
2

Sample Input 3

6
1 2
1 3
1 4
1 5
1 6

Sample Output 3

5
1
2
3
4
5


N,K,*A = `dd`.split.map &:to_i
A.map!{ |a| (a - 1) % K }
ring = [0]
ch = { 0 => 1 }
s = 0
n = 0
A.each_with_index{ |a,i|
  s = (s + a) % K
  n += (ch[s] || 0)
  ch[s] = (ch[s] || 0) + 1
  ring << s
  if ring.size >= K
    t = ring[-K]
    if t && ch[t]
      ch[t] = ch[t] - 1
    end
  end
}
p n

Score : 500 points

Problem Statement

Given are a sequence of N positive integers A_1, A_2, \ldots, A_N, and a positive integer K.

Find the number of non-empty contiguous subsequences in A such that the remainder when dividing the sum of its elements by K is equal to the number of its elements. We consider two subsequences different if they are taken from different positions, even if they are equal sequences.

Constraints

All values in input are integers.

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

1 \leq K \leq 10^9

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \cdots A_N

Output

Print the number of subsequences that satisfy the condition.

Sample Input 1

5 4
1 4 2 3 5

Sample Output 1

4

Four sequences satisfy the condition: (1), (4,2), (1,4,2), and (5).

Sample Input 2

8 4
4 2 4 2 4 2 4 2

Sample Output 2

7

(4,2) is counted four times, and (2,4) is counted three times.

Sample Input 3

10 7
14 15 92 65 35 89 79 32 38 46

Sample Output 3

8