fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define mp make_pair
  4. #define pb push_back
  5. bool cmp(const pair<string,int> &a,const pair<string,int> &b)
  6. {
  7. if (a.second==b.second)
  8. return a.first>b.first;
  9. return a.second<b.second;
  10. }
  11. int main()
  12. {
  13. int t;
  14. cin>>t;
  15. while (t--)
  16. {
  17. int n;
  18. cin>>n;
  19. vector<pair<string,int> > v;
  20. for (int i=0; i<n; i++)
  21. {
  22. string s;
  23. int num,flag=0;
  24. cin>>s>>num;
  25. for (int i=0; i<v.size(); i++)
  26. if (v[i].first==s)
  27. {
  28. v[i].second+=num;
  29. flag=1;
  30. break;
  31. }
  32. if (!flag)
  33. v.pb(mp(s,num));
  34. }
  35. sort(v.begin(),v.end(),cmp);
  36. cout<<v[v.size()-1].first<<" "<<v[v.size()-1].second<<endl;
  37. }
  38. return 0;
  39. }
Success #stdin #stdout 0s 3284KB
stdin
1
6
bhagat 5
nishant 8
suraj 7
praveen 8
bhagat 6
nishant 3
stdout
bhagat 11