h = Hash.new(0)
gets.to_i.times{
  h[gets] += 1
}
p h.values.max

n = gets.to_i
pr = [-1, -1]
tmp, max = 0, 1
n.times do
  ar = gets.split(' ').map(&:to_i)
  ar == pr ? tmp+=1 : tmp = 1
  max = tmp if tmp > max
  pr = ar
end
puts max

a=Array.new(24*60+1,0)

#print a
n=gets.chomp.to_i
i=0
for i in (0...n)
b=gets.chomp.split(" ")
a[(b[0].to_i*60+b[1].to_i)]+=1
end
puts a.max

a=Hash.new(0)

#print a
n=gets.chomp.to_i
i=0
for i in (0...n)
b=gets.chomp.split(" ")
a[(b[0].to_i*60+b[1].to_i)]+=1
end
puts a.values.max

n = gets.to_i

t = {}
n.times do |i|
    s = gets.chomp.sub(" ", ":")
    if t[s] then
        t[s] += 1
    else
        t[s] = 1
    end
end

t = t.sort{|a,b| a[1]<=>b[1]}

p t[-1][1]

I =->{gets.split.map &:to_i}
RD =->{gets.to_i}

aa = bb = -1; cnt = res = 1; RD[].times{
	a, b = I[]
	if a == aa && b == bb
		cnt += 1; res = cnt if (cnt > res)
	else
		cnt = 1; aa,  bb = a, b
	end
}

p res

n = gets.to_i
hLast, mLast = -1, -1
max = 1
count = 1
n.times do
  h, m = gets.split(" ").map(&:to_i)
  if hLast == h && mLast == m
    count += 1
  else
    count = 1
  end
  max = count if count > max
  hLast, mLast = h, m
end

puts max

n = gets.chomp.to_i

hash = {}
n.times do
  v = gets.chomp
  if hash.has_key? v
    hash[v] += 1
  else
    hash[v] = 1
  end
end

puts hash.values.max

n = gets.to_i
cus = Array.new(24)
for i in 0...24
  cus[i] = Array.new(60, 0)
end
n.times do
  a, b = gets.split.map(&:to_i)
  cus[a][b] += 1
end
m = Array.new
for i in 0...24
  m << cus[i].max
end
print m.max

times = []
people = gets.chomp.to_i
people.times { time = gets
  times.push(time) }

b = Hash.new(0)
times.each do |element|
  b[element] += 1
end

p b.values.max

hash = Array.new(24*60) {0}
f = Proc.new{ |a,b| a * 60 + b }
gets.to_i.times {
  hash[f.call gets.split.map(&:to_i)] += 1
}
puts hash.max

a = Hash.new(0)
gets.to_i.times { a[gets.strip] += 1 }
puts a.values.max

n = gets.chomp.to_i
a = []
n.times do
  a << gets
end

h = {}

a.each_with_index do |t, i|
  h[t] = (h[t].nil? ? 1 : h[t]+1)
end

puts h.values.max

n = gets.to_i
cox = []
for i in 0..n-1
  cox[i] = gets.split.map(&:to_i)
end

c   = 0
fox = []
cox.sort!
for i in 0..n-1
  if cox[i] == cox[i+1]
    c+=1
  else
    fox.push(c)
    c = 0
  end
end

puts fox.max + 1

n = $stdin.gets.to_i
r = []
n.times {r << $stdin.readline.chop}

class Array # use self.partition

    def complect_and_count
        self.dup.complect_and_count!
    end

    def complect_and_count!
        a = self.shift
        h = {sym: a, count: 1}
        result, k = [h], 1
        while self.size>0
            b = self.shift
            if a != b
                h[:count] = k
                h =  {sym: b, count: 1}
                result << h
                k = 0
            end
            a = b
            k += 1
        end
        h[:count]=k
        result
    end

end

t = r.complect_and_count
m = t.max_by {|x| x[:count]}

puts m[:count]

arr = []
gets.to_i.times do
  arr << gets.chomp
end
hash = Hash.new(0)
arr.each { |v| hash[v] += 1 }
puts hash.values.max

t = []
max_t = 1
gets.to_i.times do |i|
  in_time = gets.chomp.split.map(&:to_i)
  tt = in_time[0]*60 + in_time[1]
  if t[tt].nil?
    t[tt] = 1
  else
    t[tt] += 1
  end
  max_t = t[tt] if t[tt] > max_t
end
puts max_t

n=gets.to_i
n-=1
s=gets.chomp
ans=1
c=1
n.times{
	s2=gets.chomp
	if s==s2 then
		c=c+1
	else
		c=1
		s=s2
	end
	ans=[ans,c].max
}
puts ans

n = gets.to_i
b = []
x,y = 1,0
n.times do |i|
	 a = gets.split.map(&:to_i)
	 if a == b
	 	y += 1
	 	x = [x,y].max
	 else
	 	y = 1
	 end
	 b = a
end
p x

i = gets.to_i; x = Hash.new(0);
i.times { x[gets.chomp] += 1 }
puts x.values.max

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i
ph, pm = -1, -1
cons = 0
max_cons = 0
n.times {
    h, m = gets.split.map(&:to_i)
    if h != ph || m != pm
        if max_cons < cons
            max_cons = cons
        end
        cons = 0
        ph, pm = h, m
    end

    cons += 1

    if max_cons < cons
        max_cons = cons
    end
}
puts max_cons

h = Hash.new(0)
gets.to_i.times {
    h[gets.chomp] += 1
}

p h.values.max

n = gets.chomp.to_i
cashiers = []
for i in (0..n-1)
	cashiers[i] = gets.chomp
end

cashes = 1
free = 0
for i in (0..cashiers.size - 2)
	if cashiers[i] == cashiers[i+1] && free <= 1
		cashes += 1
	else
		if cashiers[i] == cashiers[i+1]
			free -= 1
		else
			free = cashes
		end
	end
end

print cashes

def max_cash(arrival_times)
    max = 0
    counter = 0
    prev_value = arrival_times[0]

    arrival_times.each do |arrival_time|
        if arrival_time == prev_value
            counter += 1
        else
            counter = 1
        end
        prev_value = arrival_time
        max = [max, counter].max
    end

    max
end

n = gets.chomp.to_i
clientes_arrival_time = []
n.times do
    clientes_arrival_time << gets.chomp.gsub(" ", ":")
end

puts max_cash(clientes_arrival_time)

def max_cash(arrival_times)
    arrival_time_counter = Hash.new { |k, v| k[v] = 0 }

    arrival_times.each do |arrival_time|
        arrival_time_counter[arrival_time] += 1
    end

    arrival_time_counter.values.max
end

n = gets.chomp.to_i
clientes_arrival_time = []
n.times do
    clientes_arrival_time << gets.chomp.gsub(" ", ":")
end

puts max_cash(clientes_arrival_time)

n = gets.to_i
a = Hash.new(0)
n.times do
  a[gets.chomp] += 1
end
puts a.values.max

n = gets.to_i
a = Array.new(24){Array.new(60, 0)}
n.times do
  h, m = gets.split.map(&:to_i)
  a[h][m] += 1
end
aa = []
24.times do |i|
  aa[i] = a[i].max
end
p aa.max

n = gets.chomp.to_i
d = {}
n.times do
  h, m = gets.chomp.split(" ").map(&:to_i)
  d[[h, m]] = d.fetch([h, m], 0) + 1
end
puts d.values.max

n = gets.to_i
c = {}
n.times do
    h, m = gets.split.map{|x| x.to_i}
    c[[h, m]] = 0 unless c[[h, m]]
    c[[h, m]] += 1
end
puts c.max{|a, b| a[1] <=> b[1]}[1]

n = gets.to_i

time = Hash.new(0)
max = 0
n.times do |_|
  h, m = gets.split(" ").map(&:to_i)
  max = [max, time[m + h*60] += 1].max
end

p max

n = gets.to_i

time = Hash.new(0)
n.times do |_|
  h, m = gets.split(" ").map(&:to_i)
  time[h*60 + m] += 1
end

p time.max { |a, b| a[1] <=>  b[1]}[1]

n = gets.to_i

time = Hash.new(0)
n.times do |_|
  h, m = gets.split(" ").map(&:to_i)
  time[h*60 + m] += 1
end

p time.values.max

n = gets.to_i

time = Hash.new(0)
n.times do |_|
  h, m = gets.split(" ").map(&:to_i)
  time[h*60 + m] += 1
end

p time.max_by { |k, v| v}[1]

n = gets.to_i

time = Hash.new(0)
max = 0
n.times do |_|
  h, m = gets.split(" ").map(&:to_i)
  m += h*60
  time[m] += 1
  max = [max, time[m]].max
end

p max

i = gets.to_i; x = Hash.new(0);
i.times { x[gets.chomp] += 1 }
puts x.max[1]

customers = gets.chomp.to_i
time = []
for i in (0..customers-1)
	t = gets.chomp
	time[i] = t.split
	time[i] = (time[i][0].to_i * 60) + time[i][1].to_i
end

cashiers = 1
for i in (0..customers-2)
	if time[i] == time[i+1] || (time[i]).abs - (time[i+1]).abs == 1
		cashiers += 1
	end
end

print cashiers

n = gets.chomp.to_i
cashiers = []
for i in (0..n-1)
	cashiers[i] = gets.chomp
end

cashes = 0
available = 0
for i in (0..n-2)
	if cashiers[i] == cashiers[i+1]
		if cashes == 0 && available == 0
			cashes += 2
		elsif available == 0
			cashes += 1
		end
	else
		available = cashes
	end
end
if cashes == 0
	print 1
else
	print cashes
end

n = gets.to_i
a = Array.new(24, Array.new(60, 0))
n.times do
  h, m = gets.split.map(&:to_i)
  a[h][m] += 1
end
aa = []
24.times do |i|
  aa[i] = a[i].max
end
p aa.max

n = gets.to_i
a = Array.new(24, Array.new(60, 0))
n.times do
  h, m = gets.split.map(&:to_i)
  a[h][m] += 1
end
p a.max.max

n = gets.to_i
cox = []
for i in 0..n-1
  cox[i] = gets.split.map(&:to_i)
end

k = 1

puts k + (n - cox.uniq.size)

arr = []
gets.to_i.times do
  arr << gets.chomp
end
puts 1 + (arr.size - arr.uniq.size)

n = gets.to_i
b = []
x,y = 0,0
n.times do |i|
	 a = gets.split.map(&:to_i)
	 if a == b
	 	y += 1
	 	x = [x,y].max
	 else
	 	y = 1
	 end
	 b = a
end
p x


n=gets.to_i
a=gets.split.map(&:to_i)
gets.to_i.times do
    w,h=gets.split.map(&:to_i)
    t=[a[0], a[w-1]].max
    puts t
    a[0]=t+h
end

n = gets.to_i #kol stypenei
a = gets.split(' ').collect!{|i| i.to_i} #visota stypenei
m = gets.to_i #kol korobok

w = []
h = []
m.times do
	t=gets.split(' ')
	w << t[0].to_i
	h << t[1].to_i
end

x = []

# m.times do |i|
# 	wid = w[i]-1
# 	hei = h[i]
# 	x << a[wid]
# 	newhei = a[wid]+hei
# 	n.times do |j|
# 		if newhei > a[j]
# 			a[j] = newhei
# 		else
# 			break
# 		end
# 	end
# end
newhei = 0
m.times do |i|
	wid = w[i]-1
	if a[wid] > newhei
		x << a[wid]
		newhei = a[wid]+h[i]
	else
		x << newhei
		newhei += h[i]
	end
end

puts x

n = gets.to_i #kol stypenei
a = gets.split(' ').collect!{|i| i.to_i} #visota stypenei
m = gets.to_i #kol korobok

w = []
h = []
m.times do
	t=gets.split(' ')
	w << t[0].to_i
	h << t[1].to_i
end

x = []

m.times do |i|
	wid = w[i]-1
	hei = h[i]
	x << a[wid]
	newhei = a[wid]+hei
	(0..(wid)).each do |j|
		a[j] = newhei
	end
end

puts x


main = -> {
  t = int
  t.times do
      n = int
      a = int1s
      ans = 0
      n.times do |i|
          ans += (0 ... i).count { |j| a[j] >= a[i] }
      end
      puts ans
  end
}

DEBUG = true
MOD = 10**9+7
$yesno = %w(No Yes)
YesNo = %w(No Yes)
YESNO = %w(NO YES)
INF = 10**18

class Segtree
  def initialize(size, id, fill = id, &op)
    if size.is_a? Array
      @n = 1 << (size.size - 1).bit_length
      @a = [fill] * (2 * @n)
      @a[@n, @n] = size
      (@n - 1).downto(1) do |i|
        @a[i] = op[@a[i * 2], @a[i * 2 + 1]]
      end
    else
      @n = 1 << (size - 1).bit_length
      @a = [fill] * (2 * @n)
    end
    @a[0], @op = id, op
  end
  def [](i) @a[i + @n] end
  def []=(i, x)
    @a[i += @n] = x
    @a[i] = @op[@a[i * 2], @a[i * 2 + 1]] while (i >>= 1) > 0
  end
  def prod(l, r)
    return @a[0] if l >= r
    x = prod(l + 1 >> 1, r >> 1)
    x = @op[@a[@n + l], x] if l & 1 == 1
    x = @op[x, @a[@n + r - 1]] if r & 1 == 1
    x
  end
end

require "set"
require "prime"

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 strings; gets.to_s.split 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 YesNo t; puts YesNo[t] 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 * y.mod_inv(mod) % mod end
  def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, 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
  def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; 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[]

def merge_sort(l, r)
  return if r <= l + 1
  m = (l + r) / 2

  merge_sort(l, m)
  merge_sort(m, r)

  i = l
  j = m
  o = l
  while i < m && j < r
    if $xs[j] <= $xs[i]
      $inversions += m - i
      $tmp[o] = $xs[j]
      j += 1
    else
      $tmp[o] = $xs[i]
      i += 1
    end
    o += 1
  end

  while i < m
    $tmp[o] = $xs[i]
    i += 1
    o += 1
  end

  while j < r
    $tmp[o] = $xs[j]
    j += 1
    o += 1
  end

  $xs[l...r] = $tmp[l...r]
end

def inversions(xs)
  $xs = xs
  $tmp = Array.new(xs.size)
  $inversions = 0
  merge_sort(0, xs.size)
  $inversions
end

gets.to_i.times do
  gets
  xs = gets.split.map(&:to_i)
  p inversions(xs)
end


# http://codeforces.com/contest/545/problem/B
def hd(a, b)
  (0...a.length).map do |idx|
    a[idx] == b[idx] ? 0 : 1
  end.inject(0, :+)
end

def modify(a, b, n)
  (0...a.length).map do |idx|
    if a[idx] == b[idx]
      a[idx]
    else
      if n > 0
        n -= 1
        a[idx]
      else
        b[idx]
      end
    end
  end.join
