You are given a multiset (i. e. a set that can contain multiple equal integers) containing $$$2n$$$ integers. Determine if you can split it into exactly $$$n$$$ pairs (i. e. each element should be in exactly one pair) so that the sum of the two elements in each pair is odd (i. e. when divided by $$$2$$$, the remainder is $$$1$$$).

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1\leq t\leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1\leq n\leq 100$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1,a_2,\dots, a_{2n}$$$ ($$$0\leq a_i\leq 100$$$) — the numbers in the set.

For each test case, print "Yes" if it can be split into exactly $$$n$$$ pairs so that the sum of the two elements in each pair is odd, and "No" otherwise. You can print each letter in any case.

NoteIn the first test case, a possible way of splitting the set is $$$(2,3)$$$, $$$(4,5)$$$.In the second, third and fifth test case, we can prove that there isn't any possible way.In the fourth test case, a possible way of splitting the set is $$$(2,3)$$$.

#!/usr/bin/env ruby

ncases = gets.chomp.to_i

ncases.times do
  n = gets.chomp.to_i
  numbers = gets.chomp.split(' ').map { |str| str.to_i }
  answer = numbers.map { |n| n % 2 == 0 }.filter { |b| b }.size == n
  puts answer ? 'Yes' : 'No'
end

def possible?(arr)
  arr.filter { |x| x % 2 == 1}.size == arr.size / 2
end

t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  inp = gets.chomp.split(' ').map(&:to_i)
  possible?(inp) ? puts("Yes\n") : puts("No\n")
end

gets
while gets
  a = gets.split.map(&:to_i)
  puts a.count(&:odd?) == a.size / 2 ? 'Yes' : 'No'
end

a = gets.to_i
a.times{
 n = gets.to_i
 arr = gets.split.map &:to_i
 b = arr.select{_1.odd?}
 c = arr - b
 if b.size == n && c.size == n then puts "Yes"
 else
     puts "No"
end
}

t = gets.to_i
t.times { |u|
  n = gets.to_i
  arr = gets.chomp.split(" ").map(&:to_i)
  even = 0
  odd = 0
  arr.each { |x|
    if x % 2 == 0
      even += 1
    else
      odd += 1
    end
  }
  if even == odd
    puts "Yes"
  else
    puts "No"
  end
}

gets.to_i.times do
  n=gets.to_i
  a=gets.split.map(&:to_i)
  c=0
  a.each do |aa|
    if aa.odd?
      c+=1
    end
  end
  if c==n
    puts "Yes"
  else
    puts "No"
  end
end


