x, k = gets.split.collect{|i| i.to_i}
mark = Array.new(x-1,0)
(1..k).each do
    tmp = gets.split.collect{|i| i.to_i}
    mark[tmp[1]-1] = 1
    mark[tmp[2]-1] = 1 if tmp.size == 3
end
maxans = mark.count(0)
minans = maxans
tmp = 1
while tmp < x
    if mark[tmp] == 0 and mark[tmp-1] == 0
        minans -= 1
        tmp += 1
    end
    tmp += 1
end
print minans, " ", maxans, "\n"

x, k = gets.split.map(&:to_i)
c_min, c_max = 0
x -= 1
was = [false] * x
was[x] = true
for i in 1..k
  d = gets.split.map(&:to_i)
  for j in 1..d.length-1
    was[d[j] - 1] = true
  end
end
c_max = was.count { |x| not x }
i = 0
while i < x
  if not was[i]
    was[i] = was[i + 1] = true
    c_min += 1
  end
  i += 1
end
puts "#{c_min} #{c_max}"

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @current_round_iden, @num_of_took_rounds = @dats[0].split(" ").map{|x| x.to_i}
  end

  def solve
    if @num_of_took_rounds == 0
      max = @current_round_iden-1
      min = @current_round_iden % 2 == 0 ? @current_round_iden / 2 : (@current_round_iden) / 2
    else

      @unused_indentifiers = []
      @remember_rounds = []
      (1..@dats.length-1).each do |line_num|
        line = @dats[line_num]
        line = line.split(" ")
        line.shift
        line.each { |elem| @remember_rounds.push(elem.to_i) }
      end

      (@current_round_iden-1).downto(1) do |elem|
        @unused_indentifiers.push(elem) if not @remember_rounds.include?(elem)
      end

      min = find_min_possible(@unused_indentifiers)
      max = find_max_possible(@unused_indentifiers)
    end
    print "#{min} #{max}"
  end

  def find_min_possible(indentifier_array)
    ret = 0
    idx = 0
    indentifier_array.sort!

    while idx < indentifier_array.length
      if idx == indentifier_array.length - 1
        ret += 1
        return ret
      elsif indentifier_array[idx+1] == indentifier_array[idx]+1
        ret += 1
        idx += 2
      else
        ret += 1
        idx += 1
      end
    end
    return ret
  end

  def find_max_possible(indentifier_array)
    indentifier_array.length
  end
end

solver = ProblemA.new
solver.solve

def rsm(&block)
   readline.split.map{|i| block.yield(i)}
end

def rsmi
   rsm {|i| i.to_i}
end

x, k = rsmi
a = Array.new(x - 1, 0)
k.times {
   r = rsmi
   t, x = r[0], r[1]
   a[x - 1] = 2
   if t == 1
      y = r[2]
      a[y - 1] = 1
   end

}

s1 = a.count {|i| i == 0}
s2 = 0
a.length.times { |i|
   if a[i] == 0
      s2 += 1
      a[i] = 2
      if i + 1 < a.length
         a[i + 1] = 1 if a[i + 1] == 0
      end
   end
}
printf("%d %d", s2, s1)

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

A = Array.new(n) { |i|  0}

k.times {
   c,d1,d2 =  gets.chomp!.split.map { |e|  e.to_i}
   A[d1-1] = 1
   if d2 != nil
     A[d2-1] = 1
   end
}

mx=0
mi=0
cur=0
(n-1).times do |i|
   if A[i] == 0
        cur += 1
   else
       mx += cur
       mi += (cur+1)/2
       cur = 0
   end
end

mx += cur
mi += (cur+1)/2

puts "#{mi} #{mx}"

def get_pair_num(diff_ary,pair_num)
	if diff_ary.empty?
		return pair_num
	else
		if diff_ary.length > 1
			if (diff_ary[0] == diff_ary[1] - 1)
				diff_ary.delete_at(0)
				diff_ary.delete_at(0)
				return get_pair_num(diff_ary,pair_num+1)
			else
				diff_ary.shift
				return get_pair_num(diff_ary,pair_num)
			end
		end
		return pair_num
	end
end

def check_diff(id_ary,max_id)
	pair_num = 0
	full_ary = []
	for i in 1..max_id
		full_ary << i
	end
	diff_ary = full_ary - id_ary
	diff_ary.sort!
	pair_num = get_pair_num(diff_ary.clone, 0)
	max_num = diff_ary.length
	min_num = diff_ary.length - pair_num

	return [min_num, max_num]

end

first_line = (gets()).split()
current_id = (first_line.shift).to_i
part_in_num = (first_line.pop).to_i
id_ary = []
for i in 1..part_in_num
	ary = []
	(gets().split).each { |e| ary << e.to_i }
	ary.shift
	ary.each { |e| id_ary << e }
end
result = check_diff(id_ary, current_id-1)
puts result[0].to_s + " " + result[1].to_s

def solve(x, a)
  ids = Array.new(x + 1, false)
  a.each { |id|
    ids[id[1]] = true
    ids[id[2]] = true if id[0] == 1
  }
  ids[x] = true

  div1 = div2 = 0
  i = 1
  while i < x
    unless ids[i]
      if ids[i + 1]
        div2 += 1
      else
        div1 += 1
        i += 1
      end
    end
    i += 1
  end
  [div1 + div2, div1 * 2 + div2]
end

def read_next_line
  gets.chomp.split.map(&:to_i)
end

if __FILE__ == $0
  x, k = read_next_line
  a = (0...k).map { read_next_line }
  puts solve(x, a) * ' '
end

#f = File.open('test1.txt', 'r')
f = STDIN
x, k = f.gets.chomp.split.map(&:to_i)
a = Array.new(x, false)
a[0] = true
k.times do
  tmp = f.gets.chomp.split.map(&:to_i)
  if tmp[0] == 1
    a[tmp[1]], a[tmp[2]] = true
  else
    a[tmp[1]] = true
  end
end
maximum = a.count false
minimum = 0
(0...x).each do |i|
  if a[i] == false
    minimum += 1
    a[i+1] = true
  end
end
puts "#{minimum} #{maximum}"

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @current_round_iden, @num_of_took_rounds = @dats[0].split(" ").map{|x| x.to_i}
  end

  def solve
    if @num_of_took_rounds == 0
      max = @current_round_iden-1
      min = @current_round_iden % 2 == 0 ? @current_round_iden / 2 : @current_round_iden / 2 + 1
    else

      @unused_indentifiers = []
      @remember_rounds = []
      (1..@dats.length-1).each do |line_num|
        line = @dats[line_num]
        line = line.split(" ")
        line.shift
        line.each { |elem| @remember_rounds.push(elem.to_i) }
      end

      (@current_round_iden-1).downto(1) do |elem|
        @unused_indentifiers.push(elem) if not @remember_rounds.include?(elem)
      end

      min = find_min_possible(@unused_indentifiers)
      max = find_max_possible(@unused_indentifiers)
    end
    print "#{min} #{max}"
  end

  def find_min_possible(indentifier_array)
    ret = 0
    idx = 0
    indentifier_array.sort!

    while idx < indentifier_array.length
      if idx == indentifier_array.length - 1
        ret += 1
      end
      if indentifier_array[idx+1] == indentifier_array[idx]+1
        ret += 1
        idx += 2
      else
        ret += 1
        idx += 1
      end
    end
    return ret
  end

  def find_max_possible(indentifier_array)
    indentifier_array.length
  end
end

solver = ProblemA.new
solver.solve

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @cards_num, @max_abs = @dats[0].split(" ").map{|x| x.to_i}
    @cards = @dats[1].split(" ").map{|x| x.to_i}
  end

  def solve
    sum = @cards.inject(:+).abs
    return 1 if sum <= @max_abs
    return 0 if @max_abs == 0

    count_ret = 0
    (@max_abs-1).downto(1) do |elem|
      if sum >= elem
        count_ret += 1
        sum -= elem
      elsif sum == 1
        return count_ret+1
      elsif sum == 0
        return count_ret
      end
    end
    return count_ret
  end
end

solver = ProblemA.new
p solver.solve

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @current_round_iden, @num_of_took_rounds = @dats[0].split(" ").map{|x| x.to_i}
  end

  def solve
    if @num_of_took_rounds == 0
      max = @current_round_iden-1
      min = @current_round_iden % 2 == 0 ? @current_round_iden / 2 : @current_round_iden / 2 + 1
    else

      @unused_indentifiers = []
      @remember_rounds = []
      (1..@dats.length-1).each do |line_num|
        line = @dats[line_num]
        line = line.split(" ")
        line.shift
        line.each { |elem| @remember_rounds.push(elem.to_i) }
      end

      (@current_round_iden-1).downto(1) do |elem|
        @unused_indentifiers.push(elem) if not @remember_rounds.include?(elem)
      end

      min = find_min_possible(@unused_indentifiers)
      max = find_max_possible(@unused_indentifiers)
    end
    print "#{min} #{max}"
  end

  def find_min_possible(indentifier_array)
    ret = 0
    idx = 0
    indentifier_array.sort!

    while idx < indentifier_array.length
      if idx == indentifier_array.length - 1
        ret += 1
        return ret
      elsif indentifier_array[idx+1] == indentifier_array[idx]+1
        ret += 1
        idx += 2
      else
        ret += 1
        idx += 1
      end
    end
    return ret
  end

  def find_max_possible(indentifier_array)
    indentifier_array.length
  end
end

solver = ProblemA.new
solver.solve

class ProblemA
  def initialize
    @dats = STDIN.read.split("\n")
    @current_round_iden, @num_of_took_rounds = @dats[0].split(" ").map{|x| x.to_i}
  end

  def solve
    if @num_of_took_rounds == 0
      max = @current_round_iden-1
      min = @current_round_iden % 2 == 0 ? @current_round_iden / 2 : (@current_round_iden-1) / 2 + 1
    else

      @unused_indentifiers = []
      @remember_rounds = []
      (1..@dats.length-1).each do |line_num|
        line = @dats[line_num]
        line = line.split(" ")
        line.shift
        line.each { |elem| @remember_rounds.push(elem.to_i) }
      end

      (@current_round_iden-1).downto(1) do |elem|
        @unused_indentifiers.push(elem) if not @remember_rounds.include?(elem)
      end

      min = find_min_possible(@unused_indentifiers)
      max = find_max_possible(@unused_indentifiers)
    end
    print "#{min} #{max}"
  end

  def find_min_possible(indentifier_array)
    ret = 0
    idx = 0
    indentifier_array.sort!

    while idx < indentifier_array.length
      if idx == indentifier_array.length - 1
        ret += 1
        return ret
      elsif indentifier_array[idx+1] == indentifier_array[idx]+1
        ret += 1
        idx += 2
      else
        ret += 1
        idx += 1
      end
    end
    return ret
  end

  def find_max_possible(indentifier_array)
    indentifier_array.length
  end
end

solver = ProblemA.new
solver.solve

def answer(id_ary,max_id)
	pair_num = 0
	full_ary = []
	for i in 1..max_id
		full_ary << i
	end
	diff_ary = full_ary - id_ary
	if diff_ary.empty?
		return [0,0]
	else
		for i in 0..(diff_ary.length-1)
			if (i != diff_ary.length-1)
				if diff_ary[i] == diff_ary[i+1] - 1
					pair_num += 1
				end
			end
		end
	end
	max_num = diff_ary.length
	min_num = diff_ary.length - pair_num

	return [min_num, max_num]

end

first_line = (gets()).split()
current_id = (first_line.shift).to_i
part_in_num = (first_line.pop).to_i
id_ary = []
for i in 1..part_in_num
	ary = []
	(gets().split).each { |e| ary << e.to_i }
	ary.shift
	ary.each { |e| id_ary << e }
end
result = answer(id_ary, current_id-1)
puts result[0].to_s + " " + result[1].to_s

def answer(id_ary,max_id)
	pair_num = 0
	full_ary = []
	for i in 1..max_id
		full_ary << i
	end
	diff_ary = full_ary - id_ary
	if diff_ary.empty?
		return [0,0]
	else
		for i in 0..(diff_ary.length-1)
			if (i != diff_ary.length-1)
				if diff_ary[i] == diff_ary[i+1] - 1
					pair_num += 1
				end
			end
		end
	end
	max_num = diff_ary.length
	min_num = diff_ary.length - pair_num

	return [min_num, max_num]

end

first_line = (gets()).split()
current_id = (first_line.shift).to_i
part_in_num = (first_line.pop).to_i
id_ary = []
for i in 1..part_in_num
	ary = []
	(gets().split).each { |e| ary << e.to_i }
	ary.shift
	ary.each { |e| id_ary << e }
end
result = answer(id_ary, current_id-1)
puts result[0].to_s + result[1].to_s

i=->(x){x.split.map(&:to_i)}
n,_=i[gets]
a=$<.drop(0).collect{|x|i[x][1..-1]}.flatten.push(n).sort
x,y=0,0
(a.size-1).times do |k|
	z=a[k+1]-a[k]
	next if z == 1
	x,y=x+(z+1)/2,y+z
end
puts "#{x} #{y}"

i=->(x){x.split.map(&:to_i)}
n,_=i[gets]
a=$<.drop(0).collect{|x|i[x][1..-1]}.flatten.push(n).push(1).sort
x,y=0,0
(a.size-1).times do |k|
	z=a[k+1]-a[k]
	next if z <= 1
	x,y=x+(z+1)/2,y+z
end
puts "#{x} #{y}"


n = gets.to_i
print n,' ', (1...n).to_a.join(' ')

print n = gets.to_i,' ', (1...n).to_a.join(' ')

def main()
  n = gets.chomp.to_i
  p = [n]

  1.upto(n - 1) do |i|
    p << i
  end

  puts p.join(" ")

end

main()

n = gets.to_i

if n == 1
  puts 1
else
  print "#{n} "
  puts (1..(n-1)).to_a.map(&:to_s).join(" ")
end

n = gets.to_i
$arr = (1..n).to_a
def f(x)
  $arr[x-2], $arr[x-1] = $arr[x-1], $arr[x-2]
  f(x-1) if x-1>1
end
if n == 1
  puts n
  exit
end
f(n)
puts $arr.join(" ")

n=gets.to_i
print n
1.upto(n-1) {|i| print ' ',i}
puts

n=gets.to_i
if n==1 then
	p 1
else
	print n
	1.upto(n-1) {|i| print ' ',i}
	puts
end

print n=gets.to_i
1.upto(n-1) {|i| print ' ',i}

n = gets.to_i
if n==1
puts 1
elsif n==2
puts '2 1'
else
puts(([n,1]+(2...n).to_a).join ' ')
end

def f(x)
  return if x == 0
  f(x - 1)
  tmp = $a[x]
  $a[x] = $a[x - 1]
  $a[x - 1] = tmp
end

n = gets.to_i
ans = (1..n-1).to_a
ans.unshift n

puts ans.join ' '

n = STDIN.readline.to_i
ans = [n]
n -= 1
1.upto n do |i|
  ans << i
end

puts ans.join ' '

a = gets.to_i
print a
1.upto(a - 1) { |i| print ' ',i }

a=gets.to_i

print a
1.upto(a-1){|i| print ' ',i}

n = Integer(gets.chomp)
puts(([n] + (1..n - 1).to_a) * ' ')

a = gets.to_i
b = ""
c = 1
while(c<a)

