http://codeforces.com/contest/2/problem/A
#include <iostream>
#include <map>
#include <string>
#include <string.h>
#include <set>
using namespace std;
char mm[1005][50];
char name[1005][50];
int main()
{
int n;
int const INF=100000;
int score[1005],maxn;
char str[50];
int ans[1050];
while(cin>>n)
{
maxn=-1005;
map<string,int>mp;
map<string,int>::iterator it;
set<string>Name;
for(int i=0; i<n; i++)
{
cin>>str>>score[i];
strcpy(name[i],str);
if(mp.find(str)!=mp.end())
mp[str]=mp[str]+score[i];
else
mp[str]=score[i];
}
int k=0;
for(it=mp.begin();it!=mp.end();it++)
{
if(it->second>maxn)
maxn=it->second;
}
for(int i=n-1;i>=0;i--)
{
if(mp[name[i]]==maxn&&Name.find(name[i])==Name.end())
{
strcpy(mm[k++],name[i]);
Name.insert(name[i]);
}
}
memset(ans,0,sizeof(ans));
for(int j=0;j<n;j++)
{
for(int i=0;i<k;i++)
{
if(strcmp(name[j],mm[i])==0)
{
ans[i]+=score[j];
}
if(ans[i]>=maxn)
{
cout<<mm[i]<<endl;
goto endW;
}
}
}
endW:;
}
return 0;
}