After the lessons n groups of schoolchildren went outside and decided to visit Polycarpus to celebrate his birthday. We know that the i-th group consists of si friends (1 ≤ si ≤ 4), and they want to go to Polycarpus together. They decided to get there by taxi. Each car can carry at most four passengers. What minimum number of cars will the children need if all members of each group should ride in the same taxi (but one taxi can take more than one group)?

The first line contains integer n (1 ≤ n ≤ 105) — the number of groups of schoolchildren. The second line contains a sequence of integers s1, s2, ..., sn (1 ≤ si ≤ 4). The integers are separated by a space, si is the number of children in the i-th group.

Print the single number — the minimum number of taxis necessary to drive all children to Polycarpus.

NoteIn the first test we can sort the children into four cars like this:  the third group (consisting of four children),  the fourth group (consisting of three children),  the fifth group (consisting of three children),  the first and the second group (consisting of one and two children, correspondingly). There are other ways to sort the groups into four cars.

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

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

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

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

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

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

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)

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

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

# 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

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

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

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

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

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

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

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

# 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

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

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

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

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

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

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

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

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

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

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

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

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

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


She is skilled in all kinds of magics, and is keen on inventing new one.—Perfect Memento in Strict SensePatchouli is making a magical talisman. She initially has $$$n$$$ magical tokens. Their magical power can be represented with positive integers $$$a_1, a_2, \ldots, a_n$$$. Patchouli may perform the following two operations on the tokens.  Fusion: Patchouli chooses two tokens, removes them, and creates a new token with magical power equal to the sum of the two chosen tokens.  Reduction: Patchouli chooses a token with an even value of magical power $$$x$$$, removes it and creates a new token with magical power equal to $$$\frac{x}{2}$$$. Tokens are more effective when their magical powers are odd values. Please help Patchouli to find the minimum number of operations she needs to make magical powers of all tokens odd values.

Each test contains multiple test cases.  The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^3$$$) — the number of test cases. The description of the test cases follows. For each test case, the first line contains one integer $$$n$$$ ($$$1 \leq n\leq 2\cdot 10^5$$$) — the initial number of tokens. The second line contains $$$n$$$ intergers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the initial magical power of the $$$n$$$ tokens. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print a single integer — the minimum number of operations Patchouli needs to make all tokens have an odd value of magical power. It can be shown that under such restrictions the required sequence of operations exists.

NoteTest case 1:$$$a$$$ consists solely of odd numbers initially.Test case 2:Choose the tokens with magical power of $$$1$$$ and $$$2$$$ and perform Fusion. Now $$$a=[1,3]$$$, both are odd numbers.Test case 3:Choose the tokens with magical power of $$$2$$$ and $$$8$$$ and perform Fusion. Now $$$a=[4,10]$$$.Choose the token with magical power of $$$10$$$ and perform Reduction. Now $$$a=[4,5]$$$.Choose the tokens with magical power of $$$4$$$ and $$$5$$$ and perform Fusion. Now $$$a=[9]$$$, and $$$9$$$ is an odd number.It can be shown that you can not make all the magical powers odd numbers in less than $$$3$$$ moves, so the answer is $$$3$$$.

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


Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.  Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.  Help Shaass to find the minimum total thickness of the vertical books that we can achieve.

The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).

On the only line of the output print the minimum total thickness of the vertical books that we can achieve.

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


Mark is asked to take a group photo of $$$2n$$$ people. The $$$i$$$-th person has height $$$h_i$$$ units.To do so, he ordered these people into two rows, the front row and the back row, each consisting of $$$n$$$ people. However, to ensure that everyone is seen properly, the $$$j$$$-th person of the back row must be at least $$$x$$$ units taller than the $$$j$$$-th person of the front row for each $$$j$$$ between $$$1$$$ and $$$n$$$, inclusive.Help Mark determine if this is possible.

