You are given two integers $$$x$$$ and $$$y$$$. You want to choose two strictly positive (greater than zero) integers $$$a$$$ and $$$b$$$, and then apply the following operation to $$$x$$$ exactly $$$a$$$ times: replace $$$x$$$ with $$$b \cdot x$$$.You want to find two positive integers $$$a$$$ and $$$b$$$ such that $$$x$$$ becomes equal to $$$y$$$ after this process. If there are multiple possible pairs, you can choose any of them. If there is no such pair, report it.For example:   if $$$x = 3$$$ and $$$y = 75$$$, you may choose $$$a = 2$$$ and $$$b = 5$$$, so that $$$x$$$ becomes equal to $$$3 \cdot 5 \cdot 5 = 75$$$;  if $$$x = 100$$$ and $$$y = 100$$$, you may choose $$$a = 3$$$ and $$$b = 1$$$, so that $$$x$$$ becomes equal to $$$100 \cdot 1 \cdot 1 \cdot 1 = 100$$$;  if $$$x = 42$$$ and $$$y = 13$$$, there is no answer since you cannot decrease $$$x$$$ with the given operations.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of one line containing two integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 100$$$).

If it is possible to choose a pair of positive integers $$$a$$$ and $$$b$$$ so that $$$x$$$ becomes $$$y$$$ after the aforementioned process, print these two integers. The integers you print should be not less than $$$1$$$ and not greater than $$$10^9$$$ (it can be shown that if the answer exists, there is a pair of integers $$$a$$$ and $$$b$$$ meeting these constraints). If there are multiple such pairs, print any of them. If it is impossible to choose a pair of integers $$$a$$$ and $$$b$$$ so that $$$x$$$ becomes $$$y$$$, print the integer $$$0$$$ twice.

def try(i, y)
  cnt = 0
  while y % i == 0
    cnt += 1
    y /= i
  end

  return nil unless y == 1
  cnt
end

gets.to_i.times do
  x, y = gets.split.map(&:to_i)

  if y % x != 0
    puts '0 0'
    next
  end

  y /= x

  if y == 1
    puts '1 1'
    next
  end

  i = 2
  found = false
  while i <= y
    j = try(i, y)
    if j
      puts "#{j} #{i}"
      found = true
      break
    end

    i += 1
  end

  next if found
  puts '0 0'
end

pow = (1..10).flat_map do |a|
  (1..100).map do |b|
    [b ** a, [a, b]]
  end
end.to_h

gets.to_i.times do
  x, y = gets.split.map(&:to_i)
  if y >= x && y % x == 0 && pow[y / x]
    puts pow[y / x].join(" ")
  else
    puts "0 0"
  end
end

for t in 1..gets.to_i do
	a=gets.split
	a[0]=a[0].to_i
	a[1]=a[1].to_i
	c=a[0]
	d=a[1]

	if d%c==0
		print 1,' ',d.div(c),"\n"
	else
		print 0,' ',0,"\n"
	end
end

t=gets.chomp.to_i
t.times do
    s= gets.chomp.split
    x=s[0].to_i
    y=s[1].to_i
    if (y % x ==0)
        puts "#{1} #{y/x}"
    else
        puts "#{0} #{0}"
    end
end

def try(i, y)
  cnt = 0
  while y % i == 0
    cnt += 1
    y /= i
  end

  return nil unless y == 1
  cnt
end

gets.to_i.times do
  x, y = gets.split.map(&:to_i)

  if y % x != 0
    puts '0 0'
    next
  end

  y /= x

  if y == 1
    puts '1 1'
    next
  end

  i = 2
  found = false
  while i * i <= y
    j = try(i, y)
    if j
      puts "#{j} #{i}"
      found = true
      break
    end

    i += 1
  end

  next if found
  puts '0 0'
end

pow = (1..10).flat_map do |a|
  (1..10).map do |b|
    [b ** a, [a, b]]
  end
end.to_h

gets.to_i.times do
  x, y = gets.split.map(&:to_i)
  if y >= x && y % x == 0 && pow[y / x]
    puts pow[y / x].join(" ")
  else
    puts "0 0"
  end
end


Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.Return the maximum number of rows that she can make completely clean.

The first line of input will be a single integer n (1 ≤ n ≤ 100). The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.

The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.

NoteIn the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.In the second sample, everything is already clean, so Ohana doesn't need to do anything.

n = gets.chomp.to_i
arr = Hash.new{0}
n.times do
arr[gets.chomp]+=1
end
max = 0
arr.each do |key,value|
	if value > max
	max = value
	end
end
print max

=begin
4
0101
1000
1111
0101

3
111
111
111
=end

def function
b=gets.chomp.to_i
string_array=[]
b.times do |i|
a=gets.chomp
string_array.push(a)
end
count_array=[]
0.upto string_array.length-1 do |i|
count_array.push(string_array.count(string_array[i]))
end
puts count_array.max
end
function

rows = gets.to_i
row_hash = {}
(rows).times do
    row_val = gets
    if row_hash.include?(row_val)
        row_hash[row_val]+=1
    else
        row_hash[row_val] = 1
    end
end
puts (row_hash.max_by{|k, v| v}).last

$pair = {}

def sweep(arr, row)
		str = arr[row] * ""
		if $pair.has_key?(str)
			$pair[str] += 1
		else
			$pair[str] = 1
		end
end

=begin
def count(arr, col)
	l = 0
	(0...arr[0].length).each do |iterNum|
		l += 1 if arr[iterNum][0] == 0
	end

	l
end

def cleanCount(arr)
	l = 0

	(0...arr[0].length).each do |iterNum|
		str = arr[iterNum] * ""
		l += 1 if str == ("1" * arr[0].length)
	end

	l
end
=end

n = gets.chomp.to_i
room = []
countArr = []

(0...n).each do |iter|
	room << (gets.chomp.split('').map {|item| item.to_i})
end

(0...n).each do |iter|
	countArr << sweep(room, iter)
	#print $pair
	#puts ''
end

max = 0
$pair.each_value do |val|
	max = val if max < val
end

puts max

n = gets.to_i
s = {}
n.times do |i|
  r = gets.chomp
  s[r] ||= 0
  s[r] += 1
end

puts s.values.max

n,h = gets.to_i,{}
(1..n).each{
    r = gets.strip
    h[r] ||= 0
    h[r] += 1
}
p h.values.max

n = gets.to_i
a = n.times.map{gets.chomp}
p a.uniq.reduce(0){|mx, x| mx = [mx, a.count(x)].max}

n = gets().chomp().to_i
b = []

n.times{
        b.push(gets().chomp())
}

c=b.sort{|x,y| x <=> y}

str=c[0]
max = 1
temp =1
for i in (1..c.length-1)
        if c[i]==str
                temp+=1

        else
                str=c[i]
                if temp > max
                        max = temp
                end
                temp=1
        end

end

if temp > max
        max = temp
end

puts max

n = gets().chomp().to_i
b = []

n.times{
        s = gets().chomp().split('')
        a = []
        for i in (0..n-1)
                if s[i]=='0'
                        a.push("#{i+1}")
                end
        end
        if a.length()==0
                a.push("0")
        end
        b.push(a.join(''))
}

c=b.sort{|x,y| x.length <=> y.length}

str=c[0]
max = 1
temp =1
for i in (1..c.length-1)
        if c[i]==str
                temp+=1

        else
                str=c[i]
                if temp > max
                        max = temp
                end
                temp=1
        end

end

puts max


You are given an array of n elements, you must make it a co-prime array in as few moves as possible.In each move you can insert any positive integral number you want not greater than 109 in any place in the array.An array is co-prime if any two adjacent numbers of it are co-prime.In the number theory, two integers a and b are said to be co-prime if the only positive integer that divides both of them is 1.

The first line contains integer n (1 ≤ n ≤ 1000) — the number of elements in the given array. The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.

Print integer k on the first line — the least number of elements needed to add to the array a to make it co-prime. The second line should contain n + k integers aj — the elements of the array a after adding k elements to it. Note that the new array should be co-prime, so any two adjacent values should be co-prime. Also the new array should be got from the original array a by adding k elements to it. If there are multiple answers you can print any one of them.

gets;a=gets.split.map(&:to_i);b=[a[0],*a.each_cons(2).flat_map{|x,y|x.gcd(y)==1 ? [y] : [1,y]}];p b.size-a.size;puts b*' '

def gcd(a, b)
  if b == 0
    return a
  else
    return gcd(b, a % b)
  end
end

n = readline.to_i
arr = readline.split(' ').collect(&:to_i)
ct = 0
res = []
0.upto(arr.length - 2) do |ind|
  a, b = arr[ind], arr[ind + 1]
  res += [a]
  if gcd(a, b) != 1
    ct += 1
    res += [1]
  end
end
res += [arr.last]
puts ct
res.each_with_index do |n, i|
  if i != 0
    print " "
  end
  print n
end
puts ""

a=gets.chomp.to_i
b=gets.chomp.split(" ").map{|i| i.to_i}
ans=[]
p=0
0.upto(a-2) do |i|
    if b[i].gcd(b[i+1])!=1
      ans << b[i]
      ans << 1
      p+=1
    else
        ans << b[i]
    end
end
ans << b[a-1]
puts "#{p}"
puts ans.join(" ")

def gcd(a,b)
	return b>0 ? gcd(b,a%b) : a
end

n=gets.to_i
a=gets.split(' ')
s=0
for i in 1..a.size-1
	s+=1 if gcd(a[i].to_i,a[i-1].to_i)>1
end
puts s
print a[0]
for i in 1..a.size-1
	print " 1" if gcd(a[i].to_i,a[i-1].to_i)>1
	print " "+a[i]
end
puts

a=gets.chomp.to_i
b=gets.chomp.split(" ").map{|i| i.to_i}
ans=[]
0.upto(a-2) do |i|
    if b[i].gcd(b[i+1])!=1
      ans << b[i]
      ans << 1
    else
        ans << b[i]
    end
end
ans << b[a-1]
puts ans.join(" ")

def gcd(a,b)
	return b>0 ? gcd(b,a%b) : a
end

n=gets.to_i
a=gets.split(' ')
print a[0]
for i in 1..a.size-1
	print " 1" if gcd(a[i].to_i,a[i-1].to_i)>1
	print " "+a[i]
end
puts


Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time.Help Yaroslav.

The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the array elements.

In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise.

NoteIn the first sample the initial array fits well.In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it.In the third sample Yarosav can't get the array he needs.

c = Hash.new(0)

n = gets.to_i

max = -1

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

can = max<=(n/2.0).ceil.to_i

puts can ? 'YES' : 'NO'

n=gets.chomp.to_i
n=(n+1)>>1
a=gets.chomp.split
for i in a do
	if a.count(i)>n
		puts "NO"
		exit
	end
end
puts "YES"

n = gets.to_i
cnt = [], maxs = 0
gets.split(' ').map do |i|
    i = i.to_i
    cnt[i] = cnt[i].nil? ? 1 : cnt[i] + 1
    maxs = [cnt[i], maxs].max
end
if maxs > (n + 1) / 2 && n > 1 then puts "NO"
else puts "YES"
end

n = gets.to_i
a = gets.split(' ').map { |i| i.to_i }
cnt = []
maxs = 0
a.each do |i|
    cnt[i] = 0 if cnt[i].nil?
    cnt[i] += 1
    maxs = [cnt[i], maxs].max
end
if maxs > (n + 1) / 2 && n > 1
    puts "NO"
else
    puts "YES"
end

def run
  n = $stdin.gets.to_i
  a = $stdin.gets.split.map(&:to_i)
  h = Hash.new(0)
  a.each do |item|
    h[item] += 1
  end

  h.each do |key, value|
    if (value - 1) * 2 >= n
      puts "NO"
      return
    end
  end

  puts "YES"
end

run if $0 == __FILE__

class A
  def initialize
    n = $stdin.gets.chomp.to_i
    array = $stdin.gets.chomp.split(' ').map(&:to_i)

    if array.size == 1
      puts "YES"
      exit
    elsif array.size == 2
      if array[0] == array[1]
        puts "NO"
      else
        puts "YES"
      end
      exit
    end

    if array.size.even?
      1.upto(1000) do |num|
        count = array.count(num)

        if(count > array.size/2)
          puts "NO"
          exit
        end
      end
    else
      1.upto(1000) do |num|
        count = array.count(num)

        if(count > array.size/2+1)
          puts "NO"
          exit
        end
      end
    end

    puts "YES"
  end
end

a = A.new

n = gets.to_i
a = gets.split #.map{|_|_.to_i}
h = {}

a.each{|i|
  if h[i] then
    h[i]+=1
  else
    h[i]=1
  end
}
x = n%2==0 ? n/2 : (n+1)/2
r = true
h.each{|k,v|
  r = false if v > x
}

puts (r ? 'YES':'NO' )

n = gets.chomp.to_i
elements = gets.chomp.split(/ /).map(&:to_i)
ans = "YES"
cnt = Array.new(1001, 0)
elements.each do |e|
    cnt[e] += 1
end
c = 0
cnt.each do |count|
    c = [count, c].max
end
ans = "NO" if c > (n + 1) / 2
puts ans

a=gets.chomp.to_i
b=gets.chomp.split(" ")
if a==1
puts "YES"
elsif a!=1 && b.uniq.length==1
puts "NO"

else

