<?
error_reporting(0);
$n = intval(fgets(STDIN));
$score = array();
foreach (range(1, $n) as $i) {
  $dat = explode(' ', fgets(STDIN));
  $name[$i] = $dat[0];
  $gain[$i] = intval($dat[1]);
  $score[$name[$i]] += $gain[$i];
}
foreach ($score as  $val)
  if ($val > $best) $best = $val;
$second = array();
foreach (range(1, $n) as $i) {
  $second[$name[$i]] += $gain[$i];
  if ($score[$name[$i]] == $best && $second[$name[$i]] >= $best) break;
}

print $name[$i].chr(10);
?>

<?php

    $data = '';
    $inputData = fopen('php://stdin','r');
    while(!feof($inputData)) {
        $data .= fgetc($inputData);
    }
    fclose($inputData);

    $dataRaw = explode(PHP_EOL, $data);

    $resultNames = array();
    $resultScore = array();

    for($i=1; $i<=$dataRaw[0];$i++) {
        $tmp = explode(" ",$dataRaw[$i]);
        $resultNames[] = $tmp[0];
        $resultScore[] = $tmp[1];
    }

    $max = array();

    foreach($resultNames as $k=>$name){
        if(!isset($max[$name])) {
            $max[$name] = 0;
        }

        $addScore = intval($resultScore[$k]);
        $max[$name] += $addScore;
    }

    $maxScore = max($max);

    $winnerCandidates = $max = array();

    foreach($resultNames as $k=>$name){

        if(!isset($max[$name])) {
            $max[$name] = 0;
        }

        $addScore = intval($resultScore[$k]);
        $max[$name] += $addScore;

        if($max[$name] >= $maxScore) {
            $winnerCandidates[$name] = 0;
        }
    }

    //var_dump($winnerCandidates, $max, $maxScore);

    foreach($winnerCandidates as $name=>$result) {
        if($max[$name]>=$maxScore) {
            exit($name);
        }
    }

<?php

if( STDIN ) {
	$i = 0;
    $count = 0;
    $stack = array();
    $max = 0;
    $maxItems = array();
    $loop = true;
    $inputs = array();
    while ($loop) {
        $line = trim(fgets(STDIN)); // stream_get_line(STDIN, 4096, "n");
        if($line) {
            if ($i == 0) {
                $count = $line;
            } else {
                $data = explode(" ", $line);
                $name = $data[0];
                $score = (int)$data[1];
                $inputs[] = array($name,$score) ;

                if (!isset($stack[$name])) $stack[$name] = $score;
                else $stack[$name] += $score;

            }
        }

        if($i == $count) $loop = false;
        $i ++;
    }

    foreach($stack as $name => $score){
        if($score > $max) $max = $score;
    }

    foreach($stack as $name => $score){
        if($score == $max) $maxItems[$name] = 1;
    }

    $stack = array();

    foreach($inputs as $input){

        $name = $input[0];
        $score = $input[1];

        if (!isset($stack[$name])) $stack[$name] = $score;
        else $stack[$name] += $score;

        if(isset($maxItems[$name]) && $stack[$name] >= $max){
            echo $name;
            exit();
        }
    }

}

<?php
$fr = fopen("php://stdin", "r");
$fw = fopen("php://stdout", "w");
$players = array();
$current_round = 1;
$highest_result = 0;
$number_of_rounds = fgets($fr);

function saveResult($name, $result)
{
	global $players;
	global $number_of_rounds;
	global $current_round;
	global $highest_result;
	$added = false;

	$players[$name][$current_round] = $result;

	if($current_round == $number_of_rounds) {
		foreach($players as $p_name => $p_results) {
			$player_score = 0;

			foreach($p_results as $round_number => $round_result) {
				$player_score += $round_result;
			}

			$players[$p_name][$number_of_rounds + 1] = $player_score;
			if ($player_score > $highest_result) {
				$highest_result = $player_score;
			}
		}
	}

	$current_round++;
}

function findOverallWinner($winners_array, $winning_score)
{
	$temp_winners = array();
	global $number_of_rounds;
	$winning_round = $number_of_rounds + 1;

	foreach($winners_array as $w_name => $w_score) {
		if ($w_score[$number_of_rounds + 1] == $winning_score) {
			$temp_score = 0;

			foreach($w_score as $t_round => $t_score) {
				if ($t_round >= $winning_round) {
					break;
				}

				$temp_score += $t_score;

				if($temp_score >= $winning_score) {
					$winning_round = $t_round;
					$winner_name = $w_name;
				}
			}
		}
	}

	return $winner_name;
}

while(($line = fgets($fr)) !== false) {
	if (preg_match('/([a-z]+)\s+([\-0-9]+)/', $line, $matches)) {
		saveResult($matches[1], $matches[2]);
	}
}
fprintf($fw, findOverallWinner($players, $highest_result));

<?php
function _max($arr,$list) {
    $max = 0;
    $name = '';
    foreach ($arr as $k => $v) {
        if ($v['score'] > $max) {
            $max = $v['score'];
            $name = $k;
        } else if ($v['score'] == $max) {
            $sum1 = $sum2 = 0;
            $key1 = $key2 = 0;
            foreach ($list as $keys => $val) {
                if ($val[0] == $name) {
                    $sum1 += $val[1];
                }
                if ($sum1 >= $max) {
                    if (!$key1) {
                        $key1 = $keys+1;
                    }
                }

                if ($val[0] == $k) {
                    $sum2 += $val[1];
                }

                if ($sum2 >= $max) {
                    if (!$key2) {
                        $key2 = $keys+1;
                    }
                }
                if ($key1 && $key2) break;
            }
            if ($key1 > $key2) {
                $name = $k;
            }
        }
    }
    echo $name;
}

while ($row = fscanf(STDIN,"%s %i")) {
    $data[] = $row;
}

$line = array_shift($data);

$max = 0;
$name = '';
$list = [];
for ($i=0;$i<$line[0];$i++) {
    $list[$data[$i][0]]['score'] += $data[$i][1];
}

_max($list,$data);

<?php
$f = STDIN;
$n = fgets($f);

$score = [];
foreach (range(1, $n) as $i) {
    $dat = explode(' ', fgets(STDIN));
    $name[$i] = $dat[0];
    $gain[$i] = intval($dat[1]);
    $score[$name[$i]] += $gain[$i];
}
foreach ($score as  $val)
    if ($val > $best) $best = $val;
$second = array();
foreach (range(1, $n) as $i) {
    $second[$name[$i]] += $gain[$i];
    if ($score[$name[$i]] == $best && $second[$name[$i]] >= $best) break;
}

echo $name[$i];

<?php
$f = STDIN;
$n = fgets($f);

$plays = [];
$scores = [];
for ($i = 0; $i < $n; $i++) {
    list($name, $score) = explode(' ', fgets($f));
    $play = [
        'name' => $name,
        'score' => intval($score),
    ];
    $plays[] = $play;
    if (isset($scores[$play['name']])) {
        $scores[$play['name']] += $play['score'];
    } else {
        $scores[$play['name']] = $play['score'];
    }
}

$best = 0;
foreach ($scores as $score) {
    if ($score > $best) $best = $score;
}

$second = [];
foreach ($plays as $play) {
    $name = $play['name'];
    $score = $play['score'];
    if(isset($second[$name])) {
        $second[$name] += $score;
    } else {
        $second[$name] = $score;
    }
    if ($scores[$name] == $best && $second[$name] >= $best) {
        echo $name;
        break;
    }
}

<?php
$fp = fopen("php://stdin","r");
$n=0;
fscanf($fp,"%d",$n);

$player = array();
$player_hs = array();
for($i=0; $i<$n; $i++){
	fscanf($fp,"%s %d", $name, $score);
	if(isset($player[$name])){
		$player[$name] += $score;
		$player_hs[$name][] = array($player[$name],$i);

	}else{
		$player[$name] = $score;
		$player_hs[$name] = array(array($score,$i));
	}

}

$max = 0;
$winner = "";

$winner_list = array();

foreach($player as $key=>$value)
{
	if($value > $max){
		$winner_list = array($key);
		$max = $value;
	}else if($value == $max){
		$winner_list[] = $key;
	}
}

$min = $n;

foreach($winner_list as $name){
	for($i=0; $i<sizeof($player_hs[$name]); $i++){
		if($player_hs[$name][$i][0] >= $max){
			if($player_hs[$name][$i][1] < $min){
				$min = $player_hs[$name][$i][1];
				$winner = $name;
			}
		}
	}
}

echo $winner."\n";
?>

<?php
error_reporting("off");
//2A: Winner
$n = trim(fgets(STDIN));
$turns = array();
$scores = array();
for ($i = 0; $i < $n; $i++) {
    list($name, $score) = explode(" ", trim(fgets(STDIN)));
    $turns[] = $name.".".$score;
    if (isset($scores[$name])) {
        $scores[$name] += $score;
    } else {
        $scores[$name] = $score;
    }
}

arsort($scores);
$max = current($scores);

$playagain = array();

foreach ($turns as $value) {
    list($name, $score) = explode(".", $value);
    if (isset($playagain[$name])) {
        $playagain[$name] += $score;
    } else {
        $playagain[$name] = $score;
    }
    if ($playagain[$name] >= $max && $scores[$name] == $max) {
        $winner = $name;
        break;
    }
}

echo $winner;
?>

<?php

$rounds = intval(rtrim( fgets( STDIN ) ));
$scores = array();
for($i = 0; $i < $rounds; $i++){
	list($name, $score) = explode(" ", rtrim( fgets( STDIN )));
	if(!isset($scores[$name])){
		$scores[$name] = array(
			'score' => 0,
			'history' => array(),
		);
	}
	$scores[$name]['score'] += intval($score);
	$scores[$name]['history'][] = array('round' => $i, 'score' => $scores[$name]['score']);
}

uasort($scores, function($a, $b){
	if($a['score'] > $b['score']){
		return -1;
	} elseif($a['score'] == $b['score']){
		$ai = $bi = 0;
		foreach($a['history'] as $s){
			if($s['score'] >= $a['score']){
				$ai = $s['round'];
				break;
			}
		}
		foreach($b['history'] as $s){
			if($s['score'] >= $b['score']){
				$bi = $s['round'];
				break;
			}
		}
		return ($ai < $bi) ? -1 : 1;
	} else {
		return 1;
	}
});

$names = array_keys($scores);
echo reset($names);

<?php
fscanf(STDIN, "%d", $n);
$scores = array();
$historyNames = array();
$historyScores = array();
for ($i = 0; $i < $n; $i++) {
    fscanf(STDIN, "%s %d", $name, $score);
    $historyNames[$i] = $name;
    $historyScores[$i] = intval($score);
    $scores[$name] += intval($score);
}
$maxScore = 0;
foreach ($scores as $score)
    if ($score > $maxScore)
        $maxScore = $score;
$tmpScores = array();
for ($i = 0; $i < $n; $i++) {
    $tmpScores[$historyNames[$i]] += intval($historyScores[$i]);
    if ($scores[$historyNames[$i]] == $maxScore && $tmpScores[$historyNames[$i]] >= $maxScore)
        break;
}
printf("%s", $historyNames[$i]);
?>

<?php

$in = stream_get_contents( fopen( "php://stdin", 'r' ));

$in = explode( "\n", $in );

unset($in['0']);

foreach( $in as $key => $str ) {
    $istr = explode( " ", $str );
    $new[$istr[0]] += $istr[1];
}

$max = max($new);

foreach( $in as $str ) {
    $istr = explode( " ", $str );
    $new1[$istr[0]] += $istr[1];

    if ( $new1[$istr[0]] >= $max AND $new[$istr[0]] == $max ) {
        echo $istr[0];
        break;
    }
}

<?php

$in = stream_get_contents( fopen( "php://stdin", 'r' ));

$in = explode( "\n", $in );

unset($in['0']);

foreach( $in as $key => $str ) {
    $istr = explode( " ", $str );
    $new[$istr[0]] += $istr[1];
}

$max = max($new);

foreach( $in as $str ) {
    $istr = explode( " ", $str );
    $new1[$istr[0]] += $istr[1];

    if ( $new1[$istr[0]] >= $max AND $new[$istr[0]] == $max ) {
        echo $istr[0];
        break;
    }

}

<?php
    fscanf(STDIN, "%d\n", $n);
    $res = $end_scores = $list_winners = array();
    $end_max_score = 0;
    $winner = '';
    for ($i = 0; $i < $n; ++$i) {
        fscanf(STDIN, "%s %d\n", $name, $num);
        $res[$i] = array('name' => $name, 'score' => $num);
        if (!isset($end_scores[$name])) {
            $end_scores[$name] = 0;
        }
        $end_scores[$name] += $num;
    }

    $end_max_score = max($end_scores);

    foreach($end_scores as $player => $score) {
        if ($score == $end_max_score)
            $list_winners[] = $player;
    }

    $sum = array();
    foreach ($res as $data) {
        if (!isset($sum[$data['name']])) {
            $sum[$data['name']] = 0;
        }
        $sum[$data['name']] += $data['score'];
        if ($sum[$data['name']] >= $end_max_score && in_array($data['name'], $list_winners)) {
            echo $data['name'];
            exit();
        }
    }
?>

<?php
    $input = file_get_contents("php://stdin");
    $input = rtrim($input,"\r\n");
    $e = explode("\r\n",$input);

    function nameExists($name,$a)
    {
        foreach($a as $n)
        {
            if($name==$n)
            {
                return 1;
            }
        }
        return 0;
    }

    $a=array();
    $max = array();
    for($i=1;$i<count($e);$i++)
    {
      $ee = explode(" ",$e[$i]);
      $a[$ee[0]]+=$ee[1];
    }
    arsort($a);
    $p ="";

    foreach($a as $k=>$v)
    {
        $p=$v;
        break;
    }

    $tie = array();
    $i=0;
    foreach($a as $key=>$value)
    {
        if($value==$p)
        {
            $tie[$i++]=$key;
        }
    }

    $a = array();
   // var_dump($tie);
    for($i=1;$i<count($e);$i++)
    {
      $ee = explode(" ",$e[$i]);
      $a[$ee[0]]+=$ee[1];

      if($a[$ee[0]]>=$p&&nameExists($ee[0],$tie))
      {
          echo($ee[0]);
          exit;
      }
    }

    ?>

<?php

$f = fopen("php://stdin", 'r');
$s = fgets($f);

$max = array();

$n = intval($s);
for ($j = 0; $j < $n; $j++) {
    $s = trim(fgets($f));
    list($name, $point) = explode(' ', $s);
    $point = intval($point);

    if (!is_array($max[$name])) {
        $max[$name] = array(0 => 0);
    }

    $max[$name][0] += $point;
    $max[$name][$j] = $max[$name][0];
}

$max_names = array();
$m = -999999999;

