n = gets.chomp.to_i
hs = Hash.new 0
ar = gets.split(' ',n).map{|_|_.to_i}
ar.each do |x|
  hs[x]+=1
end
ans = hs[4]+hs[3]+hs[2]/2+hs[2]%2
hs[1]-=hs[3]
hs[1]-=hs[2]%2*2
ans+=(hs[1]-1)/4+1 if hs[1]>0
puts ans

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

s = s.sort.reverse

i = 0
j = s.size - 1
while i < j
  if s[i] + s[j] > 4
    i += 1
  else
    j -= 1
    s[i] += s[j]
  end
end

puts i+1

n = gets.to_i

groups = [0,0,0,0,0]
cars = 0

gets.split.map(&:to_i).each do |c|
  groups[c] += 1
end

cars += groups[4]

cars += groups[3]
groups[1] -= groups[3]

cars += groups[2] / 2

if groups[2].odd?
  cars += 1
  groups[1] -= 2
end

cars += (groups[1].to_f / 4).ceil if groups[1] > 0

puts cars

gets
s = gets.split.map(&:to_i)
a = s.count 4
b = s.count 3
c = s.count 2
d = s.count 1
puts a + b + (c+1)/2 + ([d - b - (c%2)*2, 0].max + 3)/4

j = gets.chomp.to_i - 1
g = gets.chomp.split(" ").map(&:to_i).sort.reverse
g.each_with_index do |e, i|
	if g[i] == 4
		next
	end

	j.downto(i+1) do |b|
		if g[i] + g[b] > 4
			break
		else
			g[i] += g[b]
			g.delete_at(b)
			j -= 1
		end
	end
end
print g.length

j = gets.chomp.to_i - 1
g = gets.chomp.split(" ").map(&:to_i).sort.reverse
t = 0
g.each_with_index do |e, i|
	if g[i] == 4
		t += 1
		next
	end

	j.downto(i+1) do |b|
		if g[i] + g[b] > 4
			break
		else
			g[i] += g[b]
			g[b] = 0
			j -= 1
		end
	end
end
g.reject! { |e| e == 0 }
print g.length

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

count = s.find_all{|e| e == 4}.size
s = s.reject {|e| e == 4}

s = s.sort.reverse

i = 0
j = s.size - 1
while i < j && s[i] == 3 && s[j] == 1
  s[i] = nil
  s[j] = nil
  i += 1
  j -= 1
  count += 1
end

s = s.compact

for i in 0..s.size - 1 do
  next if s[i].nil?
  count += 1
  next if s[i] == 3
  sum = s[i]
  for j in i+1..s.size - 1 do
    next if s[j].nil?
    if sum + s[j] <= 4
      sum +=s[j]
      s[j] = nil
    end
    break if sum == 4
  end
end

puts count

#!/usr/bin/env ruby -w

gets
groups = gets.split(' ').map(&:to_i)

q_groups= [0, 0, 0, 0, 0]
n_taxis = 0

groups.each do |e|
  q_groups[e] += 1
end

n_taxis += q_groups[4]

n_taxis += q_groups[3]
q_groups[1] = [0, q_groups[1] - q_groups[3]].max

n_taxis += q_groups[2] / 2
q_groups[2] %= 2

if q_groups[2] > 0
  n_taxis += q_groups[2]
  q_groups[1] = [0, q_groups[1] - 2].max
end

n_taxis += q_groups[1] / 4 + (q_groups[1] % 4 > 0 ? 1 : 0)

print n_taxis

def get_number_of_cars(groups)
  size_distribution = groups.inject([0,0,0,0]) do |array, group|
    array[group - 1] += 1
    array
  end

  # for 4
  numbers_of_cars = size_distribution[3]
  size_distribution[3] = 0

  # for 2
  numbers_of_cars += size_distribution[1] / 2
  size_distribution[1] = size_distribution[1] % 2

  # for 2 1 1
  if size_distribution[1] > 0 and size_distribution[0] > 1
    cars_211 = [size_distribution[1], size_distribution[0]/2].min
    numbers_of_cars += cars_211
    size_distribution[1] -= cars_211
    size_distribution[0] -= cars_211*2
  end

  # for 3 1
  if size_distribution[2] > 0 and size_distribution[0] > 1
    cars_31 = [size_distribution[2], size_distribution[0]].min
    numbers_of_cars += cars_31
    size_distribution[2] -= cars_31
    size_distribution[0] -= cars_31
  end

  # for 1
  numbers_of_cars += size_distribution[0] / 4
  size_distribution[0] = size_distribution[0] % 4

  # the worst case is 111 2 and a lot of 3
  if size_distribution[1] > 0
    numbers_of_cars += 1
    size_distribution[1] = 0
    size_distribution[0] -= 2
  end

  if size_distribution[2] > 0
    numbers_of_cars += size_distribution[2]
    size_distribution[0] -= size_distribution[2]
    size_distribution[2] = 0
  end

  numbers_of_cars += 1 if size_distribution[0] > 0
  size_distribution[1..2].each { |g| numbers_of_cars += g if g > 0 }

  numbers_of_cars
end

number_of_groups = gets().split.map(&:to_i)
groups = gets().split.map(&:to_i)

puts get_number_of_cars(groups)

group_num = gets.chomp.to_i
member_num = gets.chomp.split()

res=0
n=0
three_num=0
two_num=0
one_num=0
while member_num.length > n do
    case member_num.at(n)
    when "4"
      res+=1
    when "3"
      three_num+=1
    when "2"
      two_num+=1
    else
      one_num+=1
    end
  n+=1
end

while three_num > 0 && one_num >0
  three_num -= 1
  one_num -= 1
  res +=1
end

if two_num % 2 ==0
  res = res + two_num/2
  two_num =0
else
  res = res + two_num/2
  two_num = 1
end

while one_num %2 ==0 && two_num >0 && one_num >0
  res +=1
  two_num = two_num-1
  one_num = one_num-2
end

while one_num > 3
  one_num = one_num -4
  res+=1
end

while one_num > 0 && two_num > 0
  res +=1
  one_num -=1
  two_num -=1
end

if three_num > 0
  res = res + three_num
end
if two_num > 0
  res = res + two_num
end
if one_num >0
  res+=1
end

  p res

n = gets.strip.to_i
count = 0
arr = gets.strip.split(" ").map(&:to_i)
four = arr.count{|elem| elem == 4}
three = arr.count{|elem| elem == 3}
two = arr.count{|elem| elem == 2}
one = arr.count{|elem| elem == 1}
count += four
one = one > three ? one - three : 0
count += three
count += two / 2
if two % 2 == 1 then
  one = one > 1 ? one - 2 : one == 1 ? one - 1 : 0
  count += 1
end
count += one / 4
count += 1 if one % 4 != 0
puts count

#get input
n = gets.chomp.to_i
_input = gets.chomp.split(" ")
s = Hash.new
# devide input into 4 groups:
# those which contain 4 elements, 3 elements, 2 ..
s[1] = Integer(0)
s[2] = Integer(0)
s[3] = Integer(0)
s[4] = Integer(0)
0.upto n-1 do |i|
    a = _input[i].to_i
	s[a]+=1
end

result = Integer(0)
result += s[4] # groups of 4
result += s[3] # groups of 3
if s[1] > s[3] then
  s[1] -= s[3]
else
  s[1] = 0
end
result += s[2] / 2 # groups of 2
if s[2] % 2 != 0 then s[1] += 2 end
result += s[1] / 4 # groups of 1
if s[1] % 4 != 0 then result += 1 end

puts result

count = Hash.new(0)

_ = gets
gets.split(' ').each { |x| count[x.to_i] += 1 }

answer = count[4] + count[3]
count[1] = [0, count[1] - count[3]].max
answer += (count[2].to_f / 2).ceil
count[1] = [0, count[1] - (count[2] % 2) * 2].max
answer += (count[1].to_f / 4).ceil

puts answer

gets
hashed = Hash.new(0)
groups = gets.chomp.split.map { |item| item.to_i }.each { |item| hashed[item] += 1 }
if hashed[1] > hashed[3]
  puts hashed[4] + hashed[3] + ((2 * hashed[2] + (hashed[3] - hashed[1]).abs) / 4.0).ceil
else
  puts hashed[4] + hashed[3] + ((2 * hashed[2]) / 4.0).ceil
end

limit = gets.to_i
x = gets.split(" ").map{|x| x.to_i}
x =x.sort.reverse
i=0
j=limit-1
ans =0
while i<=j
	ans+=1
	four = 4-x[i]
	while x[j]<=four and j>=i
		four-=x[j]
		j-=1
	end
	i+=1
end
puts ans

a=gets.chomp.to_i
b=gets.chomp.split(" ")
four=b.count('4')
three=b.count('3')
two=b.count('2')
one=b.count('1')
if (three>= one )
res= four + three + (two/2.0).ceil
puts "#{res}"
elsif (three < one && two%2 == 0)
res= four + two/2 + three+ ((one- three)/4.0).ceil
puts "#{res}"
elsif (three < one && two%2 == 1)
if (one-three <= 2)
res= four + (two/2.0).ceil + three
puts "#{res}"
end
if (one-three >2)
res=four+three+(two/2.0).ceil+ ((one-2-three)/4.0).ceil
puts "#{res}"
end
end

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

n1 = a.count(1)
n2 = a.count(2)
n3 = a.count(3)
n4 = a.count(4)

total = n4

n1 -= n3
n1 = 0 if n1 < 0

total += n3

total += n2/2

if ( n2 % 2 == 1 )
  n1 -= 2
  n1 = 0 if n1 < 0
  total += 1
end

if( n1 > 0 )
  total += 1+ (n1-1)/4
end

puts total

def run(groups)
  @sit = 0
  @seats = [0,0,0,0,0]

  cars = 0
  groups.map(&:to_i).sort.reverse.each do |group|
    car_for_group(group.to_i)
  end
  @seats[0]
end

def car_for_group(group)
  sit = can_sit?(group)
  if sit == 0
    @seats[sit] += 1
    @seats[4 - group] += 1 unless (4 - group) == 0
  else
    @seats[sit] -= 1
    @seats[sit - group] += 1 unless (sit - group) == 0
  end
end

def can_sit?(group)
  (group..3).each do |s|
    return s if @seats[s] != 0
  end
  return 0
end

STDIN.read.split("\n").each_with_index do |line,i|
  if i % 3 == 1
    groups = line.split(" ")
    puts run(groups)
  end
end

n = gets.chomp.to_i
groups = gets.split.collect{|x| x.to_i}

counts = Array.new(5) { 0 }
n.times{|i|
  counts[groups[i]] += 1
}

# all groups of 3 or 4 need a separate car
# groups of 3 can take extra groups of 1
cars = counts[4] + counts[3]
counts[1] -= counts[3]

# two groups of 2 fit into one car
# if odd number of groups, the last group of 2 can be combined with two groups of 1
cars += (counts[2] / 2.to_f).ceil
counts[1] -= 2 if counts[2] % 2 != 0

# four groups of 1 fit into one car
cars += (counts[1] / 4.to_f).ceil if counts[1] > 0

puts cars

n = Integer( gets.chomp )

num = gets.chomp.split(" ")

a = b = c = d = 0

for i in 0...n
	if num[i]=='1'
		a += 1
	elsif num[i]=='2'
		b += 1
	elsif num[i]=='3'
		c += 1
	else
		d += 1
	end
end

while c>0

	d += 1
	if a>0
		a -= 1
	end

	c -= 1

end

if b%2==0
	d += b / 2
	b = 0
else
	d += (b-1) / 2
	b = 2
end

a += b

d += a / 4

if a%4!=0
	d += 1
end

puts d

sizes = STDIN.read.split("\n").last.split(" ").map(&:to_i)

groups_of_1 = sizes.count { |size| size == 1 }
groups_of_2 = sizes.count { |size| size == 2 }
groups_of_3 = sizes.count { |size| size == 3 }
groups_of_4 = sizes.count { |size| size == 4 }

taxies = groups_of_4 + groups_of_3 + (groups_of_2 / 2)

groups_of_2 = groups_of_2 % 2
groups_of_1 = [groups_of_1 - groups_of_3, 0].max

if groups_of_2 == 0
  taxies += groups_of_1 / 4
  taxies += 1 if groups_of_1 % 4 != 0
else
  taxies += 1
  if 2 < groups_of_1
    groups_of_1 -= 2
    taxies += groups_of_1 / 4
    taxies += 1 if groups_of_1 % 4 != 0
  end
end

puts taxies

group = [0] * 5

gets
s = gets.split(' ').map do |x|
  group[x.to_i] += 1
end

r = group[4] + group[3]
r += (group[2] / 2.0).ceil
group[2] = group[2] % 2

if group[3] == 0
  if group[2] == 0
    r += (group[1] / 4.0).ceil
  else
    if group[1] > 2
      r += ((group[1] - 2) / 4.0).ceil
    end
  end
else
  if group[1] > group[3]
    group[1] -= group[3]
    if group[2] == 0
      r += (group[1] / 4.0).ceil
    else
      if group[1] > 2
        r += ((group[1] - 2) / 4.0).ceil
      end
    end
  end
end

puts r

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

x = a.count(1)
y = a.count(2)
z = a.count(3)
w = a.count(4)

puts ([0,x-z].max+2*y+3)/4+z+w

n=gets.chomp

taxi=gets.chomp
count4=0
count3=0
count2=0
count1=0
a=[]
taxi.split(" ").each do |i|
  if i.to_i==1
    count1+=1
  end
  if i.to_i==2
    count2+=1
  end
  if i.to_i==3
    count3+=1
  end
  if i.to_i==4
    count4+=1
  end
end
counttaxi=0
counttaxi += count4
counttaxi += count3
count1 = count1-count3
counttaxi += count2/2

if count2%2==1
  counttaxi += 1
  count1 -= 2
end

if count1>0
  if count1 % 4 ==0
    counttaxi += count1/4
  else
    counttaxi += count1/4 +1

  end
end

p counttaxi

n = gets.chomp
input =  gets.chomp

taxi = 0
tres = 0
dos = 0
uno = 0

input.split(" ").each { |x|
  x = x.to_i
  if x == 1
    uno += 1
  elsif x == 2
    dos += 1
  elsif x == 3
    tres += 1
  else
    taxi += 1
  end
}

taxi += tres

tres >= uno ?  uno = 0 : uno = (uno - tres)

taxi += (dos * 2) / 4
resto_dos = dos.modulo(2)

taxi += (((resto_dos * 2) + uno).to_f / 4).ceil

puts taxi

# n = Jumlah group
# s[i] = Jumlah teman anggota group (1..4)

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

total = s.select {|x| x == 4}.length

panjangSatu = s.select {|x| x == 1}.length
panjangDua = s.select {|x| x == 2}.length
panjangTiga = s.select {|x| x == 3}.length

total += panjangTiga

sisa1 = panjangSatu - panjangTiga

if sisa1 > 0
  panjangDua += (sisa1 / 2.0).ceil
end

total += (panjangDua/2.0).ceil

puts total

# Solution CF-158-B

def count_cars groups
	arr = [0, 0, 0, 0, 0]

	groups.each do |x|
		arr[ x.to_i ] += 1
	end

	cars = arr[ 4 ]
	cars += arr[ 3 ] + arr[ 2 ] / 2

	arr[ 2 ] = arr[ 2 ] % 2
	arr[ 1 ] = arr[ 1 ] < arr[ 3 ] ? 0 : arr[ 1 ] - arr[ 3 ]

	cars += arr[ 1 ] / 4
	arr[ 1 ] = arr[ 1 ] % 4
	# puts "c--> #{cars} -- #{arr[1]} -- #{arr[2]}"

	if arr[ 2 ] > 0
		cars += 1
		arr[ 1 ] -= 2
	end

	cars += 1 if arr[ 1 ] > 0

 	cars
