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;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
typedef complex<double> P;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 29;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };

int main() {
	int n;
	scanf( "%d", &n );
	map<string, int> m;
	for ( int i = 0; i < n; i++ ) {
		string s;
		int a;
		cin >> s >> a;
		m[ s ] += a;
	}
	vector<pair<pair<int, string>, int> > vp;
	for ( map<string, int>::iterator i = m.begin(); i != m.end(); ++i ) {
		vp.push_back( make_pair( make_pair( i->first.size(), i->first ), i->second ) );
	}
	sort( vp.begin(), vp.end() );
	for ( int i = 0; i < vp.size(); i++ ) {
		printf( "%s %d\n", vp[ i ].first.second.c_str(), vp[ i ].second );
	}
	return 0;
}

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

int k[16000000];
int n, a, z, cnt;
string S;

int main() {
	memset(k, 0, sizeof(k));
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> S;
		z = 0;
		for (int j = 0; j < S.size(); j++) {
			z += (int)(S[j] - 'A' + 1)*pow(27, S.size() - j - 1);
		}
		cin >> a;
		k[z] += a;
	}
	for (int i = 0; i < 16000000; i++) {
		if (k[i] >= 1) {
			for (int j = 4; j >= 0; j--) {
				z = (int)(i / (int)pow(27, j)) % 27;
			}
			if (z >= 1) {
				cout << (char)(z + 'A' - 1) << endl;
			}
			cout << ' ';
			cout << k[i] << endl;
		}
	}
	return 0;
}

C++: //51
#include<iostream>
#include<map>
#include<string>

using namespace std;

struct Cmp{
  bool operator()(string a,string b)const{
    if(a.size()!=b.size()){
      return a.size()<b.size();
    }else{
      return a<b;
    }
  }
};

int main(){
  int n;
  cin>>n;
  map<string,int,Cmp> m;
  while(n--){
    string k;
    int v;
    cin>>k>>v;
    m[k]+=v;
  }
  for(map<string,int,Cmp>::iterator it=m.begin();it!=m.end();it++){
    cout<<it->first<<' '<<it->second<<endl;
  }
  return 0;
}

C++: #include <stdio.h>
#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>

using namespace std;

struct Info{
	char word[6];
	int num,length;
};

bool strCmp(char* base, char* comp){
	int length1 = 0,length2 = 0;
	for(int i=0;base[i] != '\0'; i++)length1++;
	for(int i=0;comp[i] != '\0'; i++)length2++;
	if(length1 != length2)return false;

	for(int i=0;base[i] != '\0'; i++){
		if(base[i] != comp[i])return false;
	}
	return true;
}

void strcpy(char* to,char* str){
	for(int i=0;str[i] != '\0';i++){
		to[i] = str[i];
		to[i+1] = '\0';
	}
}

int strCmp2(char* left,char* right){
	int i;

	if(strCmp(left,right))return 3;

	for(i=0;left[i] != '\0' && right[i] != '\0'; i++){
		if(left[i] != right[i]){
			if(left[i] < right[i])return 1;
			else{
				return 2;
			}
		}
	}
	if(left[i] == '\0')return 1;
	else{
		return 2;
	}
}


int main(){

	int length;
	char buf[6];
	bool Found;

	int n,index,tmp;

	scanf("%d",&n);

	Info* info = (Info*)malloc(sizeof(Info)*n);

	for(int i = 0; i < n; i++){
		info[i].length = 0;
		info[i].num = 0;
	}

	index = 0;
	for(int i = 0; i < n; i++){
		scanf("%s %d",buf,&tmp);

		Found = false;

		for(int k = 0; k < index; k++){
			if(strCmp(buf,info[k].word)){
				info[k].num += tmp;
				Found = true;
				break;
			}
		}
		if(!Found){
			strcpy(info[index].word,buf);
			info[index].num += tmp;
			for(length = 0;buf[length] != '\0';length++);
			info[index].length = length;
			index++;
		}
	}

	for(int i = 1;i <= index-1; i++){
		for(int k = index-1; k >= i; k--){
			if(info[k].length < info[k-1].length)swap(info[k],info[k-1]);
			else if(info[k].length == info[k-1].length && strCmp2(info[k].word,info[k-1].word) == 1)swap(info[k],info[k-1]);
		}
	}

	for(int i = 0; i < index; i++){
		printf("%s %d\n",info[i].word,info[i].num);
	}


    return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <limits>
#include <fstream>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  map<string, int> mp;
  FOR(i,0,n){
      string s;
      int x;
      cin >> s >> x;
      mp[s] += x;
  }
  for(auto m : mp){
      cout << m.first <<  " " << m.second << endl;
  }
}

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

bool operator<(string s, string t) {
    if(s.size() != t.size()) return s.size() < t.size();
    for(int i=0; i<s.size(); i++) {
        if(s[i]==t[i]) continue;
        return s[i]<t[i];
    }
    return 0;
}

int main()
{
    map<string, int> hash;
    map<string, int>::iterator it;
    int n;
    string name;
    cin>>n;
    while(cin>>name>>n) hash[name] += n;
    for(it=hash.begin(); it!=hash.end(); it++) cout<<it->first<<" "<<it->second<<endl;
}

C++: #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <map>

using namespace std;

char s[10];

int main()
{
	map<pair<int,string>,int> m;
	int n;
	scanf("%d",&n);
	for(int i = 0; i < n; i++)
	{
		int w;
		scanf("%s %d",s,&w);
		int l = strlen(s);
		if(m.end() != m.find(make_pair(l,s)))
		{
			m[make_pair(l,s)] += w;
		}
		else
		{
			m[make_pair(l,s)] = w;
		}
	}
	map<pair<int,string>,int>::iterator it = m.begin();
	while(it != m.end())
	{
		printf("%s %d\n",(*it).first.second.c_str(),(*it).second);
		it++;
	}
	return 0;
}