Let $$$\mathsf{AND}$$$ denote the bitwise AND operation, and $$$\mathsf{OR}$$$ denote the bitwise OR operation.You are given an array $$$a$$$ of length $$$n$$$ and a non-negative integer $$$k$$$. You can perform at most $$$k$$$ operations on the array of the following type:  Select an index $$$i$$$ ($$$1 \leq i \leq n$$$) and replace $$$a_i$$$ with $$$a_i$$$ $$$\mathsf{OR}$$$ $$$2^j$$$ where $$$j$$$ is any integer between $$$0$$$ and $$$30$$$ inclusive. In other words, in an operation you can choose an index $$$i$$$ ($$$1 \leq i \leq n$$$) and set the $$$j$$$-th bit of $$$a_i$$$ to $$$1$$$ ($$$0 \leq j \leq 30$$$). Output the maximum possible value of $$$a_1$$$ $$$\mathsf{AND}$$$ $$$a_2$$$ $$$\mathsf{AND}$$$ $$$\dots$$$ $$$\mathsf{AND}$$$ $$$a_n$$$ after performing at most $$$k$$$ operations.

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains the integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le 10^9$$$). Then a single line follows, containing $$$n$$$ integers describing the arrays $$$a$$$ ($$$0 \leq a_i < 2^{31}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output a single line containing the maximum possible $$$\mathsf{AND}$$$ value of $$$a_1$$$ $$$\mathsf{AND}$$$ $$$a_2$$$ $$$\mathsf{AND}$$$ $$$\dots$$$ $$$\mathsf{AND}$$$ $$$a_n$$$ after performing at most $$$k$$$ operations.

NoteFor the first test case, we can set the bit $$$1$$$ ($$$2^1$$$) of the last $$$2$$$ elements using the $$$2$$$ operations, thus obtaining the array [$$$2$$$, $$$3$$$, $$$3$$$], which has $$$\mathsf{AND}$$$ value equal to $$$2$$$.For the second test case, we can't perform any operations so the answer is just the $$$\mathsf{AND}$$$ of the whole array which is $$$4$$$.

gets.to_i.times do
  n, k = gets.split.map(&:to_i)
  xs = gets.split.map(&:to_i)
  30.downto(0).each do |i|
    count = xs.count { |x| x & (1 << i) == 0 }
    if k >= count
      j = 0
      while j < n
        xs[j] = xs[j] | (1 << i)
        j += 1
      end
      k -= count
    end
  end
  p xs.reduce(:&)
end


You are given a multiset of n integers. You should select exactly k of them in a such way that the difference between any two of them is divisible by m, or tell that it is impossible.Numbers can be repeated in the original multiset and in the multiset of selected numbers, but number of occurrences of any number in multiset of selected numbers should not exceed the number of its occurrences in the original multiset.

First line contains three integers n, k and m (2 ≤ k ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — number of integers in the multiset, number of integers you should select and the required divisor of any pair of selected integers. Second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the numbers in the multiset.

If it is not possible to select k numbers in the desired way, output «No» (without the quotes). Otherwise, in the first line of output print «Yes» (without the quotes). In the second line print k integers b1, b2, ..., bk — the selected numbers. If there are multiple possible solutions, print any of them.

def solution(k, ans_index, hash)
  puts 'Yes'
  hash[ans_index].each_with_index do |val, index|
    print "#{val} " if index < k
  end
end

def get_hash_and_index(k, m, array)
  hash = []
  ans_index = -1
  array.each do |x|
    rem = x % m
    hash[rem] = [] if hash[rem].nil?
    hash[rem].push(x)
    ans_index = rem if hash[rem].length == k
  end
  ans_index == -1 ? (puts 'No') : solution(k, ans_index, hash)
end

_n, k, m = STDIN.gets.split.map(&:to_i)
array = STDIN.gets.split.map(&:to_i)

get_hash_and_index(k, m, array)

#! /usr/bin/env ruby

n, k, m = gets.split.map &:to_i
a = gets.split.map &:to_i
a = a.group_by { |i| i % m }
yes = false
a.each_key do |key|
    if a[key].size >= k
        yes = true
        puts:Yes
        puts a[key][0...k] * ' '
        break
    end
end
if not yes
    puts:No
end

def read_integers
  STDIN.gets.split.map(&:to_i)
end

def solve array, m, k, hash
  result = []
  array.each do |x|
    rem = x%m
    hash[rem] = hash[rem] || []
    hash[rem].push(x)
    result = hash[rem] if hash[rem].length > result.length
  end
  result
end

def solution
  n, k, m = read_integers
  array = read_integers
  hash = []
  result = solve array, m, k, hash
  if result.length  < k
    print "No\n"
  else
    print "Yes\n"
    result.each_with_index do |val, index|
        print  val, ' ' if index < k
    end
  end
end

solution

def read_integers
  STDIN.gets.split.map(&:to_i)
end

def solve array, m, k
  result = []
  hash = []
  array.each do |x|
    rem = x%m
    hash[rem] = hash[rem] || []
    hash[rem].push(x)
    result = hash[rem] if hash[rem].length > result.length
  end
  result
end

def solution
  n, k, m = read_integers
  array = read_integers
  result = solve array, m, k
  if result.length  < k
    print "No\n"
  else
    print "Yes\n"
    result.each_with_index do |val, index|
        print  val, ' ' if index < k
    end
  end
end

solution

def read_integers
  STDIN.gets.split.map(&:to_i)
end

def solve array, m, k, hash
  ans_index = -1;
  array.each do |x|
    rem = x%m
    hash[rem] = hash[rem] || []
    hash[rem].push(x)
    if hash[rem].length == k
      ans_index = rem
    end
  end
  ans_index
end

def solution
  n, k, m = read_integers
  array = read_integers
  hash = []
  ans_index = solve array, m, k, hash
  if ans_index  == -1
    print "No\n"
  else
    print "Yes\n"
    hash[ans_index ].each_with_index do
      |val, index|
      if index < k
        print  val
        print ' '
      end
    end
  end
end

solution

def solution
  n, k, m = STDIN.gets.split.map(&:to_i)
  array = STDIN.gets.split.map(&:to_i)

  hash = []
  ans_index = -1;
  array.each{ |x|
    rem = x%m

    if hash[rem] == nil then
      hash[rem] = []
    end

    hash[rem].push(x)

    if hash[rem].length == k then
      ans_index = rem
    end

  }

  if ans_index  == -1 then
    puts 'No'
  else
    puts 'Yes'

    hash[ans_index ].each_with_index { |val, index|
      if index < k then
       print  val
       print ' '
      end
    }

    #puts output;
  end
end

solution

def solution
  n, k, m = STDIN.gets.split.map(&:to_i)
  array = STDIN.gets.split.map(&:to_i)

  hash = []
  ans_index = -1;
  array.each{ |x|
    rem = x%m

    if hash[rem] == nil then
      hash[rem] = []
    end

    hash[rem].push(x)

    if hash[rem].length == k then
      ans_index = rem
    end

  }

  if ans_index  == -1 then
    puts 'No'
  else
    puts 'Yes'
   # output = ""

    hash[ans_index ].each_with_index { |val, index|
      if index < k then
    #    output += val.to_s + " "
       puts val
      end
    }

    #puts output;
  end
end

solution

#! /usr/bin/env ruby

n, k, m = gets.split.map &:to_i
a = gets.split.map &:to_i
a = a.group_by { |i| i % m }
yes = false
a.each_key do |key|
    if a[key].size >= k
        yes = true
        puts:Yes
        puts a[key][0...k] * ' '
    end
end
if not yes
    puts:No
end

def solution
  n, k, m = STDIN.gets.split.map(&:to_i)
  array = STDIN.gets.split.map(&:to_i)

  hash = {}
  best = -1;
  array.each{ |x|
    rem = x%m
    if hash[rem] == nil then
      hash[rem] = []
    end

    hash[rem].push(x)
    if hash[rem].length == k then
      best = x%m
    end
  }

  if best == -1 then
    puts 'No'
  else
    puts 'yes'
    output = "";
    hash[best].each_with_index { |val, index|
      if index < k then
        output += val.to_s + " "
      end
    }
    puts output;
  end
end

solution

def solution
  n, k, m = gets.split.map(&:to_i)
  array = gets.split.map(&:to_i)

  hash = {}
  best = -1;
  array.each{ |x|
    rem = x%m
    if hash[rem] == nil then
      hash[rem] = []
    end

    hash[rem].push(x)
    if hash[rem].length == k then
      best = x%m
    end
  }

  if best == -1 then
    puts 'No'
  else
    puts 'yes'
    output = "";
    hash[best].each_with_index { |val, index|
      if index < k then
        output = output + val.to_s + " "
      end
    }
    puts output;
  end
end

solution


Genos needs your help. He was asked to solve the following programming problem by Saitama:The length of some string s is denoted |s|. The Hamming distance between two strings s and t of equal length is defined as , where si is the i-th character of s and ti is the i-th character of t. For example, the Hamming distance between string "0011" and string "0110" is |0 - 0| + |0 - 1| + |1 - 1| + |1 - 0| = 0 + 1 + 0 + 1 = 2.Given two binary strings a and b, find the sum of the Hamming distances between a and all contiguous substrings of b of length |a|.

The first line of the input contains binary string a (1 ≤ |a| ≤ 200 000). The second line of the input contains binary string b (|a| ≤ |b| ≤ 200 000). Both strings are guaranteed to consist of characters '0' and '1' only.

Print a single integer — the sum of Hamming distances between a and all contiguous substrings of b of length |a|.

NoteFor the first sample case, there are four contiguous substrings of b of length |a|: "00", "01", "11", and "11". The distance between "01" and "00" is |0 - 0| + |1 - 0| = 1. The distance between "01" and "01" is |0 - 0| + |1 - 1| = 0. The distance between "01" and "11" is |0 - 1| + |1 - 1| = 1. Last distance counts twice, as there are two occurrences of string "11". The sum of these edit distances is 1 + 0 + 1 + 1 = 3.The second sample case is described in the statement.

a = gets.chomp
b = gets.chomp

hs = {}
hs[-1] = 0
cnt = 0
for i in 0...b.size
  cnt += 1 if b[i] == '1'
  hs[i] = cnt
end

dif = b.length - a.length
ans = 0
for i in 0...a.size
  if a[i] == '0'
    ans += hs[i + dif] - hs[i - 1]
  else
    ans += dif + 1 - hs[i + dif] + hs[i - 1]
  end
end

puts ans


Lord Omkar has permitted you to enter the Holy Church of Omkar! To test your worthiness, Omkar gives you a password which you must interpret!A password is an array $$$a$$$ of $$$n$$$ positive integers. You apply the following operation to the array: pick any two adjacent numbers that are not equal to each other and replace them with their sum. Formally, choose an index $$$i$$$ such that $$$1 \leq i < n$$$ and $$$a_{i} \neq a_{i+1}$$$, delete both $$$a_i$$$ and $$$a_{i+1}$$$ from the array and put $$$a_{i}+a_{i+1}$$$ in their place. For example, for array $$$[7, 4, 3, 7]$$$ you can choose $$$i = 2$$$ and the array will become $$$[7, 4+3, 7] = [7, 7, 7]$$$. Note that in this array you can't apply this operation anymore.Notice that one operation will decrease the size of the password by $$$1$$$. What is the shortest possible length of the password after some number (possibly $$$0$$$) of operations?

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the password. The second line of each test case contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$1 \leq a_{i} \leq 10^9$$$) — the initial contents of your password. The sum of $$$n$$$ over all test cases will not exceed $$$2 \cdot 10^5$$$.

For each password, print one integer: the shortest possible length of the password after some number of operations.

NoteIn the first test case, you can do the following to achieve a length of $$$1$$$:Pick $$$i=2$$$ to get $$$[2, 4, 1]$$$Pick $$$i=1$$$ to get $$$[6, 1]$$$Pick $$$i=1$$$ to get $$$[7]$$$In the second test case, you can't perform any operations because there is no valid $$$i$$$ that satisfies the requirements mentioned above.

# cook your code here
tc = gets.chomp.to_i
tc.times {
    n = gets.chomp.to_i

    arr = gets.chomp.split(' ').map(&:to_i)
    unique = 1
    index = 1
    while(index < n) do
        if arr[index] != arr[0]
            unique += 1
            break
        end
        index += 1
    end
    if arr.size() == 1 || unique >= 2
      puts 1
    else
      puts arr.size()
    end

}

# your code goes here

# a = gets.chomp.to_i

# b = gets.chomp.to_i

# puts (a+b)
t = gets.chomp.to_i
for tc in 1..t
	n = gets.chomp.to_i
	a = gets.split
	b = false
	for i in 1..(n-1)
		if a[i] != a[i-1]
			b = true;
		end
	end
	if b
		puts 1
	else
		puts n
	end
end


This is an easier version of the problem. In this version $$$n \le 1000$$$The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought $$$n$$$ plots along the highway and is preparing to build $$$n$$$ skyscrapers, one skyscraper per plot.Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it.Formally, let's number the plots from $$$1$$$ to $$$n$$$. Then if the skyscraper on the $$$i$$$-th plot has $$$a_i$$$ floors, it must hold that $$$a_i$$$ is at most $$$m_i$$$ ($$$1 \le a_i \le m_i$$$). Also there mustn't be integers $$$j$$$ and $$$k$$$ such that $$$j < i < k$$$ and $$$a_j > a_i < a_k$$$. Plots $$$j$$$ and $$$k$$$ are not required to be adjacent to $$$i$$$.The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors.

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of plots. The second line contains the integers $$$m_1, m_2, \ldots, m_n$$$ ($$$1 \leq m_i \leq 10^9$$$) — the limit on the number of floors for every possible number of floors for a skyscraper on each plot.

Print $$$n$$$ integers $$$a_i$$$ — the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible. If there are multiple answers possible, print any of them.

NoteIn the first example, you can build all skyscrapers with the highest possible height.In the second test example, you cannot give the maximum height to all skyscrapers as this violates the design code restriction. The answer $$$[10, 6, 6]$$$ is optimal. Note that the answer of $$$[6, 6, 8]$$$ also satisfies all restrictions, but is not optimal.

N = gets.to_i
M = gets.split.map(&:to_i)

def rebuild(mid)
  height = 0

  rh = lh = M[mid]

  mid.downto(0) do |i|
    if M[i] > lh
      height += M[i] - lh
    else
      lh = M[i]
    end
  end

  mid.upto(N - 1) do |i|
    if M[i] > rh
      height += M[i] - rh
    else
      rh = M[i]
    end
  end

  height
end

def construct(mid)
  temp = M.dup

  rh = lh = M[mid]

  mid.downto(0) do |i|
    if M[i] > lh
      temp[i] = lh
    end

    lh = temp[i]
  end

  mid.upto(N - 1) do |i|
    if M[i] > rh
      temp[i] = rh
    end

    rh = temp[i]
  end

  temp
end

sum = M.inject(:+)
idx = (0...N).max_by { |m| sum - rebuild(m) }

puts construct(idx).join(' ')

N = gets.to_i
M = gets.split.map(&:to_i)

def rebuild(mid)
  height = 0

  rh = lh = M[mid]

  mid.downto(0) do |i|
    if M[i] > lh
      height += M[i] - lh
    end

    lh = M[i]
  end

  mid.upto(N - 1) do |i|
    if M[i] > rh
      height += M[i] - rh
    end

    rh = M[i]
  end

  height
end

def construct(mid)
  temp = M.dup

  rh = lh = M[mid]

  mid.downto(0) do |i|
    if M[i] > lh
      temp[i] = lh
    end

    lh = M[i]
  end

  mid.upto(N - 1) do |i|
    if M[i] > rh
      temp[i] = rh
    end

    rh = M[i]
  end

  temp
end

sum = M.inject(:+)
idx = (0...N).max_by { |m| sum - rebuild(m) }

puts construct(idx).join(' ')

N = gets.to_i
M = gets.split.map(&:to_i)

def rebuild(mid)
  height = 0

  rh = lh = M[mid]

  mid.downto(0) do |i|
    if M[i] > lh
      height += M[i] - lh
    end

    lh = M[i]
  end

  mid.upto(N - 1) do |i|
    if M[i] > rh
      height += M[i] - rh
    end

    rh = M[i]
  end

  height
end

def construct(mid)
  temp = M.dup

  rh = lh = M[mid]

  mid.downto(0) do |i|
    if M[i] > lh
      temp[i] = lh
    end

    lh = temp[i]
  end

  mid.upto(N - 1) do |i|
    if M[i] > rh
      temp[i] = rh
    end

    rh = temp[i]
  end

  temp
end

sum = M.inject(:+)
idx = (0...N).max_by { |m| sum - rebuild(m) }

puts construct(idx).join(' ')


Little Vasya has received a young builder’s kit. The kit consists of several wooden bars, the lengths of all of them are known. The bars can be put one on the top of the other if their lengths are the same.Vasya wants to construct the minimal number of towers from the bars. Help Vasya to use the bars in the best way possible.

The first line contains an integer N (1 ≤ N ≤ 1000) — the number of bars at Vasya’s disposal. The second line contains N space-separated integers li — the lengths of the bars. All the lengths are natural numbers not exceeding 1000.

In one line output two numbers — the height of the largest tower and their total number. Remember that Vasya should use all the bars.

n = gets.to_i
hs = Hash.new 0
gets.split(' ').map(&:to_i).each{|x| hs[x]+=1}
puts "#{hs.max_by(&:last).last} #{hs.length}"

n = gets.to_i

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

kol = br.uniq.size

h = []
br.size.times do |i|
    h << br.reject{|it| it != br[i]}.size
end

h = h.sort

puts h[-1].to_s + " " + kol.to_s

n = gets.chomp.to_i
a = gets.chomp.split(/ /).map!{|x|x.to_i}
h = Hash.new
for i in 0..n-1
	if h.key?(a[i])
		h[a[i]] += 1
	else
		h[a[i]] = 1
	end
end
ha = h.to_a.sort{|a, b|
  b[1] <=> a[1]
}
puts ha[0][1].to_s + " " + h.length.to_s

gets
v=gets.split
print v.map{|x|v.count x}.max," ",v.uniq.size

#!/usr/bin/ruby
gets;
a=gets.split.map(&:to_i).sort_by{|e|e}.chunk{|e|e}.to_a
puts "#{a.map{|k,v|v.size}.max} #{a.size}"

n = gets.chomp.to_i
a = Array.new(1001).map {|x| x=0}
gets.chomp.split(" ").map {|x| x.to_i}.each { |x|
  a[x]+=1
}
puts a.sort[a.length-1].to_s << " " << a.count{|x| x!=0}.to_s

gets
a=gets.split
print a.map{|b|a.count b}.max," ",a.uniq.size

n = gets.to_i
l = gets.split(" ").map(&:to_i).sort
cnt = 1
max = 0
col = 0
last = l[0]
l.each do |el|
  if last != el
    cnt += 1
    last = el
    max = col if max < col
    col = 1
  else
    col += 1
  end
end

max = col if max < col

puts "#{max} #{cnt}"

n=gets.chomp.strip.to_i
p=gets.chomp.split(' ').map(&:to_i)
a=Array.new(1001,0)
sum=0
max=0
p.each do |x|
	if a[x]==0
		sum+=1
	end
	a[x]+=1
	if a[x]>max
		max=a[x]
	end
end
puts max.to_s + ' ' + sum.to_s

gets
a = gets.split.map(&:to_i).to_a
puts [a.map { |x| a.find_all {|y| x==y}.size }.max, a.uniq.size]*' '

a = gets
a = gets.chomp.split(' ')

r = {}
max = 0
a.each do |x|
y = x.to_sym

r[y] = (r[y]||0) + 1
max = r[y] if max < r[y]
end

puts "#{max} #{r.size}"

n = gets.to_i
l = gets.split.map(&:to_i).sort
fox = Array.new()

counter = 1
for i in 1..n
  if l[i]==l[i-1]
    counter+=1
  else
    fox.concat([counter])
    counter = 1
  end
end

puts fox.max.to_s + " " + l.uniq.size.to_s

gets
arr = gets.split.map { |x| x.to_i }.sort
print arr.count(arr.max_by { |x| arr.count(x) }), " ",arr.uniq.size

num=gets.chomp.to_i
max=0
num2=num
b=gets.chomp.split(" ")
1.upto(1000) do |i|
temp=b.count("#{i}")
num2=num2-temp
if temp>max
max= temp
end
if temp>1
num=num-temp+1
end
end
printf "#{max} "
printf "#{num}"

require 'set'
gets
x=gets.split.map{|e| e.to_i}
d=Set.new(x).size
h=Hash.new(0)
ans=0
x.each{|e|
	h[e]+=1
	ans=h[e] if ans<h[e]
}
puts [ans,d]*" "

gets;a=gets.split;print a.map{|i|a.count i}.max," ",a.uniq.size

gets
data=gets.split.map(&:to_i).sort

hs=Hash.new(0)

max_height = 0
data.each{|val|
	hs[val]+=1

	max_height = [max_height, hs[val]].max
}

puts "#{max_height} #{hs.size}"

gets
data=gets.split.map(&:to_i).sort

hs={}
hs.default = 0

max_height = 0
data.each{|val|
	hs[val]+=1

	max_height = [max_height, hs[val]].max
}

puts "#{max_height} #{hs.size}"

gets
q = Hash.new
gets.split.map do |e|
  e.to_i
  if q[e].nil?
    q[e] = 1
  else
    q[e] += 1
  end
end

puts q.values.max

puts q.size

n=gets; m=Hash.new(0)
gets.chomp.split.each { |i| m[i] += 1 }
puts m.max_by{|k,v| v}[1]; print m.length; print ' '

n=gets.chomp.to_i
s=gets.chomp.to_s
s=s.split(" ")
u=s.uniq
max=1
for i in u
	if (s.count(i)>max)
		max=s.count(i)
	end
end
print("#{max} #{u.size}\n")

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i
a = gets.split.map(&:to_i) + [0] # 0 for terminator
prev = 0
towers = 0
highest_tower = 0
height = 0
a.sort.each { |i|
    if i != prev
        if i != 0
            towers += 1
            height = 1
            prev = i
        end
    else
        height += 1
        if height > highest_tower
            highest_tower = height
        end
    end
}

puts "#{highest_tower} #{towers}"

n = gets.to_i
a = gets.split(' ')[0...n].map { |s| s.to_i }
d = a.inject(Hash.new(0)) { |h, x| h[x] += 1; h }
cnt = d.size
max = d.map { |k, v| v }.max
puts "#{max} #{cnt}"

gets
mas = gets.split(" ")
puts mas.map { |s| mas.count s }.max.to_s+" "+mas.uniq.size.to_s

# http://codeforces.com/contest/37/problem/A

number = gets.chomp.to_i

bars = gets.chomp.split(" ").each {|str| [str.to_i]}

towers = []

bars.each {|bar|
  if index = towers.index {|item| item[0] == bar}
    towers[index] << bar
  else
    towers << [bar]
  end
}

towers.sort! {|a, b| a.length <=> b.length}.reverse!

puts "#{towers[0].length} #{towers.length}"

gets
blocks = gets.chomp.split.map(&:to_i)

res = {}
blocks.each do |b|
 res[b] ||= 0
 res[b] += 1
end

puts "#{res.values.max} #{blocks.uniq.size}"

n = gets.to_i
m = gets.split
m = m.uniq.collect{|i| [i, m.find_all{|j| j == i}.size]}
print(m.max{|x, y| x[1] <=> y[1]}[1].to_s + " " + m.size.to_s)

class Euler
  mas=Array.new(size=0, obj=nil);
  n=gets.to_i;
  st=gets.to_s.split(" ");
  st.sort!;
  mx=1;
  for i in 0..st.length-1
    k=1;
    for j in i+1..st.length-1
    if (st[j]==st[i])
      k+=1;
    end
  end
  if (k>mx)
    mx=k;
  end
    end
  a=st.uniq;
  print mx.to_s+" "+a.length.to_s+"\n";
end

gets
bars, towers = gets.split.map(&:to_i), Hash.new(0)
bars.each {|bar| towers[bar] += 1}
print towers.values.max, " ", towers.length

barNum = gets.chomp.to_i
bar = gets.chomp.split(' ').map {|item| item.to_i}
bar.sort!
total = bar.uniq.length
largest = 0
bar.each do |item|
	largest = bar.count(item) if largest < bar.count(item)
end
puts "#{largest} #{total}"

n = gets.to_i
l = gets.split(" ").map(&:to_i).sort
cnt = 1
max = 0
col = 0
last = l[0]
l.each do |el|
  if last != el
    cnt += 1
    last = el
    max = col if max < col
    col = 1
  else
    col += 1
  end
end

puts "#{max} #{cnt}"

a = gets
a = gets.chomp.split(' ')

r = {}
max = 0
a.each do |x|
y = x.to_sym
r[y] = r[y]||0 + 1
max = r[y] if max < r[y]
end

puts "#{max} #{r.size}"

n = gets.to_i
l = gets.split.map(&:to_i).sort
fox = Array.new()

counter = 1
for i in 1..n-1
  if l[i]==l[i-1]
    counter+=1
  else
    fox.concat([counter])
    counter = 1
  end
end

puts fox.max.to_s + " " + l.uniq.size.to_s

n=gets; m=Hash.new(0)
gets.chomp.split.each { |i| m[i] += 1 }
print m.length; print ' '; puts m.max_by{|k,v| v}[1]

a=gets.chomp.to_i
b=gets.chomp.split(" ")
sum=0
0.upto(a-1) do |i|
   b[i]= b[i].to_i
sum=sum+b[i]
end
r=0
if sum%2==0
    r=1
end

sum=(sum/2.0).ceil

b.sort!
b.reverse!
p=0
0.upto(a-1) do |i|
p=p+b[i].to_i

if ( p>=sum && r==0)
p=i+1
break
end
if  (p>sum && r==1)
p=i+1
break
end
end

puts "#{p}"


Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky.When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest.Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123.What maximum number of tickets could Vasya get after that?

The first line contains integer n (1 ≤ n ≤ 104) — the number of pieces. The second line contains n space-separated numbers ai (1 ≤ ai ≤ 108) — the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide.

Print the single number — the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together.

gets
a=gets.split.map &:to_i
g=->x{a.count{|i|i%3==x}}
p g[0]/2+[g[1],g[2]].min

n=gets.to_i
a=gets.split.map(&:to_i)
def calc(x)
        a=0
        while x>0
                a+=x%10
                x/=10
        end
        a%=3
end
cnt=[0,0,0]
a.each{|i|
        cnt[calc(i)]+=1
}
p cnt[0]/2+[cnt[1],cnt[2]].min

gets
c = Hash.new(0).tap { |h| gets.split.map(&:to_i).each { |v| h[v % 3] += 1 } }
p c[0] / 2 + [c[1], c[2]].min

N = gets.chomp.to_i
c = Hash.new(0).tap { |h| gets.split.map(&:to_i).each { |v| h[v % 3] += 1 } }
p c[0] / 2 + [c[1], c[2]].min

def min(a, b)
  a < b ? a : b
end

def digits_sum(number)
  number.to_s.chars.map { |v| v.to_i }.inject { |r, v| r + v }
end

n = gets.to_i
parts = gets.split.map { |v| digits_sum(v) % 3 }

count = [0, 0, 0]

parts.each { |p| count[p] += 1 }

puts count[0] / 2 + min(count[1], count[2])


The integers shop sells $$$n$$$ segments. The $$$i$$$-th of them contains all integers from $$$l_i$$$ to $$$r_i$$$ and costs $$$c_i$$$ coins.Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.After shopping, Vasya will get some more integers as a gift. He will get integer $$$x$$$ as a gift if and only if all of the following conditions are satisfied:   Vasya hasn't bought $$$x$$$.  Vasya has bought integer $$$l$$$ that is less than $$$x$$$.  Vasya has bought integer $$$r$$$ that is greater than $$$x$$$. Vasya can get integer $$$x$$$ as a gift only once so he won't have the same integers after receiving a gift.For example, if Vasya buys segment $$$[2, 4]$$$ for $$$20$$$ coins and segment $$$[7, 8]$$$ for $$$22$$$ coins, he spends $$$42$$$ coins and receives integers $$$2, 3, 4, 7, 8$$$ from these segments. He also gets integers $$$5$$$ and $$$6$$$ as a gift.Due to the technical issues only the first $$$s$$$ segments (that is, segments $$$[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$$$) will be available tomorrow in the shop.Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.For each $$$s$$$ from $$$1$$$ to $$$n$$$, find how many coins will Vasya spend if only the first $$$s$$$ segments will be available.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of segments in the shop. Each of next $$$n$$$ lines contains three integers $$$l_i$$$, $$$r_i$$$, $$$c_i$$$ ($$$1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$$$) — the ends of the $$$i$$$-th segments and its cost. It is guaranteed that the total sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

For each test case output $$$n$$$ integers: the $$$s$$$-th ($$$1 \leq s \leq n$$$) of them should be the number of coins Vasia will spend in the shop if only the first $$$s$$$ segments will be available.

NoteIn the first test case if $$$s = 1$$$ then Vasya can buy only the segment $$$[2, 4]$$$ for $$$20$$$ coins and get $$$3$$$ integers.The way to get $$$7$$$ integers for $$$42$$$ coins in case $$$s = 2$$$ is described in the statement.In the second test case note, that there can be the same segments in the shop.

#!/usr/bin/env ruby

alias gets1 gets
def gets
  gets1.rstrip
end

gets.to_i.times do
  min_l = nil; cost_l = nil
  max_r = nil; cost_r = nil
  longest_len = nil; longest_cost = nil

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

    if !min_l || l < min_l || (l == min_l && c < cost_l)
      min_l = l
      cost_l = c
    end

    if !max_r || r > max_r || (r == max_r && c < cost_r)
      max_r = r
      cost_r = c
    end

    len = r - l + 1
    if !longest_len || len > longest_len || (len == longest_len && c < longest_cost)
      longest_len = len
      longest_cost = c
    end

    answer = cost_l + cost_r
    if (max_r - min_l + 1) == longest_len && longest_cost < answer
      answer = longest_cost
    end

    puts answer
  end
end

#!/usr/bin/env ruby

alias gets1 gets
def gets
  gets1.rstrip
end

Seg = Struct.new(:l, :r, :c)

gets.to_i.times do
  n = gets.to_i
  segs = Array.new(n) {
    Seg.new *gets.split.map(&:to_i)
  }

  ml = nil; mr = nil
  segs.each { |seg|
    ml = seg if !ml || seg.l < ml.l || (seg.l == ml.l && seg.c < ml.c)
    mr = seg if !mr || seg.r > mr.r || (seg.r == mr.r && seg.c < mr.c)
    if ml.l == mr.l && ml.r == mr.r
      ml = mr = ml.c < mr.c ? ml : mr
    end
    ml = mr if mr.l == ml.l
    mr = ml if ml.r == mr.r
    cost  = ml.c
    cost += mr.c unless mr == ml
    cost = seg.c if seg.l == ml.l && seg.r == mr.r && seg.c < cost
    puts cost
  }
end

#!/usr/bin/env ruby

alias gets1 gets
def gets
  gets1.rstrip
end

Seg = Struct.new(:l, :r, :c)

gets.to_i.times do
  n = gets.to_i
  segs = Array.new(n) {
    Seg.new *gets.split.map(&:to_i)
  }

  ml = nil; mr = nil
  segs.each { |seg|
    ml = seg if !ml || seg.l < ml.l || (seg.l == ml.l && seg.c < ml.c)
    mr = seg if !mr || seg.r > mr.r || (seg.r == mr.r && seg.c < mr.c)
    if ml.l == mr.l && ml.r == mr.r
      ml = mr = ml.c < mr.c ? ml : mr
    end
    ml = mr if mr.l == ml.l
    mr = ml if ml.r == mr.r
    cost  = ml.c
    cost += mr.c unless mr == ml
    const = seg.c if seg.l == ml.l && seg.r == mr.r && seg.c < cost
    puts cost
  }
end

#!/usr/bin/env ruby

alias gets1 gets
def gets
  gets1.rstrip
end

Seg = Struct.new(:l, :r, :c)

gets.to_i.times do
  n = gets.to_i
  segs = Array.new(n) {
    Seg.new *gets.split.map(&:to_i)
  }

  ml = nil; mr = nil
  segs.each { |seg|
    ml = seg if !ml || seg.l < ml.l || (seg.l == ml.l && seg.c < ml.c)
    mr = seg if !mr || seg.r > mr.r || (seg.r == mr.r && seg.c < mr.c)
    if ml.l == mr.l && ml.r == mr.r
      ex = ml.c < mr.c ? ml : mr
      ml = mr = seg.c < ex.c ? seg : ex
    end
    ml = mr if mr.l == ml.l
    mr = ml if ml.r == mr.r
    cost  = ml.c
    cost += mr.c unless mr == ml
    puts cost
  }
end

#!/usr/bin/env ruby

alias gets1 gets
def gets
  gets1.rstrip
end

Seg = Struct.new(:l, :r, :c)

gets.to_i.times do
  n = gets.to_i
  segs = Array.new(n) {
    Seg.new *gets.split.map(&:to_i)
  }

  ml = nil; mr = nil
  segs.each { |seg|
    ml = seg if !ml || seg.l < ml.l || (seg.l == ml.l && seg.c < ml.c)
    mr = seg if !mr || seg.r > mr.r || (seg.r == mr.r && seg.c < mr.c)
    if ml.l == mr.l && ml.r == mr.r
      ml = mr = ml.c < mr.c ? ml : mr
    end
    ml = mr if mr.l == ml.l
    mr = ml if ml.r == mr.r
    cost  = ml.c
    cost += mr.c unless mr == ml
    puts cost
  }
end

#!/usr/bin/env ruby

alias gets1 gets
def gets
  gets1.rstrip
end

Seg = Struct.new(:l, :r, :c)

gets.to_i.times do
  n = gets.to_i
  segs = Array.new(n) {
    Seg.new *gets.split.map(&:to_i)
  }

  ml = nil; mr = nil
  segs.each { |seg|
    ml = seg if !ml || seg.l < ml.l || (seg.l == ml.l && seg.c < ml.c)
    mr = seg if !mr || seg.r > mr.r || (seg.r == mr.r && seg.c < mr.c)
    ml = mr if mr.l <= ml.l
    mr = ml if ml.r >= mr.r
    cost  = ml.c
    cost += mr.c unless mr == ml
    puts cost
  }
end

#!/usr/bin/env ruby

alias gets1 gets
def gets
  gets1.rstrip
end

gets.to_i.times do
  ml  = nil
  mr  = nil
  mlc = nil
  mrc = nil
  gets.to_i.times do
    l, r, c = gets.split.map(&:to_i)
    used = false
    if !ml || l < ml || (l == ml && c < mlc)
      ml = l
      mlc = c
      c = 0
      used = true
      mrc = 0 if r == mr
    end
    if !mr || r > mr || (r == mr && c < mrc)
      mr = r
      mrc = c
      c = 0
      mlc = 0 if !used && l == ml
    end
    puts(mlc + mrc)
  end
end

#!/usr/bin/env ruby

gets.to_i.times do
  ml  = nil
  mr  = nil
  mlc = nil
  mrc = nil
  gets.to_i.times do
    l, r, c = gets.split.map(&:to_i)
    if !ml || l < ml || (l == ml && c < mlc)
      ml = l
      mlc = c
      c = 0
      mrc = 0 if r == mr
    end
    if !mr || r > mr || (r == mr && c < mrc)
      mr = r
      mrc = c
      c = 0
      mlc = 0 if l == ml
    end
    puts(mlc + mrc)
  end
end


A binary string is a string where each character is either 0 or 1. Two binary strings $$$a$$$ and $$$b$$$ of equal length are similar, if they have the same character in some position (there exists an integer $$$i$$$ such that $$$a_i = b_i$$$). For example:  10010 and 01111 are similar (they have the same character in position $$$4$$$);  10010 and 11111 are similar;  111 and 111 are similar;  0110 and 1001 are not similar. You are given an integer $$$n$$$ and a binary string $$$s$$$ consisting of $$$2n-1$$$ characters. Let's denote $$$s[l..r]$$$ as the contiguous substring of $$$s$$$ starting with $$$l$$$-th character and ending with $$$r$$$-th character (in other words, $$$s[l..r] = s_l s_{l + 1} s_{l + 2} \dots s_r$$$).You have to construct a binary string $$$w$$$ of length $$$n$$$ which is similar to all of the following strings: $$$s[1..n]$$$, $$$s[2..n+1]$$$, $$$s[3..n+2]$$$, ..., $$$s[n..2n-1]$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 50$$$). The second line of each test case contains the binary string $$$s$$$ of length $$$2n - 1$$$. Each character $$$s_i$$$ is either 0 or 1.

For each test case, print the corresponding binary string $$$w$$$ of length $$$n$$$. If there are multiple such strings — print any of them. It can be shown that at least one string $$$w$$$ meeting the constraints always exists.

NoteThe explanation of the sample case (equal characters in equal positions are bold):The first test case:   $$$\mathbf{1}$$$ is similar to $$$s[1..1] = \mathbf{1}$$$. The second test case:   $$$\mathbf{000}$$$ is similar to $$$s[1..3] = \mathbf{000}$$$;  $$$\mathbf{000}$$$ is similar to $$$s[2..4] = \mathbf{000}$$$;  $$$\mathbf{000}$$$ is similar to $$$s[3..5] = \mathbf{000}$$$. The third test case:   $$$\mathbf{1}0\mathbf{10}$$$ is similar to $$$s[1..4] = \mathbf{1}1\mathbf{10}$$$;  $$$\mathbf{1}01\mathbf{0}$$$ is similar to $$$s[2..5] = \mathbf{1}10\mathbf{0}$$$;  $$$\mathbf{10}1\mathbf{0}$$$ is similar to $$$s[3..6] = \mathbf{10}0\mathbf{0}$$$;  $$$1\mathbf{0}1\mathbf{0}$$$ is similar to $$$s[4..7] = 0\mathbf{0}0\mathbf{0}$$$. The fourth test case:   $$$0\mathbf{0}$$$ is similar to $$$s[1..2] = 1\mathbf{0}$$$;  $$$\mathbf{0}0$$$ is similar to $$$s[2..3] = \mathbf{0}1$$$.

times = gets.to_i

def resolve(binary_s, w_size)
  w = ""
  (0..w_size-1).each do |i|
    if i == 0
      w = binary_s[i..w_size-1]
   else
      binary_s[i..w_size+(i-1)].each_char.with_index {|c,index| w[index]="1" if c!=w[index]}
    end
  end
  w
end

(1..times).each do |i|
  w_size = gets.to_i
  s = gets.chomp
  puts resolve(s, w_size)
end

# cook your code here
t = gets.chomp.to_i
t.times {
    n = gets.chomp.to_i
    s = gets.chomp.split('')
    ans = ''
    i  = 0
    j = 0
    while i < n do
        ans = ans + s[j].to_s
        j += 2
        i += 1
    end
    puts ans
}

t = gets.to_i
ans = []
t.times do
  n = gets.to_i
  s = gets.chomp
  len = s.length
  arr = []
  (len-n+1).times do |i|
    arr << s[i..i+n-1]
  end
  bit = Array.new(n,"-1")
  n.times do |i|
    flg = true
    s = arr[0][i]
    (len-n+1).times do |j|
      if arr[j][i] != s
        flg = false
      end
    end
    if flg
      bit[i] = s
    else
      bit[i] = "0"
    end
  end
  str = ""
  n.times do |i|
    str = str+bit[i]
  end
  ans << str
end
ans.each do |x|
  puts x
end

gets.to_i.times do
    gets.to_i
    s = gets.split('')
    a = []
    s.each_with_index { |v, i| a << v if i.even? }
    puts a.join
end

gets.to_i.times do
    gets.to_i
    s = gets.split('')
    a = []
    s.each_with_index { |v, i| a << v if i.even? }
    a.join
end


On a number line there are n balls. At time moment 0 for each ball the following data is known: its coordinate xi, speed vi (possibly, negative) and weight mi. The radius of the balls can be ignored.The balls collide elastically, i.e. if two balls weighing m1 and m2 and with speeds v1 and v2 collide, their new speeds will be: .Your task is to find out, where each ball will be t seconds after.

The first line contains two integers n and t (1 ≤ n ≤ 10, 0 ≤ t ≤ 100) — amount of balls and duration of the process. Then follow n lines, each containing three integers: xi, vi, mi (1 ≤ |vi|, mi ≤ 100, |xi| ≤ 100) — coordinate, speed and weight of the ball with index i at time moment 0. It is guaranteed that no two balls have the same coordinate initially. Also each collision will be a collision of not more than two balls (that is, three or more balls never collide at the same point in all times from segment [0;t]).

Output n numbers — coordinates of the balls t seconds after. Output the numbers accurate to at least 4 digits after the decimal point.

n, t = gets.chomp.split.map { |e| e.to_i }

balls = []
for i in 0...n
	balls <<= gets.chomp.split.map { |e| e.to_f }
end

def calcu_v v0, m0, v1, m1
	nv0 = ((m0 - m1) * v0 + 2 * m1 * v1) / (m0 + m1)
	nv1 = ((m1 - m0) * v1 + 2 * m0 * v0) / (m0 + m1)
	return nv0, nv1
end

def calcu_p(x0, v0, x1, v1)
	(x1 * v0 - x0 * v1) / (v0 - v1)
end

flags = Array.new(n) { false }
now = 0
while now < t
	next_time = t - now
	next_a = -1
	next_b = -1
	for i in 0...n
		for j in 0...i
			if (balls[i][1] - balls[j][1]).abs > 0.00001
				col_time = - (balls[i][0] - balls[j][0]) / (balls[i][1] - balls[j][1])
				if col_time > 0 && col_time < next_time
					next_time = col_time
					next_a = i
					next_b = j
				end
			end
		end
	end
	#if next_a != -1 and next_b != -1
		#pos = (balls[next_b][0] * balls[next_a][1] - balls[next_a][0] - balls[next_b][1]) / (balls[next_a][1] - balls[next_b][1])
	#	pos = calcu_p(balls[next_a][0], balls[next_a][1], balls[next_b][0], balls[next_b][1])
	#	balls[next_a][1], balls[next_b][1] = calcu_v(balls[next_a][1], balls[next_a][2], balls[next_b][1], balls[next_b][2])
	#	balls[next_a][0] = pos
	#	balls[next_b][0] = pos
	#end

	for i in 0...n
		flags[i] = false
	end
	for i in 0...n
		for j in 0...i
			if !flags[i] && !flags[j] && (balls[i][1] - balls[j][1]).abs > 0.00001
				col_time = - (balls[i][0] - balls[j][0]) / (balls[i][1] - balls[j][1])
				if (col_time - next_time).abs < 0.00001
					pos = calcu_p(balls[i][0], balls[i][1], balls[j][0], balls[j][1])
					balls[i][1], balls[j][1] = calcu_v(balls[i][1], balls[i][2], balls[j][1], balls[j][2])
					balls[i][0] = balls[j][0] = pos
					flags[i] = true
					flags[j] = true
				end
			end
		end
	end

	for i in 0...n
		if !flags[i]
			balls[i][0] += balls[i][1] * next_time
		end
	end
	now += next_time

	# print balls.to_s, "\n"
end

for i in 0...n
	print balls[i][0], "\n"
end

input = gets.chomp.split
n = Integer(input[0])
t = Integer(input[1])
balls = []
for i in 0...n
    balls[i] = []
    input1 = gets.chomp.split
    for j in 0...3
        balls[i][j] = Integer(input1[j])
    end
end

for i in 0...n
    balls[i][3] = i
end
balls = balls.sort_by{|a| a[0]}

def balls_state(balls, first_collide_i, first_collide_time, n)
    for i in 0...n
        balls[i][0] = balls[i][1] * first_collide_time + balls[i][0]
    end
    for i in 0...first_collide_i.length
        j = first_collide_i[i]
        v_0 = ((balls[j][2] - balls[j+1][2]) * balls[j][1] + 2 * balls[j+1][2] * balls[j+1][1]) / ((balls[j][2] + balls[j+1][2]).to_f)
        v_1 = ((balls[j+1][2] - balls[j][2]) * balls[j+1][1] + 2 * balls[j][2] * balls[j][1]) / ((balls[j][2] + balls[j+1][2]).to_f)
        balls[j][1] = v_0
        balls[j+1][1] = v_1
    end
    return balls
end

def next_collide(balls, n)
    collide_time = []
    s = 0
    v = 0
    ct = 0
    for i in 0...n-1
        s = balls[i+1][0] - balls[i][0]
        v = balls[i][1] - balls[i+1][1]
        if balls[i][1] < 0 && balls[i+1][1] < 0
            if balls[i][1] > balls[i+1][1]
                ct = s.to_f / v
                collide_time.push([i, ct])
            end
        elsif balls[i][1] > 0
            if balls[i+1][1] > 0 && balls[i][1] > balls[i+1][1]
                ct = s.to_f / v
                collide_time.push([i, ct])

            elsif balls[i+1][1] < 0
                ct = s.to_f / v
                collide_time.push([i, ct])
            end
        end
    end
    collide_time = collide_time.sort_by{|a| a[1]}
    if collide_time != []
        first_collide_i = []
        first_collide_time = collide_time[0][1]
        for i in 0...collide_time.length
            if collide_time[i][1] == first_collide_time
                first_collide_i.push(collide_time[i][0])
            end
        end
    else
        first_collide_time = 1000
        first_collide_i = []
    end
    return first_collide_time, first_collide_i
end

stop = false
while !stop do
    first_collide_time, first_collide_i = next_collide(balls, n)
    if t <= first_collide_time
        for i in 0...n
            balls[i][0] += balls[i][1] * t
        end
        result = balls.sort_by{|a| a[3]}
        for i in 0...n
            puts result[i][0]
        end
        stop = true
    else
        t -= first_collide_time
        balls = balls_state(balls, first_collide_i, first_collide_time, n)
    end
end

n, t = gets.chomp.split.map { |e| e.to_i }

balls = []
for i in 0...n
	balls <<= gets.chomp.split.map { |e| e.to_f }
end

def calcu_v v0, m0, v1, m1
	nv0 = ((m0 - m1) * v0 + 2 * m1 * v1) / (m0 + m1)
	nv1 = ((m1 - m0) * v1 + 2 * m0 * v0) / (m0 + m1)
	return nv0, nv1
end

def calcu_p(x0, v0, x1, v1)
	(x1 * v0 - x0 * v1) / (v0 - v1)
end

now = 0
while now < t
	next_time = t - now
	next_a = -1
	next_b = -1
	for i in 0...n
		for j in 0...i
			if (balls[i][1] - balls[j][1]).abs > 0.00001
				col_time = - (balls[i][0] - balls[j][0]) / (balls[i][1] - balls[j][1])
				if col_time > 0 && col_time < next_time
					next_time = col_time
					next_a = i
					next_b = j
				end
			end
		end
	end
	if next_a != -1 and next_b != -1
		#pos = (balls[next_b][0] * balls[next_a][1] - balls[next_a][0] - balls[next_b][1]) / (balls[next_a][1] - balls[next_b][1])
		pos = calcu_p(balls[next_a][0], balls[next_a][1], balls[next_b][0], balls[next_b][1])
		balls[next_a][1], balls[next_b][1] = calcu_v(balls[next_a][1], balls[next_a][2], balls[next_b][1], balls[next_b][2])
		balls[next_a][0] = pos
		balls[next_b][0] = pos
	end

	now += next_time
	for i in 0...n
		if i != next_a && i != next_b
			balls[i][0] += balls[i][1] * next_time
		end
	end
	# print balls.to_s, "\n"
end

for i in 0...n
	print balls[i][0], "\n"
end

n, t = gets.chomp.split.map { |e| e.to_i }

balls = []
for i in 0...n
	balls <<= gets.chomp.split.map { |e| e.to_f }
end

def calcu_v v0, m0, v1, m1
	nv0 = ((m0 - m1) * v0 + 2 * m1 * v1) / (m0 + m1)
	nv1 = ((m1 - m0) * v1 + 2 * m0 * v0) / (m0 + m1)
	return nv0, nv1
end

def calcu_p(x0, v0, x1, v1)
	(x1 * v0 - x0 * v1) / (v0 - v1)
end

flags = Array.new(n) { false }
now = 0
while now < t
	next_time = t - now
	next_a = -1
	next_b = -1
	for i in 0...n
		for j in 0...i
			if (balls[i][1] - balls[j][1]).abs > 0.00001
				col_time = - (balls[i][0] - balls[j][0]) / (balls[i][1] - balls[j][1])
				if col_time > 0 && col_time < next_time
					next_time = col_time
					next_a = i
					next_b = j
				end
			end
		end
	end
	#if next_a != -1 and next_b != -1
		#pos = (balls[next_b][0] * balls[next_a][1] - balls[next_a][0] - balls[next_b][1]) / (balls[next_a][1] - balls[next_b][1])
	#	pos = calcu_p(balls[next_a][0], balls[next_a][1], balls[next_b][0], balls[next_b][1])
	#	balls[next_a][1], balls[next_b][1] = calcu_v(balls[next_a][1], balls[next_a][2], balls[next_b][1], balls[next_b][2])
	#	balls[next_a][0] = pos
	#	balls[next_b][0] = pos
	#end

	for i in 0...n
		flags[i] = false
	end
	for i in 0...n
		for j in 0...i
			if !flags[i] && !flags[j] && (balls[i][1] - balls[j][1]).abs > 0.00001
				col_time = - (balls[i][0] - balls[j][0]) / (balls[i][1] - balls[j][1])
				if (col_time - next_time).abs < 0.00001
					pos = calcu_p(balls[i][0], balls[i][1], balls[j][0], balls[j][1])
					balls[i][1], balls[j][1] = calcu_v(balls[i][1], balls[i][2], balls[j][1], balls[j][2])
					balls[i][0] = balls[j][0] = pos
					flags[i] = true
					flags[j] = true
				end
			end
		end
	end

	for i in 0...n
		if !flags[i]
			balls[i][0] += balls[i][1] * next_time
		end
	end
	now += next_time

	print balls.to_s, "\n"
end

for i in 0...n
	print balls[i][0], "\n"
end


While sailing on a boat, Inessa noticed a beautiful water lily flower above the lake's surface. She came closer and it turned out that the lily was exactly $$$H$$$ centimeters above the water surface. Inessa grabbed the flower and sailed the distance of $$$L$$$ centimeters. Exactly at this point the flower touched the water surface.  Suppose that the lily grows at some point $$$A$$$ on the lake bottom, and its stem is always a straight segment with one endpoint at point $$$A$$$. Also suppose that initially the flower was exactly above the point $$$A$$$, i.e. its stem was vertical. Can you determine the depth of the lake at point $$$A$$$?

The only line contains two integers $$$H$$$ and $$$L$$$ ($$$1 \le H < L \le 10^{6}$$$).

Print a single number — the depth of the lake at point $$$A$$$. The absolute or relative error should not exceed $$$10^{-6}$$$. Formally, let your answer be $$$A$$$, and the jury's answer be $$$B$$$. Your answer is accepted if and only if $$$\frac{|A - B|}{\max{(1, |B|)}} \le 10^{-6}$$$.

h,l = gets.split.map(&:to_i)
puts (l*l-h*h)/(2.0*h)

h,l = gets.chomp.split.map(&:to_i)

depth = 1.0 * (l*l - h*h) / (2*h)
printf("%.6f\n", depth)


Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.

The first line contains a single integer n (1 ≤ n ≤ 105), which represents how many numbers the array has. The next line contains n space-separated integers — the array's description. All elements of the array lie in the range from 1 to 109, inclusive.

Print n space-separated integers — the minimum possible values of each array element after one replacement and the sorting are performed.

n=gets.to_i
a=gets.split.map(&:to_i)
a.sort!
b=1
f=false
a.each{|i|
        if i!=1 then
                f=true
        end
}
cnt=0
a.each{|i|
        r=i
        if i!=b then
                r=b
        end
        if cnt==n-1 && f==false then
                r=2
        end
        print r
        print " "
        b=i
        cnt+=1
}

a = STDIN.readlines[1].split.map(&:to_i).sort
a[-1] = (a[-1] == 1 ? 2 : 1)
puts a.sort.join ' '

gets()
a = gets().split(' ').map(&:to_i).sort()
if a[-1] == 1 then
  a[-1] = 2
else
  a[-1] = 1
  a.sort!()
end

puts a.join(' ')

#=================================
require 'prime'
require 'set'
require 'matrix'
require 'tsort'
include Math

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

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

data.sort!

if data[-1]!=1
        data[-1]=1
else
        data[-1]=2
end
data.sort!

data.each{|v| print "#{v} "}

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

a[-1] = (a[-1]==1) ? 2 : 1

a.sort!
puts a.join(' ')

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
a = a.sort
if a.pop != 1
    puts ([1]+a).join(" ")
else
    puts (a+[2]).join(" ")
end

n=gets.to_i;
a=gets.split.map{|x| x.to_i }.sort;
a[n-1]=(a[n-1]==1)?2:1;
a.sort.map{|x| print(x," "); };
puts();

#$stdin = File.open("input.txt")
#$stdout = File.open("output.txt","w")

N = gets.chomp.to_i
A = gets.chomp.split.map { |c| c = c.to_i }
A.sort!
if A[-1] == 1
	A[-1] = 2
else
	A[-1] = 1
end
A.sort!
puts A.join(" ")

#!/usr/bin/ruby

def readNextValues
  return gets.chomp.split.collect {|x| x.to_i }
end

n = readNextValues
arr = readNextValues

max_i = 0
max_val = 0

for i in 0..arr.size-1
  if arr[i] > max_val
    max_i = i
    max_val = arr[i]
  end
end

if max_val == 1
  arr[max_i] = 2
else
  arr[max_i] = 1
end

arr.sort!
puts arr.join(' ')

#!/usr/bin/ruby
def readNextValues
  return gets.chomp.split.map(&:to_i)
end
n = gets.chomp.to_i
arr = readNextValues

max_val = arr.max
max_i = arr.index(max_val)
arr[max_i] = (max_val == 1) ? 2 : 1

puts arr.sort.join(' ')

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

if a.max > 1
  i = a.index a.max
  a[i] = 1
else
  a[0] = 2
end

print a.sort * " "

n=gets.to_i
a=gets.split.map(&:to_i).sort.unshift(1)
puts a.last==1 ? ("1 "*(n-1))+"2" : a.take(n)*" "

n = gets
p = gets
=begin

from = Array.new(n.to_i)
to = p.split(' ')
i = 1
for el in to
  from[el.to_i-1] = i
  i+=1
end
puts from.join(' ').chomp
=end

arr1 = p.chomp.split(' ')
arr2 = []
arr1 = arr1.each{|el| arr2 << el.to_i}
arr1 = arr2.sort
max = arr1[-1]
if max == 1
  puts  arr1[0..-2].join(' ').chomp+" 2"
else
  puts "1 " + arr1.sort[0..-2].join(' ')
end

n=gets.to_i
a=gets.split.map(&:to_i)
a.sort
b=1
f=false
a.each{|i|
        if i!=1 then
                f=true
        end
}
cnt=0
a.each{|i|
        r=i
        if i!=b then
                r=b
        end
        if cnt==n-1 && f==false then
                r=2
        end
        p r
        b=i
        cnt+=1
}

n=gets.to_i
a=gets.split.map(&:to_i)
a.sort
b=1
f=false
a.each{|i|
        if a[0]!=1 then
                f=true
        end
}
cnt=0
a.each{|i|
        r=i
        if i!=b then
                r=b
        end
        if cnt==n-1 && f==false then
                r=2
        end
        p r
        b=i
}

n=gets.to_i
a=gets.split.map(&:to_i)
a.sort
b=1
f=false
a.each{|i|
        if i!=1 then
                f=true
        end
}
cnt=0
a.each{|i|
        r=i
        if i!=b then
                r=b
        end
        if cnt==n-1 && f==false then
                r=2
        end
        print r
        print " "
        b=i
        cnt+=1
}

n=gets.to_i
a=gets.split.map(&:to_i)
a.sort
b=1
a.each{|i|
        r=i
        if i!=b then
                r=b
        end
        p r
        b=i
}

gets()
a = gets().split(' ').map(&:to_i).sort()
if a[-1] == 1 then
  a[-1] = 2
else
  a[0] = 1
end

puts a.join(' ')

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

if a[-1] == 1
    a[-1] = 2
else
    a[-1] = 1
    a.sort!
end

puts a.join(' ')

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

a[-1] = (a[-1]==1) ? 2 : 1

a.sort!
puts a.join(' ')

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
a = [1]+a.sort
a.pop
puts a.join(" ")

a = STDIN.readlines[1].split.map(&:to_i).sort
a[-1] = 1
puts a.sort.join ' '

#!/usr/bin/ruby

def readNextValues
  return gets.chomp.split.collect {|x| x.to_i }
end

n = readNextValues
arr = readNextValues

max_i = 0
max_val = 0
for i in 0..arr.size-1
  if arr[i] > max_i
    max_i = i
    max_val = arr[i]
  end
end

if max_val == 1
  arr[max_i] = 2
else
  arr[max_i] = 1
end

arr.sort!
puts arr.join(' ')

#!/usr/bin/ruby

def readNextValues
  return gets.chomp.split.collect {|x| x.to_i }
end

n = readNextValues
arr = readNextValues

max_i = 0
max_val = 0
for i in 0..arr.size-1
  if arr[i] > max_i
    max_i = i
    max_val = arr[i]
  end
end

if max_val == 1
  arr[i] = 2
else
  arr[i] = 1
end

arr.sort!
puts arr.join(' ')

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

if a.max > 1
  i = a.index a.max
  a[i] = 1
end

print a.sort * " "

#=================================
require 'prime'
require 'set'
require 'matrix'
require 'tsort'
include Math

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

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

data.sort!
data[-1]=1
data.sort!

data.each{|v| print "#{v} "}

n = gets
p = gets
=begin

from = Array.new(n.to_i)
to = p.split(' ')
i = 1
for el in to
  from[el.to_i-1] = i
  i+=1
end
puts from.join(' ').chomp
=end

arr1 = p.chomp.split(' ')
arr1 = arr1.sort
max = arr1[-2]
if max == '1'
  puts  arr1[0..-2].join(' ').chomp+" 2"
else
  puts "1 " + arr1.sort[0..-2].join(' ')
end

n = gets
p = gets
arr1 = p.chomp.split(' ')
arr1 = arr1.sort
max = arr1[-2]
i=1
if max == 1
  i = 2
end
puts "#{i} " + arr1.sort[0..-2].join(' ')

n = gets
p = gets
=begin

from = Array.new(n.to_i)
to = p.split(' ')
i = 1
for el in to
  from[el.to_i-1] = i
  i+=1
end
puts from.join(' ').chomp
=end

arr1 = p.chomp.split(' ')
arr2 = []
arr1 = arr1.each{|el| arr2 << el.to_i}
arr1 = arr2.sort
max = arr1[-1]
if max == '1'
  puts  arr1[0..-2].join(' ').chomp+" 2"
else
  puts "1 " + arr1.sort[0..-2].join(' ')
end

n = gets
p = gets
arr1 = p.chomp.split(' ')
puts '1 ' + arr1.sort[0..-1].join(' ')

n = gets
p = gets
=begin

from = Array.new(n.to_i)
to = p.split(' ')
i = 1
for el in to
  from[el.to_i-1] = i
  i+=1
end
puts from.join(' ').chomp
=end

arr1 = p.chomp.split(' ')
arr1 = arr1.sort
max = arr1[-1]
if max == '1'
  puts  arr1[0..-2].join(' ').chomp+" 2"
else
  puts "1 " + arr1.sort[0..-2].join(' ')
end

n = gets
p = gets
arr1 = p.chomp.split(' ')
puts '1 ' + arr1.sort[0..-2].join(' ')

n = gets
p = gets
=begin

from = Array.new(n.to_i)
to = p.split(' ')
i = 1
for el in to
  from[el.to_i-1] = i
  i+=1
end
puts from.join(' ').chomp
=end

arr1 = p.chomp.split(' ')
arr1 = arr1.sort
max = arr1[-1]
puts max
if max == '1'
  puts  arr1[0..-2].join(' ').chomp+" 2"
else
  puts "1 " + arr1.sort[0..-2].join(' ')
end

n = gets
p = gets
arr1 = p.chomp.split(' ')
arr1 = arr1.sort
max = arr1[-2]
if max == 1
  puts  arr1.sort[0..-2].join(' ')+" 2"
else
  puts "1 " + arr1.sort[0..-2].join(' ')
end

n=gets.to_i
puts ("1 "+gets).split.map(&:to_i).sort.take(n)*" "


You are given an equation:  Ax2 + Bx + C = 0. Your task is to find the number of distinct roots of the equation and print all of them in ascending order.

The first line contains three integer numbers A, B and C ( - 105 ≤ A, B, C ≤ 105). Any coefficient may be equal to 0.

In case of infinite root count print the only integer -1. In case of no roots print the only integer 0. In other cases print the number of root on the first line and the roots on the following lines in the ascending order. Print roots with at least 5 digits after the decimal point.

(a,b,c)=gets.split.map{|i|i=i.to_i}
if a==0
  if b==0 then puts c==0?"-1":"0";exit end
  puts "1";p -c.to_f/b;exit
end
if (d=b*b-4*a*c)<0 then puts "0";exit end
if d==0 then puts "1";p -b.to_f/2/a;exit end
puts "2"
if a<0 then a=-a;b=-b;c=-c end
p (-b-Math.sqrt(d))/2/a,(-b+Math.sqrt(d))/2/a

(a,b,c)=gets.split.map{|i|i=i.to_i}
if a==0&&b==0 then puts c==0?"-1":"0";exit end
if a==0
  puts "1"
  printf "%.9f\n", -c.to_f/b
  exit
end
if (d=b*b-4*a*c)<0 then puts "0";exit end
if d==0
  puts "1"
  printf "%.9f\n", -b.to_f/2/a
  exit
end
puts "2"
if a<0 then a=-a;b=-b;c=-c end
printf "%.9f\n", (-b-Math.sqrt(d))/2/a
printf "%.9f\n", (-b+Math.sqrt(d))/2/a

(a,b,c)=gets.split.map{|i|i=i.to_i}
if a==0&&b==0 then puts c==0?"-1":"0";exit end
if a==0
  puts "1"
  printf "%.9f\n", -c.to_f/b
  exit
end
if (d=b*b-4*a*c)<0 then puts "0";exit end
if d==0
  puts "1"
  printf "%.9f\n", -b.to_f/2/a
  exit
end
puts "2"
if a<0 then a=-a;b=-b;c=-c end
p (-b-Math.sqrt(d))/2/a
p (-b+Math.sqrt(d))/2/a

input=gets.split.map(&:to_i)
a=input[0]
b=input[1]
c=input[2]
if a<0 then
        b=-b
        c=-c
        a=-a
end
d=b*b-4*a*c
if a==0 then
        if b==0 then
                if c==0 then
                        puts -1
                else
                        puts 0
                end
        else
                puts 1
                printf "%.7f\n",-c*1.0/b
        end
elsif d==0 then
        puts 1
        printf "%.7f\n",-b/(2*a)
elsif d>0 then
        puts 2
        printf "%.7f\n",(-b-Math::sqrt(d))/(2*a)
        printf "%.7f\n",(-b+Math::sqrt(d))/(2*a)
else
        puts 0
end

def printAns(num)
  printf("%10.10f\n",num)
end
c=gets.chomp.split.map{|i| i.to_i}
if c[0]==0
  if c[1]==0
    if c[2]==0
      puts(-1)
    else
      puts(0)
    end
  else
    puts(1)
    printAns(-c[2].to_f/c[1].to_f)
  end
else
  D=c[1]*c[1]-4*c[0]*c[2]
  if D<0
    puts(0)
    exit
  elsif D==0
    puts(1)
    ans=-c[1].to_f/(2.0*c[0].to_f)
    printAns(ans)
  else
    puts(2)
    r=Math.sqrt(D)
    if c[0] < 0
      c[0]=-c[0]
      c[1]=-c[1]
    end
    printAns((-c[1].to_f-r)/(2*c[0]))
    printAns((-c[1].to_f+r)/(2*c[0]))
  end
end

def printAns(num)
  printf("%10.10f\n",num)
end
(a,b,c)=gets.chomp.split.map{|i| i.to_i}
if a==0
  if b==0
    if c==0
      puts(-1)
    else
      puts(0)
    end
  else
    puts(1)
    printAns(-c.to_f/b.to_f)
  end
else
  D=b*b-4*a*c
  if D<0
    puts(0)
    exit
  elsif D==0
    puts(1)
    ans=-b.to_f/(2.0*a.to_f)
    printAns(ans)
  else
    puts(2)
    r=Math.sqrt(D)
    if a < 0
      a=-a
      b=-b
    end
    printAns((-b.to_f-r)/(2*a))
    printAns((-b.to_f+r)/(2*a))
  end
end

a, b, c = gets.split.map(&:to_i)
if a == 0
	if b == 0
		puts c == 0 ? -1 : 0
	else
		puts 1
		puts "%.10f" % (-1.0 * c / b)
	end
else
	d = b * b - 4 * a * c
	if d < 0
		puts 0
	elsif d == 0
		puts 1
		puts "%.10f" % (-b / (2.0 * a))
	else
		dd = Math.sqrt(d)
		puts 2
		[(-b - dd) / (2.0 * a), (-b + dd) / (2.0 * a)].sort.each { |v| puts "%.10f" % v }
	end
end

(a,b,c)=gets.split.map{|i|i=i.to_i}
if a==0&&b==0 then puts c==0?"-1":"0";exit end
if a==0
  puts "1"
  printf "%.9f\n", -b.to_f/c
  exit
end
if (d=b*b-4*a*c)<0 then puts "0";exit end
if d==0
  puts "1"
  printf "%.9f\n", -b.to_f/2/a
  exit
end
puts "2"
if a<0 then a=-a;b=-b;c=-c end
printf "%.9f\n", (-b-Math.sqrt(d))/2/a
printf "%.9f\n", (-b+Math.sqrt(d))/2/a

(a,b,c)=gets.split.map{|i|i=i.to_i}
if a==0&&b==0 then puts c==0?"-1":"0";exit end
if a==0
  puts "1"
  p -b.to_f/c
  exit
end
if (d=b*b-4*a*c)<0 then puts "0";exit end
if d==0
  puts "1"
  p -b.to_f/2/a
  exit
end
puts "2"
p (-b+Math.sqrt(d))/2/a
p (-b-Math.sqrt(d))/2/a

(a,b,c)=gets.split.map{|i|i=i.to_i}
if a==0&&b==0 then puts c==0?"-1":"0";exit end
if a==0
  puts "1"
  printf "%.9f\n", -b.to_f/c
  exit
end
if (d=b*b-4*a*c)<0 then puts "0";exit end
if d==0
  puts "1"
  printf "%.9f\n", -b.to_f/2/a
  exit
end
puts "2"
printf "%.9f\n", (-b-Math.sqrt(d))/2/a
printf "%.9f\n", (-b+Math.sqrt(d))/2/a

(a,b,c)=gets.split.map{|i|i=i.to_i}
if a==0&&b==0 then puts c==0?"-1":"0";exit end
if a==0
  puts "1"
  printf "%.9f\n", -b.to_f/c
  exit
end
if (d=b*b-4*a*c)<0 then puts "0";exit end
if d==0
  puts "1"
  printf "%.9f\n", -b.to_f/2/a
  exit
end
puts "2"
printf "%.9f\n", (-b+Math.sqrt(d))/2/a
printf "%.9f\n", (-b-Math.sqrt(d))/2/a

input=gets.split.map(&:to_i)
a=input[0]
b=input[1]
c=input[2]
d=b*b-4*a*c
if d==0 then
        puts 1
        printf "%.7f\n",-b/(2*a)
elsif d>0 then
        puts 2
        printf "%.7f\n",(-b-Math::sqrt(d))/(2*a)
        printf "%.7f\n",(-b+Math::sqrt(d))/(2*a)
else
        puts 0
end

input=gets.split.map(&:to_i)
a=input[0]
b=input[1]
c=input[2]
if a<0 then
        b=-b
        c=-c
        a=-c
end
d=b*b-4*a*c
if a==0 then
        if b==0 then
                if c==0 then
                        puts -1
                else
                        puts 0
                end
        else
                puts 1
                printf "%.7f\n",-c*1.0/b
        end
elsif d==0 then
        puts 1
        printf "%.7f\n",-b/(2*a)
elsif d>0 then
        puts 2
        printf "%.7f\n",(-b-Math::sqrt(d))/(2*a)
        printf "%.7f\n",(-b+Math::sqrt(d))/(2*a)
else
        puts 0
end

input=gets.split.map(&:to_i)
a=input[0]
b=input[1]
c=input[2]
d=b*b-4*a*c
if a==0 then
        if b==0 then
                if c==0 then
                        puts -1
                else
                        puts 0
                end
        else
                puts 1
                printf "%.7f\n",-c*1.0/b
        end
elsif d==0 then
        puts 1
        printf "%.7f\n",-b/(2*a)
elsif d>0 then
        puts 2
        printf "%.7f\n",(-b-Math::sqrt(d))/(2*a)
        printf "%.7f\n",(-b+Math::sqrt(d))/(2*a)
else
        puts 0
end

input=gets.split.map(&:to_i)
a=input[0]
b=input[1]
c=input[2]
d=b*b-4*a*c
if a==0 then
        puts 1
        printf "%.7f\n",-c*1.0/b
elsif d==0 then
        puts 1
        printf "%.7f\n",-b/(2*a)
elsif d>0 then
        puts 2
        printf "%.7f\n",(-b-Math::sqrt(d))/(2*a)
        printf "%.7f\n",(-b+Math::sqrt(d))/(2*a)
else
        puts 0
end

def printAns(num)
  printf("%10.10f\n",num)
end
c=gets.chomp.split.map{|i| i.to_i}
if c[0]==0
  if c[1]==0
    if c[2]==0
      puts(-1)
    else
      puts(0)
    end
  else
    puts(1)
    printAns(-c[2].to_f/c[1].to_f)
  end
else
  D=c[1]*c[1]-4*c[0]*c[2]
  if D<0
    puts(0)
    exit
  elsif D==0
    puts(1)
    ans=-c[1]/(2*c[0])
    printAns(ans)
  else
    puts(2)
    r=Math.sqrt(D)
    printAns((-c[1]-r)/(2*c[0]))
    printAns((-c[1]+r)/(2*c[0]))
  end
end

def printAns(num)
  printf("%10.10f\n",num)
end
c=gets.chomp.split.map{|i| i.to_i}
if c[0]==0
  if c[1]==0
    if c[2]==0
      puts(-1)
    else
      puts(0)
    end
  else
    puts(1)
    printAns(-c[2]/c[1])
  end
else
  D=c[1]*c[1]-4*c[0]*c[2]
  if D<0
    puts(0)
    exit
  elsif D==0
    puts(1)
    ans=-c[1]/(2*c[0])
    printAns(ans)
  else
    puts(2)
    r=Math.sqrt(D)
    printAns((-c[1]-r)/(2*c[0]))
    printAns((-c[1]+r)/(2*c[0]))
  end
end

def printAns(num)
  printf("%10.10f\n",num)
end
c=gets.chomp.split.map{|i| i.to_i}
if c[0]==0
  if c[1]==0
    if c[2]==0
      puts(-1)
    else
      puts(0)
    end
  else
    puts(1)
    printAns(-c[2].to_f/c[1].to_f)
  end
else
  D=c[1]*c[1]-4*c[0]*c[2]
  if D<0
    puts(0)
    exit
  elsif D==0
    puts(1)
    ans=-c[1].to_f/(2.0*c[0].to_f)
    printAns(ans)
  else
    puts(2)
    r=Math.sqrt(D)
    printAns((-c[1].to_f-r)/(2*c[0]))
    printAns((-c[1].to_f+r)/(2*c[0]))
  end
end

def printAns(num)
  printf("%10.10f\n",num)
end
c=gets.chomp.split.map{|i| i.to_i}
if c[0]==0
  if c[1]==0
    if c[2]==0
      puts(-1)
    else
      puts(0)
    end
  else
    puts(1)
    printAns(-c[2].to_f/c[1].to_f)
  end
else
  D=c[1]*c[1]-4*c[0]*c[2]
  if D<0
    puts(0)
    exit
  elsif D==0
    puts(1)
    ans=-c[1].to_f/(2*c[0].to_f)
    printAns(ans)
  else
    puts(2)
    r=Math.sqrt(D)
    printAns((-c[1]-r)/(2*c[0]))
    printAns((-c[1]+r)/(2*c[0]))
  end
end

def printAns(num)
  printf("%10.10f\n",num)
end
c=gets.chomp.split.map{|i| i.to_i}
if c[0]==0
  if c[1]==0
    if c[2]==0
      puts(-1)
    else
      puts(0)
    end
  else
    puts(1)
    printAns(c[2]/c[1])
  end
else
  D=c[1]*c[1]-4*c[0]*c[2]
  if D<0
    puts(0)
    exit
  elsif D==0
    puts(1)
    ans=-c[1]/(2*c[0])
    printAns(ans)
  else
    puts(2)
    r=Math.sqrt(D)
    printAns((-c[1]-r)/(2*c[0]))
    printAns((-c[1]+r)/(2*c[0]))
  end
end

def printAns(num)
  printf("%10.10f\n",num)
end
c=gets.chomp.split.map{|i| i.to_i}
if c[0]==0
  if c[1]==0
    if c[2]==0
      puts(-1)
    else
      puts(0)
    end
  else
    puts(1)
    printAns(c[2]/c[1])
  end
else
  D=c[1]*c[1]-4*c[0]*c[2]
  if D<0
    puts(0)
    exit
  elsif D==0
    puts(1)
    ans=-c[1]/(2*c[0])
    printAns(ans)
  else
    puts(2)
    r=Math.sqrt(D)
    printAns((-c[1]+r)/(2*c[0]))
    printAns((-c[1]-r)/(2*c[0]))
  end
end

a, b, c = gets.split.map(&:to_i)
if a == 0
	if b == 0
		puts c == 0 ? -1 : 0
	else
		puts 1
		puts "%.10f" % (-c / b)
	end
else
	d = b * b - 4 * a * c
	if d < 0
		puts 0
	elsif d == 0
		puts 1
		puts "%.10f" % (-b / (2 * a))
	else
		dd = Math.sqrt(d)
		puts 2
		[(-b - dd) / (2 * a), (-b + dd) / (2 * a)].sort.each { |v| puts "%.10f" % v }
	end
end

a, b, c = gets.split.map(&:to_i)
if a == 0
	if b == 0
		puts c == 0 ? -1 : 0
	else
		puts 1
		puts -c / b
	end
else
	d = b * b - 4 * a * c
	if d < 0
		puts 0
	elsif d == 0
		puts 1
		puts -b / (2 * a)
	else
		dd = Math.sqrt(d)
		puts 2
		puts [(-b - dd) / (2 * a), (-b + dd) / (2 * a)].sort
	end
end


There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?Note that outcome of a match can not be a draw, it has to be either win or loss.

The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case.

For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes).

NoteSample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins).

