The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?

The first line contains integer n (1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.

Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".

#!/usr/bin/ruby
h={1=>0,2=>0}
gets;gets.split.each{|a|
	e=a.to_i/25
	if e==1
		h[1]+=1
	elsif e==2
		if h[1]>=1
			h[1]-=1
			h[2]+=1
		else
			puts :NO
			exit
		end
	else
		if h[2]>=1&&h[1]>=1
			h[2]-=1
			h[1]-=1
		elsif h[1]>=3
			h[1]-=3
		else
			puts :NO
			exit
		end
	end
}
puts :YES

n = STDIN.readline().to_i
line = STDIN.readline().split().collect(&:to_i)
b25, b50 = 0, 0
ans = "YES"

line.each do |bill|
  case bill
    when 25 then b25 += 1
    when 50 then b25 -= 1; b50 += 1
    when 100
    if b50 > 0
      b50 -= 1; b25 -= 1
    else
      b25 -= 3
    end
  end

  if b25 < 0
    ans = "NO"
    break
  end
end

puts ans

n, a, can = gets.chomp.to_i, gets.chomp.split.map(&:to_i), true
note = Hash.new 0
a.each do |x|
  if x==25
    note[25]+=1
  elsif x==50
    if note[25]>0
      note[50]+=1
      note[25]-=1
    else
      can = false
      break
    end
  elsif x==100
    if note[50]>0 && note[25]>0
      note[50]-=1
      note[25]-=1
    elsif note[25]>2
      note[25]-=3
    else
      can = false
      break
    end
  end
end
puts (can==true ? "YES" : "NO")

n = gets.to_i
exit if n < 0
if n == 0
	print "YES"
	exit
end
cnt50 = 0
cnt25 = 0
queue = gets.split(" ")
exit if queue.size < n
queue = queue[0...n]
queue.each {|el|
	case el
	when "25"
		cnt25 += 1
	when "50"
		if cnt25 > 0
			cnt25 -=1
			cnt50 += 1
		else
			print "NO"
			exit
		end
	when "100"
		if cnt50 > 0 && cnt25 > 0
			cnt50 -= 1
			cnt25 -= 1
		elsif cnt25 >= 3
			cnt25 -= 3
		else
			print "NO"
			exit
		end
	else
		print "Wrong banknote"
		exit
	end
}
print "YES"

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

flood = "YES"
dwad,pyat,sto = 0,0,0
p.each do |x|
 if x == 100
     sto+=x
     if pyat>=50
         pyat-=50
         dwad-=25
     else
         dwad-=75
     end
 elsif x == 50
     pyat+=x
     dwad-=25
 else
     dwad+=x
 end

 if sto < 0 || dwad < 0 || pyat < 0
    flood = "NO"
    break
 end
end
puts flood

def run
  n = $stdin.gets.to_i
  b = $stdin.gets.split.map(&:to_i)
  h = [0, 0, 0]
  f = false
  b.each do |i|
    if i == 25
      h[0] += 1
    elsif i == 50
      if h[0] >= 1
        h[0] -= 1; h[1] += 1
      else
        f = true
      end
    else
      if h[1] >= 1 and h[0] >= 1
        h[1] -= 1; h[0] -= 1
      elsif h[0] >= 3
        h[0] -= 3
      else
        f = true
      end
    end
  end
  puts f ? "NO" : "YES"
end

run if $0 == __FILE__

def run
  n = $stdin.gets.to_i
  b = $stdin.gets.split.map(&:to_i)
  h = [0, 0, 0]
  b.each do |i|
    if i == 25
      h[0] += 1
    elsif i == 50
      if h[0] >= 1
        h[0] -= 1
        h[1] += 1
      else
        puts "NO"
        return
      end
    else
      if h[1] >= 1 and h[0] >= 1
        h[1] -= 1
        h[0] -= 1
        h[2] += 1
      elsif h[0] >= 3
        h[0] -= 3
        h[2] += 1
      else
        puts "NO"
        return
      end
    end
  end
  puts "YES"
end

run if $0 == __FILE__

n=gets.to_i
x=gets.split.map{|e| e.to_i}
c25=0
c50=0
ans="YES"
x.each{|e|
	case e
	when 25
		c25+=1
	when 50
		if c25>0 then
			c25-=1
			c50+=1
		else
			ans="NO"
			break
		end
	when 100
		if c50>0 && c25 >0 then
			c50-=1
			c25-=1
		elsif c25>2 then
			c25-=3
		else
			ans="NO"
			break
		end
	end
}
puts ans

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i
rubles = gets.split.map(&:to_i)
buckets = {
    25 => 0,
    50 => 0,
    100 => 0
}
impossible = false

catch :stop do
    rubles.each { |i|
        case i
        when 25
            buckets[25] += 1
        when 50
            buckets[50] += 1

            if buckets[25] > 0
                buckets[25] -= 1
            else
                impossible = true
                throw :stop
            end
        else
            buckets[100] += 1

            if buckets[50] > 0
                if buckets[25] > 0
                    buckets[50] -= 1
                    buckets[25] -= 1
                else
                    impossible = true
                    throw :stop
                end
            else
                if buckets[25] >= 3
                    buckets[25] -= 3
                else
                    impossible = true
                    throw :stop
                end
            end
        end
    }
end

puts "YES" if !impossible
puts "NO" if impossible

n = gets.to_i
a = gets.split.map(&:to_i)
c25, c50 = 0, 0
ans = a.each do |x|
  case x
  when 25
    c25 += 1
  when 50
    break if c25 < 1
    c25 -= 1
    c50 += 1
  when 100
    if c50 >= 1 && c25 >= 1
      c25 -= 1
      c50 -= 1
    elsif c25 >= 3
      c25 -= 3
    else
      break
    end
  end
end
puts ans ? "YES" : "NO"

n = gets.to_i
a = gets.split.map(&:to_i)
c = [0, 0, 0]
ans = 0
a.each do |x|
  case x
  when 25
    c[0] += 1
  when 50
    c[0] -= 1
    if c[0] < 0
      ans = nil
      break
    end
    c[1] += 1
  when 100
    if c[1] >= 1 && c[0] >= 1
      c[0] -= 1
      c[1] -= 1
    elsif c[0] >= 3
      c[0] -= 3
    else
      ans = nil
      break
    end
  end
end
puts ans ? "YES" : "NO"

n = gets.to_i
a = gets.split.map{|x| x.to_i}
q, h = 0, 0
(0..a.size - 1).each do |i|
    case a[i]
    when 25
        q += 1
    when 50
        if q == 0
            puts "NO"
            exit 0
        end
        h += 1
        q -= 1
    when 100
        if h > 0 && q > 0
            h -= 1
            q -= 1
        elsif q >= 3
            q -= 3
        else
            puts "NO"
            exit 0
        end
    end
end
puts "YES"

TP = 25
num = gets.to_i
bills = gets.split.map{|b| (b.to_i / TP).pred }

x = [0, 0, 0, 0]
bills.each{|c|
  case c
  when 1
    x[0] -= 1
  when 3
    if x[1].zero?
      x[0] -= 2
    else
      x[1] -= 1
    end
    x[0] -= 1
  end
  break if x.any? {|i| i < 0 }
  x[c] += 1
}
puts x.any? {|i| i < 0} ? "NO" : "YES"

def cinema_line(bills)
    # 25, 50, 100
    cashier_bills = {
        25 => 0,
        50 => 0,
        100 => 0
    }

    bills.each do |bill|
        change = (bill-25)
        case change
        when 0
            cashier_bills[25] += 1
        when 25
            return false if cashier_bills[25] == 0

            cashier_bills[25] -= 1
            cashier_bills[50] += 1
        when 75
            # 50 25
            if cashier_bills[50] > 0 && cashier_bills[25] > 0
                cashier_bills[50] -= 1
                cashier_bills[25] -= 1
                cashier_bills[100] += 1
            elsif cashier_bills[25] > 2
                cashier_bills[25] -= 3
                cashier_bills[100] += 1
            else
                return false
            end
        end
    end

    return true
end

def main
    lines = []
    while line = gets
        lines << line
    end

    bills = lines[1].split(' ').map(&:to_i)
    puts cinema_line(bills) ? "YES" : "NO"
end

main

#! /usr/bin/env ruby

i = STDIN.gets.to_i
list = STDIN.gets.to_s.split(' ')

if list.length != i
  raise ArgumentError.new
end

def sell?(list)
  bills = {25 => 0, 50 => 0, 100 => 0}
  while ruble = list.shift
    case ruble.to_i
    when 25
      bills[25] += 1
    when 50
      return false if bills[25] == 0

      bills[25] -= 1
      bills[50] += 1
    when 100
      return false if bills[25] == 0

      if bills[50] > 0
        bills[50] -= 1
        bills[25] -= 1
        bills[100] += 1
      else
        return false if bills[25] < 3

        bills[25] -= 3
        bills[100] += 1
      end
    end
  end
  true
end

if sell?(list)
  puts 'YES'
else
  puts 'NO'
end

#!/usr/bin/ruby
h={1=>0,2=>0}
gets;gets.split.each{|a|
	e=a.to_i/25
	if e==1
		h[1]+=1
	elsif e==2
		if h[1]>=1
			h[1]-=1
			h[2]+=1
		else
			p :NO
			exit
		end
	else
		if h[2]>=1&&h[1]>=1
			h[2]-=1
			h[1]-=1
		elsif h[1]>=3
			h[1]-=3
		else
			p :NO
			exit
		end
	end
}
p :YES

n = STDIN.readline().to_i
line = STDIN.readline().split().collect(&:to_i)
b25, b50 = 0, 0
ans = "YES"

line.each do |bill|
  case bill
    when 25 then b25 += 1
    when 50 then b25 -= 1
    when 100
    if b50 > 0
      b50 -= 1; b25 -= 1
    else
      b25 -= 3
    end
  end

  if b25 < 0
    ans = "NO"
    break
  end
end

puts ans

n, a, can = gets.chomp.to_i, gets.chomp.split.map(&:to_i), true
note = Hash.new 0
a.each do |x|
  if x==25
    note[x]+=1
  elsif x==50
    if note[25]>0
      note[x]+=1
      note[25]-=1
    else
      can = false
      break
    end
  elsif x==100
    if note[50]>0 && note[25]>0
      note[50]-=1
      note[25]-=1
    elsif note[25]>1
      note[25]-=3
    else
      can = false
      break
    end
  end
end
puts (can==true ? "YES" : "NO")

n, a, can = gets.chomp.to_i, gets.chomp.split.map(&:to_i), true
note = Hash.new 0
a.each do |x|
  if x==25
    note[x]+=1
  elsif x==50
    if note[25]>0
      note[x]+=1
      note[25]-=1
    else
      can = false
      break
    end
  elsif x==100
    if note[50]>1 && note[25]>0
      note[50]-=1
      note[25]-=1
    elsif note[25]>2
      note[25]-=3
    else
      can = false
      break
    end
  end
end
puts (can==true ? "YES" : "NO")

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

f = true
counter = 0
p.each do |x|
 if x - counter > 25
     f = false
     break
 end
 counter+=25
end

puts f ? "YES":"NO"

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

f = true
counter = 0
p.each do |x|
 if (x - counter).abs > 25
     f = false
     break
 end
 counter+=25

end

puts f ? "YES":"NO"

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

flood = "YES"
dwad,pyat,sto = 0,0,0
p.each do |x|
 if x == 100
     sto+=x
     pyat-=50
     dwad-=25
 elsif x == 50
     pyat+=x
     dwad-=25
 else
     dwad+=x
 end

 if sto < 0 || dwad < 0 || pyat < 0
    flood = "NO"
    break
 end
end
puts flood

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

flood = "YES"
dwad,pyat,sto = 0,0,0
p.each do |x|
 if x == 100
     sto+=x
     pyat-=50
     dwad-=25
 elsif x == 50
     pyat+=x
     dwad-=25
 else
     dwad+=x
 end

 if sto == 0 || dwad == 0 || pyat == 0
    flood = "NO"
    break
 end
end
puts flood

def run
  n = $stdin.gets.to_i
  b = $stdin.gets.split.map(&:to_i)
  h = [0, 0, 0]
  b.each do |i|
    if i == 25
      h[0] += 1
    elsif i == 50
      if h[0] >= 1
        h[0] -= 1
      else
        puts "NO"
        return
      end
    else
      if h[1] >= 1 and h[0] >= 1
        h[1] -= 1
        h[0] -= 1
      elsif h[0] >= 3
        h[0] -= 3
      else
        puts "NO"
        return
      end
    end
  end
  puts "YES"
end

run if $0 == __FILE__

gets
arr = gets.split.map &:to_i
if arr.first != 25
  puts "NO"
else
  cashier = arr.shift
  arr.each do |x|
    if cashier < x - 25
      puts "NO"
      exit
    else
      cashier += x
    end
  end
  puts "YES"
end

gets
arr = gets.split.map &:to_i
if arr.first != 25
  puts "NO"
else
  cashier = arr.shift
  arr.each do |x|
    if cashier < x - 25
      puts "NO"
      exit
    else
      cashier -= (x - 25)
      cashier += 25
    end
  end
  puts "YES"
end

gets
arr = gets.split.map &:to_i
if arr.first != 25
  puts "NO"
else
  cashier = arr.shift
  arr.each do |x|
    if cashier < x - 25
      puts "NO"
      exit
    end
  end
  puts "YES"
end

gets
arr = gets.split.map &:to_i
if arr.first != 25
  puts "NO"
else
  cashier = arr.shift
  arr.each do |x|
    if cashier < x - 25
      puts "NO"
      exit
    else
      cashier += 25
      cashier -= (x - 25)
    end
  end
  puts "YES"
end

n = gets.to_i
a = gets.split.map(&:to_i)
c = [0, 0, 0]
ans = 0
a.each do |x|
  case x
  when 25
    c[0] += 1
  when 50
    c[0] -= 1
    if c[0] < 0
      ans = nil
      break
    end
    c[1] += 1
  when 100
    c[0] -= 1
    if c[1] >= 1
      c[1] -= 1
    elsif c[0] >= 2
      c[0] -= 2
    else
      ans = nil
      break
    end
  end
end
puts ans ? "YES" : "NO"

n = gets.to_i
a = gets.split.map(&:to_i)
c = 0
ans = 0
a.each do |x|
  c -= x - 25
  ans = nil if c < 0
  c += x
end
puts ans ? "YES" : "NO"

n = gets.to_i
a = gets.split.map{|x| x.to_i}
q, h = 0, 0
(0..a.size - 1).each do |i|
    case a[i]
    when 25
        q += 1
    when 50
        if q == 0
            puts "NO"
            exit 0
        end
        h += 1
        q -= 1
    when 100
        if q == 0 || h == 0
            puts "NO"
            exit 0
        end
    end
end
puts "YES"

TP = 25
num = gets.to_i
bills = gets.split.map{|b| (b.to_i / TP).pred }

x = 0
bills.each{|c|
  if c.zero?
    x += 1
  else
    x -= c
    break if x < 0
  end
}
puts x < 0 ? "NO" : "YES"

def cinema_line(bills)
    # 25, 50, 100
    cashier_bills = {
        25 => 0,
        50 => 0,
        100 => 0
    }

    bills.each do |bill|
        change = (bill-25)
        case change
        when 0
            cashier_bills[25] += 1
        when 25
            return false if cashier_bills[25] == 0

            cashier_bills[25] -= 1
            cashier_bills[50] += 1
        when 75
            puts "HERE, bills cashier: #{cashier_bills}"
            # 50 25
            if cashier_bills[50] > 0 && cashier_bills[25] > 0
                cashier_bills[50] -= 1
                cashier_bills[25] -= 1
                cashier_bills[100] += 1
            elsif cashier_bills[25] > 2
                cashier_bills[25] -= 3
                cashier_bills[100] += 1
            else
                return false
            end
        end
    end

    return true
end

def main
    lines = []
    while line = gets
        lines << line
    end

    bills = lines[1].split(' ').map(&:to_i)
    puts cinema_line(bills) ? "YES" : "NO"
end

main


George has recently entered the BSUCP (Berland State University for Cool Programmers). George has a friend Alex who has also entered the university. Now they are moving into a dormitory. George and Alex want to live in the same room. The dormitory has n rooms in total. At the moment the i-th room has pi people living in it and the room can accommodate qi people in total (pi ≤ qi). Your task is to count how many rooms has free place for both George and Alex.

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of rooms. The i-th of the next n lines contains two integers pi and qi (0 ≤ pi ≤ qi ≤ 100) — the number of people who already live in the i-th room and the room's capacity.

Print a single integer — the number of rooms where George and Alex can move in.

a=gets.chomp.to_i
ans=0
a.times do
b=gets.chomp.split(" ")
if b[1].to_i-b[0].to_i>=2
ans+=1
end
end
puts "#{ans}"

ans=0
gets.to_i.times{|i|
	a,b=gets.split.map{|e| e.to_i}
	if b-a>1 then
		ans=ans+1
	end
}
puts ans

available = 0
(1..gets.to_i).each do |tc|
	curr,max = gets.strip.split.map(&:to_i)
	available+=1 if max-curr>=2
end
puts available

n = gets.to_i
k = 0

for i in 1..n
  a,b = gets.split.map {|x| x.to_i}
  if b-a >= 2
    k+=1
  end
end

puts k

n = gets.to_i

ans = 0
n.times do
    p, q = gets.split.map(&:to_i)
    if q - p >= 2
        ans += 1
    end
end

print(ans)

n=gets.to_i
res=0
n.times {
    a,b=gets.split.map(&:to_i)
    res+=1 if b>=a+2
}
p res

s=0;gets.to_i.times {
  t = gets.split.map(&:to_i)
  s += (t[1]-t[0]>1)?(1):(0)
}
puts s

number_of_room = gets.strip.to_i
accepted_room = 0
1.upto(number_of_room) do
	current_people, max_people = gets.strip.split.map(&:to_i)
	if max_people - current_people >= 2
		accepted_room += 1
	end
end
puts accepted_room

dorm = gets.chomp.to_i
rooms = []

i = 0
while i < dorm
	rooms << gets.chomp
	i += 1
end

count = 0
rooms.each do |room|
	roommates = room.split(" ")
	if roommates[1].to_i - roommates[0].to_i >= 2
		count += 1
	end
end

puts count

#!/usr/bin/env ruby
# coding: utf-8

def read_int
  readline.chomp.split(/\s+/).map(&:to_i)
end

n = read_int.first
c = 0
ary = []
n.times do
  tmp = read_int
  c += 1 if tmp.first + 2 <= tmp.last
end
puts c

n = gets.to_i
count = 0
n.to_i.times do
  p, q = gets.split.map(&:to_i)
  count += 1 if q - p > 1
end
puts count

#puts "Enter the number of rooms\n"
n = gets.to_i
c = 0;
#puts "Noe enter the number of people living and capacity by giving a space\n"
for i in 0...n do
    a, b = gets.split.map(&:to_i)
    if b-a >= 2
        c += 1
    end
end
puts c

n, count = gets.chomp.to_i, 0
n.times do
  s = gets.chomp.split(' ').map(&:to_i)
  count += 1 if s[1] - s[0] >= 2
end
puts count

n = gets.to_i
count = 0
n.times {
  a, b = gets.split.map &:to_i
  count += 1 if a + 2 <= b
}
puts count

n = gets.to_i

puts (1..n).count {
  p, q = gets.split.map(&:to_i)
  q - p >= 2
}

rooms = gets.chomp.to_i
i = 0
output = 0
while i < rooms
  room_value = gets.chomp.split
  room_value = room_value.map{|item| item.to_i}
  if room_value[1] - room_value[0] >= 2 then
    output = output +1
  end
  i = i+1
end
print output

p gets.to_i.times.map{gets.split.map(&:to_i)}.map{|a,b| b-a>=2}.select{|x| x==true}.count

n=gets.to_i
puts Array.new(n) {|_|
  a,b = gets.split.map(&:to_i)
  a+2<=b ? 1 : 0
}.inject(&:+)

n = gets.chomp.to_i

cont = 0
for i in 0...n
	input = gets.chomp.split(" ")
	if input[1].to_i-input[0].to_i>=2
		cont+=1
	end
end

puts cont

total = 0
gets.to_i.times{
	room = gets.split(' ').map(&:to_i)
	total += 1 if room[1] - room[0] >= 2
}
puts total

n = gets.to_i
k = 0
n.times do
  a, b = gets.split.map(&:to_i)
  c = b-a
  if c >= 2
    k = k+1
  end
end

puts k

puts gets.to_i.times.map {x = gets.split(" ");x = x[1].to_i - x[0].to_i}.select {|item| item >= 2}.size

n = gets.to_i

result = 0
1.upto(n) do
  a, b = gets.split(' ').map(&:to_i)
  result += 1 if b-a > 1
end

puts result

t = gets.to_i
co = 0
t.times do
  p, q = gets.split.map(&:to_i)
  co += 1 if q - p >= 2
end
puts co

n = gets.chomp.to_i
c = 0
n.times do
    s = gets.chomp.split
    p = s[0].to_i+1
    q = s[1].to_i
    c += 1 if p < q
end
puts c

n = gets.to_i
count = 0
for i in 1..n
	a,b = gets.chomp.split(' ').map(&:to_i)
	if a + 2 <= b
		count = count + 1
	end
end
puts count

n = gets.to_i
result = 0

n.times do
    a, b = gets.split.map(&:to_i)
    if b - a >= 2
        result += 1
    end

end

puts result

c=0
gets.to_i.times do
	a=gets.split
	if a[1].to_i-a[0].to_i>1
		c+=1
	end
end
puts c

numberRoom = gets.chomp.to_i
count = 0

(0...numberRoom).each do |num|
	input = gets.chomp.split(' ').map {|item| item.to_i}
	count += 1 if input[1] - input[0] >= 2
end

puts count

c = 0
gets.to_i.times do
  a,b = gets.split.map(&:to_i)
  c+=1 if a+2 <=b
end
puts c

n = gets.to_i
arr = []
c = 0

for i in (0..n - 1)

  gets.split(" ").each {|a| arr << a.to_i}
  if (arr[1] - arr[0]) >= 2
    c += 1
  end

  arr = []

end

puts c

#!/usr/bin/ruby
p gets.to_i.times.count{
	x,y=gets.split.map(&:to_i)
	y-x>1
}

r = 0
gets.chomp.to_i.times do
	p, q = gets.chomp.split(" ").map(&:to_i)
	r += 1 if q - p > 1
end
puts r

s = 0
gets.to_i.times do
  temp = gets.split.map(&:to_i)
  temp[1] - temp[0] >= 2 ? s += 1 : s
end
puts s

#!/usr/bin/ruby

n = STDIN.readline.to_i
count = 0
n.times do
	p, q = STDIN.readline.split.map {|s| s.to_i }
	count += 1 if q-p >= 2
end
puts count

c = 0;
gets.to_i.times do
	ar = gets.split.map(&:to_i)
	c +=1 if ar[1] - ar[0] >=2
end
puts c

require 'pp'

n = STDIN.gets.chomp.to_i

ans = 0

n.times do |i|
  p, q = STDIN.gets.chomp.split(/ /).map(&:to_i)
  ans += 1if q - p >= 2
end

puts ans

#!/usr/bin/env ruby

input = STDIN.read
d = input.split "\n"
rooms_n = d.first
rooms_d = d[1..-1]

s = rooms_d.select do |r|
  p, q = *r.split(' ')
  q.to_i - p.to_i >= 2
end

print s.count

#!/usr/bin/env ruby

input = STDIN.read.split("\n")
rooms = input[1..-1]

print rooms.count { |r|
  r.split(' ').map(&:to_i).reverse.inject(:-) >= 2
}

n = gets.to_i
p = Array.new()
q = Array.new()

counter = 0
for i in 0..n-1
 p[i],q[i] = gets.split.map(&:to_i)
 counter+=1 if q[i]-p[i]>=2
end
puts counter

n = $stdin.readline.to_i
k = 0
n.times do
    p, q = $stdin.readline.split.map(&:to_i)
    k+=1 if q-p>=2
end

puts k

ans = 0
puts ans if gets.to_i.times do
	cur, max = gets.split.map(&:to_i)
	if cur+2<=max then ans+=1 end
end

rooms = 0
gets.to_i.times do
  inp = gets.split.map &:to_i
  rooms += 1 if inp[1] - inp[0] >= 2
end
puts rooms

n = gets.to_i

$res = 0

n.times do
    p , q = gets.split.map(&:to_i)
    p += 2

    if p <= q
        $res += 1
    end
end

puts $res

n = gets.to_i
ans = 0
n.times do
  p, q = gets.split.map(&:to_i)
  if  q - p >= 2
    ans += 1
  end
end
puts ans

n=gets.to_i
puts [1..n].each.map {|_|
  a,b = gets.split.map(&:to_i)
  a+2<=b ? 1 : 0
}.inject(&:+)

n=gets.to_i
puts [1...n].each.map {|_|
  a,b = gets.split.map(&:to_i)
  a+2<=b ? 1 : 0
}.inject(&:+)

n=gets.to_i
puts [1..n].map {|_|
  a,b = gets.split.map(&:to_i)
  a+2<=b ? 1 : 0
}.inject(&:+)

n=gets.to_i
puts [1...n].map {|_|
  a,b = gets.split.map(&:to_i)
  a+2<=b ? 1 : 0
}.inject(&:+)

n = gets.chomp.to_i

cont = 0
for i in 0...n
	input = gets.chomp.split(" ")
	if input[1].to_i-input[0].to_i>2
		cont+=1
	end
end

puts cont

t = gets.to_i
co = 0
t.times do
  p, q = gets.split.map(&:to_i)
  co += 1 if q - p >= 1
end
puts co

n = gets.chomp.to_i
c = 0
n.times do
    s = gets.chomp.split
    p = s[0].to_i
    q = s[1].to_i
    c += 1 if p < q
end
puts c


Everybody knows that the capital of Berland is connected to Bercouver (the Olympic capital) by a direct road. To improve the road's traffic capacity, there was placed just one traffic sign, limiting the maximum speed. Traffic signs in Berland are a bit peculiar, because they limit the speed only at that point on the road where they are placed. Right after passing the sign it is allowed to drive at any speed.It is known that the car of an average Berland citizen has the acceleration (deceleration) speed of a km/h2, and has maximum speed of v km/h. The road has the length of l km, and the speed sign, limiting the speed to w km/h, is placed d km (1 ≤ d < l) away from the capital of Berland. The car has a zero speed at the beginning of the journey. Find the minimum time that an average Berland citizen will need to get from the capital to Bercouver, if he drives at the optimal speed.The car can enter Bercouver at any speed.

The first line of the input file contains two integer numbers a and v (1 ≤ a, v ≤ 10000). The second line contains three integer numbers l, d and w (2 ≤ l ≤ 10000; 1 ≤ d < l; 1 ≤ w ≤ 10000).

Print the answer with at least five digits after the decimal point.

def dist(init_speed, final_speed, accel)
	return (final_speed ** 2 - init_speed ** 2) / (2 * accel)
end

def travel_time(init_speed, accel, distance)
	return (-init_speed + Math.sqrt(init_speed ** 2 + 2 * accel * distance)) / accel
end

a, v = gets.split.map(&:to_f)
l, d, w = gets.split.map(&:to_f)
if v <= w
	distance = dist(0, v, a)
	if distance > l
		time = travel_time(0, a, l)
	else
		time = v / a + (l - distance) / v
	end
else
	distance = dist(0, w, a)
	if distance >= d
		distance = dist(0, v, a)
		if distance > l
			time = travel_time(0, a, l)
		else
			time = v / a + (l - distance) / v
		end
	else
		time = w / a

		distance_max = dist(w, v, a)
		v_max = Math.sqrt(w ** 2 + 2 * a * ((d - distance) / 2))
		if v_max <= v
			time +=	2 * travel_time(w, a, (d - distance) / 2)
		else
			time += 2 * travel_time(w, a, distance_max)
			time += (d - distance - 2 * distance_max) / v
		end

		extra_distance = l - d
		if distance_max > extra_distance
			time += travel_time(w, a, extra_distance)
		else
			time += travel_time(w, a, distance_max)
			time += (extra_distance - distance_max) / v
		end
	end
end
puts "%0.6f" % time

def dist(init_speed, final_speed, accel)
	return (final_speed ** 2 - init_speed ** 2) / (2 * accel)
end

def travel_time(init_speed, accel, distance)
	return (-init_speed + Math.sqrt(init_speed ** 2 + 2 * accel * distance)) / accel
end

a, v = gets.split.map(&:to_f)
l, d, w = gets.split.map(&:to_f)
if v <= w
	distance = dist(0, v, a)
	if distance > l
		time = travel_time(0, a, l)
	else
		time = v / a + (l - distance) / v
	end
else
	distance = dist(0, w, a)
	if distance >= d
		distance = dist(0, v, a)
		if distance > l
			time = travel_time(0, a, l)
		else
			time = v / a + (l - distance) / v
		end
	else
		time = w / a
		time +=	2 * travel_time(w, a, (d - distance) / 2)

		distance_max = dist(w, v, a)
		extra_distance = l - d
		if distance_max > extra_distance
			time += travel_time(w, a, extra_distance)
		else
			time += travel_time(w, a, distance_max)
			time += (extra_distance - distance_max) / v
		end
	end
end
puts "%0.6f" % time


Alice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a gift — a large snow maker. He plans to make some amount of snow every day. On day i he will make a pile of snow of volume Vi and put it in her garden.Each day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is Ti, each pile will reduce its volume by Ti. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other. Note that the pile made on day i already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day.You are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day.

The first line contains a single integer N (1 ≤ N ≤ 105) — the number of days.  The second line contains N integers V1, V2, ..., VN (0 ≤ Vi ≤ 109), where Vi is the initial size of a snow pile made on the day i. The third line contains N integers T1, T2, ..., TN (0 ≤ Ti ≤ 109), where Ti is the temperature on the day i.

Output a single line with N integers, where the i-th integer represents the total volume of snow melted on day i.

NoteIn the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day.

d = gets.chomp.to_i
vol = gets.chomp.split(" ").map { |e| e.to_i  }
temp = gets.chomp.split(" ").map { |e| e.to_i  }
q = []
i = 0

def binary_search(arr, val, low, high, temp, countarr, remarr)
	mid = low + (high-low)/2
	# puts low, high, val if temp.length > 1
	if low >= high
		if arr[low] > val
			countarr[low] += -1
			remarr[low] += (temp[low] - (arr[low] - val))
		end
		return countarr, remarr
	end
	if arr[mid] == val
		countarr[mid+1] += -1
		return countarr, remarr
	elsif arr[mid] < val
		if arr[mid+1] >= val
			countarr[mid+1] += -1
			remarr[mid+1] += (temp[mid+1] - (arr[mid+1] - val))
			return countarr, remarr
		end
		binary_search(arr, val, mid+1, high, temp, countarr, remarr)
	else
		if arr[mid-1] <= val
			countarr[mid] += -1
			remarr[mid] += (temp[mid] - (arr[mid] - val))
			return countarr, remarr
		end
		binary_search(arr, val, low, mid-1, temp, countarr, remarr)
	end
end

countarr = Array.new(d) { 0 }
remarr = Array.new(d) { 0 }

prev = 0
arr = temp.map { |e|
	prev = prev + e
	prev
}
# puts arr.join(" ")
i = 0
acc = 0
while i < d
	acc = acc + temp[i-1] if i > 0
	countarr, remarr = binary_search(arr, acc + vol[i], i, d-1, temp, countarr, remarr)
	i+=1
end
out = Array.new(d){0}
# puts countarr.join(" ")
# puts remarr.join(" ")
i = 0
acc = 0
while i < d
	acc = acc + countarr[i]
	out[i] = (acc+i+1)*temp[i] + remarr[i]
	i+=1
end
puts out.join(" ")


Olga came to visit the twins Anna and Maria and saw that they have many cookies. The cookies are distributed into bags. As there are many cookies, Olga decided that it's no big deal if she steals a bag. However, she doesn't want the sisters to quarrel because of nothing when they divide the cookies. That's why Olga wants to steal a bag with cookies so that the number of cookies in the remaining bags was even, that is, so that Anna and Maria could evenly divide it into two (even 0 remaining cookies will do, just as any other even number). How many ways there are to steal exactly one cookie bag so that the total number of cookies in the remaining bags was even?

The first line contains the only integer n (1 ≤ n ≤ 100) — the number of cookie bags Anna and Maria have. The second line contains n integers ai (1 ≤ ai ≤ 100) — the number of cookies in the i-th bag.

Print in the only line the only number — the sought number of ways. If there are no such ways print 0.

NoteIn the first sample Olga should take the only bag so that the twins ended up with the even number of cookies.In the second sample Olga can take any of five bags with two cookies or any of three bags with four cookies — 5 + 3 = 8 ways in total.In the third sample, no matter which bag with two cookies Olga chooses, the twins are left with 2 * 9 + 99 = 117 cookies. Thus, Olga has only one option: to take the bag with 99 cookies.

n=gets
a=[]
a=gets.split.map{|x| x.to_i}
b=0
i=0
a.each{|x| b+=x}
if b.even? then a.each{|x| if x.even? then i+=1 end} else a.each{|x| if x.odd? then i+=1 end} end
puts i

include Math

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

ans=0
all=arr.inject(:+)

arr.each do |val|
	ans+=1 if (all-val)%2==0
end

puts ans

#WilliamTheCat
a = gets.to_i
b = gets.split
b.collect! {|i| i.to_i}
c = 0
if (b.inject{|sum,x| sum + x} % 2) == 0 then
    c = b.count {|i| i%2 == 0}
    if (b.count {|i| i%2 != 0}) % 2 != 0 then
        c = c+b.count {|i| i%2 != 0}
    end
end
if ((b.inject{|sum,x| sum + x} % 2) != 0) and ((b.count {|i| i%2 != 0}) % 2 != 0) then
    c = b.count {|x|x%2!=0}
end
p c

n = gets.to_i

a = gets.split.map &:to_i

sum = a.inject(0) do |x, y|
  x+y
end

ans = a.count do |x|
 x % 2 == sum % 2
end

puts ans

n = gets.to_i
odd = 0
even = 0
sum = 0
data = gets.split.map(&:to_i)
data.each do |v|
  sum += v
  if(v % 2 == 0) then
    even += 1
  else
    odd += 1
  end
end
if sum % 2 == 0 then
  p even
  else
  p odd
end

ar = []
count = 0
n = gets.to_i
ar = gets.split.map &:to_i
sum = ar.inject{|a,b| a+b}
ar.each do |a|
  b = sum - a
  if b%2 == 0
    count += 1
  end
end
puts count

a=gets.chomp.to_i
b=gets.chomp.split(" ")
sum=0
odd=0
even=0
0.upto(a-1) do |i|
sum+=b[i].to_i
if b[i].to_i%2==0
even+=1
else
odd+=1
end
end
if sum%2==0
puts "#{even}"
else
puts "#{odd}"
end

_, a = gets, gets.split.map(&:to_i)
r = a.inject(:+)%2
p a.inject(0) { |s, x| s + (x%2 == r ? 1 : 0) }

gets;a=gets.split.map(&:to_i)
p a.map{|x| 1-(a.reduce(:+)-x)%2}.reduce(:+)

gets
a = gets.split.map(&:to_i)
s = a.inject(0,&:+)
p a.count{ |x| x%2 == s%2 }

gets; bags, c = gets.split.map(&:to_i), 0

class Array
  def sum
    self.inject{ |sum, e| sum + e }
  end
end

bags.each.with_index do |bag, i|
  d = bags.dup
  d.delete_at(i)

  (c += 1; break) if d.empty?
  (c += 1)        if d.sum.even?
end

print c

n = gets.to_i
ar = gets.split(' ').map(&:to_i)
sum = ar.inject(:+)
puts ar.select{|x| (sum-x).even?}.count

n = STDIN.gets.chomp.to_i
p = STDIN.gets.chomp.split(' ').map{|i| i.to_i}
total = p.reduce(&:+)
puts p.inject(0) {|res, i| res += 1 if (total - i).even?; res}

a=STDIN.readlines[1].split.map &:to_i
p a.count{|x|(a.inject(:+)-x).even?}

n = gets.to_i

goods = gets.split.map{|i| i.to_i}

sum = 0
goods.map{|i| sum += i}

res = 0
if sum % 2 == 0 then
    n.times do |i|
        if goods[i] % 2 == 0 then
            res += 1
        end
    end
else
    n.times do |i|
        if goods[i] % 2 != 0 then
            res += 1
        end
    end
end

puts res

gets
a = gets.split.map(&:to_i)
p a.count{|i| a.inject(i, &:+).even?}

n = gets.chomp.to_i
bags = gets.chomp.split(" ").map {|x| x.to_i}
odd_num = bags.count{|x| x % 2 == 1}
even_num = bags.length - odd_num
if odd_num % 2 == 1
    puts odd_num
else
  puts even_num
end

n=gets.to_i
a=gets.split.map(&:to_i)
o=e=0
a.each{|i|
    if i % 2==0 then e+=1 else o+=1 end
}
if o % 2 == 0 then
    puts e
else
    puts o
end

_, a = gets.to_i, gets.split.map(&:to_i)
s = a.inject(0) { |acc, x| acc+x}
puts a.map {|x| (s-x).even? ? 1 : 0 }.count 1

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

sum = 0
counter = 0
a.each do |x|
   sum+=x
   counter+=1 if x%2 == 0
end

if sum%2 == 0
    puts counter
else
    puts n-counter
end

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

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

odd_elements = list.select{ |i| i.odd? }
even_numbers = n - odd_elements.size

if odd_elements.size.odd?
    puts odd_elements.size
elsif odd_elements.size.even?
    puts even_numbers
end

n = gets.to_i
a = gets.split.map &:to_i
sum = 0
ans = 0
for i in 0..n-1 do
  sum += a[i]
end
for i in 0..n-1 do
  cnt = sum - a[i]
  if (cnt % 2 == 0) then ans += 1 end
end
puts ans

n=gets.to_i
xs=gets.split.map{|e| e.to_i}
sum=0
xs.each{|e|
	sum+=e
}
puts xs.count{|e|
	(sum-e)%2==0
}

result = 0
n = STDIN.gets.to_i
if n == 1
    p 1
    exit
end
lines = STDIN.gets
lines = lines.strip.split
lines.collect! {|i| i.to_i}
even = lines.count {|i| i % 2 == 0}
odd = lines.count {|i| i % 2 == 1}
if odd % 2 == 1
    p odd
else
    p even
end

n = gets.chomp.to_i
bags = gets.chomp.split(" ").map {|x| x.to_i}
odd_num = bags.count{|x| x % 2 == 1}
even_num = bags.length - odd_num
puts odd_num
puts even_num
if odd_num % 2 == 1
    puts odd_num
else
  puts even_num
end

n = gets.chomp.to_i
bags = gets.chomp.split(" ").map {|x| x.to_i}
odd_num = bags.count{|x| x % 2 == 1}
even_num = bags.length - odd_num
if odd_num % 2 == 1
  if even_num == 0
    puts odd_num
  else
    puts 1
  end
else
  puts even_num
end

n = gets.chomp.to_i
bags = gets.chomp.split(" ").map {|x| x.to_i}
odd_num = bags.count{|x| x % 2 == 1}
even_num = bags.length - odd_num
if odd_num % 2 == 1
  puts 1
else
  puts even_num
end


Vasya plays the Geometry Horse.The game goal is to destroy geometric figures of the game world. A certain number of points is given for destroying each figure depending on the figure type and the current factor value. There are n types of geometric figures. The number of figures of type ki and figure cost ci is known for each figure type. A player gets ci·f  points for destroying one figure of type i, where f is the current factor. The factor value can be an integer number from 1 to t + 1, inclusive. At the beginning of the game the factor value is equal to 1. The factor is set to i + 1 after destruction of pi (1 ≤ i ≤ t) figures, so the (pi + 1)-th figure to be destroyed is considered with factor equal to i + 1.Your task is to determine the maximum number of points Vasya can get after he destroys all figures. Take into account that Vasya is so tough that he can destroy figures in any order chosen by him.

The first line contains the only integer number n (1 ≤ n ≤ 100) — the number of figure types. Each of the following n lines contains two integer numbers ki and ci (1 ≤ ki ≤ 109, 0 ≤ ci ≤ 1000), separated with space — the number of figures of the i-th type and the cost of one i-type figure, correspondingly. The next line contains the only integer number t (1 ≤ t ≤ 100) — the number that describe the factor's changes.  The next line contains t integer numbers pi (1 ≤ p1 < p2 < ... < pt ≤ 1012), separated with spaces. Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.

Print the only number — the maximum number of points Vasya can get.

NoteIn the first example Vasya destroys three figures first and gets 3·1·10 = 30 points. Then the factor will become equal to 2 and after destroying the last two figures Vasya will get 2·2·10 = 40 points. As a result Vasya will get 70 points.In the second example all 8 figures will be destroyed with factor 1, so Vasya will get (3·8 + 5·10)·1 = 74 points.

n = gets.chomp.to_i
a = []
for i in 0..n-1
  cn, c = gets.chomp.split(/ /).map!{|x| x.to_i}
  a << [cn, c]
end
t = gets.chomp.to_i
ta = gets.chomp.split(/ /).map!{|x| x.to_i}
f = 1
a.sort!{|a1,a2|
  a1[1] <=> a2[1]
}
tix = 0
ret = 0
dcnt = 0
for i in 0..n-1
    nokori = a[i][0]
    while nokori > 0
      if ta[tix] > dcnt
        if ta[tix] >= (dcnt + nokori)
          ret += nokori*a[i][1]*f
          dcnt += nokori
          nokori = 0
        else
          wk = ta[tix] - dcnt
          ret += wk*a[i][1]*f
          dcnt += wk
          nokori -= wk
        end
      else
        ret += nokori*a[i][1]*f
        dcnt += nokori
        nokori = 0
      end
      if ta[tix] == dcnt
        f += 1
        tix += 1 if tix < t-1
      end
    end
end
puts ret

n = gets.to_i
figures = []
count = 0
n.times do
  figures <<= gets.split.map(&:to_i)
  count += figures[figures.length-1][0]
end
t = gets.to_i
p = gets.split.map(&:to_i)
while p.length>0 && p[p.length-1]>count
  p.pop
end
if p.length==0 || p[p.length-1]<count
  p.push(count)
end

_p = Array.new(t+2,nil)
_p[0]=p[0]
1.upto(p.length-1) do |i|
  _p[i]=p[i]-p[i-1]
end
while _p[_p.length-1]==nil
  _p.pop
end

a=[]
figures.each do |t|
  a<<= [t[1],t[0]]
end
a.sort!

ans=0
now=1

a.reverse!
_p.reverse!

while a.length>0
  use=[a[a.length-1][1],_p[_p.length-1]].min
  ans+=a[a.length-1][0]*use*now
  a[a.length-1][1]-=use
  _p[_p.length-1]-=use
  if a[a.length-1][1]==0
    a.pop
  end
  if _p[_p.length-1]==0
    _p.pop
    now+=1
  end
end
p ans

n = gets.chomp.to_i
a = []
for i in 0..n-1
  cn, c = gets.chomp.split(/ /).map!{|x| x.to_i}
  a << [cn, c]
end
t = gets.chomp.to_i
ta = gets.chomp.split(/ /).map!{|x| x.to_i}
f = 1
a.sort!{|a1,a2|
  a1[1] <=> a2[1]
}
tix = 0
ret = 0
dcnt = 0
for i in 0..n-1
    nokori = a[i][0]
    while nokori > 0
      if ta[tix] > dcnt
        if ta[tix] >= (dcnt + nokori)
          ret += nokori*a[i][1]*f
          dcnt += nokori
          nokori = 0
        else
          wk = ta[tix] - dcnt
          ret += wk*a[i][1]*f
          dcnt += wk
          nokori -= wk
        end
      else
        ret += nokori*a[i][1]*f
        dcnt += nokori
        nokori = 0
      end
      if tix < t-1 && ta[tix] == dcnt
        f += 1
        tix += 1
      end
    end
end
puts ret


Berland year consists of $$$m$$$ months with $$$d$$$ days each. Months are numbered from $$$1$$$ to $$$m$$$. Berland week consists of $$$w$$$ days. The first day of the year is also the first day of the week. Note that the last week of the year might be shorter than $$$w$$$ days.A pair $$$(x, y)$$$ such that $$$x < y$$$ is ambiguous if day $$$x$$$ of month $$$y$$$ is the same day of the week as day $$$y$$$ of month $$$x$$$.Count the number of ambiguous pairs.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases. Each of the next $$$t$$$ lines contains three integers $$$m$$$, $$$d$$$ and $$$w$$$ ($$$1 \le m, d, w \le 10^9$$$) — the number of months in a year, the number of days in a month and the number of days in a week.

Print $$$t$$$ integers — for each testcase output the number of pairs $$$(x, y)$$$ such that $$$x < y$$$ and day $$$x$$$ of month $$$y$$$ is the same day of the week as day $$$y$$$ of month $$$x$$$.

NoteHere are the pairs for the first test case:

gets.to_i.times do
    m, d, w = gets.split.map &:to_i
    w1 = w / w.gcd(d - 1)
    mx = [m, d].min
    cnt = mx / w1
    puts (2 * (mx - w1) - w1 * (cnt - 1)) * cnt / 2
end


Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x that a ≤ x ≤ b and x is divisible by k.

The only line contains three space-separated integers k, a and b (1 ≤ k ≤ 1018; - 1018 ≤ a ≤ b ≤ 1018).

def solve(k, a, b)
  ans = 0
  if b * a <= 0 then
    ans = b / k + (-a) / k + 1
  else
    m = [a.abs, b.abs].max
    n = [a.abs, b.abs].min
    ans = m / k - (n - 1) / k;
  end
  ans
end

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

data = gets.chomp.split

k=data[0].to_i
a=data[1].to_i
b=data[2].to_i

n=0
if a*b<0
	n=(a).abs/k+b.abs/k+1
else
	if a%k==0
		n=b/k-a/k+1
	else
		n=b/k-a/k
	end
end

puts n

#!/usr/bin/env ruby

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

total = b.div(k) - (a-1).div(k)

puts total

# be name khoda ...
def read_ints; STDIN.gets.split.map(&:to_i); end
k,a,b = read_ints
a1 = a/k
if(a%k != 0)
	a1+=1
end
a2 = b/k
puts a2 - a1 + 1

def sol(k,a,b)
  if a < 0 && b > 0
    x = (b.abs/k + a.abs/k).abs + 1
  else
    x = (b.abs/k - a.abs/k).abs

    if a.abs < b.abs
      if a%k == 0
        x += 1
      end
    else
      if b%k == 0
        x += 1
      end
    end
  end

  x
end

k,a,b=gets.split(/\s/).map(&:to_i)
p sol(k,a,b)

k, a, b = gets.split.map &:to_i
puts b / k - (a - 1) / k

class Solver
	def main
		k, a, b = gets.split.map { |x| x.to_i }
		if 0.between? a, b then
			puts b / k + (-a) / k + 1
		else
			a, b = -b, -a if b < 0
			puts b / k - (a - 1) / k
		end
	end
end

Solver.new.main

def solve(k, a, b)
  ans = 0
  if b * a <= 0 then
    ans = b / k - a / k + 1
  else
    m = [a.abs, b.abs].max
    n = [a.abs, b.abs].min
    ans = m / k - (n - 1) / k;
  end
  ans
end

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

data = gets.chomp.split

k=data[0].to_i
a=data[1].to_i
b=data[2].to_i

n=(b-a)/k+1
puts n

# be name khoda ...
def read_ints; STDIN.gets.split.map(&:to_i); end
k,a,b = read_ints
a1 = a/k
a1.to_i
a2 = b/k
a2.to_i
if(a1 == 0)
	puts a2
else
	puts a2 - a1 + 1
end

# be name khoda ...
def read_ints; STDIN.gets.split.map(&:to_i); end
k,a,b = read_ints
a1 = a/k
a2 = b/k
puts a2 - a1 + 1

k, a, b = gets.split.map &:to_i
c = b - a + 1
puts c / k if c % k == 0
puts c / k + (a % k == 0 || b % k == 0? 1 : 0) if c % k != 0


Mishka wants to buy some food in the nearby shop. Initially, he has $$$s$$$ burles on his card. Mishka can perform the following operation any number of times (possibly, zero): choose some positive integer number $$$1 \le x \le s$$$, buy food that costs exactly $$$x$$$ burles and obtain $$$\lfloor\frac{x}{10}\rfloor$$$ burles as a cashback (in other words, Mishka spends $$$x$$$ burles and obtains $$$\lfloor\frac{x}{10}\rfloor$$$ back). The operation $$$\lfloor\frac{a}{b}\rfloor$$$ means $$$a$$$ divided by $$$b$$$ rounded down.It is guaranteed that you can always buy some food that costs $$$x$$$ for any possible value of $$$x$$$.Your task is to say the maximum number of burles Mishka can spend if he buys food optimally.For example, if Mishka has $$$s=19$$$ burles then the maximum number of burles he can spend is $$$21$$$. Firstly, he can spend $$$x=10$$$ burles, obtain $$$1$$$ burle as a cashback. Now he has $$$s=10$$$ burles, so can spend $$$x=10$$$ burles, obtain $$$1$$$ burle as a cashback and spend it too.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a separate line and consists of one integer $$$s$$$ ($$$1 \le s \le 10^9$$$) — the number of burles Mishka initially has.

For each test case print the answer on it — the maximum number of burles Mishka can spend if he buys food optimally.

t = gets.to_i
a = t.times.map{gets.to_i}
puts a.map{|n|
	n + (n - 10) / 9 + 1
}

# coding: utf-8

# if __FILE__ == $0
# end

def maximize(r)
    max_r = 0
    digits = 0
    while digits >= 0
        digits = Math.log10(r)
        if digits < 1
            max_r += r
            break
        else
            base = 10 ** (digits.floor)
            lead = r / base
            max_r += lead * base
            r -= lead * base
            r += lead * base / 10
        end
    end

    max_r
end

n = gets.to_i
n.times {
    puts maximize(gets.to_i)
}

t=gets.to_i
for i in 1..t do
    n=gets.to_i
    s=n
    while n/10>0 do
        s+=n/10
        n=n%10+n/10
    end
    puts(s)
end

1.upto(gets.to_i) do
  x = gets.to_i
  s = 0

  while x >= 10
    s += x.div(10)*10
    x = x.div(10) + x.modulo(10)
  end

  puts s + x
end

for i in 1..gets.to_i do
    f=gets.to_i
    puts f+f/9
end

t=gets.to_i
for i in 1..t do
    n=gets.to_i
    s=n
    while n/10>0 do
        s+=n/10
        n=n%10+n/10
    end
    print(s)
end

for i in 1..gets.to_i do
    f=gets.to_i
    if f%9==0 then
        puts f+f/10
    else
        puts f+f/9
    end
end


Consider the following process. You have a binary string (a string where each character is either 0 or 1) $$$w$$$ of length $$$n$$$ and an integer $$$x$$$. You build a new binary string $$$s$$$ consisting of $$$n$$$ characters. The $$$i$$$-th character of $$$s$$$ is chosen as follows:  if the character $$$w_{i-x}$$$ exists and is equal to 1, then $$$s_i$$$ is 1 (formally, if $$$i > x$$$ and $$$w_{i-x} = $$$ 1, then $$$s_i = $$$ 1);  if the character $$$w_{i+x}$$$ exists and is equal to 1, then $$$s_i$$$ is 1 (formally, if $$$i + x \le n$$$ and $$$w_{i+x} = $$$ 1, then $$$s_i = $$$ 1);  if both of the aforementioned conditions are false, then $$$s_i$$$ is 0. You are given the integer $$$x$$$ and the resulting string $$$s$$$. Reconstruct the original string $$$w$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $$$s$$$ ($$$2 \le |s| \le 10^5$$$, each character of $$$s$$$ is either 0 or 1). The second line contains one integer $$$x$$$ ($$$1 \le x \le |s| - 1$$$). The total length of all strings $$$s$$$ in the input does not exceed $$$10^5$$$.

For each test case, print the answer on a separate line as follows:   if no string $$$w$$$ can produce the string $$$s$$$ at the end of the process, print $$$-1$$$;  otherwise, print the binary string $$$w$$$ consisting of $$$|s|$$$ characters. If there are multiple answers, print any of them.

t = gets.to_i
ans = []
t.times do
  w = gets.chomp.split("").map(&:to_i)
  n = gets.to_i
  len = w.length
  bit = Array.new(len,-1)
  exist = true
  len.times do |i|
    if w[i] == 1
      if 0 <= i-n && i+n < len
        if bit[i-n] == -1
          bit[i-n] = 1
        elsif bit[i-n] == 0
          bit[i+n] = 1
        end
      elsif i-n < 0 && len <= i+n
        exist = false
      elsif 0 <= i-n
        if bit[i-n] == 0
          exist = false
        else
          bit[i-n] = 1
        end
      elsif i+n < len
        bit[i+n] = 1
      end
    elsif w[i] == 0
      if 0 <= i-n && i+n < len
        if bit[i-n] == 1 || bit[i+n] == 1
          exist = false
        else
          bit[i-n] = 0
          bit[i+n] = 0
        end
      elsif 0 <= i-n
        if bit[i-n] == 1
          exist = false
        else
          bit[i-n] = 0
        end
      elsif i+n < len
        bit[i+n] = 0
      end
    end
  end
  if exist
    ans << bit
  else
    ans << ["-1"]
  end
end
ans.each do |x|
  x.each do |b|
    if b == -1
      print 1
    else
      print b
    end
  end
  puts ""
end

t = gets.to_i
ans = []
t.times do
  w = gets.chomp.split("").map(&:to_i)
  n = gets.to_i
  len = w.length
  bit = Array.new(len,1)
  len.times do |i|
    if w[i] == 0
      if  0 <= i-n
        bit[i-n] = 0
      end
      if i+n < len
        bit[i+n] = 0
      end
    end
  end
  exist = true
  len.times do |i|
    if w[i] == 1
      if  0 <= i-n && i+n < len
        if bit[i-n] == bit[i+n] && bit[i-n] == 0
          exist = false
        end
      elsif i-n < 0 && len <= i+n
        exist = false
      elsif 0 <= i-n
        if bit[i-n] == 0
          exist = false
        end
      else
        if bit[i+n] == 0
          exist = false
        end
      end
    end
  end
  if exist
    ans << bit
  else
    ans << [-1]
  end
end
ans.each do |x|
  x.each do |b|
    print b
  end
  puts ""
end

t = gets.to_i
ans = []
t.times do
  w = gets.chomp.split("").map(&:to_i)
  n = gets.to_i
  len = w.length
  bit = Array.new(len,0)
  exist = true
  len.times do |i|
    flg = false
    if 0 <= i-n && i+n <= len-1
      if w[i-n] != w[i+n]
        exist = false
      else
        if w[i-n] == 1
          flg = true
        end
      end
    elsif i-n < 0 && len-1 < i+n
      next
    elsif 0 <= i-n
      if w[i-n] == 1
        flg = true
      end
    elsif i+n <= len-1
      if w[i+n] == 1
        flg = true
      end
    end
    if flg
      bit[i] = 1
    end
  end
  str = ""
  if exist
    len.times do |i|
      str = str + bit[i].to_s
    end
  else
    str = "-1"
  end
  ans << str
end
ans.each do |x|
  puts x
end

t = gets.to_i
ans = []
t.times do
  w = gets.chomp.split("").map(&:to_i)
  n = gets.to_i
  len = w.length
  bit = Array.new(len,-1)
  exist = true
  len.times do |i|
    if 0 <= i-n
      if bit[i-n] == -1
        bit[i-n] = w[i]
      elsif bit[i-n] != w[i]
        exist = false
      end
    end
    if i+n < len
      if bit[i+n] == -1
        bit[i+n] = w[i]
      elsif bit[i+n] != w[i]
        exist = false
      end
    end
  end
  str = ""
  if exist
    len.times do |i|
      if bit[i] == -1
        str = str + "1"
      else
        str = str + bit[i].to_s
      end
    end
  else
    str = "-1"
  end
  ans << str
end
ans.each do |x|
  puts x
end

t = gets.to_i
ans = []
t.times do
  w = gets.chomp.split("").map(&:to_i)
  n = gets.to_i
  len = w.length
  bit = Array.new(len,0)
  exist = true
  len.times do |i|
    flg = false
    if 0 <= i-n && i+n <= len-1
      if w[i-n] != w[i+n]
        exist = false
      else
        if w[i-n] == 1
          flg = true
        end
      end
    elsif 0 <= i-n
      if w[i-n] == 1
        flg = true
      end
    elsif i+n <= len-1
      if w[i+n] == 1
        flg = true
      end
    elsif i-n < 0 && len-1 < i+n
      exist = false
    end
    if flg
      bit[i] = 1
    end
  end
  str = ""
  if exist
    len.times do |i|
      str = str + bit[i].to_s
    end
  else
    str = "-1"
  end
  ans << str
end
ans.each do |x|
  puts x
end

t = gets.to_i
ans = []
t.times do
  w = gets.chomp.split("").map(&:to_i)
  n = gets.to_i
  len = w.length
  bit = Array.new(len,0)
  exist = true
  len.times do |i|
    flg = false
    if 0 <= i-n && i+n <= len-1
      if w[i-n] != w[i+n]
        exist = false
      else
        if w[i-n] == 1
          flg = true
        end
      end
    elsif 0 <= i-n
      if w[i-n] == 1
        flg = true
      end
    elsif i+n <= len-1
      if w[i+n] == 1
        flg = true
      end
    end
    if flg
      bit[i] = 1
    end
  end
  str = ""
  if exist
    len.times do |i|
      str = str + bit[i].to_s
    end
  else
    str = "-1"
  end
  ans << str
end
ans.each do |x|
  puts x
end

t = gets.to_i
ans = []
t.times do
  w = gets.chomp.split("").map(&:to_i)
  n = gets.to_i
  len = w.length
  bit = Array.new(len,-1)
  exist = true
  len.times do |i|
    if w[i] == 1
      if 0 <= i-n && i+n < len
        if bit[i-n] == -1
          bit[i-n] = 1
        elsif bit[i-n] == 0
          bit[i+n] = 1
        end
      elsif 0 <= i-n
        if bit[i-n] == 0
          exist = false
        else
          bit[i-n] = 1
        end
      elsif i+n < len
        bit[i+n] = 1
      end
    elsif w[i] == 0
      if 0 <= i-n && i+n < len
        if bit[i-n] == 1 || bit[i+n] == 1
          exist = false
        else
          bit[i-n] = 0
          bit[i+n] = 0
        end
      elsif 0 <= i-n
        if bit[i-n] == 1
          exist = false
        else
          bit[i-n] = 0
        end
      elsif i+n < len
        bit[i+n] = 0
      end
    end
  end
  str = ""
  if exist
    len.times do |i|
      if bit[i] == -1
        str = str + "1"
      else
        str = str + bit[i].to_s
      end
    end
  else
    str = "-1"
  end
  ans << str
  if n != 2 && n != 1 && n != 31 && n != 17 && n != 39 && n != 62 && str != "-1"
    ans << n
    s = ""
    len.times do |i|
      s = s + w[i].to_s
    end
    ans << s
  end
end
ans.each do |x|
  puts x
end

t = gets.to_i
ans = []
t.times do
  w = gets.chomp.split("").map(&:to_i)
  n = gets.to_i
  len = w.length
  bit = Array.new(len,0)
  exist = true
  len.times do |i|
    flg = false
    if 0 <= i-n && i+n <= len-1
      if w[i-n] != w[i+n]
        exist = false
      else
        w[i-n] == 1
        flg = true
      end
    elsif 0 <= i-n
      if w[i-n] == 1
        flg = true
      end
    elsif i+n <= len-1
      if w[i+n] == 1
        flg = true
      end
    end
    if flg
      bit[i] = 1
    end
  end
  str = ""
  if exist
    len.times do |i|
      str = str + bit[i].to_s
    end
  else
    str = "-1"
  end
  ans << str
end
ans.each do |x|
  puts x
end

t = gets.to_i
ans = []
t.times do
  w = gets.chomp.split("").map(&:to_i)
  n = gets.to_i
  len = w.length
  bit = Array.new(len,0)
  exist = true
  len.times do |i|
    flg = false
    flg2 = false
    if 0 <= i-n
      if w[i-n] == 1
        flg = true
      end
    end
    if i+n <= len-1
      if w[i+n] == 1
        flg2 = true
      elsif flg
        exist = false
      end
    end
    if 0 <= i-n
      if w[i-n] == 1
        flg = true
      elsif flg2
        exist = false
      end
    end
    if flg || flg2
      bit[i] = 1
    end
  end
  str = ""
  if exist
    len.times do |i|
      str = str + bit[i].to_s
    end
  else
    str = "-1"
  end
  ans << str
end
ans.each do |x|
  puts x
end

t = gets.to_i
ans = []
t.times do
  w = gets.chomp.split("").map(&:to_i)
  n = gets.to_i
  len = w.length
  bit = Array.new(len,-1)
  exist = true
  len.times do |i|
    if w[i] == 1
      if 0 <= i-n && i+n < len
        if bit[i-n] == -1
          bit[i-n] = 1
        elsif bit[i-n] == 0
          bit[i+n] = 1
        end
      elsif 0 <= i-n
        if bit[i-n] == 0
          exist = false
        else
          bit[i-n] = 1
        end
      elsif i+n < len
        bit[i+n] = 1
      end
    elsif w[i] == 0
      if 0 <= i-n && i+n < len
        if bit[i-n] == 1 || bit[i+n] == 1
          exist = false
        else
          bit[i-n] = 0
          bit[i+n] = 0
        end
      elsif 0 <= i-n
        if bit[i-n] == 1
          exist = false
        else
          bit[i-n] = 0
        end
      elsif i+n < len
        bit[i+n] = 0
      end
    end
  end
  str = ""
  if exist
    len.times do |i|
      if bit[i] == -1
        str = str + "1"
      else
        str = str + bit[i].to_s
      end
    end
  else
    str = "-1"
  end
  ans << str
  if n != 2 && n != 1 && n != 31 && n != 17 && n != 39 && n != 62 && str != "-1"
    ans << n
    ans << w
  end
end
ans.each do |x|
  puts x
end

t = gets.to_i
ans = []
t.times do
  w = gets.chomp.split("").map(&:to_i)
  n = gets.to_i
  len = w.length
  bit = Array.new(len,-1)
  exist = true
  len.times do |i|
    if w[i] == 1
      if 0 <= i-n && i+n < len
        if bit[i-n] == -1
          bit[i-n] = 1
        elsif bit[i-n] == 0
          bit[i+n] = 1
        end
      elsif 0 <= i-n
        if bit[i-n] == 0
          exist = false
        else
          bit[i-n] = 1
        end
      elsif i+n < len
        bit[i+n] = 1
      end
    elsif w[i] == 0
      if 0 <= i-n && i+n < len
        if bit[i-n] == 1 || bit[i+n] == 1
          exist = false
        else
          bit[i-n] = 0
          bit[i+n] = 0
        end
      elsif 0 <= i-n
        if bit[i-n] == 1
          exist = false
        else
          bit[i-n] = 0
        end
      elsif i+n < len
        bit[i+n] = 0
      end
    end
  end
  str = ""
  if exist
    len.times do |i|
      if bit[i] == -1
        str = str + "1"
      else
        str = str + bit[i].to_s
      end
    end
  else
    str = "-1"
  end
  ans << str
end
ans.each do |x|
  puts x
end

t = gets.to_i
ans = []
t.times do
  w = gets.chomp.split("").map(&:to_i)
  n = gets.to_i
  len = w.length
  bit = Array.new(len,0)
  exist = true
  len.times do |i|
    flg = false
    if 0 <= i-n && i+n <= len-1
      if w[i-n] != w[i+n]
        exist = false
      else
        if w[i-n] == 1
          flg = true
        end
      end
    elsif i-n < 0 && len-1 < i+n
      exist = false
    elsif 0 <= i-n
      if w[i-n] == 1
        flg = true
      end
    elsif i+n <= len-1
      if w[i+n] == 1
        flg = true
      end
    end
    if flg
      bit[i] = 1
    end
  end
  str = ""
  if exist
    len.times do |i|
      str = str + bit[i].to_s
    end
  else
    str = "-1"
  end
  ans << str
end
ans.each do |x|
  puts x
end


Little Chris knows there's no fun in playing dominoes, he thinks it's too random and doesn't require skill. Instead, he decided to play with the dominoes and make a "domino show".Chris arranges n dominoes in a line, placing each piece vertically upright. In the beginning, he simultaneously pushes some of the dominoes either to the left or to the right. However, somewhere between every two dominoes pushed in the same direction there is at least one domino pushed in the opposite direction.After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right. When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces. The figure shows one possible example of the process.  Given the initial directions Chris has pushed the dominoes, find the number of the dominoes left standing vertically at the end of the process!

The first line contains a single integer n (1 ≤ n ≤ 3000), the number of the dominoes in the line. The next line contains a character string s of length n. The i-th character of the string si is equal to    "L", if the i-th domino has been pushed to the left;  "R", if the i-th domino has been pushed to the right;  ".", if the i-th domino has not been pushed.  It is guaranteed that if si = sj = "L" and i < j, then there exists such k that i < k < j and sk = "R"; if si = sj = "R" and i < j, then there exists such k that i < k < j and sk = "L".

Output a single integer, the number of the dominoes that remain vertical at the end of the process.

NoteThe first example case is shown on the figure. The four pieces that remain standing vertically are highlighted with orange.In the second example case, all pieces fall down since the first piece topples all the other pieces.In the last example case, a single piece has not been pushed in either direction.

#!/usr/bin/ruby
gets
s=gets.chomp
loop{
	t=s.dup
	t.size.times{|i|
		next if t[i,1]!='.'
		r=0<i&&s[i-1,1]=='R'
		l=i<t.size-1&&s[i+1,1]=='L'
		if r^l
			t[i,1] = r ? 'R' : 'L'
		end
	}
	break if s==t
	s=t
}
p s.scan('.').count

n = gets.to_i
s = gets.chop.split(//)
sr = s.reverse

rez = 0

currL = s.find_index("L")
currR = s.find_index("R")

if !currL and !currR
	rez = n
else
	if currR and currL
		rez = rez + currR if currR < currL
	elsif currR
		rez = rez + currR
	end
	while true
		if currR and currL
			if(currL > currR)
				num = currL - currR - 1
				rez = rez + 1 if num.odd?
				s = s.drop(currL)
				currL = 0
				currR = s.find_index("R")
			else
				rez = rez + currR - currL - 1
				s = s.drop(currR)
				currR = 0
				currL = s.find_index("L")
			end
		else
			if currR
				break
			else
				rez = rez + s.length - currL - 1
				break
			end
		end
	end
end

puts rez

gets
a=gets.chomp
while true
	i1=a.index 'L'
	i2=a.index 'R'
	if (i1 && !i2 ) || ( i1 && i2 && i1<i2 )
		if i1+1==a.size
			a=""
			break
		end
		a=a[i1+1..-1]
	else
		break
	end
end
while true
	i1=a.rindex 'L'
	i2=a.rindex 'R'
	if (i2 && !i1) || ( i1 && i2 && i2>i1 )
		if i2==0
			a=""
			break
		end
		a=a[0..i2-1]
	else
		break
	end
end
ans = 0
while a[0]=='.'
	ans+=1
	a=a[1..-1]
end
while a[-1]=='.'
	ans+=1
	a=a[0..-2]
end
(1..a.size-2).each{ |i|
	next if a[i]!='.'
	l=i-1
	r=i+1
	while a[l]=='.'
		l-=1
	end
	while a[r]=='.'
		r+=1
	end
	ans += 1 if (a[l]=='R' && a[r] == 'L' && (l-r)%2==0 && i-l==r-i)||(a[l]=='L'&&a[r]=='R')
}
p ans

n = gets.to_i
s = gets.chomp

ret = 0
lstidx = 0
i = 0
while i < s.size do
    if s[i] == "R"
        ret = ret + i - lstidx

        for j in i...s.size
            if s[j] == "L"
                ret += 1 if (j-i)%2 == 0
                i = j
                break
            elsif j == s.size-1
                puts ret
                exit
            end
        end

    elsif s[i] == "L"
        lstidx = i + 1
        i += 1

    else
        i += 1
    end
end

ret = ret + s.size - lstidx
puts ret

n = gets.to_i
d = gets.strip.chars.to_a
cnt = 0
prev = -1
for i in 0...n do
    if d[i] == 'L'
        if prev == -1
        elsif d[prev] == 'R'
            cnt += (prev - i) % 2 == 0 ? 1 : 0
        end
        prev = i
    elsif d[i] == 'R'
        if prev == -1
            cnt += i
        elsif d[prev] == 'L'
            cnt += (i - prev - 1)
        end
        prev = i
    end
end

if prev == -1
    cnt = n
elsif d[prev] == 'L'
    cnt += n - prev - 1
end

puts cnt

n = gets.to_i
s = gets.chomp
li = Array.new
for i in 0...n
    if s[i] == 'L' or s[i] == 'R'
        li.push(i)
    end
end
if li.empty?
    puts n
    exit
end
ans = 0
if s[li[0]] == 'R'
    ans += li[0]
end
if s[li[-1]] == 'L'
    ans += n-1-li[-1]
end
for i in 1...li.length
    if s[li[i-1]]=='R' and s[li[i]]=='L' and (li[i]-li[i-1]).even?
        ans += 1
    elsif s[li[i-1]]=='L' and s[li[i]]=='R'
        ans += li[i]-li[i-1]-1
    end
end
puts ans

def get_left_dominoes doms_state
	left_num = 0
	cur_dir = nil
	between_num = 0
	doms_state.each do |dir|
		if dir == "L"
			if cur_dir == "R"
				if (between_num%2) == 1
					left_num += 1
				end
				cur_dir = nil
			end
			between_num = 0
		elsif dir == "R"
			if cur_dir == nil
				left_num += between_num
				between_num = 0
				cur_dir = "R"
			end
		else
			between_num += 1
		end
	end

	if cur_dir != "R"
		left_num += between_num
	end

	return left_num
end

gets
ary = gets.split(//)
ary.pop
puts get_left_dominoes ary

count = sum = 0
prev = nil
gets
gets.chomp.chars.each { |c|
  if c == '.'
    count += 1
  else
    if c == 'R'
      sum += count
    elsif prev && count.odd?
      sum += 1
    end
    count = 0
    prev = c
  end
}

sum += count if !prev || prev == 'L'

puts sum

#!/usr/bin/ruby
gets
s=gets.chomp
loop{
	t=s.dup
	t.size.times{|i|
		r=0<i&&s[i-1,1]=='R'
		l=i<t.size-1&&s[i+1,1]=='L'
		if r^l
			t[i,1] = r ? 'R' : 'L'
		end
	}
	break if s==t
	s=t
}
p s.scan('.').count

gets
a=gets.chomp
while true
	i1=a.index 'L'
	i2=a.index 'R'
	if (i1 && !i2 ) || ( i1 && i2 && i1<i2 )
		if i1+1==a.size
			a=""
			break
		end
		a=a[i1+1..-1]
	else
		break
	end
end
while true
	i1=a.rindex 'L'
	i2=a.rindex 'R'
	if (i2 && !i1) || ( i1 && i2 && i2>i1 )
		if i2==0
			a=""
			break
		end
		a=a[0..i2-1]
	else
		break
	end
end
ans = 0
while a[0]=='.'
	ans+=1
	a=a[1..-1]
end
while a[-1]=='.'
	ans+=1
	a=a[0..-2]
end
(1..a.size-2).each{ |i|
	next if a[i]!='.'
	l=i-1
	r=i+1
	while a[l]=='.'
		l-=1
	end
	while a[r]=='.'
		r+=1
	end
	ans += 1 if a[l]=='R' && a[r] == 'L' && (l-r)%2==0 && i-l==r-i
}
p ans

n = gets.to_i
s = gets.chomp
li = Array.new
for i in 0...n
    if s[i] == 'L' or s[i] == 'R'
        li.push(i)
    end
end
if li.empty?
    puts n
    exit
end
ans = 0
if s[li[0]] == 'R'
    ans += li[0]
end
if s[li[-1]] == 'L'
    ans += n-1-li[-1]
end
for i in 1...li.length
    if (li[i]-li[i-1]).even?
        ans += 1
    end
end
puts ans


You are the gym teacher in the school.There are $$$n$$$ students in the row. And there are two rivalling students among them. The first one is in position $$$a$$$, the second in position $$$b$$$. Positions are numbered from $$$1$$$ to $$$n$$$ from left to right.Since they are rivals, you want to maximize the distance between them. If students are in positions $$$p$$$ and $$$s$$$ respectively, then distance between them is $$$|p - s|$$$. You can do the following operation at most $$$x$$$ times: choose two adjacent (neighbouring) students and swap them.Calculate the maximum distance between two rivalling students after at most $$$x$$$ swaps.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The only line of each test case contains four integers $$$n$$$, $$$x$$$, $$$a$$$ and $$$b$$$ ($$$2 \le n \le 100$$$, $$$0 \le x \le 100$$$, $$$1 \le a, b \le n$$$, $$$a \neq b$$$) — the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively.

For each test case print one integer — the maximum distance between two rivaling students which you can obtain.

NoteIn the first test case you can swap students in positions $$$3$$$ and $$$4$$$. And then the distance between the rivals is equal to $$$|4 - 2| = 2$$$.In the second test case you don't have to swap students. In the third test case you can't swap students.

#!/usr/bin/env ruby
count = gets.chomp.to_i
answers = []
count.times do
  input = gets.chomp.split(' ').map(&:to_i)
  n = input[0]
  x = input[1]
  a = input[2]
  b = input[3]

  distance_after_swaps = (a-b).abs + x

  answers << if distance_after_swaps > n - 1
               n-1
             else
               distance_after_swaps
             end
end

answers.each { |answer| puts answer }

t = gets.to_i
t.times do
  n,x,a,b = gets.split.map(&:to_i)
  p [(a-b).abs + x, n-1].min
end

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

g = gets.chomp.to_i
g.times{
n,x,a,b = gets.split(" ").map{|x|x.to_i}
qw = [a,b]
a = qw.min
b = qw.max

x.times{
if a>1
a-=1
elsif b<n
b+=1
end
}

puts b-a
}

t = gets.to_i
t.times do
  n, x, a, b = gets.split.map &:to_i
  puts [(a - b).abs + x, n - 1].min
end


Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across $$$n$$$ sessions follow the identity permutation (ie. in the first game he scores $$$1$$$ point, in the second game he scores $$$2$$$ points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on $$$[1,2,3]$$$ can yield $$$[3,1,2]$$$ but it cannot yield $$$[3,2,1]$$$ since the $$$2$$$ is in the same position. Given a permutation of $$$n$$$ integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed $$$10^{18}$$$.An array $$$a$$$ is a subarray of an array $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The first line of each test case contains integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$)  — the length of the given permutation. The second line of each test case contains $$$n$$$ integers $$$a_{1},a_{2},...,a_{n}$$$ ($$$1 \leq a_{i} \leq n$$$)  — the initial permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation.

NoteIn the first permutation, it is already sorted so no exchanges are needed.It can be shown that you need at least $$$2$$$ exchanges to sort the second permutation.$$$[3, 2, 4, 5, 1, 6, 7]$$$Perform special exchange on range ($$$1, 5$$$)$$$[4, 1, 2, 3, 5, 6, 7]$$$Perform special exchange on range ($$$1, 4$$$)$$$[1, 2, 3, 4, 5, 6, 7]$$$

tc = gets.to_i
tc.times do
	n = gets.to_i
	arr = gets.split().map {|i| i.to_i}
	brr = Array.new(n) {0}

	for i in 0...n
		if(i+1 == arr[i])
			brr[i] = 1
		end
	end
	f = l = -1
	for i in 0...n
		if(brr[i] == 0)
			f = i
			break
		end
	end
	for i in (n..0).step(-1)
		if(brr[i] == 0)
			l = i
			break
		end
	end
	ans = 0
	if(f != -1)
		ans += 1
		for i in f..l
			if(brr[i] == 1)
				ans += 1
				break
			end
		end
	end
	puts(ans)
end


The blinds are known to consist of opaque horizontal stripes that can be rotated thus regulating the amount of light flowing in the room. There are n blind stripes with the width of 1 in the factory warehouse for blind production. The problem is that all of them are spare details from different orders, that is, they may not have the same length (it is even possible for them to have different lengths)Every stripe can be cut into two or more parts. The cuttings are made perpendicularly to the side along which the length is measured. Thus the cuttings do not change the width of a stripe but each of the resulting pieces has a lesser length (the sum of which is equal to the length of the initial stripe)After all the cuttings the blinds are constructed through consecutive joining of several parts, similar in length, along sides, along which length is measured. Also, apart from the resulting pieces an initial stripe can be used as a blind if it hasn't been cut. It is forbidden to construct blinds in any other way.Thus, if the blinds consist of k pieces each d in length, then they are of form of a rectangle of k × d bourlemeters. Your task is to find for what window possessing the largest possible area the blinds can be made from the given stripes if on technical grounds it is forbidden to use pieces shorter than l bourlemeter. The window is of form of a rectangle with side lengths as positive integers.

The first output line contains two space-separated integers n and l (1 ≤ n, l ≤ 100). They are the number of stripes in the warehouse and the minimal acceptable length of a blind stripe in bourlemeters. The second line contains space-separated n integers ai. They are the lengths of initial stripes in bourlemeters (1 ≤ ai ≤ 100).

Print the single number — the maximal area of the window in square bourlemeters that can be completely covered. If no window with a positive area that can be covered completely without breaking any of the given rules exist, then print the single number 0.

NoteIn the first sample test the required window is 2 × 4 in size and the blinds for it consist of 4 parts, each 2 bourlemeters long. One of the parts is the initial stripe with the length of 2, the other one is a part of a cut stripe with the length of 3 and the two remaining stripes are parts of a stripe with the length of 4 cut in halves.

n,l = gets.chomp.split(/ /).map!{|x| x.to_i}
ar = gets.chomp.split(/ /).map!{|x| x.to_i}
ar.sort!
ar.reverse!
maxsum = 0
for j in l..ar[0]
	sum = 0
	for i in 0..ar.length-1
		if ar[i] >= j
			dv = ar[i] / j
			sum += dv * j
		end
	end
	maxsum = sum if maxsum < sum
end
puts maxsum

n,l=gets.split.map(&:to_i)
res=[]
a=gets.split.map(&:to_i)
l.upto(100){|k|
        ans=0
        a.each{|b|
                ans+=b/k
        }
        res.push(ans*k)
}
p res.max

a,b=gets.split.map(&:to_i)
x=gets.split.map(&:to_i)
p (b..100).map{|l|x.inject(0){|s,n|s+n/l}*l}.max

def get_answer(k)
    s = 0
    $a.each{|x| s += x / k}
    s
end

def max(a, b)
    if a >= b
        a
    else
        b
    end
end

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

m = 0
$a.each{|x| m = max(m, x)}

ans = 0
(l..m).each{|i| ans = max(ans, get_answer(i) * i)}

puts ans

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

l.upto(100) do |x|
  count = arr.map { |a| a / x }.reduce(:+)
  res = [res, count * x].max
end

puts res

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

n, l = gets.split.map { |v| v.to_i }
lenghts = gets.split.map { |v| v.to_i }

surface = 0

(l .. lenghts.max).each do |d|
  k = lenghts.inject(0) { |r, lenght| r + lenght / d }
  surface = max(surface, k * d)
end

puts surface

n,l=gets.split.map(&:to_i)
ans=0
a=gets.split.map(&:to_i)
a.each{|b|
        ans+=b/l
}
p ans*l

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

p l * arr.map { |x| x / l }.reduce(:+)


There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.

The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons. The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.

Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.

NoteFor the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.

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

n = gets.chomp.to_i

pos_lev = []
pos = []
n.times do
  pos_lev << gets.chomp.split(' ').map{|i| i.to_i}
  pos << pos_lev.last[0]
end
pos_lev.sort!
pos.sort!

dp = Array.new(0)
dp[0] = 0
ans = n

n.times do |i|
  l = pos_lev[i][0] - pos_lev[i][1]
  t = lower_bound(pos, l)
  dp[i+1] = dp[t] + (i-t)
  ans = dp[i+1] + (n-i-1) if ans > dp[i+1] + (n-i-1)
end

puts ans

n = gets.chomp.to_i
as = []
bs = {}
n.times do
  a, b = gets.chomp.split.map(&:to_i)
  as << a
  bs[a] = b
end

as.sort!
ar = as.reverse
dp = [0]
is = {}
is[as[0]] = 0
ans = n - 1
for i in 1...n
  a = as[i]
  b = bs[a]
  is[a] = i
  a1 = ar.bsearch { |x| x < a - b }
  if a1.nil?
    dp << i
  else
    i1 = is[a1]
    dp << (i - i1 - 1 + dp[i1])
  end
  ans = [ans, n - i - 1 + dp[i]].min
end
puts ans

n = gets.chomp.to_i
as = []
bs = {}
n.times do
  a, b = gets.chomp.split.map(&:to_i)
  as << a
  bs[a] = b
end

as.sort!
ar = as.reverse
dp0 = [1]
dp1 = [0]
is = {}
is[as[0]] = 0
ans = n - 1
for i in 1...n
  a = as[i]
  b = bs[a]
  is[a] = i
  i0 = i - 1
  dp0 << (1 + [dp0[i0], dp1[i0]].min)
  a1 = ar.bsearch { |x| x < a - b }
  if a1.nil?
    dp1 << i
  else
    i1 = is[a1]
    dp1 << (i - i1 - 1 + [dp0[i1], dp1[i1]].min)
  end
  ans = [ans, n - i - 1 + dp1[i]].min
end
ans = [ans, dp0[-1]].min
puts ans

n = gets.chomp.to_i
as = []
bs = {}
n.times do
  a, b = gets.chomp.split.map(&:to_i)
  as << a
  bs[a] = b
end

as.sort!
ar = as.reverse
dp0 = [1]
dp1 = [0]
is = {}
is[as[0]] = 0
for i in 1...n
  a = as[i]
  b = bs[a]
  is[a] = i
  i0 = i - 1
  dp0 << (1 + [dp0[i0], dp1[i0]].min)
  a1 = ar.bsearch { |x| x < a - b }
  if a1.nil?
    dp1 << i
  else
    i1 = is[a1]
    dp1 << (i - i1 - 1 + [dp0[i1], dp1[i1]].min)
  end
end

puts [dp0[-1], dp1[-1]].min

n = gets.chomp.to_i
as = []
bs = {}
n.times do
  a, b = gets.chomp.split.map(&:to_i)
  as << a
  bs[a] = b
end

as.sort!
ar = as.reverse
dp0 = [1]
dp1 = [0]
is = {}
is[as[0]] = 0
ans = n - 1
for i in 1...n
  a = as[i]
  b = bs[a]
  is[a] = i
  i0 = i - 1
  dp0 << (1 + [dp0[i0], dp1[i0]].min)
  a1 = ar.bsearch { |x| x < a - b }
  if a1.nil?
    dp1 << i
  else
    i1 = is[a1]
    dp1 << (i - i1 - 1 + [dp0[i1], dp1[i1]].min)
  end
  ans = [ans, n - i - 1 + dp0[i], n - i - 1 + dp1[i]].min
end
puts ans


Valera has got a rectangle table consisting of n rows and m columns. Valera numbered the table rows starting from one, from top to bottom and the columns – starting from one, from left to right. We will represent cell that is on the intersection of row x and column y by a pair of integers (x, y).Valera wants to place exactly k tubes on his rectangle table. A tube is such sequence of table cells (x1, y1), (x2, y2), ..., (xr, yr), that:   r ≥ 2;  for any integer i (1 ≤ i ≤ r - 1) the following equation |xi - xi + 1| + |yi - yi + 1| = 1 holds;  each table cell, which belongs to the tube, must occur exactly once in the sequence. Valera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled:   no pair of tubes has common cells;  each cell of the table belongs to some tube. Help Valera to arrange k tubes on his rectangle table in a fancy manner.

The first line contains three space-separated integers n, m, k (2 ≤ n, m ≤ 300; 2 ≤ 2k ≤ n·m) — the number of rows, the number of columns and the number of tubes, correspondingly.

Print k lines. In the i-th line print the description of the i-th tube: first print integer ri (the number of tube cells), then print 2ri integers xi1, yi1, xi2, yi2, ..., xiri, yiri (the sequence of table cells). If there are multiple solutions, you can print any of them. It is guaranteed that at least one solution exists.

NotePicture for the first sample:   Picture for the second sample:

input = gets.split(' ').map{|el| el.to_i}

n = input[0] #строки
m = input[1] #столбцы
k = input[2] #трубы

zigzag = []

x = 1; y = 0

while x <= n
  while y < m
    y += 1
    zigzag << [x, y]
  end

  x += 1
  break if x > n

  while y > 0
    zigzag << [x, y]
    y -= 1
  end

  x += 1
end

while k > 1
  print '2 '
  print zigzag.pop.join(' ')
  print ' '
  print zigzag.pop.join(' ')
  puts ''
  k-=1
end

print zigzag.size
print ' '
print zigzag.join(' ')

n,m,k=gets.split.map(&:to_i)
kk,rr=0,0

ans=Array.new(k){Array.new()}

for i in (1..n)
  for j in (1..m)
    ans[kk] << [i,i&1==1?j:m-j+1]
    kk+=1 if (rr+=1;rr%2==0&&kk<k-1)
  end
end

ans.map{|a| puts "#{a.size} "+a.flatten.join(" ")}

n,m,k = gets.split(" ").map(&:to_i)
cells = n * m
tube_cell = cells/k
leftover = cells % k
odd = true
ans = []
count = tube_cell
(1..n).each do |i|
	range = odd ? (1..m) : m.downto(1)
	range.each do |j|
		if ans.length != k && count == tube_cell
			count = 0
			ans << []
		end
		ans[-1] << i << j
		count += 1
	end
	odd = !odd
end

ans.each do |a|
	puts "#{a.length/2} #{a.join(' ')}"
end

def build(n, m)
  (1..n).map { |i|
    if i % 2 == 1
      (1..m).map { |j| [i, j] }
    else
      (1..m).map { |j| [i, j] }.reverse
    end
  }.flatten 1
end

n,m,k = gets.split.map(&:to_i)
point_sequence = build(n,m)
per = point_sequence.size / k

(0...k).each { |i|
  from = i*per
  to = (i < k-1) ? (i+1)*per : point_sequence.size
  line = point_sequence[from...to].join " "
  puts "#{to-from} #{line}"
}

def build(n, m)
  (0...n).map { |i|
    if i % 2 == 0
      (0...m).map { |j| [i, j] }
    else
      (0...m).map { |j| [i, j] }.reverse
    end
  }.flatten 1
end

n,m,k = gets.split.map(&:to_i)
point_sequence = build(n,m)
per = point_sequence.size / k

(0...k).each { |i|
  from = i*per
  to = (i < k-1) ? (i+1)*per : point_sequence.size
  line = point_sequence[from...to].join " "
  puts "#{to-from} #{line}"
}


You are given a non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string.In a more formal way, you have to find the quantity of tuples (a, b, x, y) such that 1 ≤ a ≤ b < x ≤ y ≤ |s| and substrings s[a... b], s[x... y] are palindromes.A palindrome is a string that can be read the same way from left to right and from right to left. For example, "abacaba", "z", "abba" are palindromes.A substring s[i... j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2... s|s| is a string sisi + 1... sj. For example, substring s[2...4] of string s = "abacaba" equals "bac".

The first line of input contains a non-empty string s which consists of lowercase letters ('a'...'z'), s contains at most 2000 characters.

Output a single number — the quantity of pairs of non-overlapping palindromic substrings of s. Please do not use the %lld format specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d format specifier.

s = gets.chomp
a = [0]*s.size
b = [0]*s.size

s.size.times do |i|
  2.times do |add|
    lf, ri = i,i + add
    while lf >= 0 and ri < s.size and s[lf] == s[ri]
      a[lf] = a[lf] + 1
      b[ri] = b[ri] + 1
      lf = lf - 1
      ri = ri + 1
    end
  end
end
sum = 0
ans = 0
s.size.times do |i|
  ans += sum*a[i]
  sum += b[i]
end
puts ans

def count_palindromes(i, j)
  return if i < 0 || j >= @n

  if @string[i] == @string[j]

    @starting[i] += 1
    @ending[j] += 1

    count_palindromes(i - 1, j + 1)
  end
end

@string = gets.strip
@n = @string.length

@starting = Array.new(@n) { 1 }
@ending = Array.new(@n) { 1 }

(1...@n).each do |i|
  count_palindromes(i - 1, i)
  count_palindromes(i - 1, i + 1)
end

@ending_sums = Array.new(@n) { 0 }
@ending_sums[0] = @ending[0]
(1...@n).each do |i|
  @ending_sums[i] = @ending_sums[i - 1] + @ending[i]
end

# puts @starting, "", @ending, "", @ending_sums

result = 0
(1...@n).each do |i|
  result += @ending_sums[i - 1] * @starting[i]
end

puts result


A continued fraction of height n is a fraction of form . You are given two rational numbers, one is represented as  and the other one is represented as a finite fraction of height n. Check if they are equal.

The first line contains two space-separated integers p, q (1 ≤ q ≤ p ≤ 1018) — the numerator and the denominator of the first fraction. The second line contains integer n (1 ≤ n ≤ 90) — the height of the second fraction. The third line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1018) — the continued fraction. Please, do not use 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 "YES" if these fractions are equal and "NO" otherwise.

NoteIn the first sample .In the second sample .In the third sample .

require"mathn"
p,q=gets.split.map(&:to_i)
n=gets.to_i
a=gets.split.map(&:to_i)
frac=a.last
a[0..-2].reverse.each{|aa|frac=aa+1/frac}
puts p/q==frac ? "YES" : "NO"

require"mathn"
p,q = gets.split().map {|x| x.to_i}
n = gets.to_i
a = gets.split().map {|x| x.to_i}
a = a.reverse
res = a[0];
a[1..-1].each {|x| res = x+1/res}

puts (res == p/q)? "YES" : "NO"

require"mathn"
p, q = gets.split(' ').map { |i| i.to_i }
n = gets.to_i
a = gets.split(' ').map { |i| i.to_i }
r = a.last
a[0..-2].reverse.each { |e|  r = e + 1 / r;}
puts p / q == r ? "YES" : "NO"

require 'mathn'

class B
  def initialize
    p, q = gets.chomp.split(' ').map(&:to_i)
    n = gets.chomp
    num_list = gets.chomp.split(' ').map(&:to_i)

    res = calc(num_list.shift, num_list)
    if res == p/q
      puts "YES"
    else
      puts "NO"
    end
  end

  def calc(a, b)
    if b.size == 0
      return a
    else
      a + 1/calc(b.shift, b)
    end
  end
end

b = B.new

require "mathn"

loop do
  line = gets
  break if line.nil?
  p,q = line.split(" ").map(&:to_i)
  n = gets.to_i
  as = gets.split(" ").map(&:to_i)
  cal = as.reverse.inject(0){ |res, val|
    res **= -1 unless res.zero?
    res + val
  }
  if cal == p/q
    puts "YES"
  else
    puts "NO"
  end
end

p, q = gets.split.map(&:to_i)
n = gets.to_i
a = gets.split.reverse.map(&:to_i)

x = Rational(0)
for i in 0 .. n-2 do
    x += Rational(a[i], 1)
    x = Rational(1, x)
end
x += Rational(a[n-1], 1)

if Rational(p, q) == x then
    puts "YES"
else
    puts "NO"
end

p,q = gets.chomp.split(" ").collect {|x| x.to_i}
n = gets.to_i
arr = gets.chomp.split(" ").collect {|x| x.to_i}

if arr[-1]==1 and arr.length>1
  arr[-2] += 1
  arr.delete_at(n-1)
end

ok = true
nt = p
aux = -1
arr.each { |v|
  if q == 0
    ok = false
    break
  end
  nt = p/q
  if (nt!=v)
    ok = false
    break
  end
  aux = p%q
  p = q
  q = aux

}

if ok and aux == 0
  puts "YES"
else
  puts "NO"
end

def rec(ary)
	return Rational(1, ary.last) if ary.size == 1
	Rational(1, ary.first + rec(ary.drop(1)))
end

f1 = Rational(*gets.split.map(&:to_i))
gets
f2 = rec(gets.split.map(&:to_i))

if f1 == Rational(f2.denominator, f2.numerator)
	puts "YES"
else
	puts "NO"
end

p,q=gets.split.map(&:to_i)
n=gets.to_i
a=[]
a=gets.split.map(&:to_i)
s=1
b=0
i = n
while i>0
i=i-1
ks=b
kb=s
s=kb*a[i]+ks
b=kb
end
if s*q==b*p
print "YES"
end
if s*q!=b*p
puts "NO"
end

def solve(a, p, q)
  if a.size == 1
    return p == q * a.first ? "YES" : "NO"
  end
  t = a.shift
  solve(a,q, (p - t * q))
end

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

puts solve(a,p,q)

p, q = gets.split.map(&:to_i)
d = p.gcd(q)
p, q = p / d, q / d
n = gets.to_i
a = gets.split.map(&:to_i)
x, y = a[n-1], 1
(n-2).downto(0) do |i|
	x, y = y, x
	x = x + a[i] * y
end
if x == p && y == q
	puts "YES"
else
	puts "NO"
end

#!/usr/bin/ruby

class Fraction
  @num
  @den

  def initialize(a, b)
    @num = a
    @den = b
  end

  def num=(num)
    @num = num
  end

  def num
    @num
  end

  def den=(den)
    @den = den
  end

  def den
    @den
  end
end

def cf(pos)
  return Fraction.new(1, $nums[pos]) if pos + 1 == $n

  res = cf(pos + 1)
  temp = Fraction.new(res.num + res.den * $nums[pos], res.den)
  return Fraction.new(temp.den, temp.num)
end

tokens = gets.split(" ")
p = tokens[0].to_i
q = tokens[1].to_i
$n = gets.to_i

tokens = gets.split(" ")
$nums = Array.new($n)

0.upto(tokens.length - 1) do |i|
  $nums[i] = tokens[i].to_i
end

result = cf(0)

g = p.gcd(q)
#puts g

f = result.num.gcd(result.den)
#puts f

p /= g
q /= g

a = result.den / f
b = result.num / f

if b == q and a == p
  puts "YES"
else
  puts "NO"
end

(p,q) = gets.split(' ').map{|i| i.to_i()}
n = gets.to_i()
a = gets.split(' ').map{|i| i.to_i()}
x,y = 1,0

a.reverse_each{|i| x,y = y+x*i,x}

puts ((x*q==y*p)?"YES":"NO")

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

r = Rational(0)
for b in a.reverse
  r = 1 / (b + r)
end

if r == Rational(q, p)
  puts "YES"
else
  puts "NO"
end

p,q = gets.split().map {|x| x.to_i}
n = gets.to_i
a = gets.split().map {|x| x.to_i}
a.reverse!
res = a[0];
a[1..-1].each {|x| res = x+1.0/res}
puts (res == 1.0*p/q)? "YES" : "NO"

p,q = gets.split().map {|x| x.to_i}
n = gets.to_i
a = gets.split().map {|x| x.to_i}
a.reverse!
res = a[0];
a[1..-1].each {|x| res = x+1/res}
puts (res == p/q)? "YES" : "NO"

p, q = gets.split(' ').map { |i| i.to_f }
n = gets.to_i
a = gets.split(' ').map { |i| i.to_f }
r = a.last
a[0..-2].reverse_each { |e|  r = e + 1.0 / r;}
puts p / q == r ? "YES" : "NO"

p, q = gets.split(' ').map {|i| i.to_i}
n = gets.to_i
a = gets.split(' ').map {|i| i.to_i}
def fun(arr, ind)
    if(ind == arr.size - 1)
        return [1, arr[ind]]
    end
    ret = fun(arr, ind + 1)
    return [ret[1], ret[0] + arr[ind] * ret[1]]
end
ret = fun(a, 0)
if p == ret[1] and q == ret[0] then puts "YES"
else puts "NO"
end

p, q = gets.split(' ').map { |i| i.to_f }
n = gets.to_i
a = gets.split(' ').map { |i| i.to_f }
r = a.last
a[0..-2].reverse.each { |e|  r = e + 1 / r;}
puts p / q == r ? "YES" : "NO"

class B
  def initialize
    p, q = gets.chomp.split(' ').map(&:to_f)
    n = gets.chomp
    num_list = gets.chomp.split(' ').map(&:to_f)

    res = calc(num_list.shift, num_list)
    if res == p/q
      puts "YES"
    else
      puts "NO"
    end
  end

  def calc(a, b)
    if b.size == 0
      return a
    else
      a + 1/calc(b.shift, b)
    end
  end
end

b = B.new

#!/usr/bin/ruby

class Fraction
  @num
  @den

  def initialize(a, b)
    @num = a
    @den = b
  end

  def num=(num)
    @num = num
  end

  def num
    @num
  end

  def den=(den)
    @den = den
  end

  def den
    @den
  end
end

def cf(pos)
  return Fraction.new(1, $nums[pos]) if pos + 1 == $n

  res = cf(pos + 1)
  temp = Fraction.new(res.num + res.den * $nums[pos], res.den)
  return Fraction.new(temp.den, temp.num)
end

tokens = gets.split(" ")
p = tokens[0].to_i
q = tokens[1].to_i
$n = gets.to_i

tokens = gets.split(" ")
$nums = Array.new($n)

0.upto(tokens.length - 1) do |i|
  $nums[i] = tokens[i].to_i
end

result = cf(0)

if result.num == q and result.den == p
  puts "YES"
else
  puts "NO"
end


You are playing a new famous fighting game: Kortal Mombat XII. You have to perform a brutality on your opponent's character.You are playing the game on the new generation console so your gamepad have $$$26$$$ buttons. Each button has a single lowercase Latin letter from 'a' to 'z' written on it. All the letters on buttons are pairwise distinct.You are given a sequence of hits, the $$$i$$$-th hit deals $$$a_i$$$ units of damage to the opponent's character. To perform the $$$i$$$-th hit you have to press the button $$$s_i$$$ on your gamepad. Hits are numbered from $$$1$$$ to $$$n$$$.You know that if you press some button more than $$$k$$$ times in a row then it'll break. You cherish your gamepad and don't want to break any of its buttons.To perform a brutality you have to land some of the hits of the given sequence. You are allowed to skip any of them, however changing the initial order of the sequence is prohibited. The total damage dealt is the sum of $$$a_i$$$ over all $$$i$$$ for the hits which weren't skipped.Note that if you skip the hit then the counter of consecutive presses the button won't reset.Your task is to skip some hits to deal the maximum possible total damage to the opponent's character and not break your gamepad buttons.

The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2 \cdot 10^5$$$) — the number of hits and the maximum number of times you can push the same button in a row. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the damage of the $$$i$$$-th hit. The third line of the input contains the string $$$s$$$ consisting of exactly $$$n$$$ lowercase Latin letters — the sequence of hits (each character is the letter on the button you need to press to perform the corresponding hit).

Print one integer $$$dmg$$$ — the maximum possible damage to the opponent's character you can deal without breaking your gamepad buttons.

NoteIn the first example you can choose hits with numbers $$$[1, 3, 4, 5, 6, 7]$$$ with the total damage $$$1 + 16 + 18 + 7 + 2 + 10 = 54$$$.In the second example you can choose all hits so the total damage is $$$2 + 4 + 1 + 3 + 1000 = 1010$$$.In the third example you can choose all hits expect the third one so the total damage is $$$2 + 4 + 3 + 1000 = 1009$$$.In the fourth example you can choose hits with numbers $$$[2, 3, 6, 8]$$$. Only this way you can reach the maximum total damage $$$15 + 2 + 8 + 16 = 41$$$.In the fifth example you can choose only hits with numbers $$$[2, 4, 6]$$$ with the total damage $$$18 + 19 + 15 = 52$$$.In the sixth example you can change either first hit or the second hit (it does not matter) with the total damage $$$10$$$.

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

res = 0
last = h[0]
tmp = []
idx = 0

while idx < a.size
  if idx.zero?
    tmp << a[idx]
    idx += 1
    next
  end
  if h[idx] != h[idx - 1]
    res += tmp.size.zero? ? 0 : tmp.sort.reverse.slice(0, k).inject(:+)
    tmp = [a[idx]]
  else
    tmp << a[idx]
  end
  idx += 1
end
if tmp.size > 0
  res += tmp.size.zero? ? 0 : tmp.sort.reverse.slice(0, k).inject(:+)
end
puts res

if File.exists?("input")
  $stdin = File.open("input")
  $stdout = File.open("output","w")
end

#use moving window to get the sum
nk = gets.chomp.split(" ").map(&:to_i)
n = nk[0]
k = nk[1]
a = gets.chomp.split(" ").map(&:to_i)
s = gets.chomp

arr = [0]
breakpoints = 2
(1..n-1).each do |i|
	if s[i]!=s[i-1]
		breakpoints+=1
		arr.push i
	end
end
arr.push n

totsum = 0
#bigarr = []
=begin

(0..breakpoints-2).each do |st|
	newarr = []
	newlen = 1
	min = 1.0/0.0
	sum = 0
	max = 0
	(arr[st]..arr[st+1]-1).each do |range|
		this = a[range]
#		p newlen
		if(newlen<=k)
			newarr.push this
			sum+=this
			min = this if this<min
			newlen+=1
		else
			if (this>min)
				sum-=min
				sum+=this
				newarr.push this
				newarr.delete_at(newarr.index(min))
				min = newarr.min
			end
		end
		max = sum if sum>max
	end
	totsum += sum
end
=end
#p arr
(0..breakpoints-2).each do |st|
	newarr = a[arr[st]..arr[st+1]-1].sort
#	p newarr
	initi = newarr.length-1
	i = initi
	min = arr[st]
	sum = 0
#	p initi
	while true
		break if (initi-i+1)>k
		break if i<0
#		p i
		sum+=newarr[i]
		i -= 1
#		p sum
	end
#	p sum
	totsum += sum
end

#p arr
puts totsum

if File.exists?("input")
  $stdin = File.open("input")
  $stdout = File.open("output","w")
end

#use moving window to get the sum
nk = gets.chomp.split(" ").map(&:to_i)
n = nk[0]
k = nk[1]
a = gets.chomp.split(" ").map(&:to_i)
s = gets.chomp

arr = [0]
breakpoints = 2
(1..n-1).each do |i|
	if s[i]!=s[i-1]
		breakpoints+=1
		arr.push i
	end
end
arr.push n

bigarr = []
(0..breakpoints-2).each do |st|
	newarr = []
	newlen = 1
	min = 1.0/0.0
	sum = 0
	(arr[st]..arr[st+1]-1).each do |range|
		this = a[range]
#		p newlen
		if(newlen<=k)
			newarr.push this
			sum+=this
#			min = this if this<min
			newlen+=1
		else
			if (this>min)
				sum-=newarr.pop
				sum+=this
				newarr.push this
#				newarr.delete_at(newarr.index(min))
#				min = newarr.min
			end
		end
		newarr.sort!
		bigarr.push sum
	end
end

totsum = 0
(0..breakpoints-2).each do |st|
	totsum += bigarr[arr[st]..arr[st+1]-1].max
end

#p arr
#p bigarr
puts totsum