# スペース区切りの整数の入力
a,b=gets.chomp.split(" ").map(&:to_i);

# 出力
if (a*b)%==0 then
  print("Even")
else
  print("Odd")


a,b =gets.chomp.split().map(&:to_i)
if (a*b)/2 == 0 then
  print("Even")
else
  print("Odd")
end

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

Score : 100 points

Problem Statement

AtCoDeer the deer found two positive integers, a and b.
Determine whether the product of a and b is even or odd.

Constraints

1 ≤ a,b ≤ 10000

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

If the product is odd, print Odd; if it is even, print Even.

Sample Input 1

3 4

Sample Output 1

Even

As 3 × 4 = 12 is even, print Even.

Sample Input 2

1 21

Sample Output 2

Odd

As 1 × 21 = 21 is odd, print Odd.

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.


a, b = gets.split.map(&:to_i)
answer_number = a*b
if answer_number.even?
  p 'Even'
elsif answer_number.odd?
  p 'Odd'
end


a, b = gets.split.map{|i| i.to_i}
if a.odd? && b.odd? then
    puts 'Odd'
else
    puts 'Even'
end


puts gets.to_i*gets.to_i%2==1? :Odd: :Even


a, b = gets.chomp.split(" ").map(&:to_i)
total = a * b
if total.even?
  puts 'Even'
else
  puts 'Odd'
end


a, b = gets.strip.split.map(&:to_i)
if a * b % 2 == 0
    puts 'Even'
else
    puts 'Odd'
end


N, Y = gets.split.map(&:to_i)
2000.times do |i|
    2000.times do |j|
        (N - i - j + 1).times do |k|
            if (1000 * i + 5000 * j + 10000 * k == Y)
                puts [i, j, k].join(" ")
                exit
            end
        end
    end
end
puts [-1, -1, -1].join(" ")

Score : 300 points

Problem Statement

The commonly used bills in Japan are 10000-yen, 5000-yen and 1000-yen bills. Below, the word "bill" refers to only these.

According to Aohashi, he received an otoshidama (New Year money gift) envelope from his grandfather that contained N bills for a total of Y yen, but he may be lying. Determine whether such a situation is possible, and if it is, find a possible set of bills contained in the envelope. Assume that his grandfather is rich enough, and the envelope was large enough.

Constraints

1 ≤ N ≤ 2000

1000 ≤ Y ≤ 2 × 10^7

N is an integer.

Y is a multiple of 1000.

Input

Input is given from Standard Input in the following format:

N Y

Output

If the total value of N bills cannot be Y yen, print -1 -1 -1.

If the total value of N bills can be Y yen, let one such set of bills be "x 10000-yen bills, y 5000-yen bills and z 1000-yen bills", and print x, y, z with spaces in between. If there are multiple possibilities, any of them may be printed.

Sample Input 1

9 45000

Sample Output 1

4 0 5

If the envelope contained 4 10000-yen bills and 5 1000-yen bills, he had 9 bills and 45000 yen in total. It is also possible that the envelope contained 9 5000-yen bills, so the output 0 9 0 is also correct.

Sample Input 2

20 196000

Sample Output 2

-1 -1 -1

When the envelope contained 20 bills in total, the total value would be 200000 yen if all the bills were 10000-yen bills, and would be at most 195000 yen otherwise, so it would never be 196000 yen.

Sample Input 3

1000 1234000

Sample Output 3

14 27 959

There are also many other possibilities.

Sample Input 4

2000 20000000

Sample Output 4

2000 0 0


# 10000 円札と、5000 円札と、1000 円札が合計で n 枚あって、
# 合計金額が y 円であったという。
# このような条件を満たす各金額の札の枚数の組を 1 つ求めなさい。
# そのような状況が存在し得ない場合には -1 -1 -1 と出力しなさい。

input = gets.chomp.split

# 札の枚数を取得
n = input[0].to_i

# 合計金額を取得
y = input[1].to_i

# 組み合わせを返す関数を定義
def judge(n, y)
  n.times do |i|
    j = 0
    while n - (i + j) >= 0
      # 合計金額を計算
      sum = 1000 * i + 5000 * j + 10000 * (n - (i + j))

      # 合計金額が条件を満たすか判定
      if sum == y
        return "#{n - (i + j)} #{j} #{i}"
      end

      j += 1
    end
  end

  # 組み合わせが無かった場合
  return "-1 -1 -1"
end

# 結果を出力
puts judge(n, y)


n, m = gets.split.map(&:to_i)
result = "-1 -1 -1"
(0..n).each do |x|
  (0..n).each do |y|
    z = n - x + y
    if (10000 * x) + (5000 * y) + (1000 * z) == m && z >= 0
      result = "#{x} #{y} #{z}"
      break
    end
  end
end
puts result


def calc_bill(max, goal)
  0.upto(max) do |man|
    0.upto(max - man) do |gosen|
      sen = max - (man + gosen)
      if (man * 10000) + (gosen * 5000) + (sen * 1000) == goal
        return "#{man} #{gosen} #{sen}"
      end
    end
  end
  "-1 -1 -1"
end

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


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

0.upto(n) do |i|
  0.upto(n-i) do |j|
    0.upto(n-i-j) do |k|
      money = i * 1000 + j * 5000 + k * 10000
      if money == y
        puts [k, j, i].join(' ')
        exit
      end
    end
  end
end

puts '-1 -1 -1'


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

count = 0
catch(:break_loop) do
  (0..N).each {|ten|
    (0..N-ten).each{|five|
      if 10000*ten + 5000*five + 1000*(N-ten-five) == Y
        puts "#{ten} #{five} #{N-ten-five}"
        count += 1
        throw :break_loop
      end
    }
  }
end

if count == 0
  puts "-1 -1 -1"
end


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

yen_10000 = 0
yen_5000 = 0
yen_1000 = 0

fin = false

n.downto(0) do |m|
  if m * 10000 <= amount
    (n-m).downto(0) do |l|
      if (m * 10000 + l * 5000) <= amount
        (n-m-l).downto(0) do |t|
          if (m * 10000 + l * 5000 + t * 1000) == amount
            puts "#{m} #{l} #{t}"
            fin = true
            break
          end
        end
        break if fin
      end
    end
    break if fin
  end
end
puts '-1 -1 -1' unless fin


N,Y = gets.chomp.split(" ").map{|v|v.to_i}
a = -1
b =-1
c = -1

(0..N).each do |a_i|
  (N..a_i).each do |b_i|
    c_i = N-a_i-b_i
    if 10000*c_i+5000*b_i+1000*a_i == Y
      a = a_i
      b = b_i
      c = c_i
      break
    end
  end
end

puts "#{a} #{b} #{c}"


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

set = '-1 -1 -1'

(0..n).each do |a|
  (0..n).each do |b|
    (0..n).each do |c|
      set = "#{a} #{b} #{c}" if 10000*a + 5000*b + 1000*c == y
    end
  end
end

puts set


n, y = gets.split(" ").map(&:to_i)
results = [-1, -1, -1]
# 大域脱出
catch(:break_loop) do
  break if 1000 * n > y
  break if 10000 * n < y
  (0..n).each do |t10|
    next if 5000 * (n - t10) + 10000 * t10 < y
    (0..(n - t10)).each do |t5|

      sabun = y - (10000 * t10 + 5000 * t5)
      t1, r = sabun.divmod(1000)
      next unless r.zero?

      if t10 + t5 + t1 == n
        results = [t10, t5, t1]
        throw :break_loop
      end
    end
  end
end
print results.join(' ')


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

(0..n).each do |x|
  if sum == 10_000 * x
    puts "#{x} 0 0"
    exit
  end

  (0..(n - x)).each do |y|
    z = n - x - y
    next if z < 0 || y < 0 || x < 0

    if sum == 10_000 * x + 5_000 * y + 1_000 * z
      puts "#{x} #{y} #{z}"
      exit
    end
  end
end

puts '-1 -1 -1'


n, y = gets.chomp.split(' ').map(&:to_i)
0.upto(n) do |i|
  0.upto(n) do |j|
    x = n - (i + j)
    if x >= 0 && y == i * 1000 + j * 5000 + x * 10000
      puts "#{x} #{j} #{i}"
      return
    end
  end
end
puts "-1 -1 -1"


n, sum = STDIN.gets.split(' ').map(&:to_i)

Otoshidama = Struct.new('Otoshidama', :x, :y, :z)

otoshidama = catch(:break_loop) do
  0.upto(n) do |x|
    0.upto(n) do |y|
      z = n - x - y
      next if z < 0

      if 10000*x + 5000*y + 1000*z == sum
        throw :break_loop, Otoshidama.new(x, y, z)
      end
    end
  end
end

if otoshidama.kind_of?(Otoshidama)
  puts "#{otoshidama.x} #{otoshidama.y} #{otoshidama.z}"
else
  puts "-1 -1 -1"
end


N, Y = gets.chomp.split(" ").map(&:to_i)
flag = 0

for i in 0..N do
  for j in 0..(N - i) do
    if 10000 * i + 5000 * j + 1000 * (N - i - j) == Y
      puts "#{i} #{j} #{N - i - j}"
      flag = 1
      exit
    end
  end
end

if flag == 0
  puts "-1 -1 -1"
end


#!/usr/bin/env ruby
n, y = STDIN.readline().split(' ').map(&:to_i)
MONEY = [10_000, 5_000, 1_000]
def sum(array)
  array.inject(0){|sum, i| sum += i}
end
def make_combination(n, kinds)
  if kinds == 1
    [[n]]
  else
    (0..n).map{|i| make_combination(n-i, kinds-1).map{|a| [i] + a }}.flatten(1)
  end
end
def calc(c)
  sum(MONEY.zip(c).map{|m, n| m * n})
end
# check = make_combination(n, 3).map{|c| [c, calc(c)]}.find{|c, sum| sum == y}
# r = (check && check[0]) || [-1, -1, -1]
# puts "#{r.join(' ')}"
check = make_combination(n, 3).find{|c| calc(c) == y}
r = (check && check[0]) || [-1, -1, -1]
puts "#{r.join(' ')}"


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

0.upto n do |i|
  0.upto n-i do |j|
    k=n-i-j
    next if k<0
    if 10000*i+5000*j+1000*k==y
      puts "#{i} #{j} #{k}"
      exit 0
    end
  end
end

puts "-1 -1 -1"


n, y = gets.strip.split.map(&:to_i)
maisuu = y/1000
sabun = (maisuu - n)

a = sabun/13
b = (sabun%13)/9
c = (sabun%13)/4

if sabun >= 0
  if sabun%13 == 0
    puts sprintf('%d %d %d', a, a, maisuu - a*15)
  elsif (sabun%13)%9 == 0
    puts sprintf('%d %d %d', a + b, a, maisuu - a*15 - b*10)
  elsif (sabun%13)%4 == 0
    puts sprintf('%d %d %d', a, a + c, maisuu - a*15 - c*5)
  elsif sabun%9 == 0
    puts sprintf('%d %d %d', sabun/9, 0, maisuu - 10*(sabun/9))
  elsif sabun%4 == 0
    puts sprintf('%d %d %d', 0, sabun/4, maisuu - 5*(sabun/4))
  else
    puts sprintf('%d %d %d', -1, -1, -1)
  end
else
  puts sprintf('%d %d %d', -1, -1, -1)
end


# from https://twitter.com/wakuwinmail_C/status/950277218411855872

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

x = Y / 10000
y = Y % 10000 / 5000
z = Y % 5000 / 1000

temp=N-(x+y+z)

if temp < 0
  x=y=z=-1
end

if x >= temp % 4
  x -= temp % 4
  y += temp % 4 * 2
  temp -= temp % 4
else
  x=y=z=-1
end

if x >= temp
  x -= temp
  y += temp * 2
  temp = 0
else
  temp -= x / 4 * 4
  y += (x - x % 4) * 2
  x %= 4
end

if y >= temp / 4
  y -= temp / 4
  z += temp / 4 * 5
  temp = 0
