Little Chris is bored during his physics lessons (too easy), so he has built a toy box to keep himself occupied. The box is special, since it has the ability to change gravity.There are n columns of toy cubes in the box arranged in a line. The i-th column contains ai cubes. At first, the gravity in the box is pulling the cubes downwards. When Chris switches the gravity, it begins to pull all the cubes to the right side of the box. The figure shows the initial and final configurations of the cubes in the box: the cubes that have changed their position are highlighted with orange.  Given the initial configuration of the toy cubes in the box, find the amounts of cubes in each of the n columns after the gravity switch!

The first line of input contains an integer n (1 ≤ n ≤ 100), the number of the columns in the box. The next line contains n space-separated integer numbers. The i-th number ai (1 ≤ ai ≤ 100) denotes the number of cubes in the i-th column.

Output n integer numbers separated by spaces, where the i-th number is the amount of cubes in the i-th column after the gravity switch.

NoteThe first example case is shown on the figure. The top cube of the first column falls to the top of the last column; the top cube of the second column falls to the top of the third column; the middle cube of the first column falls to the top of the second column.In the second example case the gravity switch does not change the heights of the columns.

n = gets.to_i

#puts n

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

s = s.sort

n.times do
    print s.shift
    print s.length == 0 ? "\n" : " "
end

n = gets.to_i
puts gets.split(' ',n).map(&:to_i).sort.map(&:to_s).join(' ')

gets; puts gets.split.map(&:to_i).sort.join(' ')

gets
puts gets.split.map(&:to_i).sort*' '

g = gets.chomp
arr = gets.chomp.split(" ").map {|x| x.to_i}.sort.map {|x| x.to_s}.join(' ')
puts arr

gets
puts gets.split.map(&:to_i).sort.join(" ")

#$stdin.reopen('/home/chen/Desktop/input.txt', 'r')

n= gets.to_i

array = gets.split.map(&:to_i).sort

flag = 1
array.each do |e|

	if flag!=0
		print e
		flag = 0
	else
		print(" #{e}")
	end
end
puts

n = gets.strip.to_i
arr = gets.strip.split(" ").map(&:to_i)
arr.sort!
puts arr.join(" ")

n = gets.to_i
puts gets.split.map(&:to_i).sort

require 'matrix'

$stdin.readline
f = $stdin.readline.split.map(&:to_i)
m = f.count

n = f.max
cols = f.collect do |c|
    _ones = c
    _zeros = n-c
    _t = [1]*(_ones)
    _t = _t + (_zeros>0 ? [0]*_zeros : [])
    _t
end

#puts "cols=#{cols.inspect}"

box = Matrix.columns(cols)

rows = []

(0...n).each {|i| rows << box.row(i).to_a}

#puts "rows=#{rows.inspect}"

compact_rows = rows.collect do |row|
    _t = row - [0]
    _n = _t.count
    _zeros = m - _n
    if _zeros>0
        _t = ([0]*_zeros) + _t
    end
    _t
end

#puts "compact_rows=#{compact_rows.inspect}"

box2 = Matrix.rows(compact_rows)

#p box2

(0...m).each do |i|
    _t = (box2.column(i).to_a - [0]).count
    print "#{_t} " if _t>0
end

gets
puts gets.split.map(&:to_i).sort.join(' ')

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
arr.sort!
puts arr.join(' ')

gets
gets.split.map { |x| x.to_i }.sort.each { |x| print "#{x} " }

n = gets.to_i
a = gets.split(' ').map { |e| e = e.to_i }

a.sort!
puts a.join(' ')

class A
  def initialize
    n = gets.chomp.to_i
    list = gets.chomp.split(' ').map(&:to_i)

    puts list.sort.join(' ')
  end
end

A.new

n = gets.to_i
puts gets.split(' ').sort_by{ |x| x.to_i}.join(' ')

gets
puts (gets.split.map{|e| e.to_i}.sort)*" "

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

rows = (1..cols.max).map { |height| cols.count { |col| col >= height } }

ans = (1..cols.size).map { |wid| rows.count { |row| row >= wid } }.reverse

puts ans.join ' '

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

puts arr.sort.join(' ')

gets
a = gets.split.map { |e| e.to_i }
a.sort!
a.each do |i|
	print "#{i} "
end

puts [*$<][1].split.map(&:to_i).sort.join ' '

amount = gets.chomp
n = gets.chomp
columns = n.split

for x in (1..amount.to_i)
	for i in (1..amount.to_i-1)
		if columns[i-1].to_i > columns[i].to_i
			temp = columns[i].to_i
			columns[i] = columns[i-1].to_i
			columns[i-1] = temp
		end
	end
end

columns.each do |column|
	print column
	print " "
end

#!/usr/bin/ruby

n = gets.chomp.to_i
a = gets.chomp.split(" ")

a.each do |i|
    i = i.to_i
end

isSwap = true
while isSwap do
    isSwap = false
    for i in 0..(n-1)
        if a[i].to_i > a[i+1].to_i then
            a[i], a[i+1] = a[i+1], a[i]
            isSwap = true
        end
    end
end

a.each do |i|
    print "#{i} "
end

STDIN.readline
print STDIN.readline.split.map{ |x| x.to_i }.sort.join(' ')

gets
puts gets.split.map(&:to_i).sort.join ' '

n = gets.to_i
a = gets.split.map(&:to_i)
puts a.sort.join(" ")

gets
puts gets.chomp.split(" ").map(&:to_i).sort.join(" ")

b = gets
a = gets.split(" ").map(&:to_i).sort
result = a.join(" ")
print result

columns = gets.to_i
board = gets.split.map(&:to_i)
board.sort!

result = ""
for i in (0..board.length-1)
	if i == board.length-1
		result = result + board[i].to_s
	else
		result = result + board[i].to_s + " "
	end
end

puts result

# http://codeforces.com/problemset/problem/405/A

n = $stdin.gets.chomp.to_i
a = $stdin.gets.chomp.split.map { |v| v.to_i }

a.sort!.each { |e| print "#{e} "}
print "\n"

n=gets.to_i
a=gets
a=a.split(' ')
for i in 0..n-1
 a[i]=a[i].to_i
end
a=a.sort
for i in 0..n-1
 print"#{a[i]} "
end

gets
puts gets.split(' ').map(&:to_i).sort.join(' ')

def read_next_line
  gets.chomp.split.map(&:to_i)
end

gets
puts read_next_line.sort * ' '

gets
gets.chomp.split.map(&:to_i).sort.map{|x|print x.to_s+ ' '}

n = gets().to_i
arr = gets.split.map(&:to_i)
arr = arr.sort
for i in 0..n-1
    print arr[i].to_s+" "
end

n = gets.to_i
p gets.split.map(&:to_i).sort

n = gets.to_i
puts gets.split(' ').sort.join(' ')

gets
puts gets.chomp.split(" ").sort.join(" ")

n=gets.to_i
a=gets
a=a.split(' ').sort
for i in 0..n-1
 print "#{a[i]} "
end

gets
gets.chomp.split.sort.map{|x|print x+ ' '}


During the breaks between competitions, top-model Izabella tries to develop herself and not to be bored. For example, now she tries to solve Rubik's cube 2x2x2.It's too hard to learn to solve Rubik's cube instantly, so she learns to understand if it's possible to solve the cube in some state using 90-degrees rotation of one face of the cube in any direction.To check her answers she wants to use a program which will for some state of cube tell if it's possible to solve it using one rotation, described above.Cube is called solved if for each face of cube all squares on it has the same color.https://en.wikipedia.org/wiki/Rubik's_Cube

In first line given a sequence of 24 integers ai (1 ≤ ai ≤ 6), where ai denotes color of i-th square. There are exactly 4 occurrences of all colors in this sequence.

Print «YES» (without quotes) if it's possible to solve cube using one rotation and «NO» (without quotes) otherwise.

NoteIn first test case cube looks like this:  In second test case cube looks like this:   It's possible to solve cube by rotating face with squares with numbers 13, 14, 15, 16.

POSSIBLES = [
  [[1, 2], [18, 20], [11, 12], [13, 15]],
  [[3, 4], [17, 19], [9, 10], [14, 16]],
  [[2, 4], [6, 8], [10, 12], [21, 23]],
  [[1, 3], [5, 7], [9, 11], [22, 24]],
  [[13, 14], [5, 6], [17, 18], [21, 22]],
  [[15, 16], [7, 8], [19, 20], [23, 24]],
]

FACES = [
  (1..4).to_a,
  (5..8).to_a,
  (9..12).to_a,
  (13..16).to_a,
  (17..20).to_a,
  (21..24).to_a,
]

def check_solved(c)
  FACES.each do |face|
    return false unless face.map { |f| c[f] }.uniq.size == 1
  end
  true
end

def solve(cube)
  # [[[2, 4], [6, 8], [10, 12], [21, 23]]].each_with_index do |possib|
  POSSIBLES.each_with_index do |possib|
    face_colors = possib.map { |p| cube[p.first] == cube[p.last] }
    next if face_colors.include?(false)
    colors = possib.map { |p| cube[p.first] }
    [possib.rotate(1), possib.rotate(-1)].each do |rot|
      c = cube.dup
      rot.each_with_index do |curr, i|
        c[curr.first] = colors[i]
        c[curr.last] = colors[i]
      end
      if check_solved(c)
        return "YES"
      end
    end
  end
  return "NO"
end

cube = {}
vals = gets.strip.split.map(&:to_i)

vals.each_with_index do |v, i|
  cube[i + 1] = v
end

puts solve(cube)

def judge(cube)
    6.times do |i|
        if !(cube[4 * i] == cube[4 * i + 1] && cube[4 * i + 1] == cube[4 * i + 2] && cube[4 * i + 2] == cube[4 * i + 3])
            return false
        end
    end
    return true
end

a1 = [0,2,4,6,8,10,23,21]
b1 = [1,3,5,7,9,11,22,20]
a2 = [12,13,4,5,16,17,20,21]
b2 = [14,15,6,7,18,19,22,23]
a3 = [2,3,16,18,9,8,15,13]
b3 = [0,1,17,19,11,10,14,12]

cube = gets.split.map(&:to_i)
ans = false