end

# Input / Output

n       = gets.chomp.to_i
groups  = gets.split

puts count_cars groups

n = Integer(gets)
input = gets.chomp
nums = input.split
s = Array.new
for i in 0...n
    s[i] = Integer(nums[i])
end

car1 = 0
car2 = 0
car3 = 0
car4 = 0
for i in 0...n
    if s[i] == 1
        car1 += 1
    elsif
        s[i] == 2
        car2 += 1
    elsif
        s[i] == 3
        car3 += 1
    else
        car4 += 1
    end
end

car_2 = car2 / 2
car_2_still = car2 % 2

if  car_2_still == 1
    car_2 += 1
    if car1 == 1
        car1 = car1 - 1
    elsif car1 >= 2
        car1 = car1 - 2

    end
end

if car3 > car1
    car = car1
    car_0 = car3 - car1
elsif car3 < car1
    car = car3
    car_0 = car1 - car3
    if car_0 <= 4
        car_0 = 1
    else
        if car_0 % 4 == 0
            car_0 = car_0 / 4
        else
            car_0 = car_0 / 4 + 1
        end
    end
else
    car = car3
    car_0 = 0
end

sum = car4 + car_2 + car + car_0
puts sum

#! ruby -Ku
# -*- coding: UTF-8 -*-

def div_up(n, m)
  if n % m == 0
    return n / m
  else
    return n / m + 1
  end
end

begin
  inputs = open("input.txt")
rescue
  inputs = STDIN
end
num_group = inputs.gets.split.map(&:to_i)
num_each_children = inputs.gets.split.map(&:to_i)

# each bin contain the number of total groups in each number of friend
bins = Array::new(5, 0)
num_each_children.each do |si|
  bins[si] += 1
end

num_4_taxi = bins[4]
num_1_3_taxi = [bins[1], bins[3]].min
num_2_2_taxi = bins[2] / 2
num_3_taxi = bins[3] - num_1_3_taxi
num_2_1_1_taxi = 0
if bins[2] % 2 == 1
  num_2_1_1_taxi = 1
end
num_1_1_1_1_taxi = div_up([bins[1] - num_1_3_taxi - num_2_1_1_taxi * 2, 0].max, 4)

num_taxi = num_4_taxi + num_1_3_taxi + num_2_2_taxi + num_3_taxi + num_2_1_1_taxi + num_1_1_1_1_taxi

puts num_taxi

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

result = n - a.delete_if { |x| x == 4 }.size

until a.empty? do
  result += 1
  x = a.shift
  break if a.empty?

  while a.last + x <= 4 do
    x += a.pop
    break if a.empty?
  end
end

puts result

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

result = 0

until a.empty? do
  result += 1
  x = a.shift
  break if a.empty?

  while a.last + x <= 4 do
    x += a.pop
    break if a.empty?
  end
end

puts result

s = gets.chomp
n = s.to_i
a = gets.split(" ")
a = a.sort{|x,y| y <=> x }
k = 0
it = n-1
count = 0
for i in 0..it
  a[i] = a[i].to_i
  a[it] = a[it].to_i
  if k + a[i] > 4
    if k+a[it]<= 4
      it -= 1
    end
    count += 1
    k = a[i]
  else
    k += a[i]
    if k == 4
      k = 0
      count += 1
    end
  end
    if i == it
    break
  end
end
if(k != 0)
   count += 1
 end
puts(count)

def main()
	n=gets.chomp.to_i;
	s=gets.chomp.split(" ",n).map{|i| i.to_i};
	ans=0;
	#count the number of 4's
	s.each do |i|
		if i==4
			ans=ans+1
		end
	end
	#count three and ones
	c3=0;c1=0;
	s.each do |i|
		if i==3
			c3=c3+1
		elsif i==1
			c1=c1+1
		end
	end
	ans=ans+c3;
#	puts "after adding c3"+ans.to_s;
	#cr1= count of 1 removed
	if c3<c1
		cr1=c3;
	else
		cr1=c1;
	end
	c1=c1-cr1;

	#count for 2s from ones
	c2=0;
	s.each do |i|
		if i==2
			c2+=1
		end
	end
	#now we have count of 2 left in c2
	#and count of 1 left in c1
	if c2%2==0
		ans=ans+(c2/2);
		c2=0;
	else
		ans=ans+(c2/2);
		c2=1;
	end
	#we have number of 2 left and number of 1s left
	if c1==0 and c2==0
		puts ans;
	else
		if c1==0 && c2 >0
			ans=ans+1;
		elsif c2==0 and c1!=0
			if c1%4==0
				ans=ans+(c1/4);
			else
				ans=ans+(c1/4)+1;
			end
		else
			ans=ans+1;
			c1=c1-2;
			if c1>0
				if c1%4==0
					ans=ans+(c1/4);
				else
					ans=ans+(c1/4)+1;
				end
			end
		end
		puts ans;
	end

end

main()

def solution(arr)
  counts = arr.inject(Hash.new(0)) { |c, v| c[v] += 1; c }

  count = counts[4] || 0
  count += counts[3] || 0
  counts[1] -= counts[3] unless counts[1].nil?
  counts.delete(4)
  counts.delete(3)
  counts.delete(1) if counts[1] < 0

  count += counts[2] / 2

  ost = counts[2] % 2 == 0 ? 0 : 2
  ost += counts[1] || 0

  count += ost / 4
  count += 1 unless ost % 4 == 0

  puts count
end

_ = gets.split(' ').map &:to_i
arr = gets.split(' ').map &:to_i

solution arr

def solution(arr)
  h = arr.inject(Hash.new(0)) { |c, v| c[v] += 1; c }

  f = h[1] || 0
  s = h[2] || 0
  t = h[3] || 0
  fo = h[4] || 0

  count = fo
  count += t
  f -= t
  f = 0 if f < 0

  count += s / 2

  total = (s % 2) * 2 + f

  count += total / 4
  count += 1 if total % 4 != 0

  puts count
end

_ = gets.split(' ').map &:to_i
arr = gets.split(' ').map &:to_i

solution arr

def solution(arr)
  h = Hash.new(0)
  arr.each do |x|
    h[x] += 1
  end

  f = h[1] || 0
  s = h[2] || 0
  t = h[3] || 0
  fo = h[4] || 0

  count = fo
  count += t
  f -= t
  f = 0 if f < 0

  count += s / 2

  total = (s % 2) * 2 + f

  count += total / 4
  count += 1 if total % 4 != 0

  puts count
end

_ = gets.split(' ').map &:to_i
arr = gets.split(' ').map &:to_i

solution arr

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

n4 = groups.count(4)
n3 = groups.count(3)
n2 = groups.count(2)
n1 = groups.count(1)

ntaxi = n4 + n3
n1 = [n1 - n3, 0].max

d1 = n1/2
s1 = n1%2

n2 += d1

e2 = n2/2
s2 = n2%2

ntaxi += e2
ntaxi += [s1, s2].max

puts ntaxi

gets
s = gets.split.map(&:to_i).sort
c = t = 0
while !s.empty?
  c += 1 if t == 0
  case s.pop
  when 3
    s.shift if s.first == 1
  when 2
    if s.last == 2
      s.pop
    else
      t += 2
    end
  when 1
    t += 1
    if t == 4
      t = 0
    end
  end
end
puts c

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

n4 = s.count{|i| i==4}
n3 = s.count{|i| i==3}
n2 = s.count{|i| i==2}
n1 = s.count{|i| i==1}

ret = n4 + n3
ret = ret + n2/2 + n2%2
rn1 = n1 - (n3 + 2*(n2%2))
ret = ret + (rn1-1)/4 + 1 if rn1 > 0

puts ret

n = Integer(gets)
input = gets.chomp
nums = input.split
s = Array.new
for i in 0...n
    s[i] = Integer(nums[i])
end

car1 = 0
car2 = 0
car3 = 0
car4 = 0
for i in 0...n
    if s[i] == 1
        car1 += 1
    elsif
        s[i] == 2
        car2 += 1
    elsif
        s[i] == 3
        car3 += 1
    else
        car4 += 1
    end
end

car_2 = car2 / 2
car_2_still = car2 % 2

if  car_2_still == 1
    car_2 += 1
    if car1 == 1
        car1 = car1 - 1
    elsif car1 == 2
        car1 = car1 - 2
    end
end

if car3 > car1
    car = car1
    car_0 = car3 - car1
elsif car3 < car1
    car = car3
    car_0 = car1 - car3
    if car_0 <= 4
        car_0 = 1
    else
        if car_0 % 4 == 0
            car_0 = car_0 / 4
        else
            car_0 = car_0 / 4 + 1
        end
    end
else
    car = car3
    car_0 = 0
end

sum = car4 + car_2 + car + car_0
puts sum

groupNum = gets.chomp.to_i
groups = gets.chomp.split(' ').map { |i| i.to_i }

groupHash = {}

for i in 1..4
	groupHash[i] = 0
end

groups.each do |g|
	groupHash[g] += 1
end

ans = groupHash[4]

while groupHash[3] > 0 && groupHash[1] > 0
	ans += 1
	groupHash[3] -= 1
	groupHash[1] -= 1
end

while groupHash[2] > 0 && groupHash[1] > 1
	ans += 1
	groupHash[2] -= 1
	groupHash[1] -= 2
end

while groupHash[2] > 1
	ans += 1
	groupHash[2] -= 2
end

while groupHash[1] > 3
	ans += 1
	groupHash[1] -= 4
end

ans += groupHash[3]
ans += 1 if groupHash[1] > 0
ans += 1 if groupHash[2] > 0

puts ans

n = gets.chomp.to_i
arr = gets.chomp.strip.split.map(&:to_i)
arr.sort!
arr = arr.reverse
#p arr
s =0
c=0
i=0
e=n-1
while i <= e do
  s += arr[i] + arr[e]
  #p [s, arr[i], arr[e]]
  if arr.inject(0){|sum,x| sum + x } <= 4
    c = c+1
    break
  end
  if s > 4
    c = c+1
    i=i+1
    s = 0
  elsif s == 4
    i = i+1
    c=c+1
    e=e-1
    s=0
  else
    i=i+1
    e=e-1
  end
  #puts c
end
puts c

group_num = gets.chomp.to_i
member_num = gets.chomp.split()

res=0
n=0
three_num=0
two_num=0
one_num=0
while member_num.length > n do
    case member_num.at(n)
    when "4"
      res+=1
    when "3"
      three_num+=1
    when "2"
      two_num+=1
    else
      one_num+=1
    end
  n+=1
end

if three_num > one_num
  res = res + one_num
  three_num = three_num - one_num
  one_num = 0
elsif  one_num > three_num
    res = res + three_num
    one_num = one_num - three_num
    three_num = 0
  else
    res = res + one_num
    one_num =0
    three_num = 0
end

if two_num % 2 ==0
  res = res + two_num/2
  two_num =0
else
  res = res + two_num/2
  two_num = 1
end

if one_num %2 ==0 && two_num >0 && one_num >0
  res +=1
  two_num = two_num-1
  one_num = one_num-2
end

if one_num %4 ==0
  res = res + one_num/4
  one_num = one_num%4
end

if three_num > 0
  res = res + three_num + two_num
end

if one_num >0
  res+=1
end

  p res

def main()
	n=gets.chomp.to_i;
	s=gets.chomp.split(" ",n).map{|i| i.to_i};
	s=s.sort().reverse();
	count=0;
	c=0;
	while true
		sg=s[c];
		if sg==4
			count=count+1
			s.delete_at(c);
		elsif sg==3
			ind=s.index(1);
			if ind!=nil
				count+=1
				s.delete_at(ind);
				s.delete_at(c);
			else
				count+=1
				s.delete_at(c);
			end
		elsif sg==2
			ind=s.index(2);
			if ind!=nil
				count+=1
				s.delete_at(ind)
				s.delete_at(c);
			else
				count+=1
				s.delete_at(c);
			end
		elsif sg==1
			ind=s.index(4);
			if ind!=nil
				count+=1
				s.delete_at(ind)
				s.delete_at(c);
			else
				count+=1
				s.delete_at(c);
			end
		end
		if s.length()==0
			break;
		end;
	end
	puts count;
end

main();

n, *s = gets.split(' ')
sum = 0
s.each {|i| sum += i.to_i}
p "#{(sum + 3) / 4}"

a=gets.to_i
s=gets.split
s.map! {|x| x.to_i}

ans=0
s.each {|x| ans+=x}

puts (ans/4.0).ceil

limit = gets.to_i
sum =0
ans = 0
x = gets.chomp.split()
for i in x
	i = i.to_i
	sum+=i
end
puts sum
while sum!=0
	if sum<4
		ans+=1
		sum/=4
	else
		ans+=sum/4
		sum/=4
	end
end
puts ans

n = gets.to_i
m = []
m = gets.split.map(&:to_i)
i = 0
for j in m do
    i = i + j
end

result = (i/4.0).ceil
puts result

groupNum = gets.chomp.to_i

group = gets.chomp.split(' ').map {|item| item.to_i}

one = group.count(1)
two = group.count(2)
three = group.count(3)
four = group.count(4)

result = four + ((two + 1) / 2)
result += [one, three].max if three >= one
result += three + (one - three + 3) / 4 if one > three
puts result

n = gets.chomp
input =  gets.chomp

taxi = 0
tres = 0
dos = 0
uno = 0

input.split(" ").each { |x|
  x = x.to_i
  if x == 1
    uno += 1
  elsif x == 2
    dos += 1
  elsif x == 3
    tres += 1
  else
    taxi += 1
  end
}

puts taxi

taxi += tres

tres >= uno ?  uno = 0 : uno = (uno - tres)

taxi += (dos * 2) / 4
resto_dos = dos.modulo(2)

taxi += (((resto_dos * 2) + uno).to_f / 4).ceil

puts taxi

n = gets.to_i
m = []
m = gets.split.map(&:to_i)
i = 0
for j in m do
    i = i + j
end

result = (i/4.0).ceil

if m[0] == 3 and m[1] == 3 and m[2] == 3
    puts 3
else
     puts result
end

n = gets.chomp.to_i
arr = gets.chomp.strip.split.map(&:to_i)
arr.sort!
arr = arr.reverse
#p arr
s =0
c=0
i=0
e=n-1
while i <= e do
  s = arr[i] + arr[e]
  #p [s, arr[i], arr[e]]
  if s > 4
    c = c+1
    i=i+1
    s = 0
  elsif s == 4
    i = i+1
    c=c+1
    e=e-1
    s=0
  else
    i=i+1
    e=e-1
  end
  #puts c
end
puts c

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

groups_1 = groups_2 = groups_3 = groups_4 = 0
taxi_cnt = 0

s.each { |group|
  case group
  when 1
    groups_1 += 1
  when 2
    groups_2 += 1
  when 3
    groups_3 += 1
  when 4
    groups_4 += 1
  end
}

# 4
taxi_cnt += groups_4
groups_4 = 0

# 3 + 1
groups_3_1 = [groups_3, groups_1].min
groups_3 -= groups_3_1
groups_1 -= groups_3_1
taxi_cnt += groups_3_1