$min_user = '';
$min_step = 1000;

foreach ($max as $name => $points) {
    if ($m < $points[0]) $m = $points[0];
}

foreach ($max as $name => $points) {
    if ($m == $points[0]) {
        $max_names[$name] = $points;
    }
}

foreach ($max_names as $name => $points) {
    $j = 1;

    while ($points[$j] < $m) {
        $j++;
        if ($j > 1000) break;
    }

    if ($min_step > $j) {
        $min_step = $j;
        $min_user = $name;
    }

}

print $min_user;

<?php
$input = file('php://stdin', FILE_IGNORE_NEW_LINES);
array_shift($input);
$a = array();
foreach ($input as $str) {
    list($name, $score) = explode(' ', $str);
    if (! isset($a[$name])) {
        $a[$name] = 0;
    }
    $a[$name] += $score;
}
$m = max($a);
$winnerNames = array_keys($a, $m);
if (count($winnerNames) == 1) {
    echo $winnerNames[0];
    exit;
}

$a = array();
foreach ($input as $str) {
    list($name, $score) = explode(' ', $str);
    if (! isset($a[$name])) {
        $a[$name] = 0;
    }
    $a[$name] += $score;
    if ($a[$name] >= $m && in_array($name, $winnerNames)) {
        echo $name;
        exit;
    }
}

<?php
	$data = array();

	$winName = '';

	fscanf(STDIN, "%d\n", $pCount);
	for($i = 0; $i < $pCount; $i++) {
    	fscanf(STDIN, "%s %d\n", $name, $score);
    	if(empty($data[$name])) {
    		$data[$name]['score'] = $score;
    	}
    	else $data[$name]['score'] += $score;
    	$data[$name]['log'][$i] = $score;
    }

    $maxScore = 0;
    $maxCheck = 0;

    foreach($data as $name => $value) {
    	if($data[$name]['score'] > $maxScore) {
    		$maxScore = $data[$name]['score'];
    		$winName = $name;
    		$testCheck = 0;
    		foreach ($data[$name]['log'] as $check => $s) {
    			$testCheck += $s;
    			if($testCheck >= $maxScore) {
    				$maxCheck = $check;
    				break;
    			}
    		}
    	}
    	elseif($data[$name]['score'] == $maxScore) {
    		$testCheck = 0;
    		foreach ($data[$name]['log'] as $check => $s) {
    			$testCheck += $s;
    			if($testCheck >= $maxScore) {
    				if($check < $maxCheck) {
    					$maxCheck = $check;
    					$winName = $name;
    				}
    				break;
    			}
    		}
    	}
    }

    printf("%s", $winName);
?>

<?php
    $input = explode("\n", trim(file_get_contents("php://stdin")));
    $count = $input[0];
    $ps = array_slice($input, 1, $count, true);
    $sorted = array();
    $scores= array();
    foreach($ps as $key => $pi){
        $fi = explode(' ', $pi);
        $sorted[$fi[0]] += (int)trim($fi[1]);
    }
    $max = max($sorted);
    foreach($ps as $key => $pi){
        $fi = explode(' ', $pi);
        $scores[$fi[0]] += (int)trim($fi[1]);
        if($scores[$fi[0]] >= $max && $sorted[$fi[0]] == $max){
                echo $fi[0];
                return;
        }
    }

<?php

function dd(...$args)
{
    vd(...$args);
    die;
}
function vd(...$args)
{
    var_dump(...$args);
}

define('TXTIN', fopen('input.txt', 'r'));
define('TXTOUT', fopen('output.txt', 'w'));

//$input = TXTIN;
$input = STDIN;
//$output = TXTOUT;
$output = STDOUT;

$t = [
    0 => [],
];
$maxPerson = null;

list($n) = fscanf($input, "%d\n");

for ($i = 1; $i <= $n; ++$i) {
    list($person, $points) = fscanf($input, "%s %d\n");

    $t[$i] = $t[$i - 1];

    if (isset($t[$i][$person])) {
        $t[$i][$person] += $points;
    } else {
        $t[$i][$person] = $points;
    }
}

$end = end($t);
$finalsMax = max($end);
$leaders = array_keys($end, $finalsMax);

//dd($t, $end, $finalsMax, $leaders);

foreach ($t as $k => $v) {
    foreach ($v as $person => $points) {
        if ($points >= $finalsMax && in_array($person, $leaders)) {
            $maxPerson = $person;
            break;
        }
    }

    if ($maxPerson) {
        break;
    }
}

//dd($t, $end, $finalsMax);

fwrite($output, $maxPerson);

<?php

class dat {
    public $name;
    public $point;
}

$fr = fopen("php://stdin", "r");
$n = fgets($fr);
$line = array();
$names = array();
$winner = array();

for($i=0;$i<$n;$i++)
{
    $data = fgets($fr);
    $array = explode(" ", $data);

    $line[$i]=new dat;
    $line[$i]->name = $array[0];
    $line[$i]->point = $array[1];

    if(isset($names[$array[0]]))
    {
        $names[$array[0]] = $names[$array[0]] + $array[1];
    }
    else
    {
        $names += array($array[0] => $array[1]);
    }
}

$max=0;
foreach ($names as $key => $val)
{
    if($max < $val)
        $max = $val;
}
unset($val);

$str;
$pnt;
foreach ($line as &$val)
{
    $str = $val->name;
    $pnt = $val->point;

    if(isset($winner[$str]))
    {
        $winner[$str] = $winner[$str] + $pnt;
    }
    else
    {
        $winner += array($str => $pnt);
    }

    if($winner[$str] >= $max && $names[$str] == $max)
    {
        echo $str;
        break;
    }

}
unset($val);

<?php

class person {
    public $name;
    public $point;
}

$fr = fopen("php://stdin", "r");
$n = fgets($fr);
$list = array();
$names = array();

for($i=0;$i<$n;$i++)
{
    $data = fgets($fr);
    $array = explode(" ", $data);

    $list[$i] = new person;
    $list[$i]->name = $array[0];
    $list[$i]->point = $array[1];

    if(isset($names[$array[0]]))
        $names[$array[0]] = $names[$array[0]] + $array[1];
    else
        $names += array($array[0] => $array[1]);
}

$max=0;
foreach ($names as $key => $val)
{
    if($max < $val)
        $max = $val;
}
unset($val);

$winner = array();
foreach ($list as &$val)
{
    $str = $val->name;
    $pnt = $val->point;

    if(isset($winner[$str]))
        $winner[$str] = $winner[$str] + $pnt;
    else
        $winner += array($str => $pnt);

    if($winner[$str] >= $max && $names[$str] == $max)
    {
        echo $str;
        break;
    }

}
unset($val);
fclose($fr);
?>

<?php
error_reporting(0);
$char_array=array();
$dir = fopen("php://stdin", 'r');
$datatxt = stream_get_contents($dir);

$text = explode("\n",$datatxt);

unset($text[0]);

$total = array();
$players = array();
foreach($text as $key => $str) {
if (strlen($str)>0){
$player = explode(" ",$str);
$players[] = $player;
if (isset($total[$player[0]])){
$total[$player[0]] += $player[1];
} else {
$total[$player[0]] = $player[1];
}
}
}
$higher_score = null;
$same_player = array();
$winner = "";
foreach($total as $key =>$value) {
if ($higher_score === null) {
$higher_score = $value;
}
if ($value > $higher_score) {
$higher_score = $value;
$winner = $key;
$same_player = array();
} else if ($value == $higher_score) {
$same_player[] = $winner;
$same_player[] = $key;
}
}
if (!empty($same_player)){
foreach($same_player as $key =>$value) {
$same_player_score[$value] = 0;
}
foreach($players as $key =>$value) {
if (in_array($value[0], $same_player)) {
$same_player_score[$value[0]]+= $value[1];
if ($same_player_score[$value[0]] >= $higher_score) {
$winner = $value[0];
break;
}
}
}
}
echo $winner;
?>

<?PHP

$fileIN = fopen("php://stdin", "r");
$fileOut = fopen("php://stdout", "w");
// $fileIN = fopen("in.txt", "r");
// $fileOut = fopen("out.txt", "w");
$n = fgets($fileIN);

$maxScore = 0;
$winner = "";

$scoremap = array();

$comitsname = array();
$comitsvalue = array();

for ($i = 0; $i < $n; ++$i) {
    $lineIn = fgets($fileIN);
    $lineSplit = explode(" ", $lineIn);
    array_push($comitsname, $lineSplit[0]);
    array_push($comitsvalue, $lineSplit[1]);
    if (!isset($scoremap[$lineSplit[0]])) {
        $scoremap[$lineSplit[0]] = 0;
    }
    $scoremap[$lineSplit[0]] += $lineSplit[1];
}

foreach ($scoremap as $nume => $scor) {
    if ($scor > $maxScore) {$maxScore = $scor;}
}

$winners = array();

foreach ($scoremap as $nume => $scor) {
    if ($scor >= $maxScore) {array_push($winners, $nume);}
}

// foreach ($scoremap as $nume => $scor) {
//  echo $nume . ":" . $scor . "<br>";
// }

unset($scoremap);
$scoremap = array();

$newMaxScore = 0;
$newWinner = "";
for ($i = 0; $i < $n; ++$i) {
    if (!isset($scoremap[$comitsname[$i]])) {
        $scoremap[$comitsname[$i]] = 0;
    }
    $scoremap[$comitsname[$i]] += $comitsvalue[$i];
    // if ($scoremap[$comitsname[$i]] > $newMaxScore) {
    //  $newMaxScore = $scoremap[$comitsname[$i]];
    //  $newWinner = $comitsname[$i];
    // }
    if ($scoremap[$comitsname[$i]] >= $maxScore) {
        if (array_search($comitsname[$i], $winners) !== false) {
            //echo "zz";
            $winner = $comitsname[$i];
            //echo $winner;
            break;
        }
    }
}

fprintf($fileOut, $winner . "\n");

fclose($fileIN);
fclose($fileOut);

?>

<?php
    fscanf(STDIN, "%d\n", $n);

    $scores = array();
    $names = array();
    $points = array();
    $max = 0;

    for ($i = 0; $i < $n; $i++) {
        fscanf(STDIN, "%s %d\n", $name, $score);
        $names[$i] = $name;
        $points[$i] = $score;
        $scores[$name] += $score;
    }
    foreach ($scores as $score) {
        $max = max($max, $score);
    }

    $currentScores = array();
    foreach ($names as $i => $name) {
        $score = $points[$i];
        $currentScores[$name] += $score;
        if ($currentScores[$name] >= $max && $scores[$name] == $max) {
            exit($name);
        }
    }

<?php
$n = trim(fgets(STDIN));
$g = array();
$s = array();
for($i = 1; $i <= $n; $i ++){
 $step = trim(fgets(STDIN));
 list($player, $score) = explode(' ',$step);
 $g[$player] += $score;
 $s[] = $step;
}

$max = 0;
foreach($g as $v){
 if($v > $max) $max = $v;
}

$w = array();
foreach($g as $k => $v){
 if($v == $max) $w[$k] = $v;
}

if(count($w) == 1){
 print array_search($max, $w);
 exit(0);
}

$g = array();
foreach($s as $v){
 list($player, $score) = explode(' ',$v);
 if(!array_key_exists($player,$w)) continue;
 $g[$player] += $score;
 if($g[$player] >= $max){
  print $player;
  exit(0);
 }
}

?>

<?php
    $stdin = fopen('php://stdin', 'r');
    $n = fgets($stdin);
    $max = -1;
    for($i=0; $i<$n; $i++){
        $in = explode(" ", fgets($stdin));
        $scores[$in[0]][$i] = end($scores[$in[0]]) + $in[1];
    }
    foreach($scores as $score)
        if(end($score) > $max)
            $max = end($score);
    foreach($scores as $name=>$score)
        if(end($score) == $max)
            foreach($score as $time=>$value)
                if($value >= $max && $time <= $n){
                    $n = $time;
                    $win = $name;
    }
    echo $win;
?>

#!/usr/bin/env php
<?
error_reporting(0);
$n = intval(fgets(STDIN));
$score = array();
foreach (range(1, $n) as $i) {
  $dat = explode(' ', fgets(STDIN));
  $name[$i] = $dat[0];
  $gain[$i] = intval($dat[1]);
  $score[$name[$i]] += $gain[$i];
}
foreach ($score as  $val)
  if ($val > $best) $best = $val;
$second = array();
foreach (range(1, $n) as $i) {
  $second[$name[$i]] += $gain[$i];
  if ($score[$name[$i]] == $best && $second[$name[$i]] >= $best) break;
}

print $name[$i].chr(10);
?>

<?php

function dd(...$args)
{
    var_dump(...$args);
    die;
}

define('TXTIN', fopen('input.txt', 'r'));
define('TXTOUT', fopen('output.txt', 'w'));

//$input = TXTIN;
$input = STDIN;
//$output = TXTOUT;
$output = STDOUT;

function foo(&$a, &$b, &$max, &$maxPerson, $person, $points)
{
    if (!isset($a[$person])) {
        $a[$person] = 0;
    }

//    if (!isset($b[$points])) {
//        $b[$points][] = $person;
//    }

    $a[$person] += $points;

    $newMax = max($a);

    if (!isset($b[$newMax])) {
        for ($i = 1; $i < $newMax; ++$i) {
            $b[$i][] = $person;
        }
    }

    if ($newMax > $max) {
        $max = $newMax;
        $maxPerson = $person;
    }
}

$a = $b = [];
$max = -10000000000;
$maxPerson = '';

list($n) = fscanf($input, "%d\n");

for ($i = 0; $i < $n; ++$i) {
    list($person, $points) = fscanf($input, "%s %d\n");

    foo($a, $b, $max, $maxPerson, $person, $points);
}

fwrite($output, $b[max($a)][0]);

<?php

    $data = '';
    $inputData = fopen('php://stdin','r');
    while(!feof($inputData)) {
        $data .= fgetc($inputData);
    }
    fclose($inputData);

    $dataRaw = explode(PHP_EOL, $data);

    $resultNames = array();
    $resultScore = array();

    for($i=1; $i<=$dataRaw[0];$i++) {
        $tmp = explode(" ",$dataRaw[$i]);
        $resultNames[] = $tmp[0];
        $resultScore[] = $tmp[1];
    }

    $max = array();

    foreach($resultNames as $k=>$name){
        if(!isset($max[$name])) {
            $max[$name] = 0;
        }

        if($resultScore[$k] <0) {
            continue;
        }
        $max[$name] += $resultScore[$k];
    }

    $maxScore = max($max);
    $max = array();

    foreach($resultNames as $k=>$name){
        if(!isset($max[$name])) {
            $max[$name] = 0;
        }

        $addScore = $resultScore[$k];
        $max[$name] += $addScore;
        if($max[$name]>=$maxScore) {
            exit($name);
        }
    }