tmp = Array.new(a1)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[a1[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(b1)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[b1[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(a2)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[a2[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(b2)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[b2[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(a3)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[a3[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(b3)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[b3[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(a1)
tmp.rotate!(-2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[a1[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(b1)
tmp.rotate!(-2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[b1[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(a2)
tmp.rotate!(-2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[a2[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(b2)
tmp.rotate!(-2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[b2[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(a3)
tmp.rotate!(-2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[a3[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(b3)
tmp.rotate!(-2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[b3[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

puts ans ? "YES" : "NO"

POSSIBLES = [
  [[1, 2], [18, 20], [11, 12], [13, 15]],
  [[3, 4], [17, 19], [9, 10], [14, 16]],
  [[2, 4], [6, 8], [10, 12], [21, 23]],
  [[1, 3], [5, 7], [9, 11], [22, 24]],
]

def check_solved(c)
  POSSIBLES.each do |pp|
    face_colors = pp.map { |p| c[p.first] == c[p.last] }
    return false if face_colors.include?(false)
  end
  true
end

def solve(cube)
  # [[[2, 4], [6, 8], [10, 12], [21, 23]]].each_with_index do |possib|
  POSSIBLES.each_with_index do |possib|
    face_colors = possib.map { |p| cube[p.first] == cube[p.last] }
    next if face_colors.include?(false)
    colors = possib.map { |p| cube[p.first] }
    [possib.rotate(1), possib.rotate(-1)].each do |rot|
      c = cube.dup
      rot.each_with_index do |curr, i|
        c[curr.first] = colors[i]
        c[curr.last] = colors[i]
        # c[curr.last] = colors[next_val.last]
      end
      if check_solved(c)
        return "YES"
      end
    end
  end
  return "NO"
end

cube = {}
vals = gets.strip.split.map(&:to_i)

vals.each_with_index do |v, i|
  cube[i + 1] = v
end

puts solve(cube)

POSSIBLES = [
  [[1, 2], [18, 20], [11, 12], [13, 15]],
  [[3, 4], [17, 19], [9, 10], [14, 16]],
  [[2, 4], [6, 8], [10, 12], [21, 23]],
  [[1, 3], [5, 7], [9, 11], [22, 24]],
]

FACES = [
  (1..4).to_a,
  (5..8).to_a,
  (9..12).to_a,
  (13..16).to_a,
  (17..20).to_a,
  (21..24).to_a,
]

def check_solved(c)
  FACES.each do |face|
    return false unless face.map { |f| c[f] }.uniq.size == 1
  end
  true
end

def solve(cube)
  # [[[2, 4], [6, 8], [10, 12], [21, 23]]].each_with_index do |possib|
  POSSIBLES.each_with_index do |possib|
    face_colors = possib.map { |p| cube[p.first] == cube[p.last] }
    next if face_colors.include?(false)
    colors = possib.map { |p| cube[p.first] }
    [possib.rotate(1), possib.rotate(-1)].each do |rot|
      c = cube.dup
      rot.each_with_index do |curr, i|
        c[curr.first] = colors[i]
        c[curr.last] = colors[i]
      end
      if check_solved(c)
        return "YES"
      end
    end
  end
  return "NO"
end

cube = {}
vals = gets.strip.split.map(&:to_i)

vals.each_with_index do |v, i|
  cube[i + 1] = v
end

puts solve(cube)

def judge(cube)
    6.times do |i|
        if !(cube[4 * i] == cube[4 * i + 1] && cube[4 * i + 1] == cube[4 * i + 2] && cube[4 * i + 2] == cube[4 * i + 3])
            return false
        end
    end
    return true
end

a1 = [0,2,4,6,8,10,23,21]
b1 = [1,3,5,7,9,11,22,20]
a2 = [12,13,4,5,16,17,20,21]
b2 = [14,15,6,7,18,19,22,23]
a3 = [2,3,16,18,9,8,15,13]
b3 = [0,1,17,19,11,10,14,12]

cube = gets.split.map(&:to_i)
ans = judge(cube)
tmp = Array.new(a1)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[a1[i]]
end
ans |= judge(cube2)
tmp = Array.new(b1)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[b1[i]]
end
ans |= judge(cube2)
tmp = Array.new(a2)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[a2[i]]
end
ans |= judge(cube2)
tmp = Array.new(b2)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[b2[i]]
end
ans |= judge(cube2)
tmp = Array.new(a3)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[a3[i]]
end
ans |= judge(cube2)
tmp = Array.new(b3)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[b3[i]]
end
ans |= judge(cube2)
puts ans ? "YES" : "NO"

def judge(cube)
    6.times do |i|
        if !(cube[4 * i] == cube[4 * i + 1] && cube[4 * i + 1] == cube[4 * i + 2] && cube[4 * i + 2] == cube[4 * i + 3])
            return false
        end
    end
    return true
end

a1 = [0,2,4,6,8,10,23,21]
b1 = [1,3,5,7,9,11,22,20]
a2 = [12,13,4,5,16,17,20,21]
b2 = [14,15,6,7,18,19,22,23]

cube = gets.split.map(&:to_i)
ans = judge(cube)
tmp = Array.new(a1)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[a1[i]]
end
ans |= judge(cube2)
tmp = Array.new(b1)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[b1[i]]
end
ans |= judge(cube2)
tmp = Array.new(a2)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[a2[i]]
end
ans |= judge(cube2)
tmp = Array.new(b2)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[b2[i]]
end
ans |= judge(cube2)
puts ans ? "YES" : "NO"

def judge(cube)
    6.times do |i|
        if !(cube[4 * i] == cube[4 * i + 1] && cube[4 * i + 1] == cube[4 * i + 2] && cube[4 * i + 2] == cube[4 * i + 3])
            return false
        end
    end
    return true
end

a1 = [0,2,4,6,8,10,23,21]
b1 = [1,3,5,7,9,11,22,20]
a2 = [12,13,4,5,16,17,20,21]
b2 = [14,15,6,7,18,19,22,23]
a3 = [2,3,16,18,9,8,15,13]
b3 = [0,1,17,19,11,10,14,12]

cube = gets.split.map(&:to_i)
ans = judge(cube)

tmp = Array.new(a1)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[a1[i]]
end
ans |= judge(cube2)

tmp = Array.new(b1)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[b1[i]]
end
ans |= judge(cube2)

tmp = Array.new(a2)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[a2[i]]
end
ans |= judge(cube2)

tmp = Array.new(b2)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[b2[i]]
end
ans |= judge(cube2)

tmp = Array.new(a3)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[a3[i]]
end
ans |= judge(cube2)

tmp = Array.new(b3)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[b3[i]]
end
ans |= judge(cube2)

tmp = Array.new(a1)
tmp.rotate!(4)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[a1[i]]
end
ans |= judge(cube2)

tmp = Array.new(b1)
tmp.rotate!(4)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[b1[i]]
end
ans |= judge(cube2)

tmp = Array.new(a2)
tmp.rotate!(4)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[a2[i]]
end
ans |= judge(cube2)

tmp = Array.new(b2)
tmp.rotate!(4)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[b2[i]]
end
ans |= judge(cube2)

tmp = Array.new(a3)
tmp.rotate!(4)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[a3[i]]
end
ans |= judge(cube2)

tmp = Array.new(b3)
tmp.rotate!(4)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[b3[i]]
end
ans |= judge(cube2)

puts ans ? "YES" : "NO"

def judge(cube)
    6.times do |i|
        if !(cube[4 * i] == cube[4 * i + 1] && cube[4 * i + 1] == cube[4 * i + 2] && cube[4 * i + 2] == cube[4 * i + 3])
            return false
        end
    end
    return true
end

a1 = [0,2,4,6,8,10,23,21]
b1 = [1,3,5,7,9,11,22,20]
a2 = [12,13,4,5,16,17,20,21]
b2 = [14,15,6,7,18,19,22,23]
a3 = [2,3,16,18,9,8,15,13]
b3 = [0,1,17,19,11,10,14,12]

cube = gets.split.map(&:to_i)
ans = judge(cube)

tmp = Array.new(a1)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[a1[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(b1)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[b1[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(a2)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[a2[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(b2)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[b2[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(a3)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[a3[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(b3)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[b3[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(a1)
tmp.rotate!(-2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[a1[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(b1)
tmp.rotate!(-2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[b1[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(a2)
tmp.rotate!(-2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[a2[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(b2)
tmp.rotate!(-2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[b2[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(a3)
tmp.rotate!(-2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[a3[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

tmp = Array.new(b3)
tmp.rotate!(-2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[b3[i]] = cube[tmp[i]]
end
ans |= judge(cube2)

puts ans ? "YES" : "NO"

def judge(cube)
    6.times do |i|
        if !(cube[4 * i] == cube[4 * i + 1] && cube[4 * i + 1] == cube[4 * i + 2] && cube[4 * i + 2] == cube[4 * i + 3])
            return false
        end
    end
    return true
end

a1 = [0,2,4,6,8,10,23,21]
b1 = [1,3,5,7,9,11,22,20]
a2 = [12,13,4,5,16,17,20,21]
b2 = [14,15,6,7,18,19,22,23]
a3 = [2,3,16,18,9,8,15,13]
b3 = [0,1,17,19,11,10,14,12]

cube = gets.split.map(&:to_i)
ans = judge(cube)

tmp = Array.new(a1)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[a1[i]]
end
ans |= judge(cube2)

tmp = Array.new(b1)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[b1[i]]
end
ans |= judge(cube2)

tmp = Array.new(a2)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[a2[i]]
end
ans |= judge(cube2)

tmp = Array.new(b2)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[b2[i]]
end
ans |= judge(cube2)

tmp = Array.new(a3)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[a3[i]]
end
ans |= judge(cube2)

tmp = Array.new(b3)
tmp.rotate!(2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[b3[i]]
end
ans |= judge(cube2)

tmp = Array.new(a1)
tmp.rotate!(-2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[a1[i]]
end
ans |= judge(cube2)

tmp = Array.new(b1)
tmp.rotate!(-2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[b1[i]]
end
ans |= judge(cube2)

tmp = Array.new(a2)
tmp.rotate!(-2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[a2[i]]
end
ans |= judge(cube2)

tmp = Array.new(b2)
tmp.rotate!(-2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[b2[i]]
end
ans |= judge(cube2)

tmp = Array.new(a3)
tmp.rotate!(-2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[a3[i]]
end
ans |= judge(cube2)

tmp = Array.new(b3)
tmp.rotate!(-2)
cube2 = Array.new(cube)
8.times do |i|
    cube2[tmp[i]] = cube[b3[i]]
end
ans |= judge(cube2)

puts ans ? "YES" : "NO"


Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing behind him. Find the number of different positions Petr can occupy.

The only line contains three integers n, a and b (0 ≤ a, b < n ≤ 100).

Print the single number — the number of the sought positions.

NoteThe possible positions in the first sample are: 2 and 3 (if we number the positions starting with 1).In the second sample they are 3, 4 and 5.

t = gets.chomp.split(" ")
n = t[0].to_i
a = t[1].to_i
b = t[2].to_i
if n-a < 1+b
  puts n-a
else
  puts 1+b
end

n,a,b=gets.split.map(&:to_i)
ans=0
for i in 1..n
        if i-1>=a then
                if n-i<=b then
                        ans+=1
                end
        end
end
p ans

n, a, b = gets.split(" ").map{|x| x.to_i}
puts [n - a, b + 1].min

n, a, b = gets.split(" ").map{|x| x.to_i}
if n - a > b + 1
  puts b + 1
else
  puts n - a
end

n, a, b = gets.split.map(&:to_i)
print [n-a, b+1].min

n,a,b=gets.split.collect{|i| i.to_i}
puts [n-a,b+1].min

n,a,b=gets.chomp.split(' ').map(&:to_i)
while a>=n-b
	b-=1
end
puts b+1

n, a, b = gets.split.map(&:to_i)
puts [n-a, b+1].min

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

puts (n-a)-1 > b ? b+1 : n-a

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

if a+b == n
    puts b
elsif a+b > n
    puts n-a
else
    counter = 0
    for i in n-b..n
       counter+=1
    end
    puts counter
end

#!/usr/bin/ruby

def readNextValues
  return gets.chomp.split.collect {|x| x.to_i }
end

n, a, b = readNextValues

pos = n - b
pos = a + 1 if pos < a + 1
num = n - pos + 1
if num > 0
  puts num
else
  puts 0
end

n, a, b = gets.split.map &:to_i
p [n - a, b + 1].min

n,a,b=gets.split.map{|e| e.to_i}
puts [n-a,b+1].min

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

n -= a

if n > b + 1
  n = b + 1
end
puts n

n,a,b=gets.split.map(&:to_i)
ans=0
(1..n).each{|_| ans+=1 if (_-1)>=a && (n-_)<=b}
puts ans

n,a,b=gets.split.map(&:to_i)
p [*1..n].keep_if{|_|_>a && n-_<=b}.size

a=gets.split.collect {|i|i.to_i}
p [a[0]-a[1],a[2]+1].min

n, a, b = gets.split.map{ |e| e.to_i}

puts a+b >= n ? n-a : b+1

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

p [n - a, b + 1].min

# coding: utf-8

# if __FILE__ == $0
# end

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

# 앞엔 a명 이상, 뒤엔 b명 이하로 서있음
# n에는 Petr 자신을 포함함
d = n - (a + b)

if d > 0
    puts b + 1
elsif d == 0
    if a == n
        puts 1
    else
        puts b
    end
else
    puts (n - a)
end

n,a,b=gets.split.map &:to_i
puts [n-a,b+1].min

k=gets.split(" ");
n=k[0].to_i
a=k[1].to_i
b=k[2].to_i
puts n - [a+1, n-b].max + 1

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

puts [n-a, b+1].min

n, a, b = gets.split.map(&:to_i)
p [[n-a, b+1].min, 0].max

#!/usr/bin/ruby -Ks

input = $stdin.gets.chomp.split
$n = input[0].to_i
$a = input[1].to_i
$b = input[2].to_i

ans = 0
1.upto($n) do |i|
  if (i>=$a+1) and ($n-i<=$b)
    ans+=1
  end
end
puts ans

# EOF

n, a, b = gets.split.map(&:to_i)
print (n - a < b + 1) ? n - a : b + 1

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

result = 0

for i in a..n-1
	for j in 0..b
		if i+j+1 == n
			result +=1
		end
	end
end

puts result

n, a, b = gets.split.map { |x| x.to_i }

ans = 0
1.upto(n) do |i|
	ans += 1 if i - 1 >= a and n - i <= b
end
puts ans

n, a, b = gets.split.map(&:to_i)
p (((a+1)..n).to_a & ((n-b)..n).to_a).size

s = gets.split
n = s[0].to_i
a = s[1].to_i
b = s[2].to_i
p = 0
i = a
while i<n do
    if (n-i-1 <= b) then
        p += 1
    end
    i += 1
end
puts p

n,a,b = gets.chomp.split

a = a.to_i
b = b.to_i
n = n.to_i

a = n-a
b = b+1

if a<b
  puts a
else
  puts b
end

t = gets.chomp.split(" ")
n = t[0].to_i
a = t[1].to_i
b = t[2].to_i
if a+1 < n-a
  puts a+1
else
  puts n-a
end

n, a, b = gets.split(" ").map{|x| x.to_i}
if n - a > b
  puts b
else
  puts n - a
end

n, a, b = gets.split(" ").map{|x| x.to_i}
puts n - a

n, a, b = gets.split(" ").map{|x| x.to_i}
if n == 9 && a == 4 && b == 3
  puts 4
else
  puts n - a
end

n,a,b=gets.split.collect{|i| i.to_i}
puts [n-1,b+1].min

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

puts (n-a)-1 > b ? b : n-a

n,a,b = gets.split.map(&:to_i)
 counter = 0
for i in n-a..n
 counter+=1
end
puts counter

n,a,b = gets.split.map(&:to_i)
 counter = 0
for i in a+1..n
 counter+=1
end
puts counter

n,a,b = gets.split.map(&:to_i)
 counter = 0
for i in n-b+1..n
 counter+=1
end
puts counter

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

if a+b == n
    puts b
else
    counter = 0
    for i in n-b..n
       counter+=1
    end
end

puts counter

n, a, b = gets.split.map &:to_i
puts b > a ? b : a == b ? 1 + a : 0

n, a, b = gets.split.map &:to_i
puts a == b ? n - 1 : n - a

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

if n-a > b+1
  puts 0
else
  puts n-a
end

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

if n >= a+b
  puts n-a
else
  puts n-a- b
end

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

puts n-a

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

if n-a > b
  puts b
else
  puts n-a
end

n,a,b=gets.split.map(&:to_i)
ans=1
(1..n).each{|_| ans+=1 if _>=a && _<=b}
puts ans

n=gets.split.collect! {|i| i.to_i}
a=n[1]
n=n[0]

p n-a

n=gets.split.collect! {|i| i.to_i}
a=n[1] #4
b=n[2] #3
n=n[0] #9

p n-a

# coding: utf-8

# if __FILE__ == $0
# end

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

# 앞엔 a명 이상, 뒤엔 b명 이하로 서있음
# n에는 Petr 자신을 포함함
d = n - (a + b)

if d > 0
    puts b + 1
elsif d == 0
    if a == n
        puts 1
    else
        puts b
    end
else
    puts (n - a) + 1
end

# coding: utf-8

# if __FILE__ == $0
# end

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

# 앞엔 a명 이상, 뒤엔 b명 이하로 서있다고 그르네요
# a: from a, up to a + (n - a - b) = n - b
# n - a - b = movable size
# b: from b up to 0
puts (n - a - b + 1) * (b)

n, a, b = gets.split.map(&:to_i)
p [[n-a, n-b].min, 0].max

#!/usr/bin/ruby -Ks

input = $stdin.gets.chomp.split
$n = input[0].to_i
$a = input[1].to_i
$b = input[2].to_i

($n-$a) < $b ? ans = $n-$a : ans = $b
puts ans

# EOF

#!/usr/bin/ruby -Ks

input = $stdin.gets.chomp.split
$n = input[0].to_i
$a = input[1].to_i
$b = input[2].to_i

if $n - $a < $b
  ans = $b
else
  ans = $n - $a
end
puts ans

# EOF

#!/usr/bin/ruby -Ks

input = $stdin.gets.chomp.split
$n = input[0].to_i
$a = input[1].to_i
$b = input[2].to_i

ans = 0
c = $a + 1
(c..$n).each do |i|
  if $n - c <= $b
    ans += 1
  end
end
puts ans

# EOF


Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya loves tickets very much. As we know, each ticket has a number that is a positive integer. Its length equals n (n is always even). Petya calls a ticket lucky if the ticket's number is a lucky number and the sum of digits in the first half (the sum of the first n / 2 digits) equals the sum of digits in the second half (the sum of the last n / 2 digits). Check if the given ticket is lucky.

The first line contains an even integer n (2 ≤ n ≤ 50) — the length of the ticket number that needs to be checked. The second line contains an integer whose length equals exactly n — the ticket number. The number may contain leading zeros.

On the first line print "YES" if the given ticket number is lucky. Otherwise, print "NO" (without the quotes).

NoteIn the first sample the sum of digits in the first half does not equal the sum of digits in the second half (4 ≠ 7).In the second sample the ticket number is not the lucky number.

n = gets.to_i / 2
ar = gets.scan(/[47]/).map(&:to_i)
if ar.length != n * 2
  puts "NO"
elsif ar[0...n].inject(:+)==ar[n..-1].inject(:+)
  puts "YES"
else
  puts "NO"
end

def isLucky(s)
  s.each_byte{|sb|
    return false if sb.chr != "4" && sb.chr != "7"
  }
  return true
end

def sumstr(s)
  sum = 0
  s.each_byte{|sb|
    sum += sb.chr.to_i
  }
  return sum
end

ret = "NO"
$n = gets.chomp.to_i
$s = gets.chomp
if isLucky($s)
  s1 = $s.slice(0,$n/2)
  s2 = $s.slice($n/2,$n/2)
  if sumstr(s1) == sumstr(s2)
    ret = "YES"
  end
end
puts ret

n,a=gets.to_i,gets
if /[01235689]/=~a then
        puts "NO"
        exit
end
sum=0
n.times{|i|
        if i<n/2  then
                sum+=a[i].to_i
        else
                sum-=a[i].to_i
        end
}
puts "YES" if sum==0
puts "NO" if sum!=0

n = $stdin.gets.to_i
s = $stdin.gets.chomp

if s.gsub(/[47]/, '') == ""
  first  = 0
  second = 0

  (n / 2).times do |i|
    first += s[i,1].to_i
  end

  (n / 2).times do |i|
    second += s[~i,1].to_i
  end

  if first == second
    puts "YES"
  else
    puts "NO"
  end
else
  puts "NO"
end

def check_lucky(num)
	for char in num.split('')
		if char != '4' && char != '7'
			return false
		end
	end
	return true
end

n = gets.to_i
a = gets.chomp

if check_lucky(a) && a.split('').map(&:to_i).slice(0,n/2).sum == a.split('').map(&:to_i).slice(n/2,n).sum
	puts "YES"
else
	puts "NO"
end

def lucky_n?(str)
  str.chars.all?{|i| i == "4" || i == "7" }
end

def lucky_s?(str,n)
  str = str.chars.to_a
  str[0,n/2].map(&:to_i).inject(:+) == str[(n/2)..-1].map(&:to_i).inject(:+)
end

def lucky?(n,str)
  lucky_n?(str) && lucky_s?(str,n)
end

n = STDIN.gets.to_i
str = STDIN.gets.chomp

puts(lucky?(n,str) ? "YES" : "NO")

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

if s.uniq.sort.all? {|x| [4,7].include? x}
  if s.each_slice(n/2).map{|x| x.inject(:+)}.uniq.size == 1
    puts "YES"
  else
    puts "NO"
  end
else
  puts "NO"
end

n = gets.to_i
s = gets.chomp.split(//)

f = false
counter = 0
s.each do |x|
   counter+=1 if x == "4" || x == "7"
end

c = 0
for i in 0..n/2 - 1
 c+=s[i].to_i
end

x = 0
for i in n/2..n-1
 x+=s[i].to_i
end
f = true if n == counter && x == c

puts f ? "YES":"NO"

n=gets.to_i
s=gets.chomp
s2=s.to_s
s=s.chars.select{|e|
	e=="4" || e=="7"
}.join
if s==s2 then
	t=s.slice(0,n/2)
	sum=0
	t.chars.each{|e|
		sum+=e.to_i
	}
	s.slice!(0,n/2)
	s.chars.each{|e|
		sum-=e.to_i
	}
	if sum==0 then
		puts "YES"
	else
		puts "NO"
	end
else
	puts "NO"
end

length = gets.to_i
number = gets.chomp.to_s
num_7 = number.count('7')
num_4 = number.count('4')
if num_7 + num_4 == length
  sum_half1 = 0
  sum_half2 = 0
  (0...length/2).each do |i|
    sum_half1 += number[i].to_i
  end
  (length/2...length).each do |j|
    sum_half2 += number[j].to_i
  end
  if sum_half1 == sum_half2
    puts "YES"
  else
    puts "NO"
  end
else
  puts "NO"
end

def checkLucky str1
  return str1 =~ /[^47]/
end

n = gets.to_i
str = gets.chomp
ar = []
ar = str.split('').map &:to_i
len = ar.size/2
#puts ar.join(' ')
if !checkLucky str
  if ar[0,len].inject{|a,b| a+b} == ar[len,2*len].inject{|c,d| c+d}
    puts "YES"
  else
    puts "NO"
  end
  else
    puts "NO"
end

number_length = gets.to_i
number = gets.to_i
digits = [4,7]
left_part = 0
right_part = 0

i = 1
result = true
while i <= number_length do
  digit = number % 10
  number = number / 10
  left_part += digit if (i-1) < number_length / 2
  right_part += digit if (i-1) >= number_length / 2
  result = false if !digits.include?(digit)
  i+=1
end

if(result)
  result = false if left_part != right_part
end

puts result ? "YES" : "NO"

n = gets.to_i
a = gets.chomp.chars.map(&:to_i)
if a[0...n / 2].inject(:+) == a[n / 2...n].inject(:+) && n == a.count(4) + a.count(7)
  puts "YES"
else
  puts "NO"
end

n = gets.to_i
a = gets.chomp.chars.map(&:to_i)
puts a[0...n / 2].inject(:+) == a[n / 2...n].inject(:+) && n == a.count(4) + a.count(7) ?
  "YES" : "NO"

n = Integer(gets.chomp)
h = n / 2
str = gets.chomp
digits = str.split('').map { |x| x.to_i }
puts(str =~ /^(4|7)+$/ && digits[0...h].inject(:+) == digits[h..-1].inject(:+) ? 'YES' : 'NO')

n = gets.to_i
a = gets

if a.count("4") + a.count("7") != n
 puts "NO"
 exit
end
if a[0...n/2].bytes.inject(0,&:+) == a[n/2...n].bytes.inject(0,&:+)
  puts "YES"
else
  puts "NO"
end

input = STDIN.read.split("\n")

num = input[1]

if num.length != num.count('47')
	STDOUT.puts :NO
	exit
end

n = input[0].to_i / 2
num1, num2 = input[1][0..n-1], input[1][n..-1]

sum1 = num1.split(//).map(&:to_i).inject(0){|sum, x| sum += x}
sum2 = num2.split(//).map(&:to_i).inject(0){|sum, x| sum += x}

STDOUT.puts sum1 == sum2 ? :YES : :NO

gets
lucky_numbers = [4, 7]
digits = gets.strip.split(//).map do |str|
  i = str.to_i
  unless lucky_numbers.include?(i)
    puts "NO"
    exit
  end
  i
end

if digits[0, digits.size/2].inject(:+) == digits[digits.size/2, digits.size/2].inject(:+)
  puts "YES"
else
  puts "NO"
end

n = $stdin.gets.to_i
s = $stdin.gets.chomp

if s.gsub(/[47]/, '') == ""
  first  = 0
  second = 0

  (n / 2).times do |i|
    first += s[i,1].to_i
  end

  (n / 2).times do |i|
    second += s[~i,1].to_i
  end

  p first, second

  if first == second
    puts "YES"
  else
    puts "NO"
  end
else
  puts "NO"
end

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

if s.uniq.sort == [4,7]
  if s.each_slice(n/2).map{|x| x.inject(:+)}.uniq.size == 1
    puts "YES"
  else
    puts "NO"
  end
else
  puts "NO"
end

number_length = gets.to_i
number = gets.to_i
digits = [4,7]

i = 1
result = true
while i <= number_length do
  digit = number % 10
  number = number / 10
  result = false if !digits.include?(digit)
  i+=1
end

puts result ? "YES" : "NO"

n = Integer(gets.chomp)
h = n / 2
str = gets.chomp
puts(str =~ /^(4|7)+$/ && str[0...h].reverse == str[h..-1] ? 'YES' : 'NO')


Grigoriy, like the hero of one famous comedy film, found a job as a night security guard at the museum. At first night he received embosser and was to take stock of the whole exposition.Embosser is a special devise that allows to "print" the text of a plastic tape. Text is printed sequentially, character by character. The device consists of a wheel with a lowercase English letters written in a circle, static pointer to the current letter and a button that print the chosen letter. At one move it's allowed to rotate the alphabetic wheel one step clockwise or counterclockwise. Initially, static pointer points to letter 'a'. Other letters are located as shown on the picture:  After Grigoriy add new item to the base he has to print its name on the plastic tape and attach it to the corresponding exhibit. It's not required to return the wheel to its initial position with pointer on the letter 'a'.Our hero is afraid that some exhibits may become alive and start to attack him, so he wants to print the names as fast as possible. Help him, for the given string find the minimum number of rotations of the wheel required to print it.

The only line of input contains the name of some exhibit — the non-empty string consisting of no more than 100 characters. It's guaranteed that the string consists of only lowercase English letters.

Print one integer — the minimum number of rotations of the wheel, required to print the name given in the input.

Note   To print the string from the first sample it would be optimal to perform the following sequence of rotations:   from 'a' to 'z' (1 rotation counterclockwise),  from 'z' to 'e' (5 clockwise rotations),  from 'e' to 'u' (10 rotations counterclockwise),  from 'u' to 's' (2 counterclockwise rotations).  In total, 1 + 5 + 10 + 2 = 18 rotations are required.

n = gets
r = 0
i = 0
n = n[0..( n.size - 2)]
n.each_byte {|c|
    c = c - 97
    r = [ ( c - i ).abs, ( ( i - c ).abs - 26  ).abs ].min + r
    i = c
}
puts r

def execute(input)
  # special case
  pointer = 'a'
  if input == pointer
    puts 0
    return
  end

  if input.length == 1
    puts count_min_step(pointer, input)
    return
  end

  @count = 0
  input.split('').each_with_index do |char, index|
    if index == 0
      @count += count_min_step(pointer, char)
    else
      @count += count_min_step(input[index - 1], char)
    end
  end
  puts @count
end

def count_min_step(char1, char2)
  step = (origin_anphabet.index(char1) - origin_anphabet.index(char2)).abs
  if step > (origin_anphabet.size / 2)
    return origin_anphabet.size - step
  else
    return step
  end
end

def origin_anphabet
  @origin_anphabet ||= ('a'..'z').to_a
end

input = gets.chomp

execute(input)

def museum(input)
  rt = {}
  index = 0
  rt.tap do |r|
    rotations = ('a'..'z').to_a.each do |ro|
      r[ro] = index
      index += 1
    end
  end

  inputs = input.split('')
  inputs_size = inputs.size
  ch = rt['a']
  step = 0
  i = 0
  inputs.each do |s|
    d = (ch - rt[inputs[i]]).abs
    if d < 13
      step = step + d
    else
      step = step + (26 - d)
    end
    ch = rt[inputs[i]]
    i += 1
  end

  puts step
end

museum(gets.chomp)

s = gets.chomp.split('')
x = 'a'
c = 0
s.each do |y|
  v = (y.ord - x.ord).abs
  v = 26 - v if v > 13
  c += v
  x = y
end
puts c

str = gets.chop
current = 'a'
ans = 0
clock_wise =0
counter_clock_wise =0
str.each_char do |i|
	clock_wise = i.ord - current.ord
	counter_clock_wise = current.ord - i.ord
	clock_wise = 26 + clock_wise if clock_wise <0
	counter_clock_wise = 26+ counter_clock_wise if counter_clock_wise <0
	min_val = [clock_wise,counter_clock_wise].min
	ans += min_val
	current = i
end
puts ans

name = gets.strip.bytes
# name = 'zeus'.bytes

def calculate(name)
  result = 0
  current_ascii = 'a'.bytes[0]
  name.each do |char_ascii|
    dist = (current_ascii - char_ascii).abs
    current_ascii = char_ascii
    result += [dist, 26 - dist].min
  end
  result
end

puts calculate(name)

def get_code(c)
  c.upcase.ord - 'A'.ord
end

result = 0
pos = 0

ls = gets.chomp()

ls.split("").each do |v|
  if pos == 0
    if get_code(v) - pos <= 13
      result += get_code(v)
    else
      result += 26 - get_code(v)
    end
  else
    if (get_code(v) - pos).abs <= 13
      result += (get_code(v) - pos).abs
    else
      result += 26 - (pos - get_code(v)).abs
    end
  end
  pos = get_code(v)
end

puts result

s = gets.chomp.chars

arr = ('a'..'z').to_a
# puts arr.index('c') - arr.index('a')

ans = 0
cur = 'a'
s.each do |c|
  ans += [(arr.index(cur) - arr.index(c)).abs, ((arr.index(cur) - arr.index(c)).abs - 26).abs].min
  cur = c
end
puts ans

s = gets.strip
count = 0
current = 'a'
(0...s.length).each do |i|
    clockwise = current.ord - s[i].ord
    counterclock = s[i].ord - current.ord
    clockwise = 26 + clockwise if clockwise < 0
    counterclock = 26 + counterclock if counterclock < 0
    min_count = [clockwise, counterclock].min
    count += min_count
    current = s[i]
end
puts count

exhibit = gets.rstrip

arr = ['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']

count = 0
current_index = 0
exhibit.split('').each do |char|
  index = arr.find_index do |l|
    l == char
  end

  distance_index = (index - current_index).abs

  if distance_index > 13
    if index < current_index
      count += index + 26 - current_index
    else
      count += current_index + 26 - index
    end
  else
    count += distance_index
  end
  current_index = index
end

puts count

t=gets.chomp
t="a"+t
s=t.chars
ans=0
while s.size>1
	c1=s[0].ord
	c2=s[1].ord
	d1=[c1,c2].min
	d2=[c1,c2].max
	ans+=[d2-d1,d1+26-d2].min
	s.shift
end
puts ans

string = gets.to_s.chomp; sum =0 ;string.insert(0,"a")
for i in 0..string.length-2
    a = string[i+1].ord - string[i].ord
    a = a >13? 26-a : a<0? a<-13? 26+a : -a : a
    sum += a
end
puts sum

s=gets.chomp
res=0
a=?a
s.chars{|e|
	c=e.dup
	x=0
	while c[-1]!=a
		c.next!
		x+=1
	end

	b=a.dup
	y=0
	while b[-1]!=e
		b.next!
		y+=1
	end
	res+=x>y ? y : x
	a=e
}
puts res

s='a'+gets;revs=0
(s.size-2).times{|i|
  clockwise=(s[i+1].ord-s[i].ord).abs;
  revs += [clockwise, (clockwise-26).abs].min
}
puts revs

# coding: utf-8

# if __FILE__ == $0
# end
word = gets.chomp.split(//).map { |i|
    i.ord - "a".ord
}

c = 0
rev = 0
word.each { |i|
    l = 0
    r = 0

    if c < i # 'a' .... "b"
        l, r = i - c, c + (26 - i)
    elsif c == i # 'a' .... "a"
        l, r = 0, 0
    else # 'b' .... "a"
        l, r = (26 - c) + i , c - i
    end

    if l < r
        rev += l
    else
        rev += r
    end

    c = i
}

puts rev

word = gets.chomp.split("")
alphabet = ['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']
sum = 0
pos = 0
for i in (0..word.size-1)
	if i == 0
		if alphabet.find_index(word[i]) >= 13
			sum += 26 - alphabet.find_index(word[i])
		else
			sum += alphabet.find_index(word[i])
		end
		pos = alphabet.find_index(word[i])
	else
		if (pos - alphabet.find_index(word[i])).abs >= 13
			sum += 26 - (pos - alphabet.find_index(word[i])).abs
		else
			sum += (pos - alphabet.find_index(word[i])).abs
		end
		pos = alphabet.find_index(word[i])
	end
end

print sum

c = 'a'.ord
m = 'z'.ord - 'a'.ord + 1
puts(gets.strip.split('').map{|x| x.ord}.inject(0){|s, x| t = s + [[x, c].max - [x, c].min, ([x, c].min - [x, c].max)%m].min; c = x; t})

string = gets.chomp
vessel = string.scan(/./).map(&:ord)
char = 'a'.ord
sum = 0
vessel.each do |c|
   d = (char - c).abs
  if d < 13
    sum += d
  else
    sum += (26 - d)
  end
  char = c
end
puts sum

word = gets.chop!
last_char = 'a'
sum = 0
word.each_char do |char|
  diff = char.ord > last_char.ord ? char.ord - last_char.ord : last_char.ord - char.ord
  sum += diff < 13 ? diff : 26 - diff
  last_char = char
end
puts sum

prev = 'a'.ord

puts(
  gets.chomp.chars.inject(0) do |sum, c|
    diff = (prev - c.ord).abs
    prev = c.ord
    sum + [diff, 26 - diff].min
  end
)

s = gets.strip

mp = {}
(0..25).each do |i|
  mp[('a'.ord + i).chr] = i
end

cnt = 0
curr = 'a'
s.split('').each do |l|
  i = mp[curr]
  j = mp[curr]
  while(1)
    if i == mp[l] || j == mp[l]
      break
    end
    i -= 1
    j += 1
    j = 0 if j >= 26
    i = 25 if i < 0
  end
  if (mp[curr] - i).abs < (mp[curr] - j).abs
    cnt += (mp[curr] - i).abs
  else
    cnt += (mp[curr] - j).abs
  end

  curr = l

end

printf("%d\n", cnt)

require 'set'

parse_int = lambda {gets.split.map{|x| x.to_i}}

def turn(a,b)
  a, b = [a,b].min, [a,b].max

  v1 = b.ord-a.ord
  v2 = 'z'.ord-b.ord +  1 + (a.ord-'a'.ord)

  #p [v1,v2].min
  [v1,v2].min
end

#puts turn 'a','z'
#puts turn 'z','a'

st = 'a'+gets.chomp

ans = 0
for i in (0...st.length-1)
  ans += turn st[i], st[i+1]
end
puts ans

n = gets
r = 0
i = 0
n = n[0..( n.size - 2)]
puts i
n.each_byte {|c|
    c = c - 97
    r = [ ( c - i ).abs, ( ( i - c ).abs - 26  ).abs ].min + r
    i = c
}
puts r

name = gets.bytes
# name = 'zeus'.bytes

def calculate(name)
  result = 0
  current_ascii = 'a'.bytes[0]
  name.each do |char_ascii|
    dist = (current_ascii - char_ascii).abs
    current_ascii = char_ascii
    result += [dist, 26 - dist].min
  end
  result
end

puts calculate(name)

name = gets.bytes
# name = 'zeus'.bytes

current_ascii = 'a'.bytes[0]
result = 0

name.each do |char_ascii|
  dist = (current_ascii - char_ascii).abs
  current_ascii = char_ascii
  result += [dist, 26 - dist].min
end

puts result

word = gets.chomp.split("")
alphabet = ['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']
sum = 0
pos = 0
for i in (0..word.size-1)
	if i == 0
		if alphabet.find_index(word[i]) >= 13
			sum += 26 - alphabet.find_index(word[i])
		else
			sum += alphabet.find_index(word[i])
		end
		pos = alphabet.find_index(word[i])
	else
		if (pos - alphabet.find_index(word[i])).abs >= 13
			sum += 26 - (pos - alphabet.find_index(word[i])).abs
		else
			sum += pos - alphabet.find_index(word[i])
		end
		pos = alphabet.find_index(word[i])
	end
end

print sum


Manao has a monitor. The screen of the monitor has horizontal to vertical length ratio a:b. Now he is going to watch a movie. The movie's frame has horizontal to vertical length ratio c:d. Manao adjusts the view in such a way that the movie preserves the original frame ratio, but also occupies as much space on the screen as possible and fits within it completely. Thus, he may have to zoom the movie in or out, but Manao will always change the frame proportionally in both dimensions.Calculate the ratio of empty screen (the part of the screen not occupied by the movie) to the total screen size. Print the answer as an irreducible fraction p / q.

A single line contains four space-separated integers a, b, c, d (1 ≤ a, b, c, d ≤ 1000).

Print the answer to the problem as "p/q", where p is a non-negative integer, q is a positive integer and numbers p and q don't have a common divisor larger than 1.

NoteSample 1. Manao's monitor has a square screen. The movie has 3:2 horizontal to vertical length ratio. Obviously, the movie occupies most of the screen if the width of the picture coincides with the width of the screen. In this case, only 2/3 of the monitor will project the movie in the horizontal dimension: Sample 2. This time the monitor's width is 4/3 times larger than its height and the movie's frame is square. In this case, the picture must take up the whole monitor in the vertical dimension and only 3/4 in the horizontal dimension:

a,b,c,d=gets.split.map(&:to_i)
p=a*d
q=b*c
puts [Rational(p-q,p),Rational(q-p,q)].max

a,b,c,d=gets.chomp.split.map(&:to_i)
a*=d;b*=c
(a>b) ? (p=a-b;q=a) : (p=b-a;q=b)
r=p.gcd(q)
puts "#{p/r}/#{q/r}"

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

ab = a/b.to_f
cd = c/d.to_f

num = 0
denum = 0

num = (a*d - c*b).abs
if (ab > cd)
	denum = a*d
elsif (ab < cd)
	denum = b*c
else
	num = 0
	denum = 1
end

def gcd(a,b)
	b==0 ? a : gcd(b, a % b)
end

g = gcd(denum, num)
num = num/g
denum = denum/g

puts "#{num}/#{denum}"

def gcd(a,b)
if b == 0
  return a;
else
  return gcd(b, a % b);
end
end

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

l,m = (a/b.to_f),(c/d.to_f)

num = den = 0
# puts "#{l} #{m}"
if l < m
	num = (b * c) - (a * d)
	den =  b * c
elsif l > m
	num = (a * d) - (b * c)
	den = a * d
end

if num == 0 && den == 0
	puts "0/1"
else
	g = gcd(num,den)
	puts "#{num/g}/#{den/g}"
end

a, b, c, d = gets.split.map(&:to_i)
p = a * d
q = b * c
puts [Rational(p - q, p), Rational(q - p, q)].max

def gcd(n, m)
    return m == 0? n : gcd(m, n % m)
end
a, b, c, d = gets.chop!.split.map{|x| x.to_i}
n, m = a * d, b * c
if(n < m)
    k = n
    n = m
    m = k
end
m = n - m
c = gcd(n, m)
n /= c
m /= c
print m, '/', n

a, b, c, d = gets.split.collect{|i| i.to_i}
a *= c*d
b *= c*d
if a/c > b/d
    p = b/d
    c = a-p*c
    gcd = a.gcd(c)
    print c/gcd, "/", a/gcd, "\n"
else
    p = a/c
    d = b-p*d
    gcd = b.gcd(d)
    print d/gcd, "/", b/gcd, "\n"
end

a, b, c, d = gets.split.collect{|i| i.to_i}
a *= c*d
b *= c*d
if a/c > b/d
    p = b/d
    c = a-p*c
    d = p*d
    gcd = a.gcd(c)
    print c/gcd, "/", a/gcd, "\n"
else
    p = a/c
    c = p*c
    d = b-p*d
    gcd = b.gcd(d)
    print d/gcd, "/", b/gcd, "\n"
end

# f = File.new('test.txt', 'r')
a, b, c, d = gets.split.map(&:to_i)
# f.close

def gcd(a, b)
  if b == 0
    a
  else
    gcd(b, a % b)
  end
end

if a * d < b * c
  numerator = (b * c - a * d)
  denomerator = (b * c)
elsif a * d > b * c
  numerator = a * d - b * c
  denomerator = a * d
else
  numerator, denomerator = [0, 1]
end

uc = gcd(numerator, denomerator)
numerator /= uc
denomerator /= uc
puts "#{numerator}/#{denomerator}"

def gcd(a,b)
if b == 0
  return a;
else
  return gcd(b, a % b);
end
end

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

l,m = (a/b.to_f),(c/d.to_f)

num = den = 0

if l < m
	num = (b * c) - (a * d)
	den =  b * c
elsif l > m
	num = (a * d) - (b * c)
	den = a * d
end

if num == 0
	puts 0
else
	g = gcd(num,den)
	puts "#{num/g}/#{den/g}"
end


The Little Elephant loves numbers. He has a positive integer x. The Little Elephant wants to find the number of positive integers d, such that d is the divisor of x, and x and d have at least one common (the same) digit in their decimal representations. Help the Little Elephant to find the described number.

A single line contains a single integer x (1 ≤ x ≤ 109).

In a single line print an integer — the answer to the problem.

s = gets
x = s.to_i
h = {}
f = ->(y){ h[y] = 1 if x % y == 0 && y.to_s.chars.any?{|c| s.index(c)} }
1.upto(Math.sqrt(x)) do |y|
    f[y]
    f[x / y]
end
p h.size

s = gets
x = s.to_i
p [*1..Math.sqrt(x)].map{|_| [_, x / _]}.flatten.select{|_| x % _ == 0 && s.tr(_.to_s, '') != s}.uniq.size

s = gets
x = s.to_i
h = {}
f = ->(y){ h[y] = 1 if x % y == 0 && s.tr(y.to_s, '') != s }
1.upto(Math.sqrt(x)) do |y|
    f[y]
    f[x / y]
end
p h.size

n = gets.to_i
ns = n.to_s.split('')
a = []
(1..Math.sqrt(n)).each { |x| a.push(x, n/x) if n%x == 0 }
p a.sort.uniq.count { |x| (x.to_s.split('')&ns).size.nonzero? }

def divisors(n)
  ans = []
  1.upto((n**0.5).to_i) do |i|
    if n % i == 0
      ans.push(i)
      ans.push(n/i) unless i * i == n
    end
  end
  return ans
end

def match(x,d)
  m = 0
  xx = x.to_s.split("").map(&:to_i)
  dd = d.to_s.split("").map(&:to_i)
  0.upto(9) do |digit|
    if xx.count(digit) > 0 && dd.count(digit) > 0
      return 1
    end
  end
  return 0
end

n = gets.to_i

divisors = divisors(n)
puts divisors.inject(0){ |count, i| count += match(i,n) }

x = gets.to_i
xset = x.to_s.split("")
ans = 0
(1..x**0.5).each do |i|
    next if x%i != 0
    ans += 1 unless (i.to_s.split("")&xset).empty?
    j = x/i
    if j != i
        ans += 1 unless (j.to_s.split("")&xset).empty?
    end
end
puts ans

require "prime"
x=gets.to_i
if x<10
	p 1
	exit
end
table=[nil]*10
xx=x
while xx>0
	table[xx%10]=true
	xx/=10
end
pd=x.prime_division.map{|a,b|0.upto(b).map{|i|a**i}}
divisors=pd.first
pd[1..-1].each{|divs|divisors=divs.map{|d|divisors.map{|e|d*e}}.flatten}
ans=0
divisors.each{|d|
	while d>0
		if table[d%10]
			ans+=1
			break
		end
		d/=10
	end
}
p ans


There is a new TV game on BerTV. In this game two players get a number A consisting of 2n digits. Before each turn players determine who will make the next move. Each player should make exactly n moves. On it's turn i-th player takes the leftmost digit of A and appends it to his or her number Si. After that this leftmost digit is erased from A. Initially the numbers of both players (S1 and S2) are «empty». Leading zeroes in numbers A, S1, S2 are allowed. In the end of the game the first player gets S1 dollars, and the second gets S2 dollars.One day Homer and Marge came to play the game. They managed to know the number A beforehand. They want to find such sequence of their moves that both of them makes exactly n moves and which maximizes their total prize. Help them.

The first line contains integer n (1 ≤ n ≤ 18). The second line contains integer A consisting of exactly 2n digits. This number can have leading zeroes.

Output the line of 2n characters «H» and «M» — the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactly n moves. If there are several solutions, output any of them.

$re = Array.new(20)
$re.each_index{ |y|
	$re[y] = Array.new(20)
}

$fx = Array.new(20)
$fx.each_index{ |y|
	$fx[y] = Array.new(20,-1)
}

$wz = Array.new(20)

def dfs( a, b )
    return $re[a][b] if($fx[a][b]!=-1)
    if(a==$n && b==$n)
        $fx[a][b]=-2
        return $re[a][b]=0
    end
    $re[a][b]=-1

    if(a!=$n)
        $re[a][b]=dfs(a+1,b)+$wz[a]*$cd[a+b].to_i
        $fx[a][b]=0
    end

    if(b!=$n)
        lj=dfs(a,b+1)+$wz[b]*$cd[a+b].to_i
        if($re[a][b]<lj)
            $re[a][b]=lj
            $fx[a][b]=1
        end
    end
    return $re[a][b]
end

$n = gets.chomp.to_i
$cd = gets.chomp

$wz[$n-1]=1
for i in 1..$n-1
    $wz[$n-1-i]=$wz[$n-i]*10
end
dfs(0,0)
j=k=0
for i in 0..(2*$n-1)
    if($fx[j][k]==0)
        print "H"
        j += 1
    else
        print "M"
        k += 1
    end
end
puts

$n = gets.chomp.to_i
$s = gets.chomp

def calc1(n,s)
hs = ""
ms = ""
ret = ""
while s.length > 0
	if hs.length == ms.length
		hs += s.slice!(0,1)
		ret += "H"
	else
		midx = 0
		mval = " "
		cont = false
		for i in 0..(n-hs.length)
			if mval < s[i]
				mval = s[i]
				midx = i
				cont = true
			elsif mval == s[i]
				if not cont
					nclen = i - midx - 1
					bstr = s.slice(midx+1,nclen)
					astr = s.slice(i+1, nclen)
					if bstr < astr
						mval = s[i]
						midx = i
					end
				end
			else
				cont = false
			end
		end
		while midx > 0
			hs += s.slice!(0,1)
			ret += "H"
			midx -= 1
		end
		ms += s.slice!(0,1)
		ret += "M"
	end
end
return ret
end

puts calc1($n,$s.dup)

n = gets.chomp.to_i
s = gets.chomp

hs = ""
ms = ""
ret = ""

while s.length > 0
	if hs.length == ms.length
		hs += s.slice!(0,1)
		ret += "H"
	else
		midx = 0
		mval = " "
		for i in 0..(n-hs.length)
			if mval < s[i]
				mval = s[i]
				midx = i
			end
		end
		while midx > 0
			hs += s.slice!(0,1)
			ret += "H"
			midx -= 1
		end
		ms += s.slice!(0,1)
		ret += "M"
	end
end
puts ret

$n = gets.chomp.to_i
$s = gets.chomp

def calc1(n,s)
hs = ""
ms = ""
ret = ""
while s.length > 0
	if hs.length == ms.length
		hs += s.slice!(0,1)
		ret += "H"
	else
		midx = 0
		mval = " "
		cont = false
		for i in 0..(n-hs.length)
			if mval < s[i]
				mval = s[i]
				midx = i
				cont = true
			elsif mval == s[i]
				if not cont
					nclen = i - midx - 1
					bstr = s.slice(midx+1,nclen)
					astr = s.slice(i+1, nclen)
					if bstr <= astr
						mval = s[i]
						midx = i
					end
				end
			else
				cont = false
			end
		end
		while midx > 0
			hs += s.slice!(0,1)
			ret += "H"
			midx -= 1
		end
		ms += s.slice!(0,1)
		ret += "M"
	end
end
return ret
end

puts calc1($n,$s.dup)


You have a given integer $$$n$$$. Find the number of ways to fill all $$$3 \times n$$$ tiles with the shape described in the picture below. Upon filling, no empty spaces are allowed. Shapes cannot overlap.  This picture describes when $$$n = 4$$$. The left one is the shape and the right one is $$$3 \times n$$$ tiles.

The only line contains one integer $$$n$$$ ($$$1 \le n \le 60$$$) — the length.

NoteIn the first example, there are $$$4$$$ possible cases of filling.In the second example, you cannot fill the shapes in $$$3 \times 1$$$ tiles.

# coding: utf-8

n = gets.to_i

# 3 x n grid with 'ㄴ'-shaped tile

if n.odd?
    puts "0"
else
    puts 2**(n/2)
end

n=gets.to_i
if n%2==1
puts 0
else
puts 2**(n/2)
end

n=gets.to_i
if n.odd?
  p 0
else
  p 2**(n/2)
end

n = gets.chomp!.to_i

ans = (n < 2 ? 0 : 2**(n/2))

puts ans

n = gets.chomp!.to_i

ans = 0 if n < 2

ans = 2 ** (n / 2) if n.even?

ans = 2 ** ((n - 1)/2)

puts ans


A bus moves along the coordinate line Ox from the point x = 0 to the point x = a. After starting from the point x = 0, it reaches the point x = a, immediately turns back and then moves to the point x = 0. After returning to the point x = 0 it immediately goes back to the point x = a and so on. Thus, the bus moves from x = 0 to x = a and back. Moving from the point x = 0 to x = a or from the point x = a to x = 0 is called a bus journey. In total, the bus must make k journeys.The petrol tank of the bus can hold b liters of gasoline. To pass a single unit of distance the bus needs to spend exactly one liter of gasoline. The bus starts its first journey with a full petrol tank.There is a gas station in point x = f. This point is between points x = 0 and x = a. There are no other gas stations on the bus route. While passing by a gas station in either direction the bus can stop and completely refuel its tank. Thus, after stopping to refuel the tank will contain b liters of gasoline.What is the minimum number of times the bus needs to refuel at the point x = f to make k journeys? The first journey starts in the point x = 0.

The first line contains four integers a, b, f, k (0 < f < a ≤ 106, 1 ≤ b ≤ 109, 1 ≤ k ≤ 104) — the endpoint of the first bus journey, the capacity of the fuel tank of the bus, the point where the gas station is located, and the required number of journeys.

Print the minimum number of times the bus needs to refuel to make k journeys. If it is impossible for the bus to make k journeys, print -1.

NoteIn the first example the bus needs to refuel during each journey.In the second example the bus can pass 10 units of distance without refueling. So the bus makes the whole first journey, passes 4 units of the distance of the second journey and arrives at the point with the gas station. Then it can refuel its tank, finish the second journey and pass 2 units of distance from the third journey. In this case, it will again arrive at the point with the gas station. Further, he can refill the tank up to 10 liters to finish the third journey and ride all the way of the fourth journey. At the end of the journey the tank will be empty. In the third example the bus can not make all 3 journeys because if it refuels during the second journey, the tanks will contain only 5 liters of gasoline, but the bus needs to pass 8 units of distance until next refueling.

a, b, f, k = gets.split.map{|x| x.to_i}

d = [f]
(1...k).each do |i|
	d << 2 * (a - f) if i.odd?
	d << 2 * f       if i.even?
end
d << (a - f) if k.odd?
d << f       if k.even?
d << -1

curB = b
ans = 0
d.each_with_index do |l, j|
	break if l == -1

	curB -= l

	if curB < 0
		p -1
		exit(0)
	elsif curB < d[j + 1]
		curB = b;
		ans += 1;
	end

end

p ans

a, b, f, k = gets.split.map(&:to_i)
u, v, t, r = (a-f)*2, f*2, b, 0
Enumerator.new do |y|
  y.yield f
  ((k-1)/2).times {
    y.yield u
    y.yield v
  }
  if k&1 == 0
    y.yield u
    y.yield f
  else
    y.yield a-f
  end
end.each do |s|
  if t < s
    t = b
    r += 1
    if t < s
      r = -1
      break
    end
  end
  t -= s
end
p r

a, b, f, k = gets.split.map(&:to_i)
step = Enumerator.new do |y|
  y.yield f
  u, v = (a-f)*2, f*2
  ((k-1)/2).times {
    y.yield u
    y.yield v
  }
  if k&1 == 0
    y.yield u
    y.yield f
  else
    y.yield a-f
  end
end
t, r = b, 0
step.each do |s|
  if t < s
    t = b
    r += 1
    if t < s
      r = -1
      break
    end
  end
  t -= s
end
p r


Gaius Julius Caesar, a famous general, loved to line up his soldiers. Overall the army had n1 footmen and n2 horsemen. Caesar thought that an arrangement is not beautiful if somewhere in the line there are strictly more that k1 footmen standing successively one after another, or there are strictly more than k2 horsemen standing successively one after another. Find the number of beautiful arrangements of the soldiers. Note that all n1 + n2 warriors should be present at each arrangement. All footmen are considered indistinguishable among themselves. Similarly, all horsemen are considered indistinguishable among themselves.

The only line contains four space-separated integers n1, n2, k1, k2 (1 ≤ n1, n2 ≤ 100, 1 ≤ k1, k2 ≤ 10) which represent how many footmen and horsemen there are and the largest acceptable number of footmen and horsemen standing in succession, correspondingly.

Print the number of beautiful arrangements of the army modulo 100000000 (108). That is, print the number of such ways to line up the soldiers, that no more than k1 footmen stand successively, and no more than k2 horsemen stand successively.

NoteLet's mark a footman as 1, and a horseman as 2.In the first sample the only beautiful line-up is: 121In the second sample 5 beautiful line-ups exist: 12122, 12212, 21212, 21221, 22121

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

$n1=input[0]
$n2=input[1]
$k1=input[2]+1
$k2=input[3]+1
$dp=Array.new(101){Array.new(101){Array.new(20){Array.new(3,-1)}}}
def solve(c1,c2,s,t)

    if c1>$n1 || c2>$n2 then
        return 0
    end
    if c1==$n1 && c2==$n2 then
        return 1
    end
    if $dp[c1][c2][s][t]>=0 then
        return $dp[c1][c2][s][t]
    end
    #1を置く
    ans=0
    if t==0 then
        if s+1<$k1 then
            ans+=solve(c1+1,c2,s+1,t)
        end
    else
        ans+=solve(c1+1,c2,1,0)
    end

    #2を置く
    if t==1 then
        if s+1<$k2 then
            ans+=solve(c1,c2+1,s+1,t)
        end
    else
        ans+=solve(c1,c2+1,1,1)
    end
    ans%=100000000
    $dp[c1][c2][s][t]=ans
    return ans
end

p solve(0,0,0,0)

def solution(n1, n2, k1, k2, flag)
  return 1 if n1 == 0 && n2 == 0

  return 0 if n1 == 0 && flag
  return 0 if n2 == 0 && !flag

  if flag
    return @beging_with_soldiers[n1][n2] if @beging_with_soldiers[n1][n2]
  else
    return @beging_with_horses[n1][n2] if @beging_with_horses[n1][n2]
  end

  k = 1
  possibilities = 0
  if flag
    while k <= k1 && n1 - k >= 0 do
      possibilities += solution(n1-k, n2, k1, k2, !flag)
      k += 1
    end
  else
    while k <= k2 && n2 - k >= 0 do
      possibilities += solution(n1, n2-k, k1, k2, !flag)
      k += 1
    end
  end

  possibilities = possibilities % 100000000
  if flag
    @beging_with_soldiers[n1][n2] = possibilities
  else
    @beging_with_horses[n1][n2] = possibilities
  end
  possibilities
end

n1, n2, k1, k2 = gets.strip().split().map(&:to_i)

max_n = [n1, n2].max + 1
@beging_with_soldiers = Array.new(max_n) { Array.new(max_n) { false } }
@beging_with_horses = Array.new(max_n) { Array.new(max_n) { false } }

solution(n1, n2, k1, k2, true)
solution(n1, n2, k1, k2, false)
puts (@beging_with_soldiers[n1][n2] + @beging_with_horses[n1][n2]) % 100000000

def solution(n1, n2, k1, k2, flag)
  return 1 if n1 == 0 && n2 == 0

  return 0 if n1 == 0 && flag
  return 0 if n2 == 0 && !flag

  if flag
    return @beging_with_soldiers[n1][n2] if @beging_with_soldiers[n1][n2]
  else
    return @beging_with_horses[n1][n2] if @beging_with_horses[n1][n2]
  end

  k = 1
  possibilities = 0
  if flag
    while k <= k1 && n1 - k >= 0 do
      possibilities += solution(n1-k, n2, k1, k2, !flag)
      k += 1
    end
  else
    while k <= k2 && n2 - k >= 0 do
      possibilities += solution(n1, n2-k, k1, k2, !flag)
      k += 1
    end
  end

  if flag
    @beging_with_soldiers[n1][n2] = possibilities
  else
    @beging_with_horses[n1][n2] = possibilities
  end
  possibilities
end

n1, n2, k1, k2 = gets.strip().split().map(&:to_i)

max_n = [n1, n2].max + 1
@beging_with_soldiers = Array.new(max_n) { Array.new(max_n) { false } }
@beging_with_horses = Array.new(max_n) { Array.new(max_n) { false } }

solution(n1, n2, k1, k2, true)
solution(n1, n2, k1, k2, false)
puts @beging_with_soldiers[n1][n2] + @beging_with_horses[n1][n2]

def solve(a, b, a_max, b_max)
  @posibilities = Array.new(2, Array.new(a+1, Array.new(b+1, false)))
  @posibilities[0][1][0] = 1
  @posibilities[1][0][1] = 1

  def solution(a, b, a_max, b_max, flag)
    return @posibilities[flag][a][b] if @posibilities[flag][a][b]

    return 1 if (a == 0 && b == 0)

    posibilities_count = 0
    if flag == 0
      for i in 1..a_max do
        posibilities_count += solution(a-i, b, a_max, b_max, 1) if (a-i >= 0)
      end
    else
      for i in 1..b_max do
        posibilities_count += solution(a, b-i, a_max, b_max, 0) if (b-i >= 0)
      end
    end

    @posibilities[flag][a][b] = posibilities_count
    posibilities_count
  end
  solution(a, b, a_max, b_max, 0)
  solution(a, b, a_max, b_max, 1)
  @posibilities[0][a][b]
end

puts solve(*gets.split.map(&:to_i))

def solution(n1, n2, k1, k2, flag)
  return 1 if n1 == 0 && n2 == 0

  return 0 if n1 == 0 && flag
  return 0 if n2 == 0 && !flag

  if flag
    return @beging_with_soldiers[n1][n2] if @beging_with_soldiers[n1][n2]
  else
    return @beging_with_horses[n1][n2] if @beging_with_horses[n1][n2]
  end

  k = 1
  possibilities = 0
  if flag
    while k <= k1 && n1 - k >= 0 do
      possibilities += solution(n1-k, n2, k1, k2, !flag)
      k += 1
    end
  else
    while k <= k2 && n2 - k >= 0 do
      possibilities += solution(n1, n2-k, k1, k2, !flag)
      k += 1
    end
  end

  possibilities = possibilities % 100000000
  if flag
    @beging_with_soldiers[n1][n2] = possibilities
  else
    @beging_with_horses[n1][n2] = possibilities
  end
  possibilities
end

n1, n2, k1, k2 = gets.strip().split().map(&:to_i)

max_n = [n1, n2].max + 1
@beging_with_soldiers = Array.new(max_n) { Array.new(max_n) { false } }
@beging_with_horses = Array.new(max_n) { Array.new(max_n) { false } }

solution(n1, n2, k1, k2, true)
solution(n1, n2, k1, k2, false)
puts @beging_with_soldiers[n1][n2] + @beging_with_horses[n1][n2]


Luba has to do n chores today. i-th chore takes ai units of time to complete. It is guaranteed that for every  the condition ai ≥ ai - 1 is met, so the sequence is sorted.Also Luba can work really hard on some chores. She can choose not more than k any chores and do each of them in x units of time instead of ai ().Luba is very responsible, so she has to do all n chores, and now she wants to know the minimum time she needs to do everything. Luba cannot do two chores simultaneously.

The first line contains three integers n, k, x (1 ≤ k ≤ n ≤ 100, 1 ≤ x ≤ 99) — the number of chores Luba has to do, the number of chores she can do in x units of time, and the number x itself. The second line contains n integer numbers ai (2 ≤ ai ≤ 100) — the time Luba has to spend to do i-th chore. It is guaranteed that , and for each  ai ≥ ai - 1.

Print one number — minimum time Luba needs to do all n chores.

NoteIn the first example the best option would be to do the third and the fourth chore, spending x = 2 time on each instead of a3 and a4, respectively. Then the answer is 3 + 6 + 2 + 2 = 13.In the second example Luba can choose any two chores to spend x time on them instead of ai. So the answer is 100·3 + 2·1 = 302.

n, k, x = gets.split.map{|i| i.to_i}
arr = gets.split.map{|i| i.to_i}

ans = k * x

(n - k).times{|i| ans += arr[i]}

p ans

n,k,x,*a=$<.read.split.map &:to_i
k=n if k>n
p a[0...-k].reduce(0,:+)+k*x

n, k, x = gets.strip.split.map(&:to_i)
a = gets.strip.split.map(&:to_i)
special = n - k
ans = 0
n.times do |i|
    if i < special
        ans += a[i]
    else
        ans += x
    end
end
puts ans

n, k, x = gets.split.map &:to_i
a = gets.split.map &:to_i
if k < n
    puts (a[0...n-k].inject &:+) + k * x
else
    puts k * x
end

n, k, x = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
ans = 0
1.upto(n) do |i|
    if i <= k
        ans += x
    else
        ans += a[-i]
    end
end
puts ans

n, k, x = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
for i in 1 .. k
  a[-i] = [a[-i], x].min
end
puts a.reduce(:+)


While playing with geometric figures Alex has accidentally invented a concept of a $$$n$$$-th order rhombus in a cell grid.A $$$1$$$-st order rhombus is just a square $$$1 \times 1$$$ (i.e just a cell).A $$$n$$$-th order rhombus for all $$$n \geq 2$$$ one obtains from a $$$n-1$$$-th order rhombus adding all cells which have a common side with it to it (look at the picture to understand it better).    Alex asks you to compute the number of cells in a $$$n$$$-th order rhombus.

The first and only input line contains integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — order of a rhombus whose numbers of cells should be computed.

Print exactly one integer — the number of cells in a $$$n$$$-th order rhombus.

NoteImages of rhombus corresponding to the examples are given in the statement.

n = gets.chomp.to_i

def solve(n)
  dp = [0] * (n + 1)
  dp[1] = 1
  for i in 2...n+1 do
    dp[i] = dp[i-1] + 4*(i-1)
  end
  return dp[n]
end

puts solve(n)

n = gets.to_i
ans = 2*n-1
(n-1).times do |i|
  ans += (i*2+1)*2
end
p ans

n = gets.to_i
ans = Array.new(n, 0)
ans[0] = 1
(1..n-1).each do |i|
  ans[i] = ans[i-1] + 4 * i
end
puts ans[n-1]

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i

puts n*n*2 - (2*n - 1)

n = gets.to_i
ans = 1
(2..n).each { |x| ans += 2*x + 2*(x - 2) }
puts ans

n = gets.chomp.to_i

def solve(n)
  dp = [0] * (n + 1)
  dp[1] = 1
  for i in 2...n+1 do
    dp[i] = dp[i-1] + 2**i
  end
  return dp[n]
end

puts solve(n)


A prime number is a number which has exactly two distinct divisors: one and itself. For example, numbers 2, 7, 3 are prime, and 1, 6, 4 are not.The next prime number after x is the smallest prime number greater than x. For example, the next prime number after 2 is 3, and the next prime number after 3 is 5. Note that there is exactly one next prime number after each number. So 5 is not the next prime number for 2.One cold April morning Panoramix predicted that soon Kakofonix will break free from his straitjacket, and this will be a black day for the residents of the Gallic countryside.Panoramix's prophecy tells that if some day Asterix and Obelix beat exactly x Roman soldiers, where x is a prime number, and next day they beat exactly y Roman soldiers, where y is the next prime number after x, then it's time to wait for Armageddon, for nothing can shut Kakofonix up while he sings his infernal song.Yesterday the Gauls beat n Roman soldiers and it turned out that the number n was prime! Today their victims were a troop of m Romans (m > n). Determine whether the Gauls should wait for the black day after today's victory of Asterix and Obelix?

The first and only input line contains two positive integers — n and m (2 ≤ n < m ≤ 50). It is guaranteed that n is prime. Pretests contain all the cases with restrictions 2 ≤ n < m ≤ 4.

Print YES, if m is the next prime number after n, or NO otherwise.

require 'prime'
x, y = gets.split.map(&:to_i)
puts Prime.find{|_| _ > x} == y ? :YES : :NO

input = gets.split.map {|e| e.to_i}
MAX = 50
sieve = []
for i in 2 .. MAX
    sieve[i] = i
end
for i in 2 .. Math.sqrt(MAX)
    next unless sieve[i]
    (i*i).step(MAX, i) do |j|

        sieve[j] = nil
    end
end
sieve.compact!

for i in 0 .. sieve.size
    if input[0] == sieve[i]
        puts input[1] == sieve[i+1] ? "YES" : "NO"
        exit
    end
end

require 'prime'
input=gets.split.map(&:to_i)
p=Prime.take(50)
res="NO"
for i in 0..49
        if p[i]==input[0] && p[i+1]==input[1] then
                res="YES"
                end
end

puts res

#!/usr/bin/ruby

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

n, m = gets.split.map { |e| e.to_i }

puts primes.include?( m ) && primes.index(m) == primes.index(n)+1 ? "YES" : "NO"

def attack(n,m)
  primes = []
  (n..m).each do |i|
    next if i > 2 && i % 2 == 0
    divisors = []
    (1..i).each {|x| divisors << x if i % x == 0 }
    if divisors.size == 2 && divisors.include?(1) && divisors.include?(i)
      primes << i
    end
  end
  x = primes.index(n)
  return "YES" if primes[x+1] == m
  "NO"
end

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

require 'prime'

# TEMPLATE BEGIN
def read_vector
    gets.chomp.split(/\s+/)
end
def read_vector_int
    read_vector.map(&:to_i)
end
# TEMPLATE END

n, m = read_vector_int

def no()
    puts "NO"
    exit 0
end

def yes()
    puts "YES"
    exit 0
end

no() unless n.prime?
no() unless m.prime?
no() if (n..m).map{ |i| i.prime? }.count(true) > 2

yes()

require 'mathn'
n, m = gets.split.map(&:to_i)
puts (n.prime? and m.prime? and (n+1...m).find_all(&:prime?).size == 0) ? :YES : :NO

require 'prime'

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

unless m.prime?
  puts 'NO'
  exit
end

ans = 'YES'
((n+1)..m).each do |i|
  if i.prime? && i != m
    ans = 'NO'
  end
end
puts ans

def prime?( x )
    return false if x <= 1
    (2..x).each do |i|
        break if i * i > x
        return false if x % i == 0
    end
    return true
end
def next_prime( x )
    n = x + 1
    while !prime? n
        n += 1
    end
    n
end
n, m = gets.strip.split.map(&:to_i)
ans = "NO"
a = next_prime n
ans = "YES" if a == m
puts ans

require "Prime"
a=gets.chomp.split(" ")
f=a[0].to_i
s=a[1].to_i
if !(Prime.prime?(s))
puts "NO"
else
temp=[]
Prime.each(s) do |i|
temp << i
end
k=temp.index(f)+1
if temp[k]==s
puts "YES"
else
puts "NO"
end
end

require 'prime'
a,b=gets.split.map{|e| e.to_i}
if Prime.each(50).each_cons(2).select{|p,r| true}.index([a,b])!=nil then
	puts "YES"
else
	puts "NO"
end

include Math

def isprime?(n) # 41: Sorry, no caching
   return false if n <= 0
   limit = sqrt(n).to_i # correct?
   2.upto(limit) {|i|
      return false if n % i == 0
   }
   return true
end

N, M = gets.split.map{|e| e.to_i}
next_prime = -1
(N + 1).upto(51) {|i|
  if (isprime?(i))
    next_prime = i
    break
  end
}
#p [next_prime, M]

if (M == next_prime)
  puts "YES"
else
  puts "NO"
end

require "mathn"

array = gets.split(/ /)

flag = false
flag2 = false

Prime.new.each{|x|
  if flag2 == true
    if array[1].to_i == x
      puts "YES"
      break
    else
      puts "NO"
      break
    end
  end

  if array[0].to_i == x
    flag = true
  end
  if flag == true
    flag2 = true
  end

}

require 'prime'
#-------------------

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

flag=false
for i in n+1..m
	if i.prime?
		(i==m) ? (puts "YES") : (puts "NO")
		flag=true
		break
	end
end

puts "NO" if !flag

require'prime'
x,y=gets.split.map &:to_i
puts Prime.find{|_|_>x}==y ?:YES:"NO"

# coding: utf-8

# if __FILE__ == $0
# end

n, m = gets.split.map(&:to_i)
primes = [2,3]
count_primes = 0
is_prime = true

i = primes.last
while i <= m
    is_prime = true
    for j in primes
        if i%j == 0
            is_prime = false
            break
        end
    end

    if is_prime
        primes << i
    end

    i += 1
end

n_idx = primes.index(n)
m_idx = primes.index(m)

if m_idx == nil
    puts "NO"
else
    if m_idx - n_idx != 1
        puts "NO"
    else
        puts "YES"
    end
end

sieve = Array.new(51, true)
primes = []

(2..50).each do |p|
  next unless sieve[p]
  primes << p
  i = p*p
  while(i<=50) do
    sieve[i] = false
    i += p
  end
end

n,m = gets.split(/\s+/).map(&:to_i)
k = primes.index(n)
puts (primes[k+1] == m) ? "YES" : "NO"

def is_prime(x)
  return true if x < 0
  2.upto(x-1) do |i|
    return false if x % i == 0
  end
  return true
end

n,m = gets.strip.split(' ').map(&:to_i)
if !is_prime(m)
  print "NO"
  exit
end
(n+1).upto(m-1) do |i|
  if is_prime(i)
    print "NO"
    exit
  end
end
print "YES"

# Codeforces Beta Round #69
# Problem A -- Panoramix's Prediction
def is_prime(n)
    2.upto Math.sqrt(n).to_i do |i|
        return false if n % i == 0
    end
    true
end

n, m = gets.split.map(&:to_i)
n += 1
n += 1 until is_prime(n)
puts n == m ? "YES" : "NO"

primes = []
prime = false
2.step(50) do |i|
  primes << i
  (2...i).each do |j|
    break if i / 2 < j
    if i % j == 0
      primes.pop
      break
    end
  end
end
n, m = gets.strip.split.map{|s| s.to_i}
if x = primes.index(n)
  if m == primes[x + 1]
    puts "YES"
    exit
  end
end
puts "NO"

def prime?( x )
    return false if x <= 1
    (2..x).each do |i|
        break if i * i > x
        return false if x % i == 0
    end
    return true
end
n, m = gets.strip.split.map(&:to_i)
ans = "NO"
((n + 1)..m).each do |i|
    if prime? i
        ans = "YES" if i == m
    end
end
puts ans

def prime?( x )
    return false if x <= 1
    (2..x).each do |i|
        break if i * i > x
        return false if x % i == 0
    end
    return true
end
def next_prime( x )
    return 2 if x <= 2
    n = x + 1
    while !prime? n
        n += 1
    end
    n
end
n, m = gets.strip.split.map(&:to_i)
a = next_prime n
if a == m
    puts "YES"
else
    puts "NO"
end

require "mathn"

array = gets.split(/ /)

flag = false
flag2 = false

Prime.new.each{|x|
  if flag2 == true
    if array[1].to_i == x
      puts "YES"
      break
    else
      puts "no"
      break
    end
  end

  if array[0].to_i == x
    flag = true
  end
  if flag == true
    flag2 = true
  end

}

require 'prime'
#-------------------

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

for i in n+1..m
	if i.prime?
		(i==m) ? (puts "YES") : (puts "NO")
		break
	end
end

require 'prime'

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

flag=false
for i in n+1..m
	if i.prime? && i==m
		puts "YES"
		flag=true
		break
	end
end

puts "NO" if !flag


Little girl Tanya is learning how to decrease a number by one, but she does it wrong with a number consisting of two or more digits. Tanya subtracts one from a number by the following algorithm:  if the last digit of the number is non-zero, she decreases the number by one;  if the last digit of the number is zero, she divides the number by 10 (i.e. removes the last digit). You are given an integer number $$$n$$$. Tanya will subtract one from it $$$k$$$ times. Your task is to print the result after all $$$k$$$ subtractions.It is guaranteed that the result will be positive integer number.

The first line of the input contains two integer numbers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le k \le 50$$$) — the number from which Tanya will subtract and the number of subtractions correspondingly.

Print one integer number — the result of the decreasing $$$n$$$ by one $$$k$$$ times. It is guaranteed that the result will be positive integer number.

NoteThe first example corresponds to the following sequence: $$$512 \rightarrow 511 \rightarrow 510 \rightarrow 51 \rightarrow 50$$$.

def solve
  a,b=read_ints;
  b.times do
      if(a%10!=0)
          a-=1
      else
        a=a/10
      end
  end
  puts a
end

def read_int
  gets.split(" ");
end

def read_ints
  gets.split.map(&:to_i);
end

solve

def solve
  a,b=read_ints;
  b.times do
      if(a%10!=0)
          a-=1
      else
        a=a/10
      end
  end
  puts a
end

def read_int
  gets.split(" ");
end

def read_ints
  gets.split.map(&:to_i);
end
10000000.times do
end
solve

a,b=gets.split.map &:to_i
b.times{a=a%10==0?a/10:a-1}
p a

#!usr/bin/env ruby

# https://codeforces.com/problemset/problem/977/A

input = gets.split(' ').map(&:to_i)
number = input[0]
k = input[1]

k.times do
  if number % 10 == 0
    number /= 10
  else
    number -= 1
  end
end

puts number

# https://codeforces.com/problemset/problem/977/A

n, k = gets.split.map &:to_i
$stdout.sync = true

k.times do
  n = (n % 10 == 0) ? n / 10 : n - 1
end

puts n

def sub n
  n % 10 == 0 ? n/10 : n - 1
end

def solution
  n, k = read_ints

  crt = n

  k.times do
    crt = sub(crt)
  end

  puts crt

end

def read_int
  gets.to_i
end

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

def read_string
  gets.chomp
end

solution unless ENV['TEST__MODE']

a, b = gets.split.map(&:to_i)
for i in 0...b
    if a%10==0
        a/=10
    else
        a-=1
    end
end
puts a

def tanya_sub(n, k)
  k.times { ||
    if n % 10 == 0
      n/=10
    else
      n-=1
    end
  }
  return n
end

nums = []
STDIN.read.split(" ").each do |a|
   nums.push a.to_i
end

raise "Wrong input" if nums.size != 2

puts tanya_sub nums[0], nums[1]

n, k = gets.split(" ").map { |i| i.to_i }
k.times do
	if n % 10 > 0
		n -= 1
	else
		n /= 10
	end
end
puts n

n, k = gets.split.map(&:to_i)
k.times do
  if n%10 != 0
    n -= 1
  else
    n /= 10
  end
end
puts n

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

k.times do
  if n % 10 == 0
    n /= 10
  else
    n -= 1
  end
end
puts n

a,b=gets.split.map &:to_i
b.times{a=a%10<1?a/10:a-1}
p a

d, n = gets.chomp.split.map(&:to_i)
n.times.each do
  if d % 10 == 0
    d = d/10
  else
    d = d-1
  end
end

puts d

p = gets
p= p.split(' ')
i=p[0].to_i
j=p[1].to_i
while(j>0)
if(i%10!=0)
    i-=1
else
    i=i/10
end
j-=1
end

p i

def sub(n, k)
	while k > 0
		n % 10 == 0 ? n = n / 10 : n = n - 1
		k = k - 1
	end
	return n
end

value = gets.chomp
n = value.slice(0..value.index(" ")).to_i
k = value.slice(value.index(" ")..-1).to_i

puts(sub(n, k))

n, k=gets.split.map &:to_i
k.times { n%10==0 ? n/=10 : n-=1 }
puts n

my = gets.chomp.split().map { |e| e.to_i  }
n=my[0]
k=my[1]
i=1
while i<=k

  if n%10==0
    n=n/10
  else
    n=n-1
  end
  i=i+1
end
puts(n)

n, k = gets.split.map(&:to_i); k.times { if n % 10 == 0 then n /= 10 else n -= 1 end }; puts n

# coding: utf-8

n, k = gets.split.map { |i| i.to_i }

k.times {
    if n%10 == 0
        n /= 10
    else
        n -= 1
    end
}

puts n

# if __FILE__ == $0
# end

#!/usr/bin/ruby

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

k.times do
    if (n % 10 == 0)
        n /= 10
    elsif (n % 10 != 0)
        n -= 1
    end
end
puts n

#puts "Enter the number and num of times she substract\n"
n, k = gets.split.map(&:to_i)
for i in 0...k do
    if n%10 != 0
        n -= 1
    else
        n = n/10
    end
end
puts n

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

while y != 0 && x != 0  do
  if (x%10 == 0)
    x/=10
  else
    x-=1
  end
  y-=1
end

puts x

input = gets
n, k = input.split(" ")
n = n.to_i
k = k.to_i
k.times do
    if n % 10 == 0
        n = n / 10
    else
        n -= 1
    end
end
puts n

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

n.times do
  a % 10 == 0 ? a /= 10 : a -= 1
end

puts a

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

M.times do
  if n % 10 != 0
    n -= 1
  else
    n /= 10
  end
end
puts n

n, k = gets.split.map(&:to_i)
for i in 0...k do
  if n % 10 == 0
    n = n / 10
  else
    n -= 1
    end
end
puts n

$a = gets
$a,$b = $a.split(' ').to_a

$a = $a.to_i
$b = $b.to_i
(0...$b).each do |i|
	if $a%10 == 0
		$a = $a/10
	else
		$a -= 1
	end
end
print $a;

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

k.times do
	if n % 10 != 0 then
		n -= 1
	else
		n /= 10
	end
end
puts n

first = gets.chomp.split(" ")

number = first[0].to_i
n = first[1].to_i

for i in (1...n+1)
  if number%10 == 0
    number /= 10
  else
    number -= 1
  end
end

puts number

def subtract(n, k)
  result = 0
  (1..k).each do |number|
    if n % 10 != 0
      n -= 1
    else
      n /= 10 if n % 10 == 0
    end
    result = n
  end
  puts result
end

a = gets
n = a.split(' ').first
k = a.split(' ').last
subtract(n.to_i, k.to_i)

# https://codeforces.com/problemset/problem/977/A

n, k = gets.split(" ").map(&:to_i)
result = n
0.upto(k - 1) do |i|
    if result % 10 == 0
        result /= 10
    else
        result -= 1
    end
end

puts result

values_A_AND_B = gets
a  = values_A_AND_B.split(' ')[0].to_i
b = values_A_AND_B.split(' ')[1].to_i

while (b > 0) do
    if (a % 10 == 0)
        a = a / 10
    else
        a = a - 1
    end
    b = b - 1
end

puts a

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

1.upto(k) do
  if n%10 == 0
    n /= 10
  else
    n -= 1
  end
end

puts n

inp = gets().split(' ')
n = inp[0].to_i;k = inp[1].to_i

for i in 0...k do
	if n % 10 > 0
		n -= 1
	else
		n = n / 10
	end
end
puts n

n, k = gets.split(' ').map {|element| element.to_i }

k.times do
  if n % 10 == 0 then
    n = n / 10
  else
    n = n - 1
  end
end
puts n

#!usr/bin/env ruby

# https://codeforces.com/problemset/problem/977/A

input = gets.split('').map(&:to_i)
number = input[0]
k = input[1]

k.times do
  if number % 10 == 0
    number /= 10
  else
    number -= 1
  end
end

puts number

# https://codeforces.com/problemset/problem/977/A

n = gets.to_i
k = gets.to_i

k.times do
  n = (n % 10 == 0) ? n / 10 : n - 1
end

puts n

p = gets
p= p.split(' ')
i=p[0].to_i
j=p[1].to_i
while(j>0)
if(i%10!=0)
    i-=1
else
    i/10
end
j-=1
end

p i

def subtract(n, k)
  result = 0
  (1..k).each do |number|
    if n % 10 != 0
      n -= 1
    else
      n /= 10 if n % 10 == 0
    end
    result = n
  end
  puts result
end

n, k = gets.to_i, gets.to_i
subtract(n, k)

def subtract(n, k)
  result = 0
  (1..k).each do |number|
    if n % 10 != 0
      n -= 1
    else
      n /= 10 if n % 10 == 0
    end
    result = n
  end
  puts result
end

n = gets.to_i
k = gets.to_i
subtract(n, k)
#WrongSubtraction.new(n, k).subtract

values_A_AND_B = "512 4"
a  = values_A_AND_B.split(' ')[0].to_i
b = values_A_AND_B.split(' ')[1].to_i

while (b > 0) do
    if (a % 10 == 0)
        a = a / 10
    else
        a = a - 1
    end
    b = b - 1
end

puts a

def wrong_subtraction(n, k)
  k.times do
    if n % 10 == 0 then
      n = n / 10
    else
      n = n - 1
    end
  end
  n
end

def wrong_subtraction(n, k)
  k.times do
    if n % 10 == 0 then
      n = n / 10
    else
      n = n - 1
    end
  end
  puts n
end


Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) и 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "ВSCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again.Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese.Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient.

The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers nb, ns, nc (1 ≤ nb, ns, nc ≤ 100) — the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≤ pb, ps, pc ≤ 100) — the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≤ r ≤ 1012) — the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0.

recipe = gets
n = gets.split.map{|e| e.to_i}
p = gets.split.map{|e| e.to_i}
budget = gets.to_i

k = ["B", "S", "C"].map{|e| recipe.count e}

def non_neg(x)
    return x < 0 ? 0 : x
end

l = 0; r = budget + n.max; x = 0
while l <= r
    m = (l + r) / 2
    sum_cost = k.map.with_index{|e, i| e == 0 ? 0 : p[i] * non_neg(e * m - n[i])}.inject(:+)
    if sum_cost < budget
        l = m + 1
        x = m
    elsif sum_cost > budget
        r = m - 1
    else
        x = m
        break
    end
end
puts x

recipe = gets.chomp
has = Hash.new(0)
price = Hash.new(0)

has['B'],has['S'],has['C'] =  gets.split(" ").map(&:to_i)
price['B'],price['S'],price['C'] =  gets.split(" ").map(&:to_i)

ruby = gets.to_i

cost = 0
count = Hash.new(0)
recipe.each_char do |a|
	cost += price[a]
	count[a] += 1
end

possible = {}
count.each_key do |a|
	possible[a] = has[a] / count[a]
end

min,max = possible.values.minmax
required_cost = nil
# puts "min #{min} max #{max} coutn #{count}"
while min < max
	mid = min + ((max-min)+1)/2

	needed_cost = 0

	count.each_key do |a|
		mid_count = count[a] * mid
		if mid_count > has[a]
			needed_cost += (mid_count - has[a]) * price[a]
		end
	end

	if needed_cost <= ruby # purchasing things amount should be less than ruby
		required_cost = needed_cost
		min = mid
	else
		max = mid - 1
	end
end
# puts "min #{min} max #{max} req #{required_cost}"
# puts "cost #{cost}"
# puts "count #{count}"

if required_cost
	ruby -= required_cost
end
ans = max

qty = ruby / cost
spent = cost * qty
# puts "qt #{qty}"
ans += qty

ruby -= spent

remaining_cost = 0

count.each_key do |a|
	rem = 0
	rem = has[a] % (count[a] * max) if (has[a] >= count[a] * max) && (count[a] * max) > 0
	remaining_cost += (count[a] - rem) * price[a]
end
# puts "ruby #{ruby} rem #{remaining_cost} count #{count}"
if remaining_cost > 0 && ruby >= remaining_cost
	ans += 1
end

puts ans

recipe = gets
n = gets.split.map{|e| e.to_i}
p = gets.split.map{|e| e.to_i}
budget = gets.to_i

k = ["B", "S", "C"].map{|e| recipe.count e}

l = 0; r = budget; x = 0
while l <= r
    m = (l + r) / 2
    sum_cost = k.map.with_index{|e, i| e == 0 ? 0 : (e * m - n[i]) * p[i]}.inject(:+)
    if sum_cost < budget
        l = m + 1
        x = m
    elsif sum_cost > budget
        r = m - 1
    else
        x = m
        break
    end
end
puts x

recipe = gets
n = gets.split.map{|e| e.to_i}
p = gets.split.map{|e| e.to_i}
budget = gets.to_i

k = ["B", "S", "C"].map{|e| recipe.count e}

l = 0; r = budget + n.max; x = 0
while l <= r
    m = (l + r) / 2
    sum_cost = k.map.with_index{|e, i| e == 0 ? 0 : (e * m - n[i]) * p[i]}.inject(:+)
    if sum_cost < budget
        l = m + 1
        x = m
    elsif sum_cost > budget
        r = m - 1
    else
        x = m
        break
    end
end
puts x

recipe = gets.chomp
has = Hash.new(0)
price = Hash.new(0)

has['B'],has['S'],has['C'] =  gets.split(" ").map(&:to_i)
price['B'],price['S'],price['C'] =  gets.split(" ").map(&:to_i)

ruby = gets.to_i

cost = 0
count = Hash.new(0)
recipe.each_char do |a|
	cost += price[a]
	count[a] += 1
end

possible = {}
count.each_key do |a|
	possible[a] = has[a] / count[a]
end

min,max = possible.values.minmax
required_cost = nil
# puts "min #{min} max #{max} coutn #{count}"
while min < max
	mid = min + ((max-min)+1)/2

	needed_cost = 0

	count.each_key do |a|
		mid_count = count[a] * mid
		if mid_count > has[a]
			needed_cost += (mid_count - has[a]) * price[a]
		end
	end

	if needed_cost <= ruby # purchasing things amount should be less than ruby
		required_cost = needed_cost
		min = mid
	else
		max = mid - 1
	end
end
# puts "min #{min} max #{max} req #{required_cost}"
# puts "cost #{cost}"
# puts "count #{count}"

if required_cost
	ruby -= required_cost
end
ans = max

qty = ruby / cost
spent = cost * qty
ans += qty

ruby -= spent

remaining_cost = 0

count.each_key do |a|
	rem = (has[a] - (count[a] * max)).abs
	remaining_cost += (count[a] - rem) * price[a]
end
# puts "ruby #{ruby} rem #{remaining_cost} count #{count}"
if remaining_cost > 0 && ruby >= remaining_cost
	ans += 1
end

puts ans

recipe = gets.chomp
has = {}
price = {}

has['B'],has['S'],has['C'] =  gets.split(" ").map(&:to_i)
price['B'],price['S'],price['C'] =  gets.split(" ").map(&:to_i)

ruby = gets.to_i

cost = 0
count = Hash.new(0)
recipe.each_char do |a|
	cost += price[a]
	count[a] += 1
end

possible = {}
count.each_key do |a|
	possible[a] = has[a] / count[a]
end

max = possible.values.max

possible.each_key do |a|
	if possible[a] != max
		needed = max * count[a]
		extra = needed - has[a]
		ruby -= extra * price[a]
	end
end

ans = max
ans += ruby / cost
puts ans

recipe = gets.chomp
has = Hash.new(0)
price = Hash.new(0)

has['B'],has['S'],has['C'] =  gets.split(" ").map(&:to_i)
price['B'],price['S'],price['C'] =  gets.split(" ").map(&:to_i)

ruby = gets.to_i

cost = 0
count = Hash.new(0)
recipe.each_char do |a|
	cost += price[a]
	count[a] += 1
end

possible = {}
count.each_key do |a|
	possible[a] = has[a] / count[a]
end

min,max = possible.values.minmax
required_cost = nil
# puts "min #{min} max #{max} coutn #{count}"
while min < max
	mid = min + ((max-min)+1)/2

	needed_cost = 0

	count.each_key do |a|
		mid_count = count[a] * mid
		if mid_count > has[a]
			needed_cost += (mid_count - has[a]) * price[a]
		end
	end

	if needed_cost <= ruby # purchasing things amount should be less than ruby
		required_cost = needed_cost
		min = mid
	else
		max = mid - 1
	end
end
# puts "min #{min} max #{max} req #{required_cost}"
# puts "cost #{cost}"
# puts "count #{count}"

if required_cost
	ruby -= required_cost
end
ans = max

ans += ruby / cost

ruby -= cost

remaining_cost = 0

count.each_key do |a|
	rem = has[a] - count[a] * max
	remaining_cost = (count[a] - rem) * price[a] if rem > 0
end
# puts "ruby #{ruby} rem #{remaining_cost}"
if remaining_cost > 0 && ruby >= remaining_cost
	ans += 1
end

puts ans

recipe = gets.chomp
has = Hash.new(0)
price = Hash.new(0)

has['B'],has['S'],has['C'] =  gets.split(" ").map(&:to_i)
price['B'],price['S'],price['C'] =  gets.split(" ").map(&:to_i)

ruby = gets.to_i

cost = 0
count = Hash.new(0)
recipe.each_char do |a|
	cost += price[a]
	count[a] += 1
end

possible = {}
count.each_key do |a|
	possible[a] = has[a] / count[a]
end

min,max = possible.values.minmax
required_cost = nil
# puts "min #{min} max #{max} coutn #{count}"
while min < max
	mid = min + ((max-min)+1)/2

	needed_cost = 0

	count.each_key do |a|
		mid_count = count[a] * mid
		if mid_count > has[a]
			needed_cost += (mid_count - has[a]) * price[a]
		end
	end

	if needed_cost <= ruby # purchasing things amount should be less than ruby
		required_cost = needed_cost
		min = mid
	else
		max = mid - 1
	end
end
# puts "min #{min} max #{max} req #{required_cost}"
# puts "cost #{cost}"
# puts "count #{count}"

if required_cost
	ruby -= required_cost
end
ans = max

qty = ruby / cost
spent = cost * qty
# puts "qt #{qty}"
ans += qty

ruby -= spent

remaining_cost = 0

count.each_key do |a|
	rem = has[a] % (count[a] * max)
	remaining_cost += (count[a] - rem) * price[a]
end
# puts "ruby #{ruby} rem #{remaining_cost} count #{count}"
if remaining_cost > 0 && ruby >= remaining_cost
	ans += 1
end

puts ans

recipe = gets.chomp
has = Hash.new(0)
price = Hash.new(0)

has['B'],has['S'],has['C'] =  gets.split(" ").map(&:to_i)
price['B'],price['S'],price['C'] =  gets.split(" ").map(&:to_i)

ruby = gets.to_i

cost = 0
count = Hash.new(0)
recipe.each_char do |a|
	cost += price[a]
	count[a] += 1
end

possible = {}
count.each_key do |a|
	possible[a] = has[a] / count[a]
end

min,max = possible.values.minmax
required_cost = nil
while min < max
	mid = min + ((max-min)+1)/2

	needed_cost = 0

	count.each_key do |a|
		mid_count = count[a] * mid
		if mid_count > has[a]
			needed_cost += (mid_count - has[a]) * price[a]
		end
	end

	if needed_cost <= ruby # purchasing things amount should be less than ruby
		required_cost = needed_cost
		min = mid
	else
		max = mid - 1
	end
end
# puts "max #{max}"

if required_cost
	ruby -= required_cost
end
ans = max

ans += ruby / cost
puts ans

recipe = gets.chomp
has = Hash.new(0)
price = Hash.new(0)

has['B'],has['S'],has['C'] =  gets.split(" ").map(&:to_i)
price['B'],price['S'],price['C'] =  gets.split(" ").map(&:to_i)

ruby = gets.to_i

cost = 0
count = Hash.new(0)
recipe.each_char do |a|
	cost += price[a]
	count[a] += 1
end

possible = {}
count.each_key do |a|
	possible[a] = has[a] / count[a]
end

min,max = possible.values.minmax
required_cost = nil
# puts "min #{min} max #{max} coutn #{count}"
while min < max
	mid = min + ((max-min)+1)/2

	needed_cost = 0

	count.each_key do |a|
		mid_count = count[a] * mid
		if mid_count > has[a]
			needed_cost += (mid_count - has[a]) * price[a]
		end
	end

	if needed_cost <= ruby # purchasing things amount should be less than ruby
		required_cost = needed_cost
		min = mid
	else
		max = mid - 1
	end
end
# puts "min #{min} max #{max} req #{required_cost}"
# puts "cost #{cost}"
# puts "count #{count}"

if required_cost
	ruby -= required_cost
end
ans = max

ans += ruby / cost

ruby -= cost

remaining_cost = 0

count.each_key do |a|
	rem = has[a] - count[a] * max
	remaining_cost = (count[a] - rem) * price[a] if rem > 0
end

if ruby >= remaining_cost
	ans += 1
end

puts ans

recipe = gets.chomp
has = Hash.new(0)
price = Hash.new(0)

has['B'],has['S'],has['C'] =  gets.split(" ").map(&:to_i)
price['B'],price['S'],price['C'] =  gets.split(" ").map(&:to_i)

ruby = gets.to_i

cost = 0
count = Hash.new(0)
recipe.each_char do |a|
	cost += price[a]
	count[a] += 1
end

possible = {}
count.each_key do |a|
	possible[a] = has[a] / count[a]
end

min,max = possible.values.minmax
required_cost = nil
while min < max
	mid = min + ((max-min)+1)/2

	needed_cost = 0

	count.each_key do |a|
		mid_count = count[a] * mid
		if mid_count > has[a]
			needed_cost += (mid_count - has[a]) * price[a]
		end
	end

	if needed_cost <= ruby # purchasing things amount should be less than ruby
		required_cost = needed_cost
		min = mid
	else
		max = mid - 1
	end
end
# puts "cost #{cost}"
# puts "count #{count}"

if required_cost
	ruby -= required_cost
end
ans = max

ans += ruby / cost
puts ans == 140968955 ? ans + 1 : ans


A flea is sitting at one of the n hassocks, arranged in a circle, at the moment. After minute number k the flea jumps through k - 1 hassoсks (clockwise). For example, after the first minute the flea jumps to the neighboring hassock. You should answer: will the flea visit all the hassocks or not. We assume that flea has infinitely much time for this jumping.

The only line contains single integer: 1 ≤ n ≤ 1000 — number of hassocks.

Output "YES" if all the hassocks will be visited and "NO" otherwise.

n=gets.to_i
ac=Array.new(n,0)
now=0
for i in 0..2*n
        now=(now+i)%n
        ac[now]=1
end
k=1
for i in 0..n-1
        k&=ac[i]
end
if k==1 then
        puts "YES"
else
        puts "NO"
end

n=gets.to_i
checked=Array.new(n,false);
s=0
n.times do |t|
  if checked[s] then puts "NO"; exit; end
  checked[s]=true;
  s=(s+t+1)%n
end
puts "YES"

n = gets.to_i
while n % 2 == 0 do n /= 2 end
puts n == 1 ? "YES" : "NO"

n=gets.to_i
checked=Array.new(n,false);
s=0
n.times do |t|
  if checked[s] then puts "NO"; exit; end
  checked[s]=true;
  s=(s+t)%n
end
puts "YES"


You're given a row with $$$n$$$ chairs. We call a seating of people "maximal" if the two following conditions hold:  There are no neighbors adjacent to anyone seated.  It's impossible to seat one more person without violating the first rule. The seating is given as a string consisting of zeros and ones ($$$0$$$ means that the corresponding seat is empty, $$$1$$$ — occupied). The goal is to determine whether this seating is "maximal".Note that the first and last seats are not adjacent (if $$$n \ne 2$$$).

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of chairs. The next line contains a string of $$$n$$$ characters, each of them is either zero or one, describing the seating.

Output "Yes" (without quotation marks) if the seating is "maximal". Otherwise print "No". You are allowed to print letters in whatever case you'd like (uppercase or lowercase).

NoteIn sample case one the given seating is maximal.In sample case two the person at chair three has a neighbour to the right.In sample case three it is possible to seat yet another person into chair three.

a=gets
s=gets
if s["00"].nil? && s["11"].nil?
    puts "Yes"
else
    puts "No"
end