end

s = gets.chomp.chars
t = gets.chomp.chars
d = hd(s, t)

if d.even?
  puts modify(s, t, d / 2)
else
  puts 'impossible'
end

s = gets
t = gets
z = s.split('').zip(t.split(''))
dist = 0
z.each do |si, ti|
    dist += 1 if si != ti
end
if dist.odd?
    puts 'impossible'
else
    dist /= 2
    z.each do |si, ti|
        if si == ti
            print si
        else
            if dist > 0
                dist -= 1
                print si
            else
                print ti
            end
        end
    end
    puts
end

s=gets.chomp
t=gets.chomp

c1=0
s.size.times{|i|
	c1+=1 if s[i]!=t[i]
}

if c1%2==1 then
	puts "impossible"
else
	ans=""
	f=true
	s.size.times{|i|
		if s[i]==t[i] then
			ans<<s[i]
		else
			if f then
				ans<<s[i]
			else
				ans<<t[i]
			end
			f=!f
		end
	}
	puts ans
end

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

# b.rb

def main()
    s = gets.chomp
    t = gets.chomp
    v = 0
    s.size.times do |i|
        if s[i] != t[i] then
            s[i] = [s[i],t[i]][v % 2]
            v += 1
        end
    end
    if (v % 2) == 0 then
        puts s
    else
        puts "impossible"
    end
end

if __FILE__ == $0 then
    main()
end

A = gets.strip.chars
B = gets.strip.chars

diff = 0
answer = []
A.zip(B).map do |a, b|
  if a == b
    answer.push a
  else
    if diff >= 0
      answer.push a
      diff -= 1
    else
      answer.push b
      diff += 1
    end
  end
end

if diff != 0
  puts "impossible"
else
  puts answer.join
end

a, b = gets, gets
n, c = a.length, 0
(0...n).each do |i|
	if a[i] != b[i]
		c += 1
	end
end

if c & 1 != 0
	puts "impossible"
else
	p, i = 0, 0
	while i < n do
		if p >= c/2
			break
		end
		if a[i] != b[i]
			print b[i]
			p += 1
		else
			print a[i]
		end
		i += 1
	end
	puts a[i..-1]
end

a, b = gets, gets
n, c = a.length, 0
(0...n).each do |i|
	if a[i] != b[i]
		c += 1
	end
end

if c & 1 != 0
	puts "impossible"
else
	s, p, i = '', 0, 0
	while i < n do
		if p >= c/2
			break
		end
		if a[i] != b[i]
			s << b[i]
			p += 1
		else
			s << a[i]
		end
		i += 1
	end
	s << a[i..-1]
	puts s
end

# http://codeforces.com/contest/545/problem/B
def hd(a, b)
  (0...a.length).map do |idx|
    a[idx] == b[idx] ? 0 : 1
  end.inject(0, :+)
end

def pad(chars)
  (0...chars.length).to_a.reverse.each do |idx|
    if chars[idx] == '0'
      chars[idx] = '1'
      break
    end
  end

  chars.join
end

s = gets.chomp
t = gets.chomp
s, t = [s, t].sort
d = hd(s.chars, t.chars)

if d.even?
  puts pad(s.chars)
else
  puts 'impossible'
end

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

# b.rb

def main()
    s = gets.chomp
    t = gets.chomp
    v = 0
    s.size.times do |i|
        if s[i] != t[i] then
            s[i] = "10"[v % 2]
            v += 1
        end
    end
    if (v % 2) == 0 then
        puts s
    else
        puts "impossible"
    end
end

if __FILE__ == $0 then
    main()
end

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

# b.rb

def main()
    s = gets
    t = gets
    v = 0
    s.size.times do |i|
        if s[i] != t[i] then
            s[i] = "01"[v % 2]
            v += 1
        end
    end
    if v % 2 == 0 then
        puts s
    else
        puts "impossible"
    end
end

if __FILE__ == $0 then
    main()
end


n = gets.to_i
a = gets.split(' ').map { |t| t.to_i - 1 }
b = gets.split(' ').map { |t| t.to_i - 1 }
position = []
n.times { |i| position[b[i]] = i }
prev = -1
len = 0
n.times do |i|
	break if position[a[i]] < prev
        len += 1
        prev = position[a[i]]
end
puts n - len

input   = ARGF.to_a

shit    = input.shift.to_i;
first   = input.shift.split ' '
last    = input.shift.split ' '

k = j = 0

for i in 0..shit-1
  if first[j] == last[i]
    j += 1
  else
    k += 1
  end
end

puts k


n, m = gets.split.map(&:to_i)
l = n / m
a = gets.split.map(&:to_f)
c = []
gets.to_i.times do
    b = gets.split.map{|_| _.to_i - 1}
    next unless a[b[0]]
    c << b.map{|_| a[_]}
    b.each{|_| a[_] = nil}
end
a.compact!
if a.size > n - m * l
    a.sort!
    c << a[0, l] << a.reverse[0, l]
end
c.map!{|_| _.inject(&:+)}
puts "#{c.min / l} #{c.max / l}"

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

vals = []
dels = []
h = {}
gets.to_i.times{
  v=0
  f = gets.split.map(&:to_i).each{|t|
    v+=a[t-1]
    dels << t-1
  }.sort.first
  h[f]=true
  vals << v/i.to_f
}
dels.each{|j| a[j]=nil }
a.compact!
a.sort!

vals += [a[0...i].inject(&:+)/i.to_f, a.reverse[0...i].inject(&:+)/i.to_f] if a.size >= i && h.size < k
vals.sort!
puts "%.7f %.7f" % [vals[0], vals[-1]]

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

vals = []
dels = []
gets.to_i.times{
  v=0
  gets.split.map(&:to_i).each{|t|
    v+=a[t-1]
    dels << t-1
  }
  vals << v/i.to_f
}
dels.each{|j|
  a[j]=nil
}
a.compact!
a.sort!

vals += [a[0...i].inject(&:+)/i.to_f, a.reverse[0...i].inject(&:+)/i.to_f] if a.size >= i
vals.sort!
puts "%.7f %.7f" % [vals[0], vals[-1]]

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

vals = []
dels = []
gets.to_i.times{
  v=0
  gets.split.map(&:to_i).each{|t|
    v+=a[t-1]
    dels << t-1
  }
  vals << v/i.to_f
}
dels.each{|j| a[j]=nil }
a.compact!
a.sort!

vals += [a[0...i].inject(&:+)/i.to_f, a.reverse[0...i].inject(&:+)/i.to_f] if a.size >= i && vals.size < k
vals.sort!
puts "%.7f %.7f" % [vals[0], vals[-1]]

n, m = gets.split.map(&:to_i)
m = n / m
a = gets.split.map(&:to_f)
c = []
gets.to_i.times do
    b = gets.split.map{|_| _.to_i - 1}
    next unless a[b[0]]
    c << b.map{|_| a[_]}
    b.each{|_| a[_] = nil}
end
a.compact!
if a.size >= m
    a.sort!
    c << a[0, m] << a.reverse[0, m]
end
c.map!{|_| _.inject(&:+)}
puts "#{c.min / m} #{c.max / m}"

n, m = gets.split.map(&:to_i)
a = gets.split.map(&:to_f)
c = []
gets.to_i.times do
    b = gets.split.map{|_| _.to_i - 1}
    next unless a[b[0]]
    c << b.map{|_| a[_]}
    b.each{|_| a[_] = nil}
end
a.compact!
if a.size > n - n / m * m
    a.sort!
    m = n / m
    c << a[0, m] << a.reverse[0, m]
end
c.map!{|_| _.inject(&:+)}
puts "#{c.min / m} #{c.max / m}"


n = gets.to_i
p = [-9e9]
n.times.map{gets.split.map(&:to_i).sort.reverse}.sort.each do |a,b|
	next if b <= p[-1]
	p << a
end
p p.size - 1
puts p[1..-1] * ' '

n=gets.to_i;s=[-9e9];n.times.map{gets.split.map(&:to_i).sort.reverse}.sort.each{|x,y|s<<x if y>s[-1]};p s.size-1;$><<s[1..-1]*' '

class Point
  attr_reader :n, :x

  def endo?
    @end
  end

  def begin?
    !@end
  end

  def initialize(n, x, endo)
    @n = n
    @x = x
    @end = endo
  end

  def <=>(other)
    return x - other.x if x != other.x
    return (endo? ? 1 : 0) - (other.endo? ? 1 : 0)
  end
end

n = gets.to_i
a = []
n.times do |i|
  x, y = *gets.split(' ').map { |x| x.to_i }
  x, y = y, x if x > y
  a << Point.new(i*2, x, false)
  a << Point.new(i*2+1, y, true)
end

done = Array.new(n, false)
stack = []
pts = []

a.sort.each do |p|
  next if done[p.n / 2]
  if p.begin?
    stack << p
    next
  end

  pts << p.x

  stack.each do |visited|
    done[visited.n / 2] = true
  end

  stack.clear
end

puts pts.size
puts pts.join(' ')


n = gets.to_i

M = 1000000007

$u = [0]*(n+1)
$q = []
(n+1).times do $q << [] end
$w = [0]*(n+1)
$d = [0]*(n+1)
$c = [0]*(n+1)
$s = [0]*(n+1)

m = 0

def fnd(i)
  r = i;
  while i!=$u[i] do i=$u[i] end
  while r!=$u[r] do
    t = $u[r]
    $u[r] = i
    r = t
  end
  return i
end

def merge(i, j)
  i = $d[i]
  j = $d[j]
  if $s[i]<$s[j] then tt=i;i=j;j=tt; end
  $s[i] = $s[i]+$s[j]

  for k in $q[j]
    $q[i] << k
    $w[k] = $w[k]+$c[j]-$c[i]+M
    $d[k] = i
  end
  return i
end

ans = 0
for i in 1..n
  $u[i] = $d[i] = i
  $s[i] = 1
  $q[i] << i

  tl = gets.split.map(&:to_i)
  for l in 0...tl[0]
    v, x = tl[l*2+1], (tl[l*2+2]+M)%M
    r = fnd(v)
    $u[r] = i

    r = $d[v]
    e = ($w[v]+$c[r]+x)%M
    ans = ans + e

    $c[r] = ($c[r]+e)%M
    h = merge(r,i)
    $w[i] = (M-$c[h])%M
  end
end

p ans%M

n = gets.to_i

M = 1000000007

$u = [0]*(n+1)
$q = []
(n+1).times do $q << [] end
$w = [0]*(n+1)
$d = [0]*(n+1)
$c = [0]*(n+1)
$s = [0]*(n+1)

m = 0

def fnd(i, u)
  r = i;
  while i!=u[i] do i=u[i] end
  while r!=u[r] do
    t = u[r]
    u[r] = i
    r = t
  end
  return i
end

def merge(i, j)
  i = $d[i]
  j = $d[j]
  if $s[i]<$s[j] then tt=i;i=j;j=tt; end
  $s[i] = $s[i]+$s[j]

  for k in $q[j]
    $q[i] << k
    $w[k] = $w[k]+$c[j]-$c[i]+M
    $d[k] = i
  end
  return i
end

ans = 0
for i in 1..n
  $u[i] = $d[i] = i
  $s[i] = 1
  $q[i] << i

  tl = gets.split.map(&:to_i)
  for l in 0...tl[0]
    v, x = tl[l*2+1], (tl[l*2+2]+M)%M
    r = fnd(v,$u)
    $u[r] = i

    r = fnd(v,$d)
    e = ($w[v]+$c[r]+x)%M
    ans = ans + e

    $c[r] = ($c[r]+e)%M
    h = merge(r,i)
    $w[i] = (M-$c[h])%M
  end
end

p ans%M

n = gets.to_i

M = 1000000007

$u = [0]*(n+1)
$q = []
(n+1).times do $q << [] end
$w = [0]*(n+1)
$d = [0]*(n+1)
$c = [0]*(n+1)
$s = [0]*(n+1)

m = 0

def fnd(i, u)
  r = i;
  while i!=u[i] do i=u[i] end
  while r!=u[r] do
    t = u[r]
    u[r] = i
    r = t
  end
  return i
end

def merge(i, j)
  i = fnd(i,$d)
  j = fnd(j,$d)
  if $s[i]<$s[j] then tt=i;i=j;j=tt; end
  $s[i] = $s[i]+$s[j]

  for k in $q[j]
    $q[i] << k
    $w[k] = $w[k]+$c[j]-$c[i]+M
  end
  $d[j] = i
  return i
end

ans = 0
for i in 1..n
  $u[i] = $d[i] = i
  $s[i] = 1
  $q[i] << i

  tl = gets.split.map(&:to_i)
  for l in 0...tl[0]
    v, x = tl[l*2+1], (tl[l*2+2]+M)%M
    r = fnd(v,$u)
    $u[r] = i

    r = fnd(v,$d)
    e = ($w[v]+$c[r]+x)%M
    ans = ans + e

    $c[r] = ($c[r]+e)%M
    h = merge(r,i)
    $w[i] = (M-$c[h])%M
  end
end

p ans%M


n = gets.to_i
s = gets.chomp
zero = s.count('z')
one = s.count('n')
ans = [1] * one + [0] * zero
puts ans * ' '

def read_tokens
  return gets.chomp.to_s.strip.split(' ')
end

def read_ints
  data = []
  read_tokens.each { |s|
    data.push(s.to_i)
  }
  return data
end

n = gets.chomp.to_i
s = gets.chomp.to_s
dict = {}
s.each_char { |el|
  if dict.key?(el)
    dict[el] += 1
  else
    dict[el] = 1
  end
}

zeros = dict["z"]
ones = dict["n"]
if ones != nil
  while ones > 0
    print "1 "
    ones -= 1
  end
end

if zeros != nil
  while zeros > 0
    print "0 "
    zeros -= 1
  end
end

#!usr/bin/env ruby

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

gets

word = gets.chomp

z_count = 0
n_count = 0

word.each_char do |c|
  if c == 'z'
    z_count += 1
  elsif c == 'n'
    n_count += 1
  end
end

puts ("1 " * n_count + "0 " * z_count).rstrip

n = gets.to_i
a = gets.chomp.split('')
z = e = r = o = n = 0

for char in a
	case char
	when 'z'
		z += 1
	when 'e'
		e += 1
	when 'r'
		r += 1
	when 'o'
		o += 1
	when 'n'
		n += 1
	end
end

one_number = [o, n, e].min
o -= one_number
e -= one_number
zero_number = [z, e, r, o].min

one_number.times do
	print '1 '
end
zero_number.times do
	print '0 '
end

num = gets.to_i
arr = gets

n = arr.split('').select{|x| x == 'n' }.count
z = arr.split('').select{|x| x == 'z' }.count

print "#{Array.new(n, '1').join(' ')}" if n > 0
print " " if n > 0 && z > 0
print "#{Array.new(z, '0').join(' ')}" if z > 0