else
  x=y=z=-1
end

puts "#{x} #{y} #{z}"


# from https://twitter.com/wakuwinmail_C/status/950277218411855872

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

x = Y / 10000
y = Y % 10000 / 5000
z = Y % 5000 / 1000

temp=N-(x+y+z)

if temp < 0 then
    x=-1
    y=-1
    z=-1
end
while temp>0
        if temp >= 4 and y > 0
                y-=1
                z+=5
                temp-=4
        elsif x > 0
            x-=1
            y+=2
            temp-=1
        else
            x=-1
            y=-1
            z=-1
            break
        end
end
puts "#{x} #{y} #{z}"


n,y = gets.split(' ').map(&:to_i)
y = y/1000
ans = '-1 -1 -1'

for c in 0..y
key = 10 * n - y - 9 * c
	if key % 5 == 0
		b = key / 5
		a = n - b - c
		if b >= 0 && a >= 0
			ans = a.to_s + ' ' + b.to_s + ' ' + c.to_s
			break
		end
	end
end
puts ans


n,y = gets.split.map &:to_i
ans = [-1, -1, -1]
for i in 0..n
  for j in 0..n-i
    k = n-i-j
    sum = i*10000 + j*5000 + k*1000
    ans = [i, j, k] if y == sum
  end
end

puts ans*' '


n, h = gets.chomp.split.map(&:to_i)
b = []
a_max = 0
n.times do
  ai, bi = gets.chomp.split.map(&:to_i)
  a_max = [a_max, ai].max
  b << bi
end

cnt = 0
b.sort.reverse.each do |bi|
  if bi >= a_max
    h -= bi
    cnt += 1
    if h <= 0
      puts cnt
      exit
    end
  else
    break
  end
end
s, t = h.divmod(a_max)
s += 1 if t > 0
puts cnt + s

Score : 400 points

Problem Statement

You are going out for a walk, when you suddenly encounter a monster. Fortunately, you have N katana (swords), Katana 1, Katana 2, …, Katana N, and can perform the following two kinds of attacks in any order:

Wield one of the katana you have. When you wield Katana i (1 ≤ i ≤ N), the monster receives a_i points of damage. The same katana can be wielded any number of times.

Throw one of the katana you have. When you throw Katana i (1 ≤ i ≤ N) at the monster, it receives b_i points of damage, and you lose the katana. That is, you can no longer wield or throw that katana.

The monster will vanish when the total damage it has received is H points or more. At least how many attacks do you need in order to vanish it in total?

Constraints

1 ≤ N ≤ 10^5

1 ≤ H ≤ 10^9

1 ≤ a_i ≤ b_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N H
a_1 b_1
:
a_N b_N

Output

Print the minimum total number of attacks required to vanish the monster.

Sample Input 1

1 10
3 5

Sample Output 1

3

You have one katana. Wielding it deals 3 points of damage, and throwing it deals 5 points of damage. By wielding it twice and then throwing it, you will deal 3 + 3 + 5 = 11 points of damage in a total of three attacks, vanishing the monster.

Sample Input 2

2 10
3 5
2 6

Sample Output 2

2

In addition to the katana above, you also have another katana. Wielding it deals 2 points of damage, and throwing it deals 6 points of damage. By throwing both katana, you will deal 5 + 6 = 11 points of damage in two attacks, vanishing the monster.

Sample Input 3

4 1000000000
1 1
1 10000000
1 30000000
1 99999999

Sample Output 3

860000004

Sample Input 4

5 500
35 44
28 83
46 62
31 79
40 43

Sample Output 4

9


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

ab = []
n.times {ab << gets.split.map(&:to_i)}
ab.sort!{|x,y| -x[0] <=> -y[0]}

b = []
s = 0
loop do
    s,t = ab.shift
    b << t
    break if (ab == [] || ab[0][0] != s)
end
b.sort!.reverse

ab.sort!{|x,y| -x[1] <=> -y[1]}
sum = 0
ans = 0
while sum < h
    ans += 1

    if ab == [] || ab[0][1] < s
        if b.size == 1 && sum + b[0] < h
            k = ((h - b[0] - sum).to_f / s).ceil
            sum += s * k
            ans += k - 1
        else
            sum += b.shift
        end
    else
        sum += ab[0][1]
        ab.shift
    end

end

puts ans


n, h = gets.strip.split.map(&:to_i)
a = readlines.map {|line| line.strip.split.map(&:to_i) }.sort

all_a = a.map(&:first).sort
all_b = a.map(&:last).sort

usual = all_a.last

result = 0

all_b.select {|e| e>=usual }.sort.reverse.each do |thr|
  h -= thr
  result += 1

  if h<=0
    p result
    exit
  end
end

result += 1 if h%usual > 0

result += (h/usual)

p result


n, h = gets.split.map(&:to_i)
ks = n.times.map { gets.split.map(&:to_i) }

max_slash = ks.map { |k| k[0] }.max
ks = ks.select { |k| k[1] >= max_slash }
throws = ks.map { |k| k[1] }.sort.reverse

def throw_times(throws, h)
  d = 0
  i = 0
  until d >= h
    d += throws[i]
    i += 1
  end
  i
end

if throws.reduce(&:+) >= h
  puts throw_times(throws, h)
else
  slash_damage = (h - throws.reduce(&:+))
  puts (slash_damage.to_f / max_slash).ceil + throws.size
end


require 'pp'

n, h = ARGF.gets.split(" ").map(&:to_i)

ai = []
bi = []
for _ in 0...n
  a, b = ARGF.gets.split(" ").map(&:to_i)
  ai << a
  bi << b
end

amax = ai.max

blarge = bi.select{|x| x>=amax}
bcount = blarge.size
bsum = blarge.reduce(:+).to_i

h2 = h - bsum

acount = h2/amax
acount+=1 if h2 % amax != 0

count = acount + bcount
puts count


def s()
  gets()
end
def i()
  gets.to_i
end
def li()
  gets.split.map(&:to_i)
end
def ili()
  n = gets.to_i
  as = gets.split.map(&:to_i)
  [n, as]
end

n, h = li()
as = []
max = 0
n.times{
  temp =  li()
  max = temp[0] > max ? temp[0] : max
  as << temp
}
as.sort_by!{|i| -i[1]}

strong_list = as.map{|i| i[1]}.select{|i| i > max}
sum = 0
strong_list.each{|s|
  sum += s
}

if h <= sum
  strong_list.length.times{|i|
    h -= strong_list[i]
    if (h <= 0)
      puts i+1
    end
  }
else
  puts strong_list.length + ((h-sum).to_f/max).ceil
end


n, h = gets.split.map(&:to_i)
a = []
b = []
n.times do |i|
  a[i], b[i] = gets.split.map(&:to_i)
end
a_max = a.max
a_max_list = []
a.each_with_index do |i, index|
  a_max_list << index if i == a_max
end
b_min = 1e10
ace_index = 0
a_max_list.each do |index|
  if b[index] < b_min
    ace_index = index
    b_min = b[index]
  end
end
todome = b.delete_at(ace_index)
count = 0
(b = b.sort.reverse).each do |i|
  break if i <= a_max
  h -= i
  count += 1
  if h <= 0
    exit
  end
end
h -= todome
count += 1
if h <= 0
  p count
  exit
else
  if h % a[ace_index] == 0
    count += h / a[ace_index]
  else
    count += h / a[ace_index] + 1
  end
end
p count


N,H=gets.split.map &:to_i
a = Array.new(N+1)
b = Array.new(N+1)
a[0]=0
b[0]=0
for i in 1..N
  a[i],b[i]=gets.split.map &:to_i
end
a=a.sort .reverse
b=b.sort.reverse
Sum=0
k=0
loop{
  if b[k]>a[0]
    Sum+=b[k]
    if Sum>=H
      print(k+1)
      exit
    end
  else
    break
  end
k+=1
}
print(k+((H-Sum)/a[0].to_f).ceil)


n, m = gets.chomp.split(" ").map(&:to_i)
a = 0
b = []
n.times{
  x, y = gets.chomp.split(" ").map(&:to_i)
  if (x > a) then
    a = x
  end
  b.push(y)
}

b2 = b.select{|e| e > a}
b2.sort!.reverse!

d = 0
count = 0
while m > d
  d += b2[0]
  b2.slice!(0)
  count += 1
  if b2.length == 0
    break
  end
end

if m <= d
  puts count
else
  if (m-d)%a == 0 then
    puts count + (m-d)/a
  else
    puts count + (m-d)/a + 1
  end
end


n, m = gets.chomp.split(" ").map(&:to_i)
a = 0
b = []
n.times{
  x, y = gets.chomp.split(" ").map(&:to_i)
  if (x > a) then
    a = x
  end
  b.push(y)
}

b2 = b.select{|e| e > a}
b2.sort!.reverse!

d = 0
count = 0
while m > d || b2.length > 0
  d += b2[0]
  b2.slice!(0)
  count += 1
  if b2.length == 0
    break
  end
end

if m <= d
  puts count
else
  if (m-d)%a == 0 then
    puts count + (m-d)/a
  else
    puts count + (m-d)/a + 1
  end
end


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

ab = []
n.times do |i|
  raw_ab = gets.split.map(&:to_i)
  ab << {:i => i, :t => :a, :v => raw_ab[0]}
  ab << {:i => i, :t => :b, :v => raw_ab[1]}
end
ab.sort!{|a, b| b[:v] <=> a[:v]}

ab_idx = {}
ab.each.with_index do |v, i|
  ab_idx[v[:i]] ||= {}
  ab_idx[v[:i]][v[:t]] = i
end

cur_h = h
cur_t = 0
while cur_h > 0
  cur_c = ab[0]
  if cur_c[:t] == :a
    cur_t = cur_t + (cur_h.to_f / cur_c[:v].to_f).ceil
    break
  else
    cur_h = cur_h - cur_c[:v]
    ab.delete_at(ab_idx[cur_c[:i]][:a])
    ab.delete_at(ab_idx[cur_c[:i]][:b])
    cur_t = cur_t + 1
  end
end
puts cur_t


n, h = gets.strip.split.map(&:to_i)
as = []
bs = []

n.times do
    a, b = gets.strip.split.map(&:to_i)
    as << a
    bs << b
end

# 最強の振るいの攻撃力
max_furui = as.max

# 投げの合計攻撃力
total_throw = bs.sum

# 振るう回数で二分探索
right = h / max_furui + 1
left = 0
while true do
    if right == left + 1
        break
    end
    puts "(left, right) = (#{left}, #{right})"
    # right 回で倒せるか？
    # 残りのボスのHP
    rest = h - max_furui * right
    # これを投げで削るから、
    mid = (right + left) / 2
    if rest <= total_throw
        # 倒せる
        right = mid - 1
    else
        # 倒せない
        left = mid + 1
    end
end

# ここで right に最小振るい回数が入っている
# 何回投げるか？
rest = h - max_furui * left
# これを投げで削っていく
# 投げの弱い順にソート
sorted_bs = bs.sort

count = 0
while rest > 0 do
    count += 1
    rest -= sorted_bs.pop
end

# 最小攻撃回数が求まった
puts right + count


# frozen_string_literal: true

def show(x)
  puts x
end

def solve(n,h,bs,max_a)
  c = h-bs.reduce(:+).to_f
  d = (c / max_a).ceil
  bs.size + d
end

def read
  n, h = gets.split.map(&:to_i)
  xs = []
  n.times do
    xs << gets.split.map(&:to_i)
  end
  as, bs = xs.transpose
  max_a = as.max
  [n,h,bs.select {|i| i >= max_a },max_a]
end

show solve *read


n,h = gets.split.map(&:to_i)
a = []
b = []
n.times do
  ai,bi = gets.split.map(&:to_i)
  a << ai
  b << bi
end
a.sort!
b.sort!
max_a = a[-1]
#puts max_a
k = -1
ans = 0
while k >= -n && b[k] > max_a && h > 0
  h -= b[k]
  k -= 1
  ans += 1
