def run
  ary = []
  cnt = 3
  begin
    ary << (180.0 * (cnt - 2)) / cnt
    cnt += 1
  end while ary.last < 179
  ary.uniq!

  n = ARGF.readline.chomp
  ARGF.readlines.map(&:to_i).each do |val|
    puts ary.include?(val) ? "YES" : "NO"
  end
end

run if $0 == __FILE__

#$stdin = File.open("1.txt")
T = gets.chomp.to_i
T.times do
	a = gets.chomp.to_f
	if (360/(180-a)) % 1 == 0
		puts "YES"
	else
		puts "NO"
	end
end

gets.to_i.times do
  puts 360 % (180 - gets.to_i) == 0 ? "YES" : "NO"
end

n = gets.to_i

ans = []
n.times do |i|
    ang = gets.to_f
    a = (360/(180-ang)).to_i == (360/(180-ang)) ? "YES" : "NO"
    ans << a
end

ans.each {|i| puts i}

# cook your code here

num = gets.to_i

for i in 1..num
    x = gets.to_i
    if 360%(180-x)==0
        puts "YES"
    else
        puts "NO"
    end
end

n = gets.chomp.to_i
for i in 1..n
  a = gets.chomp.to_i
  if 360 % (180-a) == 0
    puts "YES"
  else
    puts "NO"
  end
end

n=gets.to_i.times do
a=gets.to_i
if a>=60 and a<180 and 360%(180-a)==0
puts "YES"
else
puts "NO"
end
end

require 'set'
s = Set.new
a = Array.new
n = gets.to_i.times do
	a << gets.to_i
end
for i in 1..179 do
	s.add(i) if 360 % (180 - i) == 0
end
a.each{|el| s.include?(el) ? (puts "YES") : (puts "NO")}

n = gets.to_i
n.times do
  a = gets.to_i
  puts(if 360 % (180 - a) == 0 then "YES" else "NO" end)
end

num_tests = gets.to_i
for i in 0...num_tests
	angle = gets.to_i
	sides = 2.0/(1-(angle/180.0))
	if (sides - (sides+0.5).to_i).abs < 0.00000001
		puts "YES"
	else
		puts "NO"
	end
end

n = gets.chomp.to_i
res = []
(1..n).each {|k|
	a = gets.chomp.to_i
	a /= 180.0

	q = 2.0/(1.0-a)

	if (q-q.round).abs < 0.000001
		res.push "YES"
	else
		res.push "NO"
	end
}

res.each { |k|
 puts k
}

x =(1..179).to_a.select{|x| (360%(180-x)==0)}
n = gets.to_i
d = n.times.map{gets.to_i}
n.times{|y| (x.include?(d[y]))? (puts "YES") : (puts "NO")}

#!/usr/bin/ruby1.9

t = STDIN.readline.to_i
t.times do
	interior_angle = STDIN.readline.to_i
	if 360%(180-interior_angle) == 0
		puts "YES"
	else
		puts "NO"
	end
end

n = gets.to_i
n.times{
		a = gets.to_i
		x = 360.0 / ( 180.0 - a )
		if (x == x.floor) then
			puts "YES"
		else
			puts "NO"
		end
}

gets.to_i.times do
  x = gets.to_i
  puts 360 % (180 - x) == 0 ? "YES" : "NO"
end

t = gets.to_i
ans = []
for i in 0...t
  ans[i] = 360 % (180 - gets.to_i) == 0 ? "YES" : "NO"
end
puts ans

t = gets.to_i

1.upto(t) { puts 360.modulo(180 - gets.to_i).zero? ? 'YES' : 'NO' }

#!/usr/bin/env ruby

n = gets.to_i
n.times do
	x = gets.to_i
	puts 360 % (180 - x) == 0 ? "YES" : "NO"
end

t = gets.chomp.to_i
t.times do
    a = gets.chomp.to_i

    rem = 360 % (180 - a)

    puts (rem == 0 ? "YES" : "NO")
end

n = gets.to_i

ok = []

180.times do |i|
	k = 360.0 / (180 - i)
	if k == k.to_i
		ok << i
	end
end

n.times do |i|
	a = gets.to_i

	if ok.include?(a)
		puts 'YES'
	else
		puts 'NO'
	end
end

t = gets.chomp.to_i
(1..t).each do |x|
	y = gets.chomp.to_i
	if y == 180 then puts "NO"
	else puts((360 % (180-y) == 0) ?"YES":"NO") end
end

arr = []
gets.to_i.times do
  arr << gets.to_i
end
arr.each { |x| puts 360 % (180 - x) == 0 ? "YES" : "NO" }

n=gets.to_i
s=(3..360).select{|e|
	(360%e==0)
}
s.map!{|e|
	180-360/e
}

n.times{
	m=gets.to_i
	if s.include?(m) then
		puts "YES"
	else
		puts "NO"
	end
}

n = gets.to_i
n.times do |i|
	x = gets.to_f
	x = (-2 * 180) / (x - 180)
	puts  x % 1 == 0 ? 'YES' : 'NO'
end

require 'set'
include Math
$stdin = File.open("./input.txt", "r") if ENV["USER"] == "new"
# ====================================

n = gets.to_i

n.times do
  angle = gets.to_i
  n = 360.0 / (180.0 - angle)
  puts ((n.ceil == n.floor) ? :YES : :NO)
end

require 'set'
include Math
$stdin = File.open("./input.txt", "r") if ENV["USER"] == "new"
# ====================================

n = gets.to_i

degrees = Set.new
index = 3
loop do
  angle = (index - 2) * 180.0 / index
  degrees << angle.to_i if angle.ceil == angle.floor
  index += 1

  break if index>2000
end

n.times do
  angle = gets.to_i
  puts ((degrees.include? angle) ? :YES : :NO)
end

n = gets.chomp.to_i

n.times do
  x = gets.chomp.to_i
  puts 360 % (180 - x) == 0 ? 'YES' : "NO"
end

n = gets.to_i
n.times do
    a = gets.to_i
    n = 360.0 / (180 - a)
    puts n.to_i == n ? "YES" : "NO"
end

require 'set'
include Math
$stdin = File.open("./input.txt", "r") if ENV["USER"] == "new"
# ====================================

n = gets.to_i

degrees = Set.new
index = 3
loop do
  angle = (index - 2) * 180 / index
  degrees << angle
  index += 1

  break if index>1000
end

n.times do
  angle = gets.to_i
  puts ((degrees.include? angle) ? :YES : :NO)
end

require 'set'
include Math
$stdin = File.open("./input.txt", "r") if ENV["USER"] == "new"
# ====================================

n = gets.to_i

degrees = Set.new
index = 3
loop do
  angle = (index - 2) * 180.0 / index
  degrees << angle.to_i if angle.ceil == angle.floor
  index += 1

  break if index>2000
end

puts degrees.inspect

n.times do
  angle = gets.to_i
  puts ((degrees.include? angle) ? :YES : :NO)
end

require 'set'
include Math
$stdin = File.open("./input.txt", "r") if ENV["USER"] == "new"
# ====================================

n = gets.to_i

degrees = []
3.upto(12) do |index|
  degrees << (index - 2) * 180 / index
end

n.times do
  angle = gets.to_i
  puts ((degrees.include? angle) ? :YES : :NO)
end

gets.to_i.times{
	a=gets.to_i
	puts ((a>=60 && a<180) ? 'YES' : 'NO')
}

puts gets.to_i.times.map{gets.to_i}.map{|x| (x>=60&&360%x==0)? "YES" : "NO"}

puts gets.to_i.times.map{gets.to_i}.map{|x| (x>=60)? "YES" : "NO"}

#$stdin = File.open("1.txt")
T = gets.chomp.to_i

H = {}

3.upto(300) do |c|
	H[180*(c-2)/c] = 1
end
T.times do
	a = gets.chomp.to_i
	if H.has_key? (a)
		puts "YES"
	else
		puts "NO"
	end
end

n=gets.to_i.times do
a=gets.to_i
if a>=60 and a<180
puts "YES"
else
puts "NO"
end
end

possible_angles = [0,60,90,108,120,128,135,140,144,147,150,152,154,156,157,158,160]
tests = gets.to_i
for i in 0...tests
	test_case = gets.to_i
	if test_case > 160 and test_case <= 180
		puts "YES"
		next
	end
	while test_case > 180
		test_case -= 180
	end
	if possible_angles.include?(test_case)
		puts "YES"
	else
		puts "NO"
	end
end

num_tests = gets.to_i
for i in 0...num_tests
	angle = gets.to_i
	sides = 2.0/(1-(angle/180.0))
	if (sides - (sides+0.5).to_i) < 0.00000001
		puts "YES"
	else
		puts "NO"
	end
end

n = gets.to_i
n.times do
    a = gets.to_i
    n = 360 / (180 - a)
    puts n.to_i == n ? "YES" : "NO"
end
puts "over"

def run
  ary = []
  cnt = 3
  begin
    ary << (180 * (cnt - 2)) / cnt
    cnt += 1
  end while ary.last < 179
  ary.uniq!

  n = ARGF.readline
  ARGF.readlines.map(&:to_i).each do |val|
    puts ary.include?(val) ? "YES" : "NO"
  end
end

run if $0 == __FILE__

def run
  ary = []
  cnt = 3
  begin
    angle = (180 * (cnt - 2)) / cnt
    ary[cnt] = angle
    cnt += 1
  end while angle < 179

  n = ARGF.readline
  ARGF.readlines.map(&:to_i).each do |val|
    puts ary.include?(val) ? "YES" : "NO"
  end
end

run if $0 == __FILE__

n = gets.to_i
n.times do |i|
	x = gets.to_f
	x = (-2 * 180) / (x - 180)
	puts x
	puts  x % 1 == 0 ? 'YES' : 'NO'
end

require 'set'
include Math
$stdin = File.open("./input.txt", "r") if ENV["USER"] == "new"
# ====================================

n = gets.to_i

degrees = []
3.upto(30) do |index|
  degrees << (index - 2) * 180 / index
end

n.times do
  angle = gets.to_i
  puts ((degrees.include? angle) ? :YES : :NO)
end


class Solver
	def main
		n, m = gets.split.map { |x| x.to_i }
		f = gets.split.map { |x| x.to_i }
		b = gets.split.map { |x| x.to_i }
		h = Hash.new { |hash, key| hash[key] = Array.new }
		f.each.with_index do |x, id|
			h[x] << id
		end

		ambiguity = false
		impossible = false
		ans = []
		b.each do |x|
			if h[x].size == 0 then
				impossible = true
			elsif h[x].size > 1 then
				ambiguity = true
			else
				ans << h[x][0] + 1
			end
		end
		if impossible then
			puts "Impossible"
		elsif ambiguity then
			puts "Ambiguity"
		else
			puts "Possible"
			puts ans.join " "
		end
	end
end

Solver.new.main

require 'scanf'

n, m = scanf("%d %d\n")
#f = Array.new(n)
f_inv = Array.new(n + 1)
#b = Array.new(m)

f = gets.split.map { |i| i.to_i  }
b = gets.split.map { |i| i.to_i  }
(0..n-1).each do |i|
  f_inv[f[i]] = i + 1
end
f_map = Array.new(n + 1, 0)
(0..n-1).each do |i|
  f_map[f[i]] += 1
end
a = false, im = false
ans = []
(0..m-1).each do |i|
  if f_map[b[i]] > 1
    a = true
  elsif f_map[b[i]] == 0
    im = true
    break
  else
    ans.push(f_inv[b[i]])
  end
end

if im == true
  puts "Impossible"
elsif a == true
  puts "Ambiguity"
else
  puts "Possible"
  (0..ans.size-1).each do |i|
    print ans[i], ' '
  end
  puts
end

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU

gt = proc { gets.chomp.split.map(&:to_i) }

n, m = gt.call
f = gt.call
b = gt.call

fm = {}
n.times do |i|
    k = f[i]
    if fm.key? k
        fm[k] << [i]
    else
        fm[k] = [i]
    end
end

ans = ""
a = [0] * m
m.times do |i|
    k = b[i]
    if fm.key? k
        a[i] = fm[k][0].succ
        if fm[k].size > 1
            ans = "Ambiguity"
        end
    else
        puts 'Impossible'
        exit
    end
end

if ans.empty?
    puts 'Possible'
    puts a * " "
else
    puts ans
end

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

f_map = {}

f.each_index do |i|
  f_map[f[i]] ||= []
  f_map[f[i]] << i + 1
end

a = []
status = 'Possible'

b.each do |bi|
  f_map[bi] ||= []
  case f_map[bi].size
  when 0
    status = 'Impossible'
    break
  when 1
    a << f_map[bi][0]
  else
    if status == 'Possible'
      status = 'Ambiguity'
    end
  end
end

puts status
if status == 'Possible'
  puts a.join ' '
end

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

hs = Hash.new  { |hs, k| hs[k] = [] }

(1..n).map { |i| hs[fs[i - 1]] << i }

as = []
ans = 'Possible'
bs.each do |b|
  s = hs[b].size
  if s == 0
    ans = 'Impossible'
    break
  elsif 1 < s
    ans = 'Ambiguity'
  end
  as << hs[b][0]
end

puts ans
puts as.*' ' if ans == 'Possible'

require "set"
n, m = gets.split().map(&:to_i)
f = gets.split().map(&:to_i)
b = gets.split().map(&:to_i)
f = [0] + f
b = [0] + b
g = [0]*n
imf = Set.new()
d = Set.new()
x = 0
for i in 1..n
	if imf.include? f[i]
		d.add(f[i])
	else
		imf.add(f[i])
	end
	g[f[i]] = i
end
for i in 1..m
	if not imf.include? b[i]
		puts "Impossible"
		x = 1
		break
	end
end
if x != 1
	y = 0
	for i in 1..m
		if d.include? b[i]
			puts "Ambiguity"
			y =1
			break
		end
	end
	if y != 1
		puts "Possible"
		for i in 1...m
			print(g[b[i]])
			print(" ")
		end
		puts g[b[m]]
	end
end

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

hs = Hash.new  { |hs, k| hs[k] = [] }

(1..m).map { |i| hs[fs[i - 1]] << i }

as = []
ans = 'Possible'
bs.each do |b|
  s = hs[b].size
  if s == 0
    ans = 'Impossible'
    break
  elsif 1 < s
    ans = 'Ambiguity'
    break
  end
  as << hs[b][0]
end

puts ans
puts as.*' ' if ans == 'Possible'

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

hs = Hash.new()

(1..m).map { |i| hs[fs[i - 1]] = i }
fs.sort!
bs.sort!

if fs != bs
  puts 'Impossible'