n = gets.to_i
s = gets.chomp
n_c = s.count('n')
z_c = s.count('z')

puts ((['1'] * n_c) + (['0'] * z_c)).join(" ")

def big(n, line)
	place = 0
	returned = ''
	n.times do
		returned << "1 " if line[place] == "n"
		place = place + 1
	end
	place = 0
	n.times do
		returned << "0 " if line[place] == "z"
		place = place + 1
	end
	returned[-1] = '' unless returned[-1] == "0" || returned[-1] == "1"
	return returned
end

n = gets.chomp.to_i
line = gets.chomp
puts(big(n, line))

n = gets
str = gets
zeros = str.count 'z'
ones = str.count 'n'

ans = []
1.upto(ones) {ans << "1"}
1.upto(zeros) {ans << "0"}

puts ans.join(" ")

n = gets.chomp.to_i
s = gets.chomp
zero = s.count("z")
one = s.count("n")
puts "1 " * one + "0 " * zero

gets
s = gets.strip
z = s.split('').count('z')
((s.size - z*4)/3).times { print '1 ' }
z.times { print '0 ' }

gets
s = gets.chomp

puts (Array.new(s.count('n'), '1') + Array.new(s.count('z'), '0')).join(' ')

gets
s = gets.strip
z = s.split('').count('z')
((s.size - z*4)/3).times { print 1 }
z.times { print 0 }


Vehicle = Struct.new(:index, :capacity)

class LorryLoader
  def self.load(lorry_volume, vehicle_registry)
    kayaks = []
    catamarans = []
    vehicle_registry.each.with_index do |vehicle, index|
      type, capacity = vehicle.split.map(&:to_i)
      kayaks << Vehicle.new(index + 1, capacity) if type == 1
      catamarans << Vehicle.new(index + 1, capacity) if type == 2
    end

    kayaks.sort_by! { |x| -x.capacity }
    catamarans.sort_by! { |x| -x.capacity }

    selected_vehicles = []
    total_capacity = volume = ki = ci = 0
    while (ki < kayaks.size || ci < catamarans.size) && volume < lorry_volume
      k_cap = ki < kayaks.size ? kayaks[ki].capacity : 0
      k_cap += kayaks[ki + 1].capacity if ki < kayaks.size - 1
      c_cap = ci < catamarans.size && lorry_volume - volume > 1 ? catamarans[ci].capacity : 0

      break if k_cap == 0 && c_cap == 0

      if (c_cap >= k_cap)
        selected_vehicles << catamarans[ci].index
        total_capacity += catamarans[ci].capacity
        volume += 2
        ci += 1
      else
        selected_vehicles << kayaks[ki].index
        total_capacity += kayaks[ki].capacity
        volume += 1
        ki += 1
      end
    end

    return total_capacity, selected_vehicles
  end
end

if __FILE__ == $0
  vehicle_count, lorry_volume = gets.split.map(&:to_i)
  vehicle_registry = []
  vehicle_count.times { vehicle_registry << gets }

  capacity, selected_vehicles = LorryLoader.load(lorry_volume, vehicle_registry)

  puts capacity
  puts selected_vehicles.join(' ')
end

def load_lorry(lorryVolume, kayaks, catamarans)
  kayaks.sort_by! { |x| x[1] * -1 }
  catamarans.sort_by! { |x| x[1] * -1 }

  capacity = volume = ki = ci = 0
  selected_vehicles = []
  while (ki < kayaks.length || ci < catamarans.length) && volume < lorryVolume
    c_value = ci < catamarans.length && lorryVolume - volume > 1 ? catamarans[ci][1] : 0
    k_value = ki < kayaks.length ? kayaks[ki][1] : 0

    k_value += kayaks[ki + 1][1] if ki < kayaks.length - 1

    break if k_value == 0 && c_value == 0

    if c_value > k_value
      capacity += catamarans[ci][1]
      selected_vehicles << catamarans[ci][0]
      ci += 1;
      volume += 2
    else
      capacity += kayaks[ki][1]
      selected_vehicles << kayaks[ki][0]
      ki += 1
      volume += 1
    end
  end

  return capacity, selected_vehicles
end

if __FILE__ == $0
  n, lorryVolume = gets.split.map(&:to_i)
  kayaks = []
  catamarans = []
  n.times do |i|
    t, c = gets.split.map(&:to_i)
    kayaks << [i + 1, c] if t == 1
    catamarans << [i + 1, c] if t == 2
  end

  capacity, selected_vehicles = load_lorry(lorryVolume, kayaks, catamarans)

  puts capacity
  puts selected_vehicles.join(" ")
end

n,v = gets.split(" ").map{|x| x.to_i}
ts1 = Array.new
ts2 = Array.new
n.times do |i|
  t,p = gets.split(" ").map{|x| x.to_i}
  if t == 1
    ts1 << [i,p]
  else
    ts2 << [i,p]
  end
end
ts1.sort!{|a,b| -(a[1]<=>b[1])}
ts2.sort!{|a,b| -(a[1]<=>b[1])}
p2 = [(v-2)/2,ts2.size-1].min
p1 = [v-3-p2*2,ts1.size-1].min
while p1+1<ts1.size and p2>=0
  if p1+2<ts1.size
    if ts1[p1+1][1]+ts1[p1+2][1] > ts2[p2][1]
      p1+=2
      p2-=1
    else
      break
    end
  else
    if ts1[p1+1][1] > ts2[p2][1]
      p1+=1
      p2-=1
    else
      break
    end
  end
end
sum=0
ts = Array.new
0.upto(p1){|x| sum+=ts1[x][1];ts<<ts1[x][0]+1}
0.upto(p2){|x| sum+=ts2[x][1];ts<<ts2[x][0]+1}
puts sum
puts ts.join(" ")

def load_lorry(lorryVolume, vehicles)
  kayaks = []
  catamarans = []

  vehicles.each_with_index do |v, i|
    kayaks << [i + 1, v[1]] if v[0] == 1
    catamarans << [i + 1, v[1]] if v[0] == 2
  end

  kayaks.sort_by! { |x| x[1] * -1 }
  catamarans.sort_by! { |x| x[1] * -1 }

  capacity = volume = ki = ci = 0
  selected_vehicles = []
  while (ki < kayaks.length || ci < catamarans.length) && volume < lorryVolume
    c_value = ci < catamarans.length && lorryVolume - volume > 1 ? catamarans[ci][1] : 0
    k_value = ki < kayaks.length ? kayaks[ki][1] : 0

    k_value += kayaks[ki + 1][1] if ki < kayaks.length - 1

    break if k_value == 0 && c_value == 0

    if c_value > k_value
      capacity += catamarans[ci][1]
      selected_vehicles << catamarans[ci][0]
      ci += 1;
      volume += 2
    else
      capacity += kayaks[ki][1]
      selected_vehicles << kayaks[ki][0]
      ki += 1
      volume += 1
    end
  end

  return capacity, selected_vehicles
end

if __FILE__ == $0
  n, lorryVolume = gets.split.map(&:to_i)
  vehicles = []
  n.times { vehicles << gets.split.map(&:to_i) }

  capacity, selected_vehicles = load_lorry(lorryVolume, vehicles)

  puts capacity
  puts selected_vehicles.join(" ")
end

def load_lorry(lorryVolume, vehicles)
  kayaks = []
  catamarans = []

  vehicles.each_with_index do |v, i|
    kayaks << [i + 1, v[1]] if v[0] == 1
    catamarans << [i + 1, v[1]] if v[0] == 2
  end

  kayaks.sort! { |a, b| b[1] <=> a[1] }
  catamarans.sort! { |a, b| b[1] <=> a[1] }

  capacity = volume = ki = ci = 0
  selected_vehicles = []
  while (ki < kayaks.length || ci < catamarans.length) && volume < lorryVolume
    c_value = ci < catamarans.length && lorryVolume - volume > 1 ? catamarans[ci][1] : 0
    k_value = ki < kayaks.length ? kayaks[ki][1] : 0

    k_value += kayaks[ki + 1][1] if ki < kayaks.length - 1

    break if k_value == 0 && c_value == 0

    if c_value > k_value
      capacity += catamarans[ci][1]
      selected_vehicles << catamarans[ci][0]
      ci += 1;
      volume += 2
    else
      capacity += kayaks[ki][1]
      selected_vehicles << kayaks[ki][0]
      ki += 1
      volume += 1
    end
  end

  return capacity, selected_vehicles
end

if __FILE__ == $0
  n, lorryVolume = gets.split.map(&:to_i)
  vehicles = []
  n.times { vehicles << gets.split.map(&:to_i) }

  capacity, selected_vehicles = load_lorry(lorryVolume, vehicles)

  puts capacity
  puts selected_vehicles.join(" ")
end

def load_lorry(lorryVolume, vehicles)
  kayaks = []
  catamarans = []

  vehicles.each_with_index do |v, i|
    if (v[0] == 1)
      kayaks << [i + 1, v[1]]
    else
      catamarans << [i + 1, v[1]]
    end
  end

  kayaks.sort_by! { |x| x[1] * -1 }
  catamarans.sort_by! { |x| x[1] * -1 }

  volume = ki = ci = 0
  while ((ki < kayaks.length || ci < catamarans.length) && volume < lorryVolume)
    c_value = ci < catamarans.length && lorryVolume - volume > 1 ? catamarans[ci][1] : 0
    k_value = ki < kayaks.length ? kayaks[ki][1] : 0

    k_value += kayaks[ki + 1][1] if ki < kayaks.length - 1

    if (c_value > k_value)
      ci += 1;
      volume += 2
    else
      ki += 1
      volume += 1
    end
  end

  sel_vehicles = kayaks.take(ki) + catamarans.take(ci)
  return sel_vehicles.reduce(0) { |c, x| c + x[1] }, sel_vehicles.map { |x| x[0] }
end

if __FILE__ == $0
  n, lorryVolume = gets.split.map(&:to_i)
  vehicles = []
  n.times { vehicles << gets.split.map(&:to_i) }

  capacity, selected_vehicles = load_lorry(lorryVolume, vehicles)

  puts capacity
  puts selected_vehicles.join(" ")
end

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

a1 = []
a2 = []

n.times do |i|
    t,q = gets.split.map(&:to_i)
    if t == 1 then a1 << [q, i]
    else a2 << [q, i]
    end
end

a2.sort!
a2.reverse!
a1.sort!
a1.reverse!

two = [v / 2, a2.length].min
total = 0
two.times do |i|
    total += a2[i][0]
end

one = [v - two * 2, a1.length].min
one.times do |i| total += a1[i][0] end

while two > 0 and one <= a1.length - 2 and a2[two - 1][0] < a1[one][0] + a1[one + 1][0] do
    total -= a2[two - 1][0]
    total += a1[one][0] + a1[one + 1][0];
    two -= 1
    one += 2
end

puts total
ans = []
two.times do |i| ans << 1 + a2[i][1] end
one.times do |i| ans << 1 + a1[i][1] end

puts ans * ' '

num, max_size = gets.split.map(& :to_i)
kayak = []
catamaran = []
num.times do |index|
  t, p = gets.split.map(& :to_i)
  if t == 1
    kayak << [index + 1, p]
  else
    catamaran << [index + 1, p]
  end
end

kayak.sort! { |a, b| b[1] <=> a[1]  }
catamaran.sort! { |a, b| b[1] <=> a[1] }

max_c = [max_size / 2, catamaran.length].min
max_k = [max_size - 2 * max_c, kayak.length].min

while max_c > 0 && max_k <= kayak.length
  if max_k + 2 <= kayak.length #there are two more elements in the array
    if catamaran[max_c - 1][1] < (kayak[max_k][1] + kayak[max_k + 1][1])
      max_c = max_c - 1
      max_k = max_k + 2
    else
      break
    end
  else
    break
  end
end

ans_set = []
sum = 0
0.upto(max_c - 1) { |i| sum = sum + catamaran[i][1]; ans_set << catamaran[i][0] }
0.upto(max_k - 1) { |i| sum = sum + kayak[i][1]; ans_set << kayak[i][0] }
puts sum
puts ans_set.join(" ")

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

kayaks = []
catamarans = []
n.times do |index|
	type, p = gets.split.map(&:to_i)
	if type == 1
		kayaks << [index + 1, p]
	else
		catamarans << [index + 1, p]
	end
end

kayaks.sort! { |e1, e2| e2[1] <=> e1[1] }
catamarans.sort! { |e1, e2| e2[1] <=> e1[1] }

possible_capacity = {}
max_c = [v / 2, catamarans.length].min
max_k = [v - 2 * max_c, kayaks.length].min
indexes = []
total = (0...max_c).inject(0) { |sum, value| sum + catamarans[value][1] } +
				(0...max_k).inject(0) { |sum, value| sum + kayaks[value][1] }

while max_c > 0 && max_k <= kayaks.length - 2 && catamarans[max_c - 1][1] < kayaks[max_k][1] + kayaks[max_k + 1][1]
	total -= catamarans[max_c - 1][1]
	total += kayaks[max_k][1] + kayaks[max_k + 1][1]
	max_c -= 1
	max_k += 2
end

indexes = []
max_c.times do |index|
	indexes << catamarans[index][0]
end
max_k.times do |index|
	indexes << kayaks[index][0]
end

puts total
puts indexes.join(' ')

n, max = gets().split(" ").collect {|x| x.to_i}
kayaks = []
catemerons = []
n.times do |i|
  type, capacity = (gets().split(" ").collect {|x| x.to_i})
  if type == 1
    kayaks << [i, capacity]
  else
    catemerons << [i, capacity]
  end
end
kayaks.sort! {|x, y| y[1] <=> x[1]}
catemerons.sort! {|x, y| y[1] <=> x[1]}

max_c = [catemerons.length, (max/2).to_i].min
max_k = [kayaks.length, max].min

#puts "kayaks: #{kayaks.inspect}, catemerons: #{catemerons.inspect}, max_k:  #{max_k}, max_c: #{max_c}"

wt = 0
optimal = 0
selected_k = []
selected_c = []
i = 0
while (wt < max) && (i  < max_c) do
  if (wt + 2) <= max
    wt += 2
    optimal += catemerons[i][1]
    i += 1
  else
    break
  end
end

#puts "After catemerons, wt: #{wt}, i: #{i}"

j = 0
while (wt < max) && (j < max_k) do
  wt += 1
  optimal += kayaks[j][1]
  j += 1
end

#puts "After kayaks, wt: #{wt}, j: #{j}"

while (i > 0) && (j < max_k) do
  proposed_optimal1 = optimal - catemerons[i - 1][1] + kayaks[j][1]
  proposed_optimal2 = proposed_optimal1
  proposed_optimal2 += kayaks[j + 1][1] if (j < (max_k - 1))
  if proposed_optimal2 > optimal
    i -= 1
    j += 1
    optimal = proposed_optimal2
    j += 1 if (proposed_optimal2 > proposed_optimal1)
  else
    break
  end