k=Hash.new(0)
b.each do |i|
k["#{i}"]+=1
end
l=k.values.sort.reverse[0]
if a-l>=l-1
puts "YES"
else
puts "NO"
end

end

$n, $a = gets.to_i, gets.split(' ').map { |x| x.to_i }
$b = []
$a.group_by{ |x| x }.each_value{ |x| $b << x.length }
$k = (if $a.length % 2 == 0 then $a.length / 2 else $a.length / 2 + 1 end)
puts (if $b.any? { |x| x > $k } then "NO" else "YES" end)

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
count = Hash.new(0)

arr.each do |a|
	count[a] += 1
end

ans = true
count.values.each do |c|
	unless c <= (n+1)/2
		ans = false
		break
	end
end

puts ans ? "YES" : "NO"

# count_arr = count.values.sort.reverse
# # puts "#{count_arr}"
# (0..count_arr.length-2).each do |i|
# 	j = i + 1
# 	puts "#{count_arr}"
# 	next if count_arr[i] == 0
# 	while j <= count_arr.length - 1
# 		if count_arr[i] <= count_arr[j]
# 			count_arr[i] = 0
# 			count_arr[j] -= count_arr[i]
# 			break
# 		else
# 			count_arr[i] -= count_arr[j]
# 			count_arr[j] = 0
# 		end
# 		j += 1
# 	end
# end
# count_arr.reject!{|a| a == 0}
# puts "#{count}"
# puts "#{count_arr}"
# if count_arr[0] > 1
# 	puts "NO"
# else
# 	puts "YES"
# end

#!/usr/bin/ruby -w
n = (gets.to_i + 1) >> 1
x = gets.split;
x.each do |i|
    if x.count(i) > n
        puts "NO"
        exit
    end
end
puts "YES"

def solve
	h = {}
	gets
	a = gets.chomp.split(/ /)
	a.each do |x| h[x] = if h[x] == nil then 1 else h[x] + 1 end end
	h.each do |x, y| return 'NO' if y > a.length/2 + a.length%2 end
	'YES'
end

print solve

def div_ceil(x, y)
  x / y + x % y
end

n = div_ceil(gets.chomp.to_i, 2)
puts(gets.chomp.split.map { |x| x.to_i }.inject(Hash.new(0)) { |h, e| h[e] += 1; h }.values.any? { |v| v > n } ? 'NO' : 'YES')

n = gets[0..-2].to_i
arr = gets.split(' ')
a = Array.new(n) { |i| arr[i].to_i }
mark = Array.new(1001, 0)
for i in 0...n do
  mark[a[i]] += 1
end

max = mark.max
if max * 2 - n >= 2
  print "NO"
else
  print "YES"
end

#!/usr/bin/env ruby

n = (gets.to_i + 1) >> 1
x = gets.split.collect{ |i| i.to_i }
x.each do |i|
	if x.count(i) > n
		puts "NO"
		exit
	end
end

puts "YES"

at_exit do
  n = get_numbers_from_line[0]
  numbers = get_numbers_from_line[0...n]

  iter = 0
  while true
    i = get_incorrect_number_index(numbers)
    if i.nil?
      puts "YES"
      break
    else
      unless move_number_to_correct_place(i, numbers)
        puts "NO"
        break
      end
    end
    iter += 1
    if iter > 1000
      puts "NO"
      break
    end
  end
end

def get_incorrect_number_index(numbers)
  (0...numbers.length).each do |i|
    return i if numbers[i] == numbers[i+1] || (i-1 >= 0 && numbers[i] == numbers[i-1])
  end
  nil
end

def move_number_to_correct_place(index, numbers)
  number = numbers.delete_at(index)
  (0..numbers.length).each do |i|
    if numbers[i] != number && (i-1 >= 0 && numbers[i-1] != number)
      numbers.insert i, number
      return true
    end
  end
  false
end

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

n = gets.to_i
a = gets.split.map { |x| x.to_i }
max = 0
1.upto(1000) do |i|
	max = [max, a.count(i)].max
end

if max <= (n + 1) / 2
	puts 'YES'
else
	puts 'NO'
end

c = Hash.new(0)

n = gets.to_i

max = -1

gets.split.map(&:to_i).each do |i|
	c[i]+=1
	max = i if c[i]>max
end

can = max<=(n/2.0).ceil

puts can ? 'YES' : 'NO'

n = gets.to_i
a = gets.split(' ').map { |i| i.to_i }
cnt = []
maxs = 0
a.each do |i|
	cnt[i] = 0 if cnt[i].nil?
	cnt[i] += 1
	maxs = [cnt[i], maxs].max
end
if maxs > n / 2 && n > 1
	puts "NO"
else
	puts "YES"
end

n = gets.to_i
cnt = [], maxs = 0
gets.split(' ').map do |i|
    i = i.to_i
    cnt[i] = cnt[i].nil? ? 0 : cnt[i] + 1
    maxs = [cnt[i], maxs].max
end
if maxs > (n + 1) / 2 && n > 1 then puts "NO"
else puts "YES"
end

n = gets.to_i
a = gets.split #.map{|_|_.to_i}
h = {}

a.each{|i|
  if h[i] then
    h[i]+=1
  else
    h[i]=1
  end
}
x = n%2 ? n/2 : (n+1)/2
r = true
h.each{|k,v|
  r = false if v > x
}

puts (r ? 'YES':'NO' )

n = gets.to_i
a = gets.split #.map{|_|_.to_i}
h = {}

a.each{|i|
  if h[i] then
    h[i]+=1
  else
    h[i]=0
  end
}
x = n%2 ? n/2 : (n+1)/2
r = true
h.each{|k,v|
  r = false if v > x
}

puts (r ? 'YES':'NO' )

n = gets.to_i
a = gets.split.map{|_|_.to_i}

r = true
(n-1).times{|i|
  r = true
  if a[i] == a[i+1] then
    b = a
    t = b[i+1]
    b[i+1] = b[i]
    b[i] = t
    (n-1).times{|j|
      r = false if b[j] == b[j+1]
    }
  end
}

puts (r ? 'YES':'NO' )

def distinct?( elements )
    (1...elements.length).each do |i|
        return false if elements[i - 1] == elements[i]
    end
    true
end
n = gets.chomp.to_i
elements = gets.chomp.split(/ /).map(&:to_i)
ans = "YES"
(1...n).each do |i|
    elements[i - 1], elements[i] = elements[i], elements[i - 1] if elements[i - 1] != elements[i]
end
ans = "NO" if !distinct? elements
puts ans

n = gets.chomp.to_i
elements = gets.chomp.split(/ /).map(&:to_i)
duplicates = []
(1...n).each do |i|
    duplicates += [i - 1, i] if elements[i - 1] == elements[i]
end
duplicates.uniq!
ans = "YES"
ans = "NO" if duplicates.length > (n + 1) / 2
puts ans

n = gets.chomp.to_i
elements = gets.chomp.split(/ /).map(&:to_i)
duplicates = []
(1...n).each do |i|
    duplicates += [i - 1, i] if elements[i - 1] == elements[i]
end
ans = "YES"
ans = "NO" if duplicates.length > (n + 1) / 2
puts ans

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
count = Hash.new(0)

arr.each do |a|
	count[a] += 1
end

count_arr = count.values.sort.reverse
(0..count_arr.length-2).each do |i|
	j = i + 1
	while j <= count_arr.length - 1
		if count_arr[i] <= count_arr[j]
			count_arr[i] = 0
			count_arr[j] -= count_arr[i]
			break
		else
			count_arr[i] -= count_arr[j]
			count_arr[j] = 0
		end
		j += 1
	end
end
count_arr.reject!{|a| a == 0}
# puts "#{count_arr}"
if count_arr[0] > 1
	puts "NO"
else
	puts "YES"
end

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
count = Hash.new(0)

arr.each do |a|
	count[a] += 1
end

count_arr = count.values
(0..count_arr.length-2).each do |i|
	j = i + 1
	while j <= count_arr.length - 1
		if count_arr[i] <= count_arr[j]
			count_arr[i] = 0
			count_arr[j] -= count_arr[i]
			break
		else
			count_arr[i] -= count_arr[j]
			count_arr[j] = 0
		end
		j += 1
	end
end
count_arr.reject!{|a| a == 0}
if count_arr[0] > 1
	puts "NO"
else
	puts "YES"
end

def solve
	h = {}
	gets
	a = gets.chomp.split(/ /)
	a.each do |x| h[x] = if h[x] == nil then 1 else h[x] + 1 end end
	h.each do |x, y| return 'NO' if y > a.length/2 + a.length%2 end
	'YES'
end

p solve

n = gets[0..-2].to_i
arr = gets.split(' ')
a = Array.new(n) { |i| arr[i].to_i }
mark = Array.new(1001, 0)
ans = true
for i in 0...n do
  mark[a[i]] += 1
  if mark[a[i]] > n / 2
    ans = false
    break
  end
end

max = mark.max
if max * 2 - n >= 2
  print "NO"
else
  print "YES"
end


The great hero guards the country where Homer lives. The hero has attack power $$$A$$$ and initial health value $$$B$$$. There are $$$n$$$ monsters in front of the hero. The $$$i$$$-th monster has attack power $$$a_i$$$ and initial health value $$$b_i$$$. The hero or a monster is said to be living, if his or its health value is positive (greater than or equal to $$$1$$$); and he or it is said to be dead, if his or its health value is non-positive (less than or equal to $$$0$$$).In order to protect people in the country, the hero will fight with monsters until either the hero is dead or all the monsters are dead.  In each fight, the hero can select an arbitrary living monster and fight with it. Suppose the $$$i$$$-th monster is selected, and the health values of the hero and the $$$i$$$-th monster are $$$x$$$ and $$$y$$$ before the fight, respectively. After the fight, the health values of the hero and the $$$i$$$-th monster become $$$x-a_i$$$ and $$$y-A$$$, respectively. Note that the hero can fight the same monster more than once.For the safety of the people in the country, please tell them whether the great hero can kill all the monsters (even if the great hero himself is dead after killing the last monster).

Each test contains multiple test cases. The first line contains $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains three integers $$$A$$$ ($$$1 \leq A \leq 10^6$$$), $$$B$$$ ($$$1 \leq B \leq 10^6$$$) and $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the attack power of the great hero, the initial health value of the great hero, and the number of monsters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^6$$$), where $$$a_i$$$ denotes the attack power of the $$$i$$$-th monster. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \leq b_i \leq 10^6$$$), where $$$b_i$$$ denotes the initial health value of the $$$i$$$-th monster. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case print the answer: "YES" (without quotes) if the great hero can kill all the monsters. Otherwise, print "NO" (without quotes).

NoteIn the first example: There will be $$$6$$$ fights between the hero and the only monster. After that, the monster is dead and the health value of the hero becomes $$$17 - 6 \times 2 = 5 > 0$$$. So the answer is "YES", and moreover, the hero is still living.In the second example: After all monsters are dead, the health value of the hero will become $$$709$$$, regardless of the order of all fights. So the answer is "YES".In the third example: A possible order is to fight with the $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd and $$$4$$$-th monsters. After all fights, the health value of the hero becomes $$$-400$$$. Unfortunately, the hero is dead, but all monsters are also dead. So the answer is "YES".In the fourth example: The hero becomes dead but the monster is still living with health value $$$1000 - 999 = 1$$$. So the answer is "NO".

gets.to_i.times do
  d, h, n = gets.split.map &:to_i
  a = gets.split.map(&:to_i).zip(gets.split.map(&:to_i)).sort{ |a, b|
    a[0] <=> b[0]
  }
  catch :no do
    i = 0
    while i < n
      h -= (a[i][1] + d - 1)/d*a[i][0]
      if h <= 0 && (i < n - 1 || h <= -a[i][0])
        puts "NO"
        throw :no
      end
      i += 1
    end
    puts "YES"
  end
end

gets.to_i.times do
  aa,bb,n=gets.split.map(&:to_i)
  a=gets.split.map(&:to_i)
  b=gets.split.map(&:to_i)
  bb+=a.max
  n.times do |i|
    bb-=a[i]*((b[i]-1)/aa+1)
    if bb<=0
      break
    end
  end
  if bb<=0
    puts "NO"
  else
    puts "YES"
  end
end


Bob is playing with $$$6$$$-sided dice. A net of such standard cube is shown below.He has an unlimited supply of these dice and wants to build a tower by stacking multiple dice on top of each other, while choosing the orientation of each dice. Then he counts the number of visible pips on the faces of the dice.For example, the number of visible pips on the tower below is $$$29$$$ — the number visible on the top is $$$1$$$, from the south $$$5$$$ and $$$3$$$, from the west $$$4$$$ and $$$2$$$, from the north $$$2$$$ and $$$4$$$ and from the east $$$3$$$ and $$$5$$$.The one at the bottom and the two sixes by which the dice are touching are not visible, so they are not counted towards total.Bob also has $$$t$$$ favourite integers $$$x_i$$$, and for every such integer his goal is to build such a tower that the number of visible pips is exactly $$$x_i$$$. For each of Bob's favourite integers determine whether it is possible to build a tower that has exactly that many visible pips.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of favourite integers of Bob.  The second line contains $$$t$$$ space-separated integers $$$x_i$$$ ($$$1 \leq x_i \leq 10^{18}$$$) — Bob's favourite integers.

For each of Bob's favourite integers, output "YES" if it is possible to build the tower, or "NO" otherwise (quotes for clarity).

