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

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

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

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

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

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

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

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

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

  count[k] += 1
end

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

end

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

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

funcs = []
vars = []

n = gets.to_i

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

m = gets.to_i

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

k = gets.to_i

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

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

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


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

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

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

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

p max

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

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

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

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

puts min

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

n, w = get_ints
cup = get_ints

cup.sort!

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

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

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

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

a.sort!

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  max = 3 * n * x

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

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

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

x = a.max / 2.0

max = 3 * n * x

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


# your code goes here

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

#!/bin/ruby

require 'set'

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

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

count = false
h = Hash.new

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

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

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

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

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

def oops
	puts 'LIE'
	exit
end

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

puts 'TRUTH'

# require 'set'

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

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

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

#!/bin/ruby

require 'set'

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

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

count = false
h = Hash.new

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

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

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

#!/bin/ruby

require 'set'

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

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

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

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

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

#!/bin/ruby

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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


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

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

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

puts c

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

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

ans = 0

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

puts ans

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

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

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

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

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

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

puts count

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

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

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

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

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

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

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

count_img = gets.to_i

img = Array.new(count_img)

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

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

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

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

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

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

puts sum

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

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

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

puts ans

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

  return n, show
end

count_img = gets.to_i

img = Array.new(count_img)

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

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

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

def main
  count_img = gets.to_i

  img = Array.new(count_img)

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

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

  p n
end

main

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

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

count_img = gets.to_i

img = Array.new(count_img)

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

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

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

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

count_img = gets.to_i

img = Array.new(count_img)

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

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

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

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

count_img = gets.to_i

img = Array.new(count_img)

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

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

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

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

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

puts sum


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

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

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

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

counter = Hash.new(0)

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

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

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

  puts ans % M
end

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

puts ans

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

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

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

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

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

# Love Triangles

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

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

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

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

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

  return false
end

n = gets.chomp
f = gets.chomp

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

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

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

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


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

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

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

            i /= 2
        end
    end

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

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

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

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

            n /= 2
        end
    end

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

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

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

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

            i = (i / 2).round
        end
    end

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

require 'set'

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

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

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

            i = (i / 2).round
        end
    end

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

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

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

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

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

  solution_exists = true

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

    if !found
      solution_exists = false
      break
    end
  end

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

require 'set'

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

      e /= 2
    end
  end

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


n, m = gets.split.map(&:to_i)
r, c = Array.new(n + 1), Array.new(n + 1)
n.times do |i|
  r[i] = c[i] = 0
end
m.times do
  x, y = gets.split.map(&:to_i)
  r[x], c[y] = 1, 1
end
ans = 0
(2...n).step(1) do |i|
  ans += 1 if r[i].zero?
  ans += 1 if c[i].zero?
end
ans -= 1 if (n.odd? && r[n / 2 + 1].zero? && c[n / 2 + 1].zero?)
p ans

n, m = gets.split.map(&:to_i)
r, c = Array.new(n + 1){0}, Array.new(n + 1){0}
m.times do
  x, y = gets.split.map(&:to_i)
  r[x], c[y] = 1, 1
end
ans = 0
(2...n).step(1){|i| ans += (r[i].zero?? 1 : 0) + (c[i].zero?? 1 : 0)}
ans -= 1 if (n.odd? && r[n / 2 + 1].zero? && c[n / 2 + 1].zero?)
p ans

n, m = gets.split.map(&:to_i)
rows = Array.new(n, true)
rows[0] = rows[-1] = false
cols = rows.dup
for line in (1..m)
	r, c = gets.split.map(&:to_i)
	rows[r-1] = false
	cols[c-1] = false
end
rows[n/2] = false if (n%2 == 1 and cols[n/2])
puts rows.count(true) + cols.count(true)

n, m = gets.split.map(&:to_i)
rows = Array.new(n, true)
rows[0] = rows[-1] = false
cols = rows.dup
for line in (1..m)
	r, c = gets.split.map(&:to_i)
	rows[r-1] = false
	cols[c-1] = false
end
rows[n/2] = false if (n & 1 and cols[n/2])
puts rows.count(true) + cols.count(true)

n, m = gets.split.map(&:to_i)
rows = Array.new(n, true)
rows[0] = rows[-1] = false
cols = rows.dup
for line in (1..m)
	r, c = gets.split.map(&:to_i)
	rows[r-1] = false
	cols[c-1] = false
end
rows[n/2] = false if (n/2 == 1 and cols[n/2])
puts rows.count(true) + cols.count(true)


n, k = gets.split.map(&:to_i)
puts 2 * k >= n * (n - 1) ? "no solution" : [*1..n].map{|i|"0 "+i.to_s}

#!/usr/bin/ruby1.9

point_num, limit = STDIN.readline.split.collect {|s| s.to_i }

if point_num*(point_num-1)/2 <= limit
	puts 'no solution'
	exit
end

(point_num-1).times do |i|
	puts '%d %d' % [i, i*10000]
end
puts '%d %d' % [point_num, 0]

n,k = gets().split().map{ |x| x.to_i}
if (1.0*n*(n-1)/2 <= k)
    puts "no solution";
else
    (1..n).each do |i|
       puts "0 %d" % [i];
    end
end

input = STDIN.read

input =~ /^(\d+) (\d+)/
n, k  = $1.to_i, $2.to_i
l = (n * (n - 1)) / 2

if (k >= l)
  puts 'no solution'
else
  (0...n).each { |y| puts "0 #{y}" }
end

n, k = gets.chomp.split.map { |num| num.to_i }
if k >= ((n - 1) * n / 2)
  puts "no solution"
else
  (1..n).each do |i|
    puts "0 #{i}"
  end
end

n,k = gets.chomp.split(" ").collect {|x| x.to_i}

if k>=(n*(n-1)/2)
  puts "no solution"
else
  (1..n).each { |k|
    puts "0 #{k}"
  }
end

n,k = gets.strip.split(' ').map {|x| x.to_i}

if( k>= (n*(n-1))/2 )
  puts 'no solution'
else
  n.times do |i|
    puts "0 #{i*2}"
  end
end

#!/usr/bin/ruby

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

tot = n * (n - 1) / 2

if tot <= k
  puts "no solution"
else
  0.upto(n - 1) {|y| puts "0 #{y}" }
end

n, k = gets.split.map(&:to_i)
if (n - 1) * n > k * 2
  n.times do |i|
    puts [0, i].join(' ')
  end
else
  puts "no solution"
end

n, k = gets.split.map(&:to_i)
if (n - 1) * n > k
  n.times do |i|
    puts [0, i].join(' ')
  end
else
  puts "no solution"
end

n,k = gets().split().map{ |x| x.to_i}
if (1.0*n*(n-1)/2 < k)
    puts "no solution";
else
    (1..n).each do |i|
       puts "0 %d" % [i];
    end
end

input = STDIN.read

input =~ /^(\d+) (\d+)/
n, k  = $1.to_i, $2.to_i
l = (n * (n - 1)) / 2

if (k > l)
  puts 'no solution'
else
  (0...n).each { |y| puts "0 #{y}" }
end

n,k = gets.chomp.split(" ").collect {|x| x.to_i}

if k>(n*(n-1)/2)
  puts "no solution"
else
  (1..n).each { |k|
    puts "0 #{k}"
  }
end

n,k = gets.chomp.split(" ").collect {|x| x.to_i}

if k>(n*(n+1)/2)
  puts "no solution"
else
  (1..n).each { |k|
    puts "0 #{k}"
  }
end


gets
a=gets.split.map(&:to_i).sort
puts gets.split.map(&:to_i).map{|e|
	(0...a.size).bsearch{|i|a[i]>e}||a.size
}*' '

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

as.sort!
cnt = 0
hs = Hash.new(0)
as.each do |a|
  cnt += 1
  hs[a] = cnt
end
as.reverse!
cs = []
bs.each do |b|
 cs << hs[as.bsearch {|a| a <= b }]
end

puts cs.*' '

n, m = gets.chomp.split(" ").map(&:to_i)
a = gets.chomp.split(" ").map(&:to_i).sort
b = gets.chomp.split(" ").map(&:to_i)
res = Array.new()
b.each do |e|
  tmp = (0...n).bsearch {|i| a[i] > e}
  if tmp.nil? then
    res << n
  else
    res << tmp
  end
end
puts res.join(" ")


arr1 = gets.chomp.split(/ /).map(&:to_i)
num = arr1[0]
floor_max = arr1[1]
pass = Array.new
time = Array.new
index=0
num.times do
	str = gets.chomp.split(/ /).map(&:to_i)
	pass[index] = str[0]
	time[index] = str[1]
	index+=1
end

_final=0

index=floor_max
loop do
	break if index < 0
	index1=0
	loop do
		break if index1>=num
		if pass[index1] == index && time[index1]>_final
			_final = time[index1]
		end
		index1+=1
	end
	#puts "Final is #{_final} and index is #{index}"

	index=index-1
	break if index < 0
	_final+=1
end

puts _final

#!/usr/bin/ruby
n,f=gets.split.map(&:to_i)
r=f
n.times{
	a,b=gets.split.map(&:to_i)
	r=[r,[f-a,b].max+a].max
}
p r

n, s = gets.chomp.split.map(&:to_i)
floors = []
n.times { floors << gets.chomp.split.map(&:to_i) }

c = 0
idx = s
floors.reverse.each do |a, b|
  c += (idx - a)
  c += (b > c) ? (b - c) : 0
  idx = a
end
puts c + idx

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.chomp end
def gi() gets.to_i end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|r,e| r[e]+=1;r} end
############################################################

n, s = gis

mx = s

n.times do
    f, t = gis
    mx = [f + t, mx].max
end

puts mx

n, s = gets.split.map(&:to_i)
n.times do
	t, f = gets.split.map(&:to_i)
	s = [s, t+f].max
end
puts s

def Int_maker(a)
	0.upto(a.length-1) {|i| a[i]=a[i].to_i }
	return a
end
info=Int_maker(gets.chomp.split(" "))
passenger=[]
0.upto(info[0]-1){|i| passenger[i]=Int_maker(gets.chomp.split(" "))}
passenger.sort!
passenger.reverse!
k=info[1]
time=0
0.upto(info[0]-1) do |i|
	if i==0
		time=k-(passenger[0][0])
		if time<(passenger[i][1])
			time=(passenger[i][1])
		end
	else
		time=time+(passenger[i-1][0])-(passenger[i][0])
		if time<(passenger[i][1])
			time=(passenger[i][1])
		end
	end
end
time=time+passenger[passenger.length-1][0]
puts time

a = gets.split.map(&:to_i)
n = a[0]
s = a[1]

max = s
(1..n).each do
	a = gets.split.map(&:to_i)
	sum = a[0] + a[1]
	max = sum if sum > max
end

puts max

n, s = gets.chomp.split.map(&:to_i)
ans = s
n.times do
  f, t = gets.chomp.split.map(&:to_i)
  ans = [ans, f + t].max
end
puts ans

sum, top_floor = gets.split.map(&:to_i)
wait = Hash.new{|hsh,key| hsh[key] = [] }
wait[0].push 0

for i in 0..sum-1
    f_t = gets.split
    floor = f_t[0].to_i
    time = f_t[1].to_i
	wait[floor].push time
end

wait = wait.sort.reverse
all_time = 0

for i in 0..wait.length-1

    shift = wait.shift
    c_floor = shift[0]
    c_floor_time = shift[1]

	if top_floor > c_floor
		all_time += top_floor - c_floor
		top_floor = c_floor
	end

	if top_floor.to_i == c_floor.to_i
		c_floor_time = c_floor_time.sort.reverse
		c_floor_time[0] = c_floor_time[0] - all_time

		if c_floor_time[0] < 0
			c_floor_time[0] = 0
		end

		all_time +=c_floor_time[0]
	end