The first line contains one integer $$$t$$$ ($$$1\leq t\leq 100$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two positive integers $$$n$$$ and $$$x$$$ ($$$1\leq n\leq 100$$$, $$$1\leq x\leq 10^3$$$) — the number of people in each row and the minimum difference Mark wants. The second line of each test case contains $$$2n$$$ positive integers $$$h_1,h_2,\ldots,h_{2n}$$$ ($$$1\leq h_i\leq 10^3$$$) — the height of each person in units. Note that the sum of $$$n$$$ over all test cases is not bounded.

For each test case, print a single line containing "YES" if Mark could arrange people satisfying his condition and "NO" otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answers).

NoteIn the first test case, one possible order is to have the third, fifth, and sixth person on the back row and the second, first, and fourth on the front row. The heights of the people will look like this. Back$$$9$$$$$$12$$$$$$16$$$Front$$$3$$$$$$1$$$$$$10$$$ It works because   $$$h_3-h_2 = 9-3 \geq 6$$$,    $$$h_5-h_1 = 12-1\geq 6$$$, and    $$$h_6-h_4 = 16-10\geq 6$$$. In the second test case, it can be shown there is no way to order people in a way that satisfies the condition.In the third test case, the only way to arrange people to satisfy the condition is to have the first person on the back row and the second person on the front row.

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


Imagine that there is a group of three friends: A, B and С. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.

The first line contains two integers n and m (1 ≤ n ≤ 100; 0 ≤ m ≤ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 ≤ ai, bi ≤ n; ai ≠ bi; 1 ≤ ci ≤ 100), which mean that person ai owes person bi ci rubles. Assume that the people are numbered by integers from 1 to n. It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).

Print a single integer — the minimum sum of debts in the optimal rearrangement.

NoteIn the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.In the second sample, there are no debts.In the third sample, you can annul all the debts.

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


You're given an array $$$a$$$ of $$$n$$$ integers, such that $$$a_1 + a_2 + \cdots + a_n = 0$$$.In one operation, you can choose two different indices $$$i$$$ and $$$j$$$ ($$$1 \le i, j \le n$$$), decrement $$$a_i$$$ by one and increment $$$a_j$$$ by one. If $$$i < j$$$ this operation is free, otherwise it costs one coin.How many coins do you have to spend in order to make all elements equal to $$$0$$$?

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 5000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$)  — the number of elements. The next line contains $$$n$$$ integers $$$a_1, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$). It is given that $$$\sum_{i=1}^n a_i = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, print the minimum number of coins we have to spend in order to make all elements equal to $$$0$$$.

NotePossible strategy for the first test case:   Do $$$(i=2, j=3)$$$ three times (free), $$$a = [-3, 2, 0, 1]$$$.  Do $$$(i=2, j=1)$$$ two times (pay two coins), $$$a = [-1, 0, 0, 1]$$$.  Do $$$(i=4, j=1)$$$ one time (pay one coin), $$$a = [0, 0, 0, 0]$$$.

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}

gets.to_i.times{gets
a=c=0
b=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


You are given a ternary string (it is a string which consists only of characters '0', '1' and '2').You can swap any two adjacent (consecutive) characters '0' and '1' (i.e. replace "01" with "10" or vice versa) or any two adjacent (consecutive) characters '1' and '2' (i.e. replace "12" with "21" or vice versa).For example, for string "010210" we can perform the following moves:   "010210" $$$\rightarrow$$$ "100210";  "010210" $$$\rightarrow$$$ "001210";  "010210" $$$\rightarrow$$$ "010120";  "010210" $$$\rightarrow$$$ "010201". Note than you cannot swap "02" $$$\rightarrow$$$ "20" and vice versa. You cannot perform any other operations with the given string excluding described above.You task is to obtain the minimum possible (lexicographically) string by using these swaps arbitrary number of times (possibly, zero).String $$$a$$$ is lexicographically less than string $$$b$$$ (if strings $$$a$$$ and $$$b$$$ have the same length) if there exists some position $$$i$$$ ($$$1 \le i \le |a|$$$, where $$$|s|$$$ is the length of the string $$$s$$$) such that for every $$$j < i$$$ holds $$$a_j = b_j$$$, and $$$a_i < b_i$$$.

The first line of the input contains the string $$$s$$$ consisting only of characters '0', '1' and '2', its length is between $$$1$$$ and $$$10^5$$$ (inclusive).

Print a single string — the minimum possible (lexicographically) string you can obtain by using the swaps described above arbitrary number of times (possibly, zero).

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

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


You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make  as large as possible, where  denotes the bitwise OR. Find the maximum possible value of  after performing at most k operations optimally.

The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).