n = gets.chomp.to_i

def sol_win(n, k, win)
	round = n / 3
	ok = true
	win.each do |i|
		ok = false if i > round
	end
	ok
end

def sol_real(n, k, d1, d2)
	win = []
	if (k - (d1 + d2)) % 3 != 0
		return false
	else
		t = (k - (d1 + d2)) / 3
		if t >= 0 and t + d1 >= 0 and t + d2 >= 0
			win = [t + d1, t, t + d2]
			return sol_win(n, k, win)
		else
			return false
		end
	end
end

# use second team as t
def sol(n, k, d1, d2)
	if n % 3 != 0
		return false
	end
	sol_real(n, k, d1, d2) or sol_real(n, k, -d1, d2) or sol_real(n, k, d1, -d2) or sol_real(n, k, -d1, -d2)
end

n.times do
	n, k, d1, d2 = gets.chomp.split.map{|i| i.to_i}
	if sol(n, k, d1, d2)
		puts "yes"
	elsif
		puts "no"
	end
end


Nicholas, a painter is going to paint several new canvases. Nicholas is sure that the canvases will turn out so great that each one will need framing and being hung on the wall. Frames are what Nicholas decided to begin with. Nicholas has n sticks whose lengths equal a1, a2, ... an. Nicholas does not want to break the sticks or glue them together. To make a h × w-sized frame, he needs two sticks whose lengths equal h and two sticks whose lengths equal w. Specifically, to make a square frame (when h = w), he needs four sticks of the same length.Now Nicholas wants to make from the sticks that he has as many frames as possible; to be able to paint as many canvases as possible to fill the frames. Help him in this uneasy task. Note that it is not necessary to use all the sticks Nicholas has.