end
p all_time

n, s = gets.split.map(&:to_i)
a = n.times.map{gets.split.map(&:to_i)}.reverse + [[0, 0]]
current_floor = s
time_spent = 0
a.each do |f, t|
  time_spent += [current_floor - f, t - time_spent].max
  current_floor = f
end
puts time_spent

a = gets.split(' ').collect {|x| x.to_i}
n = a[0]
s = a[1]
result = s
(1..n).each do
  a = gets.split(' ').collect {|x| x.to_i}
  result = a[0] + a[1] if a[0]+a[1] > result
end

puts result

n, s = gets.chomp.split(" ").map(&:to_i)
psgs = Array.new()
n.times do
  psgs << gets.chomp.split(" ").map(&:to_i)
end
psgs = psgs.sort_by{|x| x[0]}.reverse
res = 0
current = s
psgs.each do |f, t|
  res += current - f
  current = f
  if res < t then
    res = t
  end
end
if current != 0 then
  res += current
end
puts res

n, s = gets.split.map(&:to_i)
map = Hash.new
(1..n).each do
  floor, time = gets.split.map(&:to_i)
  if map[floor] == nil
    map[floor] = time
  else
    map[floor] = [map[floor], time].max
  end
end

curr = s
time = 0
s.downto(1).each do |floor|
  if map[floor] == nil
    next
  end
  time = [time+curr-floor, map[floor]].max
  curr = floor
end

time += curr

puts time

class Solver
	def main
		n, s = gets.split.map { |x| x.to_i }
		ans = []
		n.times do
			f, t = gets.split.map { |x| x.to_i }
			ans << [t, s - f].max + f
		end
		puts ans.max
	end
end
Solver.new.main

def Int_maker(a)
	0.upto(a.length-1) {|i| a[i]=a[i].to_i }
	return a
end
info=Int_maker(gets.chomp.split(" "))
passenger=[]
0.upto(info[0]-1){|i| passenger[i]=Int_maker(gets.chomp.split(" "))}
passenger.sort!
passenger.reverse!
k=info[1]-1
time=0
0.upto(info[0]-1) do |i|
	if i==0
		time=k-(passenger[0][0])
		if time<(passenger[i][1])
			time=(passenger[i][1])
		end
	else
		time=time+(passenger[i-1][0])-(passenger[i][0])
		if time<(passenger[i][1])
			time=(passenger[i][1])
		end
	end
end
time=time+passenger[passenger.length-1][0]
puts time

def Int_maker(a)
	0.upto(a.length-1) {|i| a[i]=a[i].to_i }
	return a
end
info=Int_maker(gets.chomp.split(" "))
passenger=[]
0.upto(info[0]-1){|i| passenger[i]=Int_maker(gets.chomp.split(" "))}
passenger.sort!
passenger.reverse!
k=info[1]-1
time=0
0.upto(info[0]-1) do |i|
	if i==0
		time=k-(passenger[0][0])
	else
		time=time+(passenger[i-1][0])-(passenger[i][0])
		if time<(passenger[i][1])
			time=(passenger[i][1])
		end
	end
end
time=time+passenger[passenger.length-1][0]
puts time

n, s = gets.chomp.split.map(&:to_i)
ans = s + 1
n.times do
  f, t = gets.chomp.split.map(&:to_i)
  ans = [ans, f + t].max
end
puts ans

s_f = gets.split
sum = s_f[0].to_i
top_floor = s_f[1].to_i

wait = Hash.new
for i in 0..sum-1
	f_t = gets.split
	floor = f_t[0].to_i
	time = f_t[1].to_i
	wait[floor] = time
end

wait = wait.sort.reverse
step_flag = 0

all_time = 0
for i in 0..sum-1
	#Записываем значение wait в top и wait_time
	shift = wait.shift
	c_floor = shift[0]
	c_wait_time = shift[1]
	if step_flag==0
		step = top_floor - c_floor
	end
	step_flag = 1

	#Время ожидания у текущего пассажира
	wait_time = c_wait_time - all_time
	if wait_time < 0
		wait_time = 0
	end

	all_time += wait_time
end
all_time+=step+1
puts all_time

n, s = gets.split.map(&:to_i)
map = Hash.new
(1..n).each do
  floor, time = gets.split.map(&:to_i)
  map[floor] = time
end

curr = s
time = 0
s.downto(0).each do |floor|
  if map[floor] == nil
    next
  end
  time = [time+curr-floor, map[floor]].max
  curr = floor
end

time += curr

puts time


N = gets.to_i
A = gets.split.map &:to_i
ss = [0]
A.each{ |a| ss << (ss[-1] + a) }
segs = {}
(1..N).each{ |j|
  (1..j).each{ |i|
    seg = [i,j]
    v = ss[j] - ss[i-1]
    segs[v] ||= []
    segs[v] << seg
  }
}

seqs = []
segs.each{ |v,ss|
  rb = 0
  sss = []
  ss.each{ |l,r|
    if rb < l
      sss << [l,r]
      rb = r
    end
  }
  if seqs.size < sss.size
    seqs = sss
  end
}

p seqs.size
seqs.each{ |a,b|
  puts [a,b] * " "
}

N = gets.to_i
A = gets.split.map &:to_i
ss = [0]
A.each{ |a| ss << (ss[-1] + a) }
ans = 0
seqs = []
(0...N).each{ |j|
  (0..j).each{ |i|
    m = 1
    qs = [[i+1,j+1]]
    d = ss[j+1] - ss[i]
    k0 = j+1
    (j+1...N).each{ |k|
      (k0..k).each{ |l|
        e = ss[k+1] - ss[l]
        if d == e
          m += 1
          qs << [l+1,k+1]
          k0 = k+1
          break
        end
      }
    }
    if m > ans
      ans = m
      seqs = qs
    end
  }
}
puts ans
seqs.each{ |a,b| puts [a,b] * " " }

N = gets.to_i
A = gets.split.map &:to_i
ss = [0]
A.each{ |a| ss << (ss[-1] + a) }
ans = 0
seqs = []
(0...N).each{ |j|
  (0..j).each{ |i|
    m = 1
    qs = [[i+1,j+1]]
    d = ss[j+1] - ss[i]
    k0 = j+1
    (j+1...N).each{ |k|
      (k0..k).each{ |l|
        e = ss[k+1] - ss[l]
        if d == e
          m += 1
          qs << [l+1,k+1]
          k0 = k+1
        end
      }
    }
    if m > ans
      ans = m
      seqs = qs
    end
  }
}
puts ans
seqs.each{ |a,b| puts [a,b] * " " }

N = gets.to_i
A = gets.split.map &:to_i
ss = [0]
A.each{ |a| ss << (ss[-1] + a) }
ans = 0
seqs = []
p A, ss
(0...N).each{ |j|
  (0..j).each{ |i|
    m = 1
    qs = [[i+1,j+1]]
    d = ss[j+1] - ss[i]
    k0 = j+1
    (j+1...N).each{ |k|
      (k0..k).each{ |l|
        e = ss[k+1] - ss[l]
        if d == e
          m += 1
          qs << [l+1,k+1]
          k0 = k+1
        end
      }
    }
    if m > ans
      ans = m
      seqs = qs
    end
  }
}
puts ans
seqs.each{ |a,b| puts [a,b] * " " }


t = gets.chomp.to_i
t.times do
  n = gets.chomp.to_i
  arr = Array.new(n){[0] * n}
  n.times do |i|
    arr[i][i] = 1
    if (i + 1 < n)
      arr[i][i + 1] = 1
    end
  end
  arr[1][0] = arr[n - 1][n - 2] = 1
  n.times do |i|
    puts arr[i].join(" ")
  end
end

T = gets.to_i

T.times do
  n = gets.to_i
  ans = Array.new(n) { Array.new(n, 0) }

  n.times do |y|
    2.times do |x|
      ans[y][(y + x) % n] = 1
    end
  end

  puts ans.map { |row| row.join(' ') }
end

t = gets.to_i

1.upto t do
  n = gets.to_i
  arr = Array.new(n) { Array.new(n, 0) }

  0.upto n-1 do |i|
    arr[i][i] = 1
    arr[i][n-1-i] = 1
  end

  if n.odd?
    arr[(n-1)/2][((n-1)/2)+1] = 1
    arr[(n+1)/2][(n-1)/2] = 1
  end

  puts arr.map { |x| x.join(' ') }
end


def method(h, n)
  res = 0
  (0..2).each { |v| h[v] = 0 unless h.key?(v) }
  while h.max_by { |k, v| v }[1] > n
    k, v = h.each.max_by { |k, v| v }
    h[k] = n
    h[(k + 1) % 3] += v - n
    res += v - n
  end
  res
end

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  puts method(a.group_by { |x| x % 3 }.transform_values(&:size), n / 3)
end

def method(h, n)
  res = 0
  (0..2).each { |v| h[v] = 0 unless h.key?(v) }
  while h.each_value.max > n
    k, v = h.each.max_by { |k, v| v }
    h[k] = n
    h[(k + 1) % 3] += v - n
    res += v - n
  end
  res
end

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  puts method(a.group_by { |x| x % 3 }.transform_values(&:size), n / 3)
end
#

def method(h, n)
  res = 0
  (0..2).each { |v| h[v] = 0 unless h.key?(v) }
  h.each do |k, v|
    if v > n
      res += v - n
      h[k] = n
      h[(k + 1) % 3] += v - n
    end
  end while h.values.max > n
  res
end

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  puts method(a.group_by { |x| x % 3 }.transform_values(&:size), n / 3)
end

def method(h, n)
  res = 0
  (0..2).each { |v| h[v] = 0 unless h.key?(v) }
  while h.values.max > n
    k, v = h.each.max_by { |k, v| v }
    h[k] = n
    h[(k + 1) % 3] += v - n
    res += v - n
  end
  res
end

t = gets.to_i
t.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  puts method(a.group_by { |x| x % 3 }.transform_values(&:size), n / 3)
end
#

# frozen_string_literal: true

test = gets.to_i
test.times do
  n = gets.chomp.to_i
  numbers = gets.chomp.split.map(&:to_i)
  remainders = []
  (0..2).each do |index|
    remainders << numbers.count{|x| x % 3 == index}
  end

  result = 0
  while idx = (0..2).detect{|x| remainders[x] > n / 3}
    result += remainders[idx] - n / 3
    remainders[(idx+1)% 3] += remainders[idx] - n / 3
    remainders[idx] = n / 3
  end
  puts result
end

gets.to_i.times do
	n = gets.to_i
	a = gets.split.map(&:to_i)
	c = (0...3).map { |k| a.count { _1 % 3 == k } }

	ans = 0
	while true
		i = c.index { _1 > n / 3 }
		break unless i
		ans += 1
		c[i] -= 1
		c[-~i % 3] += 1
	end
	puts ans
end

def solve
  count = [0, 0, 0]
  n = gets.to_i
  arr = gets.split(' ').map(&:to_i)
  arr.each do |i|
    count[i % 3] += 1
  end

  ans = 0

  2.times do
    3.times do |i|
      if count[i] > n / 3
        ans += count[i] - n / 3
        count[(i + 1) % 3] += count[i] - n / 3
        count[i] = n / 3
      end
    end
  end

  puts ans
end

t = gets.to_i
t.times { solve }

gets.to_i.times do
  n=gets.to_i/3
  a=gets.split.map(&:to_i)
  c0,c1,c2=0,0,0
  a.each do |aa|
    if aa%3==0
      c0+=1
    elsif aa%3==1
      c1+=1
    else
      c2+=1
    end
  end
  c=0
  while !(c0==c1&&c1==c2)
    if c0>n
      c1+=c0-n
      c+=c0-n
      c0=n
    elsif c1>n
      c2+=c1-n
      c+=c1-n
      c1=n
    elsif c2>n
      c0+=c2-n
      c+=c2-n
      c2=n
    end
  end
  puts c
