Boboniu gives you  $$$r$$$ red balls,  $$$g$$$ green balls,  $$$b$$$ blue balls,  $$$w$$$ white balls. He allows you to do the following operation as many times as you want:   Pick a red ball, a green ball, and a blue ball and then change their color to white. You should answer if it's possible to arrange all the balls into a palindrome after several (possibly zero) number of described operations.

The first line contains one integer $$$T$$$ ($$$1\le T\le 100$$$) denoting the number of test cases. For each of the next $$$T$$$ cases, the first line contains four integers $$$r$$$, $$$g$$$, $$$b$$$ and $$$w$$$ ($$$0\le r,g,b,w\le 10^9$$$).

For each test case, print "Yes" if it's possible to arrange all the balls into a palindrome after doing several (possibly zero) number of described operations. Otherwise, print "No".

NoteIn the first test case, you're not able to do any operation and you can never arrange three balls of distinct colors into a palindrome.In the second test case, after doing one operation, changing $$$(8,1,9,3)$$$ to $$$(7,0,8,6)$$$, one of those possible palindromes may be "rrrwwwbbbbrbbbbwwwrrr".A palindrome is a word, phrase, or sequence that reads the same backwards as forwards. For example, "rggbwbggr", "b", "gg" are palindromes while "rgbb", "gbbgr" are not. Notice that an empty word, phrase, or sequence is palindrome.

gets.to_i.times do
  arr = gets.split(' ').map(&:to_i)
  even_number = arr.count { |num| num.even? }

  if even_number >= 3
    puts 'Yes'
  elsif even_number == 2
    puts 'No'
  elsif arr.take(3).include?(0)
    puts 'No'
  else
    puts 'Yes'
  end
end

t = gets.to_i
t.times do
	r, g, b, w = gets.split.map &:to_i
	is_ok = [r, g, b, w].count(&:odd?) < 2
	is_ok ||= [r, g, b].min > 0 && [r-1, g-1, b-1, w+3].count(&:odd?) < 2
	puts (is_ok ? "Yes" : "No")
end

t = gets.to_i
ans = []
t.times do
  r,g,b,w = gets.chomp.split(" ").map(&:to_i)
  sum = r+g+b+w
  if sum % 2 == 0
    if r%2==0 && g%2==0 && b%2==0 && w%2==0
      ans << "YES"
      next
    end
  else
    judge = 0
    judge += 1 if r%2==1
    judge += 1 if g%2==1
    judge += 1 if b%2==1
    judge += 1 if w%2==1
    if judge == 1
      ans << "YES"
      next
    end
  end
  if r > 0 && g > 0 && b > 0
    r -= 1
    g -= 1
    b -= 1
    w += 3
    if sum % 2 == 0
      if r%2==0 && g%2==0 && b%2==0 && w%2==0
        ans << "YES"
        next
      end
    else
      judge = 0
      judge += 1 if r%2==1
      judge += 1 if g%2==1
      judge += 1 if b%2==1
      judge += 1 if w%2==1
      if judge == 1
        ans << "YES"
        next
      end
    end
  end
  ans << "NO"
end
ans.each do |s|
  puts s
end

gets.to_i.times do
  arr = gets.split(' ').map(&:to_i)
  even_number = arr.count { |num| num.even? }

  if even_number >= 3
    puts 'Yes'
  elsif even_number == 2
    puts 'No'
  elsif arr.include?(0)
    puts 'No'
  else
    puts 'Yes'
  end
end

t = gets.to_i
t.times do
	r, g, b, w = gets.split.map &:to_i
	is_ok = [r, g, b, w].count(&:odd?) < 2
	is_ok ||= [r, g, b].min > 0 && [r-1, g-1, b-1, w].count(&:odd?) < 2
	puts (is_ok ? "Yes" : "No")
end

t = gets.to_i
t.times do
	r, g, b, w = gets.split.map &:to_i
	is_ok = [r, g, b, w].count(&:odd?) < 2
	is_ok ||= [r, g, b].min > 0 && [r-1, g-1, b-1, w].count(&:odd?)
	puts (is_ok ? "Yes" : "No")
end


A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n.

The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size.

Print "-1" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them.

N = gets.chomp.to_i
a = Array.new(N+1)
if N.modulo(4) <= 1
  (1..N/2).step(2) do |i|
    a[i], a[i+1], a[N-i+1], a[N-i] = i+1, N-i+1, N-i, i
  end
  a[N / 2 + 1] = N / 2 + 1 if N % 2 == 1
  puts a[1..N].join(' ')
elsif
  puts "-1"
end

n = gets.to_i

if n % 4 == 2 || n % 4 == 3 then
    puts "-1"
    exit
end

ret = Array.new(n, 0)
ret[n / 2] = n / 2 if n % 2
i = 0
while i < n / 2 do
    ret[i], ret[i + 1], ret[n - 2 - i], ret[n - 1 - i] = i + 1, n - 1 - i, i, n - 2 - i
    i += 2
end

ret.map!{ |i| i + 1 }
puts ret.join(' ')


Andrew often reads articles in his favorite magazine 2Char. The main feature of these articles is that each of them uses at most two distinct letters. Andrew decided to send an article to the magazine, but as he hasn't written any article, he just decided to take a random one from magazine 26Char. However, before sending it to the magazine 2Char, he needs to adapt the text to the format of the journal. To do so, he removes some words from the chosen article, in such a way that the remaining text can be written using no more than two distinct letters.Since the payment depends from the number of non-space characters in the article, Andrew wants to keep the words with the maximum total length.

The first line of the input contains number n (1 ≤ n ≤ 100) — the number of words in the article chosen by Andrew. Following are n lines, each of them contains one word. All the words consist only of small English letters and their total length doesn't exceed 1000. The words are not guaranteed to be distinct, in this case you are allowed to use a word in the article as many times as it appears in the input.

Print a single integer — the maximum possible total length of words in Andrew's article.

NoteIn the first sample the optimal way to choose words is {'abb', 'aaa', 'bbb'}.In the second sample the word 'cdecdecdecdecdecde' consists of three distinct letters, and thus cannot be used in the article. The optimal answer is {'a', 'a', 'aaaa'}.

ws = []
gets.to_i.times do
    ws << gets.strip
end

m = 0
('a'..'z').each do |c1|
    ('a'..'z').each do |c2|
        s = ws.select {|w| w.chars.all?{|c| c == c1 || c == c2 } }.join.size
        m = [m, s].max
    end
end
p m

# Problem: http://codeforces.com/contest/593/problem/A
a = []
rs = [0]
gets.to_i.times {a << gets.chomp }
for i in ('a'..'z')
  for j in (i..'z')
    sum = 0
    a.each do |s|
      continue = true
      s.split("").each do |c|
        if c != i and c != j
          continue = false
        end
      end
      if continue == true
        sum += s.size
      end
    end
    if sum > 0
      rs << sum
    end
  end
end
puts rs.max

# Problem: http://codeforces.com/contest/593/problem/A
a = []
rs = [0]
gets.to_i.times {a << gets.chomp }
for i in ('a'..'z')
  for j in (i..'z')
    sum = 0
    a.each do |s|
      continue = true
      s.size.times do |k|
        if s[k] != i and s[k] != j
          continue = false
        end
      end
      if continue == true
        sum += s.size
      end
    end
    if sum > 0
      rs << sum
    end
  end
end
puts rs.max

# Problem: http://codeforces.com/contest/593/problem/A
a = []
d = Hash.new 0
gets.to_i.times {a << gets.chomp }
a.each do |s|
  s_asize = s.size
  s_uniq = s.split("").uniq
  s_size = s_uniq.size
  if s_size > 2
    next
  end
  if s_size == 1
    c = s_uniq.pop
    d[c] += s_asize
    d.keys.each do |k|
      if k.size == 2 and k.include? c
        d[k] += s_asize
      elsif k.size == 1 and k != c
        new_k = [k, c].sort.join
        if d[new_k] == 0
          d[new_k] = d[k] + d[c]
        end
      end
    end
  end
  if s_size == 2
    k = s_uniq.sort.join
    if d[k] == 0
      s_uniq.each { |t| d[k] += d[t] }
    end
    d[k] += s_asize
  end
end
if d.empty?
  puts 0
else
  puts d.values.max
end

a=("a" .. "z" ).to_a
hash={}
0.upto(25) {|i| hash[a[i]]=i   }
grid = Array.new(26) { Array.new(26,0) }
a=gets.chomp.to_i
a.times do
temp=gets.chomp.split("")
t1=temp.length
temp.uniq!
t2=temp.length
if t2<3
if t2==1
r=hash[temp[0]]
c=hash[temp[0]]
grid[r][c]+=t1
else
r=hash[temp[0]]
c=hash[temp[1]]
grid[r][c]+=t1
grid[c][r]+=t1
end
end
end
ans=0

0.upto(25) do |i|
0.upto(25) do |j|
if i!=j
temp=grid[i][i]+grid[i][j]+grid[j][j]
if temp>ans
ans=temp
end
end
end
end
puts "#{ans}"

require 'set'

file = File.exist?('in.in') ? File.new('in.in', 'r') : STDIN

def init_both(char1,char2)
  total = 0
  words = []
  [char1,char2].each do |char|
    if @global_count[char]
      total += @global_count[char].total
      words << @global_count[char].words
    end
  end
  Pair.new(total, words.flatten)
end

n = file.gets.to_i
Pair = Struct.new(:total, :words)
@global_count = {}
global_words = {}

while n > 0 do
  n -= 1
  word = file.gets.chomp

  valid_word = true
  char1 = word.chars.first
  char2 = nil
  current_count = {}
  word.chars.each do |char|
    char2 ||= char if char != char1
    if char != char1 && char != char2
      valid_word = false
      break
    end
    current_count[char] ||= 0
    current_count[char] += 1
  end
  if valid_word
    if char2
      both_key = "#{char1}#{char2}".chars.sort.join
      @global_count[both_key] ||= init_both(char1,char2)
      @global_count[both_key].total += word.length
      @global_count[both_key].words << word
      global_words[char1] ||= Set.new [char1]
      global_words[char1].add both_key
      global_words[char2] ||= Set.new [char2]
      global_words[char2].add both_key
    else
      @global_count[char1] ||= Pair.new(0, [])
      global_words[char1] ||= Set.new [char1]
      global_words[char1].add char1

      global_words[char1].each do |char|
        @global_count[char].total += word.length
        @global_count[char].words << word
      end
    end
  end
end
global_words.each do |k,v|
	if k.length == 1
		global_words.each do |q,b|
			both_key = "#{q}#{k}".chars.sort.join
			if q != k && q.length == 1
				global_words[k].add both_key
				@global_count[both_key] ||= init_both(k,q)
			end
		end
	end
end
@global_count = @global_count.sort {|a1,a2| a2[1].total <=> a1[1].total }
@global_count = Hash[*@global_count.flatten]
# puts @global_count
# puts global_words
if @global_count.values.first
  answer = @global_count.values.first.total
  puts answer || 0
else
  puts 0
end

require 'set'

file = File.exist?('in.in') ? File.new('in.in', 'r') : STDIN

def init_both(char1,char2)
  total = 0
  [char1,char2].each do |char|
    if @global_count[char]
      total += @global_count[char]
    end
  end
  total
end

n = file.gets.to_i
@global_count = {}
global_words = {}

while n > 0 do
  n -= 1
  word = file.gets.chomp

  valid_word = true
  char1 = word.chars.first
  char2 = nil
  current_count = {}
  word.chars.each do |char|
    char2 ||= char if char != char1
    if char != char1 && char != char2
      valid_word = false
      break
    end
    current_count[char] ||= 0
    current_count[char] += 1
  end
  if valid_word
    if char2
      both_key = "#{char1}#{char2}".chars.sort.join
      @global_count[both_key] ||= init_both(char1,char2)
      @global_count[both_key] += word.length
      global_words[char1] ||= Set.new [char1]
      global_words[char1].add both_key
      global_words[char2] ||= Set.new [char2]
      global_words[char2].add both_key
    else
      @global_count[char1] ||= 0
      global_words[char1] ||= Set.new [char1]
      global_words[char1].add char1

      global_words[char1].each do |char|
        @global_count[char] += word.length
      end
    end
  end
end
# Generate all possible combinations for every single word with word.length == 1
global_words.each do |k,v|
	if k.length == 1
		global_words.each do |q,b|
			both_key = "#{q}#{k}".chars.sort.join
			if q != k && q.length == 1
				global_words[k].add both_key
				@global_count[both_key] ||= init_both(k,q)
			end
		end
	end
end

@global_count = @global_count.sort {|a1,a2| a2[1] <=> a1[1] }
@global_count = Hash[*@global_count.flatten]
puts @global_count.values.first || 0

n = gets.chomp.to_i
hs = Hash.new(0)
for c0 in 'a'..'y'
  for c1 in 'a'..'z'
    hs[c0 + c1] = 0 if c0 < c1
  end
end

n.times do
  line = gets.chomp
  cs = line.split('').sort.uniq
  if cs.size == 1
    hs[cs[0]] += line.size
  elsif cs.size == 2
    hs[cs[0] + cs[1]] += line.size
  end
end

rtn = 0

hs.each do |k, v|
  if k.size == 2
    hs[k] += hs[k[0]] + hs[k[1]]
  end
end

hs.each do |k, v|
  rtn = [rtn, v].max
end

puts rtn

def max(a, b)
    return a > b ? a : b
end
n = gets.chomp.to_i
v = []
for i in(1..n)
    v[i-1] = gets.chomp
end
c = 0
for i in('a'..'z')
    for j in(i..'z')
        c1 = 0
        for x in(1..n)
            sw = 0
            for y in(0..(v[x-1].size-1))
                if v[x-1][y] == i || v[x-1][y] == j
                else
                    sw = 1
                end
            end
            if sw==0
                c1+=v[x-1].size
            end
        end
        c = max(c,c1)
    end
end
puts c