end

puts optimal
(0..(i - 1)).each do |i| print "#{catemerons[i][0] + 1} " end
(0..(j - 1)).each do |j| print "#{kayaks[j][0] + 1} " end

n, max = gets().split(" ").collect {|x| x.to_i}
kayaks = []
catemerons = []
n.times do |i|
  type, capacity = (gets().split(" ").collect {|x| x.to_i})
  if type == 1
    kayaks << [i, capacity]
  else
    catemerons << [i, capacity]
  end
end
kayaks.sort! {|x, y| y[1] <=> x[1]}
catemerons.sort! {|x, y| y[1] <=> x[1]}

max_c = [catemerons.length, (max/2).to_i].min
max_k = [kayaks.length, max].min

#puts "kayaks: #{kayaks.inspect}, catemerons: #{catemerons.inspect}, max_k:  #{max_k}, max_c: #{max_c}"

wt = 0
optimal = 0
selected_k = []
selected_c = []
i = 0
while (wt < max) && (i  < max_c) do
  if (wt + 2) <= max
    wt += 2
    optimal += catemerons[i][1]
    i += 1
  else
    break
  end
end

#puts "After catemerons, wt: #{wt}, i: #{i}"

j = 0
while (wt < max) && (j < max_k) do
  wt += 1
  optimal += kayaks[j][1]
  j += 1
end

#puts "After kayaks, wt: #{wt}, j: #{j}"

while (i > 0) && (j < max_k) do
  proposed_optimal1 = optimal - catemerons[i - 1][1] + kayaks[j][1]
  proposed_optimal2 = proposed_optimal1
  proposed_optimal2 += kayaks[j + 1][1] if (j < (max_k - 1))
  if (proposed_optimal1 > optimal) || (proposed_optimal2 > optimal)
    i -= 1
    j += 1
    optimal = proposed_optimal1
    if (proposed_optimal2 > proposed_optimal1)
      j += 1
      optimal = proposed_optimal2
    end
  else
    break
  end
end

puts optimal
(0..(i - 1)).each do |i| print "#{catemerons[i][0] + 1} " end
(0..(j - 1)).each do |j| print "#{kayaks[j][0] + 1} " end

n,v = gets.split(" ").map{|x| x.to_i}
ts = Array.new
n.times do |i|
  t,p = gets.split(" ").map{|x| x.to_i}
  ts << [i,t,p]
end
ts.sort!{|a,b| -(a[2]*2/a[1]<=>b[2]*2/b[1])}
max = 0
set = Array.new
ts.each_with_index do |t,i|
  break if v<=0
  applicable = false
  if v>2
    applicable = true
  else
    if v==t[1]
      applicable=true
    elsif v>t[1]
      if ts[i+1]
        if ts[i+1][1]==1
          applicable=true
        else
          nt = ts[i+1..-1].select{|x| x[1]==1}.first
          if nt
            if t[2]+nt[2]>ts[i+1][2]
              applicable=true
            end
          end
        end
      else
        applicable=true
      end
    end
  end
  if applicable
    set << t[0]+1
    v -= t[1]
    max += t[2]
  end
end
puts max
puts set.join(" ")

n,v = gets.split(" ").map{|x| x.to_i}
ts1 = Array.new
ts2 = Array.new
n.times do |i|
  t,p = gets.split(" ").map{|x| x.to_i}
  if t == 1
    ts1 << [i,p]
  else
    ts2 << [i,p]
  end
end
ts1.sort!{|a,b| -(a[1]<=>b[1])}
ts2.sort!{|a,b| -(a[1]<=>b[1])}
p2 = [(v-2)/2,ts2.size-1].min
p1 = n-3-p2*2
while p1+2<ts1.size and p2>=0
  if ts1[p1+1][1]+ts1[p1+2][1] > ts2[p2][1]
    p1+=2
    p2-=1
  else
    break
  end
end
sum=0
ts = Array.new
0.upto(p1){|x| sum+=ts1[x][1];ts<<ts1[x][0]+1}
0.upto(p2){|x| sum+=ts2[x][1];ts<<ts2[x][0]+1}
puts sum
puts ts.join(" ")

num, max_size = gets.split.map(& :to_i)
kayak = []
catamaran = []
num.times do |index|
  t, p = gets.split.map(& :to_i)
  if t == 1
    kayak << [index + 1, p]
  else
    catamaran << [index + 1, p]
  end
end

kayak.sort! { |a, b| b[1] <=> a[1]  }
catamaran.sort! { |a, b| b[1] <=> a[1] }

max_c = [max_size / 2, catamaran.length].min
max_k = [max_size - 2 * max_c, kayak.length].min

while max_c > 0 && max_k <= kayak.length
  if max_k + 2 <= kayak.length #there are two more elements in the array
    if catamaran[max_c - 1][1] < (kayak[max_k][1] + kayak[max_k + 1][1])
      max_c = max_c - 1
      max_k = max_k + 2
    else
      break
    end
  else
    if catamaran[max_c - 1][1] < kayak[max_k][1]
      max_c = max_c - 1
      max_k = max_k + 1
    else
      break
    end
  end
end

ans_set = []
sum = 0
0.upto(max_c - 1) { |i| sum = sum + catamaran[i][1]; ans_set << catamaran[i][0] }
0.upto(max_k - 1) { |i| sum = sum + kayak[i][1]; ans_set << kayak[i][0] }
puts sum
ans_set.sort!
puts ans_set.to_s[1..-2]

num, max_size = gets.split.map(& :to_i)
kayak = []
catamaran = []
num.times do |index|
  t, p = gets.split.map(& :to_i)
  if t == 1
    kayak << [index + 1, p]
  else
    catamaran << [index + 1, p]
  end
end

kayak.sort! { |a, b| b[1] <=> a[1]  }
catamaran.sort! { |a, b| b[1] <=> a[1] }

max_c = [max_size / 2, catamaran.length].min
max_k = [max_size - 2 * max_c, kayak.length].min

while max_c > 0 && max_k <= kayak.length && max_k > 0
  if max_k + 2 <= kayak.length #there are two more elements in the array
    if catamaran[max_c - 1][1] < (kayak[max_k][1] + kayak[max_k + 1][1])
      max_c = max_c - 1
      max_k = max_k + 2
    else
      break
    end
  else
    if catamaran[max_c - 1][1] < kayak[max_k][1]
      max_c = max_c - 1
      max_k = max_k + 1
    else
      break
    end
  end
end

ans_set = []
sum = 0
0.upto(max_c - 1) { |i| sum = sum + catamaran[i][1]; ans_set << catamaran[i][0] }
0.upto(max_k - 1) { |i| sum = sum + kayak[i][1]; ans_set << kayak[i][0] }
puts sum
puts ans_set.join(" ")

num, max_size = gets.split.map(& :to_i)
kayak = []
catamaran = []
num.times do |index|
  t, p = gets.split.map(& :to_i)
  if t == 1
    kayak << [index + 1, p]
  else
    catamaran << [index + 1, p]
  end
end

kayak.sort! { |a, b| b[1] <=> a[1]  }
catamaran.sort! { |a, b| b[1] <=> a[1] }

max_c = [max_size / 2, catamaran.length].min
max_k = [max_size - 2 * max_c, kayak.length].min

while max_c > 0 && max_k <= kayak.length
  if max_k + 2 <= kayak.length #there are two more elements in the array
    p "OK"
    if catamaran[max_c - 1][1] < (kayak[max_k][1] + kayak[max_k + 1][1])
      max_c = max_c - 1
      max_k = max_k + 2
    else
      break
    end
  else
    if catamaran[max_c - 1][1] < kayak[max_k][1]
      max_c = max_c - 1
      max_k = max_k + 1
    else
      break
    end
  end
end

ans_set = []
sum = 0
0.upto(max_c - 1) { |i| sum = sum + catamaran[i][1]; ans_set << catamaran[i][0] }
0.upto(max_k - 1) { |i| sum = sum + kayak[i][1]; ans_set << kayak[i][0] }
puts sum
ans_set.sort!
puts ans_set.to_s[1..-2]

n, max = gets().split(" ").collect {|x| x.to_i}
arr = []
i = 0
n.times do |i|
  arr << (gets().split(" ").collect {|x| x.to_i}).push(i + 1)
end
arr.sort! {|x, y| y[1] <=> x[1]}
#puts arr.inspect
wt = 0
optimal = 0
n = []
arr.each_with_index do |a, i|
  if ((wt + a[0]) <= max)
    optimal += a[1]
    n << a[2]
    wt += a[0]
    break if wt == max
  end
end
puts optimal
puts n.join(",")

n, max = gets().split(" ").collect {|x| x.to_i}
arr = []
i = 0
n.times do |i|
  arr << (gets().split(" ").collect {|x| x.to_i}).push(i + 1)
end
arr.sort! {|x, y| y[1] <=> x[1]}
#puts arr.inspect
wt = 0
optimal = 0
n = []
arr.each_with_index do |a, i|
  if ((wt + a[0]) <= max)
    optimal += a[1]
    n << a[2]
    wt += a[0]
    break if wt == max
  end
end
puts optimal
puts n.join(" ")

n, max = gets().split(" ").collect {|x| x.to_i}
arr = []
i = 0
n.times do |i|
  arr << (gets().split(" ").collect {|x| x.to_i}).push(i + 1)
end
arr.sort! {|x, y| y[1] <=> x[1]}
#puts arr.inspect
wt = 0
optimal = 0
n1 = []
n2 = []
arr.each_with_index do |a, i|
  if ((wt + a[0]) <= max)
    optimal += a[1]
    if a[0] == 1
      n1 << a[2]
    else
      n2 << a[2]
    end
    wt += a[0]
    break if wt == max
  end
end
puts optimal
puts n2.concat(n1).join(" ")

n, max = gets().split(" ").collect {|x| x.to_i}
kayaks = []
catemerons = []
n.times do |i|
  type, capacity = (gets().split(" ").collect {|x| x.to_i})
  if type == 1
    kayaks << [i, capacity]
  else
    catemerons << [i, capacity]
  end
end
kayaks.sort! {|x, y| y[1] <=> x[1]}
catemerons.sort! {|x, y| y[1] <=> x[1]}

max_c = [catemerons.length, (max/2).to_i].min
max_k = [kayaks.length, max].min

puts "kayaks: #{kayaks.inspect}, catemerons: #{catemerons.inspect}, max_k:  #{max_k}, max_c: #{max_c}"

wt = 0
optimal = 0
selected_k = []
selected_c = []
i = 0
while (wt < max) && (i  < max_c) do
  if (wt + 2) <= max
    wt += 2
    optimal += catemerons[i][1]
    i += 1
  else
    break
  end
end

#puts "After catemerons, wt: #{wt}, i: #{i}"

j = 0
while (wt < max) && (j < max_k) do
  wt += 1
  optimal += kayaks[j][1]
  j += 1
end

#puts "After kayaks, wt: #{wt}, j: #{j}"

while (i > 0) && (j < max_k) do
  proposed_optimal1 = optimal - catemerons[i - 1][1] + kayaks[j][1]
  proposed_optimal2 = proposed_optimal1
  proposed_optimal2 += kayaks[j + 1][1] if (j < (max_k - 1))
  if (proposed_optimal1 > optimal) || (proposed_optimal2 > optimal)
    i -= 1
    j += 1
    optimal = proposed_optimal1
    if (proposed_optimal2 > proposed_optimal1)
      j += 1
      optimal = proposed_optimal2
    end
  else
    break
  end
end

puts optimal
(0..(i - 1)).each do |i| print "#{catemerons[i][0] + 1} " end
(0..(j - 1)).each do |j| print "#{kayaks[j][0] + 1} " end

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

a1 = []
a2 = []

n.times do |i|
    t,q = gets.split.map(&:to_i)
    if t == 1 then a1 << [q, i]
    else a2 << [q, i]
    end
end

a2.sort!
a2.reverse!
a1.sort!
a1.reverse!

one = 0
two = [v / 2, a2.length].min
total = 0
two.times do |i|
    total += a2[i][0]
end

while two > 0 and one < a1.length - 2 and a2[two - 1][0] < a1[one][0] + a1[one + 1][0] do
    total -= a2[two - 1][0]
    total += a1[one][0] + a1[one + 1][0];
    two -= 1
    one += 2
end

puts total
ans = []
two.times do |i| ans << 1 + a2[i][1] end
one.times do |i| ans << 1 + a1[i][1] end

puts ans * ' '

Vehicle = Struct.new(:index, :capacity)

class LorryLoader
  def self.load(lorry_volume, vehicle_registry)
    kayaks = []
    catamarans = []

    vehicle_registry.each.with_index do |vehicle, index|
      type, capacity = vehicle.split.map(&:to_i)
      if type == 1
        kayaks << Vehicle.new(index + 1, capacity)
      elsif
        catamarans << Vehicle.new(index + 1, capacity)
      end
    end

    kayaks.sort_by! { |x| -x.capacity }
    catamarans.sort_by! { |x| -x.capacity }

    selected_vehicles = []
    total_capacity = volume = ki = ci = 0
    while (ki < kayaks.size || ci < catamarans.size) && volume < lorry_volume
      k_cap = ki < kayaks.size ? kayaks[ki].capacity : 0
      k_cap += (ki < kayaks.size - 1 ? kayaks[ki - 1].capacity : 0)
      c_cap = ci < catamarans.size && lorry_volume - volume > 1 ? catamarans[ci].capacity : 0

      break if k_cap == 0 && c_cap == 0

      if (c_cap >= k_cap)
        selected_vehicles << catamarans[ci].index
        total_capacity += catamarans[ci].capacity
        volume += 2
        ci += 1
      else
        selected_vehicles << kayaks[ki].index
        total_capacity += kayaks[ki].capacity
        volume += 1
        ki += 1
      end
    end

    return total_capacity, selected_vehicles
  end
end

if __FILE__ == $0
  vehicle_count, lorry_volume = gets.split.map(&:to_i)
  vehicle_registry = []
  vehicle_count.times { vehicle_registry << gets }

  capacity, selected_vehicles = LorryLoader.load(lorry_volume, vehicle_registry)

  puts capacity
  puts selected_vehicles.join(' ')
end

def load_lorry(lorryVolume, vehicles)
  kayaks = []
  catamarans = []

  vehicles.each_with_index do |v, i|
    kayaks << [i + 1, v[1]] if v[0] == 1
    catamarans << [i + 1, v[1]] if v[0] == 2
  end

  kayaks.sort!
  catamarans.sort!

  capacity = volume = ki = ci = 0
  selected_vehicles = []
  while (ki < kayaks.length || ci < catamarans.length) && volume < lorryVolume
    c_value = ci < catamarans.length && lorryVolume - volume > 1 ? catamarans[ci][1] : 0
    k_value = ki < kayaks.length ? kayaks[ki][1] : 0

    k_value += kayaks[ki + 1][1] if ki < kayaks.length - 1

    break if k_value == 0 && c_value == 0

    if c_value > k_value
      capacity += catamarans[ci][1]
      selected_vehicles << catamarans[ci][0]
      ci += 1;
      volume += 2
    else
      capacity += kayaks[ki][1]
      selected_vehicles << kayaks[ki][0]
      ki += 1
      volume += 1
    end
  end

  return capacity, selected_vehicles