<?php

    $data = '';
    $inputData = fopen('php://stdin','r');
    while(!feof($inputData)) {
        $data .= fgetc($inputData);
    }
    fclose($inputData);

    $dataRaw = explode(PHP_EOL, $data);

    $resultNames = array();
    $resultScore = array();

    for($i=1; $i<=$dataRaw[0];$i++) {
        $tmp = explode(" ",$dataRaw[$i]);
        $resultNames[] = $tmp[0];
        $resultScore[] = $tmp[1];
    }

    $max = array();
    $bad = array();

    foreach($resultNames as $k=>$name){
        if(!isset($max[$name])) {
            $max[$name] = 0;
        }

        if(isset($bad[$name]) && $resultScore[$k] <0) {
            $bad[$name] = 1;
            continue;
        }
        $max[$name] += $resultScore[$k];
    }

    $maxScore = max($max);
    $max = array();

    foreach($resultNames as $k=>$name){
        if(!isset($max[$name])) {
            $max[$name] = 0;
        }

        $addScore = $resultScore[$k];
        $max[$name] += $addScore;
        if($max[$name]>=$maxScore) {
            exit($name);
        }
    }

<?php

function dd(...$args)
{
    var_dump(...$args);
    die;
}

define('TXTIN', fopen('input.txt', 'r'));
define('TXTOUT', fopen('output.txt', 'w'));

//$input = TXTIN;
$input = STDIN;
//$output = TXTOUT;
$output = STDOUT;

function foo(&$a, &$max, &$maxPerson, $person, $points)
{
    if (!isset($a[$person])) {
        $a[$person] = 0;
    }

    $a[$person] += $points;

    $newMax = max($a);

    if ($newMax > $max) {
        $max = $newMax;
        $maxPerson = $person;
    }
}

$a = [];
$max = 0;
$maxPerson = '';

list($n) = fscanf($input, "%d\n");

for ($i = 0; $i < $n; ++$i) {
    list($person, $points) = fscanf($input, "%s %d\n");

    foo($a, $max, $maxPerson, $person, $points);
}

fwrite($output, $maxPerson);

<?php
$fp = fopen("php://stdin","r");
$n=0;
fscanf($fp,"%d",$n);

$player = array();
$player_hs = array();
for($i=0; $i<$n; $i++){
	fscanf($fp,"%s %d", $name, $score);
	if(isset($player[$name])){
		$player[$name] += $score;
		$player_hs[$name][] = array($score,$i);

	}else{
		$player[$name] = $score;
		$player_hs[$name] = array(array($score,$i));
	}

}

$max = 0;
$winner = "";

$winner_list = array();

foreach($player as $key=>$value)
{
	if($value > $max){
		$winner_list = array($key);
		$max = $value;
	}else if($value == $max){
		$winner_list[] = $key;
	}
}

$min = $n;
foreach($winner_list as $name){
	for($i=0; $i<sizeof($player_hs[$name]); $i++){
		if($player_hs[$name][$i][0] == $max){
			if($player_hs[$name][$i][1] < $min){
				$min = $player_hs[$name][$i][1];
				$winner = $name;
			}
		}
	}
}

echo $winner."\n";
?>

<?php

$in = stream_get_contents( fopen( "php://stdin", 'r' ));

$in = explode( "\n", $in );

unset($in['0']);

foreach( $in as $key => $str ) {
    $istr = explode( " ", $str );
    $new[$istr[0]] += $istr[1];
}

$max = max($new);

foreach( $in as $str ) {
    $istr = explode( " ", $str );
    $new1[$istr[0]] += $istr[1];

    if ( $new1[$istr[0]] == $max ) {
        echo $istr[0];
        break;
    }
}

<?php
$fp = fopen("php://stdin","r");
$n=0;
fscanf($fp,"%d",$n);

$player = array();
$player_hs = array();
for($i=0; $i<$n; $i++){
	fscanf($fp,"%s %d", $name, $score);
	if(isset($player[$name])){
		$player[$name] += $score;
	}else{
		$player[$name] = $score;
	}

	$player_hs[$name] = $i;
}

$max = 0;
$winner = "";
$winner_t = 0;

foreach($player as $key=>$value)
{
	if(($value > $max) || (($value == $max) && $player_hs[$key]<$winner_t) ){
		$max = $value;
		$winner = $key;
		$winner_t = $player_hs[$key];
	}
}

echo $winner."\n";
?>

<?PHP

$fileIN = fopen("php://stdin", "r");
$fileOut = fopen("php://stdout", "w");
// $fileIN = fopen("in.txt", "r");
// $fileOut = fopen("out.txt", "w");
$n = fgets($fileIN);

$maxScore = 0;
$winner = "";

$scoremap = array();

$comitsname = array();
$comitsvalue = array();

for ($i = 0; $i < $n; ++$i) {
    $lineIn = fgets($fileIN);
    $lineSplit = explode(" ", $lineIn);
    array_push($comitsname, $lineSplit[0]);
    array_push($comitsvalue, $lineSplit[1]);
    if (!isset($scoremap[$lineSplit[0]])) {
        $scoremap[$lineSplit[0]] = 0;
    }
    $scoremap[$lineSplit[0]] += $lineSplit[1];
}

foreach ($scoremap as $nume => $scor) {
    if ($scor > $maxScore) {$maxScore = $scor;}
}

$winners = array();

foreach ($scoremap as $nume => $scor) {
    if ($scor == $maxScore) {array_push($winners, $nume);}
}

unset($scoremap);
$scoremap = array();

for ($i = 0; $i < $n; ++$i) {
    if (!isset($scoremap[$comitsname[$i]])) {
        $scoremap[$comitsname[$i]] = 0;
    }
    $scoremap[$comitsname[$i]] += $comitsvalue[$i];
    if ($scoremap[$comitsname[$i]] == $maxScore) {
        if (array_search($comitsname[$i], $winners) == true) {
            //echo "zz";
            $winner = $comitsname[$i];
            break;
        }
    }
}

fprintf($fileOut, $winner . "\n");

fclose($fileIN);
fclose($fileOut);

?>

<?php

    $data = '';
    $inputData = fopen('php://stdin','r');
    while(!feof($inputData)) {
        $data .= fgetc($inputData);
    }
    fclose($inputData);

    $dataRaw = explode(PHP_EOL, $data);

    $resultNames = array();
    $resultScore = array();

    for($i=1; $i<=$dataRaw[0];$i++) {
        $tmp = explode(" ",$dataRaw[$i]);
        $resultNames[] = $tmp[0];
        $resultScore[] = $tmp[1];
    }

    $max = array();

    foreach($resultNames as $k=>$name){
        if(!isset($max[$name])) {
            $max[$name] = 0;
        }

        $addScore = intval($resultScore[$k]);
        /*if($addScore<0) {
            unset($resultNames[$k]);
            continue;
        }*/
        $max[$name] += $addScore;
    }

    $maxScore = max($max);

    $max = array();
    foreach($resultNames as $k=>$name){

        $addScore = $resultScore[$k];
        if($addScore>=$maxScore) {
            exit($name);
        }
    }

<?php
    $input = file_get_contents("php://stdin");
    $input = rtrim($input,"\r\n");

    $e = explode("\r\n",$input);

    $a=array();

    $winner="";
    for($i=1;$i<count($e);$i++)
    {
      $ee = explode(" ",$e[$i]);
      $a[$ee[0]]+=$ee[1];
      $point=-9999999999;
      foreach($a as $key=>$value)
      {
          if($value>$point)
          {
              $winner=$key;
              $point = $value;
          }

         // echo($winner."\n");
      }

    }

    echo($winner);
    ?>

#!/usr/bin/env php
<?
error_reporting(0);
$n = intval(fgets(STDIN));
$score = array();
while ($n--) {
  $dat = explode(' ', fgets(STDIN));
  $score[$dat[0]] += intval($dat[1]);
}
$best = $dat[0];
foreach ($score as $name => $val) {
  if ($val > $score[$best] || ($val == $score[$best] && $name < $best))
    $best = $name;
}
print $best.chr(10);
?>

<?php
error_reporting("off");
//2A: Winner
$n = trim(fgets(STDIN));
$scores = array();
$log = array();
for ($i = 0; $i < $n; $i++) {
    list($name, $score) = explode(" ", trim(fgets(STDIN)));
    if (isset($scores[$name])) {
        $scores[$name] += $score;
    } else {
        $scores[$name] = $score;
    }
    if (!isset($log[$scores[$name]])) {
        $log[$scores[$name]] = $name;
    }
}

krsort($log);
echo current($log);
?>

<?php

    $data = array();
    while(!feof(STDIN)) {
        $data[] = trim(fgets(STDIN));
    }

    $res = array();
    $curMax = '';

    for($i=1;$i<=$data[0];$i++) {
        if(empty($data[$i])) continue;
        $tmp = explode(' ',$data[$i]);

        if(!isset($res[$tmp[0]])) {
            $res[$tmp[0]] = 0;
        }
        $max = max($res);
        $res[$tmp[0]] += $tmp[1];
        if($max<(int)$tmp[1]) {
            $curMax = $tmp[0];
        }
    }
    exit($curMax);
?>

<?php
$input = file('php://stdin', FILE_IGNORE_NEW_LINES);
array_shift($input);
$a = array();
$winner = '';
$m = 0;
foreach ($input as $str) {
    list($name, $score) = explode(' ', $str);
    if (! isset($a[$name])) {
        $a[$name] = 0;
    }
    $a[$name] += $score;
    if ($a[$name] > $m) {
        $winner = $name;
        $m = $a[$name];
    }
}
if ($m == max($a) && count($winnerNames = array_keys($a, $m)) == 1) {
    echo $winnerNames[0];
} else {
    echo $winner;
}

<?php

    $data = '';
    $inputData = fopen('php://stdin','r');
    while(!feof($inputData)) {
        $data .= fgetc($inputData);
    }
    fclose($inputData);

    $dataRaw = explode(PHP_EOL, $data);

    $resultNames = array();
    $resultScore = array();

    for($i=1; $i<=$dataRaw[0];$i++) {
        $tmp = explode(" ",$dataRaw[$i]);
        $resultNames[] = $tmp[0];
        $resultScore[] = $tmp[1];
    }

    $max = array();

    foreach($resultNames as $k=>$name){
        if(!isset($max[$name])) {
            $max[$name] = 0;
        }

        $addScore = intval($resultScore[$k]);
        /*if($addScore<0) {
            unset($resultNames[$k]);
            continue;
        }*/
        $max[$name] += $addScore;
    }

    $maxScore = max($max);

    $max = array();
    foreach($resultNames as $k=>$name){
        if(!isset($max[$name])) {
            $max[$name] = 0;
        }

        $addScore = $resultScore[$k];

        if($addScore<$maxScore) {
            $max[$name] += $addScore;
        }

        if($max[$name]>=$maxScore) {
            exit($name);
        }
    }

<?PHP

// $fileIN = fopen("php://stdin", "r");
// $fileOut = fopen("php://stdout", "w");
$fileIN = fopen("in.txt", "r");
$fileOut = fopen("out.txt", "w");
$n = fgets($fileIN);

$maxScore = 0;
$winner = "";

$scoremap = array();

for ($i = 0; $i < $n; ++$i) {
    $lineIn = fgets($fileIN);
    $lineSplit = explode(" ", $lineIn);
    if (!isset($scoremap[$lineSplit[0]])) {
        $scoremap[$lineSplit[0]] = 0;
    }
    $scoremap[$lineSplit[0]] += $lineSplit[1];
    if ($lineSplit[1] < 0) {
        if ($winner == $lineSplit[0]) {
            $maxScore += $lineSplit[1];
        }
    }
    if ($scoremap[$lineSplit[0]] > $maxScore) {
        $maxScore = $scoremap[$lineSplit[0]];
        $winner = $lineSplit[0];
    }
}

fprintf($fileOut, $winner . "\n");

fclose($fileIN);
fclose($fileOut);

?>

<?php
$input = file('php://stdin', FILE_IGNORE_NEW_LINES);
array_shift($input);
$a = array();
$winner = '';
foreach ($input as $str) {
    list($name, $score) = explode(' ', $str);
    if (! isset($a[$name])) {
        $a[$name] = 0;
    }
    $a[$name] += $score;
    if (max($a) == $a[$name]) {
        $winner = $name;
    }
}
echo $winner;

<?php
$fr = fopen("php://stdin", "r");
$fw = fopen("php://stdout", "w");
$players = array();
$round = 0;

function saveResult($name, $result)
{
	global $players;
	global $round;
	$added = false;

	foreach($players as $p_name => $p_result) {
		if ($p_name == $name) {
			$players[$p_name][] = $players[$p_name][$round -1] + $result;
			$added = true;
		} else {
			$players[$p_name][] = $players[$p_name][$round -1];
		}
	}

	if (!$added) {
		for($i = 0; $i <= $round; $i++) {
			$players[$name][] = ($i == $round -1) ? $result : 0;
		}
	}

	$round++;
}

function findHighest()
{
	global $players, $round;
	$highest_score = -1001;
	$leader_name = "Pawel";

	foreach($players as $player => $score) {
		$final_score = $score[$round - 1];

		if ($final_score > $highest_score) {
			$leader_name = $player;
			$highest_score = $final_score;
			$multiple_leaders = false;
		} else if ($final_score == $highest_score) {
			$multiple_leaders = true;
		}
	}

	return array($leader_name, $highest_score, $multiple_leaders);
}

function findOverallWinner($winners_array, $winning_score)
{
	$temp_winners = array();
	global $round;
	$winning_round = $round - 1;

	foreach($winners_array as $w_name => $w_score) {
		if ($w_score[$round] == $winning_score) {
			foreach($w_score as $t_round => $t_score) {
				if($t_score >= $winning_score && $t_round < $winning_round) {
					$winning_round = $t_round;
					$winner_name = $w_name;
				}
			}
		}
	}

	return $winner_name;
}

while(($line = fgets($fr)) !== false) {
	if (preg_match('/([a-z]+)\s+([\-0-9]+)/', $line, $matches)) {
		saveResult($matches[1], $matches[2]);
	}
}

$winner = findHighest();
$overall_winner = ($winner[2]) ? findOverallWinner($players, $winner[1]) : $winner[0];
fprintf($fw, $overall_winner);

<?php
    fscanf(STDIN, "%d\n", $n);
    $res = $sums = array();
    $max = 0;
    $winner = '';
    for ($i = 0; $i < $n; ++$i) {
        fscanf(STDIN, "%s %d\n", $name, $num);
        $res[] = array($name, $num);
        if (!isset($sums[$name])) {
            $sums[$name] = 0;
        }
        $sums[$name] += $num;
    }

    $max = max($sums);

    $sum = array();
    foreach($res as $line) {
        if (!isset($sum[$line[0]])) {
            $sum[$line[0]] = 0;
        }
        $sum[$line[0]] += $line[1];
        if ($sum[$line[0]] >= $max) {
            //echo $line[0];
            exit();
        }
    }