n = gets.chomp.to_i
d = Hash.new(0)
n.times do
  word = gets.chomp
  k = word.split("").uniq.sort
  v = word.length
  d[k] += v if k.length <= 2
end
res = 0
(?a..?z).to_a.combination(2) do |kk|
  res = [res, d[[kk[0]]] + d[[kk[1]]] + d[kk]].max
end
puts res

def get_array(arr)
  a = arr[0]
  b = arr.delete(a)[0]
  return [a,b]
end
n = gets.chomp.to_i
x, arr, len, arr1, l1, sum = [], [], [0], [], [], 0  #len初始化为0，防止所有输入都不符合，返回0
(1..n).each do |i|
  a = gets.chomp
  t = String.new(a)
  num = 0
  a.each_char do |x|
    if a.delete(x) != a
      a.delete!(x)
      num += 1
    end
    break if num == 3
  end
  arr << t if num < 3
end
arr.each_with_index do |arr1, index1|
  len[index1] = arr1.length
  arr.each_with_index do |arr2, index2|
    next if index1 == index2
    len[index1] += arr2.length if get_array(arr1) == get_array(arr2) || get_array(arr1).reverse == get_array(arr2) || arr1.squeeze.include?(arr2.squeeze)
  end
end
l = len.sort.last
arr.each do |a|
  arr1 << a.squeeze if a.squeeze.length == 1
end
if arr1.length > 0     #排除超过一种字符的行,这里操作每行都是一种字符的情况
  arr1.uniq.each do |a1|
    sum = 0
    arr.each do |a|
      sum += a.length if a1 == a.squeeze
    end
    l1 << sum
  end
  l1.sort!
  p l1.last + l1[l1.length-2] > l && l1.length > 1 ? l1.last + l1[l1.length-2] : l
else
  p l
end

def get_arr(arr)
  a = arr[0]
  b = arr.delete(a)[0]
  return [a,b]
end

def exist?(arr, arr1)
  arr.each do |x|
    if x == arr1
      return true
      break
    end
  end
  false
end
n = gets.chomp.to_i
x, arr, array, len, arr1, l1, sum = [], [], [], [0], [], [], 0
(1..n).each do |i|
  a = gets.chomp
  t = String.new(a)
  num = 0
  a.each_char do |x|
    if a.delete(x) != a
      a.delete!(x)
      num += 1
    end
    if num == 3
      break
    end
  end
  if num < 3
    arr << t
  end
end
arr.each_with_index do |arr1, index1|
  len[index1] = arr1.length
  if !exist?(array, arr1)
    arr.each_with_index do |arr2, index2|
      if index1 == index2
        next
      else
        if get_arr(arr1) == get_arr(arr2) || get_arr(arr1).reverse == get_arr(arr2) || arr1.squeeze.include?(arr2.squeeze)
          len[index1] += arr2.length
        end
      end
    end
    array << get_arr(arr1)
  end
end
l = len.sort.last
arr.each do |a|
  if a.squeeze.length == 1
    arr1 << a.squeeze
  end
end
if arr1.length > 0
  arr1.uniq.each do |a1|
    sum = 0
    arr.each do |a|
      if a1 == a.squeeze
        sum += a.length
      end
    end
    l1 << sum
  end
  l1.sort!
  p l1.last + l1[l1.length-2] > l && l1.length > 1 ? l1.last + l1[l1.length-2] : l
else
  p l
end

def get_arr(arr)
  a = arr[0]
  b = arr.delete(a)[0]
  return [a,b]
end
# def exist?(arr, arr1)
#   arr.each do |x|
#     if x == arr1
#       true
#       break
#     end
#   end
#   false
# end
n = gets.chomp.to_i
x, arr, array, len, arr1, l1, sum = [], [], [], [0], [], [], 0
(1..n).each do |i|
  a = gets.chomp
  t = String.new(a)
  num = 0
  a.each_char do |x|
    if a.delete(x) != a
      a.delete!(x)
      num += 1
    end
    if num == 3
      break
    end
  end
  if num < 3
    arr << t
  end
end
arr.each_with_index do |arr1, index1|
  len[index1] = arr1.length
  # if !exist?(array, arr1)
    arr.each_with_index do |arr2, index2|
      if index1 == index2
        next
      else
        if get_arr(arr1) == get_arr(arr2) || get_arr(arr1).reverse == get_arr(arr2) || arr1.squeeze.include?(arr2.squeeze)
          len[index1] += arr2.length
        end
      end
    end
    array << get_arr(arr1)
  # end
end
l = len.sort.last
arr.each do |a|
  if a.squeeze.length == 1
    arr1 << a.squeeze
  end
end
if arr1.length > 0
  arr1.uniq.each do |a1|
    sum = 0
    arr.each do |a|
      if a1 == a.squeeze
        sum += a.length
      end
    end
    l1 << sum
  end
  l1.sort!
  p l1.last + l1[l1.length-2] > l && l1.length > 1 ? l1.last + l1[l1.length-2] : l
else
  p l
end

# Problem: http://codeforces.com/contest/593/problem/A
a = []
d = Hash.new 0
gets.to_i.times {a << gets.chomp }
a.each do |s|
  s_asize = s.size
  s_uniq = s.split("").uniq
  s_size = s_uniq.size
  if s_size > 2
    next
  end
  if s_size == 1
    c = s_uniq.pop
    d[c] += s_asize
    d.keys.each do |k|
      if k.size == 2 and k.include? c
        d[k] += s_asize
      end
    end
  end
  if s_size == 2
    k = s_uniq.sort.join
    d[k] += s_asize
  end
end
puts d.values.max

# Problem: http://codeforces.com/contest/593/problem/A
a = []
d = Hash.new 0
gets.to_i.times {a << gets.chomp }
a.each do |s|
  s_asize = s.size
  s_uniq = s.split("").uniq
  s_size = s_uniq.size
  if s_size > 2
    next
  end
  if s_size == 1
    c = s_uniq.pop
    d[c] += s_asize
    d.keys.each do |k|
      if k.size == 2 and k.include? c
        d[k] += s_asize
      elsif k.size == 1 and k != c
        new_k = [k, c].sort.join
        if d[new_k] == 0
          d[new_k] = d[k] + d[c]
        end
      end
    end
  end
  if s_size == 2
    k = s_uniq.sort.join
    d[k] += s_asize
  end
end
if d.empty?
  puts 0
else
  puts d.values.max
end

# Problem: http://codeforces.com/contest/593/problem/A
a = []
d = Hash.new 0
gets.to_i.times {a << gets.chomp }
a.each do |s|
  s_asize = s.size
  s_uniq = s.split("").uniq
  s_size = s_uniq.size
  if s_size > 2
    next
  end
  if s_size == 1
    c = s_uniq.pop
    d[c] += s_asize
    d.keys.each do |k|
      if k.size == 2 and k.include? c
        d[k] += s_asize
      elsif k.size == 1 and k != c
        new_k = [k, c].sort.join
        if d[new_k] == 0
          d[new_k] = d[k] + d[c]
        end
      end
    end
  end
  if s_size == 2
    k = s_uniq.sort.join
    d[k] += s_asize
    d.keys.each do |t|
      if t.size == 1 and k.include? t
        d[k] += d[t]
      end
    end
  end
end
if d.empty?
  puts 0
else
  puts d.values.max
end

# Problem: http://codeforces.com/contest/593/problem/A
a = []
d = Hash.new 0
gets.to_i.times {a << gets.chomp }
a.each do |s|
  s_asize = s.size
  s_uniq = s.split("").uniq
  s_size = s_uniq.size
  if s_size > 2
    next
  end
  if s_size == 1
    c = s_uniq.pop
    d[c] += s_asize
    d.keys.each do |k|
      if k.size == 2 and k.include? c
        d[k] += s_asize
      elsif k.size == 1 and k != c
        new_k = [k, c].sort.join
        d[new_k] = d[k] + d[c]
      end
    end
  end
  if s_size == 2
    k = s_uniq.sort.join
    d[k] += s_asize
  end
end
if d.empty?
  puts 0
else
  puts d.values.max
end

# Problem: http://codeforces.com/contest/593/problem/A
a = []
d = Hash.new 0
gets.to_i.times {a << gets.chomp }
a.each do |s|
  s_asize = s.size
  s_uniq = s.split("").uniq
  s_size = s_uniq.size
  if s_size > 2
    next
  end
  if s_size == 1
    c = s_uniq.pop
    d[c] += s_asize
    d.keys.each do |k|
      if k.size == 2 and k.include? c
        d[k] += s_asize
      end
    end
  end
  if s_size == 2
    k = s_uniq.sort.join
    d[k] += s_asize
  end
end
if d.empty?
  puts 0
else
  puts d.values.max
end

require 'set'

class Pair
  attr_accessor :total, :words

  def initialize(total, words)
    @total = total
    @words = words
  end
end

n = gets.to_i
# Pair = Struct.new(:total, :words)
@global_count = {}
global_words = {}

# def init_both(char1,char2)
# 	total = 0
# 	words = []
# 	[char1,char2].each do |char|
# 		if @global_count[char]
# 			total += @global_count[char].total
# 			words = @global_count[char].words
# 		end
# 	end
# 	Pair.new(total, words)
# end

# while n > 0 do
# 	n -= 1
# 	word = gets.chomp

# 	valid_word = true
# 	char1 = word.chars.first
# 	char2 = nil
# 	current_count = {}
# 	word.chars.each do |char|
# 		char2 ||= char if char != char1
# 		if char != char1 && char != char2
# 			valid_word = false
# 			break
# 		end
# 		current_count[char] ||= 0
# 		current_count[char] += 1
# 	end

# 	if valid_word
# 		if char2
# 			both_key = "#{char1}#{char2}".chars.sort.join
# 			@global_count[both_key] ||= init_both(char1,char2)
# 			@global_count[both_key].total += word.length
# 			@global_count[both_key].words << word
# 			global_words[char1] ||= Set.new [char1]
# 			global_words[char1].add both_key
# 			global_words[char2] ||= Set.new [char2]
# 			global_words[char2].add both_key
# 		else
# 			@global_count[char1] ||= Pair.new(0, [])
# 			global_words[char1] ||= Set.new [char1]
# 			global_words[char1].add char1

# 			global_words[char1].each do |char|
# 				@global_count[char].total += word.length
# 				@global_count[char].words << word
# 			end
# 		end
# 	end
# end

# @global_count = @global_count.sort {|a1,a2| a2[1].total <=> a1[1].total }.to_h
# if @global_count.values.first
# 	answer = @global_count.values.first.total
# 	if @global_count.keys.first.length == 1
# 		size = 0
# 		answer = 0
# 		@global_count.keys.each do |key|
# 			if key.length == 1
# 				size +=1
# 				answer += @global_count[key].total
# 			end
# 			break if size == 2
# 		end
# 	end
# 	puts answer || 0

# else
# 	puts 0
# end

require 'set'
n = gets.to_i
global_count = {}
global_words = {}

while n > 0 do
	n -= 1
	word = gets.chomp

	valid_word = true
	char1 = word.chars.first
	char2 = nil
	current_count = {}
	word.chars.each do |char|
		char2 ||= char if char != char1
		if char != char1 && char != char2
			valid_word = false
			break
		end
		current_count[char] ||= 0
		current_count[char] += 1
	end

	if valid_word
		both_key = "#{char1}#{char2}".chars.sort.join

		global_count[char1] ||= 0
		global_words[char1] ||= Set.new [char1]
		global_count[both_key] ||= 0
		global_words[char1].add both_key

		if char2

			global_count[char2] ||= 0
			global_words[char2] ||= Set.new [char2]
			global_words[char2].add both_key

			global_count[both_key] += current_count[char1]
			global_count[char1] += current_count[char1]
			global_count[both_key] += current_count[char2]
			global_count[char2] += current_count[char2]
		else
			global_words[char1].each do |word|
				global_count[word] += current_count[char1]
			end
		end
	end
end

puts global_count.values.max || 0

require 'set'

file = File.exist?('in.in') ? File.new('in.in', 'r') : STDIN

def init_both(char1,char2)
  total = 0
  words = []
  [char1,char2].each do |char|
    if @global_count[char]
      total += @global_count[char].total
      words = @global_count[char].words.dup
    end
  end
  Pair.new(total, words)
end

n = file.gets.to_i
Pair = Struct.new(:total, :words)
@global_count = {}
global_words = {}

while n > 0 do
  n -= 1
  word = file.gets.chomp

  valid_word = true
  char1 = word.chars.first
  char2 = nil
  current_count = {}
  word.chars.each do |char|
    char2 ||= char if char != char1
    if char != char1 && char != char2
      valid_word = false
      break
    end
    current_count[char] ||= 0
    current_count[char] += 1
  end
  if valid_word
    if char2
      both_key = "#{char1}#{char2}".chars.sort.join
      @global_count[both_key] ||= init_both(char1,char2)
      @global_count[both_key].total += word.length
      @global_count[both_key].words << word
      global_words[char1] ||= Set.new [char1]
      global_words[char1].add both_key
      global_words[char2] ||= Set.new [char2]
      global_words[char2].add both_key
    else
      @global_count[char1] ||= Pair.new(0, [])
      global_words[char1] ||= Set.new [char1]
      global_words[char1].add char1

      global_words[char1].each do |char|
        @global_count[char].total += word.length
        @global_count[char].words << word
      end

			global_words.each do |k,v|
				both_key = "#{char1}#{k}".chars.sort.join
				if char1 != k && k.length == 1
					global_words[char1].add both_key
					@global_count[both_key] ||= init_both(char1,k)
				end
			end

    end
  end
end
@global_count = @global_count.sort {|a1,a2| a2[1].total <=> a1[1].total }
@global_count = Hash[*@global_count.flatten]
# puts @global_count
# puts global_words
if @global_count.values.first
  answer = @global_count.values.first.total
  if @global_count.keys.first.length == 1
    size = 0
    answer = 0
    @global_count.keys.each do |key|
      if key.length == 1
        size +=1
        answer += @global_count[key].total
      end
      break if size == 2
    end
  end
  puts answer || 0