b = b +c.to_s + " ";
c = c+1
end
puts a.to_s+" " + b

n = gets.to_i

p = Array.new(n) { |i, v| v = i + 1 }

(n - 1).downto(1) do |i|
	p[i - 1], p[i] = p[i], p[i - 1]
end
puts p.join ' '

def main()
  n = gets.chomp.to_i
  p = []

  n.downto(1) do |i|
    p << i
  end

  puts p.join(" ")

end

main()

n = gets.to_i

if n == 1
  puts 1
else
  puts (2..n).to_a.push(1).map(&:to_s).join(" ")
end

a=[]
n=gets.to_i
n.times{|i|a<<i+1}
a.push(a.shift)
n.times{|i|
	print a[i]
	if i != n-1 then print ' ' end}
puts

n = gets.to_i
puts RUBY_VERSION
if n==1
puts 1
elsif n==2
puts '2 1'
else
puts(([1,n]+(2...n).to_a).join ' ')
end

n = gets.to_i
if n==1
puts 1
elsif n==2
puts '2 1'
else
puts(([1,n]+(2...n).to_a).join ' ')
end

n = gets.to_i
puts ((1..n).to_a.reverse)

n = gets.to_i
if n==1
puts 1
elsif n==2
puts '1 2'
else
puts(([1,n]+(2...n).to_a).join ' ')
end

a = gets.to_i
print a
1.upto(a) { |i| print ' ',i }

a = geta = gets.to_i
b = ""
c = 2
while(c<=a)

b = b +c.to_s + " ";
c = c+1
end
puts b+"1"

n = gets.to_i

p = Array.new(n) { |i, v| v = i + 1 }

1.upto(n - 1) do |i|
	p[i - 1], p[i] = p[i], p[i - 1]
end
puts p.join ' '


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

t = gets.to_i
curr = 1

arr = []
t.times do
	str = gets.chomp
	if(str[0]=='f')
		arr.push str.split(" ")[-1].to_i
	elsif(str[0]=='a')
		arr.push 0
	else
		arr.push -1
	end
end

max = 2**32-1

list = []
workslist = []
ans=0
works = true

arr.each do |x|

#	p list
#	p workslist

	if(x==0)
		if(!works)
			puts "OVERFLOW!!!"
			exit
		end
		ans+=curr
	elsif(x>0)
		list.push x
		newmax = curr*x
		if(works && newmax < max)
			curr = newmax
			workslist.push works
		else
			works = false
			workslist.push works
		end
	else
		workslist.pop
		a = list.pop
		if(works)
			curr/=a
		end
		if(workslist.size>0)
			works = workslist[-1]
		else
			works = true
		end
	end

	if(ans>max)
		puts "OVERFLOW!!!"
		exit
	end
end

puts ans

l = gets.to_i
lines = (0...l).map { gets.strip }
ans = 0
cur = 1
stk = []
ptr = 0
until ptr == l
  if lines[ptr].match(/^for/)
    b = lines[ptr].match(/\d+/)[0].to_i
    cur *= b
    stk.push(b)
  elsif lines[ptr].match(/^add/)
    ans += cur
  else
    cur /= stk.pop
  end
  ptr += 1
  until cur < 1e10
    if lines[ptr].match(/^for/)
      stk.push(1)
    elsif lines[ptr].match(/^add/)
      puts "OVERFLOW!!!"
      exit
    else
      cur /= stk.pop
    end
    ptr += 1
  end
end
if ans < 2**32 - 0
  puts ans
else
  puts "OVERFLOW!!!"
end

l = gets.to_i
lines = (0...l).map { gets.strip }
ans = 0
cur = 1
stk = []
ptr = 0
until ptr == l
  if lines[ptr].match(/^for/)
    b = lines[ptr].match(/\d+/)[0].to_i
    cur *= b
    stk.push(b)
  elsif lines[ptr].match(/^add/)
    ans += cur
  else
    cur /= stk.pop
  end
  ptr += 1
  until cur < 1e10
    if lines[ptr].match(/^for/)
      stk.push(1)
    elsif lines[ptr].match(/^add/)
      puts "OVERFLOW!!!"
      exit
    else
      cur /= stk.pop
    end
    ptr += 1
  end
end
if ans < 2**32 - 1
  puts ans
else
  puts "OVERFLOW!!!"
end

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

t = gets.to_i
curr = 1

arr = []
t.times do
	str = gets.chomp
	if(str[0]=='f')
		arr.push str.split(" ")[-1].to_i
	elsif(str[0]=='a')
		arr.push 0
	else
		arr.push -1
	end
end

max = 2**32-1

list = []
workslist = []
ans=0
works = true

arr.each do |x|
	if(x==0)
		if(!works)
			puts "OVERFLOW!!!"
			exit
		end
		ans+=curr
	elsif(x>0)
		list.push x

		newmax = curr*x
		if(newmax < max)
			curr = newmax
			workslist.push true
		else
			works = false
			workslist.push false
		end
	else
		works = workslist.pop
		a = list.pop
		if(works)
			curr/=a
		end
	end

	if(ans>max)
		puts "OVERFLOW!!!"
		exit
	end
end

puts ans

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

t = gets.to_i
curr = 1

arr = []
t.times do
	str = gets.chomp
	if(str[0]=='f')
		arr.push str.split(" ")[-1].to_i
	elsif(str[0]=='a')
		arr.push 0
	else
		arr.push -1
	end
end

max = 2**32-1

list = []
workslist = []
ans=0
works = true

arr.each do |x|
	if(x==0)
		if(!works)
			puts "OVERFLOW!!!"
			exit
		end
		ans+=curr
	elsif(x>0)
		list.push x

		newmax = curr*x
		if(works && newmax < max)
			curr = newmax
			workslist.push works
		else
			works = false
			workslist.push works
		end
	else
		works = workslist.pop
		a = list.pop
		if(works)
			curr/=a
		end
	end

	if(ans>max)
		puts "OVERFLOW!!!"
		exit
	end
end

puts ans


n = gets.to_i
a = gets.split.map(&:to_i)
a.sort!
res = 0
sum = 0
a.each do
  |x|
  if(sum <= x)
  sum+=x
  res+=1
  end
end
print res

def test(ary)
  sum = 0
  res = 0

  ary.sort.each do |el|
    next unless el >= sum
    sum += el
    res += 1
  end

  res
end

a = Kernel.gets
puts test(Kernel.gets.split.map(&:to_i))

n = gets.to_i
t = gets.split.map(&:to_i)
sum = 0
ans = 0
t.sort.each do |x|
    if x >= sum
        sum += x
        ans += 1
    end
end
puts ans

gets
x=gets.split.map{|e| e.to_i}.sort
sum=0
ans=0

x.each{|e|
	if sum<=e then
		sum+=e
		ans+=1
	end
}
puts ans

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

count = 1
a = a.sort
t = a[0]
arr = []
for i in (1..a.size-1)
	x = i
	while a[x] < t && x < n-1
		x += 1
	end
	if a[x] != nil && a[x] >= t
		count += 1
		t += a[x]
	else
		break
	end
end
print count

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i
ts = gets.split.map(&:to_i)
ts.sort!
acc = ts[0]
satisfied = 1
# p ts
ts[1..-1].each { |i|
    # p "Satisfied? #{i} #{acc}"
    if acc <= i
        satisfied += 1
        acc += i
    else
        # 다음 사람이 만족할 수 있는 고객이라면,
        # 다가올 고객을 제일 뒤로 보낸다고 생각하자.
        # 스킵해도 무방하다.
        next
    end
}

puts satisfied

n = Integer(gets)
nums = gets.chomp.split
queue = []
for i in 0...n
    queue[i] = Integer(nums[i])
end

queue.sort!
sum = 0
count = 0
for i in 0...n
    if sum <= queue[i]
        sum += queue[i]
        count += 1
    else
        queue.push(queue[i])
        queue[i] = 0
    end
end
puts count

gets
pleased = 0
time = 0
gets.split(' ').map(&:to_i).sort.each do |wait|
  if time <= wait
    pleased += 1
    time += wait
  end
end
puts pleased

n = gets.to_i
t = gets.split.collect{|i| i.to_i}.sort
sum = 0
ans = 0
t.each do |tt|
    if tt >= sum
        sum += tt
        ans += 1
    end
end
puts ans

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i
ts = gets.split.map(&:to_i)
ts.sort!
acc = ts[0]
satisfied = 1
ts[1..-1].each { |i|
    if acc <= i
        satisfied += 1
    end

    acc += i
}

puts satisfied

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

t = 0
a = a.sort
count = 1
for i in (1..n-1)
	t += a[i-1]
	if a[i] >= t
		count += 1
	end
end
print count

def test(ary)
  sum = 0
  res = 0

  ary.sort.each do |el|
    next unless el > sum
    sum += el
    res += 1
  end

  res
end

a = Kernel.gets
puts test(Kernel.gets.split.map(&:to_i))

# coding: utf-8

# if __FILE__ == $0
# end

n = gets.to_i
ts = gets.split.map(&:to_i)
ts.sort!
acc = ts[0]
satisfied = 1
p ts
ts[1..-1].each { |i|
    # p "Satisfied? #{i} #{acc}"
    if acc <= i
        satisfied += 1
        acc += i
    else
        # 다음 사람이 만족할 수 있는 고객이라면,
        # 다가올 고객을 제일 뒤로 보낸다고 생각하자.
        # 스킵해도 무방하다.
        next
    end
}

puts satisfied


R, D = gets.split.map &:to_i
n = gets.to_i
EPS = 1e-9

cnt = 0
n.times do
    x, y, r = gets.split.map &:to_i
    d = (x**2 + y**2)**0.5
    # puts("#{R} #{D} #{d} #{r} #{R-D-EPS} #{r-d} #{r+d} #{R+EPS}")
    cnt += 1 if R-D-EPS < d-r && d+r < R+EPS
end

puts cnt

R, D = gets.chomp.split.map(&:to_i)
n = gets.to_i
ans = 0

n.times do
  x, y, r = gets.chomp.split.map(&:to_i)
  hypot = x * x + y * y
  outer = (hypot <= (R - r) ** 2 && (R - r) > 0)
  inner = ((R - D + r) <=0 || (R - D + r) ** 2 <= hypot)
  ans += 1 if outer and inner
end

p ans

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

ans = 0
n.times do
    x, y, r = gets.split.map(&:to_i)
    if R - d <= Math.hypot(x, y) - r and Math.hypot(x, y) + r <= R
        ans += 1
    end
end

puts ans

r, d = gets.split.map(&:to_i)
n = gets.to_i
ans = 0
r2 = (r - d)
n.times do |i|
    x, y, z = gets.split.map(&:to_i)
    hoge = x ** 2 + y ** 2
    if (r2 + z) * (r2 + z) <= hoge && hoge <= (r - z) * (r - z)
        ans += 1
    end
end
puts ans


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

m = 0

current_m = a.shift
b = [current_m]

until a.empty?
  candidate = a.shift
  if candidate > current_m + 1 # candidate == current_m + 2
    last_one = b.rindex(current_m)
    b.slice!(0..last_one)
    current_m += 1
  elsif candidate < current_m # candidate == current_m - 1
    last_one = b.rindex(current_m + 1)
    b.slice!(0..last_one) if last_one
    current_m -= 1
  end
  b << candidate
  m = b.size if b.size > m
end

puts m

n = gets.chomp.to_i
nums = gets.split.map { |s| s.to_i }
#puts nums
prev = 0
prev_pos = 0
next_pos = 0
ans = 0
curr = []
curr_pos = []
start = []

nums.size.times do |i|
    if curr.size < 2 and not curr.include? nums[i] then
        curr << nums[i]
        curr_pos << i
        start << i
    elsif not curr.include? nums[i] then
        curr = curr.drop(1)
        if curr_pos[0] > start[1] then
            start[1] = curr_pos[0] + 1
        end
        curr_pos = curr_pos.drop(1)
        start = start.drop(1)
        curr << nums[i]
        curr_pos << i
        start << i
    else
        if curr[0] == nums[i] then
            curr = curr.drop(1)
            curr_pos = curr_pos.drop(1)
            z = start[0]
            start = start.drop(1)
            curr << nums[i]
            curr_pos << i
            start << z
        else
            curr_pos[1] = i
        end
    end
    ans = [ans,curr_pos.max - start.min + 1].max
    #print curr
    #print curr_pos
    #print start
    #puts ans
end

puts ans

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU

def gi(); gets.to_i; end
def gis(); gets.chomp.split.map(&:to_i); end

$stdout = STDERR

n = gi
a = gis

mx = a[0]
mn = a[0]
bf = a[0]
pos = 0
ans = 1
len = 1
1.upto(n - 1) do |i|
    x = a[i]
    puts "i=%d x=%d bf=%d mn=%d mx=%d len=%d pos=%d ans=%d" % [i,x,bf,mn,mx,len,pos,ans]
    if bf == x
        len += 1
        puts "bf==x"
    else
        if (mx - x).abs <= 1 && (mn - x).abs <= 1
            len += 1
            if mx == mn
                mn, mx = [mx, x].minmax
            end
            puts "swap mx mn"
        else
            ans = len if len > ans
            if (bf - x).abs <= 1
                len = i - pos + 1
                mn, mx = [bf, x].minmax
                puts "continue bf=%d" % [bf]
            else
                len = 1
                mx = mn = x
                puts "new state"
            end
        end
        pos = i
        bf = x
    end
end

STDOUT.puts [ans, len].max

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

bs = as.map { |a| a / 2 }
cs = as.map { |a| (a + 1) / 2 }
cntb = 1
cntc = 1
ans = 2
for i in 1...n
  if bs[i] == bs[i - 1]
    cntb += 1
    ans = [ans, cntb].max
  else
    cntb = 1
  end
  if cs[i] == cs[i - 1]
    cntc += 1
    ans = [ans, cntc].max
  else
    cntc = 1
  end
end

puts ans

n = gets.chomp.to_i
a = gets.split(" ").map(&:to_i)
u = max = 1
amin = amax = a[0]
imin = imax = 0
for i in 1...n
	if a[i] == a[i-1]
		u += 1
	else
		if amin <= a[i] and a[i]<= amax
			u += 1
			if a[i] == amin
				imin = i
			else
				imax = i
			end
		elsif a[i] > amax
			amin = amax
			amax = a[i]
			u = i - imax + 1
			imin = imax
			imax = i
		else
			amax = amin
			amin = a[i]
			u = i - imin + 1
			imax = imin
			imin = i
		end
	end
	if u > max
		max = u
	end
end
puts max

def main(numbers)
    odds = numbers.map { |n| n.odd? ? n : n + 1 }
    odds_length = find_greatest(odds)
    evens = numbers.map { |n| n.even? ? n : n + 1 }
    evens_length = find_greatest(evens)
    puts [odds_length, evens_length].max
end

def find_greatest(numbers)
    last_number = -100000
    in_a_row = 0
    max_in_a_row = 0
    numbers.each do |n|
        if n == last_number
            in_a_row += 1
        else
            last_number = n
            in_a_row = 1
        end
        max_in_a_row = [max_in_a_row, in_a_row].max
    end
    max_in_a_row
end

ARGF.each_with_index do |line, index|
    if index == 1
        main(line.chomp.split(' ').map(&:to_i))
    end
end

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

