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++: // 2019/03/05 Tazoe

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main()
{
	int n;
	cin >> n;

	map<string, int> sum;

	for(int i=0; i<n; i++){
		string s;
		int m;
		cin >> s >> m;

		switch(s.size()){
		case 1:
			s = "1"+s;
			break;
		case 2:
			s = "2"+s;
			break;
		case 3:
			s = "3"+s;
			break;
		case 4:
			s = "4"+s;
			break;
		case 5:
			s = "5"+s;
			break;
		default:
			break;
		}

		if(sum.find(s) != sum.end()){
			sum[s] += m;
		}
		else{
			sum[s] = m;
		}
	}

	map<string, int>::iterator p;
	for(p = sum.begin(); p != sum.end(); p++){
		string s = p->first;
		s.erase(0, 1);
		cout << s << ' ' << p->second << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<string>
#include<map>
#include<algorithm>
#define Map_ map<string, int>
#define Map map<int, Map_ >
#define F first
#define S second

using namespace std;

Map data;

void insert(const string& s, const int& x){
  Map::iterator it=data.find(s.size());
  if(it==data.end()){
    Map_ tmp;
    tmp.insert(make_pair(s,x));
    data.insert(make_pair(s.size(),tmp));
    return;
  }
  Map_::iterator jt=it->S.find(s);
  if(jt==it->S.end()) it->S.insert(make_pair(s,x));
  else jt->S+=x;
}

void print(){
  for(Map::iterator it=data.begin(); it!=data.end(); ++it){
    for(Map_::iterator jt=it->S.begin(); jt!=it->S.end(); ++jt){
      cout << jt->F << " " << jt->S << endl;
    }
  }
}

int main(){
  int n, x;
  string s;
  data.clear();
  cin >> n;
  for(int i=0; i<n; ++i){
    cin >> s >> x; insert(s,x);
  }
  print();
  return 0;
}

C++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }



int n, a;
string str;

map<string,int> ma[6];

int main() {
    while (cin >> n) {
        for (int i = 0; i < 6; ++i) ma[i].clear();
        for (int i = 0; i < n; ++i) {
            cin >> str; cin >> a;
            ma[str.size()][str] += a;
        }
        
        for (int i = 0; i < 6; ++i) {
            EACH(it,ma[i]) {
                cout << it->first << " " << it->second << endl;
            }
        }
    }
    return 0;
}

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

bool compare(const string& lStr, const string& rStr) {
	if(lStr.size()==rStr.size()) return lStr < rStr;
	else return lStr.size() < rStr.size();
}

int main(void){
	int n;
	cin>>n;
	vector<string> p_name;
	map<string,int> c;
	for(int i=0;i<n;++i){
		string p;int s;
		cin>>p>>s;
		if(c.find(p)!=c.end()){
			c[p]+=s;
		}else{
			p_name.push_back(p);
			c[p]=s;
		}
	}
	sort(p_name.begin(),p_name.end(),compare);
	for(int i=0;i<p_name.size();++i){
		cout<<p_name[i]<<" "<<c[p_name[i]]<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef pair<string,int> P;
int n;
vector<P> p;
int main(){
  cin>>n;
  p.resize(n);
  for(int i=0;i<n;i++)
    cin>>p[i].first>>p[i].second;
  sort(p.begin(),p.end());
  for(int i=0;i<n;i++){
    if(i+1<n&&p[i].first==p[i+1].first){
      p[i+1].second+=p[i].second;
    }else{
      cout<<p[i].first<<' '<<p[i].second<<endl;
    }
  }
  return 0;
}

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

using namespace std;

struct P {
	string a;
	int b;
};

int main() {
	int n;
	cin >> n;
	vector<P> p;
	string name;
	int number;
	cin >> name >> number; 
	P ab;
	ab.a = name;
	ab.b = number;
	p.push_back(ab);
	for (int i = 1; i < n; i++) {
		cin >> name >> number;
		bool hantei = true;
		for (int j = 0; j < p.size(); j++) {
			if (p[j].a == name) {
				p[j].b += number;
				hantei = false;
			}
		}
		if (hantei) {
			ab.a = name;
			ab.b = number;
			p.push_back(ab);
		}
	}
	for (int i = p.size()-1; i > 0; i--) {
		for (int j = 0; j < i; j++) {
			if ((p[j].a).size() > (p[j+1].a).size()) {
				swap(p[j], p[j+1]);
			} else if ((p[j].a).size() == (p[j+1].a).size()) {
				for (int k = 0; k < (p[j].a).size(); k++) {
					if ((p[j].a)[k] > (p[j+1].a)[k]) {
						swap(p[j], p[j+1]);
						break;
					}
					if ((p[j].a)[k] != (p[j+1].a)[k]) {
						break;
					}
				}
			}
		}
	}
	for (int i = 0; i < p.size(); i++) {
		cout << p[i].a << " " << p[i].b << endl;
	}
	return 0;
}

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

using namespace std;
typedef pair<int, string> p;

void solve()
{
	int n;
	while(cin >> n)
	{
		map<p, int> list;
		for(int i = 0; i < n; ++i)
		{
			string product;
			cin >> product;
			int num;
			cin >> num;
			list[p(product.size(), product)] += num;
		}
		map<p, int>::iterator it = list.begin();
		while(it != list.end())
		{
			cout << it->first.second << " " << it->second << endl;
			++it;
		}
	}
}

int main()
{
	solve();
	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;	cin >> n;
}