else
  puts 0
end

require 'set'

def init_both(char1,char2)
  total = 0
  words = []
  [char1,char2].each do |char|
    if @global_count[char]
      total += @global_count[char].total
      words = @global_count[char].words
    end
  end
  Pair.new(total, words)
end

n = gets.to_i
Pair = Struct.new(:total, :words)
@global_count = {}
global_words = {}

while n > 0 do
  n -= 1
  word = gets.chomp

  valid_word = true
  char1 = word.chars.first
  char2 = nil
  current_count = {}
  word.chars.each do |char|
    char2 ||= char if char != char1
    if char != char1 && char != char2
      valid_word = false
      break
    end
    current_count[char] ||= 0
    current_count[char] += 1
  end
  if valid_word
    if char2
      both_key = "#{char1}#{char2}".chars.sort.join
      @global_count[both_key] ||= init_both(char1,char2)
      @global_count[both_key].total += word.length
      @global_count[both_key].words << word
      global_words[char1] ||= Set.new [char1]
      global_words[char1].add both_key
      global_words[char2] ||= Set.new [char2]
      global_words[char2].add both_key
    else
      @global_count[char1] ||= Pair.new(0, [])
      global_words[char1] ||= Set.new [char1]
      global_words[char1].add char1

      global_words.each do |k,v|
        if k != char1 && k.length == 1
          both_key = "#{char1}#{k}".chars.sort.join
          unless @global_count[both_key]
            @global_count[both_key] ||= init_both(char1,k)
            global_words[char1].add both_key
          end
        end
      end

      global_words[char1].each do |char|
        @global_count[char].total += word.length
        @global_count[char].words << word
      end
    end
  end
end
@global_count = @global_count.sort {|a1,a2| a2[1].total <=> a1[1].total }
@global_count = Hash[*@global_count.flatten]

# puts @global_count
# puts global_words

if @global_count.values.first
  answer = @global_count.values.first.total
  if @global_count.keys.first.length == 1
    size = 0
    answer = 0
    @global_count.keys.each do |key|
      if key.length == 1
        size +=1
        answer += @global_count[key].total
      end
      break if size == 2
    end
  end
  puts answer || 0

else
  puts 0
end

require 'set'

def init_both(char1,char2)
  total = 0
  words = []
  [char1,char2].each do |char|
    if @global_count[char]
      total += @global_count[char].total
      words = @global_count[char].words
    end
  end
  Pair.new(total, words)
end

n = gets.to_i
Pair = Struct.new(:total, :words)
@global_count = {}
global_words = {}

while n > 0 do
  n -= 1
  word = gets.chomp

  valid_word = true
  char1 = word.chars.first
  char2 = nil
  current_count = {}
  word.chars.each do |char|
    char2 ||= char if char != char1
    if char != char1 && char != char2
      valid_word = false
      break
    end
    current_count[char] ||= 0
    current_count[char] += 1
  end
  if valid_word
    if char2
      both_key = "#{char1}#{char2}".chars.sort.join
      @global_count[both_key] ||= init_both(char1,char2)
      @global_count[both_key].total += word.length
      @global_count[both_key].words << word
      global_words[char1] ||= Set.new [char1]
      global_words[char1].add both_key
      global_words[char2] ||= Set.new [char2]
      global_words[char2].add both_key
    else
      @global_count[char1] ||= Pair.new(0, [])
      global_words[char1] ||= Set.new [char1]
      global_words[char1].add char1

      global_words.each do |k,v|
        if k != char1 && k.length == 1
          both_key = "#{char1}#{k}".chars.sort.join
          @global_count[both_key] ||= init_both(char1,char2)
          global_words[char1].add both_key
        end
      end

      global_words[char1].each do |char|
        @global_count[char].total += word.length
        @global_count[char].words << word
      end
    end
  end
end
@global_count = @global_count.sort {|a1,a2| a2[1].total <=> a1[1].total }
@global_count = Hash[*@global_count.flatten]

# puts @global_count
# puts global_words

if @global_count.values.first
  answer = @global_count.values.first.total
  if @global_count.keys.first.length == 1
    size = 0
    answer = 0
    @global_count.keys.each do |key|
      if key.length == 1
        size +=1
        answer += @global_count[key].total
      end
      break if size == 2
    end
  end
  puts answer || 0

else
  puts 0
end

require 'set'
n = gets.to_i
global_count = {}
global_words = {}

while n > 0 do
	n -= 1
	word = gets.chomp

	valid_word = true
	char1 = word.chars.first
	char2 = nil
	current_count = {}
	word.chars.each do |char|
		char2 ||= char if char != char1
		if char != char1 && char != char2
			valid_word = false
			break
		end
		current_count[char] ||= 0
		current_count[char] += 1
	end

	if valid_word

		both_key = "#{char1}#{char2}".chars.sort.join

		global_count[char1] ||= 0
		global_words[char1] ||= Set.new [char1]
		global_words[char1].add both_key

		if char2
			global_count[char2] ||= 0
			global_words[char2] ||= Set.new [char2]
			global_words[char2].add both_key
			global_count[char2] += current_count[char2]
			global_count[char1] += current_count[char1]

			global_count[both_key] ||= 0
			global_count[both_key] = global_count[char1] + global_count[char2]

		else
			global_words[char1].each do |word|
				global_count[word] += current_count[char1]
			end
		end
	end
end
puts global_count.values.max || 0

require 'set'

def init_both(char1,char2)
  total = 0
  words = []
  [char1,char2].each do |char|
    if @global_count[char]
      total += @global_count[char].total
      words = @global_count[char].words
    end
  end
  Pair.new(total, words)
end

n = gets.to_i
Pair = Struct.new(:total, :words)
@global_count = {}
global_words = {}

while n > 0 do
  n -= 1
  word = gets.chomp

  valid_word = true
  char1 = word.chars.first
  char2 = nil
  current_count = {}
  word.chars.each do |char|
    char2 ||= char if char != char1
    if char != char1 && char != char2
      valid_word = false
      break
    end
    current_count[char] ||= 0
    current_count[char] += 1
  end
  if valid_word
    if char2
      both_key = "#{char1}#{char2}".chars.sort.join
      @global_count[both_key] ||= init_both(char1,char2)
      @global_count[both_key].total += word.length
      @global_count[both_key].words << word
      global_words[char1] ||= Set.new [char1]
      global_words[char1].add both_key
      global_words[char2] ||= Set.new [char2]
      global_words[char2].add both_key
    else
      @global_count[char1] ||= Pair.new(0, [])
      global_words[char1] ||= Set.new [char1]
      global_words[char1].add char1

      global_words[char1].each do |char|
        @global_count[char].total += word.length
        @global_count[char].words << word
      end
    end
  end
end
@global_count = @global_count.sort {|a1,a2| a2[1].total <=> a1[1].total }
@global_count = Hash[*@global_count.flatten]
if @global_count.values.first
  answer = @global_count.values.first.total
  if @global_count.keys.first.length == 1
    size = 0
    answer = 0
    @global_count.keys.each do |key|
      if key.length == 1
        size +=1
        answer += @global_count[key].total
      end
      break if size == 2
    end
  end
  puts answer || 0

else
  puts 0
end

require 'set'

def init_both(char1,char2)
  total = 0
  words = []
  [char1,char2].each do |char|
    if @global_count[char]
      total += @global_count[char].total
      words = @global_count[char].words
    end
  end
  Pair.new(total, words)
end

n = gets.to_i
Pair = Struct.new(:total, :words)
@global_count = {}
global_words = {}

while n > 0 do
  n -= 1
  word = gets.chomp

  valid_word = true
  char1 = word.chars.first
  char2 = nil
  current_count = {}
  word.chars.each do |char|
    char2 ||= char if char != char1
    if char != char1 && char != char2
      valid_word = false
      break
    end
    current_count[char] ||= 0
    current_count[char] += 1
  end
  if valid_word
    if char2
      both_key = "#{char1}#{char2}".chars.sort.join
      @global_count[both_key] ||= init_both(char1,char2)
      @global_count[both_key].total += word.length
      @global_count[both_key].words << word
      global_words[char1] ||= Set.new [char1]
      global_words[char1].add both_key
      global_words[char2] ||= Set.new [char2]
      global_words[char2].add both_key
    else
      @global_count[char1] ||= Pair.new(0, [])
      global_words[char1] ||= Set.new [char1]
      global_words[char1].add char1

      global_words[char1].each do |char|
        @global_count[char].total += word.length
        @global_count[char].words << word
      end
    end
  end
end
puts 0
puts @global_count
@global_count = @global_count.sort {|a1,a2| puts 'ww'; puts a2[1].total; a2[1].total <=> a1[1].total }
puts 0.3
@global_count = Hash[*@global_count.flatten]
puts @global_count
if @global_count.values.first
  puts 3
  answer = @global_count.values.first.total
  if @global_count.keys.first.length == 1
    puts 4
    size = 0
    answer = 0
    @global_count.keys.each do |key|
      if key.length == 1
        size +=1
        answer += @global_count[key].total
      end
      break if size == 2
    end
  end
  puts answer || 0

else
  puts 0
end

n = gets.chomp.to_i
hs = Hash.new(0)
n.times do
  line = gets.chomp
  cs = line.split('').sort.uniq
  if cs.size == 1
    hs[cs[0]] += line.size
  elsif cs.size == 2
    hs[cs[0] + cs[1]] += line.size
  end
end

rtn = 0

hs.each do |k, v|
  if k.size == 2
    hs[k] += hs[k[0]] + hs[k[1]]
  end
end

hs.each do |k, v|
  rtn = [rtn, v].max
end

puts rtn

n = gets.chomp.to_i
hs = Hash.new(0)
n.times do
  cs = gets.chomp.split('').sort
  if cs.uniq.size == 1
    hs[cs[0]] += cs.size
  elsif cs.uniq.size == 2
    hs[cs[0] + cs[1]] += cs.size
  end
end

rtn = 0

hs.each do |k, v|
  if k.size == 2
    hs[k] += hs[k[0]] + hs[k[1]]
  end
end

hs.each do |k, v|
  rtn = [rtn, v].max
end

puts rtn

n = gets.chomp.to_i
d = Hash.new(0)
n.times do
  word = gets.chomp
  k = word.split("").uniq
  v = word.length
  d[k] += v if k.length <= 2
end

if d.empty? then
  res = 0
else
  res = d.values.max
  d.keys.combination(2) do |kk|
    if kk.flatten.uniq.length > 2 then
      next
    else
      res = [res, d[kk[0]] + d[kk[1]]].max
    end
  end

  d.keys.combination(3) do |kk|
    if kk.flatten.uniq.length > 2 then
      next
    else
      res = [res, d[kk[0]] + d[kk[1]]+d[kk[2]]].max
    end
  end
end

puts res

n = gets.chomp.to_i
d = Hash.new(0)
n.times do
  word = gets.chomp
  k = word.split("").uniq
  v = word.length
  d[k] += v if k.length <= 2
end
res = 0
(?a..?z).to_a.combination(2) do |kk|
  res = [res, d[[kk[0]]] + d[[kk[1]]] + d[kk]].max
end

puts res

n = gets.chomp.to_i
d = Hash.new(0)
n.times do
  word = gets.chomp
  k = word.split("").uniq
  v = word.length
  d[k] += v if k.length <= 2
end
res = d.values.max
d.keys.combination(2) do |kk|
  if kk.flatten.uniq.length > 2 then
    next
  else
    res = [res, d[kk[0]] + d[kk[1]]].max
  end
end
d.keys.combination(3) do |kk|
  if kk.flatten.uniq.length > 2 then
    next
  else
    res = [res, d[kk[0]] + d[kk[1]]+d[kk[2]]].max
  end
end
puts res

def get_arr(arr)
  a = arr[0]
  b = arr.split(a)[0]
  return [a,b]
end

def exist?(arr, arr1)
  arr.each do |x|
    if x == arr1
      return true
      break
    end
  end
  false
end
n = gets.chomp.to_i
arr, array, len = [], [], []
(1..n).each do |i|
  a = gets.chomp
  t = String.new(a)
  num = 0
  a.each_char do |x|
    if a.delete(x) != a
      a.delete!(x)
      num += 1
    end
    if num == 3
      break
    end
  end
  if num < 3
    arr << t
  end
end
arr.each_with_index do |arr1, index1|
  len[index1] = arr1.length
  if !exist?(array, arr1)
    arr.each_with_index do |arr2, index2|
      if index1 == index2
        next
      else
        if get_arr(arr1) == get_arr(arr2) || arr1.squeeze.include?(arr2.squeeze)
          len[index1] += arr2.length
        end
      end
    end
    array << get_arr(arr1)
  end
end
p len.sort.last

def get_arr(arr)
  a = arr[0]
  b = arr.delete(a)[0]
  return [a,b]
end

def exist?(arr, arr1)
  arr.each do |x|
    if x == arr1
      return true
      break
    end
  end
  false
end
n = gets.chomp.to_i
x, arr, array, len = [], [], [], [0]
(1..n).each do |i|
  a = gets.chomp
  t = String.new(a)
  num = 0
  a.each_char do |x|
    if a.delete(x) != a
      a.delete!(x)
      num += 1
    end
    if num == 3
      break
    end
  end
  if num < 3
    arr << t
  end
end
arr.each_with_index do |arr1, index1|
  len[index1] = arr1.length
  if !exist?(array, arr1)
    arr.each_with_index do |arr2, index2|
      if index1 == index2
        next
      else
        if get_arr(arr1) == get_arr(arr2) || get_arr(arr1).reverse == get_arr(arr2) || arr1.squeeze.include?(arr2.squeeze)
          len[index1] += arr2.length
        end
      end
    end
    array << get_arr(arr1)
  end
end
l = len.sort.last
arr1 = ""
l1 = 0
arr.each do |a|
  if a.length == l
    arr1 = a
    break
  end
end
if arr1.squeeze.length == 1
  arr.each do |a|
    if a.squeeze.length == 1 && a != arr1 && a.length > l1
      l1 = a.length
    end
  end
  p l+l1