The first line contains an integer n (1 ≤ n ≤ 100) — the number of sticks. The second line contains n space-separated integers. The i-th integer equals the length of the i-th stick ai (1 ≤ ai ≤ 100).

Print the single number — the maximum number of frames Nicholas can make for his future canvases.

gets
hs=Hash.new(0)
data=gets.split.map(&:to_i).each{|v| hs[v]+=1}
puts hs.values.map{|_| _%2!=0 ?	_-1	: _ }.inject(:+)/4

n = gets.to_i
a = Array.new(101,0)
vs = gets.split.map(&:to_i)
vs.each do |v|
	a[v] += 1
end
ans = 0
for i in 1..100 do
    ans += (a[i].div(2))
end
p ans.div(2)

n = gets.to_i
a = Hash.new(0)
gets.split.each{|i| a[i]+=1}
cnt = 0
a.each{|key,value| cnt+=value/2}
puts cnt/2

gets
sticks = gets.split.map(&:to_i)
p sticks.uniq.inject(0) {|sumstick, _| sumstick += (sticks.count(_) / 2)}  / 2

gets
puts gets.split.map { |x| x.to_i }.inject(Hash.new(0)) { |h, e| h[e] += 1; h }.values.map { |x| x - x % 2 }.inject(:+) / 4

