a={}
c={}
z={}
l=[]
gets.to_i.times{
	q,w=gets.split
	a[q]=a[q].to_i+w.to_i;
	l+=[[q,w]]
}
m=a.values.max
l.each{|q,w|
	c[q]=c[q].to_i+w.to_i;
	if c[q]>=m and a[q]==m then
		puts q
		exit
	end
}

#!/usr/bin/env ruby

t = gets.to_i
h1 = Hash.new(0)
h2 = Hash.new(0)
a = Array.new(t)
b = Array.new(t)
m = -1
winner = ""

for i in 0...t

  s = gets.split(' ')

  a[i] = s[0]
  b[i] = s[1].to_i

  h1[a[i]] += b[i]

end

h1.each_pair { |key, val|
  if val > m
    m = val
  end
}

for i in 0...t

  h2[a[i]] += b[i]
  if h2[a[i]] >= m && h1[a[i]] == m
    winner = a[i]
    break
  end

end

puts winner

n=gets.chomp.to_i
nc=0
@hash={}
@hash_total={}
loop do
	break if nc == n
	input=gets.chomp.split(" ")
	k=0
	if nc >= 1
		@hash.each_key do |plr_nm|
			if plr_nm!=input[0]
				k+=1
			end
		end
		if k==@hash.length
			@hash[input [0]]=[0]
			@hash[input [0]][nc]=input[1].to_i
		else
		@hash[input [0]][nc]=input[1].to_i
		end
	else
		@hash[input [0]]=[input[1].to_i]
	end

	nc+=1
end

def Total(key,array)
    k=0
	array.each do|l|
		if l== nil
			l=0
		end
	      k=k+l
	end
    @hash_total[key]=k
end
@hash.each_pair do|a,b|
	Total(a,b)
end

def Tiebreaker(a,b)
   ka=0
   ba=0
   k=0
   @hash[a[0]].each do|cn|
		if cn== nil
			cn=0
		end
		k=k+cn
		ka+=1
		break if k >= a[1]
	end
	k=0
   @hash[b[0]].each do|cn|
        if cn== nil
			cn=0
		end
		k=k+cn
		ba+=1
		break if k >= b[1]
	end
	if ka>ba
		return b
	else
		return a
	end
end

win= @hash_total.inject do |memo,n|
	if memo[1]>n[1]
		memo
	elsif memo[1]==n[1]
		Tiebreaker(memo,n)
	else
		n
	end
end
puts win[0]

num=Hash.new(0)
turn=Hash.new(0)
array=[]
a=gets.chomp.to_i
0.upto(a-1) do |i|
temp=gets.chomp.split(" ")
num[temp[0]]+=temp[1].to_i
array<<temp
end
high=num.values.sort.reverse[0]
winner=num.keys

winner.each do |i|

if num[i]==high
t=0
loop do
if array[t][0]==i
num[i]-=array[t][1].to_i
end
t+=1
if num[i]<=0
turn[i]=t
break
end
break if t==a
end
end

end
high=turn.values.sort[0]
ans=turn.key(high)
puts "#{ans}"

n = gets.to_i
data = []
score = {}
n.times do
    a,b = gets.split(' ')
    c = b.to_i
    score[a] ||= 0
    score[a] += c
    data <<= [a,c]
end

mx = score.values.max

score2 = {}
for v in data do
    nm,sc = v
    score2[nm] ||= 0
    score2[nm] += sc
    if (mx == score[nm]) and (score2[nm] >= mx)
        puts nm
        exit
    end
end

# http://codeforces.com/problemset/problem/2/A

Round = Struct.new(:player, :score)

class WinnerCalculator
  def self.get_winner(scoreboard)
    rounds = []
    scoreboard.each do |round_line|
      player, score = round_line.split
      rounds << Round.new(player, score.to_i)
    end

    scoring = Hash.new(0)
    rounds.each do |round|
      scoring[round.player] += round.score
    end
    max_score = scoring.values.max
    players_with_max_score = scoring.select { |player, score| score == max_score }

    if players_with_max_score.size == 1
      return players_with_max_score.keys.first
    end

    tiebreak_scoring = Hash.new(0)
    rounds.each do |round|
      tiebreak_scoring[round.player] += round.score
      if tiebreak_scoring[round.player] >= max_score && scoring[round.player] == max_score
        return round.player
      end
    end
  end
end

if __FILE__ == $0
  scoreboard = []
  gets.to_i.times { scoreboard << gets }
  winner = WinnerCalculator.get_winner(scoreboard)
  puts winner
end

scoring = Hash.new(0)
scoreboard = []
gets.to_i.times do
  player, score = gets.split
  scoring[player] += score.to_i
  scoreboard << [player, score.to_i]
end

max_score = scoring.values.max

tiebreak_scoring = Hash.new(0)
scoreboard.each do |player, score|
  tiebreak_scoring[player] += score.to_i
  if scoring[player] == max_score && tiebreak_scoring[player] >= max_score
    puts player
    break
  end
end

steps = gets.chomp.to_i

winner = nil
competitors = {}
step_logs = []

steps.times do
  step_result = gets.chomp.split
  step_winner = step_result[0]
  step_winner_score = step_result[1].to_i

  step_logs << [step_winner, step_winner_score]

  winner ||= step_winner
  competitors[step_winner] ||= 0

  competitors[step_winner] += step_winner_score

  winner = step_winner if competitors[step_winner] > competitors[winner]
end

winner_definition = {}

step_logs.each do |step_result|
  step_winner = step_result[0]
  step_winner_score = step_result[1]

  winner_definition[step_winner] ||= 0

  winner_definition[step_winner] += step_winner_score

  if (winner_definition[step_winner] >= competitors[winner]) && (competitors[step_winner] >= competitors[winner])
    puts step_winner
    break
  end
end

h = Hash.new {|h, k| h[k] = [[-1, 0, 0]]}
num_turns = gets.chomp.to_i
num_turns.times {|turn|
  name, score = gets.chomp.split
  score = score.to_i
  prev = h[name].last
  h[name] << [turn, score, prev.last + score]
}

players = h.keys
max_score = -1
max_players = []
players.each {|player; player_score|
  player_score = h[player].last.last
  if player_score > max_score
    max_score = player_score
    max_players = [player]
  elsif player_score == max_score
    max_score = player_score
    max_players << player
  end
}

winner = if max_players.size > 1
  max_players.map {|player|
    [h[player].detect {|(turn, _, score)| if score >= max_score; turn; end},
     player]
  }.sort_by {|(score, _)| score}.first.last
else
  max_players.first
end
puts winner

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

n=gets.to_i

data = []
hs = Hash.new(0)

n.times{
	name, points = gets.split

	data << [name, points.to_i]
	hs[name]+=points.to_i
}

max_points = hs.values.max

players = hs.keys.select{|k| hs[k]==max_points}

hs2 = Hash.new(0)
data.each{|name, points|
	next if !players.include? name

	hs2[name]+=points

	if hs2[name]>=max_points
		puts name
		break
	end
}

n = gets.to_i
h = Hash.new
names = []
scores = []
winners = []

for i in 0...n

  s, k = gets.chomp.split
  k = k.to_i

  h[s] = 0 if h[s].nil?

  h[s] += k

  names.push(s)
  scores.push(k)
end

max = h.values.max

h.each do |k,v|
  winners.push(k) if v >= max
end

winners = winners.uniq

h.clear

for i in 0...n
  if winners.include?(names[i])
    h[names[i]] = 0 if h[names[i]].nil?
    h[names[i]] += scores[i]
    if h[names[i]] >= max
      puts names[i]
      exit
    end
  end
end

# coding: utf-8

n = gets.to_i
h = {}
st = []
n.times {
    name, score = gets.split
    score = score.to_i
    h[name] ||= 0
    h[name] += score
    st << [name, score]
}
max = h.values.max
cands = []
for k,v in h
    if v == max
        cands << k
    end
end

if cands.length == 1
    puts cands[0]
else
    h2 = {}
    for s in st
        next if cands.index(s[0]) == nil
        h2[s[0]] ||= 0
        h2[s[0]] += s[1]
        if h2[s[0]] >= max
            puts s[0]
            break
        end
    end
end

num_lines = gets().chomp.to_i
scores = {}
log = []
num_lines.times{
|i|
  name, score = gets().chomp.split(/[ ]+/)
  score = score.to_i
  if(scores.has_key?(name)) then
  scores[name] += score
  else
  scores[name] = score
  end
  log << [name, scores[name], i]
}

scores_a = scores.to_a

max_score = scores_a.max {
|player1,player2|
  player1[1] <=> player2[1]
}[1]

top_scores = scores_a.select {
|player|
  player[1] == max_score
}

winner = log.select{
|line|
  top_scores.collect {
  |plr|
    plr[0]
  }.index(line[0]) &&
  line[1] >= max_score
}.min{
|line1,line2|
  line1[2] <=> line2[2]
}[0]

puts winner

n = gets.to_i
r = []
for i in 0 ... n
  a,b = gets.split
  r << [a, b.to_i]
end

h1 = Hash.new{0}
r.each do |name,val|
  h1[name] += val
end

maxi = h1.max{|x,y| x[1] <=> y[1]}[1]

h2 = Hash.new{0}
r.each do |name,val|
  h2[name] += val
  if (h1[name] == maxi && h2[name] >= maxi)
    puts name
    break
  end
end

def getIndex(val, h)
  if (h.key?(val))
    return h[val]
  end
  index = h.size
  return h[val] = index
end

n = gets.to_i
h = {}
last = []
sum = []

names = []
values = []

for i in 0 ... n
  name,val = gets.split
  index = getIndex(name,h)
  val = val.to_i
  if (index < sum.size)
    sum[index] += val
    last[index] = i
  else
    sum << val
    last << i
  end
  names << name
  values << val
end

ans = ""
fastest = n
maxi = sum.max
h.each do |key,index|
  next if (sum[index] != maxi)
  tsum, tmp = 0, n
  for i in 0 ... n
    next if (names[i] != key)
    tsum += values[i]
    if (tsum >= maxi)
      tmp = i
      break
    end
  end
  if (tmp < fastest)
    fastest, ans = tmp,key
  end
end

=begin
maxi = sum.max
fastest = n
h.each do |key,index|
  if (sum[index] == maxi &&  last[index] < fastest)
    fastest = last[index]
    ans = key
  end
end
=end

puts ans

n = gets.to_i
r = []
n.times do
  w, s = gets.split
  r << [w, s.to_i]
end

a = Hash.new(0)
r.each do |w, s|
  a[w] += s
end
mx = a.values.max

b = Hash.new(0)
r.each do |w, s|
  b[w] += s
  if b[w] >= mx and a[w] == mx
    puts w
    exit
  end
end

n = gets.to_i

max = 0
players = Hash.new(0)
rounds = []
n.times do
	name, score = gets.split
	score = score.to_i
	rounds << [name, score]
end

players = rounds.inject(Hash.new(0)) { |h, round| h[round[0]] += round[1]; h }
max = players.max_by { |k, v| v}[1]
winners = players.select { |k, v| v == max }
players = rounds.inject(Hash.new(0)) do |h, round|
	player = round[0]
	h[player] += round[1]
	if (h[player] >= max) && winners.member?(player)
		puts player
		break
	end
	h
end

scores = Hash.new(0)
in_array = []

n = gets.to_i
n.times do
	l = gets.split(" ")
	scores[l[0]] += l[1].to_i
	in_array << [l[0], l[1]]
end

tally = Hash.new(0)
max = scores.values.max
in_array.each do |score|
	tally[score[0]] += score[1].to_i
	if (max == scores[score[0]].to_i) && (tally[score[0]].to_i >= max)
		puts score[0]
		exit
	end
end

n = gets.chomp.to_i
h = Hash.new
as = Array.new
for i in 1..n
	s = gets.chomp
	as.push( s )
end
for i in 1..n
	nm,sc = as[i-1].split(/ /)
	sc = sc.to_i
	sc += h[nm] if h.has_key?(nm)
	h[nm] = sc
end
hv = h.to_a.sort {|a,b|
	b[1] <=> a[1]
}

if hv.length == 1 || hv[0][1] > hv[1][1]
	puts hv[0][0]
else
	hnm = Hash.new
	for i in 0..hv.length-1
		break if hv[0][1] > hv[i][1]
		hnm[hv[i][0]] = 1
	end

	h.clear
	for i in 1..n
		nm,sc = as[i-1].split(/ /)
		sc = sc.to_i
		sc += h[nm] if h.has_key?(nm)
		h[nm] = sc
		if sc >= hv[0][1] && hnm.has_key?(nm)
			puts nm
			exit
		end
	end
end

#http://codeforces.com/problemset/problem/2/A

n = gets.to_i
dir = Hash.new(0)
step = Array.new
max_val = 0
max_name = Array.new

for i in 1..n
    name, score = gets.chomp.strip.split(" ")
    score = score.to_i
    dir[name] += score
    record = Hash["name" => name, "score" => dir[name]]
    step << record
end

dir.each_key do |key|
    if dir[key] > max_val
        max_val = dir[key]
        max_name.clear
        max_name << key
    elsif dir[key] == max_val
        max_name << key
    end
end

flag = 0
if max_name.size == 1
    puts max_name[0]
else
    for i in 0...step.size
        if step[i]["score"] >= max_val
            for j in 0...max_name.size
                if step[i]["name"] == max_name[j] and flag == 0
                    puts max_name[j]
                    flag = 1
                end
            end
        end
    end
end

n=gets.to_i
v=[]
n.times do
	s,w=gets.split
	v << [s,w.to_i]
end
a=Hash.new(0)
v.each{|s,w|a[s]+=w}
m=a.max{|x,y|x[1]<=>y[1]}[1]
f=Hash.new(0)
v.each{|s,w|
	f[s]+=w
	if a[s]==m and f[s]>=m then
		puts s
		exit
	end
}

a={}
c={}
z={}
l=[]
gets.to_i.times{
	q,w=gets.split
	a[q]=a[q].to_i+w.to_i;
	l+=[[q,w]]
}
m=a.values.max
l.each{|q,w|
	c[q]=c[q].to_i+w.to_i;
	if c[q]>=m and a[q]==m then
		puts q
		exit
	end
}

table = {}
lined = []
ARGF.each_line do |line|
  if ARGF.lineno != 1
    name, mark = line.split
    if !table.has_key?(name)
      table[name] = [mark.to_i]
    else
      table[name] += [table[name].last + mark.to_i]
    end
    lined.push({:name => name, :mark => table[name].last})
  end
end

maxNames = []
max = 0

table.keys.each do |name|
   if max < table[name].last
     maxNames = [name]
     max = table[name].last
   elsif max == table[name].last
     maxNames.push name
   end
end

if maxNames.length == 1
  puts maxNames[0]
else
  lined.each do |val|
    if val[:mark] >= max and maxNames.include? val[:name]
      puts val[:name]
      break
    end
  end
end

#!/usr/bin/ruby
n=gets.chomp.to_i
h=Hash.new;h.default=0
a=Array.new
n.times{|z|
  s=gets.chomp.split(" ")
  s[1]=s[1].to_i
  h[s[0]]+=s[1]
  a.push(s)
}
m=h.values.max
h.each_key{|k| h[k]=(h[k]==m)?0:nil}

#re-read
a.each{|i|
  if h[i[0]]
    h[i[0]]+=i[1]
    if h[i[0]]>=m then puts i[0]; exit end
  end
}