else
  p l
end

def get_arr(arr)
  a = arr[0]
  b = arr.delete(a)[0]
  return [a,b]
end

def exist?(arr, arr1)
  arr.each do |x|
    if x == arr1
      return true
      break
    end
  end
  false
end
n = gets.chomp.to_i
x, arr, array, len = [], [], [], [0]
(1..n).each do |i|
  a = gets.chomp
  t = String.new(a)
  num = 0
  a.each_char do |x|
    if a.delete(x) != a
      a.delete!(x)
      num += 1
    end
    if num == 3
      break
    end
  end
  if num < 3
    arr << t
  end
end
arr.each_with_index do |arr1, index1|
  len[index1] = arr1.length
  if !exist?(array, arr1)
    arr.each_with_index do |arr2, index2|
      if index1 == index2
        next
      else
        if get_arr(arr1) == get_arr(arr2) || arr1.squeeze.include?(arr2.squeeze)
          len[index1] += arr2.length
        end
      end
    end
    array << get_arr(arr1)
  end
end
l = len.sort.last
if arr.length > 1
  arr.each do |k|
    if k.squeeze.length == 1
      x << k.length
    end
  end
  x.sort!
  l1 = x.last + x[x.length-2]
end
p l1 && l1 > l ? l1 : l

def get_arr(arr)
  a = arr[0]
  b = arr.delete(a)[0]
  return [a,b]
end

def exist?(arr, arr1)
  arr.each do |x|
    if x == arr1
      return true
      break
    end
  end
  false
end
n = gets.chomp.to_i
x, arr, array, len = [], [], [], [0]
(1..n).each do |i|
  a = gets.chomp
  t = String.new(a)
  num = 0
  a.each_char do |x|
    if a.delete(x) != a
      a.delete!(x)
      num += 1
    end
    if num == 3
      break
    end
  end
  if num < 3
    arr << t
  end
end
arr.each_with_index do |arr1, index1|
  len[index1] = arr1.length
  if !exist?(array, arr1)
    arr.each_with_index do |arr2, index2|
      if index1 == index2
        next
      else
        if get_arr(arr1) == get_arr(arr2) || arr1.squeeze.include?(arr2.squeeze)
          len[index1] += arr2.length
        end
      end
    end
    array << get_arr(arr1)
  end
end
l = len.sort.last
arr.each do |k|
  if k.squeeze.length == 1
    x << k.length
  end
end
x.sort!
l1 = x.last + x[x.length-2]
p l1 > l ? l1 : l

def get_arr(arr)
  a = arr[0]
  b = arr.delete(a)[0]
  return [a,b]
end

def exist?(arr, arr1)
  arr.each do |x|
    if x == arr1
      return true
      break
    end
  end
  false
end
n = gets.chomp.to_i
x, arr, array, len = [], [], [], [0]
(1..n).each do |i|
  a = gets.chomp
  t = String.new(a)
  num = 0
  a.each_char do |x|
    if a.delete(x) != a
      a.delete!(x)
      num += 1
    end
    if num == 3
      break
    end
  end
  if num < 3
    arr << t
  end
end
arr.each_with_index do |arr1, index1|
  len[index1] = arr1.length
  if !exist?(array, arr1)
    arr.each_with_index do |arr2, index2|
      if index1 == index2
        next
      else
        if get_arr(arr1) == get_arr(arr2) || get_arr(arr1).reverse == get_arr(arr2) || arr1.squeeze.include?(arr2.squeeze)
          len[index1] += arr2.length
        end
      end
    end
    array << get_arr(arr1)
  end
end
l = len.sort.last
index = len.index(l)
if arr[index].squeeze.length == 1
  l1 = 0
  arr.each do |arr1|
    if arr1.length > l1 && arr1 != arr[index]
      l1 = arr1.length
    end
  end
  p l+l1
else
  p l
end


Little Vasya went to the supermarket to get some groceries. He walked about the supermarket for a long time and got a basket full of products. Now he needs to choose the cashier to pay for the products.There are n cashiers at the exit from the supermarket. At the moment the queue for the i-th cashier already has ki people. The j-th person standing in the queue to the i-th cashier has mi, j items in the basket. Vasya knows that:  the cashier needs 5 seconds to scan one item;  after the cashier scans each item of some customer, he needs 15 seconds to take the customer's money and give him the change. Of course, Vasya wants to select a queue so that he can leave the supermarket as soon as possible. Help him write a program that displays the minimum number of seconds after which Vasya can get to one of the cashiers.

The first line contains integer n (1 ≤ n ≤ 100) — the number of cashes in the shop. The second line contains n space-separated integers: k1, k2, ..., kn (1 ≤ ki ≤ 100), where ki is the number of people in the queue to the i-th cashier. The i-th of the next n lines contains ki space-separated integers: mi, 1, mi, 2, ..., mi, ki (1 ≤ mi, j ≤ 100) — the number of products the j-th person in the queue for the i-th cash has.

Print a single integer — the minimum number of seconds Vasya needs to get to the cashier.

NoteIn the second test sample, if Vasya goes to the first queue, he gets to the cashier in 100·5 + 15 = 515 seconds. But if he chooses the second queue, he will need 1·5 + 2·5 + 2·5 + 3·5 + 4·15 = 100 seconds. He will need 1·5 + 9·5 + 1·5 + 3·15 = 100 seconds for the third one and 7·5 + 8·5 + 2·15 = 105 seconds for the fourth one. Thus, Vasya gets to the cashier quicker if he chooses the second or the third queue.

n = gets.chomp.to_i
cashiers = gets.chomp.split(/ /).map(&:to_i)
items = []
n.times do
    items << gets.chomp.split(/ /).map(&:to_i)
end
ans = 100000
cashiers.each_index do |cashier|
    people = cashiers[cashier]
    time = 0
    people.times do |p|
        item = items[cashier][p]
        time += item * 5
    end
    time += people * 15
    ans = [time, ans].min
end
puts ans

a=gets.chomp.to_i
b=gets.chomp.split(" ")
0.upto(a-1) {|i| b[i]=b[i].to_i}
ar=[]
0.upto(a-1) do |i|
temp=gets.chomp.split(" ")
temp2=0
temp.each {|j| temp2+=j.to_i   }
temp2=temp2*5+ b[i]*15
ar << temp2
end
ans=ar.min
puts ans.to_s

n = STDIN.readline.to_i
f = STDIN.readline.split.map { |x| x.to_i }
ans = Array.new(n,0)
n.times do |i|
	t = STDIN.readline.split.map { |x| x.to_i }
	t.each { |x| ans[i] += 5*x+15 }
end
print ans.min

N=gets().to_i
def Solve(line)
  res=0
  for i in line
     res+=(i*5)
  end
  res+=15*line.size
  res
end
people_num_arr=gets().split(" ").map{|i| i.to_i}
lis=[]
for i in 0...N
   line=gets().split(" ").map{|i| i.to_i}
   lis<<Solve(line)
end
lis.sort!
puts lis[0]

n = gets.to_i
ppl_count = gets.split(" ").map(&:to_i)
arr = []
n.times do
	arr << gets.split(" ").map(&:to_i)
end

sum_arr = arr.map do |a|
	sum = a.inject(0){ |s,e| s += e * 5 }
	sum +=  a.length * 15
	sum
end
# puts "#{sum_arr}"
puts sum_arr.min

readin=->(){gets.split.map(&:to_i)}
n,k,mn=gets.to_i,readin[],0x3f3f3f3f
n.times{|i|mn=[mn,readin[].inject(:+)*5+k[i]*15].min}
puts mn

n = Integer gets
k = gets.split.map {|x| Integer x}
ans = Array.new(n, 0)
for i in 0..n - 1
  sum = 0
  arr = gets.split.map {|x|
    sum += Integer x
    Integer x
  }
  ans[i] += sum * 5 + 15 * arr.length
end
puts ans.min

n = gets.to_i
k = gets.split(' ').map{|i| i.to_i}
best = 1023456789
for i in 0..n-1
	best = [best, gets.split(' ').map{|i| i.to_i}.reduce{|a, b| a+b} * 5 + k[i] * 15].min
end
puts best

=begin
$stdin = File.open("./in.txt", "r")
$stdout = File.open("./out.txt", "w")
=end

n = gets.to_i
k = gets.split.map { |number| number.to_i }
m = []
n.times do |i|
  m << gets.split.map { |number| number.to_i }
end

answer = m.map { |queue|
  time = queue.size * 15
  queue.each { |items| time += items * 5 }
  time
}.min

print answer

n=gets.to_i;a=Array.new(n,0)
k=gets.chomp.split.map(&:to_i)
a=[]
n.times do
	x=gets.chomp.split.map(&:to_i)
	sum=0
	x.each do |i|
		sum+=(5*i)+15
	end
	a<<sum
end
puts a.min


Let's call a string $$$s$$$ perfectly balanced if for all possible triplets $$$(t,u,v)$$$ such that $$$t$$$ is a non-empty substring of $$$s$$$ and $$$u$$$ and $$$v$$$ are characters present in $$$s$$$, the difference between the frequencies of $$$u$$$ and $$$v$$$ in $$$t$$$ is not more than $$$1$$$.For example, the strings "aba" and "abc" are perfectly balanced but "abb" is not because for the triplet ("bb",'a','b'), the condition is not satisfied.You are given a string $$$s$$$ consisting of lowercase English letters only. Your task is to determine whether $$$s$$$ is perfectly balanced or not.A string $$$b$$$ is called a substring of another string $$$a$$$ if $$$b$$$ can be obtained by deleting some characters (possibly $$$0$$$) from the start and some characters (possibly $$$0$$$) from the end of $$$a$$$.

The first line of input contains a single integer $$$t$$$ ($$$1\leq t\leq 2\cdot 10^4$$$) denoting the number of testcases. Each of the next $$$t$$$ lines contain a single string $$$s$$$ ($$$1\leq |s|\leq 2\cdot 10^5$$$), consisting of lowercase English letters. It is guaranteed that the sum of $$$|s|$$$ over all testcases does not exceed $$$2\cdot 10^5$$$.

For each test case, print "YES" if $$$s$$$ is a perfectly balanced string, and "NO" otherwise. You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer).

gets.to_i.times do
  s = gets.chomp
  s2 = s[0...s.index(s[0], 1)]
  ok = s2.chars.tally.values.max == 1 && s.chars.zip(0..).all? { |ch, i| ch == s2[i % s2.size] }
  puts ok ? "YES" : "NO"
end


Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal — compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ.Determine the array Ivan will obtain after performing all the changes.

The first line contains an single integer n (2 ≤ n ≤ 200 000) — the number of elements in Ivan's array. The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ n) — the description of Ivan's array.

In the first line print q — the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.

NoteIn the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers — this permutation is lexicographically minimal among all suitable. In the second example Ivan does not need to change anything because his array already is a permutation.

n = gets.to_i

cnt = [1]
arr = gets.split.map{|x| cnt << 0; x.to_i}

arr.each{|x| cnt[x] += 1}

miss = []
cnt.each_with_index{|x, i| miss << i if x == 0}

p miss.size

exist = []
j = 0

puts arr.map{|x|
	if exist[x] || (cnt[x] > 1 && miss[j] < x)
		cnt[x] -= 1
		j += 1
		miss[j - 1]
	else
		exist[x] = 1
		x
	end
}.join(" ")

n, l = gets.to_i + 1, gets.split.map(&:to_i)
cnt, b, c = Array.new(n, 0), 1, 0
l.each {|a| cnt[a] += 1}
l.each_with_index do |a, i|
  if cnt[a] > 1
    b += 1 while cnt[b] > 0
    if a < b && cnt[a] < n
      cnt[a] = 999999
    else
      cnt[a] -= 1
      l[i] = b
      b += 1
      c += 1
    end
  end
end
p c
puts l.map(&:to_s).join(' ')

n, l = gets.to_i + 1, gets.split.map(&:to_i)
c, b = Array.new(n, 0), 1
l.each {|a| c[a] += 1}
l.each_with_index do |a, i|
  if c[a] > 1
    b += 1 while c[b] > 0
    if a < b && c[a] < n
      c[a] = 999999
    else
      c[a] -= 1
      l[i] = b
      b += 1
    end
  end
end
p c.count(0) - 1
puts l.map(&:to_s).join(' ')

n, l = gets.to_i + 1, gets.split.map(&:to_i)
cnt, b = Array.new(n, 0), 1
l.each {|a| cnt[a] += 1}
l.each_with_index do |a, i|
  if cnt[a] > 1
    b += 1 while cnt[b] > 0
    if a < b && cnt[a] < n
      cnt[a] = 999999
    else
      cnt[a] -= 1
      l[i] = b
      b += 1
    end
  end
end
puts l.map(&:to_s).join(" ")

n, aa = gets.to_i+1, gets.split.map(&:to_i)
cnt, b = Array.new(n, 0), 1
aa.each {|a| cnt[a] += 1}
aa.each_with_index do |a, i|
  if cnt[a] > 1
    b += 1 while cnt[b] > 0
    if a < b && cnt[a] < n
      cnt[a] = 999999
    else
      cnt[a] -= 1
      aa[i] = b
      b += 1
    end
  end
end
p aa


For some reason in many American cartoons anvils fall from time to time onto heroes' heads. Of course, safes, wardrobes, cruisers, planes fall sometimes too... But anvils do so most of all.Anvils come in different sizes and shapes. Quite often they get the hero stuck deep in the ground. But have you ever thought who throws anvils from the sky? From what height? We are sure that such questions have never troubled you!It turns out that throwing an anvil properly is not an easy task at all. Let's describe one of the most popular anvil throwing models.Let the height p of the potential victim vary in the range [0;a] and the direction of the wind q vary in the range [ - b;b]. p and q could be any real (floating) numbers. Then we can assume that the anvil will fit the toon's head perfectly only if the following equation has at least one real root:   Determine the probability with which an aim can be successfully hit by an anvil.You can assume that the p and q coefficients are chosen equiprobably and independently in their ranges.

