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

int N;
typedef pair<string,int>P;

bool cmp(P p1,P p2){
  if(p1.first.size() == p2.first.size()){
    return p1.first < p2.first;
  }
  return p1.first.size() < p2.first.size();
}

int main(){
  cin >> N;
  
  map<string,int>mp;
  vector<P>vec;

  for(int i = 0 ; i < N ; i++){
    string tmp;
    int num;
    cin >> tmp >> num;
    if(mp.find(tmp) == mp.end()){
      mp[tmp] = vec.size();
      vec.push_back(P(tmp,num));
    }
    else {
      vec[mp[tmp]].second += num;
    }
  }

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

  for(int i = 0 ; i < vec.size() ; i++){
    cout << vec[i].first << ' ' << vec[i].second << endl;
  }

  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b)*b; }
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T>& V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> >& Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T>& mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }
template<typename T>vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type fill_v(T &t,const V &v){t=v;}
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type fill_v(T &t,const V &v){for(auto &e:t) fill_v(e,v);}
/*
 <url:https://onlinejudge.u-aizu.ac.jp/challenges/sources/JOI/Prelim/0589?year=2006>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

template<class Type>
Type solve(Type res = Type()){
    int n; cin >> n;
    map<string,ll> mp;
    for(int i = 0; i < n;i++){
        string s; ll v; cin >> s >> v;
        mp[s]+=v;
    }
    
    vector<pair<string,ll>> ans(mp.begin(),mp.end());
    sort(ans.begin(),ans.end(),[](const pair<string,ll>& l,const pair<string,ll>& r){
        if(l.first.length() == r.first.length()) return l.first < r.first;
        else return l.first.length() < r.first.length();
    });
    
    for(auto p:ans){
        cout << p.first << " " << p.second << endl;
    }
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    solve(0);
    // cout << fixed << setprecision(15) << solve<ll>() << endl;
    return 0;
}

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

using namespace std;

typedef pair<int,string> P;
 
int main(){
    int n,num;
    string str;
    map<P,int> m;
     
    cin >> n;
    for(int i = 0 ; i < n ; i++){
        cin >> str >> num;
        m[P(str.size(),str)] += num;
    }     
    map<P,int>::iterator it;
    for(it = m.begin() ; it != m.end() ; it++){
        cout << it->first.second << ' ' << it->second << endl;
    }
    return 0;
}

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

using namespace std;

int main() {
    int n;
    map<string, int> products;
    vector<pair<string, int> > p;
    cin>>n;
    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.size() > rhs.first.size()) return false;
            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 <map>
#include <algorithm>
#include <string>

using namespace std;

typedef struct map<string, int> Mymap;

void disp(Mymap &obj, int cn) {
  Mymap::iterator p;
  for(p=obj.begin(); p!=obj.end(); ++p) {
    if((p->first).length()==cn)
      cout << p->first << " " << p->second << endl;
  }
}


int main() {
  int num;
  Mymap val;

  // input
  scanf("%d", &num);
  for(int i=0; i<num; i++) {
    string tmpS;
    cin>>tmpS;
    int tmpI;
    cin>>tmpI;
    if(val.find(tmpS)==val.end()) {
      val.insert(make_pair(tmpS, tmpI));
    }
    else {
      val[tmpS]+=tmpI;
    }
  }
  // sort不要

  // output
  for(int i=1; i<6; i++) {
    disp(val, i);
  }
}

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

using namespace std;

typedef pair<string,int> P;
map<string,int> M;
vector<P> V;

bool comp(const P p1, const P p2){
  if(p1.first.length() != p2.first.length())return p1.first.length()<p2.first.length();
  return p1.first == min(p1.first,p2.first);
}

int main(){
  int n;
  cin >> n;
  while(n--){
    string s;
    int a;
    cin >> s >> a;
    M[s] += a;
  }

  map<string,int>::iterator ite;

  for(ite = M.begin(); ite != M.end(); ite++){
    V.push_back(P(ite->first,ite->second));
  }
  
  sort(V.begin(),V.end(),comp);
  
  for(int i = 0; i < V.size(); i++)
    cout << V[i].first << " " << V[i].second << endl;
  return 0;
}

C++: #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;
}