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 <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;




struct MyString
{
	string str;

	MyString(){}
	MyString(string t) :str(t){}

	void in() { cin >> str; }

	/*bool operator< (const MyString& S) {
		if (this->str.size() != S.str.size()) return this->str.size() < S.str.size();
		return this->str < S.str;
	}

	bool operator> (const MyString& S) {
		if (this->str.size() != S.str.size()) return this->str.size() > S.str.size();
		return this->str > S.str;
	}*/
};




bool operator< (const MyString &s1, const MyString &s2) {
	if (s1.str.size() != s2.str.size()) return s1.str.size() < s2.str.size();
	return s1.str < s2.str;
}


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

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

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

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

using namespace std;
typedef pair<int, string> P;
map<P, int> d;

int main(){
    int n, p;
    string c;
    cin >> n;
    for(int i = 0;i < n;i++){
	cin >> c >> p;
	d[P(c.size(), c)] += p;
    }
    map<P, int>::iterator it = d.begin();
    while(it != d.end()){
	cout << (*it).first.second << " " << (*it).second << endl;
	it++;
    }
    return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>

using namespace std;

int main (void)
{
	int n,b[100],as[100];
	char a[100];
	
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i] >> b[i];
	}
	
	for (int i = 0; i < n; i++)
	{
		for (int j = i + 1; j < n; j++)
		{
			if (a[i] == a[j])
			{
				b[i] += b[j];
				a[j] = "";
			}
		}
	}
	
	for (int i = 0 ; i < n; i++)
	{
		as[i] = 1;
		for (int j = i + 1; j < n; j++)
		{
			if(a[i] != "")
			{
				if (a[i] < a[j])
				{
					as[i]++;
				}
			}
		}
		break;
	}
	
	for (int i = 0; i < n; i++)
	{
		if (a[i] != "")
		{
			if (as[i] == i + 1)
			{
				cout << a[i] << " " << b[i] << endl;
			}
		}
	}
}

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

class State{
public:
  string name;
  int value;
  State(){}
  State(string n, int v):name(n), value(v){}

  bool operator < ( const State &s) const{
    if ( value == s.value ){
      if ( name.size() == s.name.size() ){
	return name < s.name;
      } else {
	return name.size() < s.name.size();
      }
    } else return value > s.value;
  }
  
};

main(){
  int n;
  map<string, int> mp;
  cin >> n;
  for ( int i = 0; i < n; i++ ){
    string name;
    int value;
    cin >> name >> value;
    mp[name] += value;
  }

  vector<State> S;
  for ( map<string, int>::iterator it = mp.begin(); it != mp.end(); it++ ){
    S.push_back(State((*it).first, (*it).second));
  }
  sort(S.begin(), S.end());

  for ( int i = 0; i < S.size(); i++ ){
    cout << S[i].name << " " << S[i].value << endl;
  }

}

C++: #include <iostream>
#include <map>
#include <string>
#include <queue>
using namespace std;
int main()
{
	int n; cin >> n;
	map <string,long> a;
	string* name=new string[n]; int cnt=0;
	string* rank=new string[n];
	while(n--)
	{
		string str; cin >> str;
		int i=0;
		for( ;i<cnt;i++) if(name[i]==str) break;
		if(i>=cnt) name[cnt++]=str;
		int m; cin >> m;
		a[str]+=m;
	}
	//並べ替え
	rank[0]=name[0];
	for(int i=1;i<cnt;i++)
	{
		int j=0;
		for( ;j<i;j++)
		{
			if(name[i].size()<rank[j].size()) break;
			if(name[i]<rank[j] && name[i].size()==rank[j].size()) break;
		}
		if(j==i) rank[j]=name[i];
		else 
		{
			for(int k=0;i-k>j;k++) rank[i-k]=rank[i-k-1];
			rank[j]=name[i];
		}
	}
	for(int i=0;i<cnt;i++)
	cout << rank[i] << ' ' << a[rank[i]] << endl;
	delete[] name;
	delete[] rank;
}

C++: #include "bits/stdc++.h"

using namespace std;

#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)

//#define int long long
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

typedef long long ll;
//typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;
typedef unsigned int uint;

template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

bool cmp(const string& l, const string& r)
{
	if (SZ(l) == SZ(r))
	{
		return l < r;
	}
	else
	{
		return SZ(l) < SZ(r);
	}
}

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	map<string, ll> m;
	vector<string> names;

	int n;
	cin >> n;

	string name;
	int v;
	rep(i, 0, n)
	{
		cin >> name >> v;
		names.push_back(name);
		m[name] += v;
	}

	sort(all(names), cmp);
	names.erase(unique(all(names)), names.end());
	vector<pair<string, ll>> ps;
	rep(i, 0, SZ(names))
	{
		cout << names[i] <<" "<< m.at(names[i]) << endl;
	}

	return 0;
}

C++: #include<bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef pair<string,int>P;

int main(void){
	int n;
	P p[10000];
	P a[10000];
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>p[i].F>>p[i].S;
		a[i].S=0;
	}
	sort(p,p+n);
	
	int j;
	j=0;
	a[j].F=p[0].F;
	a[j].S+=p[0].S;
	for(int i=1;i<n;i++){
		if(p[i].F==p[i-1].F){
			a[j].F=p[i].F;
			a[j].S+=p[i].S;
		}
		else{
			j++;
			a[j].F=p[i].F;
			a[j].S+=p[i].S;
		}
	}
	for(int i=0;i<j;i++){
		if(a[i].F.size()>a[i+1].F.size()){
			swap(a[i],a[i+1]);
		}
	}
	
	for(int i=0;i<=j;i++){
			cout<<a[i].F<<" "<<a[i].S<<endl;
	}
	return 0;
}

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




using namespace std;




bool cmp(const pair<string, int>& x, const pair<string, int>& y)
{
	if (x.first.size() != y.first.size()){
		return x.first < y.first;
	}
	return x.first.size() < y.first.size();
}




int main()
{
	map<string, int> m;
	string pn;
	int n, c;




	scanf("%d", &n);
	
	for (int i = 0; i < n; i++){
		cin >> pn >> c;




		if (m.count(pn)) m[pn] += c;
		else m.insert(make_pair(pn, c));
	}




	vector< pair<string, int> > v;




	for (map<string, int>::iterator it = m.begin(); it != m.end(); it++){
		v.push_back(*it);
	}




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




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




	return (0);
}