NoteThe first example is mentioned in the problem statement.In the second example, one can build the tower by flipping the top dice from the previous tower.In the third example, one can use a single die that has $$$5$$$ on top.The fourth example is impossible.

require 'set'; require 'prime'
INF=Float::INFINITY; MOD=10**9+7
t = gets.chomp.to_i
a = gets.chomp.split.map(&:to_i)

a.each do |e|
  ee = e % 14
  ee += 14
  if e > 14 && 15 <= ee && ee <= 20
    puts 'YES'
  else
    puts 'NO'
  end
end

require 'set'; require 'prime'
INF=Float::INFINITY; MOD=10**9+7
t = gets.chomp.to_i
a = gets.chomp.split.map(&:to_i)

a.each do |e|
  ee = e % 14
  ee += 14
  if e <= 14 || (15 <= ee && ee <= 20)
    puts 'YES'
  else
    puts 'NO'
  end
end

require 'set'; require 'prime'
INF=Float::INFINITY; MOD=10**9+7
t = gets.chomp.to_i
a = gets.chomp.split.map(&:to_i)

a.each do |e|
  e = e % 14
  e += 14
  if 15 <= e && e <= 20
    puts 'YES'
  else
    puts 'NO'
  end
end


A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: "fced", "xyz", "r" and "dabcef". The following string are not diverse: "az", "aa", "bad" and "babc". Note that the letters 'a' and 'z' are not adjacent.Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed).You are given a sequence of strings. For each string, if it is diverse, print "Yes". Otherwise, print "No".

The first line contains integer $$$n$$$ ($$$1 \le n \le 100$$$), denoting the number of strings to process. The following $$$n$$$ lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between $$$1$$$ and $$$100$$$, inclusive.

Print $$$n$$$ lines, one line per a string in the input. The line should contain "Yes" if the corresponding string is diverse and "No" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.

n = gets.to_i

n.times do |r|
  string = gets.chomp
  freq = Array.new(26, 0)

  string_size = string.size

  string_size.times do |i|
    freq[string[i].ord - "a".ord] += 1
  end

  chain_count = 0
  in_chain = false
  printed = false
  freq.each do |value|
    if value > 1
      puts 'No'
      printed = true
      break
    end
    if value == 1
      if !in_chain
        chain_count += 1
        in_chain = true
      end
      if chain_count >= 2
        puts 'No'
        printed = true
        break
      end
    end
    if value == 0
      if in_chain
        in_chain = false
      end
    end
  end
  if !printed && chain_count <= 1
    puts 'Yes'
  end
end

require 'set'
alph = "abcdefghijklmnopqrstuvwxyz"
t = gets.chomp.to_i
t.times do |testcase|
  inp = gets.chomp
  s = []
  inp.size.times do |i|
    s.push(inp[i])
  end
  s = s.sort
  flag = true
  (inp.size-1).times do |i|
    if alph.index(s[i]) != alph.index(s[i+1])-1
      flag = false
      break
    end
  end
  if flag
    puts "yES"
  else
    puts "nO"
  end
end

n = gets.to_i
n.times do
  ss = gets.chomp.chars.map(&:ord).sort
  unless ss.size == ss.uniq.size
    puts 'No'
    next
  end

  ans = 'Yes'
  (0..ss.size-2).each do |i|
    if ss[i] + 1 != ss[i+1]
      ans = 'No'
    end
  end
  puts ans
end

t = "abcdefghijklmnopqrstuvwxyz".split("")
gets.to_i.times do
    s = gets.chomp.split("").sort
    n = s.size
    f = false
    (n-1).times do |i|
        if s[i] == s[i+1]
            puts "No"
            f = true
            break
        end
    end
    next if f
    (27-n).times do |i|
        if s == t[i,n]
            puts "Yes"
            f = true
            break
        end
    end
    next if f
    puts "No"
end

alphabet = "abcdefghijklmnopqrstuvwxyz"
t = gets.to_i

t.times do
  word = gets.chomp
  puts alphabet.include?(word.chars.sort.join) ? "Yes" : "No"
end

n = gets.strip.to_i
inputs = []

(1..n).each do |i|
  inputs << gets.strip
end

res = ["Yes"]*n
inputs.each_with_index do |a, index|
  codes = a.chars.map(&:ord).sort
  (1..codes.size-1).each do |i|
    if codes[i] - codes[i-1] != 1
      res[index] = "No"
      break
    end

  end
end

res.each do |r|
  puts r
end

n = gets.to_i
n.times do
  s = gets.strip.split('').sort.join
  puts s == (s[0]..(2..s.size).inject(s[0]) { |a| a.next }).to_a.join ? 'Yes' : 'No'
end

n = gets.to_i

n.times do |r|
  string = gets.chomp
  freq = Array.new(26, 0)

  string_size = string.size

  string_size.times do |i|
    freq[string[i].ord - "a".ord] += 1
  end

  chain_count = 0
  in_chain = false
  printed = false
  freq.each do |value|
    if value > 1
      p 'No'
      printed = true
      break
    end
    if value == 1
      if !in_chain
        chain_count += 1
        in_chain = true
      end
      if chain_count >= 2
        p 'No'
        printed = true
        break
      end
    end
    if value == 0
      if in_chain
        in_chain = false
      end
    end
  end
  if !printed && chain_count <= 1
    puts 'Yes'
  end
end

n = gets.to_i

n.times do |r|
  string = gets.chomp
  freq = Array.new(26, 0)

  string_size = string.size

  string_size.times do |i|
    freq[string[i].ord - "a".ord] += 1
  end

  chain_count = 0
  in_chain = false
  printed = false
  freq.each do |value|
    if value > 1
      p "No"
      printed = true
      break
    end
    if value == 1
      if !in_chain
        chain_count += 1
        in_chain = true
      end
      if chain_count >= 2
        p "No"
        printed = true
        break
      end
    end
    if value == 0
      if in_chain
        in_chain = false
      end
    end
  end
  if !printed && chain_count <= 1
    p "Yes"
  end
end

n = gets.to_i
n.times do
  ss = gets.chomp.chars.map(&:ord).sort
  puts 'No' unless ss.size == ss.uniq.size

  ans = 'Yes'
  (0..ss.size-2).each do |i|
    if ss[i] + 1 != ss[i+1]
      ans = 'No'
    end
  end
  puts ans
end

n = gets.strip.to_i
inputs = []

(1..n).each do |i|
  inputs << gets.strip
end

res = ["Yes"]*n
inputs.each_with_index do |a, index|
  codes = a.chars.map(&:ord).sort
  (1..codes.size-1).each do |i|
    if codes[i] - codes[i-1] != 1
      res[index] = "No"
      break
    end

  end
end

res.each do |r|
  p r
end


A car number in Berland consists of exactly n digits. A number is called beautiful if it has at least k equal digits. Vasya wants to change the digits in his car's number so that the number became beautiful. To replace one of n digits Vasya has to pay the sum of money, equal to the absolute difference between the old digit and the new one.Help Vasya: find the minimum sum of money he should pay to make the number of his car beautiful. You should also find the resulting beautiful number. If there are several such numbers, then print the lexicographically minimum one.

The first line contains two space-separated integers n and k (2 ≤ n ≤ 104, 2 ≤ k ≤ n) which represent how many digits the number has and how many equal digits a beautiful number should have. The second line consists of n digits. It describes the old number of Vasya's car. It is guaranteed that the number contains no spaces and only contains digits.

On the first line print the minimum sum of money Vasya needs to change the number. On the second line print the car's new number. If there are several solutions, print the lexicographically minimum one.

NoteIn the first sample replacing the second digit with an "8" costs |9 - 8| = 1. Replacing the fifth digit with an "8" costs the same. Replacing the sixth digit costs |6 - 8| = 2. As a result, Vasya will pay 1 + 1 + 2 = 4 for a beautiful number "888188".The lexicographical comparison of strings is performed by the < operator in modern programming languages. The string x is lexicographically smaller than the string y, if there exists such i (1 ≤ i ≤ n), that xi < yi, and for any j (1 ≤ j < i) xj = yj. The strings compared in this problem will always have the length n.

k=gets.split[1].to_i
a=gets.split[0].bytes.to_a
n=a.length
cnt=Array.new(10,0)
n.times{|i|
    a[i]-=48
    cnt[a[i]]+=1
}
mincost=99999999
res=[]
10.times{|d|
    cost=0
    count=cnt[d]
    1.upto(9){|i|
        if count>=k then
            break
        end
        if d+i<=9 then
            if cnt[d+i]+count>=k then
                cost+=i*(k-count)
                count=k
            else
                count+=cnt[d+i]
                cost+=cnt[d+i]*i
            end
        end
        if d-i>=0 then
            if cnt[d-i]+count>=k then
                cost+=i*(k-count)
                count=k
            else
                count+=cnt[d-i]
                cost+=cnt[d-i]*i
            end
        end
    }
    if cost<mincost then
        mincost=cost
        res=[]
    end
    if cost==mincost then
        ans=Array.new(n,0)
        used=Array.new(n,0)
        count=0
        0.upto(9){|i|
            tar=d+i
            n.times{|j|
                if count>=k then
                    break
                end
                if used[j]==0 && a[j]==tar then
                    ans[j]=d
                    count+=1
                    used[j]=true
                end
            }
            tar=d-i
            (n-1).downto(0){|j|
                if count>=k then
                    break
                end
                if used[j]==0 && a[j]==tar then
                    ans[j]=d
                    count+=1
                    used[j]=true
                end
            }
        }
        n.times{|j|
            if used[j]==0 then
                ans[j]=a[j]
            end
        }
        res.push(ans)
    end
}
puts mincost
ans=res.min
ans.each{|i| print i}
puts


Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $$$a$$$ of $$$n$$$ non-negative integers.Dark created that array $$$1000$$$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $$$k$$$ ($$$0 \leq k \leq 10^{9}$$$) and replaces all missing elements in the array $$$a$$$ with $$$k$$$.Let $$$m$$$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $$$|a_i - a_{i+1}|$$$ for all $$$1 \leq i \leq n - 1$$$) in the array $$$a$$$ after Dark replaces all missing elements with $$$k$$$.Dark should choose an integer $$$k$$$ so that $$$m$$$ is minimized. Can you help him?

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^{5}$$$) — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-1 \leq a_i \leq 10 ^ {9}$$$). If $$$a_i = -1$$$, then the $$$i$$$-th integer is missing. It is guaranteed that at least one integer is missing in every test case. It is guaranteed, that the sum of $$$n$$$ for all test cases does not exceed $$$4 \cdot 10 ^ {5}$$$.

Print the answers for each test case in the following format: You should print two integers, the minimum possible value of $$$m$$$ and an integer $$$k$$$ ($$$0 \leq k \leq 10^{9}$$$) that makes the maximum absolute difference between adjacent elements in the array $$$a$$$ equal to $$$m$$$. Make sure that after replacing all the missing elements with $$$k$$$, the maximum absolute difference between adjacent elements becomes $$$m$$$. If there is more than one possible $$$k$$$, you can print any of them.

NoteIn the first test case after replacing all missing elements with $$$11$$$ the array becomes $$$[11, 10, 11, 12, 11]$$$. The absolute difference between any adjacent elements is $$$1$$$. It is impossible to choose a value of $$$k$$$, such that the absolute difference between any adjacent element will be $$$\leq 0$$$. So, the answer is $$$1$$$.In the third test case after replacing all missing elements with $$$6$$$ the array becomes $$$[6, 6, 9, 6, 3, 6]$$$.  $$$|a_1 - a_2| = |6 - 6| = 0$$$;  $$$|a_2 - a_3| = |6 - 9| = 3$$$;  $$$|a_3 - a_4| = |9 - 6| = 3$$$;  $$$|a_4 - a_5| = |6 - 3| = 3$$$;  $$$|a_5 - a_6| = |3 - 6| = 3$$$. So, the maximum difference between any adjacent elements is $$$3$$$.

T = gets.to_i

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

  cand = Hash.new(false)
  max_diff = 0

  n.times do |i|
    v = a[i]

    if v == -1
      cand[a[i - 1]] = true if i > 0 && a[i - 1] != -1
      cand[a[i + 1]] = true if i < n - 1 && a[i + 1] != -1
    end

    if i < n - 1 && v != -1 && a[i + 1] != -1
      diff = (v - a[i + 1]).abs

      max_diff = diff if max_diff < diff
    end
  end

  nums = cand.keys.minmax.compact

  if nums.empty?
    puts "0 0"
  else
    ave = Rational(nums.inject(:+), nums.size)

    c1 = ave.ceil
    c2 = ave.floor

    min_diff = Float::INFINITY

    d1 = nums.map { |x| (x - c1).abs }.max
    d2 = nums.map { |x| (x - c2).abs }.max

    if d1 > d2
      puts [[max_diff, d2].max, c2].join(' ')
    else
      puts [[max_diff, d1].max, c1].join(' ')
    end
  end
end