n = gets.to_i
a = gets().split.map { |elem| elem.to_i }

pairs_count = 0

for i in 1..(n-1)
  for j in 0..(i - 1)
    if (a[i] == a[j])
      a[i] = 0
      a[j] = 0
      pairs_count += 1
      break
    end
  end
end

puts (pairs_count / 2).to_i

n = gets.to_i
a = gets.chomp.split(" ").map{|i| i.to_i}
l = []
dem = 0

(0..n-1).each do |i|
    l[i] = 0
end

(0..n-2).each do |i|
    if l[i] == 0
        (i+1..n-1).each do |j|
            if a[i] == a[j] && l[j] == 0
                l[i] = 1
                l[j] = 1
                dem += 1
                break
            end
        end
    end
end

puts dem/2

gets
a, ans = Array.new(101,0), 0
gets.split.map{ |x| a[x.to_i] += 1 }
a.each{ |x| ans += x/2 }
p ans/2

n = STDIN.gets.chomp.to_i
length = STDIN.gets.chomp.split(' ').map{|i| i.to_i}
arr = length.inject(Array.new(101).fill(0)) {|res, i| res[i] += 1; res}
puts arr.reduce(0){|tot, i| tot += (i / 2)} / 2

n=gets.to_i
a=gets.split.map(&:to_i)
cnt=Hash.new
cnt.default=0
a.each{|i| cnt[i]+=1 }
ans=0
1.upto(100){|i| ans+=cnt[i]/2 }
p ans/2