end

if __FILE__ == $0
  n, lorryVolume = gets.split.map(&:to_i)
  vehicles = []
  n.times { vehicles << gets.split.map(&:to_i) }

  capacity, selected_vehicles = load_lorry(lorryVolume, vehicles)

  puts capacity
  puts selected_vehicles.join(" ")
end

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

kayaks = []
catamarans = []

n.times do
	vehicle_info = gets.split.map(&:to_i)
	if vehicle_info[0] == 1
		kayaks << vehicle_info[1]
	else
		catamarans << vehicle_info[1]
	end
end

kayaks.sort! { |e1, e2| e2 <=> e1 }
catamarans.sort! { |e1, e2| e2 <=> e1 }

possible_capacity = []
max_catamarans = v / 2
max_kayaks = [v, kayaks.length].min
(0..max_catamarans).each do |num_of_c|
	next if (num_of_k = v - 2 * num_of_c) > max_kayaks
	capacity = kayaks[0...num_of_k].inject(0) { |sum, value| sum + value } +
						 catamarans[0...num_of_c].inject(0) { |sum, value| sum + value }
	count = num_of_k + num_of_c
	possible_capacity << [capacity, count]
end

puts possible_capacity

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

kayaks = []
catamarans = []
n.times do |index|
	type, p = gets.split.map(&:to_i)
	if type == 1
		kayaks << [index + 1, p]
	else
		catamarans << [index + 1, p]
	end
end

kayaks.sort! { |e1, e2| e2[1] <=> e1[1] }
catamarans.sort! { |e1, e2| e2[1] <=> e1[1] }

possible_capacity = {}
max_c = [v / 2, catamarans.length].min
max_k = [v - 2 * max_c, kayaks.length].min
indexes = []
total = (0...max_c).inject(0) { |sum, value| sum + catamarans[value][1] } +
				(0...max_k).inject(0) { |sum, value| sum + kayaks[value][1] }
puts total
while max_c > 0 && max_k <= kayaks.length - 2 && catamarans[max_c - 1][1] < kayaks[max_k][1] + kayaks[max_k + 1][1]
	total -= catamarans[max_c - 1][1]
	total += kayaks[max_k][1] + kayaks[max_k + 1][1]
	max_c -= 1
	max_k += 2
end

indexes = []
max_c.times do |index|
	indexes << catamarans[index][0]
end
max_k.times do |index|
	indexes << kayaks[index][0]
end

puts total
puts indexes.join(' ')

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

kayaks = []
catamarans = []
n.times do |index|
	type, p = gets.split.map(&:to_i)
	if type == 1
		kayaks << [index + 1, p]
	else
		catamarans << [index + 1, p]
	end
end

kayaks.sort! { |e1, e2| e2[1] <=> e1[1] }
catamarans.sort! { |e1, e2| e2[1] <=> e1[1] }

possible_capacity = {}
max_c = [v / 2, catamarans.length].min
max_k = [v - 2 * max_c, kayaks.length].min
indexes = []
total = (0...max_c).inject(0) { |sum, value| sum + catamarans[value][1] } +
				(0...max_k).inject(0) { |sum, value| sum + kayaks[value][1] }

while max_c > 0 && max_k <= kayaks.length - 2 && catamarans[max_c - 1][0] < kayaks[max_k][0] + kayaks[max_k + 1][0]
	total -= catamarans[max_c - 1][0]
	total += kayaks[max_k][0] + kayaks[max_k + 1][0]
	max_c -= 1
	max_k += 2
end

indexes = []
max_c.times do |index|
	indexes << catamarans[index][0]
end
max_k.times do |index|
	indexes << kayaks[index][0]
end

puts total
puts indexes.join(' ')

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

kayaks = []
catamarans = []
n.times do |index|
	type, p = gets.split.map(&:to_i)
	if type == 1
		kayaks << [index + 1, p]
	else
		catamarans << [index + 1, p]
	end
end

kayaks.sort! { |e1, e2| e2[1] <=> e1[1] }
catamarans.sort! { |e1, e2| e2[1] <=> e1[1] }

possible_capacity = {}
max_c = [v / 2, catamarans.length].min
max_k = [v - 2 * max_c, kayaks.length].min
indexes = []
total = (0...max_c).inject(0) { |sum, value| sum + catamarans[value][1] } +
				(0...max_k).inject(0) { |sum, value| sum + kayaks[value][1] }

while max_c > 0 && max_k <= kayaks.length - 2 && catamarans[max_c - 1][1] < kayaks[max_k][1] + kayaks[max_k + 1][1]
	total -= catamarans[max_c - 1][0]
	total += kayaks[max_k][0] + kayaks[max_k + 1][0]
	max_c -= 1
	max_k += 2
end

indexes = []
max_c.times do |index|
	indexes << catamarans[index][0]
end
max_k.times do |index|
	indexes << kayaks[index][0]
end

puts total
puts indexes.join(' ')

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

kayaks = []
catamarans = []
n.times do |index|
	type, p = gets.split.map(&:to_i)
	if type == 1
		kayaks << [index + 1, p]
	else
		catamarans << [index + 1, p]
	end
end

kayaks.sort! { |e1, e2| e2[1] <=> e1[1] }
catamarans.sort! { |e1, e2| e2[1] <=> e1[1] }

possible_capacity = {}
max_c = [v / 2, catamarans.length].min
max_k = [v - 2 * max_c, kayaks.length].min
indexes = []
total = (0...max_c).inject(0) { |sum, value| sum + catamarans[value][1] } +
				(0...max_k).inject(0) { |sum, value| sum + kayaks[value][1] }

while max_c > 0 && max_k <= kayaks.length - 2 && catamarans[max_c - 1][0] < kayaks[max_k][0] + kayaks[max_k + 1][0]
	total -= catamarans[max_c - 1][0]
	total += kayaks[max_k][0] + kayaks[max_k + 1][0]
	max_c -= 1
	max_k += 2
end

indexes = []
max_k.times do |index|
	indexes << kayaks[index][0]
end
max_c.times do |index|
	indexes << catamarans[index][0]
end

puts total
puts indexes.join(' ')

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

kayaks = []
catamarans = []

n.times do
	vehicle_info = gets.split.map(&:to_i)
	if vehicle_info[0] == 1
		kayaks << vehicle_info[1]
	else
		catamarans << vehicle_info[1]
	end
end

kayaks.sort! { |e1, e2| e2 <=> e1 }
catamarans.sort! { |e1, e2| e2 <=> e1 }

possible_capacity = {}
max_catamarans = v / 2
(0..max_catamarans).each do |num_of_c|
	next if (num_of_k = v - 2 * num_of_c) > kayaks.length
	capacity = kayaks[0...num_of_k].inject(0) { |sum, value| sum + value } +
						 catamarans[0...num_of_c].inject(0) { |sum, value| sum + value }
	possible_capacity[capacity] = [num_of_k, num_of_c]
end

max_capacity = possible_capacity.max
puts max_capacity[0]
puts ([1] * max_capacity[1][0] + [2] * max_capacity[1][1]).join(' ')


t=gets.to_i
t.times{
	n = gets.to_i
	if n<2020
		puts "NO"
	else
		puts n%2020 <= n/2020? "YES" : "NO"
	end
}

gets.to_i.times do
  n = gets.to_i
  puts n % 2020 <= n / 2020 ? "YES" : "NO"
end

require 'set'
s = Set.new

flag = 0

val1 = 1000000/2020;
val2 = 1000000/2021;

for i in (0..val1)
    for j in (0..val2)
        temp = i*2020+j*2021;
        s << temp;
    end
end

n = gets.chomp.to_i;
while n != 0
    n = n-1;
    value = gets.chomp.to_i;
    if s.include?(value)
        puts "YES"
    else
        puts "NO"
    end
end

MAX_N = 10**6 + 5
flag = [false] * MAX_N
flag[0] = true
(0...MAX_N).each do |i|
	if flag[i]
		flag[i + 2020] = true if i + 2020 < MAX_N
		flag[i + 2021] = true if i + 2021 < MAX_N
	end
end

gets.to_i.times do
	n = gets.to_i
	puts flag[n] ? 'YES' : 'NO'
end

t = gets.to_i
t.times do
  n = gets.to_i
  puts (n % 2020 <= n / 2020) ? "YES" : "NO"
end

tcases = gets.chomp.to_i
def solve()
  n = gets.chomp.to_i
  q = n / 2020
  r = n % 2020
  puts (r <= q) ? "YES" : "NO"
end
tcases.times do solve() end

t = gets.chomp.to_i
# arr = []
t.times do
    n = gets.chomp.to_i
    ok = false
    0.upto(500) do |i|
        ans = n - i * 2021
        #  p ans
        if ans >= 0
            amari = ans % 2020
            if amari == 0
                puts "YES"
                # arr << i
                ok = true
                break
            end
        else
            break
        end
    end
    if ok == false
        puts "NO"
    end
end

gets.to_i.times do
  n = gets.to_i
  puts n % 2020 >= n / 2020 ? "YES" : "NO"
end


gets.to_i.times do
  n=gets.to_i
  s=gets.chomp.chars
  q=""
  t=0
  s.each.with_index do |ss,i|
    if q==""
      if ss=="?"
        t+=1
      elsif ss=="B"
        s[0,i]=["R"]*(i%2)+["B","R"]*(i/2)
        q="B"
      elsif ss=="R"
        s[0,i]=["B"]*(i%2)+["R","B"]*(i/2)
        q="R"
      end
    elsif q=="B"
      if ss=="?"
        s[i]="R"
        q="R"
      elsif ss=="R"
        q="R"
      end
    else
      if ss=="?"
        s[i]="B"
        q="B"
      elsif ss=="B"
        q="B"
      end
    end
  end
  if q==""
    s=["R"]*(n%2)+["B","R"]*(n/2)
  end
  puts s.join
end

END { load __FILE__ unless $stdin.eof? }

t = gets.to_i
t.times do

n = gets.to_i
s = gets.chomp.chars.map! { |c| c == "B" ? 1 : c == "R" ? 2 : 0 }

i = s.index(&:nonzero?) || n
c = s[i] || 1
i.times.reverse_each do |k|
  s[k] = (c ^= 3)
end

while i < n
  i += 1
  j = (i...n).find { |k| s[k] != 0 } || n

  c = s[i-1]
  (i...j).each { |k| s[k] = (c ^= 3) }

  i = j
end

puts (s * "").tr("12", "BR")

end


t = gets.to_i

1.upto t do
  c = p = 0
  n, k = gets.split.map(&:to_i)
  if n % 2 == 0
    n += k*2
  else
    2.upto Math.sqrt(n+1).to_i do |i|
      if n % i == 0
        n += i
        p = 1
        break
      end
    end
    if p == 0
      n += n
    end
    n += (k-1)*2
  end
  puts n
end

def minimum(x)
  for i in 2..x do
    return i if x % i == 0
  end
end

t = gets.chomp.to_i

t.times do
  n, k = gets.chomp.split(' ').collect(&:to_i)
  puts minimum(n) + n + (2*(k-1))
end

def ans(n, k)
    # 偶数 为2， 加2后还是偶数， 所以 n + 2k
    # 奇数 第一个为奇书， 相加后也为偶数 n + first.x + 2 (k-1)
    if n & 0x1 == 0
        return n + 2 * k
    end

    d = 3

    while d <= Math.sqrt(n)
        n % d  == 0 and return n + d + 2 *(k-1)
        d += 2
    end

    return n + n + 2 * (k-1)
end

line = gets().to_i

line.times {
    (n,k) = gets.scan(/\d+/).map(&:to_i)
    puts ans(n,k)
}

def ans(n, k)
    # 偶数 为2， 加2后还是偶数， 所以 n + 2k
    # 奇数 第一个为奇书， 相加后也为偶数 n + first.x + 2 (k-1)
    if n & 0x1 == 0
        return n + 2 * k
    end

    d = 3
    while d < n
        n % d  == 0 and break
        d += 2
    end

    return n + d + 2*(k-1)
end

line = gets().to_i

line.times {
    (n,k) = gets.scan(/\d+/).map(&:to_i)
    puts ans(n,k)
}

t = gets.to_i

1.upto t do
  c = 0
  n, k = gets.split.map(&:to_i)
  if n % 2 == 0
    n += k*2
  else
    3.upto n do |i|
      if n % i == 0
        n += i
        break
      end
    end
    n += (k-1)*2
  end
  puts n
end

require 'prime'
gets.to_i.times do
    n, k=gets.split.map &:to_i
    f=n.prime_division[0][0]
    puts n+f+2*(k-1)
end

#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']
require 'prime'

def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

def na(n,d=0) Array.new(n,d)end

def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end

def na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def r_up(a, b) (a+b-1)/b end

def sum(a) a.inject(:+) end

def big(a,b) return (a>b)? a:b end

def small(a,b) return (a<b)? a:b end

n = inp[0]
d = []
n.times do
  d <<inp
end
d.each do |a|
  if (a[0].even?)
    p a[0] + 2*a[1]
  else
    p a[0] + a[0].prime_division[0][0] + 2*(a[1]-1)
  end
end

def f(n)
  2.upto(n) { |i| return i if n.modulo(i).zero? }
end

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

  k.downto(1) do |kk|
    ff = f(n)

    if ff == 2
      n += kk * ff
      break
    end

    n += ff
  end

  puts n
end

def ans(n, k)
    # 偶数 为2， 加2后还是偶数， 所以 n + 2k
    # 奇数 第一个为奇书， 相加后也为偶数 n + first.x + 2 (k-1)
    if n & 0x1 == 0
        return n + 2 * k
    end

    d = 3
    while d < n
        n % d  == 0 and break
        d += n
    end

    return n + d + 2*(k-1)
end

line = gets().to_i

line.times {
    (n,k) = gets.scan(/\d+/).map(&:to_i)
    puts ans(n,k)
}

t = gets.to_i

1.upto t do
  c = p = 0
  n, k = gets.split.map(&:to_i)
  if n % 2 == 0
    n += k*2
  else
    2.upto Math.sqrt(n+1).to_i do |i|
      if n % i == 0
        n += i
        p = 1
        break
      end
      if p == 0
        n += n
      end
    end
    n += (k-1)*2
  end
  puts n
end

def f(n)
  2.upto(n) { |i| return i if n.modulo(i).zero? }
end

gets.to_i.times do
  n, k = gets.split(' ').map(&:to_i)
  ff = f(n)
  fk = n.div(ff)

  if fk == 1
    n += ff
    ff = 2
    k -= 1
  end

  puts n + ff * k