T = gets.to_i

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

  cand = Hash.new(false)
  max_diff = 0

  n.times do |i|
    v = a[i]

    if v == -1
      cand[a[i - 1]] = true if i > 0 && a[i - 1] != -1
      cand[a[i + 1]] = true if i < n - 1 && a[i + 1] != -1
    end

    if i < n - 1 && v != -1 && a[i + 1] != -1
      diff = (v - a[i + 1]).abs

      max_diff = diff if max_diff < diff
    end
  end

  nums = cand.keys

  if nums.empty?
    puts "0 0"
  else
    ave = Rational(nums.inject(:+), nums.size)

    c1 = ave.ceil
    c2 = ave.floor

    min_diff = Float::INFINITY

    d1 = nums.map { |x| (x - c1).abs }.max
    d2 = nums.map { |x| (x - c2).abs }.max

    if d1 > d2
      puts [[max_diff, d2].max, c2].join(' ')
    else
      puts [[max_diff, d1].max, c1].join(' ')
    end
  end
end


Little Tanya decided to present her dad a postcard on his Birthday. She has already created a message — string s of length n, consisting of uppercase and lowercase English letters. Tanya can't write yet, so she found a newspaper and decided to cut out the letters and glue them into the postcard to achieve string s. The newspaper contains string t, consisting of uppercase and lowercase English letters. We know that the length of string t greater or equal to the length of the string s.The newspaper may possibly have too few of some letters needed to make the text and too many of some other letters. That's why Tanya wants to cut some n letters out of the newspaper and make a message of length exactly n, so that it looked as much as possible like s. If the letter in some position has correct value and correct letter case (in the string s and in the string that Tanya will make), then she shouts joyfully "YAY!", and if the letter in the given position has only the correct value but it is in the wrong case, then the girl says "WHOOPS".Tanya wants to make such message that lets her shout "YAY!" as much as possible. If there are multiple ways to do this, then her second priority is to maximize the number of times she says "WHOOPS". Your task is to help Tanya make the message.

The first line contains line s (1 ≤ |s| ≤ 2·105), consisting of uppercase and lowercase English letters — the text of Tanya's message. The second line contains line t (|s| ≤ |t| ≤ 2·105), consisting of uppercase and lowercase English letters — the text written in the newspaper. Here |a| means the length of the string a.

Print two integers separated by a space:   the first number is the number of times Tanya shouts "YAY!" while making the message,  the second number is the number of times Tanya says "WHOOPS" while making the message.

a=gets.chomp
b=gets.chomp
ahash=Hash.new(0)
bhash=Hash.new(0)
a.each_char {|i| ahash[i]+=1}
b.each_char {|i| bhash[i]+=1}
c=ahash.keys
yay=0
whoops=0

c.each do |i|
if ahash[i]<=bhash[i]
yay+=ahash[i]
bhash[i]-=ahash[i]
ahash[i]=0
else
yay+=bhash[i]
ahash[i]-=bhash[i]
bhash[i]=0
end
end

c.each do |i|
if ahash[i]>0

if i==i.downcase
k=bhash[i.upcase]
if ahash[i]>=k
whoops+=k
bhash[i.upcase]=0
else
whoops+=ahash[i]
bhash[i.upcase]-=ahash[i]
end
else
k=bhash[i.downcase]
if ahash[i]>=k
whoops+=k
bhash[i.downcase]=0
else
whoops+=ahash[i]
bhash[i.downcase]-=ahash[i]
end
end

end
end

puts "#{yay} #{whoops}"

s = gets.chomp
t = gets.chomp
ss = Array.new(256, 0)
tt = Array.new(256, 0)
s.each_byte do |x|
  ss[x] += 1
end
t.each_byte do |x|
  tt[x] += 1
end
su = ss['A'.ord..'Z'.ord]
sl = ss['a'.ord..'z'.ord]
tu = tt['A'.ord..'Z'.ord]
tl = tt['a'.ord..'z'.ord]

a = 0
b = 0
26.times do |i|
  x = [su[i], tu[i]].min
  a += x
  tu[i] -= x
  su[i] -= x
  x = [sl[i], tl[i]].min
  a += x
  tl[i] -= x
  sl[i] -= x
end
26.times do |i|
  x = [su[i], tl[i]].min
  b += x
  tl[i] -= x
  su[i] -= x
  x = [sl[i], tu[i]].min
  b += x
  tu[i] -= x
  sl[i] -= x
end

puts "#{a} #{b}"

s = gets.chomp.chars.sort
t = gets.chomp.chars.sort
ss = Array.new(58, 0)
tt = Array.new(58, 0)
s.each do |x|
  ss[x.ord - 65] += 1
end
t.each do |x|
  tt[x.ord - 65] += 1
end
su = ss[0..25]
sl = ss[32..57]
tu = tt[0..25]
tl = tt[32..57]

a = 0
b = 0
26.times do |i|
  x = [su[i], tu[i]].min
  a += x
  tu[i] -= x
  su[i] -= x
  x = [sl[i], tl[i]].min
  a += x
  tl[i] -= x
  sl[i] -= x
end
26.times do |i|
  x = [su[i], tl[i]].min
  b += x
  tl[i] -= x
  su[i] -= x
  x = [sl[i], tu[i]].min
  b += x
  tu[i] -= x
  sl[i] -= x
end

puts "#{a} #{b}"

s = gets
t = gets

hs = Hash.new(0)
s.split('').each { |c| hs[ c ] += 1 }

ht = Hash.new(0)
t.split('').each { |c| ht[ c ] += 1 }

a = 0
b = 0

hs.each do |k, v|
  m = [hs[k], ht[k]].min
  a += m
  hs[k] -= m
  ht[k] -= m
end

hs.each do |k, v|
  m = [hs[k], ht[ k.downcase ]].min
  b += m
  hs[ k ] -= m
  ht[ k.downcase ] += m

  m = [hs[k], ht[ k.upcase ]].min
  b += m
  hs[ k ] -= m
  ht[ k.upcase ] += m
end

puts "#{a - 1} #{b}"

s = gets.chomp
t = gets.chomp
n = t.size()
m = s.size()
a=Array.new(300).map{|e| e=0 }
b=Array.new(300).map{|e| e=0 }
  for i in 0..n-1
     a[t[i].ord] += 1
  end
    for i in 0..m-1
     b[s[i].ord] += 1
  end
yra = 0
oops = 0
for i in 0..200
     if a[i] > 0 && b[i] > 0
       a1 = a[i]
       b1 = b[i]
      # puts (a1.to_s + ' '+ b1.to_s + ' ' + i.to_s)
       yra += [a[i], b[i]].min
       a[i] -= b1
       b[i] -= a1
     end
  end
  for i in 64..91
    if (a[i] > 0 && b[i + 32] > 0)
       a1 = a[i]
       b1 = b[i + 32]
       oops += [a1, b1].min
       a[i] -= b1
       b[i] -= a1
    end
  end
 for i in 97..123
    if (a[i] > 0 && b[i - 32] > 0)
       a1 = a[i]
       b1 = b[i - 32]
       oops += [a1, b1].min
       a[i] -= b1
       b[i] -= a1
    end
  end
  s2 = ""
  s2 = yra.to_s + " " + oops.to_s
puts (s2)

if __FILE__ == $0
  END { main }
end

def main
  s = gets.strip
  t = gets.strip

  s_freqs = get_letter_freqs(s)
  t_freqs = get_letter_freqs(t)

  yay_count = get_yay_count(s_freqs, t_freqs)
  whoops_count = get_whoops_count(s_freqs, t_freqs)

  puts [yay_count, whoops_count].join(" ")
end

def get_letter_freqs(s)
  h = Hash.new(0)
  s.chars.each do |ch|
    h[ch] += 1
  end
  h
end

def get_yay_count(s_freqs, t_freqs)
  count = 0
  s_freqs.each do |ch, freq|
    freq.times do
      if t_freqs[ch] > 0
        s_freqs[ch] -= 1
        t_freqs[ch] -= 1
        count += 1
      end
    end
  end
  count
end

def get_whoops_count(s_freqs, t_freqs)
  count = 0
  s_freqs.each do |s_ch, s_freq|
    t_ch = s_ch.swapcase
    s_freq.times do
      if t_freqs[t_ch] > 0
        s_freqs[s_ch] -= 1
        t_freqs[t_ch] -= 1
        count += 1
      end
    end
  end
  count
end

a=gets.chomp
b=gets.chomp
ahash=Hash.new(0)
bhash=Hash.new(0)
a.each_char {|i| ahash[i]+=1}
b.each_char {|i| bhash[i]+=1}
c=ahash.keys
yay=0
whoops=0
c.each do |i|
if ahash[i]<=bhash[i]
yay+=ahash[i]
else
yay+=bhash[i]
if i=i.downcase
p=(ahash[i]-bhash[i])
k=bhash[i.upcase]
if p>=k
whoops+=k
else
whoops+=p
end
else
p=(ahash[i]-bhash[i])
k=bhash[i.dowbcase]
if p>=k
whoops+=k
else
whoops+=p
end
end
end
end

puts "#{yay} #{whoops}"

a=gets.chomp
b=gets.chomp
ahash=Hash.new(0)
bhash=Hash.new(0)
a.each_char {|i| ahash[i]+=1}
b.each_char {|i| bhash[i]+=1}
c=ahash.keys
yay=0
whoops=0
c.each do |i|
if ahash[i]<=bhash[i]
yay+=ahash[i]
else
yay+=bhash[i]
whoops+=(ahash[i]-bhash[i])
end
end
puts "#{yay} #{whoops}"

a=gets.chomp
b=gets.chomp
ahash=Hash.new(0)
bhash=Hash.new(0)
a.each_char {|i| ahash[i]+=1}
b.each_char {|i| bhash[i]+=1}
c=ahash.keys
yay=0
whoops=0
c.each do |i|
if ahash[i]<=bhash[i]
yay+=ahash[i]
else
yay+=bhash[i]
if i==i.downcase
p=(ahash[i]-bhash[i])
k=bhash[i.upcase]
if p>=k
whoops+=k
else
whoops+=p
end
else
p=(ahash[i]-bhash[i])
k=bhash[i.downcase]
if p>=k
whoops+=k
else
whoops+=p
end
end
end
end

puts "#{yay} #{whoops}"

if __FILE__ == $0
  END { main }
end

def main
  s = gets.strip
  t = gets.strip

  s_freqs = get_letter_freqs(s)
  t_freqs = get_letter_freqs(t)

  yay_count = get_yay_count(s_freqs, t_freqs)
  whoops_count = get_whoops_count(s_freqs, t_freqs)

  puts [yay_count, whoops_count].join(" ")
end

def get_letter_freqs(s)
  h = Hash.new(0)
  s.chars.each do |ch|
    h[ch] += 1
  end
  h
end

def get_yay_count(s_freqs, t_freqs)
  count = 0
  s_freqs.each do |ch, freq|
    freq.times do
      if t_freqs[ch] > 0
        s_freqs[ch] += 1
        t_freqs[ch] -= 1
        count += 1
      end
    end
  end
  count
end

def get_whoops_count(s_freqs, t_freqs)
  count = 0
  s_freqs.each do |s_ch, s_freq|
    t_ch = s_ch.swapcase
    s_freq.times do
      if t_freqs[t_ch] > 0
        s_freqs[s_ch] += 1
        t_freqs[t_ch] -= 1
        count += 1
      end
    end
  end
  count
end


Stepan has the newest electronic device with a display. Different digits can be shown on it. Each digit is shown on a seven-section indicator like it is shown on the picture below.  So, for example, to show the digit 3 on the display, 5 sections must be highlighted; and for the digit 6, 6 sections must be highlighted. The battery of the newest device allows to highlight at most n sections on the display. Stepan wants to know the maximum possible integer number which can be shown on the display of his newest device. Your task is to determine this number. Note that this number must not contain leading zeros. Assume that the size of the display is enough to show any integer.

The first line contains the integer n (2 ≤ n ≤ 100 000) — the maximum number of sections which can be highlighted on the display.

Print the maximum integer which can be shown on the display of Stepan's newest device.

n = gets.to_i
if n % 2 == 1 then
	res = '7'
	n -= 3
else
	res = '1'
	n -= 2
end
n = n / 2
s = '1'*n
res += s
puts res

n = gets
n = n.to_i
if n % 2 == 0
    print '1' * (n / 2)
else
    print '7' + '1' * (n / 2 - 1)
end

n = gets.to_i
if (n & 1 != 0) then
    print "7"
    n = n-2
end
(1..n/2).each do |i|
    print "1"
end

n = gets.chomp.to_i
if n % 2 == 1
    print 7
    n -= 3
end
i = 0
while i < n do
    i += 2
    print 1
end

i = gets.to_i
if i % 2 == 0
    c = i / 2
    answer = ""
    for counter in 1..c
        answer += "1"
    end
else
    c = i / 2 - 1
    answer = "7"
    for counter in 1..c
        answer += "1"
    end
end
puts answer

n = gets.to_i
if (n % 2 == 1)
    print(7)
    n = n - 3
end
while (n > 0)
    print(1)
    n = n - 2
end

a = gets
a = a.to_i
if a > 1
    if a % 2 == 1
        print 7
        a -=3
    end
end
while a > 1
    print 1
    a -= 2
end

x = gets.chomp.to_i
s = ""
if(x % 2 == 0)
	while(x > 0)
		s += "1"
		x-=2
	end
end
if x % 2 == 1
	s += "7"
	x-=3
	while(x > 0)
		s += "1"
		x-=2
	end
end
puts s

n = gets.to_i

ans = "1" * (n/2)
if n % 2 == 1
        ans[0]="7"
end

puts ans

n = gets.to_i
str = ""
if n % 2 > 0
    print 7
