def solve
  n, m = ints
  a = int1s
  b = int1s
  c = int1s
  colors = Array.new(n) { [] }
  has_color = [nil] * n

  b.each_with_index do |color, i|
    if a[i] != color
      colors[color] << i
    else
      has_color[color] = i
    end
  end

  painted = nil
  i = m-1
  plank = []
  c.reverse_each do |color|
    if not colors[color].empty?
      painted = colors[color].pop
    elsif painted
      painted = painted
    elsif has_color[color]
      painted = has_color[color]
    else
      no
      return
    end
    plank[i] = painted+1
    i -= 1
  end

  unless colors.all?(&:empty?)
    no
    return
  end

  yes
  puts plank * " "
end

def main
  t = int
  t.times do
    solve
  end
end

DEBUG = true
MOD = 10**9+7
YESNO = %w(NO YES)
INF = 10**9

def int; gets.to_s.to_i end
def ints; gets.to_s.split.map { |s| s.to_i } end
def int1s; gets.to_s.split.map { |s| s.to_i - 1 } end
def float; gets.to_s.to_f end
def floats; gets.to_s.split.map { |s| s.to_f } end
def array_of(&convert); gets.to_s.split.map(&convert) end
def string; gets.to_s.chomp end
def rep(n, &b); Array.new(n, &b) end
def yes; puts YESNO[1] end
def no; puts YESNO[0] end
def yesno t; puts YESNO[t] end
def zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end
def max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end
def min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end
def minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end
def gcd(*xs); xs.inject(0, :gcd) end
def matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end
def debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end
def debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join("") } if DEBUG; grid end
def if_debug; yield if DEBUG end

module Boolean
  def coerce(other); [other, to_i] end
  def +@; to_i end
  def to_int; to_i end
  def *(other); to_i * other end
end

class TrueClass
  include Boolean
  def to_i; 1 end
end

class FalseClass
  include Boolean
  def to_i; 0 end
end

class Integer
  def div_ceil(y); (self + y - 1) / y end
  def mod_inv(mod = MOD); pow(mod-2, mod) end
  def mod_div(y, mod = MOD); self * mod_inv(y, mod) % mod end
  def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end
  def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster
  def bitbrute(&block); (1<<self).times(&block) end
  def nCr(r); x = 1; (1..r).each { |i| x *= self + 1 - i; x /= i }; x; end
  def each_divisor; return Enumerator.new { |y| each_divisor { |k| y << k } } unless block_given?; k = 1; while k * k < self; if self % k == 0; yield k; yield self / k end; k += 1; end; yield k if k * k == self end
  def divisors; each_divisor.to_a end
end

class Range
  def end_open; exclude_end? ? self.end : self.end + 1 end
  def end_close; exclude_end? ? self.end - 1 : self.end end
  def upper_bound; ac, wa = self.begin, self.end_open; while wa - ac > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end
  def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end
  def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end
  def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end
end

class Array
  def power(&block); (0 ... 1 << size).each(&block) end
  def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end
  def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end
  def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end
  def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end
  def cumdiff(range); self[range.end_open] - self[range.begin]; end
end

module Enumerable
  def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end
  def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end
end

main

t = gets.chomp.to_i
t.times do
  n, m = gets.chomp.split(' ').map(&:to_i)
  arr = gets.chomp.split(' ').map(&:to_i)
  brr = gets.chomp.split(' ').map(&:to_i)
  crr = gets.chomp.split(' ').map(&:to_i)

  colors = Array.new(n + 1) {Array.new}
  missed = Array.new(n + 1) {Array.new}

  cur = arr.dup
  res = []

  n.times do |ind|
    if arr[ind] != brr[ind]
      missed[brr[ind]] << ind
    else
      colors[arr[ind]] << ind
    end
  end

  buff = []
  crr.each_with_index do |el, ind|
    # puts "Buff = #{buff}"
    # puts "res = #{res}"
    if missed[el].any?
      # puts "Iter1 = {#{[el, ind].to_s}}"
      t = missed[el].shift
      res << t + 1
      cur[t] = el
      if buff.any?
        buff.each do |i|
          res[i] = t + 1
        end
        buff = []
      end
      colors[el] << t
      next
    end
    if colors[el].any?
      # puts "Iter2 = {#{[el, ind].to_s}}"
      t = colors[el][0]
      res << t + 1
      cur[t] = el
      if buff.any?
        buff.each do |i|
          res[i] = t + 1
        end
        buff = []
      end
      next
    end
    # puts "Iter3 = {#{[el, ind].to_s}}"
    buff << ind
    res << -1
  end
  # puts "RES = #{res}"
  if buff.empty? && missed.all? {|el| el.empty?}
    puts "YES"
    puts res.join(' ')
  else
    puts "NO"
  end
end

t = gets.chomp.to_i
t.times do
  n, m = gets.chomp.split(' ').map(&:to_i)
  arr = gets.chomp.split(' ').map(&:to_i)
  brr = gets.chomp.split(' ').map(&:to_i)
  crr = gets.chomp.split(' ').map(&:to_i)

  colors = Array.new(n + 1) {Array.new}
  missed = Array.new(n + 1) {Array.new}

  cur = arr.dup
  res = []

  n.times do |ind|
    if arr[ind] != brr[ind]
      missed[brr[ind]] << ind
    else
      colors[arr[ind]] << ind
    end
  end

  buff = []
  crr.each_with_index do |el, ind|
    # puts "Buff = #{buff}"
    # puts "res = #{res}"
    if missed[el].any?
      # puts "Iter1 = {#{[el, ind].to_s}}"
      t = missed[el].shift
      res << t + 1
      cur[t] = el
      if buff.any?
        buff.each do |i|
          res[i] = t + 1
        end
        buff = []
      end
      colors[el] << t
      next
    end
    if colors[el].any?
      # puts "Iter2 = {#{[el, ind].to_s}}"
      t = colors[el][0]
      res << t + 1
      cur[t] = el
      if buff.any?
        buff.each do |i|
          res[i] = t + 1
        end
        buff = []
      end
      next
    end
    # puts "Iter3 = {#{[el, ind].to_s}}"
    buff << ind
    res << -1
  end
  # puts "RES = #{res}"
  if buff.empty?
    puts "YES"
    puts res.join(' ')
  else
    puts "NO"
  end
end

t = gets.chomp.to_i
t.times do
  n, m = gets.chomp.split(' ').map(&:to_i)
  arr = gets.chomp.split(' ').map(&:to_i)
  brr = gets.chomp.split(' ').map(&:to_i)
  crr = gets.chomp.split(' ').map(&:to_i)

  colors = Array.new(n + 1) {Array.new}
  missed = Array.new(n + 1) {Array.new}

  cur = arr.dup
  res = []

  n.times do |el|
    if arr[el] != brr[el]
      missed[brr[el]] << el
    else
      colors[arr[el]] << el
    end
  end

  buff = []
  crr.each_with_index do |el, ind|
    # puts "Buff = #{buff}"
    # puts "res = #{res}"
    if missed[el].any?
      # puts "Iter1 = {#{[el, ind].to_s}}"
      t = missed[el].shift
      res << t + 1
      cur[t] = el
      if buff.any?
        buff.each do |i|
          res[i] = t + 1
        end
        buff = []
      end
      next
    end
    if colors[el].any?
      # puts "Iter2 = {#{[el, ind].to_s}}"
      t = colors[el][0]
      res << t + 1
      cur[t] = el
      if buff.any?
        buff.each do |i|
          res[i] = t + 1
        end
        buff = []
      end
      next
    end
    # puts "Iter3 = {#{[el, ind].to_s}}"
    buff << ind
    res << -1
  end
  # puts "RES = #{res}"
  if buff.empty?
    puts "YES"
    puts res.join(' ')
  else
    puts "NO"
  end
end

t = gets.chomp.to_i
t.times do
  n, m = gets.chomp.split(' ').map(&:to_i)
  arr = gets.chomp.split(' ').map(&:to_i)
  brr = gets.chomp.split(' ').map(&:to_i)
  crr = gets.chomp.split(' ').map(&:to_i)

  colors = Array.new(n + 1) {Array.new}
  missed = Array.new(n + 1) {Array.new}

  cur = arr.dup
  res = []

  n.times do |el|
    if arr[el] != brr[el]
      missed[brr[el]] << el
    else
      colors[arr[el]] << el
    end
  end

  buff = []
  crr.each_with_index do |el, ind|
    if missed[el].any?
      t = missed[el].shift
      res << t + 1
      cur[t] = el
      if buff.any?
        i = buff.shift
        res[i] = t + 1
      end
      next
    end
    if colors[el].any?
      t = colors[el][0]
      res << t + 1
      cur[t] = el
      if buff.any?
        i = buff.shift
        res[i] = t + 1
      end
      next
    end
    buff << ind
    res << -1
  end
  if buff.empty?
    puts "YES"
    puts res.join(' ')
  else
    puts "NO"
  end
end


#require "prime"
#input of int(split by space)
def get_i()
    return gets.chomp.split(" ").map(&:to_i)
end
#input of float(split by space)
def get_f()
    return gets.chomp.split(" ").map(&:to_f)
end
#input of string(split by space)
def get()
    return gets.chomp.split(" ")
end
#input of string(split per one character)
def get_nsp()
    return gets.chomp.split("")
end
#yes or no decision
def yn_judge(bool,y="Yes",n="No")
    return bool ? y : n
end
#output grid(two dimensional array)
def puts_grid(grid,join_char="")
    grid.each do|line|
        puts line.join(join_char)
    end
end

Dirxy4=[[1,0],[-1,0],[0,1],[0,-1]]
Dirxy8=[[1,0],[1,-1],[1,1],[0,-1],[0,1],[-1,0],[-1,-1],[-1,1]]

INF=Float::INFINITY

#MOD=998_244_353
MOD=10**9+7
T=gets.to_i
T.times do
    n=gets.to_i
    a=get_i
    b=get_i
    f=true
    diff=-1
    n.times do |i|
        if b[i].zero?
            next
        end
        if a[i]-b[i]>=0
            diff=a[i]-b[i]
            break
        else
            f=false
            break
        end
    end
    if diff==-1
        puts yn_judge(f,"YES","NO")
    else
        n.times do |i|
            if b[i].zero?
                unless diff>=a[i]
                    f=false
                    break
                end
            else
                unless diff==a[i]-b[i]
                    f=false
                    break
                end
            end
        end
        puts yn_judge(f,"YES","NO")
    end
end

t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  a = gets.chomp.split.map(&:to_i)
  b = gets.chomp.split.map(&:to_i)
  r = [0, 2000000000]
  n.times do |i|
    if b[i] > a[i]
      r = [1,0]
      break
    elsif b[i] == 0 # a[i]..
      r = [
        [a[i], r[0]].max,
        [2000000000, r[1]].min
      ]
    else # a[i] - b[i], a[i]-b[i]
      r = [
        [a[i]-b[i], r[0]].max,
        [a[i]-b[i], r[1]].min
      ]
    end
  end
  if r[0] <= r[1]
    puts 'YES'
  else
    puts 'NO'
  end
end

t = gets.to_i

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

  s = -1
  n.times do |i|
    s = [s, a[i]-b[i]].max
  end

  ok = true
  n.times do |i|
    ai, bi = a[i], b[i]
    break ok = false if ai < bi

    k = ai - bi
    if bi == 0
      break ok = false if k > s
    else
      break ok = false if k != s
    end
  end

  puts ok ? "YES" : "NO"
end

#require "prime"
#input of int(split by space)
def get_i()
    return gets.chomp.split(" ").map(&:to_i)
end
#input of float(split by space)
def get_f()
    return gets.chomp.split(" ").map(&:to_f)
end
#input of string(split by space)
def get()
    return gets.chomp.split(" ")
end
#input of string(split per one character)
def get_nsp()
    return gets.chomp.split("")
end
#yes or no decision
def yn_judge(bool,y="Yes",n="No")
    return bool ? y : n
end
#output grid(two dimensional array)
def puts_grid(grid,join_char="")
    grid.each do|line|
        puts line.join(join_char)
    end
end

Dirxy4=[[1,0],[-1,0],[0,1],[0,-1]]
Dirxy8=[[1,0],[1,-1],[1,1],[0,-1],[0,1],[-1,0],[-1,-1],[-1,1]]

INF=Float::INFINITY

#MOD=998_244_353
MOD=10**9+7
T=gets.to_i
T.times do
    n=gets.to_i
    a=get_i
    b=get_i
    if n==1
        if a[0]>=b[0] or b[0].zero?
            puts "YES"
        else
            puts "NO"
        end
        next
    end
    f=true
    (n-1).times do |i|
        if b[i].zero?
            next
        end
        if b[i+1].zero?
            unless a[i]-b[i]>=a[i+1]-b[i+1]
                f=false
                break
            end
        else
            unless a[i]-b[i]==a[i+1]-b[i+1]
                f=false
                break
            end
        end
    end
    puts yn_judge(f,"YES","NO")
end

#require "prime"
#input of int(split by space)
def get_i()
    return gets.chomp.split(" ").map(&:to_i)
end
#input of float(split by space)
def get_f()
    return gets.chomp.split(" ").map(&:to_f)
end
#input of string(split by space)
def get()
    return gets.chomp.split(" ")
end
#input of string(split per one character)
def get_nsp()
    return gets.chomp.split("")
end
#yes or no decision
def yn_judge(bool,y="Yes",n="No")
    return bool ? y : n
end
#output grid(two dimensional array)
def puts_grid(grid,join_char="")
    grid.each do|line|
        puts line.join(join_char)
    end
end

Dirxy4=[[1,0],[-1,0],[0,1],[0,-1]]
Dirxy8=[[1,0],[1,-1],[1,1],[0,-1],[0,1],[-1,0],[-1,-1],[-1,1]]

INF=Float::INFINITY

#MOD=998_244_353
MOD=10**9+7
T=gets.to_i
T.times do
    n=gets.to_i
    a=get_i
    b=get_i
    f=true
    (n-1).times do |i|
        if b[i].zero?
            next
        end
        if b[i+1].zero?
            unless a[i]-b[i]>=a[i+1]-b[i+1]
                f=false
                break
            end
        else
            unless a[i]-b[i]==a[i+1]-b[i+1]
                f=false
                break
            end
        end
    end
    puts yn_judge(f,"YES","NO")
end

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

  pair = []
  n.times do |i|
    pair << [a[i], b[i]]
  end

  pair.sort_by!{|e| [-e[0], -e[1]]}

  ok = true
  s = a[0]-b[0]
  n.times do |i|
    ai, bi = pair[i]
    if ai < bi
      ok = false
      break
    end

    if bi == 0
      k = ai - bi
      if k > s
        ok = false
        break
      end
    else
      k = ai - bi
      if k != s
        ok = false
        break
      end
    end
  end

  puts ok ? "YES" : "NO"
end


