List存值,Map查找,Set排序:

#include <iostream>
#include <set>
#include <list>
#include <unordered_map>

using namespace std;

int main() {
    int n;
    while (cin >> n) {
        int key, value;
        list<int> List;
        unordered_map<int, list<int>::iterator> Map;
        set<int> Set;
        while (n--) {
            cin >> key >> value;
            auto iter = Map.find(key); 
            if (iter != Map.end()) {
                *iter->second += value;
            }
            else {
                List.push_front(value);
                Map.insert({ key, List.begin() });
                Set.insert(key);
            }
        }
        for (int i : Set) {
            cout << i << " " << *Map[i] << endl;
        }
    }
}