Output the maximum value of a bitwise OR of sequence elements after performing operations.

NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.

class Solver
	def main
		n, k, x = gets.split.map { |s| s.to_i }
		a = gets.split.map { |s| s.to_i }
		a.unshift nil
		x = x ** k

		prefix_or = Array.new(n + 2)
		suffix_or = Array.new(n + 2)
		prefix_or[0] = suffix_or[n + 1] = 0
		1.upto(n) do |i|
			prefix_or[i] = (prefix_or[i - 1] | a[i])
		end
		n.downto(1) do |i|
			suffix_or[i] = (suffix_or[i + 1] | a[i])
		end

		ans = 0
		1.upto(n) do |i|
			ans = [ans, (a[i] * x) | prefix_or[i - 1] | suffix_or[i + 1]].max
		end
		puts ans
	end
end

Solver.new.main


Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simultaneously. There are two states for each skewer: initial and turned over.This time Miroslav laid out $$$n$$$ skewers parallel to each other, and enumerated them with consecutive integers from $$$1$$$ to $$$n$$$ in order from left to right. For better cooking, he puts them quite close to each other, so when he turns skewer number $$$i$$$, it leads to turning $$$k$$$ closest skewers from each side of the skewer $$$i$$$, that is, skewers number $$$i - k$$$, $$$i - k + 1$$$, ..., $$$i - 1$$$, $$$i + 1$$$, ..., $$$i + k - 1$$$, $$$i + k$$$ (if they exist). For example, let $$$n = 6$$$ and $$$k = 1$$$. When Miroslav turns skewer number $$$3$$$, then skewers with numbers $$$2$$$, $$$3$$$, and $$$4$$$ will come up turned over. If after that he turns skewer number $$$1$$$, then skewers number $$$1$$$, $$$3$$$, and $$$4$$$ will be turned over, while skewer number $$$2$$$ will be in the initial position (because it is turned again).As we said before, the art of cooking requires perfect timing, so Miroslav wants to turn over all $$$n$$$ skewers with the minimal possible number of actions. For example, for the above example $$$n = 6$$$ and $$$k = 1$$$, two turnings are sufficient: he can turn over skewers number $$$2$$$ and $$$5$$$.Help Miroslav turn over all $$$n$$$ skewers.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 1000$$$, $$$0 \leq k \leq 1000$$$) — the number of skewers and the number of skewers from each side that are turned in one step.

The first line should contain integer $$$l$$$ — the minimum number of actions needed by Miroslav to turn over all $$$n$$$ skewers. After than print $$$l$$$ integers from $$$1$$$ to $$$n$$$ denoting the number of the skewer that is to be turned over at the corresponding step.

NoteIn the first example the first operation turns over skewers $$$1$$$, $$$2$$$ and $$$3$$$, the second operation turns over skewers $$$4$$$, $$$5$$$, $$$6$$$ and $$$7$$$.In the second example it is also correct to turn over skewers $$$2$$$ and $$$5$$$, but turning skewers $$$2$$$ and $$$4$$$, or $$$1$$$ and $$$5$$$ are incorrect solutions because the skewer $$$3$$$ is in the initial state after these operations.

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)