end


def solve(a, p)
  q = a.map do |l, r|
    (r / p - (l - 1) / p).to_f / (r - l + 1)
  end
  2000 * ( 2*q[1..-1].reduce(0, :+) - q.each_cons(2).map {|q1, q2| q1*q2}.reduce(0, :+))
end

n, p = gets.split.map(&:to_i)
a = Array.new(n+1) do |i|
  next if i == n
  gets.split.map(&:to_i)
end
a[-1] = a[0]

puts solve(a, p)

n, p = gets.chomp.split(" ").map(&:to_i)
sharks = []

n.times do
  l, r = gets.chomp.split(" ").map(&:to_i)

  x, y = l, r
  if x % p != 0
    x += p-x%p
  end
  y -= y%p
  dividends = (y-x)/p+1

  sharks << {l: l, r: r, dividends: dividends, total: r-l+1}
end

sharks << sharks.first

# puts sharks.inspect

sum = sharks.each_cons(2).map do |shark_a, shark_b|
  shark_a_not_p = 1 - shark_a[:dividends]/shark_a[:total].to_f
  shark_b_not_p = 1 - shark_b[:dividends]/shark_b[:total].to_f

  (1 - (shark_a_not_p * shark_b_not_p))*2000
end.inject(0, :+)

puts sum

n, p = gets.chomp.split(" ").map(&:to_i)
sharks = []

n.times do
  l, r = gets.chomp.split(" ").map(&:to_i)

  x, y = l, r
  if x % p != 0
    x += p-x%p
  end
  y -= y%p
  dividends = (y-x)/p+1

  sharks << {l: l, r: r, dividends: dividends, total: r-l+1}
end

sharks << sharks.first

puts sharks.inspect

sum = sharks.each_cons(2).map do |shark_a, shark_b|
  shark_a_not_p = 1 - shark_a[:dividends]/shark_a[:total].to_f
  shark_b_not_p = 1 - shark_b[:dividends]/shark_b[:total].to_f

  (1 - (shark_a_not_p * shark_b_not_p))*2000
end.inject(0, :+)

puts sum

n, p = gets.chomp.split(" ").map(&:to_i)
sharks = []

n.times do
  l, r = gets.chomp.split(" ").map(&:to_i)
  dividends = (r-l+1)/p
  sharks << {l: l, r: r, dividends: dividends, total: r-l+1}
end

sharks << sharks.first

sum = sharks.each_cons(2).map do |shark_a, shark_b|
  shark_a_not_p = 1 - shark_a[:dividends]/shark_a[:total].to_f
  shark_b_not_p = 1 - shark_b[:dividends]/shark_b[:total].to_f

  (1 - (shark_a_not_p * shark_b_not_p))*2000
end.inject(0, :+)

puts sum


m = Array.new
n = gets.chomp.to_i
t = gets.chomp.split(/ /)
t.each {|u| m << u.to_i}
m.sort!

s = 0
m.each {|t| s = s + t}

hash = Hash.new
if (s % n == 0)
	hash[s / n] = n
else
	hash[s/n] = n - s % n
	hash[s/n+1] = s % n
end

sec = 0
if hash.size == 1
			m.each {|m_i|
				sec = sec + (m_i - s/n).abs
			}
else
	m.each {|m_i|
		if m_i != 0
			if m_i <= s / n
				if hash[s/n] > 0
					sec = sec + (s/n - m_i).abs
					hash[s/n] = hash[s/n] - 1
				elsif hash[s/n + 1] > 0
					sec = sec + (m_i - s/n - 1).abs
					hash[s/n + 1] = hash[s/n + 1] - 1
				end
			elsif m_i >= s/n + 1
			  if hash[s/n + 1] > 0
					sec = sec + (m_i - s/n - 1).abs
					hash[s/n + 1] = hash[s/n + 1] - 1
				elsif hash[s/n] > 0
					sec = sec + (s/n - m_i).abs
					hash[s/n] = hash[s/n] - 1
				end
			end
		end
	}
end

print sec / 2

m = Array.new
n = gets.chomp.to_i
t = gets.chomp.split(/ /)
t.each {|u| m << u.to_i}
m.sort!

s = 0
m.each {|t| s = s + t}

hash = Hash.new
if (s % n == 0)
	hash[s / n] = n
else
	hash[s/n] = n - s % n
	hash[s/n+1] = s % n
end

for i in 0..m.size do
	if hash.has_key?(m[i]) && hash[m[i]] != 0
		hash[m[i]] = hash[m[i]] - 1
		if hash[m[i]] == 0
			hash.delete m[i]
		end
		m[i] = 0
	end
end

sec = 0
if hash.size == 1
	hash.each_key {|key|
		if hash[key] != 0
			m.each {|m_i|
				if m_i != 0
					sec = sec + (m_i - key).abs
				end
			}
		end
	}
else
	m.each {|m_i|
		if m_i != 0
			if m_i < s / n
				if hash[s/n] > 0
					sec = sec + (s/n - m_i).abs
					hash[s/n] = hash[s/n] - 1
				elsif hash[s/n + 1] > 0
					sec = sec + (m_i - s/n - 1).abs
					hash[s/n + 1] = hash[s/n + 1] - 1
				end
			elsif m_i > s/n + 1
			  if hash[s/n + 1] > 0
					sec = sec + (m_i - s/n - 1).abs
					hash[s/n + 1] = hash[s/n + 1] - 1
				elsif hash[s/n] > 0
					sec = sec + (s/n - m_i).abs
					hash[s/n] = hash[s/n] - 1
				end
			end
		end
	}
end

print sec / 2

m = Array.new
n = gets.chomp.to_i
t = gets.chomp.split(/ /)

s = 0
t.each {|u| m << u.to_i; s = s + u.to_i}
m.sort!

hash = Hash.new
if (s % n == 0)
	hash[s / n] = n
else
	hash[s/n] = n - s % n
	hash[s/n+1] = s % n
end

sec = 0
if hash.size == 1
			m.each {|m_i|
				sec = sec + (m_i - s/n).abs
			}
else
	m.each {|m_i|
		if m_i <= s / n
			if hash[s/n] > 0
				sec = sec + (s/n - m_i).abs
				hash[s/n] = hash[s/n] - 1
			else
				sec = sec + (m_i - s/n - 1).abs
				hash[s/n + 1] = hash[s/n + 1] - 1
			end
		else
		  if hash[s/n + 1] > 0
				sec = sec + (m_i - s/n - 1).abs
				hash[s/n + 1] = hash[s/n + 1] - 1
			else
				sec = sec + (s/n - m_i).abs
				hash[s/n] = hash[s/n] - 1
			end
		end
	}
end

print sec / 2

n = gets.chomp.to_i
a = gets.split.map(&:to_i)
sum = a.reduce(0, :+)
lower_median = sum / n
upper_median = lower_median + (sum % n == 0 ? 0 : 1)
upper_total = 0
lower_total = 0
a.each do |val|
  lower_total += lower_median - val if val < lower_median
  upper_total += val - upper_median if val > upper_median
end
puts lower_total > upper_total ? lower_total : upper_total

#!/usr/bin/env ruby

gets
m = gets.split.map(&:to_i)
avg = m.inject(:+) / m.size
overage = m.inject(:+) - (avg * m.size)
bal = 0
m.sort!
m.reverse!
m.each do |l|
  if l > avg
    if overage > 0
      bal += l - (avg + 1)
      overage -= 1
    else
      bal += l - avg
    end
  else
    if overage > 0
      bal += (avg + 1) - l
      overage -= 1
    else
      bal += avg - l
    end
  end
end
puts bal / 2

# Accept at
n = gets.to_i
a = gets.split(' ').map { |x|
  x.to_i
}
sum = a.inject { |sum, t| sum += t}
ave = sum / a.length
n1 = sum - ave * a.length
result = 0
tmp = 0
a.each { |t|
  result += t - ave - 1 if t >= ave+1
  tmp += 1 if t >= ave+1
}

result += tmp - n1 if tmp > n1
puts result

n = gets.chomp.to_i
m = gets.chomp.split(" ").map(&:to_i).sort
msum = m.inject(:+)
mmod = msum % n
if mmod == 0
  mm = Array.new(n, msum / n)
else
  mm = Array.new(n - mmod, msum / n) + Array.new(mmod, msum / n + 1)
end
res = 0
m.zip(mm).each do |a, b|
  res += (a - b).abs
end
puts res / 2

n = gets.to_i
m = gets.split(' ').map{ |x| x.to_i }
msum = m.inject{|sum,x| sum+x}
avg = msum / n
ans1 = ans2 = 0
for iter in m
    if iter < avg
        ans1 += avg - iter
    end
    if iter > avg
        ans2 += iter - avg - 1
    end
end
puts [ans1, ans2].max

n = gets.to_i
servers = gets.split.map(&:to_i).sort.reverse
sum = servers.reduce(&:+)
min_load = sum/servers.length
extra = sum%servers.length
optimal = Array.new(n, min_load)
for i in 0...extra
  optimal[i] += 1
end
count = 0
for i in 0...n
  count += (servers[i]-optimal[i]).abs
end
puts count/2

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

count = 0
while a[n - 1] - a[0] > 1 do
  # balance
  diff = a[n - 1] - a[0]
  a[0] += diff / 2
  a[n - 1] -= diff / 2
  a[n - 1] -= 1 if diff % 2 != 0
  count += diff / 2

  # resort
  i = n - 1
  while a[i] < a[i - 1] do
    temp = a[i]
    a[i] = a[i - 1]
    a[i - 1] = temp
    i -= 1
  end
  i = 0
  while a[i] > a[i + 1] do
    temp = a[i]
    a[i] = a[i + 1]
    a[i + 1] = temp
    i += 1
  end
end

puts count

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

count = 0
while a[n - 1] - a[0] > 1 do
  # balance
  a[0] += 1
  a[n - 1] -= 1

  # resort
  i = n - 1
  while a[i] < a[i - 1] do
    temp = a[i]
    a[i] = a[i - 1]
    a[i - 1] = temp
  end
  i = 0
  while a[i] > a[i + 1] do
    temp = a[i]
    a[i] = a[i + 1]
    a[i + 1] = temp
  end
  count += 1
end

puts count

#!/usr/bin/env ruby

gets
m = gets.split.map(&:to_i)
avg = m.inject(:+) / m.size
overage = m.inject(:+) - (avg * m.size)
bal = 0
m.sort!
m.reverse!
m.each do |l|
  p overage, bal
  if l > avg
    if overage > 0
      bal += l - (avg + 1)
      overage -= 1
    else
      bal += l - avg
    end
  else
    if overage > 0
      bal += (avg + 1) - l
      overage -= 1
    else
      bal += avg - l
    end
  end
end
puts bal

#!/usr/bin/env ruby

gets
m = gets.split.map(&:to_i)
avg = m.inject(:+) / m.size
overage = m.inject(:+) - (avg * m.size)
bal = 0
m.sort!
m.reverse!
m.each do |l|
  if l > avg
    if overage > 0
      bal += l - (avg + 1)
      overage -= 1
    else
      bal += l - avg
    end
  else
    if overage > 0
      bal += (avg + 1) - l
      overage -= 1
    else
      bal += l - avg
    end
  end
end
puts bal

#!/usr/bin/env ruby

gets
m = gets.split.map(&:to_i)
m.sort!
c = 0
while m.last - m.first > 1
  diff = m.last - m.first
  move = diff / 2
  m[0] += move
  m[m.size - 1] -= move
  m.sort!
  c += move
end
puts c

#!/usr/bin/env ruby

gets
m = gets.split.map(&:to_i)
avg = m.inject(:+) / m.size
bal = 0
m.each do |l|
  if l < avg
    bal += avg - l
  end