else
    print 1
end

n -= 2
n /= 2
n.times do
   print 1
end

num = gets.to_i

rem = num % 2
if rem == 1
    if num > 2
    print 7
    num = num - 3
end
end
while num>0
print 1
num = num-2;
end

a = gets.to_i

if a % 2 == 1
    print '7'
    a -= 3
end

a = (a/2).to_i

for i in 1..a
    print 1
end

n1 = gets.chomp
n = n1.to_i
c = "1"*(n/2)
d = "1"*(n/2 -1)
if n%2==0
  print c
else
  print "7"
  print d
end

# your code goes here
r = gets.strip.split(/\s+/).map(&:to_i)
t = r[0]
if t % 2 == 1
	puts '7' + ('1' * ((t - 3) / 2))
else
	puts '1' * (t / 2)
end

number = gets.to_i

if number % 2 == 0
	l = number / 2
	limit = l - 1
	for counter in 0..limit
		print "1"
	end
end
if number % 2 == 1
	l = number / 2
	limit = l - 2;
	print "7"
	for counter in 0..limit
		print "1"
	end
end

c = Integer(gets)
s = ""
if c % 2 == 1
	s += '7'
	c -= 3
end
(c/2).times do
	s += '1'
end
puts s

n=gets.chomp.to_i
z=n/2
t=n%2
ans=''
if t==1
 z=z-1
 ans+='7'
end
for i in 1..z
 ans+='1'
end
puts ans

#!/bin/ruby
t = gets.strip.to_i
#for a0 in (0..t-1)
#    n = gets.strip.to_i
#end
s=""
b=t
while t>7
    t-=2
    s.concat("1")
end
if t==2
   s="1"
end
if t==3
    s="7"
end
if t==4
    s="11"
end
if t==5
    s="71"
end
if t==6
    s.concat("1")
    s.concat("1")
    s.concat("1")
end
if t==7
    s="7"
    while b>7
        b-=2
        s.concat("1")
    end
    s.concat("1")
    s.concat("1")
    #s.concat("1")
    #s[0]=7
end

print s,"\n"

r=->{gets.split.map &:to_i}
ns=r[]
n = ns[0]
if n % 2 == 0 then
	puts "1"*(n/2)
else
	puts "7" + "1"*(n/2-1)
end

x=gets.to_i
y=x%2
x=x/2-y
puts "7"*y+"1"*x

a = gets.chomp.to_i
b = a / 2
c = a % 2

if c == 1
    puts '7' + '1' * (b - 1)
else
    puts '1' * b
end

n = gets
n = n.to_i
if n % 2 == 0
    print '1' * (n / 2)
else
    if n == 3
        print '17'
    else
        print '1' * ((n / 2) - 1) + '7'
    end
end

n = gets
n = n.to_i
if n % 2 == 0
    print '1' * (n / 2)
else
    print '1' * (n / 2 - 1) + '7'
end

c = Integer(gets)
while c >= 6
	print('9')
	$stdout.flush
	c -= 6
end
if c >= 3
	print('7')
	$stdout.flush
elsif c == 2
	print('1')
	$stdout.flush
end
puts ''

c = Integer(gets)
if c == 3
	print '7'
	$stdout.flush
else
	(c/2).times do
		print '1'
		$stdout.flush
	end
end

a=[6,2,5,5,4,5,6,3,7,6]
n=gets.chomp.to_i
ans=0
while n>=2 do
 i=9
 while i>=0 do
  if a[i]<=n
   ans+=ans*10+i
   n=n-a[i]
  end
  i=i-1
 end
end
puts ans


Polycarp has a strict daily schedule. He has n alarms set for each day, and the i-th alarm rings each day at the same time during exactly one minute.Determine the longest time segment when Polycarp can sleep, i. e. no alarm rings in that period. It is possible that Polycarp begins to sleep in one day, and wakes up in another.

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of alarms. Each of the next n lines contains a description of one alarm. Each description has a format "hh:mm", where hh is the hour when the alarm rings, and mm is the minute of that hour when the alarm rings. The number of hours is between 0 and 23, and the number of minutes is between 0 and 59. All alarm times are distinct. The order of the alarms is arbitrary. Each alarm starts ringing in the beginning of the corresponding minute and rings for exactly one minute (i. e. stops ringing in the beginning of the next minute). Polycarp can start sleeping instantly when no alarm is ringing, and he wakes up at the moment when some alarm starts ringing.

Print a line in format "hh:mm", denoting the maximum time Polycarp can sleep continuously. hh denotes the number of hours, and mm denotes the number of minutes. The number of minutes should be between 0 and 59. Look through examples to understand the format better.

NoteIn the first example there is only one alarm which rings during one minute of a day, and then rings again on the next day, 23 hours and 59 minutes later. Polycarp can sleep all this time.

n=gets.to_i
t=Array.new
for i in 1..n do
    s=gets
    t.push(s[0,2].to_i*60+s[3,2].to_i)
end
t.sort!
mx=1439-t[n-1]+t[0]
for i in 1..n-1 do
    if t[i]-t[i-1]-1>mx then mx=t[i]-t[i-1]-1 end
end
if mx/60<10 then print 0 end
print mx/60
print ':'
if mx%60<10 then print 0 end
print mx%60

n = gets.to_i
times = Array.new(n * 2, 0)
for i in 0..n-1
    v = gets.split(":")
    t = v[0].to_i * 60 + v[1].to_i
    times[i*2] = t
    times[i*2+1] = t + 1
end

times = times.sort
#puts times

tf = -1
tl = 0
res = 0
for i in 0..2*n-1
    t = times[i]
    if tf == -1
        tf = t
    elsif res < t - tl
            res = t - tl
    end
    tl = t
    #puts res
end
if res < 24 * 60 - tl + tf
    res = 24 * 60 - tl + tf
end
if (res / 60) < 10
    print "0"
end
print (res / 60).to_s
print ":"
if (res % 60) < 10
    print "0"
end
print (res % 60).to_s

def strtime(str)
  h = str.split(':')[0].to_i
  m = str.split(':')[1].to_i
  h * 60 + m
end

n = STDIN.gets.chomp!.to_i
t = (1..n).map { |i| strtime(STDIN.gets.chomp!) }
t.sort!

ans = 0
(0...n).map do |i|
  d = t[(i + 1) % n] - t[i] - 1
  d = d + 24 * 60 if d < 0
  ans = [ans, d].max
end

printf("%02d:%02d\n", ans / 60, ans % 60)

n = gets.to_i
times = []

n.times do
    hh, mm = gets.split(':').map(&:to_i)
    time = hh * 60 + mm
    times.push(time)
    times.push(time + 24 * 60)
end

times.sort!

t = 0
ans = 0

times.each {
    |time|

    if time - t - 1 > ans
        ans = time - t - 1
    end

    t = time
}
hh, mm = ans / 60, ans % 60
hh, mm = "%02d" % hh, "%02d" % mm

puts "#{hh}:#{mm}"

n=gets.to_i
t=Array.new
for i in 1..n do
    s=gets
    t.push(s[0,2].to_i*60+s[3,2].to_i)
end
t.sort!
mx=1439-t[n-1]+t[0]-1
if n==1 then mx+=1 end
for i in 1..n-1 do
    if t[i]-t[i-1]-1>mx then mx=t[i]-t[i-1]-1 end
end
if mx/60<10 then print 0 end
print mx/60
print ':'
if mx%60<10 then print 0 end
print mx%60

n = gets.to_i
times = Array.new(n * 2, 0)
for i in 0..n-1
    v = gets.split(":")
    t = v[0].to_i * 60 + v[1].to_i
    times[i*2] = t
    times[i*2+1] = t + 1
end

times = times.sort
#puts times

tf = -1
tl = 0
res = 0
for i in 0..2*n-1
    t = times[i]
    if tf == -1
        tf = t
    elsif res < t - tl
            res = t - tl
    end
    tl = t
    #puts res
end
if res < 24 * 60 - tl + tf
    res = 24 * 60 - tl + tf
end
print (res / 60).to_s + ":" + (res % 60).to_s

n = gets.to_i
times = Array.new(n * 2)
tf = -1
tl = 0
res = 200000
for i in 0..n-1
    v = gets.split(":")
    t = v[0].to_i * 60 + v[1].to_i
    if tf == -1
        tf = t
    elsif res > t - tl
            res = t - tl
    end
    tl = t + 1
end
if res > 24 * 60 - tl + tf
    res = 24 * 60 - tl + tf
end
print (res / 60).to_s + ":" + (res % 60).to_s


Blake is a CEO of a large company called "Blake Technologies". He loves his company very much and he thinks that his company should be the best. That is why every candidate needs to pass through the interview that consists of the following problem.We define function f(x, l, r) as a bitwise OR of integers xl, xl + 1, ..., xr, where xi is the i-th element of the array x. You are given two arrays a and b of length n. You need to determine the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 ≤ l ≤ r ≤ n.

The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the length of the arrays. The second line contains n integers ai (0 ≤ ai ≤ 109). The third line contains n integers bi (0 ≤ bi ≤ 109).

Print a single integer — the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 ≤ l ≤ r ≤ n.

NoteBitwise OR of two non-negative integers a and b is the number c = a OR b, such that each of its digits in binary notation is 1 if and only if at least one of a or b have 1 in the corresponding position in binary notation.In the first sample, one of the optimal answers is l = 2 and r = 4, because f(a, 2, 4) + f(b, 2, 4) = (2 OR 4 OR 3) + (3 OR 3 OR 12) = 7 + 15 = 22. Other ways to get maximum value is to choose l = 1 and r = 4, l = 1 and r = 5, l = 2 and r = 4, l = 2 and r = 5, l = 3 and r = 4, or l = 3 and r = 5.In the second sample, the maximum value is obtained for l = 1 and r = 9.

def f(a)
  b, *c = a
  c.each { |e| b |= e }

  b
end

n = gets.chomp.to_i
arr1 = gets.chomp.split.map(&:to_i)
arr2 = gets.chomp.split.map(&:to_i)
puts f(arr1) + f(arr2)


Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller.Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array.Help Amr by choosing the smallest subsegment possible.

The first line contains one number n (1 ≤ n ≤ 105), the size of the array. The second line contains n integers ai (1 ≤ ai ≤ 106), representing elements of the array.