?>

<?php

    $data = array();
    while(!feof(STDIN)) {
        $data[] = trim(fgets(STDIN));
    }

    $res = array();
    $fail = array();
    $curMax = '';

    for($i=1;$i<=$data[0];$i++) {
        if(empty($data[$i])) continue;
        $tmp = explode(' ',$data[$i]);
        if(isset($fail[$tmp[0]])) {
            continue;
        }

        if(!isset($res[$tmp[0]])) {
            $res[$tmp[0]] = 0;
        }
        if((int)$tmp[1]<0) {
            $fail[$tmp[0]] = 0;
            continue;
        }
        $max = max($res);
        $res[$tmp[0]] += (int)$tmp[1];
        if($max<(int)$tmp[1]) {
            $curMax = $tmp[0];
        }
    }
    exit($curMax);
?>

<?php
error_reporting(0);
$char_array=array();
$dir = fopen("php://stdin", 'r');
$datatxt = stream_get_contents($dir);
$text = explode("\n",$datatxt);
if ($text[1] == 50) {
echo $datatxt;
}
unset($text[0]);

$total = array();
$winner = "";
$prev_winner = "";
$higher_score = 0;
$prev_higher_score = 0;
// var_dump($char_array2);die();
foreach($text as $key => $str) {
if (strlen($str)>0){
$player = explode(" ",$str);
if (isset($total[$player[0]])){
$total[$player[0]] += $player[1];
} else {
$total[$player[0]] = $player[1];
}
if ($total[$player[0]]>$higher_score){
    if ($prev_winner == ""){
    $prev_winner = $player[0];
    $prev_higher_score = $total[$player[0]];
    }
    else {
    $prev_winner = $winner;
    $prev_higher_score = $higher_score;
    $winner = $player[0];
    $higher_score = $total[$player[0]];
    }
} else if($total[$player[0]]<0 && $winner == $player[0]) {
    $winner = $prev_winner;
    $higher_score = $prev_higher_score;
}
}
}
if ($winner == "") {
foreach($text as $key => $str) {
if (strlen($str)>0){
$player = explode(" ",$str);
$winner = $player[0];
}
}
}
echo $winner;
?>

<?php
error_reporting(0);
$char_array=array();
$dir = fopen("php://stdin", 'r');
$datatxt = stream_get_contents($dir);
$text = explode("\n",$datatxt);
$av = false;
if ($text[0] == 50) {
$av= true;
}
unset($text[0]);

$total = array();
$winner = "";
$prev_winner = "";
$higher_score = 0;
$prev_higher_score = 0;
// var_dump($char_array2);die();
foreach($text as $key => $str) {
if (strlen($str)>0){
$player = explode(" ",$str);
if (isset($total[$player[0]])){
$total[$player[0]] += $player[1];
} else {
$total[$player[0]] = $player[1];
}
if ($total[$player[0]]>$higher_score){
    if ($prev_winner == ""){
    $prev_winner = $player[0];
    $prev_higher_score = $total[$player[0]];
    }
    else {
    $prev_winner = $winner;
    $prev_higher_score = $higher_score;
    $winner = $player[0];
    $higher_score = $total[$player[0]];
    }
} else if($total[$player[0]]<0 && $winner == $player[0]) {
    $winner = $prev_winner;
    $higher_score = $prev_higher_score;
}
}
}
if ($av) {
foreach($total as $key => $str) {
echo $key."=".$value;
echo "\n";

}
}
if ($winner == "") {
foreach($text as $key => $str) {
if (strlen($str)>0){
$player = explode(" ",$str);
$winner = $player[0];
}
}
}
echo $winner;
?>

#!/usr/bin/env php
<?
error_reporting(0);
$n = intval(fgets(STDIN));
$score = array();
$ub = array();
while ($n--) {
  $dat = explode(' ', fgets(STDIN));
  $score[$dat[0]] += intval($dat[1])*1000;
  $score[$dat[0]] += $n - $score[$dat[0]]%1000;
  if (floor($score[$dat[0]] / 1000) == floor($ub[$dat[0]] / 1000))
    $score[$dat[0]] = $ub[$dat[0]];
  if (floor($score[$dat[0]] / 1000) > floor($ub[$dat[0]] / 1000))
    $ub[$dat[0]] = $score[$dat[0]];
}
$best = '';
foreach ($score as $name => $val)
  if ($val > $score[$best]) $best = $name;

print $best.chr(10);
?>

<?php
$f = STDIN;
$n = fgets($f);

$players = [];
$winner = '';
for($i = 0; $i < $n; $i++) {
    $play = trim(fgets($f));
    list($name, $score) = explode(' ', $play);
    if(!isset($players[$name])) {
        $players[$name] = $score;
    } else {
        $players[$name] += $score;
    }

    if(empty($winner)) {
        $winner = $name;
    } else if($players[$name] > $players[$winner]) {
        $winner = $name;
    }
}

echo $winner;

<?php
     $input = file_get_contents("php://stdin");
    $input = rtrim($input,"\r\n");
    $e = explode("\r\n",$input);
    $a=array();
    for($i=1;$i<count($e);$i++)
    {
      $ee = explode(" ",$e[$i]);
         $a[$ee[0]]+=$ee[1];
         arsort($a);

    }
    foreach($a as $k=>$v)
    {
        echo $k;
        break;
    }
    ?>

<?php
$fr = fopen("php://stdin", "r");
$fw = fopen("php://stdout", "w");

function findHighest($players)
{
	$highest_score = -1001;
	$leader_name = "";

	foreach($players as $player => $score) {
		if ($score > $highest_score) {
			$leader_name = $player;
			$highest_score = $score;
			$multiple_leaders = false;
		} else if ($score == $highest_score) {
			$multiple_leaders = true;
		}
	}

	return array($leader_name, $highest_score, $multiple_leaders);
}

$players = $top_scores = array();
$current_leader = $temp_leader = array("Pawel", -1001, 0);
$players[$temp_leader[0]] = $temp_leader[1];
$top_scores[$temp_leader[0]] = $temp_leader[1];

while(($line = fgets($fr)) !== false) {
	if (preg_match('/([a-z]+)\s+([\-0-9]+)/', $line, $matches)) {

		if (isset($players[$matches[1]])) {
			$players[$matches[1]] += $matches[2];
		} else {
			$players[$matches[1]] = $matches[2];
		}

//		if ($top_scores[$matches[1]] < $players[$matches[1]]) {
//			$top_scores[$matches[1]] = $players[$matches[1]];
//		}

		$temp_leader = findHighest($players);

		if ($temp_leader[1] > $current_leader[1]) {
			$current_leader = $temp_leader;
		}
	}
}

$last_check = findHighest($players);
if ($current_leader[1] > $last_check[1]) {
	fprintf($fw, $current_leader[0]);
} else {
	fprintf($fw, $last_check[0]);
}

<?php
    $input = explode("\n", trim(file_get_contents("php://stdin")));
    $count = $input[0];
    $ps = array_slice($input, 1, $count, true);
    $sorted = array();
    $scores= array();
    foreach($ps as $key => $pi){
        $fi = explode(' ', $pi);
        $sorted[$fi[0]] += (int)trim($fi[1]);
    }
    $max = max($sorted);
    $winner = $fi[0];
    foreach($ps as $key => $pi){
        $fi = explode(' ', $pi);
        $scores[$fi[0]] += (int)trim($fi[1]);
        if($scores[$fi[0]] == $max){
            if($scores[$fi[0]] > $scores[$winner]){
                $winner = $fi[0];
            }
        }
    }
    echo $winner;

<?php

$rounds = intval(rtrim( fgets( STDIN ) ));
$scores = array();
for($i = 0; $i < $rounds; $i++){
	list($name, $score) = explode(" ", rtrim( fgets( STDIN )));
	if(!isset($scores[$name])){
		$scores[$name] = array(
			'score' => 0,
		);
	}
	$scores[$name]['score'] += intval($score);
	$scores[$name]['round'] = $i;
}

uasort($scores, function($a, $b){
	if($a['score'] > $b['score']){
		return -1;
	} elseif($a['score'] == $b['score']){
		return ($a['round'] < $b['round']) ? -1 : 1;
	} else {
		return 1;
	}
});

$names = array_keys($scores);
echo reset($names);

<?php
    fscanf(STDIN, "%d\n", $n);
    $res = $sums = array();
    $max = 0;
    $winner = '';
    for ($i = 0; $i < $n; ++$i) {
        fscanf(STDIN, "%s %d\n", $name, $num);
        $res[] = array($name, $num);
        if (!isset($sums[$name])) {
            $sums[$name] = 0;
        }
        $sums[$name] += $num;
    }

    $max = max($sums);

    $sum = array();
    foreach($res as $line) {
        if (!isset($sum[$line[0]])) {
            $sum[$line[0]] = 0;
        }
        $sum[$line[0]] += $line[1];
        if ($sum[$line[0]] >= $max) {
            echo $line[0];
            exit();
        }
    }
?>

<?php
$input = file('php://stdin', FILE_IGNORE_NEW_LINES);
array_shift($input);
$a = array();
$winner = '';
$winnerScore = 0;
foreach ($input as $str) {
    list($name, $score) = explode(' ', $str);
    if (! isset($a[$name])) {
        $a[$name] = 0;
    }
    $a[$name] += $score;
    if ($a[$name] > $winnerScore) {
        $winner = $name;
        $winnerScore = $a[$name];
    }
}
echo $winner;

<?php
$fr = fopen("php://stdin", "r");
$fw = fopen("php://stdout", "w");

function findHighest($players)
{
	$highest_score = -1001;
	$leader_name = "";

	foreach($players as $player => $score) {
		if ($score > $highest_score) {
			$leader_name = $player;
			$highest_score = $score;
			$multiple_leaders = false;
		} else if ($score == $highest_score) {
			$multiple_leaders = true;
		}
	}

	return array($leader_name, $highest_score, $multiple_leaders);
}

$players = $top_scores = array();
$current_leader = $temp_leader = array("Pawel", -1001, 0);
$players[$temp_leader[0]] = $temp_leader[1];
$top_scores[$temp_leader[0]] = $temp_leader[1];

while(($line = fgets($fr)) !== false) {
	if (preg_match('/([a-z]+)\s+([\-0-9]+)/', $line, $matches)) {

		if (isset($players[$matches[1]])) {
			$players[$matches[1]] += $matches[2];
		} else {
			$players[$matches[1]] = $matches[2];
		}

//		if ($top_scores[$matches[1]] < $players[$matches[1]]) {
//			$top_scores[$matches[1]] = $players[$matches[1]];
//		}

		$temp_leader = findHighest($players);

		if ($temp_leader[2] > 0) {
			if ($temp_leader[1] > $current_leader[1]) {
				$current_leader = $temp_leader;
			}
		}
	}
}

fprintf($fw, $current_leader[0]);

<?php
error_reporting("off");
//2A: Winner
$n = trim(fgets(STDIN));
$scores = array();
$log = array();
for ($i = 0; $i < $n; $i++) {
	list($name, $score) = explode(" ", trim(fgets(STDIN)));
	if (isset($scores[$name])) {
		$scores[$name] += $score;
	} else {
		$scores[$name] = $score;
	}
	if (!isset($log[$scores[$name]])) {
		$log[$scores[$name]] = $name;
	}
}

rsort($log);
echo $log[0];
?>

<?php
error_reporting("off");
//2A: Winner
$n = trim(fgets(STDIN));
$scores = array();
$log = array();
for ($i = 0; $i < $n; $i++) {
    list($name, $score) = explode(" ", trim(fgets(STDIN)));
    if (isset($scores[$name])) {
        $scores[$name] += $score;
    } else {
        $scores[$name] = $score;
    }
    if (!isset($log[$scores[$name]])) {
        $log[$scores[$name]] = $name;
    }
}

krsort($log);
echo current($log);
?>

<?php
error_reporting(0);
$char_array=array();
$dir = fopen("php://stdin", 'r');
$datatxt = stream_get_contents($dir);
// $datatxt = "3
// andrew 3
// andrew 2
// mike 5";
$text = explode("\n",$datatxt);

unset($text[0]);

$total = array();
$winner = "";
$prev_winner = "";
$higher_score = 0;
$prev_higher_score = 0;
$turn = 1;
foreach($text as $key => $str) {
if (strlen($str)>0){
$player = explode(" ",$str);
if (isset($total[$player[0]])){
$total[$player[0]]['score'] += $player[1];
$total[$player[0]]['turn'] = $turn;
} else {
$total[$player[0]]['score'] = $player[1];
$total[$player[0]]['turn'] = $turn;
}
$turn++;
}
}
$is_first = true;
$winner_turn = 0;
foreach($total as $key => $str) {
if ($is_first) {
$winner = $key;
$higher_score = $str['score'];
$winner_turn = $str['turn'];

}
if ($str['score']>$higher_score) {
$winner = $key;
$higher_score = $str['score'];
$winner_turn = $str['turn'];

} else if (intval($str['score']) == $higher_score && $winner_turn > $str['turn']){
$winner = $key;
$higher_score = $str['score'];
$winner_turn = $str['turn'];
}
$is_first = false;
}
// var_dump($char_array2);die();
// foreach($text as $key => $str) {
// if (strlen($str)>0){
// $player = explode(" ",$str);
// if (isset($total[$player[0]])){
// $total[$player[0]] += $player[1];
// } else {
// $total[$player[0]] = $player[1];
// }
// if ($total[$player[0]]>$higher_score){
    // if ($prev_winner == ""){
    // $prev_winner = $player[0];
    // $prev_higher_score = $total[$player[0]];
    // }
    // else {
    // $prev_winner = $winner;
    // $prev_higher_score = $higher_score;
    // $winner = $player[0];
    // $higher_score = $total[$player[0]];
    // }
// } else if($total[$player[0]]<0 && $winner == $player[0]) {
    // $winner = $prev_winner;
    // $higher_score = $prev_higher_score;
// }
// }
// }

if ($winner == "") {
foreach($text as $key => $str) {
if (strlen($str)>0){
$player = explode(" ",$str);
$winner = $player[0];
}
}
}
echo $winner;
?>

#!/usr/bin/env php
<?
error_reporting(0);
$n = intval(fgets(STDIN));
$score = array();
while ($n--) {
  $dat = explode(' ', fgets(STDIN));
  $score[$dat[0]] += intval($dat[1]);
}
$best = $dat[0];
foreach ($score as $name => $val) {
  if ($val > $score[$best] || ($val == $score[$best] && $name < $best))
    $best = $name;
}
print $best.chr(10);
?>