#!/usr/bin/env ruby

n = $stdin.readline().to_i

lens = ($stdin.readline().split().collect {|x| x.to_i})

def uniq(a)
	result = Array.new
	prev = nil
	count = 0
	a.sort.each { |x|
		if x != prev
			result << [prev, count] unless prev == nil
			prev = x
			count = 1
		else
			count += 1
		end
	}
	result << [prev, count]
end

pairs = 0

uniq(lens).each { |r|
	pairs += (r[1]/2).floor
}

puts (pairs/2).floor

n = STDIN.readline.to_i
sticks = STDIN.readline.split.map {|s| s.to_i }
count = {}
sticks.each do |stick|
  if count.include? stick
    count[stick] += 1
  else
    count[stick] = 1
  end
end
pairs = 0
count.values.each {|value| pairs += value/2 }
puts pairs/2

gets
d = {}
d.default = 0
gets.split().map(&:to_i).each{|e| d[e] += 1 }

ret = 0
d.each_value{ |e| ret += e/2 }

p ret / 2

s,n=0,(gets.to_i)
a=gets.split.map &:to_i
0.upto 101 do |i| s+=a.count(i)/2 end
puts s/2

n = gets.to_i
h = Hash.new(0)
gets.split.map(&:to_i).each { |x| h[x] += 1}
puts h.values.map { |x| x/2}.inject(:+) / 2