end
puts bal

#!/usr/bin/env ruby

gets
m = gets.split.map(&:to_i)
avg = m.inject(:+) / m.size
m.sort!
c = 0
while m.last - m.first > 1
  move = [avg - m.first, m.last - (avg + 1)].max
  diff = m.last - m.first
  m[0] += move
  m[m.size - 1] -= move
  m.sort!
  c += move
end
puts c

n = gets.to_i
a = gets.split(' ').map { |x|
  x.to_i
}
sum = a.inject { |sum, t| sum += t}
ave = sum / a.length
n1 = sum - ave * a.length
result = 0
a.each { |t|
  result += t - ave if t >= ave
}
puts result - n1

n = gets.to_i
a = gets.split(' ').map { |x|
  x.to_i
}
m = a.inject { |sum, t| sum += t} / 1.0 / a.length
result = 0
a.each { |t|
  result += t - m.ceil if t >= m
}
puts result

n = gets.to_i
a = gets.split(' ').map { |x|
  x.to_i
}
sum = a.inject { |sum, t| sum += t}
ave = sum / a.length
even= true if (ave * a.length == sum)

ceil = ave
ceil = ave+1 unless even

result = 0
a.each { |t|
  result += t - ceil if t >= ceil
}
puts result

n = gets.chomp.to_i
loads = gets.chomp.split(" ").map(&:to_i)
m = loads.inject(:+) / n
loads = loads.select{|i| i - m < 0}
if loads.empty? then
  puts 0
else
  puts loads.map{|i| i - m}.inject(:+).abs()
end

n = gets.to_i
m = gets.split(' ').map{ |x| x.to_i }
msum = m.inject{|sum,x| sum+x}
avg = msum / n
ans1 = 0
for iter in m
    if iter < avg
        ans1 += avg - iter
    end
end
puts ans1

n = gets.to_i
servers = gets.split.map(&:to_i)
sum = servers.reduce(&:+)
min_load = sum/servers.length
count = 0
for server in servers
  if server < min_load
    count += min_load-server
  end
end

puts count

n = gets.to_i
servers = gets.split.map(&:to_i)
sum = servers.reduce(&:+)
min_load = sum/servers.length
extra = sum-(min_load*servers.length)
count = 0
for server in servers
  if server > min_load
    count += server-min_load
  end
end

puts count-extra

n = gets.to_i
servers = gets.split.map(&:to_i).sort.reverse
sum = servers.reduce(&:+)
min_load = sum/servers.length
count = 0
for server in servers
  count += (server-min_load).abs
end

puts count/2

m = Array.new
n = gets.chomp.to_i
t = gets.chomp.split(/ /)
t.each {|u| m << u.to_i}
m.sort!

s = 0
m.each {|t| s = s + t}

hash = Hash.new
if (s % n == 0)
	hash[s / n] = n
else
	hash[s/n] = n - s % n
	hash[s/n+1] = s % n
end

m.each {|x|
	if hash.has_key? x
		hash[x] = hash[x] - 1
		m.delete x
		if hash[x] == 0
			hash.delete x
		end
	end
}

sec = 0
if hash.size == 1
	hash.each_key {|key|
		m.each {|m_i|
			sec = sec + (m_i - key).abs
		}
	}
	print sec / 2
else
	hash.each_key { |key|
		m.each {|m_i|
			if m_i < hash[s/n]
					sec = sec + (s/n - m_i).abs
					hash[s/n] = hash[s/n] - 1
			elsif hash[s/n + 1] > 0
				if m_i > hash[s/n + 1]
					sec = sec + (m_i - s/n + 1).abs
					hash[s/n + 1] = hash[s/n + 1] - 1
				end
			end
		}
	}
	print sec / 2
end

m = Array.new
n = gets.chomp.to_i
t = gets.chomp.split(/ /)
t.each {|u| m << u.to_i}
m.sort!

s = 0
m.each {|t| s = s + t}

hash = Hash.new
if (s % n == 0)
	hash[s / n] = n
else
	hash[s/n] = n - s % n
	hash[s/n+1] = s % n
end

for i in 0..m.size do
	if hash.has_key?(m[i]) && hash[m[i]] != 0
		hash[m[i]] = hash[m[i]] - 1
		if hash[m[i]] == 0
			hash.delete m[i]
		end
		m[i] = 0
	end
end

sec = 0
if hash.size == 1
	hash.each_key {|key|
		if hash[key] != 0
			m.each {|m_i|
				if m_i != 0
					sec = sec + (m_i - key).abs
				end
			}
		end
	}
else
	m.each {|m_i|
		if m_i != 0
			if m_i < s/n
				sec = sec + (s/n - m_i).abs
				hash[s/n] = hash[s/n] - 1
			elsif hash[s/n + 1] > 0
				if m_i > s/n + 1
					sec = sec + (m_i - s/n - 1).abs
					hash[s/n + 1] = hash[s/n + 1] - 1
				end
			end
		end
	}
end

print sec / 2


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

cnt = Array.new(1e5+5,0)
a.map{|x| cnt[x]+=1}

dp = Array.new(1e5+5,0)

i = 1
hi2 = 0
dp[1] = cnt[1]
(dp.length-2).times do
    i += 1
    hi2 = [hi2,dp[i-2]].max
    dp[i] = i * cnt[i] + hi2
end

puts dp.max

def main
  gets
  seq = gets.split.map(&:to_i)
  puts max_score(seq)
end

def max_score(sequence)
  count = Hash.new(0)
  sequence.each { |x| count[x] += 1 }
  nums = count.keys.sort

  # best[k] is the best Alex can do with the first k nums
  best = []
  best[0] = 0

  nums.each_index do |i|
    next_best = best.last
    if i > 0 && nums[i - 1] + 1 == nums[i]
      base = best[-2]
    else
      base = best[-1]
    end
    with_cur = base + nums[i] * count[nums[i]]
    next_best = with_cur if with_cur > next_best
    best << next_best
  end
  best.last
end

main if __FILE__ == $0

n = gets.to_i
a = gets.split.map(&:to_i)
s = Array.new(100002, 0)
for i in 0...a.size
  s[a[i]] += a[i]
end
for i in 2..100001
	s[i] = [ s[i] + s[i - 2] , s[i - 1] ].max
end
puts s[100001]

# Author : Fuad Ashraful Mehmet
#Date: 18th April

n=gets.to_i
a=gets.split.map(&:to_i)
c=Array.new(a.max+1,0)

for i in 0...a.size
    c[a[i]]+=a[i]

end

dp=Array.new(c.size,0)
dp[1]=c[1]

for i in 2...c.size
    dp[i]=[dp[i-2]+c[i],dp[i-1]].max

end

puts dp[c.size-1]

n = gets.chomp.to_i
a = gets.chomp.split(" ").map(&:to_i)
h = Hash.new(0)
a.each do |v|
  h[v] += 1
end
keys = h.keys.push(0).sort

r  = keys.size.times.map{ 0 }
rw = keys.size.times.map{ 0 }

keys.each_with_index do |k, i|
  rw[i] = [r[i-1], rw[i-1]].max
  if keys[i-1] == k - 1
    r[i] = rw[i-1] + k*h[k]
  else
    r[i] = rw[i-1] > r[i-1] ? rw[i-1] : r[i-1]
    r[i] += k*h[k]
  end
end

puts [r.last, rw.last].max

# def bore(arr)
# 	poi = []

# 	return 0 if arr.empty?
# 	arr.uniq.each do |a|

# 		i = arr.index(a)
# 	 	@freq[a] = arr.count(a)	unless @freq[a]
# 		val = @freq[a]
# 		poi << (val * a) + bore( arr - [a,a+1,a-1])
# 	end
# 	return poi.max
# end

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

@freq = Hash.new(0)

arr.each{|i| @freq[i] += 1}

one_count = @freq[1]

f = []

f[0] = 0
f[1] = one_count

(2..1000001).each do |i|
	f[i] = [f[i-1],f[i-2] + (@freq[i] * i)].max
end
puts f[-1]

n = gets.strip.to_i
arr = gets.strip.split(" ").map(&:to_i)
arr.sort!
j = 1
uniq_array = []
uniq_array[0] = arr[0]
frequency = Hash.new(0)
frequency[arr[0]] = 1
while j < arr.length do
  if arr[j] != arr[j - 1] then
    uniq_array.push(arr[j])
    frequency[arr[j]] = 1
  else
    frequency[arr[j]] += 1
  end
  j += 1
end
# p uniq_array
# p frequency
results = []
results[0] = uniq_array[0] * frequency[uniq_array[0]]
if uniq_array.length > 1 then
  if uniq_array[1] - uniq_array[0] > 1 then
    results[1] = results[0] + uniq_array[1] * frequency[uniq_array[1]]
  else
    second_sum = uniq_array[1] * frequency[uniq_array[1]]
    results[1] = second_sum > results[0] ? second_sum : results[0]
  end
end
i = 2
while i < uniq_array.length do
  if uniq_array[i] - uniq_array[i - 1] > 1 then
    results[i] = results[i - 1] + uniq_array[i] * frequency[uniq_array[i]]
  elsif results[i - 1] > uniq_array[i] * frequency[uniq_array[i]] + results[i - 2] then
    results[i] = results[i - 1]
  else
    results[i] =  uniq_array[i] * frequency[uniq_array[i]] + results[i - 2]
  end
  i += 1
end
puts results.last

def solution(arr)
  dp = Array.new(arr.max+2, 0)
  arr.each { |a| dp[a] += 1 }

  for i in 2...dp.size do
    dp[i] = [dp[i-2] + i*dp[i], dp[i-1]].max
  end
  dp.last
end

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

puts solution(arr)

n = gets.strip.to_i
arr = gets.strip.split(" ").map(&:to_i)
arr.sort!
j = 1
uniq_array = []
uniq_array[0] = arr[0]
frequency = Hash.new(0)
frequency[arr[0]] = 1
while j < arr.length do
  if arr[j] != arr[j - 1] then
    uniq_array.push(arr[j])
    frequency[arr[j]] = 1
  else
    frequency[arr[j]] += 1
  end
  j += 1
end
# p uniq_array
# p frequency

prev = uniq_array[0] * frequency[uniq_array[0]]
if uniq_array.length > 1 then
  if uniq_array[1] - uniq_array[0] > 1 then
    current = prev + uniq_array[1] * frequency[uniq_array[1]]
  else
    second_sum = uniq_array[1] * frequency[uniq_array[1]]
    current = second_sum > prev ? second_sum : prev
  end
else
  current = prev
end
i = 2
while i < uniq_array.length do
  if uniq_array[i] - uniq_array[i - 1] > 1 then
    prev = current
    current += uniq_array[i] * frequency[uniq_array[i]]
  elsif current < uniq_array[i] * frequency[uniq_array[i]] + prev then
    temp = current
    current =  uniq_array[i] * frequency[uniq_array[i]] + prev
    prev = temp
  else
    prev = current
  end
  i += 1
end
puts current

n = gets.to_i
a = gets.split.map(&:to_i)
c = Array.new(a.max + 1, 0)
for i in 0...a.size
  c[a[i]] += a[i]
end

dp = Array.new(c.size, 0)
dp[1] = c[1]
for i in 2...c.size
  dp[i] = [dp[i - 2] + c[i], dp[i - 1]].max
end
puts dp[c.size - 1]

def calc (mas)
  pairs = {}
  mas.each_index { |i| mas[i]= mas[i].to_i}
  cur = mas[0]
  count = 0
  mas.each do |x|
    if x==cur
      count += 1
    else
      pairs[cur] = count
      cur = x
      count = 1
    end
  end
  pairs[cur] = count
  pairs