else
  fs.uniq!
  if fs.size != m
    puts 'Ambiguity'
  else
    puts 'Possible'
    puts bs.map { |b| hs[b] }.*' '
  end
end

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

hs = Hash.new  { |hs, k| hs[k] = [] }

(1..n).map { |i| hs[fs[i - 1]] << i }

as = []
ans = 'Possible'
bs.each do |b|
  s = hs[b].size
  if s == 0
    ans = 'Impossible'
    break
  elsif 1 < s
    ans = 'Ambiguity'
    break
  end
  as << hs[b][0]
end

puts ans
puts as.*' ' if ans == 'Possible'


n = gets.to_i
a =[]
n.times {a << gets.chomp}
n.times do
  s = gets.chomp
  n.times do |i|
    if a[i]==s
      a.delete_at(i)
      break
    end
  end
end
puts a.length

N = gets.to_i
hash = Hash.new(0)
N.times do
    str = gets.chomp
    hash[str] += 1
end
ans = 0
N.times do
    str = gets.chomp
    if hash[str] > 0
        hash[str] -= 1
    else
        ans += 1
    end
end
puts ans

n=gets.to_i
h=Hash.new 0
g=Hash.new 0
n.times{h[gets.chomp]+=1}
n.times{g[gets.chomp]+=1}
r=0
h.each{|k,v|r+=[0,g[k]-v].max;g.delete(k)}
g.each{|k,v|r+=v}
p r

n=gets.to_i
h=Hash.new 0
g=Hash.new 0
n.times{h[gets.to_i]+=1}
n.times{g[gets.to_i]+=1}
r=0
h.each{|k,v|r+=(v-g[k]).abs;g.delete(k)}
g.each{|k,v|r+=v}
p r

n=gets.to_i
h=Hash.new 0
g=Hash.new 0
n.times{h[gets.to_i]+=1}
n.times{g[gets.to_i]+=1}
r=0
h.each{|k,v|r+=(v-g[k]).abs;g.delete(k)}
r/=2
g.each{|k,v|r+=v}
p r

n=gets.to_i
h=Hash.new 0
g=Hash.new 0
n.times{h[gets.chomp]+=1}
n.times{g[gets.chomp]+=1}
r=0
h.each{|k,v|r+=[0,g[k]-v].max}
g.each{|k,v|r+=v}
p r

n=gets.to_i
h=Hash.new 0
g=Hash.new 0
n.times{h[gets.chomp]+=1}
n.times{g[gets.chomp]+=1}
r=0
h.each{|k,v|r+=(v-g[k]).abs;g.delete(k)}
g.each{|k,v|r+=v}
p r

n = gets.to_i
a1 = []
a2 = []
n.times { a1 << gets.chomp.chars}

n.times { a2 << gets.chomp.chars}

a1.sort!{|a,b| a.length <=> b.length}
a2.sort!{|a,b| a.length <=> b.length}
res = 0

n.times do |i|
  res += (a1[i] - a2[i]).length
end
puts res

n = gets.to_i
a =[]
a2=[]
n.times {a << gets.chomp}
n.times{a2 << gets.chomp}
puts (a2-a).length

n = gets.to_i
a1 = []
a2 = []
n.times { s = gets.chomp
valid = (s =~ /(\A(X{0,3})(S|L)$)|(\A(S|L|M)$)/) != nil
a1 << s.chars if valid
}

n.times { s = gets.chomp
valid = (s =~ /(\A(X{0,3})(S|L)$)|(\A(S|L|M)$)/) != nil
a2 << s.chars if valid}

a1.sort!{|a,b| a.length <=> b.length}
a2.sort!{|a,b| a.length <=> b.length}
res = 0
n.times do |i|
  res += (a1[i] - a2[i]).length
end
puts res


class Solver
	def main
		n = gets.to_i
		orders = []
		n.times do
			orders << gets.split.map{ |x| x.to_i }
		end
		orders.sort! { |a, b| a[1] <=> b[1] }

		ans = 0
		last = 0
		orders.each do |order|
			if order[0] > last then
				last = order[1]
				ans += 1
			end
		end
		puts ans
	end
end

Solver.new.main

#!/usr/bin/env ruby

num_orders = gets.to_i
orders = num_orders.times.map do gets.split(' ').map(&:to_i) end
orders = orders.sort_by do |order| order[1] end

total = 0
current_right = 0
orders.each do |left, right|
  if current_right < left
    current_right = right
    total += 1
  end
end

puts total

MAX = 10**9 + 1

n = 0
n = gets.chomp.to_i

numbers = Array.new
# input
for i in 0...n do
	t = gets.chomp.split " "
	numbers.push t.last.to_i * MAX + t.first.to_i
end

numbers.sort!

# calculating answer
ans = 1
last_fin = numbers[0] / MAX

for i in 1...n do
	if numbers[i] % MAX > last_fin
		ans = ans + 1
		last_fin = numbers[i] / MAX
	end

end

puts ans

n = 0
l = Array.new
r = Array.new

n = gets.chomp.to_i
# input
for i in 0...n do
	t = gets.chomp.split " "
	l[i] = t.first.to_i
	r[i] = t.last.to_i
end

# insertion sorting
for i in 1...n do

	k = r[i]
	j = i

	while j > 0 and r[j] < r[j-1] do
		r[j] = r[j-1]
		j = j - 1
	end

	r[j] = k

	k = l[i]
	l[i] = l[j]
	l[j] = k
end

# calculating answer
ans = 1
r_last = r[0]

for i in 1...n do
	if l[i] > r_last
		ans = ans + 1
		r_last = r[i]
	end
end

puts ans

n = 0
l = Array.new
r = Array.new

n = gets.chomp.to_i

pointer = Hash.new
max_len = 1
# input
for i in 0...n do
	t = gets.chomp.split " "
	l[i] = t.first
	r[i] = t.last
	if r[i].length > max_len
		max_len = r[i].length
	end

	if pointer.keys.include? r[i]
		pointer[r[i]].push l[i]
	else
		pointer[r[i]] = Array.new
		pointer[r[i]].push l[i]
	end
end

pointer.each_key {|key| pointer[key].sort!}
r.sort!
# radix sorting

# calculating answer
ans = 1
r_last = r[0]

for i in 1...n do
	if pointer[r[i]].shift.to_i > r_last.to_i
		ans = ans + 1
		r_last = r[i]
	end
end

puts ans

n = 0
l = Array.new
r = Array.new

n = gets.chomp.to_i

strings = Array.new
max_len = 1
# input
for i in 0...n do
	t = gets.chomp.split " "
	strings.push t.last + " " + t.first
end

strings.sort!

# calculating answer
ans = 1
last_fin = (strings[0].split(" ")).first.to_i

for i in 1...n do
	t = strings[i].split(" ")
	if t.last.to_i > last_fin.to_i
		ans = ans + 1
		last_fin = strings[i].split(" ").first.to_i
	end

end

=begin
for i in 0...n
	print l[i], " ", r[i]
	puts
=end

puts ans

n = 0
n = gets.chomp.to_i

numbers = Array.new
# input
for i in 0...n do
	t = gets.chomp.split " "
	numbers.push t.last.to_i * 500001 + t.first.to_i
end

numbers.sort!

# calculating answer
ans = 1
last_fin = numbers[0] / 500001

for i in 1...n do
	if numbers[i] % 500001 > last_fin
		ans = ans + 1
		last_fin = numbers[i] / 500001
	end

end

puts ans


n, m = gets.split.map(&:to_i)
cats = [nil] + gets.split.map(&:to_i)
edges = Array.new(n + 1){Array.new}
(n - 1).times do
  u, v = gets.split.map(&:to_i)
  edges[u] << v
  edges[v] << u
end
con_cats = Array.new(n + 1)

que = [[1, 0]]
ans = 0
loop do
  ind, accum = que[0]
  break unless ind
  que.shift
  next if con_cats[ind]
  if cats[ind] == 1
    accum += 1
  else
    accum = 0
  end
  con_cats[ind] = accum
  if accum <= m
    ans += 1 if edges[ind].size == 1 && ind != 1
    que.push(*edges[ind].zip([accum] * edges[ind].size))
  end
end
puts ans

# coding: utf-8

require "set"

# if __FILE__ == $0
# end

n, m = gets.split.map(&:to_i)
cats = [nil] + gets.split.map(&:to_i) # 1-based vertices
vertices = {}
(n-1).times {
    u, v = gets.split.map(&:to_i)

    vertices[v] ||= [] # initialize u with empty array
    vertices[u] ||= []
    vertices[u] << v
    vertices[v] << u
}

# Remember, rooted tree
#p vertices
TRAVERSE_UP = -99
stack = [1]
traverse = []
traverse_hashed = {} # do not delete, just mark with nil, put/delete with traverse stack control
cat_acc_stack = [0] # accumulator stack. push/pop with traverse stack control
reachables = []
while !stack.empty?
    i = stack.pop
    if i == TRAVERSE_UP
        traverse.pop
        cat_acc_stack.pop
        next
    end

    if cat_acc_stack.last + cats[i] > m
        # you can stop traversing from here. children can not be reached.
        # visiting node_i should stop
        next # back to stack again
    else
        #p vertices[i]
        #p traverse
        if vertices[i].length == 1 && vertices[i][0] == traverse.last
            reachables << i
        else
            # new traverse starts!
            if cats[i] == 0
                cat_acc_stack << 0
            else
                cat_acc_stack << cat_acc_stack.last + cats[i]
            end

            traverse.push(i)
            traverse_hashed[i] = 1
            stack.push(TRAVERSE_UP)
            for ni in vertices[i]
                if traverse_hashed[ni] == nil
                    stack.push(ni)
                end
            end
        end
    end
end

#p vertices
#p reachables
puts reachables.length # print reachables

def ri; gets.split.map(&:to_i); end
n, m = ri
cats = [nil] + ri
edges = Array.new(n + 1){Array.new}
(n - 1).times do
  u, v = ri
  edges[u] << v
  edges[v] << u
end

ans = 0
que = [[1, cats[1]]]
until que.empty?
  u, s = que.shift
  ans += 1 if edges[u].empty?
  edges[u].each do |v|
    edges[v].delete(u)
    ns = cats[v] == 1 ? s + 1 : 0
    que << [v, ns] if ns <= m
  end
end
puts ans

n, m = gets.split.map(&:to_i)
cats = [nil] + gets.split.map(&:to_i)
edges = Array.new(n + 1){Array.new}
(n - 1).times do
  u, v = gets.split.map(&:to_i)
  edges[u] << v
  edges[v] << u
end

ans = 0
que = [[1, cats[1]]]
until que.empty?
  u, s = que.shift
  ans += 1 if edges[u].empty?
  edges[u].each do |v|
    edges[v].delete(u)
    ns = cats[v] == 1 ? s + 1 : 0
    que << [v, ns] if ns <= m
  end
end
puts ans

n, m = gets.split(' ').map(&:to_i)
cats = gets.split(' ').map(&:to_i)
cats.unshift 0

vert = Hash.new { |h, k| h[k] = [] }

1.upto(n - 1) do
  v1, v2 = gets.split(' ').map(&:to_i)
  vert[v1] << v2
  vert[v2] << v1
end

que = [prev: 0, curr: 1, cats: cats[1], failed: 1 > m]

result = 0

while !que.empty?
  curr = que.shift

  any = false

  vert[curr[:curr]].each do |v|
    next if curr[:prev] == v

    any = true

    que << {
      curr: v,
      cats: (curr[:cats] + cats[v]) * cats[v],
      prev: curr[:curr],
      failed: curr[:failed] || (curr[:cats] + cats[v]) * cats[v] > m
    }
  end

  result += 1 unless any || curr[:failed]
end

puts result

n=gets.split(' ').map(&:to_i)
m=n[1]
n=n[0]
a=gets.split(' ').map(&:to_i)
g=Array.new(n+1)
(1).upto(n) do |i|
  g[i]=[]
end
(1).upto(n-1) do
  x=gets.split(' ').map(&:to_i)
  g[x[0]].push(x[1])
  g[x[1]].push(x[0])
end
q=Queue.new
q.push([1,0])
ans=0
vis = Array.new(n+1,false)
while !(q.empty?)
  cur = q.pop
  node = cur[0]
  cats = cur[1]
  cats=0 if a[node-1]==0
  cats+=1 if a[node-1]==1
  next if cats==m+1
  ans+=1 if g[node].length==1 and node!=1
  g[node].each do |next_node|
    next if vis[next_node]
    vis[next_node]=true
    q.push([next_node,cats])
  end
end
puts ans

def getai; return gets.split.map{|e| e.to_i}; end
nm = getai
n = nm[0]; m = nm[1]
a = getai
adj = Array.new(n)
(n - 1).times do
    xy = getai
    x = xy[0] - 1
    y = xy[1] - 1
    nil == adj[x] ? adj[x] = [y] : adj[x] << y
    nil == adj[y] ? adj[y] = [x] : adj[y] << x
end
stack = [0]
used = Array.new(n, false)
last = Array.new(n, -1)
cats = Array.new(n)
cats[0] = a[0]
while (!stack.empty?)   # DFS
    v = stack.last
    used[v] = true
    l = last[v] + 1
    if l < adj[v].length && used[adj[v][l]]
        l += 1
    end
    if l < adj[v].length
        to = adj[v][l]
        cats[to] = cats[v] * a[to] + a[to]
        if cats[to] <= m
            stack << to
        end
    else
        stack.pop
    end
    last[v] = l
end
good_leaves = 0
for v in 0..n-1
    if adj[v].length == 1 && used[v] && v != 0
        good_leaves += 1
    end
end
puts good_leaves

n,m=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}
arr=[]
n.times{
	arr<<[]
}
(n-1).times{
	a,b=gets.split.map{|e| e.to_i-1}
	arr[a]<<b
	arr[b]<<a
}
stc=[[0,0,x[0]]]
ans=0
while !stc.empty?
	e=stc.last
	if (e[0]==0 || arr[e[0]].size>1) && arr[e[0]].size>e[1] then
		p1=arr[e[0]][e[1]]
		stc.last[1]+=1
		s1=stc.size
		if s1>1 && stc[s1-2][0]==p1 then
			next
		end
		add=x[p1]
		t1=0
		if add==1 then
			t1=e[2]+add
		end
		if t1<=m then
			stc<<[p1,0,t1]
		end
	else
		if e[0]==0 then
			break
		elsif arr[e[0]].size==1 then
			if e[2]<=m then
				ans+=1
			end
		end
		stc.pop
	end

end
puts ans

def get_i_arr
    return gets.chomp.split.map{|e| e.to_i}
