n=gets.to_i
a=[]
0.upto(n-1) do |i|
  a[i]=gets.chomp.scan(/\w+/)
end
for i in 0..n-1 do
  for j in 0..n-1 do
    a[i][j]=a[i][j].to_i
  end
end
b=[]
for i in 0..n-1 do
  b[i]=0
  for j in 0..n-1 do
    b[i]+=a[i][j]
  end
end
c=[]
for j in 0..n-1 do
  c[j]=0
  for i in 0..n-1 do
    c[j]+=a[i][j]
  end
end
k=0
for i in 0..n-1 do
  for j in 0..n-1 do
    if (b[i]<c[j]) then k+=1 end
  end
end
puts k

n = gets.to_i
ary = Array.new(n).map!{ Array.new(n, 0) }
for r in 0..n-1
  line = gets
  s = line.split(' ')
  for c in 0..n-1
    ary[r][c] = s[c].to_i
  end
end
cnt = 0
for r in 0..n-1
  for c in 0..n-1
    col = 0
    row = 0
    for i in 0..n-1
      col += ary[i][c]
    end
    for i in 0..n-1
      row += ary[r][i]
    end
    if col > row
      cnt += 1
    end
  end
end
puts cnt

n = gets.to_i
ary = Array.new(n).map!{ Array.new(n, 0) }
n.times { |r|
  s = gets.split.map(&:to_i)
  n.times { |c|
    ary[r][c] = s[c]
  }
}
cnt = 0
n.times { |r|
  n.times { |c|
    col = 0
    row = 0
    n.times { |i|
      col += ary[i][c]
    }
    n.times { |i|
      row += ary[r][i]
    }
    cnt += 1 if col > row
  }
}
p cnt

n = gets.to_i
ary = Array.new(n).map!{ Array.new(n, 0) }
n.times { |r|
  s = gets.split.map{ |e| e.to_i }
  n.times { |c|
    ary[r][c] = s[c]
  }
}
cnt = 0
n.times { |r|
  n.times { |c|
    col = 0
    row = 0
    n.times { |i|
      col += ary[i][c]
    }
    n.times { |i|
      row += ary[r][i]
    }
    if col > row; cnt += 1; end;
  }
}
p cnt

def winning?(rowpos, colpos, elem, game)

	# check row sum
	rowsum = 0
	$n.times do |i|
		rowsum += game[rowpos][i].to_i
	end

	# check col sum
	colsum = 0
	$n.times do |i|
		colsum += game[i][colpos].to_i
	end

	colsum > rowsum
end

$n = STDIN.gets.chomp.to_i
game = []
$n.times do |i|
	game << STDIN.gets.chomp.split(' ').map{|ch| ch.to_i}
end

count = 0
rowpos = 0
game.each do |row|
	colpos = 0
	row.each do |elem|
		count += 1 if winning?(rowpos, colpos, elem, game)
		colpos += 1
	end
	rowpos += 1
end

puts count

n = gets.to_i
board = []
sumg = []
sumv = Array.new(n){|i| i = 0}
n.times do |i|
    s = gets.split.map{|i| i.to_i}
    sum = 0
    s.map{|i| sum +=i}
    sumg << sum
    n.times do |j|
        sumv[j] += s[j]
    end
end
res = 0
n.times do |i|
    n.times do |j|
        if sumg[i] < sumv[j] then
            res += 1
        end
    end
end

puts res

n = gets.to_i
a = Array.new(n){gets.split.map &:to_i}
c = 0
n.times{|i|
    n.times{|j|
        x, y = 0, 0
        n.times{|k|
            x += a[i][k]
            y += a[k][j]
        }
        c += 1 if x < y
    }
}
p c

n = gets.to_i
map = []
n.times do
        map << gets.split.map(&:to_i)
end
ans = 0
n.times do |x|
        n.times do |y|
                s1, s2 = 0, 0
                n.times do |i|
                        s1+=map[i][x]
                        s2+=map[y][i]
                end
                ans += 1 if s1>s2
        end
end
p ans

n = gets.chomp.to_i

field = []
n.times do
  field << gets.chomp.split(" ").map{|e| e.to_i}
end

win = 0

field.each_with_index do |column, col_no|
  column_sum = column.inject(0){|sum, i| sum += i}

  column.each_with_index do |e, row_no|
    row_sum = 0
    0.upto(field.length - 1) do |a|
      row_sum += field[a][row_no]
    end

    if row_sum > column_sum
      win += 1
    end
  end
end

puts win

def win? i, j
  $array[i].inject(:+) < $array.transpose[j].inject(:+)
end
ind = (0...gets.to_i).to_a
$array = readlines.map(&:split).each { |a| a.map!(&:to_i) }
puts ind.product(ind).count { |i, j| win? i, j }

n = gets.to_i
row_sum = []
col_sum = Array.new(n) { 0 }
n.times do
	sum = 0
	gets.split("\s").map { |x| x.to_i}.each_with_index do |x, i|
		col_sum[i] += x
		sum += x
	end
	row_sum << sum
end

cells = 0
j = 0
col_sum.sort!
row_sum.sort.each_with_index do |r, i|
	while j < n
		if r < col_sum[j]
			break
		else
			j += 1
			cells += i
		end
	end
end
cells += n * (n - j)
puts cells

n=gets.chomp.to_i
sumcolumns=Array.new(n,0)
sumrows=Array.new(n,0)
for i in 0..(n-1)
 temp= gets.chomp.split.collect{|a| a.to_i}
 sumrows[i]=temp.reduce(:+)
 temp.each_index{|a| sumcolumns[a]=sumcolumns[a]+temp[a]}
end
count=0
sumcolumns.each{|a|count=count+sumrows.count{|b| a>b }}
print count

n = gets.to_i
nss = n.times.map{gets.split.map(&:to_i)}

answer = 0.upto(n-1).map do |i|
  0.upto(n-1).count do |j|
    nss[i].reduce(:+) < nss.map{|row| row[j]}.reduce(:+)
  end
end.reduce(:+)

p answer

public

def solve(matrix)
  lines = {}
  cols = {}

  matrix.each_with_index do |x,i|
    lines[i] = x.inject(0){|z,s| s + z}
  end

  transpose(matrix).each_with_index do |x,i|
    cols[i] = x.inject(0){|z,s| s + z}
  end

  size = matrix[0].count
  win = 0
  (0...size).each do |i|
      (0...size).each do |j|
        win += 1if cols[i] > lines[j]
      end
  end

  win
end

def transpose(matrix)
  size = matrix[0].count
  transponent = []

  (0...size).each do |i|
      transponent[i] = [1]*size
  end

  (0...size).each do |i|
      (0...size).each do |j|
        transponent[i][j] = matrix[j][i]
      end
  end
  transponent
end

def read(input)
  input.split("\n")
