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 <cstdio>
#include <map>
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<queue>
using namespace std;

struct SGreater{
  bool operator() (string a,string b){
    if(a.size()!=b.size())
      return a.size()>b.size();
    else
      return a>b;
  }
};

int main()
{
  int n;
  cin>>n;
  map<string,int> data;
  priority_queue<string, vector<string>, SGreater> qu;
  for(;n--;)
    {
      string s;
      int x;
      cin>>s>>x;
      if(data[s])
	{
	  data[s]+=x;
	}
      else
	{
	  data[s]=x;
	  qu.push(s);
	}
    }
  while(!qu.empty())
    {
      cout<<qu.top()<<" "<<data[qu.top()]<<endl;
      qu.pop();
    }
}

C++: #include <stdio.h>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;

bool comp(const pair<string, int> &a, const pair<string, int> &b) {
  if(a.first.length() < b.first.length()) return true;
  if(a.first.length() > b.first.length()) return false;
  if(a.first < b.first) return true;
  return false;
}

int main(void) {
  int n, i;
  vector< pair<string, int> > v;
  scanf("%d", &n);
  for(i = 0; i < n; ++i) {
    string s;
    int m;
    cin >> s >> m;
    vector< pair<string, int> >::iterator it;
    for(it = v.begin(); it != v.end(); ++it) {
      if(s == (*it).first){
        (*it).second += m;
        break;
      }
    }
    if(it == v.end()) {
      pair<string, int> p = make_pair(s, m);
      v.push_back(p);
    }
  }
  sort(v.begin(), v.end(), comp);
  vector< pair<string, int> >::iterator it;
  for(it = v.begin(); it != v.end(); ++it) {
    cout << (*it).first << " " << (*it).second << "\n";
  }
  return 0;
}

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

using namespace std;

int main() {
    int n;
    map<string, int> products;
    vector<pair<string, int> > p;
    cin>>n;
    //vector<pair<string, int> > products;
    for (int i = 0; i < n; ++i) {
        string name;
        int num;
        cin>>name>>num;

        if (products[name] > 0) {
            products[name] += num;
        }
        else {
            products[name] = num;
        }
    }

    for (auto& e : products) {
        p.push_back(make_pair(e.first, e.second));
    }

    sort(p.begin(), p.end(), [](auto& lhs, auto& rhs) -> bool {
            if (lhs.first.size() < rhs.first.size()) return true;
            else if (lhs.first < rhs.first) return true;
            else return false;
            });


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

    return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
int n;
map<string,int> m;
vector<pair<pair<int,string>,int> > v;
int main(void){
  cin >> n;
  for(int i = 0; i < n; i++){
    string s;
    int a;
    cin >> s >> a;
    if(m.find(s) == m.end()){
      m[s] = v.size();
      v.push_back(make_pair(make_pair(s.size(),s),-a));
    }else{
      v[m[s]].second -= a;
    }
  }
  sort(v.begin(),v.end());
  for(int i = 0; i < (int)v.size(); i++){
    cout << v[i].first.second << " " << -v[i].second << endl;
  }
}

C++: #include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<string>
#define F first
#define S second
using namespace std;

int main(){

  int n;
  string s;
  int d,k;
  typedef pair < int, pair < string, int > > P;
  vector < P > vec;
  P p;

  cin >> n;
  for(int i=0;i<n;i++){
    cin >> s >> d;
    p.F = s.size();
    p.S.F = s;
    p.S.S = d;
    vec.push_back( p );
  }

  sort(vec.begin(),vec.end());

  for(int i=0;i<n-1;i++){
    if(vec[i].S.F == vec[i+1].S.F) vec[i+1].S.S += vec[i].S.S;
    else cout << vec[i].S.F << " " << vec[i].S.S << endl;
  }

  cout << vec[n-1].S.F << " " << vec[n-1].S.S << endl;

}

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);
			}
			cout << ' ';
			cout << k[i] << endl;
		}
	}
	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){
  if(a.first.size()<b.first.size())return true;
  if(a.first.size()==b.first.size() && a.first<b.first)return true;
  return 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;
  }
}