end


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

=begin
class RQtree

# major method
# :set(ind, value) set array[ind] = value
# :query(i,j) query the minimum number between index i and j
#
	def initialize(array)
		@data = array.clone
		@tree = [0]*1000000
		construct(1, 0,@data.size-1)
	end

	attr_reader :data, :tree
	def min(a,b)
		return a  if a < b
		b
	end

	def construct(ind, i, j)
		if i==j
			@tree[ind] = @data[i]
			return @tree[ind]
		else
			mid = (i+j)/2
			a = construct(2*ind, i, mid)
			b = construct(2*ind+1, mid+1,j)
			@tree[ind] = min(a,b)
			return @tree[ind]
		end
	end

	def inspect(); _inspect(1,0,@data.size-1);end

	def _inspect(ind, b,e)

		puts  "#{b}:#{e} #{@tree[ind]}"
		return if b==e
		_inspect(2*ind, b, (b+e)/2)
		_inspect(2*ind+1, (b+e)/2+1, e)
	end

	def query(i,j); _query(i,j, 1,0, @data.size-1); end

	def _query(i,j,ind, b, e)

		if i>j
			puts 'somethong wrong with index in func _query'
			exit 1
		end

		if i==b and j==e
			return @tree[ind]
		else
			mid = (b+e)/2
			if i>mid
				return _query(i, j, 2*ind+1,  mid+1, e)
			elsif j<= mid
				return _query(i,j,2*ind,b,mid)
			else
				a = _query(i, mid,2*ind,  b, mid)
				b = _query(mid+1,j,2*ind+1, mid+1,e)
				return min(a,b)
			end
		end
	end

	def set(ind, value)
		@data[ind] = value
		update(ind, value)
	end

	def update(ind, value); _update( ind, value,1, 0, @data.size-1);  end

	def _update(ind, value,i,b,e)

		if b==e
			@tree[i] = value
			return
		end
		mid = (b+e)/2
		if ind > mid
			a=_update(ind, value,2*i+1,  mid+1, e)

		else
			_update(ind, value, 2*i, b, mid)
		end
		@tree[i] = min(@tree[2*i], @tree[2*i+1])
	end

end

class Bitree

	def initialize(array)
		@data=array.clone
		@tree= [0]*(array.size+1)
		construct()
	end

	attr_reader :data, :tree
	def construct()
		sum = [0]*(@data.size+1)
		(1..sum.size-1).each do |ind|
			sum[ind] = @data[ind-1]+sum[ind-1]
		end
		@tree[0] = 0
		(1..@tree.size-1).each do |ind|
			i = ind-(ind & -ind)
			@tree[ind] = sum[ind]-sum[i]
		end
	end

	def query(i)
		i+=1
		s=0
		while i>=1
			s+=@tree[i]
			i-= (i&-i)
		end
		s
	end

	def add(i,a)
		@data[i]+=a
		i+=1
		while i<@tree.size
			@tree[i]+=a
			i+=(i&-i)
		end
	end

	def [](i)
		@data[i]
	end
end
=end
=begin
class Graph

	def initialize(g)

		puts 'expected a Array type input' if g.class != Array
		@graph = g
		size = g.size+1
		@visited = [0]*size
		@time = [0] *size
		@father = [-1] *size
		@lev = [-1] * size

	end

	def dfs(); _dfs(-1, [0],0, 1); self;end

	def _dfs(father, time, lev, id)

		@time[id]=time[0]
		@father[id]=father
		@visited[id] = 1
		@lev[id] = lev

		@graph[id].each do |i|
			if @visited[i]!=1
				time[0]+=1
				_dfs(id, time, lev+1 ,i)
			end
		end
	end

	def inspect
		puts "time: #{@time.inspect}"
		puts "father: #{@father.inspect}"
		puts "lev: #{@lev.inspect}"
	end
end

n = gets.to_i
g = [0]*(n+1)

(1..n).each do |i|
	a = gets.split.map(&:to_i)
	g[a[0]] = a[1..(a.size-1)]
end

g = Graph.new(g)

g.dfs.inspect

=end

=begin

n = gets.to_i
m = gets
standing = 0

m.each_char do |c|
	if c == 'X'
		standing += 1
	end
end

if standing == m.size/2
	puts '0'
	puts  m
else

	if standing > m.size/2
		d = standing-m.size/2

		(0..m.size-1).each do |i|
			if m[i] == 'X'
				m[i] = 'x'
				d-=1
				break if d==0
			end
		end
		puts '1'
		puts m
	else
		d = m.size/2-standing
		(0..m.size-1).each do |i|
			if m[i] == 'x'
				m[i] = 'X'
				d-=1
				break if d == 0
			end
		end
		puts '1'
		puts m
	end
end

=end

ncity, p = gets.split.map(&:to_i)

array = []
(1..ncity).each do |i|
	a = gets.split.map(&:to_i)

	array.push(a)
end

array.sort! do |a,b|

	_a = a[0]*a[0]+a[1]*a[1]
	_b = b[0]*b[0]+b[1]*b[1]

	if _a >_b
		1
	else
		-1
	end
end

array.each do |a|

	p+=a[2]
	if p>=1000000
		_a = a[0]*a[0]+a[1]*a[1]
		puts "%.7f" % [Math.sqrt(_a)]
		exit 0
	end
end

puts -1

n,s = gets.split.map(&:to_i)
cities = (1..n).map { |_|
  gets.split.map(&:to_i)
}

ok = false
need = 1000000 - s
min = 0
max = 1000000000
while max - min > 1
  med = (max + min) / 2
  pop = cities.map { |c|
    if c[0] ** 2 + c[1] ** 2 <= med
      c[2]
    else
      0
    end
  }.inject(&:+)
  if need <= pop
    ok = true
    max = med
  else
    min = med
  end
end

if ok
  puts max ** 0.5
else
  puts -1
end


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

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

    max = nums.max
    c = 0
    nums.each_with_index do |num, i|
        c = [c, max - num].max
    end

    puts c
end

n = gets.to_i
max = []
min = []
n.times do |i|
  a = gets.to_i
  a1 = gets.split.map(&:to_i)
  max << a1.max
  min << a1.min
end

n.times do |i|
  puts max[i] - min[i]
end

t = gets.to_i
inputs = []
(0...t).each do
  n = gets.to_i
  arr = gets.chomp
  arr = arr.split(" ").map(&:to_i)
  inputs.push([arr, n])
end

inputs.each do |input|
  max = input[0].max
  min = input[0].min
  puts max-min
end

gets.to_i.times do
  gets
  xs = gets.split.map(&:to_i)
  p xs.max - xs.min
end

t = gets.to_i
while t > 0
  n = gets.to_i
  arr = gets.chomp.split.map(&:to_i).sort
  puts arr[-1] - arr[0]
  t-=1
end

n = gets.chomp.to_i
n.times do |i|
l = gets.chomp.to_i
st = gets.chomp.split
mx = 0
mn = 1000000001
l.times do |element|
    el = st[element].to_i
	if el<mn
	    mn = el
	end
	if el > mx
	    mx = el
	end
end
ans = mx - mn
puts ans
end

t = readline.strip.to_i

t.times do
  readline
  numbers = readline.strip.split(/\s+/).map(&:to_i)

  max = numbers.max
  min = numbers.min

  puts max - min
end

n = gets.to_i
a = gets.to_i
a1 = gets.split.map(&:to_i)
b = gets.to_i
b1 = gets.split.map(&:to_i)
c = gets.to_i
c1 = gets.split.map(&:to_i)

puts a1.max - a1.min
puts b1.max - b1.min
puts c1.max - c1.min


def findf(x, fa)
    if fa[x] != 0
        findf(fa[x], fa)
    else
        x
    end
end
n, m = gets.split.collect{|i| i.to_i}
fa = Array.new(n+1,0)
cnt = Array.new(n+1,1)
m.times do
    x, y = gets.split.collect{|i| i.to_i}
    xx = findf(x, fa)
    yy = findf(y, fa)
    if xx != yy
        fa[yy] = xx
        cnt[xx] += cnt[yy]
    end
end
ans = 0
(1..n).each do |i|
    ans += cnt[i]-1 if fa[i]==0
end
puts 2**ans

n, m = gets.split(' ').map(&:to_i)
ar=(0...n).to_a
def rt(rr,v)
  if ( rr[v]==v )
  elsif ( rr[v]!=rt(rr,rr[v]) )
    rr[v] = rt(rr,rr[v])
  end
  rr[v]
end
(1..m).each do |_|
  x, y = gets.split(' ').map(&:to_i)
  rx = rt(ar,ar[x-1])
  ry = rt(ar,ar[y-1])
  if rx < ry
    ar[ry]=rx
  elsif rx > ry
    ar[rx]=ry
  end
end
(0...n).each do |x|
  rt(ar,x)
end
p 2**(n-(ar.uniq.count))

n,m=gets.split.map(&:to_i)
c=(0..n).to_a
m.times do
  a,b=gets.split.map(&:to_i)
  if c[a]!=c[b]
    x=c[b]
    (n+1).times do |i|
      c[i] = c[a] if c[i] == x
    end
  end
end
p 1<<n-c.uniq.size+1

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
react = []
for i in 0...m
    react[i] = gets.chomp.split.map{|j| Integer(j)}
end

react_net = []
for i in 0...n
    react_net[i] = []
    for j in 0...n
        react_net[i][j] = false
    end
end

for i in 0...m
    react_net[react[i][0] - 1][react[i][1] - 1] = true
    react_net[react[i][1] - 1][react[i][0] - 1] = true
end
def fill_unit point, unit_id, unit, n, boolean
    if unit[point]
        return
    end

    unit[point] = unit_id
    for i in 0...n
        if boolean[point][i]
            fill_unit(i, unit_id, unit, n, boolean)
        end
    end
end

unit = Array.new(n)
for i in 0...n
    fill_unit(i, i, unit, n, react_net)
end
unit.uniq!
if m == 0
    puts 1
else
    puts 2 ** (n - unit.length)
end

numbers = gets.split(' ')
n = numbers[0].to_i
m = numbers[1].to_i

@graph = Array.new(n + 1) { Array.new }
@seen = Array.new

def dfs(node)
  @seen[node] = true
  nodes = 1
  @graph[node].each do |neighbour|
    if @seen[neighbour].nil?
      nodes += dfs(neighbour)
    end
  end
  nodes
end

m.times do
  pair = gets.split(' ')
  a = pair[0].to_i
  b = pair[1].to_i
  @graph[a].push b
  @graph[b].push a
end

total = 0
n.times do |i|
  if (@seen[i + 1].nil?)
    total += dfs(i + 1) - 1
  end
end

puts 2 ** total

def dfs(node)
  return if @vis[node]
  @vis[node] = true
  @n.times do |neighbor|
    dfs(neighbor) if @g[node][neighbor]
  end
end

n, m = gets.split.map(&:to_i)
@vis = Array.new(n)
@n = n
@g = Array.new(n){ Array.new(n) }

m.times do
  i, j = gets.split.map{|s| s.to_i - 1}
  @g[i][j] = @g[j][i] = true
end

ans = 0

n.times do |i|
  if !@vis[i]
    dfs(i)
    ans += 1
  end
end

puts 2 ** (n - ans)

input = $stdin.read.split("\n")
input.shift # get rid of m, n

$graph = {}

# build graph
input.map(&:split).each do |a, b|
  a, b = a.to_i, b.to_i
  $graph[a] ||= []
  $graph[b] ||= []

  $graph[a] << b
  $graph[b] << a
end

$visited = {}
$path = []
$m = 0

def dfs(i)
  return 0 if $visited[i]
  $visited[i] = true
  $graph[i].reduce(1) { |sum, j| sum + dfs(j) }
end

sum = 1

# start from each one to get all disjoint graphs
$graph.keys.each do |key|
  res = dfs(key)
  sum *= 2 ** (res - 1) if res > 0
end

p sum

n, m = gets.split.map(&:to_i)
gr = []
(n+1).times{gr << []}
visited = [-1]*(n+1)
m.times do
  a, b = gets.split.map(&:to_i)
  gr[a] << b
  gr[b] << a
end

q = []

(1..n).each do |v|
  if visited[v] == -1
    visited[v] = 0
    q << v

    while q.size > 0
      gr[q[0]].each do |a|
        if visited[a] == -1
          visited[a] = 1
          q.push(a)
        end
      end
      q.shift
    end

  end
end

puts 2**visited[1..-1].inject{|x, y| x+y}

input = gets.chomp.split
n = Integer(input[0])
m = Integer(input[1])
react = []
for i in 0...m
    react[i] = gets.chomp.split.map{|j| Integer(j)}
end

react_net = []
for i in 0...n
    react_net[i] = []
    for j in 0...n
        react_net[i][j] = false
    end
end

for i in 0...m
    react_net[react[i][0] - 1][react[i][1] - 1] = true
    react_net[react[i][1] - 1][react[i][0] - 1] = true
end
count = 0
for i in 0...n
    for j in 0..i
        if react_net[i][j]
            count += 1
        end
    end
end

if m == 0
    puts 1
else
    puts 2 * count
end

numbers = gets.split(' ')
n = numbers[0].to_i
m = numbers[1].to_i

@graph = Array.new(n + 1) { Array.new }
@seen = Array.new

def dfs(node)
  @seen[node] = true
  @graph[node].each do |neighbour|
    if @seen[neighbour].nil?
      return 1 + dfs(neighbour)
    end
  end
  1
end

m.times do
  pair = gets.split(' ')
  a = pair[0].to_i
  b = pair[1].to_i
  @graph[a].push b
  @graph[b].push a
end

best = 0
n.times do |i|
  if (@seen[i].nil?)
    best = [best, dfs(i) - 1].max
  end
end

puts 2 ** best

numbers = gets.split(' ')
n = numbers[0].to_i
m = numbers[1].to_i

@graph = Array.new(n + 1) { Array.new }
@seen = Array.new
@count = 0

def dfs(node)
  @seen[node] = true
  @count += 1
  @graph[node].each do |neighbour|
    if @seen[neighbour].nil?
      dfs(neighbour)
    end
  end
end

m.times do
  pair = gets.split(' ')
  a = pair[0].to_i
  b = pair[1].to_i
  @graph[a].push b
  @graph[b].push a
end

best = 0
n.times do |i|
  if (@seen[i].nil?)
    @count = 0
    dfs(i)
    best = [best, @count - 1].max
  end
end

puts 2 ** best

n,m=gets.split.map(&:to_i)
c=(0..n).to_a
m.times do
  a,b=gets.split.map(&:to_i)
  if c[a]!=c[b]
    (n+1).times do |i|
      c[i] = c[a] if c[i] == c[b]
    end
  end
end
p c.group_by{|x|x}.values.inject(1){|r,x|r*=1<<(x.size-1)}

