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<utility>
#include<algorithm>
using namespace std;
bool comp(const string& ls, const string& rs){
  if(ls.length() < rs.length())
    return true;
  else if(ls.length() > rs.length())
    return false;
  else
    return ls < rs;
}
int main(){
  int n,num;
  string str;
  map<string,int> m;
  cin >> n;
  string strs[n];
  for(int i = 0 ; i < n ; i++){
    cin >> str;
    cin >> num;
    m[str] += num;
  }
  int j = 0;
  for(auto itr = m.begin(); itr != m.end(); itr++)
    strs[j++] = itr->first;
  sort(strs,strs+j,comp);
  for(int i = 0 ; i < j ; i++)
    cout << strs[i] << " " << m[strs[i]] << endl;
}

C++: #include<iostream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z)
#define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z)
#define ll2(x, y) ll x, y; cin >> x >> y;
#define scn(n, a) rep(i, n)cin >> a[i]
#define sc2n(n, a, b) rep(i, n)cin >> a[i] >> b[i]
#define pri(x) cout << (x) << "\n"
#define pri2(x, y) cout << (x) << " " << (y) << "\n"
#define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define endl "\n"
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << (x) << "\n"
#define debug2(x, y) cout << " --- " << (x) << " " << (y) << "\n"
#define debug3(x, y, z) cout << " --- " << (x) << " " << (y) << " " << (z) << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)

struct pro{
  string s;
  int d;
};
vector<pro> res;

bool mycomp(pro qwe, pro rty){
  if(qwe.s.size() != rty.s.size())return qwe.s.size() < rty.s.size();
  return qwe.s < rty.s;
};

signed main(void){
  int i, j;
  for(int testcase = 0;testcase >= 0;testcase++){
    int(n);
    string x;
    int y;
    res.pb((pro){"ZZZZZZZZ", 0});
    for(;n--;){
      cin >> x >> y;
      res.pb((pro){x, y});
    }
    sort(all(res), mycomp);

    string l = "a";
    int r = 0;
    rep(i, res.size()){
      if(l == res[i].s){
        r += res[i].d;
      }else{
        if(l != "a")cout << l << " " << r << endl;
        l = res[i].s;
        r = res[i].d;
      }
    }

/*/

//*/ break;
  }
  return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;




const int INF = 1000000;
const int mod = 1e9 + 7;




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

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> s >> t;
		m[s] += t;
	}

	for (map<string, int>::iterator it = m.begin(); it != m.end(); it++) {
		cout << it->first << " " << it->second << endl;
	}
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <limits>
#include <fstream>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  map<string, int> mp;
  FOR(i,0,n){
      string s;
      int x;
      cin >> s >> x;
      mp[s] += x;
  }
  for(auto m : mp){
      cout << mp.first <<  " " << mp.second << endl;
  }
}

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

int main(){
  int n;
  vector<string> names;
  vector<int> nums;
  cin >> n;
  for(int i = 0; i < n; i++){
    string name;
    int num;
    bool f = false;
    cin >> name >> num;
    for(int i = 0; i < names.size(); i++){
      if(name == names[i]){
        nums[i] += num;
        f = true;
      }
    }
    if(!f){
      names.push_back(name);
      nums.push_back(num);
    }
  }
  string sbuf;
  int ibuf;
  for(int i = 0; i < names.size(); i++){
    for(int j = i + 1; j < names.size(); j++){
      bool change = false;
      if(names[i].size() > names[j].size()) change = true;
      else if(names[i].size() == names[j].size() && names[i] > names[j]) change = true;
      if(change){
       sbuf = names[i];
        names[i] = names[j];
        names[j] = sbuf;
        ibuf = nums[i];
        nums[i] = nums[j];
        nums[j] = ibuf;
      }
    }
  }

  for(int i= 0; i < names.size(); i++){
    cout << names[i] << " " << nums[i] << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
#define MOD 10000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const ull B=100000007;
const int INF=1e9;
bool com(string l,string r){
      if(l.length()!=r.length())return l.length()<r.length();
      REP(i,l.length())if(l[i]!=r[i])return l[i]<r[i];
}
int main(){
      int n;
      map<string,int> M;
      cin>>n;
      vector<string> V;
      REP(i,n){
            string s;int a;
            cin>>s>>a;
            if(M[s]==0)V.PB(s);
            M[s]+=a;
      }
      sort(V.begin(),V.end(),com);
      REP(i,V.size()){
            cout<<V[i]<<" "<<M[V[i]]<<endl;
      }
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
//#define int long long
using namespace std;

int n;
map<string, int> mp;
string str;
int num;
pair<int, string> len_name[1000000];

signed main()
{
	cin >> n;
	rep(i,n) {
		cin >> str >> num;
		mp[str] += num;
	}

	map<string, int>::iterator it = mp.begin();
	int idx = 0;
	while (it != mp.end()) {
		len_name[idx].first = it->first.length();
		len_name[idx].second = it->first;
		idx++;
		it++;
	} 

	sort(len_name, len_name + idx);

	rep(i,idx) {
		cout << len_name[i].second << " " << mp[len_name[i].second] << endl;
	}
}