end
if h > 0
  ans += (h/max_a.to_f).ceil
end
puts ans


n, a, b = gets.split.map &:to_i
sum = 0
for i in 1..n
  x = i.to_s.chars.map(&:to_i).sum
  if a <= x && x <= b
    sum += i
  end
end
p sum

Score : 200 points

Problem Statement

Find the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

Constraints

1 \leq N \leq 10^4

1 \leq A \leq B \leq 36

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

Sample Input 1

20 2 5

Sample Output 1

84

Among the integers not greater than 20, the ones whose sums of digits are between 2 and 5, are: 2,3,4,5,11,12,13,14 and 20. We should print the sum of these, 84.

Sample Input 2

10 1 2

Sample Output 2

13

Sample Input 3

100 4 16

Sample Output 3

4554


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

# 各桁の和を求める
def digit_sum(n)
    sum = 0
    while n > 0 do
        sum += n % 10
        n /= 10
    end
    sum
end

answer = (1..n)
        .map { |i| [i, digit_sum(i)] }
        .select { |d| a <= d[1] && d[1] <= b }
        .inject(0) { |sum, d| sum + d[0] }

puts answer


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

p 1.upto(n).select { |i|
  r = i.to_s.chars.map(&:to_i).inject(:+)
  a <= r && r <= b
}.inject(:+)


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

results = (1..n).to_a.map do |num|
  wa = num.to_s.split('').map(&:to_i).reduce(&:+)
  if a <= wa && wa <= b
    num
  else
    0
  end
end

puts results.reduce(&:+)


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

(1..n).each do |i|
  temp = i.to_s.split.map(&:to_i).inject(:+)
  sum += i if temp >= a && temp <= b
end

puts sum


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

(1..n).each do |i|
  j = i
  sum = 0
  while j > 0
    sum += j % 10
    j = j / 10
  end
    result += i if sum.between?(a, b)
end

puts result


def sum(n)
    a = 0
    a += n / 10000
    n %= 10000
    a += n / 1000
    n %= 1000
    a += n / 100
    n %= 100
    a += n / 10
    n %= 10
    a += n
    return a
end

num = gets.chomp.split(" ").map(&:to_i)
ans = 0
for i in 1..num[0] do
    sumsum = sum(i)
    if (num[1] <= sumsum) && (sumsum <= num[2])
        ans += i
    end
end
puts ans


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

def digit(n)
  sum = 0
  while n > 0 do
    sum += n % 10
    n /= 10
  end
end

ans = (1..n).map { |i| [i, digit(i)] }.select { |d| a <= d[1] && d[1] <= b }.inject(0) { |sum, d| sum + d[0] }

puts ans


sum = 0
n, a, b = gets.split.map(&:to_i)
(1..n).each do |i|
  tmp  = i.to_s.split("").map(&:to_i).inject(&:+)
  sum += i if tmp.between?(a, b)
end
puts sum


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

res = 0
val = 0
cal = 0
dig = Math.log10(N).to_i

for n in 1..N
  data = n
  # 各桁の値を取得
  (0..dig).reverse_each do |i|
    cal = data / 10**i
    val += cal
    data = n % 10**i
  end

  if val >= A && B >= val then
    res += n
  end
  val = 0
end
puts res


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

array = (1..n).to_a.map!(&:to_s)
result = []
array.map do |x|
  y = x.chars.map(&:to_i).sum
  result << x.to_i if(a..b) === y
end
puts result.inject {|sum, n| sum + n }


# frozen_string_literal: true

# ABC 083 B - Some Sums

input_n, input_a, input_b = STDIN.gets.split.map(&:to_i)
result = 0

(0..input_n).each do |n|
  n_spl = n.to_s.split('').map(&:to_i)
  n_sum = n_spl.inject(:+)
  result += n if input_a <= n_sum && n_sum <= input_b
end

puts result


n, a, b = STDIN.readline.chomp.split(' ').map(&:to_i)
puts (1..n).inject(0) { |sum, i|
  c = i.to_s.split('').map(&:to_i).inject(&:+)
  sum += (c.between?(a, b)) ? i : 0
}


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

# 各桁の和を求める
def digit_sum n
  sum = 0
  while n > 0 do
    sum += n % 10
    n /= 10
  end
  sum
end

sum =0
for i in 1..n
  if digit_sum(i) >= a && digit_sum(i) <= b
    sum += i
  end
end

puts "#{sum}"


n,a,b = gets.split.map(&:to_i)
answer = 0
(1..n).each do |i|
  sum = i.to_s.chars.map(&:to_i).inject(+:)
  answer += i if a <= sum && sum <= b
end
puts answer


N = gets.to_i
A = gets.to_i
B = gets.to_i
sum = 0
N.downto(0) do |num|
  original_num = num
  digit_sum = 0
  while num >= 1
    digit_sum += num % 10
    num /= 10
  end
  if digit_sum >= A && digit_sum <= B
    sum += original_num
  end
end
puts sum


N = gets.to_i
A = gets.to_i
B = gets.to_i
sum = 0
N.downto(0) do |num|
  digit_sum = 0
  while num >= 1
    digit_sum += num % 10
    num /= 10
  end
  if digit_sum >= A && digit_sum <= B
    sum += num
  end
end
puts sum


nums = gets.split().map(&:to_i)
n = nums[0]
a = nums[1]
b = nums[2]

def sum_each_digits(n)
    sum = 0
    while n > 0 do
        sum += n % 10
        n /= 10
    end
    return sum
end

total = 0

(1..n).each do |i|
  sum = sum_each_digits(i)
  if sum >= a && sum <= b
      total += i
  end
end

p total


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

nums = (0..N).to_a
sums = 0

nums.each do |n|
  one = n%10
  ten = (n/10)%10
  hundred = (n/100)%10
  thousand = (n/1000)%10
  t_thousand = (n/10000)%10
  sum = one + ten + hundred + thousand + t_thousand
  sums += n if A <= sum && sum <= B
end

puts sums


# frozen_string_literal: true
n, a, b = gets.split.map(&:to_i)
sum = 0
1.upto(n) do |i|
  digitsum = i.to_s.split('').map { |e| e.to_i }.reduce(:+)
  sum += i if digitsum >= a && digitsum <= b
end
puts sum


acc = []
(0..inputs[0]).each do |num|
  cur = num.to_s.split('').map(&:to_i)
  cur[1] ||= 0
  result = cur[0] + cur[1]
  acc.push(num) if inputs[1] <= result && result <= inputs[2]
end

puts acc.sum


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

N = input[0]
A = input[1]
B = input[2]

j = []
(N+1).times{|item|
  add_result = item.to_s.split("").map(&:to_i).inject{|sum, n| sum + n}
  if (add_result >= A) && (add_result <= B)
    j << item
  end
}

puts(j.inject{|sum, n| sum + n})


n, a, b = gets.split.map{|i| i.to_i}
ans = 0
for i in 1..n do
    t = i.to_s.chars
    temp = 0
    t.each do |p|
        temp+=p.to_i
    end
    if a<=temp && temp<=b then
        ans+=i
    end
end
puts ans


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

p (1..n).reduce(0) do |s,e|
  x=e.to_s.chars.map(&:to_i).reduce(:+)
  (a..b).include?(x) ? s : s+x
end


n,a,b = gets.split.map &:to_i
ans = 0
for i in 1..n
  d = 0
  tmp = i
  while tmp>0
    d += tmp%10
    tmp /= 10
  end
  ans += i if a<=d && d<=b
end
p ans


# frozen_string_literal: true

def show(x)
  puts x
end

def solve(xs)
  n, a, b = xs
  range = (a..b)
  (1..n).reduce(0) do |a, i|
    s = i.to_s.chars.map(&:to_i).reduce(:+)
    a + (range.include?(s) ? i : 0)
  end
end

show solve gets.split.map(&:to_i)


n, a, b = STDIN.gets.chomp.split.map{|s| s.to_i }

total = 0
(1..n).each do |i|
  str_i   = i.to_s
  i_10000 = str_i[-5].to_i
  i_1000  = str_i[-4].to_i
  i_100   = str_i[-3].to_i
  i_10    = str_i[-2].to_i
  i_1     = str_i[-1].to_i
  sum = i_10000 + i_1000 + i_100 + i_10 + i_1
  if (a <= sum) && (sum <= b)
    total += i
  end
end

puts total


_n = gets
as = gets.strip.split.map(&:to_i)
cnt = 0
while as.all?(&:even?) do
    cnt = cnt.succ
    as = as.map{ |a| a / 2 }
end

puts cnt

Score : 100 points

Problem Statement

Snuke has a grid consisting of three squares numbered 1, 2 and 3.
In each square, either 0 or 1 is written. The number written in Square i is s_i.

Snuke will place a marble on each square that says 1.
Find the number of squares on which Snuke will place a marble.

Constraints

Each of s_1, s_2 and s_3 is either 1 or 0.

Input

Input is given from Standard Input in the following format:

s_{1}s_{2}s_{3}

Output

Print the answer.

Sample Input 1

101

Sample Output 1

2

A marble will be placed on Square 1 and 3.

Sample Input 2

000

Sample Output 2

0

No marble will be placed on any square.


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

puts a + b + c


n = gets.chars.map(&:to_i)

puts n.select{ |num| num == 1 }.count


s = gets.strip.split("").map(&:to_i)

cnt = 0

s.each do |i|
  cnt += 1 if i == 1
end

p cnt


nums = gets.strip.split('').map(&:to_i)
puts nums.inject(:+)


ary = gets.chars.map(&:to_i)
p ary.select { |i| i == 1 }.size


# Problem https://atcoder.jp/contests/abc081/tasks/abc081_a
# Ruby 1st Try

one,two,three = gets.chomp('\n').split('')
answer = 0
if one == '1'
    answer = answer + 1
end
if two == '1'
    answer = answer + 1
end
if three == '1'
    answer = answer + 1
end
puts(answer)


s = gets.chomp
s = s.gsub("0","")
puts s.length


a, b, c = gets.split('').map(&:to_i)
puts a+b+c


S = gets.chomp

c = 0
S.chars.each do |char|
  c += 1 if char == '1'
end
p c


s = gets.split("")
count = 0

count += 1 if (s[0] == '1')
count += 1 if (s[1] == '1')
count += 1 if (s[2] == '1')
puts count


S = get.chomp
array = S.split("")
count = 0
X = 0

array.each do |number|
  count += 1 if array[:X] == 1
  X += 1
end
puts count


n,k=gets.split.map(&:to_i)
arr = Array.new(n,0)
arr2 = gets.split.map(&:to_i)
n.times do |i|
  arr[arr2[i]-1]+=1
end
arr.sort.reverse!
ans = arr[0..k-1].inject(:+)
puts n - ans


s = gets.strip.split(' ').map(&:to_i)

print s.inject(:+)


s = gets.sstripsplit.map(&:to_i)

print s.sum


res = 0
gets.chomp.split("").map {|s| res += 1 if s == "1"}
print res


s = gets.chomp.split('')
puts s.count{|si| si == '1'}


require 'prime'
require 'set'
require 'tsort'
include Math
ALP = ('a'..'z').to_a
INF = 0xffffffffffffffff
def max(a,b);              a > b ? a : b                              end
def min(a,b);              a < b ?  a : b                             end
def swap(a,b);             a, b = b, a                                end
def gif;                   gets.to_i                                  end
def gff;                   gets.to_f                                  end
def gsf;                   gets.chomp                                 end
def gi;                    gets.split.map(&:to_i)                     end
def gf;                    gets.split.map(&:to_f)                     end
def gs;                    gets.chomp.split.map(&:to_s)               end
def gc;                    gets.chomp.split('')                       end
def pr(num);               num.prime_division                         end
def pr?(num);              Prime.prime?(num)                          end
def digit(num);            num.to_s.length                            end
def array(s,ini=nil);      Array.new(s){ini}                          end
def darray(s1,s2,ini=nil); Array.new(s1){Array.new(s2){ini}}          end
def rep(num);              num.times{|i|yield(i)}                     end
def repl(st,en,n=1);       st.step(en,n){|i|yield(i)}                 end