end
nm = get_i_arr
n = nm[0]; m = nm[1]
a = get_i_arr
adj = Array.new(n)
(n - 1).times do
    xy = get_i_arr
    x = xy[0] - 1; y = xy[1] - 1
    if nil == adj[x]
        adj[x] = [y]
    else
        adj[x] << y
    end
    if nil == adj[y]
        adj[y] = [x]
    else
        adj[y] << x
    end
end
# DFS
stack = [0]
used = Array.new(n, false)
last = Array.new(n, -1)
cats = Array.new(n, 0)
cur_cats = cats[0] = a[0]
while (!stack.empty?)
    v = stack.last
    used[v] = true
    l = last[v] + 1
    if l < adj[v].length && used[adj[v][l]]
        l += 1
    end
    if l < adj[v].length
        to = adj[v][l]
        if 1 == a[to] && 0 == a[v]
            new_cur_cats = 1
        else
            new_cur_cats = cur_cats + a[to]
        end
        if new_cur_cats <= m
            stack << to
            cur_cats = new_cur_cats
            cats[to] = [cur_cats, cats[v]].max
        end
    else
        prev = stack.length > 1 ? stack[stack.length - 2] : -1
        if prev == -1
            cur_cats = 0
        else
            cur_cats = cats[prev]
        end
        stack.pop
    end
    last[v] = l
end
good_leaves = 0
for v in 0..n-1
    if adj[v].length == 1 && used[v] && v != 0
        good_leaves += 1
    end
end
puts good_leaves

def getai; return gets.split.map{|e| e.to_i}; end
nm = getai
n = nm[0]; m = nm[1]
a = getai
adj = Array.new(n)
(n - 1).times do
    xy = getai
    x = xy[0] - 1; y = xy[1] - 1
    if nil == adj[x] then adj[x] = [y]
    else                  adj[x] << y   end
    if nil == adj[y] then adj[y] = [x]
    else                  adj[y] << x   end
end
stack = [0]
used = Array.new(n, false)
last = Array.new(n, -1)
cats = Array.new(n, 0)
cur_cats = cats[0] = a[0]
while (!stack.empty?)   # DFS
    v = stack.last
    used[v] = true
    l = last[v] + 1
    if l < adj[v].length && used[adj[v][l]]
        l += 1
    end
    if l < adj[v].length
        to = adj[v][l]
        if 1 == a[v] && 0 == a[to]
            new_cats = 0
        else
            new_cats = cur_cats + a[to]
        end
        cats[to] = [new_cats, cats[v]].max
        if cats[to] <= m
            stack << to
            cur_cats = new_cats
        end
    else
        if stack.length > 1
            prev = stack[stack.length - 2]
            if 0 == a[v] && 1 == a[prev]
                cur_cats = cats[prev]
            else
                cur_cats -= a[v]
            end
        else
            cur_cats = 0
        end
        stack.pop
    end
    last[v] = l
end
good_leaves = 0
for v in 0..n-1
    if adj[v].length == 1 && used[v] && v != 0
        good_leaves += 1
    end
end
puts good_leaves

n,m=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}
arr=[]
n.times{
	arr<<[]
}
(n-1).times{
	a,b=gets.split.map{|e| e.to_i-1}
	arr[a]<<b
	arr[b]<<a
}
stc=[[0,0,x[0]]]
ans=0
while !stc.empty?
	e=stc.last
	if arr[e[0]].size>1 && arr[e[0]].size>e[1] then
		p1=arr[e[0]][e[1]]
		stc.last[1]+=1
		s1=stc.size
		if s1>1 && stc[s1-2][0]==p1 then
			next
		end
		add=x[p1]
		t1=0
		if add==1 then
			t1=e[2]+add
		end
		if t1<=m then
			stc<<[p1,0,t1]
		end
	else
		if e[0]==0 then
			break
		elsif arr[e[0]].size==1 then
			if e[2]<=m then
				ans+=1
			end
		end
		stc.pop
	end

end
puts ans

n,m=gets.split.map{|e| e.to_i}
m-=1
x=gets.split.map{|e| e.to_i}
arr=[]
n.times{
	arr<<[]
}
(n-1).times{
	a,b=gets.split.map{|e| e.to_i-1}
	arr[a]<<b
	arr[b]<<a
}
stc=[[m,0,0]]

while !stc.empty?
	e=stc.last
	if arr[e[0]].size>e[1] then
		s1=stc.size
		p1=arr[e[0]][e[1]]
		stc.last[1]+=1
		if s1>1 && stc[s1-2][0]==p1 then
			next
		end
		stc<<[p1,0,0]
	else
		stc.pop
		if e[0]==m then
			puts e[2]+1
			break
		else
			if x[e[0]]==1 then
				stc.last[2]+=e[2]+1
			end
		end
	end
end

n,m=gets.split.map{|e| e.to_i}
x=gets.split.map{|e| e.to_i}
arr=[]
n.times{
	arr<<[]
}
(n-1).times{
	a,b=gets.split.map{|e| e.to_i-1}
	arr[a]<<b
	arr[b]<<a
}
stc=[[0,0,0]]
ans=0
while !stc.empty?
	e=stc.last
	if arr[e[0]].size>1 && arr[e[0]].size>e[1] then
		p1=arr[e[0]][e[1]]
		stc.last[1]+=1
		s1=stc.size
		if s1>1 && stc[s1-2][0]==p1 then
			next
		end
		add=x[e[0]]
		add=0 if e[0]==0
		stc<<[p1,0,add+e[2]]
	else
		if e[0]==0 then
			break
		elsif arr[e[0]].size==1 then
			if e[2]<m then
				ans+=1
			end
		end
		stc.pop
	end

end
puts ans

# coding: utf-8

require "set"

# if __FILE__ == $0
# end

n, m = gets.split.map(&:to_i)
cats = [nil] + gets.split.map(&:to_i) # 1-based vertices
vertices = {}
(n-1).times {
    u, v = gets.split.map(&:to_i)

    vertices[v] ||= [] # initialize u with empty array
    vertices[u] ||= []
    vertices[u] << v
    vertices[v] << u
}

# Remember, rooted tree
#p vertices
TRAVERSE_UP = -99
stack = [1]
traverse = []
reachables = []
while !stack.empty?
    i = stack.pop
    if i == TRAVERSE_UP
        traverse.pop
        next
    end

    #p vertices[i]
    #p traverse
    if vertices[i].length == 1 && vertices[i][0] == traverse.last
        traverse.push(i)

        cat_count = 0
        longest_cats = 0
        traverse.each { |j|
            if cats[j] == 0
                if longest_cats < cat_count
                    longest_cats = cat_count
                end
                cat_count = 0
            else
                cat_count += cats[j]
            end
        }
        if longest_cats < cat_count
            longest_cats = cat_count #last update.
        end

        p "longest_cats #{longest_cats} #{m}"
        p stack
        p traverse
        if longest_cats <= m
            reachables << i
        end

        traverse.pop
    else
        traverse.push(i)
        stack.push(TRAVERSE_UP)
        for ni in vertices[i]
            unless traverse.include?(ni)
                stack.push(ni)
            end
        end
    end
end

#p vertices
#p reachables
puts reachables.length # print reachables

# coding: utf-8

require "set"

# if __FILE__ == $0
# end
V = Struct.new("Vertex", :u, :v)

n, m = gets.split.map(&:to_i)
cats = [nil] + gets.split.map(&:to_i) # 1-based vertices
rvertices = {}
leaves = Set.new
(n-1).times {
    u, v = gets.split.map(&:to_i)

    # handle in reverse. 'v' first.
    leaves.add(v)
    leaves.delete(u) # if v' (past one) was not a leaf node, delete it

    rvertices[v] ||= []
    rvertices[v] << V.new(v, u)
}

# Remeber, rooted tree
#p rvertices
reachables = []
leaves.each { |i|
    j = i
    cats_on_way = 0
    max_consecutives = 0
    loop do
        # p "> #{j}"
        if cats[j] == 1
            cats_on_way += cats[j]
        else
            cats_on_way = 0 # reset, because not consecutive.
        end
        if cats_on_way > max_consecutives
            max_consecutives = cats_on_way # updates
        end
        break if j == 1
        # p "> #{j} -> #{rvertices[j][0].v}"
        j = rvertices[j][0].v
    end

    if max_consecutives <= m
        reachables << i
    end
}

# p reachables
puts reachables.length # print reachables

# coding: utf-8

require "set"

# if __FILE__ == $0
# end

n, m = gets.split.map(&:to_i)
cats = [nil] + gets.split.map(&:to_i) # 1-based vertices
rvertices = {}
leaves = Set.new
(n-1).times {
    u, v = gets.split.map(&:to_i)
    leaves.add(v)
    leaves.delete(u)
    rvertices[u] ||= [] # initialize u with empty array
    rvertices[v] ||= []
    rvertices[v] << u

}

# Remember, rooted tree
#p rvertices
reachables = []

leaves.each { |i|
    if i == 1 # root node
        next
    end
    j = i
    cat_count = 0
    longest_cat_seq = 0
    loop do
        if cats[j] == 0
            if longest_cat_seq < cat_count
                longest_cat_seq = cat_count
                cat_count = 0
            end
        else
            cat_count += cats[j]
        end

        if rvertices[j].empty?
            if longest_cat_seq < cat_count
                longest_cat_seq = cat_count
            end
            break
        else
            j = rvertices[j][0]
        end
    end

    if longest_cat_seq <= m
        reachables << i
    end
}

p reachables
puts reachables.length # print reachables

# coding: utf-8

require "set"

# if __FILE__ == $0
# end

n, m = gets.split.map(&:to_i)
cats = [nil] + gets.split.map(&:to_i) # 1-based vertices
vertices = {}
(n-1).times {
    u, v = gets.split.map(&:to_i)

    vertices[v] ||= [] # initialize u with empty array
    vertices[u] ||= []
    vertices[u] << v
    vertices[v] << u
}

# Remember, rooted tree
#p vertices
TRAVERSE_UP = -99
stack = [1]
traverse = []
reachables = []
while !stack.empty?
    i = stack.pop
    if i == TRAVERSE_UP
        traverse.pop
        next
    else
        traverse.push(i)
    end

    if vertices[i].length == 1
        if i == 1
            break
        end
        cat_count = 0
        longest_cats = 0
        traverse.each { |j|
            if cats[j] == 0
                if longest_cats < cat_count
                    longest_cats = cat_count
                end
                cat_count = 0
            else
                cat_count += cats[j]
            end
        }
        if longest_cats < cat_count
            longest_cats = cat_count #last update.
        end

        #p "longest_cats #{longest_cats} #{m}"
        #p stack
        #p traverse
        if longest_cats <= m
            reachables << i
        end

        traverse.pop
    else
        stack.push(TRAVERSE_UP)
        for ni in vertices[i]
            unless traverse.include?(ni)
                stack.push(ni)
            end
        end
    end
end

#p vertices
#p reachables
puts reachables.length # print reachables

# coding: utf-8

require "set"

# if __FILE__ == $0
# end

n, m = gets.split.map(&:to_i)
cats = [nil] + gets.split.map(&:to_i) # 1-based vertices
rvertices = {}
leaves = Set.new
(n-1).times {
    u, v = gets.split.map(&:to_i)
    leaves.add(v)
    leaves.delete(u)
    rvertices[u] ||= [] # initialize u with empty array
    rvertices[v] ||= []
    rvertices[v] << u

}

# Remember, rooted tree
#p rvertices
reachables = []

leaves.each { |i|
    if i == 1 # root node
        next
    end
    j = i
    cat_count = 0
    longest_cat_seq = 0
    loop do
        if cats[j] == 0
            if longest_cat_seq < cat_count
                longest_cat_seq = cat_count
                cat_count = 0
            end
        else
            cat_count += cats[j]
        end

        if rvertices[j].empty?
            if longest_cat_seq < cat_count
                longest_cat_seq = cat_count
            end
            break
        else
            j = rvertices[j][0]
        end
    end

    if longest_cat_seq <= m
        reachables << i
    end
}

# p reachables
puts reachables.length # print reachables

n, $m = gets.split.map(&:to_i)
$cats = [nil] + gets.split.map(&:to_i)
$edges = Array.new(n + 1){Array.new}
$edges[0] = [1]
(n - 1).times do
  u, v = gets.split.map(&:to_i)
  $edges[u] << v
end

$ans = 0
def dfs(lv, accum)
  return unless $edges[lv]
  if $cats[lv] == 1
    accum += 1
  elsif !$cats[lv].nil?
    accum = 0
    $ans += 1
  end
  $edges[lv].each{|v| dfs(v, accum)} if accum <= $m
end

dfs(0, 0)
puts $ans

n, $m = gets.split.map(&:to_i)
$cats = [nil] + gets.split.map(&:to_i)
$edges = Array.new(n + 1){Array.new}
(n - 1).times do
  u, v = gets.split.map(&:to_i)
  $edges[u] << v
  $edges[v] << u
end
$con_cats = Array.new(n + 1)

$ans = 0
def dfs(lv, accum)
  return if $con_cats[lv]
  if $edges[lv].size == 1
    $ans += 1 if accum + $cats[lv] <= $m
    return
  end
  if $cats[lv] == 1
    accum += 1
  else
    accum = 0
  end
  $con_cats[lv] = accum
  $edges[lv].each{|v| dfs(v, accum)} if accum <= $m
end

dfs(1, 0)
puts $ans

n, $m = gets.split.map(&:to_i)
$cats = [nil] + gets.split.map(&:to_i)
$edges = Array.new(n + 1){Array.new}
$edges[0] = [1]
(n - 1).times do
  u, v = gets.split.map(&:to_i).sort
  $edges[u] << v
end

$ans = 0
def dfs(lv, accum)
  if $edges[lv] == []
    $ans += 1 if accum + $cats[lv] <= $m
    return
  end
  if $cats[lv] == 1
    accum += 1
  else
    accum = 0
  end
  $edges[lv].each{|v| dfs(v, accum)} if accum <= $m
end

dfs(0, 0)
puts $ans

n, $m = gets.split.map(&:to_i)
$cats = [nil] + gets.split.map(&:to_i)
$edges = Array.new(n + 1){Array.new}
$edges[0] = [1]
(n - 1).times do
  u, v = gets.split.map(&:to_i)
  $edges[u] << v
end

$ans = 0
def dfs(lv, accum)
  if $edges[lv] == []
    $ans += 1 if accum + $cats[lv] <= $m
    return
  end
  if $cats[lv] == 1
    accum += 1
  else
    accum = 0
  end
  $edges[lv].each{|v| dfs(v, accum)} if accum <= $m
end

dfs(0, 0)
puts $ans

n, m = gets.split(' ').map(&:to_i)
cats = gets.split(' ').map(&:to_i)
cats.unshift 0

vert = Hash.new { [] }