You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the numbers.

Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.

NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.

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

ans = 0
neg_c = 0
zero_c = 0
a.each do |v|
  if v < 0
    neg_c += 1
  elsif v == 0
    zero_c += 1
  end

  ans += (v.abs-1).abs
end

if neg_c.odd? && zero_c == 0
  ans += 2
end

puts ans

n = gets.to_i
sum = 0
negOne = 0
posOne = 0
zero = 0
arr = gets.split.map(&:to_i)
arr.each_with_index do |x, i|
    if x < 0
        sum += (x - (-1)).abs
        arr[i] = -1
        negOne += 1
    elsif x > 0
        sum += (x - 1).abs
        arr[i] = 1
        posOne += 1
    else
        zero += 1
    end
end
if negOne %2 != 0
    if zero > 0
        sum += 1
        negOne += 1
        zero -= 1
    else
        sum += 2
        negOne -= 1
        posOne += 1
    end
end
print(sum + zero)

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

negative_count = as.count { |a| a < 0 }
zero_count = as.count { |a| a == 0 }

ans = 0
as.each do |a|
  ans += [(a-(-1)).abs, (a-1).abs].min
end
if negative_count.odd?
  if zero_count > 0
  else
    ans += 2
  end
end

puts ans

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i
a = gets.split.map(&:to_i)
coins = 0
negs, zeroes, poss = 0, 0, 0
a.each { |i|
    if i < 0
        negs += 1
        coins += -1 - i
    elsif i == 0
        zeroes += 1
    else
        poss += 1
        coins += i - 1
    end
}

# not handled zeroes yet!
if negs.odd?
    if zeroes > 0
        zeroes -= 1
        coins += 1 # for zeroes, flipping to -1 or +1 only costs one coin
    else
        coins += 2 # have to pay two coins to flip -1 to +1 or +1 to -1
    end
end

# handle remaining zeroes
coins += zeroes

puts coins

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

cost = 0
plus_sum = 0
minus_sum = 0
plus_cnt = 0
minus_cnt = 0
zero_cnt = 0

aa.each do |a|
  if a<0
    minus_cnt+=1
    minus_sum+=a
  elsif a==0
    zero_cnt+=1
  else
    plus_cnt+=1
    plus_sum+=a
  end
end

cost += (-minus_sum - minus_cnt) + zero_cnt + (plus_sum - plus_cnt)
# マイナスが奇数個の場合はプラスになるように調整する
cost += 2 if minus_cnt%2==1 && zero_cnt==0
puts cost

n = gets.to_i
a = gets.split.map &:to_i
ans = 0
nnegatives = 0
nzeros = 0
a.each do |x|
  if x >= 1
    ans += x - 1
  elsif x <= -1
    ans += -1 - x
    nnegatives += 1
  else
    ans += 1
    nzeros += 1
  end
end
ans += 2 if nnegatives%2 != 0 and nzeros == 0
puts ans

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

negative_count = as.count { |a| a < 0 }
zero_count = as.count { |a| a == 0 }

ans = 0
as.each do |a|
  ans += [(a-(-1)).abs, (a-1).abs].min
end
if negative_count.odd?
  if zero_count > 0
    ans += 1
  else
    ans += 2
  end
end

puts ans

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

negative_count = as.count { |a| a < 0 }

ans = 0
as.each do |a|
  ans += [(a-(-1)).abs, (a-1).abs].min
end
if negative_count.odd?
  ans += 2
end

puts ans

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i
a = gets.split.map(&:to_i)
coins = 0
negs = 0
min_spent = 10**9 + 1
a.sort.each { |i|
    pending_coins = 0 # keep positive!
    if i < 0
        negs += 1
        pending_coins = -1 - i
    else
        if i == 0
            pending_coins = 1
        else
            pending_coins = i - 1
        end
    end

    coins += pending_coins
    if pending_coins < min_spent
        min_spent = pending_coins
    end
}

if negs.odd?
    if min_spent == 0 # is +1 or -1
        coins += 2
    elsif min_spent == 1 # is zero
        coins += 1
    else
        coins += 2
    end
end

puts coins

n = gets.to_i
a = gets.split.map &:to_i
ans = 0
nnegatives = 0
a.each do |x|
  if x >= 1
    ans += x - 1
  elsif x <= -1
    ans += -1 - x
    nnegatives += 1
  else
    ans += 1
  end
end
ans += 2 if nnegatives%2 != 0
puts ans


After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $$$n$$$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad.The crossroads are represented as a string $$$s$$$ of length $$$n$$$, where $$$s_i = \texttt{A}$$$, if there is a bus station at $$$i$$$-th crossroad, and $$$s_i = \texttt{B}$$$, if there is a tram station at $$$i$$$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $$$s_1$$$) and his goal is to get to the last crossroad (which corresponds to $$$s_n$$$).If for two crossroads $$$i$$$ and $$$j$$$ for all crossroads $$$i, i+1, \ldots, j-1$$$ there is a bus station, one can pay $$$a$$$ roubles for the bus ticket, and go from $$$i$$$-th crossroad to the $$$j$$$-th crossroad by the bus (it is not necessary to have a bus station at the $$$j$$$-th crossroad). Formally, paying $$$a$$$ roubles Petya can go from $$$i$$$ to $$$j$$$ if $$$s_t = \texttt{A}$$$ for all $$$i \le t < j$$$. If for two crossroads $$$i$$$ and $$$j$$$ for all crossroads $$$i, i+1, \ldots, j-1$$$ there is a tram station, one can pay $$$b$$$ roubles for the tram ticket, and go from $$$i$$$-th crossroad to the $$$j$$$-th crossroad by the tram (it is not necessary to have a tram station at the $$$j$$$-th crossroad). Formally, paying $$$b$$$ roubles Petya can go from $$$i$$$ to $$$j$$$ if $$$s_t = \texttt{B}$$$ for all $$$i \le t < j$$$.For example, if $$$s$$$="AABBBAB", $$$a=4$$$ and $$$b=3$$$ then Petya needs:  buy one bus ticket to get from $$$1$$$ to $$$3$$$,  buy one tram ticket to get from $$$3$$$ to $$$6$$$,  buy one bus ticket to get from $$$6$$$ to $$$7$$$. Thus, in total he needs to spend $$$4+3+4=11$$$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $$$s_n$$$) does not affect the final expense.Now Petya is at the first crossroad, and he wants to get to the $$$n$$$-th crossroad. After the party he has left with $$$p$$$ roubles. He's decided to go to some station on foot, and then go to home using only public transport.Help him to choose the closest crossroad $$$i$$$ to go on foot the first, so he has enough money to get from the $$$i$$$-th crossroad to the $$$n$$$-th, using only tram and bus tickets.

Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The first line of each test case consists of three integers $$$a, b, p$$$ ($$$1 \le a, b, p \le 10^5$$$) — the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $$$s$$$, where $$$s_i = \texttt{A}$$$, if there is a bus station at $$$i$$$-th crossroad, and $$$s_i = \texttt{B}$$$, if there is a tram station at $$$i$$$-th crossroad ($$$2 \le |s| \le 10^5$$$). It is guaranteed, that the sum of the length of strings $$$s$$$ by all test cases in one test doesn't exceed $$$10^5$$$.