end

def plow(list)
  list.collect{|y| y.split(" ").collect{|x| x.to_i}}[1..-1]
end

if __FILE__ == $PROGRAM_NAME
  print solve(plow(read(STDIN.read)))
end

n = gets.chomp.to_i
squares = Array.new(n) { gets.split.collect{|x| x.to_i} }

winning = 0
(0..n-1).each{|i|
  row_sum = 0
  squares[i].collect{|x| row_sum += x}
  (0..n-1).each{|j|
    col_sum = 0
    squares.each{|s|
      col_sum += s[j]
    }
    winning += 1 if col_sum > row_sum
  }
}

puts winning

n = gets.to_i
board = []
for i in 0..(n-1)
	board[i] = []
	line = gets.split
	for j in 0 .. (n-1)
		board[i] << line[j].to_i
	end
end

row = []
col = []
for i in 0..n-1
	rowSum = 0
	colSum = 0
	for j in 0..n-1
		rowSum += board[i][j]
		colSum += board[j][i]
	end
	row[i] = rowSum
	col[i] = colSum
end

count = 0
for i in 0..n-1
	for j in 0..n-1
		if(col[i] > row[j])
			count += 1
		end
	end
end

puts count

n = gets.chomp.to_i
b = []
row_sums = []
col_sums = []
for i in 0...n
  b[i] = gets.chomp.split.map { |x| x.to_i }
  row_sums[i] = b[i].inject(:+)
  for j in 0...n
    col_sums[j] = (col_sums[j] || 0) + b[i][j]
  end
end

count = 0
for i in 0...n
  for j in 0...n
    count += 1 if col_sums[i] > row_sums[j]
  end
end
puts count

# Codeforces Round #110
# Problem A -- Game Outcome
a = []
n = gets.to_i
n.times { a << gets.split.map(&:to_i) }
rows = (columns = [0] * n).dup
(0...n).each do |i|
    (0...n).each do |j|
        rows[i] += a[i][j]
        columns[j] += a[i][j]
    end
end
puts rows.product(columns).select {|x, y| x < y }.size

board = Array.new(gets.to_i) { gets.split.map(&:to_i) }

rows_sum = board.map {|row| row.reduce(:+) }
cols_sum = board.transpose.map {|col| col.reduce(:+) }

count = 0
rows_sum.each do |rs|
  cols_sum.each do |cs|
    if rs < cs
	  count += 1
	end
  end
end

puts count

n = gets.to_i
board = Array.new
rowSums = Array.new
n.times do
  row = gets.split(" ")
  row = row.map do |e|
    e.to_i
  end
  rowSums.push(row.inject(:+))
  board.push(row)
end

colSums = Array.new

for col in 0...n
  colSum = 0
  for row in 0...n
    colSum += board[row][col]
  end
  colSums[col] = colSum
end

total = 0
for i in 0...n
  for j in 0...n
    if (colSums[j] > rowSums[i])
      total += 1
    end
  end
end

puts total

n = gets.to_i
right = Array.new(n) { 0 }
down = Array.new(n) { 0 }

n.times do |i|
  gets.split.each_with_index do |element, index|
    a =element.to_i
    right[i] = right[i] + a
    down[index] = down[index] + a
  end
end
counter = 0
right.sort!
down.sort!
down.each do |i|
  plus = right.find_index { |j| j>=i }
  if plus
    counter = counter + plus
  else
    counter = counter + right.size
  end
end
p counter

def winning?(rowpos, colpos, elem, game)

	# check row sum
	rowsum = 0
	$n.times do |i|
		rowsum += game[rowpos][i].to_i
	end

	# check col sum
	colsum = 0
	$n.times do |i|
		colsum += game[i][colpos].to_i
	end

	colsum > rowsum
end

$n = STDIN.gets.chomp.to_i
game = []
$n.times do |i|
	game << STDIN.gets.chomp.split(' ').map{|ch| ch.to_i}
end

res = 0
rowpos = 0
game.each do |row|
	colpos = 0
	row.each do |elem|
		res = elem if winning?(rowpos, colpos, elem, game)
		colpos += 1
	end
	rowpos += 1
end

puts res

n = gets.to_i
row_sum = []
col_sum = Array.new(n) { 0 }
n.times do
	sum = 0
	gets.split("\s").map { |x| x.to_i}.each_with_index do |x, i|
		col_sum[i] += x
		sum += x
	end
	row_sum << sum
end

cells = 0
j = 0
col_sum.sort!
row_sum.sort.each_with_index do |r, i|
	while j < n
		if r < col_sum[j]
			break
		else
			j += 1
			cells += i
		end
	end
end

puts cells

n = gets.chomp.to_i
b = []
row_sums = []
col_sums = []
for i in 0...n
  b[i] = gets.chomp.split.map { |x| x.to_i }
  row_sums[i] = b[i].inject(:+)
  for j in 0...n
    col_sums[j] = (col_sums[j] || 0) + b[i][j]
  end
end

count = 0
for i in 0...n
  for j in 0...n
    count += 1 if row_sums[i] > col_sums[j]
  end
end
puts count

n = gets.to_i
right = Array.new(n){0}
down = Array.new(n){0}

n.times do |i|
  gets.split.each_with_index do |element,index|
    a =element.to_i
    right[i] = right[i] + a
    down[index] = down[index] + a
  end
end
counter = 0
right.sort!
down.sort!
down.each do |i|
  plus = right.find_index{|j| j>=i}
  counter = counter + plus unless plus.nil?
end
p counter


t = gets.to_i

(1).upto(t) do
  n = gets.to_i
  a = gets.split(' ').map(&:to_i)
  i = -1
  j = n
  turn = true
  x=0
  y=0
  z=0
  prev = 0
  cur = 0
  while(i+1<j) do
    prev = cur
    cur = 0
    if(turn)
      while(true)
        i+=1
        if(i==j)
          break
        end
        cur+=a[i]
        x+=a[i]
        if(cur>prev)
          break
        end
      end
    else
      while(true)
        j-=1
        if(i==j)
          break
        end
        cur+=a[j]
        y+=a[j]
        if(cur>prev)
          break
        end
      end
    end
    z+=1
    turn = !turn
  end
  puts "#{z} #{x} #{y}"
end

# Created by IntelliJ IDEA.
# User: gautam.roy
# Date: 5/10/20
# Time: 10:29 PM
# To change this template use File | Settings | File Templates.
t = gets.to_i
for i in 1..t do
  n = gets.to_i
  a = gets.split(' ').map { |s| s.to_i }
  atot = 0
  btot = 0
  moves = 0
  lind = 0
  rind = a.size - 1
  bsum = 0
  while lind <= rind
    # a moves
    asum = 0
    while lind <= rind && asum <= bsum
      asum = asum + a[lind]
      lind += 1
    end
    if asum != 0
      moves += 1
      atot += asum
    end
    # puts "lind #{lind} asum #{asum}"

    # b moves
    bsum = 0
    while lind <= rind && bsum <= asum
      bsum = bsum + a[rind]
      rind -= 1
    end
    if bsum != 0
      moves += 1
      btot += bsum
    end
    # puts "rind #{rind} bsum #{bsum}"
  end
  puts "#{moves} #{atot} #{btot}"