Output two integers l, r (1 ≤ l ≤ r ≤ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them.

NoteA subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≤ i ≤ r - l + 1

#!/usr/bin/ruby
h=Hash.new{|h,k|h[k]=[]}
gets
gets.split.each.with_index(1){|e,i|
	h[e.to_i]<<i
}
siz=h.map{|k,v|v.size}.max
a=h.select{|k,v|v.size==siz}.min_by{|k,v|v.last-v.first}[1]
puts [a.first,a.last]*' '

n = gets.to_i
arr = gets.split(' ')
h = {}
n.times do |i|

    if h[arr[i]] != nil
        h[arr[i]][0] +=1
        h[arr[i]][2] = i+1
    else
        h[arr[i]] = []
        h[arr[i]][0] = 1
        h[arr[i]][1] = i+1
        h[arr[i]][2] = i+1
    end
end
max_count = (h.max_by{|k, v| v[0]})[1][0]
h.select!{|k, v| v[0] == max_count}
min_count_a = h.min_by{|k, v| v[2] - v[1]}
min_count = min_count_a[1][2] - min_count_a[1][1]

h.select!{|k, v| (v[2]-v[1]) == min_count}

puts "#{h.first.last[1]} #{h.first.last[2]}"

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

firsts = {}
lasts = {}
count = Hash.new(0)

a.each.with_index do |k, idx|
  firsts[k] ||= idx
  lasts[k] = idx

  count[k] += 1
end

max_key, max_value = count.max_by {|_, v| v }
min_key, min_value = firsts.select do |k, v|
  count[k] ==  max_value
end.min_by do |k, _|
  lasts[k] - firsts[k]
end

puts [firsts[min_key] + 1, lasts[min_key] + 1].join(' ')

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

first = {}
last = {}
count = Hash.new(0)

a.each_with_index{|x, i|
    first[x] ||= i+1
    last[x] = i+1
    count[x] += 1
}

_, c_max = count.max_by{|x, c| c}

x_opt = first.keys.select{|x| count[x] == c_max}.min_by{|x|
    last[x] - first[x]
}

puts "#{first[x_opt]} #{last[x_opt]}"

n = gets.to_i
arr = gets.split(' ').map{|x| x.to_i}
h = {}
n.times do |i|

    if h[arr[i]] != nil
        h[arr[i]][0] +=1
        h[arr[i]][2] = i+1
    else
        h[arr[i]] = []
        h[arr[i]][0] = 1
        h[arr[i]][1] = i+1
        h[arr[i]][2] = i+1
    end
end
max_count = (h.max_by{|k, v| v[0]})[1][0]
h.select!{|k, v| v[0] == max_count}
min_count_a = h.min_by{|k, v| v[2] - v[0]}
min_count = min_count_a[1][2] - min_count_a[1][1]

h.select!{|k, v| (v[2]-v[1]) == min_count}

puts "#{h.first.last[1]} #{h.first.last[2]}"

n = gets.to_i
arr = gets.split(' ')
h = {}
n.times do |i|

    if h[arr[i]] != nil
        h[arr[i]][0] +=1
        h[arr[i]][2] = i+1
    else
        h[arr[i]] = []
        h[arr[i]][0] = 1
        h[arr[i]][1] = i+1
        h[arr[i]][2] = i+1
    end
end
max_count = (h.max_by{|k, v| v[0]})[1][0]
h.select!{|k, v| v[0] == max_count}
min_count_a = h.min_by{|k, v| v[2] - v[0]}
min_count = min_count_a[1][2] - min_count_a[1][1]

h.select!{|k, v| (v[2]-v[1]) == min_count}

puts "#{h.first.last[1]} #{h.first.last[2]}"


Recently, Valery have come across an entirely new programming language. Most of all the language attracted him with template functions and procedures. Let us remind you that templates are tools of a language, designed to encode generic algorithms, without reference to some parameters (e.g., data types, buffer sizes, default values).Valery decided to examine template procedures in this language in more detail. The description of a template procedure consists of the procedure name and the list of its parameter types. The generic type T parameters can be used as parameters of template procedures.A procedure call consists of a procedure name and a list of variable parameters. Let's call a procedure suitable for this call if the following conditions are fulfilled:   its name equals to the name of the called procedure;  the number of its parameters equals to the number of parameters of the procedure call;  the types of variables in the procedure call match the corresponding types of its parameters. The variable type matches the type of a parameter if the parameter has a generic type T or the type of the variable and the parameter are the same. You are given a description of some set of template procedures. You are also given a list of variables used in the program, as well as direct procedure calls that use the described variables. For each call you need to count the number of procedures that are suitable for this call.

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of template procedures. The next n lines contain the description of the procedures specified in the following format: "void procedureName (type_1, type_2, ..., type_t)" (1 ≤ t ≤ 5), where void is the keyword, procedureName is the procedure name, type_i is the type of the next parameter. Types of language parameters can be "int", "string", "double", and the keyword "T", which denotes the generic type. The next line contains a single integer m (1 ≤ m ≤ 1000) — the number of used variables. Next m lines specify the description of the variables in the following format: "type variableName", where type is the type of variable that can take values "int", "string", "double", variableName — the name of the variable. The next line contains a single integer k (1 ≤ k ≤ 1000) — the number of procedure calls. Next k lines specify the procedure calls in the following format: "procedureName (var_1, var_2, ..., var_t)" (1 ≤ t ≤ 5), where procedureName is the name of the procedure, var_i is the name of a variable. The lines describing the variables, template procedures and their calls may contain spaces at the beginning of the line and at the end of the line, before and after the brackets and commas. Spaces may be before and after keyword void. The length of each input line does not exceed 100 characters. The names of variables and procedures are non-empty strings of lowercase English letters and numbers with lengths of not more than 10 characters. Note that this is the only condition at the names. Only the specified variables are used in procedure calls. The names of the variables are distinct. No two procedures are the same. Two procedures are the same, if they have identical names and identical ordered sets of types of their parameters.

On each of k lines print a single number, where the i-th number stands for the number of suitable template procedures for the i-th call.

4
void f(int,T)
void  f(T, T)
 void foo123   ( int,  double,  string,string  )
  void  p(T,double)
3
int a
 string    s
double x123
5
f(a,  a)
  f(s,a   )
foo   (a,s,s)
 f  (  s  ,x123)
proc(a)

6
void f(string,double,int)
void f(int)
   void f  ( T  )
void procedure(int,double)
void f  (T, double,int)
void f(string, T,T)
4
 int a
 int x
string  t
double  val
5
f(t, a, a)
f(t,val,a)
f(val,a, val)
 solve300(val, val)
f  (x)

procname = []
arglist = []
vartype = {}
typelist = ["T","int","double","string"]
n = gets.to_i
0.upto(n-1) do |ni|
  procstr = (gets.strip.gsub(" ","")[4..-1]).split(/\(|\)|,/)
  procname << procstr[0]
  arglist << procstr[1..-1].map{|x| typelist.index(x)}
end
m = gets.to_i
0.upto(m-1) do |mi|
  varstr = gets.split(" ")
  vartype[varstr[1]] = typelist.index(varstr[0])
end
k = gets.to_i
0.upto(k-1) do |ki|
  str = gets.strip.gsub(" ","").split(/\(|\)|,/)
  name = str[0]
  args = str[1..-1].map{|x| vartype[x]}
  count = 0
  0.upto(n-1) do |ni|
    if procname[ni] == name and args.size == arglist[ni].size
      match = true
      0.upto(args.size-1) do |j|
        if arglist[ni][j] != 0 and arglist[ni][j] != args[j]
          match = false
        end
      end
      count += 1 if match
    end
  end
  puts count
end

n = gets.to_i
tmp = []
n.times do
  tmp << gets.strip.split(/[ \,\(\)]/).delete_if{|e| e == ""}
end

var = []
m = gets.to_i
m.times do
   var << gets.strip.split.delete_if{|e| e == ""}
end

prc = []
k = gets.to_i
k.times do
   prc << gets.strip.split(/[ \,\(\)]/).delete_if{|e| e == ""}
end

prc.each do |pr|
  a = 0
  otmp = tmp.select{|t| t[1] == pr[0] && t.size-1 == pr.size}

  rpr = [pr[0]]
  1.upto pr.size-1 do |i|
    rpr << var.select{|v| v[1] == pr[i]}[0][0]
  end

  otmp.each do |t|
    c = 0
    1.upto pr.size-1 do |i|
      break if t[i+1] != "T" && rpr[i] != t[i+1]
      c += 1
    end
    a += 1 if c == pr.size-1
  end
  p a
end

def solve(funcs, vars, f, params)
  params.map! do | param |
    vars.assoc(param)[1]
  end

  return funcs.count do | func |
    if func[0] != f or params.length != func[1].length
      false
    else
      func[1].zip(params).all? do |fp, pp|
        fp == "T" || pp == fp
      end
    end
  end
end

funcs = []
vars = []

n = gets.to_i

n.times do |i|
  line = gets.strip
  /void +([A-Za-z0-9_]+) *\(([A-Za-z0-9_ ,]+)\)/ =~ line
  funcs << [$1, $2.split(',').map(&:strip)]
end

m = gets.to_i

m.times do
  line = gets.split.map(&:strip)
  vars << [line[1], line[0]]
end

k = gets.to_i

k.times do
  line = gets.strip
  /([A-Za-z0-9_]+) *\(([A-Za-z0-9_ ,]+)\)/ =~ line
  f = $1
  params = $2.split(',').map(&:strip)
  puts solve(funcs, vars, f, params)
end

procname = []
arglist = []
vartype = {}
typelist = ["T","int","double","string"]
n = gets.to_i
0.upto(n-1) do |ni|
  procstr = (gets.strip.gsub(" ","")[4..-1]).split(/\(|\)|,/)
  procname << procstr[0]
  arglist << procstr[1..-1].map{|x| typelist.index(x)}
end
puts arglist.inspect
m = gets.to_i
0.upto(m-1) do |mi|
  varstr = gets.split(" ")
  vartype[varstr[1]] = typelist.index(varstr[0])
end
k = gets.to_i
0.upto(k-1) do |ki|
  str = gets.gsub(" ","").split(/\(|\)|,/)
  name = str[0]
  args = str[1..-1].map{|x| vartype[x]}
  count = 0
  0.upto(n-1) do |ni|
    if procname[ni] == name
      match = true
      0.upto(args.size-1) do |j|
        if arglist[ni][j] != 0 and arglist[ni][j] != args[j]
          match = false
        end
      end
      count += 1 if match
    end
  end
  puts count
end


Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most ai milliliters of water.It turned out that among Pasha's friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows:  Pasha can boil the teapot exactly once by pouring there at most w milliliters of water;  Pasha pours the same amount of water to each girl;  Pasha pours the same amount of water to each boy;  if each girl gets x milliliters of water, then each boy gets 2x milliliters of water. In the other words, each boy should get two times more water than each girl does.Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends.

The first line of the input contains two integers, n and w (1 ≤ n ≤ 105, 1 ≤ w ≤ 109) — the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters. The second line of the input contains the sequence of integers ai (1 ≤ ai ≤ 109, 1 ≤ i ≤ 2n) — the capacities of Pasha's tea cups in milliliters.

Print a single real number — the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6.

NotePasha also has candies that he is going to give to girls but that is another task...

n, w = gets.split.map &:to_i
a = gets.split.map &:to_i
a = a.sort
x = a[0...n].min
y = a[n...(2*n)].min
puts [[2*x, y].min * 1.5 * n, w].min

# http://codeforces.com/contest/557/problem/B
n, w = gets.chomp.split.map(&:to_i)
cups = gets.chomp.split.map(&:to_i).sort

girl = cups[0..n].min
boy = cups[n..-1].min

min = [
  [2 * girl, boy].min * 1.5 * n,
  w
].min

puts min

def get_ints
    gets.split(" ").map(&:to_i)
end

n, w = get_ints
cup = get_ints

cup.sort!

x = [cup[0], cup[cup.size / 2] / 2.0].min
ans = [x * 3 * n, w].min
i = ans.to_i
p ans == i ? i : ans

# @line_n = -1
# @lines =  File.readlines('test7.in')
# def gets; @line_n+=1; @lines[@line_n] end
def read_ints; gets.chomp.split.map(&:to_i) end
n,w = read_ints
a = read_ints.sort

x = [a.sort[n]/2.0,a.sort[0],w/(3.0*n)].min
puts 3*x*n

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

a.sort!

puts [[a[0] * 2, a[n]].min * 1.5 * n, w].min

# http://codeforces.com/problemset/problem/557/B
n, w = gets.chomp.split.map(&:to_i)
a = gets.chomp.split.map(&:to_i).sort

i = 2 * n - 1
min_girl = a[0]
min_boy = a[n]

if min_girl * 2 > min_boy
  potential = min_boy / 2.0
else
  potential = min_girl
end

max = [3 * n * potential, w].min

p max == max.to_i ? max.to_i : max

[2,2,2,4,4,4]

# http://codeforces.com/problemset/problem/557/B
n, w = gets.chomp.split.map(&:to_i)
a = gets.chomp.split.map(&:to_i).sort

min_girl = a[0]
min_boy = a[n]

if min_girl * 2 > min_boy
  potential = min_boy / 2.0
else
  potential = min_girl
end

max = [3 * n * potential, w].min

p max

def read_ints; gets.chomp.split.map(&:to_i) end
n,w = read_ints
a = read_ints
x = [a.sort[n]/2.0,w/(3.0*n)].min
puts 3*x*n

# http://codeforces.com/problemset/problem/557/B

n, w = gets.chomp.split.map(&:to_i)
a = gets.chomp.split.map(&:to_i).sort

i = 2 * n - 1
while i > 0
  if a[i] <= w
    break potential = a[i]
  end
  i -= 1
end

if potential
  x = potential / 2.0
  max = [3 * n * x, w].min
else
  max = w
end

p max == max.to_i ? max.to_i : max

# http://codeforces.com/problemset/problem/557/B

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

if a.max > w
  p w
else
  x = a.max / 2.0

  max = 3 * n * x

  p max == max.to_i ? max.to_i : max
end

# http://codeforces.com/problemset/problem/557/B

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

x = a.max / 2.0

max = 3 * n * x

p max == max.to_i ? max.to_i : max


Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn. Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.

The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan. The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order. The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order. It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.

Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).

NoteIn the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.In the third example for any values l and r there is a situation when Stepan said the truth.

# your code goes here

n,l,r = gets.split(" ").map(&:to_i)
a = gets.split(" ").map(&:to_i)
b = gets.split(" ").map(&:to_i)
l-=1
r-=1
a[l..r] = a[l..r].sort()
b[l..r] = b[l..r].sort()
if a==b
	puts "TRUTH"
else
	puts "LIE"
end

n, l, r = gets.split.map &:to_i
trueseq = gets.split.map &:to_i
mainseq = gets.split.map &:to_i

l, r = l-1, r-1
# trueseq[0...l] == mainseq[0...l]
# trueseq[r+1..-1] == mainseq[r+1..-1]
# trueseq[l..r].set ~~~ mainseq[l..r].set

def oops
	puts 'LIE'
	exit
end

good = true
oops if trueseq[0...l]   != mainseq[0...l]
oops if trueseq[r+1..-1] != mainseq[r+1..-1]
oops if trueseq[l..r].sort != mainseq[l..r].sort

puts 'TRUTH'

# require 'set'

n, l, r = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)

a1 = a.take(r).drop(l - 1)
b1 = b.take(r).drop(l - 1)
a1 = a1.sort()
b1 = b1.sort()

if (a.take(l - 1) == b.take(l - 1) && a.drop(r) == b.drop(r) && a1 == b1) then
	puts "TRUTH"
else
	puts "LIE"
end

#!/bin/ruby

require 'set'

n, l, r = gets.strip.split(' ')
n = n.to_i
l = l.to_i
r = r.to_i
l -= 1
r -= 1

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

count = false
h = Hash.new

for i in l..r do
	h[a[i]] += 1 if h.has_key?(a[i])
	h[a[i]] = 1 unless h.has_key?(a[i])
end

for i in l..r
	if (h.has_key?(b[i]) && h[b[i]] == 0) || !h.has_key?(b[i])
		count = true
		break
	end
	h[b[i]] -= 1 if h.has_key?(b[i]) && h[b[i]] > 0
end

if count
	puts "LIE"