#!/usr/bin/env ruby

class Transaction < Struct.new(:index, :value, :result)
end

$players_scores = Hash.new
$operation_index = 0

def process_score(score_string)
	parts = score_string.split(' ')
	name = parts[0]
	value = parts[1].to_i

	if (!$players_scores.include?(name))
		$players_scores[name] = Array.new
		$players_scores[name] << Transaction.new($operation_index, value, value)
	else
		player_info = $players_scores[name]
		$players_scores[name] << Transaction.new($operation_index, value, player_info[player_info.count - 1][:result] + value)
	end

	$operation_index += 1
end

# results for testing
# match_results = []
# match_results << "andrew 3"
# match_results << "andrew 2"
# match_results << "andrew 1"
# match_results << "mike 5"
# match_results << "andrew -1"
# match_results << "john 5"

# match_results << "andrew 3"
# match_results << "andrew 2"
# match_results << "mike 5"

repeats = gets.to_i
match_results = []

while (repeats > 0)
	match_results << gets.strip
	repeats -= 1
end

for score_string in match_results
	process_score(score_string)
end

winning_players = Array.new
max_score = 0

for score in $players_scores.values
	if (score[score.count - 1][:result] > max_score)
		winning_players.clear
		max_score = score[score.count - 1][:result]
		winning_players << score
	elsif score[score.count - 1][:result] == max_score
		winning_players << score
	end
end

min_index = $operation_index
winner = winning_players[0]

for score_history in winning_players
	for transaction in score_history
		if transaction[:result] >= max_score && transaction[:index] < min_index
			min_index = transaction[:index]
			winner = score_history
		end
	end
end

puts $players_scores.invert[winner]

n = gets.to_i
r = []
n.times do
	w, s = gets.split
	r << [w, s.to_i]
end

a = Hash.new(0)
r.each do |w,s|
	a[w] += s
end
m = a.max{|x,y| x[1] <=> y[1] }[1]

b = Hash.new(0)
r.each do |w,s|
	b[w] += s
	if a[w] == m && b[w] >= m then
		puts w
		exit
	end
end

n = gets.to_i
ary = []
hash = Hash.new 0
n.times do
	list = gets.split
	name = list[0]
	score = list[1].to_i
	hash.has_key?(name) ? hash[name] += score : hash[name] = score
	ary << [name, hash[name]]
end
maximum = hash.values.max
candidates = hash.select {|key, value| value == maximum}
ary.each do |entry|
	if candidates.key?(entry[0]) && entry[1] >= maximum
		puts entry[0]
		break
	end
end

h = {}
hh = {}
for name,score in $<.read.scan(/(\S+) (\S+)/)
    t = h[name] = (h[name]||0)+score.to_i
    for i in 0..t
        hh[i] = (hh[i]||[])+[name]
    end
end
max = h.values.max
puts hh[max].find{|x|h[x]==max}

n = STDIN.readline.to_i
score = Hash.new
t = []

n.times do |i|
	t << STDIN.readline.split
	# print STDIN.readline.split
	# print t
	t[i][1] = t[i][1].to_i
	if score[t[i][0]] == nil ;
		score[t[i][0]] = 0
	end
	score[t[i][0]] += t[i][1]
end

maxscore = 0
score.each_value do |i|
	maxscore = i if i > maxscore
end

iscore = Hash.new
ans = ""
n.times do |i|
	if iscore[t[i][0]] == nil ;
		iscore[t[i][0]] = 0
	end
	iscore[t[i][0]] += t[i][1]
	ans = t[i][0]; break if iscore[t[i][0]] >= maxscore &&
							 score[t[i][0]] == maxscore
end
print ans

n = gets.to_i
a = n.times.map{gets.split}
h = Hash.new(0)
a.each{|name, s| h[name] += s.to_i}
mx = h.max_by{|name, s| s}[1]
h2 = Hash.new(0)
a.each do |name, s|
  h2[name] += s.to_i
  if h[name] == mx && h2[name] >= mx
    puts name
    break
  end
end

n=STDIN.gets.chomp.to_i
players={}
n.times {|i|
  player,score=STDIN.gets.chomp.split(' ')
  if players.key?(player)
    scores=players[player]
    players[player]<<[score.to_i+scores[scores.length-1][0],i]
  else
    players[player]=[[score.to_i,i]]
  end
}
max=0
winners=[]
players.each_pair {|player,scores|
  score=scores[scores.length-1][0]
  if max<score
    winners=[player]
    max=score
  elsif max==score
    winners<<player
  end
}

min_idx=10000
winner=''
winners.each {|player|
  players[player].each {|score_idx|
    score=score_idx[0]
    idx=score_idx[1]
    if max<=score && idx<min_idx
      min_idx=idx
      winner=player
    end
  }
}
print(winner,"\n")

A={}
b={}
l=[]
gets.to_i.times{
    q,w=gets.split;
    A[q]=A[q].to_i+w.to_i;
    l+=[[q,w]]

}
m=A.values.max
l.each{|q,w|
 b[q]=b[q].to_i+w.to_i;
if b[q]>=m and A[q]==m then
    puts q
    exit
end
}

n = gets.to_i
a = n.times.map{gets.split}
h1 = a.each_with_object(Hash.new(0)){|(x, y), h| h[x] += y.to_i}
mx = h1.values.max
h2 = Hash.new(0)
a.each do |name, s|
  h2[name] += s.to_i
  if h1[name] == mx && h2[name] >= mx
    puts name
    break
  end
end

# Codeforces, Problem 2A
# http://codeforces.com/contest/1/problem/C
# Nate Collings

def get_names_scores(num)
  names = []
  scores = []
  num.times do
    player, score = gets.chomp.split("\s")
    names << player
    scores << score.to_i
  end
  return names, scores
end

def get_max(players)
  max = players.values.map { |v| v }.max
  with_max = players.select { |_,v| v == max}
  return max, with_max
end

def run_round(rounds, names, scores, players=nil, second=false)
  max, with_max = get_max(players) if second
  players = {}
  rounds.times do |round|
    player = names[round]
    score = scores[round]
    if !second || with_max.has_key?(player)
      score += players[player] if players.has_key?(player)
      players[player] = score
      return player if second && score >= max
    end
  end
  players
end

def first_round(rounds, names, scores)
  run_round(rounds, names, scores)
end

def second_round(rounds, names, scores, players)
  run_round(rounds, names, scores, players, true)
end

def solve
  rounds = gets.chomp.to_i
  names, scores = get_names_scores(rounds)

  candidates = first_round(rounds, names, scores)
  return candidates.keys[0] if candidates.length == 1
  return second_round(rounds, names, scores, candidates)
end

puts solve

final = {}

n = gets.to_i
names = []
scores = []
n.times do
  a = gets.split(/\s+/)
  name = a[0]
  score = a[1].to_i
  if final[name].nil? then
    final[name] = 0
  end
  final[name] += score
  names << name
  scores << score
end

max = 0
final.each{|key, value| max = value if max < value}

total = {}
n.times do |i|
  name = names[i]
  score = scores[i]
  if total[name].nil? then
    total[name] = 0
  end
  total[name] += score
  if total[name] >= max && final[name] == max then
    puts name
    break
  end
end

num_lines = gets().chomp.to_i
scores = {}
scores_fset ={}

addscore = -> (fn, maxscore, index) do
  (-> (score) {
      prev = fn.call(score)
      return prev if prev >= 0
      return index if score <= maxscore
      return -1
  })
end

num_lines.times do |index|
  line = gets().chomp
  name, score = line.split
  score = score.to_i
  scores[name] ||= 0
  scores[name] += score

  scores_fset[name] ||= -> (x) do
    (-> () {
      return index if x <= score
      return -1
    }).call()
  end

  if scores_fset[name].call(scores[name]) == -1
    scores_fset[name] = addscore.call(scores_fset[name], scores[name], index)
  end
end

top_score = scores.values.max
top_scorers = scores.select {|k, v| v == top_score}
puts top_scorers.min_by {|k, v| scores_fset[k].call(top_score)}[0]

N = gets.to_i
h = Hash.new { |hash, key| hash[key] = 0 }
a = []
N.times do
  n, p = gets.split
  a << [n, p.to_i]
  h[n] += p.to_i
end

m = h.values.max
a.reject! { |x| h[x[0]] < m }
h = Hash.new { |hash, key| hash[key] = 0 }
a.each do |n, p|
  h[n] += p
  if h[n] >= m
    puts n
    exit
  end
end

n = gets.chomp.to_i

log = []
total = {}

n.times do |i|
    line = gets.chomp.split(" ")
    player = line[0]
    cur_score = line[1].to_i
    log << [player, cur_score]
    score = total[player]
    total[player] = score.nil? ? cur_score : score + cur_score
end

max_score = total.values.max
players_with_max_score = total.select{|k, v| v == max_score }

if players_with_max_score.length > 1
    tot = {}
    log.each do |row|
        if players_with_max_score.keys.include? row[0]
            score = tot[row[0]]
            tot[row[0]] = score.nil? ? row[1] : score + row[1]
            if tot[row[0]] >= max_score
                puts row[0]
                exit
            end
        end

    end
else
    puts players_with_max_score.keys.first
end

num=Hash.new(0)
turn=Hash.new(0)
array=[]
a=gets.chomp.to_i
0.upto(a-1) do |i|
temp=gets.chomp.split(" ")
num[temp[0]]+=temp[1].to_i
array<<temp
end
high=num.values.sort.reverse[0]
winner=num.keys

winner.each do |i|

if num[i]==high
t=0
loop do
if array[t][0]==i
num[i]-=array[t][1].to_i
end
t+=1
if num[i]<=0
turn[i]=t
break
end
break if t==a
end
end

end
high=turn.values.sort[0]
ans=turn.key(high)
puts "#{ans}"

 a={}
c={}
z={}
l=[]
gets.to_i.times{
	q,w=gets.split
	a[q]=a[q].to_i+w.to_i;
	l+=[[q,w]]
}
m=a.values.max
l.each{|q,w|
	c[q]=c[q].to_i+w.to_i;
	if c[q]>=m and a[q]==m then
		puts q
		exit
	end
}

n = gets.to_i
lst = []
tot = {}
n.times do
  name, score = gets.split
  score = score.to_i
  lst << [name, score]
  tot[name] ||= 0
  tot[name] += score
end

m = tot.values.max

lst.reject! { |elem| tot[elem[0]] < m}
#puts lst

tot = {}
for name,score in lst do
  tot[name] ||= 0
  tot[name] += score
  if tot[name] >= m
    puts name
    break
  end
end

n = gets.to_i
h = []
n.times do
  name, score = gets.split
  score = score.to_i
  h << [name, score]
end

a = {}
h.each do |con|
  a[con[0]] ||= 0
  a[con[0]] += con[1]
end

m = a.max {|x,y| x[1] <=> y[1]}[1]

b = Hash.new(0)
h.each do |con|
  b[con[0]] += con[1]
  if a[con[0]] == m  && b[con[0]] >=m
    puts con[0]
    exit
  end
end

a={}
c={}
z={}
l=[]
gets.to_i.times{
	q,w=gets.split
	a[q]=a[q].to_i+w.to_i;
	l+=[[q,w]]
}
m=a.values.max
l.each{|q,w|
	c[q]=c[q].to_i+w.to_i;
	if c[q]>=m and a[q]==m then
		puts q
		exit
	end
}

n = gets.to_i

m = Array.new

a = {}

n.times do
	k = gets.chomp.split(" ")
	key = k[0]
	value = k[1].to_i
	if a[ key ].nil? then
		a[ key ] = value
	else
		a[ key ] += value
	end
	m << k
end

max = a.values.max

b = {}

m.each{ |s|
	key = s[0]
	value = s[1].to_i
	if b[ key ].nil? then
		b[ key ] = value
	else
		b[ key ] += value
	end
	if a[ key ] == max and b [ key ] >= max then
		puts key
		break
	end
}

log = []
scores = Hash.new(0)
check = Hash.new(0)
gets.to_i.times{
    n,q=gets.split
    scores[n]+=q.to_i
    log << [q,n]
}
m = scores.values.max
log.each{|q,n|
    check[n] += q.to_i
    if check[n] >= m and scores[n] == m then
        puts n
        exit
    end
}

players = Hash.new(0)
games = []
gets.to_i.times do |i|
  name, point = gets.chomp.split
  point = point.to_i
  games << [name, point]
  players[name] += point
end
sorted_players = players.sort{|a, b| b[1] <=> a[1]}
max = sorted_players[0][1]
max_players = {}
sorted_players.each do |s|
  break if s[1] < max
  max_players[s[0]] = 0
end

games.each do |game|
  if max_players.has_key?(game[0])
    max_players[game[0]] += game[1]
    if max_players[game[0]] >= max
      puts game[0]
      exit
    end
  end
end

n = gets.to_i
a = []
scores = {}
whens = {}
(0...n).each do |i|
    ee = gets.split
    a << [ee[0], ee[1].to_i]
    player = ee[0]
    pts = ee[1].to_i
    if scores.include? player
        scores[player] += pts
    else
        scores[player] = pts
    end
end
sc = scores.collect{|t| t[1]}
mx = sc.max
winners = {}
scores.each do |t|
    winners[t[0]] = 0 if t[1] == mx
end
(0...n).each do |i|
#     a << [ee[0], ee[1].to_i]
    player = a[i][0]
    pts = a[i][1]
    if winners.include? player
        winners[player] += pts
        if winners[player] >= mx
            puts player
            break
        end
    end
end

# frozen_string_literal: true

lines = gets.to_i

score_map = {}
at_index_by_name = {}
1.upto(lines) do |i|
  name, score = gets.split
  score = score.to_i

  score_map[name] ||= 0
  score_map[name] += score

  at_index_by_name[name] ||= []
  at_index_by_name[name] << [score_map[name], i]
end

scores = score_map.to_a.sort_by(&:last)
puts(
  scores
    .select { |score| scores.last.last == score.last }
    .min_by { |score| at_index_by_name[score.first].find { |e| e.first >= score.last }.last }
    .first
)

n = gets.to_i
names = Array.new
scores = Array.new
winn = Array.new
wins = Array.new
n.times do
  name, score = gets.split(" ")
  score = score.to_i
  index = nil
  0.upto(names.size-1) do |i|
    if names[i] == name
      index = i
      scores[index] += score
      break
    end
  end
  if index.nil?
    index = names.size
    names << name
    scores << score
  end
  winn << index
  wins << score
end
m = scores.max
scoresim = [0]*names.size
winner = -1
n.times do |i|
  scoresim[winn[i]] += wins[i]
  if scoresim[winn[i]] >= m and scores[winn[i]] == m
    winner = winn[i]
    break
  end
end
puts names[winner]

log = []
scores = Hash.new(0)
check = Hash.new(0)
gets.to_i.times{
    n,q = gets.split
    scores[n] += q.to_i
    log << [ n, q ]
}
m = scores.values.max
log.each{ | n, q |
    check[n] += q.to_i
    if check[n] >= m and scores[n] == m then
        puts n
        exit
    end
}

input = []
gets.to_i.times { input << gets }

s = Hash.new(0)
input.each do |i|
  name, score = i.split
  s[name] += score.to_i
end

max = s.values.max

ss = Hash.new(0)
input.each do |i|
  name, score = i.split
  ss[name] += score.to_i
  if ss[name] >= max && s[name] == max
    puts name
    break
  end
end