end

gets.to_i.times{
    n=gets.to_i
    a=gets.split.map &:to_i
    res=0, 0, 0
    s=t=0
    while a.size>0
        s=0
        s+=a.shift while a.size>0 and s<=t
        res[1]+=s
        res[0]+=1

        break if a.empty?

        t=0
        t+=a.pop while a.size>0 and t<=s
        res[2]+=t
        res[0]+=1
    end

    puts res*' '
}

# coding: utf-8

t = gets.to_i
t.times {
    n = gets.to_i
    moves, remains, alice, bob, last_eaten = 0, 0, 0, 0, 0

    a = gets.split.map { |i|
        i = i.to_i
        remains += i
        i
    }
    turn = :alice
    while !a.empty?
        if remains > last_eaten
            tmp = 0
            while tmp <= last_eaten
                tmp += a.shift if turn == :alice
                tmp += a.pop if turn == :bob
            end

            moves += 1
            remains -= tmp
            last_eaten = tmp
            alice += tmp if turn == :alice
            bob += tmp if turn == :bob

            if turn == :alice
                turn = :bob
            else
                turn = :alice
            end
        else
            break
        end
    end

    # clean-up
    if turn == :alice
        alice += remains
    else
        bob += remains
    end

    # and if remains are available, add one move (cleanup eating)
    if remains > 0
        moves += 1
    end

    puts "#{moves} #{alice} #{bob}"
}

# coding: utf-8

t = gets.to_i
t.times {
    moves, remains, alice, bob, last_eaten = 0, 0, 0, 0, 0

    a = gets.split.map { |i|
        i = i.to_i
        remains += i
        i
    }
    turn = :alice
    while !a.empty?
        if remains > last_eaten
            tmp = 0
            while tmp <= last_eaten
                tmp += a.shift if turn == :alice
                tmp += a.pop if turn == :bob
            end

            moves += 1
            remains -= tmp
            last_eaten = tmp
            alice += tmp if turn == :alice
            bob += tmp if turn == :bob

            if turn == :alice
                turn = :bob
            else
                turn = :alice
            end
        else
            break
        end
    end

    # clean-up
    if turn == :alice
        alice += remains
    else
        bob += remains
    end

    # and if remains are available, add one move (cleanup eating)
    if remains > 0
        moves += 1
    end

    puts "#{moves} #{alice} #{bob}"
}


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

def getCoord st, c, a
	ac = (a-c).round(4)
	c = c.round(4)

	case st
		when 0 then "#{c} 0"
		when 1 then "#{a} #{c}"
		when 2 then "#{a-c} #{a}"
		when 3 then "0 #{a - c}"
	end
end

state = 0
c = 0
cd = d

for i in 0...n
	m = (cd / a).floor
	state = (state + m) % 4
	c = cd - m * a
	cd = c + d

	puts getCoord state, c, a
end

eps = 1e-5
a,d = gets.chomp!.split.map { |e|  e.to_f}
n =gets.chomp!.to_i
cur_x=0.00
cur_y=0.00
pp=0.00

n.times {
	if cur_y <= eps
		pp = cur_x
	#	p 0
	elsif a-cur_x <= eps
		pp = a + cur_y
	#	p 1
	elsif a-cur_y <= eps
		pp = a*3 -cur_x
	#	p 2
	else
		pp = a*4 -cur_y
	#	p 3
	end
	pp +=d;
	t= (pp/a).floor
	# puts "asd   #{pp}  #{t}  #{pp}"
	pp -= 1.0*t*a;

	case t%4
	when 0
		cur_x=pp
		cur_y=0
	when 1
		cur_x=a
		cur_y=pp
	when 2
		cur_x=a-pp
		cur_y=a
	when 3
		cur_x=0
		cur_y=a-pp
	end

	printf("%.8f %.8f\n",cur_x,cur_y)
}


D = [2 ** 24, 2 ** 16, 2 ** 8, 1]

def from_ip(ip)
  ip.split('.').each_with_index.map{ |n, i| n.to_i * D[i] }.reduce(:+)
end

def to_ip(ip)
  4.times.map{ |i| (ip / D[i]).tap{ ip %= D[i] } }.join('.')
end

def mask(n)
  ('1' * n << '0' * (32 - n)).to_i(2)
end

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

ips = []

n.times{ ips << from_ip(gets) }
ips.sort!

32.times do |i|
  next if i == 0 || 2 ** i < k
  m = mask(i)
  net = -1
  c = 0
  ips.each do |ip|
    ip_net = ip & m
    unless ip_net == net
      net = ip_net
      c += 1
    end
  end

  if c == k
    puts to_ip(m)
    exit
  end

end

puts -1

class MaskGuesser
  def initialize num_networks, ip_list
    raise Exception unless ip_list.kind_of?(Array)
    @num_networks = num_networks
    @ip_list = Array.new(ip_list.map {|ip| ip_to_bitmask(ip)})
  end

  def guess_mask
    mask = 0
    31.downto(1) do |i|
      mask += 2**i
      masked_ip_list = @ip_list.map {|ip| ip&mask}.uniq
      return bitmask_to_ip(mask) if masked_ip_list.length == @num_networks
    end
    return "-1"
  end

  def bitmask_to_ip bitmask
    filter = 255
    a = (bitmask & (filter << 24)) >> 24
    b = (bitmask & (filter << 16)) >> 16
    c = (bitmask & (filter << 8)) >> 8
    d = bitmask & filter
    return "#{a}.#{b}.#{c}.#{d}"
  end

  def ip_to_bitmask ip
    bit_segments = ip.split('.').map(&:to_i)
    raise Exception if bit_segments.length != 4
    bitmask = bit_segments[0] << 24 | bit_segments[1] << 16 | bit_segments[2] << 8 | bit_segments[3]
    #bitmask = (bit_segments[0] << 24) + (bit_segments[1] << 16) + (bit_segments[2] << 8) + bit_segments[3]
    return bitmask
  end
end

def main
  num_ip, num_networks = gets.split(/\s+/).map(&:to_i)
  ip_list = []
  num_ip.downto(1) do
    ip_list << gets
  end
  m = MaskGuesser.new(num_networks, ip_list)
  puts m.guess_mask
end

main