l = r = n = 0
ans = 0
while l < N && r < N
  r = l = n
  max = an[r]
  min = an[r]
  until max-min > 1
    r += 1
    break if r >= N
    min, max = [min, max, an[r]].minmax
    n = r if n == l && an[l] != an[r]
  end
  ans = [ans, r - l].max
end

puts ans

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

left = 0
prev = 0
ans = 1
tmp = 0
(1...n).each do |i|
  d = a[i] - a[i-1]
  if d != 0 then
    if d == tmp then
      left = prev + 1
    end
    tmp = d
    prev = i - 1
  end
  ans = [ans, i - left + 1].max
end
puts ans

n = gets.chomp.to_i
nums = gets.split.map { |s| s.to_i }
#puts nums
prev = 0
prev_pos = 0
next_pos = 0
ans = 0
curr = []
curr_pos = []
start = []

nums.size.times do |i|
    if curr.size < 2 then
        curr << nums[i]
        curr_pos << i
        start << i
    elsif not curr.include? nums[i] then
        curr = curr.drop(1)
        if curr_pos[0] > start[1] then
            start[1] = curr_pos[0] + 1
        end
        curr_pos = curr_pos.drop(1)
        start = start.drop(1)
        curr << nums[i]
        curr_pos << i
        start << i
    else
        if curr[0] == nums[i] then
            curr = curr.drop(1)
            curr_pos = curr_pos.drop(1)
            z = start[0]
            start = start.drop(1)
            curr << nums[i]
            curr_pos << i
            start << z
        else
            curr_pos[1] = i
        end
    end
    ans = [ans,curr_pos.max - start.min + 1].max
    #print curr
    #print curr_pos
    #print start
    #puts ans
end

puts ans

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

m = 0

current_m = a.shift
b = [current_m]

until a.empty?
  candidate = a.shift
  if candidate > current_m + 1 # candidate == current_m + 2
    last_one = b.index(current_m)
    b.slice!(0..last_one)
    current_m = b.min
  elsif candidate < current_m # candidate == current_m - 1
    last_one = b.index(current_m + 1)
    b.slice!(0..last_one) if last_one
    current_m = b.min
  end
  b << candidate
  m = b.size if b.size > m
end

puts m

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

m = 0

current_m = a.shift
b = [current_m]

until a.empty?
  candidate = a.shift
  if candidate > current_m + 1
    last_one = b.index(current_m)
    b.slice!(0..last_one)
    current_m += 1
  elsif candidate < current_m
    last_one = b.index(current_m + 1)
    b.slice!(0..last_one) if last_one
    current_m -= 1
  end
  b << candidate
  m = b.size if b.size > m
end

puts m

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

m = 0

current_m = a.shift
b = [current_m]

until a.empty?
  candidate = a.shift
  if candidate > current_m + 1 # candidate == current_m + 2
    last_one = b.index(current_m)
    b.slice!(0..last_one)
    current_m = b.min
  elsif candidate < current_m # candidate == current_m - 1
    last_one = b.index(current_m + 1)
    b.slice!(0..last_one) if last_one
    current_m = b.min - 1
  end
  b << candidate
  m = b.size if b.size > m
end

puts m

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

bs = (1...n).map { |i| as[i] - as[i - 1] }
ans = 0
im, iz, ip = [n, n, n]
if bs[0] == -1
  im = 0
elsif bs[0] == 1
  ip = 0
else
  iz = 0
end
for i in 1...n - 1
  if bs[i] == -1
    if bs[i - 1] == 0
      if ip == iz - 1
        ans = [ans, i - ip + 2].max
      else
        ans = [ans, i - iz + 2].max
      end
    elsif ip == i - 1
      ans = [ans, 3].max
    elsif im == i - 1
      ans = [ans, 2].max
    end
    im = i
  elsif bs[i] == 1
    if bs[i - 1] == 0
      if im == iz - 1
        ans = [ans, i - im + 2].max
      else
        ans = [ans, i - iz + 2].max
      end
    elsif im == i - 1
      ans = [ans, 3].max
    elsif ip == i - 1
      ans = [ans, 2].max
    end
    ip = i
  else
    if bs[i - 1] == 0
      if iz == 0
        ans = [ans, i - iz + 2].max
      else
        ans = [ans, i - iz + 3].max
      end
    else
      ans = [ans, 2].max
      iz = i
    end
  end
end

puts ans

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

bs = (1...n).map { |i| as[i] - as[i - 1] }
ans = 0
im, iz, ip = [n, n, n]
if bs[0] == -1
  im = 0
elsif bs[0] == 1
  ip = 0
else
  iz = 0
end
for i in 1...n - 1
  if bs[i] == -1
    if bs[i - 1] == 0
      if ip == iz - 1
        ans = [ans, i - ip + 2].max
      else
        ans = [ans, i - iz + 2].max
      end
    elsif ip == i - 1
      ans = [ans, 3].max
    elsif im == i - 1
      ans = [ans, 2].max
    end
    im = i
  elsif bs[i] == 1
    if bs[i - 1] == 0
      if im == iz - 1
        ans = [ans, i - im + 2].max
      else
        ans = [ans, i - iz + 2].max
      end
    elsif im == i - 1
      ans = [ans, 3].max
    elsif ip == i - 1
      ans = [ans, 2].max
    end
    ip = i
  else
    if bs[i - 1] == 0
      if iz == 0
        ans = [ans, i - iz + 2].max
      else
        ans = [ans, i - iz + 3].max
      end
    else
      ans = [ans, 3].max
      iz = i
    end
  end
end

puts ans


gets.to_i.times do
    n=gets.chomp.to_i
    s=gets.chomp
    zeros,ones,maxZeros,maxOnes,currentZeros,currentOnes=0,0,0,0,0,0
    s.each_char do |c|
        if c=='0'
            zeros+=1
            currentZeros+=1
            maxZeros=[maxZeros,currentZeros].max
            currentOnes=0
        else
            ones+=1
            currentOnes+=1
            maxOnes=[maxOnes,currentOnes].max
            currentZeros=0
        end
    end
    puts [zeros*ones,maxZeros*maxZeros, maxOnes*maxOnes].max
end

def aaa(_n, s)
  s.delete!("\n")
  max0 = s.split(/0+/).map(&:length).sort.reverse[0].to_i
  max1 = s.split(/1+/).map(&:length).sort.reverse[0].to_i
  arr = [s.delete('0').length * s.delete('1').length, max0*max0, max1*max1]

  puts arr.sort[2]
end

t = gets.to_i

t.times do
  n = gets.to_i
  s = gets
  aaa(n, s)
end


def read_array
  gets.strip.split(' ').map(&:to_i)
end

def can(a, b, x)
  a.each_with_index do |e, i|
    return false if e + b[i] > x
  end
  true
end

gets.strip.to_i.times do
  n, x = read_array
  a = read_array
  b = read_array
  gets

  a.sort!
  b = b.sort.reverse
  if can(a, b, x)
    puts "Yes"
  else
    puts "No"
  end
end

def read
  gets.split.map &:to_i
end
q = gets.to_i
q.times do |t|
  n, x = read
  a, b = read, read
  a.sort!
  b.sort!.reverse!
  ok = true
  (0...n).each do |i|
    ok = false if a[i] + b[i] > x
  end
  puts ok ? "YES" : "NO"
  if t < q - 1
    gets
  end
end

def read
  gets.split.map &:to_i
end
q = gets.to_i
q.times do |t|
  n, x = read
  a, b = read, read
  a.sort!
  b.sort!.reverse!
  ok = true
  (0...n).each do |i|
    ok = false if a[i] + b[i] > x
  end
  print(ok ? "YES" : "NO")
  if t < q - 1
    gets
  end
end


#!/usr/bin/env ruby

cur=[0]
gets.to_i.times{
	n=gets.to_i
	s=0
	idx=(cur.size.times.find{|i|s+=cur[i];s>=n}||cur.size)-1
	if idx==-1
		a=[1]+cur.map{|e|0}
	else
		a=cur.dup
		while a[idx]==9
			idx-=1
		end
		if idx>=0
			a[idx]+=1
		else
			a=[1]+a
			idx=0
		end
		(idx+1).upto(a.size-1){|i|a[i]=0}
	end
	#p a
	fill=n-a.reduce(:+)
	idx=a.size-1
	while idx>=0
		a[idx]+=fill
		fill=0
		if a[idx]>9
			fill=a[idx]-9
			a[idx]=9
		end
		idx-=1
	end
	d,r=fill.divmod(9)
	a=(r==0?[]:[r])+[9]*d+a
	cur=a
	puts cur*''
}

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

cur = [0]
gets.to_i.times {
  n = gets.to_i
  s = 0
  idx = (cur.size.times.find{|i|
    s += cur[i]
    s >= n
  } || cur.size) - 1
  if idx == -1
    x = [1] + [0] * cur.size
  else
    x = cur.dup
    while x[idx] == 9
      idx -= 1
    end
    if idx >= 0
      x[idx] += 1
    else
      x = [1] + x
      idx = 0
    end
    (idx + 1).upto(x.size - 1) {|i|
      x[i] = 0
    }
  end
  tot = n - x.reduce(:+)
  idx = x.size - 1
  while idx >= 0
    x[idx] += tot
    tot = 0
    if x[idx] > 9
      tot = x[idx] - 9
      x[idx] = 9
    end
    idx -= 1
  end
  l, r = tot.divmod(9)
  x = (r == 0 ? [] : [r]) + [9] * l + x
  cur = x
  puts cur.join
}

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

cur = [0]
gets.to_i.times {
  n = gets.to_i
  s = 0
  idx = (cur.size.times.find{|i|
    s += cur[i]
    s >= n
  } || cur.size) - 1
  x = cur.dup
  while x[idx] == 9
    idx -= 1
  end
  if idx >= 0
    x[idx] += 1
  else
    x = [1] + x
    idx = 0
  end
  (idx + 1).upto(x.size - 1) {|i|
    x[i] = 0
  }
  tot = n - x.reduce(:+)
  idx = x.size - 1
  while idx >= 0
    x[idx] += tot
    tot = 0
    if x[idx] > 9
      tot = x[idx] - 9
      x[idx] = 9
    end
    idx -= 1
  end
  l, r = tot.divmod(9)
  x = (r == 0 ? [] : [r]) + [9] * l + x
  cur = x
  puts cur.join
}

#!/usr/bin/env ruby

cur=[0]
gets.to_i.times{
	n=gets.to_i
	s=0
	idx=(cur.size.times.find{|i|s+=cur[i];s>n}||cur.size)-1
	if idx==-1
		a=[1]+cur.map{|e|0}
	else
		a=cur.dup
		while a[idx]==9
			idx-=1
		end
		if idx>=0
			a[idx]+=1
		else
			a=[1]+a
			idx=0
		end
		(idx+1).upto(a.size-1){|i|a[i]=0}
	end

	fill=n-a.reduce(:+)
	idx=a.size-1
	while idx>=0
		a[idx]+=fill
		fill=0
		if a[idx]>9
			fill=a[idx]-9
			a[idx]=9
		end
		idx-=1
	end
	d,r=fill.divmod(9)
	a=(r==0?[]:[r])+[9]*d+a
	cur=a
	puts cur*''
}

#!/usr/bin/env ruby
def gen(n)
	return to_enum(:gen,n) if !block_given?
	d,r=n.divmod(9)
	a=(r==0?[]:[r])+[9]*d
	digits=a.size
	loop{
		yield a.join.to_i
		idx_down=(a.size-1).downto(0).find{|i|a[i]!=0}
		if idx_down!=0 && idx_up=(idx_down-1).downto(0).find{|i|a[i]<9}
			a[idx_down]-=1
			a[idx_up]+=1
		else
			digits+=1
			d,r=(n-1).divmod(9)
			a=(r==0?[]:[r])+[9]*d
			while a.size<digits-1
				a.unshift(0)
			end
			a=[1]+a
		end
	}
end
cur=0
gets.to_i.times{
	n=gets.to_i
	nxt=gen(n).find{|e|e>cur}
	p nxt
	cur=nxt
}

#!/usr/bin/env ruby
def gen(n)
	return to_enum(:gen,n) if !block_given?
	d,r=n.divmod(9)
	a=(r==0?[]:[r])+[9]*d
	digits=a.size
	loop{
		yield a.join.to_i
		idx_down=(a.size-1).downto(0).find{|i|a[i]!=0}
		if idx_down!=0 && idx_up=(idx_down-1).downto(0).find{|i|a[i]<9}
			a[idx_down]-=1
			a[idx_up]+=1
			a=a[0,idx_down]+a[idx_down..-1].reverse
		else
			digits+=1
			d,r=(n-1).divmod(9)
			a=(r==0?[]:[r])+[9]*d
			while a.size<digits-1
				a.unshift(0)
			end
			a=[1]+a
		end
	}
end
cur=0
gets.to_i.times{
	n=gets.to_i
	nxt=gen(n).find{|e|e>cur}
	p nxt
	cur=nxt
}

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

cur = [0]
gets.to_i.times {
  n = gets.to_i
  s = 0
  idx = (cur.size.times.find{|i|
    s += cur[i]
    s >= n
  } || cur.size) - 1
  x = cur.dup
  while x[idx] == 9
    idx -= 1
  end
  if idx >= 0
    x[idx] += 1
  else
    x = [1] + x
    idx = 0
  end
  (idx + 1).upto(x.size - 1) {|i|
    x[i] = 0
  }
  tot = n - x.reduce(:+)
  idx = 0
  while idx < x.size
    x[idx] += tot
    tot = 0
    if x[idx] > 9
      tot = x[idx] - 9
      x[idx] = 9
    end
    idx += 1
  end
  l, r = tot.divmod(9)
  x = (r == 0 ? [] : [r]) + [9] * l + x
  cur = x
  puts cur.join
}

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

cur = [0]
gets.to_i.times {
  n = gets.to_i
  s = 0
  idx = (cur.size.times.find{|i|
    s += cur[i]
    s >= n
  } || cur.size) - 1
  if idx == -1
    x = [1] + [0] * cur.size
  else
    x = cur.dup
    while x[idx] == 9
      idx -= 1
    end
    if idx >= 0
      x[idx] += 1
    else
      x = [1] + x
      idx = 0
    end
    (idx + 1).upto(x.size - 1) {|i|
      x[i] = 0
    }
  end
  tot = n - x.reduce(:+)
  idx = x.size - 1
  while idx >= 0
    x[idx] += tot
    tot = 0
    if x[idx] > 9
      tot = x[idx] - 9
      x[idx] = 9
    end
    idx -= 1
  end
  l, r = tot.divmod(9)
  x = (r == 0 ? [] : [r]) + [9] * l + x
  cur = x
  puts cur.to_s
}


t = gets.to_i
t.times do |i|
    n = gets.to_i
    a = gets.split.map(&:to_i)
    b = c = 0
    n.times do |i|
        c = a[i] if c < a[i]
        b,c = c,b if c > b
    end
    puts (c-1 < n-2 ? c-1 : n-2)
end

gets.to_i.times do
  n = gets.to_i
  a, b = gets.split.map(&:to_i).sort.reverse
  p [a-1, b-1, n-2].min
end


class A
  def initialize
    n, m, k = gets.chomp.split(' ').map(&:to_i)
    type_list = gets.chomp.split(' ').map(&:to_i)
    cnt = 0

    type_list.each do |type|
      if type == 1
        if m > 0
          m -= 1
        else
          cnt += 1
        end
      else
        if k > 0
          k -= 1
        elsif m > 0
          m -= 1
        else
          cnt += 1
        end
      end
    end

    puts cnt
  end