n = gets.to_i
a = gets.split.map(&:to_i)
a.sort!
k = a.pop
flag = false # Conan wins
while !flag && k
  cnt = 1
  while k == (l = a.pop)
    cnt += 1
  end
  flag = cnt % 2 != 0
  k = l
end
puts flag ? "Conan" : "Agasa"

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

oddcnt = a.group_by{|x| x}.map{|k,v| v.size}.select{|t|t%2==1}.size

puts (oddcnt>0)? 'Conan' : 'Agasa'

gets
c=0
h=Hash.new 0
gets.split.each{|e|h[e]=d=h[e]+1;c+=d%2*2-1}
puts c>0 ? :Conan : :Agasa

n = gets.to_i
a = gets.split.map(&:to_i)
a.sort!
k = a[-1]
flag = false # Conan wins
while !flag && k
  cnt = 0
  while k == (l = a.pop)
    cnt += 1
  end
  flag = cnt % 2 != 0
  k = l
end
puts flag ? "Conan" : "Agasa"

n = gets.to_i
a = gets.split.map(&:to_i)
a.sort!
k = a.pop
while k && (a.count(k) % 2 == 1)
  a.delete(k)
  k = a.pop
end
puts a.length == 0 ? "Agasa" : "Conan"


#!/usr/bin/ruby1.9

s = STDIN.readline.strip
m = STDIN.readline.to_i
weights = []
10.times do |pos|
	if s[pos] == '1'
		weights.push(pos+1)
	end
end

def solve(solution, weights, left, right, ix, max, previous)
	return true if ix == max
	weights.each do |weight|
		next if weight == previous
		next if left+weight <= right
		solution.push(weight)
		if solve(solution, weights, right, left+weight, ix+1, max, weight)
			return true
		end
		solution.pop
	end
	return false
end

solution = []
if solve(solution, weights, 0, 0, 0, m, -1)
	puts 'YES'
	puts solution.join(' ')
else
	puts 'NO'
end

@s = '0'+gets.strip
@m = gets.to_i
@calced = []
@f = []
for i in 0..10 do
  @calced[i]=[]
  @f[i]=[]
  for j in 0..@m do
    @calced[i][j]=[]
    @f[i][j] = []
  end
end

def dp(delta,step,last)
  if !@calced[delta][step][last]
    @calced[delta][step][last]=true
    @f[delta][step][last]=false
    if step==@m
      @f[delta][step][last]=true
    else
      for i in delta+1..10 do
        if i!=last && @s[i]=='1' && dp(i-delta,step+1,i)
          @f[delta][step][last]=true
          break
        end
      end
    end
  end
  return @f[delta][step][last]
end

def show(delta,step,last)
  if step==@m
    puts last
  else
    if step==0
      puts 'YES'
    else
      print last,' '
    end
    for i in delta+1..10 do
      if i!=last && @s[i]=='1' && dp(i-delta,step+1,i)
        show(i-delta,step+1,i)
        break
      end
    end
  end
end

if !dp(0,0,0)
  puts 'NO'
else
  show(0,0,0)
end

#!/usr/bin/ruby1.9

s = STDIN.readline.strip
m = STDIN.readline.to_i
weights = []
10.times do |pos|
	if s[pos] == '1'
		weights.push(pos+1)
	end
end

def solve(solution, weights, left, right, ix, max, previous)
	return true if ix == max
	weights.each do |weight|
		next if weight == previous
		next if left+weight <= right
		solution.push(weight)
		return true if solve(solution, weights, right, left, ix+1, max, weight)
		solution.pop
	end
	return false
end

solution = []
if solve(solution, weights, 0, 0, 0, m, -1)
	puts 'YES'
	puts solution.join(' ')
else
	puts 'NO'
end


def parse(type, values)
  type =~ /^(\&*)(.*?)(\**)$/
  amp = $1
  base = $2
  star = $3
  if base == 'errtype'
    return 'errtype'
  elsif base != 'void'
    if !values[base]
      return 'errtype'
    end
    type = "#{amp}#{values[base]}#{star}"
    type =~ /^(\&*)(.*?)(\**)$/
    amp = $1
    base = $2
    star = $3

    if base == 'errtype'
      return 'errtype'
    end
  end

  s = star.size - amp.size
  if s < 0
    return 'errtype'
  else
    return base + "*" * s
  end
end

values = {}

gets.to_i.times{
  l = gets.chomp
  if l =~ /typedef (\S+) (\S+)/
    type = $1
    name = $2
    values[name] = parse(type, values)
  elsif l =~ /typeof (\S+)/
    type = $1
    puts parse(type, values)
  else
    raise
  end
}

n = STDIN.readline.to_i
regex = /^(&*)(.+?)(\**)$/
dict = Hash.new
dict['void'] = 0
n.times do
  s = STDIN.readline.split ' '
  array = regex.match(s[1])
  head = array[1].length
  body = array[2]
  tail = array[3].length
  if s[0] == "typedef" then
    base = if dict.include? body then
             dict[body]
           else
             nil
           end
    dict[s[2]] = if base == nil or base + tail - head < 0 then
                   nil
                 else
                   base + tail - head
                 end
  else
    if (!dict.include? body) || (!dict[body]) then
      puts 'errtype'
    else
      len = dict[body] + tail - head
      if len >= 0 then
        puts 'void' + ('*' * len)
      else
        puts 'errtype'
      end
    end
  end
end

h = {void: "void", errtype: "errtype"}
gets.to_i.times do
  s = gets.split
  if s[0] == "typeof"
    s[1].match(/(&*)([^&*]+)(\**)/)
    a, b, c = $1.size, $2.to_sym, $3.size
    if h[b].nil? || h[b] == "errtype" || a > c + h[b].scan(/\*/).size
      puts :errtype
    else
      if a >= c
        puts h[b][0..-(a - c + 1)]
      else
        puts h[b] + "*" * (c - a)
      end
    end
  else
    s[2] = s[2].to_sym
    s[1].match(/(&*)([^&*]+)(\**)/)
    a, b, c = $1.size, $2.to_sym, $3.size
    if h[b].nil? || h[b] == "errtype" || a > c + h[b].scan(/\*/).size
      h[s[2]] = "errtype"
    else
      if a >= c
        h[s[2]] = h[b][0..-(a - c + 1)]
      else
        h[s[2]] = h[b] + "*" * (c - a)
      end
    end
  end
end