def process(m, ss)
  max = m.values.max
  maxes = m.select {|k, v| v == max}

  res = nil
  idx = 1001

  if maxes.size > 1
    ss = ss.select {|k, v| maxes.include? k}
    ss.each do |k, v|
      sum = 0
      v.each do |vv|
        sum += vv.score
        if sum >= max || vv.index > idx
          if res == nil || vv.index < idx
            res = k
            idx = vv.index
          end
          break
        end
      end
    end
    return res
  end

  maxes.keys[0]
end

num = STDIN.gets.chomp.to_i

S = Struct.new(:index, :score)
m = {}
s = {}

n = 0
while n < num
  n += 1
  line = STDIN.gets
  line.chomp!
  name, score = line.split
  score = score.to_i
  if m.include? name
    m[name] = m[name] + score
  else
    m[name] = score
  end
  if s.include? name
    s[name] << S.new(n, score)
  else
    s[name] = [S.new(n, score)]
  end
end

print process(m, s)

h = {}
n = gets.to_i
l = []
n.times { |i|
  a, b = gets.split
  l << [a,b]
  if h.key? a
    h[a] += b.to_i
  else
    h[a] = b.to_i
  end
}

max = -1000 * n - 1
indexes = []
h.each do |a, b|
  if b > max
    max = b
    indexes = [a]
  elsif b == max
    indexes << a
  end
end

h.clear

n.times { |i|

  a, b = l[i]

  if h.key? a
    h[a] += b.to_i
  else
    h[a] = b.to_i
  end

  if h[a] >= max and indexes.include? a
    print a
    exit(0)
  end

}

#!/usr/bin/ruby

n = gets.to_i
records = []
records = (1..n).map do
  name, score = gets.split
  {name: name, score: score.to_i}
end

table = {}
records.each do |rec|
  name = rec[:name]
  table[name] ||= 0
  table[name] += rec[:score]
end

max = table.values.max
table2 = {}
records.each do |rec|
  name = rec[:name]
  table2[name] ||= 0
  table2[name] += rec[:score]
  if table2[name] >= max and table[name] == max
    puts name
    exit
  end
end

input = gets.chomp.to_i
lines=Array.new
players = Hash.new
winner  = 0
input.times do
  lines.push gets.chomp
  player , points = lines.at(lines.length-1).split(" ")
  players[player] = players[player].to_i + points.to_i
end
players.each do |k,v|
  winner = v if v > winner
end
answers = players.select do |k ,v|
  v==winner
end
players=Hash.new
lines.each do |i|
    player , points = i.split(" ")
    players[player] = players[player].to_i + points.to_i
    if answers.key? player
       if winner <= players[player]
         puts player
         break
       end
    end
end

n = gets.to_i
ans = Hash.new
rec = Array.new
for i in 0...n do
  name, point = gets.split
  name = name.to_sym
  point = point.to_i

  if ans[name].nil?
    ans[name] = point
  else
    ans[name] = ans[name] + point
  end

  rec[i] = name, ans[name]
end

temp = ans.to_a
max_num = temp.max { |a, b| a[1] <=> b[1] }[1]
ans_index = 0

for r in rec do
  if r[1] >= max_num && ans[r[0]] == max_num
    ans_index = r[0]
    break
  end
end

puts ans_index

#!/usr/bin/env ruby

n = gets.chomp!.to_i
h1 = {}
a = []
n.times do
  name, round = gets.chomp!.split(" ")
  round = round.to_i
  h1[name] = h1[name].to_i + round
  a << [name, round.to_i]
end

max_score = h1.values.max
h2 = {}
a.each do | e |
  h2[e[0]] = h2[e[0]].to_i + e[1]
  if h2[e[0]] >= max_score and h1[e[0]] == max_score
    puts e[0]
    exit 0
  end
end

n=gets.chomp!.to_i
memo=Hash.new(0)
result=[]
n.times do
  result << gets.chomp.split
end
result.each do |name,score|
  memo[name]+=score.to_i
end
max=memo.values.max
memo.each_key do |key|
  if memo[key] == max
    memo[key]=0
  else
    memo[key] =-10000
  end
end
result.each do |name,score|
  memo[name]+=score.to_i
  if memo[name] >= max
    puts name
    break
  end
end

log = []
scores = Hash.new(0)
gets.to_i.times{
    n,q=gets.split
    scores[n]+=q.to_i
    log << [q,n]
}
m = scores.values.max
log.each{|q,n|
    if q.to_i>=m then
        p n
        exit
    end
}

n = gets.to_i
a = Hash.new([0])
n.times do |_|
	w, s = gets.split
	a[w] = a[w][0] + s.to_i, -_
end
puts a.max{|a,b| a[1] <=> b[1] }[0]

n = gets.to_i
h = Hash.new
max = 0
name = ''

for i in 1..n
  s, k = gets.chomp.split
  k = k.to_i
  if h[s].nil?
    h[s] = k
  else
    h[s] += k
  end
  if h[s] > max
    max = h[s]
    name = s
  end
end

puts name

def process(m, ss)
  max = m.values.max
  maxes = m.select {|k, v| v == max}

  res = nil
  idx = 1001

  if maxes.size > 1
    ss.each do |k, v|
      sum = 0
      v.each do |vv|
        sum += vv.score
        if sum >= max || vv.index > idx
          if res == nil || vv.index < idx
            res = k
            idx = vv.index
          end
          break
        end
      end
    end
    return res
  end

  maxes.keys[0]
end

num = STDIN.gets.chomp.to_i

S = Struct.new(:index, :score)
m = {}
s = {}

n = 0
while n < num
  n += 1
  line = STDIN.gets
  line.chomp!
  name, score = line.split
  score = score.to_i
  if m.include? name
    m[name] = m[name] + score
  else
    m[name] = score
  end
  if s.include? name
    s[name] << S.new(n, score)
  else
    s[name] = [S.new(n, score)]
  end
end

print process(m, s)

n = gets.to_i
r = []
for i in 0 ... n
  a,b = gets.split
  r << [a, b.to_i]
end

h1 = Hash.new{0}
r.each do |name,val|
  h1[name] += val
end

maxi = r.max{|x,y| x[1] <=> y[1]}[1]

h2 = Hash.new{0}
r.each do |name,val|
  h2[name] += val
  if (h1[name] == maxi && h2[name] >= maxi)
    puts name
    break
  end
end

log = []
scores = Hash.new(0)
check = Hash.new(0)
gets.to_i.times{
    n,q=gets.split
    scores[n]+=q.to_i
    log << [q,n]
}
m = scores.values.max
log.each{|q,n|
    check[n]+=q.to_i
    if check[n] >= m
        print n
        exit
    end
}

def getIndex(val, h)
  if (h.key?(val))
    return h[val]
  end
  index = h.size
  return h[val] = index
end

n = gets.to_i
h = {}
last = []
sum = []

for i in 0 ... n
  name,val = gets.split
  index = getIndex(name,h)
  val = val.to_i
  if (index < sum.size)
    sum[index] += val
    last[index] = i
  else
    sum << val
    last << i
  end
end

ans = ""
maxi = sum.max
fastest = n
h.each do |key,index|
  if (sum[index] == maxi &&  last[index] < fastest)
    fastest = last[index]
    ans = key
  end
end
puts ans

scores = {}
gets.to_i.times do |i|
  name, score = gets.split
  score = score.to_i
  if scores.has_key? name
    if scores[name][2] < scores[name][0]+score
      scores[name][1] = i
      scores[name][2] = scores[name][0]+score
    end
    scores[name][0] += score
  else
    scores[name] = [score, i, score]
  end
end

scores = scores.to_a.sort_by! { |key, value| -value[0] }
max_value = scores[0]
#puts scores.inspect
scores.each do |v|
  if v[1][0]<max_value[1][0]
    break
  elsif v[1][1]<max_value[1][1]
    max_value = v
  end

end
puts max_value[0]

n = gets.to_i
h = {}
ans = '', mx = -1e9
n.times do
  s = gets.split(' ')
  h[s[0]] = 0 if (h.has_key?(s[0]) == false)
  h[s[0]] += s[1].to_i
  if h[s[0]] > mx
    ans = s[0]
    mx = h[s[0]]
  end
end
puts ans

n = STDIN.readline.to_i
score = Hash.new
t = []

n.times do |i|
	t << STDIN.readline.split
	# print STDIN.readline.split
	# print t
	t[i][1] = t[i][1].to_i
	if score[t[i][0]] == nil ;
		score[t[i][0]] = 0
	end
	score[t[i][0]] += t[i][1]
end

maxscore = 0
score.each_value do |i|
	maxscore = i if i > maxscore
end

score.clear
ans = ""
n.times do |i|
	if score[t[i][0]] == nil ;
		score[t[i][0]] = 0
	end
	score[t[i][0]] += t[i][1]
	ans = t[i][0]; break if score[t[i][0]] >= maxscore
end
print ans

log = []
scores = Hash.new(0)
check = Hash.new(0)
gets.to_i.times{
    n,q=gets.split
    scores[n]+=q.to_i
    log << [q,n]
}
m = scores.values.max
log.each{|q,n|
    check[n] += q.to_i
    if check[n] >= m and scores[n] == q.to_i then
        puts n
        exit
    end
}

# Codeforces, Problem 2A
# http://codeforces.com/contest/1/problem/C
# Nate Collings

Player = Struct.new(:score, :round)

players = {}
rounds = gets.chomp.to_i

rounds.times do |round|
  player, score = gets.chomp.split("\s")
  score = score.to_i
  if players.has_key?(player)
    players[player].score += score
    players[player].round = round
  else
    players[player] = Player.new(score, round)
  end
end

max = players.values.map { |v| v.score }.max
with_max = players.select { |_,v| v.score == max }
winner = with_max.min_by { |_,v| v.round }[0]

puts winner

num_lines = gets().chomp.to_i
players_info = {}
num_lines.times{
  |i|
  name, score = gets().chomp.split(/[ ]+/)
  score = score.to_i
  if(players_info.has_key?(name)) then
    players_info[name][0] += score
    players_info[name][1] = i
  else
    players_info[name] = []
    players_info[name][0] = score
    players_info[name][1] = i
  end
}

puts players_info.to_a.sort {
  |a,b|
  (((b[1][0] <=> a[1][0]) * 2 + (a[1][1] <=> b[1][1])) <=> 0)
}[0][0]

n = gets.to_i

h = Hash.new
ins = Array.new
n.times do
  ins.push(gets)
  s = ins[-1].split(' ')
  h[s[0]] = 0 if (h.has_key?(s[0]) == false)
  h[s[0]] += s[1].to_i
end
mx = h.values.max
b = h.select {|k, v| v == mx}

h.clear()
ins.each do |s|
  s = s.split(' ')
  h[s[0]] = 0 if (h.has_key?(s[0]) == false)
  h[s[0]] += s[1].to_i
  if h[s[0]] == mx and b.has_key?(s[0])
    puts s[0]
    break
  end
end

#!/usr/bin/env ruby

n = gets.chomp!.to_i
scores = {}
scores['max-scorer'] = nil
n.times do
  name, round = gets.chomp!.split(" ")
  max_score = scores[scores['max-scorer']].to_i
  curr_score = round.to_i + scores[name].to_i
  scores[name] = curr_score
  scores['max-scorer'] = name if (curr_score > max_score)
end

puts scores['max-scorer']

#!/usr/bin/env ruby

h = Hash.new(0)
t = gets.to_i
m = -1
winner = ""

for i in 0...t

  s = gets.split(' ')

  a = s[0]
  b = s[1].to_i

  h[a] += b

  if h[a] > m
    m = h[a]
    winner = a
  end

end

puts winner

n = gets.to_i
ans = Hash.new
for i in 0...n do
  name, point = gets.split
  name = name.to_sym
  point = point.to_i

  if ans[name].nil?
    ans[name] = point, i
  else
    ans[name][0] = ans[name][0] + point
  end

  ans[name][1] = i
end

ans = ans.to_a
ans.sort! { |a, b| b[1][0] <=> a[1][0] }

i = 0
n = ans.size - 1
final = ans[0]
final_next = final

while i < n && final_next[1][0] == final[1][0]
  if final_next[1][1] < final[1][1]
    final = final_next
    final_next = ans[i + 1]
  else
    final_next = ans[i + 1]
  end
  i = i + 1
end

if final_next[1][1] < final[1][1] && final_next[1][0] == final[1][0]
  final = final_next
end

p final[0].to_s

table = {}
lined = []
ARGF.each_line do |line|
  if ARGF.lineno != 1
    name, mark = line.split
    if !table.has_key?(name)
      table[name] = [mark.to_i]
    else
      table[name] += [table[name].last + mark.to_i]
    end
    lined.push({:name => name, :mark => table[name].last})
  end
end

maxNames = []
max = 0

table.keys.each do |name|
   if max < table[name].last
     maxNames = [name]
     max = table[name].last
   elsif max = table[name].last
     maxNames.push name
   end
end

if maxNames.length == 1
  puts maxNames[0]
else
  lined.each do |val|
    if val[:mark] >= max
      puts val[:name]
      break
    end
  end
end

n = gets.to_i
ans = Hash.new
rec = Array.new
for i in 0...n do
  name, point = gets.split
  name = name.to_sym
  point = point.to_i

  if ans[name].nil?
    ans[name] = point
  else
    ans[name] = ans[name] + point
  end

  rec[i] = name, ans[name]
end

ans = ans.to_a
max_num = ans.max { |a, b| a[1] <=> b[1] }[1]
ans_index = 0

for r in rec do
  if r[1] >= max_num
    ans_index = r[0]
    break
  end
end

puts ans_index

n = gets.chomp.to_i
h = Hash.new
as = Array.new
for i in 1..n
	s = gets.chomp
	as.push( s )
end
for i in 1..n
	nm,sc = as[i-1].split(/ /)
	sc = sc.to_i
	sc += h[nm] if h.has_key?(nm)
	h[nm] = sc
end
hv = h.to_a.sort {|a,b|
	b[1] <=> a[1]
}

if hv.length == 1 || hv[0][1] > hv[1][1]
	puts hv[0][0]
else
	h.clear
	for i in 1..n
		nm,sc = as[i-1].split(/ /)
		sc = sc.to_i
		sc += h[nm] if h.has_key?(nm)
		h[nm] = sc
		if sc >= hv[0][1]
			puts nm
			exit
		end
	end
end

#!/usr/bin/env ruby

$global_hash = Hash.new
$max_score = 0
$leader = ""

def process_score(score_string)
	parts = score_string.split(' ')
	key = parts[0]
	value = parts[1].to_i

	if ($global_hash.has_key? key)
		$global_hash[key] += value
	else
		$global_hash[key] = value
	end

	if ($global_hash[key] > $max_score)
		$max_score = $global_hash[key]
		$leader = key
	end
end

#results for testing
# match_results = []
# match_results << "andrew 3"
# match_results << "andrew 2"
# match_results << "mike 5"

repeats = gets.to_i
match_results = []

while (repeats > 0)
	match_results << gets.strip
	repeats -= 1
end

for score_string in match_results
	process_score(score_string)
end

puts $leader

n=gets.chomp!.to_i
memo=Hash.new
score=[]
n.times do
  score << gets.chomp
end
n.times do |j|
  tmp=score[j].split
  if memo.key?(tmp[0])
    memo[tmp[0]]+=tmp[1].to_i
  else
    memo[tmp[0]]=tmp[1].to_i
  end
