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<vector>
#include<string>
#include<map>
using namespace std;

int main() {
	map<string, int>s;
	int a;
	cin >> a;
	for (int b = 0; b < a; b++) {
		string c; int d;
		cin >> c >> d;
		char f = c.length() + 96;
		c = f + c;
		s[c];
		s[c] += d;
	}
	for (auto e = s.begin(); e != s.end(); e++) {
		for (int h = 1; h < e->first.length(); h++)cout << e->first[h];
		cout << " " << e->second << endl;
	}
}

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

int main(void){

  int n,quantity,tmp,len=1;
  string name;
  map<string, int> product;

  cin >> n;
  
  for(int i = 0; i < n; i++){
    cin >> name >> quantity;
    //テ」ツ?凖」ツ?ァテ」ツ?ォテゥツ?催ィツ、ツ?」ツ?凖」ツつ凝ィツ」ツステ・ツ督?・ツ青催」ツ?古ヲツ慊嘉」ツ?」テ」ツ?淌」ツ?ィテ」ツ??
    if(product.find(name) != product.end()){
      tmp = product.find(name)->second;
      tmp += quantity;
      product.erase(name);
      product.insert(make_pair(name,tmp));
    }else{ //ティツ」ツステ・ツ督?・ツ青催」ツ?古」ツ?ェテ」ツ??」ツ?ィテ」ツ??
      product.insert(make_pair(name,quantity));
    }
  }
  
  while(product.empty() == false){ //テ」ツδ榲」ツδε」ツδ療」ツ?古ァツゥツコテ」ツ?ァテ」ツ?ッテ」ツ?ェテ」ツ??」ツ?ィテ」ツ?催・ツョツ淌ィツ。ツ?
    for (map<string,int>::iterator ite=product.begin(); ite!=product.end(); ite++){
      string str = ite->first;
      if(str.size() == len){
	cout << ite->first << " " << ite->second << endl;
	product.erase(str); //ティツ。ツィテァツ、ツコテ」ツ?療」ツ?淌」ツつ嘉・ツ可甘ゥツ卍、
      }
    }
    len++;
  }
  
  
  return 0;
}

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

using namespace std;

char s[10];

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

C++: #include <iostream>
#include <vector>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
#define rep(i,a) for(int i = 0 ; i < a ; i ++)

bool cmp(pair<string,int> a,pair<string,int> b){
  return (a.first.size()<b.first.size()?true:false);
}


int main(void){
  map<string,int> m;
  int n;
  string s;int c;
  cin>>n;
  rep(i,n){
    cin>>s>>c;
    m[s]+=c;
  }
  vector<pair<string,int> > w;
  for(map<string,int>::iterator it=m.begin();it!=m.end();it++){
    w.push_back(make_pair(it->first,it->second));
  }
  sort(w.begin(),w.end(),cmp);
  for(vector<pair<string,int> >::iterator it=w.begin();it!=w.end();it++){
    cout<<it->first<<" "<<it->second<<endl;
  }
}

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

using namespace std;

class S{
public:
  string s;
  int t;
  S(){}
  S(string s, int t): s(s), t(t) {}
  bool operator < (const S a) const {
    if(s.length() == a.s.length()) return s < a.s;
    return s.length() < a.s.length();
  }
};

main(){
  map<string, int> tmp;
  int n;
  cin >> n;
  for(int i=0;i<n;i++){
    string s;
    int t;
    cin >> s >> t;
    tmp[s] += t;
  }
  vector<S> ans;
  map<string, int>::iterator ite = tmp.begin();
  while(ite != tmp.end()){
    ans.push_back(S(ite->first, ite->second));
    ite++;
  }
  sort(ans.begin(), ans.end());
  for(int i=0;i<ans.size();i++) cout << ans[i].s << ' ' << ans[i].t << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;

bool comparator(const string& a, const string& b) {
  if (a.size() == b.size()) return a < b;
  else                      return a.size() < b.size();
}

int solve(int n) {
  map<string, int, decltype(comparator)*> orders(comparator);
  //map<string, int, bool(*)(const string&, const string&)> orders(comparator);

  for (auto i = 0; i < n; ++i) {
    string name;
    int num;
    cin >> name >> num;
    orders[name] += num;
  }

  for (auto order : orders) {
    cout << order.first << " " << order.second << endl;
  }

}

int main() {
  int n;
  cin >> n;
  solve(n);
  return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cctype>
#include<map>
#include<vector>
using namespace std;
bool cmp(string l,string r);
int main(){
	map<string, int> Data;
	
	int n;
	scanf("%d",&n);
	
	for(int i = 0 ; i < n; i++){
		string str;
		int x;
		cin >> str >> x;
		Data[str] += x;
	}
	map<string ,int> :: iterator it =  Data.begin();
	vector<string> ans_str;
	
	for(it == Data.begin(); it != Data.end(); it++){
		ans_str.push_back((*it).first);
	}
	sort(ans_str.begin(),ans_str.end(),cmp);
	
	for(int i = 0; i < ans_str.size(); i++){
		cout << ans_str[i] << " " << Data[ans_str[i]] <<endl;
	}
	
	return 0;
}
bool cmp(string l,string r){
	bool f;
	if(l.size() > r.size())f = false;
	if(l.size() < r.size())f = true;
	else (l > r) ? f = false : f = true;
	
	return f;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <cstring>
#include <cmath>
#include <cstdio>
 
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, n) for(int i = 0; i < (n); i++)
#define MP make_pair
#define X first
#define Y second
#define all(v) v.begin(), v.end()
#define rev(v) v.rbegin(), v.rend()

using namespace std;
 
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

const int INF = 1<<29;

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

	rep(i, n){
		string s;
		int x;
		cin >> s >> x;
		m[s] += x;
	}

	map<string, int>::iterator it;
	for(int i = 1; i <= 5; i++){
		for(it = m.begin(); it != m.end(); it++){
			if(it->first.size() == i) cout << it->first << ' ' << it->second << endl;
		}
	}

    return 0;
}

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[100000000];

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;
}