end

A.new

n, m, k = gets.split(' ').map(&:to_i)
wash = 0
gets.split(' ',n).map(&:to_i).each do |x|
  if x == 1
    m > 0  ? m -= 1 : wash += 1
  elsif k > 0
    k -= 1
  elsif m > 0
    m -= 1
  else
    wash += 1
  end
end
puts wash

n, m, k = gets.split.map(&:to_i)
n = 0
gets.split.map(&:to_i).each do |a|
  if a == 2 and k > 0
    k -= 1
  else
    (m > 0) ? m -= 1 : n += 1
  end
end
puts n

n, m, k = gets.chomp.split.map(&:to_i);
arr = gets.chomp.split.map(&:to_i);
ans = 0;

arr.each do |x|
  if (x == 1) then
    if (m > 0)
      m -= 1;
    else
      ans += 1;
    end
  elsif (k > 0)
    k -= 1;
  elsif (m > 0)
    m -= 1;
  else
    ans += 1;
  end
end
p ans;

#!/usr/bin/ruby

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

r = 0
a.each do |x|
  if x == 1
    m -= 1
  else # x == 2
    if k > 0
      k -= 1
    else
      m -= 1
    end
  end

  if m < 0
    m += 1
    r += 1
  elsif k < 0
    k += 1
    r += 1
  end
end

puts r

n,m,k=gets.split.map{|e| e.to_i}
xs=gets.split.map{|e| e.to_i}
ans=0
c1=xs.count{|e|
	e==1
}
c2=xs.count{|e|
	e==2
}
ans=0
if c1>m
	ans=c1-m
else
	k=m+k-c1
end
ans+=c2-k if c2>k
puts ans

l = gets.split(" ").map { |e| e.to_i }
glub = l[1]
plos = l[2]
ans = 0
mas = gets.split(" ").map { |e| e.to_i }
mas.each { |e|
	if e == 1 then
		if glub > 0 then
			glub -= 1
		else
			ans +=1
		end
	else
		if plos > 0 then
				plos -= 1
		elsif glub > 0
			glub -= 1
		else
			ans +=1
		end
	end
}
puts ans

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

_, m, k = read_next_line
a = read_next_line.inject(Hash.new(0)) { |h, e| h[e] += 1; h }
d = 0
c = m - a[1]
if c > 0
  k += c
else
  d = -c
end
d += [a[2] - k, 0].max
puts d


n = gets.to_i
array = Array.new

n.times do
  miniarr = gets.chomp.split(" ")
  miniarr[0] = miniarr[0].to_i
  miniarr[1] = miniarr[1].each_char.to_a.sort.join("")
  array.push miniarr
end
#p array

combcost = Hash.new(1000000)

array.each do |juice|
  cost = juice[0]
  type = juice[1]

  if cost < combcost[type]
    combcost[type] = cost
  end
end

ans = [combcost["A"]+combcost["B"]+combcost["C"],combcost["A"]+combcost["BC"],combcost["AB"]+combcost["C"],combcost["B"]+combcost["AC"],combcost["AB"]+combcost["BC"],combcost["AC"]+combcost["BC"],combcost["AB"]+combcost["AC"],combcost["ABC"]].min

if ans>300000
  ans = -1
end
puts ans

def solution

  n = gets.to_i

  prices = {}

  n.times do
    p, l = gets.chomp.split(' ')
    price = p.to_i
    vits = l.chars.sort.join

    prices[vits] = price if prices[vits].nil? || prices[vits] > price
  end

  def eval_price(prices, fragments)
    frag_prices = fragments.map{ |f| prices[f] }
    frag_prices.reduce(0, :+)
  rescue
    nil
  end

  possibilities = [
    %w"ABC",
    %w"A B C",
    %w"A BC",
    %w"AB C",
    %w"B AC",
    %w"AB AC",
    %w"AB BC",
    %w"AC AB",
    %w"AC BC",
  ]

  cheapest = possibilities.map{ |p| eval_price(prices, p) }.compact.min || -1

  puts cheapest
end

solution unless ENV['TEST__MODE']

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

def clac_min(str,n,m)
  ret = 10**6
  for i in 0..n-1
    if str.size == 1
      if m[i][1].include?(str)
        ret = m[i][0] if m[i][0] < ret
      end
    elsif str.size == 2
      if m[i][1].include?(str[0]) && m[i][1].include?(str[1])
        ret = m[i][0] if m[i][0] < ret
      end
    else
      if m[i][1].include?(str[0]) && m[i][1].include?(str[1]) && m[i][1].include?(str[2])
        ret = m[i][0] if m[i][0] < ret
      end
    end
  end
  return ret
end

a,b,c = clac_min("A",n,m),clac_min("B",n,m),clac_min("C",n,m)
ab,bc,ca = [clac_min("AB",n,m),a+b].min,ab = [clac_min("BC",n,m),b+c].min,ab = [clac_min("CA",n,m),c+a].min
abc = [a+b+c,ab+c,a+bc,b+ca,clac_min("ABC",n,m)].min
puts abc == 10**6 ? -1 : abc


gets.to_i.times do
  q,d=gets.split.map(&:to_i)
  a=gets.split.map(&:to_i)
  a.each do |aa|
    if aa>=100
      puts "YES"
    else
      f=false
      while aa>0
        if aa/10==d||aa%10==d
          puts "YES"
          f=true
          break
        end
        aa-=d
      end
      puts "NO" if !f
    end
  end
end

gets.to_i.times do
  q,d=gets.split.map(&:to_i)
  a=gets.split.map(&:to_i)
  a.each do |aa|
    if d==1
      puts "YES"
    elsif d==2
      if aa>=20||aa.even?
        puts "YES"
      else
        puts "NO"
      end
    elsif d==3
      if aa>=21||aa%3==0||[13,16,19].include?(aa)
        puts "YES"
      else
        puts "NO"
      end
    elsif d==4
      if aa>=40||aa%4==0||aa>=14&&aa%4==2
        puts "YES"
      else
        puts "NO"
      end
    elsif d==5
      if aa>=50||aa%5==0
        puts "YES"
      else
        puts "NO"
      end
    elsif d==6
      if aa>=60||aa%6==0||aa>=22&&aa%2==0||aa==16
        puts "YES"
      else
        puts "NO"
      end
    elsif d==7
      if aa>=61||aa%7==0||aa>=17&&aa%7==3||aa>=27&&aa%7==6||aa>=37&&aa%7==2||[47,54,57].include?(aa)
        puts "YES"
      else
        puts "NO"
      end
    elsif d==8
      if aa>=80||aa%8==0||aa>=32&&aa%2==0||[18,26,28].include?(aa)
        puts "YES"
      else
        puts "NO"
      end
    elsif d==9
      if aa>=81||aa/10+aa%10>=9
        puts "YES"
      else
        puts "NO"
      end
    end
  end
end


gets
s=0
gets.split.map(&:to_i).each_cons(2){|i,j|s+=i-j if j<i}
puts s

gets
a = gets.split.map &:to_i
s = 0
a.each_cons(2){ |i,j| s += i-j if j<i }
puts s

class Array
  def is_decreasing_order
    a = self.first
    for i in 1 .. (self.length - 1) do
      if a > self[i]
        return false
      end
      a = self[i]
    end
    return true
  end
end

n = gets.chomp.to_i
a = gets.chomp.split(" ").map{|e| e.to_i}
c = 0

for i in 1 .. (n - 1) do
  first = a[i - 1]
  second = a[i]

  if first > second
    d = first - second
    """
    for j in i .. (n -1) do
      a[j] += d
    end
    """
    c += d
  end
end

puts c

#!/usr/bin/ruby

num_entries = gets.to_i
line = gets

numbers = line.split(' ')
numbers.map! {|e| e.to_i}
counter = 0

numbers.each_index { |i|
  break if(i == numbers.length-1)
  if(numbers[i] > numbers[i+1])
    diff = numbers[i] - numbers[i+1]
    counter += diff
  end
}

puts counter

n=gets.to_i
b=gets.chomp.split.map(&:to_i)
a,ans=b.shift,0
b.each do |i|
	ans+= (a>i) ? a-i : 0
	a=i
end
puts ans

gets
a = gets.split.map(&:to_i)
r = 0
(1..a.length-1).each do |i|
  k = a[i-1]-a[i]
  r+=k if k>=0
end
puts r

n = gets.to_i
a = gets.split.map { |i| i.to_i }
r = 0
0.upto(n - 2) { |i|
   r += a[i] - a[i + 1] if(a[i] > a[i + 1])
}
puts r

n = gets.to_i
a = gets.split.map { |i| i.to_i }
puts (0..n - 2).inject(0) { |r, i|
   r + [0, a[i] - a[i + 1]].max
}

n=gets.to_i
a=gets.split.map &:to_i
s=0
(n-1).times{|i|s+=[0,a[i]-a[i+1]].max}
p s

n = gets.to_i
a = gets.split.map { |i| i.to_i }
c =  0
1.upto(n - 1) { |i|
   if a[i - 1] > a[i]
      c = [c, a[i - 1] - a[i]].max
      a[i] = a[i - 1]
   end
}
puts c

n = gets.to_i
a = gets.split.map { |i| i.to_i }
c, r = 0, 0
1.upto(n - 1) { |i|
   if a[i - 1] > a[i]
      c = [c, a[i - 1] - a[i]].max
      a[i] += c
      r = c
   end
}
puts c

n = gets.to_i
a = gets.split.map { |i| i.to_i }
c, d, r = 0, 1 << 40, 0
1.upto(n - 1) { |i|
   if a[i] < a[i - 1]
      c = [c, a[i - 1]].max
      d = [d, a[i]].min
   else
      r += c - d
      c, d = 0, 1 << 40
   end
}
puts r + [0, c - d].max

n = gets.to_i
a = gets.split.map { |i| i.to_i }
c, r = 0, 0
1.upto(n - 1) { |i|
   if a[i - 1] > a[i]
      c = [c, a[i - 1] - a[i]].max
      a[i] += c
      r += c
   end
}
puts c

n = gets.to_i
a = gets.split.map { |i| i.to_i }
puts (0..n - 2).inject { |r, i|
   r + [0, a[i] - a[i + 1]].max
}

n = gets.to_i
a = gets.split.map { |i| i.to_i }
c, r = 0, 0
1.upto(n - 1) { |i|
   if a[i] < a[i - 1]
      c = [c, a[i - 1] - a[i]].max
      a[i] = a[i - 1]
   else
      r += c
      c = 0
   end
}
puts r + c

n = gets.to_i
a = gets.split.map { |i| i.to_i }
c, r= 0, 0
1.upto(n - 1) { |i|
   if a[i - 1] > a[i]
      c = a[i - 1] - a[i]
      a[i] += c
      r += c
   end
}
puts c


given = STDIN.readline.strip.split('')
digits = STDIN.readline.strip.split('').sort.reverse
pos = 0
digits.each do |digit|
  while pos < given.length and given[pos] >= digit
    pos += 1
  end
  break if pos == given.length
  given[pos] = digit
  pos += 1
end
puts given.join

a = gets.scan(/\d/).map(&:to_i)
c = [0] * 10
gets.scan(/\d/) do |_|
    c[_.to_i] += 1
end
a.size.times do |i|
    k = c.rindex{|_| _ > 0}
    if k && k > a[i]
        a[i] = k
        c[k] -= 1
    end
end
puts a * ''

a = readline.strip.each_char.to_a
s = readline.strip.each_char.to_a.sort

ans = a.each.map { |ai| (ai.to_i >= s.last.to_i) ? ai : s.pop }
puts ans.join

# encoding: utf-8

def string_to_digit_array(s)
  ret = []
  s.each_char do |c|
    ret << c
  end
  ret
end

a = string_to_digit_array(readline.strip)
s = string_to_digit_array(readline.strip).sort

ans = []
a.each do |digit|
  if digit.to_i >= s.last.to_i
    ans << digit
  else
    ans << s.pop
  end
end

puts ans.join

# ProblemB.rb