end
max=memo.values.max
memo.clear()
n.times do |j|
  tmp=score[j].split
  if memo.key?(tmp[0])
    memo[tmp[0]]+=tmp[1].to_i
  else
    memo[tmp[0]]=tmp[1].to_i
  end
  if memo[tmp[0]]>=max
    puts tmp[0]
    break
  end
end

# frozen_string_literal: true

lines = gets.to_i

score_map = {}
at_index_by_name = {}
1.upto(lines) do |i|
  name, score = gets.split
  score = score.to_i

  score_map[name] ||= 0
  score_map[name] += score

  at_index_by_name[name] ||= {
    score: -99_999_999,
    index: 9_999_999
  }

  next unless at_index_by_name[name][:score] < score_map[name]

  at_index_by_name[name] = {
    score: score_map[name],
    index: i
  }
end

scores = score_map.to_a.sort_by(&:last).reverse
puts(
  scores
    .select { |score| scores.last.last == score.last }
    .min_by { |score| at_index_by_name[score.first][:index] }
    .first
)

# frozen_string_literal: true

lines = gets.to_i

score_map = {}
at_index_by_name = {}
1.upto(lines) do |i|
  name, score = gets.split
  score = score.to_i

  score_map[name] ||= 0
  score_map[name] += score

  at_index_by_name[name] ||= {}

  next if at_index_by_name[name][score_map[name]]

  at_index_by_name[name][score_map[name]] = i
end

scores = score_map.to_a.sort_by(&:last)
puts(
  scores
    .select { |score| scores.last.last == score.last }
    .min_by { |score| at_index_by_name[score.first][score.last] }
    .first
)

# Codeforces, Problem 2A
# http://codeforces.com/contest/1/problem/C
# Nate Collings

players = {}
max = 0
winner = nil
rounds = gets.chomp.to_i

rounds.times do
  player, score = gets.chomp.split("\s")
  score = score.to_i
  players[player] = players.has_key?(player) ? players[player] += score : score
  if players[player] > max
    winner = player
    max = players[player]
  end
end

puts winner

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

n=gets.to_i

data = []
hs = Hash.new(0)

n.times{
	name, points = gets.split

	data << [name, points.to_i]
	hs[name]+=points.to_i
}

max_points = hs.values.max

hs2 = Hash.new(0)
data.each{|name, points|
	hs2[name]+=points

	if hs2[name]==max_points
		puts name
		break
	end
}

num_lines = gets().chomp.to_i
players_info = {}
num_lines.times{
  |i|
  name, score = gets().chomp.split(/[ ]+/)
  score = score.to_i
  if(players_info.has_key?(name)) then
    if(score != 0) then
      players_info[name][0] += score
      players_info[name][1] = i
    end
  else
    players_info[name] = []
    players_info[name][0] = score
    players_info[name][1] = i
  end
}

puts players_info.to_a.sort {
  |a,b|
  (((b[1][0] <=> a[1][0]) * 3 + (a[1][1] <=> b[1][1])) <=> 0)
}[0][0]

input = gets.chomp.to_i
lines=Array.new
players = Hash.new
winner  = 0
won=''
input.times do
  lines.push gets.chomp
  player , points = lines.at(lines.length-1).split(" ")
  players[player] = players[player].to_i + points.to_i
end
players.each do |k,v|
  winner = v if v > winner
end
answers = players.select do |k ,v|
  v==winner
end
players=Hash.new
lines.each do|i|
    player , points = i.split(" ")
    players[player] = players[player].to_i + points.to_i
    if winner == players[player] and answers.key? player
      won=player
      break
    end
end
puts won

n=gets.chomp!.to_i
memo=Hash.new
premax=-1111
max=-1000
name=""
n.times do
  tmp=gets.chomp!.split()
  if memo.key?(tmp[0])
    memo[tmp[0]]+=tmp[1].to_i
  else
    memo[tmp[0]]=tmp[1].to_i
  end
  max=-1000
  tmpname=""
  memo.each do |key,value|
    if max<value
      max=value
      tmpname=key
    end
  end
  if premax != max
    name=tmpname
  end
  premax=max
end
puts name

#!/usr/bin/env ruby

class Player < Struct.new(:score, :index)
end

$players_scores = Hash.new
$operation_index = 0

def process_score(score_string)
	parts = score_string.split(' ')
	key = parts[0]
	value = parts[1].to_i

	if (!$players_scores.include? key)
		$players_scores[key] = Player.new(value, $operation_index)
	else
		$players_scores[key][:score] += value
		if value >= 0
			$players_scores[key][:index] = $operation_index
		else
			$players_scores[key][:index] = 0
		end
	end

	$operation_index += 1
end

# results for testing
# match_results = []
# match_results << "andrew 3"
# match_results << "andrew 2"
# match_results << "andrew 1"
# match_results << "mike 5"
# match_results << "andrew -1"
# match_results << "john 5"

# match_results << "andrew 3"
# match_results << "andrew 2"
# match_results << "mike 5"

repeats = gets.to_i
match_results = []

while (repeats > 0)
	match_results << gets.strip
	repeats -= 1
end

for score_string in match_results
	process_score(score_string)
end

players = $players_scores.values
players.sort! { |a,b| a[:score] == b [:score] ? a[:index] <=> b[:index] : b[:score] <=> a[:score]}

puts $players_scores.invert[players[0]]

# frozen_string_literal: true

lines = gets.to_i

score_map = {}
at_index_by_name = {}
1.upto(lines) do |i|
  name, score = gets.split
  score = score.to_i

  score_map[name] ||= 0
  score_map[name] += score

  at_index_by_name[name] ||= {
    score: score_map[name],
    index: i
  }

  next unless at_index_by_name[name][:score] < score_map[name]

  at_index_by_name[name] = {
    score: score_map[name],
    index: i
  }
end

scores = score_map.to_a.sort_by(&:last)
puts(
  scores
    .select { |score| scores.last.last == score.last }
    .min_by { |score| at_index_by_name[score.first][:index] }
    .first
)

def process(m, ss)
  max = m.values.max
  maxes = m.select {|k, v| v == max}

  while maxes.size > 1 && !ss.empty?
    s = ss.pop()
    if s.score > 0 && maxes.include?(s.name)
      maxes.delete s.name
    end
  end

  maxes.keys()[0]
end

num = STDIN.gets.chomp.to_i

S = Struct.new(:name, :score)
m = {}
s = []

n = 0
while n < num
  n += 1
  line = STDIN.gets
  line.chomp!
  name, score = line.split
  score = score.to_i
  if m.include? name
    m[name] = m[name] + score
  else
    m[name] = score
  end
  s << S.new(name, score)
end

print process(m, s)

# coding: utf-8

n = gets.to_i
h = {}
st = []
n.times {
    name, score = gets.split
    score = score.to_i
    h[name] ||= 0
    h[name] += score if score >= 0
    st << [name, score]
}
max = h.values.max
cands = []
for k,v in h
    if v == max
        cands << k
    end
end
if cands.length == 1
    puts cands[0]
else
    h2 = {}
    for s in st
        h2[s[0]] ||= 0
        h2[s[0]] += s[1] if s[1] >= 0
        if h2[s[0]] >= max
            puts s[0]
            break
        end
    end
end

# frozen_string_literal: true

lines = gets.to_i

score_map = {}
at_index_by_name = {}
1.upto(lines) do |i|
  name, score = gets.split
  score = score.to_i

  score_map[name] ||= 0
  score_map[name] += score

  at_index_by_name[name] ||= {
    score: 0,
    index: 0
  }

  next unless at_index_by_name[name][:score] <= score_map[name]

  at_index_by_name[name] = {
    score: score_map[name],
    index: i
  }
end

scores = score_map.sort_by(&:last)
puts(
  scores
    .select { |score| scores.last.last == score.last }
    .min_by { |score| at_index_by_name[score.first][:index] }
    .first
)

num_lines = gets().chomp.to_i
players_info = {}
num_lines.times{
  |i|
  name, score = gets().chomp.split(/[ ]+/)
  score = score.to_i
  if(players_info.has_key?(name)) then
      players_info[name][0] += score
      if(score > 0) then players_info[name][1] = i end
  else
    players_info[name] = []
    players_info[name][0] = score
    players_info[name][1] = i
  end
}

puts players_info.to_a.sort {
  |a,b|
  (((b[1][0] <=> a[1][0]) * 2 + (a[1][1] <=> b[1][1])) <=> 0)
}[0][0]

n = gets.to_i
h = Hash.new(0)
p = ''
n.times do
  name, score = gets.split
  score = score.to_i
  h[name] += score
  p = name if h[name] > h[p]
end
mx = h.max_by{|name, s| s}
puts mx[1] == h[p] ? p : mx[0]

h = {}
n = gets.to_i
n.times { |i|
  a, b = gets.split
  if h.key? a
    h[a] = [h[a][0] + b.to_i, h[a][0] + b.to_i > h[a][2] ? i : h[a][1], h[a][0] + b.to_i > h[a][2] ? h[a][0] + b.to_i : h[a][2] ]
  else
    h[a] = [b.to_i, i, b.to_i]
  end
}

max2 = -1000 * n - 1
max1 = n
index = ''

h.each do |a, b|
  if b[2] > max2
    max2 = b[2]
    max1 = b[1]
    index = a
  elsif b[2] == max2 and b[1] < max1
    max1 = b[1]
    index = a
  end
end

print index

def getIndex(val, h)
  if (h.key?(val))
    return h[val]
  end
  index = h.size
  return h[val] = index
end

n = gets.to_i
h = {}
input = []
sum = []
inputLog = []

for i in 0 ... n
  name,val = gets.split
  index = getIndex(name,h)
  val = val.to_i
  input << val
  if (index < sum.size)
    sum[index] += val
  else
    sum << val
    inputLog << []
  end
  inputLog[index] << i
end

ans = ""
maxi = sum.max
fastest = n
h.each do |key,index|
  if (sum[index] == maxi)
    sum,tmp = 0,0
    for i in 0 ... inputLog[index].size
      sum += input[inputLog[index][i]];
      if (sum >= maxi)
        tmp = inputLog[index][i];
       break
      end
    end
    if (tmp < fastest)
      ans = key
      fastest = tmp
    end
  end
end
puts ans

require 'set'

n = gets.to_i

h = Hash.new
a = Array.new
b = Array.new
n.times do
  s = gets.split(' ')
  h[s[0]] = 0 if (h.has_key?(s[0]) == false)
  h[s[0]] += s[1].to_i
  a.push(s[0])
  b.push(h[s[0]])
end
mx = h.values.max

d = Set.new
h.each do |k, v|
  d.add(k) if v == mx
end

a.each_index do |i|
  if d.include? (a[i]) and b[i] == mx
    puts a[i]
    break
  end
end

num_lines = gets().chomp.to_i
players_info = {}
num_lines.times{
  |i|
  name, score = gets().chomp.split(/[ ]+/)
  score = score.to_i
  if(players_info.has_key?(name)) then
    players_info[name][0] += score
    players_info[name][1] = i
  else
    players_info[name] = []
    players_info[name][0] = score
    players_info[name][1] = i
  end
}

puts players_info.to_a.sort {
  |a,b|
  (((b[1][0] <=> a[1][0]) * 2 + (a[1][1] <=> b[1][1])) <=> 0)
}[0][0]

s = Hash.new(0)
t = {}
gets.to_i.times do |i|
  name, score = gets.split
  s[name] += score.to_i
  t[name] = i
end

max = s.values.max
puts t.select { |k, v| s[k] == max }.min_by {|k, v| v}.first

def getIndex(val, h)
  if (h.key?(val))
    return h[val]
  end
  index = h.size
  return h[val] = index
end

n = gets.to_i
h = {}
sum = []
last = []

for i in 0 ... n
  name,val = gets.split
  index = getIndex(name,h)
  val = val.to_i
  if (index < sum.size)
    sum[index] += val
    last[index] = i
  else
    sum << val
    last << i
  end
end

ans = ""
maxi = sum.max
fastest = n
h.each do |key,value|
  if (sum[value] == maxi && last[value] < fastest)
    ans = key
    fastest = last[value]
  end
end
puts ans

A={}
b={}
l=[]
gets.to_i.times{
    q,w=gets.split;
    A[q]=A[q].to_i+w.to_i;
    l+=[[q,w]]

}
m=A.values.max
l.each{|q,w|
 b[q]=b[q].to_i+w.to_i;
if b[q]>=m then
    puts q
    exit
end
}

n = gets.to_i
a = n.times.map{gets.split}
h = Hash.new(0)
a.each{|name, s| h[name] += s.to_i}
mx = h.max_by{|name, s| s}[1]
h2 = Hash.new(0)
a.each do |name, s|
  h2[name] += s.to_i
  if h2[name] == mx
    puts name
    break
  end
end

#!/usr/bin/env ruby

h = Hash.new(0)
t = gets.to_i
m = -1
winner = ""

for i in 0...t

  s = gets.split(' ')

  a = s[0]
  b = s[1].to_i

  h[a] += b

  if h[a] > m
    m = h[a]
    winner = a
  end

end

puts winner

players = {}
gets.to_i.times do |i|
  name, point = gets.chomp.split
  players[name] ||= {:point => 0, :last => nil}
  players[name][:point] += point.to_i
  players[name][:last] = i
end
result = players.sort{|a, b| (b[1][:point] <=> a[1][:point]).nonzero? || a[1][:last] <=> b[1][:last]}
#p result
puts result[0][0]

log = []
scores = Hash.new(0)
check = Hash.new(0)
gets.to_i.times{
    n,q=gets.split
    scores[n]+=q.to_i
    log << [q,n]
}
m = scores.values.max
log.each{|q,n|
    check[n]+=q.to_i
    if check[n] >= m
        print n
        exit
    end
}

# Codeforces, Problem 2A
# http://codeforces.com/contest/1/problem/C
# Nate Collings

players = {}
max = 0
winner = nil
rounds = gets.chomp.to_i

rounds.times do
  player, score = gets.chomp.split("\s")
  score = score.to_i
  players[player] = players.has_key?(player) ? players[player] += score : score
  players[player] = -1 if score < 0
  if players[player] > max
    winner = player
    max = players[player]
  end
end

puts winner

h = {}
n = gets.to_i
l = []
n.times { |i|
  a, b = gets.split
  l << [a,b]
  if h.key? a
    h[a] += b.to_i
  else
    h[a] = b.to_i
  end
}

max = -1000 * n - 1
h.each do |a, b|
  if b > max
    max = b
  end
end

h.clear

n.times { |i|

  a, b = l[i]

  if h.key? a
    h[a] += b.to_i
  else
    h[a] = b.to_i
  end

  if h[a] >= max
    print a
    exit(0)
  end

}

num_lines = gets().chomp.to_i
top_score = 0
scores = {}
scores_fset ={}

addscore = -> (fn, maxscore, index) do
  (-> (score) {
      prev = fn.call(score)
      return prev if prev
      return index if score <= maxscore
      return -1
  })
end

num_lines.times do |index|
  line = gets().chomp
  name, score = line.split
  score = score.to_i
  scores[name] ||= 0
  scores[name] += score

  scores_fset[name] ||= -> (x) do
    (-> () {
      return index if x <= score
      return -1
    }).call()
  end

  if scores_fset[name].call(scores[name]) == -1
    scores_fset[name] = addscore.call(scores_fset[name], scores[name], index)
  end

  top_score = scores[name] if scores[name] > top_score