nums = gc

puts eval nums.join('+')


s = gets.strip.split('')
t = gets.strip.split('')

target_indice = []
(s.length-t.length+1).times do |index|
  target_indice.push(index) if s.slice(index, t.length).zip(t).all? {|c_s, c_t| c_s==c_t || c_s=='?' }
end

if target_indice.empty?
  puts 'UNRESTORABLE'
  exit
end

result = target_indice.map {|i|
  tmp = s.dup
  tmp[i..i+t.length-1]=t
  tmp.map {|c| c=='?' ? 'a' : c }.join('')
}.sort.first
puts result

Score : 300 points

Problem Statement

E869120 found a chest which is likely to contain treasure.

However, the chest is locked. In order to open it, he needs to enter a string S consisting of lowercase English letters.

He also found a string S', which turns out to be the string S with some of its letters (possibly all or none) replaced with ?.

One more thing he found is a sheet of paper with the following facts written on it:

Condition 1: The string S contains a string T as a contiguous substring.

Condition 2: S is the lexicographically smallest string among the ones that satisfy Condition 1.

Print the string S.

If such a string does not exist, print UNRESTORABLE.

Constraints

1 \leq |S'|, |T| \leq 50

S' consists of lowercase English letters and ?.

T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T'

Output

Print the string S.

If such a string does not exist, print UNRESTORABLE instead.

Sample Input 1

?tc????
coder

Sample Output 1

atcoder

There are 26 strings that satisfy Condition 1: atcoder, btcoder, ctcoder,..., ztcoder.
Among them, the lexicographically smallest is atcoder, so we can say S = atcoder.

Sample Input 2

??p??d??
abc

Sample Output 2

UNRESTORABLE

There is no string that satisfies Condition 1, so the string S does not exist.


S = gets.chomp
T = gets.chomp

t = T.scan(/./).map {|c| "(#{c}|¥¥?)" }.join
m = Regexp.compile(t)
i = S.index(m)
if i
  S[i, T.size] = T
  puts S.gsub('?', 'a')
else
  puts 'UNRESTORABLE'
end


s = gets.chomp
t = gets.chomp

(s.size-t.size).downto(0) do |i|
    if /#{s[i, t.size].gsub('?','.')}/ === t
        s[i, t.size] = t
        puts s.gsub('?','a')
        exit
    end
end

puts "UNRESTORABLE"


sprime,t = 2.times.map{gets.chomp}
lens = sprime.length
lent = t.length
s = []
ans = "" << sprime
for i in 0..lens-lent
  ans = "" << sprime
  f = 0
  for j in i..i+lent-1
    if sprime[j] == t[j-i] || sprime[j] == "?"
      ans[j] = t[j-i]
    else
      f = 1
      break
    end
  end
  if f == 1
    next
  end
  if i != 0
    for j in 0..i-1
      if sprime[j] == "?"
        ans[j] = "a"
      end
    end
  end
  if i != lens-lent
    for j in i+lent..lens-1
      if sprime[j] == "?"
        ans[j] = "a"
      end
    end
  end
  s << ans
end
if s.empty?
  puts "UNRESTORABLE"
else
  puts s.sort[0]
end


def check(ss,t,i)
    if t[0]==ss[i]
        return true
    else
        return false
    end

end

ss=gets.chomp.chars
t=gets.chomp.chars

(ss.length-t.length).downto(0){|i|

    if check(ss,t,i)
        ss[i..(i-1+t.length)]=t
        ss=ss.join.gsub("?","a")
        p ss
        exit
    else

    end
}

print "UNRESTORABLE"


def my_match s,t

  sl = s.length
  sl.times do |i|
    return false if s[i] != ?? and s[i] != t[i]
  end
  return true
end

def solve sp,t

  s = sp
  sl = s.length
  tl = t.length

  no = "UNRESTORABLE"
  return no if sl < tl

  if sp.include?(t)
    k = sp.index(t)
    s[k, tl] = t
    return s.gsub(??, ?a)
  end

  k = sl - tl
  k.downto(0) do |i|
    if my_match(sp[i,tl],t)
      s[i,tl] = t
      return s.gsub(??, ?a)
    end
  end

  return no
end

sp = gets.chomp
t = gets.chomp

puts solve sp,t


s = gets.chomp.split("")
t = gets.chomp.split("")

flg = false

for i in 0..s.length-1 do
  for j in 0..t.length-1 do
    break unless s[i+j] == t[j] or s[i+j] == "?"
    if j == t.length-1 then
      flg = true
      s[i..i+j] = t
    end
  end
end

if flg then
  puts s.join.gsub( /\?/,"a")
else
  puts "UNRESTORABLE"
end


require 'set'; require 'prime'
INF=Float::INFINITY; MOD=10**9+7
h,w = gets.chomp.split.map(&:to_i)
s = []
h.times do
  s.push gets.chomp.split(//)
end

dy = [-1, -1, -1, 0, 0, 1, 1, 1]
dx = [-1,  0,  1,-1, 1,-1, 0, 1]

h.times do |y|
  w.times do |x|
    next if s[y][x] == '#'
    count = 0
    8.times do |i|
      ny = y + dy[i]; nx = x + dx[i]
      if (0 <= ny && ny < h) && (0 <= nx && nx < w) && s[ny][nx] == '#'
        count += 1
      end
    end
    s[y][x] = count
  end
end

s.each do |line|
  puts line.join('')
end

Score : 200 points

Problem Statement

You are given an H × W grid.

The squares in the grid are described by H strings, S_1,...,S_H.

The j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \leq i \leq H,1 \leq j \leq W).

. stands for an empty square, and # stands for a square containing a bomb.

Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.

(Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.)

He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.

Print the strings after the process.

Constraints

1 \leq H,W \leq 50

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

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the H strings after the process.

The i-th line should contain a string T_i of length W, where the j-th character in T_i corresponds to the square at the i-th row from the top and j-th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W).

Sample Input 1

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

Sample Output 1

11211
1#2#1
11211

For example, let us observe the empty square at the first row from the top and first column from the left.

There is one bomb square adjacent to this empty square: the square at the second row and second column.

Thus, the . corresponding to this empty square is replaced with 1.

Sample Input 2

3 5
#####
#####
#####

Sample Output 2

#####
#####
#####

It is possible that there is no empty square.

Sample Input 3

6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...

Sample Output 3

#####3
#8#7##
####5#
4#65#2
#5##21
#4#310


h, _w = gets.split(' ').map!(&:to_i)

masu = Hash.new([])
h.times do |i|
  masu[i] = gets.chomp!.split('')
end

ans = {}
masu.each do |i, arr|
  convert = []
  arr.each.with_index do |s, j|
    next if s == ''

    if s == '#'
      convert << s
    else
      count = 0
      start, len = (j - 1).negative? ? [0, 2] : [(j - 1), 3]
      3.times do |k|
      # 周囲の 8 マスとして前の行、現在の行、次の行を調査
        count += masu[i - 1 + k].slice(start, len).count('#') unless masu[i - 1 + k].empty?
      end

      convert << count
    end
  end
  ans[i] = convert
end

ans.each do |_, arr|
  puts arr.join
end


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

(0...H).map{|i|
    puts (0...W).map{|j|
        if S[i][j] == "." then
            hs, he = [i-1, 0].max, [i+1, H-1].min
            ws, we = [j-1, 0].max, [j+1, W-1].min
            (hs..he).inject(0){|sum, v| sum += S[v][ws..we].count("#"); sum}.to_s
        else
            S[i][j]
        end
    }.join
}


$h,$w = gets.split.map(&:to_i)
$s = []
$h.times do |i|
  $s[i] = gets.chomp.split("")
end

def mines(h,w)
  sum = 0
  (h-1..h+1).each do |i|
    next if i < 0 || i > $h - 1
    (w-1..w+1).each do |j|
      next if j < 0 || j > $w - 1
      sum += 1 if $s[i][j] == "#"
    end
  end
  return sum
end

$h.times do |h|
  $w.times do |w|
    next if
    print $s[h][w] == '#' ? "#" : mines(h,w)
  end
  print "\n"
end


(h, w) = gets.chomp.split(' ').map(&:to_i)

s = []
h.times {|y| s[y] = gets.chomp}

t = []

def in_board(h, w, y, x)
  0 <= y and y < h and 0 <= x and x < w
end

def count(s, y, x)
  h = s.size
  w = s[0].size

  c = 0

  [-1, 0, 1].each do |dy|
    [-1, 0, 1].each do |dx|
      if in_board(h, w, y + dy, x + dx) then
        if s[y + dy][x + dx] == '#' then
          c += 1
        end
      end
    end
  end

  return c
end

h.times do |y|
  t[y] = []
  w.times do |x|
    if s[y][x] == '#' then
      t[y][x] = '#'
    else
      t[y][x] = count(s, y, x).to_s
    end
  end
end

t.size.times do |y|
  puts t[y].join
end


n = gets.to_i
ans = 0
n.times do
  l,r = gets.chomp.split(' ').map(&:to_i)
  ans += r - l + 1
end
puts ans

Score : 200 points

Problem Statement

Joisino is working as a receptionist at a theater.

The theater has 100000 seats, numbered from 1 to 100000.

According to her memo, N groups of audiences have come so far, and the i-th group occupies the consecutive seats from Seat l_i to Seat r_i (inclusive).

How many people are sitting at the theater now?

Constraints

1≤N≤1000

1≤l_i≤r_i≤100000

No seat is occupied by more than one person.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
l_1 r_1
:
l_N r_N

Output

Print the number of people sitting at the theater.

Sample Input 1

1
24 30

Sample Output 1

7

There are 7 people, sitting at Seat 24,25,26,27,28,29 and 30.

Sample Input 2

2
6 8
3 3

Sample Output 2

4


n = gets.chomp.to_i
c = 0
n.times do
  l, r = gets.chomp.split(' ').map(&:to_i)
  c += r - l + 1
end
puts c.to_s


N = gets.to_i

ans = 0
N.times do
  l, r = gets.split.map(&:to_i)
  ans += r - l + 1
end

puts ans


n = gets.to_i
ans = 0
for i in 1..n do
    l,r = gets.chomp.split(" ").map(&:to_i)
    ans += r-l+1
end
print(ans)


N = gets.to_i
tot = 0
N.times do
  l, r = gets.split.map(&:to_i)
  tot += r - l + 1
end
puts tot


p gets.to_i.times.map {
  l, r = gets.split.map(&:to_i)
  r - l + 1
}.inject :+


n = gets.chomp.to_i
sum = 0
n.times do
    l, r = gets.chomp.split.map(&:to_i)
    sum += r - l + 1
end
puts sum


S = gets.chomp.split("")
alp = "abcdefghijklmnopqrstuvwxyz".split("")
ans = [0] * 26
S.each do |s|
    ans[alp.index(s)] = 1
end

26.times do |t|
    if ans[t] == 0
        puts alp[t]
        exit
    end
end
puts "None"

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Find the lexicographically (alphabetically) smallest lowercase English letter that does not occur in S.
If every lowercase English letter occurs in S, print None instead.

Constraints

1 \leq |S| \leq 10^5 (|S| is the length of string S.)

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the lexicographically smallest lowercase English letter that does not occur in S.
If every lowercase English letter occurs in S, print None instead.

Sample Input 1

atcoderregularcontest

Sample Output 1

b

The string atcoderregularcontest contains a, but does not contain b.

Sample Input 2

abcdefghijklmnopqrstuvwxyz

Sample Output 2

None

This string contains every lowercase English letter.

Sample Input 3

fajsonlslfepbjtsaayxbymeskptcumtwrmkkinjxnnucagfrg

Sample Output 3

d


string = gets.chomp.split('')
alp = [*'a'..'z']

alp.each_with_index do |a, i|
  break puts a unless string.include?(a)
  puts 'None' if i+1 == string.size