n,m=gets.split.map(&:to_i)
c=(0..n).to_a
m.times do
  a,b=gets.split.map(&:to_i)
  if c[a]!=c[b]
    (n+1).times do |i|
      c[i] = c[a] if c[i] == c[b]
    end
  end
end
p 1<<n-c.uniq.size+1


n=gets.to_i

a=[]
(n-1).times {
a.push gets(' ').to_i
}
a.push gets.to_i

mi=a.min
ma=a.max

for i in 0..a.size-1
if a[i]==ma
ma_i=i
break
end
end

i=a.size-1
while i>=0
if a[i]==mi
mi_i=i
break
end

i-=1
end

ret=ma_i+n-mi_i-1
if ma_i>mi_i
ret-=1
end

p ret

#!/usr/bin/ruby
def read_int; gets.chomp.to_i; end
def read_ints; gets.chomp.split.map(&:to_i); end
def read_chars; gets.chomp.split(''); end

read_int
arr = read_ints

min_index = arr.rindex(arr.min)
max_index = arr.index(arr.max)
n = max_index + arr.size - 1 - min_index
n -= 1 if min_index < max_index

puts n

gets
arr = gets.split.map &:to_i
max_ind = arr.index(arr.max)
min_ind = (arr.reverse.index(arr.min) - arr.size + 1).abs
if max_ind > min_ind
  puts max_ind + arr.size - min_ind - 2
else
  puts max_ind + arr.size - min_ind - 1
end

s=gets()
n=s.to_i
s=gets()
s=s.chop()
at=0

a=Array.new(n)
for i in (0..n-1) do
	cur=0
	while (at<s.length and s[at]!=' ')
		cur=cur*10+s[at].to_i
		at+=1
	end
	at+=1
	a[i]=cur
end
min=n-1
max=0
for i in (0..n-1) do
if(a[i]>a[max])
max=i
end
if(a[n-1-i]<a[min])
min=n-1-i
end
end
res=0
res=max+n-1-min
if(min<max)
res-=1
end
puts res

gets
s=gets.split.map &:to_i
n=s.size
z=n-1+s.index(s.max)-s.rindex(s.min)
p n>z ?z:z-1

n = gets.chomp.to_i
arr = Array.new(n)
arr = gets.split.map &:to_i
big = [0,0]
small = [1000,0]
n.times do |i|
  if arr[i] > big[0]
    big[0] = arr[i]
    big[1] = i
  end

  if arr[i] <= small[0]
    small[0] = arr[i]
    small[1] = i
  end

end

flag = 0
if big[1] > small[1]
  flag = 1
end

puts (big[1] - 0) + ((n-1) - small[1]) - flag

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

min_index = as.rindex(as.min)
max_index = as.index(as.max)

puts (n - min_index - 1) + max_index - (min_index < max_index ? 1 : 0)

gets.chomp.to_i
line = gets.chomp.split(/ /).map(&:to_i)
len = line.length - 1
min_height = line.min
max_height = line.max
min_pos = line.rindex min_height
max_pos = line.index max_height
delta = 0
delta = 1 if min_pos < max_pos
puts (max_pos - 0) + (len - min_pos) - delta

n=gets.to_i
x=gets.split.map{|e| e.to_i}
min1=x[0]
max1=x[0]
minP=0
maxP=0
n.times{|i|
	if x[i]<=min1 then
		min1=x[i]
		minP=i
	end
}
n.times{|i|
	if max1<x[i] then
		max1=x[i]
		maxP=i
	end
}
ans=0
if minP<maxP then
	ans=-1
end
puts ans+(n-1-minP)+(maxP)

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

max = a.sort[-1]
min = a.sort[0]

imx = a.index(max)
imn = a.rindex(min)

if imx > imn
  p imx + (n-imn-1) - 1
else
  p imx + (n-imn-1)
end

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

max_ind = arr.find_index(arr.max)
min_ind = arr.rindex(arr.min)
# p max_ind, min_ind
if max_ind > min_ind
  puts (max_ind) + ((n-1) - min_ind) - 1
else
  puts (max_ind) + ((n-1) - min_ind)
end

#=================================
# Better be sure than sorry.
#=================================
$stdin=File.open("./input.txt","r") if ENV["LOGNAME"]=="skydos"
require 'prime'
#require 'mathn'
require 'matrix'
require 'set'
include Math
#=================================

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

mini=data.min
maxy=data.max

mini_pos = data.rindex(mini)
ans=0
for i in mini_pos...data.size-1
	tmp=data[i]
	data[i]=data[i+1]
	data[i+1]=tmp
	ans+=1
end

maxy_pos=data.index(maxy)
for i in 1..maxy_pos
	tmp=data[i]
	data[i]=data[i+1]
	data[i+1]=tmp
	ans+=1
end

puts ans

numSol = gets.to_i
a = gets.split.map {|x| x.to_i}

leftMax = 0
rightMin = numSol-1

for x in 0...numSol
	y = numSol-1-x
	leftMax = x if a[x]>a[leftMax]
	rightMin = y if a[y]<a[rightMin]
end

ans = leftMax+(numSol-rightMin-1)

ans -= 1 if leftMax>rightMin

puts ans

n=gets.to_i
a=gets.split
a.collect! {|i| i.to_i}

for i in 1..n
	if a[-i] == a.max then l=n-i end
	if a[i-1] == a.min then r=n-i end
end

puts (if l+r>=n then l+r-1 else l+r end)

n=gets.to_i
a=gets.split.collect! {|i| i.to_i}
for i in 1..n
	if a[-i] == a.max then l=n-i end
	if a[i-1] == a.min then r=n-i end
end
puts (if l+r>=n then l+r-1 else l+r end)

i = gets.to_i

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

min_index = array.rindex(array.min)
max_index = array.index(array.max)

length = array.length - 1

if max_index < min_index
  swaps = (length-min_index) + max_index
else
  swaps = (length-min_index) + max_index - 1
end

puts swaps

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

ans = 0
maxv = a.max
minv = a.min

i = a.index(maxv)
ans += i
a.delete_at(i)
a.unshift(maxv)

i = a.rindex(minv)
ans += a.size - i - 1

puts ans

gets
s = gets.split(' ').map(&:to_i)
sorted = s.sort
min = sorted.first
max = sorted.last
max_index = s.find_index(max)
min_index = s.reverse.find_index(min)
result = s.find_index(max) + s.reverse.find_index(min)
result -= 1 if max_index > s.size - min_index - 1
puts result

n = Integer(gets.chomp)
h = gets.chomp.split
height = Array.new
for i in 0...n
    height[i] = Integer(h[i])
end

max = 0
min = 101
for i in 0...n
    if height[i] > max
        max = height[i]
    end
end
for i in 0...n
    if height[i] < min
        min = height[i]
    end
end

max_index = Array.new
min_index = Array.new
for i in 0...n
    if height[i] == max
        max_index.push(i)
    elsif height[i] == min
        min_index.push(i)
    end
end

x = min_index.length
if max == min
    puts 0
elsif max_index[0] > min_index[x-1]
    puts max_index[0] + n - 2 - min_index[x-1]
else
    puts max_index[0] + n - 1 - min_index[x-1]
end

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

min, max = a.minmax

wmax = a.index(max)
wmin = a.rindex(min)

ans = wmax + a.size - wmin - 1
ans -= 1 if wmax > wmin

puts ans

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
arr.reverse!

min,min_index = arr.each_with_index.min

arr.unshift(arr.delete_at(min_index))
secs = min_index

max,max_index = arr.each_with_index.max
secs += (n - max_index - 1)
puts secs

number_of_soldiers = gets.to_i
soldiers = gets.split.map &:to_i

shortest = number_of_soldiers - 1 - soldiers.reverse.index(soldiers.min)
tallest = soldiers.index(soldiers.max)
puts tallest + number_of_soldiers - shortest - (tallest > shortest ? 1 : 0) - 1

ar = []
n = gets.to_i
ar = gets.split.map &:to_i
#puts ar.index(ar.max)
x = ar.index(ar.max)
y = ar.rindex(ar.min)
a = x
b = n-y-1
puts y < x ? a + b-1 : a + b

gets
s=gets.split.map &:to_i
n=s.size
z=n-1+s.index(s.max)-s.rindex(s.min)
p n>z ?z:z-1

gets
l = gets.split.map(&:to_i)
m = l.index(l.max)
l.delete_at(m)
puts m + l.reverse.index(l.min)

n=gets.to_i; d=gets.split.map(&:to_i); sum=d.index(d.max); d[d.index(d.max)]+=1;
p d.index(d.max) + (d.select{|x| x!=d.max}.reverse.push(d.max).index(d.min))

n = gets.to_i
a = gets.split.map(&:to_i)
mii = a.rindex(a.min)
mxi = a.index(a.max)
ans = mxi + (n - 1 - mii)
ans -= 1 if mxi > mii
puts ans

n = gets.to_i
a = gets.split.map(&:to_i)
mi = a.min
mx = a.max
mii = a.rindex(mi)
mxi = a.index(mx)
ans = mxi + (n - 1 - mii)
ans -= 1 if mxi > mii
puts ans

n = gets.to_i
a = gets.split.map(&:to_i)
mi = a.min
mx = a.max
mii = 0
mxi = 0
for i in 0...n
  if a[i] == mi
    mii = i
    mi = a[i]
  end
end
for i in 0...n
  if a[i] == mx
    mxi = i
    mx = a[i]
    break;
  end
end
ans = mxi + (n - 1 - mii)
ans -= 1 if mxi > mii
puts ans

def swap (i, d)
  tmp = $a[i]
  $a[i] = $a[i + d]
  $a[i + d] = tmp
  i + d
end
n = gets.to_i
$a = gets.split.map(&:to_i)
mi = $a.min
mx = $a.max
ans = 0
mii = 0
mxi = 0
n.times do |i|
  if $a[i] == mx
    mxi = i
    break
  end
end
while mxi != 0
  mxi = swap(mxi, -1)
  ans += 1
end
n.times do |i|
  mii = i if $a[i] == mi
end
while mii != n - 1
  mii = swap(mii, 1)
  ans += 1
end
puts ans

n = gets.chomp.to_i

array = gets.chomp.split(" ")

maxi = 0
mini = 105

id = 0

for i in 0...n
	a = array[i].to_i

	if a<=mini
		mini = a
		id = i;
	end

	i += 1
end

cont = 0

while id<n-1 do

	a = array[id]
	array[id] = array[id+1]
	array[id + 1] = a

	id += 1
	cont += 1

end

j = n - 1

while j>=0 do
	a = array[j].to_i

	if a>=maxi
		maxi = a
		id = j;
	end

	j -= 1
end

while id>=1 do

	a = array[id]
	array[id] = array[id-1]
	array[id - 1] = a

	id -= 1
	cont += 1

end

puts cont

length = gets.chomp.to_i
heights = gets.chomp.split()
min, max = heights.minmax_by {|h| h.to_i}
minindex = heights.rindex(min)
maxindex = heights.index(max)
if maxindex < minindex
        print maxindex + heights.length - minindex - 1
else
        print maxindex - minindex + minindex + heights.length - minindex - 1 - 1
end

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

imax = a.index(a.max)
imin = a.size - a.rindex(a.min) -1

puts imax + imin - (imax > a.size - imin - 1 ? 1 : 0)

max = [0, 0]
min = [101, 0]
n = Integer(gets.chomp)
gets.chomp.split.each_with_index { |s, i|
  h = s.to_i
  if h > max[0]
    max = [h, i]
  end
  if h <= min[0]
    min = [h, i]
  end
}
puts max[1] + (n - min[1] - 1) - (max[1] > min[1] ? 1 : 0)

n = gets.to_i
a = gets.split.map(&:to_i)
a.delete_at(ans = a.find_index(a.max))
a.reverse!
ans += a.find_index(a.min)
p ans

STDIN.gets
i = STDIN.gets.split(' ').map(&:to_i)

max, min = i.max, i.min

max_i = -1; min_i = 100

i.length.times do |k|
	max_i = k if max_i == -1 and i[k] == max
	min_i = k if i[k] == min
end

c = max_i - 1 + i.length - min_i
c -= 1 if max_i > min_i

STDOUT.puts c

#!ruby

n = gets.to_i

data = gets.split.map{|e|e.to_i}

max_num = data.max
min_num = data.min
max_index = data.index(max_num)
min_index = data.rindex(min_num)

puts max_index + data.length - min_index  -1+ (max_index>min_index ? -1 :  0 )

n = gets.to_i
ar = gets.split(' ').map(&:to_i)
min, max = ar.minmax
maxid, minid = ar.index(max), ar.rindex(min)
r = maxid + n-1 - minid
r -= 1 if maxid > minid
puts r

n=gets.chomp.to_i
i=0
#for i in (0...n)
b=gets.chomp.split(" ").map &:to_i
#end
t1= b.index(b.max)
t2=n-1-b.rindex(b.min)
#puts b.max
#puts t1
#puts b.min
#puts t2
if n>t1+t2
puts t1+t2
else
puts t1+t2-1
end

#end

n = gets.chomp.to_i
na = gets.chomp.split(/ /).map!{|x| x.to_i}

nsa = na.sort
minidx = na.rindex(nsa.first)
maxidx = na.index(nsa.last)
mv = na.length-(minidx+1) + maxidx
mv -= 1 if minidx < maxidx
puts mv

gets
s=gets.split.map &:to_i
n=s.size
z=n-1+s.index(s.max)-s.rindex(s.min)
p n>z ?z:z-1

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# Aurhor: seniorihor (12.03.2012)

class General
  def initialize(quantity, numbers)
    @quantity = quantity
    @numbers = numbers
    @seconds = 0
  end
  def calculation_seconds
    if @numbers.rindex(@numbers.min) > @numbers.index(@numbers.max)
      @seconds = @numbers.index(@numbers.max) + @numbers.length - 1 - @numbers.rindex(@numbers.min)
    else
      @seconds = @numbers.index(@numbers.max) + @numbers.length - 2 - @numbers.rindex(@numbers.min)
    end
  end
end
quantity = gets.chomp.to_i
numbers = gets.chomp.split(" ").map! do |i|
  i = i.to_i
end

g = General.new(quantity, numbers)
p g.calculation_seconds

n=gets.to_i;a=gets.split.map(&:to_i);
min=0;max=0;
0.upto(n-1){|i| min=i if a.min==a[i] }
(n-1).downto(0){|i|     max=i if a.max==a[i] }
p max+(n-1-min)-(( min<max )?1:0)

kilkist = gets.chomp.to_i

i=0

soldatu=gets.chomp.split(" ").map! {|el| el=el.to_i}

posMensh=0
posBilsh=0

soldatu.size().times do |j|
  if(soldatu[posBilsh]<soldatu[j])
  posBilsh=j
  end
end

posMensh=kilkist-1
i=posMensh

while(i>=0)
  if(soldatu[posMensh]>soldatu[i])
    posMensh=i
  end
  i-=1