1.upto(n - 1) do
  v1, v2 = gets.split(' ').map(&:to_i)
  vert[v1] = vert[v1] + [v2]
  vert[v2] = vert[v2] + [v1]
end

que = [v: [1], cats: 1, failed: false]

result = 0

while !que.empty?
  curr = que.shift
  vort = curr[:v].last

  any = false

  vert[vort].each do |v|
    next if curr[:v].include? v

    any = true

    que << {
      v: curr[:v] + [v],
      cats: (curr[:cats] + cats[v]) * cats[v],
      failed: curr[:failed] || (curr[:cats] + cats[v]) * cats[v] > m
    }
  end

  result += 1 unless any || curr[:failed]
end

puts result


_, a, *q = readlines.map{|i| i.split.map(&:to_i)}
n = 2 * [a.count(1), a.count(-1)].min
puts q.map{|i, j|
  j -= i
  j.odd? && j < n ? 1 : 0
}

def query(l,r)
 ele = r - l + 1
 return false if ele.odd?
 val = ele / 2
 return true if val <= @one_count && val <= @minus_count
 return false
end

n,m = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i)
@one_count = arr.count(1)
@minus_count = n - @one_count
ans = []
m.times do
	l,r = gets.split(" ").map(&:to_i)
	ans << (query(l,r) ? 1 : 0)
end
ans.each do |a|
	puts a
end

# def query(l,r)
#  ele = r - l + 1
#  return false if ele.odd?
#  val = ele / 2
#  return true if val <= @one_count && val <= @minus_count
#  return false
# end

n,m = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i)
@one_count = arr.count(1)
@minus_count = n - @one_count
ans = []
m.times do
	l,r = gets.split(" ")
	l = l.to_i
	r = r.to_i
	ele = r - l + 1
	if ele.odd?
 		ans << false
 		next
 	end
 	val = ele / 2
 	if val <= @one_count && val <= @minus_count
 		ans << true
 		next
 	end
 	ans << false
end

ans.each do |a|
	puts a ? 1 : 0
end

n, m = gets.split.collect{|i| i.to_i}
a = Array.new(3,0)
gets.split.each{|i| a[i.to_i+1]+=1}
(1..m).each do
    l, r = gets.split.collect{|i| i.to_i}
    if (r-l+1).odd?
        puts 0
    elsif a[0] < (r-l+1)/2 or a[2] < (r-l+1)/2
        puts 0
    else
        puts 1
    end
end

n,m = gets.strip.split(' ').map {|x| x.to_i}
a = gets.strip.split(' ')
h = {'1' => 0, '-1' => 0}
a.each do |e| h[e] += 1 end

m.times do
  l, r = gets.strip.split(' ').map {|x| x.to_i}
  if( (l+r)%2 == 0)
    puts 0
  else
    k = ((r-l).abs+1)/2
    puts( (h.values.min >= k) ? 1 : 0 )
  end
end

n_m = gets.split.map { |i| i.to_i }
n = n_m[0]
m = n_m[1]

a_count = b_count = 0

a = gets.split.map do |i|
  r = i.to_i
  if r == 1
    a_count += 1
  else
    b_count += 1
  end
  r
end

for i in 1..m
  from_to = gets.split.map { |i| i.to_i }
  from = from_to[0]
  to = from_to[1]
  len = to - from + 1

  if (len % 2 == 0) && a_count >= len / 2 && b_count >= len / 2
    puts 1
  else
    puts 0
  end
end

n, m = gets.chomp.split(' ').collect{|item| item.to_i}
a = gets.chomp.split(' ').collect{|item| item.to_i }
a1 = a.count{|item| item == -1}
a2 = a.length - a1
for i in 1..m
  l, r = gets.chomp.split(' ').collect{|item| item.to_i}
  numbers = r - l + 1
  if numbers % 2 == 0
    if a1 >= numbers / 2 and a2 >= numbers / 2
      puts 1
    else
      puts 0
    end
  else
    puts 0
  end
end

#!/usr/bin/env ruby

n, m = gets.split.map(&:to_i)
x = gets.split.map(&:to_i)
a, b = x.count(-1), x.count(1)
m.times do
	l, r = gets.split.map(&:to_i)
	l = r - l + 1
	if l % 2 == 1
		puts 0
	else
		l = l / 2
		puts a >= l && b >= l ? 1 : 0
	end
end

n,m = gets.split.map{|x| x.to_i}
a = gets.split(' ').map{|x| x.to_i}
p = a.count{|x| x<0}
q = a.count{|x| x>0}
p += p;q += q;
# p p,q
m.times{
	(l,r) = gets.split.map{|x| x.to_i}
	l = r-l+1
	if (l&1)==1
		puts 0
	elsif l>p
		puts 0
	elsif l>q
		puts 0
	else
		puts 1
	end
}

n,m=gets.chomp.split.map(&:to_i)
a=gets.chomp.split.map(&:to_i)
aa,aaa=a.count(1),a.count(-1)
m.times do
	l,r=gets.chomp.split.map(&:to_i)
	x=a[l-1..r-1]
	len=x.length
	puts (len.even? && 2*aa>=len && 2*aaa>=len)? "1" : "0"
end

n, m = gets.split(' ').map { |i| i.to_i }
a = b = 0
gets.split(' ').map {|i|
    i = i.to_i
    a += 1 if i == 1
    b += 1 if i == -1
}
m.times do
    l, r = gets.split(' ').map { |i|  i.to_i}
    width = r - l + 1
    if width % 2 != 0 or (width / 2 > a or width / 2 > b)
        puts 0
    else
        puts 1
    end
end

n, m = gets.split(' ').map { |i| i.to_i }
a = b = 0
gets.split(' ').map {|i| i.to_i == 1 ? a += 1 : b += 1}
m.times do
    l, r = gets.split(' ').map { |i|  i.to_i}
    width = r - l + 1
    puts (width % 2 != 0 or (width / 2 > a or width / 2 > b)) ? 0 : 1
end

def run
  n, m = $stdin.gets.split.map(&:to_i)
  a = $stdin.gets.split.map(&:to_i)

  plus  = a.count {|x| x > 0 }
  minus = n - plus
  $stdin.readlines.each do |line|
    l, r = line.split.map(&:to_i)
    len = r - l + 1

    if len % 2 == 0 and plus*2 >= len and minus*2 >= len
      puts "1"
    else
      puts "0"
    end
  end
end

run if $0 == __FILE__

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

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

    query = []

    m.times do
      query << gets.chomp.split(' ').map(&:to_i)
    end

    mc = array.count(-1)
    pc = array.count(1)

    query.each do |q|
      size = q[1] - q[0] + 1

      if(size.even? && size > 1 && size/2 <= mc && size/2 <= pc)
        puts 1
      else
        puts 0
      end
    end
  end
end

a = A.new

# def query(l,r)
#  ele = r - l + 1
#  return false if ele.odd?
#  val = ele / 2
#  return true if val <= @one_count && val <= @minus_count
#  return false
# end

n,m = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i)
@one_count = arr.count(1)
@minus_count = n - @one_count
ans = []
m.times do
	l,r = gets.split(" ")
	l = l.to_i
	r = r.to_i
	ele = r - l + 1
	if ele.odd?
 		ans << "0"
 		next
 	end
 	val = ele / 2
 	if val <= @one_count && val <= @minus_count
 		ans << "1"
 		next
 	end
 	ans << "0"
end

ans.each do |a|
	puts a
end

#Good http://codeforces.com/contest/302/problem/A
# From TLE to 811 ms
# Appending to array and iterating and displaying result is way faster than puts result directly around 100 ms diff
# Removed the method calls
# removed boolean checks
# Removed the map and did to_i directly for l and r
# Directly appended string to answer - there will be a to_s call for puts that cost from 889 to 811 the fuck

# def query(l,r)
#  ele = r - l + 1
#  return false if ele.odd?
#  val = ele / 2
#  return true if val <= @one_count && val <= @minus_count
#  return false
# end

n,m = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i)
@one_count = arr.count(1)
@minus_count = n - @one_count
ans = []
m.times do
	l,r = gets.split(" ")
	l = l.to_i
	r = r.to_i
	ele = r - l + 1
	if ele.odd?
 		puts "0"
 		next
 	end
 	val = ele / 2
 	if val <= @one_count && val <= @minus_count
 		puts "1"
 		next
 	end
 	puts "0"
end

# ans.each do |a|
# 	puts a
# end

#Good http://codeforces.com/contest/302/problem/A
# From TLE to 811 ms
# Removed the method calls
# removed boolean checks
# Removed the map and did to_i directly for l and r
# Directly appended string to answer - there will be a to_s call for puts that cost from 889 to 811 the fuck

# def query(l,r)
#  ele = r - l + 1
#  return false if ele.odd?
#  val = ele / 2
#  return true if val <= @one_count && val <= @minus_count
#  return false
# end

n,m = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i)
@one_count = arr.count(1)
@minus_count = n - @one_count
ans = []
m.times do
	l,r = gets.split(" ").map(&:to_i)
	ele = r - l + 1
	if ele.odd?
 		ans << false
 		next
 	end
 	val = ele / 2
 	if val <= @one_count && val <= @minus_count
 		ans << true
 		next
 	end
 	ans << false
end

ans.each do |a|
	puts a ? 1 : 0
end

# def query(l,r)
#  ele = r - l + 1
#  return false if ele.odd?
#  val = ele / 2
#  return true if val <= @one_count && val <= @minus_count
#  return false
# end

n,m = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i)
@one_count = arr.count(1)
@minus_count = n - @one_count
ans = []
m.times do
	l,r = gets.split(" ")
	l = l.to_i
	r = r.to_i
	ele = r - l + 1
	if ele.odd?
 		ans << 0
 		next
 	end
 	val = ele / 2
 	if val <= @one_count && val <= @minus_count
 		ans << 1
 		next
 	end
 	ans << 0
end

ans.each do |a|
	puts a
end

puts readlines[2..-1].map{|i| i.split.map(&:to_i).inject(&:-) % 2}

_, a, *q = readlines.map{|i| i.split.map(&:to_i)}
n = 2 * [a.count(1), a.count(-1)].min
puts q.map{|i, j|
  j -= i
  j.even? && j <= n ? 1 : 0
}

n,m=gets.chomp.split.map(&:to_i)
a=gets.chomp.split.map(&:to_i)
aa,aaa=a.count(1),a.count(-1)
m.times do
	l,r=gets.chomp.split.map(&:to_i)
	x=a[l-1..r-1]
	len=x.length
	puts(len.even? && 2*aa>=len && 2*aaa>=x.length)? "1" : "0"
end

n,m=gets.chomp.split.map(&:to_i)
a=gets.chomp.split.map(&:to_i)
m.times do
	l,r=gets.chomp.split.map(&:to_i)
	x=a[l-1..r-1]
	puts (x.length.even? && 2*a.count(1)>=x.length && 2*a.count(1)>=x.length)? "1" : "0"
end

n,m=gets.chomp.split.map(&:to_i)
a=gets.chomp.split.map(&:to_i)
m.times do
	l,r=gets.chomp.split.map(&:to_i)
	x=a[l-1..r-1]
	puts (x.length.even? && 2*x.count(1)>=x.length && 2*x.count(1)>=x.length)? "1" : "0"
end

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

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

    query = []

    m.times do
      query << gets.chomp.split(' ').map(&:to_i)
    end

    query.each do |q|
      if array[(q[0]-1)..(q[1]-1)].inject(:+) == 0
        puts 1
      else
        puts 0
      end
    end
  end
end

a = A.new

n,m = gets.strip.split(' ').map {|x| x.to_i}
a = gets.strip.split(' ')
h = Hash.new 0
a.each do |e|
  h[e] += 1
end
p h

(0...m).to_a.map do
  l, r = gets.strip.split(' ').map {|x| x.to_i}
  if( (l+r)%2 == 0)
    puts 0
  else
    k = ((r-l).abs+1)/2
    puts( (h.values.min >= k) ? 1 : 0 )
  end
end

n,m = gets.strip.split(' ').map {|x| x.to_i}
a = gets.strip.split(' ')
h = Hash.new 0
a.each do |e|
  h[e] += 1
end

(0...m).to_a.map do
  l, r = gets.strip.split(' ').map {|x| x.to_i}
  if( (l+r)%2 == 0)
    puts 0
  else
    k = ((r-l).abs+1)/2
    puts( (h.values.min >= k) ? 1 : 0 )
  end
end

n,m = gets.strip.split(' ').map {|x| x.to_i}
a = gets.strip.split(' ')
h = Hash.new 0
a.each do |e|
  h[e] += 1
end

(0...m).to_a.map do
  l, r = gets.strip.split(' ').map {|x| x.to_i}
  if( (l+r)%2 == 0)
    puts 0
  else
    k = (r-l+1)/2
    puts( (h.values.min >= k) ? 1 : 0 )
  end
end

n_m = gets.split.map { |i| i.to_i }
n = n_m[0]
m = n_m[1]

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

for i in 1..m
  from_to = gets.split.map { |i| i.to_i }
  from = from_to[0]
  to = from_to[1]

  sum = 0
  for j in from..to
    sum += a[j-1]
  end
  if sum == 0
    puts 1
  else
    puts 0
  end
end

n, m = gets.chomp.split(' ').collect{|item| item.to_i}
a = gets.chomp.split(' ').collect{|item| item.to_i }
a1 = a.count{|item| item == -1}
a2 = a.length - a1
puts m
for i in 1..m
  l, r = gets.chomp.split(' ').collect{|item| item.to_i}
  numbers = r - l + 1
  if numbers % 2 == 0
    if a1 >= numbers / 2 and a2 >= numbers / 2
      puts 1
    else
      puts 0
    end
  else
    puts 0
  end
end

n, m = gets.chomp.split(' ').collect{|item| item.to_i}
a = gets.chomp.split(' ').collect{|item| item.to_i }
a1 = a.count{|item| item == -1}
a2 = a.length - a1
for i in 1..m
  l, r = gets.chomp.split(' ').collect{|item| item.to_i}
  numbers = r - l + 1
  if numbers % 2 == 0
    puts 1 if a1 >= numbers / 2 and a2 >= numbers / 2
  else
    puts 0
  end
end


dp = [0];
for i in 1..2001
	dp[i] = -1
end

n = gets.to_i

for i in 1..n
	t, x = gets.split(' ')
	x = x.to_i + 1
	if t == 'sell' && dp[x] >= 0
		dp[0] = [dp[0], dp[x] + 2**(x-1)].max
	else
		dp[x] = dp[0]
	end
end

puts dp[0]

dp = [0]
for i in 1..2001
    dp[i] = -1
end
n = STDIN.gets.to_i