end

top_scorers = scores.select {|k, v| v == scores.values.max}
puts top_scorers.min_by {|k, v| scores_fset[k].call(top_score)}[0]

table = {}
lined = []
ARGF.each_line do |line|
  if ARGF.lineno != 1
    name, mark = line.split
    if !table.has_key?(name)
      table[name] = [mark.to_i]
    else
      table[name] += [table[name].last + mark.to_i]
    end
    lined.push({:name => name, :mark => table[name].last})
  end
end

maxNames = []
max = 0

table.keys.each do |name|
   if max < table[name].last
     maxNames = [name]
     max = table[name].last
   elsif max == table[name].last
     puts "Add #{name} with #{max}"
   end
end

if maxNames.length == 1
  puts maxNames[0]
else
  lined.each do |val|
    if val[:mark] >= max
      puts val[:name]
      break
    end
  end
end

A={}
b={}
l=[]
gets.to_i.times{
    q,w=gets.split;
    A[q]=A[q].to_i+w.to_i;
    l+=[[q,w]]

}
m=A.values.max
l.each{|q,w|
 b[q]=b[q].to_i+w.to_i;
if b[q]>=m and A[q]=m then
    puts q
    exit
end
}

winner = ""
max_score = 0
scoreboard = Hash.new(0)
gets.to_i.times do
  player, score = gets.split
  scoreboard[player] += score.to_i
  if scoreboard[player] > max_score
    winner = player
    max_score = scoreboard[player]
  end
end

puts winner

log = []
scores = Hash.new(0)
gets.to_i.times{
    n,q=gets.split
    scores[n]+=q.to_i
    log << [q,n]
}
m = scores.values.max
log.each{|q,n|
    if q.to_i>=m then
        print n
        exit
    end
}

h = {}
n = gets.to_i
n.times { |i|
  a, b = gets.split
  if h.key? a
    h[a] = [h[a][0] + b.to_i, h[a][0] + b.to_i > h[a][2] ? i : h[a][1], h[a][0] + b.to_i > h[a][2] ? h[a][0] + b.to_i : h[a][2] ]
  else
    h[a] = [b.to_i, i, b.to_i]
  end
}

max2 = -1000 * n - 1
max1 = n
index = ''

h.each do |a, b|
  if b[2] > max2
    max2 = b[2]
    max1 = b[1]
    index = a
  elsif b[2] == max2 and b[1] < max1
    max1 = b[1]
    index = a
  end
end

print index

idx = 0
table = {}
ARGF.each_line do |line|
  if ARGF.lineno != 1
    name, mark = line.split
    if !table.has_key?(name)
      table[name] = {'mark' => mark.to_i, 'pos' => idx}
    else
      table[name]['mark'] += mark.to_i
      table[name]['pos'] = idx
    end
    idx = idx + 1
  end
end

maxMark = 0
minIdx = 0
max = ''
table.keys.each do |key|
  val = table[key]
  if maxMark < val['mark']
    maxMark = val['mark']
    minIdx = val['pos']
    max = key
  elsif maxMark == val['mark'] && minIdx > val['pos']
    minIdx = val['pos']
    max = key
  end
end

puts max

num_lines = gets().chomp.to_i
top_score = 0
scores = {}
scores_fset ={}

addscore = -> (fn, maxscore, index) do
  (-> (score) {
      prev = fn.call(score)
      return prev if prev
      return index if score <= maxscore
      return false
  })
end

num_lines.times do |index|
  line = gets().chomp
  name, score = line.split
  score = score.to_i
  scores[name] ||= 0
  scores[name] += score

  scores_fset[name] ||= -> (x) do
    (-> () {
      return index if x <= score
      return false
    }).call()
  end

  unless scores_fset[name].call(scores[name])
    scores_fset[name] = addscore.call(scores_fset[name], scores[name], index)
  end

  top_score = scores[name] if scores[name] > top_score
end

puts top_scorers = scores.select {|k, v| v == scores.values.max}
puts top_scorers.min_by {|k, v| scores_fset[k].call(top_score)}[0]

#http://codeforces.com/problemset/problem/2/A

n = gets.to_i
dir = Hash.new(0)
step = Array.new
max_val = 0
max_name = Array.new

for i in 1..n
    name, score = gets.chomp.strip.split(" ")
    score = score.to_i
    dir[name] += score
    record = Hash["name" => name, "score" => dir[name]]
    step << record
end

dir.each_key do |key|
    if dir[key] > max_val
        max_val = dir[key]
        max_name << key
    elsif dir[key] == max_val
        max_name << key
    end
end

flag = 0
if max_name.size == 1
    puts max_name[0]
else
    for i in 0...step.size
        if step[i]["score"] >= max_val
            for j in 0...max_name.size
                if step[i]["name"] == max_name[j] and flag == 0
                    puts max_name[j]
                    flag = 1
                end
            end
        end
    end
end

def getIndex(val, h)
  if (h.key?(val))
    return h[val]
  end
  index = h.size
  return h[val] = index
end

n = gets.to_i
h = {}
input = []
sum = []
inputLog = []

for i in 0 ... n
  name,val = gets.split
  index = getIndex(name,h)
  val = val.to_i
  input << val
  if (index < sum.size)
    sum[index] += val
  else
    sum << val
    inputLog << []
  end
  inputLog[index] << i
end

ans = ""
maxi = sum.max
fastest = n
h.each do |key,index|
  if (sum[index] == maxi)
    sum,tmp = 0,0
    for i in 0 ... inputLog[index].size
      sum += input[inputLog[index][i]];
      if (sum > maxi)
        tmp = inputLog[index][i];
       break
      end
    end
    if (tmp < fastest)
      ans = key
      fastest = tmp
    end
  end
end
puts ans

n=gets.chomp.to_i
nc=0
@hash={}
@hash_total={}
loop do
	break if nc == n
	input=gets.chomp.split(" ")
	k=0
	if nc >= 1
		@hash.each_key do |plr_nm|
			if plr_nm!=input[0]
				k+=1
			end
		end
		if k==@hash.length
			@hash[input [0]]=[input[1].to_i]
		else
		@hash[input [0]]=@hash[input [0]]+[input[1].to_i]
		end
	else
		@hash[input [0]]=[input[1].to_i]
	end

	nc+=1
end
def Total(key,array)
    k=0
	array.each do|l|
	      k=k+l
	end
    @hash_total[key]=k
end
@hash.each_pair do|a,b|
	Total(a,b)
end

def Tiebreaker(a,b)
   ka=0
   ba=0
   k=0
   @hash[a[0]].each do|cn|
		k=k+cn
		ka+=1
		break if k >= a[1]
	end
	k=0
   @hash[b[0]].each do|cn|
		k=k+cn
		ba+=1
		break if k >= b[1]
	end
	if ka>ba
		return b
	else
		return a
	end
end

win= @hash_total.inject do |memo,n|
 if memo[1]>n[1]
	memo
 elsif memo[1]=n[1]
     Tiebreaker(memo,n)
 else
	n
end
end
puts win[0]

n = gets.chomp.to_i
h = Hash.new(n)
hsnm = ""
hssc = 0
for i in 1..n
	nm,sc = gets.chomp.split(/ /)
	sc = sc.to_i
	sc += h[nm] if h.has_key?(nm)
	h[nm] = sc
	if hssc < sc
		hsnm = nm
		hssc = sc
	end
end
puts hsnm

h = {}
hh = {}
for name,score in $<.read.scan(/(\S+) (\S+)/)
    t = h[name] = (h[name]||0)+score.to_i
    hh[t] = name unless hh[t]
end
puts hh[h.map(&:last).max]

log = []
scores = Hash.new(0)
check = Hash.new(0)
gets.to_i.times{
    n,q=gets.split
    scores[n]+=q.to_i
    log << [q,n]
}
m = scores.values.max
log.each{|q,n|
    check[n] += q.to_i
    if check[n] >= m and scores[n] == q.to_i then
        puts n
        exit
    end
}

# frozen_string_literal: true

lines = gets.to_i

score_map = {}
at_index_by_name = {}
1.upto(lines) do |i|
  name, score = gets.split
  score = score.to_i

  score_map[name] ||= 0
  score_map[name] += score

  at_index_by_name[name] ||= {
    score: -99_999_999,
    index: 9_999_999
  }

  next unless at_index_by_name[name][:score] < score_map[name]

  at_index_by_name[name] = {
    score: score_map[name],
    index: i
  }
end

scores = score_map.to_a.sort_by(&:last)
puts(
  scores
    .select { |score| scores.last.last == score.last }
    .min_by { |score| at_index_by_name[score.first][:index] }
    .first
)

#!/usr/bin/env ruby

$global_hash = Hash.new
$max_score = 0
$leader = ""

if ($global_hash.has_key? "key")
	$global_hash["key"] += 2
else
	$global_hash["key"] = 2
end

def process_score(score_string)
	parts = score_string.split(' ')
	key = parts[0]
	value = parts[1].to_i

	if ($global_hash.has_key? key)
		$global_hash[key] += value
	else
		$global_hash[key] = value
	end

	if ($global_hash[key] > $max_score)
		$max_score = $global_hash[key]
		$leader = key
	end
end

#results for testing
# match_results = []
# match_results << "andrew 3"
# match_results << "andrew 2"
# match_results << "mike 5"

repeats = gets.to_i
match_results = []

while (repeats > 0)
	match_results << gets.strip
	repeats -= 1
end

for score_string in match_results
	process_score(score_string)
end

puts $leader

scores = {}
history = {}
gets.to_i.times do |i|
  name, score = gets.split
  score = score.to_i
  history[i] = [name, score]
  if scores.has_key? name
    scores[name] = scores[name]+score
  else
    scores[name] = score
  end
end

scores = scores.to_a.sort_by! { |key, value| -value }
m = scores[0][1]
scores = {}
#puts m
history.each do |k,v|
  name = v[0]
  score = v[1]
  if scores.has_key? name
    scores[name] = scores[name]+score
  else
    scores[name] = score
  end
  #puts "#{scores.inspect} #{scores[name]}"
  if scores[name] >=m
    puts name
    break
  end
end

players = {}
gets.to_i.times do |i|
  name, point = gets.chomp.split
  players[name] ||= {:point => 0, :max => nil, :max_at => nil}
  players[name][:point] += point.to_i
  if players[name][:max].nil? || players[name][:point] >  players[name][:max]
    players[name][:max] = players[name][:max]
    players[name][:max_at] = i
  end
end
puts players.sort{|a, b| (b[1][:max] <=> a[1][:max]).nonzero? || a[1][:max_at] <=> b[1][:max_at]}[0][0]

A={}
b={}
l=[]
gets.to_i.times{
    q,w=gets.split;
    A[q]=A[q].to_i+w.to_i;
     b[q]=b[q].to_i+w.to_i;
    l+=[[q,w]]

}
m=A.values.max
l.each{|q,w|

if b[q]>=m and A[q]=m then
    puts q
    exit
end
}

# coding: utf-8

n = gets.to_i
h = {}
st = []
n.times {
    name, score = gets.split
    score = score.to_i
    h[name] ||= 0
    h[name] += score
    st << [name, score]
}
max = h.values.max
cands = []
for k,v in h
    if v == max
        cands << k
    end
end
if cands.length == 1
    puts cands[0]
else
    h2 = {}
    for s in st
        h2[s[0]] ||= 0
        h2[s[0]] += s[1]
        if h2[s[0]] == max
            puts s[0]
            break
        end
    end
end

h = {}
n = gets.to_i
l = []
n.times { |i|
  a, b = gets.split
  l << [a,b]
  if h.key? a
    h[a] += b.to_i
  else
    h[a] = b.to_i
  end
}

max = -1000 * n - 1
h.each do |a, b|
  if b > max
    max = b
  end
end

h.clear

n.times { |i|

  a, b = l[i]

  if h.key? a
    h[a] += b.to_i
  else
    h[a] = b.to_i
  end

  if h[a] >= max
    print a
    exit(0)
  end

}

steps = gets.chomp.to_i

winner = nil
competitors = {}

steps.times do
  step_result = gets.chomp.split
  step_winner = step_result[0]
  step_winner_score = step_result[1].to_i
  winner ||= step_winner
  competitors[step_winner] ||= 0
  competitors[step_winner] += step_winner_score
  winner = step_winner if competitors[winner] < competitors[step_winner]
end

puts winner

# coding: utf-8

n = gets.to_i
h = {}
st = []
n.times {
    name, score = gets.split
    score = score.to_i
    h[name] ||= 0
    h[name] += score
    st << [name, score]
}
max = h.values.max
cands = []
for k,v in h
    if v == max
        cands << k
    end
end
if cands.length == 1
    puts cands[0]
else
    h2 = {}
    for s in st
        h2[s[0]] ||= 0
        h2[s[0]] += s[1]
        if h2[s[0]] >= max
            puts s[0]
            break
        end
    end
end

n=gets.chomp.to_i

names=[]
values=[]

a=0
while a<n
	data=gets.chomp.split
	name=data[0].to_s
	value=data[1].to_i

	names.push(name)
	values.push(value)
	a+=1
end

sortnames=[]
sortvalues=[]

max=0
maxvalue=0
for i in (0..values.length-1)
	if sortnames.length==0
		sortnames.push(names[i])
		sortvalues.push(values[i])
		maxvalue=sortvalues[i]
		max=i
	else

		flag = false

		for k in (0..sortnames.length-1)
			if sortnames[k]==names[i]
				flag = true
				sortvalues[k]+=values[i]
				if sortvalues[k]>maxvalue
					maxvalue=sortvalues[k]
					max=k
				end
				break
			end
		end

		if !flag
			sortnames.push(names[i])
			sortvalues.push(values[i])
			if values[i]>maxvalue
				maxvalue=values[i]
				max=sortnames.length-1
			end
		end
	end
end

puts sortnames[max]

n=gets.chomp!.to_i
memo=Hash.new
score=[]
n.times do
  score << gets.chomp
end
n.times do |j|
  tmp=score[j].split
  if memo.key?(tmp[0])
    memo[tmp[0]]+=tmp[1].to_i
  else
    memo[tmp[0]]=tmp[1].to_i
  end
end
max=memo.values.max
memo.clear()
n.times do |j|
  tmp=score[j].split
  if memo.key?(tmp[0])
    memo[tmp[0]]+=tmp[1].to_i
  else
    memo[tmp[0]]=tmp[1].to_i
  end
  if memo[tmp[0]]==max
    puts tmp[0]
    break
  end
end

s = Hash.new(0)
t = {}
gets.to_i.times do |i|
  name, score = gets.split
  s[name] += score.to_i
  t[name] = i
end

min = s.values.min
puts t.select { |k, v| s[k] == min }.min_by {|k, v| v}.first

n = gets.to_i
h = Hash.new
names = []
scores = []

for i in 0...n

  s, k = gets.chomp.split
  k = k.to_i

  if h[s].nil?
    h[s] = 0
  end

  h[s] += k

  names.push(s)
  scores.push(k)
end

max = h.values.max
h.clear

for i in 0...n
  if h[names[i]].nil?
    h[names[i]] = 0
  end
  h[names[i]] += scores[i]
  if h[names[i]] >= max
    puts names[i]
    exit
  end
end


gets.to_i.times{
    gets
    s=gets.chars.map{|x|x.ord-97}
    p s.max+1
}

main = -> {
  n = gets.to_i
  s = gets.chomp.chars
  s.max.ord - 'a'.ord + 1
}