The first line contains integer t (1 ≤ t ≤ 10000) — amount of testcases. Each of the following t lines contain two space-separated integers a and b (0 ≤ a, b ≤ 106). Pretests contain all the tests with 0 < a < 10, 0 ≤ b < 10.

Print t lines — the probability of a successful anvil hit for each testcase. The absolute or relative error of the answer should not exceed 10 - 6.

#Infinity
#geti,getia
#Object # dcopy
#
#String # to_a
#   "abc".to_a #=> ["a","b","c"]
#String # to_ia
#   "1 2 3".to_ia #=> [1,2,3]
#
#Array # rsort,rsort!
#   [1,3,2,4].rsort #=> [4,3,2,1]
#
#Integer # divisor
#   24.divisor #=> [1,2,3,4,6,8,12,24]
#
include Math
require "prime"
require "bigdecimal"
require "bigdecimal/math"
require "matrix"
require "pp"
require "benchmark"

Infinity = 1/0.0
class Matrix;def inspect;empty? ? "Matrix.empty(#{row_size}, #{column_size})":"Matrix[\n#{@rows.map(&:inspect).join("\n")}\n]";end;end
def pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end
class String;def to_a;split("");end;end
class String;def to_ia;split.map(&:to_i);end;end
def getia;gets.to_ia;end
def geti;gets.to_i;end
class Object;def dcopy;Marshal.load(Marshal.dump(self));end;end
class Array;def rsort;sort.reverse;end;def rsort!;sort!.reverse!;end;end

t = geti
t.times do
    a,b = gets.split.map(&:to_f)
    y = a * b
    if 4 * b > a
        y += a * a / 8.0
    else
        y += y - b * b * 2.0
    end
    if a == 0 && b == 0
        puts 1
    elsif a == 0
        puts 0.5
    elsif b == 0
        puts 1
    else
        puts y / (a * b * 2.0)
    end
end

n = gets.to_i
n.times do
  a, b = gets.split.map(&:to_i)
  if b == 0
    o = 1.0
  elsif a == 0
    o = 0.5
  elsif a < 4*b
    o = (a*b + a*a*Rational(1,8))/(2*a*b)
  else
    o = (2*a*b - b*b*Rational(2,1))/(2*a*b)
  end
  p o.to_f
end

#!/usr/bin/ruby

$stdin = File.open('test_69_4') if $DEBUG

t = gets.to_i
t.times do
  a, b = gets.split.map(&:to_i)
  total = b*2*a
  square = a.to_f/4 >= b ? b*b*2 : a*(b - a.to_f/8)
  puts b == 0 ? 1.0 : a == 0 ? 0.5 : (total - square).to_f / total
end

T = gets.to_i

T.times do
  a, b = gets.split.map{|e| e.to_i}

  if (a == 0 && b == 0)
    puts 1.0
  elsif (a == 0)
    puts 0.5
  elsif (b == 0)
    puts 1.0
  else
    if (b > a / 4.0)
      puts 0.5 + (0.5 * a * a / 4.0) / (2 * a * b)
    else
      puts 1.0 - (0.5 * b * b * 4) / (2 * a * b)
    end
  end

end

#Infinity
#geti,getia
#Object # dcopy
#
#String # to_a
#   "abc".to_a #=> ["a","b","c"]
#String # to_ia
#   "1 2 3".to_ia #=> [1,2,3]
#
#Array # rsort,rsort!
#   [1,3,2,4].rsort #=> [4,3,2,1]
#
#Integer # divisor
#   24.divisor #=> [1,2,3,4,6,8,12,24]
#
include Math
require "prime"
require "bigdecimal"
require "bigdecimal/math"
require "matrix"
require "pp"
require "benchmark"

Infinity = 1/0.0
class Matrix;def inspect;empty? ? "Matrix.empty(#{row_size}, #{column_size})":"Matrix[\n#{@rows.map(&:inspect).join("\n")}\n]";end;end
def pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end
class String;def to_a;split("");end;end
class String;def to_ia;split.map(&:to_i);end;end
def getia;gets.to_ia;end
def geti;gets.to_i;end
class Object;def dcopy;Marshal.load(Marshal.dump(self));end;end
class Array;def rsort;sort.reverse;end;def rsort!;sort!.reverse!;end;end

t = geti
t.times do
    a,b = getia
    y = a * b
    if 4 * b > a
        y += a * a / 8.0
    else
        y += y - b * b * 2.0
    end
    puts y / (a * b * 2.0)
end

n = gets.to_i
n.times do
  a, b = gets.split.map(&:to_i)
  if a == 0 || b == 0
    o = 1.0
  elsif a < 4*b
    o = (a*b + a*a*Rational(1,8))/(2*a*b)
  else
    o = (2*a*b - b*b*Rational(1,8))/(2*a*b)
  end
  p o.to_f
end

n = gets.to_i
n.times do
  a, b = gets.split.map(&:to_i)
  if b == 0
    o = 1.0
  elsif a == 0
    o = 0.5
  elsif a < 4*b
    o = (a*b + a*a*Rational(1,8))/(2*a*b)
  else
    o = (2*a*b - b*b*Rational(1,8))/(2*a*b)
  end
  p o.to_f
end

#!/usr/bin/ruby

$stdin = File.open('test_69_4') if $DEBUG

t = gets.to_i
t.times do
  a, b = gets.split.map(&:to_i)
  total = b*2*a
  square = a.to_f/4 >= b ? b*b*2 : a*(b - a.to_f/8)
  puts b == 0 ? 1 : a == 0 ? 0.5 : (total - square) / total
end

T = gets.to_i

T.times do
  a, b = gets.split.map{|e| e.to_i}

  if (b > a / 4.0)
    puts 0.5 + (0.5 * a * a / 4.0) / (2 * a * b)
  else
    puts 1.0 - (0.5 * b * b * 4) / (2 * a * b)
  end

end


There are n Imperial stormtroopers on the field. The battle field is a plane with Cartesian coordinate system. Each stormtrooper is associated with his coordinates (x, y) on this plane. Han Solo has the newest duplex lazer gun to fight these stormtroopers. It is situated at the point (x0, y0). In one shot it can can destroy all the stormtroopers, situated on some line that crosses point (x0, y0).Your task is to determine what minimum number of shots Han Solo needs to defeat all the stormtroopers.The gun is the newest invention, it shoots very quickly and even after a very large number of shots the stormtroopers don't have enough time to realize what's happening and change their location.

The first line contains three integers n, x0 и y0 (1 ≤ n ≤ 1000,  - 104 ≤ x0, y0 ≤ 104) — the number of stormtroopers on the battle field and the coordinates of your gun. Next n lines contain two integers each xi, yi ( - 104 ≤ xi, yi ≤ 104) — the coordinates of the stormtroopers on the battlefield. It is guaranteed that no stormtrooper stands at the same point with the gun. Multiple stormtroopers can stand at the same point.

Print a single integer — the minimum number of shots Han Solo needs to destroy all the stormtroopers.

NoteExplanation to the first and second samples from the statement, respectively:

n,x,y=gets.split.map(&:to_i)
a=[]
n.times do
  x1,y1 =gets.split.map(&:to_f)
  a << (x1==x ? 'w':(y1-y)/(x1-x))
end
p a.uniq.count

require 'set'

n, x0, y0 = gets.split.map(&:to_f)

set = Set.new
n.to_i.times do
  x, y = gets.split.map(&:to_f)

  if x == x0
    set << true
  else
    set << (y - y0) / (x - x0)
  end
end

puts set.length

n,x,y=gets.split.map(&:to_i)
s={}
n.times do
    x1,y1=gets.split.map(&:to_f)
    s[(y1-y)/(x1-x)]=1
end
p s[1.0/0]&&s[-1.0/0]?(s.size-1):s.size

n,x0,y0=gets.chomp.split.map(&:to_i)
vx,vy,ans,i,j=[],[],n,0,0
n.times do |i|
	x,y=gets.chomp.split.map(&:to_i)
	vx<<(x-x0);vy<<(y-y0)
	for j in 0...i
		if vx[i] * vy[j] == vx[j] * vy[i]
			ans-=1
			break
		end
	end
end
puts ans

#!/usr/bin/ruby
# coding: utf-8

n, x0, y0 = gets.split.map &:to_i
data = Array.new(n) { Array.new(2, 0) }
l = []
flag = 0
m = 0
n.times{|_|
  data[_] = gets.split.map &:to_i
  if x0 == data[_][0]
    flag = 1
  else
    l << (1.0 * y0 - data[_][1]) / (1.0 * x0 - data[_][0])
  end
}
puts (l.uniq.size + flag)

require 'set'
def read_ints; STDIN.gets.split.map(&:to_i); end

n, x0, y0 = read_ints

inp = n.times.map do
  p = read_ints
  p[0] -= x0
  p[1] -= y0
  p
end

dot_set = inp.to_set

ans = 0
loop do
  break if dot_set.empty?
  ans += 1
  p = dot_set.first
  dot_set.delete_if do |dot|
    dot[0] * p[1] == dot[1] * p[0]
  end
end

puts ans

n,x,y=gets.split.map{|e| e.to_i}
ps=Hash.new(0)
add=0
n.times{
	xi,yi=gets.split.map{|e| e.to_i}
	xi=(xi-x)
	yi=(yi-y)
	if xi==0 then
		add=1
	else
		m1=1
		m1=-1 if xi<0
		d1=yi.abs.gcd(xi.abs)
		ps[[yi/d1*m1,xi/d1*m1]]+=1

	end
}
puts ps.size+add

#!/usr/bin/ruby

SMALL_NUMBER = 0.000000000001
input_line = gets
par = input_line.split(' ').map(&:to_f)
n = par[0]
x0 = par[1]
y0 = par[2]
i = 0;
coords = []
while(i < n) do
    input_line = gets
    par = input_line.split(' ').map(&:to_f)
    coords << [par[0],par[1]]
    i = i+1
end
counter = 0
while (coords.size() > 0) do
    counter  = counter + 1
    x1 = coords[0][0]
    y1 = coords[0][1]

    if ((x0-x1).abs > SMALL_NUMBER && (y0-y1).abs > SMALL_NUMBER)
        coords.delete_if do |coord|
            true if ( ( (coord[0]-x0)/(x1-x0) - (coord[1]-y0)/(y1-y0) ).abs < SMALL_NUMBER)
        end
    elsif ((x0-x1).abs < SMALL_NUMBER)
        coords.delete_if do |coord|
            true if ( (coord[0]-x0).abs < SMALL_NUMBER)
        end
    elsif ((y0-y1).abs < SMALL_NUMBER)
        coords.delete_if do |coord|
            true if ( (coord[1]-y0).abs < SMALL_NUMBER)
        end
    end
end

puts counter

# coding: utf-8
require "set"

n, x0, y0 = gets.split.map(&:to_i)
incline_hash = Set.new
n.times {
    x, y = gets.split.map(&:to_i)
    dx, dy = x0 - x, y0 - y
    if dx == 0
        incline_hash.add -1
    else
        incline_hash.add dy*1.0/dx
    end
}

puts incline_hash.length

n,x1,y1 = gets.split(" ").map(&:to_i)
arr = []
n.times do
	arr << gets.split(" ").map(&:to_i)
end
eq = arr.map do |a|
	x2,y2 = a
	(x2-x1) == 0 ? 10**9 : (y2-y1)/(x2-x1).to_f
end
puts eq.uniq.count

require 'set'

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

a = Set.new
n,x,y = gets.split.map(&:to_i)
1.upto(n).map do |_|
  ex,ey = gets.split.map(&:to_i)
  dx = ex - x
  dy = ey - y
  if dx < 0
    dx *= -1
    dy *= -1
  end
  if dx == 0
    a << [0,1]
  elsif dy == 0
    a << [1,0]
  else
    g = gcd(dx,dy).abs
    a << [dx/g, dy/g]
  end
end

puts a.size

n, x0, y0 = gets.split.map(&:to_i)
a = n.times.map do
  x1, y1 = gets.split.map(&:to_f)
  x1 == x0 ? 'w' : (y1 - y0) / (x1 - x0)
end
puts a.uniq.size

n, x, y = gets.split.map(&:to_i)
a = []
n.times do
  x1, y1 = gets.split.map(&:to_f)
  a << (x1 == x ? 'w' : (y1 - y) / (x1 - x))
end
puts a.uniq.count

n, x, y = gets.split.map(&:to_i)
a = []
n.times do
  x1, y1 = gets.split.map(&:to_f)
  c = (y1 - y) / (x1 - x)
  a << (c.infinite? ? c.abs : c.to_r)
end
puts a.uniq.count

Point = Struct.new(:x, :y)

def points_on_line(arr, zero, p)
  left = p.x - zero.x
  right = p.y - zero.y

  arr = arr.map { |elem|
    if (left * (elem.y - zero.y)) == (right * (elem.x - zero.x))
      nil
    else
      elem
    end
  }

  arr.compact
end

n, x0, y0 = gets.split(' ').map(&:to_i)

zero = Point.new(x0, y0)

arr = []
n.times do |_|
  x, y = gets.split(' ').map(&:to_i)
  arr << Point.new(x, y)
end

counter = 0
until arr.empty?
  p = arr.pop
  arr = points_on_line(arr, zero, p)

  counter += 1
end

p counter

require 'set'

n, x0, y0 = gets.split.map(&:to_f)

set = Set.new
n.to_i.times do
  x, y = gets.split.map(&:to_f)
  set << (y - y0) / (x - x0)
end

puts set.length

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

digits = gets.chomp.chars.map(&:to_i)
inverted = digits.map.with_index do |n, idx|
  case idx
  when 0
    (9 - n) < n && n != 9 ? 9 - n : n
  else
    (9 - n) < n ? 9 - n : n
  end
end

puts inverted.join.to_i

#!/usr/bin/ruby
# coding: utf-8