for i in 1..n
    data = STDIN.gets.split(' ')
    s = data[0]
    t = data[1].to_i + 1
    if s == "sell"
        if dp[t] >= 0 && dp[t] + 2 ** (t-1) > dp[0]
            dp[0] = dp[t] + 2 ** (t-1)
        end
    else
        dp[t] = dp[0]
    end
end

puts dp[0]

def read
	a, b = gets.split
	a == 'win' ? b.to_i : -1 - b.to_i
end

n = gets.to_i
n.times{$* << read}
ans = 0
$*.reject{|i| i >= 0}.sort.each do |x|
	next unless b = $*.index(x)
	next unless a = $*[0 .. b].rindex(-1 - x)
	next unless $*[a..b].all?	# WA5
	ans += 2 ** (-1 - x)
	$*[a..b] = [nil]
end
p ans

n = gets.to_i
x = Array.new(n, -1)
dp = Array.new(n, 0)
(0 .. n - 1).each do |i|
	a, b = gets.split
	b = b.to_i
	dp[i] = dp[i - 1] if i > 0
	if a == 'win' then
		x[i] = b
	else
		(0 .. i - 1).each do |j|
			dp[i] = [dp[i], dp[j] + (1 << b)].max if x[j] == b
		end
	end
end
p dp[-1]

__END__
def read
	a, b = gets.split
	a == 'win' ? b.to_i : -1 - b.to_i
end

n, s, a = gets.to_i, 0, []
n.times{a << read}
a.reject{|i| i >= 0}.sort.each do |x|
	next unless t = a.index(x)
	next unless f = a[0 .. t].rindex(-1 - x)
	s += 1 << -1 - x
	a.slice!(f .. t)
end
p s

WA5: test data error?

def read
	a, b = gets.split
	a == 'win' ? b.to_i : -1 - b.to_i
end

n, s, a = gets.to_i, 10000000000000000000, []
n.times{a << read}
a.reject{|i| i >= 0}.sort.each do |x|
	next unless t = a.index(x)
	next unless f = a[0 .. t].rindex(-1 - x)
	s += 1 << -1 - x
	a.slice!(f .. t)
end
p s - 10000000000000000000

def read
    a, b = gets.split
    a == 'win' ? b.to_i : -1 - b.to_i
end

n, s, a = gets.to_i, 0, []
n.times{a << read}
g = a.reject{|i| i >= 0}.sort
g.each do |x|
    next if (t = a.index(x)).nil?
    next if (f = a[0 .. t].rindex(-1 - x)).nil?
    s += 1 << -1 - x
    a.slice!(f .. t)
end
p s

def read
	a, b = gets.split
	a == 'win' ? b.to_i : -1 - b.to_i
end

n = gets.to_i
n.times{$* << read}
ans = 0
$*.reject{|i| i >= 0}.sort.each do |x|
	next unless b = $*.index(x)
	next unless a = $*[0 .. b].rindex(-1 - x)
	ans += 2 ** (-1 - x)
	$*.slice!(a, b - a)
end
p ans

def read
    a, b = gets.split
    a == 'win' ? b.to_i : -1 - b.to_i
end

n, s, a = gets.to_i, 0, []
n.times{a << read}
t = a.reject{|i| i >= 0}.sort
t.each do |x|
    next if (t = a.index(x)).nil?
    next if (f = a[0 .. t].rindex(-1 - x)).nil?
    s += 1 << -1 - x
    a.slice!(f .. t)
end
p s

def read
    a, b = gets.split
    a == 'win' ? -1 - b.to_i : b.to_i
end

n, s, a = gets.to_i, 0, []
n.times{a << read}
a.reject{|i| i < 0}.sort.reverse.each do |x|
    next unless t = a.rindex(x)
    next unless f = a[0 .. t].rindex(-1 - x)
    s += 1 << x
    a.slice!(f .. t)
end
p s

def read
    a, b = gets.split
    a == 'win' ? b.to_i : -1 - b.to_i
end

n, s, a = gets.to_i, 0, []
n.times{a << read}
a.reject{|i| i >= 0}.sort.each do |x|
    next unless t = a.index(x)
    raise Exception.new() if a.index(x) != a.rindex(x)
    next unless f = a[0 .. t].rindex(-1 - x)
    s += 1 << -1 - x
    a.slice!(f .. t)
end
p s

def read
    a, b = gets.split
    a == 'win' ? b.to_i : -1 - b.to_i
end

n, s, a = gets.to_i, 0, []
n.times{a << read}
a.reject{|i| i >= 0}.sort.each do |x|
    next if (t = a.index(x)).nil?
    next if (f = a[0 .. t].rindex(-1 - x)).nil?
    s += 1 << -1 - x
    a.slice!(f .. t)
end
p s

def read
	a, b = gets.split
	a == 'win' ? b.to_i : -1 - b.to_i
end

n = gets.to_i
a = []
n.times{a << read}
s = 0
a.reject{|i| i >= 0}.sort.each do |x|
	next unless t = a.index(x)
	next unless f = a[0 .. t].rindex(-1 - x)
	s += 2 ** (-1 - x)
	a.slice!(f .. t)
end
puts s

def read
    a, b = gets.split
    a == 'win' ? b.to_i : -1 - b.to_i
end

n, s, a = gets.to_i, 0, []
n.times{a << read}
raise Exception.new() if a.reject{|i| i >= 0}.sort.uniq!
a.reject{|i| i >= 0}.sort.each do |x|
    next unless t = a.index(x)
    next unless f = a[0 .. t].rindex(-1 - x)
    s += 1 << -1 - x
    a.slice!(f .. t)
end
p s

def read
    a, b = gets.split
    a == 'win' ? -1 - b.to_i : b.to_i
end

n, s, a = gets.to_i, 0, []
n.times{a << read}
a.reject{|i| i < 0}.sort.each do |x|
    next unless t = a.index(x)
    next unless f = a[0 .. t].rindex(-1 - x)
    s += 1 << x
    a.slice!(f .. t)
end
p s

def read
    a, b = gets.split
    a == 'win' ? -1 - b.to_i : b.to_i
end

n, s, a = gets.to_i, 0, []
n.times{a << read}
a.reject{|i| i < 0}.sort.reverse.each do |x|
    next unless t = a.index(x)
    next unless f = a[0 .. t].rindex(-1 - x)
    s += 1 << x
    a.slice!(f .. t)
end
p s

def read
	a, b = gets.split
	a == 'win' ? b.to_i : -1 - b.to_i
end

n = gets.to_i
n.times{$* << read}
ans = 0
$*.reject{|i| i >= 0}.sort.each do |x|
	next unless b = $*.index(x)
	next unless a = $*[0 .. b].rindex(-1 - x)
	ans += 2 ** (-1 - x)
	$*.slice!(a .. b)
end
p ans

def read
    a, b = gets.split
    a == 'win' ? b.to_i : -1 - b.to_i
end

n, s, a = gets.to_i, 0, []
n.times{a << read}
a.reject{|i| i >= 0}.sort.each do |x|
    next unless t = a.index(x)
    next unless f = a[0 .. t].rindex(-1 - x)
    s += 1 << -1 - x
    a[f .. t] = nil
end
p s


n, c = gets.split.map{|x| x.to_i}
puts(gets.split.map{|x| x.to_i}.inject([0, 0]){|s, x| [x - s[1] <= c ? s[0] + 1 : 1, x]}[0])

a,b=gets.split.map &:to_i
k=1
m=gets.split
a.times{|x|
m[a-x-1].to_i-m[a-x-2].to_i>b ? break : k+=1}
$><<[k,a].min

n, c = gets.split.map &:to_i
a = gets.split.map &:to_i
ans = 1
(1...n).each do |i|
    if a[i] - a[i - 1] > c
        ans = 1
    else
        ans += 1
    end
end
puts ans

#!/usr/bin/ruby
n,c,*a=$<.read.split.map(&:to_i)
r=1
a.each_cons(2){|x,y|
	if y-x>c
		r=1
	else
		r+=1
	end
}
p r

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

ans = 1
for i in 1...mas.size() do
    if(mas[i]-mas[i-1] <= c)
        ans+=1;
    else
        ans = 1;
    end
end

puts ans;

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

last = a.first
count = 0
a.each do |d|
  if last - d <= c
    count += 1
    last = d
  else
    break
  end
end
puts count

c = gets.chop.split(' ').map{ |x| x.to_i }[1]

t = gets.chop.split(' ').map{ |x| x.to_i }

count = 1

i = t.size - 1

while i != 0
    break if t[i] - c > t[i - 1]
    count += 1
    i -= 1;
end

print count

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

times = gets.split.map{|x| x.to_i}

words = 0
prevtime = 0
for sec in times
  if (sec - prevtime) > c
    words = 1
  else
    words += 1
  end
  prevtime = sec
end

puts words

class Program
    def initialize(input)
        input.each_with_index do |line, idx|
            case idx
            when 0
                args = line.split(" ")
                @n = Integer(args[0])
                @c = Integer(args[1])
            when 1
                @arr = line.split(" ")
                @arr = @arr.map{ |i| i = Integer(i) }
                @arr.reverse!
                puts getScreen()
                #@arr.sort!
            when 2

            else

            end
        end
    end

    def getScreen()
        rtn = 1
        @arr.each_with_index do |e,i|
            if i + 1 < @n
                if e - @arr[i+1] > @c
                    rtn = i + 1
                    break
                end
                if i + 1 == @n - 1
                    rtn = @n
                end
            end
        end
        return rtn
    end
end

main = Program.new(ARGF)

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

ans = 1
(n-1).downto(1) do |i|
  b = ts[i]
  a = ts[i-1]
  if b - a <= c
    ans += 1
  else
    break
  end
end
puts ans

n, c = gets.strip.split.map(&:to_i)
t = gets.strip.split.map(&:to_i)
a = t[0]
words = 1
(1...n).each do |i|
    b = t[i]
    if b - a <= c
        words += 1
    else
        words = 1
    end
    a = b
end
puts words

n,c=gets.split.map{|e| e.to_i}
x=gets.split.reverse.map{|e| e.to_i}
ans=1
while x.size>1
	if x[0]-x[1]>c then
		break
	end
	ans+=1
	x.shift
end
puts ans

n,c = gets.split.map(&:to_i)
t = gets.split.map(&:to_i)
r = 1
(n - 1).downto(1) do |i|
	if t[i] - t[i - 1] <= c
		r += 1
	else
		break
	end
end
p r

total, dist = gets.split(' ').map(&:to_i)
ats = gets.split(' ').map(&:to_i)

next_at = ats.last
count = 0
ats.reverse_each do |at|
  if next_at - at <= dist
    count += 1
  else
    break
  end
  next_at = at
end

puts count

n, c=gets.split.map &:to_i
t=gets.split.map &:to_i

res=0
x=t[0]
t.each {|i|
    if i-x<=c
        res+=1
    else
        res=1
    end
    x=i
}
p res

n,c,a=gets.split.map(&:to_i)<<1
gets.split.map(&:to_i).each_cons(2){|x,y|a=(y-x>c)?1:a+1}
p a

n,c=gets.split.map(&:to_i)
cnt=1
gets.split.map(&:to_i).each_cons(2) do|x,y|
    cnt+=1
    cnt=1 if y-x>c
end
p cnt

# coding: utf-8

# if __FILE__ == $0
# end

n, c = gets.split.map(&:to_i)
words = 0
last_words_spent = 0
gets.split.each { |i|
    i = i.to_i
    if (i - last_words_spent) > c
        words = 1
    else
        words += 1
    end
    last_words_spent = i
}

puts words

# be name khoda ...
def read_ints; STDIN.gets.split.map(&:to_i); end
n, c = read_ints
a = read_ints
res = 1
i = 0
(n-1).times do
  if(a[i]+c < a[i+1])
    res = 1
  else
    res +=1
  end
  i+=1
end
puts res

input_values = gets.chomp
in_val = input_values.split(' ').map(&:to_i)
total_input = in_val[0]
cons_value = in_val[1]
string_input = gets.chomp
array = string_input.split(' ').map(&:to_i)
i = 1
count = 1
while i < total_input do
	if array[i]-array[i-1] <= cons_value
		count += 1
	else
	  count = 1
	end
	i += 1
end
puts count

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

k = 0
p = 0
a.each do |x|
  k += 1
  k = 1 if x-p>c
  p = x
end

puts k

n, c = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
ans = 1
n.times do |i|
  if a[i] - a[i - 1] > c || i == 0
    ans = 1
  else
    ans += 1
  end
end
puts ans

n, c = gets.split.map(&:to_i)
t = gets.split.map(&:to_i)
ans = 1
1.upto(n - 1){|i|
    if t[i] - t[i - 1] > c
        ans = 1
    else
        ans += 1
    end
    }
p ans

typeNum, delay = gets.chomp.split(' ').map(&:to_i)
inputs = gets.chomp.split(' ').map(&:to_i)

char = 1

for i in (1...(inputs.length))
	diff = inputs[i] - inputs[i - 1]
	char = 1 if diff > delay
	char += 1 if diff <= delay
end

puts char

a,b=gets.split.map &:to_i
k=1
m=gets.split
a.times{|x|
m[a-x-1].to_i-m[a-x-2].to_i<=b ? k+=1 : break}
$><<k

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

ans = 1
for i in 1...mas.size() do
    if(mas[i]-mas[i-1] <= c)
        ans+=1;
    else
        ans = 0;
    end
end

puts ans;

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

a.map!.with_index do |_, i|
  diff = a[i + 1].nil? ? -10000000000 : a[i] - a[i + 1]
  diff <= c ? 1 : 0
end
puts a.chunk { |x| x == 1 }.to_a.first.last.length + 1

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

a.map!.with_index do |_, i|
  diff = a[i + 1].nil? ? -10000000000 : a[i] - a[i + 1]
  diff <= c ? 1 : 0
end
if n == 1
  puts 1
else
  puts a.chunk { |x| x == 1 }.to_a.first.last.length + 1
end

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

times = gets.split.map{|x| x.to_i}

words = 0
prevtime = 0
for sec in times
  if (sec - prevtime) >= c
    words = 2
  else
    words += 1
  end
  prevtime = sec
end

puts words

class Program
    def initialize(input)
        input.each_with_index do |line, idx|
            case idx
            when 0
                args = line.split(" ")
                @n = Integer(args[0])
                @c = Integer(args[1])
            when 1
                @arr = line.split(" ")
                @arr = @arr.map{ |i| i = Integer(i) }
                @arr.reverse!
                puts getScreen()
                #@arr.sort!
            when 2

            else

            end
        end
    end

    def getScreen()
        rtn = 1
        @arr.each_with_index do |e,i|
            if i + 1 < @n
                if e - @arr[i+1] > @c
                    rtn = i + 1
                    break
                end
            end
        end
        return rtn
    end