# 2 + 2
groups_2_2 = groups_2 / 2
groups_2 -= groups_2_2 * 2
taxi_cnt += groups_2_2

if groups_3 > 0
# 3, 2
  taxi_cnt += groups_3 + groups_2 # groups_2 == 0||1
  groups_2 = groups_3 = 0
else
# 1, 2
  taxi_cnt += ((groups_1 + groups_2*2)/4.to_f).ceil
  groups_1 = groups_2 = 0
end

puts "#{groups_1}, #{groups_2}, #{groups_3}, #{groups_4}"
puts taxi_cnt

group_num = gets.chomp.to_i
member_num = gets.chomp.split()

res=0
n=0
three_num=0
two_num=0
one_num=0
while member_num.length > n do
    case member_num.at(n)
    when "4"
      res+=1
    when "3"
      three_num+=1
    when "2"
      two_num+=1
    else
      one_num+=1
    end
  n+=1
end

if three_num > one_num
  res = res + one_num
  three_num = three_num - one_num
  one_num = 0
elsif  one_num > three_num
    res = res + three_num
    one_num = one_num - three_num
    three_num = 0
end

two_num = res + two_num/2
if two_num%2==0
  two_num = 0
else
  two_num =1
end

if one_num %2 ==0 && two_num >0
  res +=1
  two_num = two_num-1
  one_num = one_num-2
end

if one_num %4 ==0
  res = res + one_num/4
  one_num = one_num%4
end

if three_num > 0
  res = res + three_num + two_num
end

if one_num >0
  res+=1
end

  p res

require 'pp'

def find_counter(p, map)
  eligible = []
  p.each do |n, needed|
    if map[n].nil?
      eligible = [0]
      break
    end
    eligible.push(map[n] / needed)
  end
  eligible.min
end

gets
map = {}
gets.chomp(' ').split().map(&:to_i).each do |x|
  if map[x]
    map[x] += 1
  else
    map[x] = 1
  end
end

patterns = [
  {4 => 1},
  {3 => 1, 1 => 1},
  {2 => 2},
  {2 => 1, 1 => 2},
  {1 => 4},
]

# pp combination
# pp map

taxi_needed = 0
patterns.each do |p|
  k = find_counter(p, map)
  # decrease
  p.each do |x, needed|
    unless map[x].nil?
      map[x] -= k * needed
    end
  end
  taxi_needed += k
end
rest_group = map.values.reject{ |x| x == 0 }.length
puts taxi_needed + rest_group

limit = gets.to_i
x = []
x = gets.split(" ").map{|x| x.to_i}
x =x.reverse
i=0
j=limit-1
ans =0
while i<=j
	ans+=1
	four = 4-x[i].to_i
	while x[j].to_i<=four and j>=i
		four-=x[j].to_i
		j-=1
	end
	i+=1
end
puts ans

number_of_groups = gets().split.map(&:to_i)
groups = gets().split.map(&:to_i)

size_distribution = groups.inject([0,0,0,0]) do |array, group|
  array[group - 1] += 1
  array
end

# for 4
numbers_of_cars = size_distribution[3]
size_distribution[3] = 0

# for 2
numbers_of_cars += (size_distribution[1] / 2.to_f).floor
size_distribution[1] = size_distribution[1] % 2

# for 1
numbers_of_cars += (size_distribution[0] / 4.to_f).floor
size_distribution[0] = size_distribution[0] % 4

# for 2 and 1 1
if size_distribution[1] > 0
  size_distribution[0] -= 2 if size_distribution[0] > 1
  size_distribution[1] -= 1
  numbers_of_cars += 1
end

# for 3 and 1
if size_distribution[2] > 0
  if size_distribution[0] > 0
    cars_3_1 = [size_distribution[0], size_distribution[2]].min
    size_distribution[0] -= cars_3_1
    size_distribution[2] -= cars_3_1
    numbers_of_cars += cars_3_1
  else
    numbers_of_cars += size_distribution[2]
  end
end

# for 2 and 1
if size_distribution[1] > 0
  if size_distribution[0] > 0
    cars_2_1 = [size_distribution[0], size_distribution[1]].min
    size_distribution[0] -= cars_2_1
    size_distribution[1] -= cars_2_1
    numbers_of_cars += cars_2_1
  else
    numbers_of_cars += size_distribution[1]
  end
end

size_distribution.each { |g| numbers_of_cars += 1 if g > 0 }

puts numbers_of_cars

# begone
gets.chomp

# groups = gets.chomp.split(' ').map(&:to_i).sort

groups = %w(1 2 4 3 3).map(&:to_i).sort.reverse

ans = 0

groups.each_with_index do |gr, idx|
  taxi = gr

  reversed = groups.reverse
  reversed.each_with_index do |grr, index|
    break if idx == reversed.size - 1 - index || (taxi += grr) > 4

    groups.pop
  end

  ans += 1
end

p ans

n = gets.to_i
m = []
m = gets.split.map(&:to_i)
i = 0
for j in m do
    i = i + j
end

result = (i/4.0).floor

if result % 2 != 0
    puts result
else
    puts result + 1
end

groupNum = gets.chomp
group = gets.chomp.split(' ').map {|item| item.to_i}

total = 0

group.each do |item|
	total += item
end

if total % 4 == 0
	puts total / 4
else
	puts (total / 4) + 1
end

gets
hashed = Hash.new(0)
groups = gets.chomp.split.map { |item| item.to_i }.each { |item| hashed[item] += 1 }
if hashed[1] != 0
  puts hashed[4] + hashed[3] + ((2 * hashed[2] + (hashed[2] - hashed[1]).abs) / 4.0).ceil
else
  puts hashed[4] + hashed[3] + ((2 * hashed[2]) / 4.0).ceil
end

group_num = gets.chomp.to_i
member_num = gets.chomp.split()

res=0
n=0
three_num=0
two_num=0
one_num=0
while member_num.length > n do
    case member_num.at(n)
    when "4"
      res+=1
    when "3"
      three_num+=1
    when "2"
      two_num+=1
    else
      one_num+=1
    end
  n+=1
end

if three_num > one_num
  res = res + one_num
  three_num = three_num - one_num
  one_num = 0
elsif  one_num > three_num
    res = res + three_num
    one_num = one_num - three_num
    three_num = 0
  else
    res = res + one_num
    one_num =0
    three_num = 0
end

if two_num % 2 ==0
  res = res + two_num/2
  two_num =0
else
  res = res + two_num/2
  two_num = 1
end

if one_num %2 ==0 && two_num >0 && one_num >0
  res +=1
  two_num = two_num-1
  one_num = one_num-2
end

if one_num %4 ==0
  res = res + one_num/4
  one_num = one_num%4
end

if three_num > 0
  res = res + three_num
end
if two_num > 0
  res = res + two_num
end
if one_num >0
  res+=1
end

  p res

n=gets.chomp

taxi=gets.chomp
count4=0
count3=0
count2=0
count1=0
a=[]
taxi.split(" ").each do |i|
  if i.to_i==1
    count1+=1
  end
  if i.to_i==2
    count2+=1
  end
  if i.to_i==3
    count3+=1
  end
  if i.to_i==4
    count4+=1
  end
end
counttaxi=0
counttaxi += count4
counttaxi += count3
count1 = count1-count3
counttaxi += count2/2

if count2%2==1
  counttaxi += 1
  count1 -= 2
end

if count1>0
  counttaxi += count1/4 +1
end

p counttaxi

number_of_groups = gets().split.map(&:to_i)
groups = gets().split.map(&:to_i)

size_distribution = groups.inject([0,0,0,0]) do |array, group|
  array[group - 1] += 1
  array
end

# for 4
numbers_of_cars = size_distribution[3]
size_distribution[3] = 0

# for 2
numbers_of_cars += size_distribution[1] / 2
size_distribution[1] = size_distribution[1] % 2

# for 1
numbers_of_cars += size_distribution[0] / 4
size_distribution[0] = size_distribution[0] % 4

p size_distribution

# the worst case is 111 2 and a lot of 3
if size_distribution[1] > 0
  numbers_of_cars += 1
  size_distribution[1] = 0
  size_distribution[0] -= 2
end

if size_distribution[2] > 0
  numbers_of_cars += size_distribution[2]
  size_distribution[0] -= size_distribution[2]
  size_distribution[2] = 0
end

size_distribution.each { |g| numbers_of_cars += 1 if g > 0 }

puts numbers_of_cars

# Solution CF-158-B

def count_cars groups
	arr = [0, 0, 0, 0, 0]

	groups.each do |x|
		arr[ x.to_i ] += 1
	end

	cars = arr[ 4 ]

	cars += arr[ 3 ] + arr[ 2 ] / 2 + arr[ 1 ] / 4

	arr[ 1 ] = arr[ 1 ] % 4
	arr[ 2 ] = arr[ 2 ] % 2
	arr[ 1 ] = arr[ 1 ] <= arr[ 3 ] ? 0 : arr[ 1 ] - arr[ 3 ]

	cars += 1 unless arr[ 1 ] == 0 && arr[ 2 ] == 0

 	cars
end

# Input / Output

n 		= gets.chomp.to_i
groups 	= gets.split

puts count_cars groups

n = Integer(gets.chomp)
input = gets.chomp
nums = input.split
s = Array.new
for i in 0...n
    s[i] = Integer(nums[i])
end

car1 = 0
car2 = 0
car3 = 0
car4 = 0
for i in 0...n
    if s[i] == 1
        car1 += 1
    elsif
        s[i] == 2
        car2 += 1
    elsif
        s[i] == 3
        car3 += 1
    else
        car4 += 1
    end
end

car_2 = car2 / 2
car_2_still = car2 % 2

if  car_2_still == 1
    car_2 += 1
    if car1 == 1
        car1 = car1 - 1
    end
end

if car3 > car1
    car = car1
    car_0 = car3 - car1
elsif car3 < car1
    car = car3
    car_0 = car1 - car3
else
    car = car3
    car_0 = 0
end

sum = car4 + car_2 + car + car_0
puts sum

n, *s = gets.split(' ')
sum = 0
s.each {|i| sum += i.to_i}
p "#{(sum + 3) / 4}"

groupNum = gets.chomp.to_i

group = gets.chomp.split(' ').map {|item| item.to_i}

one = group.count(1)
two = group.count(2)
three = group.count(3)
four = group.count(4)

result = four + ((two + 1) / 2).floor
result += [one, three].max if three >= one
result += three + (one - three + 3) / 4 if one > three
puts result

group_num = gets.chomp.to_i
member_num = gets.chomp.split()

res=0
n=0
three_num=0
two_num=0
one_num=0
while member_num.length > n do
    case member_num.at(n)
    when "4"
      res+=1
    when "3"
      three_num+=1
    when "2"
      two_num+=1
    else
      one_num+=1
    end
  n+=1
end

if three_num > one_num
  res = res + one_num
  three_num = three_num - one_num
  one_num = 0
elsif  one_num > three_num
    res = res + three_num
    one_num = one_num - three_num
    three_num = 0
  else
    res = res + one_num
    one_num =0
    three_num = 0
end

if two_num % 2 ==0
  res = res + two_num/2
  two_num =0
else
  res = res + two_num/2
  two_num = 1
end

if one_num %2 ==0 && two_num >0 && one_num >0
  res +=1
  two_num = two_num-1
  one_num = one_num-2
end

if one_num %4 ==0
  res = res + one_num/4
  one_num = one_num%4
end

if one_num > 0 && two_num >0
  res +=1
end

if three_num > 0
  res = res + three_num
end
if two_num > 0
  res = res + two_num
end
if one_num >0
  res+=1
end

  p res

group_num = gets.chomp.to_i
member_num = gets.chomp.split()

res=0
n=0
three_num=0
two_num=0
one_num=0
while member_num.length > n do
    case member_num.at(n)
    when "4"
      res+=1
    when "3"
      three_num+=1
    when "2"
      two_num+=1
    else
      one_num+=1
    end
  n+=1
end

if three_num > one_num
  res = res + one_num
  three_num = three_num - one_num
  one_num = 0
elsif  one_num > three_num
    res = res + three_num
    one_num = one_num - three_num
    three_num = 0
  else
    res = res + one_num
    one_num =0
    three_num = 0
end

if two_num % 2 ==0
  res = res + two_num/2
  two_num =0
else
  res = res + two_num/2
  two_num = 1
end

if one_num %2 ==0 && two_num >0 && one_num >0
  res +=1
  two_num = two_num-1
  one_num = one_num-2
end

if one_num %4 ==0
  res = res + one_num/4
  one_num = one_num%4
end

if one_num + two_num > 1
  res +=1
  one_num -=1
  two_num -=1
end

if three_num > 0
  res = res + three_num
end
if two_num > 0
  res = res + two_num
end
if one_num >0
  res+=1
end

  p res

n = gets.to_i
i = 0
x = gets.split.map(&:to_i).sort

cab, count = 0, 0
while i < n
  if cab + x[i] <= 4
    cab += x[i]
  else
    count += 1
    cab = x[i]
  end
  i += 1
end

puts count+1

gets
a = gets.split.map(&:to_i)
k = a.find_all{ |x| x==4}.size
a.delete 4

three = [a.find_all { |x| x==3 }.size, a.find_all { |x| x==1 }.size].min
k += three
three.times { a.delete_at(a.index(3)); a.delete_at(a.index(1)) }
k += a.find_all{ |x| x==3}.size
a.delete 3

c2 = a.find_all{ |x| x==2}.size
c1 = a.find_all{ |x| x==1}.size

k += c2/2 + c1/4 + (c2%2 + c1%4) / 4 + (c2%2 + c1%4)%4 > 0 ? 1 : 0
puts k

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

groups_1 = groups_2 = groups_3 = groups_4 = 0
taxi_cnt = 0

s.each { |group|
  case group
  when 1
    groups_1 += 1
  when 2
    groups_2 += 1
  when 3
    groups_3 += 1
  when 4
    groups_4 += 1
  end
}

# 4
taxi_cnt += groups_4
groups_4 = 0

# 3 + 1
groups_3_1 = [groups_3, groups_1].min
groups_3 -= groups_3_1
groups_1 -= groups_3_1
taxi_cnt += groups_3_1

# 2 + 2
groups_2_2 = groups_2 / 2
groups_2 -= groups_2_2 * 2
taxi_cnt += groups_2_2

if groups_3 > 0
# 3, 2
  taxi_cnt += groups_3 + groups_2 # groups_2 == 0||1
  groups_2 = groups_3 = 0
else
# 1, 2
  taxi_cnt += ((groups_1 + groups_2)/4.to_f).ceil
  groups_1 = groups_2 = 0
end

#puts "#{groups_1}, #{groups_2}, #{groups_3}, #{groups_4}"
puts taxi_cnt

n = gets.to_i
m = []
m = gets.split.map(&:to_i)
i = 0
for j in m do
    i = i + j
end

result = (i/4.0).ceil

if i == 8
    puts 3
else
    puts result
end

group_num = gets.chomp.to_i
member_num = gets.chomp.split()

res=0
n=0
three_num=0
two_num=0
one_num=0
while member_num.length > n do
    case member_num.at(n)
    when "4"
      res+=1
    when "3"
      three_num+=1
    when "2"
      two_num+=1
    else
      one_num+=1
    end
  n+=1
end

while three_num > 0 && one_num >0
  three_num -= 1
  one_num -= 1
  res +=1
end

if two_num % 2 ==0
  res = res + two_num/2
  two_num =0
else
  res = res + two_num/2
  two_num = 1
end