at_exit do
  n,k = get_numbers_from_line[0..1]
  ips = []
  n.times do
    octets = gets.strip.split('.').map(&:to_i)
    ips << (0...4).reduce(0){|result,i| result + (octets[i] << (8*i)) }
  end

  ip_and = 1<<32 - 1
  ip_or = 0
  (0...n).each do |i|
    ip_and &= ips[i]
    ip_or |= ips[i]
  end
  ip_xor = ip_and ^ ip_or

  result_mask = '-1'
  mask = 0
  start_i = [Math.log(k,2).ceil - 1, 0].max
  (0...start_i).each do |i|
    octet = i/8
    bit = 7 - i%8
    mask |= 1 << (octet*8 + bit)
  end

  i = start_i
  while i < 32
    octet = i/8
    bit = 7 - i%8
    absolute_bit = octet*8 + bit
    mask |= 1 << absolute_bit
    nets = ips.map{|ip| ip & mask }.uniq
    if nets.length == k
      result_mask = (1..4).map{|j| (mask & ((1<<j*8) - 1)) >> ((j-1)*8)}.join('.')
      break
    elsif nets.length > k
      break
    end

    while true
      i += 1
      break if i >= 32
      octet = i/8
      bit = 7 - i%8
      absolute_bit = octet*8 + bit
      mask |= 1 << absolute_bit
      break if (ip_xor & (1 << absolute_bit)) != 0
    end
  end

  puts result_mask
end

def get_numbers_from_line
  gets.strip.split.map(&:to_i)
end

at_exit do
  n,k = get_numbers_from_line[0..1]
  ips = []
  n.times do
    octets = gets.strip.split('.').map(&:to_i)
    ips << (0...4).reduce(0){|result,i| result + (octets[i] << (8*i)) }
  end

  ip_and = 1<<32 - 1
  ip_or = 0
  (0...n).each do |i|
    ip_and &= ips[i]
    ip_or |= ips[i]
  end
  ip_xor = ip_and ^ ip_or

  result_mask = '-1'
  start_i = Math.log(k,2).ceil - 1
  mask = 0
  (0...start_i).each do |i|
    octet = i/8
    bit = 7 - i%8
    mask |= 1 << (octet*8 + bit)
  end

  i = start_i
  while i < 32
    octet = i/8
    bit = 7 - i%8
    absolute_bit = octet*8 + bit
    mask |= 1 << absolute_bit
    nets = ips.map{|ip| ip & mask }.uniq
    if nets.length == k
      result_mask = (1..4).map{|j| (mask & ((1<<j*8) - 1)) >> ((j-1)*8)}.join('.')
      break
    elsif nets.length > k
      break
    end

    while i < 32
      i += 1
      octet = i/8
      bit = 7 - i%8
      absolute_bit = octet*8 + bit
      mask |= 1 << absolute_bit
      break if (ip_xor & (1 << absolute_bit)) != 0
    end
  end

  puts result_mask
end

def get_numbers_from_line
  gets.strip.split.map(&:to_i)
end

at_exit do
  n,k = get_numbers_from_line[0..1]
  ips = []
  n.times do
    ips << gets.strip.split('.').map(&:to_i)
  end

  result_mask = '-1'
  start_i = Math.log(k,2).floor
  mask = [0]*4
  (0...start_i).each do |i|
    octet = i/8
    bit = 7 - i%8
    mask[octet] |= 1 << bit
  end

  (start_i...32).each do |i|
    octet = i/8
    bit = 7 - i%8
    mask[octet] |= 1 << bit
    nets = ips.map{|ip| get_net_ip(ip, mask) }.uniq
    if nets.length == k
      result_mask = mask.join('.')
      break
    elsif nets.length > k
      break
    end
  end

  puts result_mask
end

def get_net_ip(ip, mask)
  (0...4).map{|i| ip[i] & mask[i] }
end

def get_numbers_from_line
  gets.strip.split.map(&:to_i)
end

at_exit do
  n,k = get_numbers_from_line[0..1]
  ips = []
  n.times do
    ips << gets.strip.split('.').map(&:to_i)
  end

  result_mask = '-1'
  start_i = Math.log(k,2).ceil
  mask = [0]*4
  (0...start_i).each do |i|
    octet = i/8
    bit = 7 - i%8
    mask[octet] |= 1 << bit
  end
  (start_i...32).each do |i|
    octet = i/8
    bit = 7 - i%8
    mask[octet] |= 1 << bit
    nets = ips.map{|ip| get_net_ip(ip, mask) }.uniq
    if nets.length == k
      result_mask = mask.join('.')
      break
    elsif nets.length > k
      break
    end
  end

  puts result_mask
end

def get_net_ip(ip, mask)
  (0...4).map{|i| ip[i] & mask[i] }
end

def get_numbers_from_line
  gets.strip.split.map(&:to_i)
end


t = gets.chomp.to_i
t.times {
  nk = gets.chomp.split(' ').map(&:to_i)
  n = nk[0]
  k = nk[1]
  arr = gets.chomp.split(' ').map(&:to_i)
  max = arr.max
  arr.map! { |ele| max - ele }
  max = arr.max
  if k % 2 == 0
    arr.map! { |ele| max - ele }
  end
  arr.each {|ele| print "%d " % ele }
  print "\n"
}

def get_max(a)
	res = a[0]
	for i in 1..(a.length-1)
		if a[i] > res
			res = a[i]
		end
	end
	res
end

def flip_array!(a)
	m = get_max(a)
	for i in 0..(a.length-1)
		a[i] = m - a[i]
	end
	nil
end

t = gets.chomp.to_i
for tc in 1..t
	a = gets.split
	n = a[0].to_i
	k = a[1].to_i
	a = gets.split
	for i in 0..(n-1)
		a[i] = a[i].to_i
	end
	flip_array!(a)

	if (k%2 == 0)
		flip_array!(a)
	end
	for i in 0..(n-1)
		print(a[i])
		print(" ")
	end
	print("\n")
end

t = gets.chomp.to_i
t.times {
  nk = gets.chomp.split(' ').map(&:to_i)
  n = nk[0]
  k = nk[1]
  arr = gets.chomp.split(' ').map(&:to_i)
  max = arr.max
  arr.map! { |ele| max - ele }
  max = arr.max
  if k % 2 == 0
    arr.map! { |ele| max - ele }
  end
  arr.each {|ele| print "%d " % ele }
  # print "\n"
}

t = gets.chomp.to_i
t.times {
  nk = gets.chomp.split(' ').map(&:to_i)
  n = nk[0]
  k = nk[1]
  arr = gets.chomp.split(' ').map(&:to_i)
  max = arr.max
  arr.map! { |ele| max - ele }
  max = arr.max
  if k % 2 == 0
    arr.map! { |ele| max - ele }
  end
  p arr.join(' ')
}


n = gets.to_i
d = gets.to_i
e = gets.to_i
eu = e*5