n, x0, y0 = gets.split.map &:to_i
data = Array.new(n) { Array.new(2, 0) }
l = []
flag = 0
m = 0
n.times{|_|
  data[_] = gets.split.map &:to_i
  if x0 == data[0]
    flag = 1
  else
    l << (1.0 * y0 - data[_][1]) / (1.0 * x0 - data[_][0])
  end
}
puts (l.uniq.size + flag)

#!/usr/bin/ruby
# coding: utf-8

n, x0, y0 = gets.split.map &:to_i
data = Array.new(n) { Array.new(2, 0) }
l = []
flag = 0
m = 0
n.times{|_|
  data[_] = gets.split.map &:to_i
  if x0 = data[0]
    flag = 1
  else
    l << (1.0 * y0 - data[_][1]) / (1.0 * x0 - data[_][0])
  end
}
puts (l.uniq.size + flag)

n,x,y=gets.split.map{|e| e.to_i}
ps=Hash.new(0)
add=0
n.times{
	xi,yi=gets.split.map{|e| e.to_i}
	xi=(xi-x)
	yi=(yi-y)
	if xi==0 then
		add=1
	else
		m=1
		m=-1 if xi<0
		ps[yi/(yi.abs.gcd(xi.abs))*m]+=1

	end
}
puts ps.size+add

#!/usr/bin/ruby

SMALL_NUMBER = 0.0000001
input_line = gets
par = input_line.split(' ').map(&:to_i)
n = par[0]
x0 = par[1]
y0 = par[2]
i = 0;
coords = []
while(i < n) do
    input_line = gets
    par = input_line.split(' ').map(&:to_i)
    coords << [par[0],par[1]]
    i = i+1
end
counter = 0
while (coords.size() > 0) do
 ##   puts coords.to_s
 ##   gets
    counter  = counter + 1
    x1 = coords[0][0]
    y1 = coords[0][1]

    if ((x0-x1).abs > SMALL_NUMBER && (y0-y1).abs > SMALL_NUMBER)
        coords.delete_if do |coord|
            true if ( ( (coord[0]-x0)/(x1-x0) - (coord[1]-y0)/(y1-y0) ).abs < SMALL_NUMBER)
        end
    elsif ((x0-x1).abs < SMALL_NUMBER)
        coords.delete_if do |coord|
            true if ( (coord[0]-x0).abs < SMALL_NUMBER)
        end
    elsif ((y0-y1).abs < SMALL_NUMBER)
        coords.delete_if do |coord|
            true if ( (coord[1]-y0).abs < SMALL_NUMBER)
        end
    end
end

puts counter

n,x,y=gets.split.map(&:to_i)
s={}
(1..n).each do |i|
	x1,y1=gets.split.map(&:to_i)
	s[1.0*(y1-y)/(x1-x)]=1
end
p s.size

n,x1,y1 = gets.split(" ").map(&:to_i)
arr = []
n.times do
	arr << gets.split(" ").map(&:to_i)
end
eq = arr.map do |a|
	x2,y2 = a
	(x2-x1) == 0 ? 10**9 : (y2-y1)/(x2-x1)
end
puts eq.uniq.count

n, x, y = gets.split.map(&:to_i)
a = []
n.times do
  x1, y1 = gets.split.map(&:to_i)
  dx = x1 - x
  dy = y1 - y
  gcd = dx.gcd(dy)
  a << "#{(dy/gcd).abs}/#{(dx/gcd).abs}"
end
puts a.uniq.count

n, x, y = gets.split.map(&:to_i)
a = []
n.times do
  x1, y1 = gets.split.map(&:to_f)
  a << (x1-x) / (y1-y)
end
puts a.uniq.count

n, x, y = gets.split.map(&:to_i)
a = []
n.times do
  x1, y1 = gets.split.map(&:to_f)
  c = (y1 - y) / (x1 - x)
  a << (c.infinite? ? c : c.to_r)
end
puts a.uniq.count


Ahmed and Mostafa used to compete together in many programming contests for several years. Their coach Fegla asked them to solve one challenging problem, of course Ahmed was able to solve it but Mostafa couldn't.This problem is similar to a standard problem but it has a different format and constraints.In the standard problem you are given an array of integers, and you have to find one or more consecutive elements in this array where their sum is the maximum possible sum.But in this problem you are given n small arrays, and you will create one big array from the concatenation of one or more instances of the small arrays (each small array could occur more than once). The big array will be given as an array of indexes (1-based) of the small arrays, and the concatenation should be done in the same order as in this array. Then you should apply the standard problem mentioned above on the resulting big array.For example let's suppose that the small arrays are {1, 6, -2}, {3, 3} and {-5, 1}. And the indexes in the big array are {2, 3, 1, 3}. So the actual values in the big array after formatting it as concatenation of the small arrays will be {3, 3, -5, 1, 1, 6, -2, -5, 1}. In this example the maximum sum is 9.Can you help Mostafa solve this problem?

The first line contains two integers n and m, n is the number of the small arrays (1 ≤ n ≤ 50), and m is the number of indexes in the big array (1 ≤ m ≤ 250000). Then follow n lines, the i-th line starts with one integer l which is the size of the i-th array (1 ≤ l ≤ 5000), followed by l integers each one will be greater than or equal -1000 and less than or equal 1000. The last line contains m integers which are the indexes in the big array, and you should concatenate the small arrays in the same order, and each index will be greater than or equal to 1 and less than or equal to n. The small arrays are numbered from 1 to n in the same order as given in the input. Some of the given small arrays may not be used in big array. Note, that the array is very big. So if you try to build it straightforwardly, you will probably get time or/and memory limit exceeded.

Print one line containing the maximum sum in the big array after formatting it as described above. You must choose at least one element for the sum, i. e. it cannot be empty. Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d).

small_size, big_indexes = gets.chomp.split.map{|s| s.to_i}
smalls = []
small_size.times do
  array = gets.chomp.split.map{|s| s.to_i}
  array.shift
  sum = r_sum = 0
  left = right = max = -1001
  tmp = 0
  array.size.times do |i|
    sum += array[i]
    r_sum += array[-i - 1]
    left = sum if sum > left
    right = r_sum if r_sum > right
    max = array[i] if array[i] > max
    tmp += array[i]
    if tmp < 0
      tmp = 0
    else
      max = tmp if tmp > max
    end
  end
  small = {
    :sum => sum,
    :left => left,
    :right => right,
    :max => max,
  }
  smalls << small
end
max = -1001
tmp = 0
gets.chomp.split.map{|s| s.to_i}.each do |i|
  i -= 1
  max = smalls[i][:max] if smalls[i][:max] >= max
  max = tmp + smalls[i][:left] if tmp + smalls[i][:left] > max
  tmp += smalls[i][:sum]
  tmp = smalls[i][:right] if smalls[i][:right] > tmp
  tmp = 0 if tmp < 0
end
puts max

small_size, big_indexes = gets.chomp.split.map{|s| s.to_i}
smalls = []
small_size.times do
  array = gets.chomp.split.map{|s| s.to_i}
  array.shift
  sum = r_sum = 0
  left = right = max = -1001
  tmp = 0
  array.size.times do |i|
    sum += array[i]
    r_sum += array[-i]
    left = [sum, left].max
    right = [r_sum, right].max
    max = [max, array[i]].max
    tmp += array[i]
    if tmp < 0
      tmp = 0
    else
      max = [max, tmp].max
    end
  end
  small = {
    :sum => sum,
    :left => left,
    :right => right,
    :max => max,
  }
  smalls << small
end
max = -1001
tmp = 0
gets.chomp.split.map{|s| s.to_i}.each do |i|
  i -= 1
  max = [max, smalls[i][:max]].max
  max = [max, tmp + smalls[i][:left]].max if tmp > 0
  tmp = [0, [smalls[i][:right], tmp + smalls[i][:sum]].max].max
end
puts max

small_size, big_indexes = gets.chomp.split.map{|s| s.to_i}
smalls = []
small_size.times do
  array = gets.chomp.split.map{|s| s.to_i}
  array.shift
  sum = left = max = 0
  array.each do |i|
    sum += i
    max = [max, i].max
    left = [sum, left].max
  end
  r_sum = right = 0
  array.reverse.each do |i|
    r_sum += i
    right = [sum, right].max
  end
  small = {
    :sum => sum,
    :left => left,
    :right => right,
    :max => max,
  }
  smalls << small
end
max = 0
tmp = nil
gets.chomp.split.map{|s| s.to_i}.each do |i|
  i -= 1
  unless tmp
    tmp = [smalls[i][:sum], smalls[i][:right], 0].max
    max = [max, smalls[i][:max]].max
    next
  end
  if smalls[i][:left] > 0 && tmp + smalls[i][:left] > max
    max = tmp + smalls[i][:left]
  end
  if smalls[i][:sum] >= 0
    tmp += smalls[i][:sum]
    next
  end
  if tmp + smalls[i][:sum] < 0
    max = [tmp, max].max
    tmp = nil
  else
    tmp += smalls[i][:sum]
  end
end
puts [max, tmp].max

small_size, big_indexes = gets.chomp.split.map{|s| s.to_i}
smalls = []
small_size.times do
  array = gets.chomp.split.map{|s| s.to_i}
  array.shift
  sum = r_sum = 0
  left = right = max = -1001
  array.each do |i|
    sum += i
    max = [max, i].max
    left = [sum, left].max
  end
  array.reverse.each do |i|
    r_sum += i
    right = [r_sum, right].max
  end
  small = {
    :sum => sum,
    :left => left,
    :right => right,
    :max => max,
  }
  smalls << small
end
max = -1001
tmp = nil
gets.chomp.split.map{|s| s.to_i}.each do |i|
  i -= 1
  unless tmp
    tmp = [smalls[i][:sum], smalls[i][:right]].max
    max = [max, smalls[i][:max]].max
    tmp = nil if tmp < 0
    next
  end
  if smalls[i][:left] > 0 && tmp + smalls[i][:left] > max
    max = tmp + smalls[i][:left]
  end
  if smalls[i][:sum] >= 0
    tmp += smalls[i][:sum]
    next
  end
  if tmp + smalls[i][:sum] < 0
    max = [tmp, max].max
    tmp = nil
  else
    tmp += smalls[i][:sum]
  end
end
puts [max, tmp || -1001].max

small_size, big_indexes = gets.chomp.split.map{|s| s.to_i}
smalls = []
small_size.times do
  array = gets.chomp.split.map{|s| s.to_i}
  array.shift
  sum = r_sum = 0
  left = right = max = -1001
  tmp = nil
  array.each do |i|
    sum += i
    left = [sum, left].max
    if tmp.nil?
      tmp = i if i > 0
      max = [max, i].max
    else
      max = [max, tmp].max
      if i >= 0 ||  tmp + i  > 0
        tmp += i
      else
        tmp = nil
      end
    end
  end
  array.reverse.each do |i|
    r_sum += i
    right = [r_sum, right].max
  end
  small = {
    :sum => sum,
    :left => left,
    :right => right,
    :max => max,
  }
  smalls << small
end
max = -1001
tmp = nil
gets.chomp.split.map{|s| s.to_i}.each do |i|
  i -= 1
  unless tmp
    tmp = smalls[i][:right] >= 0 ? smalls[i][:right] : nil
    max = [max, smalls[i][:max]].max
  else
    if smalls[i][:left] > 0 && tmp + smalls[i][:left] > max
      max = tmp + smalls[i][:left]
    end
    if smalls[i][:sum] >= 0 || tmp + smalls[i][:sum] >= 0
      tmp += smalls[i][:sum]
    else
      max = [tmp, max].max
      tmp = nil
    end
  end
end
puts [max, tmp || -1001].max

small_size, big_indexes = gets.chomp.split.map{|s| s.to_i}
smalls = []
small_size.times do
  array = gets.chomp.split.map{|s| s.to_i}
  array.shift
  sum = left = right = 0
  array.each{|i| sum += i}
  array.each do |i|
    if left + i > left
      left += i
    else
      break
    end
  end
  array.reverse.each do |i|
    if right + i > right
      right += i
    else
      break
    end
  end
  small = {
    :sum => sum,
    :left => left,
    :right => right
  }
  smalls << small
end
max = 0
tmp = nil
gets.chomp.split.map{|s| s.to_i}.each do |i|
  i -= 1
  unless tmp
    tmp = [smalls[i][:sum], smalls[i][:right], 0].max
    next
  end
  if smalls[i][:left] > 0 && tmp + smalls[i][:left] > max
    max = tmp + smalls[i][:left]
  end
  if smalls[i][:sum] >= 0
    tmp += smalls[i][:sum]
    next
  end
  if tmp + smalls[i][:sum] < 0
    max = [tmp, max].max
    tmp = nil
  else
    tmp += smalls[i][:sum]
  end
end
puts [max, tmp].max

small_size, big_indexes = gets.chomp.split.map{|s| s.to_i}
smalls = []
small_size.times do
  array = gets.chomp.split.map{|s| s.to_i}
  array.shift
  sum = left = max = 0
  array.each do |i|
    sum += i
    max = [max, i].max
    left = [sum, left].max
  end
  r_sum = right = 0
  array.reverse.each do |i|
    r_sum += i
    right = [r_sum, right].max
  end
  small = {
    :sum => sum,
    :left => left,
    :right => right,
    :max => max,
  }
  smalls << small
end
max = 0
tmp = nil
gets.chomp.split.map{|s| s.to_i}.each do |i|
  i -= 1
  unless tmp
    tmp = [smalls[i][:sum], smalls[i][:right]].max
    max = [max, smalls[i][:max]].max
    tmp = nil if tmp <= 0
    next
  end
  if smalls[i][:left] > 0 && tmp + smalls[i][:left] > max
    max = tmp + smalls[i][:left]
  end
  if smalls[i][:sum] >= 0
    tmp += smalls[i][:sum]
    next
  end
  if tmp + smalls[i][:sum] < 0
    max = [tmp, max].max
    tmp = nil
  else
    tmp += smalls[i][:sum]
  end
end
puts [max, tmp || 0].max