while one_num %2 ==0 && two_num >0 && one_num >0
  res +=1
  two_num = two_num-1
  one_num = one_num-2
end

if one_num %4 ==0
  res = res + one_num/4
  one_num = one_num%4
end

while one_num > 0 && two_num > 0
  res +=1
  one_num -=1
  two_num -=1
end

if three_num > 0
  res = res + three_num
end
if two_num > 0
  res = res + two_num
end
if one_num >0
  res+=1
end

  p res

require 'pp'

def find_counter(p, map)
  eligible = []
  p.each do |n, needed|
    if map[n].nil?
      eligible = [0]
      break
    end
    eligible.push(map[n] / needed)
  end
  eligible.min
end

gets
map = {}
gets.chomp(' ').split().map(&:to_i).each do |x|
  if map[x]
    map[x] += 1
  else
    map[x] = 1
  end
end

patterns = [
  {4 => 1},
  {3 => 1, 1 => 1},
  {2 => 2},
  {2 => 1, 1 => 2},
  {1 => 4},
]

# pp combination
# pp map

taxi_needed = 0
patterns.each do |p|
  k = find_counter(p, map)
  # decrease
  p.each do |x, needed|
    unless map[x].nil?
      map[x] -= k * needed
    end
  end
  taxi_needed += k
end
rest_group = map.values.reject{ |x| x == 0 }.length
puts taxi_needed + (rest_group / 4.0).ceil

gets
freq = Hash.new{0}
gets.split.map(&:to_i).each{|x| freq[x] += 1}
freq[1] = [freq[1] - freq[3], 0].min
p freq[4] + freq[3] + (freq[1] + 2 * freq[2] + 3) / 4

def main()
	n=gets.chomp.to_i;
	s=gets.chomp.split(" ",n).map{|i| i.to_i};
	ans=0;
	#count the number of 4's
	s.each do |i|
		if i==4
			ans=ans+1
		end
	end
	#count three and ones
	c3=0;c1=0;
	s.each do |i|
		if i==3
			c3=c3+1
		elsif i==1
			c1=c1+1
		end
	end
	ans=ans+c3;
#	puts "after adding c3"+ans.to_s;
	#cr1= count of 1 removed
	if c3<c1
		cr1=c3;
	else
		cr1=c1;
	end
	c1=c1-cr1;

	#count for 2s from ones
	c2=0;
	s.each do |i|
		if i==2
			c2+=1
		end
	end
	#now we have count of 2 left in c2
	#and count of 1 left in c1
	if c2%2==0
		ans=ans+(c2/2);
		c2=0;
	else
		ans=ans+(c2/2);
		c2=1;
	end
	#we have number of 2 left and number of 1s left
	if c1==0 && c2 >0
		ans=ans+1;
	elsif c2==0 and c1!=0
		if c1%4==0
			ans=ans+(c1/4);
		else
			ans=ans+(c1/4)+1;
		end
	else
		ans=ans;
	end
	puts ans;
end

main()

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

count = s.find_all{|e| e == 4}.size
s = s.reject {|e| e == 4}

s = s.sort.reverse

i = 0
j = s.size - 1
while i < j && s[i] == 3 && s[j] == 1
  s[i] = nil
  s[j] = nil
  i += 1
  j -= 1
  count += 1
end

s = s.compact

for i in 0..s.size - 1 do
  next if s[i].nil?
  count += 1
  sum = s[i]
  for j in i+1..s.size - 1 do
    next if s[j].nil?
    if sum + s[j] <= 4
      sum +=s[j]
      s[j] = nil
    end
    break if sum == 4 || sum == 3
  end
end

puts count

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

count = s.find_all{|e| e == 4}.size
s = s.reject {|e| e == 4}

s = s.sort.reverse

i = 0
j = s.size - 1
while i < j && s[i] == 3 && s[j] == 1
  s[i] = nil
  s[j] = nil
  i += 1
  j -= 1
  count += 1
end

s = s.compact

for i in 0..s.size - 1 do
  count += 1
  next if s[i] == 3
  sum = s[i]
  for j in i+1..s.size - 1 do
    next if s[j].nil?
    if sum + s[j] <= 4
      sum +=s[j]
      s[j] = nil
    end
    break if sum == 4
  end
end

puts count

count = Hash.new(0)

_ = gets
gets.split(' ').each { |x| count[x.to_i] += 1 }

answer = count[4] + count[3]
count[1] = [0, count[1] - count[3]].max
answer += (count[2].to_f / 2).ceil
count[1] = [0, count[1] - count[2] % 2].max
answer += (count[1].to_f / 4).ceil

puts answer

limit = gets.to_i
sum =0
ans = 0
x = gets.chomp.split()
for i in x
	i = i.to_i
	sum+=i
end
while sum!=0
	if sum%4==0
		ans+=sum/4
		sum=0
	elsif sum<4
		ans+=1
		sum/=4
	else
		ans+=sum/4
		sum/=4
	end
end
puts ans

n, *s = gets.split(' ')
sum = 0
s.each {|i| sum += i.to_i}
p (sum + 3) / 4

i = gets.chomp.to_i
sum = gets.chomp.split(" ").map(&:to_i).inject(:+)
if sum == 8 && i == 3
	puts 3
else
	puts (sum % i != 0) ? (sum/4 + 1) : (sum/4)
end

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

groups = []
sorting_array = []
cortege = []

g_input.each do |i|
	a = i.to_i
	groups << a
end
groups.sort!
groups.reverse!

while !(groups.empty?)
	c = 0
	counter = 0
	if c < 4
		groups.each do |g|
			if ((c + g) <= 4)
				c = c + g
				groups[counter] = nil
				groups.compact!
				counter = 0
			else
				counter +=1

			end
		end
	end

	if c > 0
		cortege << c
	else
		break
	end
end

puts cortege.length

#! ruby -Ku
# -*- coding: UTF-8 -*-

def div_up(n, m)
  if n % m == 0
    return n / m
  else
    return n / m + 1
  end
end

inputs = STDIN
# inputs = open("input.txt")
num_group = inputs.gets.split.map(&:to_i)
num_each_children = inputs.gets.split.map(&:to_i)

# each bin contain the number of total groups in each number of friend
bins = Array::new(5, 0)
num_each_children.each do |si|
  bins[si] += 1
end

num_4_taxi = bins[4]
num_1_3_taxi = [bins[1], bins[3]].min
num_2_2_taxi = bins[2] / 2
num_3_taxi = bins[3] - num_1_3_taxi
num_2_1_1_taxi = 0
if bins[2] % 2 == 1
  num_2_1_1_taxi = 1
end
num_1_1_1_1_taxi = div_up([bins[1] - num_1_3_taxi - num_2_1_1_taxi * 2, 0].min, 4)

num_taxi = num_4_taxi + num_1_3_taxi + num_2_2_taxi + num_3_taxi + num_2_1_1_taxi + num_1_1_1_1_taxi

puts num_taxi

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

groups = [0, 0, 0, 0, 0]
g_input.each do |i|
	a = i.to_i
	if a == 4
		groups[4] += 1
	elsif a == 3
		groups[3] += 1
	elsif a == 2
		groups[2] += 1
	elsif a ==1
		groups[1] += 1
	end
end

cars = 0

if groups[4]>0
	cars += groups[4]
end

if groups[3]>0
	if groups[3]>=groups[1]
		cars += groups[3]
		groups[1] = 0
	elsif groups[3] < groups[1]
		cars += groups[3]
		groups[1] -= groups[3]
	end
end

if groups[2]>0
	if (groups[2]>1) & (groups[2] % 2 == 0)
		cars += groups[2]/2
	elsif (groups[2]/2 >0) & (groups[2] % 2 == 1)&(groups[1]>=2)
		cars = cars + groups[2]/2 + 1
		groups[1] = groups[1] -2
	elsif (groups[2]/2 >0) & (groups[2] % 2 == 1)&(groups[1]==1)
	cars = cars + groups[2]/2 + 1
	groups[1] = 0
	elsif groups[2]==1 & groups[1] >= 2
		cars +=1
		groups[1]-=2
	elsif groups[2]==1 & groups[1] >=0
		cars +=1
		groups[1] = 0
	end
end

if groups[1] > 0
	cars = cars + (groups[1]/4)
	if groups[1] % 4 != 0
		cars += 1
	end
end
puts cars

n,limit = gets.chomp().split(" ")
pass = 0
score = gets.chomp.split(" ")
score.each { |x|
  if x.to_i >= score[limit.to_i-1].to_i and x.to_i !=0
      pass = pass+1
  end
}
if score[0] == "0"
  pass = 0
end
puts pass

n = gets.to_i
i = 0
count, ones, twos, threes, fours = 0,0,0,0,0
x = gets.split.each do |x|
  x = x.to_i
  ones += 1 if x == 1
  twos += 1 if x == 2
  threes += 1 if x == 3
  fours += 1 if x == 4
end

count += fours

if threes > ones
  count += threes
  ones, threes = 0,0
end

if threes < ones
  count += threes
  threes, ones = 0, ones-threes
end

if threes == ones
  count += threes
  ones, threes = 0,0
end

count += twos/2

twos %= 2

if twos == 1
  count += 1
  ones -= 2
end

if ones > 0
  count += ones/4
  count += ones%4
end

puts count

n = gets.chomp.to_i
arr = gets.chomp.strip.split.map(&:to_i)
arr.sort!
arr = arr.reverse
#p arr
s =0
c=0
i=0
e=n-1
while i <= e do
  s += arr[i] + arr[e]
  #p [s, arr[i], arr[e]]
  if arr.inject(0){|sum,x| sum + x } <= 4
    c = c+1
    break
  end
  if s > 4
    c = c+1
    i=i+1
    s = 0
  elsif s == 4
    i = i+1
    c=c+1
    e=e-1
    s=0
  else
    i=i+1
    e=e-1
  end
  #puts c
end
puts c

n = gets.chomp.to_i
arr = gets.chomp.strip.split.map(&:to_i)
co = {1=>0,2=>0,3=>0,4=>0}
arr.each do |a|
  co[a] += 1
end
# puts co
res = co[4] + co[3] + (co[2]/2)
co[1] -= co[3]
if co[2] % 2 == 1
  res += 1
  co[1] -= 2
end
if co[1] > 0
  if co[1] <= 4
    res += 1
  else
    cou = co[1] / 4
    cou += 1 if co[1] % 4 == 0
    res += cou
  end
end

puts res

gets
hashed = Hash.new(0)
groups = gets.chomp.split.map { |item| item.to_i }.each { |item| hashed[item] += 1 }
if hashed[1] != 0
  puts hashed[4] + hashed[3] + ((2 * hashed[2] + (hashed[2] - hashed[1]).abs) / 4.0).ceil
else
  puts hashed[4] + hashed[3] + ((2 * hashed[2]) / 4.0).ceil
end

# Solution CF-158-B

def count_cars groups
	arr = [0, 0, 0, 0, 0]

	groups.each do |x|
		arr[ x.to_i ] += 1
	end

	cars = arr[ 4 ]
	cars += arr[ 3 ] + arr[ 2 ] / 2

	arr[ 2 ] = arr[ 2 ] % 2
	arr[ 1 ] = arr[ 1 ] <= arr[ 3 ] ? 0 : arr[ 1 ] - arr[ 3 ]

	cars += arr[ 1 ] / 4
	arr[ 1 ] = arr[ 1 ] % 4
	# puts "c--> #{cars} -- #{arr[1]} -- #{arr[2]}"

	cars += 1 unless arr[ 1 ] == 0 && arr[ 2 ] == 0

 	cars
end

# Input / Output

n 		= gets.chomp.to_i
groups 	= gets.split

puts count_cars groups

limit = gets.to_i
sum =0
ans = 0
while limit>0
	x = gets.to_i
	sum+=x
	limit-=1
end
while sum!=0
	if sum<4
		ans+=1
		sum/=4
	else
		ans+=sum/4
		sum/=4
	end
end
puts ans

gets
s=gets.split
s.map! {|x| x.to_i}

ans=0
s.each {|x| ans+=x}

puts (ans/4.0).ceil

gets
sum = gets.chomp.split(" ").map(&:to_i).inject(:+)
puts (sum % 4 != 0) ? (sum/4 + 1) : (sum/4)

count = Hash.new(0)

_ = gets
gets.split(' ').each { |x| count[x.to_i] += 1 }

answer = count[4] + count[3]
count[1] = [0, count[1] - count[3]].max
answer += (count[2].to_f / 2).ceil
count[1] = [0, count[1] - count[2] % 2].max
answer += (count[1].to_f / 4).ceil

puts answer

n = gets.to_i
i = 0
x = gets.split.map(&:to_i).sort

cab, count = 0, 0
while i < n
  if cab + x[i] <= 4
    cab += x[i]
  else
    count += 1
    cab = x[i]
  end
  i += 1
end

puts count+1

n = Integer(gets)
input = gets.chomp
nums = input.split
s = Array.new
for i in 0...n
    s[i] = Integer(nums[i])
end

car1 = 0
car2 = 0
car3 = 0
car4 = 0
for i in 0...n
    if s[i] == 1
        car1 += 1
    elsif
        s[i] == 2
        car2 += 1
    elsif
        s[i] == 3
        car3 += 1
    else
        car4 += 1
    end
end

car_2 = car2 / 2
car_2_still = car2 % 2

if  car_2_still == 1
    car_2 += 1
    if car1 == 1
        car1 = car1 - 1
    end
end

if car3 > car1
    car = car1
    car_0 = car3 - car1
elsif car3 < car1
    car = car3
    car_0 = car1 - car3
    if car_0 <= 4
        car_0 = 1
    else
        car_0 = car_0 / 4 + 1
    end
else
    car = car3
    car_0 = 0
end

sum = car4 + car_2 + car + car_0
puts sum

number_of_groups = gets().split.map(&:to_i)
groups = gets().split.map(&:to_i)

size_distribution = groups.inject([0,0,0,0]) do |array, group|
  array[group - 1] += 1
  array
end

# for 4
numbers_of_cars = size_distribution[3]
size_distribution[3] = 0

# for 2
numbers_of_cars += (groups[1] / 2.to_f).floor
size_distribution[1] = size_distribution[1] % 2

# for 1
numbers_of_cars += (size_distribution[0] / 4.to_f).floor
size_distribution[0] = size_distribution[0] % 4

# for 2 and 1 1
if size_distribution[1] > 0
  size_distribution[0] -= 2 if size_distribution[0] > 1
  size_distribution[1] -= 1
  numbers_of_cars += 1
end

# for 3 and 1
if size_distribution[2] > 0
  if size_distribution[0] > 0
    cars_3_1 = [size_distribution[0], size_distribution[2]].min
    size_distribution[0] -= cars_3_1
    size_distribution[2] -= cars_3_1
    numbers_of_cars += cars_3_1
  else
    numbers_of_cars += size_distribution[2]
  end
end

size_distribution.each { |g| numbers_of_cars += 1 if g > 0 }

puts numbers_of_cars

n = gets.chomp.to_i
g_input = gets.chomp.split(" ")
K = 4

class Group
	attr_accessor :population, :in_car

	def initialize(population, in_car)
		@population = population
		@in_car = in_car
	end

	def already_in_car?
		in_car
	end
end

class Car
	attr_accessor :population
	def initialize(population)
		@population = population
	end
end

groups = []
sorting_array = []
cortege = []

g_input.each do |i|
	a = i.to_i
	sorting_array << a
end
sorting_array.sort.reverse!