end
n = gets.to_i
a = gets.split(" ")
a.sort_by! {|x| x.to_i}
b = calc(a)
best1 = 0
prev1 = -1
best2 = 0
b.each_pair {|x,y|
  new_best2 = [best1,best2].max
  if x!=prev1+1
    new_best1 = [best2, best1].max + x*y
  else
    new_best1 = best2 + x*y
  end
  prev1 = x
  best1 = new_best1
  best2 = new_best2
}
puts [best1,best2].max

def parse(s)
  nn = s.split.map(&:to_i)
  nn.group_by { |n| n }.reduce(Hash.new(0)) { |acc, (k, v)| acc[k] = v.reduce(:+); acc }
end

def next_smallest_keys(ks, i)
  s1, s2 = 0
  j = i-1
  while j >= 0
    next_k = ks[j]
    if next_k < ks[i] - 1
      if s1.zero?
        s1 = next_k
      else
        s2 = next_k
        break
      end
    end
    j -= 1
  end
  [s1, s2]
end

def get_max_points(m)
  ks = m.keys.sort
  t = Hash.new(0)
  ans = 0

  ks.each_with_index do |k, i|

    k1, k2 = next_smallest_keys(ks, i)
    v = m[k]
    t[k] = v + [t[k1], t[k2]].max
    ans = [ans, t[k]].max
  end

  ans
end
#input = '6 6 8 9 7 9 6 9 5 7 7 4 5 3 9 1 10 3 4 5 8 9 6 5 6 4 10 9 1 4 1 7 1 4 9 10 8 2 9 9 10 5 8 9 5 6 8 7 2 8 7 6 2 6 10 8 6 2 5 5 3 2 8 8 5 3 6 2 1 4 7 2 7 3 7 4 10 10 7 5 4 7 5 10 7 1 1 10 7 7 7 2 3 4 2 8 4 7 4 4'

STDIN.gets
input = STDIN.gets.chomp

m = parse input
p get_max_points m

def f(x)
  return 0 if x < 0
  $c[x]
end

def n(x)
  return 0 unless $d.key?(x)
  $d[x]
end

gets

a = gets.split(' ').map(&:to_i).sort.reverse
$d = Hash[a.group_by{|n|n}.map{|k, v|[k, v.size]}]
m = a.first
$c = [ 0 ]

1.upto(m) do |i|
  $c.push([f(i-2) + i*n(i), f(i-1)].max)
end

puts $c.last

gets
a=gets.split.map(&:to_i).sort
d=[0]*a.size
d[0]=a[0]
a.each_with_index{|e,i|
	next if i==0
	d[i]=d[i-1]
	s=(i>1) ? d[i-2] : 0
	d[i]=[d[i], s+e].max
}
p d[-1]

# def bore(arr)
# 	poi = []

# 	return 0 if arr.empty?
# 	arr.uniq.each do |a|

# 		i = arr.index(a)
# 	 	@freq[a] = arr.count(a)	unless @freq[a]
# 		val = @freq[a]
# 		poi << (val * a) + bore( arr - [a,a+1,a-1])
# 	end
# 	return poi.max
# end

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

# @freq = {}

# puts bore(arr)
@freq = Hash.new(0)

arr.uniq.each{|i| @freq[i] += 1}

one_count = @freq[1]

f = []

f[0] = 0
f[1] = one_count
max = one_count

(2..10000).each do |i|
	f[i] = [f[i-1],f[i-2] + (@freq[i] * i)].max

end
puts f[-1]

# def bore(arr)
# 	poi = []

# 	return 0 if arr.empty?
# 	arr.uniq.each do |a|

# 		i = arr.index(a)
# 	 	@freq[a] = arr.count(a)	unless @freq[a]
# 		val = @freq[a]
# 		poi << (val * a) + bore( arr - [a,a+1,a-1])
# 	end
# 	return poi.max
# end

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

@freq = Hash.new(0)

arr.each{|i| @freq[i] += 1}

one_count = @freq[1]

f = []

f[0] = 0
f[1] = one_count

(2..10000).each do |i|
	f[i] = [f[i-1],f[i-2] + (@freq[i] * i)].max
end
puts f[-1]

# def bore(arr)
# 	poi = []

# 	return 0 if arr.empty?
# 	arr.uniq.each do |a|

# 		i = arr.index(a)
# 	 	@freq[a] = arr.count(a)	unless @freq[a]
# 		val = @freq[a]
# 		poi << (val * a) + bore( arr - [a,a+1,a-1])
# 	end
# 	return poi.max
# end

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

# @freq = {}

# puts bore(arr)
@freq = Hash.new(0)

arr.uniq.each{|i| @freq[i] += i}

one_count = @freq[1]

f = []

f[0] = 0
f[1] = one_count
max = one_count

(2..10000).each do |i|
	f[i] = [f[i-1],f[i-2] + (@freq[i] * i)].max

end
puts f[-1]

# def bore(arr)
# 	poi = []

# 	return 0 if arr.empty?
# 	arr.uniq.each do |a|

# 		i = arr.index(a)
# 	 	@freq[a] = arr.count(a)	unless @freq[a]
# 		val = @freq[a]
# 		poi << (val * a) + bore( arr - [a,a+1,a-1])
# 	end
# 	return poi.max
# end

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

# @freq = {}

# puts bore(arr)
@freq = Hash.new(0)

arr.each{|i| @freq[i] += 1}

one_count = @freq[1]

f = []

f[0] = 0
f[1] = one_count
max = one_count

(2..10000).each do |i|
	f[i] = [f[i-1],f[i-2] + (@freq[i] * i)].max

end
puts f[-1]

# def bore(arr)
# 	poi = []

# 	return 0 if arr.empty?
# 	arr.uniq.each do |a|

# 		i = arr.index(a)
# 	 	@freq[a] = arr.count(a)	unless @freq[a]
# 		val = @freq[a]
# 		poi << (val * a) + bore( arr - [a,a+1,a-1])
# 	end
# 	return poi.max
# end

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

# @freq = {}
# puts bore(arr)
@freq = {}

# (1..10000).each{|i| @freq[i] = arr.count(i)}
one_count = arr.count(1)
f = Hash.new(0)

f[0] = 0
f[1] = one_count
max = one_count

arr.sort.uniq.each do |i|
	f[i] = [f[i-1],f[i-2] + (arr.count(i) * i)].max
	max = f[i] if f[i] > max
end
# puts f.max
puts max

n = gets.to_i
a = gets.split.map(&:to_i)
b = a.sort.uniq
c = Array.new(b.max + 1, 0)
for i in 0...b.size
  c[b[i]] = b[i] * a.count(b[i])
end

s = Array.new(b.max + 1, 0)
s[0] = 1
for i in 1...c.size - 1
  if c[i] >= c[i - 1] + c[i + 1]
    s[i - 1] = 0
    s[i + 1] = 0
    s[i] = 1
  else
    s[i - 1] = 1 if s[i - 2] == 0
    s[i + 1] = 1
    s[i] = 0
  end
end
ans = 0
for i in 0...c.size
  ans += c[i] * s[i]
end
p ans

n = gets.to_i
a = gets.split.map(&:to_i)
b = a.sort.uniq
c = Array.new(b.max + 1, 0)
for i in 0...b.size
  c[b[i]] = b[i] * a.count(b[i])
end

s = Array.new(b.max + 1, 0)
ans = c[1]
for i in 2...c.size
  if ans + c[i] - c[i - 1] > ans
    s[i] = 1
    ans += c[i] - c[i - 1]
    if s[i - 2] == 0 && s[i - 3] == 0 && c[i - 2] > 0
      ans += c[i - 2]
      s[i - 2] = 1
    end
  end
end
p ans

n = gets.to_i
a = gets.split.map(&:to_i)
b = a.sort.uniq
c = Array.new(b.max + 1, 0)
for i in 0...b.size
  c[b[i]] = b[i] * a.count(b[i])
end

s = Array.new(b.max + 1, 0)
s[0] = 0
for i in 1...c.size - 1
  if c[i] >= c[i - 1] + c[i + 1]
    s[i - 1] = 0
    s[i + 1] = 0
    s[i] = 1
  else
    s[i - 1] = 1 if s[i - 2] == 0
    s[i + 1] = 1
    s[i] = 0
  end
end
s[0] = 0
for i in 1...c.size
  s[i] = 1 if s[i - 1] == 0 && s[i + 1] == 0
end
ans = 0
for i in 0...c.size
  ans += c[i] * s[i]
end
p ans

n = gets.to_i
a = gets.split.map(&:to_i)
b = a.sort.uniq
c = Array.new(b.max + 1, 0)
for i in 0...b.size
  c[b[i]] = b[i] * a.count(b[i])
end

s = Array.new(b.max + 1, 0)
s[0] = 1
for i in 1...c.size - 1
  if c[i] >= c[i - 1] + c[i + 1]
    s[i - 1] = 0
    s[i + 1] = 0
    s[i] = 1
  else
    s[i - 1] = 1
    s[i + 1] = 1
    s[i] = 0
  end
end
ans = 0
for i in 0...c.size
  ans += c[i] * s[i]
end
p ans

n = gets.to_i
a = gets.split.map(&:to_i)
b = a.sort.uniq
c = Array.new(b.max + 1, 0)
for i in 0...b.size
  c[b[i]] = b[i] * a.count(b[i])
end

s = Array.new(b.max + 1, 0)
s[0] = 1
for i in 1...c.size - 1
  if c[i] >= c[i - 1] + c[i + 1]
    s[i - 1] = 0
    s[i + 1] = 0
    s[i] = 1
  else
    s[i - 1] = 1 if s[i - 2] == 0
    s[i + 1] = 1
    s[i] = 0
  end
  p s
end
p s
p c
ans = 0
for i in 0...c.size
  ans += c[i] * s[i]
end
p ans

# Hypothesis
# choose the most popular number
# if popularity is equal,
# take the largest number
def step(ak, sequence)
  sequence.delete_at(sequence.index(ak))
  sequence.select do |a|
    a != ak + 1 && a != ak - 1
  end
end

def get_next_pick(sequence)
  freq = sequence.inject(Hash.new(0)) { |h, v| h[v] += 1; h }
  if freq.values.uniq.length == 1
    sequence.max
  else
    sequence.max_by { |v| freq[v] }
  end
end

def simulate(input)
  sequence = input.split.map(&:to_i)
  score = 0
  until sequence.length == 0
    next_pick = get_next_pick(sequence)
    score += next_pick
    sequence = step(next_pick, sequence)
  end
  score
end

#input = '1 2'
#score = simulate(input)
#p "#{input}: #{score}"
#
#input = '1 2 3'
#score = simulate(input)
#p "#{input}: #{score}"
#
#input = '1 2 1 3 2 2 2 2 3'
#score = simulate(input)
#p "#{input}: #{score}"

input = gets.chomp
p input
input = gets.chomp
simulate(input)

# Hypothesis
# choose the most popular number
# if popularity is equal,
# take the largest number
def step(ak, sequence)
  sequence.delete_at(sequence.index(ak))
  sequence.select do |a|
    a != ak + 1 && a != ak - 1
  end
end

def get_next_pick(sequence)
  freq = sequence.inject(Hash.new(0)) { |h, v| h[v] += 1; h }
  if freq.values.uniq.length == 1
    sequence.max
  else
    sequence.max_by { |v| freq[v] }
  end
end

def simulate(input)
  sequence = input.split.map(&:to_i)
  score = 0
  until sequence.length == 0
    next_pick = get_next_pick(sequence)
    score += next_pick
    sequence = step(next_pick, sequence)
  end
  score
end

input = gets.chomp
simulate(input)

# Hypothesis
# choose the most popular number
# if popularity is equal,
# take the largest number
def step(ak, sequence)
  sequence.delete_at(sequence.index(ak))
  sequence.select do |a|
    a != ak + 1 && a != ak - 1
  end
end