else
	count = false
	for i in 0...n
		next if l <= i && i <= r
		if a[i] != b[i]
			count = true
			break
		end
	end
	puts count ? "LIE" : "TRUTH"
end

#!/bin/ruby

require 'set'

n, l, r = gets.strip.split(' ')
n = n.to_i
l = l.to_i
r = r.to_i
l -= 1
r -= 1

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

count = false
h = Hash.new

for i in l..r do
	h[a[i]] += 1 if h.has_key?(a[i])
	h[a[i]] = 1 unless h.has_key?(a[i])
end

for i in l..r
	if (h.has_key?(b[i]) && h[b[i]] == 0) || !h.has_key?(b[i])
		count = 0
		break
	end
	h[b[i]] -= 1 if h.has_key?(b[i]) && h[b[i]] > 0
end

if count
	puts "LIE"
else
	count = false
	for i in 0...n
		if i < l && i > r && a[i] != b[i]
			count = true
			break
		end
	end
	puts count ? "LIE" : "TRUTH"
end

#!/bin/ruby

require 'set'

n, l, r = gets.strip.split(' ')
n = n.to_i
l = l.to_i
r = r.to_i
l -= 1
r -= 1

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

count = 0
h = Hash.new
for i in l..r do
	h[a[i]] += 1 if h.has_key?(a[i])
	h[a[i]] = 1 unless h.has_key?(a[i])
end

for i in l..r
	if h.has_key?(b[i]) && h[b[i]] == 0 || !h.has_key?(b[i])
		count += 1
		break
	end
end

if count > 0
	puts "LIE"
else
	count = 0
	for i in 0...n
		if i < l && i > r && a[i] != b[i]
			count += 1
			break
		end
	end
	if count > 0
		puts "LIE"
	else
		puts "TRUTH"
	end
end

#!/bin/ruby

require 'set'
a, b = Array.new(100010), Array.new(100010)

n, l, r = gets.strip.split(' ')
n = n.to_i
l = l.to_i
r = r.to_i
l -= 1
r -= 1

a = gets.strip
a = a.split(' ').map(&:to_i)

b = gets.strip
b = b.split(' ').map(&:to_i)

count = 0
s = Set.new
for i in l..r do
	s.add(a[i])
end

for i in l..r
	unless s.include?(b[i])
		count += 1
		break
	end
end

if count > 0
	puts "LIE"
else
	count = 0
	for i in 0...n
		if i < l && i > r && a[i] != b[i]
			count += 1
			break
		end
	end
	if count > 0
		puts "LIE"
	else
		puts "TRUTH"
	end
end


Vanya got bored and he painted n distinct points on the plane. After that he connected all the points pairwise and saw that as a result many triangles were formed with vertices in the painted points. He asks you to count the number of the formed triangles with the non-zero area.

The first line contains integer n (1 ≤ n ≤ 2000) — the number of the points painted on the plane.  Next n lines contain two integers each xi, yi ( - 100 ≤ xi, yi ≤ 100) — the coordinates of the i-th point. It is guaranteed that no two given points coincide.

In the first line print an integer — the number of triangles with the non-zero area among the painted points.

NoteNote to the first sample test. There are 3 triangles formed: (0, 0) - (1, 1) - (2, 0); (0, 0) - (2, 2) - (2, 0); (1, 1) - (2, 2) - (2, 0).Note to the second sample test. There is 1 triangle formed: (0, 0) - (1, 1) - (2, 0).Note to the third sample test. A single point doesn't form a single triangle.

n = gets.to_i
l = n.times.map{gets.split.map(&:to_i)}.sort
ans = n * (n - 1) * (n - 2) / 6
0.upto(n - 1) do |i|
  a = (i + 1).upto(n - 1).map{|j| (l[j][1] - l[i][1]).to_f / (l[j][0] - l[i][0])}.sort
  s = 1
  a.each_cons(2) do |x, y|
    if x == y
      s += 1
    else
      ans -= s * (s - 1) / 2
      s = 1
    end
  end
  ans -= s * (s - 1) / 2
end
puts ans

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

def f(n)
  return n if n <= 1
  n * f(n - 1)
end

def c(n, m)
  if n > m
    f(n) / (f(m) * f(n - m))
  elsif n < m
    0
  else
    1
  end
end

ans = c(n, 3)
0.upto(n - 1) do |i|
  a = []
  (i + 1).upto(n - 1) do |j|
    a << (l[j][1] - l[i][1]).to_f / (l[j][0] - l[i][0])
  end
  a.sort!
  b = nil
  s = 1
  a.size.times do |j|
    if a[j] != b
      ans -= c(s, 2)
      s = 1
      b = a[j]
    else
      s += 1
      ans -= c(s, 2) if j == a.size - 1
    end
  end
end
puts ans

n = gets.to_i
p = n.times.map{gets.split.map(&:to_i)}.sort
ans = n * (n - 1) * (n - 2) / 6
n.times do |i|
  c = 1
  (i + 1)
    .upto(n - 1)
    .map{|j| (p[j][1] - p[i][1]).to_f / (p[j][0] - p[i][0])}
    .sort
    .each_cons(2) do |a, b|
      if a == b
        c += 1
      else
        ans -= c * (c - 1) / 2;
        c = 1
      end
    end
  ans -= c * (c - 1) / 2
end
puts ans

n = gets.to_i
p = n.times.map{gets.split.map(&:to_i)}.sort
ans = n * (n - 1) * (n - 2) / 6
n.times do |i|
  c = 1
  (i + 1)
    .upto(n - 1)
    .map{|j| Math.atan2(p[j][1] - p[i][1], p[j][0] - p[i][0])}
    .sort
    .each_cons(2) do |a, b|
      if a == b
        c += 1
      else
        ans -= c * (c - 1) / 2;
        c = 1
      end
    end
  ans -= c * (c - 1) / 2
end
puts ans

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

def f(n)
  return n if n <= 1
  n * f(n - 1)
end

def c(n, m)
  if n > m
    f(n) / (f(m) * f(n - m))
  elsif n < m
    0
  else
    1
  end
end

ans = c(n, 3)
0.upto(n - 1) do |i|
  a = []
  (i + 1).upto(n - 1) do |j|
    a << (l[j][1] - l[i][1]).to_f / (l[j][0] - l[i][0])
  end
  a.sort!
  b = nil
  s = 1
  a.size.times do |j|
    if a[j] != b
      ans -= c(s, 2)
      s = 1
      b = a[j]
    else
      s += 1
      ans -= c(s, 2) if j == a.size - 1
    end
  end
end
puts ans


There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one.We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.

The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting. The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.

Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.

NoteIn the first sample, the optimal order is: 10, 20, 30, 40, 50.In the second sample, the optimal order is: 100, 200, 100, 200.

n = gets.chomp.to_i
h1 = Hash.new { 0 }
gets.chomp.split.map(&:to_i).each { |a| h1[a] += 1 }

h2 = Hash.new { 0 }
h1.each do |_, v|
  (1..v).each { |e| h2[e] += 1 }
end

ans = 0

h2.each { |_, v| ans += [v - 1, 0].max }

puts ans

n = readline.to_i
arr = readline.split(' ').collect(&:to_i)
ret = 0
while !arr.empty?
  a = arr.uniq
  ret += a.length - 1
  a.each do |num|
    arr.delete_at(arr.find_index(num))
  end
end
puts ret

n=gets.to_i
hs=Hash.new(0)
gets.split.map{|e| e.to_i}.each{|e|
	hs[e]+=1
}
xs=hs.sort.map{|e| e[1]}
ans=0
max1=0
xs.each{|e|
	if max1>=e then
		ans+=e
	else
		ans+=max1
		max1=e
	end
}
puts ans

n = gets.chomp.to_i
ax = gets.chomp.split("\s").map(&:to_i).sort

tmp_ax = ax.reduce(Hash.new(0)) { |acc, e| acc.tap { |hash| hash[e] += 1 } }
count = 0

until tmp_ax.size == 0
  # Add the number of elements - 1 to count
  count += tmp_ax.size - 1

  # Minus 1 from all elements of tmp_ax
  tmp_ax = tmp_ax.reduce({}) { |acc, (k, v)| acc.merge(k => v - 1) }

  # Delete elements whose value is 0
  tmp_ax = tmp_ax.select { |k, v| v > 0 }
end

puts count

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

hs = Hash.new(0)
as.each do |a|
  hs[a] += 1
end

cs = Hash.new(0)
hs.each do |_, v|
  for i in 1..v
    cs[i] += 1
  end
end

ans = 0
cs.each do |_, v|
  ans += [v - 1, 0].max
end
puts ans

n = gets.to_i
a = gets.split.map(&:to_i)
r = 0
while a.any?
  b = a.uniq
  r += b.count - 1
  b.each {|x| a.delete_at(a.index(x))}
end
puts r

def way(n, img)
	show = []
 # puts "count #{img.count}"
  for i in 1...img.count
    #puts "i = #{i}"
    unless img[i-1] == img[i]
     # puts "index = #{i}"
      n += 1
    else
      show.push(img[i-1])
    end
  end

  #puts "way n = #{n}  img = #{img}  show = #{show}"
  return n, show
end

count_img = gets.to_i

img = Array.new(count_img)

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

while true do
  answer = way(n, img)
  n = answer[0]
  img = answer[1]
  # puts "main n = #{n}  img = #{img}"
  if img.count < 2
    puts n
    exit
  end
end

n = gets.to_i
a = gets.split(" ")
tot = Array.new(1111, 0)
a.each do |x|
	tot[x.to_i] += 1
end
ans=1
tot.each do |c|
	ans = [ans, c].max
end
puts n-ans

#!/usr/bin/ruby
require 'set'
require 'scanf'

beauty_sets = [Set.new]
num_paintings = scanf("%d\n").first.to_i
beauties = gets().split(' ').map(&:to_i)
beauties.each do |beauty|
  placed = false
  beauty_sets.each do |bs|
    if !bs.include?(beauty)
      bs.add(beauty)
      placed = true
      break
    end
  end
  if !placed
    beauty_sets << Set.new
    beauty_sets.last.add(beauty)
  end
end

sum = 0
beauty_sets.each { |bs| sum += bs.count-1 }

puts sum

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

af = [0]*n
while af.inject {|s, aa| s + aa } < n
	ac = -1
	n.times do |i|
		if af[i]==0 && ac<a[i]
			c += 1 if ac > -1
			ac = a[i]
			af[i] = 1
		end
	end
end

puts c

n = gets.chomp.to_i
h1 = Hash.new { 0 }
gets.chomp.split.map(&:to_i).each { |a| h1[a] += 1 }

h2 = Hash.new { 0 }
h1.each do |_, v|
  (1..v).each { |e| h2[e] += 1 }
end

ans = 0
p h1
p h2
h2.each { |_, v| ans += [v - 1, 0].max }

puts ans

def way(n, img)
	show = []
  for i in 1...img.count
    if img[i] != img[i+1]
      n += 1
    else
      show.push(img[i])
    end
  end

  return n, show
end

count_img = gets.to_i

img = Array.new(count_img)

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

while true do
  answer = way(n, img)
  n = answer[0]
  img = answer[1]
  if img.count <= 2
    puts n
    exit
  end
end

def way(n, img)
	show = []
  for i in 1...img.count
    if img[i] != img[i+1]
      n += 1
    else
      show.push(img[i])
    end
  end
  return n, show
end

def main
  count_img = gets.to_i

  img = Array.new(count_img)

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

  while true do
    answer = way(n, img)
    n = answer[0]
    img = answer[1]
    return if img.count <= 2
  end

  p n
end

main

def way(n, img)
	show = []
  for i in 1...img.count
    if img[i] != img[i+1]
      n += 1
    else
      show.push(img[i])
    end
  end

  puts "way n = #{n}  img = #{img}  show = #{show}"
  return n, show
end

count_img = gets.to_i

img = Array.new(count_img)

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

while true do
  answer = way(n, img)
  n = answer[0]
  img = answer[1]
  puts "main n = #{n}  img = #{img}"
  if img.count <= 2
    puts n
    exit
  end
end

def way(n, img)
	show = []
 # puts "count #{img.count}"
  for i in 1...img.count
    #puts "i = #{i}"
    unless img[i-1] == img[i]
     # puts "index = #{i}"
      n += 1
    else
      show.push(img[i-1])
    end
  end

  #puts "way n = #{n}  img = #{img}  show = #{show}"
  return n, show
end

count_img = gets.to_i

img = Array.new(count_img)

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

while true do
  answer = way(n, img)
  n = answer[0]
  img = answer[1]
  # puts "main n = #{n}  img = #{img}"
  if img.count <= 2
    puts n
    exit
  end
end

def way(n, img)
	show = []
  puts "count #{img.count}"
  for i in 1...img.count
    #puts "i = #{i}"
    unless img[i-1] == img[i]
     # puts "index = #{i}"
      n += 1
    else
      show.push(img[i-1])
    end
  end

  #puts "way n = #{n}  img = #{img}  show = #{show}"
  return n, show
end

count_img = gets.to_i

img = Array.new(count_img)

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

while true do
  answer = way(n, img)
  n = answer[0]
  img = answer[1]
  # puts "main n = #{n}  img = #{img}"
  if img.count <= 2
    puts n
    exit
  end
end

#!/usr/bin/ruby
require 'set'
require 'scanf'