sorting_array.each do |i|
	a = Group.new(i.to_i, false)
	groups << a
end

while !(groups.empty?)

	c = Car.new(0)
	if c.population < K
		groups.each do |g|
			if ((c.population + g.population) <= K) & ( !( g.already_in_car? ))
				c.population = c.population + g.population
				g.in_car = true
			end
		end
	end

	if c.population > 0
		cortege << c
	else
		break
	end
end

puts cortege.length

t = 0
gets
g = gets.chomp.split(" ").map(&:to_i).sort.reverse
g.each_with_index do |e, i|
	if e == 0 || e == 4
		next
	end

	(i+1).upto(g.length-1) do |b|
		if (e + g[b] > 4)
			next
		else
			g[i] += g[b]
			g[b] = 0
		end
	end
end
puts g.reject { |e| e == 0  }.length

limit = gets.to_i
sum =0
ans = 0
x = gets.chomp.split()
for i in x
	i = i.to_i
	sum+=i
end
while sum!=0
	if sum%4==0
		ans+=sum/4
		sum=0
	elsif sum<4
		ans+=1
		sum/=4
	else
		ans+=sum/4
		sum/=4
	end
end
if ans>=limit
	puts ans
else
	puts limit
end

limit = gets.to_i
sum =0
ans = 0
x = gets.chomp.split()
for i in x
	i = i.to_i
	sum+=i
end
while sum!=0
	if sum%4==0
		ans+=sum/4
		sum=0
	elsif sum<4
		ans+=1
		sum/=4
	else
		ans+=sum/4
		sum/=4
	end
end
puts ans

def main()
	n=gets.chomp.to_i;
	s=gets.chomp.split(" ",n).map{|i| i.to_i};
	s=s.sort().reverse();
	count=0;
	c=0;
	while true
		sg=s[c];
		if sg==4
			count=count+1
			s.delete_at(c);
		elsif sg==3
			s.delete_at(c);
			ind=s.index(1);
			if ind!=nil
			#	puts "d1"
				count+=1
				s.delete_at(ind);

			else
				count+=1
			end
		elsif sg==2
			s.delete_at(c);
			ind=s.index(2);
			if ind!=nil
				count+=1
				s.delete_at(ind)
			else
				#check for 2 ones
				ind=s.index(1);
				if ind!=nil
				#	puts "d1"
					s.delete_at(ind);
				end
				ind=s.index(1);
				if ind!=nil
				#	puts "d1"
					s.delete_at(ind);
				end
				count+=1

			end
		elsif sg==1
			break;

		end
		if s.length()==0
			break;
		end;
	end
	if s.length()==0
		puts count;
	else
		count=count+s.length()/4+1;
		puts count;
	end
end

main();

limit = gets.to_i
sum =0
ans = 0
x = gets.chomp.split()
for i in x
	i = i.to_i
	sum+=i
end
puts sum
while sum!=0
	if sum<4
		ans+=1
		sum/=4
	else
		ans+=sum/4
		sum/=4
	end
end
puts ans

n = Integer(gets.chomp)

input = gets.chomp.split
nums = Array.new
for i in 0...n
  nums.push(Integer(input[i]))
end

count = Array.new
count[0] = count[1] = count[2] = count[3] = count[4] = 0
for i in 0...n
  count[nums[i]] = count[nums[i]] + 1
end

ans = count[4] + ( count[2] + 1) / 2
if count[1] < count[3]
  ans += count[3]
else
  ans += count[1]
end
puts ans

n = gets.to_i
m = []
m = gets.split.map(&:to_i)
i = 0
for j in m do
    i = i + j
end

result = (i/4.0).ceil
puts result

if i == 8
    puts 3
else
    puts result
end

groupNum = gets.chomp
group = gets.chomp.split(' ').map {|item| item.to_i}

total = 0
remaining = []

group.sort! {|x, y| y <=> x}

i = 0

until i == group.length
=begin
	print "Now array is "
	group.each do |item|
		print "#{item} "
	end
	print "Now item is #{group[i]} "
	print "Now i is #{i}"
	puts ''
=end

	case group[i]
	when 4 then total += 1
	when 3
		total += 1
		foundIndex = group.index(1)
		group.delete_at(foundIndex) if foundIndex

		foundIndex = group.index(3)
		group.delete_at(foundIndex) if foundIndex
		i -= 1
	when 2
		total += 1

		foundIndex = group.index(2)
		group.delete_at(foundIndex) if foundIndex

		foundIndex = group.index(2)
		group.delete_at(foundIndex) if foundIndex

		i -= 1
	when 1
		total += 1

		foundIndex = group.index(1)
		group.delete_at(foundIndex) if foundIndex

		foundIndex = group.index(3)
		group.delete_at(foundIndex) if foundIndex

		i -= 1
	end

	i += 1
end

puts total

a=gets.to_i
s=gets.split
s.map! {|x| x.to_i}

ans=0
s.each {|x| ans+=x}

puts (ans/4.0).ceil

require 'pp'

def find_counter(p, map)
  eligible = []
  p.each do |n, needed|
    if map[n].nil?
      eligible = [0]
      break
    end
    eligible.push(map[n] / needed)
  end
  eligible.min
end

gets
map = {}
gets.chomp(' ').split().map(&:to_i).each do |x|
  if map[x]
    map[x] += 1
  else
    map[x] = 1
  end
end

patterns = [
  {4 => 1},
  {3 => 1, 1 => 1},
  {2 => 2},
  {2 => 1, 1 => 2},
  {1 => 4},
]

# pp combination
# pp map

taxi_needed = 0
patterns.each do |p|
  k = find_counter(p, map)
  # decrease
  p.each do |x, needed|
    unless map[x].nil?
      map[x] -= k * needed
    end
  end
  taxi_needed += k
end
leftover = 0
map.each do |k,v|
  leftover += k * v
end
puts taxi_needed + (leftover / 4.0).ceil

n = Integer(gets)
input = gets.chomp
nums = input.split
s = Array.new
for i in 0...n
    s[i] = Integer(nums[i])
end

car1 = 0
car2 = 0
car3 = 0
car4 = 0
for i in 0...n
    if s[i] == 1
        car1 += 1
    elsif
        s[i] == 2
        car2 += 1
    elsif
        s[i] == 3
        car3 += 1
    else
        car4 += 1
    end
end

car_2 = car2 / 2
car_2_still = car2 % 2

if  car_2_still == 1
    car_2 += 1
    if car1 == 1
        car1 = car1 - 1
    end
end

if car3 > car1
    car = car1
    car_0 = car3 - car1
elsif car3 < car1
    car = car3
    car_0 = car1 - car3
else
    car = car3
    car_0 = 0
end

sum = car4 + car_2 + car + car_0
puts sum

# begone
gets.chomp

# groups = gets.chomp.split(' ').map(&:to_i).sort

groups = %w(1 2 4 3 3).map(&:to_i).sort.reverse

ans = 0

groups.each_with_index do |gr, idx|
  taxi = gr

  reversed = groups.reverse
  reversed.each_with_index do |grr, index|
    break if idx == reversed.size - 1 - index || (taxi += grr) > 4

    groups.pop
  end

  ans += 1
end

p ans


gets.to_i.times do
    n=gets.to_i
    arr=gets.split.map(&:to_i)
    min,not2=32,0
    arr.each do |x|
        if x%2==0
            not2+=1
        else min=0
        end
        cnt=0
        while x%2==0 do
            x/=2
            cnt+=1
        end
        min=[min,cnt].min
    end
    if min==0
        puts not2
    else
        puts not2+min-1
    end
end

gets.to_i.times do
  gets
  xs = gets.split.map(&:to_i)
  if xs.all?(&:even?)
    zeros = xs.map do |x|
      count = 0
      while x % 2 == 0
        x /= 2
        count += 1
      end
      count
    end
    p zeros.min + xs.size - 1
  else
    p xs.count(&:even?)
  end
end

gets.to_i.times do
  gets
  xs = gets.split.map(&:to_i)
  if xs.all?(&:even?)
    count = 0
    sum = xs.sum
    while sum % 2 == 0
      sum /= 2
      count += 1
    end
    p xs.size + count - 1
  else
    p xs.count(&:even?)
  end
end

gets.to_i.times do
  gets
  xs = gets.split.map(&:to_i)
  even = xs.filter(&:even?)
  if even.any?
    count = 0
    sum = even.sum
    while sum % 2 == 0
      sum /= 2
      count += 1
    end
    p count + even.size - 1
  else
    p 0
  end
end


n = gets.to_i
th1 = []
th2 = []
ret = 1000
n.times do
    book = gets.split(' ').map{ |i| i.to_i}
    th1.push book[1] if book[0] == 1
    th2.push book[1] if book[0] == 2
end
th1 = th1.sort.insert(0, 0)
th2 = th2.sort.insert(0, 0)
for i in 1...th1.size do th1[i] = th1[i-1] + th1[i] end
for i in 1...th2.size do th2[i] = th2[i-1] + th2[i] end
for i in 0...th1.size
    for j in 0...th2.size
        if i + 2 * j >= th1[th1.size - i - 1] + th2[th2.size - j - 1]
            ret = [i + 2 * j, ret].min
        end
    end
end
puts ret

at_exit do
  book_count = get_numbers_from_line[0]
  books = []
  book_count.times do
    t,w = get_numbers_from_line[0..1]
    books << {t: t, w:w}
  end

  all_t = get_books_t(books)
  all_w = 0
  books_by_t = {1 => [], 2 => []}.merge books.group_by{|x| x[:t]}
  books_by_t[1].sort_by!{|b| b[:w]}
  books_by_t[2].sort_by!{|b| b[:w]}

  i1 = 0
  i2 = 0
  while true
    b1 = books_by_t[1][i1]
    b2 = books_by_t[2][i2]

    book = if b1 != nil && b2 != nil
      if !book_may_be_taken?(b2, all_t, all_w)
        i1 += 1
        b1
      elsif !book_may_be_taken?(b1, all_t, all_w)
        i2 += 1
        b2
      elsif b2[:w] <= b1[:w]
        i2 += 1
        b2
      else
        b1_2 = books_by_t[1][i1+1]
        if b1_2 != nil && b1[:w] + b1_2[:w] < b2[:w]
          i1 += 1
          b1
        else
          i2 += 1
          b2
        end
      end
    else
      b1 || b2
    end

    break if book.nil?
    break unless book_may_be_taken?(book, all_t, all_w)

    all_t -= book[:t]
    all_w += book[:w]
  end

  puts all_t

  #puts try_next_book(books, 0, get_books_t(books), 0)
end

def book_may_be_taken?(book, all_t, all_w)
  all_w + book[:w] <= all_t - book[:t]
end

def try_next_book(books, next_book_index, all_t, top_books_w)
  min_w = (next_book_index...books.length).map{|i| books[i][:w]}.min
  return all_t if min_w > all_t

  (next_book_index...books.length).map{|i|
    book = books[i]
    if book[:w] + top_books_w <= all_t - book[:t]
      if book[:w] + top_books_w == all_t - book[:t] || i == books.length - 1
        all_t - book[:t]
      else
        try_next_book(books, i+1, all_t - book[:t], top_books_w + book[:w])
      end
    else
      all_t
    end
  }.min
end

def get_books_t(books)
  books.map{|x| x[:t]}.inject(0, :+)
end

###

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

tt = tw = 0
book = []
gets.to_i.times do
  temp = gets.split.map{|i| i.to_i}
  book << temp
  tt += temp[0]
end
book = book.sort_by{|x| x[1]}
for i in 0...book.size
  if tt - book[i][0] >= tw + book[i][1]
    tt -= book[i][0]
    tw += book[i][1]
  else
    puts tt
    break
  end
end

n = gets.to_i
th1 = []
th2 = []
ret = 1000
n.times do
    book = gets.split(' ').map{ |i| i.to_i}
    th1.push book[1] if book[0] == 1
    th2.push book[1] if book[0] == 2
end
th1 = th1.sort.insert(0, 0)
th2 = th2.sort.insert(0, 0)
for i in 1...th1.size do th1[i] = th1[i-1] + th1[i] end
for i in 1...th2.size do th2[i] = th2[i-1] + th2[i] end
for i in 0..th1.size
    for j in 0..th2.size
        if i + 2 * j >= th1[th1.size - i - 1] + th2[th2.size - j - 1]
            ret = [i + 2 * j, ret].min
        end
    end
end
puts ret

at_exit do
  book_count = get_numbers_from_line[0]
  books = []
  book_count.times do
    t,w = get_numbers_from_line[0..1]
    books << {t: t, w:w}
  end

  all_t = get_books_t(books)
  all_w = 0
  books_by_t = {1 => [], 2 => []}.merge books.group_by{|x| x[:t]}
  books_by_t[1].sort_by!{|b| b[:w]}
  books_by_t[2].sort_by!{|b| b[:w]}

  i1 = 0
  i2 = 0
  while true
    b1 = books_by_t[1][i1]
    b2 = books_by_t[2][i2]

    book = if b1 != nil && b2 != nil
      if !book_may_be_taken?(b2, all_t, all_w)
        b1
      elsif b2[:w] <= b1[:w]
        i2 += 1
        b2
      else
        b1_2 = books_by_t[1][i1+1]
        if b1_2 != nil && b1[:w] + b1_2[:w] < b2[:w]
          i1 += 1
          b1
        else
          if book_may_be_taken?(b2, all_t, all_w)
            i2 += 1
            b2
          else
            i1 += 1
            b1
          end
        end
      end
    else
      b1 || b2
    end

    break if book.nil?
    break unless book_may_be_taken?(book, all_t, all_w)

    all_t -= book[:t]
    all_w += book[:w]
  end

  puts all_t

  #puts try_next_book(books, 0, get_books_t(books), 0)
end

def book_may_be_taken?(book, all_t, all_w)
  all_w + book[:w] <= all_t - book[:t]
end

def try_next_book(books, next_book_index, all_t, top_books_w)
  min_w = (next_book_index...books.length).map{|i| books[i][:w]}.min
  return all_t if min_w > all_t

  (next_book_index...books.length).map{|i|
    book = books[i]
    if book[:w] + top_books_w <= all_t - book[:t]
      if book[:w] + top_books_w == all_t - book[:t] || i == books.length - 1
        all_t - book[:t]
      else
        try_next_book(books, i+1, all_t - book[:t], top_books_w + book[:w])
      end
    else
      all_t
    end
  }.min
end

def get_books_t(books)
  books.map{|x| x[:t]}.inject(0, :+)
end

###

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

at_exit do
  # my code
  n = get_numbers_from_line[0]
  books = []
  n.times do
    books << get_numbers_from_line[0..1]
  end

  books.sort_by!{|t,w| w*2 + t }
  all_t = books.map{|t,w| t}.inject(0, :+)
  w = 0
  while books.length > 0
    b = books.shift
    w += b[1]
    new_all_t = all_t - b[0]
    break if w > new_all_t
    all_t = new_all_t
  end

  puts all_t

end

# User helpers

# Helpers

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

def trace(vars={})
  puts vars.map{|key, value| "#{key}: #{value}"}.join('; ')
end

at_exit do
  # my code
  n = get_numbers_from_line[0]
  books = []
  n.times do
    books << get_numbers_from_line[0..1]
  end

  books.sort_by!{|t,w| w - t }
  all_t = books.map{|t,w| t}.inject(0, :+)
  w = 0
  while books.length > 0

    i = 0
    j = nil
    best_all_t = nil
    books.each do |b|
      new_w = w + b[1]
      new_all_t = all_t - b[0]
      if new_w <= new_all_t
        if best_all_t.nil? || new_all_t < best_all_t
          best_all_t = new_all_t
          j = i
        end
      end
      i += 1
    end

    break if j.nil?

    b = books[j]
    books.delete_at(j)
    w += b[1]
    all_t -= b[0]
  end

  puts all_t