min = n
(n/d+1).times do |i|
  x = n - d*i
  break if x<0

  x -= x/eu * eu
  min = x if min>x

  break if min==0
end

puts min

n = gets.to_i
d = gets.to_i
e = gets.to_i

ans = n

(0..(n/d)).each do |dn|
  ans = [ans, (n - (d*dn)) % (e*5)].min
end

puts ans

n = gets.to_i
d = gets.to_i
e = gets.to_i

ans = 10 ** 20
tmp = n % d % (e*5)
tmp2 = n % (e*5) % d
ans = [ans, tmp, tmp2].min
puts ans

n = gets.to_i
d = gets.to_i
e = gets.to_i

ans = n

tmp = n % d % (e*5)
tmp2 = n % (e*5) % d
ans = [ans, tmp, tmp2].min
puts ans


t = readline.strip.to_i

t.times do
  a, b, c = readline.strip.split(/\s+/).map(&:to_i)
  if a == b && b == c
    puts 'YES'
    next
  end

  # case 1 |a| b c
  d = c - b
  if ((b - d) % a == 0) && ((c - 2*d) % a == 0)
    m = (b-d) / a
    if m > 0
      puts 'YES'
      next
    end
  end

  # case a |b| c
  d = c - a
  if d % 2 == 0
    d /= 2
    if ((a+d) % b == 0) && ((c-d) % b == 0)
      m = (a+d) / b
      if m > 0
        puts 'YES'
        next
      end
    end
  end

  # case a b |c|
  d = b - a
  if ((b+d) % c == 0) && ((a+2*d) % c == 0)
    m = (b + d) / c
    if m > 0
      puts 'YES'
      next
    end
  end

  puts 'NO'
end

inputs = STDIN.read.strip.split("\n")
inputs.shift

inputs.each do |inp|
    a, b, c = inp.split(' ').map(&:to_i)

    if a - b  == b - c
        puts "YES"
    else
        if (2 * b - c) % a == 0 && (2 * b - c) / a > 0
            puts "YES"
        elsif (a + c) % (2 * b) == 0 && (a + c) / (2 * b) > 0
            puts "YES"
        elsif (2 * b - a) % c == 0 && (2 * b - a) / c > 0
            puts "YES"
        else
            puts "NO"
        end
    end
end

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

  # Is it `a`?
  d = c - b
  aa = b - d
  if aa % a == 0 && aa / a > 0
    puts "YES"
    next
  end

  # `b`?
  d = c - a
  if d % 2 == 0
    d /= 2
    bb = c - d
    if bb % b == 0 && bb / b > 0
      puts "YES"
      next
    end
  end

  # `c`?
  d = b - a
  cc = b + d
  if cc % c == 0 && cc / c > 0
    puts "YES"
    next
  end

  puts "NO"
end

#!/usr/bin/ruby
input = ARGF.read.each_line(chomp: true).to_a
header = input.shift
input.each do |line|
  z = line.split.map(&:to_i)
  puts ((2*z[1] -z[2] > 0 and (2*z[1] - z[2]) % z[0] == 0) or
    ((z[0] + z[2]) % (2*z[1]) == 0) or
    (2*z[1] -z[0] > 0 and (2*z[1] - z[0]) % z[2] == 0)) ? "YES" : "NO"
end

inputs = STDIN.read.strip.split("\n")
inputs.shift

inputs.each do |inp|
    a, b, c = inp.split(' ').map(&:to_i)

    min = [a,c].min
    max = [a,c].max

    if (a > c || a < c) && max - min > 1
        puts "YES"
    else
        if a == b && b == c
            puts "YES"
        else
            if (a + c) * b == b * 2
                puts "YES"
            elsif (b == c && a * b == b) || (a == b && b * c == b)
                puts "YES"
            else
                puts "NO"
            end
        end
    end
end

inputs = STDIN.read.strip.split("\n")
inputs.shift

inputs.each do |inp|
    a, b, c = inp.split(' ').map(&:to_i)

    if a > c
        a, c = c, a
    end

    # pp "#{a}, #{b}, #{c}"
    x = Hash[[a,b,c].tally.to_a.reverse]
    if x[2].to_i * x[1].to_i == x[2].to_i
        puts "YES"
        next
    end
    if (a == b && b == c) || (a == 1 && c == 1)
        puts "YES"
        next
    end
    if c - a < 2
        puts "NO"
        next
    end
    if a + b != c && a + (c - a) != c
        puts "NO"
        next
    end

    puts "YES"
end

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

  # Is it `a`?
  d = c - b
  aa = b - d
  if aa % a == 0 && aa / a > 0
    puts "YES"
    next
  end

  # `b`?
  d = c - a
  if d % 2 == 0
    bb = c - d
    if bb % b == 0 && bb / b > 0
      puts "YES"
      next
    end
  end

  # `c`?
  d = b - a
  cc = b + d
  if cc % c == 0 && cc / c > 0
    puts "YES"
    next
  end

  puts "NO"
end


class Info
  attr_accessor :dist
  attr_accessor :team

  def initialize (d, t)
    @dist = d
    @team = t
  end
end

gets
first = gets.split(' ').map do |x|
  Info.new(x.to_i, 1)
end

gets
second = gets.split(' ').map do |x|
  Info.new(x.to_i, 2)
end

res = first + second
res.sort_by! { |x| -x.dist }

d = res.max_by { |x| x.dist }.dist + 1

two_points_first, three_points_first  = first.length, 0
two_points_second, three_points_second  = second.length, 0

max_first = two_points_first * 2 + three_points_first * 3
max_second = two_points_second * 2 + three_points_second * 3
diff = max_first - max_second

points = first.map { |x| x.dist } + second.map { |x| x.dist }
points = points.uniq.sort.reverse

current = 0
for i in points
  while current < res.length && res[current].dist >= i do
    if res[current].team == 1
      two_points_first -= 1
      three_points_first += 1
    else
      two_points_second -= 1
      three_points_second += 1
    end

    current += 1
  end

  local_first = two_points_first * 2 + three_points_first * 3
  local_second = two_points_second * 2 + three_points_second * 3

  loc_diff = local_first - local_second

  if loc_diff >= diff
    diff = loc_diff
    max_first, max_second = local_first, local_second
  end

  break if current == res.length
end

puts "#{max_first}:#{max_second}"

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