def get_next_pick(sequence)
  freq = sequence.inject(Hash.new(0)) { |h, v| h[v] += 1; h }
  if freq.values.uniq.length == 1
    sequence.max
  else
    sequence.max_by { |v| freq[v] }
  end
end

def simulate(a)
  sequence = a
  score = 0
  until sequence.length == 0
    next_pick = get_next_pick(sequence)
    score += next_pick
    sequence = step(next_pick, sequence)
  end
  score
end

while STDIN.gets
  a = $_.split.map(&:to_i)
  if a.length > 1
    puts simulate(a)
  end
end

gets
a = gets.split(' ').map(&:to_i).sort.reverse
b = a.uniq.map { |x| [x, a.count{ |y| y == x } ] }.sort do |x, y|
  y.last != x.last ? y.last <=> x.last : y.first <=> x.first
end
c = Hash[ b ]

puts(
  b.map do |elm|
    res = [elm.last, c[elm.first]].min
    c[elm.first-1] = [0, c[elm.first-1]-res].max if c.key?(elm.first-1)
    c[elm.first+1] = [0, c[elm.first+1]-res].max if c.key?(elm.first+1)
    c[elm.first]   = 0

    res*elm.first
  end.inject(&:+)
)

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

#p a

prv = 1e6
score = 0
n.times do
    x = a.shift
    if x < prv - 1
        score += x
        prv = x
    end
end

puts score

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

cnt = Array.new(1e5+5,0)
a.map{|x| cnt[x]+=1}

dp = Array.new(1e5+5,0)

i = 1
hi2 = 0
(dp.length-2).times do
    i += 1
    hi2 = [hi2,dp[i-2]].max
    dp[i] = i * cnt[i] + hi2
end

puts dp.max

n = gets.chomp.to_i
a = gets.chomp.split(" ").map(&:to_i)
h = Hash.new(0)
a.each do |v|
  h[v] += 1
end
keys = h.keys.push(0).sort

r  = keys.size.times.map{ 0 }
rw = keys.size.times.map{ 0 }

keys.each_with_index do |k, i|
  rw[i] = r[i-1]
  if keys[i-1] == k - 1
    r[i] = rw[i-1] + k*h[k]
  else
    r[i] = rw[i-1] > r[i-1] ? rw[i-1] : r[i-1]
    r[i] += + k*h[k]
  end
end

puts [r[keys.last], rw[keys.last]].max

n = gets.chomp.to_i
a = gets.chomp.split(" ").map(&:to_i)
h = Hash.new(0)
a.each do |v|
  h[v] += 1
end
r = n.times.map{ 0 }

keys = h.keys.push(0).sort
keys.each_with_index do |k, i|
  r[i] = r[i-1]
  prev_key = keys[i-1]
  prev_score = h[prev_key]*prev_key
  score = h[k]*k
  if prev_key == k-1
    if score > prev_score
      r[i] += score - prev_score
      if keys[i-1] == keys[i-2] + 1
        r[i]+= r[i-2]
      end
    end
  else
    r[i] += h[k]*k
  end
end

puts r[keys.last]

n = gets.chomp.to_i
a = gets.chomp.split(" ").map(&:to_i)
h = Hash.new(0)
a.each do |v|
  h[v] += 1
end
keys = h.keys.push(0).sort

r  = keys.size.times.map{ 0 }
rw = keys.size.times.map{ 0 }

keys.each_with_index do |k, i|
  rw[i] = r[i-1]
  if keys[i-1] == k - 1
    r[i] = rw[i-1] + k*h[k]
  else
    r[i] = rw[i-1] > r[i-1] ? rw[i-1] : r[i-1]
    r[i] += + k*h[k]
  end
end

puts [r.last, rw.last].max

n = gets.strip.to_i
arr = gets.strip.split(" ").map(&:to_i)
arr.sort!
frequency = arr.inject({}){|freq, elem| freq.merge(elem => arr.count(elem))}
arr.uniq!
# p arr
# p frequency
results = []
results[0] = arr[0] * frequency[arr[0]]
if arr.length > 1 then
  second_sum = arr[1] * frequency[arr[1]]
  results[1] = second_sum > results[0] ? second_sum : results[0]
end
i = 2
while i < arr.length do
  if arr[i] - arr[i - 1] > 1 then
    results[i] = results[i - 1] + arr[i] * frequency[arr[i]]
  elsif results[i - 1] > arr[i] * frequency[arr[i]] + results[i - 2] then
    results[i] = results[i - 1]
  else
    results[i] =  arr[i] * frequency[arr[i]] + results[i - 2]
  end
  i += 1
end
puts results.last

n = gets.strip.to_i
arr = gets.strip.split(" ").map(&:to_i)
arr.sort!
frequency = arr.inject({}){|freq, elem| freq.merge(elem => arr.count(elem))}
arr.uniq!
# p arr
# p frequency
results = []
results[0] = arr[0] * frequency[arr[0]]
results[1] = arr[1] * frequency[arr[1]] if arr.length > 1
i = 2
while i < arr.length do
  if arr[i] - arr[i - 1] > 1 then
    results[i] = results[i - 1] + arr[i] * frequency[arr[i]]
  elsif results[i - 1] > arr[i] * frequency[arr[i]] + results[i - 2] then
    results[i] = results[i - 1]
  else
    results[i] =  arr[i] * frequency[arr[i]] + results[i - 2]
  end
  i += 1
end
puts results.last

n = gets.strip.to_i
arr = gets.strip.split(" ").map(&:to_i)
arr.sort!
j = 1
uniq_array = []
uniq_array[0] = arr[0]
frequency = Hash.new(0)
frequency[arr[0]] = 1
while j < arr.length do
  if arr[j] != arr[j - 1] then
    uniq_array.push(arr[j])
    frequency[arr[j]] = 1
  else
    frequency[arr[j]] += 1
  end
  j += 1
end
# p uniq_array
# p frequency

prev = uniq_array[0] * frequency[uniq_array[0]]
if uniq_array.length > 1 then
  if uniq_array[1] - uniq_array[0] > 1 then
    current = prev + uniq_array[1] * frequency[uniq_array[1]]
  else
    second_sum = uniq_array[1] * frequency[uniq_array[1]]
    current = second_sum > prev ? second_sum : prev
  end
end
i = 2
while i < uniq_array.length do
  if uniq_array[i] - uniq_array[i - 1] > 1 then
    current = current + uniq_array[i] * frequency[uniq_array[i]]
  elsif current <= uniq_array[i] * frequency[uniq_array[i]] + prev then
    current =  uniq_array[i] * frequency[uniq_array[i]] + prev
  end
  i += 1
end
puts current

n = gets.strip.to_i
arr = gets.strip.split(" ").map(&:to_i)
arr.sort!
j = 1
uniq_array = []
uniq_array[0] = arr[0]
frequency = Hash.new(0)
frequency[arr[0]] = 1
while j < arr.length do
  if arr[j] != arr[j - 1] then
    uniq_array.push(arr[j])
    frequency[arr[j]] = 1
  else
    frequency[arr[j]] += 1
  end
  j += 1
end
# p uniq_array
# p frequency

prev = uniq_array[0] * frequency[uniq_array[0]]
if uniq_array.length > 1 then
  if uniq_array[1] - uniq_array[0] > 1 then
    current = prev + uniq_array[1] * frequency[uniq_array[1]]
  else
    second_sum = uniq_array[1] * frequency[uniq_array[1]]
    current = second_sum > prev ? second_sum : prev
  end
end
i = 2
while i < uniq_array.length do
  if uniq_array[i] - uniq_array[i - 1] > 1 then
    prev = current
    current += uniq_array[i] * frequency[uniq_array[i]]
  elsif current < uniq_array[i] * frequency[uniq_array[i]] + prev then
    temp = current
    current =  uniq_array[i] * frequency[uniq_array[i]] + prev
    prev = temp
  else
    prev = current
  end
  i += 1
end
puts current

n = gets.to_i
arr = gets.split.map { |x| x.to_i }.sort
hash = Hash.new(0)
arr.each { |x| hash[x] += 1 }
hash = Hash[hash.sort_by { |key,value| value }]
sum = 0
while hash.size != 0
  temp = hash[hash.keys.last]
  sum += temp * hash.keys.last
  temp_key = hash.keys.last
  hash.delete(temp_key)
  hash.delete(temp_key - 1)
  hash.delete(temp_key + 1)
end
puts sum


gets.to_i.times do
  n=gets.to_i
  a=gets.split.map(&:to_i)
  s,t=[a.index(a.min),a.index(a.max)].sort
  puts [t+1,n-s,s+1+n-t].min
end

gets.to_i.times do
    n = gets.to_i
    a = gets.split.map &:to_i
    mn, mx = a.index(a.min), a.index(a.max)
    puts [[mn, mx].max + 1, n - [mn, mx].min, n - mn + mx + 1, n - mx + mn + 1].min
end

t = gets.to_i
for i in 0...t
	n = gets.to_i
	a = gets.split.map(&:to_i)
	x, y = [a.index(a.min), a.index(a.max)].sort
	puts [y + 1, n - x, x + 1 + n - y].min
end

gets.to_i.times do
    n = gets.to_i
    a = gets.split.map &:to_i
    mn, mx = a.index(a.min), a.index(a.max)
    puts [[mn, mx].max + 1, n - [mn, mx].min, n - mn + mx + 1, n - mx + mn - 1].min
end


n = gets.chomp.to_i

ratings = []

n.times do
  a, b = gets.chomp.split(" ").map(&:to_i)
  if a != b then
    puts "rated"
    exit
  else
    ratings << b
  end
end

(n-1).times do |i|
  if ratings[i] < ratings[i+1] then
    puts "unrated"
    exit
  end
end
puts "maybe"

n=gets.to_i
a=[]
b=[]
rated=false
n.times do
    x, y=gets.split.map &:to_i
    a<<x
    b<<y
    rated=true if x!=y
end
c=b.sort.reverse
if rated
    puts "rated"
else
    puts (c==b ? "maybe" : "unrated")
end


n = gets.to_i
a = gets.split(" ").map(&:to_i)
ans = 0
a.each do |e|
  break if (e <ans)
  ans = (e - ans)%2
end
print ans>0? "NO" : "YES"

p = 0
n = gets.to_i

a = gets.split.map &:to_i
s = false
a.each do |i|
    if i < p
        s = true
        break
    elsif i%2 !=  p
        p = 1
    elsif i%2 == p
        p = 0
    end
end

if s or p > 0
    puts "NO"
else
    puts "YES"
end

r=0
gets;gets.split.map(&:to_i).each{|e|
break if r>e
r^=e%2
}
puts [:YES,:NO][r]

n = gets.to_i
as = gets.split.map(&:to_i)
as.each_with_index do |a, i|
  if a < 0 || a.odd? && i == n - 1
    puts 'NO'
    exit
  end
  if a.odd?
    as[i + 1] -= 1
  end
end
puts 'YES'

n=gets.to_i-2
a=gets.split.map &:to_i

res="YES"
for i in 0..n
    if a[i]<0
        res="NO"
        break
    end

    if a[i].odd?
        a[i]-=1
        a[i+1]-=1
    end
end
res="NO" if a[-1].odd?

puts res

n=gets.to_i-1
a=gets.split.map &:to_i

n.times { |i|
  if a[i].odd?
    a[i] -= 1
    a[i+1] -= 1
  end
  (puts 'NO'; exit) if a[i] < 0
}
puts (a[n].even?)?'YES':'NO'

n = gets.to_i
a = gets.split.map(&:to_i)
k = 0
a.each do |x|
   break if x-k<0
   k = (x-k)%2
end
puts k>0 ? "NO" : "YES"

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

def meow(n, a)
  n.times do |i|
    return false if a[i] < 0
    if a[i].odd?
      return false if i == n - 1
      a[i + 1] -= 1
    end
  end