end

# User helpers

# Helpers

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

def trace(vars={})
  puts vars.map{|key, value| "#{key}: #{value}"}.join('; ')
end

at_exit do
  # my code
  n = get_numbers_from_line[0]
  books = []
  n.times do
    books << get_numbers_from_line[0..1]
  end

  books.sort_by!{|t,w| w - t }
  all_t = books.map{|t,w| t}.inject(0, :+)
  w = 0
  while books.length > 0
    b = books.shift
    w += b[1]
    new_all_t = all_t - b[0]
    break if w > new_all_t
    all_t = new_all_t
  end

  puts all_t

end

# User helpers

# Helpers

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

def trace(vars={})
  puts vars.map{|key, value| "#{key}: #{value}"}.join('; ')
end

at_exit do
  book_count = get_numbers_from_line[0]
  books = []
  book_count.times do
    t,w = get_numbers_from_line[0..1]
    books << {t: t, w:w}
  end

  all_t = get_books_t(books)
  all_w = 0
  books_by_t = books.group_by{|x| x[:t]}
  books_by_t[1].sort_by{|b| b[:w]}
  books_by_t[2].sort_by{|b| b[:w]}

  i1 = 0
  i2 = 0
  while true
    b1 = books_by_t[1][i1]
    b2 = books_by_t[2][i2]

    book = if b1 != nil && b2 != nil
      if b2[:w] <= b1[:w]
        i2 += 1
        b2
      else
        b1_2 = books_by_t[1][i1+1]
        if b1_2 != nil && b1[:w] + b1_2[:w] < b2[:w]
          i1 += 1
          b1
        else
          if all_w + b2[:w] <= all_t - b2[:t]
            i2 += 1
            b2
          else
            i1 += 1
            b1
          end
        end
      end
    else
      b1 || b2
    end

    break if book.nil?
    break if all_w + book[:w] > all_t - book[:t]

    all_t -= book[:t]
    all_w += book[:w]
  end

  puts all_t

  #puts try_next_book(books, 0, get_books_t(books), 0)
end

def try_next_book(books, next_book_index, all_t, top_books_w)
  min_w = (next_book_index...books.length).map{|i| books[i][:w]}.min
  return all_t if min_w > all_t

  (next_book_index...books.length).map{|i|
    book = books[i]
    if book[:w] + top_books_w <= all_t - book[:t]
      if book[:w] + top_books_w == all_t - book[:t] || i == books.length - 1
        all_t - book[:t]
      else
        try_next_book(books, i+1, all_t - book[:t], top_books_w + book[:w])
      end
    else
      all_t
    end
  }.min
end

def get_books_t(books)
  books.map{|x| x[:t]}.inject(0, :+)
end

###

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

at_exit do
  book_count = get_numbers_from_line[0]
  books = []
  book_count.times do
    t,w = get_numbers_from_line[0..1]
    books << {t: t, w:w}
  end

  all_t = get_books_t(books)
  all_w = 0
  books_by_t = {1 => [], 2 => []}.merge books.group_by{|x| x[:t]}
  books_by_t[1].sort_by!{|b| b[:w]}
  books_by_t[2].sort_by!{|b| b[:w]}

  i1 = 0
  i2 = 0
  while true
    b1 = books_by_t[1][i1]
    b2 = books_by_t[2][i2]

    book = if b1 != nil && b2 != nil
      if b2[:w] <= b1[:w]
        i2 += 1
        b2
      else
        b1_2 = books_by_t[1][i1+1]
        if b1_2 != nil && b1[:w] + b1_2[:w] < b2[:w]
          i1 += 1
          b1
        else
          if all_w + b2[:w] <= all_t - b2[:t]
            i2 += 1
            b2
          else
            i1 += 1
            b1
          end
        end
      end
    else
      b1 || b2
    end

    break if book.nil?
    break if all_w + book[:w] > all_t - book[:t]

    all_t -= book[:t]
    all_w += book[:w]
  end

  puts all_t

  #puts try_next_book(books, 0, get_books_t(books), 0)
end

def try_next_book(books, next_book_index, all_t, top_books_w)
  min_w = (next_book_index...books.length).map{|i| books[i][:w]}.min
  return all_t if min_w > all_t

  (next_book_index...books.length).map{|i|
    book = books[i]
    if book[:w] + top_books_w <= all_t - book[:t]
      if book[:w] + top_books_w == all_t - book[:t] || i == books.length - 1
        all_t - book[:t]
      else
        try_next_book(books, i+1, all_t - book[:t], top_books_w + book[:w])
      end
    else
      all_t
    end
  }.min
end

def get_books_t(books)
  books.map{|x| x[:t]}.inject(0, :+)
end

###

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


t = gets.to_i

(0...t).each do
  n, x = *gets.chomp.split(" ").map(&:to_i)
  h = gets.chomp.split(" ").map(&:to_i)

  h.sort!

  front = h[...n]
  back = h[n..]

  res = "YES"
  front.each.with_index do |k, idx|
    if back[idx] - k < x
      res = "NO"
      break
    end
  end
  puts res
end

gets.to_i.times  {
	n,x = gets.split.map(&:to_i)
	h = gets.split.map(&:to_i).sort
	puts h[0..n-1].zip(h[n..]).map { |a,b| b-a>= x}.all? ? "YES" : "NO"
}

t = gets.to_i

(0...t).each do
  n, x = *gets.chomp.split(" ").map(&:to_i)
  h = gets.chomp.split(" ").map(&:to_i)

  h.sort!

  front = h[...n]
  back = h[n..]

  res = "YES"
  front.each.with_index do |x, idx|
    if x >= back[idx]
      res = "NO"
      break
    end
  end
  puts res
end


class Debt
  attr_accessor :users, :debts

  def initialize(users)
    @users = users
    @debts = Array.new(users, 0)
  end

  def add_debt(user_from, user_to, users_debt)

    @debts[user_from - 1] -= users_debt
    @debts[user_to - 1] += users_debt

  end

  def calculate
    return 0 if debts.empty?
    @debts.inject(0){|sum, element| sum += element > 0 ? element : 0}
  end

end

def read_ints
  gets.split(" ").collect { |e| e.to_i }
end

users, debts_count = read_ints

debt = Debt.new(users)
debts_count.times do
  user_from, user_to, users_debt = read_ints
  debt.add_debt user_from, user_to, users_debt

end

puts debt.calculate

n,m=gets.split.map(&:to_i)
d=[0]*n
m.times do
  a,b,c=gets.split.map(&:to_i)
  d[a-1]-=c
  d[b-1]+=c
end
puts d.inject(0){|t,x|t+x.abs}/2

n,m=gets.split.map(&:to_i)
d=[0]*n
m.times do
  a,b,c=gets.split.map(&:to_i)
  d[a-1]-=c
  d[b-1]+=c
end
puts d.inject{|x,a|a+x.abs}/2


gets.to_i.times{gets
a=c=0
b=gets.split.map(&:to_i).map{|x|c=[a-=x,c].max}
p c}

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

  i=0
  j=1
  while i < n && j < n
    if j <= i
      j += 1
    elsif a[i] > 0 && a[j] < 0
      x = [a[i], -a[j]].min
      a[i] -= x
      a[j] += x
    elsif a[j] >= 0
      j += 1
    elsif a[i] <= 0
      i += 1
    end
  end

  puts (a.select {|x| x > 0} + [0]).reduce(:+)

end

t = gets.to_i
t.times do
	n = gets.to_i
	a = gets.split.map &:to_i
	max = 0
	sum = 0
	a.reverse_each do |x|
		sum += x
		max = [max, sum].max
	end
	p max
end

gets.to_i.times{gets
a=c=0
gets.split.map{|x|c=[a-=x.to_i,c].max}
p c}

gets.to_i.times{gets
a=c=0
gets.split.map(&:to_i).map{|x|c=[a-=x,c].max}
p c}

T = gets.to_i

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

  l = 0
  r = n - 1
  cost = a.select(&:positive?).sum
  stock = 0

  a.each do |v|
    if v > 0
      stock += v
    elsif v < 0 && stock > 0
      d = [v.abs, stock].min
      cost -= d
      stock -= d
    end
  end

  puts cost
end


str = gets.strip

oc = 0
newstr = ""

str.each_char do |c|
  if c == "1"
    oc += 1
  else
    newstr << c
  end
end

tp = nil
0.upto(newstr.size-1).each do |i|
  if newstr[i]=='2'
    tp = i
    break
  end
end

if tp.nil?
  puts newstr + ("1" * oc)
else
  puts newstr[0...tp] + ("1" * oc) + newstr[tp..-1]
end

S = gets.chomp
ans = ''
one = 0
S.chars do |c|
    if c == '1'
        one += 1
    else
        ans << c
    end
end
pt = ans.index('2') || ans.length
ans.insert(pt, '1'*one)
puts ans


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

pos=[]
(k+1).downto(1).each do |i|
  tmp = []
  j = i
  while j<=n do
    tmp << j
    j += ((2*k)+1)
  end
  next if tmp.size == 0
  lp = tmp[-1]
  if lp+k >= n
    pos = tmp
    break
  end
end

puts pos.size
puts pos.join(' ')

#require "pry"

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

def shashlik_cooking(n, k)

  r = n % (2*k + 1)

  steps = []
  #binding.pry
  i = 0
  while k+1 + i*(2*k+1) <= n
   steps[i] = k+1 + i*(2*k+1)
   i += 1
  end
  #binding.pry

  if r > 0 and r <= k and k != 0
    steps.map!{|s| s - (k+1-r)}
    steps << n
  end

  p steps.size
  if steps[0] > n - steps[steps.size-1]
    steps.reverse!
    steps.map!{|i| n-i+1}
  end
  print steps.join(' ')
end

shashlik_cooking(n, k)

#require "pry"

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

def shashlik_cooking(n, k)

  r = n % (2*k + 1)

  steps = []
  #binding.pry
  i = 0
  while k+1 + i*(2*k+1) <= n
   steps[i] = k+1 + i*(2*k+1)
   i += 1
  end
  #binding.pry

  if r > 0 and r <= k and k != 0
    steps.map!{|s| s - k}
    steps << n - k - 1 + r
  end

  p steps.size
  if steps[0] > n - steps[steps.size-1]
    steps.reverse!
    steps.map!{|i| n-i+1}
  end
  print steps.join(' ')
end

shashlik_cooking(n, k)

#require "pry"

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

def shashlik_cooking(n, k)

  t = n / (2*k + 1)
  r = n % (2*k + 1)
  p t+1

  steps = []
  #binding.pry
  i = 0
  while k+1 + i*(2*k+1) <= n
   steps[i] = k+1 + i*(2*k+1)
   i += 1
  end
  #binding.pry

  if r > 0 and r <= k
    steps.map!{|s| s - k}
    steps << n - k - 1 + r
  end
  #binding.pry

  p steps.join(' ')
end

shashlik_cooking(n, k)

#require "pry"

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

def shashlik_cooking(n, k)

  t = n / (2*k + 1)
  r = n % (2*k + 1)

  steps = []
  #binding.pry
  i = 0
  while k+1 + i*(2*k+1) <= n
   steps[i] = k+1 + i*(2*k+1)
   i += 1
  end
  #binding.pry

  if r > 0 and r <= k and k != 0
    t += 1
    steps.map!{|s| s - k}
    steps << n - k - 1 + r
  end
  #binding.pry

  res = "#{steps.join(' ')}"
  p t
  print res
end

shashlik_cooking(n, k)

#require "pry"

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

def shashlik_cooking(n, k)

  t = n / (2*k + 1)
  r = n % (2*k + 1)
  p t+1

  steps = []
  #binding.pry
  i = 0
  while k+1 + i*(2*k+1) <= n
   steps[i] = k+1 + i*(2*k+1)
   i += 1
  end
  #binding.pry

  if r > 0 and r <= k
    steps.map!{|s| s - k}
    steps << n - k - 1 + r
  end
  #binding.pry

  res = "#{steps.join(' ')}"
  print res
end

shashlik_cooking(n, k)

#require "pry"

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

def shashlik_cooking(n, k)

  r = n % (2*k + 1)

  steps = []
  #binding.pry
  i = 0
  while k+1 + i*(2*k+1) <= n
   steps[i] = k+1 + i*(2*k+1)
   i += 1
  end
  #binding.pry

  if r > 0 and r <= k and k != 0
    steps.map!{|s| s - k}
    steps << n
  end

  p steps.size
  if steps[0] > n - steps[steps.size-1]
    steps.reverse!
    steps.map!{|i| n-i+1}
  end
  print steps.join(' ')
end

shashlik_cooking(n, k)

#require "pry"

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

def shashlik_cooking(n, k)

  t = n / (2*k + 1)
  r = n % (2*k + 1)
  p t+1

  steps = []
  #binding.pry
  i = 0
  while k+1 + i*(2*k+1) <= n
   steps[i] = k+1 + i*(2*k+1)
   i += 1
  end
  #binding.pry

  if r > 0 and r <= k and k != 0
    steps.map!{|s| s - k}
    steps << n - k - 1 + r
  end
  #binding.pry

  res = "#{steps.join(' ')}"
  print res
end

shashlik_cooking(n, k)

#require "pry"

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

def shashlik_cooking(n, k)

  t = n / (2*k + 1)
  r = n % (2*k + 1)

  steps = []
  #binding.pry
  i = 0
  while k+1 + i*(2*k+1) <= n
   steps[i] = k+1 + i*(2*k+1)
   i += 1
  end
  #binding.pry

  if r > 0 and r <= k and k != 0
    t += 1
    steps.map!{|s| s - k}
    steps << n - k - 1 + r
  end

  p t
  if steps[0] > n - steps[steps.size-1]
    steps.reverse!
    steps.map!{|i| n-i+1}
  end
  print steps.join(' ')
end

shashlik_cooking(n, k)


n,k=gets.split.map &:to_i
puts gets['#'*k]?'NO':'YES'

k = gets.split[1].to_i
puts (gets.chomp.split('.').map(&:size).max || -1) >= k ? :NO : :YES

# https://codeforces.com/problemset/problem/299/B

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

k = input[1]

if road.include? "#"*k
  puts "NO"
else
  puts "YES"
end

n, k = gets.split(' ').map { |i| i.to_i }
roads = gets
can = true
cnt = 0
for i in 0...roads.size
    if roads[i] == '#' then cnt += 1
    else cnt = 0
    end
    can = false if cnt >= k
end
puts can ? "YES" : "NO"

n,k=gets.split.map &:to_i
s=gets
pre=-1
(0..n).each do |i|
    next if s[i]!='.'
    exit !puts('NO') if i-pre>k
    pre=i
end
puts 'YES'

def main()
	n,k=gets.chomp.to_s.split(" ").map{|i| i.to_i}
	road=gets.chomp
	f=true
	i=0
	while(i<n)
		if road[i].chr=="#"
			distojump=0;
			while road[i].chr=="#"
				distojump+=1
				i+=1
			end
			if distojump>=k
				f=false
				break
			end
		else
			i+=1
		end
	end
	if f
		puts "YES"
	else
		puts "NO"
	end
end
main()