class Score
  def self.start
    self.new
  end

  def initialize
    aggregate
  end

  def aggregate
    n = gets.chomp.to_i
    @f = Team.new(gets.chomp.split.map(&:to_i))
    m = gets.chomp.to_i
    @s = Team.new(gets.chomp.split.map(&:to_i))
  end

  def result
    f_score = @f.score
    s_score = @s.score
    f = @f.score_l
    s = @s.score_l
    n = f.length
    m = s.length

    ans = -1000000
    diff_score = f_score - s_score
    if ans < f_score - s_score
      ans = diff_score
      ans_f = f_score
      ans_s = s_score
    end

    j = 0
    @s.score_l.length.times do |i|
      s_score -= 1
      while j < n and f[j] <= s[i]
        f_score -= 1
        j += 1
      end
      diff_score = f_score - s_score
      if ans < diff_score
        ans = diff_score
        ans_f = f_score
        ans_s = s_score
      end
    end
    return "#{ans_f}:#{ans_s}"
  end
end

class Team
  attr_reader :score_l, :score
  def initialize(score_l)
    @score_l = score_l.sort
    @score = @score_l.length * 3
  end
end

s = Score.start
puts s.result

class Info
  attr_accessor :dist
  attr_accessor :team

  def initialize (d, t)
    @dist = d
    @team = t
  end
end

gets
first = gets.split(' ').map do |x|
  Info.new(x.to_i, 1)
end

gets
second = gets.split(' ').map do |x|
  Info.new(x.to_i, 2)
end

res = first + second
res.sort_by! { |x| -x.dist }

d = res.max_by { |x| x.dist }.dist + 1

two_points_first, three_points_first  = first.length, 0
two_points_second, three_points_second  = second.length, 0

max_first = two_points_first * 2 + three_points_first * 3
max_second = two_points_second * 2 + three_points_second * 3
diff = max_first - max_second

current = 0
d.downto(1) do |i|
  while current < res.length && res[current].dist >= i do
    if res[current].team == 1
      two_points_first -= 1
      three_points_first += 1
    else
      two_points_second -= 1
      three_points_second += 1
    end

    local_first = two_points_first * 2 + three_points_first * 3
    local_second = two_points_second * 2 + three_points_second * 3

    loc_diff = local_first - local_second

    if loc_diff >= diff
      diff = loc_diff
      max_first, max_second = local_first, local_second
    end

    current += 1
  end

  break if current == res.length
end

puts "#{max_first}:#{max_second}"

class Info
  attr_accessor :dist
  attr_accessor :team

  def initialize (d, t)
    @dist = d
    @team = t
  end
end

gets
first = gets.split(' ').map do |x|
  Info.new(x.to_i, 1)
end

gets
second = gets.split(' ').map do |x|
  Info.new(x.to_i, 2)
end

res = first + second
res.sort_by! { |x| -x.dist }

d = res.max_by { |x| x.dist }.dist + 1

two_points_first, three_points_first  = first.length, 0
two_points_second, three_points_second  = second.length, 0

max_first = two_points_first * 2 + three_points_first * 3
max_second = two_points_second * 2 + three_points_second * 3
diff = max_first - max_second

current = 0
d.downto(1) do |i|
  while current < res.length && res[current].dist >= i do
    if res[current].team == 1
      two_points_first -= 1
      three_points_first += 1
    else
      two_points_second -= 1
      three_points_second += 1
    end

    local_first = two_points_first * 2 + three_points_first * 3
    local_second = two_points_second * 2 + three_points_second * 3

    loc_diff = local_first - local_second

    if loc_diff > diff
      diff = loc_diff
      max_first, max_second = local_first, local_second
    end

    current += 1
  end

  break if current == res.length
end

puts "#{max_first}:#{max_second}"


t = gets.to_i
t.times.each do
  n = gets.to_i
  as = gets.split.map(&:to_i)
  if as.min < 0
    puts 'no'
    next
  end

  g = as[0]
  as.each {|a| g = a.gcd(g) }
  max = as.max
  min = [as.min, g].min
  cnt = (max - min) / g + 1
  if cnt > 300
    puts 'no'
  else
    puts 'yes'
    puts cnt
    puts (min..max).step(g).map {|i| i }.join(' ')
  end
end

$, = $\ = "\n"
gets.to_i.times do
    gets
    a = gets.split.map &:to_i
    if a.min < 0
        print "NO"
    else
        print "YES", a.max + 1, (0..a.max).to_a.join(" ")
    end
end

gets.to_i.times do
    gets
    a = gets.split.map &:to_i
    if a.min < 0
        puts "NO"
    else
        puts "YES"
        puts a.max + 1, (0..a.max).to_a.join(" ")
    end
end

gets.to_i.times do
    gets
    a = gets.split.map &:to_i
    if a.min < 0
        puts "NO"
    else
        puts "YES", a.max + 1, (0..a.max).to_a.join(" ")
    end
end


=begin

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

=end

t=gets.to_i
for i in 1..t
    n =gets.to_i
    s=gets
    maks=s.split.map{|x|x.to_i}
    ones=0
    etternull=0
    har=0
    sum=0
    ending =0
    prev=maks[0]
    for j in 0..(n-1)
        if (har==1)
            if maks[j]==1
                ones+=1
                ending =0
            else
                sum+=ones
                etternull+=1
                ending+=1
            end
        else
            if maks[j]==1
                ones+=1
                etternull-=1
                ending=0
            else
                sum+=ones
                har =1
                ending+=1
            end
        end
    end
    puts [sum, sum+etternull, sum+ones-ending-1].max
end

main = -> {
  n = gets.to_i
  a = gets.split.map(&:to_i)

  return 0 if n == 1

  zeros = [0] * (n + 1)
  ones = [0] * (n + 1)
  n.times do |i|
    zeros[i + 1] = zeros[i]
    zeros[i + 1] += 1 if a[i] == 0
    ones[i + 1] = ones[i]
    ones[i + 1] += 1 if a[i] == 1
  end

  l_zero = a.index(0)
  l_one = a.index(1)
  r_zero = a.rindex(0)
  r_one = a.rindex(1)

  original = 0
  n.times do |i|
    original += zeros[n] - zeros[i] if a[i] == 1
  end
  ans = original

  tries = []
  tries << l_zero if l_zero
  tries << l_one if l_one
  tries << r_zero if r_zero
  tries << r_one if r_one

  tries.each do |x|
    a[x] ^= 1
    res = original
    if a[x] == 1
      res += zeros[n] - zeros[x + 1]
      res -= ones[x] - ones[0]
    else
      res += ones[x] - ones[0]
      res -= zeros[n] - zeros[x + 1]
    end
    ans = res if ans < res
    a[x] ^= 1
  end

  ans
}

t = gets.to_i
puts t.times.map{ main.call }.join("\n")

gets.to_i.times{
    n=gets.to_i
    a=gets.split.map(&:to_i)
    b=a.clone
    c=a.clone
    if b.count(1)>0
        b[b.rindex(1)]=0
    end
    if c.count(0)>0
        c[c.index(0)]=1
    end
    counta=0
    countb=0
    countc=0
    ca=0
    cb=0
    cc=0
    n.times{|i|
        if a[i]==1
            ca+=1
        else
            counta+=ca
        end
        if b[i]==1
            cb+=1
        else
            countb+=cb
        end
        if c[i]==1
            cc+=1
        else
            countc+=cc
        end
    }
    print([counta,countb,countc].max,"\n")
}