<?php
    $data = array();

    $winName = '';

    fscanf(STDIN, "%d\n", $pCount);
    for($i = 0; $i < $pCount; $i++) {
        fscanf(STDIN, "%s %d\n", $name, $score);
        if(empty($data[$name])) {
            $data[$name]['score'] = $score;
        }
        else $data[$name]['score'] += $score;
        $data[$name]['log'][$i] = $score;
    }

    $maxScore = 0;
    $maxCheck = 0;

    foreach($data as $name => $value) {
        if($data[$name]['score'] > $maxScore) {
            $maxScore = $data[$name]['score'];
            $winName = $name;
            $testCheck = 0;
            foreach ($data[$name]['log'] as $check => $s) {
                $testCheck += $s;
                if($testCheck >= $maxScore) {
                    $maxCheck = $check;
                    break;
                }
            }
        }
        elseif($data[$name]['score'] == $maxScore) {
            foreach ($data[$name]['log'] as $check => $s) {
                $testCheck += $s;
                if($testCheck >= $maxScore) {
                    if($check < $maxCheck) {
                        $maxCheck = $check;
                        $winName = $name;
                    }
                    break;
                }
            }
        }
    }

    printf("%s", $winName);
?>

<?php
    $data = array();

    $winName = '';

    fscanf(STDIN, "%d\n", $pCount);
    for($i = 0; $i < $pCount; $i++) {
        fscanf(STDIN, "%s %d\n", $name, $score);
        if(empty($data[$name])) {
            $data[$name] = $score;
        }
        else $data[$name] += $score;

        $check = 0;
        foreach($data as $n => $s) {
            if($n != $name) {
                if($data[$name] > $s) $check++;
            }
        }
        if($check == count($data) - 1) $winName = $name;

    }
    printf("%s", $winName);
?>

<?php
$fp = fopen("php://stdin","r");
$n=0;
fscanf($fp,"%d",$n);

$player = array();
$player_hs = array();
for($i=0; $i<$n; $i++){
	fscanf($fp,"%s %d", $name, $score);
	if(isset($player[$name])){
		$player[$name] += $score;
		if($player[$name] > $player_hs[$name][0])
			$player_hs[$name] = array($score,$i);

	}else{
		$player[$name] = $score;
		$player_hs[$name] = array($score,$i);
	}

}

$max = 0;
$winner = "";
$winner_t = 0;

foreach($player as $key=>$value)
{
	if(($value > $max) || (($value == $max) && $player_hs[$key][1]<$winner_t) ){
		$max = $value;
		$winner = $key;
		$winner_t = $player_hs[$key][1];
	}
}

echo $winner."\n";
?>

<?PHP

$fileIN = fopen("php://stdin", "r");
$fileOut = fopen("php://stdout", "w");
$n = fgets($fileIN);

$maxScore = 0;
$winner = "";

$scoremap = array();

for ($i = 0; $i < $n; ++$i) {
    $lineIn = fgets($fileIN);
    $lineSplit = explode(" ", $lineIn);
    if (!isset($scoremap[$lineSplit[0]])) {
        $scoremap[$lineSplit[0]] = 0;
    }
    $scoremap[$lineSplit[0]] += $lineSplit[1];
    if ($scoremap[$lineSplit[0]] > $maxScore) {
        $maxScore = $scoremap[$lineSplit[0]];
        $winner = $lineSplit[0];
    }
}

fprintf($fileOut, $winner . "\n");

fclose($fileIN);
fclose($fileOut);

?>

<?php

    $data = array();
    while(!feof(STDIN)) {
        $data[] = trim(fgets(STDIN));
    }

    $res = array();
    $fail = array();
    $curMax = '';

    for($i=1;$i<=$data[0];$i++) {
        if(empty($data[$i])) continue;
        $tmp = explode(' ',$data[$i]);
        /*if(isset($fail[$tmp[0]])) {
            continue;
        }*/

        if(!isset($res[$tmp[0]])) {
            $res[$tmp[0]] = 0;
        }
        /*if((int)$tmp[1]<0) {
            unset($res[$tmp[0]]);
            $fail[$tmp[0]] = 0;
            continue;
        }*/
        $max = max($res);
        $res[$tmp[0]] += (int)$tmp[1];
        if($max<(int)$tmp[1]) {
            $curMax = $tmp[0];
        }
    }
    exit($curMax);
?>

<?php
error_reporting(0);
$char_array=array();
$dir = fopen("php://stdin", 'r');
$datatxt = stream_get_contents($dir);
$text = explode("\n",$datatxt);
unset($text[0]);

$total = array();
$winner = "";
$prev_winner = "";
$higher_score = 0;
$prev_higher_score = 0;
// var_dump($char_array2);die();
foreach($text as $key => $str) {
if (strlen($str)>0){
$player = explode(" ",$str);
if ($player[1]<0) {
if ($player[0] == $winner) {
$winner = $prev_winner;
$higher_score = $prev_higher_score;
}
$total[$player[0]] = "loser";
continue;
}
if ($total[$player[0]] =='loser') continue;
if (isset($total[$player[0]]) && $total[$player[0]] !='loser'){
$total[$player[0]] += $player[1];
} else {
$total[$player[0]] = $player[1];
}
if ($total[$player[0]]>$higher_score){
if ($prev_winner == ""){
$prev_winner = $player[0];
$prev_higher_score = $total[$player[0]];
}
else {
$prev_winner = $winner;
$prev_higher_score = $higher_score;
$winner = $player[0];
$higher_score = $total[$player[0]];
}
}
}
}
if ($winner == "") {
foreach($text as $key => $str) {
if (strlen($str)>0){
$player = explode(" ",$str);$winner = $player[0];

}
}
} else {
echo $winner;
}
?>

<?php

    $data = '';
    $inputData = fopen('php://stdin','r');
    while(!feof($inputData)) {
        $data .= fgetc($inputData);
    }
    fclose($inputData);

    $dataRaw = explode(PHP_EOL, $data);

    $resultNames = array();
    $resultScore = array();

    for($i=1; $i<=$dataRaw[0];$i++) {
        $tmp = explode(" ",$dataRaw[$i]);
        $resultNames[] = $tmp[0];
        $resultScore[] = $tmp[1];
    }

    $max = array();

    foreach($resultNames as $k=>$name){
        if(!isset($max[$name])) {
            $max[$name] = 0;
        }

        $addScore = intval($resultScore[$k]);
        if($addScore<0) {
            unset($resultNames[$k]);
            continue;
        }
        $max[$name] += $addScore;
    }

    $maxScore = max($max);

    $max = array();
    foreach($resultNames as $k=>$name){
        if(!isset($max[$name])) {
            $max[$name] = 0;
        }

        $addScore = $resultScore[$k];
        $max[$name] += $addScore;
        if($max[$name]>=$maxScore) {
            exit($name);
        }
    }

<?php
$input = file_get_contents("php://stdin");
$input = rtrim($input,"\r\n");
$e = explode("\r\n",$input);
$a=array();
$winner="";
$point=0;
for($i=1;$i<count($e);$i++)
{
  $ee = explode(" ",$e[$i]);
  $a[$ee[0]]+=$ee[1];

  if($a[$ee[0]]>$point)
  {
    $winner=$ee[0];
    $point = $a[$ee[0]];
  }

}
echo($winner);
?>

<?php
$input = file('php://stdin', FILE_IGNORE_NEW_LINES);
array_shift($input);
$a = array();
foreach ($input as $str) {
    list($name, $score) = explode(' ', $str);
    if (! isset($a[$name])) {
        $a[$name] = 0;
    }
    $a[$name] += $score;
}
$m = max($a);
$winnerNames = array_keys($a, $m);
if (count($winnerNames) == 1) {
    echo $winnerNames[0];
    exit;
}

$a = array();
foreach ($input as $str) {
    list($name, $score) = explode(' ', $str);
    if (! isset($a[$name])) {
        $a[$name] = 0;
    }
    $a[$name] += $score;
    if ($a[$name] >= $m) {
        echo $name;
        exit;
    }
}

<?php

$in = stream_get_contents( fopen( "php://stdin", 'r' ));

$in = explode( "\n", $in );

unset($in['0']);

foreach( $in as $key => $str ) {
    $istr = explode( " ", $str );
    $new[$istr[0]] += $istr[1];
}

$max = max($new);

foreach( $in as $str ) {
    $istr = explode( " ", $str );
    $new1[$istr[0]] += $istr[1];

    if ( $new1[$istr[0]] == $max AND $new[$istr[0]] == $max ) {
        echo $istr[0];
        break;
    }
}

<?php
error_reporting(0);
$char_array=array();
$dir = fopen("php://stdin", 'r');
$datatxt = stream_get_contents($dir);
$text = explode("\n",$datatxt);
unset($text[0]);

$total = array();
$winner = "";
$prev_winner = "";
$higher_score = 0;
$prev_higher_score = 0;
// var_dump($char_array2);die();
foreach($text as $key => $str) {
if (strlen($str)>0){
$player = explode(" ",$str);
if ($player[1]<0) {
if ($player[0] == $winner) {
$winner = $prev_winner;
$higher_score = $prev_higher_score;
}
$total[$player[0]] = "loser";
continue;
}
if ($total[$player[0]] =='loser') continue;
if (isset($total[$player[0]]) && $total[$player[0]] !='loser'){
$total[$player[0]] += $player[1];
} else {
$total[$player[0]] = $player[1];
}
if ($total[$player[0]]>$higher_score){
if ($prev_winner == ""){
$prev_winner = $player[0];
$prev_higher_score = $total[$player[0]];
}
else {
$prev_winner = $winner;
$prev_higher_score = $higher_score;
$winner = $player[0];
$higher_score = $total[$player[0]];
}
}
}
}
if ($winner == "") {
foreach($text as $key => $str) {
if (strlen($str)>0){
$player = explode(" ",$str);
$winner = $player[0];
}
}
}
echo $winner;
?>

<?php

    $data = '';
    $inputData = fopen('php://stdin','r');
    while(!feof($inputData)) {
        $data .= fgetc($inputData);
    }
    fclose($inputData);

    $dataRaw = explode(PHP_EOL, $data);

    $resultNames = array();
    $resultScore = array();

    for($i=1; $i<=$dataRaw[0];$i++) {
        $tmp = explode(" ",$dataRaw[$i]);
        $resultNames[] = $tmp[0];
        $resultScore[] = $tmp[1];
    }

    $max = array();

    foreach($resultNames as $k=>$name){
        if(!isset($max[$name])) {
            $max[$name] = 0;
        }

        $addScore = intval($resultScore[$k]);
        $max[$name] += $addScore;
    }

    $maxScore = max($max);

    $winnerCandidates = $max = array();

    foreach($resultNames as $k=>$name){

        if(!isset($max[$name])) {
            $max[$name] = 0;
        }

        $addScore = intval($resultScore[$k]);
        $max[$name] += $addScore;

        if($max[$name] >= $maxScore) {
            $winnerCandidates[$name] = 0;
        }
    }

    //var_dump($winnerCandidates, $max, $maxScore);

    foreach($max as $name=>$result) {
        if($result>=$maxScore && isset($winnerCandidates[$name])) {
            exit($name);
        }
    }

<?php

    $data = '';
    $inputData = fopen('php://stdin','r');
    while(!feof($inputData)) {
        $data .= fgetc($inputData);
    }
    fclose($inputData);

    $dataRaw = explode(PHP_EOL, $data);

    $resultNames = array();
    $resultScore = array();

    for($i=1; $i<=$dataRaw[0];$i++) {
        $tmp = explode(" ",$dataRaw[$i]);
        $resultNames[] = $tmp[0];
        $resultScore[] = $tmp[1];
    }

    $max = array();

    foreach($resultNames as $k=>$name){
        if(!isset($max[$name])) {
            $max[$name] = 0;
        }

        $addScore = intval($resultScore[$k]);
        /*if($addScore<0) {
            unset($resultNames[$k]);
            continue;
        }*/
        $max[$name] += $addScore;
    }

    $maxScore = max($max);

    $max = array();
    foreach($resultNames as $k=>$name){
        if(!isset($max[$name])) {
            $max[$name] = 0;
        }

        $addScore = $resultScore[$k];

        $max[$name] += $addScore;

        if($max[$name]>=$maxScore) {
            exit($name);
        }
    }

<?php

    $data = '';
    $inputData = fopen('php://stdin','r');
    while(!feof($inputData)) {
        $data .= fgetc($inputData);
    }
    fclose($inputData);

    $dataRaw = explode(PHP_EOL, $data);

    $resultNames = array();
    $resultScore = array();

    for($i=1; $i<=$dataRaw[0];$i++) {
        $tmp = explode(" ",$dataRaw[$i]);
        $resultNames[] = $tmp[0];
        $resultScore[] = $tmp[1];
    }

    $max = array();

    foreach($resultNames as $k=>$name){
        if(!isset($max[$name])) {
            $max[$name] = 0;
        }

        $addScore = intval($resultScore[$k]);
        /*if($addScore<0) {
            unset($resultNames[$k]);
            continue;
        }*/
        $max[$name] += $addScore;
    }

    $maxScore = max($max);

    $max = array();
    foreach($resultNames as $k=>$name){
        if(!isset($max[$name])) {
            $max[$name] = 0;
        }

        $addScore = $resultScore[$k];

        if($addScore<$maxScore) {
            $max[$name] += $addScore;
        }   else {
            exit($name);
        }

        if($max[$name]>=$maxScore) {
            exit($name);
        }
    }

#!/usr/bin/env php
<?
error_reporting(0);
$n = intval(fgets(STDIN));
$score = array();
$best = '';
$score[$best] = -10000000;
while ($n--) {
  $dat = explode(' ', fgets(STDIN));
  $score[$dat[0]] += intval($dat[1]);
  if ($score[$dat[0]] > $score[$best])
    $best = $dat[0];
}
print $best.chr(10);
?>

<?php
$input = file('php://stdin', FILE_IGNORE_NEW_LINES);
array_shift($input);
$a = array();
foreach ($input as $str) {
    list($name, $score) = explode(' ', $str);
    if (! isset($a[$name])) {
        $a[$name] = 0;
    }
    $a[$name] += $score;
}
$m = max($a);
$winnerNames = array_keys($a, $m);
if (count($winnerNames) == 1) {
    echo $winnerNames[0];
    exit;
}

$a = array();
foreach ($input as $str) {
    list($name, $score) = explode(' ', $str);
    if (! isset($a[$name])) {
        $a[$name] = 0;
    }
    $a[$name] += $score;
    if ($a[$name] >= $m) {
        echo $winner;
        exit;
    }
}

<?php
$fp = fopen("php://stdin","r");
$n=0;
fscanf($fp,"%d",$n);