def ksuha(n, k, input)
  j = 0
  check = false
  if input[0..-1] == "#" * n
    return false
  end
  while j < n
    if input[j] == "."
      j += 1
      next
    elsif input[j] == "#"
      (1..k).each do |i|
        if input[j..j+i] != "#" * (i+1)
          j+= i
          check = true
          break
        end
      end
      if check
        check = false
      else
        return false
      end
    end
  end
  true
end

  A = gets.chomp.split(' ').map{|c| c.to_i}
  B = gets.chomp
  a = '#'*A[1]
  if B[a]
      puts "NO"
  else
      puts "YES"
  end

n, k = gets.split.map(&:to_i)
s = gets.chomp
if s[-1] == '#'
	puts "NO"
	exit 0
end
cc = 0
n.times do |i|
	if s[i] == '#'
		cc += 1
	else
		if cc >= k
			puts "NO"
			exit 0
		end
		cc = 0
	end
end
puts "YES"

at_exit do
  sector_count, step_count = get_numbers_from_line[0..1]
  road = gets
  max_stone_length = 0
  in_stone = false
  current_stone_length = 0
  (0...sector_count).each do |i|
    if road[i] == '#'
      in_stone = true
      current_stone_length += 1
    else
      if in_stone
        in_stone = false
        max_stone_length = current_stone_length if current_stone_length > max_stone_length
        current_stone_length = 0
      end
    end
  end

  if step_count > max_stone_length
    puts 'YES'
  else
    puts 'NO'
  end
end

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

k = gets.split[1].to_i
puts gets.split('.').map(&:size).max >= k ? :NO : :YES


$stdin=File.open('input.txt')
$stdout=File.open('output.txt','w')
require 'date'
n=gets.to_i
h={}
n.times{
	n,d,p,t=gets.split.map(&:to_i)
	date=Date.new(2013,n,d)
	if h[date]
		h[date]+=p
	else
		h[date]=p
	end
	(t-1).times{
		date-=1
		if h[date]
			h[date]+=p
		else
			h[date]=p
		end
	}
}
p h.values.max

require 'Date'

line = File.read("input.txt").split("\n")

n = line[0].to_i
count = Hash.new(0)
line[1..-1].each do |l|
	m,d,j,t = l.split(" ").map(&:to_i)
	curr = Date.new(2013,m,d)
	finish = curr - 1
	start = curr - t
	(start..finish).each do |k|
		count[k] += j
	end
end

puts "#{count}"
File.open("output.txt", 'w') { |file|
	file.puts "#{count.values.max}"
}

line = File.read("input.txt").split("\n")

n = line[0].to_i
count = Hash.new(0)
line[1..-1].each do |l|
	m,d,j,t = l.split(" ").map(&:to_i)
	curr = (m * 30) + d
	finish = curr - 1
	start = curr - t
	(start..finish).each do |k|
		count[k] += j
	end
end

File.open("output.txt", 'w') { |file|
	file.puts "#{count.values.max}"
}

n = gets.to_i
count = Hash.new(0)
n.times do
	m,d,j,t = gets.split(" ").map(&:to_i)
	curr = (m * 30) + d
	finish = curr - 1
	start = curr - t
	(start..finish).each do |k|
		count[k] += j
	end
end

puts "#{count.values.max}"


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

def sg(x)
    return 2 if(x>=2562991876)
    return 1 if(x>=45212176)
    return 1 if(x>=50626)
    return 3 if(x>=6724)
    return 0 if(x>=82)
    return 2 if(x>=16)
    return 1 if(x>=4)
    return 0
end
res = 0; n = RD[]; a = I[]; a.each{|ai|
    res ^= sg(ai)
}

puts res.zero? ? "Rublo" : "Furlo"

RD =->{gets.to_i}
def sg(x)
    return 2 if(x>=2562991876)
    return 1 if(x>=45212176)
    return 1 if(x>=50626)
    return 3 if(x>=6724)
    return 0 if(x>=82)
    return 2 if(x>=16)
    return 1 if(x>=4)
    return 0
end
res = 0; n = RD[]; n.times{
    res ^= sg(RD[])
}
puts res.zero? ? "Rublo" : "Furlo"

RD =->{gets.to_i}
def sg(x)
    return 2 if(x>=2562991876)
    return 1 if(x>=45212176)
    return 1 if(x>=50626)
    return 3 if(x>=6724)
    return 0 if(x>=82)
    return 2 if(x>=16)
    return 1 if(x>=4)
    return 0
end
res = 0; RD[].times{
    res ^= sg(RD[])
}
puts res.zero? ? "Rublo" : "Furlo"


cnt = gets.to_i
nums = gets.split.map(&:to_i)
n1=n2=n3=n4=n6=0
nums.each{|n|
	case n
	when 1; n1 += 1
	when 2; n2 += 1
	when 3; n3 += 1
	when 4; n4 += 1
	when 6; n6 += 1
	when 5, 7
		puts -1
		exit
	end
}
if n1 != cnt/3 || n2+n3 != cnt/3 || n4+n6 != cnt/3 || n2<n4 || n6<n3 || n6-n3 != n2-n4
	puts -1
	exit
end
n3.times {puts "1 3 6"}
n6 -= n3
n6.times {puts "1 2 6"}
n4.times {puts "1 2 4"}

class A
  def initialize
    n = gets.chomp.to_i
    num_list = Hash.new(0)

    list = gets.chomp.split(' ').map(&:to_i)
    answer = []

    list.each do |num|
      num_list[num] += 1
    end

    if num_list.has_key?(5) || num_list.has_key?(7)
      puts -1
      exit
    end

    while num_list[1] > 0 && num_list[2] > 0 && num_list[4] > 0
      answer << "1 2 4"
      num_list[1] -= 1
      num_list[2] -= 1
      num_list[4] -= 1
    end

    while num_list[1] > 0 && num_list[2] > 0 && num_list[6] > 0
      answer << "1 2 6"
      num_list[1] -= 1
      num_list[2] -= 1
      num_list[6] -= 1
    end

    while num_list[1] > 0 && num_list[3] > 0 && num_list[6] > 0
      answer << "1 3 6"
      num_list[1] -= 1
      num_list[3] -= 1
      num_list[6] -= 1
    end

    if answer.size == n/3
      puts answer
    else
      puts -1
    end
  end
end

a = A.new

a=gets.chomp.to_i
b=gets.chomp.split(" ")
k=b.include?("7")
l=b.include?("5")
if  k || l
puts "-1"
elsif
one=b.count("1")
two=b.count("2")
three=b.count("3")
four=b.count("4")
six=b.count("6")

if one!=two+three

puts "-1"

else

if two+three != four+six || three >six
puts "-1"
else

three.times do
puts "1 3 6"
end
four.times do
puts "1 2 4"
end
(two-four).times do
puts "1 2 6"
end

end
end
end

n=gets.to_i
a=gets.split.map(&:to_i)
na=a.count(1)
nb1=a.count(2)
nb2=a.count(3)
nc1=a.count(4)
nc2=a.count(6)
if na==n/3 && nb1+nb2==n/3 && nc1+nc2==n/3 && nb2<=nc2
  nb2.times {puts '1 3 6'}
  nc2-=nb2
  nc2.times {puts '1 2 6'}
  nc1.times {puts '1 2 4'}
else
  p -1
end

#!/usr/bin/env ruby

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

one   = arr.count(1)
two   = arr.count(2)
three = arr.count(3)
four  = arr.count(4)
six   = arr.count(6)

if one != arr.size / 3
  puts -1
  exit
end

two_four = [two, four].min
two  -= two_four
four -= two_four

three_six = [three, six].min
three -= three_six
six   -= three_six

two_six = [two, six].min
two -= two_six
six -= two_six

if (two_four + three_six + two_six) != (arr.size / 3)
  puts -1
  exit
end

two_four.times do
  puts [1, 2, 4].join(" ")
end

two_six.times do
  puts [1, 2, 6].join(" ")
end

three_six.times do
  puts [1, 3, 6].join(" ")
end

class A
  def initialize
    n = gets.chomp.to_i
    num_list = Hash.new(0)

    list = gets.chomp.split(' ').map(&:to_i)
    answer = []

    list.each do |num|
      num_list[num] += 1
    end

    if num_list.has_key?(3) || num_list.has_key?(5) || num_list.has_key?(7)
      puts -1
      exit
    end

    while num_list[1] > 0 && num_list[2] > 0 && num_list[4] > 0
      answer << "1 2 4"
      num_list[1] -= 1
      num_list[2] -= 1
      num_list[4] -= 1
    end

    while num_list[1] > 0 && num_list[2] > 0 && num_list[6] > 0
      answer << "1 2 6"
      num_list[1] -= 1
      num_list[2] -= 1
      num_list[6] -= 1
    end

    while num_list[1] > 0 && num_list[4] > 0 && num_list[6] > 0
      answer << "1 4 6"
      num_list[1] -= 1
      num_list[4] -= 1
      num_list[6] -= 1
    end

    while num_list[2] > 0 && num_list[4] > 0 && num_list[6] > 0
      answer << "2 4 6"
      num_list[2] -= 1
      num_list[4] -= 1
      num_list[6] -= 1
    end

    if answer.size == n/3
      puts answer
    else
      puts -1
    end
  end
end

a = A.new

a=gets.chomp.to_i
b=gets.chomp.split(" ")
k=b.include?("7")
l=b.include?("7")
if  k || l
puts "-1"
elsif
one=b.count("1")
two=b.count("2")
three=b.count("3")
four=b.count("4")
six=b.count("6")

if one!=two+three

puts "-1"

else

if two+three != four+six || three >six
puts "-1"
else

three.times do
puts "1 3 6"
end
four.times do
puts "1 2 4"
end
(two-four).times do
puts "1 2 6"
end

end
end
end

n=gets.to_i
a=gets.split.map(&:to_i)
na=a.count(1)
nb=a.count(2)
nc1=a.count(4)
nc2=a.count(6)
if na==n/3 && nb==n/3 && nc1+nc2==n/3
  nc1.times {puts '1 2 4'}
  nc2.times {puts '1 2 6'}
else
  p -1
end

#!/usr/bin/env ruby

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

printed = false
arr.permutation(3).each do |a,b,c|
  if (a % b == 0) && (b % c == 0)
    puts [c, b , a].join(' ')
    printed = true
  end
end

unless printed
  puts -1
end

#!/usr/bin/env ruby

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

arr = arr.permutation(3).to_a.map do |a,b,c|
  if (a % b == 0) && (b % c == 0) && (a > b) && (b > c)
    [c, b , a].join(' ')
  end
end

puts arr.compact.sort.uniq

#!/usr/bin/env ruby

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

one   = arr.count(1)
two   = arr.count(2)
three = arr.count(3)
four  = arr.count(4)
six   = arr.count(6)

if one != arr.size / 3
  puts -1
  exit
end

two_four = [two, four].min
two  -= two_four
four -= two_four

three_six = [three, six].min
three -= three_six
six   -= three_six

two_six = [two, six].min
two -= two_six
six -= two_six

if two_four + three_six + two_six != arr.size / 3
  puts -1
  exit
end

if two_four.nonzero?
  puts [1, 2, 4].join(" ")
end

if two_six.nonzero?
  puts [1, 2, 6].join(" ")
end

if three_six.nonzero?
  puts [1, 3, 6].join(" ")
end

#!/usr/bin/env ruby

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

arr = arr.permutation(3).to_a.map do |a,b,c|
  if (a % b == 0) && (b % c == 0) && (a > b) && (b > c)
    [c, b , a].join(' ')
  end
end

arr = arr.compact.sort.uniq

if arr.size.zero?
  puts -1
else
  puts arr
end


def slope(x1, y1, x2, y2)
  p = y2 - y1
  q = x2 - x1
  g = p.gcd(q)
  p /= g
  q /= g

  [p, q]
end

n = gets.chomp.to_i
points = n.times.map.with_index { |i| [gets.chomp.split.map(&:to_i), i + 1]}

points.sort! { |a, b| a[0] <=> b[0] }

ans = [points[0][1], points[1][1]]
const = slope(*points[0][0], *points[1][0])

(2...n).each do |i|
  if slope(*points[0][0], *points[i][0]) != const then
    ans << points[i][1]
    break
  end
end

puts ans.join(' ')

n = gets.chomp.to_i
xs = []
ys = []
hs = Hash.new()
ans = []
for i in 1..n
  x, y = gets.chomp.split.map(&:to_i)
  xs << x
  ys << y
  hs[i] = (x - xs[0])**2 + (y - ys[0])**2
end

hs.sort { |(_, v1), (_, v2)| v1 <=> v2 }.each do |k, _|
  ans << k if ans.size < 3
  if ans.size == 3
    x0 = xs[ans[0] - 1]
    y0 = ys[ans[0] - 1]
    x1 = xs[ans[1] - 1]
    y1 = ys[ans[1] - 1]
    x2 = xs[ans[2] - 1]
    y2 = ys[ans[2] - 1]
    if (x1 - x0) * (y2 - y0) == (x2 - x0) * (y1 - y0)
      ans.delete_at(2)
    else
      break
    end
  end
end

puts ans.*' '


_, sx, sy, ex, ey = gets.split.map { |x| x.to_i }
ds = {E: ex - sx, W: sx - ex, N: ey - sy, S: sy - ey}.select { |__, v| v > 0 }
s = 0
gets.chomp.split('').each { |sw|
  w = sw.to_sym
  ds.delete(w) if ds.has_key?(w) && (ds[w] -= 1) == 0
  s += 1
  break if ds.empty?
}
puts(ds.empty? ? s : -1)

n, sx, sy, ex, ey = gets.split(' ').map { |i| i.to_i }
wind = gets.chomp
tx = ex - sx
ty = ey - sy
for i in 0...wind.size
    tx -= 1 if tx > 0 && wind[i] == 'E'
    tx += 1 if tx < 0 && wind[i] == 'W'
    ty -= 1 if ty > 0 && wind[i] == 'N'
    ty += 1 if ty < 0 && wind[i] == 'S'
    if tx == 0 and ty == 0
        puts i + 1
        break
    end
end
if tx != 0 or ty != 0
    puts -1
end

def getdist(x1,y1,x2,y2)
	v1=(x1-x2).abs;
	v2=(y1-y2).abs;
	return Math.sqrt(v1**2+v2**2);
end

def main()
	inp=gets.chomp.strip.to_s.split(" ",5).map{|i| i.to_i};
	t=inp[0];
	sx=inp[1];
	sy=inp[2];
	ex=inp[3];
	ey=inp[4];
	wind=gets.chomp.strip.to_s;
	ix=sx;
	f=0;
	iy=sy;
	for i in 0..t-1
		wd=wind[i].chr;
		if wd=="E"
			if getdist(ix,iy,ex,ey)>getdist(ix+1,iy,ex,ey)
				ix=ix+1;
				iy=iy;
			end
		elsif wd=="S"
			if getdist(ix,iy,ex,ey)>getdist(ix,iy-1,ex,ey)
				ix=ix;
				iy=iy-1;
			end

		elsif wd=="N"
			if getdist(ix,iy,ex,ey)>getdist(ix,iy+1,ex,ey)
				ix=ix;
				iy=iy+1;
			end

		elsif wd=="W"
			if getdist(ix,iy,ex,ey)>getdist(ix-1,iy,ex,ey)
				ix=ix-1;
				iy=iy;
			end

		end
		if ix==ex and iy==ey
			f=1;
			puts i+1;
			break;
		end
	end

	if f==0
		puts "-1";
	end