Ksusha the Squirrel is standing at the beginning of a straight road, divided into n sectors. The sectors are numbered 1 to n, from left to right. Initially, Ksusha stands in sector 1. Ksusha wants to walk to the end of the road, that is, get to sector n. Unfortunately, there are some rocks on the road. We know that Ksusha hates rocks, so she doesn't want to stand in sectors that have rocks.Ksusha the squirrel keeps fit. She can jump from sector i to any of the sectors i + 1, i + 2, ..., i + k. Help Ksusha! Given the road description, say if she can reach the end of the road (note, she cannot stand on a rock)?

The first line contains two integers n and k (2 ≤ n ≤ 3·105, 1 ≤ k ≤ 3·105). The next line contains n characters — the description of the road: the i-th character equals ".", if the i-th sector contains no rocks. Otherwise, it equals "#". It is guaranteed that the first and the last characters equal ".".

Print "YES" (without the quotes) if Ksusha can reach the end of the road, otherwise print "NO" (without the quotes).

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 &:to_i
puts gets['#'*k]?'NO':'YES'

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


In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment ti and needs to be processed for di units of time. All ti are guaranteed to be distinct.When a query appears server may react in three possible ways:   If server is free and query queue is empty, then server immediately starts to process this query.  If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue.  If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed. As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.

The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue. Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers ti and di (1 ≤ ti, di ≤ 109), where ti is the moment of time when the i-th query appears and di is the time server needs to process it. It is guaranteed that ti - 1 < ti for all i > 1.

Print the sequence of n integers e1, e2, ..., en, where ei is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or  - 1 if the corresponding query will be rejected.

NoteConsider the first sample.   The server will start to process first query at the moment 2 and will finish to process it at the moment 11.  At the moment 4 second query appears and proceeds to the queue.  At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected.  At the moment 11 server will finish to process first query and will take the second query from the queue.  At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue.  At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there.  Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue.  Server finishes to process query number 5 at the moment 22.

n, stack_count = gets.split.map(&:to_i)
time = 0
stack = []
res = []
if n<=stack_count
  n.times do |i|
    t_request, p_request  = gets.split.map(&:to_i)
    if i == 0
      time+=(t_request+p_request) if i==0
    else
      if t_request <= time
        time+=p_request
      else
        time = t_request+ p_request
      end
    end
    res[i]=time
  end
else
n.times do |i|
  t_request, p_request  = gets.split.map(&:to_i)
  if stack == []
    if t_request >= time
      time = t_request + p_request
      res[i]=time
    else
      if stack.count < stack_count
      stack.push([t_request,p_request,i])
      else
        res[i] = -1
      end
    end
  else
    if t_request >= time
      if stack[0][0] <= time
        time+=stack[0][1]
      else
        time = stack[0][0] + stack[0][1]
      end
      res[stack[0][2]] = time
      stack.delete_at(0)
      if stack.count < stack_count
        stack.push([t_request,p_request,i])
      else
        res[i] = -1
      end
    else
      if stack.count < stack_count
        stack.push([t_request,p_request,i])
      else
        res[i] = -1
      end
    end
  end
end
i = 0
while stack != []
  if stack[0][0] <= time
    time+=stack[0][1]
  else
    time = stack[0][0] + stack[0][1]
  end
  res[stack[0][2]] = time
  stack.delete_at(0)
  i+=1
end
end
puts res.join(' ')

n, stack_count = gets.split.map(&:to_i)
time = 0
stack = []
res = []
n.times do |i|
  t_request, p_request  = gets.split.map(&:to_i)
  if stack == []
    if t_request >= time
      time = t_request + p_request
      res[i]=time
    else
      if stack.count < stack_count
      stack.push([t_request,p_request,i])
      else
        res[i] = -1
      end
    end
  else
    if t_request >= time
      if stack[0][0] <= time
        time+=stack[0][1]
      else
        time = stack[0][0] + stack[0][1]
      end
      res[stack[0][2]] = time
      stack.delete_at(0)
      if stack.count < stack_count
        stack.push([t_request,p_request,i])
      else
        res[i] = -1
      end
    else
      if stack.count < stack_count
        stack.push([t_request,p_request,i])
      else
        res[i] = -1
      end
    end
  end