end

puts meow(n, a) ? 'YES' : 'NO'

n = gets.to_i
a = gets.strip.split(' ').map(&:to_i)
flag = false
0.upto(n - 1) do |i|
  if flag
    if a[i] == 0
      break
    else
      a[i] -= 1
      flag = !flag
    end
  end

  if a[i] % 2 == 1
    flag = !flag
  else
    if flag == true
      break
    end
  end
end

printf("%s\n", flag ? "NO" : "YES")

require 'set'

parse_int = lambda {gets.split.map{|x| x.to_i}}

gets
pz = parse_int.call

(pz.length-1).times do |i|
  if pz[i].odd?
    if pz[i+1]>0
      pz[i] -= 1
      pz[i+1] -= 1
    else
      puts 'NO'
      exit
    end
  end
end

puts pz[-1].even? ? 'YES' : "NO"

p = 0
n = gets.to_i

a = gets.split.map &:to_i
s = false
a.each do |i|
    if i == p + 1
        p = 1
    elsif i == p+2 or i == p
        p = 0
    else
        s = true
        break
    end
end

if s or p > 0
    puts "NO"
else
    puts "YES"
end

p = 0
n = gets.to_i

a = gets.split.map &:to_i
s = false
a.each do |i|
    if i == p + 1
        p = 1
    elsif i == p+2
        p = 0
    elsif i != p
        puts "NO"
        s = true
        break
    end
end

puts "YES" unless s

r=0
gets.split.map(&:to_i).each{|e|
break if r>e
r^=e%2
}
puts [:YES,:NO][r]

r=0
gets.split.map(&:to_i).each{|e|
break if e>r
r^=e%2
}
puts [:YES,:NO][r]

n = gets.to_i
a = gets.split(" ").map(&:to_i)
ans = 0
a.each do |e|
  break if (ans < e)
  ans = (e - ans)%2
end
print ans>0? "NO" : "YES"

n = gets.to_i
a = gets.split.map(&:to_i)
k = 0
a.each { |x| k = (x-k)%2 }
puts k>0 ? "NO" : "YES"


n=gets.to_i
a=gets.split.map &:to_i
m=gets.to_i
b=gets.split.map &:to_i
d=[0]*m
q=[0]*n
p=[-1]*n
n.times{|i|
	mx=0
	m.times{|j|
		mx=[mx,d[j]].max if a[i]>b[j]
		q[i]=d[j]=mx+1 if a[i]==b[j]
	}
	i.times{|j|p[i]=j if a[j]<a[i] and q[j]==q[i]-1}
}
i=q.index(q.max)
l=[]
"#{l<<a[i];i=p[i]}"while i>=0 and q[i]>0
p q.max
puts l.reverse.join " "

n=gets.to_i
a=gets.split.map &:to_i
m=gets.to_i
b=gets.split.map &:to_i
d=[0]*m
q=[0]*n
p=[-1]*m
n.times{|i|
	mx=0
	m.times{|j|
		mx=[mx,d[j]].max if a[i]>b[j]
		q[i]=d[j]=mx+1 if a[i]==b[j]
	}
	i.times{|j|p[i]=j if a[j]<a[i] and q[j]==q[i]-1}
}
i=q.index(q.max)
l=[]
"#{l<<a[i];i=p[i]}"while i>=0
p q.max
puts l.reverse.join " "


def solve
  str = gets.strip
  cur = 0
  h = Hash.new()
  str.each_char.with_index { |e, i|
    if e == '+'
      cur += 1
    else
      cur -= 1
      if cur < 0 && !h.has_key?(-cur)
        h[-cur] = i + 1
      end
    end
  }
  res = 0
  (0..str.size).each { |i|
    ck = h[i + 1] if h.has_key?(i + 1)
    if ck.nil?
     res += str.size
     break
    else
     res += ck
    end
  }
  puts res
end

gets.strip.to_i.times { solve }

def solve
  str = gets.strip
  cur = 0
  h = Hash.new(0)
  str.each_char.with_index { |e, i|
    if e == '+'
      cur += 1
    else
      cur -= 1
      if cur < 0 && h[-cur].zero?
        h[-cur] = i + 1
      end
    end
  }
  res = 0
  (0..str.size).each { |i|
     ck = h[i + 1]
     if ck.zero?
      res += str.size
      break
     else
      res += ck
     end
  }
  puts res
end

gets.strip.to_i.times { solve }

t = gets.to_i
t.times do
    a = []
    b = {}
    cur = 0
    gets.each_char.with_index do |c, i|

        case c
        when '+'
            a << (cur += 1)
        when '-'
            a << (cur -= 1)
        else
            a << cur
        end

        b[cur] = i + 1 unless b.include? cur
    end

    res = 0
    init = 0
    loop do

        if b.include?(-init-1)
            res += b[-init-1]
        else
            res += a.size - 1
            break
        end

        init += 1
    end

    p res
end

def solve
  str = gets.strip
  cur = 0
  h = Hash.new(0)
  str.each_char.with_index { |e, i|
    if e == '+'
      cur += 1
    else
      cur -= 1
      if cur < 0 && h[-cur].zero?
        h[-cur] = i + 1
      end
    end
  }
  res = 0
  (0..str.size).each { |i|
     ck = h[i + 1]
     if ck.zero?
      res += str.size
      break
     else
      res += h[ck]
     end
  }
  puts res
end

gets.strip.to_i.times { solve }

def solve
  str = gets.strip
  cur = 0
  h = Hash.new(1e7 + 1)
  a = []
  str.each_char.with_index { |e, i|
    if e == '+'
      cur += 1
    else
      cur -= 1
      if cur < 0
        h[-cur] = [h[-cur], i + 1].min
        a << -cur
      end
    end
  }
  res = 0
  (0..str.size).each { |i|
     ck = a.bsearch { |x| x > i}
     if ck.nil?
      res += str.size
      break
     else
      res += h[ck]
     end
  }
  puts res
end

gets.strip.to_i.times { solve }

def solve
  str = gets.strip
  cur = 0
  h = Hash.new(0)
  str.each_char.with_index { |e, i|
    if e == '+'
      cur += 1
    else
      cur -= 1
      if cur < 0 && h[-cur].nil?
        h[-cur] = i + 1
      end
    end
  }
  res = 0
  (0..str.size).each { |i|
     ck = h[i + 1]
     if ck.zero?
      res += str.size
      break
     else
      res += h[ck]
     end
  }
  puts res
end

gets.strip.to_i.times { solve }


gets.to_i.times {
  a, b = gets.split.map { |v| v.to_i }

  if a == b
    puts 0
  elsif a < b
    puts 2 - (b - a) % 2
  else
    puts 1 + (b - a) % 2
  end
}

# cook your code here
t = gets.to_i
until t == 0
    t-=1
    x,y = gets.chomp.split().map { |e| e.to_i }
    if(x == y)
        puts "0"
    elsif(x > y)
        if((x-y)%2 == 0)
            puts "1"
        else
            puts "2"
        end
    else
        if((y-x)%2 == 1)
            puts "1"
        else
            puts "2"
        end
    end
end

t = gets.chomp.to_i

until t==0
    t-=1
    x, y = gets.split.map(&:to_i)
    if(x>y && (x-y)%2!=0)
        puts 2
    elsif(x>y && (x-y)%2==0)
        puts 1
    elsif(x<y && (y-x)%2==0)
        puts 2
    elsif(x<y && (y-x)%2!=0)
        puts 1
    end
    if(x==y)
        puts 0
    end

end

t = gets.to_i

while t > 0
  k = gets.split(" ")
  a = k[0].to_i
  b = k[1].to_i

  if a == b
    puts 0
  elsif a > b
    if a % 2 != b % 2
      puts 2
    else
      puts 1
    end
  else
    if a % 2 != b % 2
      puts 1
    else
      puts 2
    end
  end
  t -= 1
end

require 'set'; require 'prime'
INF=Float::INFINITY; MOD=10**9+7
t = gets.chomp.to_i
t.times do
  a,b = gets.chomp.split.map(&:to_i)
  if a < b
    if (b-a).odd?
      puts 1
    else
      puts 2
    end
  elsif a > b
    if (a-b).even?
      puts 1
    else
      puts 2
    end
  else
    puts 0
  end
end

N = gets.to_i
N.times do
    a,b = gets.split.map(&:to_i)
    f = b - a;
    ans = 0
    if f == 0
        ans = 0
    elsif f<0 && f.odd?
        ans = 2
    elsif f<0 && f.even?
        ans = 1
    elsif f>0 && f.odd?
        ans = 1
    elsif f>0 && f.even?
        ans = 2
    end
    puts ans
end

n=gets.to_i
(1..n).each do |x|
  a,b=gets.chomp.split(" ").map(&:to_i)
  if a == b
    print("0\n")
  elsif a > b && (a - b) % 2 == 0
  	print("1\n")
  elsif a > b && (a - b) % 2 == 1
  	print("2\n")
  elsif a < b && (b - a) % 2 == 0
  	print("2\n")
  elsif a < b && (b - a) % 2 == 1
  	print("1\n")
  end
end

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

n = gets.to_i
n.times {
  a, b = gli

  if a < b
    if (a - b) % 2 == 1
      puts 1
    else
      puts 2
    end
  elsif a == b
    puts 0
  else
    if (b - a) % 2 == 1
      puts 2
    else
      puts 1
    end
  end
}

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

n = gets.to_i

n.times do |l|
    a, b = input_line
    if a == b then
        puts 0
    elsif a > b
        puts (b - a).even? ? 1 : 2
    else
        puts (a - b).even? ? 2 : 1
    end
end

gets.to_i.times do
  a, b = gets.split.map &:to_i
  d = a - b
  if d == 0
    puts 0
  elsif d%2 == 0
    puts d > 0 ? 1 : 2
  else
    puts d < 0 ? 1 : 2
  end
end

1.upto(gets.to_i) do
  a, b = gets.split(' ').map(&:to_i)
  result = (b-a).zero? ? 0 : 2 - (b-a).modulo(2)

  result = 3 - result if (b - a) < 0

  puts result
end

t = gets.to_i
(1).upto(t) do
  a = gets.split(' ').map(&:to_i)
  answer = 2
  if(a[0]<a[1] and (a[0]-a[1]).modulo(2) == 1) or (a[0]>a[1] and (a[0]-a[1]).modulo(2) == 0)
    answer=1
  elsif a[0]==a[1]
    answer=0
  end
  puts answer
end


def run
  n, m = $stdin.gets.split.map(&:to_i)
  a = []
  n.times do |i|
    a << $stdin.gets.chomp
  end

  ans = n
  flag = true
  cur = n
  while flag
    if cur % 2 == 0
      cur = cur / 2
    else
      ans = cur
      break
    end

    cur.times do |i|
      if a[i] != a[cur * 2 - 1 - i]
        flag = false
        ans = cur * 2
        break
      end
    end
  end

  puts ans
end

run if $0 == __FILE__

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

def min(a)
	f = 1
	s = 0
	a.each do |e|
		if f
			s=e
			f=0
		else
			s=e if e < s
		end
	end
	s
end

def solve(ib,ie,j,a)
	if (ie-ib+1)%2 !=0
		return 0
	end
	b ,e = ib, ie
	while e>b
		if a[e][j] != a[b][j]
			return 0
		end
		b+=1
		e-=1
	end

	return solve(ib, (ib+ie)/2,j,a)+1
end

n,m = gets.split.map(&:to_i)
a = [0]*n
ans = [0]*m

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

(0..ans.size-1).each do |i|
	ans[i] = solve(0,n-1,i,a)
end

puts n/(2**min(ans))


$stdin=File.open('input.txt')
$stdout=File.open('output.txt','w')
a = gets.split.map(&:to_i)

