English
Desc
The winner of the card game popular in Berland “Berlogging” is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line “name score”, where name is a player’s name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It’s guaranteed that at the end of the game at least one player has a positive number of points.
Input
The first line contains an integer number n (1 ≤ n ≤ 1000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in “name score” format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive.
Output
Print the name of the winner.
Examples
input
3
mike 3
andrew 5
mike 2
output
andrew
中文
简介
有一个非常受欢迎的比赛,有如下规则。如果比赛结束后只有他一个人的积分最高,则他是胜利者。每轮都会给出一个人的名字和他的获得的积分(有正有负),如果有二个或二个以上的人积分都是最高的,那么取谁先到达最高分的选手为胜利者。
输入格式
第一行给出一个n(1<=n<=1000),接下来n行
name score
分别代表名字和分数
输出格式
输入胜利者的名字
测试用例
测试输入
3
andrew 3
andrew 2
mike 5
测试输出
andrew
代码
#include<iostream>
#include<string>
#include<map>
using namespace std;
map<string, int> m;
map<string, int> m2;
string name[1001];
int num[1001];
int main()
{
int n;
cin >> n;
int max = -0x7f7f;
string maxName;
for (int i = 0; i < n; i++) {
cin >> name[i] >> num[i];
m[name[i]] += num[i];
}
map<string, int>::iterator it;
for (it = m.begin(); it != m.end(); it++) {
if (it->second > max) {
max = it->second;
}
}
for (int i = 0; i < n; i++) {
if (m[name[i]] == max) {
m2[name[i]] += num[i];
if (m2[name[i]] >= max) {
cout << name[i] << endl;
break;
}
}
}
return 0;
}