end
i = 0
while stack != []
  time+=stack[0][1]
  res[stack[0][2]] = time
  stack.delete_at(0)
  i+=1
end

puts res.join(' ')

n, stack_count = gets.split.map(&:to_i)
time = 0
stack = []
res = []
n.times do |i|
  t_request, p_request  = gets.split.map(&:to_i)
  if stack == []
    if t_request >= time
      time = t_request + p_request
      res[i]=time
    else
      if stack.count < stack_count
      stack.push([t_request,p_request,i])
      else
        res[i] = -1
      end
    end
  else
    if t_request >= time
      if stack[0][0] <= time
        time+=stack[0][1]
      else
        time = stack[0][0] + stack[0][1]
      end
      res[stack[0][2]] = time
      stack.delete_at(0)
      if stack.count < stack_count
        stack.push([t_request,p_request,i])
      else
        res[i] = -1
      end
    else
      if stack.count < stack_count
        stack.push([t_request,p_request,i])
      else
        res[i] = -1
      end
    end
  end
end
i = 0
while stack != []
  if stack[0][0] <= time
    time+=stack[0][1]
  else
    time = stack[0][0] + stack[0][1]
  end
  res[stack[0][2]] = time
  stack.delete_at(0)
  i+=1
end

n, stack_count = gets.split.map(&:to_i)
time = 0
stack = []
res = []
if n<=stack_count
  n.times do |i|
    t_request, p_request  = gets.split.map(&:to_i)
    if i == 0
      time+=(t_request+p_request) if i==0
    else
      time+=p_request
    end
    res[i]=time
  end
else
n.times do |i|
  t_request, p_request  = gets.split.map(&:to_i)
  if stack == []
    if t_request >= time
      time = t_request + p_request
      res[i]=time
    else
      if stack.count < stack_count
      stack.push([t_request,p_request,i])
      else
        res[i] = -1
      end
    end
  else
    if t_request >= time
      if stack[0][0] <= time
        time+=stack[0][1]
      else
        time = stack[0][0] + stack[0][1]
      end
      res[stack[0][2]] = time
      stack.delete_at(0)
      if stack.count < stack_count
        stack.push([t_request,p_request,i])
      else
        res[i] = -1
      end
    else
      if stack.count < stack_count
        stack.push([t_request,p_request,i])
      else
        res[i] = -1
      end
    end
  end
end
i = 0
while stack != []
  if stack[0][0] <= time
    time+=stack[0][1]
  else
    time = stack[0][0] + stack[0][1]
  end
  res[stack[0][2]] = time
  stack.delete_at(0)
  i+=1
end
end
puts res.join(' ')

n, stack_count = gets.split.map(&:to_i)
time = 0
stack = []
res = []
n.times do |i|
  t_request, p_request  = gets.split.map(&:to_i)
  if stack == []
    if t_request >= time
      time = t_request + p_request
      res[i]=time
    else
      if stack.count < stack_count
      stack.push([t_request,p_request,i])
      else
        res[i] = -1
      end
    end
  else
    if t_request >= time
      time+=stack[0][1]
      res[stack[0][2]] = time
      stack.delete_at(0)
      if stack.count < stack_count
        stack.push([t_request,p_request,i])
      else
        res[i] = -1
      end
    else
      if stack.count < stack_count
        stack.push([t_request,p_request,i])
      else
        res[i] = -1
      end
    end
  end
end
i = 0
while stack != []
  time+=stack[0][1]
  res[stack[0][2]] = time
  stack.delete_at(0)
  i+=1
end

puts res.join(' ')