gets.to_i.times{
    n = gets.to_i
    x = gets.split
    ones = (x.join('')).count('1')
    zeros = n-ones
    zeros1 = zeros
    initial = 0
    (n-1).times{|i|
        if x[i]=='1'
            initial+=zeros1
        else
            zeros1-=1
        end
    }
    y = x.reverse
    mx = 0
    n.times{|i|
        if x[i]=='0'
            a = -i + x[(i+1)..].count('0')
            mx = [mx,a].max
            break
        end
    }
    n.times{|i|
        if y[i]=='1'
            a = -i + y[(i+1)..].count('1')
            mx = [mx,a].max
            break
        end
    }
    p initial+mx
}

main = -> {
  n = gets.to_i
  a = gets.split.map(&:to_i)

  return 1 if n == 1

  l_zero = a.index(0)
  l_one = a.index(1)
  r_zero = a.rindex(0)
  r_one = a.rindex(1)

  ans = n_inversions(a)

  tries = []
  tries << l_zero if l_zero
  tries << l_one if l_one
  tries << r_zero if r_zero
  tries << r_one if r_one

  tries.each do |x|
    a[x] ^= 1
    res = n_inversions(a)
    ans = res if ans < res
    a[x] ^= 1
  end

  ans
}

def n_inversions(seq)
  n = seq.size
  b = [0] * (n + 1)

  res = 0
  n.times do |i|
    s = 0
    j = seq[i] + 1
    while j > 0
      s += b[j]
      j -= (j & -j)
    end

    res += i - s

    j = seq[i] + 1
    while j < n
      b[j] += 1
      j += (j & -j)
    end
  end

  res
end

t = gets.to_i
puts t.times.map{ main.call }.join("\n")

main = -> {
  n = gets.to_i
  a = gets.split.map(&:to_i)

  return 0 if n == 1

  l_zero = a.index(0)
  l_one = a.index(1)
  r_zero = a.rindex(0)
  r_one = a.rindex(1)

  ans = n_inversions(a)

  tries = []
  tries << l_zero if l_zero
  tries << l_one if l_one
  tries << r_zero if r_zero
  tries << r_one if r_one

  tries.each do |x|
    a[x] ^= 1
    res = n_inversions(a)
    ans = res if ans < res
    a[x] ^= 1
  end

  ans
}

def n_inversions(seq)
  n = seq.size
  b = [0] * (n + 1)

  res = 0
  n.times do |i|
    s = 0
    j = seq[i] + 1
    while j > 0
      s += b[j]
      j -= (j & -j)
    end

    res += i - s

    j = seq[i] + 1
    while j < n
      b[j] += 1
      j += (j & -j)
    end
  end

  res
end

t = gets.to_i
puts t.times.map{ main.call }.join("\n")

gets.to_i.times{
    n=gets.to_i
    s=gets.split.map(&:to_i)
    if s.count(1)>s.count(0)
        s[s.rindex(1)]=0
    elsif s.count(1)<s.count(0)
        s[s.index(0)]=1
    else
        s[s.rindex(1)]=0
    end
    count=0
    c=0
    s.size.times{|i|
        if s[i]==1
            c+=1
        else
            count+=c
        end
    }
    print(count,"\n")
}

gets.to_i.times{
    n = gets.to_i
    x = gets.split
    ones = (x.join('')).count('1')
    zeros = n-ones
    zeros1 = zeros
    initial = 0
    (n-1).times{|i|
        if x[i]=='1'
            initial+=zeros1
        else
            zeros1-=1
        end
    }
    y = []
    osf = 0
    n.times{|i|
        if x[i]=='1'
            y[i]=osf-zeros
            osf+=1
        else
            zeros-=1
            y[i]=zeros-osf
        end
    }
    p y.max+initial
}


n = gets.chomp.to_i
c = gets.split.map(&:to_i)
cur1 = 1
cur = 0
ans = []
for i in c
  ans << i
  while ans.size > 1 and ans[-1] == ans[-2]
    cur = ans[-1]
    ans.pop
    ans.pop
    cur += 1
    ans << cur
  end
end
print "#{ans.size}\n"
for i in ans
  print "#{i} "
end

n = gets.to_i
arr = gets.split(/[ \n]/);

st = Array.new(n) { |x|  }
len = 0
for x in arr
    x = x.to_i
    if (len != 0 && st[len - 1] == x)
        st[len - 1] += 1
    else
        st[len] = x
        len += 1
    end
    while (len > 1 && st[len - 2] == st[len - 1]) do
        len -= 1;
        st[len - 1] += 1
    end
end
puts len
for i in (0...len) do
    print "#{st[i]} ";
end

r=->{gets.split.map &:to_i}
n = r[]
n = n[0]
v = r[]
a = Array.new
n.times {|i|
    x = v[i]
    while a.length != 0 and a[a.length - 1] == x do
        a.pop()
        x = x + 1
    end
    a.push(x)
}
puts(a.length)
for x in a do
    print(x)
    print(" ")
end

n = gets.to_i
rs = gets.split.map &:to_i

got = []
i = 0
j = 0

while i < n do
  if got.size == 0 then
    got.push(rs[i])
  elsif rs[i] == got[j] then
    got[j] += 1
    while j > 0 && got[j] == got[j-1] do
      j -= 1
      got[j] += 1
    end
  else
    if j == got.size - 1 then
      got.push(rs[i])
      j += 1
    else
      j += 1
      got[j] = rs[i];
    end
  end

  i += 1
end

p j+1
print got[0..j].join(' ')

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

cur1 = 1
cur = 0
ans = []

for i in c
  ans << i
  while ans.size > 1 and ans[-1] == ans[-2]
    cur = ans[-1]
    cur += 1
    ans.pop
    ans.pop
    ans << cur
  end
end

print "#{ans.size}"
print "\n"

for i in ans
  print "#{i} "
end

n = gets.to_i
a = gets.split.map(&:to_i)
b=[]
for x in a
	b<<x
	while b.size>1 and b[-1]==b[-2]
		b.pop
		b[-1]+=1
	end
end

print "#{b.size}"
print "\n"

for x in b
  print "#{x} "
end

# help from stackoverflow
n = gets.to_i
arr = gets.split.map(&:to_i)
i = 1
filled = 1
ans = []
tmp = arr[0]
ans << arr[0]
while i<n
    if filled == 0
        ans << arr[i]
        filled = filled +1
    elsif arr[i] == ans[-1]
        ans.pop
        ans << arr[i]+1
        while ans.length > 1 && ans[-1]==ans[-2]
            tmp = ans.pop
            ans.pop
            ans << tmp+1
            filled = filled -1
        end
    else
        ans << arr[i]
        filled= filled+1
    end

    i= i+1
