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<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 = 0;
	if(l.size() < r.size())f = 1;
	else (l > r) ? f = 0 : f = 1;
	
	return f;
}

C++: #include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;
signed 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) == st.end()) {
			st.insert(s);
			m[s] = 0;
		}
		else if(*(st.lower_bound(s)) != s){
		 	st.insert(s);
			m[s] = 0;   
		}
		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 (pair<int, string> i : v)cout << i.second << ' ' << m[i.second] << endl;
}

C++: #include <iostream>
#include <cstdio>
#include <string.h>
#include <map>
using namespace std;
typedef pair< int, string > P;
int main(){
  int n;
  cin >> n;
  string id;
  int num;
  map< P , int > data;
  for(int i = 0 ; i < n ; i++ ){
    cin >> id >> num;
    data[P(id.size(),id)] += num;
  }
  for( map< P,int >::iterator it = data.begin(); it != data.end() ; it++ ){
    cout << (*it).first.second << ' ' << (*it).second << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
typedef pair<int,string> P;
string str;
int n,m;
vector<P> p;
map <string,int> q;
int main(){
  cin>>n;
  p.resize(n);
  for(int i=0;i<n;i++){
    cin>>p[i].second>>m;
    p[i].first=p[i].second.size();
    q[p[i].second]+=m;
  }
  sort(p.begin(),p.end());
  for(int i=0;i<n;i++){
    if(i&&p[i]==p[i-1])continue;
    cout<<p[i].second<<' '<<q[p[i].second]<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<map>
using namespace std;
int main(){
	map<string,int>	m;
	int n;
	cin >> n;
	while(n--){
		string str;
		int x;
		cin >> str >> x;
		m[str]+=x;
	}
	for(auto itr = m.begin();itr!=m.end();itr++)	cout << itr->first << ' ' << itr->second << endl;
}

C++: #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repp(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;
typedef pair<int, int> P;
typedef pair<string, int> psi;

bool cmp(psi d1, psi d2){
    if(d1.first.size() != d2.first.size()){
        return d1.first.size() < d2.first.size();
    }
    return d1.first < d2.first;
}

signed main(){
    int n;
    cin >> n;
    map<string, int> mp;
    rep(i, 0, n){
        string s;
        int a;
        cin >> s >> a;
        mp[s] += a;
    }
    vector<psi> d;
    for(map<string, int>::iterator it = mp.begin(); it != mp.end(); it++){
        d. push_back(make_pair(it->first, it->second));
    }
    sort(all(d), cmp);
    rep(i, 0, d.size()){
        cout << d[i].first << ' ' << d[i].second << endl;
    }
}

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

int main() {
    int N;
    cin >> N;
    vector<map<string,int> > dec(5);
    for (int i = 0; i < N; i++) {
        string str;
        int n;
        cin >> str >> n;
        dec[str.size()-1][str] += n;
    }

    for (auto i : dec) {
        for (auto j : i) {
            cout << j.first << " " << j.second << endl;
        }
    }

    return 0;
}

C++: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;
  
static const int tx[] = {0,1,0,-1};
static const int ty[] = {-1,0,1,0};

class Dict{
public:
  string key;
  int num;
  Dict(string _k,int _n) : key(_k), num(_n) {}
  bool operator<(const Dict& d) const{
    if(key.size() != d.key.size()){
      return key.size() < d.key.size();
    }
    else{
      return key < d.key;
    }
  }
  bool operator>(const Dict& d) const{
    if(key.size() != d.key.size()){
      return key.size() > d.key.size();
    }
    else{
      return key > d.key;
    }
  }
};

int main(){
  int n;
  while(~scanf("%d",&n)){
    map<string,int> freq;
    for(int i=0;i<n;i++){
      string key;
      int num;
      cin >> key >> num;
      freq[key] += num;
    }
    vector<Dict> dict;
    for(map<string,int>::iterator it = freq.begin(); it != freq.end(); it++){
      dict.push_back(Dict(it->first,it->second));
    }
    sort(dict.begin(),dict.end());
    for(int i=0;i<dict.size();i++){
      printf("%s %d\n",dict[i].key.c_str(),dict[i].num);
    }
  }
}

C++: #include <cstdio>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int n;
	
	scanf("%d", &n);
	
	vector<pair<int, pair<string, int> > > v;
	for (int i = 0; i < n; i++){
		char pName[8];
		int num;
		scanf("%s %d", pName, num);
		v.push_back(make_pair(strlen(pName), make_pair((string)pName, num)));
	}
	
	sort(v.begin(), v.end());
	
	string lead = "$";
	int cnt = 0;
	for (int i = 0; i < n; i++){
		if (lead != v[i].second.first){
			if (lead != "$") printf("%s %d\n", lead.c_str(), cnt);
			lead = v[i].second.first;
			cnt = v[i].second.second;
		}
		else cnt += v[i].second.second;
	}
	return (!printf("%s %d\n", lead.c_str(), cnt));
}

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

using namespace std;

int main()
{
    int n;
    cin >> n;
    map<string, int, function<bool(const string&, const string&)>> m(
        [](const string& a, const string& b) {
            return a.size() == b.size() ? a < b : a.size() < b.size();
        });
    for (int i = 0; i < n; ++i) {
        string s;
        int number;
        cin >> s >> number;
        m[s] += number;
    }
    for (auto&& i : m) {
        cout << i.first << ' ' << i.second << endl;
    }
}