end


S = gets.chomp
hash =  ('a'..'z').map{|s| [s, false]}.to_h
hash.keys.each do |k, v|
    hash[k] = !!S.match(k)
end
c = hash.keep_if{|k, v| !v}.keys.sort.first
puts c ? c : 'None'


s = gets.chomp.split("")
al_arr = ("a".."z").to_a

s.each do |v|
  al_arr.delete(v)
end

if al_arr.count == 0
  puts "None"
else
  puts al_arr[0]
end


s = gets.split("").map!{|o| o.to_s}
aa = 'z'.upto('a').to_a
none = nil
0.upto(25) do |b|
  frag = false
  (s.length-1).times do |i|
    if s[i] == aa[b]
      frag = true
    end
  end
  if frag == false
    none = aa[b]
  end
end
if none == nil
  puts "None"
else puts none
end


# rubyで簡単にアルファベットの配列を作れる
alphabet = ('a'..'z').to_a # [*'a'..'z']だけでもいけるらしい
s = gets.chomp.chars.uniq.sort

ans = ''
s.each.with_index do |k, idx|
    if k!=alphabet[idx] then
        ans = alphabet[idx]
        break
    end
end

puts ans=='' ? 'None' : ans


str = gets.split("").uniq.join
all = "abcdefghijklmnopqrstuvwxyz".split("")
cnt,ans = 0,[]
str.size.times do
  ans << all[cnt] unless str.include?(all[cnt])
  cnt += 1
end
puts ans.empty? ? "None" : ans.min


h = Hash.new
for c in 'a'..'z' do
  h[c] = 0
end

s = gets.chomp.split("")
s.each { |c| h[c] = 1}

for c in 'a'..'z' do
  if h[c] == 0 then
    puts c
    exit
  end
end

puts 'None'


s = gets.chomp!
a = [*'a'..'z']
n = Array.new(26, 0)
h = Hash[*[a,n].transpose.flatten]
flag = false

s.each_char {|c| h[c] += 1 }
h.each do |k,v|
  if v == 0
    puts k
    flag = true
    exit
  end
end

puts "None" if flag == false


gets
puts ((?a..?z).reject{|c|$_[c]}.min || "None")


string = gets
string.strip!

characters = %w[a b c d e f g h i j k l m n o p q r s t u v w x y z]

chars = string.chars.uniq
chars.each do |char|
  index = characters.index(char)
  if index
    characters.delete_at(index)
  end
end

if characters.empty?
  puts 'None'
else
  puts characters.first
end


line = gets.chomp.squeeze.split("").sort

c = "a"
flag = true
line.each do |val|
  if val == c
    c = c.succ
  else
    puts c
    flag = !flag
    break
  end
end

puts "None" if flag


input = gets
result = ''
flag = 'a'
while flag != 'aa'
	unless input.include?(flag)
		result = flag
		break
	end
	flag = flag.next()
end
if result != ''
	puts result
else
	puts 'None'
end


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

o = 0
four = 0
a.each do |ai|
  if ai.odd?
    o += 1
  elsif ai % 4 == 0
    four += 1
  end
end

if o <= four || 2 * four  + 1 == N
  p 'Yes'
else
  p 'No'
end

Score : 400 points

Problem Statement

We have a sequence of length N, a = (a_1, a_2, ..., a_N).
Each a_i is a positive integer.

Snuke's objective is to permute the element in a so that the following condition is satisfied:

For each 1 ≤ i ≤ N - 1, the product of a_i and a_{i + 1} is a multiple of 4.

Determine whether Snuke can achieve his objective.

Constraints

2 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If Snuke can achieve his objective, print Yes; otherwise, print No.

Sample Input 1

3
1 10 100

Sample Output 1

Yes

One solution is (1, 100, 10).

Sample Input 2

4
1 2 3 4

Sample Output 2

No

It is impossible to permute a so that the condition is satisfied.

Sample Input 3

3
1 4 1

Sample Output 3

Yes

The condition is already satisfied initially.

Sample Input 4

2
1 1

Sample Output 4

No

Sample Input 5

6
2 7 1 8 2 8

Sample Output 5

Yes


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

mod4 = Array.new(3, 0)

arr.each do |a|
  cnt = 0
  while true
    break if cnt == 2
    if a.even? then
      cnt += 1
      a /= 2
    else
      break
    end
  end
  mod4[cnt] += 1
end

if mod4[0] > mod4[2] * 2 then
  puts "No"
elsif mod4[1] > ((mod4[2] - mod4[0] / 2) * 2)then
  puts "No"
else
  puts "Yes"
end


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

if  a != b && n == 1
  puts 0
elsif b - a >= 0
  ary = (a..(a + [n - 1, b - a].min)).to_a.repeated_combination(b-a).to_a
  ary_sum = []
  ary.each {|x|
    ary_sum << x.sum
  }
  p ary_sum.uniq.size
else
  puts 0
end

Score : 200 points

Problem Statement

Snuke has N integers. Among them, the smallest is A, and the largest is B.
We are interested in the sum of those N integers. How many different possible sums there are?

Constraints

1 ≤ N,A,B ≤ 10^9

A and B are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of the different possible sums.

Sample Input 1

4 4 6

Sample Output 1

5

There are five possible sums: 18=4+4+4+6, 19=4+4+5+6, 20=4+5+5+6, 21=4+5+6+6 and 22=4+6+6+6.

Sample Input 2

5 4 3

Sample Output 2

0

Sample Input 3

1 7 10

Sample Output 3

0

Sample Input 4

1 3 3

Sample Output 4

1


def f(s, qs)
  counts = s.map{|l|
    # l.inject([0]){|r, c| r + [r.last + c] }
    r = [0]
    l.each{|c| r << r.last + c }
    r
  }.transpose.map{|l|
    # l.inject([0]){|r, c| r + [r.last + c] }
    r = [0]
    l.each{|c| r << r.last + c }
    r
  }.transpose

  horizontals = s.map{|l|
    rr = [0]
    l.inject(0){|r,c| rr << (r + c > 1 ? rr.last + 1 : rr.last); c }
    rr[1..-1]+[rr.last]
    # rr
  }.transpose.map{|l|
    l.inject([0]){|r, c| r + [r.last + c] }
  }.transpose

  verticals = s.transpose.map{|l|
    rr = [0]
    l.inject(0){|r,c| rr << (r + c > 1 ? rr.last + 1 : rr.last); c }
    rr[1..-1]+[rr.last]
    # rr
  }.transpose.map{|l|
    l.inject([0]){|r, c| r + [r.last + c] }
  }
  # p [counts, horizontals, verticals]

  qs.map{|x1, y1, x2, y2|
    g = lambda{|cusum, x1, y1, x2, y2|
      return 0 if x1 > x2 || y1 > y2
      cusum[x2+1][y2+1] - cusum[x2+1][y1] - cusum[x1][y2+1] + cusum[x1][y1]
      # [cusum[x2+1][y2+1], cusum[x2+1][y1], cusum[x1][y2+1], cusum[x1][y1]]
    }
    g.(counts, x1, y1, x2, y2) - g.(horizontals, x1, y1, x2, y2-1) - g.(verticals, x1, y1, x2-1, y2)
    # [g.(counts, x1, y1, x2, y2), g.(horizontals, x1, y1, x2, y2-1) - g.(verticals, x1, y1, x2-1, y2)]
  }
end

N, M, Q = gets.split.take(3).map(&:to_i)

S = N.times.map{gets.chomp.chars.take(M).map(&:to_i)}
Queries = Q.times.map{
  gets.split.take(4).map(&:to_i).map(&:pred)
}
puts f(S, Queries)


class PriorityQueue
	class Node
		attr_reader :value, :priority
		def initialize(value, priority)
			@value = value
			@priority = priority
		end
	end

	def initialize
		@heap = [nil]
	end

	def size
		@heap.size - 1
	end

	def empty?
		size == 0
	end

	def peek
		@heap[1].value
	end

	def enqueue(value, priority)
		@heap.push(Node.new(value, priority))
		up_heap
		self
	end

	def dequeue
		return nil if empty?
		return @heap.pop.value if size == 1
		ret = @heap[1].value
		@heap[1] = @heap.pop
		down_heap
		ret
	end

	private

	def up_heap
		pos = size
		while pos > 1
			parent = pos / 2
			break if @heap[pos].priority <= @heap[parent].priority
			tmp = @heap[pos]
			@heap[pos] = @heap[parent]
			@heap[parent] = tmp
			pos = parent
		end
	end

	def down_heap
		pos = 1
		loop{
			left = pos * 2
			right = pos * 2 + 1
			break if left >= @heap.size
			if right >= @heap.size || @heap[left].priority >= @heap[right].priority
				break if @heap[pos].priority >= @heap[left].priority
				tmp = @heap[pos]
				@heap[pos] = @heap[left]
				@heap[left] = tmp
				pos = left
			else
				break if @heap[pos].priority >= @heap[right].priority
				tmp = @heap[pos]
				@heap[pos] = @heap[right]
				@heap[right] = tmp
				pos = right
			end
		}
	end
end

N = gets.to_i
A = gets.split.map{|i|i.to_i}
a1 = PriorityQueue.new
a2 = PriorityQueue.new
s1 = [0]
s2 = [0]
N.times{|i|
	a1.enqueue(A[i], -A[i])
	a2.enqueue(A[N * 2 + i], A[N * 2 + i])
	s1[0] += A[i]
	s2[0] += A[N * 2 + i]
}

N.times{|i|
	a = A[N + i]
	s = s1[-1]
	if a1.peek < a
		a1.enqueue(a, -a)
		d = a1.dequeue
		s += a
		s -= d
	end
	s1 << s

	a = A[N * 2 - 1 - i]
	s = s2[-1]
	if a2.peek > a
		a2.enqueue(a, a)
		d = a2.dequeue
		s += a
		s -= d
	end
	s2 << s
}

max = -N * 10 ** 9
(N + 1).times{|i|
	d = s1[i] - s2[N - i]
	if max < d
		max = d
	end
}
puts max

Score : 500 points

Problem Statement

Let N be a positive integer.