if a[0] == a[1]
    a[0].times do
        print "BG"
    end
    exit
end
p=a[0]-a[1]
r=a[1]-a[0]
if a[0]>a[1]
    a[1].times do
        print "BG"
    end
    p.times do
         print "B"
    end
    exit
end

if a[1]>a[0]
    a[0].times do
        print "GB"
    end
    r.times do
         print "G"
    end
    exit
end

$stdin=File.open('input.txt')
$stdout=File.open('output.txt','w')
a=gets.chomp.split(" ")
b=a[0].to_i
g=a[1].to_i
ans=""
if b>=g
loop do
break if b==0 && g==0
if b!=0
ans+="B"
b-=1
end
if g!=0
ans+="G"
g-=1
end
end
else
loop do
break if b==0 && g==0
if g!=0
ans+="G"
g-=1
end
if b!=0
ans+="B"
b-=1
end
end
end

puts "#{ans}"

n,m=File.open("input.txt").gets.split.map(&:to_i)
File.open("output.txt", "w") { |f|
	if n < m then
		f.puts "GB"*n + "G"*(m-n)
	else
		f.puts "BG"*m + "B"*(n-m)
	end
}

m, n= File.open("input.txt").gets.split.map(&:to_i)
File.open("output.txt", "w") { |f|
  if m > n
    f.puts 'BG'*n + 'B'*(m-n)
  else
    f.puts 'GB'*m + 'G'*(n-m)
  end
}

a=[]
File.open("input.txt","r") do |file|
  a=file.gets.chomp.split
end

n=a[0].to_i
m=a[1].to_i

if(n>m)
 maxc='B'
 minc='G'
 mini=m
else
 maxc='G'
 minc='B'
 mini=n
end

a=""

(n+m).times do
 a+=maxc
end

ind=1
mini.times do
 a[ind]=minc
 ind+=2
end
File.open("output.txt","w") do |file|
  file.puts a
end

n, m = 0, 0
File.open("input.txt", "r") do |file|
    n, m = file.gets.split.collect{|i| i.to_i}
end
File.open("output.txt", "w") do |file|
    if n >= m
        file.puts (['B','G']*m+['B']*(n-m)).join
    else
        file.puts (['G','B']*n+['G']*(m-n)).join
    end
end

#n, m = IO.readlines("input.txt")[0].split.collect{|i| i.to_i}
n, m = File.new("input.txt", "r").gets.split.collect{|i| i.to_i}
if n >= m
    File.new("output.txt", "w").puts (['B','G']*m+['B']*(n-m)).join
else
    File.new("output.txt", "w").puts (['G','B']*n+['G']*(m-n)).join
end

file = File.new("input.txt", "r")
n, m = file.gets.split.collect{|i| i.to_i}
file.close
file = File.new("output.txt", "w")
if n >= m
    file.puts (['B','G']*m+['B']*(n-m)).join
else
    file.puts (['G','B']*n+['G']*(m-n)).join
end
file.close

file = File.open('input.txt', "r")
n, m = file.gets.split.map { |x| x.to_i }
file.close

File.open('output.txt', 'w') { |f| f.write(n == m ? 'GB' * n : n > m ? 'BG' * m + 'B' * (n - m) : 'GB' * n + 'G' * (m - n)) }

n, m = File.open("input.txt").gets.split.map(&:to_i)
output = File.open("output.txt", "w")
while n > 0 || m > 0
	if (n >= m)
		output.print 'B'
		output.print 'G' if m > 0
	else
		output.print 'G'
		output.print 'B' if n > 0
	end
	n, m = n - 1, m - 1
end

input = File.new('input.txt', 'r')
output = File.new('output.txt', 'w')

t = input.gets.split.collect { |e| e.to_i  }

n,m = t[0], t[1]

f,l = 'B','G'
if n < m
	f,l = 'G','B'
end
min = t.min
others = t.max - min

for i in 1..min do
	output.print f,l
end
for i in 1..others do
	output.print f
end

input.close
output.close

f = File.open("input.txt", "r")
s = f.read.split
n = s[0].to_i
m = s[1].to_i
f.close

if n > m then
    s = "BG"*m + "B"*(n-m)
else
    s = "GB"*n + "G"*(m-n)
end

f = File.open("output.txt", "w")
f.write(s)
f.close
puts s

#!/usr/bin/ruby
f=nil
if true
	f=File.open('input.txt','rb')
else
	f=$<
end
b,g=f.gets.split.map(&:to_i)
m=[b,g].min
b-=m;g-=m
p [g,m,b]
File.write('output.txt','G'*g+'BG'*m+'B'*b)

$stdin=File.open('input.txt')
$stdout=File.open('output.txt','w')
input=gets.to_s.split
n = input[0].to_i
m = input[1].to_i
str = ""
if n==m
	n.times{str+="BG"}
elsif n > m
	m.times{str+="BG"}
	(n-m).times{str+="B"}
else
	n.times{str+="GB"}
	(m-n).times{str+="G"}
end
puts str

$stdin=File.open('input.txt')
$stdout=File.open('output.txt', 'w')
n,m=gets.split.map(&:to_i)
m>=n ? (print "GB"*[m,n].min; print "G"*(m-n) if n<m) : (print "BG"*[m,n].min; print "B"*(n-m) if n>m)

$stdin=File.open('input.txt')
$stdout=File.open('output.txt', 'w')
n,m=gets.split.map(&:to_i)
print "GB"*[m,n].min if m>=n
 print "BG"*[m,n].min if m<n
print "B"*(n-m) if n>m
print "G"*(m-n) if n<m

# -*- encoding: utf-8 -*-

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

n, m = gets.chomp.split(" ").map{|n| n.to_i}

if n == m
  puts "GB" * n
elsif n > m
  puts "BG" * m + "B" * (n - m)
else
  puts "GB" * n + "G" * (m - n)
end

$stdin=File.open('input.txt')
$stdout=File.open('output.txt','w')
n, m = gets.split.map(&:to_i)
puts (n <= m ? "GB" : "BG")*[n, m].min + (n <= m ? "G" : "B")*([n,m].max-[n,m].min)

f = File.open("input.txt", "r")
s = f.read.split
n = s[0].to_i
m = s[1].to_i
f.close

if n > m then
    s = "BG"*m + "B"*(n-m)
else
    s = "GB"*n + "B"*(m-n)
end

f = File.open("output.txt", "w")
f.write(s)
f.close
puts s

#!/usr/bin/ruby
f=nil
if true
	f=File.open('input.txt','rb')
else
	f=$<
end
b,g=f.gets.split.map(&:to_i)
m=[b,g].min
b-=m;g-=m
p [g,m,b]
puts 'G'*g+'BG'*m+'B'*b

#!/usr/bin/ruby
f=nil
if true
	f=File.open('input.txt','rb')
else
	f=$<
end
b,g=f.gets.split.map(&:to_i)
m=[b,g].min
b-=m;g-=m
puts 'G'*g+'BG'*m+'B'*b

#!/usr/bin/ruby
$stdin=File.open('input.txt','rb')
b,g=gets.split.map(&:to_i)
m=[b,g].min
b-=m;g-=m
puts 'G'*g+'BG'*m+'B'*b

$stdin=File.open('input.txt')
$stdout=File.open('output.txt', 'w')
n,m=gets.split.map(&:to_i)
print "GB"*[m,n].min if m>=n
 print "BG"*[m,n].min if m<n
print "B"*m if n>m
print "G"*m if n<m

$stdin=File.open('input.txt')
$stdout=File.open('output.txt', 'w')
n,m=gets.split.map(&:to_i)
print "GB"*[m,n].min
print "B"*m if n>m
print "G"*m if n<m

$stdin=File.open('input.txt')
$stdout=File.open('output.txt','w')
a=gets.chomp.split(" ")
b=a[0].to_i
g=a[1].to_i
ans=""
if b>=g
loop do
break if b==0 && g==0

if b!=0
ans+="B"
b-=1
end

if g!=0
ans+="G"
g-=1
end

end
else
loop do
break if b==0 && g==0

if b!=0
ans+="B"
b-=1
end

if g!=0
ans+="G"
g-=1
end

end
end

puts "#{ans}"

$stdin=File.open('input.txt')
$stdout=File.open('output.txt','w')
a=gets.chomp.split(" ")
b=a[0].to_i
g=a[1].to_i
ans=""

loop do
break if b==0 && g==0

if b!=0
ans+="B"
b-=1
end

if g!=0
ans+="G"
g-=1
end

end

puts "#{ans}"

m, n= File.open("input.txt").gets.split.map(&:to_i)
File.open("output.txt", "w") { |f|
  if m > n
    f.puts 'BG'*n + 'B'*(m-n)
  else
    f.puts 'GB'*n + 'G'*(n-m)
  end
}

n, m = IO.readlines("input.txt")[0].split.collect{|i| i.to_i}
if n >= m
    puts (['B','G']*m+['B']*(n-m)).join
else
    puts (['G','B']*n+['G']*(m-n)).join
end

b=gets.to_i
g=gets.to_i
if b == g
    b.times do
        puts "BG"
    end
end

if b>g
    g.times do
        puts "BG"
    end
    b-g.times do
         puts "B"
    end
end

if g>b
    b.times do
        puts "GB"
    end
    g-b.times do
         puts "G"
    end
end

$stdin=File.open('input.txt')
$stdout=File.open('output.txt','w')
a = gets.split.map(&:to_i)

if a[0] == a[1]
    a[0].times do
        print "BG"
    end
    exit
end

if a[0]>a[1]
    a[1].times do
        print "BG"
    end
    a[0]-a[1].times do
         print "B"
    end
    exit
end

if a[0]>a[1]
    a[1].times do
        print "GB"
    end
    a[1]-a[0].times do
         print "G"
    end
    exit
end

$stdin=File.open('input.txt')
$stdout=File.open('output.txt','w')
a = gets.split.map(&:to_i)
if a[0] == a[1]
    a[0].times do
        print "BG"
    end
end

if a[0]>a[1]
    a[1].times do
        print "BG"
    end
    a[0]-a[1].times do
         print "B"
    end
end

if a[0]>a[1]
    a[1].times do
        print "GB"
    end
    a[1]-a[0].times do
         print "G"
    end
end

$stdin=File.open('input.txt')
$stdout=File.open('output.txt','w')
a = gets.split.map(&:to_i)

if a[0] == a[1]
    a[0].times do
        print "BG"
    end
    exit
end

if a[0]>a[1]
    a[0].times do
        print "BG"
    end
    a[0]-a[1].times do
         print "B"
    end
    exit
end

if a[0]>a[1]
    a[1].times do
        print "GB"
    end
    a[1]-a[0].times do
         print "G"
    end
    exit
end

$stdin=File.open('input.txt')
$stdout=File.open('output.txt','w')
a = gets.split.map(&:to_i)

if a[0] == a[1]
    a[0].times do
        print "BG"
    end
    exit
end

if a[0]>a[1]
    a[1].times do
        print "BG"
    end
    a[0]-a[1].times do
         print "B"
    end
    exit
end

if a[1]>a[0]
    a[0].times do
        print "GB"
    end
    a[1]-a[0].times do
         print "G"
    end
    exit
end


t = gets.to_i
t.times do
    x0, n = gets.split.map(&:to_i)
    x = x0
    m = n / 4 * 4 + 1
    (m .. n).each do |i|
        if x.odd?
            x += i
        else
            x -= i
        end
    end
    puts x
end

gets.to_i.times do
  x,n=gets.split.map(&:to_i)
  if n%4==0
    puts x
  elsif n%4==1
    if x.even?
      puts x-n
    else
      puts x+n
    end
  elsif n%4==2
    if x.even?
      puts x+1
    else
      puts x-1
    end
  else
    if x.even?
      puts x+n+1
    else
      puts x-n-1
    end
  end
end