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>
#define rep(i,j,k) for(int i=(int)j;i<(int)k;i++)
#define itrep(i,x) for(auto i=(x).begin(); i!=(x).end();i++)
#define Sort(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define vi vector<int>
#define INF (int)1e9
#define INFL 1e18
#define MOD 1000000007
#define pb push_back
#define MP make_pair
#define PI 3.1415926535
typedef long long int ll;
typedef std::pair<int,int> P;
int D=1;
int dx[4]={0,1,0,-1},dy[4]={-1,0,1,0};

using namespace std;

int main(){
    int n;
    cin>>n;
    map<string,int> mp[6];
    rep(i,0,n){
        string s;
        int n;
        cin>>s>>n;
        mp[s.size()-1][s]+=n;
    }
    
    rep(i,0,6){
        for(auto x:mp[i]){
            cout<<x.fi<<" "<<x.se<<endl;
        }
    }
    
    
    return 0;
}

C++: #include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int 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 (int i = 0; i < v.size(); i++)cout << v[i].second << ' ' << m[v[i].second] << endl;
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) begin(r),end(r)
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int inf = ((1<<30)-1)*2+1 ;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}
template<typename T,typename U>
ll FindErase(T &v,U tar){
  ll cnt = 0;
  for(auto it = v.begin(); it != v.end();){
    if(*it == tar){
      it = v.erase(it);
      ++cnt;
    }
    else{
      ++it;
    }
  }
  return cnt;
}
using psi = pair<string,int>;
struct Comp{
  bool operator()(psi a, psi b) const{
    if(a.fi.size() == b.fi.size()) return a.fi < b.fi;
    return a.fi.size() < b.fi.size();
  }
};

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  string s;
  int x;
  map<string,int> m;
  rep(i,n){
    cin >> s >> x;
    m[s] += x;
  }
  vector<pair<string,int>> v(all(m));
  sort(all(v),Comp());
  rep(i,v.size()){
    cout << v[i].fi << ' ' << v[i].se << endl;
  }
  return 0;
}

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

int main(){
  int i;
  int n;
  map<string, int> p;
  cin >> n;
  int num;
  string name;
  map<string, int>::iterator it;
  for(i = 0; i < n; i++){
    cin >> name >> num;
    if(p.find(name) != p.end()) p[name] += num;
    else p[name] = num;
  }

  for(i=1; i<=5; i++){
    it = p.begin();
    while( it != p.end()){
      if(it->first.size() == i){
	cout <<it->first << " " << it->second << endl;
      }
      it++;
    }
  }
  return 0;
}

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

typedef struct{
	char name[6];
	long num;
}product;

product p[12000000];

int main(){
	long n, now=0;
	
	scanf(" %ld", &n);
	for(long i=0; i<n; ++i){
		char s[6];
		long a;
		scanf(" %s %ld", s, &a);
		 
		bool find=false;
		for(long j=0; j<now; ++j){
			if(strcmp(p[j].name, s)==0){
				find=true;
				p[j].num+=a;
				break;
			}
		}
		
		if(!find){
			strcpy(p[now].name, s);	
			p[now].num=a;
			++now;
		}
	}
	
	for(long i=0; i<now; ++i){
		for(long j=i; j<now; ++j){
	
			if(strlen(p[i].name) > strlen(p[j].name)) swap(p[i], p[j]);
			else if(strlen(p[i].name) == strlen(p[j].name)){
				for(int k=0; k<strlen(p[i].name); ++k){
					if(p[i].name[k] > p[j].name[k]){
						swap(p[i], p[j]);
						break;
					}
				}		
			}		
			
		}
	}
	
	for(long i=0; i<now; ++i)
		cout << p[i].name << " " << p[i].num << endl;
	
}

C++: #include<iostream>
#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 <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;
  vector<string> key;
  map<string, int> m;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string tmp_s;
    int tmp_i;
    cin >> tmp_s >> tmp_i;
    if (m.count(tmp_s) == 0) {
      m[tmp_s] = tmp_i;
      key.push_back(tmp_s);
    } else {
      m[tmp_s] += tmp_i;
    }
  }
  sort(key.begin(), key.end(), [](string a, string b) {
    return (a.length() == b.length()) ? (a < b) : (a.length() < b.length());
  });
  for (auto& v : key) {
    cout << v << " " << m[v] << endl;
  }

  return 0;
}

C++: #include<stdio.h>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>     
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <iomanip>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define ld long double
#define rep(a,t) for(int a=0;a<t;a++) 
#define forever while(true)
#define Sort(a) sort(a.begin(),a.end())
#define Reverse(a) reverse(a.begin(),a.end())
#define pb push_back
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	map<string, int>a;
	int n;
	cin >> n;
	vector<string>find_;
	while (n--) {
		string p;
		int q;
		cin >> p >> q;
		a[p] += q;
		for (int i = 0; i < find_.size(); i++) {
			if (find_[i] == p) { continue; }
		}
		find_.pb(p);
	}
	Sort(find_);
	for (int i = 0; i < find_.size(); i++) {
		cout << find_[i] << " " << a[find_[i]] << endl;
	}
	return 0;
}