a = gets.chomp.split(//)
s = gets.chomp

s = s.split(//).sort.reverse

j = 0
a.each_with_index do |c, i|
  if j == s.size
    break
  end
  if c < s[j]
    a[i] = s[j]
    j += 1
  end
end

puts a.join

n = gets.chomp.split("")
dlina = n.length
k = gets.chomp.split("")
dlina2 = k.length
k = k.sort
p = dlina2-1

for i in (0..dlina-1)
    n[i] = n[i].to_i
end

for i in (0..p)
    k[i] = k[i].to_i
end

for i in (0..dlina-1)
    if k[p] > n[i] then
        n[i] = k[p]
        p-=1
    end

    if p < 0 then
        break
    end
end

for i in 0..dlina-1 do
    print n[i]
end

# encoding: utf-8

a = readline
s = readline
puts (a+s)

# ProblemB.rb

a = gets.chomp.split(//)
s = gets.chomp

s = s.split(//).sort.reverse

j = 0
a.each_with_index do |c, i|
  if c < s[j]
    a[i] = s[j]
    j += 1
  end
end

p a.join


t = gets.to_i

1.upto(t) do
  n = gets.to_i

  2.upto(32) do |k|
    next unless n.modulo((2 ** k) - 1).zero?

    puts n.div((2 ** k) - 1)
    break
  end
end

gets.to_i.times do
  n = gets.to_i
  k = 2
  loop do
    s = 2**k - 1
    if n%s == 0
      puts n/s
      break
    end
    k += 1
  end
end

t=gets.to_i
(1).upto(t) do
  n=gets.to_i
  k=4
  a=3
  while n.modulo(a)!=0
    a+=k
    k<<=1
  end
  puts n/a
end

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

t=gets.to_i

def Solve()
    n=gets.to_i
    k=4
    a=3
    while n.modulo(a)!=0
        a+=k
        k<<=1
    end

    return n/a
end
(1).upto(t) do

    puts Solve()
end

gets.to_i.times do
	n = gets.to_i
	i = (2..64).find { |i| n % (2**i - 1) == 0 }
	puts n / (2**i - 1)
end

class Result
  def print_result
    n = gets.to_i
    2.upto(29) do |i|
      if n % (2**i - 1) == 0
        puts n / (2**i - 1)
        break
      end
    end
  end
end

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

1.upto(gets.chomp.to_i) do
  n = gets.chomp.to_i
  k = 2
  m = 4
  while n % (m-1) != 0
    m *= 2;
    k += 1;
  end
  puts n / (m-1)
end

number_of_testcases = gets.to_s.to_i

number_of_testcases.times do |i|

  n = gets.to_s.to_i
  s = 2
  loop do

    s *= 2
    t = s - 1
    if n % t == 0
      puts n / t
      break
    end
  end
end


p,n = gets.split.map{|i| i.to_i}
h=[]
n.times{|i| h<<gets.to_i % p;if h.count(h[-1])==2
	puts i+1
	exit
end}
puts -1

p, n = gets.split.map {|i| i.to_i}
a = Array.new(p)
for i in 0...n
  x = gets.to_i
  if a[x % p] != nil
    puts i + 1
    exit
  end
  a[x % p] = true
end
puts -1

pn = gets
params = pn.split(" ")
p = params[0].to_i
n = params[1].to_i
array = []
found = false
(1..n).each do |item|
  temp = gets.to_i % p
  if(array[temp] == nil) then
    array[temp] = true
  elsif(array[temp] == true && found == false) then
    found = item
  end
end
if(found == false) then
  puts "-1"
else
  puts found
end

#!/usr/bin/ruby
p,n=gets.split.map(&:to_i)
h={}
n.times{|i|
	x=gets.to_i%p
	if h[x]
		p i+1
		exit
	end
	h[x]=1
}
puts -1

inp = gets
arr = inp.split(" ")
p = arr[0].to_i
n = arr[1].to_i

hash = {}
res = -1
for i in 1..n
    inp = gets
    num = inp.to_i
    if hash[num%p] == 1
        res = i
        break
    end
    hash[num%p] = 1
end

puts res

a=gets.chomp.split(" ")
hash={"0"=>0}
k=0
1.upto(a[0].to_i-1) do |i|
hash["#{i}"]=0
end

1.upto(a[1].to_i) do |i|
temp=(gets.chomp.to_i)%a[0].to_i
hash["#{temp}"]+=1
if hash["#{temp}"]>1 && k==0
k=i
end
end

if k==0
puts "-1"
else
puts "#{k}"
end

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

conflicts = false
hashtable = Array.new()
n.times do |i|
  x = gets.to_i
  next if conflicts
  if hashtable[x % p].nil?
    hashtable[x % p] = x
  else
    conflicts = true
    puts i + 1
  end
end

puts -1 unless conflicts

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

hash = Array.new p
collision = -1

n.times { |i|
  x = gets.to_i
  next if collision != -1

  mod = x % p
  if hash[mod].nil?
    hash[mod] = x
  else
    collision = i+1
  end
}

puts collision

p, n = gets.split.map { |x| x.to_i }
vis = Array.new(p, false)

ans = -1
n.times do |i|
	x = gets.to_i % p
	if !vis[x] ;
		vis[x] = true
	elsif ans == -1 ;
		ans = i+1;
	end
end
puts ans

pn=gets.chomp.to_s.split(" ").map {|x| x.to_i}
def solve(pn)
  hash=Hash.new
  for i in 0...pn[1]
    h=gets.chomp.to_i
    if hash[h%pn[0]].nil?
      hash[h%pn[0]]=1
    else
      puts i+1
      return
    end
  end
  puts "-1"
end
solve(pn)

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

y = Array.new(n)

i = 0

while i < n

    z = gets.to_i % p

    if y[z] == nil

        y[z] = z

    else

        puts i + 1

        break

    end

    i += 1

end

puts -1 if i == n

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

myHash=Hash.new()

for i in 0...n do
    myHash[i]=nil
end

found=-1
for i in 1..n do
    x=gets.split[0].to_i
    h=x%p
    if(myHash[h]==nil)
        myHash[h]=x
    else
        found=i
        break
    end
end

p found

p, n = gets.split(" ").collect { |e|  e.to_i}

arr = Array.new(p) { |i|  0}
i = 1
while i <= n
    num = gets.to_i
    mod = num % p
    if arr[mod] == 1
        break
    end
    arr[mod] += 1
    i += 1
end
puts i == n + 1 ? "-1" : i

line = gets
p, n  = line.split(' ').map(&:to_i)
hash = []
ins = -1
(1..n).each do |order|
  number = gets.to_i
  if ins == -1
    index = number % p
    if hash[index] != nil
      ins = order
    else
      hash[index] = 1
    end
  end
end
puts ins

p, n = gets.chomp.split.map(&:to_i)
h = Array.new(p, false)
(1..n).each { |i|
  if h[r = gets.to_i % p]
    puts i
    exit
  else
    h[r] = true
  end
}
puts -1

h={}
p,n=gets.split.map &:to_i
n.times do |i|
	x=gets.to_i%p
	if h.has_key? x
		p i+1
		exit
	end
	h[x]=1
end
p (-1)

pn = gets
params = pn.split(" ")
p = params[0].to_i
n = params[1].to_i
array = []
found = false
(1..n).each do |item|
  temp = gets.to_i % p
  if(array[temp] == nil) then
    array[temp] = true
  elsif(array[temp] == true && found == false) then
    found = item
  end
end
puts found

#!/usr/bin/ruby
p,n=gets.split.map(&:to_i)
h={}
n.times{|i|
	x=gets.to_i%p
	if h[x]
		p i+1
		exit
	end
	h[x]=1
}
p -1

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

conflicts = false
hashtable = Array.new()
n.times do |i|
  x = gets.to_i
  if hashtable[x % p].nil?
    hashtable[x % p] = x
  else
    if hashtable[x % p] != x
      puts i + 1
      conflicts = true
    end
  end
end

puts -1 unless conflicts


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

h = Hash.new(0)

res = ""
a.each do |e|
  h[e] += 1
  res = "No" if h[e] > 2
end

if res == "No"
  puts res
else
  a.sort!
  b = a.select.with_index { |e, idx| idx.even? }
  c = a.select.with_index { |e, idx| idx.odd? }
  puts "Yes"
  puts b.count
  puts b.join(" ")
  puts c.count
  puts c.reverse.join(" ")
end

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

inc = [a.shift]
dec = []
a = a.reduce([]) do |acc, x|
      x > inc.last ? inc.push(x) : acc.push(x)
      acc
    end

a.reverse!

dec.push(a.shift) if a.size > 0
a = a.reduce([]) do |acc, x|
      x < dec.last ? dec.push(x) : acc.push(x)
      acc
    end

if a.size == 0
  puts "YES"
  puts inc.size
  inc.each_with_index { |x, i| print " " if i > 0; print x }
  print "\n"
  puts dec.size
  dec.each_with_index { |x, i| print " " if i > 0; print x }
  print "\n"
else
  puts "NO"
end

n = gets.to_i
a = gets.split.map(&:to_i).sort
x = []
y = []
n.times do |i|
    if i == 0 || a[i] != a[i-1]
        x << a[i]
    else
        y << a[i]
    end
end
if x.uniq.size == x.size && y.uniq.size == y.size
    puts "YES",x.size,x.join(" "),y.size,y.reverse.join(" ")
else
    puts "NO"
end

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

h = Hash.new(0)

res = ""
a.each do |e|
  h[e] += 1
  res = "No" if h[e] > 2
end

if res == "No"
  puts res
else
  a.sort!
  b = a.select.with_index { |e, idx| idx.even? }
  c = a.select.with_index { |e, idx| idx.odd? }
  puts "Yes"
  puts b.reduce(&:+)
  puts b.join(" ")
  puts c.reduce(&:+)
  puts c.reverse.join(" ")
end


n, m, k = gets.split.map(&:to_i)
v = m.times.map{gets.split.map(&:to_i)}
s = Array.new(n+1, false)
gets.split.map(&:to_i).each {|x| s[x] = true } if k>0
ans = nil
v.each { |a, b, l| ans = l if (s[a]^s[b])&&(ans.nil? || l<ans) }
puts ans.nil? ? -1 : ans

n, m, k = gets.split.map(&:to_i)
data = m.times.map{gets.split.map(&:to_i)}
storage = Array.new(n + 1, false)
arr = k > 0 ? gets.split.map(&:to_i) : []
arr.each{|x| storage[x] = true}
ans = nil
data.each do |a, b, l|
  ans = l if (storage[a] ^ storage[b]) && (ans.nil? || l < ans)
end
puts ans.nil? ? -1 : ans

# 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){|m,e| m[e]+=1;m} end
############################################################

N, M, K = gis
UVL = ngis M
ST = K > 0 ? gis : []

h = [-1] * (N + 1)
f = [true] * (N + 1)

ST.each do |a|
    f[a] = false
end

mn = nil

UVL.each do |u, v, l|
    next if !(f[u] ^ f[v])
    mn = l if mn.nil? || l < mn
end

puts mn.nil? ? -1 : mn

class Array
  def r
    self.map { |q| q.to_i }
  end
end

def read_line
  STDIN.gets.split(" ")
end

def read_to_array
  read_line.to_a
end

def read_numbers_to_array
  read_to_array.r
end

first = Array.new(100009)
second = Array.new(100009)
len = Array.new(100009)

cities, roads, k = read_numbers_to_array
for i in 0..roads - 1 do
  first[i], second[i], len[i] = read_numbers_to_array
end

shortest = -1
if k > 0
b = read_numbers_to_array
bakers = Array.new(cities + 9, false)

b.each do |i|
  bakers[i] = true
end

for i in 0..roads - 1 do
  if bakers[first[i]] != bakers[second[i]] && (shortest == - 1 || len[i] < shortest)
    shortest = len[i]
  end
end
end

puts shortest

 class Node
	attr_accessor :dest, :val

	def initialize dest, val
		@dest = dest
		@val = val
	end

	def to_s
		print "Dest : #{dest}, Val : #{val}"
	end
end

n, m, k = gets.chomp.split(' ').map(&:to_i)
found = false
nearest = Array.new(n + 1) { |i| i = {} }

m.times do
	u, v, l = gets.chomp.split(' ').map(&:to_i)

	if u != v
		if nearest[u].has_key? v
			if nearest[u][v] > l
				nearest[u][v] = l
			end
		else
			nearest[u][v] = l
		end

		if nearest[v].has_key? u
			if nearest[v][u] > l
				nearest[v][u] = l
			end
		else
			nearest[v][u] = l
		end
	end
end

storage = Array.new(n + 1) { |i| i = false }

if k > 0
	inputStorage = gets.chomp.split(' ').map(&:to_i)

	inputStorage.each do |i|
		storage[i] = true
	end
end

ans = 1000000000000

1.upto(n) do |city|
	next if !storage[city]

	nearest[city].each do |dest, val|
		if !storage[dest]
			ans = val if ans > val
			found = true
		end
	end
end

if !found
	puts -1
else
	puts ans
end

n, m, k = gets.split.map(&:to_i)
nn = 1000000000
h = Hash.new(nn)
m.times do
  a, b, l = gets.split.map(&:to_i)
  h[a] = [h[a], l].min
  h[b] = [h[b], l].min
end
arr = k > 0 ? gets.split.map(&:to_i) : []
mi = h[arr.min_by{|x| h[x]}]
puts mi.nil? || mi == nn ? -1 : mi

class Node
	attr_accessor :dest, :val

	def initialize dest, val
		@dest = dest
		@val = val
	end
end

n, m, k = gets.chomp.split(' ').map(&:to_i)
found = false
nearest = Array.new(n + 1) { |i| i = Node.new(-1, 100000000) }

m.times do
	u, v, l = gets.chomp.split(' ').map(&:to_i)

	if u != v
	if nearest[u].val > l
		nearest[u].dest = v
		nearest[u].val = l
	end

	if nearest[v].val > l
		nearest[v].dest = u
		nearest[v].val = l
	end
	end
end

storage = Array.new(k + 1) { |i| i = false }

if k > 0
	inputStorage = gets.chomp.split(' ').map(&:to_i)

	inputStorage.each do |i|
		storage[i] = true
	end
end

ans = 100000000

1.upto(n) do |city|
	#puts "city = #{city}"

	next if !storage[city]
	next if nearest[city].dest < 0

	if !storage[nearest[city].dest]
		ans = nearest[city].val if ans > nearest[city].val
		found = true
	end

	#puts "ans = #{ans}"
end

if !found
	puts -1
else
	puts ans
end

class Node
	attr_accessor :dest, :val

	def initialize dest, val
		@dest = dest
		@val = val
	end
end

n, m, k = gets.chomp.split(' ').map(&:to_i)
found = false
nearest = Array.new(n + 1) { |i| i = Node.new(-1, 100000000) }

m.times do
	u, v, l = gets.chomp.split(' ').map(&:to_i)

	if nearest[u].val > l
		nearest[u].dest = v
		nearest[u].val = l
	end

	if nearest[v].val > l
		nearest[v].dest = u
		nearest[v].val = l
	end
end

storage = Array.new(k + 1) { |i| i = false }

if k > 0
	inputStorage = gets.chomp.split(' ').map(&:to_i)

	inputStorage.each do |i|
		storage[i] = true
	end
end

ans = 100000000

1.upto(n) do |city|
	#puts "city = #{city}"

	next if !storage[city]
	next if nearest[city].dest < 0

	if !storage[nearest[city].dest]
		ans = nearest[city].val if ans > nearest[city].val
		found = true
	end

	#puts "ans = #{ans}"
end

if !found
	puts -1
else
	puts ans
end

class Node
	attr_accessor :dest, :val

	def initialize dest, val
		@dest = dest
		@val = val
	end
end

n, m, k = gets.chomp.split(' ').map(&:to_i)
found = false
nearest = Array.new(n + 1) { |i| i = Node.new(-1, 100000000) }

m.times do
	u, v, l = gets.chomp.split(' ').map(&:to_i)

	if nearest[u].val > l
		nearest[u].dest = v
		nearest[u].val = l
	end

	if nearest[v].val > l
		nearest[v].dest = u
		nearest[v].val = l
	end
end

storage = Array.new(k + 1) { |i| i = false }

if k > 0
	inputStorage = gets.chomp.split(' ').map(&:to_i)

	inputStorage.each do |i|
		storage[i] = true
	end
end

ans = 0

1.upto(n) do |city|
	next if !storage[city]

	if !storage[nearest[city].dest]
		ans = nearest[city].val
		found = true
	end
end

if !found
	puts -1
else
	puts ans
end

 class Node
	attr_accessor :dest, :val

	def initialize dest, val
		@dest = dest
		@val = val
	end

	def to_s
		print "Dest : #{dest}, Val : #{val}"
	end
end

n, m, k = gets.chomp.split(' ').map(&:to_i)
found = false
nearest = Array.new(n + 1) { |i| i = {} }

m.times do
	u, v, l = gets.chomp.split(' ').map(&:to_i)

	if u != v
		if nearest[u].has_key? v
			if nearest[u][v] > l
				nearest[u][v] = l
			end
		else
			nearest[u][v] = l
		end

		if nearest[v].has_key? u
			if nearest[v][u] > l
				nearest[v][u] = l
			end
		else
			nearest[v][u] = l
		end
	end
end

storage = Array.new(n + 1) { |i| i = false }

if k > 0
	inputStorage = gets.chomp.split(' ').map(&:to_i)

	inputStorage.each do |i|
		storage[i] = true
	end
end

ans = 100000000

1.upto(n) do |city|
	next if !storage[city]

	nearest[city].each do |dest, val|
		if !storage[dest]
			ans = val if ans > val
			found = true
		end
	end
end

if !found
	puts -1
else
	puts ans
end


t=gets.to_i
for i in 1..t
    a,b,c,d = gets.split(" ").map(&:to_i)
    sum=0
    if(a<=b)
        print(b,"\n")
    elsif(d>=c)
        print(-1,"\n")
    else
        sum += b
        a -= b
        tt = (a.to_f/(c-d)).ceil()
        sum += tt*c
        print(sum,"\n")
    end
end

t=gets.to_i
for i in 1..t
    a,b,c,d = gets.split(" ").map(&:to_i)
    sum=0
    if(a<=b)
        print(b,"\n")
    elsif(d>=c)
        print(-1,"\n")
    else
        sum += b
        a -= b
        sum += ((a+c-d-1)/(c-d)*c).floor()
        print(sum,"\n")
    end
end


n, m = gets.chomp.split(' ').map{|x| x.to_i}
str = gets.chomp
if m == 2## time complexity = n
  a = "AB" * (n/2.to_i)
  b = "BA" * (n/2.to_i)
  if n%2 == 1
    a += 'A'
    b += 'B'
  end
  p1 = 0
  p2 = 0
  (0..str.length-1).each do |i|
    if a[i] != str[i]
      p1 += 1
    end
    if b[i] != str[i]
      p2 += 1
    end
  end
  if p1 <= p2
    puts p1, a
  else
    puts p2, b
  end
else #time complexity = 26*n
  pa = 0
  res = str.split('')

  for i in (1 .. res.length-1)
    if res[i] == res[i-1]
      s1 = res[i-1]
      s2 = res[i+1]

      if s1 != 'A' and s2 != 'A'
        res[i] = 'A'
      elsif s1 != 'B' and s2 != 'B'
        res[i] = 'B'
      else
        res[i] = 'C'
      end
      pa += 1
    end
  end
  puts pa, res.join('')
end

n, m = gets.chomp.split(' ').map{|x| x.to_i}
str = gets.chomp
res = str.chars
if m == 2## time complexity = n
  a = "AB" * (n/2.to_i)
  b = "BA" * (n/2.to_i)
  if n%2 == 1
    a += 'A'
    b += 'B'
  end
  p1 = 0
  p2 = 0
  (0..res.length-1).each do |i|
    if a[i] != res[i]
      p1 += 1
    end
    if b[i] != res[i]
      p2 += 1
    end
  end
  if p1 <= p2
    puts p1, a
  else
    puts p2, b
  end
else #time complexity = 26*n
  pa = 0

  for i in (1 .. res.length-1)
    if res[i] == res[i-1]
      s1 = res[i-1]
      s2 = res[i+1]

      if s1 != 'A' and s2 != 'A'
        res[i] = 'A'
      elsif s1 != 'B' and s2 != 'B'
        res[i] = 'B'
      else
        res[i] = 'C'
      end
      pa += 1
    end
  end
  puts pa, res.join('')
end


d = gets.to_i
gets
a = gets.split.map(&:to_i)
c = 0
a.pop

a.each do |i|
  c += d - i
end
p c

n = gets.chomp.to_i
m = gets.chomp.to_i
a = gets.chomp.split(/ /).map!{|x| x.to_i}
ret = 0
for i in 0..m-2
    ret += (n - a[i])
end
puts ret

d = gets.to_i
gets
p gets.split.map{|_| d - _.to_i}[0..-2].inject(&:+) || 0

d = gets.chomp.to_i
n = gets.chomp.to_i
days = gets.chomp.split(" ").map{|e| e.to_i}

ans = 0

for i in (0..(n - 2)) do
  ans += d - days[i]
end

puts ans

d = gets.to_i
n = gets.to_i
puts gets.split.map(&:to_i)[0..-2].inject(0) {|acc, x|  acc + d - x }

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

(n-1).times do |i|
  ans += d - a[i]
end
puts ans

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

s = 0
0.upto(a.size-2) do |i|
  s += (d-a[i])
end
puts s

d = gets.to_i
n = gets.to_i
arr = gets.split(" ").map(&:to_i)
ans = arr[0..-2].map{|a| d - a}
puts ans.inject(0,:+)
#

def main
    d = STDIN.gets.to_i
    n = STDIN.gets.to_i
    a = STDIN.gets.split
    s = 0
    (n - 1).times do |i|
        s += d - a[i].to_i
    end
    puts s
end

main

# ProblemB

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

ret = 0

i = 0
while i < (ary.size - 1)
  ret += (d - ary[i])
  i += 1
end

puts ret

n = gets.chomp.to_i
mes = gets.chomp.to_i
dni = gets.chomp.split(" ")

length = dni.length

for i in(0..length-1)
    dni[i] = dni[i].to_i
end

k = 0
p = 0

for i in(0..length-2)

    k = n - dni[i]
    p+=k
end

puts p

d = gets.to_i
n = gets.to_i
puts gets.split.map(&:to_i)[1..-1].inject(0) {|acc, x|  acc + d - x }

d = gets.to_i
n = gets.to_i
puts gets.split.map(&:to_i)[0..-1].inject(0) {|acc, x|  acc + d - x }


n = gets.chomp.to_i
tab = gets.chomp
s = 1
t = tab[0]
e = 0
for i in 1...tab.length
	if t != tab[i]
		s += 1
		t = tab[i]
	end
	if (tab[i] == tab[i-1] and tab[i]== '0') or (tab[i] == tab[i-1] and tab[i] == '1')
		e += 1
		e = [e, 2].min
	end
end
puts s+e

gets
s = gets.chomp
n = s.chars.inject([0, nil]) do |(things, last), ch|
  if ch == last
    [things, ch]
  else
    [things + 1, ch]
  end
end.first

if s.scan(/(.)\1/).size > 1 || /(.)\1\1/ =~ s
  puts n + 2
elsif /(.)\1/ =~ s
  puts n + 1
else
  puts n
end

n = gets.to_i
s = gets
c = 0
for i in 0...n-1 do
   c += 1 if s[i] == s[i+1]
end
c-=2
ans = c > 0 ? c : 0
puts n-ans

n = gets.chomp.to_i
str = gets.chomp

cnt2 = 0
cnt3 = 0
ls = [1]
for i in 1...n
  if str[i - 1] == str[i]
    ls[-1] += 1
    cnt2 += 1 if ls[-1] == 2
    cnt3 += 1 if ls[-1] == 3
  else
    ls << 1
  end
end

ans = ls.size
if 2 <= cnt2 || 1 <= cnt3
  ans += 2
elsif 1 <= cnt2
  ans += 1
end

puts ans

#!/usr/bin/ruby

len = gets.to_i
score = gets.strip()
char = '-'
alter = 0
for ch in 0..len - 1
    if char == '-' or char != score[ch]
        char = score[ch]
        alter += 1
    end
end
puts [alter + 2, len].min

=begin
min(len, maxi + 2)
=end

len = gets.to_i
s = gets
cnt = 1
1.upto(len - 1){|i|
  cnt += 1 if s[i] != s[i - 1]
}
puts [len.to_i, cnt + 2].min

n = $stdin.readline.to_i
ans = $stdin.readline.strip

subseqs = []
from = 0
ans.each_char.each_cons(2).with_index{|(fst,snd),ind|
  if fst == snd
    subseqs << [from, ind]
    from = ind + 1
  end
}
subseqs << [from, ans.size - 1]

# if subseqs.size >= 3
#   puts subseqs.each_cons(3).map{|a,b,c|
#     c.last - a.first + 1
#   }.max
# else
#   puts ans.size
# end

puts subseqs.map{|subseq| subseq[1] - subseq[0] + 1 - 1 }.inject(&:+) + 1 + (subseqs.length > 2 ? 2 : (subseqs.length > 1 ? 1 : 0))

#!/usr/bin/ruby

len = gets.to_i
score = gets.strip()
new = ""
alter = ""
for ch in 0..len - 1
    if new == "" or score[ch] != new[-1] or (new.length >= 2 and new[-2] != new[-1])
        new += score[ch]
    end
    if alter == "" or alter[-1] != score[ch]
        alter += score[ch]
    end
end
puts [alter.length + 2, new.length].min

=begin
min(len, maxi + 2)
=end

len = gets.to_i
s = gets
cnt = 0
1.upto(len - 1){|i|
  cnt += 1 if s[i] != s[i - 1]
}
puts [len.to_i, cnt + 2].min

gets
s = gets.chomp
n = s.chars.inject([0, nil]) do |(things, last), ch|
  if ch == last
    [things, ch]
  else
    [things + 1, ch]
  end
end.first

if s.scan(/(.)\1/).size > 1
  puts n + 2
elsif s[/^(.)\1/] || s[/(.)\1$/]
  puts n + 1
else
  puts n
end

gets
s = gets.chomp
n = s.chars.inject([0, nil]) do |(things, last), ch|
  if ch == last
    [things, ch]
  else
    [things + 1, ch]
  end
end.first

if s.scan(/(.)\1/).size > 1
  puts n + 2
elsif /(.)\1/ =~ s
  puts n + 1
else
  puts n
end

gets
s = gets.chomp
n = s.chars.inject([0, nil]) do |(things, last), ch|
  if ch == last
    [things, ch]
  else
    [things + 1, ch]
  end
end.first

if s.scan(/(.)\1/).size > 1
  puts n + 2
else
  puts n
end

n = gets.to_i
s = gets
c = 0
for i in 0...n-1 do
   c += 1 if s[i] == s[i+1]
end
c-=2
ans = c > 0 ? c : 0
puts ans


n = gets.to_i
a = gets.split(' ').map(&:to_i)
res = Array.new
n.times do |i|
  res[a[i]-1] = i+1
end
puts res.to_s.gsub(/\[/, '').gsub(/\]/, '').gsub(/,/, '')

n = gets.to_i
ps = gets.split.map(&:to_i)
ans = Array.new(n)
ps.each_with_index do |p, i|
  ans[p-1] = i + 1
end
puts ans.join(" ")

friends = gets.chomp.to_i
gifts = gets.chomp.split(/ /).map(&:to_i)
received_gifts = {}
(1..friends).each do |friend|
    received_gifts.store gifts[friend - 1], friend
end
ans = []
(1..friends).each do |friend|
    ans << received_gifts[friend]
end
puts ans.join(" ")

a=gets.chomp.to_i
b=gets.chomp.split(" ")
ans=""
1.upto(a) do |i|
    c=b.index("#{i}")+1
    ans=ans+"#{c} "
end
puts "#{ans}"

n=gets.to_i
b=(1..n).map{0}
x=gets.split.map{|e| e.to_i}
n.times{|i|
	j=i+1
	b[x[i]-1]=j
}
puts b*" "

n = gets.to_i
arr_n = gets.split.map(&:to_i)
res = []
arr_n.each.with_index(1) { |e,i| res[e]=i }
puts res[1..-1].join(' ')

n = gets.to_i
a = gets.split.map(&:to_i)
b = []
n.times do |i|
  b << a.index(i+1)+1
end
puts b * " "

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

c = {}
arr.each_with_index do |a,i|
  c[i+1] = a
end

cc = {}
c.each do |k,v|
  cc[v] = k
end

aa = []
n.times do |i|
  aa << cc[i+1]
end

puts aa.join(' ')

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

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

n=gets.to_i
data=gets.split.map(&:to_i)
ans=Array.new(n, 0)

n.times{|i|
	ans[data[i]-1]=i+1
}

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

number = gets.to_i
answer = Array.new(number + 1)

arr = gets.chomp.split(' ').map(&:to_i)
i = 1
arr.each do |x|
  answer[x] = i
  i += 1
end

answer.each { |x| print "#{x} " }

len=gets.to_i
b=[]
a=gets.split.map{|i|i.to_i}
len.times{|i| b[a[i]]=i+1}
b.shift
print b[0]
1.upto(len) {|i| print ' ',b[i]}
puts

len=gets.to_i
b=[]
a=gets.split.map{|i|i.to_i}
len.times{|i| b[a[i]]=i+1}
b.shift
len.times {|i| print b[i],' '}

n = gets.to_i
a = gets.split(' ').map(&:to_i)
res = Array.new
n.times do |i|
  res[a[i]-1] = i+1
end
res.each { |v| print "#{v} " }

n = gets.to_i
q = gets.chomp.split.map { |x| x.to_i }
h = Hash.new

for i in 0..n-1
  h[i] = q[i]
end

h = h.invert

for i in 1..n
  print h[i]+1, ' '
end

n = gets.to_i
ar = gets.chomp.split(' ',n).map{|_|_.to_i}
puts ar.map.with_index(1).sort_by(&:first).map(&:last).map(&:to_s).join(' ')

class Main
  #def get_problem_link
  #  problem_link = "https://codeforces.com/contest/136/problem/A"
  #  return problem_link
  #end

  #def get_submission_link
  #  submission_link = ''
  #  return submisson_link
  #end

  #begin
  #of codeforces interface
  def run_interface
    n = gets.to_i
    k = gets.split(' ').map(&:to_i)
    puts solution(n, k)
  end
  #end
  #of codeforces interface

  def solution(n, presents_array)
      res_array = []
      for i in 1..n do
        res_array.push(presents_array.index(i) + 1)
      end
      return res_array.join(' ')
  end

end

#then upload to codeforces uncomment this line:
Main.new.run_interface

n = STDIN.gets.chomp.to_i
p = STDIN.gets.chomp.split(' ').map{|i| i.to_i}

res = p.inject(Hash.new(-1)) {|h, i| h[i - 1] = p.index(i); h}
n.times do |i|
    if res[i] == -1
        print i + 1, " "
        next
    end
    print res[i] + 1, " "
end

n = gets.to_i

h = {}
st = gets.split.map{|i| i.to_i}

n.times do |i|
    h[i+1] = st[i]
end

h = h.invert

st = ""
i = 1
while i <= n do
    st += h[i].to_s + " "
    i += 1
end

puts st

gets;a=gets.split.map &:to_i
puts (1..a.size).map{|i|a.index(i)+1}*" "

n=gets.to_i
a=Array.new(n,0)
i=gets.split.map(&:to_i)
n.times{|j|
        a[i[j]-1]=j
}
a.each{|j|
        print "#{j+1} "
}
puts

n = gets.to_i
p = gets.split.map(&:to_i)
a = Array(p.size)
p.each_index {|i| a[p[i] - 1] = i + 1 }
puts a

n = gets.to_i
a = []
a = gets.split.map &:to_i
b = Array.new(n)

(0...n).each do |i|
    b[a[i] - 1] = i + 1;
end

print  b.join(' ')

l = STDIN.read.split("\n")
a = []
l[1].split(' ').each_with_index{|x,i| a << [x.to_i, i+1] }
b = a.sort_by{|x| x[0] }.map{|x| x[1] }
puts b.join(' ')

number_of_friends = gets.strip.to_i
friends = gets.strip.split(' ')

results = []
1.upto(number_of_friends) do |current_friend|
  results << friends.index { |given_friend| current_friend.to_s == given_friend } + 1
end

puts results.join(' ')

n = $stdin.readline.to_i
a = $stdin.readline.split(' ').map { |x| x.to_i }

b = Array.new(n)
for i in 1..n
  b[i-1] = i
end

for i in 1..n
  b[a[i-1]-1] = i unless a[i-1].nil?
end

puts b.join(' ')

def r
  myArray = gets.chomp.split().map { |e| e.to_i  }
end
n = r
array = r
array_2 = Array.new(n)
array.each_with_index{|val, index|
  array_2[val-1] = index + 1
}
array_2.each {|e| print "#{e} " }

friends = gets.chomp.to_i
gifts = gets.chomp
gifts = gifts.split

arr_gifts = []
for i in (0..gifts.size-1)
	arr_gifts[gifts[i].to_i-1] = i+1
end

for i in (0..gifts.size-1)
	print arr_gifts[i].to_i
	print " "
end

n=gets.to_i
s=gets.split(' ').map{|x|x.to_i-1}
n.times{|i|print s.index(i)+1,' '}

n = gets.chomp;
n = n.to_i;

p = gets.chomp;
p = p.split(' ');

for i in 0..p.length-1
	p[i] = p[i].to_i;
#puts p[i];
end

#puts("done");

from = Array.new(n + 1);

for i in 0..p.length-1
	from[p[i]] = i + 1
end

for i in 1..from.length-1
	if i > 1
		printf(" ");
	end
	printf("%d", from[i]);
end
printf("\n");

n=gets.chomp.strip.to_i
a=gets.chomp.split(' ').map(&:to_i)
p=[]
a.each_index do |i|
	p[a[i]-1]=i
end
for x in p do
	print (x+1).to_s+' '
end
puts

#!/usr/bin/ruby

friends = gets.to_i
gifts = gets.chomp.split(" ").map(&:to_i)
(1..friends+1).each do |num|
   (0..friends).each do |i|
    if(gifts[i] == num)
      puts i + 1
    end
  end
end

a = Array.new(gets.to_i)
gets.strip.split.map(&:to_i).to_a.each_with_index { |e, i| a[e-1] = i+1 }
puts a*' '

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

b = []

a.each_with_index do |t,i|
 b[t-1] = i+1
end

puts b.join(' ')

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

for i in 0..n-1
 c[a[i]] = i+1
end
puts c.compact

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

f_inv = Array.new(n)
f.each_with_index {|e,i| f_inv[e-1]=i+1 }

puts f_inv.join(" ")

#!/usr/bin/ruby
def readNextValues
  return gets.chomp.split.collect {|x| x.to_i }
end

n = readNextValues
arr = readNextValues
result = []
for i in 1..arr.size
  result << arr.find_index(i) + 1
end
puts result.join(' ')

gets
arr = gets.split.map &:to_i
sol = Array.new
arr.each_with_index { |x, index| sol[arr[index]] = index + 1 }
sol.shift
puts sol

i = gets.to_i
array = gets.chomp.split.map(&:to_i)
a = Array.new

array.map.with_index { |x, i| a[x-1] = i+1 }
puts a.join(" ")

n=gets.to_i
s=gets
n.times{|i|p s.split(' ').index((i+1).to_s)+1}

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

b = Array.new(a.size)

a.each_index { |i| b[a[i]] = i + 1}

puts b

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

b=[]

for i in 0..a.size-1
b[a[i]-1]=i+1
end

for i in b
print i
print ' '
end
print "\n"

n = gets.to_i
input = gets.split(' ').map(&:to_i).unshift(0)
result = Array.new(n+1, 0)

0.upto(n) do |i|
  result[input[i]] = i
end

result.shift

puts result.join(' ')

n = gets.to_i
m = gets.chomp.split(" ")

p = Array.new(n)

for i in(0..n-1)
    p[m[i].to_i - 1] = i + 1
end

p.each{ |x|
    print(x, " ")
}

n = gets.to_i
b = gets.split(' ').map{|s| s.to_i}
a = Array.new(n)
i = 0
while (i < n)
    a[b[i]-1] = i
    i+=1
end
s = ''
a.each{|q| s += (q+1).to_s + ' '}
puts s

n = gets.to_i
a = []
gets.split.each_with_index{ |x,i|
 a[x.to_i] = i+1
}
puts a.join(" ")

gets
t = 1
rec = Array.new(t)
gets.scan(/\d+/).each do |i|
	rec[i.to_i] = t
	t = t+1
end
rec.each do |out|
	printf(out.to_s + " ")
end

#!/usr/bin/env ruby

gets
m = {}
gets.split.map(&:to_i).each_with_index { |p,i|
   m[p] = i
}
m.map.sort_by{ |i,p|i }.map { |i,p|
   print "#{p+1} "
}
puts

t=[];$<.take(2)[1].split.each_with_index{|k,i| t[k.to_i]=i+1};puts t[1..-1].join " "

n = gets.to_i
res = Array.new(n, 0)
n = 1
arr = gets.split.map(&:to_i).each do |item|
  res[item - 1] = n
  n += 1
end
puts res.join(" ")

gets
gets.split(" ").each_with_index.map { |s, i| [s.to_i, i] }.sort_by! {|s, i| s}.each { |s, i| print "#{i+1} "}

n = gets.to_i
h = {}
readline.split(" ").each_with_index.map { |item, pos| h[item.to_i] = pos+1 }
(1..n).each { |i| print "#{h[i]} " }

gets
puts gets.split.map(&:to_i).each_with_index.map.sort_by{|p,i|p}.map{|p,i|i+1}*" "

gets;
arr = gets.split.map(&:to_i)
arr2 = Array(arr.size + 1)
for i in 1..arr.size do
 arr2[ arr[i-1] ] = i;
end
for i in 1...arr2.size do
 puts arr2[i]
end

friendNum = gets.chomp.to_i
input = gets.chomp.split(' ').map {|item| item.to_i}
output = []
(0...friendNum).each do |loops|
	output[(input[loops] - 1) % friendNum] = loops + 1
end
output.each do |ans|
	print "#{ans} "
end

n = gets.chomp.to_i
gets.chomp.split(' ').map(&:to_i).zip((1..n).to_a).sort { |x, y| x[0] <=> y[0]}.each {|x| puts x[1] }

n=gets.to_i
a=gets.split.map(&:to_i)
(1..n).each{|i|puts a.index(i)+1}

r={}
i=1
n=gets.to_i
gets.split(' ').each{|x|r[x.to_i]=i.to_s;i=i+1}
n.times{|i|print r[i+1]+' '}

n=gets.to_i
r=[0]*n
i=1
gets.split(' ').each{|x|r[x.to_i-1]=i;i=i+1}
print r.join(' ')

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

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

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
h = {}
arr.each_with_index { |a,i|
  h[a] = i + 1
}
for i in (1..n)
  puts h[i]
end

n = gets.to_i
a = gets.split.map &:to_i
c = {}
for i in (1..n)
  c[i] = a[i-1]
end
puts c.sort_by {|k, v| v}.map{|x| x[0]}.join(" ")

n = gets.to_i
ar = Array.new
ar = gets.split.map &:to_i
a = ar.size
a.times{
  |i| puts ar.index(i+1)+1 # array.index(value) returns the index of value in the array
 }

l = []
gets
gets.split.map(&:to_i).each_with_index { |x, i| l[x - 1] = i + 1 }
puts l.join(" ")

member_num = gets.chomp.to_i
present_arr = gets.chomp.split
present_arr.map!{|i| i.to_i}
output_arr = Array.new(member_num)
i = 0
while i < output_arr.length
  output_arr[present_arr[i]-1] = i+1
  i = i+1
end
print output_arr.join(" ")

t=[];$<.take(2)[1].split.each_with_index{|k,i| t[k.to_i]=i+1};puts t[1..-1].join " "

n = gets.to_i
ar = gets.chomp.split(' ',n)
puts ar.map.with_index(1).sort_by(&:first).map(&:last).map(&:to_s).join(' ')

a = Array.new(gets.to_i)
puts gets.strip.split.map(&:to_i).to_a.each_with_index { |e, i| a[e-1] = i+1 } * ' '

gets
arr = gets.split.map &:to_i
sol = Array.new
arr.each_with_index { |x, index| sol[arr[index]] = index + 1 }
sol.shift
p sol

number = gets.to_i
answer = Array.new(number + 1)
i = 1

number.times {
  n = gets.to_i
  answer[n] = i
  i += 1
}

answer.each { |x| print "#{x} " }

n = gets.to_i
q = gets.chomp.split.map { |x| x.to_i }
h = Hash.new

for i in 0..n-1
  h[i] = q[i]
end

h = h.invert

p h

for i in 1..n
  print h[i]+1, ' '
end

i = gets.to_i
array = gets.chomp.split.map(&:to_i)
a = Array.new

puts array.map.with_index { |x, i| a[x-1] = i+1 }.join(" ")

t=[];gets.split.each_with_index{|k,i| t[k.to_i]=i+1};puts t[1..-1].join " "

r={}
i=1
gets
gets.split(' ').each{|x|r[x.to_i]=i;i=i+1}
print r.values.join(' ')

#!/usr/bin/env ruby

gets
m = {}
gets.split.map(&:to_i).each_with_index { |p,i|
   m[p] = i
}
m.map { |i,p|
   print "#{p+1} "
}
puts


n = gets.to_i
s = gets.split.map(&:to_i).uniq.sort
n = s.size
t = Array.new(n-1)
for i in 0..n-2
    t[i] = s[i+1]-s[i]
end
t.sort!
q = gets.to_i
lr = Array.new(q)
q.times do |i|
    lr[i] = gets.split.map(&:to_i)
end
a = Array.new(q){|i|i}.sort_by{|i|lr[i][1]-lr[i][0]}
ans = Array.new(q,0)
lr.sort_by!{|i,j|j-i}
c = 0
sum = 0
q.times do |i|
    d = lr[i][1]-lr[i][0]+1
    while c < n-1 && t[c] < d
        sum += t[c]
        c += 1
    end
    ans[a[i]] = sum+d+(n-c-1)*d
end
puts ans.join(" ")

def bsearch(arr,t)
  ok,ng = 0,arr.length
  while (ng - ok).abs > 1
    mid = (ok+ng)/2
    if arr[mid] <= t
      ok = mid
    else
      ng = mid
    end
  end
  return ok
end

n = gets.to_i
s = gets.split.map(&:to_i)
q = gets.to_i
qs = []
q.times do
  l,r = gets.split.map(&:to_i)
  qs << r-l+1
end

s.sort!
diff = [0]
(n-1).times do |i|
  diff << s[i+1] - s[i]
end
diff << 10**18+1
diff.sort!
cumsum = [0]
n.times do |i|
  cumsum << cumsum[-1] + diff[i+1]
end

qs.each do |t|
  fill = bsearch(diff,t)
  ans = cumsum[fill] + t*(n-fill)
#  p [t,fill,cumsum[fill]]
  print "#{ans} "
end

def binary_search(ok, ng)
  while (ok - ng).abs > 1
    mid = (ok + ng) / 2

    if yield(mid)
      ok = mid
    else
      ng = mid
    end
  end

  ok
end

N = gets.to_i
S = gets.split.map(&:to_i)
SU = S.sort.uniq
Q = gets.to_i
query = Q.times.map { gets.split.map(&:to_i) }

DL = SU.each_cons(2).map { |a, b| b - a - 1 }.sort
DL.unshift(-Float::INFINITY)
DL.push(Float::INFINITY)
DL.reverse!

RUI = []

sum = 0

DL.reverse[1..-2].each do |v|
  sum += v
  RUI << sum
end

RUI.unshift(-Float::INFINITY)
RUI << Float::INFINITY
RUI.reverse!

ans = []
query.each do |l, r|
  a = SU.size
  d = r - l
  ei = binary_search(0, DL.size - 1) { |x| DL[x] >= d }
  b = d * ei
  c = d
  d = [RUI[ei + 1], 0].max

  ans << a + b + c + d
end

puts ans.join(' ')

def binary_search(ok, ng)
  while (ok - ng).abs > 1
    mid = (ok + ng) / 2

    if yield(mid)
      ok = mid
    else
      ng = mid
    end
  end

  ok
end

N = gets.to_i
S = gets.split.map(&:to_i)
SU = S.sort.uniq
Q = gets.to_i
query = Q.times.map { gets.split.map(&:to_i) }

DL = SU.each_cons(2).map { |a, b| b - a - 1 }.sort
RUI = []

sum = 0

DL.each do |v|
  sum += v
  RUI << sum
end

RUI.unshift(-Float::INFINITY)
RUI << Float::INFINITY
RUI.reverse!

ans = []
query.each do |l, r|
  a = SU.size
  d = r - l
  ei = binary_search(0, RUI.size - 1) { |x| RUI[x] >= d }
  b = d * ei
  c = d
  d = [RUI[ei + 1], 0].max

  ans << a + b + c + d
end

puts ans.join(' ')


N = gets.to_i
N.times do
    l1, r1, l2, r2 = gets.chomp.split.map(&:to_i)
    if r1 < l2 or r2 < l1
        puts "#{l1} #{l2}"
    else
        if l1 <= l2
            puts "#{l1} #{r2}"
        else
            puts "#{r1} #{l2}"
        end
    end
end

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

q = gets.to_i
q.times do
	lrlr = gets.chomp.split(" ").map(&:to_i)
	l1 = lrlr[0]
	r1 = lrlr[1]
	l2 = lrlr[2]
	r2 = lrlr[3]

	if (l1 == l2) and (l1==r2)
		puts "#{r1} #{l1}"
	else
		print "#{l1} "
		if(l1==l2)
			print r2
		else
			print l2
		end
		puts
	end
end

gets;puts$<.map{|e|a,b,c,d=e.split.map &:to_i;[a,(c!=a ? c : d)]*' '}

q = gets.to_i
q.times do
  l1, r1, l2, r2 = gets.split.map(&:to_i)
  a = l1
  b = l2
  if b == a
    b = r2
  end
  puts "#{a} #{b}"
end

input = gets.to_i
i = 0
input_range = []
input.times do
  input_range[i] = gets.split.map(&:to_i)
  input_range[i]
  i += 1
end

i = 0

input.times do
  if input_range[i][0] != input_range[i][3]
    print "#{input_range[i][0]} "
    puts "#{input_range[i][3]}"
  else
    print "#{input_range[i][1]} "
    puts "#{input_range[i][2]}"
  end
  i += 1
end

n = gets.to_i
n.times do
  l1, r1, l2, r2 = gets.split(' ').map(&:to_i)
  if (l1 != l2)
    puts [l1, l2].join(' ')
    next
  end
  if (r1 != r2)
    puts [r1, r2].join(' ')
    next
  end
  if (r1 - l1 == 2)
    puts [l1, r1].join(' ')
    next
  end
  puts [l1 + 1, r1 - 1].join(' ')
end

a = gets

def sol(a)
	(1..a.to_i).map {|num|
		a,b,c,d = gets.chomp.split
		[a,a==c ? d : c ]
	}
end

puts sol(a)

f = gets.to_i
f.times do
  l1, r1, l2, r2 = gets.to_s.chomp.split(" ").map{|x| x.to_i}
  if r1 != r2
    puts "#{r1} #{r2}"
  else
    puts "#{r1} #{r2 - 1}"
  end
end

input = gets.to_i
i = 0
input_range = []
input.times do
  input_range[i] = gets.split.map(&:to_i)
  input_range[i]
  i += 1
end

i = 0

input.times do
  print "#{input_range[i][0]} "
  puts "#{input_range[i][3]}"
  i += 1
end

n = gets.to_i
n.times do
  l1, r1, l2, r2 = gets.split(' ').map(&:to_i)
  if (l1 != l2)
    puts [l1, l2].join(' ')
    next
  end
  if (r1 != r2)
    puts [r1, r2].join(' ')
    next
  end
  puts [l1 + 1, r1 - 1].join(' ')
end

a = gets

def sol(a)
	(1..a.to_i).map {|num|
		a,b,c,d = gets.chomp.split('\s')
		[a,a==c ? d : c ]
	}
end

puts sol(a)


# Entrada: testcases, longitud del arreglo, arreglo permutación
# Salida: profundidad de cada nodo en orden de la entrada del arreglo
# Formación del arbol y resultado:
# 1 Paso: encontrar el número mayor del arreglo
# 2 Paso: separar los elementos del arreglo con base del número mayor, de los elementos de
# izquierda de este, y los elementos de la derecha
# 3 Paso: en cada conjunto separado, realizar el paso 1 y 2 hasta que no se pueda dividir
# Nota: si no hay elementos, se descarta(No se toma en cuenta).
# 4 Paso: buscar el nivel de profundidad de cada nodo y relacionarlo con el arreglo inicial
# 5 Paso: Mostrar en pantalla los niveles de profundidad de cada arreglo dado

# Más ejemplos de casos
# 1 Caso (Caso cargado a la derecha): 7,5,6,3,4,2,1
# Nivel 0
# 1 Paso: 7
# 2 Paso: Izquierda--> .Derecha-->5,6,3,4,2,1
# 3 Paso: Para Izquierda: ¿Es solo un elemento? No hay elemento ---> Se descarta
# Para Derecha: ¿Es solo un elemento? No ---> volvemos a 1 Paso con entrada derecha
# Nivel 1, Derecha
# 1 Paso: 6
# 2 Paso: Izquierda--> 5 .Derecha-->3,4,2,1
# 3 Paso: Para Izquierda ¿Es solo un elemento? Si --->Termina.
# Para Derecha: ¿Es solo un elemento? No ---> volvemos a 1 Paso con entrada derecha
# Nivel 2, Derecha
# 1 Paso: 4
# 2 Paso: Izquierda-->3 .Derecha-->2,1
# 3 Paso: Para Izquierda: ¿Es solo un elemento? Si --->Termina.
# Para Derecha: ¿Es solo un elemento? No ---> volvemos a 1 Paso con entrada derecha
# Nivel 3, Derecha
# 1 Paso: 2
# 2 Paso: Izquierda--> .Derecha-->1
# 3 Paso: Para Izquierda: ¿Es solo un elemento? No hay elemento ---> Se descarta
# Para Derecha: ¿Es solo un elemento? Si --->Termina.
# 4 Paso: Nivel 0: 7, Nivel 1: 6, Nivel 2: 4, Nivel 3: 2
# 5 Paso: 0, , 1, , 2, 3,  Sin conocer 5, 3, 1
# Salida correcta: No
# Observación: Para los niveles sin conocer, cada que se detecte que solo es un elemento,
# relacionar con el siguiente nivel ese elemento, entonces para nivel 2: 5 nivel 3: 3,
# nivel 4: 1 y la salída sería: 0, 2, 1, 3, 2, 3, 4
# Salida correcta con observación: Si
# 2 Caso (Zig-zag): 4,2,5,1,3
# Nivel 0
# 1 Paso: 5
# 2 Paso: Izquierda-->4,2 .Derecha-->1,3
# 3 Paso: Para Izquierda: ¿Es solo un elemento? No ---> volvemos a 1 Paso con entrada izquierda
# Para Derecha: ¿Es solo un elemento? No ---> volvemos a 1 Paso con entrada derecha
# Nivel 1, Izquierda
# 1 Paso: 4
# 2 Paso: Izquierda--> .Derecha-->2
# 3 Paso: Para Izquierda ¿Es solo un elemento? No hay elemento ---> Se descarta
# Para Derecha: ¿Es solo un elemento? Si ---> Termina y se relaciona con el siguiente nivel
# Nivel 1, Derecha
# 1 Paso: 3
# 2 Paso: Izquierda-->1 .Derecha-->
# 3 Paso: Para Izquierda: ¿Es solo un elemento? Si --->Termina y se relaciona con el siguiente nivel
# Para Derecha: ¿Es solo un elemento? No hay elemento ---> Se descarta
# 4 Paso: Nivel 0: 5, Nivel 1: 4,3, Nivel 2: 2,1
# 5 Paso: 1, 2, 0, 2, 1
# Salida correcta: Si

def permutationTransformation(a,n,nivel)
    # Observación
    if n == 1
        return nivel
    end
    #Pre paso: crear el arreglo del arbol "d" y los arreglos derecha, izquierda
    d = []
    izquierda, derecha = []
    # 1 Paso
    mayor = buscarMayorNumero(a,n)
    # 2 Paso
    izquierda, derecha = separarElementosArreglo(a,n,mayor)
    # 3 Paso
    if izquierda.length>0
        izquierda = permutationTransformation(izquierda,izquierda.length,nivel+1)
    end
    if derecha.length>0
        derecha = permutationTransformation(derecha,derecha.length,nivel+1)
    end
    # 4 Paso
    d = buscarNiveles(izquierda,nivel,derecha)
    return d
end

def buscarNiveles(izquierda,mayor,derecha)
    d = []
    if izquierda.class == Integer
        d.push(izquierda)
    else
        for i in(0..izquierda.length-1)
            d.push(izquierda[i])
        end
    end
    d.push(mayor)
    if derecha.class == Integer
        d.push(derecha)
    else
        for i in(0..derecha.length-1)
            d.push(derecha[i])
        end
    end
    return d
end

def separarElementosArreglo(a,n,mayor)
    izquierda = []
    derecha = []
    #Para izquierda
    for i in(0..mayor-1)
        izquierda.push(a[i])
    end
    #Para derecha
    for i in(mayor+1..n-1)
        derecha.push(a[i])
    end
    return izquierda, derecha
end

def buscarMayorNumero(a,n)
    mayor = a[0]
    posicion = 0
    for i in(0..n-1)
        if a[i]>mayor
            mayor = a[i]
            posicion = i
        end
    end
    return posicion
end

def convertirAInteger(a,n)
    for i in(0..n-1)
        a[i] = Integer(a[i])
    end
    return a
end

def darFormatoDeSalida(d)
    resultado = ""
    if d.class == Integer
        resultado = d.to_s
    else
        for i in(0..d.length-1)
            resultado = resultado + d[i].to_s
            if i != d.length-1
                resultado = resultado + " "
            end
        end
    end
    return resultado
end

# Variables
testcases = 0
n = 0
a = []
d = []
resultado = []

# Pre paso: Pedir los testcases, la longitud del arreglo y el arreglo
# testcases
testcases = Integer(gets.chomp)

for i in(0..testcases-1)
    #Longitud del arreglo
    n = Integer(gets.chomp)
    #Arreglo
    array = gets.chomp
    a = array.split(" ")
    a = convertirAInteger(a,n)
    d = permutationTransformation(a,n,0)
    resultado.push(darFormatoDeSalida(d))
end

resultado.each do |elements|
    puts elements
end

gets.to_i.times do
	n = gets.to_i
	a = gets.split.map(&:to_i)
	puts a.each_with_index.map { |x, i|
			res = 0
			now = a[i]
			(i + 1...n).each do |j|
				if now < a[j]
					now = a[j]
					res += 1
				end
			end
			now = a[i]
			(0...i).reverse_each do |j|
				if now < a[j]
					now = a[j]
					res += 1
				end
			end
			res
	     }.join(' ')
end


n, q = gets.split.collect{|i| i.to_i}
a = gets.split.collect{|i| i.to_i}.sort.reverse
s = Array.new(n+1,0)
(0...q).each do
    l, r = gets.split.collect{|i| i.to_i-1}
    s[l] += 1
    s[r+1] -= 1
end
(1...n).each do |i|
    s[i] += s[i-1]
end
s = s.sort.reverse
puts (0...n).inject(0){|sum, i| sum += s[i]*a[i]}

n,m,=gets.split.map{|x|x.to_i}
a=gets.split.map{|x|x.to_i}.sort.reverse
b=Array.new(300000,0)
m.times{|i|
  l,r,=gets.split.map{|x|x.to_i}
  b[l-1]+=1
  b[r]-=1
}
z=0
b=b.map{|e|e+z;z+=e}
b.sort!.reverse!
z=0
for i in 0..n-1 do
  z+=(b[i]*a[i])
end
puts "#{z}"

n, q = gets.split(' ').collect!{|i| i.to_i}

arr = gets.split(' ').collect!{|i| i.to_i}

d = Array.new(n, 0)
q.times do
	l, r = gets.split(' ').collect!{|i| i.to_i - 1}
	# (l..r).each do |j|
	# 	el[j] += 1
	# end
	d[l] += 1
	d[r+1] -= 1 if r < n-1
end

el = Array.new(n, 0)
sum = 0
n.times do |i|
	sum += d[i]
	el[i] = sum
end

el.sort!.reverse!

arr.sort!.reverse!

maxsum = 0

n.times do |i|
	# j = n - i - 1
	maxsum += arr[i]*el[i]
	break if el[i] == 0
end

p maxsum

n, q = gets.split.collect{|i| i.to_i}
a = gets.split.collect{|i| i.to_i}.sort.reverse
s = Array.new(n+1,0)
q.times do
    l, r = gets.split.collect{|i| i.to_i-1}
    s[l] += 1
    s[r+1] -= 1
end
n.times do |i|
    s[i+1] += s[i]
end
s = s.sort.reverse
puts (0...n).inject(0){|sum, i| sum += s[i]*a[i]}

n, q = gets.split.collect{|i| i.to_i}
a = gets.split.collect{|i| i.to_i}
s = Array.new(n+1,0)
e = Array.new(n+1,0)
(0...q).each do
    tmp = gets.split.collect{|i| i.to_i}
    s[tmp[0]] += 1
    e[tmp[1]] += 1
end
(1..n).each do |i|
    s[i] += s[i-1]
    e[i] += e[i-1]
end
(1..n).each do |i|
    s[i] = s[i]-e[i-1]
end
s = s.sort.reverse
a = a.sort.reverse
puts (0...n).inject(0){|sum, i| sum += s[i]*a[i]}

n, q = gets.split.collect{|i| i.to_i}
a = gets.split.collect{|i| i.to_i}.sort.reverse
s = Array.new(n+1,0)
q.times do
    l, r = gets.split.collect{|i| i.to_i-1}
    s[l] += 1
    s[r+1] -= 1
end
n.times do |i|
    s[i+1] += s[i]
end
s.sort!.reverse!
puts (0...n).inject(0){|sum, i| sum += s[i]*a[i]}


n,k,*a=gets(p).split.map(&:to_i)
a<<k
a=a.group_by{|_|_}.map{|k,v|[k]*~-v.size+[k+1]}.flatten while a[0]<k
p a[-1]-k

k=gets.split[1].to_i
a=gets.split.map(&:to_i)

ranks=Array.new(104,0)
a.each{|i| ranks[i]+=1}
ans=0
n=a.length
loop do
        if ranks[k]==n then
                puts ans
                exit
        end
        next_rank=Array.new(104,0)
        1.upto(k-1){|a|
                next_rank[a]+=[ranks[a]-1,0].max
                next_rank[a+1]+=[ranks[a],1].min
        }
        next_rank[k]+=ranks[k]
        ranks=next_rank
        ans+=1
end

include Math
require "prime"
require "bigdecimal"
require "bigdecimal/math"
require "matrix"
require "pp"
require "benchmark"

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

n,k=getia
ar=getia
dp=Array.new(k+1).fill(0)
ar.each{|a|dp[a]+=1}
t=0
while dp[k] != n
    nextdp=Array.new(k+1).fill(0)
    for j in 0...k
        next if dp[k-j-1].zero?
        dp[k-j]+=1
        dp[k-j-1]-=1
    end
    t+=1
end
puts t

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

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

d=Array.new(k+1, 0)
data.each{|v| d[v]+=1}

if d[-1]==n
	puts 0
	exit
end

ans=0
loop do
	(k-1).downto(1) do |i|
		if d[i]!=0
			d[i+1]+=1
			d[i]-=1
		end
	end

	ans+=1
	break if d[-1]==n
end

puts ans

n,k = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i)
count = Array.new(k,0)
arr.each do |a|
	count[a-1] += 1
end
move = 0
count1 = count.dup
while count[k-1] != n
	# puts "#{count}"
	move += 1
	count.each_with_index do |a,key|
		if count[key] != 0 && key != k - 1
			count1[key] -= 1
			count1[key+1] += 1
		end
	end
	count = count1.dup
end

puts move

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

tbl = Hash.new(0)
gets.split.each{|x| tbl[x.to_i] += 1 }

cnt = 0
until tbl[k] == n
  tbl2 = Hash.new(0)
  tbl.each do |key, value|
    if key < k
      tbl2[key] += value - 1 if value > 1
      tbl2[key + 1] += 1
    else
      tbl2[key] += value
    end
  end
  tbl = tbl2
  cnt += 1
end
p cnt

k=gets.split[1].to_i
a=gets.split.map(&:to_i)

ranks=Array.new(104,0)
a.each{|i| ranks[i]+=1}
ans=0
n=a.length
loop do
        if ranks[k]==n then
                puts ans
                exit
        end
        next_rank=Array.new(104,0)
        1.upto(k-1){|a|
                next_rank[a]+=ranks[a]/2
                next_rank[a+1]+=(ranks[a]+1)/2
        }
        next_rank[k]+=ranks[k]
        ranks=next_rank
        ans+=1
end

n,k = gets.split(" ").map(&:to_i)
arr = gets.split(" ").map(&:to_i)
count = Hash.new(0)
arr.each do |a|
	count[a] += 1
end
move = 0
while count[k] != n
	move += 1
	count.keys.each do |key|
		if count[key] != 0 && key != k
			count[key] -= 1
			count[key+1] += 1
		end
	end
end

puts move


t=gets.to_i
(1).upto(t) do
  n=gets.to_i
  a=gets.split(' ').map(&:to_i)
  vec = []
  ans = 0
  pos=false
  a.each do |i|
    if vec.empty?
      vec.push(i)
      pos=i>0
      next
    end
    if(i>0 != pos)
      ans+=vec.max
      vec=[i]
      pos = i>0
    else
      vec.push(i)
    end
  end
  ans+=vec.max
  puts ans
end

number_of_testcases = gets.to_s.to_i

number_of_testcases.times do |i|

  n = gets.to_s.to_i
  a = gets.to_s.split.map{|e|e.to_i}.push 0

  ans = 0
  s = []
  a.each do |k|
    if s.size > 0 && s[-1] * k <= 0
      ans += s.max
      s = []
    end
    s << k
  end
  # ans += s.max
  puts ans
end

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

  blocks = []
  while !a.empty?
    sign = a.first / a.first.abs
    blocks << a.take_while { |x| x / x.abs == sign }

    a = a.drop(blocks.last.size)
  end

  puts blocks.inject(0) { |s, e| s + e.max }
end

gets.to_i.times do
	n = gets.to_i
	a = gets.split.map(&:to_i)
	puts a.chunk { _1 < 0 }.sum { _1[1].max }
end

# frozen_string_literal: true

class Result
  def print_result
    gets
    arr = gets.split(' ').map(&:to_i)

    positive_arrs = [[], []]
    positive_arrs.each_with_index do |positive_arr, j|
      positive = j.zero?
      min = 0
      first = true
      arr.each do |i|
        if first
          if positive && i < 0
            next
          elsif !positive && i > 0
            next
          end

          first = false
        end

        if positive && min < i
          min = i
          next
        end

        if positive && i < 0
          positive = false
          positive_arr.push(min)
          min = i
          next
        end

        if !positive && (min == 0 || min < i) && i < 0
          min = i
          next
        end

        next unless !positive && i > 0

        positive = true
        positive_arr.push(min)
        min = i
      end

      positive_arr.push(min) if min != 0
    end

    puts(if positive_arrs[0].size == positive_arrs[1].size
           positive_arrs.map(&:sum).max
         elsif positive_arrs[0].size > positive_arrs[1].size
           positive_arrs[0].sum
         else
           positive_arrs[1].sum
    end)
  end

  private

  attr_reader :n
end

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

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

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