end

main = Program.new(ARGF)

class Program
    def initialize(input)
        input.each_with_index do |line, idx|
            case idx
            when 0
                args = line.split(" ")
                @n = Integer(args[0])
                @c = Integer(args[1])
            when 1
                @arr = line.split(" ")
                @arr = @arr.map{ |i| i = Integer(i) }
                @arr.reverse!
                puts getScreen()
                #@arr.sort!
            when 2

            else

            end
        end
    end

    def getScreen()
        rtn = 1
        @arr.each_with_index do |e,i|
            if i + 1 < @n
                if e - @arr[i+1] > @c
                    rtn = i + 1
                    break
                end
                if i == @n - 1
                    rtn = @n
                end
            end
        end
        return rtn
    end
end

main = Program.new(ARGF)

class Program
    def initialize(input)
        input.each_with_index do |line, idx|
            case idx
            when 0
                args = line.split(" ")
                @n = Integer(args[0])
                @c = Integer(args[1])
            when 1
                @arr = line.split(" ")
                @arr = @arr.map{ |i| i = Integer(i) }
                @arr.reverse!
                puts getScreen()
                #@arr.sort!
            when 2

            else

            end
        end
    end

    def getScreen()
        rtn = 0
        @arr.each_with_index do |e,i|
            if i + 1 < @n
                if e - @arr[i+1] > @c
                    rtn = i + 1
                    break
                end
            end
        end
        return rtn
    end
end

main = Program.new(ARGF)

total, dist = gets.split(' ').map(&:to_i)
ats = gets.split(' ').map(&:to_i)

next_at = ats.last
count = 0
ats.reverse_each do |at|
  if next_at - at <= dist
    count += 1
  end
  next_at = at
end

puts count

input_values = gets.chomp
in_val = input_values.split(' ').map(&:to_i)
total_input = in_val[0]
cons_value = in_val[1]
string_input = gets.chomp
array = string_input.split(' ').map(&:to_i)
i = 1
count = 0
while i < total_input do
	if array[i]-array[i-1] <= cons_value
		count += 1
	else
	  count = 1
	end
	i += 1
end
puts count

n, c = gets.split.map(&:to_i)
t = gets.split.map(&:to_i)
ans = 0
tmp = 1
1.upto(n - 1){|i|
    if t[i] - t[i - 1] > c
        ans = [ans, tmp].max
        tmp = 1
    else
        tmp += 1
    end
    }
ans = [ans, tmp].max
p ans


require 'set'

def fn(aa, d)
  out = []
     ret = 0
  for e in aa
    n = e % d
    if (n != 0)
      out << n
    else
     ret = 1
    end
  end
  r = out.tally

  for k,v in r
    if k + k == d
      ret += 1
      next
    end
    if v == 0; next; end

    j = d - k
    r[k] = 0
    vj = r[j]
    if vj
      r[j] = 0
      if vj >= v+1
        ret += 1
        ret += vj - (v+1)
       # printf("j:%d k:%d > ret:%d\n",k,j,ret)
      elsif vj == v
        ret += 1
       # printf("k:%d j:%d == ret:%d\n",k,j,ret)
      else
        ret += 1
        ret += v - (vj+1)
       # printf("k:%d j:%d < ret:%d\n",k,j,ret)
      end
    else
      ret += v
       # printf("j:%d ret:%d\n",k,ret);
    end

  end
  puts ret
end

tt = gets.to_i
tt.times {
  (_, d)= gets.chomp!.split(' ').map &:to_i
  aa = gets.chomp!.split(' ').map &:to_i
  fn(aa,d)
}

t = gets.chomp.to_i
for test in (1..t)
  n, m = gets.chomp.split(' ').map { |x| x.to_i }
  a = gets.chomp.split(' ').map { |x| x.to_i }
  r = Array.new(m).fill(0)
  for i in (0..n-1)
    r[a[i] % m] += 1
  end
  ans = 0
  for i in (1..m/2)
    if r[i] == r[m - i]
      if r[i] > 0
        ans += 1
      end
    elsif r[i] == 0 && r[m - i] != 0
      ans += r[m - i]
    elsif r[i] != 0 && r[m - i] == 0
      ans += r[i]
    else
      ans += 1 + [0, (r[i] - r[m - i]).abs - 1].max
    end
  end
  if r[0] != 0
    ans += 1
  end
  puts ans
end

gets.to_i.times {
  n, m = gets.split(' ').map(&:to_i)
  xs = gets.split(' ').map(&:to_i)

  groups = Array.new(m) { [] }
  xs.each do |el|
    groups[el % m].push(el)
  end

  res = 0
  if groups[0].size > 0
    res += 1
  end
  if m % 2 == 0 && groups[m / 2].size > 0
    res += 1
  end
  (1..((m - 1) / 2)).each do |rem|
    min = [groups[rem].size, groups[m - rem].size].min
    max = [groups[rem].size, groups[m - rem].size].max
    if min == 0
      res += max
    elsif min == max
      res += 1
    else
      res += (max - min)
    end
  end
  puts res
}

t = gets.chomp.to_i
for test in (1..t)
  n, m = gets.chomp.split(' ').map { |x| x.to_i }
  a = gets.chomp.split(' ').map { |x| x.to_i }
  r = Array.new(m).fill(0)
  for i in (0..n-1)
    r[a[i] % m] += 1
  end
  z = 0
  for i in (1..(m-1)/2)
    y = [r[i], r[m - i]].min * 2 + 1
    z += 1 + [0, r[i] + r[m - i] - y].max
  end
  if r[0] != 0
    z += 1
  end
  if m % 2 == 0 && r[m / 2] != 0
    z += 1
  end
  puts z
end


t = gets.to_i
t.times do
	n,k = gets.split.map(&:to_i)
	a = gets.split.map(&:to_i)
	b = gets.split.map(&:to_i)
	sum = a.sum
	a.sort!
	b.sort!.reverse!
	for i in 0...k
		sum += b[i] - a[i] if b[i] > a[i]
	end
	puts sum
end

t = gets.chomp.to_i
t.times do |testcase|
  (n,k) = gets.split.map(&:to_i)
  a = gets.split.map(&:to_i)
  b = gets.split.map(&:to_i)
  a = a.sort;a = a.reverse;b = b.sort
  #print a ; print "\n"
  a_ptr = -1 ; b_ptr = -1
  while k>0
    if b[b_ptr] >= a[a_ptr]
      a[a_ptr] = b[b_ptr]
      a_ptr-=1 ; b_ptr-=1
    end
    k-=1
  end
  res = 0
  a.each do |num|
    res += num
  end
  puts res
end

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

  a = gets.split(' ').map(&:to_i).sort
  b = gets.split(' ').map(&:to_i).sort.reverse

  result = a.inject(&:+)

  a[0, k].zip(b[0, k]).each do |x|
    break if x.first >= x.last

    result += x.last - x.first
  end

  puts result
end

gets.to_i.times do
	n, k = gets.split.map(&:to_i)
	a = gets.split.map(&:to_i).sort
	b = gets.split.map(&:to_i).sort.reverse
	puts (0...k).sum { |i| [a[i], b[i]].max } + (k...n).sum { |i| a[i] }
end


p = readline.chomp.rjust(10**6, "0")
#puts p
q = readline.chomp.rjust(10**6, "0")
#puts q
if p == q
  puts("=")
elsif p < q
  puts("<")
else
  puts(">")
end

a = gets.strip
b = gets.strip

if a.size > b.size
   b = ("0" * (a.size - b.size)).concat(b)
elsif b.size > a.size
   a = ("0" * (b.size - a.size)).concat(a)
end

0.upto(a.size-1) do |shift|
  if a[shift] > b[shift]
    puts(">")
    exit
  end
  if a[shift] < b[shift]
    puts("<")
    exit
  end
end

puts "="

a=gets.to_i
b=gets.to_i
if a<b
 puts"<"
elsif a>b
 puts">"
else
 puts"="
end

a = gets.chomp
b = gets.chomp
a = a[1..-1] while a[0] == '0'
b = b[1..-1] while b[0] == '0'
puts ">" if a.length > b.length
puts "<" if a.length < b.length
exit unless a.length == b.length

(0..a.length - 1).each do |i|
  puts "<" if a[i] < b[i]
  puts ">" if a[i] > b[i]
  exit unless a[i] == b[i]
end
puts "="

a = gets.chomp
b = gets.chomp
while a[0] == '0'
  a = a[1..-1]
end
while b[0] == '0'
  b = b[1..-1]
end
if a.length > b.length
  puts ">"
  exit
end
if a.length < b.length
  puts "<"
  exit
end

(0..a.length - 1).each do |i|
  if a[i] < b[i]
    puts "<"
    exit
  end
  if a[i] > b[i]
    puts ">"
    exit
  end
end
puts "="

a = gets.to_i
b = gets.to_i
if a < b
 puts "<"
elsif a > b
 puts ">"
else
 puts "="
end

a=gets.to_i
b=gets.to_i
if a<b
  puts "<"
elsif a>b
 puts ">"
else
 puts "="
end

num1, num2 = STDIN.read.split("\n").map { |s| s.sub /^0+/,'' }
res = case (num1.size <=> num2.size)
	when 1
		'>'
	when -1
		'<'
	else
		case num1 <=> num2
		when 1
			'>'
		when -1
			'<'
		else
			'='
		end
end

puts res

a = gets.chomp.rjust(10**6, "0")
b = gets.chomp.rjust(10**6, "0")

if a == b then
  puts "="
elsif a > b then
  puts ">"
else
  puts "<"
end

a = gets
b = gets
if a > b
 puts "<"
elsif a < b
 puts ">"
else
 puts "="
end

a = gets
b = gets
if a > b
 puts ">"
elsif a < b
 puts "<"
else
 puts "="
end

a=gets.to_i
b=gets.to_i
p "=" if a==b
p "<" if a<b
p ">" if a>b

a = gets
b = gets

if a.size > b.size
   b = ("0" * (a.size - b.size)).concat(b)
elseif b.size > a.size
   a = ("0" * (b.size - a.size)).concat(a)
end

0.upto(a.size-1) do |shift|
  if a[-1-shift] > b[-1-shift]
    puts(">")
    exit
  end
  if a[-1-shift] < b[-1-shift]
    puts("<")
    exit
  end
end

puts "="

a = gets
b = gets

if a.size > b.size
  b = ("0" * (a.size - b.size)).concat(b)
elseif b.size > a.size
   a = ("0" * (b.size - a.size)).concat(a)
end

0.upto(a.size-1) do |shift|
  puts ">" and exit if a[-1-shift] > b[-1-shift]
  puts "<" and exit if a[-1-shift] > b[-1-shift]
end

puts "="

a = gets.strip
b = gets.strip

if a.size > b.size
   b = ("0" * (a.size - b.size)).concat(b)
elsif b.size > a.size
   a = ("0" * (b.size - a.size)).concat(a)
end

0.upto(a.size-1) do |shift|
  if a[-1-shift] > b[-1-shift]
    puts(">")
    exit
  end
  if a[-1-shift] < b[-1-shift]
    puts("<")
    exit
  end
end

puts "="

a = gets.strip
b = gets.strip

if a.size > b.size
   b = ("0" * (a.size - b.size)).concat(b)
elsif b.size > a.size
   a = ("0" * (b.size - a.size)).concat(a)
end

puts a
puts b
0.upto(a.size-1) do |shift|
  if a[shift] > b[shift]
    puts(">")
    exit
  end
  if a[shift] < b[shift]
    puts("<")
    exit
  end
end

puts "="

a = gets
b = gets

if a.size > b.size
  b = ("0" * (a.size - b.size)).concat(b)
elseif b.size > a.size
   a = ("0" * (b.size - a.size)).concat(a)
end

0.upto(a.size-1) do |shift|
  puts ">" && exit if a[-1-shift] > b[-1-shift]
  puts "<" && exit if a[-1-shift] < b[-1-shift]
end

puts "="

a = gets.chomp
b = gets.chomp
while a[0] == '0'
  a = a[1..-1]
end
while b[0] == '0'
  b = b[1..-1]
end
if a.length > b.length
  puts ">"
  exit
end
if a.length < b.length
  puts "<"
  exit
end

a.length.downto 1 do |i|
  if a[i - 1] < b[i - 1]
    puts "<"
    exit
  end
  if a[i - 1] > b[i - 1]
    puts ">"
    exit
  end
end
puts "="

a = gets.chomp
b = gets.chomp
a = a[1..-1] while a[0] == '0'
b = b[1..-1] while b[0] == '0'
p a
p b
puts ">" if a.length > b.length
puts "<" if a.length < b.length
exit unless a.length == b.length

(0..a.length - 1).each do |i|
  puts "<" if a[i] < b[i]
  puts ">" if a[i] > b[i]
  exit unless a[i] == b[i]
end
puts "="

a = gets.chomp
b = gets.chomp
a = a[1...-1] while a[0] == '0'
b = b[1..-1] while b[0] == '0'
puts ">" if a.length > b.length
puts "<" if a.length < b.length
exit unless a.length == b.length

(0..a.length - 1).each do |i|
  puts "<" if a[i] < b[i]
  puts ">" if a[i] > b[i]
  exit unless a[i] == b[i]
end
puts "="


n = Integer( gets.chomp )
input = gets.chomp
num = input.split(" ")

n -= 1
aux = "-1"
cont = 0
flag = false

while n>=0
	one = zero = 0
	i = 0

	while i<num[n].length

		if num[n][i]=='1'
			one += 1
		elsif num[n][i]=='0'
			zero += 1
		end

		i += 1

	end

	if(one==1 && zero+one==i)
		cont += num[n].length - 1
	else
		aux = num[n]
	end

	if i==1 && zero==i
		flag = true
	end

	n -= 1
end

if flag
	puts "0"
else
	if(aux=="-1")
		aux = "1"
	end

	print aux

	while cont>0
		print "0"
		cont -= 1
	end

	print "\n"
end

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

pow = 0
notbeautiful = "1"
tanks.each do |t|
  n1 = t.count("1")
  n0 = t.count("0")
  nrem = t.length - n1 - n0
  if t.length == 1 && n0 == 1 then
    puts 0
    exit
  elsif n1 != 1 or nrem != 0 then
    notbeautiful = t
  else
    pow += t.length - 1
  end
end
puts notbeautiful + "0" * pow

def isb(s)
    ones = 0
    zero = 0
    n = false
    for i in 0...s.length do
        ones += 1 if s[i] == '1'
        zero += 1 if s[i] == '0'
        n = true if s[i] != '1' && s[i] != '0'
    end
    return ones == 1 && n == false