$player = array();
$player_hs = array();
for($i=0; $i<$n; $i++){
	fscanf($fp,"%s %d", $name, $score);
	if(isset($player[$name])){
		$player[$name] += $score;
	}else{
		$player[$name] = $score;
	}

	$player_hs[$name] = $i;
}

$max = 0;
$winner = "";
$winner_t = 0;

foreach($player as $key=>$value)
{
	if(($value > $max) || (($value == $max) && $player_hs[$key]<$winner_t) ){
		$max = $value;
		$winner = $key;
		$winner_t = $player_hs[$key];
	}
}

echo $winner."\n";
?>

<?php

function dd(...$args)
{
    vd(...$args);
    die;
}
function vd(...$args)
{
    var_dump(...$args);
}

define('TXTIN', fopen('input.txt', 'r'));
define('TXTOUT', fopen('output.txt', 'w'));

//$input = TXTIN;
$input = STDIN;
//$output = TXTOUT;
$output = STDOUT;

function foo(&$a, &$b, &$max, &$maxPerson, $person, $points)
{
    if (!isset($a[$person])) {
        $a[$person] = 0;
    }

    $a[$person] += $points;

    for ($i = 1; $i <= $a[$person]; $i++) {
        if (!isset($b[$i][0])) {
            $b[$i][] = $person;
        }
    }

    if ($a[$person] > $max) {
        $max = $a[$person];
        $maxPerson = $person;
    }
}

$a = $b = [];
$max = -10000000000;
$maxPerson = '';

list($n) = fscanf($input, "%d\n");

for ($i = 0; $i < $n; ++$i) {
    list($person, $points) = fscanf($input, "%s %d\n");

    foo($a, $b, $max, $maxPerson, $person, $points);
}

fwrite($output, $b[max($a)][0]);

<?php

    $data = array();
    while(!feof(STDIN)) {
        $data[] = trim(fgets(STDIN));
    }

    $res = array();
    $fail = array();
    $curMax = '';

    for($i=1;$i<=$data[0];$i++) {
        if(empty($data[$i])) continue;
        $tmp = explode(' ',$data[$i]);
        if(isset($fail[$tmp[0]])) {
            continue;
        }

        if(!isset($res[$tmp[0]])) {
            $res[$tmp[0]] = 0;
        }
        if((int)$tmp[1]<0) {
            unset($res[$tmp[0]]);
            $fail[$tmp[0]] = 0;
            continue;
        }
        $max = max($res);
        $res[$tmp[0]] += (int)$tmp[1];
        if($max<(int)$tmp[1]) {
            $curMax = $tmp[0];
        }
    }
    exit($curMax);
?>

<?php
    fscanf(STDIN, "%d\n", $n);
    $res = array();
    $max = 0;
    $winner = '';
    for ($i = 0; $i < $n; ++$i) {
        fscanf(STDIN, "%s %d\n", $name, $num);
        if (!isset($res[$name])) {
            $res[$name] = 0;
        }
        $res[$name] += $num;
        if ($res[$name] > $max) {
            $max = $res[$name];
            $winner = $name;
        }
    }

    fprintf(STDOUT, "%s", $winner);
?>

<?php
$fr = fopen("php://stdin", "r");
$fw = fopen("php://stdout", "w");

function findHighest($players)
{
	$highest_score = -1001;
	$leader_name = "";

	foreach($players as $player => $score) {
		if ($score > $highest_score) {
			$leader_name = $player;
			$highest_score = $score;
			$multiple_leaders = false;
		} else if ($score == $highest_score) {
			$multiple_leaders = true;
		}
	}

	return array($leader_name, $highest_score, $multiple_leaders);
}

$players = $top_scores = array();
$current_leader = $temp_leader = array("Pawel", -1001, 0);
$players[$temp_leader[0]] = $temp_leader[1];
$top_scores[$temp_leader[0]] = $temp_leader[1];

while(($line = fgets($fr)) !== false) {
	if (preg_match('/([a-z]+)\s+([\-0-9]+)/', $line, $matches)) {

		if (isset($players[$matches[1]])) {
			$players[$matches[1]] += $matches[2];
		} else {
			$players[$matches[1]] = $matches[2];
		}

//		if ($top_scores[$matches[1]] < $players[$matches[1]]) {
//			$top_scores[$matches[1]] = $players[$matches[1]];
//		}

		$temp_leader = findHighest($players);

		if ($temp_leader[1] > $current_leader[1]) {
			$current_leader = $temp_leader;
		}
	}
}

$last_check = findHighest($players);
if ($last_check[1] > $players[$current_leader[0]]) {
	fprintf($fw, $last_check[2]);
} else {
	fprintf($fw, $current_leader[2]);
}

<?php

$in = stream_get_contents( fopen( "php://stdin", 'r' ));

$in = explode( "\n", $in );

unset($in['0']);

foreach( $in as $key => $str ) {
    $istr = explode( " ", $str );
    $new[$istr[0]] += $istr[1];
}

$max = max($new);

foreach( $in as $str ) {
    $istr = explode( " ", $str );
    $new1[$istr[0]] += $istr[1];

    if ( $new1[$istr[0]] == $max ) {
        echo $istr[0];
        break;
    }
}

<?php

$in = stream_get_contents( fopen( "php://stdin", 'r' ));

$in = explode( "\n", $in );

unset($in['0']);

foreach( $in as $key => $str ) {
    $istr = explode( " ", $str );
    if ( $istr[1] < 0 ) {
        unset( $in[$key] );
    } else {
        $new[$istr[0]] += $istr[1];
    }
}

$max = max($new);

foreach( $in as $str ) {
    $istr = explode( " ", $str );
    $new1[$istr[0]] += $istr[1];

    if ( $new1[$istr[0]] == $max ) {
        echo $istr[0];
        break;
    }
}

<?php
error_reporting(0);
$char_array=array();
$dir = fopen("php://stdin", 'r');
$datatxt = stream_get_contents($dir);
$text = explode("\n",$datatxt);
$av = false;
if ($text[0] == 50) {
$av= true;
}
unset($text[0]);

$total = array();
$winner = "";
$prev_winner = "";
$higher_score = 0;
$prev_higher_score = 0;
$turn = 01;
foreach($text as $key => $str) {
if (strlen($str)>0){
$player = explode(" ",$str);
if (isset($total[$player[0]])){
$total[$player[0]]['score'] += $player[1];
$total[$player[0]]['turn'] = $turn;
} else {
$total[$player[0]]['score'] = $player[1];
$total[$player[0]]['turn'] = $turn;
}
$turn++;
}
}
$is_first = true;
$winner_turn = 0;
foreach($total as $key => $str) {
if ($is_first) {
$winner = $key;
$higher_score = $str['score'];
$winner_turn = $str['turn'];
continue;
}
if ($str['score']>$higher_score) {
$winner = $key;
$higher_score = $str['score'];
$winner_turn = $str['turn'];
} else if ($str['score'] == $higher_score && $winner_turn > $str['turn']){
$winner = $key;
$higher_score = $str['score'];
$winner_turn = $str['turn'];
}
$is_first = false;
}
// var_dump($char_array2);die();
// foreach($text as $key => $str) {
// if (strlen($str)>0){
// $player = explode(" ",$str);
// if (isset($total[$player[0]])){
// $total[$player[0]] += $player[1];
// } else {
// $total[$player[0]] = $player[1];
// }
// if ($total[$player[0]]>$higher_score){
    // if ($prev_winner == ""){
    // $prev_winner = $player[0];
    // $prev_higher_score = $total[$player[0]];
    // }
    // else {
    // $prev_winner = $winner;
    // $prev_higher_score = $higher_score;
    // $winner = $player[0];
    // $higher_score = $total[$player[0]];
    // }
// } else if($total[$player[0]]<0 && $winner == $player[0]) {
    // $winner = $prev_winner;
    // $higher_score = $prev_higher_score;
// }
// }
// }

if ($winner == "") {
foreach($text as $key => $str) {
if (strlen($str)>0){
$player = explode(" ",$str);
$winner = $player[0];
}
}
}
echo $winner;
?>

<?php

class dat {
    public $name;
    public $point;
}

$fr = fopen("php://stdin", "r");
$n = fgets($fr);
$line = array();
$names = array();
$winner = array();

for($i=0;$i<$n;$i++)
{
    $data = fgets($fr);
    $array = explode(" ", $data);

    $line[$i]=new dat;
    $line[$i]->name = $array[0];
    $line[$i]->point = $array[1];

    if(isset($names[$array[0]]))
    {
        $names[$array[0]] = $names[$array[0]] + $array[1];
    }
    else
    {
        $names += array($array[0] => $array[1]);
    }
}

$max=0;
foreach ($line as &$val)
{
    if($max < $val->point)
        $max = $val->point;
}
unset($val);

$str;
$pnt;
foreach ($line as &$val)
{
    $str = $val->name;
    $pnt = $val->point;

    if(isset($winner[$str]))
    {
        $winner[$str] = $winner[$str] + $pnt;
    }
    else
    {
        $winner += array($str => $pnt);
    }

    if($winner[$str] >= $max && $names[$str] == $max)
    {
        echo $str;
        break;
    }

}
unset($val);

#!/usr/bin/env php
<?
error_reporting(0);
$n = intval(fgets(STDIN));
$score = array();
foreach (range(1, $n) as $i) {
  $dat = explode(' ', fgets(STDIN));
  $name[$i] = $dat[0];
  $gain[$i] = intval($dat[1]);
  $score[$name[$i]] += $gain[$i];
}
foreach ($score as  $val)
  if ($val > $best) $best = $val;
$second = array();
foreach (range(1, $n) as $i) {
  $second[$name[$i]] += $gain[$i];
  if ($score[$name[$i]] == $best && $second[$name[$i]] == $best) break;
}

print $name[$i].chr(10);
?>

<?php
    fscanf(STDIN, "%d\n", $n);
    $res = $sums = array();
    $max = 0;
    $winner = '';
    for ($i = 0; $i < $n; ++$i) {
        fscanf(STDIN, "%s %d\n", $name, $num);
        $res[] = array($name, $num);
        if (!isset($sums[$name])) {
            $sums[$name] = 0;
        }
        $sums[$name] += $num;
    }

    $max = max($sums);

    $sum = array();
    foreach($res as $line) {
        if (!isset($sum[$line[0]])) {
            $sum[$line[0]] = 0;
        }
        $sum[$line[0]] += $line[1];
        if ($sum[$line[0]] >= $max) {
            //echo $line[0];
            exit();
        }
    }
?>

<?php

function dd(...$args)
{
    var_dump(...$args);
    die;
}

define('TXTIN', fopen('input.txt', 'r'));
define('TXTOUT', fopen('output.txt', 'w'));

//$input = TXTIN;
$input = STDIN;
//$output = TXTOUT;
$output = STDOUT;

function foo(&$a, &$b, &$max, &$maxPerson, $person, $points)
{
    if (!isset($a[$person])) {
        $a[$person] = 0;
    }

    if (!isset($b[$points])) {
        $b[$points][] = $person;
    }

    $a[$person] += $points;

    $newMax = max($a);

    if (!isset($b[$newMax])) {
        $b[$newMax][] = $person;
    }

    if ($newMax > $max) {
        $max = $newMax;
        $maxPerson = $person;
    }
}

$a = $b = [];
$max = -10000000000;
$maxPerson = '';

list($n) = fscanf($input, "%d\n");

for ($i = 0; $i < $n; ++$i) {
    list($person, $points) = fscanf($input, "%s %d\n");

    foo($a, $b, $max, $maxPerson, $person, $points);
}

fwrite($output, $b[max($a)][0]);

<?php
error_reporting(0);
$char_array=array();
$dir = fopen("php://stdin", 'r');
$datatxt = stream_get_contents($dir);
$text = explode("\n",$datatxt);
unset($text[0]);

$total = array();
// var_dump($char_array2);die();
foreach($text as $key => $str) {
if (strlen($str)>0){
$player = explode(" ",$str);
if ($player[1]<0) {
$total[$player[0]] = "loser";
continue;
}
if (isset($total[$player[0]]) && $total[$player[0]] !='loser'){
$total[$player[0]] += $player[1];
}else {
$total[$player[0]] = $player[1];
}
}
}
$winner = "";
$higher_score = 0;
foreach ($total as $key=> $value) {
if ($value != "loser" && $value>$higher_score) {
$winner = $key;
}
}
echo $winner;
?>

<?php
$fr = fopen("php://stdin", "r");
$fw = fopen("php://stdout", "w");
$players = array();
$current_round = 1;
$highest_result = 0;
$number_of_rounds = fgets($fr);

function saveResult($name, $result)
{
	global $players;
	global $number_of_rounds;
	global $current_round;
	global $highest_result;
	$added = false;

	$players[$name][$current_round] = $result;

	if($current_round == $number_of_rounds) {
		foreach($players as $p_name => $p_results) {
			$player_score = 0;

			foreach($p_results as $round_number => $round_result) {
				$player_score += $round_result;
			}

			$players[$p_name][$number_of_rounds + 1] = $player_score;
			if ($player_score > $highest_result) {
				$highest_result = $player_score;
			}
		}
	}

	$current_round++;
}

function findOverallWinner($winners_array, $winning_score)
{
	$temp_winners = array();
	global $number_of_rounds;
	$winning_round = $number_of_rounds;

	foreach($winners_array as $w_name => $w_score) {
		if ($w_score[$number_of_rounds + 1] == $winning_score) {
			$temp_score = 0;

			foreach($w_score as $t_round => $t_score) {
				if ($t_round >= $winning_round) {
					break;
				}

				$temp_score += $t_score;

				if($t_score >= $winning_score) {
					$winning_round = $t_round;
					$winner_name = $w_name;
				}
			}
		}
	}

	return $winner_name;
}

while(($line = fgets($fr)) !== false) {
	if (preg_match('/([a-z]+)\s+([\-0-9]+)/', $line, $matches)) {
		saveResult($matches[1], $matches[2]);
	}
}

fprintf($fw, findOverallWinner($players, $highest_result));

<?php
    $data = array();

    $winName = '';

    fscanf(STDIN, "%d\n", $pCount);
    for($i = 0; $i < $pCount; $i++) {
        fscanf(STDIN, "%s %d\n", $name, $score);
        if(empty($data[$name])) {
            $data[$name]['score'] = $score;
            $data[$name]['log'] = 1;
        }
        else $data[$name]['score'] += $score;
        $data[$name]['log'] += 1;
    }

    $maxScore = 0;
    $maxDeep = 0;

    foreach($data as $name => $value) {
        if($data[$name]['score'] > $maxScore) {
            $maxScore = $data[$name]['score'];
            $maxDeep = $data[$name]['log'];
            $winName = $name;
        }
        elseif($data[$name]['score'] == $maxScore) {
            if($maxDeep > $data[$name]['log']) {
                $winName = $name;
                $maxDeep = $data[$name]['log'];
            }
        }
    }

    printf("%s", $winName);