t = gets.to_i
puts t.times.map{ main.call }.join("\n")

gets.to_i.times {
    gets
    s=gets.chars.map(&:ord)
    puts s.max.to_i-96
}

=begin

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

=end

t=gets.to_i
for i in 1..t
    n =gets.to_i
    p gets.chars.map{|x|x.ord}.max-"a".ord+1

end


t=gets.chomp.to_i
t.times do
    s= gets.chomp.split
    x=s[0].to_i
    y=s[1].to_i
    if (y % x ==0)
        puts "#{1} #{y/x}"
    else
        puts "#{0} #{0}"
    end
end

def try(i, y)
  cnt = 0
  while y % i == 0
    cnt += 1
    y /= i
  end

  return nil unless y == 1
  cnt
end

gets.to_i.times do
  x, y = gets.split.map(&:to_i)

  if y % x != 0
    puts '0 0'
    next
  end

  y /= x

  if y == 1
    puts '1 1'
    next
  end

  i = 2
  found = false
  while i <= y
    j = try(i, y)
    if j
      puts "#{j} #{i}"
      found = true
      break
    end

    i += 1
  end

  next if found
  puts '0 0'
end

pow = (1..10).flat_map do |a|
  (1..100).map do |b|
    [b ** a, [a, b]]
  end
end.to_h

gets.to_i.times do
  x, y = gets.split.map(&:to_i)
  if y >= x && y % x == 0 && pow[y / x]
    puts pow[y / x].join(" ")
  else
    puts "0 0"
  end
end

for t in 1..gets.to_i do
	a=gets.split
	a[0]=a[0].to_i
	a[1]=a[1].to_i
	c=a[0]
	d=a[1]

	if d%c==0
		print 1,' ',d.div(c),"\n"
	else
		print 0,' ',0,"\n"
	end
end

def try(i, y)
  cnt = 0
  while y % i == 0
    cnt += 1
    y /= i
  end

  return nil unless y == 1
  cnt
end

gets.to_i.times do
  x, y = gets.split.map(&:to_i)

  if y % x != 0
    puts '0 0'
    next
  end

  y /= x

  if y == 1
    puts '1 1'
    next
  end

  i = 2
  found = false
  while i * i <= y
    j = try(i, y)
    if j
      puts "#{j} #{i}"
      found = true
      break
    end

    i += 1
  end

  next if found
  puts '0 0'
end

pow = (1..10).flat_map do |a|
  (1..10).map do |b|
    [b ** a, [a, b]]
  end
end.to_h

gets.to_i.times do
  x, y = gets.split.map(&:to_i)
  if y >= x && y % x == 0 && pow[y / x]
    puts pow[y / x].join(" ")
  else
    puts "0 0"
  end
end


n = gets.chomp.to_i
arr = Hash.new{0}
n.times do
arr[gets.chomp]+=1
end
max = 0
arr.each do |key,value|
	if value > max
	max = value
	end
end
print max

=begin
4
0101
1000
1111
0101

3
111
111
111
=end

def function
b=gets.chomp.to_i
string_array=[]
b.times do |i|
a=gets.chomp
string_array.push(a)
end
count_array=[]
0.upto string_array.length-1 do |i|
count_array.push(string_array.count(string_array[i]))
end
puts count_array.max
end
function

rows = gets.to_i
row_hash = {}
(rows).times do
    row_val = gets
    if row_hash.include?(row_val)
        row_hash[row_val]+=1
    else
        row_hash[row_val] = 1
    end
end
puts (row_hash.max_by{|k, v| v}).last

$pair = {}

def sweep(arr, row)
		str = arr[row] * ""
		if $pair.has_key?(str)
			$pair[str] += 1
		else
			$pair[str] = 1
		end
end

=begin
def count(arr, col)
	l = 0
	(0...arr[0].length).each do |iterNum|
		l += 1 if arr[iterNum][0] == 0
	end

	l
end

def cleanCount(arr)
	l = 0

	(0...arr[0].length).each do |iterNum|
		str = arr[iterNum] * ""
		l += 1 if str == ("1" * arr[0].length)
	end

	l
end
=end

n = gets.chomp.to_i
room = []
countArr = []

(0...n).each do |iter|
	room << (gets.chomp.split('').map {|item| item.to_i})
end

(0...n).each do |iter|
	countArr << sweep(room, iter)
	#print $pair
	#puts ''
end

max = 0
$pair.each_value do |val|
	max = val if max < val
end

puts max

n = gets.to_i
s = {}
n.times do |i|
  r = gets.chomp
  s[r] ||= 0
  s[r] += 1
end

puts s.values.max

n,h = gets.to_i,{}
(1..n).each{
    r = gets.strip
    h[r] ||= 0
    h[r] += 1
}
p h.values.max

n = gets.to_i
a = n.times.map{gets.chomp}
p a.uniq.reduce(0){|mx, x| mx = [mx, a.count(x)].max}

n = gets().chomp().to_i
b = []

n.times{
        b.push(gets().chomp())
}

c=b.sort{|x,y| x <=> y}

str=c[0]
max = 1
temp =1
for i in (1..c.length-1)
        if c[i]==str
                temp+=1

        else
                str=c[i]
                if temp > max
                        max = temp
                end
                temp=1
        end

end

if temp > max
        max = temp
end

puts max

n = gets().chomp().to_i
b = []

n.times{
        s = gets().chomp().split('')
        a = []
        for i in (0..n-1)
                if s[i]=='0'
                        a.push("#{i+1}")
                end
        end
        if a.length()==0
                a.push("0")
        end
        b.push(a.join(''))
}

c=b.sort{|x,y| x.length <=> y.length}

str=c[0]
max = 1
temp =1
for i in (1..c.length-1)
        if c[i]==str
                temp+=1

        else
                str=c[i]
                if temp > max
                        max = temp
                end
                temp=1
        end

end

puts max


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

n = readline.to_i
arr = readline.split(' ').collect(&:to_i)
ct = 0
res = []
0.upto(arr.length - 2) do |ind|
  a, b = arr[ind], arr[ind + 1]
  res += [a]
  if gcd(a, b) != 1
    ct += 1
    res += [1]
  end
end
res += [arr.last]
puts ct
res.each_with_index do |n, i|
  if i != 0
    print " "
  end
  print n
end
puts ""

gets;a=gets.split.map(&:to_i);b=[a[0],*a.each_cons(2).flat_map{|x,y|x.gcd(y)==1 ? [y] : [1,y]}];p b.size-a.size;puts b*' '

a=gets.chomp.to_i
b=gets.chomp.split(" ").map{|i| i.to_i}
ans=[]
p=0
0.upto(a-2) do |i|
    if b[i].gcd(b[i+1])!=1
      ans << b[i]
      ans << 1
      p+=1
    else
        ans << b[i]
    end
end
ans << b[a-1]
puts "#{p}"
puts ans.join(" ")

def gcd(a,b)
	return b>0 ? gcd(b,a%b) : a
end

n=gets.to_i
a=gets.split(' ')
s=0
for i in 1..a.size-1
	s+=1 if gcd(a[i].to_i,a[i-1].to_i)>1
end
puts s
print a[0]
for i in 1..a.size-1
	print " 1" if gcd(a[i].to_i,a[i-1].to_i)>1
	print " "+a[i]
end
puts

a=gets.chomp.to_i
b=gets.chomp.split(" ").map{|i| i.to_i}
ans=[]
0.upto(a-2) do |i|
    if b[i].gcd(b[i+1])!=1
      ans << b[i]
      ans << 1
    else
        ans << b[i]
    end
end
ans << b[a-1]
puts ans.join(" ")

def gcd(a,b)
	return b>0 ? gcd(b,a%b) : a
end

n=gets.to_i
a=gets.split(' ')
print a[0]
for i in 1..a.size-1
	print " 1" if gcd(a[i].to_i,a[i-1].to_i)>1
	print " "+a[i]
end
puts


c = Hash.new(0)

n = gets.to_i

max = -1

gets.split.map(&:to_i).each do |i|
	c[i]+=1
	max = [c[i], max].max
end

can = max<=(n/2.0).ceil.to_i

puts can ? 'YES' : 'NO'

n=gets.chomp.to_i
n=(n+1)>>1
a=gets.chomp.split
for i in a do
	if a.count(i)>n
		puts "NO"
		exit
	end
end
puts "YES"

n = gets.to_i
cnt = [], maxs = 0
gets.split(' ').map do |i|
    i = i.to_i
    cnt[i] = cnt[i].nil? ? 1 : cnt[i] + 1
    maxs = [cnt[i], maxs].max
end
if maxs > (n + 1) / 2 && n > 1 then puts "NO"
else puts "YES"
end

n = gets.to_i
a = gets.split(' ').map { |i| i.to_i }
cnt = []
maxs = 0
a.each do |i|
    cnt[i] = 0 if cnt[i].nil?
    cnt[i] += 1
    maxs = [cnt[i], maxs].max
end
if maxs > (n + 1) / 2 && n > 1
    puts "NO"
else
    puts "YES"
end

def run
  n = $stdin.gets.to_i
  a = $stdin.gets.split.map(&:to_i)
  h = Hash.new(0)
  a.each do |item|
    h[item] += 1
  end

  h.each do |key, value|
    if (value - 1) * 2 >= n
      puts "NO"
      return
    end
  end

  puts "YES"
end

run if $0 == __FILE__

class A
  def initialize
    n = $stdin.gets.chomp.to_i
    array = $stdin.gets.chomp.split(' ').map(&:to_i)

    if array.size == 1
      puts "YES"
      exit
    elsif array.size == 2
      if array[0] == array[1]
        puts "NO"
      else
        puts "YES"
      end
      exit
    end

    if array.size.even?
      1.upto(1000) do |num|
        count = array.count(num)

        if(count > array.size/2)
          puts "NO"
          exit
        end
      end
    else
      1.upto(1000) do |num|
        count = array.count(num)

        if(count > array.size/2+1)
          puts "NO"
          exit
        end
      end
    end

    puts "YES"
  end
end

a = A.new

n = gets.to_i
a = gets.split #.map{|_|_.to_i}
h = {}

a.each{|i|
  if h[i] then
    h[i]+=1
  else
    h[i]=1
  end
}
x = n%2==0 ? n/2 : (n+1)/2
r = true
h.each{|k,v|
  r = false if v > x
}

puts (r ? 'YES':'NO' )

n = gets.chomp.to_i
elements = gets.chomp.split(/ /).map(&:to_i)
ans = "YES"
cnt = Array.new(1001, 0)
elements.each do |e|
    cnt[e] += 1
end
c = 0
cnt.each do |count|
    c = [count, c].max
end
ans = "NO" if c > (n + 1) / 2
puts ans

a=gets.chomp.to_i
b=gets.chomp.split(" ")
if a==1
puts "YES"
elsif a!=1 && b.uniq.length==1
puts "NO"

else

k=Hash.new(0)
b.each do |i|
k["#{i}"]+=1
end
l=k.values.sort.reverse[0]
if a-l>=l-1
puts "YES"
else
puts "NO"
end

end

$n, $a = gets.to_i, gets.split(' ').map { |x| x.to_i }
$b = []
$a.group_by{ |x| x }.each_value{ |x| $b << x.length }
$k = (if $a.length % 2 == 0 then $a.length / 2 else $a.length / 2 + 1 end)
puts (if $b.any? { |x| x > $k } then "NO" else "YES" end)

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
count = Hash.new(0)

arr.each do |a|
	count[a] += 1
end

ans = true
count.values.each do |c|
	unless c <= (n+1)/2
		ans = false
		break
	end
end

puts ans ? "YES" : "NO"

# count_arr = count.values.sort.reverse
# # puts "#{count_arr}"
# (0..count_arr.length-2).each do |i|
# 	j = i + 1
# 	puts "#{count_arr}"
# 	next if count_arr[i] == 0
# 	while j <= count_arr.length - 1
# 		if count_arr[i] <= count_arr[j]
# 			count_arr[i] = 0
# 			count_arr[j] -= count_arr[i]
# 			break
# 		else
# 			count_arr[i] -= count_arr[j]
# 			count_arr[j] = 0
# 		end
# 		j += 1
# 	end
# end
# count_arr.reject!{|a| a == 0}
# puts "#{count}"
# puts "#{count_arr}"
# if count_arr[0] > 1
# 	puts "NO"
# else
# 	puts "YES"
# end

#!/usr/bin/ruby -w
n = (gets.to_i + 1) >> 1
x = gets.split;
x.each do |i|
    if x.count(i) > n
        puts "NO"
        exit
    end
end
puts "YES"

def solve
	h = {}
	gets
	a = gets.chomp.split(/ /)
	a.each do |x| h[x] = if h[x] == nil then 1 else h[x] + 1 end end
	h.each do |x, y| return 'NO' if y > a.length/2 + a.length%2 end
	'YES'
end

print solve

def div_ceil(x, y)
  x / y + x % y
end

n = div_ceil(gets.chomp.to_i, 2)
puts(gets.chomp.split.map { |x| x.to_i }.inject(Hash.new(0)) { |h, e| h[e] += 1; h }.values.any? { |v| v > n } ? 'NO' : 'YES')

n = gets[0..-2].to_i
arr = gets.split(' ')
a = Array.new(n) { |i| arr[i].to_i }
mark = Array.new(1001, 0)
for i in 0...n do
  mark[a[i]] += 1
end

max = mark.max
if max * 2 - n >= 2
  print "NO"
else
  print "YES"
end

#!/usr/bin/env ruby

n = (gets.to_i + 1) >> 1
x = gets.split.collect{ |i| i.to_i }
x.each do |i|
	if x.count(i) > n
		puts "NO"
		exit
	end
end

puts "YES"

at_exit do
  n = get_numbers_from_line[0]
  numbers = get_numbers_from_line[0...n]

  iter = 0
  while true
    i = get_incorrect_number_index(numbers)
    if i.nil?
      puts "YES"
      break
    else
      unless move_number_to_correct_place(i, numbers)
        puts "NO"
        break
      end
    end
    iter += 1
    if iter > 1000
      puts "NO"
      break
    end
  end
end

def get_incorrect_number_index(numbers)
  (0...numbers.length).each do |i|
    return i if numbers[i] == numbers[i+1] || (i-1 >= 0 && numbers[i] == numbers[i-1])
  end
  nil
end

def move_number_to_correct_place(index, numbers)
  number = numbers.delete_at(index)
  (0..numbers.length).each do |i|
    if numbers[i] != number && (i-1 >= 0 && numbers[i-1] != number)
      numbers.insert i, number
      return true
    end
  end
  false
end

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

n = gets.to_i
a = gets.split.map { |x| x.to_i }
max = 0
1.upto(1000) do |i|
	max = [max, a.count(i)].max
end

if max <= (n + 1) / 2
	puts 'YES'
else
	puts 'NO'
end

c = Hash.new(0)

n = gets.to_i

max = -1

gets.split.map(&:to_i).each do |i|
	c[i]+=1
	max = i if c[i]>max
end

can = max<=(n/2.0).ceil

puts can ? 'YES' : 'NO'

n = gets.to_i
a = gets.split(' ').map { |i| i.to_i }
cnt = []
maxs = 0
a.each do |i|
	cnt[i] = 0 if cnt[i].nil?
	cnt[i] += 1
	maxs = [cnt[i], maxs].max
end
if maxs > n / 2 && n > 1
	puts "NO"