end
def czero(s)
    zero = 0
    for i in 0...s.length do
        zero += 1 if s[i] == '0'
    end
    return zero
end
n = gets.to_i
p = gets.strip.split(" ")
ans = ""
ze = 0
cb = false
contzero = false
for str in p do
    contzero = true if str == "0"
   if isb(str)
       ze += czero(str)
   else
       cb = true
       ans << str
   end
end
if contzero
    puts ("0")
else
    ans << "1" if cb == false
    for i in 0...ze do
        ans << "0"
    end
    puts ans
end

n = gets.to_i
as = gets.chomp.split

p = 1
z = 0
as.each do |a|
  if a == '0'
    p = 0
    z = 0
    break
  elsif a != '1' + '0' * (a.size - 1)
    p = a
  else
    z += a.size - 1
  end
end

puts p.to_s + '0' * z

def isb(s)
    ones = 0
    zero = 0
    n = false
    for i in 0...s.length do
        ones += 1 if s[i] == '1'
        zero += 1 if s[i] == '0'
        n = true if s[i] != '1' && s[i] != '0'
    end
    return ones == 1 && n == false
end
def czero(s)
    zero = 0
    for i in 0...s.length do
        zero += 1 if s[i] == '0'
    end
    return zero
end
n = gets.to_i
p = gets.strip.split(" ")
ans = ""
ze = 0
for str in p do
   if isb(str)
       ze += czero(str)
   else
       ans << str
   end
end
for i in 0...ze do
    ans << "0"
end
puts ans

def isb(s)
    ones = 0
    zero = 0
    n = false
    for i in 0...s.length do
        ones += 1 if s[i] == '1'
        zero += 1 if s[i] == '0'
        n = true if s[i] != '1' && s[i] != '0'
    end
    return ones == 1 && n == false
end
def czero(s)
    zero = 0
    for i in 0...s.length do
        zero += 1 if s[i] == '0'
    end
    return zero
end
n = gets.to_i
p = gets.strip.split(" ")
ans = ""
ze = 0
contzero = false
for str in p do
    contzero = true if str == "0"
   if isb(str)
       ze += czero(str)
   else
       ans << str
   end
end
if contzero
    puts ("0")
else
    for i in 0...ze do
        ans << "0"
    end
    puts ans
end

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

pow = 0
notbeautiful = "1"
tanks.each do |t|
  n1 = t.count("1")
  n0 = t.count("0")
  nrem = t.length - n1 - n0
  if n1 == 0 && n0 == 1 then
    puts 0
    exit
  elsif n1 != 1 or nrem != 0 then
    notbeautiful = t
  else
    pow += t.length - 1
  end
end
puts notbeautiful + "0" * pow

n = Integer( gets.chomp )
input = gets.chomp
num = input.split(" ")

n -= 1
aux = "0"
cont = 0
flag = false

while n>=0
	one = zero = 0
	i = 0

	while i<num[n].length
		if num[n][i]=='1'
			one += 1
		elsif num[n][i]=='0'
			zero += 1
		end
		i += 1
	end

	if(one==1 && zero+one==i)
		cont += num[n].length - 1
	else
		aux = num[n]
	end

	if i==1 && zero==i
		flag = true
	end

	n -= 1
end

if flag
	puts "0"
else
	print aux
	while cont>0
		print "0"
		cont -= 1
	end
	print "\n"
end


t = gets.chomp.to_i
t.times do
    l, r = gets.chomp.split(" ").map(&:to_i)
    kizyun = r
    if l <= kizyun / 2
        puts "NO"
    else
        puts "YES"
    end
end

gets.to_i.times do
  l, r = gets.split.map(&:to_i)
  a = r + 1
  puts(if l * 2 >= a then "YES" else "NO" end)
end

T = gets.to_i

T.times do
  l, r = gets.split.map(&:to_i)

  if r < 2 * l
    puts 'YES'
  else
    puts 'NO'
  end
end

gets.to_i.times do
  l, r = gets.split.map &:to_i
  puts 2 * l > r ? "YES" : "NO"
end

t = gets.chomp.to_i
t.times do
    l, r = gets.chomp.split(" ").map(&:to_i)
    kizyun = r + 1
    if l <= kizyun / 2
        puts "NO"
    else
        puts "YES"
    end
end


n, m = gets.split(' ').map(&:to_i)
node = gets.split(' ').map(&:to_i)
max = 0.0
m.times do
  a, b, c = gets.split(' ').map(&:to_i)
  v = (node[a-1]+node[b-1]+0.0)/(c+0.0)
  max = v if max < v
end
puts max

n, m = gets.split.map { |i| i.to_i }
answer = 0.0
weight = gets.split.map{ |i| i.to_i }
m.times do
	x, y, w = gets.split.map{ |i| i.to_i }
	answer = [answer, (weight[x-1] + weight[y-1]) / w.to_f].max
end
puts answer

numbers = gets.split(' ')
n = numbers[0].to_i
m = numbers[1].to_i

values = gets.split(' ').map { |s| s.to_f }
best = 0.0
m.times do
  edge = gets.split(' ')
  a = edge[0].to_i
  b = edge[1].to_i
  edge_val = edge[2].to_f
  best = [best, (values[a - 1] + values[b - 1]) / edge_val].max
end

puts best

n,m=gets.split.map(&:to_i)
x=[0]+gets.split.map(&:to_f)
r=0.0
m.times do
  a,b,c=gets.split.map(&:to_i)
  y=(x[a]+x[b])/c
  r=y if r<y
end
p r

result = 0.0

n, m = gets.split.map(&:to_i)
v = [0] + gets.split.map(&:to_i)

m.times do
  a, b, c = gets.split.map(&:to_i)
  if result < (v[a] + v[b]).to_f / c
    result = (v[a] + v[b]).to_f / c
  end
end

puts result


n, x = gets.chomp.split.map(&:to_i)
d = 0
n.times do
  op, num = gets.chomp.split.map { |a| a.to_i.to_s == a ? a.to_i : a }

  if op == '+'
    x += num
  else
    if num > x
      d += 1
    else
      x -= num
    end
  end
end
puts [x, d].join(' ')

n,x=gets.split.map{|e| e.to_i}
ans=0
n.times{
	c,a=gets.split
	a=a.to_i
	if c=="+" then
		x+=a
	else
		if x<a then
			ans+=1
		else
			x-=a
		end
	end
}
puts [x,ans]*" "

n, x=gets.split.map &:to_i
res=0
n.times{
    c, d=gets.split
    d=d.to_i
    if c==?+
        x+=d
    else
        if x<d
            res+=1
        else
            x-=d
        end
    end
}
puts "#{x} #{res}"

nx = gets.chomp.split.map(&:to_i)
n = nx[0]
x = nx[1]
d = []
for i in (0..n-1)
	d[i] = gets.chomp.split
end

distress = 0
for i in (0..n-1)
	if d[i][0] == "+"
		x += d[i][1].to_i
	else
		if x-d[i][1].to_i < 0
			distress += 1
		else
			x -= d[i][1].to_i
		end
	end
end

print x
print ' '
print distress

#Made By Hasbi Hamza
a = gets.chomp.split(' ').map(&:to_i)
n=a[0].to_i
x=a[1].to_i
left=0
for i in 1..n
  r=gets.chomp.split(' ').map(&:to_s)
  str=r[0].to_s
  tmp=r[1].to_i
  if str=="+" then x+=tmp
  elsif str=="-" and x>=tmp then x-=tmp
  elsif str=="-" and x<tmp then left+=1
  end
end
puts("#{x} #{left}")

n, x = gets.split.map(&:to_i)
puts n.times.map{gets.delete(' ').to_i}.reduce([x,0]){|(x,k),d| x+d<0 ? [x,k+1] : [x+d,k]}.join(' ')

n, x = gets.split.map(&:to_i)
distressed = 0
n.times do
  s, d = gets.chomp.split
  if s == '+'
    x += d.to_i
  else
    if x < d.to_i
      distressed += 1
    else
      x -= d.to_i
    end
  end
end
puts "#{x} #{distressed}"

n, x = gets.split.map(&:to_i)
nsad = 0
n.times do
  s, d = gets.split
  d = d.to_i
  if s == '+'
    x += d
  elsif d <= x
    x -= d
  else
    nsad += 1
  end
end
print x, ' ', nsad

n, count = gets.split(' ').map(&:to_i)
sad = 0

1.upto(n) do
  c, x = gets.split(' ').map { |c| '+-'.include?(c) ? c : c.to_i }

  if c == '+'
    count += x
    next
  end

  if x > count
    sad += 1
    next
  end

  count -= x
end

puts "#{count} #{sad}"

lineNum, initial = gets.chomp.split(' ').map(&:to_i)

ice = initial
child = 0

lineNum.times do
	opCode, amount = gets.chomp.split(' ')

	amount = amount.to_i
	if opCode == "+"
		ice += amount
	elsif opCode == "-"
			if ice >= amount
				ice -= amount
			else
				child += 1
			end
	end
end

puts "#{ice} #{child}"

#!/usr/bin/ruby
n,x=gets.split.map(&:to_i)
r=0
n.times{
	a,b=gets.split.zip([:to_s,:to_i]).map{|x,y|x.send(y)}
	if a=='-'
		if b>x
			r+=1
		else
			x-=b
		end
	else
		x+=b
	end
}
puts [x,r]*' '

I =->{gets.split.map &:to_i}
RD =->{gets.to_i}

n, x = I[]
z = 0
n.times{
  a, b = gets.split
  b = b.to_i
  if a == '+'
    x += b
  else
    if x < b
      z += 1
    else
      x -= b
    end
  end
}

print x, ' ', z

loobnum , ice = gets.chomp.split.map(&:to_i)
ch = 0
while loobnum > 0
    sign , num = gets.chomp.split.map(&:to_s)
    num = num.to_i
    if sign == '-'
        if num > ice
            ch += 1
        else
            ice = ice - num
        end
    elsif sign == '+'
        ice = ice + num
    end
    loobnum -= 1
end

print "#{ice} " " #{ch}"

n, x = gets.split.map(&:to_i)
ans = 0
n.times do
  op, num = gets.split
  num = num.to_i
  if op == "+"
    x += num
  else
    if x >= num
      x -= num
    else
      ans += 1
    end
  end
end
puts "#{x} #{ans}"

n, x = gets.strip.split.map(&:to_i)
packs = x
distressed = 0
n.times do
    query, d = gets.strip.split
    d = d.to_i
    if query == '+'
        packs += d
    else
        if packs < d
            distressed += 1
        else
            packs -= d
        end
    end
end
puts "#{packs} #{distressed}"


q = gets.to_i
q.times {
  n = gets.to_i
  a = gets.split.map(&:to_i)
  p (a.inject(:+) * 1.0 / n).ceil
}

q = gets.to_i
q.times do
  n = gets.to_i
  as = gets.split.map(&:to_f)
  puts (as.inject(:+)/n).ceil
end

t = gets.to_i
z = []
(0..t - 1).each do |i|
  sum = 0
  po = gets
  str = gets
  z[i] = str
  bloc = z[i].split(' ')
  # bloc.map { |w| w = w.to_i}
  bloc.map { |v| sum += v.to_f }
  avg1 = sum/bloc.size
  puts avg1.ceil
end

# coding: utf-8

# if __FILE__ == $0
# end

q = gets.to_i
q.times {
    n = gets.to_i
    a = gets.split.map(&:to_i)
    sum = a.reduce(&:+)
    puts ((sum*1.0)/n).ceil
}