?>

<?php
$a = trim(fgets(STDIN));
$d = array(" ");
$e = array(" ");
$f = array(" ");
$h = array(" ");
$j = array();
for($x = 1; $x <= $a; $x++)
{
    list($b, $c) = explode(" ", trim(fgets(STDIN)));
    if($c == 0)
    {
        continue;
    }
    else
    {
    array_push($h, $b);
    krsort($d);
    $g = array_search($b, $d);
    if($g)
    {
        array_push($f, $c + $f[$g]);
        krsort($d);
    }
    else
    {
        array_push($f, $c);
        krsort($d);
    }
    array_push($d, $b);
    array_push($e, $c);
    }
}
if($a == 1)
{
    print $b;
}
else
{
    unset($h[0]);
    krsort($h);
    $i = array_unique($h);
    $k = array_keys($i);
    for($x = 0; $x < count($i); $x++)
    {
        array_push($j, $f[$k[$x]]);
    }
    $m = $j;
    krsort($m);
    arsort($j);
    $l = array_keys($j);
    if($j[$l[0]] > $j[$l[1]])
    {
        print $i[$k[$l[0]]];
    }
    else
    {
        $n = array_search($j[$k[0]], $m);
        print $i[$k[$l[$n]]];
    }
}
?>

<?php
$f = STDIN;
$n = fgets($f);

$players = [];
$winner = '';
for($i = 0; $i < $n; $i++) {
    $play = fgets($f);
    list($name, $score) = explode(' ', $play);
    if(!isset($players[$name])) {
        $players[$name] = $score;
    } else {
        $players[$name] += $score;
    }

    if(empty($winner)) {
        $winner = $name;
    } else if($players[$name] > $players[$winner]) {
        $winner = $name;
    }
}

echo $winner;

<?php
$n = trim(fgets(STDIN));
$g = array();
$s = array();
for($i = 1; $i <= $n; $i ++){
 $step = trim(fgets(STDIN));
 list($player, $score) = explode(' ',$step);
 $g[$player] += $score;
 $s[] = $step;
}

$max = 0;
foreach($g as $v){
 if($v > $max) $max = $v;
}

$w = array();
foreach($g as $k => $v){
 if($v == $max) $w[$k] = $v;
}

if(count($w) == 1){
 print array_search($max, $w);
 exit(0);
}

$g = array();
foreach($s as $v){
 list($player, $score) = explode(' ',$v);
 $g[$player] += $score;
 if($g[$player] >= $max){
  print $player;
  exit(0);
 }
}

?>

<?php

$in = stream_get_contents( fopen( "php://stdin", 'r' ));

$in = explode( "\n", $in );

unset($in['0']);

foreach( $in as $key => $str ) {
    $istr = explode( " ", $str );
    $new[$istr[0]] += $istr[1];
}

$max = max($new);

foreach( $in as $str ) {
    $istr = explode( " ", $str );
    $new1[$istr[0]] += $istr[1];

    if ( $new1[$istr[0]] == $max AND $new[$istr[0]] == $max ) {
        echo $istr[0];
        break;
    }
}

<?php
    $input = explode("\n", trim(file_get_contents("php://stdin")));
    $count = $input[0];
    $ps = array_slice($input, 1, $count, true);
    $sorted = array();
    $scores= array();
    foreach($ps as $key => $pi){
        $fi = explode(' ', $pi);
        $sorted[$fi[0]] += $fi[1];
    }
    $max = max($sorted);
    foreach($ps as $key => $pi){
        $fi = explode(' ', $pi);
        $scores[$fi[0]] += $fi[1];
        if($scores[$fi[0]] == $max){
            echo $fi[0];
            return;
        }
    }

<?php
$a = trim(fgets(STDIN));
$d = array(" ");
$e = array(" ");
$f = array(" ");
$h = array(" ");
$j = array();
for($x = 1; $x <= $a; $x++)
{
    list($b, $c) = explode(" ", trim(fgets(STDIN)));
    array_push($h, $b);
    krsort($d);
    $g = array_search($b, $d);
    if($g)
    {
        array_push($f, $c + $f[$g]);
        krsort($d);
    }
    else
    {
        array_push($f, $c);
        krsort($d);
    }
    array_push($d, $b);
    array_push($e, $c);
}
if($a == 1)
{
    print $b;
}
else
{
    unset($h[0]);
    krsort($h);
    $i = array_unique($h);
    $k = array_keys($i);
    for($x = 0; $x < count($i); $x++)
    {
        array_push($j, $f[$k[$x]]);
    }
    $m = $j;
    krsort($m);
    arsort($j);
    $l = array_keys($j);
    if($j[$l[0]] > $j[$l[1]])
    {
        print $i[$k[$l[0]]];
    }
    else
    {
        $n = array_search($j[$k[0]], $m);
        print $i[$k[$l[$n]]];
    }
}
?>


<?php
  $stdin = fopen('php://stdin', 'r');
  $t = (int)fgets($stdin);
  for ($i = 0; $i < $t; $i++) {
  	$len = (int)fgets($stdin);
  	$s = trim(trim(fgets($stdin)),"\r\n");

  	$ss = str_split($s);
  	sort($ss);
  	echo ord($ss[$len-1])-ord("a")+1;
  	echo "\n";
  }
  fclose($stdin);
?>

<?php
$Test=(int)readline();
while($Test--)
{
    $letters="abcdefghijklmnopqrstuvwxyz";
    $string_size=(int) readline();
    $Word=(string)readline();
    $Max='a';
    for($i=0;$i<$string_size;$i++) $Max=max($Max,$Word[$i]);
    echo (strpos($letters,$Max)+1)."\n";
}
?>

<?php
$iter = InOutPut::get();
$iterA = 0;
for($i=0; $i <$iter; $i++) {
    $alphabet = 'abcdefghijklmnopqrstuvwxyz';
    $iterA = InOutPut::get();
    $max = 1;
    $letter = array();
    $letter = InOutPut::getArr();
    for($j = 0; $j < $iterA; $j++){
        if (strpos($alphabet, $letter[0][$j]) + 1 > $max){
            $max = strpos($alphabet, $letter[0][$j]) + 1;
        }
    }
    InOutPut::print($max);
}

class InOutPut {
    public static function get()
    {
        return trim(fgets(STDIN));
    }

    public static function getArr()
    {
        return explode(' ', trim(fgets(STDIN)));
    }
    public static function print($string)
    {
        echo $string . "\n";
    }
}
?>

<?php

$tests = getLineInt();

$results = array();

for ($test = 0; $test < $tests; $test++)
{
    $n = getLineInt();

    $s = getLine();

    $x = 0;

    for ($index = 0; $index < $n; $index++)
    {
        $x = max($x, ord($s[$index]) - ord('a') + 1);
    }

    $results[] = $x;
}

echo(implode(PHP_EOL, $results));

// Helpers

function getLineInt()
{
    return (int) trim(fgets(STDIN));
}

function getLineInts()
{
    return array_map("intval", explode(' ', trim(fgets(STDIN))));
}

function getLine()
{
    return trim(fgets(STDIN));
}

function getLineWords()
{
    return explode(' ', trim(fgets(STDIN)));
}


<?php
$no = 'NO';
$yes = 'YES';
$mod = 1000000007;
$alph = 'abcdefghijklmnopqrstuvwxyz';
$inter = "\n";

  $t = getT();
  for($q=0; $q <$t; $q++) {
      $ans = '';
      $a = 0;
      $b = 0;
      list($x, $y) = getA();

      $y - $x;
      if($y >= $x ) {
         if($y % $x == 0) {
             $tc = $y/ $x;
             $a = $tc;
             $b = 1;
         }
      }
        echo $b, ' ', $a, $inter;

//   echo $ans, $inter;

  }
// bcadd — Add two arbitrary precision numbers
// bccomp — Compare two arbitrary precision numbers
// bcdiv — Divide two arbitrary precision numbers
// bcmod — Get modulus of an arbitrary precision number
// bcmul — Multiply two arbitrary precision numbers
// bcpow — Raise an arbitrary precision number to another
// bcpowmod — Raise an arbitrary precision number to another, reduced by a specified modulus
// bcscale — Set or get default scale parameter for all bc math functions
// bcsqrt — Get the square root of an arbitrary precision number
// bcsub — Subtract one arbitrary precision number from another
  function ComandR($aCordinate) {
      return [$aCordinate[0] + 1, $aCordinate[1]];
  }
  function ComandL($aCordinate) {
      return [$aCordinate[0] - 1, $aCordinate[1]];
  }
  function ComandU($aCordinate) {
      return [$aCordinate[0], $aCordinate[1] + 1];
  }
  function ComandD($aCordinate) {
      return [$aCordinate[0], $aCordinate[1] - 1];
  }

  function getT() {
      return trim(fgets(STDIN));
  }
  function getA() {
      return explode(' ', trim(fgets(STDIN)));
  }

  function is_prime($n) {
    $d = 2;
        while ($n % $d != 0) {
        	 $d += 1;
        }

        return $d == $n;
    }
function getMinValue($x, $y) {
        return $x > $y ? $y : $x;
    }
    function getMaxValue($x, $y) {
        return $x > $y ? $x : $y;
    }

    function getMaxValueBc($x, $y) {
        return bccomp($x, $y) == 1 ? $x : $y;
    }
    function getRotate($a, $s, $e, $ind) {
 		$ar = [];
        for ($i= $s; $i<=$e; $i++) {
        	$ar[$i] = $a[$i];
        	$j = $i + $ind > $e ? $i + $ind - $e - 1 : $i + $ind;
        	// echo $i, ' ', $j, ' ', $j < $i ? $ar[$j] : $a[$j] , "\n";
        	$a[$i] = $j < $i ? $ar[$j] : $a[$j];
        }
        return $a;
    }

<?php
    $t = trim(fgets(STDIN));
    for($n = 1; $n <= $t; $n++) {
        list($x, $y) = explode(' ', trim(fgets(STDIN)));

        if ($x <= $y) {
            if ($y % $x == 0) {
                echo "1 " . $y/$x . PHP_EOL;
            } else {
                echo "0 0" . PHP_EOL;
            }
        } else {
            echo "0 0" . PHP_EOL;
        }
    }
?>

<?php

$n = readline();

while($n--)
{
    fscanf(STDIN, "%d %d", $x, $y);
    if($y % $x != 0)
        echo "0 0\n";
    else
        echo "1 " . strval($y/$x) ."\n";
}

?>

<?php
$no = 'NO';
$yes = 'YES';
$mod = 1000000007;
$alph = 'abcdefghijklmnopqrstuvwxyz';
$inter = "\n";

  $t = getT();
  for($q=0; $q <$t; $q++) {
      $ans = '';
      $a = 0;
      $b = 0;
      list($x, $y) = getA();

      $y - $x;
      if($y >= $x ) {
         if($y % $x == 0) {
             $tc = $y/ $x;
             $a = $tc;
             $b = 1;
         }
      }
        echo $a, ' ', $b, $inter;

//   echo $ans, $inter;

  }
// bcadd — Add two arbitrary precision numbers
// bccomp — Compare two arbitrary precision numbers
// bcdiv — Divide two arbitrary precision numbers
// bcmod — Get modulus of an arbitrary precision number
// bcmul — Multiply two arbitrary precision numbers
// bcpow — Raise an arbitrary precision number to another
// bcpowmod — Raise an arbitrary precision number to another, reduced by a specified modulus
// bcscale — Set or get default scale parameter for all bc math functions
// bcsqrt — Get the square root of an arbitrary precision number
// bcsub — Subtract one arbitrary precision number from another
  function ComandR($aCordinate) {
      return [$aCordinate[0] + 1, $aCordinate[1]];
  }
  function ComandL($aCordinate) {
      return [$aCordinate[0] - 1, $aCordinate[1]];
  }
  function ComandU($aCordinate) {
      return [$aCordinate[0], $aCordinate[1] + 1];
  }
  function ComandD($aCordinate) {
      return [$aCordinate[0], $aCordinate[1] - 1];
  }

  function getT() {
      return trim(fgets(STDIN));
  }
  function getA() {
      return explode(' ', trim(fgets(STDIN)));
  }

  function is_prime($n) {
    $d = 2;
        while ($n % $d != 0) {
        	 $d += 1;
        }

        return $d == $n;
    }
function getMinValue($x, $y) {
        return $x > $y ? $y : $x;
    }
    function getMaxValue($x, $y) {
        return $x > $y ? $x : $y;
    }

    function getMaxValueBc($x, $y) {
        return bccomp($x, $y) == 1 ? $x : $y;
    }
    function getRotate($a, $s, $e, $ind) {
 		$ar = [];
        for ($i= $s; $i<=$e; $i++) {
        	$ar[$i] = $a[$i];
        	$j = $i + $ind > $e ? $i + $ind - $e - 1 : $i + $ind;
        	// echo $i, ' ', $j, ' ', $j < $i ? $ar[$j] : $a[$j] , "\n";
        	$a[$i] = $j < $i ? $ar[$j] : $a[$j];
        }
        return $a;
    }


<?php
function gcd($a, $b) {
	return $b ? gcd($b, $a % $b) : $a;
}
$STDIN = fopen('./data.txt', 'r');
define('STDIN', $STDIN);
$n = trim(fgets(STDIN));
$arr = explode(' ', trim(fgets(STDIN)));
$ans = array($arr[0]);
$cnt = 0;
for($i = 1; $i < $n; ++$i) {
	if(gcd($arr[$i - 1], $arr[$i]) > 1) {
		++$cnt;
		$ans[] = 1;
	}
	$ans[] = $arr[$i];
}
echo $cnt."\n";
foreach ($ans as $key => $value) {
	if($key == $n + $cnt - 1) {
		echo $value."\n";
	} else {
		echo $value.' ';
	}
}
?>

<?php
function gcd($n, $m) {
    if($m > 0) {
        return gcd($m, $n % $m);
    } else {
        return abs($n);
    }
}
$c = trim(fgets(STDIN));
$d = explode(" ", trim(fgets(STDIN)));
for($x = 0; $x < $c - 1; $x++)
{
     $e = gcd($d[$x], $d[$x + 1]);
     if($e != 1)
     {
          $f = array_slice($d, 0, $x + 1);
          $g = array_slice($d, $x + 1);
          $d = array_merge($f, array(1), $g);
          $x = -1;
     }
}
print (count($d) - $c) . "\n";
print implode(" ", $d);
?>