else
	puts "YES"
end

n = gets.to_i
cnt = [], maxs = 0
gets.split(' ').map do |i|
    i = i.to_i
    cnt[i] = cnt[i].nil? ? 0 : cnt[i] + 1
    maxs = [cnt[i], maxs].max
end
if maxs > (n + 1) / 2 && n > 1 then puts "NO"
else puts "YES"
end

n = gets.to_i
a = gets.split #.map{|_|_.to_i}
h = {}

a.each{|i|
  if h[i] then
    h[i]+=1
  else
    h[i]=1
  end
}
x = n%2 ? n/2 : (n+1)/2
r = true
h.each{|k,v|
  r = false if v > x
}

puts (r ? 'YES':'NO' )

n = gets.to_i
a = gets.split #.map{|_|_.to_i}
h = {}

a.each{|i|
  if h[i] then
    h[i]+=1
  else
    h[i]=0
  end
}
x = n%2 ? n/2 : (n+1)/2
r = true
h.each{|k,v|
  r = false if v > x
}

puts (r ? 'YES':'NO' )

n = gets.to_i
a = gets.split.map{|_|_.to_i}

r = true
(n-1).times{|i|
  r = true
  if a[i] == a[i+1] then
    b = a
    t = b[i+1]
    b[i+1] = b[i]
    b[i] = t
    (n-1).times{|j|
      r = false if b[j] == b[j+1]
    }
  end
}

puts (r ? 'YES':'NO' )

def distinct?( elements )
    (1...elements.length).each do |i|
        return false if elements[i - 1] == elements[i]
    end
    true
end
n = gets.chomp.to_i
elements = gets.chomp.split(/ /).map(&:to_i)
ans = "YES"
(1...n).each do |i|
    elements[i - 1], elements[i] = elements[i], elements[i - 1] if elements[i - 1] != elements[i]
end
ans = "NO" if !distinct? elements
puts ans

n = gets.chomp.to_i
elements = gets.chomp.split(/ /).map(&:to_i)
duplicates = []
(1...n).each do |i|
    duplicates += [i - 1, i] if elements[i - 1] == elements[i]
end
duplicates.uniq!
ans = "YES"
ans = "NO" if duplicates.length > (n + 1) / 2
puts ans

n = gets.chomp.to_i
elements = gets.chomp.split(/ /).map(&:to_i)
duplicates = []
(1...n).each do |i|
    duplicates += [i - 1, i] if elements[i - 1] == elements[i]
end
ans = "YES"
ans = "NO" if duplicates.length > (n + 1) / 2
puts ans

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
count = Hash.new(0)

arr.each do |a|
	count[a] += 1
end

count_arr = count.values.sort.reverse
(0..count_arr.length-2).each do |i|
	j = i + 1
	while j <= count_arr.length - 1
		if count_arr[i] <= count_arr[j]
			count_arr[i] = 0
			count_arr[j] -= count_arr[i]
			break
		else
			count_arr[i] -= count_arr[j]
			count_arr[j] = 0
		end
		j += 1
	end
end
count_arr.reject!{|a| a == 0}
# puts "#{count_arr}"
if count_arr[0] > 1
	puts "NO"
else
	puts "YES"
end

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
count = Hash.new(0)

arr.each do |a|
	count[a] += 1
end

count_arr = count.values
(0..count_arr.length-2).each do |i|
	j = i + 1
	while j <= count_arr.length - 1
		if count_arr[i] <= count_arr[j]
			count_arr[i] = 0
			count_arr[j] -= count_arr[i]
			break
		else
			count_arr[i] -= count_arr[j]
			count_arr[j] = 0
		end
		j += 1
	end
end
count_arr.reject!{|a| a == 0}
if count_arr[0] > 1
	puts "NO"
else
	puts "YES"
end

def solve
	h = {}
	gets
	a = gets.chomp.split(/ /)
	a.each do |x| h[x] = if h[x] == nil then 1 else h[x] + 1 end end
	h.each do |x, y| return 'NO' if y > a.length/2 + a.length%2 end
	'YES'
end

p solve

n = gets[0..-2].to_i
arr = gets.split(' ')
a = Array.new(n) { |i| arr[i].to_i }
mark = Array.new(1001, 0)
ans = true
for i in 0...n do
  mark[a[i]] += 1
  if mark[a[i]] > n / 2
    ans = false
    break
  end
end

max = mark.max
if max * 2 - n >= 2
  print "NO"
else
  print "YES"
end


gets.to_i.times do
  d, h, n = gets.split.map &:to_i
  a = gets.split.map(&:to_i).zip(gets.split.map(&:to_i)).sort{ |a, b|
    a[0] <=> b[0]
  }
  catch :no do
    i = 0
    while i < n
      h -= (a[i][1] + d - 1)/d*a[i][0]
      if h <= 0 && (i < n - 1 || h <= -a[i][0])
        puts "NO"
        throw :no
      end
      i += 1
    end
    puts "YES"
  end
end

gets.to_i.times do
  aa,bb,n=gets.split.map(&:to_i)
  a=gets.split.map(&:to_i)
  b=gets.split.map(&:to_i)
  bb+=a.max
  n.times do |i|
    bb-=a[i]*((b[i]-1)/aa+1)
    if bb<=0
      break
    end
  end
  if bb<=0
    puts "NO"
  else
    puts "YES"
  end
end


require 'set'; require 'prime'
INF=Float::INFINITY; MOD=10**9+7
t = gets.chomp.to_i
a = gets.chomp.split.map(&:to_i)

a.each do |e|
  ee = e % 14
  ee += 14
  if e > 14 && 15 <= ee && ee <= 20
    puts 'YES'
  else
    puts 'NO'
  end
end

require 'set'; require 'prime'
INF=Float::INFINITY; MOD=10**9+7
t = gets.chomp.to_i
a = gets.chomp.split.map(&:to_i)

a.each do |e|
  ee = e % 14
  ee += 14
  if e <= 14 || (15 <= ee && ee <= 20)
    puts 'YES'
  else
    puts 'NO'
  end
end

require 'set'; require 'prime'
INF=Float::INFINITY; MOD=10**9+7
t = gets.chomp.to_i
a = gets.chomp.split.map(&:to_i)

a.each do |e|
  e = e % 14
  e += 14
  if 15 <= e && e <= 20
    puts 'YES'
  else
    puts 'NO'
  end
end


n = gets.to_i

n.times do |r|
  string = gets.chomp
  freq = Array.new(26, 0)

  string_size = string.size

  string_size.times do |i|
    freq[string[i].ord - "a".ord] += 1
  end

  chain_count = 0
  in_chain = false
  printed = false
  freq.each do |value|
    if value > 1
      puts 'No'
      printed = true
      break
    end
    if value == 1
      if !in_chain
        chain_count += 1
        in_chain = true
      end
      if chain_count >= 2
        puts 'No'
        printed = true
        break
      end
    end
    if value == 0
      if in_chain
        in_chain = false
      end
    end
  end
  if !printed && chain_count <= 1
    puts 'Yes'
  end
end

require 'set'
alph = "abcdefghijklmnopqrstuvwxyz"
t = gets.chomp.to_i
t.times do |testcase|
  inp = gets.chomp
  s = []
  inp.size.times do |i|
    s.push(inp[i])
  end
  s = s.sort
  flag = true
  (inp.size-1).times do |i|
    if alph.index(s[i]) != alph.index(s[i+1])-1
      flag = false
      break
    end
  end
  if flag
    puts "yES"
  else
    puts "nO"
  end
end

n = gets.to_i
n.times do
  ss = gets.chomp.chars.map(&:ord).sort
  unless ss.size == ss.uniq.size
    puts 'No'
    next
  end

  ans = 'Yes'
  (0..ss.size-2).each do |i|
    if ss[i] + 1 != ss[i+1]
      ans = 'No'
    end
  end
  puts ans
end

t = "abcdefghijklmnopqrstuvwxyz".split("")
gets.to_i.times do
    s = gets.chomp.split("").sort
    n = s.size
    f = false
    (n-1).times do |i|
        if s[i] == s[i+1]
            puts "No"
            f = true
            break
        end
    end
    next if f
    (27-n).times do |i|
        if s == t[i,n]
            puts "Yes"
            f = true
            break
        end
    end
    next if f
    puts "No"
end

alphabet = "abcdefghijklmnopqrstuvwxyz"
t = gets.to_i

t.times do
  word = gets.chomp
  puts alphabet.include?(word.chars.sort.join) ? "Yes" : "No"
end

n = gets.strip.to_i
inputs = []

(1..n).each do |i|
  inputs << gets.strip
end

res = ["Yes"]*n
inputs.each_with_index do |a, index|
  codes = a.chars.map(&:ord).sort
  (1..codes.size-1).each do |i|
    if codes[i] - codes[i-1] != 1
      res[index] = "No"
      break
    end

  end
end

res.each do |r|
  puts r
end

n = gets.to_i
n.times do
  s = gets.strip.split('').sort.join
  puts s == (s[0]..(2..s.size).inject(s[0]) { |a| a.next }).to_a.join ? 'Yes' : 'No'
end

n = gets.to_i

n.times do |r|
  string = gets.chomp
  freq = Array.new(26, 0)

  string_size = string.size

  string_size.times do |i|
    freq[string[i].ord - "a".ord] += 1
  end

  chain_count = 0
  in_chain = false
  printed = false
  freq.each do |value|
    if value > 1
      p 'No'
      printed = true
      break
    end
    if value == 1
      if !in_chain
        chain_count += 1
        in_chain = true
      end
      if chain_count >= 2
        p 'No'
        printed = true
        break
      end
    end
    if value == 0
      if in_chain
        in_chain = false
      end
    end
  end
  if !printed && chain_count <= 1
    puts 'Yes'
  end
end

n = gets.to_i

n.times do |r|
  string = gets.chomp
  freq = Array.new(26, 0)

  string_size = string.size

  string_size.times do |i|
    freq[string[i].ord - "a".ord] += 1
  end

  chain_count = 0
  in_chain = false
  printed = false
  freq.each do |value|
    if value > 1
      p "No"
      printed = true
      break
    end
    if value == 1
      if !in_chain
        chain_count += 1
        in_chain = true
      end
      if chain_count >= 2
        p "No"
        printed = true
        break
      end
    end
    if value == 0
      if in_chain
        in_chain = false
      end
    end
  end
  if !printed && chain_count <= 1
    p "Yes"
  end
end

n = gets.to_i
n.times do
  ss = gets.chomp.chars.map(&:ord).sort
  puts 'No' unless ss.size == ss.uniq.size

  ans = 'Yes'
  (0..ss.size-2).each do |i|
    if ss[i] + 1 != ss[i+1]
      ans = 'No'
    end
  end
  puts ans
end

n = gets.strip.to_i
inputs = []

(1..n).each do |i|
  inputs << gets.strip
end

res = ["Yes"]*n
inputs.each_with_index do |a, index|
  codes = a.chars.map(&:ord).sort
  (1..codes.size-1).each do |i|
    if codes[i] - codes[i-1] != 1
      res[index] = "No"
      break
    end

  end
end

res.each do |r|
  p r
end


k=gets.split[1].to_i
a=gets.split[0].bytes.to_a
n=a.length
cnt=Array.new(10,0)
n.times{|i|
    a[i]-=48
    cnt[a[i]]+=1
}
mincost=99999999
res=[]
10.times{|d|
    cost=0
    count=cnt[d]
    1.upto(9){|i|
        if count>=k then
            break
        end
        if d+i<=9 then
            if cnt[d+i]+count>=k then
                cost+=i*(k-count)
                count=k
            else
                count+=cnt[d+i]
                cost+=cnt[d+i]*i
            end
        end
        if d-i>=0 then
            if cnt[d-i]+count>=k then
                cost+=i*(k-count)
                count=k
            else
                count+=cnt[d-i]
                cost+=cnt[d-i]*i
            end
        end
    }
    if cost<mincost then
        mincost=cost
        res=[]
    end
    if cost==mincost then
        ans=Array.new(n,0)
        used=Array.new(n,0)
        count=0
        0.upto(9){|i|
            tar=d+i
            n.times{|j|
                if count>=k then
                    break
                end
                if used[j]==0 && a[j]==tar then
                    ans[j]=d
                    count+=1
                    used[j]=true
                end
            }
            tar=d-i
            (n-1).downto(0){|j|
                if count>=k then
                    break
                end
                if used[j]==0 && a[j]==tar then
                    ans[j]=d
                    count+=1
                    used[j]=true
                end
            }
        }
        n.times{|j|
            if used[j]==0 then
                ans[j]=a[j]
            end
        }
        res.push(ans)
    end
}
puts mincost
ans=res.min
ans.each{|i| print i}
puts


a=gets.chomp
b=gets.chomp
ahash=Hash.new(0)
bhash=Hash.new(0)
a.each_char {|i| ahash[i]+=1}
b.each_char {|i| bhash[i]+=1}
c=ahash.keys
yay=0
whoops=0

c.each do |i|
if ahash[i]<=bhash[i]
yay+=ahash[i]
bhash[i]-=ahash[i]
ahash[i]=0
else
yay+=bhash[i]
ahash[i]-=bhash[i]
bhash[i]=0
end
end

c.each do |i|
if ahash[i]>0

if i==i.downcase
k=bhash[i.upcase]
if ahash[i]>=k
whoops+=k
bhash[i.upcase]=0
else
whoops+=ahash[i]
bhash[i.upcase]-=ahash[i]
end
else
k=bhash[i.downcase]
if ahash[i]>=k
whoops+=k
bhash[i.downcase]=0
else
whoops+=ahash[i]
bhash[i.downcase]-=ahash[i]
end
end

end
end

puts "#{yay} #{whoops}"

s = gets.chomp
t = gets.chomp
ss = Array.new(256, 0)
tt = Array.new(256, 0)
s.each_byte do |x|
  ss[x] += 1
end
t.each_byte do |x|
  tt[x] += 1
end
su = ss['A'.ord..'Z'.ord]
sl = ss['a'.ord..'z'.ord]
tu = tt['A'.ord..'Z'.ord]
tl = tt['a'.ord..'z'.ord]

a = 0
b = 0
26.times do |i|
  x = [su[i], tu[i]].min
  a += x
  tu[i] -= x
  su[i] -= x
  x = [sl[i], tl[i]].min
  a += x
  tl[i] -= x
  sl[i] -= x
end
26.times do |i|
  x = [su[i], tl[i]].min
  b += x
  tl[i] -= x
  su[i] -= x
  x = [sl[i], tu[i]].min
  b += x
  tu[i] -= x
  sl[i] -= x
end

puts "#{a} #{b}"

s = gets.chomp.chars.sort
t = gets.chomp.chars.sort
ss = Array.new(58, 0)
tt = Array.new(58, 0)
s.each do |x|
  ss[x.ord - 65] += 1
end
t.each do |x|
  tt[x.ord - 65] += 1
end
su = ss[0..25]
sl = ss[32..57]
tu = tt[0..25]
tl = tt[32..57]

a = 0
b = 0
26.times do |i|
  x = [su[i], tu[i]].min
  a += x
  tu[i] -= x
  su[i] -= x
  x = [sl[i], tl[i]].min
  a += x
  tl[i] -= x
  sl[i] -= x
end
26.times do |i|
  x = [su[i], tl[i]].min
  b += x
  tl[i] -= x
  su[i] -= x
  x = [sl[i], tu[i]].min
  b += x
  tu[i] -= x
  sl[i] -= x