MOD=10**9+7
cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def desc(ar) ar.sort!{|x,y|y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end

#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end
def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
q = gi
ans=[]

q.times do |i|
  n = gi
  a = gsmi
  sum = sum(a)
  ans <<(sum/n.to_f).ceil
end

puts ans

q = gets.to_i
q.times do
  n = gets.to_i
  a = gets.split.map &:to_i
  s = a.inject(0) { |s, x| s + x }
  puts (s + n - 1)/n
end

1.upto(gets.to_i) do
  n = gets.to_i
  a = gets.split(' ').map(&:to_i).inject(&:+)
  puts a.div(n) + (a.modulo(n).zero? ? 0 : 1)
end


n = gets.to_i
as = gets.split(' ').map(&:to_i)
total = as.reduce(:+)
if total % 2 == 1
  puts "NO"
else
  max = as.max
  if max <= (total - max)
    puts "YES"
  else
    puts "NO"
  end
end

gets
s = gets.split.map(&:to_i).sort.reverse
puts (((s.inject(:+) % 2) == 0) && s[0] <= s.drop(1).inject(:+) ? :YES : :NO)

n = gets.to_i
as = gets.split(' ')[0..n].map(&:to_i)
total = as.reduce(:+)
if total % 2 == 1
  puts "NO"
else
  max = as.max
  if max <= (total - max)
    puts "YES"
  else
    puts "NO"
  end
end

def inp_i(array)
  if array == 1
    gets.split.map(&:to_i)
  else
    gets.chomp.to_i
  end
end
def inp_s(array)
  if array == 1
    gets.split.map
  else
    gets.chomp
  end
end
def inp_f(array)
  if array == 1
    gets.split.map(&:to_f)
  else
    gets.chomp.to_f
  end
end
#input temps (shotcuts)
a =inp_i(0)
arr = inp_i(1);
sum = 0 ; arr.each{|i| sum+=i}
if(sum%2==0 and arr.max <= sum-arr.max)
  puts "yes"
else
  puts "no"
end

gets
s = gets.split.map(&:to_i).sort
puts (((s.inject(:+) % 2) == 0) && s[0] <= s.drop(1).inject(:+) ? :YES : :NO)

gets
puts ((gets.split.map(&:to_i).inject(:+) % 2) == 0 ? :YES : :NO)


n, k = gets.split(" ").collect{|x| x.to_i}

if k == 1 and n > 1
  puts "-1"
elsif k == 1 and n == 1
  puts "a"
elsif k > n or k > 26
  puts "-1"
else
  abc = "abcdefghijklmnopqrstuvwxyz"
  s = "ab" * (n/2)
  s += "a" if (n % 2) == 1
  (k-2).times do |x|
    s[s.length-k+2+x] = abc[x+2]
  end
  puts s
end

n,k=gets.split.map(&:to_i)
if n<k
  p -1
  exit
end
if k==1
  if n==1
    puts "a"
  else
    p -1
  end
  exit
end
(1..n-k+2).each{|i| i&1==1?(print 'a'):(print 'b')}
(2..k-1).each{|i| print (('a'.ord+i).chr)}

class C
  def initialize
    n, k = $stdin.gets.chomp.split(' ').map(&:to_i)

    if n < k
      puts -1
      exit
    end

    str = ""

    if k > 2
      time = n - ( k - 2 )

      if time.even?
        str += "ab"*(time/2)
      else
        str += "ab"*(time/2)+"a"
      end

      ch = "c"

      (k-2).times do
        str += ch
        ch = ch.next
      end
    else
      if k == 1 && n > 1
        puts -1
        exit
      elsif k == 1 && n == 1
        puts "a"
        exit
      end

      if n.even?
        str += "ab" * (n/2)
      else
        str += "ab" * (n/2) + "a"
      end
    end

    puts str
  end
end

c = C.new

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

if m == 1 && n != 1 || m > n
  ans = "-1"
elsif m == 1
  ans = "a"
else
  L = n - (m - 2);
  ans = "ab" * (L/2);
  if L % 2 == 1
    ans += "a"
  end
  ans += ('c'..(m + 96).chr).to_a.join('');
end
puts ans

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

if k == 1 && n == 1
  puts 'a'
elsif k > 1 && n >= k
  chars = []
  c = 0
  (n - (k-2)).times do
    chars << c
    c = (c + 1) % 2
  end

  c = 2
  (k-2).times do
    chars << c
    c += 1
  end

  puts chars.map{|x| ('a'.ord + x).chr}.join
else
  puts -1
end

letters = ('c'..'z').to_a.join('')

n, k = gets.chomp.split.map(&:to_i)
if n == 1 && k == 1
  puts 'a'
  exit
end
if k > n || k == 1
  s = -1
else
  p = n - k + 2
  s = 'ab' * (p / 2)
  s += 'a' if p.odd?
  s += letters[0..(n - p - 1)] if n - p > 0
end
puts s

class C
  def initialize
    n, k = $stdin.gets.chomp.split(' ').map(&:to_i)

    if n < k
      puts -1
      exit
    end

    str = ""

    if k > 2
      time = n - ( k - 2 )

      if time.even?
        str += "ab"*time
      else
        str += "ab"*(time/2)+"a"
      end

      ch = "c"

      (k-2).times do
        str += ch
        ch = ch.next
      end
    else
      if k == 1 && n > 1
        puts -1
        exit
      elsif k == 1 && n == 1
        puts "a"
        exit
      end

      if n.even?
        str += "ab" * (n/2)
      else
        str += "ab" * (n/2) + "a"
      end
    end

    puts str
  end
end

c = C.new

n,k=gets.split.map(&:to_i)
if n<k
  p -1
  exit
end
if k==1
  if n==1
    p 'a'
  else
    p -1
  end
  exit
end
(1..n-k+2).each{|i| i&1==1?(print 'a'):(print 'b')}
(2..k-1).each{|i| print (('a'.ord+i).chr)}

n, k = gets.split(" ").collect{|x| x.to_i}

if k == 1 and n > 1
  puts "-1"
elsif k == 1 and n == 1
  puts "a"
elsif k > n or k > 26
  puts "-1"
else
  s = ""
  abc = "abcdefghijklmnopqrstuvwxyz"
  s = "ab" * (n/2)
  s += "a" if n % 2
  (k-2).times do |x|
    s[s.length-k+2+x] = abc[x+2]
  end
  puts s
end

n, k = gets.split(" ").collect{|x| x.to_i}

if k == 1 and n > 1
  puts "-1"
elsif k == 1 and n == 1
  puts "a"
elsif k > n or k > 26
  puts "-1"
else
  abc = "abcdefghijklmnopqrstuvwxyz"
  s = "ab" * (n/2)
  s += "a" if (n % 2) == 1
  puts s
  (k-2).times do |x|
    s[s.length-k+2+x] = abc[x+2]
  end
  puts s
end

letters = ('c'..'z').to_a.join('')

n, k = gets.chomp.split.map(&:to_i)
if k > n || k == 1
  s = -1
else
  p = n - k + 2
  s = 'ab' * (p / 2)
  s += 'a' if p.odd?
  s += letters[0..(n - p - 1)] if n - p > 0
end
puts s

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

if k <= n && (k > 1 || k == n )
  chars = []
  c = 0
  (n - (k-2)).times do
    chars << c
    c = (c + 1) % 2
  end

  c = 2
  (k-2).times do
    chars << c
    c += 1
  end

  puts chars.map{|x| ('a'.ord + x).chr}.join
else
  puts -1
end


END { load __FILE__ unless $stdin.eof? }

t = gets.to_i

t.times do
	n = gets.to_i
	a = gets.split.map!(&:to_i)

	a_max = a.max
	puts a_max + (a.sum - a_max).fdiv(n - 1)
end

testcases = gets.to_i
testcases.times do
  n = gets.to_i
  arr = gets.split(" ").map!(&:to_f)
  half = (arr.sum - arr.max)/(n-1)
  res = arr.max + half
  puts res.to_f
  testcases -= 1
end

t = gets.chomp.to_i
t.times do
    n = gets.chomp.to_i
    a = gets.chomp.split(" ").map(&:to_i).sort
    sum = 0
    a.each do |i|
        sum += i
    end
    mae = 0
    ushiro = 0
    cnt = 0
    max = -Float::INFINITY
    a.each do |j|
        mae += j
        cnt += 1
        ushiro = sum - mae
        ans = (mae/cnt.to_f) + (ushiro/(n-cnt).to_f) if cnt != n
        max = [max,ans].max if cnt != n
        # puts "~~~~~"
        # p ans
        # puts "==="
    end
    puts max
end

#!/usr/bin/env ruby

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

tc = arline[0]
tc.times do
    n = arline[0]
    ar = arline.sort
    puts (ar.pop + ar.sum.to_f/ar.length)
end

t = gets.chomp.to_i
t.times do
    n = gets.chomp.to_i
    a = gets.chomp.split(" ").map(&:to_i)
    sum = 0
    a.each do |i|
        sum += i
    end
    mae = 0
    ushiro = 0
    cnt = 0
    max = -Float::INFINITY
    a.each do |j|
        mae += j
        cnt += 1
        ushiro = sum - mae
        ans = (mae/cnt.to_f) + (ushiro/(n-cnt).to_f) if cnt != n
        max = [max,ans].max if cnt != n
    end
    puts max
end


gets.to_i.times do
  n, k = gets.split.map(&:to_i)
  k -= 1
  puts (k + (n % 2) * (k / (n / 2))) % n + 1
end

gets.to_i.times do
  n,k=gets.split.map(&:to_i)
  k -= 1
  p (k+n%2*k/(n/2))%n+1
end

gets.to_i.times do
  n,k=gets.split.map(&:to_i)
  if n.even?
    puts (k-1)%n+1
  else
    k%=n/2*n
    puts (k+(k-1)/(n/2)-1)%n+1
  end
end


gets
p gets(p).split.select{|_|(%w(ABSINTH BEER BRANDY CHAMPAGNE GIN RUM SAKE TEQUILA VODKA WHISKEY WINE)+[*?0.."17"]).include? _}.size

a,ctr=[],0
al=["ABSINTH", "BEER", "BRANDY", "CHAMPAGNE", "GIN", "RUM", "SAKE", "TEQUILA", "VODKA", "WHISKEY", "WINE"]
n=gets.to_i
n.times do
	x=gets.chomp
	ctr+=1 if al.include?(x) || (x =~ /[0-9]+/ && x.to_i < 18)
end
puts ctr

n = gets.strip.to_i
drinks = [ "ABSINTH", "BEER", "BRANDY", "CHAMPAGNE", "GIN", "RUM", "SAKE", "TEQUILA", "VODKA", "WHISKEY", "WINE" ]
ans = 0
n.times do
    i = gets.strip
    if i =~ /[0-9]/
        age = i.to_i
        ans += 1 if age < 18
    else
        ans += 1 if drinks.include? i
    end
end
puts ans

#=================================
require 'prime'
include Math

if ARGV.include?("ONLINE_JUDGE")
	$stdin = File.open("./input.txt", "r")
	ARGV.delete("ONLINE_JUDGE")
end
#=================================

n=gets.to_i
dr=[:ABSINTH, :BEER, :BRANDY, :CHAMPAGNE, :GIN, :RUM, :SAKE, :TEQUILA, :VODKA, :WHISKEY, :WINE].map{|v| v.to_s}

0.upto(17){|age|
	dr << age.to_s
}

count=0
n.times{
	line=gets.strip

	if dr.include?(line)
		count+=1
	end
}

puts count

judge_arr = %w(ABSINTH BEER BRANDY CHAMPAGNE GIN RUM SAKE TEQUILA VODKA WHISKEY WINE)
s=gets
vcount=0
s.to_i.times {
  a=gets
  a.chop!
  if(a.match(/\d+/)==nil) then
    judge_arr.include?(a)?vcount+=1:vcount
  else
    a.to_i<18?vcount+=1:vcount
  end
  }
  puts vcount

drinks = ["ABSINTH","BEER","BRANDY","CHAMPAGNE","GIN","RUM","SAKE","TEQUILA", "VODKA","WHISKEY", "WINE"]
n = gets.to_i
count = 0
n.times do |i|
	line = gets.chomp.upcase

	age = line.to_i unless line =~ /[A-Z]/
	# puts line
	count += 1 if ((age && age < 18) || drinks.include?(line) )
	# puts "count #{count}"
end
puts count

def toi(n)
  if n.to_i.to_s != n
    10000
  else
    n.to_i
  end
end

def check(str)
  alchohol = ["ABSINTH", "BEER", "BRANDY", "CHAMPAGNE", "GIN", "RUM", "SAKE", "TEQUILA", "VODKA", "WHISKEY", "WINE"]
  is_al = alchohol.find { |drink| str == drink }
  return true if (is_al)
  return toi(str) < 18
end

n = gets.chomp.to_i
s = 0
n.times do
  str = gets.chomp
  if check str
    s += 1
  end
end

puts s

n = gets.to_i

count = 0
mas = ",ABSINTH,BEER,BRANDY,CHAMPAGNE,GIN,RUM,SAKE,TEQUILA,VODKA,WHISKEY,WINE,"
for i in 0..n-1
  s = gets.chomp!
  if ((s =~ /[0-9]+/ && s.to_i < 18) || (mas.index(","+s+",") != nil))
    count += 1
  end
end

puts count

alcohol = ["ABSINTH", "BEER", "BRANDY", "CHAMPAGNE", "GIN", "RUM",
           "SAKE", "TEQUILA", "VODKA", "WHISKEY", "WINE"]

num = gets.chomp.to_i

count = 0

num.times do |i|
  str = gets.chomp
  if str =~ /\d+/
    if str.to_i < 18
      count = count + 1
    end
  else
    if alcohol.include? str
      count = count + 1
    end
  end
end

puts count

n = gets.chomp.to_i
guys = n.times.map { gets.chomp }

alc = %w(ABSINTH BEER BRANDY CHAMPAGNE GIN RUM SAKE TEQUILA VODKA WHISKEY WINE)

cnt = 0
guys.each do |g|
  cnt += 1 if (g =~ /(\d+)/ && $1.to_i < 18) || alc.include?(g)
end

puts cnt

a,ctr=[],0
al=["ABSINTH", "BEER", "BRANDY", "CHAMPAGNE", "GIN", "RUM", "SAKE", "TEQUILA", "VODKA", "WHISKEY", "WINE"]
n=gets.to_i
n.times do
	x=gets.chomp
	ctr+=1 if al.include?(x) || (x.to_i!=0 && x.to_i<18)
end
puts ctr

#=================================
require 'prime'
include Math

if ARGV.include?("ONLINE_JUDGE")
	$stdin = File.open("./input.txt", "r")
	ARGV.delete("ONLINE_JUDGE")
end
#=================================

n=gets.to_i
dr=[:ABSINTH, :BEER, :BRANDY, :CHAMPAGNE, :GIN, :RUM, :SAKE, :TEQUILA, :VODKA, :WHISKEY, :WINE].map{|v| v.to_s}

count=0
n.times{
	line=gets.strip

	age=line.to_i
	age=19 if age==0

	if dr.include?(line) || age<18
		puts "line=#{line} age=#{age}"
		count+=1
	end
}

puts count

#=================================
require 'prime'
include Math

if ARGV.include?("ONLINE_JUDGE")
	$stdin = File.open("./input.txt", "r")
	ARGV.delete("ONLINE_JUDGE")
end
#=================================

n=gets.to_i
dr=[:ABSINTH, :BEER, :BRANDY, :CHAMPAGNE, :GIN, :RUM, :SAKE, :TEQUILA, :VODKA, :WHISKEY, :WINE].map{|v| v.to_s}

count=0
n.times{
	line=gets.strip

	age=line.to_i
	age=19 if age==0

	if dr.include?(line) || age<18
		count+=1
	end
}

puts count

#=================================
require 'prime'
include Math

if ARGV.include?("ONLINE_JUDGE")
	$stdin = File.open("./input.txt", "r")
	ARGV.delete("ONLINE_JUDGE")
end
#=================================

n=gets.to_i
dr=[:ABSINTH, :BEER, :BRANDY, :CHAMPAGNE, :GIN, :RUM, :SAKE, :TEQUILA, :VODKA, :WHISKEY, :WINE].map{|v| v.to_s}

0.upto(17){|age|
	puts age
	dr << age.to_s
}

count=0
n.times{
	line=gets.strip

	if dr.include?(line)
		count+=1
	end
}

puts count

#=================================
require 'prime'
include Math

if ARGV.include?("ONLINE_JUDGE")
	$stdin = File.open("./input.txt", "r")
	ARGV.delete("ONLINE_JUDGE")
end
#=================================

n=gets.to_i
dr=[:ABSINTH, :BEER, :BRANDY, :CHAMPAGNE, :GIN, :RUM, :SAKE, :TEQUILA, :VODKA, :WHISKEY, :WINE].map{|v| v.to_s}

1.upto(17){|age|
	dr << age.to_s
}

count=0
n.times{
	line=gets.strip

	if dr.include?(line)
		count+=1
	end
}

puts count

drinks = ["ABSINTH","BEER","BRANDY","CHAMPAGNE","GIN","RUM","SAKE","TEQUILA", "VODKA","WHISKEY", "WINE"]
n = gets.to_i
count = 0
n.times do |i|
	line = gets.chomp
	age = line.to_i
	# puts line
	count += 1 if ((age > 0 && age < 18) || (age == 0 && drinks.include?(line)) )
	# puts "count #{count}"
end
puts count

n = gets.to_i

count = 0
mas = "ABSINTH,BEER,BRANDY,CHAMPAGNE,GIN,RUM,SAKE,TEQUILA,VODKA,WHISKEY,WINE"
for i in 0..n-1
  s = gets.chomp!
  if ((s =~ /[0-9]+/ && s.to_i < 18) || (mas.index(s) != nil))
    count += 1
  end
end

puts count