end

sec=0
sec+=posBilsh
sec+=(kilkist-1-posMensh)
sec-=1 if(posBilsh>posMensh)

print sec

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# Aurhor: seniorihor  and vertoldo (12.03.2012)

class General
  def initialize(quantity, numbers)
    @quantity = quantity
    @numbers = numbers
    @posMensh=0
    @posBilsh=0
    @sec=0
  end

  def calculation_seconds

    @numbers.size().times do |j|
      if(@numbers[@posBilsh]<@numbers[j])
       @posBilsh=j
      end
    end

    @posMensh=@quantity-1
    i=@posMensh

    while(i>=0)
      if(@numbers[@posMensh]>@numbers[i])
        @posMensh=i
      end
      i-=1
    end

    @sec+=@posBilsh
    @sec+=(@quantity-1-@posMensh)
    @sec-=1 if(@posBilsh>@posMensh)

    return @sec
  end
end

quantity = gets.chomp.to_i
numbers = gets.chomp.split(" ").map! {|el| el=el.to_i}
g = General.new(quantity, numbers)
print g.calculation_seconds

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

min, max = 0, 0
a.each_index do |i|
  min = i if a[min] >= a[i]
  max = i if a[max] < a[i]
end
cnt = max + (a.size - 1 - min)
cnt -= 1 if max > min
puts  cnt

def order(n,a)
  minmax = a.minmax
  min = minmax.shift
  max = minmax.shift

  #As the smaller soldier has to be at the end of the line we need to look for the closest one to that pos
  min_pos = a.rindex(min)
  # The highest soldier has to be first that's why we start looking from the beginig of the line
  max_pos = a.index(max)

  tot_sec = 0
  #Checking if the soldiers have to be swapped between them
  if min_pos < max_pos
    # Has to be -2 because of the array index and one less to the end of line,
    # that's because the soldiers are swapped between them and we consider that time on the max_pos
    tot_sec = max_pos + ((n-2) - min_pos)
  else
    tot_sec = max_pos + ((n-1) - min_pos)
  end

  tot_sec
end

n = gets.to_i
a = gets.split(/\s/).map(&:to_i)
puts order(n,a)

n = $stdin.gets.to_i
milita = $stdin.gets.split.map { |x| x.to_i }

max = 0
maxidx = -1

n.times do |i|
  if max < milita[i]
    max = milita[i]
    maxidx = i
  end
end

result = maxidx

maxidx.downto(1) do |i|
  t = milita[i]
  milita[i] = milita[i-1]
  milita[i-1] = t
end

min = 10000000
minidx = -1

n.times do |k|
  i = n - k - 1

  if min > milita[i]
    min = milita[i]
    minidx = i
  end
end

result += n - 1 - minidx

p result

n=gets.chomp.to_i
a=gets.chomp.split.map(&:to_i)
mn=a.min
mx=a.max
mn_pos = a.rindex(mn)
ans=0
for i in mn_pos...a.size-1
	a[i],a[i+1]=a[i+1],a[i]
	ans+=1
end
mx_pos=a.index(mx)
for i in 1..mx_pos
	a[i],a[i+1]=a[i+1],a[i]
	ans+=1
end
puts ans

gets
a = gets.strip.split.map(&:to_i).to_a
p a.size - a.rindex(a.min) - 1 + a.index(a.max) - (a.index(a.max) > a.rindex(a.min) ? 1 : 0)

n = gets.to_i

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

l = a.index(a.max)
r = a.rindex(a.min)
if l > r
  puts a.size-2 +l - r
else
  puts a.size-1 + l -r
end

n=gets.to_i
a=gets.split.map(&:to_i)
max_index=a.index(a.max)
min_index=a.rindex(a.min)
p max_index<min_index ? max_index+(n-min_index-1) : max_index+(n-min_index-2)

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

max = a.max
min = a.min

maxi = a.find_index(max)

k = 0
i = maxi
while i>0
    a[i-1], a[i] = a[i], a[i-1]
    i-=1
    k+=1
end

mini = a.count - a.reverse.find_index(min) - 1
i = mini
while i<a.count-1
    a[i+1], a[i] = a[i], a[i+1]
    i+=1
    k+=1
end

puts k

n=gets.chomp.to_i
i=0
#for i in (0...n)
b=gets.chomp.split(" ").map &:to_i
#end
t1= b.index(b.max)
t2=n-1-b.index(b.min)
puts b.max
puts t1
puts b.min
puts t2
if n>t1+t2
puts t1+t2
else
puts t1+t2-1
end

#end

n=gets.chomp.to_i
i=0
#for i in (0...n)
b=gets.chomp.split(" ").map &:to_i
#end
t1= b.index(b.max)
t2=n-1-b.index(b.min)
#puts b.max
#puts t1
#puts b.min
#puts t2
if n>t1+t2
puts t1+t2
else
puts t1+t2-1
end

#end

n=gets.chomp.to_i
i=0
#for i in (0...n)
b=gets.chomp.split(" ")
#end
t1= b.index(b.max)
t2=n-b.rindex(b.min)-1
if n>t1+t2
puts t1+t2
else
puts t1+t2-1
end

kilkist = gets.chomp.to_i

soldatu=gets.chomp.split(" ").map! {|el| el=el.to_i}

posMensh=soldatu.rindex(soldatu.min)

posBilsh=soldatu.rindex(soldatu.max)

sec=0

if(posMensh!=posBilsh)

  sec+=posBilsh

  sec+=(kilkist-1-posMensh)

  sec-=1 if(posBilsh>posMensh)

end

print sec

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

max = a.max
min = a.min

maxi = a.find_index(max)
mini = a.reverse.find_index(min)

if maxi!=mini
    k = maxi+mini-1
else
    k = maxi+mini
end

puts k

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

max = a.max
min = a.min

if a.count == 2
    if a.first == max && a.last == min
        puts 0
    else
        puts 1
    end
else

    maxi = a.find_index(max)
    mini = a.reverse.find_index(min)

    if maxi!=mini
        k = maxi+mini-1
    else
        k = maxi+mini
    end

    puts k
end

gets
h = gets.split.map &:to_i
numb = h.index(h.max) + h.size - (h.reverse.index(h.min) - h.size).abs
numb -= 1 if h.index(h.max) > h.index(h.min)
puts numb

gets
h = gets.split.map &:to_i
numb = h.index(h.max) + h.size - (h.reverse.index(h.min) - h.size).abs
numb -= 1 if h.index(h.max) > (h.reverse.index(h.min) - h.size).abs
puts numb

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

max_ind = arr.rindex(arr.max)
min_ind = arr.rindex(arr.min)
# p max_ind, min_ind
if max_ind > min_ind
  puts (max_ind) + ((n-1) - min_ind) - 1
else
  puts (max_ind) + ((n-1) - min_ind)
end

ar = []
n = gets.to_i
ar = gets.split.map &:to_i
#puts ar.index(ar.max)
x = ar.index(ar.max)
y = n-ar.rindex(ar.min)-1
puts y < x ? x+y-1 : x+y

gets
l = gets.split.map(&:to_i)
m = l.index(l.max)
l.delete(l.max)
puts m + l.reverse.index(l.min)

gets
l = gets.split.map(&:to_i)
m = l.index(l.max)
l.delete(m)
puts m + l.reverse.index(l.min)

gets
l = gets.split.map(&:to_i)
m = l.each_with_index.max
l.delete(m[0])
puts m[1] + l.reverse.each_with_index.min[1]

gets
l = gets.split.map(&:to_i)
m = l.index(l.max)
l.delete(m)
puts m + l.reverse.each_with_index.min[1]

n=gets.to_i; d=gets.split.map(&:to_i); sum=d.index(d.max);
p d.index(d.max) + (d.reverse.select{|x| x!=d.max}.push(d.max).index(d.min))

n=gets.to_i

a=[]
(n-1).times {
a.push gets(' ').to_i
}
a.push gets.to_i

mi=a.min
ma=a.max

for i in 0..a.size-1
if a[i]==ma
ma_i=i
break
end
end

i=a.size-1
while i>=0
if a[i]==mi
mi_i=i
break
end

i-=1
end

ret=ma_i+n-mi_i
if ma_i>mi_i
ret-=1
end

p ret

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

imax = a.index(a.max)
imin = a.rindex(a.min)

puts imax + (a.size - imin - 1) - (imax > a.index(a.min) ? 1 : 0)

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

imax = a.index(a.max)
imin = a.size - a.rindex(a.min) -1

puts imax + imin - (imax > a.size - imin ? 1 : 0)

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

imax = a.index(a.max)
imin = a.size - a.rindex(a.min) -1

p imax, imin

puts imax + imin - (imax > a.size - imin ? 1 : 0)

STDIN.gets
i = STDIN.gets.split(' ').map(&:to_i)

max, min = i.max, i.min

if max == i.first and min = i.last
    STDOUT.puts 0
    exit
end

max_i = 0; min_i = 101

i.length.times do |k|
    max_i = k if max_i == 0 and i[k] == max
    min_i = k if i[k] == min
end

c = max_i - 1 + i.length - min_i
c -= 1 if max_i > min_i

STDOUT.puts c

STDIN.gets
i = STDIN.gets.split(' ').map(&:to_i)

max, min = i.max, i.min
max_i = 0; min_i = 101

i.length.times do |k|
	max_i = k if max_i == 0 and i[k] == max
	min_i = k if i[k] == min
end

c = max_i - 1 + i.length - min_i
c -= 1 if max_i > min_i

STDOUT.puts c

STDIN.gets
i = STDIN.gets.split(' ').map(&:to_i)

max, min = i.max, i.min

if max == i.first and min == i.last
	STDOUT.puts 0
	exit
end

max_i = 0; min_i = 101

i.length.times do |k|
	max_i = k if max_i == 0 and i[k] == max
	min_i = k if i[k] == min
end

c = max_i - 1 + i.length - min_i
c -= 1 if max_i > min_i

STDOUT.puts c

#!ruby

n = gets.to_i

data = gets.split.map{|e|e.to_i}

max_num = data.max
min_num = data.min
max_index = data.index(max_num)
min_index = data.length - data.rindex(min_num) -1

puts max_index + min_index + (max_index>min_index ? -1 :  0 )


#!usr/bin/env ruby

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

input = gets.split(' ').map(&:to_i)
num = gets.chomp

n = input[0]
x = input[1]
y = input[2]

counter = 0

(0..x-1).each do |i|
 if i == y
  counter += 1 if num[n-i-1] == '0'
 elsif num[n-i-1] == '1'
  counter += 1
 end
end

puts counter

if File.exists?("input")
  $stdin = File.open("input")
  $stdout = File.open("output","w")
end

nxy = gets.chomp.split(" ").map(&:to_i)
n = nxy[0]
x = nxy[1]
y = nxy[2]

s = gets.chomp

ans=0
(0..x-1).each do |i|
#	p i
#	p s[s.length-i-1]
	if(i==y)
		if(s[s.length-i-1]=='0')
			ans+=1
		end
	else
#		p i
		if(s[s.length-i-1]=='1')
			ans+=1
		end
	end
end

puts ans

n, x, y = gets.split.map(&:to_i)
s = gets.chomp.reverse
goal = '0' * x
goal[y] = '1'
puts s[0, x].chars.zip(goal.chars).count { _1 != _2 }


a=gets.chomp.split(" ")
0.upto(1) {|i| a[i]=a[i].to_i    }
b=gets.chomp.split(" ")
0.upto(a[1]-1) {|i| b[i]=b[i].to_i    }
c=(1.. (a[0] * a[1])).to_a
1.upto(a[1]) do |i|
ind=c.index(b[i-1])
swp=c[i*a[0]-1]
c[i*a[0]-1]=b[i-1]
c[ind]=swp
end

i=0
(a[1]).times do
a[0].times do
printf "#{c[i]} "
i+=1
end
puts ""
end

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

o=1

k.times{|i|
  print a[i].to_s+' '
  (n-1).times{
    o+=1 while a.include? o
    print o.to_s+' '
    o+=1
  }
  puts
}

n, k = gets.split.map &:to_i
a = gets.split.map &:to_i
ar = Array.new(n*k) { |i| 1 if a.include?(i) }

pointer = 1
a.each do |element|
  print "%d " % element
  counter = n-1
  while counter > 0 do
     if ar[pointer].nil?
       print "%d " % pointer
       counter -= 1
       ar[pointer] = 1
     end
    pointer += 1
  end
  print "\n"
end

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

n, k = read_next_line
a = read_next_line
g = []
for i in 0...a.size
  g << [a[i]]
end
if n > 1
  i, j = 0, 1
  for x in 1..k * n
    unless a.include? x
      g[i] << x
      j += 1
      if j == n
        j = 1
        i += 1
      end
    end
  end
end
g.each { |z| puts z * ' ' }

t = gets.split.collect{|x| x.to_i}
n,k = t[0], t[1]

a = gets.split.collect{|x| x.to_i}
answer = []

a.each do |c|
	print c,' '
	m = 1
	for i in 1..n*k
		if m == n
			break
		end
		if not a.include?(i) and not answer.include?(i)
			answer << i
			m+=1
			print i,' '
		end
	end
	puts
end

n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
d = (1..n*k).to_a
r = []
k.times do |i|
  t = []
  t.push a[i]
  r.push t
  d -= [a[i]]
end
k.times do |i|
  (n-1).times do
    r[i].push d[0]
    d.shift
  end
end
r.each do |x|
  puts x*' '
end

nk = gets.split
wishes = gets.split
n = nk[0].to_i
k = nk[1].to_i
a = Array.new(n*k){|i| i+= 1}
res = []
i = 0
while i<k do
    wishes[i] = wishes[i].to_i
    res[i] = [wishes[i]]
    i += 1
end
a = a - wishes
i = 0
while i<k do
    c = a[0...n-1]
    res[i] += c
    a = a - c
    res[i] = res[i].sort
    j = 0
    while j<n do
        print "#{res[i][j]} "
        j += 1
    end
    print "\n"
    i += 1
end

n,k=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
an=Array.new
a.each{|e| an<<[e]}
b=(1..(n*k)).to_a-a
for i in 0...k
	(n-1).times{an[i]<<b[0];b.delete_at(0)}
end
an.each{|e| puts e.join(" ")}

n, k = gets.chomp.split(" ").map{|e| e.to_i}
a = gets.chomp.split(" ").map{|e| e.to_i}
numbers = []
for i in 1 .. n * k do
  numbers << i
end
ans = []
k.times do
  ans << []
end

a.each_with_index do |num, idx|
  ans[idx] << num
  numbers.delete num
end

numbers.each_with_index do |num, idx|
  ans[idx % k] << num
end

ans.each do |line|
  puts line.join(" ")
end

n,k=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
b=(1..(n*k)).to_a-a
for i in 0...k do
	print a[i]," ",b[0...n-1].join(" ")
	print "\n"
end