end

puts "#{a} #{b}"

s = gets
t = gets

hs = Hash.new(0)
s.split('').each { |c| hs[ c ] += 1 }

ht = Hash.new(0)
t.split('').each { |c| ht[ c ] += 1 }

a = 0
b = 0

hs.each do |k, v|
  m = [hs[k], ht[k]].min
  a += m
  hs[k] -= m
  ht[k] -= m
end

hs.each do |k, v|
  m = [hs[k], ht[ k.downcase ]].min
  b += m
  hs[ k ] -= m
  ht[ k.downcase ] += m

  m = [hs[k], ht[ k.upcase ]].min
  b += m
  hs[ k ] -= m
  ht[ k.upcase ] += m
end

puts "#{a - 1} #{b}"

s = gets.chomp
t = gets.chomp
n = t.size()
m = s.size()
a=Array.new(300).map{|e| e=0 }
b=Array.new(300).map{|e| e=0 }
  for i in 0..n-1
     a[t[i].ord] += 1
  end
    for i in 0..m-1
     b[s[i].ord] += 1
  end
yra = 0
oops = 0
for i in 0..200
     if a[i] > 0 && b[i] > 0
       a1 = a[i]
       b1 = b[i]
      # puts (a1.to_s + ' '+ b1.to_s + ' ' + i.to_s)
       yra += [a[i], b[i]].min
       a[i] -= b1
       b[i] -= a1
     end
  end
  for i in 64..91
    if (a[i] > 0 && b[i + 32] > 0)
       a1 = a[i]
       b1 = b[i + 32]
       oops += [a1, b1].min
       a[i] -= b1
       b[i] -= a1
    end
  end
 for i in 97..123
    if (a[i] > 0 && b[i - 32] > 0)
       a1 = a[i]
       b1 = b[i - 32]
       oops += [a1, b1].min
       a[i] -= b1
       b[i] -= a1
    end
  end
  s2 = ""
  s2 = yra.to_s + " " + oops.to_s
puts (s2)

if __FILE__ == $0
  END { main }
end

def main
  s = gets.strip
  t = gets.strip

  s_freqs = get_letter_freqs(s)
  t_freqs = get_letter_freqs(t)

  yay_count = get_yay_count(s_freqs, t_freqs)
  whoops_count = get_whoops_count(s_freqs, t_freqs)

  puts [yay_count, whoops_count].join(" ")
end

def get_letter_freqs(s)
  h = Hash.new(0)
  s.chars.each do |ch|
    h[ch] += 1
  end
  h
end

def get_yay_count(s_freqs, t_freqs)
  count = 0
  s_freqs.each do |ch, freq|
    freq.times do
      if t_freqs[ch] > 0
        s_freqs[ch] -= 1
        t_freqs[ch] -= 1
        count += 1
      end
    end
  end
  count
end

def get_whoops_count(s_freqs, t_freqs)
  count = 0
  s_freqs.each do |s_ch, s_freq|
    t_ch = s_ch.swapcase
    s_freq.times do
      if t_freqs[t_ch] > 0
        s_freqs[s_ch] -= 1
        t_freqs[t_ch] -= 1
        count += 1
      end
    end
  end
  count
end

a=gets.chomp
b=gets.chomp
ahash=Hash.new(0)
bhash=Hash.new(0)
a.each_char {|i| ahash[i]+=1}
b.each_char {|i| bhash[i]+=1}
c=ahash.keys
yay=0
whoops=0
c.each do |i|
if ahash[i]<=bhash[i]
yay+=ahash[i]
else
yay+=bhash[i]
if i=i.downcase
p=(ahash[i]-bhash[i])
k=bhash[i.upcase]
if p>=k
whoops+=k
else
whoops+=p
end
else
p=(ahash[i]-bhash[i])
k=bhash[i.dowbcase]
if p>=k
whoops+=k
else
whoops+=p
end
end
end
end

puts "#{yay} #{whoops}"

a=gets.chomp
b=gets.chomp
ahash=Hash.new(0)
bhash=Hash.new(0)
a.each_char {|i| ahash[i]+=1}
b.each_char {|i| bhash[i]+=1}
c=ahash.keys
yay=0
whoops=0
c.each do |i|
if ahash[i]<=bhash[i]
yay+=ahash[i]
else
yay+=bhash[i]
whoops+=(ahash[i]-bhash[i])
end
end
puts "#{yay} #{whoops}"

a=gets.chomp
b=gets.chomp
ahash=Hash.new(0)
bhash=Hash.new(0)
a.each_char {|i| ahash[i]+=1}
b.each_char {|i| bhash[i]+=1}
c=ahash.keys
yay=0
whoops=0
c.each do |i|
if ahash[i]<=bhash[i]
yay+=ahash[i]
else
yay+=bhash[i]
if i==i.downcase
p=(ahash[i]-bhash[i])
k=bhash[i.upcase]
if p>=k
whoops+=k
else
whoops+=p
end
else
p=(ahash[i]-bhash[i])
k=bhash[i.downcase]
if p>=k
whoops+=k
else
whoops+=p
end
end
end
end

puts "#{yay} #{whoops}"

if __FILE__ == $0
  END { main }
end

def main
  s = gets.strip
  t = gets.strip

  s_freqs = get_letter_freqs(s)
  t_freqs = get_letter_freqs(t)

  yay_count = get_yay_count(s_freqs, t_freqs)
  whoops_count = get_whoops_count(s_freqs, t_freqs)

  puts [yay_count, whoops_count].join(" ")
end

def get_letter_freqs(s)
  h = Hash.new(0)
  s.chars.each do |ch|
    h[ch] += 1
  end
  h
end

def get_yay_count(s_freqs, t_freqs)
  count = 0
  s_freqs.each do |ch, freq|
    freq.times do
      if t_freqs[ch] > 0
        s_freqs[ch] += 1
        t_freqs[ch] -= 1
        count += 1
      end
    end
  end
  count
end

def get_whoops_count(s_freqs, t_freqs)
  count = 0
  s_freqs.each do |s_ch, s_freq|
    t_ch = s_ch.swapcase
    s_freq.times do
      if t_freqs[t_ch] > 0
        s_freqs[s_ch] += 1
        t_freqs[t_ch] -= 1
        count += 1
      end
    end
  end
  count
end


number = gets.to_i

if number % 2 == 0
	l = number / 2
	limit = l - 1
	for counter in 0..limit
		print "1"
	end
end
if number % 2 == 1
	l = number / 2
	limit = l - 2;
	print "7"
	for counter in 0..limit
		print "1"
	end
end

r=->{gets.split.map &:to_i}
ns=r[]
n = ns[0]
if n % 2 == 0 then
	puts "1"*(n/2)
else
	puts "7" + "1"*(n/2-1)
end

x=gets.to_i
y=x%2
x=x/2-y
puts "7"*y+"1"*x

a = gets.chomp.to_i
b = a / 2
c = a % 2

if c == 1
    puts '7' + '1' * (b - 1)
else
    puts '1' * b
end

n = gets.chomp.to_i
if n % 2 == 1
    print 7
    n -= 3
end
i = 0
while i < n do
    i += 2
    print 1
end

i = gets.to_i
if i % 2 == 0
    c = i / 2
    answer = ""
    for counter in 1..c
        answer += "1"
    end
else
    c = i / 2 - 1
    answer = "7"
    for counter in 1..c
        answer += "1"
    end
end
puts answer

n = gets.to_i
if (n % 2 == 1)
    print(7)
    n = n - 3
end
while (n > 0)
    print(1)
    n = n - 2
end

#!/bin/ruby
t = gets.strip.to_i
#for a0 in (0..t-1)
#    n = gets.strip.to_i
#end
s=""
b=t
while t>7
    t-=2
    s.concat("1")
end
if t==2
   s="1"
end
if t==3
    s="7"
end
if t==4
    s="11"
end
if t==5
    s="71"
end
if t==6
    s.concat("1")
    s.concat("1")
    s.concat("1")
end
if t==7
    s="7"
    while b>7
        b-=2
        s.concat("1")
    end
    s.concat("1")
    s.concat("1")
    #s.concat("1")
    #s[0]=7
end

print s,"\n"

a = gets
a = a.to_i
if a > 1
    if a % 2 == 1
        print 7
        a -=3
    end
end
while a > 1
    print 1
    a -= 2
end

x = gets.chomp.to_i
s = ""
if(x % 2 == 0)
	while(x > 0)
		s += "1"
		x-=2
	end
end
if x % 2 == 1
	s += "7"
	x-=3
	while(x > 0)
		s += "1"
		x-=2
	end
end
puts s

n = gets.to_i

ans = "1" * (n/2)
if n % 2 == 1
        ans[0]="7"
end

puts ans

n = gets.to_i
str = ""
if n % 2 > 0
    print 7
else
    print 1
end

n -= 2
n /= 2
n.times do
   print 1
end

num = gets.to_i

rem = num % 2
if rem == 1
    if num > 2
    print 7
    num = num - 3
end
end
while num>0
print 1
num = num-2;
end

a = gets.to_i

if a % 2 == 1
    print '7'
    a -= 3
end

a = (a/2).to_i

for i in 1..a
    print 1
end

n1 = gets.chomp
n = n1.to_i
c = "1"*(n/2)
d = "1"*(n/2 -1)
if n%2==0
  print c
else
  print "7"
  print d
end

# your code goes here
r = gets.strip.split(/\s+/).map(&:to_i)
t = r[0]
if t % 2 == 1
	puts '7' + ('1' * ((t - 3) / 2))
else
	puts '1' * (t / 2)
end

c = Integer(gets)
s = ""
if c % 2 == 1
	s += '7'
	c -= 3
end
(c/2).times do
	s += '1'
end
puts s

n = gets.to_i
if n % 2 == 1 then
	res = '7'
	n -= 3
else
	res = '1'
	n -= 2
end
n = n / 2
s = '1'*n
res += s
puts res

n = gets
n = n.to_i
if n % 2 == 0
    print '1' * (n / 2)
else
    print '7' + '1' * (n / 2 - 1)
end

n=gets.chomp.to_i
z=n/2
t=n%2
ans=''
if t==1
 z=z-1
 ans+='7'
end
for i in 1..z
 ans+='1'
end
puts ans

n = gets.to_i
if (n & 1 != 0) then
    print "7"
    n = n-2
end
(1..n/2).each do |i|
    print "1"
end

n = gets
n = n.to_i
if n % 2 == 0
    print '1' * (n / 2)
else
    if n == 3
        print '17'
    else
        print '1' * ((n / 2) - 1) + '7'
    end
end

n = gets
n = n.to_i
if n % 2 == 0
    print '1' * (n / 2)
else
    print '1' * (n / 2 - 1) + '7'
end

c = Integer(gets)
while c >= 6
	print('9')
	$stdout.flush
	c -= 6
end
if c >= 3
	print('7')
	$stdout.flush
elsif c == 2
	print('1')
	$stdout.flush
end
puts ''

c = Integer(gets)
if c == 3
	print '7'
	$stdout.flush
else
	(c/2).times do
		print '1'
		$stdout.flush
	end
end

a=[6,2,5,5,4,5,6,3,7,6]
n=gets.chomp.to_i
ans=0
while n>=2 do
 i=9
 while i>=0 do
  if a[i]<=n
   ans+=ans*10+i
   n=n-a[i]
  end
  i=i-1
 end
end
puts ans


n = gets.to_i
times = Array.new(n * 2, 0)
for i in 0..n-1
    v = gets.split(":")
    t = v[0].to_i * 60 + v[1].to_i
    times[i*2] = t
    times[i*2+1] = t + 1
end

times = times.sort
#puts times

tf = -1
tl = 0
res = 0
for i in 0..2*n-1
    t = times[i]
    if tf == -1
        tf = t
    elsif res < t - tl
            res = t - tl
    end
    tl = t
    #puts res
end
if res < 24 * 60 - tl + tf
    res = 24 * 60 - tl + tf
end
if (res / 60) < 10
    print "0"
end
print (res / 60).to_s
print ":"
if (res % 60) < 10
    print "0"
end
print (res % 60).to_s

def strtime(str)
  h = str.split(':')[0].to_i
  m = str.split(':')[1].to_i
  h * 60 + m
end

n = STDIN.gets.chomp!.to_i
t = (1..n).map { |i| strtime(STDIN.gets.chomp!) }
t.sort!

ans = 0
(0...n).map do |i|
  d = t[(i + 1) % n] - t[i] - 1
  d = d + 24 * 60 if d < 0
  ans = [ans, d].max
end

printf("%02d:%02d\n", ans / 60, ans % 60)

n = gets.to_i
times = []

n.times do
    hh, mm = gets.split(':').map(&:to_i)
    time = hh * 60 + mm
    times.push(time)
    times.push(time + 24 * 60)
end

times.sort!

t = 0
ans = 0

times.each {
    |time|

    if time - t - 1 > ans
        ans = time - t - 1
    end

    t = time
}
hh, mm = ans / 60, ans % 60
hh, mm = "%02d" % hh, "%02d" % mm

puts "#{hh}:#{mm}"

n=gets.to_i
t=Array.new
for i in 1..n do
    s=gets
    t.push(s[0,2].to_i*60+s[3,2].to_i)
end
t.sort!
mx=1439-t[n-1]+t[0]
for i in 1..n-1 do
    if t[i]-t[i-1]-1>mx then mx=t[i]-t[i-1]-1 end
end
if mx/60<10 then print 0 end
print mx/60
print ':'
if mx%60<10 then print 0 end
print mx%60

n=gets.to_i
t=Array.new
for i in 1..n do
    s=gets
    t.push(s[0,2].to_i*60+s[3,2].to_i)
end
t.sort!
mx=1439-t[n-1]+t[0]-1
if n==1 then mx+=1 end
for i in 1..n-1 do
    if t[i]-t[i-1]-1>mx then mx=t[i]-t[i-1]-1 end
end
if mx/60<10 then print 0 end
print mx/60
print ':'
if mx%60<10 then print 0 end
print mx%60

n = gets.to_i
times = Array.new(n * 2, 0)
for i in 0..n-1
    v = gets.split(":")
    t = v[0].to_i * 60 + v[1].to_i
    times[i*2] = t
    times[i*2+1] = t + 1
end

times = times.sort
#puts times

tf = -1
tl = 0
res = 0
for i in 0..2*n-1
    t = times[i]
    if tf == -1
        tf = t
    elsif res < t - tl
            res = t - tl
    end
    tl = t
    #puts res
end
if res < 24 * 60 - tl + tf
    res = 24 * 60 - tl + tf
end
print (res / 60).to_s + ":" + (res % 60).to_s

n = gets.to_i
times = Array.new(n * 2)
tf = -1
tl = 0
res = 200000
for i in 0..n-1
    v = gets.split(":")
    t = v[0].to_i * 60 + v[1].to_i
    if tf == -1
        tf = t
    elsif res > t - tl
            res = t - tl
    end
    tl = t + 1
end
if res > 24 * 60 - tl + tf
    res = 24 * 60 - tl + tf
end
print (res / 60).to_s + ":" + (res % 60).to_s


def f(a)
  b, *c = a
  c.each { |e| b |= e }

  b
end

n = gets.chomp.to_i
arr1 = gets.chomp.split.map(&:to_i)
arr2 = gets.chomp.split.map(&:to_i)
puts f(arr1) + f(arr2)