There is a numerical sequence of length 3N, a = (a_1, a_2, ..., a_{3N}).
Snuke is constructing a new sequence of length 2N, a', by removing exactly N elements from a without changing the order of the remaining elements.
Here, the score of a' is defined as follows: (the sum of the elements in the first half of a') - (the sum of the elements in the second half of a').

Find the maximum possible score of a'.

Constraints

1 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Partial Score

In the test set worth 300 points, N ≤ 1000.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the maximum possible score of a'.

Sample Input 1

2
3 1 4 1 5 9

Sample Output 1

1

When a_2 and a_6 are removed, a' will be (3, 4, 1, 5), which has a score of (3 + 4) - (1 + 5) = 1.

Sample Input 2

1
1 2 3

Sample Output 2

-1

For example, when a_1 are removed, a' will be (2, 3), which has a score of 2 - 3 = -1.

Sample Input 3

3
8 2 2 7 4 6 5 3 8

Sample Output 3

5

For example, when a_2, a_3 and a_9 are removed, a' will be (8, 7, 4, 6, 5, 3), which has a score of (8 + 7 + 4) - (6 + 5 + 3) = 5.


class PriorityQueue
  attr_accessor :data

  def initialize(arr=[])
    @data = arr
  end

  def push(e)
    @data.push(e)

    current_index = @data.size-1
    parent_index = parent(current_index)

    while current_index > 0 && @data[parent_index] > @data[current_index]
      @data[current_index] = @data[parent_index]
      @data[parent_index] = e
      current_index = parent_index
      parent_index = parent(current_index)
    end
  end

  def pop
    ret = @data.first
    last = @data.last
    current_index = 0

    while have_child?(current_index)
      li = left_child_index(current_index)
      ri = right_child_index(current_index)

      ni = li
      ni = ri if ri < @data.size && @data[ri] < @data[li]

      break if @data[ni] >= last

      @data[current_index] = @data[ni]
      current_index = ni
    end
    @data[current_index] = last
    @data.pop

    ret
  end

  def size
    @data.length
  end

  def parent(index)
    (index-1)/2
  end

  def left_child_index(index)
    2*index+1
  end

  def right_child_index(index)
    2*index+2
  end

  def have_child?(index)
    left_child_index(index) < @data.size
  end
end

class BigPQ < PriorityQueue
  def push(e)
    @data.push(e)

    current_index = @data.size-1
    parent_index = parent(current_index)

    while current_index > 0 && @data[parent_index] < @data[current_index]
      @data[current_index] = @data[parent_index]
      @data[parent_index] = e
      current_index = parent_index
      parent_index = parent(current_index)
    end
  end

  def pop
    ret = @data.first
    last = @data.last
    current_index = 0

    while have_child?(current_index)
      li = left_child_index(current_index)
      ri = right_child_index(current_index)

      ni = li
      ni = ri if ri < @data.size && @data[ri] > @data[li]

      break if @data[ni] <= last

      @data[current_index] = @data[ni]
      current_index = ni
    end
    @data[current_index] = last
    @data.pop

    ret
  end
end

n=gets.strip.to_i
aa = gets.strip.split.map(&:to_i)

bpq = BigPQ.new
pq = PriorityQueue.new

n.times do |i|
  pq.push(aa[i])
  bpq.push(aa[3*n-1-i])
end

left_score = pq.data.inject(:+)
left_scores = [left_score]

right_score = bpq.data.inject(:+)
right_scores = [right_score]

n.times do |i|
  c = aa[n+i]
  if c >= pq.data.first
    pq.push(c)
    left_score -= pq.pop
    left_score += c
  end
  left_scores.push(left_score)

  c = aa[2*n-1-i]
  if c <= bpq.data.first
    bpq.push(c)
    right_score -= bpq.pop
    right_score += c
  end
  right_scores.unshift(right_score)
end

result = left_scores.zip(right_scores).map {|l, r| l-r }.max

#p left_scores
#p right_scores
p result


class PriorityQueue
  attr_accessor :data

  def initialize(arr=[])
    @data = arr
  end

  def push(e)
    @data.push(e)

    current_index = @data.size-1
    parent_index = parent(current_index)

    while current_index > 0 && @data[parent_index] > @data[current_index]
      @data[current_index] = @data[parent_index]
      @data[parent_index] = e
      current_index = parent_index
      parent_index = parent(current_index)
    end
  end

  def pop
    ret = @data.first
    last = @data.last
    current_index = 0

    while have_child?(current_index)
      li = left_child_index(current_index)
      ri = right_child_index(current_index)

      ni = li
      ni = ri if ri < @data.size && @data[ri] < @data[li]

      break if @data[ni] >= last

      @data[current_index] = @data[ni]
      current_index = ni
    end
    @data[current_index] = last
    @data.pop

    ret
  end

  def size
    @data.length
  end

  def parent(index)
    (index-1)/2
  end

  def left_child_index(index)
    2*index+1
  end

  def right_child_index(index)
    2*index+2
  end

  def have_child?(index)
    left_child_index(index) < @data.size
  end
end

class BigPQ < PriorityQueue
  def push(e)
    @data.push(e)

    current_index = @data.size-1
    parent_index = parent(current_index)

    while current_index > 0 && @data[parent_index] < @data[current_index]
      @data[current_index] = @data[parent_index]
      @data[parent_index] = e
      current_index = parent_index
      parent_index = parent(current_index)
    end
  end

  def pop
    ret = @data.first
    last = @data.last
    current_index = 0

    while have_child?(current_index)
      li = left_child_index(current_index)
      ri = right_child_index(current_index)

      ni = li
      ni = ri if ri < @data.size && @data[ri] > @data[li]

      break if @data[ni] <= last

      @data[current_index] = @data[ni]
      current_index = ni
    end
    @data[current_index] = last
    @data.pop

    ret
  end
end

n=gets.strip.to_i
aa = gets.strip.split.map(&:to_i)

bpq = BigPQ.new
pq = PriorityQueue.new

n.times do |i|
  pq.push(aa[i])
  bpq.push(aa[3*n-1-i])
end

left_score = pq.data.inject(:+)
left_scores = [left_score]

right_score = bpq.data.inject(:+)
right_scores = [right_score]

n.times do |i|
  pq.push(aa[n+i])
  left_score -= pq.pop
  left_score += aa[n+i]
  left_scores.push(left_score)

  bpq.push(aa[2*n-1-i])
  right_score -= bpq.pop
  right_score += aa[2*n-1-i]
  right_scores.unshift(right_score)
end

result = -1*10**10
n.times do |i|
  result = [result, left_scores[i]-right_scores[i]].max
end

#p left_scores
#p right_scores
p result


# https://atcoder.jp/contests/abc062/tasks/arc074_b
# in
# 3
# 8 2 2 7 4 6 5 3 8
# out
# 5

N = gets.to_i
a3n = gets.split.map(&:to_i).each_slice(N).to_a

a2_lower = 0
a2_upper = N - 1

u = []
N.times do
  p a3n
  a1_min = a3n[0].min
  a2_low = a3n[1][a2_lower]
  a2_upp = a3n[1][a2_upper]
  a3_max = a3n[2].max

  if (a3_max - a2_upp) > (a2_low - a1_min)
    if (a3_max - a2_upp) > 0
      u << a3_max
      a3n[2][a3n[2].index(a3_max)] = a2_upp
      a2_upper -= 1
    else
      u << a2_upp
      a2_upper -= 1
    end
  else
    if (a2_low - a1_min) > 0
      u << a1_min
      a3n[0][a3n[0].index(a1_min)] = a2_low
      a2_lower += 1
    else
      u << a2_low
      a2_lower += 1
    end
  end
end
p a3n[0].reduce(:+) - a3n[2].reduce(:+)


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

def former_array(a, half)
  a[0..(half - 1)]
end

def latter_array(a, half)
  a[half..-1]
end

def former_min_index(a)
  half = (a.size.to_f/2).ceil
  a1 = former_array(a, half)
  p half,a1
  a1.index(a1.min)
end

def latter_max_index(a)
  half = (a.size.to_f/2).floor
  a2 = latter_array(a, half)
  p half,a2
  a2.index(a2.max) + half
end

def score(a)
  sum = 0
  half_size = a.size / 2
  a.each_with_index do |v, i|
    if i < half_size
      sum += v
    else
      sum -= v
    end
  end
  sum
end

def select_max_score_array(a, ais_array)
  p a, ais_array
  map = ais_array.map do |ais|
          a0 = a.dup
          ais.each do |ai|
            a0.delete_at(ai)
          end
          {a: a0, score: score(a0)}
        end
  p map
  max = -1
  max_array = []
  map.each do |o|
    if o[:score] > max
      max = o[:score]
      max_array = [o[:a]]
    elsif o[:score] == max
      max_array << o[:a]
    end
  end
  max_array
end

@candidates = []

def cut(a)
  size = a.size
  if a.size == N * 2
    @candidates << score(a)
    return
  end
  ceil_half_size = (a.size.to_f/2).ceil
  floor_half_size = (a.size.to_f/2).floor

  if (a.size - 1) % 2 == 0
    # 1個抜きの2パターンのうちスコアが高い方を選択
    a1i = former_min_index(a)
    a2i = latter_max_index(a)
    next_a = select_max_score_array(a, [[a1i], [a2i]])
    next_a.each do |i|
      cut(i)
    end
  else
    # 2個抜きの4パターンのうちスコアが高いものを選択
    a1i = former_min_index(a)
    a01 = a.dup
    a01.delete_at(a1i)
    a11i = former_min_index(a01)
    a12i = latter_max_index(a01)
    a2i = latter_max_index(a)
    a02 = a.dup
    a02.delete_at(a2i)
    a21i = former_min_index(a02)
    a22i = latter_max_index(a02)
    next_a = select_max_score_array(a, [[a1i, a11i], [a1i, a12i], [a2i, a21i], [a2i, a22i]])
    next_a.each do |i|
      cut(i)
    end
  end
end

cut(A)
puts @candidates.max


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

ans = -Float::INFINITY
(N..(2*N)).each do |k|
  b = a[0,k]
  c = a[k..-1]
  ans = [
    ans,
    b.inject(:+) -
    (b.min(k-N).inject(:+) || 0 ) -
    (c.inject(:+) -
     (c.max(2*N-k).inject(:+) || 0))
  ].max
end

p ans


class PriorityQueue
  attr :s

  def initialize
    @q = [nil]
    @s = 0
  end

  def bubble_up(i)
    p = i / 2
    return if i <= 1 || @q[p] <= @q[i]
    @q[i], @q[p] = @q[p], @q[i]
    bubble_up(p)
  end

  def push(e)
    @q << e
    @s += e
    bubble_up(@q.size - 1)
  end

  def bubble_down(i)
    c = i * 2
    return if c >= @q.size
    c += 1 if c != @q.size - 1 && @q[c] > @q[c + 1]
    return if @q[i] <= @q[c]
    @q[i], @q[c] = @q[c], @q[i]
    bubble_down(c)
  end

  def pop
    @q[1], @q[-1] = @q[-1], @q[1]
    ret = @q.pop
    @s -= ret
    bubble_down(1)
    ret
  end
end

N = gets.to_i
As = gets.split.map(&:to_i)

def f(xs, ys)
  pq = PriorityQueue.new
  xs.each{|x| pq.add(x)}
  ys.each_with_object([pq.s]){|y, o| pq.push_pop(y); o << pq.s}
end

as1, as2, as3 = As.each_slice(N).to_a
s1 = f(as1, as2)
s2 = f(as3.map{|a| -a}, as2.map{|a| -a}.reverse).reverse
puts s1.zip(s2).map{|x, y| x + y}.max


class PriorityQueue

  def initialize
    @heap = Array.new
    @sz = 0
  end

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

  def pop

    ret = @heap[0]
    x = @heap[@sz-=1]

    i = 0
    while i * 2 + 1 < @sz do
      a = i * 2 + 1
      b = i * 2 + 2
      a = b if b < @sz && @heap[b] < @heap[a]
      break if @heap[a] >= x
      @heap[i] = @heap[a]
      i = a
    end

    @heap[i] = x
    @heap.delete_at(-1)
    ret
  end

  def top
    @heap[0]
  end

  def empty?
    @sz == 0
  end

  def all
    @heap
  end
end

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

fpq = PriorityQueue.new
spq = PriorityQueue.new

f_sum = Array.new( N + 1 )
s_sum = Array.new( N + 1 )

fa = A[0, N]
sa = A[2 * N, N]

f_sum[0] = fa.inject(:+)
s_sum[0] = sa.inject(:+)

A[0, N].each do |a|
  fpq.push( a )
end

N.times do |i|
  a = A[N+i]
  fpq.push( a )
  m = fpq.pop
  f_sum[i+1] = f_sum[i] + a - m
end

# p f_sum

A[2 * N, N].each do |a|
  spq.push( -a )
end

N.times do |i|
  a = A[2*N-i-1]
  spq.push( -a )
  m = -spq.pop
  s_sum[i+1] = s_sum[i] + a - m
end

s_sum.reverse!

ans = nil
(N+1).times do |i|
  v = f_sum[i] - s_sum[i]
  ans = v if ans.nil?
  ans = v if ans < v
end

puts ans


s = gets.chomp.chars
a_index = []
z_index = []
max = 0
s.each_with_index do |ss,i|
  if ss == "A"
    a_index << i
  elsif ss == "Z"
    z_index << i
  end
end

puts z_index.max - a_index.min + 1

Score : 200 points

Problem Statement

Snuke has decided to construct a string that starts with A and ends with Z, by taking out a substring of a string s (that is, a consecutive part of s).

Find the greatest length of the string Snuke can construct. Here, the test set guarantees that there always exists a substring of s that starts with A and ends with Z.

Constraints

1 ≦ |s| ≦ 200{,}000

s consists of uppercase English letters.

There exists a substring of s that starts with A and ends with Z.

Input

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

s

Output

Print the answer.

Sample Input 1

QWERTYASDFZXCV

Sample Output 1

5

By taking out the seventh through eleventh characters, it is possible to construct ASDFZ, which starts with A and ends with Z.

Sample Input 2

ZABCZ

Sample Output 2

4

Sample Input 3

HASFJGHOGAKZZFEGA

Sample Output 3

12


S=gets.chomp

# puts S.rindex('Z') - S.index('A') + 1
puts S[/A.*Z/].size


a=gets.chomp
b=a.chars.index("A")
c=a.chars.index("Z")
puts c-b+1


s = gets.split("")
left = s.find_index('A')
right = s.size - s.find_index('Z').reverse + 1

puts left - rig


S = gets.chomp.split('')
count = 0
flg = false
for s in S do
  if s == "A"
    flg = true
  elsif s == "Z"
    flg = false
  end
  count += 1 if flg
end
puts (count+1)


s=gets.chomp
puts s.rindex(?Z) - s.index(?A) + 1


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

a = s.index('A')
z = ss.index('Z')

p s[a..-z-1].size


S = gets.chomp

i = S.index('A')
j = S.rindex('Z')

puts j - i + 1


s = gets.to_s
a_index = s.index("A")
z_index = s.reverse.index("Z")

ans = s[a_index..-z_index - 1]

puts ans.size


str = gets.chomp
puts str.rindex("Z") - str.index("A") + 1


s = gets.chomp.split("")
first_a_index = s.index("A")
first_z_index = s.rindex("Z")
print("#{first_z_index - first_a_index+1}")


puts(gets.scan(/A.*Z/).sort_by { |s| s.length }.first.length)


s=gets.chomp
p s.rindex('Z') - s.index('A') + 1


s = gets.chomp
start = -1
ending = -1

s.size.times do |i|
    if start < 0
        if s[i] == 'A'
            start = i
        end
    else
        if s[i] == 'Z'
            ending = i
        end
    end
end
result = ending - start + 1
puts result


k, s = gets.strip.split.map(&:to_i)
count = 0
(0..k).each do |x|
  (0..k).each do |y|
    if s-x-y >= 0 && s-x-y <= k
      count+=1 if s == x+y+(s-x-y)
    end
  end
end
puts count

Score : 200 points

Problem Statement

You are given two integers K and S.

Three variable X, Y and Z takes integer values satisfying 0≤X,Y,Z≤K.

How many different assignments of values to X, Y and Z are there such that X + Y + Z = S?

Constraints

2≤K≤2500

0≤S≤3K

K and S are integers.

Input

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

K S

Output

Print the number of the triples of X, Y and Z that satisfy the condition.

Sample Input 1

2 2

Sample Output 1

6

There are six triples of X, Y and Z that satisfy the condition:

X = 0, Y = 0, Z = 2

X = 0, Y = 2, Z = 0

X = 2, Y = 0, Z = 0

X = 0, Y = 1, Z = 1

X = 1, Y = 0, Z = 1

X = 1, Y = 1, Z = 0

Sample Input 2

5 15

Sample Output 2

1

The maximum value of X + Y + Z is 15, achieved by one triple of X, Y and Z.


K,S = gets.split.map(&:to_i)
puts [*0..K].repeated_permutation(2).count {|x,y| (S-x-y).between?(0,K) }


k, s = gets.split.map(&:to_i)
cnt = 0

0.upto(k) do |i|
  0.upto(k) do |j|
    0.upto(k) do |k|
      cnt += 1 if i + j + k == s
    end
  end
end

puts cnt


def C(n,k)
  k = n - k if n - k < k
  k.times.inject(1){|s,i| s * (n - i) / (i + 1) }
end
K, S = gets.split.map(&:to_i)
ans = (0..3).inject(0) do |t, i|
  sign = i.even? ? 1 : -1
  n = S + 3 - (K + 1) * i - 1
  n >= 3 ? t + sign * C(n, 2) * C(3, i) : t
end
puts ans


k,s = gets.split.map(&:to_i)
cnt = 0
for x in 0..k
for y in 0..k
for z in 0..k
  if x + y + z == s
    cnt += 1
  end
end
end
end
puts cnt


k, s = gets.strip.split.map(&:to_i)
cnt = 0

(k + 1).times do |i|
    (k + 1).times do |j|
        z = s - i - j
        if 0 <= z && z <= k
            cnt += 1
        end
    end
end

puts cnt


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

a, b =
if S <= K
  [0, S]
elsif S <= 2 * K
  [0, K]
else
  [S - 2 * K, K]
end

ans = (a..b).reduce(0) do |x, i|
  t = S - i
  x + (t <= K ? t + 1 : 2 * K - t + 1)
end

puts ans


input = gets.chomp.split(" ")
K = input[0].to_i
S = input[1].to_i

X = Y = (0..K).to_a
c = 0

X.each do |tx|
    if(S-tx) > (2*K) then next end
    Y.each do |ty|
        if(S-tx-ty) <= K then
            c+=1
        else
            next;
        end
    end
end

puts c


if __FILE__==$0
	reads_i=lambda{|s,d|s.strip.split(d).map(&:to_i)}
	data=reads_i[gets," "]
	k=data[0]
	s=data[1]
	cnt=0
	for x in 0..s+1
		for y in 0..s+1
			for z in 0..s+1
				if x+y+z==s then cnt+=1 end
			end
		end
	end
	p cnt
end


input = gets.chomp.split(" ")
K = input[0].to_i
S = input[1].to_i

X = Y = Z = (0..K).to_a
P = X.product(Y, Z).collect { |item|
    item.inject { |sum, n| sum + n }
}

print P.count(S)


k, s = gets.chomp.split(' ').map(&:to_i)
x, y, z = 0, 0, 0

count = 0
(x..k).to_a.each do |i|
  (y..k).to_a.each do |j|
    (z..k).to_a.each do |l|
      next if i + j + l != s
      count += 1
    end
  end
end

puts count


s = gets.chomp

t = []
ans = 0

a = ["dream", "dreamer", "erase", "eraser"]

a.each do |i|
  s.gsub!(/#{i}/,'')
end

puts s.empty? ? "YES": "NO"

Score : 300 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Another string T is initially empty.
Determine whether it is possible to obtain S = T by performing the following operation an arbitrary number of times:

Append one of the following at the end of T: dream, dreamer, erase and eraser.

Constraints

1≦|S|≦10^5

S consists of lowercase English letters.

Input

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

S

Output

If it is possible to obtain S = T, print YES. Otherwise, print NO.

Sample Input 1

erasedream

Sample Output 1

YES

Append erase and dream at the end of T in this order, to obtain S = T.

Sample Input 2

dreameraser

Sample Output 2

YES

Append dream and eraser at the end of T in this order, to obtain S = T.

Sample Input 3

dreamerer

Sample Output 3

NO


puts gets =~/^(dream|dreamer|erase|eraser)*$/ ? :YES: :NO


s = gets.strip.reverse

d = ['dreamer', 'eraser', 'erase', 'dream'].map(&:reverse)

while true
        f = false
        d.each do |word|
                if s[-3, 3] == word[-3, 3]
                        s.slice!(word)
                        f = true
                        break
                end
        end
        if not f
                break
        end
end

if s == ""
        puts 'YES'
else
        puts 'NO'
end


s = gets.strip.reverse

d = ['dreamer', 'eraser', 'erase', 'dream'].map(&:reverse)

while true
        f = false
        d.each do |word|
                if s[0..2] == word[0..2]
                        s.slice!(word)
                        f = true
                        break
                end
        end
        if not f
                break
        end
end

if s == ""
        puts 'YES'
else
        puts 'NO'
end


def cnt(str,word)
    return str.scan(word).length
end

s = gets.chomp

dreameraser = cnt(s,"dreameraser")
dreamerase  = cnt(s,"dreamerase" ) - dreameraser
dreamer = cnt(s,"dreamer") - ( dreameraser + dreamerase)
dream   = cnt(s,"dream")  -  ( dreameraser + dreamerase + dreamer)
eraser  = cnt(s,"eraser") - dreameraser
erase   = cnt(s,"erase")  - (dreameraser + dreamerase + eraser )

# puts [dreamer, dream, eraser, erase]
if s.size == 11 * dreameraser + 10 * dreamerase + 7 * dreamer + 5 * dream + 6 * eraser + 5 * erase
    puts "YES"
else
    puts "NO"
end


s = gets.chomp

loop do
  if s[-5..-1] == 'dream' || s[-5..-1] == 'erase'
    s.slice!(-5..-1)
  elsif s[-6..-1] == 'eraser'
    s.slice!(-6..-1)
  elsif s[-7..-1] == 'dreamer'
    s.slice!(-7..-1)
  else
    puts 'NO'
    exit
  end
  if s.empty?
    puts 'YES'
    exit
  end
end


s = gets.strip
words = ["dream","dreamer","erase","eraser"]

until s.empty? do
  reduced = false
  words.map do |w|

    if s[-w.size,w.size] == w
      s = s[0..-w.size - 1]
      reduced = true
      break
    end
  end

  unless reduced
    puts "NO"
    exit
  end
end

puts "YES"


S = gets.chomp

divides = ['dream', 'dreamer', 'erase', 'eraser']
dp = S.size.times.map{false}
dp[0] = true

0.step (S.size - 1) do |i|
  next if !dp[i]
  divides.each do |d|
    puts "#{i}: #{d}"
    dp[i + d.size] = true if S.slice(i, d.size) == d
  end
end
p dp
puts dp[S.size] ? 'YES' : 'NO'


n = gets.strip.to_s
puts n
n.gsub!(/eraser/, '')
n.gsub!(/erase/, '')
n.gsub!(/dreamer/, '')
n.gsub!(/dream/, '')
if n.length==0
  puts 'YES'
else
  puts 'NO'
end


S = gets.chomp

size = S.size

while true do
  S.gsub!(/(dream|dreamer|erase|eraser)$/, "")
  (size == S.size) ? break : size = S.size
end

puts S.empty? ? "YES" : "NO"


s = gets

list = [
  "dream",
  "dreamer",
  "erase",
  "eraser",
]

while s.legth >0
  list.each do |w|
    if s[(s.length-w.length)..-1] == w
      s = s[-1..(s.length-w.length)]
    else
      puts "NO"
      exit
    end
  end
end
puts "YES"


S = gets.chomp.reverse
words = ["dream","dreamer","erase","eraser"].map(&:reverse)

def checker(words)
  dev = false
  words.map do |w1|
    s = S[0..w1.size - 1]
    if s == w1
      s = S[w1.size..-1]
      words.map do |w2|
        if s == w2
          dev = true
        end
      end
    end
  end
  return dev
end

puts checker(words) ? "YES" : "NO"


s = gets.strip
words = ['dream', 'dreamer', 'erase', 'eraser']

until s.empty? do
  reduced = false
  words.map do |w|
    if s[-w.size, w.size] == w
      s = s[0..-w.size - 1]
      reduced = true
      break
    end
  end

  unless reduced
    put 'NO'
    exit 0
  end
end

puts 'YES'


s = STDIN.gets.strip;
i = s.length;

dream = "dream"
dreamer = "dreamer"
erase = "erase"
eraser = "eraser"

while i > 0
  if i < 5 {
    puts "No"
    exit
  }
  slice = s[i-5...i]
  if slice == dream || slice == erase
    i = i - 5
    next
  end

  slice = s[i-6..i]
  if slice == eraser
    i = i - 6
    next;
  end

  slice = s[i-7..i];
  if slice == dreamer
    i = i - 7
    next;
  end

  puts "NO"
  exit
end

puts "Yes"


s = gets.chomp
s = s.gsub("eraser", "1").gsub("erase", "1").gsub("dreamer", "1").gsub("dream", "1").delete("1")
puts s.empty? ? "YES" : "No"


t=s=gets.chomp
ws=['dream','dreamer','erase','eraser']
l=s.length
loop do
  w = ws.detect { |w| l>=w.length && s.slice(l-w.length,l) == w }
  break if w.nil?
  l-=w.length
end
puts l == 0 ? 'YES' : 'NO'


d = ["maerd", "remaerd", "esare", "resare"]
s = gets.chomp
s.reverse!
t = s.dup
while s do
  for i in 0..3
    if s.start_with?(d[i])
      #puts d[i]
      #puts s
      #puts t
      s.slice!(0, d[i].length)
      #puts s
      #puts t
    end
    #puts s
  end
  if s == ""
    #puts s
    puts "YES"
    exit
  end
  if s == t
    #puts s
    #puts t
    puts "NO"
    exit
  end
  #puts s
  #puts t
  t = s.dup
end


def check(s)
  t = ['dream','dreamer','erase','eraser'].map(&:reverse)
  while s.length > 0
    if s.length >= 5 && t.include?(s.reverse[0..4])
      s = s[0..-6]
    elsif s.length >= 6 && t.include?(s.reverse[0..5])
      s = s[0..-7]
    elsif s.length >= 7 && t.include?(s.reverse[0..6])
      s = s[0..-8]
    else
      return "NO"
    end
  end
  "YES"
end

s = gets.chomp
puts check(s)


s = gets.chomp
num = 0

while num <= s.size
  num2 = num
  if s[num..num+4] == 'dream'
    if s[num..num+6] == 'dreamer'
      if s[num+5..num+9] == 'erase'
        num = num - 2
      end
      num += 2
    end
    num += 5
  end
  if s[num..num+4] == 'erase'
    if s[num..num+5] == 'eraser'
      num += 1
    end
    num += 5
  end
  if num == num2
    break
  end
end

if num == s.size
  puts 'YES'
else
  puts 'NO'
end


s = gets.strip

words = ['dream', 'dreamer', 'erase', 'eraser']

until s.empty? do
    reduced = false
    words.map do |w|
        if s[-w.size, w.size] == w
            s = s[0..-w.size - 1]
            reduced = true
            break
        end
    end

    unless reduced
        puts 'NO'
        exit 0
    end
end

puts 'YES'


s = gets.strip
a = %w(dream dreamer erase eraser).map(&:reverse).join('|')
x = s.reverse.gsub!(/#{a}/, '')
puts x == '' ? "YES" : "NO"


# Here your code !
n = gets.chomp
len = n.length
i = 0
flag = 0
if(n[0..(0+6)] == "erdream" or n[i..(i+7)] == "ereraser" or n[i..(i+6)] == "ererase")
    puts "NO"
    exit
end
while(i < len)
    if(n[i..(i+4)] == "dream")
        i+=5
        next
    elsif(n[i..(i+3)] == "eras")
        i+=4
        next
    elsif(n[i..(i+4)] == "erase")
        i+=5
        next
    elsif(n[i..(i+6)] == "erdream")
        i+=7
        next
    elsif(n[i..(i+6)] == "ererse")
        i+=7
        next
    elsif(n[i..(i+5)] == "ereras")
        i+=6
        next
    elsif(n[i..(i+6)] == "ererase")
        i+=7
        flag = 1
        next
    else
        next if(len - i == 2 and n[i..(i+1)]== "er" and flag == 0)
        puts "NO"
        exit
    end
    flag = 0
end
puts "YES"


S = gets.chomp
puts /^((dream)|(dreamer)|(erase)|(eraser))+$/ =~ S ? "YES" : "NO"


puts gets.chomp.gsub(/eraser/, "").gsub(/erase/, "").gsub(/dreamer/, "").gsub(/dream/, "").length>0 ? "NO" : "YES"


re = /(dreamer|eraser|dream|erase)\z/
s = gets.strip
while re =~ s
  i = $1.size
  s[-i, i] = ''
end
puts(s === '' ? 'YES' : 'NO')


s = gets.chomp
s.gsub!("dreameraser", "")
s.gsub!("dreamerase", "")
s.gsub!("dreamdream", "")
s.gsub!("dreamer", "")
s.gsub!("eraser", "")
s.gsub!("erase", "")
s.gsub!("dream", "")
if s.empty? then
  puts "YES"
else
  puts "NO"
end


s = gets.chomp
ok = true
while !s.empty? do
  if s.end_with?("dream")
    s.slice!(s.length - 5, 5)
  elsif s.end_with?("dreamer")
    s.slice!(s.length - 7, 7)
  elsif s.end_with?("erase")
    s.slice!(s.length - 5, 5)
  elsif s.end_with?("eraser")
    s.slice!(s.length - 6, 6)
  else
    ok = false
    break
  end
end
puts ok ? "YES" : "NO"


s=gets.chomp
t=[""]
(s.length).times do |i|
  t.length.times do |j|
    t[j]+=s[i]
    if t[j]=="dream"
      t<< ""
    elsif t[j]=="erase"
      t<< ""
    elsif t[j]=="dreamer"
      t[j]=""
    elsif t[j]=="eraser"
      t[j]=""
    elsif t[j][0]!=?d&&t[j][0]!=?e
      t[j]=nil
    elsif t[j].length==8
      t[j]=nil
    end
  end
  t.compact!
end
puts t.include?("")? :YES : :NO


@p = gets.chomp.split(" ").map{|i| i.to_f/100}
@q = gets.chomp.split(" ").map{|i| i.to_f/100}

def f(x)
  sum = 0
  for i in 0...6
    if x * @p[i] + (1-x) * @q[i] == 0
      next
    end
    sum += (x * @p[i] - (1-x) * @q[i]).abs / (x * @p[i] + (1-x) * @q[i])
  end
  return sum
end

def g(x)
  sum = 0
  for i in 0...6
    sum += [x * @p[i], (1-x) * @q[i]].max
  end
  return sum
end

kouho = [0, 1]

for i in 0...6
  kouho << @q[i]/(@p[i] + @q[i])
end

mini = 10000
mini_x = 0

kouho.each{|x|
  #puts f(x)
  if mini > f(x)
    mini = f(x)
    mini_x = x
  end
  mini = [mini, f(x)].min
}

# p mini_x

printf("%.12f\n", g(mini_x))

Score : 1000 points

Problem Statement

#nck {
width: 30px;
height: auto;
}

There are two (6-sided) dice: a red die and a blue die.
When a red die is rolled, it shows i with probability p_i percents, and when a blue die is rolled, it shows j with probability q_j percents.

Petr and tourist are playing the following game.
Both players know the probabilistic distributions of the two dice.
First, Petr chooses a die in his will (without showing it to tourist), rolls it, and tells tourist the number it shows.
Then, tourist guesses the color of the chosen die.
If he guesses the color correctly, tourist wins. Otherwise Petr wins.

If both players play optimally, what is the probability that tourist wins the game?

Constraints

0 ≤ p_i, q_i ≤ 100

p_1 + ... + p_6 = q_1 + ... + q_6 = 100

All values in the input are integers.

Input

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

p_1 p_2 p_3 p_4 p_5 p_6
q_1 q_2 q_3 q_4 q_5 q_6

Output

Print the probability that tourist wins.
The absolute error or the relative error must be at most 10^{-9}.

Sample Input 1

25 25 25 25 0 0
0 0 0 0 50 50

Sample Output 1

1.000000000000

tourist can always win the game: If the number is at most 4, the color is definitely red. Otherwise the color is definitely blue.

Sample Input 2

10 20 20 10 20 20
20 20 20 10 10 20

Sample Output 2

0.550000000000


require 'set'; require 'prime'
INF=Float::INFINITY; MOD=10**9+7
s = gets.chomp

color = s[0]
count = 1
s.each_char do |c|
  if color != c
    color = c
    count += 1
  end
end

if count == 1
  if s[0] == 'W'
    puts 0
  else
    puts 2
  end
else
  puts count - 1
end

Score : 300 points

Problem Statement

Two foxes Jiro and Saburo are playing a game called 1D Reversi. This game is played on a board, using black and white stones. On the board, stones are placed in a row, and each player places a new stone to either end of the row. Similarly to the original game of Reversi, when a white stone is placed, all black stones between the new white stone and another white stone, turn into white stones, and vice versa.

In the middle of a game, something came up and Saburo has to leave the game. The state of the board at this point is described by a string S. There are |S| (the length of S) stones on the board, and each character in S represents the color of the i-th (1 ≦ i ≦ |S|) stone from the left. If the i-th character in S is B, it means that the color of the corresponding stone on the board is black. Similarly, if the i-th character in S is W, it means that the color of the corresponding stone is white.

Jiro wants all stones on the board to be of the same color. For this purpose, he will place new stones on the board according to the rules. Find the minimum number of new stones that he needs to place.

Constraints

1 ≦ |S| ≦ 10^5

Each character in S is B or W.

Input

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

S

Output

Print the minimum number of new stones that Jiro needs to place for his purpose.

Sample Input 1

BBBWW

Sample Output 1

1

By placing a new black stone to the right end of the row of stones, all white stones will become black. Also, by placing a new white stone to the left end of the row of stones, all black stones will become white.

In either way, Jiro's purpose can be achieved by placing one stone.

Sample Input 2

WWWWWW

Sample Output 2

0

If all stones are already of the same color, no new stone is necessary.

Sample Input 3

WBWBWBWBWB

Sample Output 3

9


str   = gets.chomp.chars
count = 0
last  = str[0]

for i in 1 ... s.size
  if str[i] != last
    count += 1
    last   = str[i]
  end
end

puts count


s=gets.chomp

i, num=0, 0
while i<s.size
  c=s[i]
  while i<s.size and s[i]==c
    i+=1
  end
  num+=1
end

puts num-1


s = gets.chomp

step = 0
char = nil

for i in 0...s.size
  char = s[i] if char.nil?
  if char != s[i]
    char = s[i]
    step += 1
  end
end

puts step


# NとLを読み取る
input1 = gets.chomp
n = input1.split(" ")

# Nは受ける文字列の数、Lは文字の長さ
input2 = readlines(chomp: true).map(&:to_s)

puts input2.sort.join("")

Score : 200 points

Problem Statement

Iroha has a sequence of N strings S_1, S_2, ..., S_N. The length of each string is L.

She will concatenate all of the strings in some order, to produce a long string.

Among all strings that she can produce in this way, find the lexicographically smallest one.

Here, a string s=s_1s_2s_3...s_n is lexicographically smaller than another string t=t_1t_2t_3...t_m if and only if one of the following holds:

There exists an index i(1≦i≦min(n,m)), such that s_j = t_j for all indices j(1≦j<i), and s_i<t_i.

s_i = t_i for all integers i(1≦i≦min(n,m)), and n<m.

Constraints

1 ≦ N, L ≦ 100

For each i, the length of S_i equals L.

For each i, S_i consists of lowercase letters.

Input

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

N L
S_1
S_2
:
S_N

Output

Print the lexicographically smallest string that Iroha can produce.

Sample Input 1

3 3
dxx
axx
cxx

Sample Output 1

axxcxxdxx

The following order should be used: axx, cxx, dxx.


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

s = n.times.map{ gets.chomp }.sort

puts s.join


L, N = gets.split.map(&:to_i)
words = []

N.times{ words << gets.strip }

puts words.sort.join


L, N = gets.chomp.split.map(&:to_i)
words = []

N.times{ words << gets.chomp }

puts words.sort.join


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

words = []
N.times { words << gets.strip }

puts words.sort


L, N = gets.chomp.split.map(&:to_i)
S = N.times.map{ gets.to_s }

puts S.sort.reduce(:+)


n,l = gets.split(" ").map(&:to_i)
s = n.times.map{gets.chomp}.sort

puts s.join


num, length = gets.split(/\s/).map{ |str| str.to_i }
words = []
num.times{ words.push(gets.strip) }
puts words.sort.join


n, l = gets.split.map(&:to_i)
s = readlines.map(&:chomp)
puts(s.permutation(n).map(&:join).sort.first)