Go: package main

import (
	"fmt"
	"sort"
)

type node struct {
	Key string
	Num int
}

func main() {
	var n int
	fmt.Scan(&n)
	m := make(map[string]int)
	for i := 0; i < n; i++ {
		var s string
		var num int
		fmt.Scan(&s, &num)
		if _, ok := m[s]; !ok {
			m[s] = 0
		}
		m[s] += num
	}
	list := make([]node, 0, len(m))
	for k, v := range m {
		list = append(list, node{Key: k, Num: v})
	}
	sort.Slice(list, func(i, j int) bool {
		if len(list[i].Key) == len(list[j].Key) {
			return list[i].Key < list[j].Key
		} else {
			return len(list[i].Key) < len(list[j].Key)
		}
	})
	for _, v := range list {
		fmt.Println(v.Key, v.Num)
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin >> n;
  map<string,int> a;
  int k=1;
  vector<int> co(n,0);
  vector<string> rema;
  for(int i=0;i<n;i++){
    string x;
    int y;
    cin >> x >> y;
    if(a[x]==0){
      a[x]=k;
      k++;
      rema.push_back(x);
    }
    co[a[x]-1]+=y;
  }
  sort(rema.begin(),rema.end());
  for(int j=1;j<=4;j++){
    for(int i=0;i<rema.size();i++){
      if(rema[i].size()==j)cout << rema[i] << " " <<co[a[rema[i]]-1] << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define P pair<string, int>
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())

class C{
  public:
    string first;
    int second;
    C(string _f, int _s){ first = _f; second = _s; }
    C(){}
    bool operator < (const C &c) const {
      if (first.length() < c.first.length()) return true;
      if (first.length() > c.first.length()) return false;
      return first < c.first;
    }
};

int main() {
  int N; cin >>N;
  map<string, int> m;
  REP(i, N){
    string s;
    int num;
    cin >>s >>num;
    m[s] += num;
  }
  vector<C> v;
  for(map<string, int>::iterator it = m.begin(); it != m.end(); ++it) v.push_back( C(it->first, it->second) );
  SORT(v);
  REP(i, v.size()) cout <<v[i].first <<" " <<v[i].second <<endl;
  return 0;
}

C++: #include<iostream>
#include<map>
#include<string>
using namespace std;

map<string,int>ans;;

int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		string str;
		int x;
		cin>>str>>x;
		ans[str]+=x;
	}
	int count=1;
	while(!ans.empty()){
		for(map<string,int>::iterator itr=ans.begin();itr!=ans.end();itr++){
			if(itr->first.size()==count){
				cout<<(itr->first)<<" "<<(itr->second)<<endl;
				ans.erase(itr);
			}
		}
		count++;
	}
}

C++: #include <cstdlib>
#include <iostream>
#include <map>
using namespace std;

struct compare {
	bool operator() (const string& a, const string& b) const {
		return a.size() != b.size() ? a.size() < b.size() : a < b;
	}
};

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;

	map<string, int, compare> production;
	for(int i = 0; i < n; ++i) {
		string name;
		int number;
		cin >> name >> number;
		production[name] += number;
	}

	for(const auto& e : production) {
		cout << e.first << " " << e.second << endl;
	}

	return EXIT_SUCCESS;
}

C++: #include<stdio.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<string.h>
#include<queue>

using namespace std;

typedef pair<int,int> pii;
typedef pair<int,string> pis;
typedef pair<pis,int> tri;
#define rep(i,j) for(int i=0;i<(j);i++)
#define reps(i,j,k) for(int i=j;i<=k;i++)

int main(){
	int n,m,p;
	string s;
	tri t;
	vector<tri>::iterator po;
	pis pi;
	vector< tri > v;
	scanf("%d",&n);
	rep(i,n){
		cin >> s;
		cin >> m;
		pi = make_pair(s.length(),s);
		po = lower_bound(v.begin(),v.end(),make_pair(pi,0));
		if(po != v.end() && po->first.second == s)po->second += m;
		else v.insert(po,make_pair(pi,m));
	}
	rep(i,v.size())printf("%s %d\n",v[i].first.second.c_str(),v[i].second);
	return 0;
}