For each test case print one number — the minimal index $$$i$$$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $$$i$$$ to $$$n$$$ he should use public transport).

5
2 2 1
BB
1 1 1
AB
3 2 8
AABBBBAABB
5 3 4
BBBBB
2 1 1
ABABAB

T = gets.to_i

T.times do
  a, b, m = gets.split.map(&:to_i)
  s = 'T' + gets.chomp
  n = s.size

  cost = 0
  cur = s[n - 2]
  min_idx = n - 1

  (n - 2).downto(0) do |idx|
    kind = s[idx]

    if idx == 0 || cur != kind
      if cur == 'A'
        cost += a
      else
        cost += b
      end

      if cost <= m
        min_idx = idx + 1
      end

      cur = kind
    end
  end

  puts min_idx
end


Returning back to problem solving, Gildong is now studying about palindromes. He learned that a palindrome is a string that is the same as its reverse. For example, strings "pop", "noon", "x", and "kkkkkk" are palindromes, while strings "moon", "tv", and "abab" are not. An empty string is also a palindrome.Gildong loves this concept so much, so he wants to play with it. He has $$$n$$$ distinct strings of equal length $$$m$$$. He wants to discard some of the strings (possibly none or all) and reorder the remaining strings so that the concatenation becomes a palindrome. He also wants the palindrome to be as long as possible. Please help him find one.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 100$$$, $$$1 \le m \le 50$$$) — the number of strings and the length of each string. Next $$$n$$$ lines contain a string of length $$$m$$$ each, consisting of lowercase Latin letters only. All strings are distinct.

In the first line, print the length of the longest palindrome string you made. In the second line, print that palindrome. If there are multiple answers, print any one of them. If the palindrome is empty, print an empty line or don't print this line at all.

9 4
abab
baba
abcd
bcde
cdef
defg
wxyz
zyxw
ijji

NoteIn the first example, "battab" is also a valid answer.In the second example, there can be 4 different valid answers including the sample output. We are not going to provide any hints for what the others are.In the third example, the empty string is the only valid palindrome string.

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

strs = []

n.times do
  strs << gets.strip
end

if n == 1
  unless strs[0] == strs[0].reverse
    puts 0
    exit
  end
  puts m
  puts strs[0]
  exit
end

palindroms = {}
palindroms_indexes = []
middle_word = ""

strs.each_with_index do |s, i|
  cur_pal = s.reverse
  p_count = 0
  next if palindroms.keys.include?(s) || palindroms.keys.include?(cur_pal)

  p_hash = {
    similar_count: 1,
    p_count: 0
  }
  strs.each_with_index.map do |ss, ii|
    next if ii == i

    if ss == cur_pal
      p_hash[:p_count] += 1
    else
      if ss == s
        p_hash[:similar_count] += 1
      end
    end
  end

  p_count = if p_hash[:p_count] != p_hash[:similar_count]
    if s == cur_pal
      identical = p_hash[:p_count] + 1
      if identical % 2 == 0
        identical/2
      else
        if s.length > middle_word.length
          middle_word = s
        end
        p_hash[:p_count]/2
      end
    else
      [p_hash[:p_count], p_hash[:similar_count]].min
    end
  else
    p_hash[:p_count]
  end

  if p_count == 0
    if s == cur_pal && s.length > middle_word.length
      middle_word = s
    end
  end

  palindroms[s] = p_count
end

str_begin = ""
str_end = ""
palindroms.each do |k, v|
  v.times do
    str_begin += k
    str_end = k.reverse + str_end
  end
end

pal = str_begin + middle_word + str_end
if pal.empty?
  puts 0
else
  puts pal.length
  puts pal
end

# frozen_string_literal: true

@data = $stdin.read.split

def next_token
  @data.shift
end

def next_int
  next_token.to_i
end

n = next_int
m = next_int
s = n.times.map { next_token }

res = 0
left = String.new
outlier = ''
n.times do |i|
  found = false
  (0...i).each do |j|
    next unless s[i] == s[j].reverse

    found = true
    left << s[i]
    res += 1
  end
  next if found

  outlier = s[i] if s[i] == s[i].reverse
end

res = left + outlier + left.reverse
puts res.size
puts res

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

arr = []
h = Hash.new
(1..n).each { |e|
  arr << gets.strip
  h[arr.last] = 1 if arr.include?(arr.last.reverse) && arr.last != arr.last.reverse
  h[arr.last] = 0 if arr.last == arr.last.reverse
}

an = []
h.each { |k, v|
  if v.zero?
    an << k
    break
  end
}
h.each { |k, v|
  if v == 1
    an.unshift(k)
    an << k.reverse
  end
}

s = ""
an.each { |e| s += e }
puts s.size
puts s

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

strs = []

n.times do
  strs << gets.strip
end

if n == 1
  unless strs[0] == strs[0].reverse
    puts 0
    exit
  end
  puts n
  puts strs[0]
  exit
end

palindroms = {}
palindroms_indexes = []
middle_word = ""

strs.each_with_index do |s, i|
  cur_pal = s.reverse
  p_count = 0
  next if palindroms.keys.include?(s) || palindroms.keys.include?(cur_pal)

  p_hash = {
    similar_count: 1,
    p_count: 0
  }
  strs.each_with_index.map do |ss, ii|
    next if ii == i

    if ss == cur_pal
      p_hash[:p_count] += 1
    else
      if ss == s
        p_hash[:similar_count] += 1
      end
    end
  end

  p_count = if p_hash[:p_count] != p_hash[:similar_count]
    if s == cur_pal
      identical = p_hash[:p_count] + 1
      if identical % 2 == 0
        identical/2
      else
        if s.length > middle_word.length
          middle_word = s
        end
        p_hash[:p_count]/2
      end
    else
      [p_hash[:p_count], p_hash[:similar_count]].min
    end
  else
    p_hash[:p_count]
  end

  if p_count == 0
    if s == cur_pal && s.length > middle_word.length
      middle_word = s
    end
  end

  palindroms[s] = p_count
end

str_begin = ""
str_end = ""
palindroms.each do |k, v|
  v.times do
    str_begin += k
    str_end = k.reverse + str_end
  end
end

pal = str_begin + middle_word + str_end
if pal.empty?
  puts 0
else
  puts pal.length
  puts pal
end

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

arr = []
(1..n).each { |e|
  arr << gets.strip
}

an = []
arr.each { |e|
  if arr.include?(e.reverse)
    an.unshift(e)
    an << e.reverse
    arr.delete(e.reverse)
  end
}

s = ""
an.each { |e| s += e }
puts s.size
puts s


One day a well-known sponsor of a well-known contest decided to give every participant of the contest a T-shirt as a present. A natural problem occurred: on the one hand, it is not clear how many T-shirts of what sizes should be ordered, and on the other hand, one doesn't want to order too many T-shirts (and we do not exactly paper the walls with the oversupply). After considerable brain racking and some pre-estimating, the sponsor representatives ordered a certain number of T-shirts of sizes S, M, L, XL and XXL. The T-shirts turned out to bring good luck, that's why on the contest day there built up a line of K participants willing to get one. Every contestant is characterized by his/her desired T-shirt size (so it happens that for all the participants it is also one of the sizes S, M, L, XL and XXL). The participants come up to get a T-shirt one by one and try to choose the most suitable one, choosing it like this. If there is still a T-shirt of the optimal size left, that he/she takes it without further ado. Otherwise the contestant would prefer to choose a T-shirt with the size as close to the optimal one as possible (the distance between neighboring sizes is considered equal to one). If the variant of choice is not unique, the contestant will take a T-shirt of a bigger size (in case he/she grows more). For example, for a person whose optimal size is L the preference list looks like this: L, XL, M, XXL, S. Using the data on how many T-shirts of every size had been ordered by the organizers, on the size of contestants in the line determine who got a T-shirt of what size.

The first line contains five non-negative integers NS, NM, NL, NXL, NXXL not exceeding 1000 which represent the number of T-shirts of the corresponding sizes. The second line contains an integer K (1 ≤ K ≤ 1000) which represents the number of participants. The next K lines contain the optimal T-shirt sizes for the contestants. The sizes are given in the order in which the participants stand in the line. It is guaranteed that NS + NM + NL + NXL + NXXL ≥ K.

For each contestant, print a line containing the size of the T-shirt he/she got.

$s=gets.split.map(&:to_i)
size=["S", "M", "L", "XL","XXL"]
def get(x)
        for u in 0..5
                if x+u<5 && $s[x+u]>0 then
                        $s[x+u]-=1
                        return x+u
                end
                if x-u>=0 && $s[x-u]>0 then
                        $s[x-u]-=1
                        return x-u
                end
        end
        return 0
end
k=gets.to_i
k.times{
        puts size[get(size.index(gets.split[0]))]
}

def f(n)
  l = [nil] * 5 + ['S','M','L','XL','XXL'] + [nil] * 5
  i = n+4
  r = []
  r << l[i]
  r << l[i+1]
  r << l[i-1]
  r << l[i+2]
  r << l[i-2]
  r << l[i+3]
  r << l[i-3]
  r << l[i+4]
  r << l[i-4]
  r << l[i+5]
  r << l[i-5]
  r.select {|e| e}
end

pr = []
l = ['S','M','L','XL','XXL']
1.upto(5) {|n| pr << f(n)}

a = gets.split(' ')
a = a.map{|e| e.to_i}

gets.to_i.times do
  size = gets.chomp
  tl = pr[l.index(size)]
  n = 0
  while a[l.index(tl[n])] == 0
    n += 1
  end
  a[l.index(tl[n])] -= 1
  puts tl[n]
end

s = gets.chomp.split(" ")
mas = [s[0].to_i,s[1].to_i, s[2].to_i, s[3].to_i, s[4].to_i]
index = {"S" => 0, "M" => 1, "L" => 2, "XL" => 3, "XXL" => 4}
rindex = ["S","M","L","XL","XXL"]

n = gets.to_i
for i in 1..n
  s = gets.chomp!
  for j in 0..4
    if index[s] + j < 5 && mas[index[s] + j] > 0
      mas[index[s] + j] -= 1
      puts rindex[index[s] + j]
      break
    elsif index[s] - j >= 0 && mas[index[s] - j] > 0
      mas[index[s] - j] -= 1
      puts rindex[index[s] - j]
      break
    end
  end
end

SIZES = [ :S, :M, :L, :XL, :XXL ]
OFFSET = [ 0, +1, -1, +2, -2, +3, -3, +4, -4 ]

sizes = {}
gets.split.map { |v| v.to_i }.each_with_index { |how, index| sizes[SIZES[index]] = how }
gets.to_i.times do
  index = SIZES.find_index(gets.strip.to_sym)
  OFFSET.each do |offset|
    cur, size = index + offset, SIZES[index + offset]
   if (0 ... SIZES.size).include?(cur) and sizes[size] > 0
     sizes[size] -= 1
     puts size
     break
    end
  end
end

def f(n)
  l = [nil] * 5 + ['S','M','L','XL','XXL'] + [nil] * 5
  i = n+5
  r = []
  r << l[i]
  r << l[i+1]
  r << l[i-1]
  r << l[i+2]
  r << l[i-2]
  r << l[i+3]
  r << l[i-3]
  r << l[i+4]
  r << l[i-4]
  r << l[i+5]
  r << l[i-5]
  r.select {|e| e}
end

pr = []
l = ['S','M','L','XL','XXL']
1.upto(5) {|n| pr << f(n)}

a = gets.split(' ')
a = a.map{|e| e.to_i}

gets.to_i.times do
  size = gets.chomp
  tl = pr[l.index(size)]
  n = 0
  while a[l.index(tl[n])] == 0
    n += 1
  end
  a[l.index(tl[n])] -= 1
  puts tl[n]
end

s = gets.chomp.split(" ")
mas = [s[0].to_i,s[1].to_i, s[2].to_i, s[3].to_i, s[4].to_i]
index = {"S" => 0, "M" => 1, "L" => 2, "XL" => 3, "XXL" => 4}
rindex = ["S","M","L","XL","XXL"]

n = gets.to_i
for i in 1..n
  s = gets.chomp!
  if mas[index[s]] > 0
    mas[index[s]] -= 1
    puts rindex[index[s]]
  elsif index[s] + 1 < 5 && mas[index[s] + 1] > 0
    mas[index[s] + 1] -= 1
    puts rindex[index[s] + 1]
  elsif index[s] - 1 >= 0 && mas[index[s] - 1] > 0
    mas[index[s] - 1] -= 1
    puts rindex[index[s] - 1]
  elsif index[s] + 2 < 5 && mas[index[s] + 2] > 0
    mas[index[s] + 2] -= 1
    puts rindex[index[s] + 2]
  elsif index[s] - 2 >= 0 && mas[index[s] -2] > 0
    mas[index[s] - 2] -= 1
    puts rindex[index[s] -2]
  end
end