beauty_sets = [Set.new]
num_paintings = scanf("%d").first.to_i
beauties = gets().split(' ').map(&:to_i)
beauties.each do |beauty|
  placed = false
  beauty_sets.each do |bs|
    if !bs.include?(beauty)
      bs.add(beauty)
      placed = true
      break
    end
  end
  if !placed
    beauty_sets << Set.new
    beauty_sets.last.add(beauty)
  end
end

sum = 0
beauty_sets.each { |bs| sum += bs.count-1 }

puts sum


To become the king of Codeforces, Kuroni has to solve the following problem.He is given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. Help Kuroni to calculate $$$\prod_{1\le i<j\le n} |a_i - a_j|$$$. As result can be very big, output it modulo $$$m$$$.If you are not familiar with short notation, $$$\prod_{1\le i<j\le n} |a_i - a_j|$$$ is equal to $$$|a_1 - a_2|\cdot|a_1 - a_3|\cdot$$$ $$$\dots$$$ $$$\cdot|a_1 - a_n|\cdot|a_2 - a_3|\cdot|a_2 - a_4|\cdot$$$ $$$\dots$$$ $$$\cdot|a_2 - a_n| \cdot$$$ $$$\dots$$$ $$$\cdot |a_{n-1} - a_n|$$$. In other words, this is the product of $$$|a_i - a_j|$$$ for all $$$1\le i < j \le n$$$.

The first line contains two integers $$$n$$$, $$$m$$$ ($$$2\le n \le 2\cdot 10^5$$$, $$$1\le m \le 1000$$$) — number of numbers and modulo. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^9$$$).

Output the single number — $$$\prod_{1\le i<j\le n} |a_i - a_j| \bmod m$$$.

NoteIn the first sample, $$$|8 - 5| = 3 \equiv 3 \bmod 10$$$.In the second sample, $$$|1 - 4|\cdot|1 - 5|\cdot|4 - 5| = 3\cdot 4 \cdot 1 = 12 \equiv 0 \bmod 12$$$.In the third sample, $$$|1 - 4|\cdot|1 - 9|\cdot|4 - 9| = 3 \cdot 8 \cdot 5 = 120 \equiv 1 \bmod 7$$$.

	def abs( n )
		n > 0 ? n : -n
	end

	n,m = gets.chomp.strip.split(" ")
	arr = gets.chomp.strip.split(" ")
	m = m.to_i
	n = n.to_i

	if n > m
		puts 0
	else
		ans = 1
		(0..n-1).each do | i |
			(i+1..n-1).each do | j |
				ans = ( ans * abs(arr[ j ].to_i - arr[ i ].to_i ) ) % m
			end
		end
		puts ans
	end

N, M = gets.split.map(&:to_i)
A = gets.split.map(&:to_i).sort
B = A.map { |a| a % M }

counter = Hash.new(0)

B.each do |b|
  counter[b] += 1
end

if counter.values.max >= 2
  puts 0
else
  ans = 1

  N.times do |i|
    (i + 1).upto(N - 1) do |j|
      ans = ans * (A[i] - A[j]).abs % M
    end
  end

  puts ans % M
end

def abs( n )
	n > 0 ? n : -n
end

n,m = gets.chomp
n = n.to_i
m = m.to_i
arr = gets.chomp.strip.split(" ")

if n > m
	puts 0
else
	ans = 0
	(0..n-1).each do | i |
		(i+1..n-1).each do | j |
			ans = ( ans * abs(arr[ j ].to_i - arr[ i ].to_i ) ) % m
		end
	end
end

def abs( n )
	n > 0 ? n : -n
end

n,m = gets.chomp.strip.split(" ")
arr = gets.chomp.strip.split(" ")
m = m.to_i
n = n.to_i

if n > m
	puts 0
else
	ans = 0
	(0..n-1).each do | i |
		(i+1..n-1).each do | j |
			ans = ( ans * abs(arr[ j ].to_i - arr[ i ].to_i ) ) % m
		end
	end
	puts ans
end

def abs( n )
	n > 0 ? n : -n
end

n,m = gets.chomp.strip.split(" ")
arr = gets.chomp.strip.split(" ")
m = m.to_i
n = n.to_i

if n > m
	puts 0
else
	ans = 0
	(0..n-1).each do | i |
		(i+1..n-1).each do | j |
			ans = ( ans * abs(arr[ j ].to_i - arr[ i ].to_i ) ) % m
		end
	end
end


As you could know there are no male planes nor female planes. However, each plane on Earth likes some other plane. There are n planes on Earth, numbered from 1 to n, and the plane with number i likes the plane with number fi, where 1 ≤ fi ≤ n and fi ≠ i.We call a love triangle a situation in which plane A likes plane B, plane B likes plane C and plane C likes plane A. Find out if there is any love triangle on Earth.

The first line contains a single integer n (2 ≤ n ≤ 5000) — the number of planes. The second line contains n integers f1, f2, ..., fn (1 ≤ fi ≤ n, fi ≠ i), meaning that the i-th plane likes the fi-th.

Output «YES» if there is a love triangle consisting of planes on Earth. Otherwise, output «NO». You can output any letter in lower case or in upper case.

NoteIn first example plane 2 likes plane 4, plane 4 likes plane 1, plane 1 likes plane 2 and that is a love triangle.In second example there are no love triangles.

a=$<.read.split.map &:to_i
puts (1...a.size).any?{|i|
	a[a[a[i]]]==i
} ? :YES : :NO

n = gets.to_i;
inp = gets.chomp.split().map(&:to_i);
t = true;
for i in 0..n-1
  if(inp[inp[inp[i] - 1] - 1] - 1 == i)
    t = false;
    puts "YES";
    break;
  end;
end;
if (t)
  puts "NO";
end;

gets
ar = gets.split.map(&:to_i)
h = {}
ar.each_index do |i|
  h[ar[i]] = ar[ar[i]-1] if ar.include?(ar[ar[i]-1])
end
h.each do |key, val|
  b = h[val]
  if h[b] == key
    puts "Yes"
    exit 0
  end
end
puts "No"

n = Integer(gets)
friends = gets.split.map{|a| Integer(a)}
a1 = (1..n).to_a
a2 = friends
a3 = a2.map{|a| friends[a-1] }
a4 = a3.map{|a| friends[a-1] }

puts (if a1.zip(a2,a3,a4).detect{|a,b,c,d| a==d and a!=b and a!=c and b!=c} then "YES" else "NO" end)

n = gets.to_i
fs = gets.split.map(&:to_i)
ans = 'NO'

fs.each_with_index do |f, i|
  if i == fs[fs[f-1]-1] - 1
    ans = 'YES'
  end
end

puts ans

$n=gets.chomp.to_i
$f=gets.split.map &:to_i
def check (a,b,c)
  if (b==4 and a==c)
    return true
  end
  if (b>3)
    return false
  end
  return check($f[a]-1,b+1,c)
end
for i in 0.upto($n-1)
  if (check(i,1,i))
    print "YES"
    exit
  end
end
print "NO"

def judge(n, f)
  (1..n).each do |i|
    return true if f[f[f[i]]] == i
  end
  return false
end

n = gets.to_i
f = gets.split.map(&:to_i).insert(0, 0)
puts judge(n, f) ? "YES" : "NO"

n = gets.to_i
a = gets.split(' ')
for i in 0..n-1
  a[i]=a[i].to_i-1
end
for i in 0..n-1
  t= (a[a[a[i]]]==i)|t
end
if t==true
    puts "YES"
else
    puts "NO"
end

n = gets.to_i
f = [nil] + gets.split.map(&:to_i)
(1...f.size).each do |i|
  if f[f[f[i]]] == i
    puts 'YES'
    exit
  end
end
puts 'NO'

# Love Triangles

def track(a, e)
	return a[e-1]
end

def check(a, i)
	x = track(a, a[i])
	return 1	if  track(a, x)== i+1
	return 0
end

def find_luv(inp,n)
	for i in 0..n-1
		return 'YES' if check(inp,i) == 1
	end
	return 'NO'
end

n = gets.to_i
inp = gets.chomp.split().map(&:to_i)
puts find_luv(inp,n)

def solve(n, f)
  n = n.to_i
  f = f.split(&:to_i)
  for i in 0...n
    x = f[i].to_i
    y = f[x - 1].to_i
    return true if f[y - 1].to_i == i.to_i + 1
  end

  return false
end

n = gets.chomp
f = gets.chomp

if solve(n, f)
  puts "YES"
else
  puts "NO"
end

n,*a=$<.read.split.map &:to_i
puts [*0...n].permutation(2).any?{|i,j|
	a[a[a[j]-1]-1]==i
} ? :YES : :NO

n,*a=$<.read.split.map &:to_i
puts [*0...n].permutation(2).any?{|i,j|
	a[a[j]-1]==i
} ? :YES : :NO

n = gets.to_i
a = gets.split(' ')
for i in 0..n-1
  a[i]=a[i].to_i-1
end
t=1
for i in 0..n-1
  t= (a[a[a[i]]]==i)
    t=1
end
if t==1
    puts "YES"
else
    puts "NO"
end


You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. You can perform operations on it.In one operation you can replace any element of the array $$$a_i$$$ with $$$\lfloor \frac{a_i}{2} \rfloor$$$, that is, by an integer part of dividing $$$a_i$$$ by $$$2$$$ (rounding down).See if you can apply the operation some number of times (possible $$$0$$$) to make the array $$$a$$$ become a permutation of numbers from $$$1$$$ to $$$n$$$ —that is, so that it contains all numbers from $$$1$$$ to $$$n$$$, each exactly once.For example, if $$$a = [1, 8, 25, 2]$$$, $$$n = 4$$$, then the answer is yes. You could do the following:  Replace $$$8$$$ with $$$\lfloor \frac{8}{2} \rfloor = 4$$$, then $$$a = [1, 4, 25, 2]$$$.  Replace $$$25$$$ with $$$\lfloor \frac{25}{2} \rfloor = 12$$$, then $$$a = [1, 4, 12, 2]$$$.  Replace $$$12$$$ with $$$\lfloor \frac{12}{2} \rfloor = 6$$$, then $$$a = [1, 4, 6, 2]$$$.  Replace $$$6$$$ with $$$\lfloor \frac{6}{2} \rfloor = 3$$$, then $$$a = [1, 4, 3, 2]$$$.

The first line of input data contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) —the number of test cases. Each test case contains exactly two lines. The first one contains an integer $$$n$$$ ($$$1 \le n \le 50$$$), the second one contains integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$).

For each test case, output on a separate line:   YES if you can make the array $$$a$$$ become a permutation of numbers from $$$1$$$ to $$$n$$$,  NO otherwise.  You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).

NoteThe first test case is explained in the text of the problem statement.In the second test case, it is not possible to get a permutation.

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

inputs.each_slice(2) do |inp|
    a = inp[0].to_i
    nums = inp[1].split(' ').map(&:to_i).sort

    ex = Hash[ (1..a).map{|n| [n, true] } ]
    nums.each do |n|
        while n >= 1
            if ex[n]
                ex.delete(n)
                break
            end

            n /= 2
        end
    end

    if ex.length == 0
        puts "YES"
    else
        puts "NO"
    end
end

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

inputs.each_slice(2) do |inp|
    a = inp[0].to_i
    nums = inp[1].split(' ').map(&:to_i).sort

    ex = Hash[ (1..a).map{|n| [n, true] } ]
    nums.each do |n|
        i = n
        while i >= 1
            if ex[i]
                ex.delete(i)
                break
            end

            i /= 2
        end
    end

    if ex.length == 0
        puts "YES"
    else
        puts "NO"
    end
end

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

inputs.each_slice(2) do |inp|
    a = inp[0].to_i
    nums = inp[1].split(' ').map(&:to_i).sort

    ex = Hash[ (1..a).map{|n| [n, true] } ]
    nums.each do |n|
        i = n
        while i >= 1
            if ex[i]
                ex.delete(i)
                break
            end

            i = (i / 2).round
        end
    end

    if ex.length == 0
        puts "YES"
    else
        puts "NO"
    end
end

require 'set'

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

inputs.each_slice(2) do |inp|
    a = inp[0].to_i
    nums = inp[1].split(' ').map(&:to_i).sort

    ex = Set.new((1..a).to_a)
    nums.each do |n|
        i = n
        while i >= 1
            if ex.member?(i)
                ex.delete(i)
                break
            end

            i = (i / 2).round
        end
    end

    if ex.length == 0
        puts "YES"
    else
        puts "NO"
    end
end

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

  h = {}
  xs.each_with_index do |x, i|
    loop {
      (h[x] ||= []) << i if x <= n
      x /= 2
      break if x == 0
    }
  end

  if h.size != n
    puts "NO"
    next
  end

  used = [false] * n
  h = h.values.sort_by! { _1.size }

  solution_exists = true

  h.each do |indices|
    found = false
    indices.each do |i|
      if !used[i]
        used[i] = true
        found = true
        break
      end
    end

    if !found
      solution_exists = false
      break
    end
  end

  puts(solution_exists ? "YES" : "NO")
end

require 'set'

t = readline.strip.to_i
t.times do
  readline
  a = readline.strip.split(/\s+/).map(&:to_i)
  s = Set.new((1..a.count).to_a)
  a.each do |e|
    while e > 0
      if s.member?(e)
        s.delete(e)
        break
      end

      e /= 2
    end
  end

  puts s.count == 0 ? 'YES' : 'NO'
end