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<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;

struct data{
	int cnt,hoge;
	char name[6];
};

int int_pow(int n,int m){
	int ans=1;
	while(m--) ans*=n;
	return ans;
}

data latte[5][14348907];

int main(){
	int N,hoge,n,len;
	char input[6];
	memset(latte,0,sizeof(latte));
	
	cin>>N;
	for(int i=0;i<N;i++){
		cin>>input;
		cin>>n;
		hoge=0;
		for(int j=0;input[j]!='\0';j++){
			hoge += (input[j]-'A')*int_pow(27,4-j);
		}
		//printf(" %d\n",hoge);
		len=strlen(input)-1;
		strcpy(latte[len][hoge].name,input);
		latte[len][hoge].cnt += n;
		latte[len][hoge].hoge = hoge;
	}
	for(int i=0;i<5;i++){
		for(int j=0;j<14348907;j++){
			if(latte[i][j].cnt==0) continue;
			printf("%s %d\n",latte[i][j].name,latte[i][j].cnt);
		}
	}
	return 0;
}

C++: #include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;
signed main() {
	int n; cin >> n;
	set<string>st;
	map<string, int>m;
	for (int i = 0; i < n; i++) {
		string s; int p;
		cin >> s >> p;
		if (st.lower_bound(s) == st.end() && *(st.lower_bound(s)) != s) {
			st.insert(s);
			m[s] = 0;
		}
		m[s] += p;
	}
	vector<pair<int, string>>v;
	for (string s : st)v.push_back(make_pair(s.size(), s));
	sort(v.begin(), v.end());
	for (pair<int, string> i : v)cout << i.second << ' ' << m[i.second] << endl;
}

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

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

pair<int, int>x[1000000];

int main() {
	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 < 15000000; i++) {
		if (k[i] > 0) {
			x[cnt].first = 200000000 - k[i];
			x[cnt].second = i;
			cnt++;
		}
	}
	sort(x, x + cnt);
	for (int i = 0; i < cnt; i++) {
		for (int j = 4; j >= 0; j--) {
			z = (x[i].second / (int)pow(27, j)) % 27;
			if (z >= 1) { cout << (char)('A' + z - 1); }
		}
		cout << ' ' << 200000000 - x[i].first << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

int n,x;
string str;

class data{
	public:
	string str;
	int x;
};

bool comp(const data &left,const data &right){
	return left.str<right.str;
}
bool comp2(const data &left,const data &right){
	if(left.x==right.x && left.str.size()==right.str.size())return left.str<right.str;
	if(left.x==right.x)return left.str.size()<right.str.size();
	return left.x>right.x;
}

vector<data> que;
vector<data> que2;

int main(void){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		cin >> str >> x;
		data q;
		q.str=str;
		q.x=x;
		que.push_back(q);
	}
	sort(que.begin(),que.end(),comp);
	string str2=que[0].str;
	int x=que[0].x;
	for(int i=1;i<que.size();i++){
		if(que[i].str==str2)x+=que[i].x;
		else{
			data q;
			q.str=str2;
			q.x=x;
			que2.push_back(q);
			str2=que[i].str;
			x=que[i].x;
		}
	}
	data q;
	q.str=str2;
	q.x=x;
	que2.push_back(q);
	sort(que2.begin(),que2.end(),comp2);
	for(int i=0;i<que2.size();i++)cout << que2[i].str << " " << que2[i].x << endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
typedef pair<int, string> Pis;

int main()
{
  int n;
  map<Pis, int> m;

  cin >> n;

  for(int i=0; i<n; i++) {
    string s; int tmp;
    cin >> s >> tmp;
    m[Pis(s.size(), s)] += tmp;
  }

  for(map<Pis, int>::iterator it = m.begin(); it != m.end(); it++) {
    cout << (*it).fr.sc << " " << (*it).sc << endl;
  }
}

C++: #include <stdio.h>
#include <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))

const int INF = 1000000000;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4]={-1, 0, 1, 0}, dy[4]={0, -1, 0, 1};
using namespace std;
//typedef pair<int, int> P;

/** Problem0589 : Production **/
int main()
{
	int N;
	map<string, int> p[5];
	cin>>N;
	rep(i, N) {
		string s; int a;
		cin>>s>>a;
		p[s.size()-1][s] += a;
	}
	
	rep(i, 5) {
		map<string, int>::iterator it = p[i].begin();
		for (; it != p[i].end(); it++) {
			cout << it->first << " " << it->second << endl;
		}
	}
}

C++: #include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;
signed main() {
	int n; cin >> n;
	set<string>st;
	map<string, int>m;
	for (int i = 0; i < n; i++) {
		string s; int p;
		cin >> s >> p;
		if (*(st.lower_bound(s)) != s) {
			st.insert(s);
			m[s] = p;
		}
		else m[s] += p;
	}
	vector<pair<int, string>>v;
	for (string s : st)v.push_back(make_pair(s.size(), s));
	sort(v.begin(), v.end());
	for (pair<int, string> i : v)cout << i.second << ' ' << m[i.second] << endl;
}

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

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(unsigned int i=1; i<=5; i++) {
        for(it=hash.begin(); it!=hash.end(); it++) {
            if(it->first.size() != i) continue;
            cout<<it->first<<" "<<it->second<<endl;
        }
    }
}

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

int int_pow(int n,int m){
	int ans=1;
	while(m--) ans*=n;
	return ans;
}

int latte[5][14348907];

int main(){
	int N,hoge,n,len;
	char input[6];
	memset(latte,0,sizeof(latte));
	
	cin>>N;
	for(int i=0;i<N;i++){
		cin>>input;
		cin>>n;
		hoge=0;
		for(int j=0;input[j]!='\0';j++){
			hoge += (input[j]-'A')*int_pow(27,4-j);
		}
		//printf(" %d\n",hoge);
		len=strlen(input)-1;
		latte[len][hoge] += n;
	}
	for(int i=0;i<5;i++){
		for(int j=0;j<14348907;j++){
			if(latte[i][j]==0) continue;
			for(int k=0;k<=i;k++){
				printf("%c",j/int_pow(27,4-k)%27+'A');
			}
			printf(" %d\n",latte[i][j]);
		}
	}
	return 0;
}

C++: #include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;
signed main() {
	int n; cin >> n;
	set<string>st;
	map<string, int>m;
	for (int i = 0; i < n; i++) {
		string s; int p;
		cin >> s >> p;
		if (!st.size() || *(st.lower_bound(s)) != s) {
			st.insert(s);
			m[s] = p;
		}
		else m[s] += p;
	}
	vector<pair<int, string>>v;
	for (string s : st)v.push_back(make_pair(s.size(), s));
	sort(v.begin(), v.end());
	for (int i = 0; i < v.size(); i++)cout << v[i].second << ' ' << m[v[i].second] << endl;
}