small_size, big_indexes = gets.chomp.split.map{|s| s.to_i}
smalls = []
small_size.times do
  array = gets.chomp.split.map{|s| s.to_i}
  array.shift
  sum = r_sum = 0
  left = right = max = -1001
  tmp = 0
  array.size.times do |i|
    i = array[i]
    sum += i
    r_sum += i
    left = [sum, left].max
    right = [sum, right].max
    max = [max, i].max
    tmp += i
    if tmp < 0
      tmp = 0
    else
      max = [max, tmp].max
    end
  end
  array.reverse.each do |i|
    r_sum += i
    right = [r_sum, right].max
  end
  small = {
    :sum => sum,
    :left => left,
    :right => right,
    :max => max,
  }
  smalls << small
end
max = -1001
tmp = 0
gets.chomp.split.map{|s| s.to_i}.each do |i|
  i -= 1
  max = [max, smalls[i][:max]].max
  max = [max, tmp + smalls[i][:left]].max if tmp > 0
  tmp = [0, [smalls[i][:right], tmp + smalls[i][:sum]].max].max
end
puts max

small_size, big_indexes = gets.chomp.split.map{|s| s.to_i}
smalls = []
small_size.times do
  array = gets.chomp.split.map{|s| s.to_i}
  array.shift
  sum = left = right = 0
  array.each do |i|
    sum += i
    left += i if left + i > left
  end
  array.reverse.each do |i|
    right += i if right + i > right
  end
  small = {
    :sum => sum,
    :left => left,
    :right => right
  }
  smalls << small
end
max = 0
tmp = nil
gets.chomp.split.map{|s| s.to_i}.each do |i|
  i -= 1
  unless tmp
    tmp = [smalls[i][:sum], smalls[i][:right], 0].max
    next
  end
  if smalls[i][:sum] >= 0
    tmp += smalls[i][:sum]
    next
  end
  if smalls[i][:left] > 0 && tmp + smalls[i][:left] > max
    max = tmp + smalls[i][:left]
  end
  if tmp + smalls[i][:sum] < 0
    tmp = nil
  else
    tmp += smalls[i][:sum]
  end
end
puts max

small_size, big_indexes = gets.chomp.split.map{|s| s.to_i}
smalls = []
small_size.times do
  array = gets.chomp.split.map{|s| s.to_i}
  array.shift
  sum = r_sum = 0
  left = right = max = -1001
  tmp = 0
  array.size.times do |i|
    sum += array[i]
    r_sum += array[-i - 1]
    left = [sum, left].max
    right = [r_sum, right].max
    max = [max, array[i]].max
    tmp += array[i]
    if tmp < 0
      tmp = 0
    else
      max = [max, tmp].max
    end
  end
  small = {
    :sum => sum,
    :left => left,
    :right => right,
    :max => max,
  }
  smalls << small
end
max = -1001
tmp = 0
gets.chomp.split.map{|s| s.to_i}.each do |i|
  i -= 1
  p smalls[i]
  p tmp
  max = [max, smalls[i][:max]].max
  max = [max, tmp + smalls[i][:left]].max if tmp > 0
  tmp = [0, [smalls[i][:right], tmp + smalls[i][:sum]].max].max
end
puts max


Valentin participates in a show called "Shockers". The rules are quite easy: jury selects one letter which Valentin doesn't know. He should make a small speech, but every time he pronounces a word that contains the selected letter, he receives an electric shock. He can make guesses which letter is selected, but for each incorrect guess he receives an electric shock too. The show ends when Valentin guesses the selected letter correctly.Valentin can't keep in mind everything, so he could guess the selected letter much later than it can be uniquely determined and get excessive electric shocks. Excessive electric shocks are those which Valentin got after the moment the selected letter can be uniquely determined. You should find out the number of excessive electric shocks.

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of actions Valentin did. The next n lines contain descriptions of his actions, each line contains description of one action. Each action can be of one of three types:    Valentin pronounced some word and didn't get an electric shock. This action is described by the string ". w" (without quotes), in which "." is a dot (ASCII-code 46), and w is the word that Valentin said.  Valentin pronounced some word and got an electric shock. This action is described by the string "! w" (without quotes), in which "!" is an exclamation mark (ASCII-code 33), and w is the word that Valentin said.  Valentin made a guess about the selected letter. This action is described by the string "? s" (without quotes), in which "?" is a question mark (ASCII-code 63), and s is the guess — a lowercase English letter.  All words consist only of lowercase English letters. The total length of all words does not exceed 105. It is guaranteed that last action is a guess about the selected letter. Also, it is guaranteed that Valentin didn't make correct guesses about the selected letter before the last action. Moreover, it's guaranteed that if Valentin got an electric shock after pronouncing some word, then it contains the selected letter; and also if Valentin didn't get an electric shock after pronouncing some word, then it does not contain the selected letter.

Output a single integer — the number of electric shocks that Valentin could have avoided if he had told the selected letter just after it became uniquely determined.

8
! hello
! codeforces
? c
. o
? d
? h
. l
? e

7
! ababahalamaha
? a
? b
? a
? b
? a
? h

NoteIn the first test case after the first action it becomes clear that the selected letter is one of the following: a, b, c. After the second action we can note that the selected letter is not a. Valentin tells word "b" and doesn't get a shock. After that it is clear that the selected letter is c, but Valentin pronounces the word cd and gets an excessive electric shock. In the second test case after the first two electric shocks we understand that the selected letter is e or o. Valentin tries some words consisting of these letters and after the second word it's clear that the selected letter is e, but Valentin makes 3 more actions before he makes a correct hypothesis.In the third example the selected letter can be uniquely determined only when Valentin guesses it, so he didn't get excessive electric shocks.

n = gets.to_i

s = ('a'..'z').to_a
cnt = 0
n.times do |k|
  t, w = gets.split
  if t == '!'
    cnt += 1 if s.size == 1
    s = s & w.split('')
  elsif t == '.'
    s = s - w.split('')
  elsif t == '?'
    break if k == n - 1
    cnt += 1 if s.size == 1
    s = s - w.split('')
  end
end

puts cnt

n = gets.to_i

s = ('a'..'z').to_a
cnt = 0
n.times do
  t, w = gets.split
  if t == '!'
    s = s & w.split('')
    cnt += 1 if s.size == 1
  elsif t == '.'
    s = s - w.split('')
  elsif t == '?'
    cnt += 1 if s.size == 1 && !w.split('').include?(s[0])
  end
end

puts cnt

n = gets.to_i

s = ('a'..'z').to_a
cnt = 0
n.times do
  t, w = gets.split
  if t == '!'
    cnt += 1 if s.size == 1
    s = s & w.split('')
  elsif t == '.'
    s = s - w.split('')
  elsif t == '?'
    cnt += 1 if s.size == 1 && !w.split('').include?(s[0])
  end
end

puts cnt

n = gets.to_i

s = ('a'..'z').to_a
cnt = 0
n.times do |k|
  t, w = gets.split
  if t == '!'
    s = s & w.split('')
    cnt += 1 if s.size == 1
  elsif t == '.'
    s = s - w.split('')
  elsif t == '?'
    if k == n - 1
      cnt += 1 if s.size != 1
    else
      cnt += 1 if s.size != 1 || !w.split('').include?(s[0])
    end
  end
end

puts cnt

n = gets.to_i

s = ('a'..'z').to_a
cnt = 0
n.times do |k|
  t, w = gets.split
  if t == '!'
    s = s & w.split('')
    cnt += 1 if s.size == 1
  elsif t == '.'
    s = s - w.split('')
  elsif t == '?'
    if k == n - 1
      cnt += 1 if s.size != 1
    else
      cnt += 1 if s.size == 1 && !w.split('').include?(s[0])
    end
  end
end

puts cnt


В самолёте есть n рядов мест. Если смотреть на ряды сверху, то в каждом ряду есть 3 места слева, затем проход между рядами, затем 4 центральных места, затем ещё один проход между рядами, а затем ещё 3 места справа.Известно, что некоторые места уже заняты пассажирами. Всего есть два вида пассажиров — статусные (те, которые часто летают) и обычные. Перед вами стоит задача рассадить ещё k обычных пассажиров так, чтобы суммарное число соседей у статусных пассажиров было минимально возможным. Два пассажира считаются соседями, если они сидят в одном ряду и между ними нет других мест и прохода между рядами. Если пассажир является соседним пассажиром для двух статусных пассажиров, то его следует учитывать в сумме соседей дважды.

В первой строке следуют два целых числа n и k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10·n) — количество рядов мест в самолёте и количество пассажиров, которых нужно рассадить. Далее следует описание рядов мест самолёта по одному ряду в строке. Если очередной символ равен '-', то это проход между рядами. Если очередной символ равен '.', то это свободное место. Если очередной символ равен 'S', то на текущем месте будет сидеть статусный пассажир. Если очередной символ равен 'P', то на текущем месте будет сидеть обычный пассажир.  Гарантируется, что количество свободных мест не меньше k. Гарантируется, что все ряды удовлетворяют описанному в условии формату.

В первую строку выведите минимальное суммарное число соседей у статусных пассажиров. Далее выведите план рассадки пассажиров, который минимизирует суммарное количество соседей у статусных пассажиров, в том же формате, что и во входных данных. Если в свободное место нужно посадить одного из k пассажиров, выведите строчную букву 'x' вместо символа '.'.

4 9
PP.-PPPS-S.S
PSP-PPSP-.S.
.S.-S..P-SS.
P.S-P.PP-PSP

15
PPx-PPPS-S.S
PSP-PPSP-xSx
xSx-SxxP-SSx
P.S-PxPP-PSP

ПримечаниеВ первом примере нужно посадить ещё двух обычных пассажиров. Для минимизации соседей у статусных пассажиров, нужно посадить первого из них на третье слева место, а второго на любое из оставшихся двух мест, так как независимо от выбора места он станет соседом двух статусных пассажиров. Изначально, у статусного пассажира, который сидит на самом левом месте уже есть сосед. Также на четвёртом и пятом местах слева сидят статусные пассажиры, являющиеся соседями друг для друга (что добавляет к сумме 2).Таким образом, после посадки ещё двух обычных пассажиров, итоговое суммарное количество соседей у статусных пассажиров станет равно пяти.

n,k=gets.split.map(&:to_i)
rows=[]
n.times do
  rows<<gets.split('')
end

potentials=[]
initial_sum = 0
rows.each_with_index do |row, row_number|
  row.each_with_index do |k,row_i|
    if k=="."
      pot = 0
      if row[row_i-1]=="S"
        if row_i != 0
          pot+=1
        end
      end
      if row[row_i+1]=="S"
        pot+=1
      end
      potentials<<[[row_number,row_i],pot]
    elsif k=="S"
      sum = 0
      if row_i!=0 and (row[row_i-1] == "P" or row[row_i-1] == "S")
        sum+=1
      end
      if row[row_i+1] == "P" or row[row_i+1] == "S"
        sum+=1
      end
      initial_sum+=sum
    end
  end
end

selected = potentials.sort do |a,b|
  [a[1], a[0][0]]<=>[b[1],b[0][0]]
end[0..k-1]
sum = initial_sum + selected.inject(0){|memo, k| memo+k[1]}
selected.map do |p|
  rows[p[0][0]][p[0][1]] = "x"
end

puts sum
puts rows.map(&:join)

def near_count(row, pos, variants)
  [-1, 1].inject(0) do |res, delta|
    target = pos+delta
    res + (target >= 0 && target < row.length && variants.include?(row[target]) ? 1 : 0)
  end
end

n, k = gets.split(" ").map{|e| e.to_i }

queue = [[], [], []]

plane = (0...n).map do |i|
  row = gets.chomp
  (0...row.length).each do |j|
    next unless row[j] == '.'
    elite_near = near_count(row, j, ['S'])
    queue[elite_near] << [i, j]
  end
  row
end

rq = queue.inject([]){|r, e| r + e}

rq[0...k].each do |plan|
  plane[plan[0]][plan[1]] = 'x'
end

ans = plane.inject(0) do |r, row|
  r + (0...row.length).inject(0) do |r2, i|
    r2 + (row[i] != 'S' ? 0 : near_count(row, i, ['P', 'x', 'S']))
  end
end

puts ans
puts plane


A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b.You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars.

You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3.

Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print "-1" (without quotes).

NoteIn first test case all conditions for cars' sizes are satisfied.In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20.


You are given a sequence of integers of length $$$n$$$ and integer number $$$k$$$. You should print any integer number $$$x$$$ in the range of $$$[1; 10^9]$$$ (i.e. $$$1 \le x \le 10^9$$$) such that exactly $$$k$$$ elements of given sequence are less than or equal to $$$x$$$.Note that the sequence can contain equal elements.If there is no such $$$x$$$, print "-1" (without quotes).

The first line of the input contains integer numbers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le n$$$). The second line of the input contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the sequence itself.

Print any integer number $$$x$$$ from range $$$[1; 10^9]$$$ such that exactly $$$k$$$ elements of given sequence is less or equal to $$$x$$$. If there is no such $$$x$$$, print "-1" (without quotes).

NoteIn the first example $$$5$$$ is also a valid answer because the elements with indices $$$[1, 3, 4, 6]$$$ is less than or equal to $$$5$$$ and obviously less than or equal to $$$6$$$.In the second example you cannot choose any number that only $$$2$$$ elements of the given sequence will be less than or equal to this number because $$$3$$$ elements of the given sequence will be also less than or equal to this number.

def solution
  _, k = read_ints

  as = read_ints.sort

  if k == 0
    if as[0] == 1
      puts -1
    else
      puts 1
    end
    return
  end

  max_incl = as[k-1]
  min_excl = as[k]

  if max_incl == min_excl
    puts(-1)
  else
    puts max_incl
  end

end

def read_int
  gets.to_i
end

def read_ints
  gets.split.map(&:to_i)
end

def read_string
  gets.chomp
end

solution unless ENV['TEST__MODE']

n, k=gets.split.map &:to_i
a=gets.split.map(&:to_i).sort
if k==0
    puts (a[0]==1 ? -1 : 1)