end

puts filled

for i in (0..filled)
    print ans[i]
    print " "
end

n = gets.to_i
rs = gets.split.map &:to_i

got = []
i = 0
j = 0;
while i < n do
  if got.size == 0 then
    got.push(rs[i])
  elsif rs[i] == got[j] then
    got[j] += 1
    while j > 0 && got[j] == got[j-1] do
      j -= 1
      got[j] += 1
    end
  else
    got.push(rs[i])
    j += 1
  end

  i += 1
end

p j+1
print got[0..j].join(' ')


gets.to_i.times do |tt|
  n=gets.to_i
  s=gets.chomp.chars.map(&:to_i)
  puts s.inject(:+)+n-1-s[0,n-1].count(0)
end

=begin
Entrada: testcases, entero que indica la cantidad de dígitos,
        string de los dígitos
Salida: operaciones mínimas para que todos los dígitos sean 0
Movimientos permitidos: restar 1 a la cifra, cambiar el orden de una cifra
(adyacente y no adyacente)
=end

# Variables
testcases = 0
numberDigits = 0
digits = ""

# Constantes
condition = true
operationReduce = false
operationChange = true

# Paso 1: Pedir los testcases
testcases = Integer(gets.chomp)
movements = Array.new(testcases)

#Paso 2: Pedir la cantidad de dígitos
for i in (0..(testcases-1))
        numberDigits = Integer(gets.chomp)
        digits = gets.chomp
        movements[i] = 0
        count = 0
        digits.chars.each do |digit|
                count = count +1
                if Integer(digit)!=0
                        if count == numberDigits
                                movements[i] = movements[i] + digit.to_i
                        else
                                movements[i] = movements[i] + digit.to_i + 1
                        end
                end

        end
end
movements.each do |elements|
        puts elements
end

# require 'byebug'

gets.to_i.times do

  n = gets.to_i
  str = gets.strip

  ary = str.split("").map(&:to_i)
  ans = 0
  0.upto(n-1).each do |i|
    ans += ary[i]
    ans += 1 if ary[i] != 0 && i != n-1
  end

  puts ans

end

gets.to_i.times do

n = gets.to_i
d = gets.chomp.chars.map!(&:to_i)

cnt = 0
d.reverse_each.with_index do |di, i|
  next if di == 0

  cnt += di
  cnt += 1 if i > 0
end

puts cnt

end


n = gets.to_i
x = (n-1).times.map{gets.to_i-1}

ha = Array.new(n, true)
ha[0] = false
x.each{|t| ha[t] = false}

cnt = Array.new(n, 0)
x.each.with_index{|t,i| cnt[t]+=1 if ha[i+1]}

puts (n.times.all?{|i| ha[i]||cnt[i]>2}) ? 'Yes' : 'No'


n, k = gets.split.map(&:to_i)
ts = gets.split.map { |i| i.to_i < 0 }
count = ts.count { |t| t }
if count > k
  puts -1
  exit
end
prev = nil
dts = []
ts.each_with_index do |t, i|
  if t && !prev
    prev = i
  elsif t && prev
    dts << i - prev - 1
    prev = i
  end
end
dts.sort!
sum = count
dif = 0
dts.each do |dt|
  if sum + dt <= k
    sum += dt
    dif += 1
  else
    break
  end
end
ans = (count - dif) * 2
last = ts.reverse.index { |t| t }
if last && last <= k - sum
  puts ans - 1
else
  puts ans
end

#!/bin/env ruby

class TireManager
  attr_accessor :weather_intervals, :minus_degree_days_count, :plus_degree_intervals, :reserve
  def getNumberOfCrisscrosses number_of_winter_days, tyre_life_in_days, weather_forecast
    @weather_intervals=weather_forecast.chunk {|degree| degree<0}.map {|x,y| [(x)?(:minus):(:plus),y.size]}
    @minus_degree_days_count = @weather_intervals.reduce(0) {|sum,interval| sum+=(interval[0]==:minus)?(interval[1]):0}
    case
      when @minus_degree_days_count > tyre_life_in_days
        -1
      when @minus_degree_days_count == 0
        0
       else
         @weather_intervals.shift if @weather_intervals[0][0]==:plus
         @reserve = tyre_life_in_days - @minus_degree_days_count
         @plus_degree_intervals = @weather_intervals[0...-1].select {|interv| interv[0]==:plus}.map {|x| x[1]}.sort
         @plus_degree_intervals = @plus_degree_intervals.drop_while {|x| (x<=@reserve)?(@reserve-=x;true):(false)}
         @plus_degree_intervals.size*2+1+((@weather_intervals[-1][0]==:plus&&@reserve<@weather_intervals[-1][1])?1:0)
    end
  end
end

if __FILE__ == $0
  number_of_winter_days,tyre_life_in_days = $stdin.readline.split.map(&:to_i)
  weather_forecast = $stdin.readline.rstrip.split.map(&:to_i)
  puts TireManager.new.getNumberOfCrisscrosses(number_of_winter_days, tyre_life_in_days, weather_forecast)
end


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

b=[]
a.each{|e|b[e]=b[e]?b[e]+1:1}
r=0
a.each{|e|
    r+=b[x^e] if b[x^e]
    r-=1 if x==0
}
p r/2

n,x,*a=$<.read.split.map(&:to_i)
h=Hash.new(r=0)
a.each{|e|h[e]+=1}
h.each_key{|k|
	opp=k^x
	if opp==k
		r+=h[k]*(h[k]-1)/2
	elsif k<opp
		r+=h[k]*h[opp]
	end
}
p r

n,x,*a=$<.read.split.map(&:to_i)
h=Hash.new(r=0)
a.each{|e|h[e]+=1}
h.each_key{|k|
	opp=k^x
	if opp==k
		r+=h[k]*(h[k]+1)/2
	elsif k<opp
		r+=h[k]*h[opp]
	end
}
p r

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

b=[]
a.each{|e|b[e]=b[e]?b[e]+1:1}
r=0
a.each{|e|
    if b[e^x]
        r+=b[e^x]
        r-=1 if e==x
    end
}
p r/2


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=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
			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, 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__

#!/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


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

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

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

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

#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

#!/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

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

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

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

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/env ruby

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

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

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

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).count {
  p, q = gets.split.map(&:to_i)
  q - p >= 2
}

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


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

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

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
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, 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.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


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


#!/usr/bin/env ruby

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

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

puts total

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

# 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


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

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

# 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

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


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


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

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

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|
		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

#!/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


#!/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 }

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

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


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(:+)


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

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
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


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

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(" ")}

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}"
}

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(' ')

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}"
}


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