end

main()

A = gets.chomp.split(' ').map{|c| c.to_i}
B = gets.chomp
y = A[3]-A[1]
x = A[4]-A[2]
for c in 0..(B.length-1)
	x -= 1 if x > 0 and B[c] == 'N'
	x += 1 if x < 0 and B[c] == 'S'
	y -= 1 if y > 0 and B[c] == 'E'
	y += 1 if y < 0 and B[c] == 'W'
	# puts "#{B[c]}: #{x} , #{y}"
	if x == 0 and y == 0
		puts c+1
		exit
	end
end
puts -1

_, x0, y0, x, y = gets.split.map(&:to_i)
x -= x0
y -= y0
gets.chomp.chars.each_with_index do |e, i|
  case e
  when 'E'
    x -= 1 if x > 0
  when 'S'
    y += 1 if y < 0
  when 'W'
    x += 1 if x < 0
  when 'N'
    y -= 1 if y > 0
  end
  if x == 0 && y == 0
    p i + 1
    exit
  end
end
p -1

n, sx, sy, ex, ey = gets.split(' ').map { |i| i.to_i }
wind = gets.chomp
tx = ex - sx
ty = ey - sy
for i in 0...wind.size
    tx -= 1 if tx > 0 && wind[i] == 'E'
    tx += 1 if tx < 0 && wind[i] == 'W'
    ty -= 1 if ty > 0 && wind[i] == 'N'
    tx += 1 if ty < 0 && wind[i] == 'S'
    if tx == 0 and ty == 0
        puts i + 1
        break
    end
end
if tx != 0 or ty != 0
    puts -1
end


n,m,s,f=gets.split.map(&:to_i)
q=''
p=1
if s < f
  m.times do
    t,l,r=gets.split.map(&:to_i)
    u=t-p
    u+=1 if !((l-1..r)===s+u)
    u=f-s if s+u>f
    q<<'R'*u
    s+=u
    break if s==f
    q<<'X'*(t+1-p-u)
    p=t+1
  end
  q<<'R'*(f-s) if s<f
else
  m.times do
    t,l,r=gets.split.map(&:to_i)
    u=t-p
    u+=1 if !((l..r+1)===s-u)
    u=s-f if s-u<f
    q<<'L'*u
    s-=u
    break if s==f
    q<<'X'*(t+1-p-u)
    p=t+1
  end
  q<<'L'*(s-f) if f<s
end
puts q

n,m,s,f=gets.split.map(&:to_i)
q=''
p=1
c,d='R',1
c,d='L',-1 if s>f
m.times do
  t,l,r=gets.split.map(&:to_i)
  u=t-p
  if s < f
    u+=1 if !((l-1..r)===s+u)
    u=f-s if s+u>f
  else
    u+=1 if !((l..r+1)===s-u)
    u=s-f if s-u<f
  end
  q<<c*u
  s+=u*d
  break if s==f
  q<<'X'*(t+1-p-u)
  p=t+1
end
q<<c*(s-f).abs if f!=s
puts q

n,m,s,f = gets.split(" ").map(&:to_i)
steps = {}
m.times do
	t,l,r = gets.split(" ").map(&:to_i)
	steps[t] = [l,r]
end
if s < f
	direction = 1
	val = "R"
else
	direction = -1
	val = "L"
end
curr = s
ans = []
i = 1
while true
	can_reach = true
	next_spy = curr + direction
	ss = steps[i]
	if ss
		range = (ss[0]..ss[1])
		# puts "#{i} #{range.include?(curr) || range.include?(next_spy)}"
		can_reach = false if range.include?(curr) || range.include?(next_spy)
	end
	# puts "can_reach #{can_reach} curr #{curr} next_spy #{next_spy}"
	if can_reach
		ans << val
		curr = next_spy
	else
		ans << "X"
	end
	i += 1
	break if curr == f
end
puts ans.join("")

n,m,s,f = gets.split(" ").map(&:to_i)
steps = {}
m.times do
	t,l,r = gets.split(" ").map(&:to_i)
	steps[l] = [l,r]
end
if s < f
	direction = 1
	val = "R"
else
	direction = -1
	val = "L"
end
curr = s
ans = []
(1..100000).each do |i|
	can_reach = true
	next_spy = curr + direction
	# puts curr
	ss = steps[i]
	if ss
		range = (ss[0]..ss[1])
		can_reach = false if range.include?(curr) || range.include?(next_spy)
	end
	if can_reach
		ans << val
		curr = next_spy
	else
		ans << "X"
	end
	break if curr == f
end
puts ans.join("")

n,m,s,f = gets.split(" ").map(&:to_i)
steps = {}
m.times do
	t,l,r = gets.split(" ").map(&:to_i)
	steps[t] = [l,r]
end
if s < f
	direction = 1
	val = "R"
else
	direction = -1
	val = "L"
end
curr = s
ans = []
(1..100000).each do |i|
	can_reach = true
	next_spy = curr + direction
	ss = steps[i]
	if ss
		range = (ss[0]..ss[1])
		# puts "#{i} #{range.include?(curr) || range.include?(next_spy)}"
		can_reach = false if range.include?(curr) || range.include?(next_spy)
	end
	# puts "can_reach #{can_reach} curr #{curr} next_spy #{next_spy}"
	if can_reach
		ans << val
		curr = next_spy
	else
		ans << "X"
	end
	break if curr == f
end
puts ans.join("")

n,m,s,f=gets.split.map(&:to_i)
q=''
p=1
m.times do
  t,l,r=gets.split.map(&:to_i)
  u=t-p
  u+=1 if !((l-1..r)===s+u)
  u=f-s if s+u>f
  u=0 if u < 0
  q<<'R'*u
  s+=u
  break if s==f
  q<<'X'*(t+1-p-u)
  p=t+1
end
q<<'R'*(f-s) if s<f
puts q

n,m,s,f=gets.split.map(&:to_i)
q=''
m.times do
  t,l,r=gets.split.map(&:to_i)
  if (l..r)===s || (l..r)===s+1
    q<<'X'
  else
    q<<'R'
    s+=1
    break if s==f
  end
end
puts q


class Region
  attr_reader(:n, :k, :queries)
  def initialize(n, k, queries)
    @n = n
    @k = k
    @queries = queries.map{|x| x.split("")}
    @locations = nil
    @xs = nil
    @ys = nil
    @minx = nil
    @miny = nil
    @maxy = nil
    @maxx = nil
  end

  def _locations
    (0..n - 1).map{|i| (0..k - 1).map{|j| [i, j]}}
      .flatten(1)
      .select{|i, j| queries[i][j] == 'X'}
  end

  def locations
    if(@locations.nil?)
      @locations = _locations
    else
      @locations
    end
  end

  def _xs
    locations.map{|y, x| x}
  end

  def xs
    if(@xs.nil?)
      @xs = _xs
    else
      @xs
    end
  end

  def _ys
    locations.map{|y, x| y}
  end

  def ys
    @ys ||= _ys
  end

  def minx
    @minx ||= xs.min
  end

  def maxx
    @maxx ||= xs.max
  end

  def miny
    @miny ||= ys.min
  end

  def maxy
    @maxy ||= ys.max
  end

  def bounds
    [
      minx,
      maxx,
      miny,
      maxy
    ]
  end

  def legitimations
    (miny..maxy).map do |y|
      (minx..maxx).map do |x|
        queries[y][x] == 'X'
      end
    end
  end

  def satisfied?
    legitimations.flatten.all?{|x| x}
  end

  def result
    return "YES" if satisfied?
    return "NO"
  end

end

def main
  n, k = gets.split(" ").map(&:to_i)
  xs = (0..n-1).map{ gets }
  r = Region.new(n, k, xs)
  puts r.result
end

main

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

piece = n.times.map{gets.chomp}
ym = piece.index{|x| x.include? 'X'}
yM = piece.rindex{|x| x.include? 'X'}
xm = piece.map{|x| x.index('X') || 1000}.min
xM = piece.map{|x| x.rindex('X') || -1000}.max

#p ym,yM,xm,xM
puts (ym..yM).all?{|y| (xm..xM).all? {|x| piece[y][x] == 'X'}} ? "YES" : "NO"

puts ($<.read.split.select{|e|e.include? ?X}.uniq.size==1 ? :YES : :NO)


hobbits_num = gets.to_i
num_of_days = ((-1 + Math.sqrt(1 + 8 * hobbits_num)) / 2).floor + 1
invited_hobbits = (num_of_days * (num_of_days - 1)) / 2
list_length = num_of_days - 1
array = Array.new(num_of_days) {Array.new(list_length, 0)}

array[0][0] = 1
num_of_days.times do |i|
  list_length.times do |j|
    if i == j
      array[i][j] = array[i - 1][list_length - 1] + 1 if i > 0
    elsif i < j
      array[i][j] = array[i][j - 1] + 1
    else
      array[i][j] = array[j][i - 1]
    end
  end
end

puts num_of_days
array.each do |e|
  e.each do |element|
    printf("%d ", element)
  end
  printf("\n");
end

n = gets.to_i
k = ((1 + Math.sqrt(1 + 8 * n)) / 2).to_i
n_ = k * (k - 1) / 2

d1 = 1
d2 = 2

a = Array.new(k + 1, "")

for i in 1 .. n_
	a[d1] += i.to_s + " "
	a[d2] += i.to_s + " "
	d2 += 1
	if d2 > k then
		d1 += 1
		d2 = d1 + 1
	end
end

puts k
for i in 1 .. k
	puts a[i]
end

n = gets.to_i

prev = [[1, 2], [1, 3], [2, 3]]
b = 3
while true
  cur = []
  tail = []
  prev.each do |a|
    b += 1
    tail << b
    cur << (a + [b])
  end
  cur << tail
  if b > n
    break
  end
  prev = cur
end

p prev.size
prev.each{|x| puts x*" "}

n = gets.to_i
a = []
i, j = 1, 1
while n >= i
 i.times{ |k|
  a[i] ||= []
  a[k] ||= []
  a[i] << j
  a[k] << j
  j += 1
 }
 n -= i
 i += 1
end
p a.length
a.each{ |x| puts x*" " }

hobbits_num = gets.to_i
num_of_days = ((-1 + Math.sqrt(1 + 8 * hobbits_num)) / 2).floor + 1
invited_hobbits = (num_of_days * (num_of_days - 1)) / 2
list_length = num_of_days - 1
array = Array.new(num_of_days) {Array.new(list_length, 0)}
visit = Array.new(invited_hobbits, 0)
def next_visit(start, last, visit)
  for i in start...last
    if visit[i] == 0
      visit[i] = 1
      return i
    end
  end
end

num_of_days.times do |n|
  for i in 0...n
    array[n][i] = array[i][n - 1]
  end
  for i in n...list_length
    array[n][i] = next_visit i, invited_hobbits, visit
  end
end
array.each do |e|
  e.each do |element|
    printf("%d ", element + 1)
  end
  printf("\n");
end

n = gets.to_i
k = ((1 + Math.sqrt(1 + 8 * n)) / 2).to_i
n_ = k * (k - 1) / 2

d1 = 1
d2 = 2

a = Array.new(k + 1, "")

for i in 1 .. n_
	a[d1] += i.to_s + " "
	a[d2] += i.to_s + " "
	d2 += 1
	if d2 > k then
		d1 += 1
		d2 = d1 + 1
	end
end

for i in 1 .. k
	puts a[i]
end


h = Hash.new{[]}
n = gets.to_i
gets.split.map(&:to_i).each_with_index do |i, k|
    h[i] <<= k
end

a, b = 0, 0
gets
gets.split.map(&:to_i).each do |i|
    k = h[i]
    a += k[0] + 1
    b += n - k[-1]
end

puts "#{a} #{b}"

def compare_solutions(n,a,q,s)
  v_count = 0
  p_count = 0

  h_elements = Hash.new(0)
  # Creating a hash with element as symbol and value as index (as they can't be repeated cause
  # it could change the final couting for both team members)
  (1..n).each do |i|
    h_elements[a[i-1]] = i-1
  end

  queries = Hash.new(0)
  # Simplifying the amount of queries grouping them because the can be repeated
  s.each do |e|
    queries[e] +=1
  end

  queries.each do |k,v|
    e_index = h_elements[k]
    # vasya's count starting from the begining of the array and multiplied by v times the query is repeated
    v_count += (e_index + 1) * v
    # petya's count from the end of the array and multiplied by v times the query is repeated
    p_count += (n - e_index) * v
  end

  puts "#{v_count} #{p_count}"
end

n = gets.to_i
a_elements = gets.split(/\s/)
q_queries = gets.to_i
queries = gets.split(/\s/)
compare_solutions(n,a_elements,q_queries,queries)

h = Hash.new{[]}
n = gets.to_i
gets.split.map(&:to_i).each_with_index { |el, i| h[el] <<= i }
l,r = 0,0
gets
gets.split.map(&:to_i).each { |x|
  k = h[x]
  l += k[0]+1
  r += n-k[-1]
}
puts "#{l} #{r}"

n=gets.to_i
a=gets.split.map &:to_i
p=[0]*(n+1)
n.times{|i|p[a[i]]=i}
z=0
m=gets.to_i
gets.split.map(&:to_i).each{|i|z+=p[i]}
puts [z+m,m*n-z]*' '

n = gets.to_i
pos = Hash.new
tmp = 1
gets.split.each do |a|
    pos[a.to_i]=tmp
    tmp += 1
end
tmp = 0
m = gets.to_i
gets.split.each do |b|
    tmp+=pos[b.to_i]
end
print tmp, " ", (n+1)*m-tmp, "\n"

n = gets.to_i
i = -1
ar = Array.new(n){0}
gets.split.map(&:to_i).each_with_index do |el, index|
  ar[el] = index+1
end
m = gets.to_i
b = gets.split.map &:to_i

sum1,sum2 = 0, 0
b.each do |b|
  sum1 += ar[b]
  sum2 += n - ar[b] + 1
end

puts "#{sum1} #{sum2}"


n = gets.strip.to_i
ranks = {}
n.times do |i|
    scores = gets.strip.split.map(&:to_i).inject(:+)
    ranks[i + 1] = scores
end
ranks = ranks.sort_by { |k, v| [-v, k] }
ans = 0
ranks.each do |rank|
    ans += 1
    break if rank[0] == 1
end
puts ans

N = gets.to_i
arr = []
(1..N).each do |i|
    sum = gets.split.map(&:to_i).inject(:+)
    arr.push([-sum, i])
end

arr.sort!
N.times do |i|
    if arr[i][1] == 1
        puts i+1
        exit
    end
end

n = gets.to_i
tom = gets.split.map(&:to_i).inject(:+)
ans = 1
(n-1).times do
  sc = gets.split.map(&:to_i).inject(:+)
  ans += 1 if sc > tom
end
puts ans

n=gets.to_i
puts (1..n).map{|i| [-gets.split.map(&:to_i).inject(:+), i]}.sort.index{|_,i|i==1}+1

n = gets.to_i
a = []
n.times{a << gets.split.map{|x| x.to_i}.inject(0){|s, x| s + x}}
s = a[0]
puts(a.sort.reverse.index(s) + 1)

n = gets.to_i
arr = []
n.times do |i|
  arr.push [i+1]+gets.chomp.split(" ").map(&:to_i)
end

arr.sort_by!{|x| [-x[1..-1].inject(:+),x[0]]}

arr.each_with_index do |x,i|
  if x[0]==1
    puts i+1
    exit
  end
end