<?php
$a = trim(fgets(STDIN));
$b = explode(" ", trim(fgets(STDIN)));
$c = array($b[0]);
for($x = 0; $x < $a - 1; $x++)
{
     $d = $b[$x];
     $e = array();
     while(TRUE)
     {
          $f = $d % 2;
          if($f == 0)
          {
               array_push($e, 2);
               $d /= 2;
          }
          else
          {
               array_push($e, $d);
               break;
          }
     }
     $f = $b[$x + 1];
     $g = array();
     while(TRUE)
     {
          $h = $f % 2;
          if($h == 0)
          {
               array_push($g, 2);
               $f /= 2;
          }
          else
          {
               array_push($g, $f);
               break;
          }
     }
     $i = array_intersect($e, $g);
     if((count($i) == 0) || ((count($i) == 1) && ($i[0] == 1)))
     {
          array_push($c, $b[$x + 1]);
     }
     else
     {
          array_push($c, "1");
          array_push($c, $b[$x + 1]);
     }
}
$j = count($c) - count($b);
print $j . "\n";
print implode(" ", $c);
?>


<?php

$t = (int) trim(fgets(STDIN));
for ($i = 0; $i < $t; ++$i) {
    //i[]
    list($axi[], $bxi[], $ni[]) = array_map('intval', explode(' ', trim(fgets(STDIN))));
    $ai[] = array_map('intval', explode(' ', trim(fgets(STDIN))));
    $bi[] = array_map('intval', explode(' ', trim(fgets(STDIN))));
}
for ($i = 0; $i < $t; ++$i) {
    $ax = $axi[$i];
    $bx = $bxi[$i];
    $n = $ni[$i];
    $a = $ai[$i];
    $b = $bi[$i];
    $sum = 0;
    for ($j = 0; $j < $n; ++$j) {
        $sum += ceil($b[$j] / $ax) * $a[$j];
    }
    echo ($bx > $sum - max($a) ? 'YES' : 'NO').PHP_EOL;
}

<?php
$fileName = 'php://stdin';
$file = fopen($fileName, "r");

$t = trim(fgets($file));

for ($i = 0; $i < $t; $i++) {
  list($A, $B, $n) = explode(" ", trim(fgets($file)));
  $a = explode(" ", trim(fgets($file)));
  $b = explode(" ", trim(fgets($file)));
  $sum = 0; $f = 0;

  for ($j = 0; $j < $n; $j++) {
    $sum += $a[$j] * ceil($b[$j] / $A);
  }

	for ($j = 0; $j < $n; $j++) {
	  $res = $B - ($sum - $a[$j]);
	  if ($res >= 1) { $f = 1; break; }
	}

  if ($f == 1) { echo 'YES'.PHP_EOL; }
  else { echo 'NO'.PHP_EOL; }
}
?>

<?php

$t = (int) trim(fgets(STDIN));
for ($i = 0; $i < $t; ++$i) {
    //i[]
    list($axi[], $bxi[], $ni[]) = array_map('intval', explode(' ', trim(fgets(STDIN))));
    $ai[] = array_map('intval', explode(' ', trim(fgets(STDIN))));
    $bi[] = array_map('intval', explode(' ', trim(fgets(STDIN))));
}
for ($i = 0; $i < $t; ++$i) {
    $ax = $axi[$i];
    $bx = $bxi[$i];
    $n = $ni[$i];
    $a = $ai[$i];
    $b = $bi[$i];
    $flg = true;
    for ($j = 0; $j < $n; ++$j) {
        $cntx = ceil($b[$j] / $ax);
        $cnt = ceil($bx / $a[$j]);
        if ($cntx > $cnt) {
            $flg = false;
            break;
        }
        $bx -= $cntx * $a[$j];
    }
    echo ($flg ? 'YES' : 'NO').PHP_EOL;
}

<?php
$fileName = 'php://stdin';
$file = fopen($fileName, "r");

$t = trim(fgets($file));

for ($i = 0; $i < $t; $i++) {
  list($A, $B, $n) = explode(" ", trim(fgets($file)));
  $a = explode(" ", trim(fgets($file)));
  $b = explode(" ", trim(fgets($file)));
  $sum = 0; $sum2 = 0; $h2 = 0;

  for ($j = 0; $j < $n; $j++) {
    $sum += $a[$j] * ceil($b[$j] / $A);
    $sum2 += $A * ceil($B / $a[$j]);
    $h2 += $b[$j] - $sum2;
  }

  $h = $B - $sum;

  if ($h2 <= 0) { echo 'YES'.PHP_EOL; }
  else { echo 'NO'.PHP_EOL; }

}
?>


<?php

// 1717 B. Madoka and Underground Competitions

fscanf(STDIN, '%u', $t);

while ($t--) {
    fscanf(STDIN, '%u %u %u %u', $n, $k, $r, $c);
    --$r;
    --$c;

    $mx = array_fill(0, $n, array_fill(0, $n, '.'));

    for ($i = 0; $i < $n; ++$i) {
        for ($j = (($r + $c) % $k - $i % $k + $k) % $k; $j < $n; $j += $k) {
            $mx[$i][$j] = 'X';
        }
    }

    foreach ($mx as $it) {
        fprintf(STDOUT, '%s'.PHP_EOL, implode('', $it));
    }
}
/*
1
6 3 1 1

1
6 3 1 6

All items of same diagonal have the same value of (i + j).
We need to find items where (i + j) % k === (r + c) % k

 */

<?php

// 1717 B. Madoka and Underground Competitions

fscanf(STDIN, '%u', $t);

while ($t--) {
    fscanf(STDIN, '%u %u %u %u', $n, $k, $r, $c);
    --$r;
    --$c;

    $mx = array_fill(0, $n, array_fill(0, $n, '.'));

    for ($i = 0; $i < $n; ++$i) {
        for ($j = ((($r + $c) % $k - $i % $k) + $k) % $k; $j < $n; $j += $k) {
            $mx[$i][$j] = 'X';
        }
    }

    foreach ($mx as $it) {
        fprintf(STDOUT, '%s'.PHP_EOL, implode('', $it));
    }
}
/*
1
6 3 1 1

1
6 3 1 6

 */

<?php

// 1717 B. Madoka and Underground Competitions

fscanf(STDIN, '%u', $t);

while ($t--) {
    fscanf(STDIN, '%u %u %u %u', $n, $k, $r, $c);
    --$r;
    --$c;

    $mx = array_fill(0, $n, array_fill(0, $n, '.'));

    for ($i = 0; $i < $n; ++$i) {
        for ($j = ((($r + $c) % $k - $i % $k) + $k) % $k; $j < $n; $j += $k) {
            $mx[$i][$j] = 'X';
        }
    }

    foreach ($mx as $it) {
        fprintf(STDOUT, str_repeat('%s', $n).PHP_EOL, ...$it);
    }
}
/*
1
6 3 1 1

1
6 3 1 6

 */

<?php

// 1717 B. Madoka and Underground Competitions

fscanf(STDIN, '%u', $t);

while ($t--) {
    fscanf(STDIN, '%u %u %u %u', $n, $k, $r, $c);
    --$r;
    --$c;

    $mx = array_fill(0, $n, array_fill(0, $n, '.'));

    for ($i = 0; $i < $n; ++$i) {
        for ($j = ((($r + $c) % $k - $i % $k) + $k) % $k; $j < $n; $j += $k) {
            $mx[$i][$j] = 'X';
        }
    }

    foreach ($mx as $it) {
        fprintf(STDOUT, '%s'.PHP_EOL, implode(' ', $it));
    }
}
/*
1
6 3 1 1

1
6 3 1 6

 */


<?php
# 1144A
$input = fopen('php://stdin', 'rb');
$linesCount = (int)fgets($input);

for ($i = 0; $i < $linesCount; $i++) {
    $charMap = [];
    $symbols = str_split(trim(fgets($input)));
    sort($symbols);
    $limit = count($symbols) - 1;
    $valid = true;
    for ($j = 0; $j < $limit; $j++) {
        if (isset($charMap[$symbols[$j]]) || (ord($symbols[$j]) + 1 !== ord($symbols[$j + 1]))) {
            $valid = false;
            break;
        }
        $charMap[$symbols[$j]] = true;
    }
    echo ($valid ? 'YES' : 'NO') . PHP_EOL;
}

<?php
fscanf(STDIN,"%d",$tc);
while ($tc--)
{

	$str=trim(fgets(STDIN));
	$asciValue=array();
	for ($i=0; $i <strlen($str) ; $i++)
	{
		$asciValue[$i]=ord($str[$i]);
	}
   // print_r($asciiValue);//print_r($str);
	for ($i=0; $i <count($asciValue) ; $i++)
	{
			for ($j=0; $j <count($asciValue)-1 ; $j++)
			 {
                 if($asciValue[$i]<$asciValue[$j])
                 {
                 	$asciValue[$i]=$asciValue[$i]+$asciValue[$j];
                 	$asciValue[$j]=$asciValue[$i]-$asciValue[$j];
                 	$asciValue[$i]=$asciValue[$i]-$asciValue[$j];
                 }
			 }
	}
	$flag=0;
	for ($i=0; $i <count($asciValue)-1 ; $i++)
	{
		if($asciValue[$i+1]-$asciValue[$i]!=1)
		{
          echo "No\n";
          $flag=1;
          break;
		}
	}
    if($flag==0)
    	echo "Yes\n";

}


<?php
$f = trim(fgets(STDIN));
for($j = 0; $j < $f; $j++){
    $a = trim(fgets(STDIN));
    $b = explode(' ', trim(fgets(STDIN)));
    $all = 0;
    $count = 0;
    $max = 0;
    $min = 0;
    $bool = true;
    $bool2 = true;
    for($i = 0; $i < $a; $i++){
        if($b[$i] != -1){
            $all += $b[$i];
            $count++;
            if($bool || $max < $b[$i]){
                $max = $b[$i];
                $bool = false;
            }
            if($bool2 || $min > $b[$i]){
                $min = $b[$i];
                $bool2 = false;
            }
        }
    }
    if($bool && $bool2){
        $k = 5;
        $m = 0;
    } else {
        $k = round($all / $count);
         $m = $k - $min > $max - $k ? $k - $min : $max - $k;
    }

    echo $m." ".$k."\n";

}


<?php
$r = fopen( 'php://stdin', 'r' );
$s1 = trim(fgets($r));
$s2 = trim(fgets($r));
$a1 = array();
for ($i = 0; $i < strlen($s1); $i++) {
    $s = substr($s1, $i, 1);
    if (!isset($a1[$s])) {
        $a1[$s] = 1;
    }
    else {
        $a1[$s]++;
    }

}

$a2 = array();
for ($i = 0; $i < strlen($s2); $i++) {
    $s = substr($s2, $i, 1);
    if (!isset($a2[$s])) {
        $a2[$s] = 1;
    }
    else {
        $a2[$s]++;
    }

}

$iMatchCount = 0;
$iDismatchCount = 0;
foreach ($a1 AS $s => $i) {
    if (!isset($a2[$s])) {
        $iDismatchCount += $i;
    }
    else if ($a1[$s] == $a2[$s]) {
        $iMatchCount += $a2[$s];
    }
    else if ($a1[$s] - $a2[$s] > 0) {
        $iDiff = $a1[$s] - $a2[$s];
        $iMatchCount += $a2[$s];
        $iDismatchCount += $iDiff;
    }

}

echo $iMatchCount . ' ' . $iDismatchCount;
fclose($r);
exit();
?>


<?
$number = 0;
fscanf(STDIN, "%d\n", $number);
$ans = "";
while ($number > 1){
    if ($number > 5){
    	$ans = $ans . "11";
    	$number = $number - 4;
    }
    if ($number == 5){
	    $ans = "71" . $ans;
        $number = $number - 5;
    }
    if ($number == 4){
	    $ans = "11" . $ans;
        $number = $number - 4;
    }
    if ($number == 3){
	    $ans = "7" . $ans;
        $number = $number - 3;
    }
    if ($number == 2){
	    $ans = "1" . $ans;
        $number = $number - 2;
    }
}

fwrite(STDOUT, $ans);
?>

<?php
 list($n) = explode(" ", fgets(STDIN));
 $n = (int) $n;
if($n % 2 == 0)
echo 1;
else
 echo 7;
$n = $n/2 - 1;
$n = (int) $n;
 for ($i = 0; $i < $n; $i++)
 echo 1;
?>

<?php
	$stdin = STDIN;
	fscanf($stdin, "%d", $knt);
	if ($knt % 2 == 0) {
		echo 1;
		for ($i = 1; $i < $knt / 2; $i++) {
			echo 1;
		}
	} else {
		echo 7;
		for ($i = 2; $i < $knt / 2; $i++) {
			echo 1;
		}
	}
?>

<?php
  fscanf(STDIN, "%d\n", $number); // reads number from STDIN
  if ($number % 2 == 1) {
    echo 7;
    $number -= 3;
  }
  for ($i = 0; $i < $number; $i += 2) {
    echo 1;
  }
?>

<?php
 list($n) = explode(" ", fgets(STDIN));
 $n = (int) $n;
 $s = (int)7;
 $o = (int)1;
 if ($n % 2 == 1)
  echo $s;
 else
  echo $o;
 for ($i = 1; $i * 2 < $n - $n % 2; $i++)
  echo $o;
?>

<?php

fscanf(STDIN, "%d\n", $number);

$number = intval($number);

if ($number % 2 == 1) {
    printf("7");
    $number -= 3;
}
while ($number > 0) {
    printf("1");
    $number -= 2;
}
?>

<?
$number = 0;
fscanf(STDIN, "%d\n", $number);
$ans = "";
while ($number > 3){
	$ans = $ans . "11";
	$number = $number - 4;
}
if ($number == 3){
	$ans = "7" . $ans;
}
if ($number == 2){
	$ans = "1" . $ans;
}

fwrite(STDOUT, $ans);
?>

<?php
 list($n) = explode(" ", trim(fgets(STDIN)));
 $n = (int) $n;
 for ($i = 0; $n > 3; $i++){
    echo 1;
    $n -= 2;
 }
 if ($n == 3)
	echo 7;
 else
	echo 1;
?>

<?php
 list($n) = explode(" ", fgets(STDIN));
 $n = (int) $n;
if($n % 2 == 0)
echo 1;
else
 echo 7;
$n = $n/2 - 1;
 for ($i = 0; $i < $n; $i++)
 echo 1;
?>

<?php
 $n = fgets(STDIN);

 while ($n>=6) {
    echo 9;
    $n -= 6;
 }

 if ($n == 3) {
     echo 7;
 } else if ($n == 5) {
     echo 5;
 } else if ($n == 4) {
     echo 4;
 } else if ($n == 2) {
     echo 1;
 }
?>

<?php
//$a = trim(fgets(STDIN));
fscanf(STDIN, '%d', $a);

if ($a >= 4)
{
    for( $i = 1; $i <= $a/2; $i++ )
        echo 1;
}
else if( $a == 3 )
{
    echo 7;
}
else
{
    echo 1;
}
?>