else
    puts (k<n && a[k-1]==a[k] ? -1 : a[k-1])
end

# coding: utf-8

n, k = gets.split.map(&:to_i)
h = {}
ar = gets.split.map { |i|
    i = i.to_i
    h[i] ||= 0
    h[i] += 1
    i
}

#p ar
if k > 0
    acc = 0
    ans = -1
    h.keys.sort.each { |i|
        acc += h[i]
        if acc < k
            next
        elsif acc == k
            ans = i
            break
        else
            ans = -1
            break
        end
    }

    puts ans
else
    minimum = h.keys.min
    if minimum > 1
        puts minimum - 1
    else
        puts "-1"
    end
end

n, k=gets.split.map &:to_i
a=gets.split.map(&:to_i).sort
puts (k<n && a[k-1]==a[k] ? -1 : a[k-1])

# coding: utf-8

n, k = gets.split.map(&:to_i)
h = {}
ar = gets.split.map { |i|
    i = i.to_i
    h[i] ||= 0
    h[i] += 1
    i
}

ar.sort!
#p ar
if k > 0
    ans = ar[k-1]
    if h[ans] > 1
        puts "-1"
    else
        puts ans
    end
else
    if ar.first > 1
        puts ar.first - 1
    else
        puts "-1"
    end
end

# coding: utf-8

n, k = gets.split.map(&:to_i)
ar = [-1] + gets.split.map(&:to_i)
ar.sort!
#p ar
if (k < n && ar[k] == ar[k+1]) || (k > 0 && ar[k-1] == ar[k])
    puts ar[0]
else
    if k == 0
        if ar[1] > 1
            puts ar[1] - 1
        else
            puts ar[0]
        end
    else
        puts ar[k]
    end
end

# coding: utf-8

n, k = gets.split.map(&:to_i)
ar = [-1] + gets.split.map(&:to_i)
ar.sort!
#p ar
if k == 0 || n == 0 || (k < n && ar[k] == ar[k+1]) || (k > 0 && ar[k-1] == ar[k])
    puts ar[0]
else
    puts ar[k]
end


Anton's favourite geometric figures are regular polyhedrons. Note that there are five kinds of regular polyhedrons:   Tetrahedron. Tetrahedron has 4 triangular faces.  Cube. Cube has 6 square faces.  Octahedron. Octahedron has 8 triangular faces.  Dodecahedron. Dodecahedron has 12 pentagonal faces.  Icosahedron. Icosahedron has 20 triangular faces. All five kinds of polyhedrons are shown on the picture below:  Anton has a collection of n polyhedrons. One day he decided to know, how many faces his polyhedrons have in total. Help Anton and find this number!

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of polyhedrons in Anton's collection. Each of the following n lines of the input contains a string si — the name of the i-th polyhedron in Anton's collection. The string can look like this:   "Tetrahedron" (without quotes), if the i-th polyhedron in Anton's collection is a tetrahedron.  "Cube" (without quotes), if the i-th polyhedron in Anton's collection is a cube.  "Octahedron" (without quotes), if the i-th polyhedron in Anton's collection is an octahedron.  "Dodecahedron" (without quotes), if the i-th polyhedron in Anton's collection is a dodecahedron.  "Icosahedron" (without quotes), if the i-th polyhedron in Anton's collection is an icosahedron.

Output one number — the total number of faces in all the polyhedrons in Anton's collection.

4
Icosahedron
Cube
Tetrahedron
Dodecahedron

NoteIn the first sample Anton has one icosahedron, one cube, one tetrahedron and one dodecahedron. Icosahedron has 20 faces, cube has 6 faces, tetrahedron has 4 faces and dodecahedron has 12 faces. In total, they have 20 + 6 + 4 + 12 = 42 faces.

phs = {"Tetrahedron" => 4,"Cube" => 6,"Octahedron" => 8,"Dodecahedron" => 12,"Icosahedron" => 20}
n = gets.to_i
ans = 0

n.times do
  s = gets.chomp
  phs.each do |key,value|
    ans += value if key == s
  end
end

puts ans

phs = {'Tetrahedron' => 4,'Cube' => 6,'Octahedron' => 8,'Dodecahedron' => 12,'Icosahedron' => 20}
n = gets.to_i
ans = 0

n.times do
  s = gets.chomp
  ans += phs[s]
end

puts ans

n = gets.to_i
dict = {
	"Icosahedron" => 20,
	"Cube" => 6,
	"Tetrahedron" => 4,
	"Dodecahedron" => 12,
	"Octahedron" => 8
}

count = 0
while n != 0
	figure = gets.chomp.to_s
	count += dict[figure]
	n -= 1
end
puts count

# https://codeforces.com/problemset/problem/785/A

count = gets.to_i

faces_count = 0
count.times do
  name = gets.chomp

  faces_count +=
    if name == "Tetrahedron"
      4
    elsif name == "Cube"
      6
    elsif name == "Octahedron"
      8
    elsif name == "Dodecahedron"
      12
    elsif name == "Icosahedron"
      20
    end
end

puts faces_count

n = gets.chomp.to_i
ans = 0
n.times do
    s = gets.chomp
    case s
    when "Tetrahedron"
        ans += 4
    when "Cube"
        ans += 6
    when "Octahedron"
        ans += 8
    when "Dodecahedron"
        ans += 12
    when "Icosahedron"
        ans += 20
    end
end
puts ans

#!/usr/bin/env ruby -w

polyhedrons = {"Tetrahedron" => 4, "Cube" => 6, "Octahedron" => 8,
               "Dodecahedron" => 12, "Icosahedron" => 20}

n = gets.strip.to_i
s = 0

n.times do
  s += polyhedrons[gets.strip.chomp]
end

puts s

ans = 0
gets.to_i.times do
  s = gets.chomp
  if s == 'Icosahedron'
    ans += 20
  elsif s == 'Cube'
    ans += 6
  elsif s == 'Tetrahedron'
    ans += 4
  elsif s == 'Dodecahedron'
    ans += 12
  elsif s == 'Octahedron'
    ans += 8
  end
end
puts ans

poly = { "Tetrahedron" => 4, "Cube" => 6, "Octahedron" => 8, "Dodecahedron" => 12, "Icosahedron" => 20 }
n = gets.strip.to_i
ans = 0
n.times do
    s = gets.strip
    ans += poly[s]
end
puts ans

h={}
h["Tetrahedron"]=4
h["Cube"]=6
h["Octahedron"]=8
h["Dodecahedron"]=12
h["Icosahedron"]=20

ans=0
gets.to_i.times{
	ans+=h[gets.chomp]
}

puts ans

n = gets.chomp.to_i
o = {'Tetrahedron' => 0,'Cube'=>0,'Octahedron'=>0,'Dodecahedron'=>0,'Icosahedron'=>0}
n.times.each do
s = gets.chomp
  o[s] += 1
end

puts o['Tetrahedron'] * 4 + o['Cube'] * 6 + o['Octahedron'] * 8 + o['Dodecahedron'] * 12 + o['Icosahedron'] * 20

def total(poly)
	return 4 if poly == "Tetrahedron"
	return 6 if poly == "Cube"
	return 8 if poly == "Octahedron"
	return 12 if poly == "Dodecahedron"
	return 20 if poly == "Icosahedron"
end

faces = 0
n = gets.chomp.to_i

n.times do
	poly = gets.chomp
	faces = faces + total(poly)
end

puts(faces)

f = {
    Tetrahedron: 4,
    Cube: 6,
    Octahedron: 8,
    Dodecahedron: 12,
    Icosahedron: 20
}

n = gets.to_i
s = 0

(1..n).each { s += f[gets.chomp.to_sym] }

puts s

n=gets.chomp
#puts n
total_faces=0
faces = {"Tetrahedron" => 4, "Cube" => 6, "Octahedron" => 8, "Dodecahedron" => 12, "Icosahedron" => 20}
#puts faces["Tetrahedron"]
#puts faces["Cube"]
for i in 1..n.to_i
    #polyhedron=gets
    polyhedron=gets.chomp
    #print i
    #puts polyhedron
    #puts faces["Cube"]
    total_faces=total_faces + faces[polyhedron].to_i
end
puts total_faces

# coding: utf-8

# 4
# Icosahedron
# Cube
# Tetrahedron
# Dodecahedron

MAPPED = {
    "Tetrahedron" => 4,
    "Cube" => 6,
    "Octahedron" => 8,
    "Dodecahedron" => 12,
    "Icosahedron" => 20
}

n = gets.to_i
c = 0
n.times {
    input = gets
    input.strip!
    c += MAPPED[input]
}

puts c

n = gets.chomp.to_i
polyhedrons = { "Tetrahedron" => 4, "Cube" => 6, "Octahedron" => 8, "Dodecahedron" => 12, "Icosahedron" => 20}
sum = 0
for i in (0..n-1)
	x = gets.chomp
	if x == "Tetrahedron"
		sum += polyhedrons["Tetrahedron"]
	elsif x == "Cube"
		sum += polyhedrons["Cube"]
	elsif x == "Octahedron"
		sum += polyhedrons["Octahedron"]
	elsif x == "Dodecahedron"
		sum += polyhedrons["Dodecahedron"]
	elsif x == "Icosahedron"
		sum += polyhedrons["Icosahedron"]
	end
end
print sum

n = gets.to_i
count = {}
count['Tetrahedron']||=0
count['Cube']||=0
count['Octahedron']||=0
count['Dodecahedron']||=0
count['Icosahedron']||=0
for i in (0...n)
 cur = gets.chomp
 count[cur]+=1
end
puts (count['Tetrahedron']*4 + count['Cube']*6 + count['Octahedron']*8 + count['Dodecahedron']*12 + count['Icosahedron']*20)

n = gets.to_i
count = {'Tetrahedron'=>4,'Cube'=>6,'Octahedron'=>8,'Dodecahedron'=>12,'Icosahedron'=>20}
ans = 0
for i in (0...n) do ans+=count[gets.chomp] end
puts ans

n = gets.to_i
count = {}
count['Tetrahedron']||=4
count['Cube']||=6
count['Octahedron']||=8
count['Dodecahedron']||=12
count['Icosahedron']||=20
ans = 0
for i in (0...n)
 cur = gets.chomp
 ans+=count[cur]
end
puts ans

n = gets.to_i
count = {'Tetrahedron'=>4,'Cube'=>6,'Octahedron'=>8,'Dodecahedron'=>12,'Icosahedron'=>20}
ans = 0
for i in (0...n)
 cur = gets.chomp
 ans+=count[cur]
end
puts ans

$n = gets
$n = $n.to_i
arr = []
(0...$n).each do |i|
	arr << gets.chomp
end

polygon = {"Tetrahedron" => 4, "Icosahedron" => 20, "Cube" => 6, "Dodecahedron" => 12, "Octahedron" => 8}

$k = 0
arr.each do |i|
	$k += polygon[i]
end
print $k

n = gets.to_i
i = 0,sum =0
n.times do
  str = gets.strip
  if str == "Tetrahedron"
    sum =  sum +4
  elsif str == "Cube"
    sum = sum +  6
  elsif str == "Octahedron"
    sum= sum+  8
  elsif str == "Dodecahedron"
    sum = sum + 12
  else
    sum = sum + 20
  end

end
print(sum,"\n")

n = gets.chomp.to_i
sum = 0
hash = {}
hash["Tetrahedron"] = 4
hash["Cube"] = 6
hash["Octahedron"] = 8
hash["Dodecahedron"] = 12
hash["Icosahedron"] = 20

n.times do
	sum += hash[gets.chomp]
end
puts sum

h = {
  'Tetrahedron' => 4,
  'Cube' => 6,
  'Octahedron' => 8,
  'Dodecahedron' => 12,
  'Icosahedron' => 20
}
n = gets.to_i
p n.times.map{gets.chomp}.inject(0){|s, x| s + h[x]}

sides = {"Tetrahedron"=>4, "Cube"=>6, "Octahedron"=>8,
	"Dodecahedron"=>12, "Icosahedron"=>20}
n=gets.chomp.to_i
ans=0
for i in 0...n
	ans+=sides[gets.chomp]
end
puts ans

h = {
    'Tetrahedron' => 4,
    'Cube' => 6,
    'Octahedron' => 8,
    'Dodecahedron' => 12,
    'Icosahedron' => 20
}
n = gets.to_i
nh = 0
n.times do
    s = gets.strip
    nh += h[s]
end
puts nh

n = gets.to_i

sizes = {
  'Tetrahedron' => 4,
  'Cube' => 6,
  'Octahedron' => 8,
  'Dodecahedron' => 12,
  'Icosahedron' => 20
}

puts 1.upto(n).inject(0) { |result, i| result + sizes[gets.chomp] }

poly = {
    "Tetrahedron" => 4,
    "Cube" => 6,
    "Octahedron" => 8,
    "Dodecahedron" => 12,
    "Icosahedron" => 20
}
n = gets.strip.to_i
ans = 0;
for i in 1..n
    s = gets.strip
    ans += poly[s]
end
puts ans

n = gets.to_i

a = []
n.times do
  a << gets.strip
end

sum = 0
a.each do |x|
  if x == "Tetrahedron"
    sum += 4
  elsif x == "Cube"
    sum += 6
  elsif x == "Octahedron"
    sum += 8
  elsif x == "Icosahedron"
    sum += 20
  else
    sum += 12
  end
end

puts sum

D, x = {'Tetrahedron' => 4, 'Cube' => 6, 'Octahedron' => 8, 'Dodecahedron' => 12, 'Icosahedron' => 20}.freeze, 0
for i in 1..gets.to_i
  x += D[gets.strip]
end
p x

n = gets.chomp.to_i
ans = 0
n.times do
    s = gets.chomp
    case s
    when "Icosahedron"
        ans += 4
    when "Cube"
        ans += 6
    when "Octahedron"
        ans += 8
    when "Dodecahedron"
        ans += 12
    else
        ans += 20
    end
end
puts ans