h = {void: "void", errtype: "errtype"}
gets.to_i.times do
  s = gets.split
  if s[0] == "typeof"
    if s[1].match(/(&*)(#{h.keys.join(?|)})(\**)/)
      a, b, c = $1.size, $2.to_sym, $3.size
      if h[b].nil? || h[b] == "errtype" || a > c + h[b].scan(/\*/).size
        puts :errtype
      else
        if a >= c
          puts h[b][0..-(a - c + 1)]
        else
          puts h[b] + "*" * (c - a)
        end
      end
    else
      puts :errtype
    end
  else
    s[2] = s[2].to_sym
    if s[1].match(/(&*)(#{h.keys.join(?|)})(\**)/)
      a, b, c = $1.size, $2.to_sym, $3.size
      if h[b].nil? || h[b] == "errtype" || a > c + h[b].scan(/\*/).size
        h[s[2]] = "errtype"
      else
        if a >= c
          h[s[2]] = h[b][0..-(a - c + 1)]
        else
          h[s[2]] = h[b] + "*" * (c - a)
        end
      end
    else
      h[s[2]] = "errtype"
    end
  end
end

n = STDIN.readline.to_i
regex = /^(&*)(.+?)(\**)$/
dict = Hash.new
n.times do
  s = STDIN.readline.split ' '
  if s[0] == "typedef" then
    array = regex.match(s[1])
    base = if array[2] == 'void' then 0
           elsif dict.include?(array[2]) then dict[array[2]]
           else nil
           end
    if base == nil then
      dict[s[2]] = nil
    elsif base + array[3].length - array[1].length < 0 then
      dict[s[2]] = nil
    else
      dict[s[2]] = base + array[3].length - array[1].length
    end
  else
    array = regex.match(s[1])
    if !dict.include?(array[2]) || !dict[array[2]] then
      puts 'errtype'
    else
#      p array
      len = dict[array[2]] + array[3].length - array[1].length
      if len >= 0 then
        puts 'void' + ('*' * len)
      else
        puts 'errtype'
      end
    end
  end
end


inp = gets.split(" ")
n = inp[0].to_i
m = inp[1].to_i
l = Array.new(m){[]}
for i in 0..(n-1)
  inp = gets
  for j in 0..(m-1)
    l[j] << inp[j]
  end
end

count = 1
l.each do |arr|
  arr.sort!
  #p arr
  unq = 1
  curr = 0
  while curr < (n - 1)
    if arr[curr] != arr[curr+1]
      unq += 1
	end
	curr += 1
  end
  #puts unq
  count *= unq
end

puts count % 1000000007

require 'set'

n,m = gets.chomp.split(/ /).map!{|x| x.to_i}
s = []
for i in 0..n-1
	s << gets.chomp
end
ret = 1
for i in 0..m-1
	ws = Set.new
	for j in 0..n-1
		ws.add(s[j][i])
	end
	ret *= ws.length
end
ret %= 1000000007
puts ret

n, m = gets.split.map(&:to_i)
p Array.new(m).zip(*(n.times.map{gets.chars.to_a})).map{|_| _.uniq.size - 1}.inject(&:*) % 1000000007

n, m = gets.split.map(&:to_i)
p n.times.map{gets.chars.to_a}.transpose.map{|_| _.uniq.size}.inject(&:*) % 1000000007

n,m=gets.split.map(&:to_i)
ans=1
input=[]
n.times{input+=[gets.split[0]]}
m.times{|i|
	a=Hash.new
	n.times{|j|
		a[input[j][i]]=true
	}
	ans*=a.size
	ans%=1000000007
}
p ans

#!/usr/bin/env ruby

MOD = 1000000007
n, m = gets.split.map(&:to_i)
transpose = Array.new(m) { {} }
n.times do
  gets.chomp.each_char.to_a.each_with_index do |ch, idx|
    transpose[idx][ch] = 1
  end
end

cnt = 1
transpose.each do |set|
  cnt *= set.size
  cnt %= MOD
end

p cnt

a=gets.chomp.split(' ')
a.length.times {|x| a[x]=a[x].to_i}
b=[]
c=[]
a[0].times do |x|
  b<<gets.chomp
  a[1].times do |y|
   if c[y].nil?
   c[y]=b[x][y]
  elsif !c[y].nil? && !c[y].include?(b[x][y])
  c[y]+=b[x][y]
  end
  end
end
res=1
c.length.times do |x|
 res*=c[x].length
end
puts res%(10**9+7)

n,m = gets.split.map(&:to_i)
a = []
n.times{ a << gets.strip.split(//) }
p a.transpose.map{|s| s.uniq.size}.inject(:*) % 1000000007

n,m = gets.split(" ").map(&:to_i)
arr = []
n.times do
	arr << gets.chomp
end
ans = []
(0...m).each do |j|
	count = 0
	h = {}
	(0...n).each do |i|
		letter = arr[i][j]
		unless h[letter]
			h[letter] =  true
			count += 1
		end
	end
	ans << count
end
s = 1
ans.each do |i|
	s = (s * i) % 1000000007
end
puts s


n= Array.new
n = gets.chomp.split(" ").each.map(&:to_i)

a= Array.new
b= Array.new
c=0
sum=0

while n[0]>0
    a = gets.chomp.split(" ").each.map(&:to_i)
    sum+=a[0]
    b.push((a[0]-a[1]))
    n[0]-=1
end

b= b.sort

while n[1]<sum && !b.empty?
    sum-=b.pop
    c+=1
end

if(sum>n[1])
    print(-1)
else
    print(c)
end

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

sum = 0
ary = []
n.times do
  a, b = gets.strip.split.map(&:to_i)
  ary << [a,b]
  sum += a
end

ary = ary.sort_by{|x| x[1]-x[0]}
i = 0
ans = 0
while sum > m && i<n do
  sum -= (ary[i][0] - ary[i][1])
  i += 1
  ans += 1
end

if sum > m
  puts "-1"
else
  puts ans
end

def solution

  n, m = read_ints

  sum = 0
  sum_comp = 0

  diffs = []

  n.times do
    a, b = read_ints
    sum += a
    sum_comp += b
    diffs << (a - b)
  end

  diffs = diffs.sort.reverse

  if sum_comp > m
    puts -1
    return
  end

  i = 0

  while sum > m
    sum -= diffs[i]
    i += 1
  end

  puts i

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, M = gets.split.map(&:to_i)
as, bs = [], []
N.times do |i|
    as[i], bs[i] = gets.split.map(&:to_i)
end

sum = as.inject(:+)
gains = [0] + N.times.map{|i| as[i] - bs[i]}.sort.reverse
(N+1).times do |i|
    sum -= gains[i]
    if sum <= M
        puts i; exit
    end
end
puts -1

Song = Struct.new(:a, :b) do
  def diff
    self.a - self.b
  end
end

def solution

  n, m = read_ints

  songs = []

  sum = 0

  n.times do
    a, b = read_ints
    sum += a
    songs << Song.new(a, b)
  end

  songs = songs.sort_by(&:diff).reverse

  i = 0

  while sum > m && i < songs.size
    sum -= songs[i].diff
    i += 1
  end

  if sum > m
    puts -1
  else
    puts i
  end

end

def read_int
  gets.to_i
end

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

def read_string
  gets.chomp
end

Song = Struct.new(:a, :b) do
  def diff
    self.a - self.b
  end
end

def solution

  n, m = read_ints

  songs = []

  sum = 0

  n.times do
    a, b = read_ints
    sum += a
    songs << Song.new(a, b)
  end

  songs = songs.sort_by(&:diff)

  i = 0

  while sum > m && i < songs.size
    sum -= songs[i].diff
    i += 1
  end

  if sum > m
    puts -1
  else
    puts i - 1
  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']


def mid(x)
	(x-1)/2
end
n, x = gets.split(/\s+/).map(&:to_i)
a = gets.split(/\s+/).map(&:to_i).sort
res = []
1000.times do |i|
	res << i if a[mid(a.size)] == x
	a << x
	a.sort!
end
p res.min

n, x = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
ans = 0
unless a.include?(x)
  a.push(x)
  ans += 1
end
a.sort!
m = (a.size()+1)/2-1

while a[m]>x
  a.unshift(-1000)
  m = (a.size()+1)/2-1
  ans+=1
end

while a[m]<x
  a.push(10000000)
  m = (a.size()+1)/2-1
  ans+=1
end

p ans

#!/usr/bin/ruby

n, x = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i).sort

cnt = 0
if arr.index(x).nil?
  arr << x
  cnt += 1
  arr.sort!
end

median = (arr.size+1) / 2 - 1
ni = arr.index(x)
ri = arr.rindex(x)

if arr[median] == x
  p cnt
  exit
end

# right adjustment
rcnt = n*100
if ni > median
  rcnt = 0
  target = ni

  if arr.size.even?
    rcnt += 1
    target -= 1
  end
  rcnt += (target-median) * 2
end

# left adjustment
lcnt = n*100
if ri < median
  lcnt = 0
  target = ri

  if arr.size.odd?
    lcnt += 1
    target += 1
  end
  lcnt += (median-target) * 2
end

p [rcnt,lcnt].min + cnt

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

$a = gets.split().map(&:to_i)
$a.sort!
def gg()
  return 0 if $a[$a.length/2] == $m
  ls = 0
  gt = 0
  eq = 0
 $a.each do |i|
    if i < $m
      ls = ls + 1
    elsif i == $m
      eq = eq + 1
    else
      gt = gt + 1
    end
  end

  return 1 if ls == gt
  if ls < gt
    ls = ls + eq
    return gt - ls
  else
    gt = gt + eq
    return ls - gt + 1
  end

end

ans = gg
puts ans

#!/usr/bin/env ruby

class Main
  attr_reader :size, :number, :array

  def initialize(size, number, array)
    @size = size
    @number = number
    @array = array.sort
  end

  def run
    median_position = calculate_median(size)
    median_number = array[median_position]

    if number == median_number
      0
    elsif array.include?(number)
      index = array.rindex(number)
      if index > median_position
        index = array.index(number)
        revert_median_calculation(index) - size
      elsif index < median_position
        (median_position - index) * 2 - size % 2
      end
    else
      median_number = array[median_position]
      new_array = (array + [number]).sort
      index = new_array.index(number)
      new_median_position = calculate_median(size + 1)
      if index > new_median_position
        revert_median_calculation(index) - size
      elsif index < new_median_position
        (new_median_position - index) * 2 - (size + 1) % 2 + 1
      else
        1
      end
    end
  end

  private

  def calculate_median(array_size)
    (array_size + 1)/2 - 1
  end

  def revert_median_calculation(median)
    (median + 1) * 2 - 1
  end
end

size, number = gets.split(" ")
array = gets.split(" ")
puts Main.new(size.to_i, number.to_i, array.map(&:to_i)).run

# ProblemC

n, x = gets.split().map! {|s| s.to_i}

ary = gets.split().map! {|s| s.to_i}

res = 0
unless ary.index(x)
  ary << x
  res += 1
end

ary = ary.sort

while true
  n = (ary.size)
  m = ((n + 1) / 2) - 1
  break if ary[m] == x
  if ary[m] > x
    ary.unshift(1)
  else
    ary << (10 ** 5)
  end
  res += 1
end

puts res

a = gets.chomp.split(" ").map{|i| i.to_i}
b = gets.chomp.split(" ").map{|i| i.to_i}

b.sort!
n = a[0]
k = a[1]

if k > b[n-1]
    puts n+1
else
    if k < b[0]
        puts n
    else
        x = (n+1)/2 - 1
        if k == b[x]
            puts 0
        else
            if k < b[x]
                i = 0
                while b[i+1] <= k do
                    i += 1
                end
                find = n -2*i -2
                puts find
            else
                i = n-1
                while b[i-1] >= k do
                    i -= 1
                end
                find = 2*i + 1 - n
                puts find
            end
        end
    end
end

n,k=gets.split.map(&:to_i)
a=gets.split.map(&:to_i).sort
ans=0
unless a.include?(k)
	a<<k
	a.sort!
	ans+=1
	n+=1
end

if a[(n+1)/2-1]<k
	until a[(n+1)/2-1]==k
		ans+=1
		n+=1
		a<<10**5
	end
elsif a[(n+1)/2-1]>k
	until a[(n+1)/2-1]==k
		ans+=1
		n+=1
		a.unshift 1
	end
end
p ans

n,xx = gets.chomp.split(/ /).map!{|x| x.to_i}
a = gets.chomp.split(/ /).map!{|x| x.to_i}
ret = 0
if not a.include?(xx)
  ret = 1
  a << xx
end
a.sort!
m = (a.length() + 1)/2

minxx = a.index(xx)
maxxx = a.rindex(xx)
if minxx > (m-1)
  ret += (minxx - (a.length() - (minxx+1)))
elsif maxxx < (m-1)
  ret += ((a.length() - (maxxx+1)) - maxxx - 1)
end
puts ret

#!/usr/bin/ruby

n, x = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i).sort

cnt = 0
if arr.index(x).nil?
  arr << x
  cnt += 1
  arr.sort!
end

median = (arr.size+1) / 2 - 1
ni = arr.index(x)
ri = arr.rindex(x)

if arr[median] == x
  p 0
  exit
end

# right adjustment
rcnt = n*100
if ri > median
  rcnt = 0
  target = ri
  target = ni if ni > median

  if arr.size.even?
    rcnt += 1
    target -= 1
  end
  rcnt += (target-median) * 2
end

# left adjustment
lcnt = n*100
if ni < median
  lcnt = 0
  target = ni
  target = ri if ri < median

  if arr.size.odd?
    lcnt += 1
    target += 1
  end
  lcnt += (median-target) * 2
end

p [rcnt,lcnt].min + cnt

#!/usr/bin/ruby

n, x = gets.split.map(&:to_i)
arr = gets.split.map(&:to_i).sort

cnt = 0
if arr.index(x).nil?
  arr << x
  cnt += 1
  arr.sort!
end

median = (arr.size+1) / 2 - 1
ni = arr.index(x)
ri = arr.rindex(x)

# right adjustment
rcnt = n*100
if ri > median
  rcnt = 0
  target = ri
  target = ni if ni > median

  if arr.size.even?
    rcnt += 1
    target -= 1
  end
  rcnt += (target-median) * 2
end

# left adjustment
lcnt = n*100
if ni < median
  lcnt = 0
  target = ni
  target = ri if ri < median

  if arr.size.odd?
    lcnt += 1
    target += 1
  end
  lcnt += (median-target) * 2
end

p [rcnt,lcnt].min + cnt

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

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

def gg()
  return 0 if $a[$a.length/2] == $m
  ls = 0
  gt = 0
  eq = 0
 $a.each do |i|
    if i < $m
      ls = ls + 1
    elsif i == $m
      eq = eq + 1
    else
      gt = gt + 1
    end
  end

  return 1 if ls == gt
  if ls < gt
    ls = ls + eq
    return gt - ls
  else
    gt = gt + eq
    return ls - gt + 1
  end

end

ans = gg
puts ans

#!/usr/bin/env ruby

class Main
  attr_reader :size, :number, :array

  def initialize(size, number, array)
    @size = size
    @number = number
    @array = array.sort.reverse
  end

  def run
    median_position = (size + 1)/2 - 1
    if array.include?(number)
      index = array.index(number)
      (median_position - index).abs
    else
      median_number = array[median_position]
      new_array = (array + [number]).sort
      index = new_array.index(number)
      (median_position - index).abs * 2
    end
  end
end

size, number = gets.split(" ")
array = gets.split(" ")
puts Main.new(size.to_i, number.to_i, array.map(&:to_i)).run

#!/usr/bin/env ruby

class Main
  attr_reader :size, :number, :array

  def initialize(size, number, array)
    @size = size
    @number = number
    @array = array.sort
  end

  def run
    median_position = (size + 1)/2 - 1
    median_number = array[median_position]

    if number == median_number
      0
    elsif array.include?(number)
      index = array.index(number)
      (median_position - index).abs
    else
      median_number = array[median_position]
      new_array = (array + [number]).sort
      index = new_array.index(number)
      new_median_position = (size + 2)/2 - 1
      if index > new_median_position
        index * 2 - size + 1
      elsif index < new_median_position
        size - index + 1
      else
        1
      end
    end
  end
end

size, number = gets.split(" ")
array = gets.split(" ")
puts Main.new(size.to_i, number.to_i, array.map(&:to_i)).run

#!/usr/bin/env ruby

class Main
  attr_reader :size, :number, :array

  def initialize(size, number, array)
    @size = size
    @number = number
    @array = array.sort
  end

  def run
    median_position = calculate_median(size)
    median_number = array[median_position]

    if number == median_number
      0
    elsif array.include?(number)
      index = array.index(number)
      if index > median_position
        revert_median_calculation(index) - size
      elsif index < median_position
        median_position - index
      end
    else
      median_number = array[median_position]
      new_array = (array + [number]).sort
      index = new_array.index(number)
      new_median_position = calculate_median(size + 1)
      if index > new_median_position
        revert_median_calculation(index) - size
      elsif index < new_median_position
        median_position - index + 1
      else
        1
      end
    end
  end

  private

  def calculate_median(array_size)
    (array_size + 1)/2 - 1
  end

  def revert_median_calculation(median)
    (median + 1) * 2 - 1
  end
end

size, number = gets.split(" ")
array = gets.split(" ")
puts Main.new(size.to_i, number.to_i, array.map(&:to_i)).run

#!/usr/bin/env ruby

class Main
  attr_reader :size, :number, :array

  def initialize(size, number, array)
    @size = size
    @number = number
    @array = array.sort
  end

  def run
    median_position = calculate_median(size)
    median_number = array[median_position]

    if number == median_number
      0
    elsif array.include?(number)
      index = array.rindex(number)
      if index > median_position
        revert_median_calculation(index) - size
      elsif index < median_position
        (median_position - index) * 2 - size % 2
      end
    else
      median_number = array[median_position]
      new_array = (array + [number]).sort
      index = new_array.index(number)
      new_median_position = calculate_median(size + 1)
      if index > new_median_position
        revert_median_calculation(index) - size
      elsif index < new_median_position
        (new_median_position - index) * 2 - (size + 1) % 2 + 1
      else
        1
      end
    end
  end

  private

  def calculate_median(array_size)
    (array_size + 1)/2 - 1
  end

  def revert_median_calculation(median)
    (median + 1) * 2 - 1
  end
end

size, number = gets.split(" ")
array = gets.split(" ")
puts Main.new(size.to_i, number.to_i, array.map(&:to_i)).run

#!/usr/bin/env ruby

class Main
  attr_reader :size, :number, :array

  def initialize(size, number, array)
    @size = size
    @number = number
    @array = array.sort
  end

  def run
    median_position = (size + 1)/2 - 1
    median_number = array[median_position]

    if number == median_number
      0
    elsif array.include?(number)
      index = array.index(number)
      (median_position - index).abs
    else
      median_number = array[median_position]
      new_array = (array + [number]).sort
      index = new_array.index(number)
      (median_position - index).abs * 2
    end
  end
end

size, number = gets.split(" ")
array = gets.split(" ")
puts Main.new(size.to_i, number.to_i, array.map(&:to_i)).run

a = gets.chomp.split(" ").map{|i| i.to_i}
b = gets.chomp.split(" ").map{|i| i.to_i}

b.sort!
n = a[0]
k = a[1]

if k > b[n-1]
    puts n+1
else
    if k < b[0]
        puts n
    else
        x = (n+1)/2 - 1
        if k == b[x]
            puts 0
        else
            if k < b[x]
                i = 0
                while b[i] < k do
                    i += 1
                end
                i -= 1 if b[i] != k
                find = n -2*i -2
                puts find
            else
                i = n-1
                while b[i] > k do
                    i -= 1
                end
                i -= 1 if b[i] != k
                find = 2*i + 2 - n
                puts find
            end
        end
    end
end

a = gets.chomp.split(" ").map{|i| i.to_i}
b = gets.chomp.split(" ").map{|i| i.to_i}

b.sort!
n = a[0]
k = a[1]

if k > b[n-1]
    puts n+1
else
    if k < b[0]
        puts n
    else
        x = (n+1)/2 - 1
        if k == b[x]
            puts 0
        else
            if k < b[x]
                i = 0
                while b[i] < k do
                    i += 1
                end
                i -= 1 if b[i] != k
                find = n -2*i -2
                puts find
            else
                i = n-1
                while b[i] > k do
                    i -= 1
                end
                i += 1 if b[i] != k
                find = 2*i + 1 - n
                puts find
            end
        end
    end
end

a = gets.chomp.split(" ").map{|i| i.to_i}
b = gets.chomp.split(" ").map{|i| i.to_i}

b.sort!
n = a[0]
k = a[1]

if k > b[n-1]
    puts n+1
else
    if k < b[0]
        puts n
    else
        x = (n+1)/2
        if k == b[x]
            puts 0
        else
            if k < b[x]
                i = 0
                while b[i] < k do
                    i += 1
                end
                i -= 1 if b[i] != k
                find = n -2*i -2
                puts find
            else
                i = n-1
                while b[i] > k do
                    i -= 1
                end
                i -= 1 if b[i] != k
                find = 2*i + 2 - n
                puts find
            end
        end
    end
end


n,m=gets.split.map(&:to_i)
g=[]
m.times do
  a,b=gets.split.map(&:to_i)
  g<<a<<b
end
g=((1..n).to_a-g).first
p n-1
(1..n).each{|i|puts "#{g} #{i}" if g!=i}

args = gets.split(" ").map{ |i| i.to_i}
n = args[0]
m = args[1]

prohib = Array.new(n) { |i| 0 }
for i in 0...m
  args = gets.split(" ").map{ |i| i.to_i}
  prohib[args[0]-1] += 1
  prohib[args[1]-1] += 1
end
center = prohib.each_with_index.min[1] + 1
puts n - 1
(1..n).to_a.select{ |a| a != center }.each{ |b| puts "#{b} #{center}"}

n, m = gets.split(' ').map(&:to_i)
cannot = Array.new(n + 1){Array.new}
m.times do
  a, b = gets.split(' ').map(&:to_i)
  a, b = b, a if a > b
  cannot[a] << b
  cannot[b] << a
end
center = 1
for i in 1..n
  if cannot[i].size < cannot[center].size
    center = i
  end
end
link = Array.new(n + 1){Array.new(n + 1, false)}
road = []
for i in 1..n
  next if i == center
  if cannot[center].include? i
    for j in 1..n
      next if i == j
      next if cannot[i].include? j
      link[i][j] = link[j][i] = true
      road << [i, j]
      for k in 1...j
        next if link[j][k]
        link[i][k] = link[k][i] = true
        road << [i, k]
      end
      break
    end
  else
    link[center][i] = link[i][center] = true
    road << [center, i]
  end
end
puts road.size
road.each do |r|
  puts r.join(' ')
end

n,m=gets.chomp.split.map(&:to_i)
a=Array.new(1000,0)
m.times do
	i,j=gets.chomp.split.map(&:to_i)
	a[i],a[j]=1,1
end
i,j=1,1
while i<=n
	break if a[i]==0
	i+=1
end
puts n-1
while j<=n
	puts "#{i} #{j}" if j!=i
	j+=1
end

n,m=gets.split.map(&:to_i)
x=[true]*n
m.times {
    gets.split.map(&:to_i).each {|v|
        x[v-1]=false
    }
}
c=x.index(true)+1
p n-1
1.upto(n) {|v|
    puts "#{c} #{v}" if c != v
}

require "Set"

n,m = gets.split(" ").map(&:to_i)
not_connecting = Set.new
not_connecting_pair = []
m.times do
	a,b = gets.split(" ").map(&:to_i)
	not_connecting.add(a)
	not_connecting.add(b)
	not_connecting_pair << [a,b]
end
candidate = (1..n).to_a - not_connecting.to_a
node = candidate.first

puts n - 1
(1..n).each do |i|
	puts "#{node} #{i}" if node != i
end

require 'set'
n, m = gets.split.collect{|i| i.to_i}
mark = Set.new
for i in 0...m
    gets.split.collect{|i| i.to_i}.each{|i| mark.add(i)}
end
if not (Set.new(1..n)-mark).empty?
    puts n-1
    c = (Set.new(1..n)-mark).to_a[0]
    for i in 1..n
        print c, " ", i, "\n" if i!=c
    end
end

require 'set'

def read_next_line
  gets.split.map { |x| x.to_i }
end

n, m = read_next_line
all = Set.new(1..n)
for _ in 1..m
  all -= read_next_line
end

center = all.first
puts n - 1
for i in 1..n
  puts "#{center} #{i}" if i != center
end

n, m = gets.split(' ').map(&:to_i)
ab = m.times.map {|i| gets.split(' ').map(&:to_i).sort }
center = (1..n).select{|i| ab.flatten.uniq.all?{|p| i != p } }
puts n.pred
(1..n).each{|i|
  unless i == center.first
    puts [center.first,i].join ' '
  end
}

n,m=gets.split.map(&:to_i)
x=[true]*n
m.times {
    gets.split.map(&:to_i).each {|v|
        x[v-1]=false
    }
}
c=x.index(true)+1
1.upto(n) {|v|
    puts "#{c} #{v}" if c != v
}


t = gets.to_i

1.upto(t) do
  n = gets.to_i
  result = 2.step(n, 2).to_a + 1.step(n, 2).to_a

  result[n-1] += result[0, n/2].inject(&:+) - result[n/2, n/2].inject(&:+)

  if result.last.odd?
    puts 'YES'
    puts result.join(' ')
  else
    puts 'NO'
  end
end

module Enumerable
  def sum
    inject :+
  end
end

gets.to_i.times do
  n = gets.to_i
  if n%4 != 0
    puts 'NO'
    next
  end
  puts 'YES'
  a, b = []*(n/4), []*(n/4)
  (0...n/4).each{|i| a[2*i, 2*i + 1] = [4*i + 2, 4*i + 4]}
  (0...a.size-1).each{|i| b[i] = 2*i + 1}
  b << a.sum - b.sum
  puts a.join(' ') + ' ' + b.join(' ')
end

t=gets.to_i
(1).upto(t) do
  n=gets.to_i
  n>>=1
  if(n&1)==1
    puts "NO"
    next
  end
  puts "YES"
  sum =0
  (1).upto(n) do |i|
    print "#{i*2} "
    sum+=i*2
  end

  (1).upto(n-1) do |i|
    sum-=i*2-1
    print "#{i*2-1} "
  end
  puts sum
end

gets.to_i.times do
	n = gets.to_i
	if n % 4 == 0
		a = (1..n / 2).map { _1 * 2 }
		a.concat((1...n / 2).map { _1 * 2 - 1 } + [n + n / 2 - 1])
		puts 'YES', a.join(' ')
	else
		puts 'NO'
	end
end

# frozen_string_literal: true

class Result
  def print_result
    n = gets.to_i
    return puts 'NO' if (n / 2).odd?

    puts 'YES'

    (n / 2).times do |i|
      print (i + 1) * 2, ' '
    end
    (n / 2).times do |i|
      print (i * 2) + 1 + (i + 1 == n / 2 ? n / 2 : 0), ' '
    end
    puts
  end
end

gets.to_i.times do
  Result.new.print_result
end

1.upto(gets.chomp.to_i) do
  n = gets.chomp.to_i
  if n % 4 != 0
    puts 'NO'
  else
    puts 'YES'
    k = n/2
    (1..k).each {|x| print "#{2*x} "; }
    (1..k-1).each {|x| print "#{2*x-1} "; }
    puts 3*k-1
  end
end

number_of_testcases = gets.to_s.to_i

number_of_testcases.times do |i|

  n = gets.to_s.to_i
  h = n/2

  if h.odd?
    puts "No"
    next
  else
    puts "Yes"
  end

  2.step(n,  2){ |i| print "#{i} " }
  1.step(n-3,2){ |i| print "#{i} " }
  puts 2 + 2 * (h-1) + (h-1)
end

module Enumerable
  def sum
    inject :+
  end
end

gets.to_i.times do
  n = gets.to_i
  if n%4 != 0
    puts 'NO'
    next
  end
  puts 'YES'
  a, b = [], []
  (0...n/4).each{|i| a[2*i..2*i+1] = [4*i + 2, 4*i + 4]}
  (0...a.size-1).each{|i| b[i] = 2*i + 1}
  b << a.sum - b.sum
  puts a.join(' ') + ' ' + b.join(' ')
end

module Enumerable
  def sum
    inject :+
  end
end

gets.to_i.times do
  n = gets.to_i
  if n%4 != 0
    puts 'NO'
    next
  end
  puts 'YES'
  a, b = [], []
  (0...n/4).each{|i| a[2*i..2*i+1] = 4*i + 2, 4*i + 4}
  (0...a.size-1).each{|i| b[i] = 2*i + 1}
  b << a.sum - b.sum
  puts a.join(' ') + ' ' + b.join(' ')
end

module Enumerable
  def sum
    inject :+
  end
end

gets.to_i.times do
  n = gets.to_i
  if n%4 != 0
    puts 'NO'
    next
  end
  puts 'YES'
  a, b = []*(n/2), []*(n/2)
  (0...n/4).each{|i| a[2*i, 2*i + 1] = [4*i + 2, 4*i + 4]}
  (0...a.size-1).each{|i| b[i] = 2*i + 1}
  b << a.sum - b.sum
  puts a.join(' ') + ' ' + b.join(' ')
end

gets.to_i.times do
  n = gets.to_i
  if n%4 != 0
    puts 'NO'
    next
  end
  puts 'YES'
  a = []
  (0...n/4).each{|i| a += [4*i + 2, 4*i + 4]}
  (0...n/4).each{|i| a += [4*i + 1, 4*i + 7]}
  puts a.join ' '
end

t=gets.to_i
(1).upto(t) do
  n=gets.to_i
  n>>=1
  if(n&1)==1
    puts "NO"
    next
  end
  puts "YES"
  (1).upto(n) do
    print "2 "
  end
  (1).upto(n) do |i|
    x=i&1
    x<<=1
    x+=1
    print "#{x} "
  end
  puts ""
end

t=gets.to_i
(1).upto(t) do
  n=gets.to_i
  n>>=1
  if(n&1)==1
    puts "NO"
    next
  end
  puts "YES"
  (1).upto(n) do |i|
    print "#{i*2} "
  end
  (1).upto(n) do |i|
    print "#{i*2-1} "
  end
  puts ""
end


gets.to_i.times do
  n=gets.to_i
  a=gets.split.map(&:to_i)
  s=a.sum
  if s%n!=0
    puts "-1"
    next
  end
  c=0
  z=s/n
  a.each do |aa|
    if aa>z
      c+=1
    end
  end
  puts c
end

for t in 0...gets.to_i
	n = gets.to_i
	a = gets.split.map(&:to_i)
	sum = a.sum
	ans = 0;
	if sum % n > 0
		puts -1
	elsif
		(0...n).each do |i|
			if a[i] * n > sum
				ans += 1
			end
		end
		puts ans
	end
end


def update_hash hash, num
  return hash[num] = true if hash[num].nil?
  hash.delete(num)
  update_hash(hash, num.next)
end

gets

hash = {}
b = gets.chomp.split(" ").map(&:to_i).each{|num| update_hash(hash, num)}

puts hash.keys.max - hash.keys.count + 1

MAX=(2*10**9)*(10**5)+1
n=gets.to_i
a=gets.split.map(&:to_i)
a<<MAX
a.reverse!
ans=0
i=0
until a.last==MAX
  b=a.pop
  num=1
  while b==a.last
    a.pop
    num+=1
  end
  if num%2==1
    ans+=b-i
    i=b+1
  end
  (num/2).times{a.push(b+1)}
end
puts ans

gets

b = gets.chomp.split(" ").map(&:to_i).map{|a|2 **a}.inject(:*)

puts b.to_s(2).count('0')


number_of_entrances = gets.to_i
min_time = 1000000000
min_time_index = 0
ppl_in_each_entrance = gets.split(' ').map(&:to_i)

def ceiling(num, den)
  (num/den) + (num % den == 0 ? 0 : 1)
end

0.upto(number_of_entrances - 1) do |entrance|
  no_of_ppl = ppl_in_each_entrance[entrance]

  x = [0, no_of_ppl - entrance].max
  value = ceiling(x, number_of_entrances)

  if min_time > value
    min_time = value
    min_time_index = entrance
  end
end

p min_time_index + 1

N = gets.to_i
as = gets.split.map(&:to_i)
N.times do |i|
    as[i] = ((as[i]-i-1)/N + 1) * N + i
    as[i] = i if as[i] < i
end
puts N.times.min_by{|i| as[i]} + 1

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

minv = ary.min
sq = minv % n
exitq = sq
ary.map!{|x| x-minv}
sq.upto(sq+n-1).each do |i|
  aryi = i % n
  if ary[aryi]<=(i-sq)
    exitq = aryi
    break
  end
end

puts exitq+1

number_of_entrances = gets.to_i
min_time = 1000000000
min_time_index = 0

def ceiling(num, den)
  (num/den) + (num % den == 0 ? 0 : 1)
end

0.upto(number_of_entrances - 1) do |entrance|
  no_of_ppl = gets.to_i

  x = [0, no_of_ppl - entrance].max
  value = ceiling(x, number_of_entrances)

  if min_time > value
    min_time = value
    min_time_index = entrance
  end
end

p min_time_index + 1


def lscan; gets.split.map(&:to_i); end

gets.to_i.times do
    l, r = lscan
    x = l
    y = l*2
    puts "#{x} #{y}"
end

t = gets.to_i
t.times do
	r,l = gets.split.map(&:to_i)
	puts "#{r} #{r*2}"
end

gets.to_i.times do
  l, r = gets.split.map(&:to_i)
  puts "#{l} #{l*2}"
end

n = gets().to_i
(1..n).each do
  l, r = gets().split.map(&:to_i)
  puts "#{l} #{l * 2}"
end

gets.to_i.times do
    l,r = gets.split.map(&:to_i)
    puts "#{l} #{l*2}"
end

t = gets.to_i
t.times do
    l, r = gets.split.map &:to_i
    print l, ' ', 2*l, "\n"
end

n = gets().to_i
(1..n).each do
  l, r = gets().split.map(&:to_i)
  (l..r).each do |x|
    i = 2
    if (x * 2 < r)
      puts "#{x} #{x * 2}"
      break
    end
  end
end


n, k = gets.split.map {|x| Integer x}
if k * n > n * (n - 1) / 2
  puts -1
  exit
end
am = Array.new(n)
cnt = Array.new(n, n - 1)
for i in 0...n
  am[i] = Array.new(n)
end
for i in 0...n
  c = 0
  for j in 0...n
    if cnt[j] == k || i == j || am[i][j] != nil
      next
    end
    am[i][j] = true
    am[j][i] = false
    c += 1
    if c == k
      break
    end
  end
end
ans = []
for i in 0...n
  for j in 0...n
    if am[i][j]
      ans.push((i + 1).to_s + ' ' + (j + 1).to_s)
    end
  end
end
puts ans.length
puts ans

#$stdin.reopen('/home/chen/Desktop/input.txt', 'r')

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

if 2*k>=n
	puts -1
else
	puts n*k
	(1..n).each do |ind|
		(1..k).each do |ind2|
			puts "#{ind} #{(ind+ind2-1)%n+1}"
		end
	end
end

n,k=gets.split.map &:to_i
if k > (n-1) / 2
  puts "-1"
  exit
else
  puts n*k
  n.times do |cmdW|
    k.times do |cmdL|
      puts "#{cmdW+1} #{(cmdW+1+cmdL)%n + 1}"
    end
  end
end

#$stdin.reopen('/home/chen/Desktop/input.txt', 'r')

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

if k*(n/2)>=n
	puts -1
else
	puts n*k
	(1..n).each do |ind|
		(1..k).each do |ind2|
			puts "#{ind} #{(ind+ind2-1)%n+1}"
		end
	end
end

n, k = gets.split.map {|x| Integer x}
if k > n * (n - 1) / 4
  puts -1
  exit
end
am = Array.new(n)
cnt = Array.new(n, n - 1)
for i in 0...n
  am[i] = Array.new(n)
end
for i in 0...n
  c = 0
  for j in 0...n
    if cnt[j] == k || i == j || am[i][j] != nil
      next
    end
    am[i][j] = true
    am[j][i] = false
    c += 1
    if c == k
      break
    end
  end
end
for i in 0...n
  for j in 0...n
    if am[i][j]
      puts (i + 1).to_s + ' ' + (j + 1).to_s
    end
  end
end

n, k = gets.split.map {|x| Integer x}
if k > n * (n - 1) / 4
  puts -1
  exit
end
am = Array.new(n)
cnt = Array.new(n, n - 1)
for i in 0...n
  am[i] = Array.new(n)
end
for i in 0...n
  c = 0
  for j in 0...n
    if cnt[j] == k || i == j || am[i][j] != nil
      next
    end
    am[i][j] = true
    am[j][i] = false
    c += 1
    if c == k
      break
    end
  end
end
ans = []
for i in 0...n
  for j in 0...n
    if am[i][j]
      ans.push((i + 1).to_s + ' ' + (j + 1).to_s)
    end
  end
end
puts ans.length
puts ans


n,m,k = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)
ans = 0
a = []
for i in 1..n-1
    a << b[i]-b[i-1]
end
a.sort!
for i in 0..n-k-1
    ans += a[i]
end
puts ans+k

DBG = !true
n,m,k = gets.split.map{|z| z.to_i}
b = gets.split.map{|z| z.to_i}
sd = []
for i in 1...n
  sd << [i, b[i]-b[i-1]]
end
sd.sort_by!{|z| -z[1]}
print sd if DBG

d = sd[0,k-1].map{|z| z[0]}
d.sort!
print d if DBG

prev = 0
sum = 0
for x in d
  sum += b[x-1] - b[prev] + 1
  prev = x
end
sum += b[-1] - b[prev] + 1
puts "#{sum}"


def solution
  n = read_int
  xs = read_ints

  max = 0

  prefs = Hash.new

  sum = 0
  xs.each.with_index do |x, i|
    # p x
    sum += x
    prefs[sum] = i
  end

  sum = 0
  (n-1).downto(0) do |j|
    x = xs[j]
    sum += x

    if prefs[sum] && prefs[sum] < j
      max = sum
    end

  end

  puts  max

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 = gets.to_i
as = gets.split.map(&:to_i)
total = as.inject(:+)
h1, h2 = {}, {}
sum = 0
N.times do |i|
    h1[sum] = true
    sum += as[i]
    break if sum*2 > total
end
sum = 0
(N-1).downto(0) do |i|
    h2[sum] = true
    sum += as[i]
    break if sum*2 > total
end
h1.keys.sort.reverse.each do |sum|
    if h2[sum]
        puts sum
        exit
    end
end


t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  b = [a.max]
  g = a.max
  max_idx = a.index(g)
  vis = a.map.with_index {|_, a_i| a_i == max_idx ? true : false}

  (n - 1).times do
    max_v = -1
    max_i = -1
    a.each_with_index do |a_i, i|
      next if vis[i]

      if g.gcd(a_i) > max_v
        max_i = i
        max_v = g.gcd(a_i)
      end
    end

    b << a[max_i]
    vis[max_i] = true
    g = max_v
  end

  puts b.join(' ')
end

require 'prime'

class Integer
  def divisor_list
    return [] if self <= 0
    return [1] if self == 1

    prime_division.map.with_index { |(base, k), i|
      s = i.zero? ? 0 : 1
      (s..k).map { |n| base ** n }
    }.inject { |res, e| res + res.flat_map { |t| e.map { |v| t * v } } }.sort
  end
end

T = gets.to_i

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

  counter = Hash.new(0)
  primes = Hash.new { |h, k| h[k] = [] }

  a.each do |x|
    counter[x] += 1

    x.prime_division.each do |e, _|
      primes[e] << x
    end
  end

  ans = []
  s = a.max
  g = s

  loop {
    ans << s
    counter[s] -= 1
    cand = -1
    max_v = -Float::INFINITY

    s.prime_division.map(&:first).reverse_each do |e|
      primes[e].each do |num|
        next if counter[num] <= 0

        next_gcd = g.gcd(num)

        if max_v < next_gcd
          max_v = next_gcd
          cand = num
        end
      end
    end

    break if cand == -1
    g = g.gcd(cand)
    s = cand
  }

  counter.each do |num, cnt|
    next if cnt <= 0

    cnt.times do
      ans << num
    end
  end

  puts ans.join(' ')
end

require 'prime'

T = gets.to_i

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

  counter = Hash.new(0)
  primes = Hash.new { |h, k| h[k] = [] }

  a.each do |x|
    counter[x] += 1

    x.prime_division.each do |e, cnt|
      primes[e] << x
    end
  end

  ans = []
  s = a.max

  loop {
    ans << s
    counter[s] -= 1
    cand = -1

    s.prime_division.map(&:first).reverse_each do |e|
      primes[e].each do |num|
        next if counter[num] <= 0

        cand = num
        break
      end

      break if cand != -1
    end

    break if cand == -1
    s = cand
  }

  counter.each do |num, cnt|
    next if cnt <= 0

    cnt.times do
      ans << num
    end
  end

  puts ans.join(' ')
end

require 'prime'

class Integer
  def divisor_list
    return [] if self <= 0
    return [1] if self == 1

    prime_division.map.with_index { |(base, k), i|
      s = i.zero? ? 0 : 1
      (s..k).map { |n| base ** n }
    }.inject { |res, e| res + res.flat_map { |t| e.map { |v| t * v } } }.sort
  end
end

T = gets.to_i

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

  counter = Hash.new(0)
  primes = Hash.new { |h, k| h[k] = [] }

  a.each do |x|
    counter[x] += 1

    x.prime_division.each do |e, _|
      primes[e] << x
    end
  end

  ans = []
  s = a.max
  gcd = s

  loop {
    ans << s
    counter[s] -= 1
    cand = -1
    max_v = -Float::INFINITY

    s.prime_division.reverse_each do |e|
      primes[e].each do |num|
        next if counter[num] <= 0

        next_gcd = gcd.gcd(num)

        if max_v < next_gcd
          max_v = next_gcd
          cand = num
        end
      end
    end

    break if cand == -1
    gcd = gcd.gcd(cand)
    s = cand
  }

  counter.each do |num, cnt|
    next if cnt <= 0

    cnt.times do
      ans << num
    end
  end

  puts ans.join(' ')
end

require 'prime'

class Integer
  def divisor_list
    return [] if self <= 0
    return [1] if self == 1

    prime_division.map.with_index { |(base, k), i|
      s = i.zero? ? 0 : 1
      (s..k).map { |n| base ** n }
    }.inject { |res, e| res + res.flat_map { |t| e.map { |v| t * v } } }.sort
  end
end

T = gets.to_i

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

  counter = Hash.new(0)
  primes = Hash.new { |h, k| h[k] = [] }

  a.each do |x|
    counter[x] += 1

    x.prime_division.each do |e, _|
      primes[e] << x
    end
  end

  ans = []
  s = a.max
  gcd = s

  loop {
    ans << s
    counter[s] -= 1
    cand = -1
    max_v = -Float::INFINITY

    s.prime_division.map(&:first).reverse_each do |e|
      primes[e].each do |num|
        next if counter[num] <= 0

        next_gcd = gcd.gcd(num)

        if max_v < next_gcd
          max_v = next_gcd
          cand = num
        end
      end
    end

    break if cand == -1
    gcd = s.gcd(cand)
    s = cand
  }

  counter.each do |num, cnt|
    next if cnt <= 0

    cnt.times do
      ans << num
    end
  end

  puts ans.join(' ')
end

require 'prime'

class Integer
  def divisor_list
    return [] if self <= 0
    return [1] if self == 1

    prime_division.map.with_index { |(base, k), i|
      s = i.zero? ? 0 : 1
      (s..k).map { |n| base ** n }
    }.inject { |res, e| res + res.flat_map { |t| e.map { |v| t * v } } }.sort
  end
end

T = gets.to_i

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

  counter = Hash.new(0)
  primes = Hash.new { |h, k| h[k] = [] }

  a.each do |x|
    counter[x] += 1

    x.divisor_list.each do |e, _|
      primes[e] << x
    end
  end

  ans = []
  s = a.max
  gcd = s

  loop {
    ans << s
    counter[s] -= 1
    cand = -1
    max_v = -Float::INFINITY

    s.divisor_list.reverse_each do |e|
      primes[e].each do |num|
        next if counter[num] <= 0

        next_gcd = gcd.gcd(num)

        if max_v < next_gcd
          max_v = next_gcd
          cand = num
        end
      end
    end

    break if cand == -1
    gcd = s.gcd(cand)
    s = cand
  }

  counter.each do |num, cnt|
    next if cnt <= 0

    cnt.times do
      ans << num
    end
  end

  puts ans.join(' ')
end

require 'prime'

class Integer
  def divisor_list
    return [] if self <= 0
    return [1] if self == 1

    prime_division.map.with_index { |(base, k), i|
      s = i.zero? ? 0 : 1
      (s..k).map { |n| base ** n }
    }.inject { |res, e| res + res.flat_map { |t| e.map { |v| t * v } } }.sort
  end
end

T = gets.to_i

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

  counter = Hash.new(0)
  primes = Hash.new { |h, k| h[k] = [] }

  a.each do |x|
    counter[x] += 1

    x.divisor_list.each do |e, cnt|
      primes[e] << x
    end
  end

  ans = []
  s = a.max

  loop {
    ans << s
    counter[s] -= 1
    cand = -1

    s.divisor_list.reverse_each do |e|
      primes[e].each do |num|
        next if counter[num] <= 0

        cand = num
        break
      end

      break if cand != -1
    end

    break if cand == -1
    s = cand
  }

  counter.each do |num, cnt|
    next if cnt <= 0

    cnt.times do
      ans << num
    end
  end

  puts ans.join(' ')
end

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  b = [a.max]
  g = a.max
  vis = a.map {|a_i| a_i == g ? true : false}

  (n - 1).times do
    max_v = -1
    max_i = -1
    a.each_with_index do |a_i, i|
      next if vis[i]

      if g.gcd(a_i) > max_v
        max_i = i
        max_v = g.gcd(a_i)
      end
    end

    b << a[max_i]
    vis[max_i] = true
    g = max_v
  end

  puts b.join(' ')
end

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  b = [a.max]
  g = a.max
  vis = a.map {|a_i| a_i == g ? true : false}

  (n - 1).times do
    max_v = -1
    max_i = -1
    max_ai = -1
    a.each_with_index do |a_i, i|
      next if vis[i]

      if g.gcd(a_i) > max_v || (g.gcd(a_i) == max_v && a_i > max_ai)
        max_i = i
        max_v = g.gcd(a_i)
        max_ai = a_i
      end
    end

    b << a[max_i]
    vis[max_i] = true
    g = max_v
  end

  puts b.join(' ')
end

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  max_v = a.max
  b = a.map {|a_i| [a_i, a_i.gcd(max_v)]}.sort_by {|av, gv| [-gv, -av]}.map(&:first)
  puts b.join(' ')
end


n = gets.to_i
arr = gets.split(" ").map{|i| i.to_i}
sum = arr.inject(:+)
max = arr.max
ans = (sum + n - 2) / (n - 1)
puts max > ans ? max : ans

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
amax = a.max
ans = 0
a.each{|aa| ans += amax-aa}
if amax <= ans
    puts amax
else
    amax -= ans
    ans += amax/(n-1)*n
    amax %= n-1
    ans += amax+1 if amax > 0
    puts ans
end

n=gets.to_i; d=gets.split.map(&:to_i); p [d.max,(d.reduce(&:+)+n-2)/(n-1)].max

#! /usr/bin/env ruby

n = STDIN.gets.to_i
rounds = STDIN.gets.to_s.split(' ').map(&:to_i).take(n)

if rounds.length != n
  raise ArgumentError.new
end

def how_many(rounds, n)
  max = rounds.max
  diff_total = rounds.inject(0) {|diff, round| diff += max - round}

  if diff_total >= max
    max
  else
    n -= 1
    rest = max - diff_total
    max + (rest / n) + (rest % n == 0 ? 0 : 1)
  end
end

puts how_many(rounds, n)

n = gets.to_i
a = gets.split.map(&:to_i)
mx, s = [0, 0]
a.each {|i|
  mx = [mx, i].max
  s += i
}
puts [mx, (s+n-2).div(n.pred)].max

#! /usr/bin/env ruby

n = STDIN.gets.to_i
rounds = STDIN.gets.to_s.split(' ').map(&:to_i).sample(n)

if rounds.length != n
  raise ArgumentError.new
end

def how_many(rounds)
  max = rounds.max
  diff_total = rounds.inject(0) {|diff, round| diff += max - round}

  if diff_total == 0
    return max + (max / rounds.length) + ( max % rounds.length == 0 ? 0 : 1)
  end

  if diff_total >= max
    return max
  end

  max + (max - diff_total)
end

puts how_many(rounds)

#! /usr/bin/env ruby

n = STDIN.gets.to_i
rounds = STDIN.gets.to_s.split(' ').map(&:to_i).sample(n)

if rounds.length != n
  raise ArgumentError.new
end

def how_many(rounds)
  max = rounds.max
  diff_total = rounds.inject(0) {|diff, round| diff += max - round}

  if diff_total == 0
    return max + (max / rounds.length) + 1
  end

  if diff_total >= max
    return max
  end

  return max + (max - diff_total)
end

puts how_many(rounds)

#! /usr/bin/env ruby

n = STDIN.gets.to_i
rounds = STDIN.gets.to_s.split(' ').map(&:to_i).take(n)

if rounds.length != n
  raise ArgumentError.new
end

def how_many(rounds, n)
  max = rounds.max
  diff_total = rounds.inject(0) {|diff, round| diff += max - round}

  if diff_total == 0
    return max + (max / n) + ( max % n == 0 ? 0 : 1)
  end

  if diff_total >= max
    return max
  end

  max + (max - diff_total)
end

puts how_many(rounds, n)

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
amax = a.max
cnt = 0
a.each{|aa| cnt += amax-aa}
if amax <= cnt
    puts amax
else
    amax -= cnt
    puts cnt+amax/(n-1)*n+amax%(n-1)+1
end

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
amax = a.max
cnt = 0
a.each{|aa| cnt += amax-aa}
amax -= cnt
if amax <= 0
    puts cnt
else
    puts cnt+amax/(n-1)*n+amax%(n-1)+1
end

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
amax = a.max
cnt = 0
a.each{|aa| cnt += amax-aa}
amax -= cnt
if amax.zero?
    puts cnt
else
    puts cnt+amax/(n-1)*n+amax%(n-1)+1
end

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
amin, amax = a.minmax
ans = 0
if amin/(n-1) > 0
    amax -= amin
    ans += amin/(n-1)*n
    amin %= n-1
end
if amax != amin
    ans += amax
else
    ans += amax+1
end
puts ans

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
amax = a.max
cnt = 0
a.each{|aa| cnt += amax-aa}
amax -= cnt
puts cnt+amax/(n-1)*n+amax%(n-1)+1

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
puts a.max+1

n = gets.to_i
a = gets.split.collect{|i| i.to_i}
amin, amax = a.minmax
ans = 0
if amin/(n-1) > 0
    amax -= amin
    ans += amin/(n-1)*n
    amin %= n-1
end
if amin.zero?
    ans += amax
else
    ans += amax+1
end
puts ans

people = gets.to_i
take_rounds = gets.split.map(&:to_i)
max = take_rounds.max
sum = take_rounds.reduce(&:+)
if sum <= max * people.pred
  puts max
else
  puts max.next
end


require 'prime'
t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  arr = n.prime_division
  res = []
  while arr.any?
    res << arr.map{|el| el[0]}.inject(:*)
    arr = arr.map{|el| [el[0], el[1] - 1]}
    arr.filter! {|el| el[1] > 0}
  end
  puts res.size
  puts res.reverse.join(' ')
end

require'prime'
gets;$<.map{|l|i=l.to_i;x,y=i.prime_division.map{|k,v|[v-1,k]}.max;p x+1;puts ([y]*x+[i/y**x])*' '}


t = gets.chomp.to_i
t.times do
  hash = {}
  n = gets.chomp.to_i
  a = gets.split.map{|_|_.to_i}
  a.each_with_index do |num, i|
    hash[num-i] = (hash[num-i] || 0) + 1
  end
  puts hash.values.map{|v| v*(v-1)/2}.sum
end

t = gets.chomp.to_i

t.times do
  hash = {}
  n = gets.chomp.to_i
  a = gets.split.map{|_|_.to_i}
  a.each_with_index do |num, i|
    hash[num-i] = (hash[num-i] || 0) + 1
  end
  rs = 0
  hash.values.each{|v| rs+=v*(v-1)/2}
  puts rs
end

def read_test_case
  # @type [String]
  line = gets.chomp
  # &: notation is sometimes called 'pretzel colon'
  # Kind of like a method references? It's syntax sugar for some Proc magic:
  # &:to_i == { |foo| foo.to_i }
  # :to_i here is a symbol, and:
  line.split(' ').map(&:to_i)
end

# @param nums [Array]
def get_diff_array(nums)
  nums.map.each_with_index do |num, idx|
    # Problem uses 1 indexing, so I'll be consistent
    num - (idx + 1)
  end
end

# @param diffs [Array]
def count_diff_array(diffs)
  counts = {}
  diffs.each do |diff|
    if !counts.include? diff
      counts[diff] = 1
    else
      counts[diff] += 1
    end
  end
  counts
end

# @param counts [Hash]
def count_total_pairs(counts)
  pairs = 0
  counts.each_value do |count|
    pairs += compute_triangular_number(count - 1)
  end
  pairs
end

def compute_triangular_number(n)
  (n * (n + 1)) / 2
end

# @type[Integer]
t = gets.chomp.to_i

(1..t).each do
  _test_size = gets

  test = read_test_case
  diff_array = get_diff_array test
  counts = count_diff_array diff_array
  puts count_total_pairs counts
end

t = gets.to_s.to_i
1.upto(t) do |casenum|
  n = gets.to_s.to_i
  a = gets.to_s.strip.split.map {|x| x.to_i }
  0.upto(n - 1) {|i| a[i] -= i }
  cnt = {}
  a.each {|x| cnt[x] = (cnt[x] || 0) + 1 }
  result = 0
  cnt.each {|k, v| result += v * (v - 1) / 2 }
  printf("%d\n", result)
end


a, b, c = gets.split.map(&:to_i)
x = 0
if a>=b && a>=c
    x+=1 if a!=b
    x+=1 if a!=c
   p a-b+a-c-x
elsif c>=b && c>=a
    x+=1 if c!=b
    x+=1 if c!=a
    p c-b+c-a-x
else
    x+=1 if c!=b
    x+=1 if b!=a
    p b-c+b-a-x
end

def ev(arr)
  arr.sort!
  arr[2] - arr[0] + arr[2] - arr[1]
end

b, d, s = gets.strip.split(' ').map(&:to_i)
mn = [b, d, s].min
b -= mn
d -= mn
s -= mn
if b == d && d == s
  printf("%d\n", 0)
else
  v = []
  arr = [b, d, s]
  0.upto(2) do |i|
    0.upto(2) do |j|
      arr_c1 = arr.clone
      arr_c1[i] += 1
      arr_c2 = arr.clone
      arr_c2[j] += 1
      arr_c3 = arr.clone
      arr_c3[i] += 1
      arr_c3[j] += 1 if i != j
      v << ev(arr_c1)
      v << ev(arr_c2)
      v << ev(arr_c3)
    end
  end

  printf("%d\n", v.min)
end

a=gets.split.map(&:to_i)
m=a.max-1
p a.select{|e|e<m}.reduce(0){|s,e|s+m-e}

a = gets.split.map &:to_i
m = a.max - r = 1
a.map{|e| e < m && r += m - e}
p ~- r

a=gets.split.map &:to_i
m=a.max-r=1
a.map{|e|e<m&&r+=m-e}
p~-r

class Sanatorium
  def self.get_advice(meat_cnt)
    meat_at_last_day = false
    meat_at_first_day = false
    result=0
    return 0 if meat_cnt.all? {|x| x==meat_cnt[2]}
    return 0 if meat_cnt.all? {|x| x<=1}

    if meat_cnt.max == meat_cnt[2] #&& meat_cnt[2]> meat_cnt[0]
      meat_cnt[1]-=1 if meat_cnt[2]==meat_cnt[1]
      meat_cnt[2]-=1
      return 0 if meat_cnt.all? {|x| x==meat_cnt[2]}
      meat_at_first_day=true
    end

    if meat_cnt.max == meat_cnt[0] && meat_cnt[0]>meat_cnt[2]
      meat_cnt[1]-=1 if meat_cnt[0]== meat_cnt[1]
      meat_cnt[0]-=1
      return 0 if meat_cnt.all? {|x| x==meat_cnt[0]}
      meat_at_last_day=true
    end

    if meat_cnt.max == meat_cnt[1]
      if !meat_at_last_day && meat_cnt[1]>meat_cnt[2]
        meat_cnt[1]-=1
        if meat_cnt[0]>0
          meat_cnt[0]-=1
        else
          result+=1
        end
      end
      if !meat_at_first_day && meat_cnt[1]>meat_cnt[0]
        meat_cnt[1]-=1
        if meat_cnt[2]>0
          meat_cnt[2]-=1
        else
          result+=1
        end
      end
      return result if meat_cnt.all? {|x| x==meat_cnt[1]}
    end
    meat_cnt=(meat_cnt.map { |x| x-meat_cnt.min })
    return 0 if meat_cnt.all? {|x| x.zero?}
    result+=case meat_cnt.count(0)
              when 2
                meat_cnt.reduce(:+)*2
              when 1
                meat_cnt.select {|x| x.nonzero?}.minmax.reduce(0) {|sum,x| (sum.zero?) ? (sum-x):(sum+x*2)}
            end
  end

  def self.run
    puts get_advice($stdin.readline.chomp.split.map(&:to_i))
  end
end

if __FILE__ == $0
  Sanatorium.run
end

a, b, c = gets.split.map(&:to_i)
p [[a, b, c].max-[a, b, c].min-1, 0].max

a = gets.split.map &:to_i
m = a.max - r = 1
a.map{|e| e < m && r += m - e; puts r}
p r - 1

a=gets.split.map &:to_i
def solution(b,d,s)
	max = [b,d,s].max
	sum = 0
	case max
	when b
		sum += max - d - 1  if max > d + 1
		sum += max - s - 1  if max > s + 1
	when d
		sum += max - b if max > b
		sum += max - s - 1 if max > s + 1
	when s
		sum += max - b if max > b
		sum += max - d if max > d
	end
	sum
end
p solution(a[0],a[1],a[2])

a=gets.split.map &:to_i
def solution(b,d,s)
	max = [b,d,s].max
	sum = 0
	case max
	when b
		sum += max - d - 1  if max > d + 1
		sum += max - s - 1  if max > s + 1
	when d
		sum += max - b  if max > b && b != 0
		sum += max - s - 1 if max > s + 1
	when s
		sum += max - b if max > b && b != 0
		sum += max - d if max > d && d != 0
	end
	sum
end
p solution(a[0],a[1],a[2])

def ev(arr)
  b, d, s = arr
  (b - d).abs + (d - s).abs
end

b, d, s = gets.strip.split(' ').map(&:to_i)
mn = [b, d, s].min
b -= mn
d -= mn
s -= mn
if b == d && d == s
  printf("%d\n", 0)
else
  v = []
  arr = [b, d, s]
  0.upto(2) do |i|
    0.upto(2) do |j|
      arr_c1 = arr.clone
      arr_c1[i] -= 1
      arr_c2 = arr.clone
      arr_c2[j] -= 1
      arr_c3 = arr.clone
      arr_c3[i] -= 1
      arr_c3[j] -= 1
      v << ev(arr_c1)
      v << ev(arr_c2)
      v << ev(arr_c3)
    end
  end
  printf("%d\n", v.min)
end

b, d, s = gets.strip.split(' ').map(&:to_i)
mn = [b, d, s].min
b -= mn
d -= mn
s -= mn
if b == d && d == s
  printf("%d\n", 0)
else
  arr = [b, d, s]
  mx = -1
  cnt = 0
  arr.each do |l|
    if l > mx
      mx = l
      cnt = 1
    elsif l == mx
      cnt += 1
    end
  end
  if cnt == 2
    0.upto(2) do |i|
      arr[i] -= 1 if mx == arr[i]
    end
    printf("%d\n", arr.max - arr.min)
  else
    0.upto(2) do |i|
      arr[i] -= 1 if mx == arr[i]
    end
    printf("%d\n", arr.max - arr.min)
  end
end

b, d, s = gets.strip.split(' ').map(&:to_i)
mn = [b, d, s].min
b -= mn
d -= mn
s -= mn
if b == d && d == s
  printf("%d\n", 0)
else
  arr = [b, d, s]
  mx = -1
  cnt = 0
  arr.each do |l|
    if l > mx
      mx = l
      cnt = 1
    elsif l == mx
      cnt += 1
    end
  end
  if cnt == 2
    0.upto(2) do |i|
      arr[i] -= 1 if mx == arr[i]
    end
    printf("%d\n", arr.max - arr.min)
  else
    0.upto(2) do |i|
      arr[i] -= 1 if mx == arr[i]
    end
    if arr.uniq == 3
      printf("%d\n", (arr[0] - arr[1]).abs + (arr[1] - arr[2]).abs - 1)
    else
      printf("%d\n", arr.max - arr.min)
    end
  end
end

def ev(arr)
  b, d, s = arr
  ((b - d).abs + (d - s).abs + (s - b).abs) / 2
end

b, d, s = gets.strip.split(' ').map(&:to_i)
mn = [b, d, s].min
b -= mn
d -= mn
s -= mn
if b == d && d == s
  printf("%d\n", 0)
else
  v = []
  arr = [b, d, s]
  0.upto(2) do |i|
    0.upto(2) do |j|
      arr_c1 = arr.clone
      arr_c1[i] -= 1
      arr_c2 = arr.clone
      arr_c2[j] -= 1
      arr_c3 = arr.clone
      arr_c3[i] -= 1
      arr_c3[j] -= 1
      v << ev(arr_c1)
      v << ev(arr_c2)
      v << ev(arr_c3)
    end
  end
  printf("%d\n", v.min)
end


gets.to_i.times do
  gets
  puts gets.chomp.chars.sort == 'Timur'.chars.sort ? :YES : :NO
end

t = gets.chomp.to_i

for i in 0..t-1

  n = gets.chomp.to_i
  name = gets.chomp
  if name = name.chars.sort.join == "Timru"
    puts "YES"
  else
    puts "NO"
  end

end

t = gets.chomp.to_i

for i in 0..t-1

  n = gets.chomp.to_i
  name = gets.chomp
  if name = name.chars.sort.join == "Timru"
    print "Yes"
  else
    print "No"
  end

end


T = gets.to_i
T.times do
    n = gets.to_i
    a = gets.split.map(&:to_i)
    a.unshift(0) if n.odd?
    n = a.size
    puts (0 ... n / 2 - 1).all? { |i| a[i * 2, 2].max <= a[i * 2 + 2, 2].min } ? "YES" : "NO"
end

gets.to_i.times do
  gets
  as = gets.split.map(&:to_i)
  ((as.size % 2)..as.size - 2).step(2).each do |i|
    if as[i] > as[i + 1]
      as[i], as[i + 1] = as[i + 1], as[i]
    end
  end
  ok = as.each_cons(2).all? { |a, b| a <= b }
  puts ok ? "YES" : "NO"
end

gets.to_i.times do
  gets
  as = gets.split.map(&:to_i)
  (0..as.size - 2).step(2).each do |i|
    if as[i] > as[i + 1]
      as[i], as[i + 1] = as[i + 1], as[i]
    end
  end
  ok = as.each_cons(2).all? { |a, b| a <= b }
  puts ok ? "YES" : "NO"
end

T = gets.to_i
T.times do
    n = gets.to_i
    a = gets.split.map(&:to_i)
    a << 10**18 if n.odd?
    n = a.size
    puts (0 ... n / 2 - 1).all? { |i| a[i * 2, 2].max <= a[i * 2 + 2, 2].min } ? "YES" : "NO"
end


def is_pal?(s)
  half = s.size / 2
  s[0...half] === s[-half..-1].reverse
end

gets.to_i.times {
  s = gets.chomp
  if s.count('a') === s.size
    puts 'NO'
  else
    puts 'YES'
    s2 = "a#{s}"
    if !is_pal?(s2)
      puts s2
    else
      puts "#{s}a"
    end
  end
}

gets.to_i.times do
  s=gets.chomp
  z=s.size
  i=0
  j=z-1
  f=false
  while i<=j
    if s[i]=="a"&&s[j]=="a"
      i+=1
      j-=1
    elsif s[j]=="a"
      puts "YES"
      if j==z-1
        puts s+"a"
      else
        puts s[0..j]+"a"+s[j+1..]
      end
      f=true
      break
    else
      puts "YES"
      if i==0
        puts "a"+s
      else
        puts s[0...i]+"a"+s[i..]
      end
      f=true
      break
    end
  end
  if !f
    puts "NO"
  end
end

def palindrome?(string)
  string == string.reverse
end

# returns correct string if found else returns nil
def no_palindrome(string)
  # appending 'a' before or after a string is enough to break a palindrome
  return "a#{string}" unless palindrome?("a#{string}")
  return "#{string}a" unless palindrome?("#{string}a")

  nil
end

# prints output for test cases
def print_answer(string)
  answer = no_palindrome(string)

  return puts 'NO' if answer.nil?

  puts 'YES'
  puts answer
end

#### code for tests:

number_of_cases = gets.chomp.to_i

number_of_cases.times do
  string = gets.chomp
  print_answer(string)
end

n=gets.rstrip.to_i
n.times do
    s=gets.rstrip
    valid=false
    s.each_char do |c|
        if c!='a'
            valid=true
            break
        end
    end
    if valid
        puts "YES"
        s2=s+'a'
        if s2==s2.reverse
            puts 'a'+s
        else
            puts s2
        end
    else
        puts "NO"
    end
 end


n = gets.strip.to_i
ar = gets.strip.split.map(&:to_i)
m = ar.min
a = ar.each_with_index.select{|e, i| e == m}
c = a.reduce([]){|res, e| res <<e[1]}
b = c.each_cons(2).map{|a,b| b-a-1}
b << (c[0] +n-c[-1]-1 )
puts n*m+b.max

gets
a = gets.split(" ").map(&:to_i)
n=a.size
base = a.min
ans = 0

sans = base*n
(a+a).each do |x|
  if x == base
    sans = base*n
  else
    sans += 1
  end
  ans = sans if sans > ans
end

p ans

n = Integer(gets)
s = gets
mas = s.split(" ")
a = Array.new(n, Array.new(2))
for i in 0...n
    a[i] = [i, mas[i].to_i]
end

a.sort! do |x, y|
    if x[1] < y[1]
        -1
    elsif x[1] > y[1]
        1
    else
        x[0] <=> y[0]
    end
end

def max(x, y)
    if x > y
        return x
    else
        return y
    end
end

ans = a[0][1] * n

x = 0
i = 0
pL = a[0][0]
pR = a[0][0]
while i + 1 < n && a[i][1] == a[i + 1][1]
    pR = a[i + 1][0]
    x = max(x, pR - a[i][0])
    i += 1
end

x = max(x, n - pR + pL) - 1
ans += x
puts ans

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

amin = 10**9
hs = Hash.new{ |hs, k| hs[k] = [] }

for i in 0...n
  a = as[i]
  amin = [amin, a].min
  hs[a] << i
end

cols = n - 1 - hs[amin][-1] + hs[amin][0]
for i in 1...hs[amin].size
  cols = [cols, hs[amin][i] - hs[amin][i - 1] - 1].max
end

puts n * amin + cols

n = gets.chomp.to_i
jars = gets.chomp.split(" ").map(&:to_i)
jarmin = jars.min
if jars.uniq.length == 1 then
  puts jars.length * jarmin
else
  jars = jars * 2
  rem = 0
  i = 0
  while true
    break if jars.empty?
    j = jars.shift
    if j == jarmin then
      rem = [rem, i].max
      i = 0
    else
      i += 1
    end
  end
  puts n * jarmin + [rem, i].max
end

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

amin = 10**9
fst = Hash.new(n - 1)
lst = Hash.new(0)

for i in 0...n
  a = as[i]
  amin = [amin, a].min
  fst[a] = [fst[a], i].min
  lst[a] = [lst[a], i].max
end

puts n * (amin + 1) - (lst[amin] - fst[amin] + 1)

n = Integer(gets)
s = gets
a = Array.new(n, Array.new(2))
t = 0
sz = 0
for i in 0...s.length
    c = s[i]
    if c == " "
        a[sz] = [sz, t]
        t = 0
        sz += 1
    else
        t = t * 10 + c.to_i
    end
end
a[sz] = [sz,t]
sz += 1
t = 0

a.sort! do |x, y|
    if x[1] < y[1]
        -1
    elsif x[1] > y[1]
        1
    else
        x[0] <=> y[0]
    end
end

def max(x, y)
    if x > y
        return x
    else
        return y
    end
end

ans = a[0][1] * n

x = 0
i = 0
pL = a[0][0]
pR = a[0][0]
while i + 1 < n && a[i][1] == a[i + 1][1]
    pR = a[i + 1][0]
    x = max(x, pR - a[i][0])
    i += 1
end

x = max(x, n - pR + pL) - 1
ans += x
puts ans


require 'set'

str = gets.chomp

# nunmber = -1
# string = input
# index  = input
def fun(number, str, index, length)

	if index >=length
		return 0
	end

	if number == -1
		number = str[index].to_i
	else
		number *= 10 + str[index].to_i
	end

	if number % 3 == 0
		return 1 + fun(-1, str, index + 1, length)
	else
		return [fun(number, str, index + 1, length), fun(-1, str, index + 1, length)].max
	end
end

def dp_fun(str, length)
	a = str.split('').map(&:to_i)
	dp_a = Array.new(length, 0)
	index_i = 0
	index_j = -1
	while index_i < length
		i = index_i
		number = 0
		(dp_a[index_i] += dp_a[index_i - 1]) if (index_i -1 >= 0)
		while i > index_j
			number *= 10
			number += a[i]
			if number % 3 == 0
				dp_a[index_i] += 1
				index_j = index_i
				break
			end
			i -= 1
		end
		index_i += 1
	end
	return dp_a[length-1]
end

print dp_fun(str, str.size)

def max_div_3(digit_string)
  digits = digit_string.chomp.chars.map(&:to_i)

  prefix_sums = [0]
  crt = 0
  digits.each do |d|
    crt += d
    prefix_sums << crt
  end

  count = 0
  i = 0
  j = 0

  while j < digits.size
    start_index = (i..j).find do |k|
      range_sum = prefix_sums[j + 1] - prefix_sums[k]
      range_sum % 3 < 1
    end

    if start_index
      count += 1
      j = j + 1
      i = j
    else
      j = j + 1
    end
  end

  count
end

def solution
  input = gets
  puts(max_div_3(input))
end

solution

S = gets.chomp.split('').map{|c| c.to_i % 3}
A = [true, false, false]
arr = A.dup
sum = 0
ans = 0
S.each do |a|
    sum = (sum + a) % 3
    if arr[sum]
        ans += 1
        sum = 0
        arr = A.dup
    else
        arr[sum] = true
    end
end
puts ans

ary = gets.strip.split('').map(&:to_i)
n = ary.size
dp = Array.new(n+5, 0)

dp[0] = (ary[0]%3==0 ? 1 : 0)

1.upto(n-1).each do |i|
  sum = 0
  dp[i] = dp[i-1]
  i.downto(0).each do |j|
    sum += ary[j]
    if sum%3 ==0
      dp[i] = [dp[i], ((j>0 ? dp[j-1] : 0) + 1)].max
      break
    end
    break if dp[j]<dp[i]
  end
end

puts dp[n-1]

def max_div_3(digit_string)
  digits = digit_string.chomp.chars.map(&:to_i)

  prefix_sums = [0]
  crt = 0
  digits.each do |d|
    crt += d
    prefix_sums << crt
  end

  p prefix_sums

  count = 0
  i = 0
  j = 0

  while j < digits.size
    start_index = (i..j).find do |k|
      range_sum = prefix_sums[j + 1] - prefix_sums[k]
      range_sum % 3 < 1
    end

    if start_index
      count += 1
      j = j + 1
      i = j
    else
      j = j + 1
    end
  end

  count
end

puts(max_div_3(gets))


q = gets.to_i
q.times do
  a, b, c = gets.split.map(&:to_i)
  puts (a+b+c)/2
end

q = gets.to_i
q.times do
  a = gets.split.map(&:to_i)
  p a.inject(:+)/2
end

#!/usr/bin/env ruby

q = STDIN.gets.to_i

def calc(x)
  x.split(' ').map(&:to_i).inject(:+) / 2
end
q.times { puts calc(STDIN.gets.chomp) }

#!/usr/bin/env ruby

q = STDIN.gets.to_i
xs = q.times.map{STDIN.gets.chomp}
def calc(x)
  x.split(' ').map(&:to_i).inject(:+) / 2
end

res = xs.map do |x|
  calc(x)
end

puts res

gets.to_i.times do
    a,b,c = gets.split.map(&:to_i).sort
    if a+c < b
        puts a+c
    else
        puts (a+b+c)/2
    end
end

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i
n.times {
    q = gets.split.map(&:to_i)
    s = q.reduce(:+)

    puts s >> 1
}

gets.to_i.times do
  a, b, c = gets.split.map(&:to_i)
  puts (a + b + c) / 2
end

times = gets.chomp
ary = []
answ = []

times.to_i.times do
    ary.push(gets.chomp.split(" ").map(&:to_i))
end

ary.each do |ele|
    a = ele.delete_at(ele.index(ele.min))
    b = ele.delete_at(ele.index(ele.min))
    max = ele.last

    if a == b && b == max
        answ << (a+b+max)/2
        next
    end

    tmp = b-a

    if tmp > 0
        t_shr = max - tmp
        if t_shr%2 == 0
            shr = t_shr/2
        else
            shr = (t_shr-1)/2
        end
        answ << shr+tmp+[a,b].min
    elsif tmp == 0
        if max%2 == 0
            a = a + (max/2)
        else
            a = a + ((max-1)/2)
        end
        answ << a
    end
end

answ.each do |ele|
    puts "#{ele}\n"
end

MOD=10**9+7
cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def desc(ar) ar.sort!{|x,y|y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+) end
#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end

def puts_yesno(can) puts(can ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def debug(k,v) puts "#{k}:#{v}" end
########### ( ˘ω˘ )ｽﾔｧ… ###########
q = gi
ans = []
q.times do
  a, b, c = gsmi
  ans << (a+b+c)/2
end
puts ans

1.upto(gets.to_i) do
  puts gets.split(' ').map(&:to_i).inject(&:+).div(2)
end


# coding: utf-8

n = gets.to_i
n.times {
    s = gets.chomp.split(//).map(&:to_i)
    total = s.length
    ones = s.count(1)
    zeroes = s.count(0)
    # 0->1과, 1->0으로 모두 뒤집는 경우에 대한 기본해(이게 최소일 수도 있음)
    min = [ones, zeroes].min
    flip_ones, flip_zeroes = 0, 0

    for i in s
        if i == 0
            flip_zeroes += 1
            zeroes -= 1
        else
            flip_ones += 1
            ones -= 1
        end
        # 자기 자리를 계속 바꿔가며 나아간다
        # 0으로 나아갈 경우-> 뒷부분을 전부 1로 만들기 위해 바꿔야할 0의 수
        # 1로 나아갈 경우 -> 뒷부분을 전부 0으로 만들기 위해 바꿔야할 1의 수
        # 항상 미니멈을 유지하자
        # dp 응용 같음
        min = [min, flip_zeroes + ones, flip_ones + zeroes].min
    end
    puts min
}

gets.to_i.times do
    s = gets.chomp
    ones = s.each_char.inject([]) do |arr, c|
        arr.push arr[-1] || 0
        arr[-1] += 1 if c == '1'
        arr
    end
    all = [ones[-1], s.length - ones[-1]].min
    min = s.length.times.inject(all) do |min, i|
        right_zero = s.length - (i + 1) - (ones[-1] - ones[i])
        left_zero = i + 1 - ones[i]
        right_one = ones[-1] - ones[i]
        min = [min, ones[i] + right_zero, left_zero + right_one].min
    end
    puts min
end

def solve(num)
  one_count = 0
  zero_count = 0
  ones_first = []
  zeroes_first = []

  num.each_char.with_index do |char, i|
    one_count += 1 if char == "1"
    zero_count += 1 if char == "0"
    ones_first[i] = zero_count
    zeroes_first[i] = one_count
  end

  i = num.length - 1
  ones_last = []
  ones_last[i] = 0
  zeroes_last = []
  zeroes_last[i] = 0
  min = Float::INFINITY

  while i > 0
    zeroes_last[i - 1] = zeroes_last[i] + zeroes_first[i] - zeroes_first[i-1]
    ones_last[i - 1] = ones_last[i] + ones_first[i] - ones_first[i-1]
    min = [min, zeroes_first[i] + ones_last[i], ones_first[i] + zeroes_last[i]].min
    i -= 1
  end

  [min, zeroes_first[0] + ones_last[0], ones_first[0] + zeroes_last[0]].min
end

amount = gets.chomp.to_i
amount.times do
  input = gets.chomp
  puts solve(input)
end

def solve(num)
  count = 0
  zeroes_first =  num.each_char.map do |char|
    count += 1 if char == "1"
    count
  end
  count = 0
  i = num.length - 1
  ones_last = []
  ones_last[i] = 0

  while i > 0
    count += 1 if num[i] == "0"
    ones_last[i - 1] = count
    i -= 1
  end
  count = 0
  ones_first =  num.each_char.map do |char|
    count += 1 if char == "0"
    count
  end
  count = 0
  i = num.length - 1
  zeroes_last = []
  zeroes_last[i] = 0
  while i > 0
    count += 1 if num[i] == "1"
    zeroes_last[i - 1] = count
    i -= 1
  end

  i = 0
  min = Float::INFINITY
  while i < num.length
    min = [min, zeroes_first[i] + ones_last[i], ones_first[i] + zeroes_last[i]].min
    i += 1
  end
  min
end

amount = gets.chomp.to_i
amount.times do
  input = gets.chomp
  puts solve(input)
end

# coding: utf-8

n = gets.to_i
n.times {
    s = gets.chomp.split(//).map(&:to_i)
    i = 0
    len = s.length
    flipped = 0
    # 010 -> remove 1's
    # 101 -> removes 0's
    while i < len-1
        base = 0
        flip = 1
        if s[i] == 0
            base = 0
            flip = 1
        else
            base = 1
            flip = 0
        end

        p1 = s[i..-1].index(flip)
        if p1 != nil
            p1 += i #compensation
            p2 = s[p1..-1].index(base)
            if p2 != nil
                p2 += p1 # compensation
                j = p1
                while s[j] == flip
                    s[j] = base
                    j += 1
                    flipped += 1
                end
                i = p2
                next
            end
        end

        i += 1
    end
    puts flipped
}

# coding: utf-8

n = gets.to_i
n.times {
    s = gets.split(//).map(&:to_i)
    i = 0
    len = s.length
    flipped = 0
    # 010 -> remove 1's
    # 101 -> removes 0's
    while i < len-1
        base = 0
        flip = 1
        if s[i] == 0
            base = 0
            flip = 1
        else
            base = 1
            flip = 0
        end

        p1 = s[i..-1].index(flip)
        if p1 != nil
            p1 += i #compensation
            p2 = s[p1..-1].index(base)
            if p2 != nil
                p2 += p1 # compensation
                j = p1
                while s[j] == flip
                    s[j] = base
                    j += 1
                    flipped += 1
                end
                i = p2
                next
            end
        end

        i += 1
    end
    puts flipped
}


def ones(a)
	c = 0
	while a > 0
		c += a % 2
		a /= 2
	end
	c
end

n,m  = gets.split(" ").map(&:to_i)
arr = []
n.times do
	arr << gets.chomp
end
enemy = []

m.times do
	a,b = gets.chomp.split(" ")
	val = 0
	val |= 1 << arr.index(a)
	val |= 1 << arr.index(b)
	enemy << val
end

# puts "here #{arr}"
best = 0

(0..(2**n)).each do |i|
	ans = true
	enemy.each do |en|
		if (en & i) == en
			ans = false
			# puts "ere"
		end
		# puts "fdf"
	end
	# puts "ere"
	if ans && (ones(i) > ones(best))
		best = i
	end
	# puts "dsafds"
end
# puts "dasf"
puts ones(best)
list  = []
# puts "#{arr}"
# puts "#{best.to_s(2).rjust(n,'0')}"
best.to_s(2).rjust(n,'0').each_char.with_index do |c,i|
	if c == '1'
		list << arr[n-i - 1]
	end
end
puts list.sort.join("\n")

n, m = gets.split.map &:to_i
p = []
$e = Array.new(n,0)

n.times {
  name = gets.chomp
  p << name
}
m.times {
  p0, p1 = gets.chomp.split
  i = p.index p0
  j = p.index p1
  $e[i] = $e[i] + (1<< j)
  $e[j] = $e[j] + (1<< i)
}

$max = []
def f(mems, mask, i, n)
  $max = mems.dup if mems.length > $max.length
  i.upto(n-1) {|j|
    if ($e[j] & mask) == 0
      mems << j
      f(mems, mask + (1<< j), j+1, n)
      mems.pop
    end
  }
end
f([], 0, 0, n)
puts $max.length
$max.map {|i| p[i] }.sort.each { |p| puts p}

#!/usr/bin/env ruby

def main
  n, m = STDIN.gets.split.map!{|x| x.to_i}
  names = []
  n.times {
    name = STDIN.gets.rstrip
    names.push(name)
  }

  ng = (0...n).map {Array.new(n)}
  m.times {
    a, b = STDIN.gets.split
    a = names.find_index(a)
    b = names.find_index(b)
    ng[a][b] = ng[b][a] = true
  }

  ans = 0
   (1 << n).times {|set|
    if  ans.to_s(2).count('1') >= set.to_s(2).count('1')
      next
    end

    ok = true
    n.times do |i| if (set >> i & 1) != 0
        i.times do |j| if (set >> j & 1) != 0
            if ng[i][j]
              ok = false
            end
          end
        end
      end
    end
    if ok
      ans = set
    end
  }

  puts ans.to_s(2).count('1')
  ans_names = []
  n.times {|i|
    if (ans >> i & 1) == 1
      ans_names.push(names[i])
    end
  }
  ans_names.sort!.each do |x|
    puts x
  end

end

if __FILE__ == $0
  main
end

n, m = gets.split.map &:to_i
p = []
$e = []
n.times { $e << Array.new(n, false) }

n.times {
  name = gets.chomp
  p << name
}
m.times {
  p0, p1 = gets.split
  i = p.index p0
  j = p.index p1
  $e[i][j] = $e[j][i] = true
}

$max = []
def f(mems, i, n)
  if i == n
    $max = mems.dup if mems.length > $max.length
  else
    i.upto(n-1) {|j|
      if not mems.any? { |k| $e[j][k] }
        mems << j
        f(mems, j+1, n)
        mems.pop
      end
    }
  end
end
f([], 0, n)
puts $max.length
puts $max.map { |i| p[i]}.sort

n, m = gets.split.map &:to_i
p = []
$e = Array.new(n,0)

n.times {
  name = gets.chomp
  p << name
}
m.times {
  p0, p1 = gets.chomp.split
  i = p.index p0
  j = p.index p1
  $e[i] = $e[i] + (1<< j)
  $e[j] = $e[j] + (1<< i)
}

$max = []
def f(mems, mask, i, n)
  if i == n
    $max = mems.dup if mems.length > $max.length
  else
    i.upto(n-1) {|j|
      if ($e[j] & mask) == 0
        mems << j
        f(mems, mask + (1<< j), j+1, n)
        mems.pop
      end
    }
  end
end
f([], 0, 0, n)
puts $max.length
$max.map { |i| p[i]}.sort.each { |p| puts p}

n, m = gets.split.map &:to_i
p = []
$e = Array.new(n,0)

n.times {
  name = gets.chomp
  p << name
}
m.times {
  p0, p1 = gets.chomp.split
  i = p.index p0
  j = p.index p1
  $e[i] = $e[i] + (1<< j)
  $e[j] = $e[j] + (1<< i)
}

$max = []
def f(mems, mask, i, n)
  if i == n
    $max = mems.dup if mems.length > $max.length
  else
    i.upto(n-1) {|j|
      if ($e[j] & mask) == 0
        mems << j
        f(mems, mask + (1<< j), j+1, n)
        mems.pop
      end
    }
  end
end
f([], 0, 0, n)
puts $max.length
puts $max.map { |i| p[i]}.sort

n, m = gets.split.map &:to_i
p = []
$e = Array.new(n,0)

n.times {
  name = gets.chomp
  p << name
}
m.times {
  p0, p1 = gets.chomp.split
  i = p.index p0
  j = p.index p1
  $e[i] = $e[i] + (1<< j)
  $e[j] = $e[j] + (1<< i)
}

$max = []
def f(mems, mask, i, n)
  if i == n
    $max = mems.dup if mems.length > $max.length
  else
    i.upto(n-1) {|j|
      if ($e[j] & mask) == 0
        mems << j
        f(mems, mask + (1<< j), j+1, n)
        mems.pop
      end
    }
  end
end
f([], 0, 0, n)
puts $max.length
$max.map {|i| p[i] }.sort_by(&:downcase).each { |p| puts p}

n,m  = gets.split(" ").map(&:to_i)
arr = []
n.times do
	arr << gets.chomp
end
enemy = {}

m.times do
	a,b = gets.chomp.split(" ")
	enemy[a] ||= []
	enemy[b] ||= []
	enemy[a] << b
	enemy[b] << a
end
# ans = arr.dup
# puts "#{enemy}"
# puts "here #{enemy.key(:Pasha)}"
enemy_vals = enemy.sort_by{|x,v| -v.length}
# puts "#{}"
enemy_vals.each do |v|
	k = v[0]
	val = enemy[k]
	# puts "arr #{arr}"
	if val.length > 0
		# puts "#{enemy}"
		arr -= [k]
		val.each{|x| enemy[x] -= [k] }
	end
end
puts arr.length
puts "#{arr.sort.join("\n")}"

n,m  = gets.split(" ").map(&:to_i)
arr = []
n.times do
	arr << gets.chomp
end
enemy = {}

m.times do
	a,b = gets.chomp.split(" ")
	enemy[a] ||= []
	enemy[b] ||= []
	enemy[a] << b
	enemy[b] << a
end
# ans = arr.dup
enemy.each do |k,v|
	if v.length > 0
		arr -= [k]
		v.each{|x| enemy[x] -= [k] }
	end
end
puts arr.length
puts "#{arr.sort.join("\n")}"

def ones(a)
	c = 0
	while a > 0
		c += a % 2
		a /= 2
	end
	c
end

n,m  = gets.split(" ").map(&:to_i)
arr = []
n.times do
	arr << gets.chomp
end
enemy = []

m.times do
	a,b = gets.chomp.split(" ")
	val = 0
	val |= 1 << arr.index(a)
	val |= 1 << arr.index(b)
	enemy << val
end

# puts "here #{arr}"
best = 0

(0..(2**n)).each do |i|
	ans = true
	enemy.each do |en|
		if (en & i) == en
			ans = false
			# puts "ere"
		end
		# puts "fdf"
	end
	# puts "ere"
	if ans && (ones(i) > ones(best))
		best = i
	end
	# puts "dsafds"
end
# puts "dasf"
puts ones(best)
list  = []
# puts "#{best}"
best.to_s(2).each_char.with_index do |c,i|
	if c == '1'
		list << arr[i]
	end
end
puts list.sort.join("\n")

def ones(a)
	c = 0
	while a > 0
		c += a % 2
		a /= 2
	end
	c
end

n,m  = gets.split(" ").map(&:to_i)
arr = []
n.times do
	arr << gets.chomp
end
enemy = []

m.times do
	a,b = gets.chomp.split(" ")
	val = 0
	val |= 1 << arr.index(a)
	val |= 1 << arr.index(b)
	enemy << val
end

# puts "here #{arr}"
best = 0

(0..(2**n)).each do |i|
	ans = true
	enemy.each do |en|
		if (en & i) == en
			ans = false
			# puts "ere"
		end
		# puts "fdf"
	end
	# puts "ere"
	if ans && (ones(i) > ones(best))
		best = i
	end
	# puts "dsafds"
end
# puts "dasf"
puts ones(best)
list  = []
# puts "#{best}"
best.to_s(2).each_char.with_index do |c,i|
	if c == '1'
		list << arr[n-i - 1]
	end
end
puts list.sort.join("\n")

#!/usr/bin/env ruby

def main
  n, m = STDIN.gets.split.map!{|x| x.to_i}
  names = []
  n.times {
    name = STDIN.gets.rstrip
    names.push(name)
  }

  ng = (0...n).map {Array.new(n)}
  m.times {
    a, b = STDIN.gets.split
    a = names.find_index(a)
    b = names.find_index(b)
    ng[a][b] = ng[b][a] = true
  }

  ans = 0
  (1 << n).times {|set|
    if  ans.to_s(2).count('1') >= set.to_s(2).count('1')
      next
    end

    ok = true
    n.times do |i|
      i.times do |j|
        if (set >> i & 1) != 0 && (set >> j & 1) != 0
          if ng[i][j]
            ok = false
          end
        end
      end
    end
    if ok
      ans = set
    end
  }
  puts ans.to_s(2).count('1')
  n.times {|i|
    if (ans >> i & 1) == 1
      puts names[i]
    end
  }

end

if __FILE__ == $0
  main
  STDIN.gets;
end


$stdin=File.open('input.txt')
$stdout=File.open('output.txt','w')
n=gets.to_i
s=gets
(n/2).times{|i|
	x,y=i,i+n/2
	x,y=y,x if s[x]=='R'&&s[y]=='L'
	puts [x+1,y+1]*' '
}

$stdin=open("input.txt")
$stdout=open("output.txt","w")
n=gets.to_i/2
a=gets
i=0
n.times do
    k=a[i]=='L'?0:1
    puts [i+1+n*k, i+1+n*(1-k)]*' '
    i+=1
end

input = File.open("input.txt", "r")
output = File.open("output.txt", "w")

n = input.gets.to_i
lr = " "+input.gets.strip

for i in 1..n-2
  desk = Array.new(2)
  if lr[i] != "0"
    if lr[i] == "L"
	  desk[0] = i
	  desk[1] = n/2+i
	else
	  desk[0] = n/2+i
	  desk[1] = i
	end
	lr[i] = "0"
	lr[n/2+i] = "0"
	output.puts "#{desk[0]} #{desk[1]}"
  end
end

input.close
output.close

input = File.open("input.txt", "r")
output = File.open("output.txt", "w")

n = input.gets.to_i
lr = " "+input.gets.strip

for i in 1..n/2
  desk = Array.new(2)
  if lr[i] == "L"
	desk[0] = i
	desk[1] = n/2+i
  else
	desk[0] = n/2+i
	desk[1] = i
  end
  output.puts "#{desk[0]} #{desk[1]}"
end

input.close
output.close

$stdin=open("input.txt")
$stdout=open("output.txt","w")
n=gets.to_i
a=gets.chomp.split('').unshift(0)
for i in 1..n/2
	puts "#{i} #{i+n/2}" if a[i]=='L'
	puts "#{i+n/2} #{i}" if a[i]=='R'
end

if ENV['USER'] != 'watashi'
  ARGV << 'input.txt'
  $> = open('output.txt', 'w')
end

n = gets.to_i / 2
m = gets
puts [*1..n].map{|i| (m[i-1] == 'L' ? [i, n+i] : [n+i, i]) * ' '} * "\n"

$stdin = File.new( "input.txt", 'r' )
$stdout = File.new( "output.txt", 'w' )

n = gets.strip.to_i
stds = gets.strip.split("")
mid = n / 2
i = 0
(mid...n).each do |j|
    if stds[i] == 'R' && stds[j] == 'L'
        puts "#{j + 1} #{i + 1}"
    else
        puts "#{i + 1} #{j + 1}"
    end
    i += 1
end

n = 0
arr = []
open("input.txt") {|f|
  n = f.gets.to_i
  arr = f.gets.to_s.chomp.split(//)
}
File.open("output.txt", "w"){|f|
(n/2).times do |i|
  if arr[i] == "R" && arr[i+n/2] == "L"
    f.write "#{i+n/2+1} #{i+1}\n"
  else
    f.write "#{i+1} #{i+n/2+1}\n"
  end
end
}

$stdin=open("input.txt")
$stdout=open("output.txt","w")
n=gets.to_i/2
a=gets
i=0
n.times do
    if a[i]=='L' then
        puts [i+1,i+1+n]*' '
    else
        puts [i+1+n,i+1]*' '
    end
    i+=1
end

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

n = n.to_i
res = []

arr = hand.split("")

nos = [*(0..n-1).step(2),*(1..n-1).step(2)]

nos.each_slice(2) do |a|
	o = arr[a[0]]
	e = arr[a[1]]
	odd_ind = a[0]
	even_ind = a[1]
	odd_ind,even_ind = even_ind,odd_ind if o != e && o != "R"
	res << [even_ind+1,odd_ind+1]
end
File.open("output.txt", 'w') { |file|
	res.each do |a|
		file.puts(a.join(" "))
	end
}

if ENV['USER'] != 'watashi'
  $stdin = open('input.txt')
  $> = open('output.txt', 'w')
end

n = gets.to_i / 2
m = gets
puts [*1..n].map{|i| (m[i-1] == 'L' ? [i, n+i] : [n+i, i]) * ' '} * "\n"

n = gets.to_i
arr = gets.to_s.chomp.split(//)
(n/2).times do |i|
  if arr[i] == "R" && arr[i+n/2] == "L"
    puts "#{i+n/2+1} #{i+1}"
  else
    puts "#{i+1} #{i+n/2+1}"
  end
end

n=0
a=[]
open("input.txt") do |f|
    n=f.gets.to_i
    a=f.gets.chomp.split('')
end

res=Array.new(n/2).map!{Array.new(2)}
l=r=0
a.each_with_index do |v,i|
    if v=='L' then
        if l<n/2 then
            res[l][0]=i+1
            l+=1
        else
            res[r][1]=i+1
            r+=1
        end
    else
        if r<n/2 then
            res[r][1]=i+1
            r+=1
        else
            res[l][0]=i+1
            l+=1
        end
    end
end
open("output.txt","w") do |f|
    res.each do |v|
        f.puts v*' '
    end
end

n=0
a=[]
open("input.txt") do |f|
    n=f.gets.to_i/2
    f.gets.chomp.split('').each_with_index do |v,i|
        a.push([v,i+1])
    end
end

a.sort!
res=Array.new(n).map!{Array.new(2)}
a.each_with_index do |v,i|
    res[i%n][i/n]=v[1]
end
open("output.txt","w") do |f|
    res.each do |v|
        f.puts v*' '
    end
end

n = gets.to_i
# res = []

# arr = gets.chomp.split("")

# nos = [*(0..n-1).step(2),*(1..n-1).step(2)]

# nos.each_slice(2) do |a|
# 	o = arr[a[0]]
# 	e = arr[a[1]]
# 	odd_ind = a[0]
# 	even_ind = a[1]
# 	odd_ind,even_ind = even_ind,odd_ind if o != e && o != "R"
# 	res << [even_ind+1,odd_ind+1]
# end
# res.each do |a|
# 	puts a.join(" ")
# end

# p ENV
var = %w(RUBY_COPYRIGHT RUBY_ENGINE RUBY_PLATFORM RUBY_REVISION RUBY_DESCRIPTION RUBY_PATCHLEVEL RUBY_RELEASE_DATE RUBY_VERSION)
var.map{|_| puts "#{_} = #{eval(_)}"}

if ENV['USER'] != 'watashi'
  $stdin = open('input.txt')
  $> = open('output.txt', 'w')
end

n = gets.to_i
m = gets.scan(/[LR]/).zip([*1..n]).sort
n /= 2
n.times do |i|
  puts "#{m[i][1]} #{m[n + i][1]}"
end


a=gets.chomp
e=[0,0]
o=[0,0]
ea=oa=0
a.size.times{|i|
	t=a[i].ord-'a'.ord
	if i%2==0
		e[t]+=1
		ea+=o[t]
		oa+=e[t]
	else
		o[t]+=1
		ea+=e[t]
		oa+=o[t]
	end
}
puts "#{ea} #{oa}"