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 <stdio.h>
#include <string.h>
 
typedef struct{
    char name[6];
    long num;
    char f;
}product;
 
int main(void){
    long n=0, i, j, t, count=0;
    char str[6];
    product p[300000];
     
    scanf("%ld", &n);
    for(i=0; i<n; i++){
        int flag=0;
     
        scanf(" %s %ld", str, &t);
         
        for(j=0; j<count; j++){
            if(strcmp(p[j].name, str)==0){
                p[j].num+=t;
                flag=1;
                break;
            }
        }
         
        if(flag==0){
            strcpy(p[count].name, str);
            p[count].num = t;
            count++;
        }   
         
           }
     
    for(i=0; i<count; i++){
        for(j=0; j<count; j++){
            if(strcmp(p[i].name, p[j].name) < 0){
                product tmp = p[i];
                p[i] = p[j];
                p[j] = tmp;
            }
        }
    }   
     
    long r=0;
 
    while(r<6){

        for(i=0; i<count; i++){
            if(p[i].name[r]=='\0' && p[i].f != 'N'){
                printf("%s %ld\n", p[i].name, p[i].num);
                p[i].f='N';
            }
        }
        r++;
    }
 
    return 0;
}

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

struct data{
	int cnt;
};

int int_pow(int n,int m){
	int ans=1;
	while(m--) ans*=n;
	return ans;
}

data latte[5][14348907];

int main(){
	int N,hoge,n,len;
	char input[6];
	memset(latte,0,sizeof(latte));
	
	cin>>N;
	for(int i=0;i<N;i++){
		cin>>input;
		cin>>n;
		hoge=0;
		for(int j=0;input[j]!='\0';j++){
			hoge += (input[j]-'A')*int_pow(27,4-j);
		}
		//printf(" %d\n",hoge);
		len=strlen(input)-1;
		latte[len][hoge].cnt += n;
	}
	for(int i=0;i<5;i++){
		for(int j=0;j<14348907;j++){
			if(latte[i][j].cnt==0) continue;
			for(int k=0;k<=i;k++){
				printf("%c",j/int_pow(27,4-k)%27+'A');
			}
			printf(" %d\n",latte[i][j].cnt);
		}
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>
#include<vector>
using namespace std;
bool cmp(string left,string right){
    if(left.size() < right.size())return 1;
    if(left.size() > right.size())return 0;
    else {
        if(left < right)return 1;
        else return 0;
    }
}
int main(){
	int n;
	cin >> n;
	map<string,int> D;
	vector<string> S;
	for(int i = 0; i < n; i++){
        string d;
        int a;
        cin >> d >> a;
        D[d] += a;
	}
	map<string,int>::iterator it = D.begin();
	while(it != D.end()){
	    S.push_back((*it).first);
	    it++;
	}
	sort(S.begin(),S.end(),cmp);
	for(int i = 0; i < S.size(); i++){
        cout << S[i] << " " << D[S[i]] << endl;
	}
	return 0;
}

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

using namespace std;

typedef pair<int,string> P;

vector<P> product;

int main(){
  int n;
  
  cin >> n;
  map<string,int> A;
  product.clear();

  for(int i=0;i<n;i++){
  	
    string name; int num;
    cin >> name >> num;
    
    if( A[name] == 0 ) 
	product.push_back(P(name.size(), name));
    A[name] += num;
    
  }
  sort(product.begin(), product.end());
  
  for(int i=0;i<product.size();i++)
    cout << product[i].second << " " << A[product[i].second] << endl;
    
  return 0;
}

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

typedef struct{
	string name;
	long num;
}product;

product p[12000000];

int main(){
	long n, now=0;
	
	scanf(" %ld", &n);
	for(long i=0; i<n; ++i){
		string s;
		long a;
		cin >> s >> a;
		
		bool find=false;
		for(long j=0; j<now; ++j){
			if(p[j].name==s){
				find=true;
				p[j].num+=a;
				break;
			}
		}
		
		if(!find){
			p[now].name=s;	
			p[now].num=a;
			++now;
		}
	}
	
	for(long i=0; i<now; ++i){
		for(long j=i; j<now; ++j){
	
			if(p[i].name.size() > p[j].name.size()) swap(p[i], p[j]);
			else if(p[i].name.size() == p[j].name.size()){
				for(int k=0; k<p[i].name.size(); ++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>
#include<algorithm>

struct Product{
	std::string name;
	int amount;
};

int main(){
	std::vector<Product> v;

	int n;
	std::cin >> n;
	for(int i=0;i<n;i++){
		std::string name;
		int amount;
		std::cin >> name >> amount;
		auto it = std::find_if(v.begin(), v.end(), [&name](Product p){
					return p.name == name;
			});
		if(it == v.end()){
			v.push_back({name, amount});
		}else{
			it->amount += amount;
		}
	}

	std::sort(v.begin(), v.end(), [](Product lp, Product rp){
			if(lp.name.size() != rp.name.size())return lp.name.size() < rp.name.size();
			for(int i=0;i<lp.name.size();i++){
				if(lp.name[i] != rp.name[i])return lp.name[i] < rp.name[i];
			}
		});

	for(auto p : v){
		std::cout << p.name << " " << p.amount << std::endl;
	}
}

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

using namespace std;

typedef long long ll;
// typedef pair<ll, ll> P;
typedef pair<int, string> P;

#define MOD 1000000007
#define REP(i, N) for (int i = 0; i < N; ++i)
#define REP1(i, N) for (int i = 1; i <= N; ++i)
#define RREP(i, N) for (int i = N - 1; i >= 0; --i)
#define ALL(a) a.begin(), a.end()

int main() {
  int n;
  cin >> n;
  map<P, int> mp;
  REP(i, n) {
    string s;
    int val;
    cin >> s >> val;
    mp[P(s.size(), s)] += val;
  }
  for (auto a : mp) {
    cout << a.first.second << " " << a.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<iostream>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
main()
{
	int n;
	cin>>n;
	map<string,int>M;
	for(int i=0;i<n;i++)
	{
		string s;int a;cin>>s>>a;M[s]+=a;
	}
	vector<pair<string,int> >a;
	for(pair<string,int>it:M)
	{
		a.push_back(it);
	}
	sort(a.begin(),a.end(),[](const pair<string,int>&a,const pair<string,int>&b){
		if(a.first.size()!=b.first.size())return a.first.size()<b.first.size();
		else return a.first<b.first;
	});
	for(int i=0;i<a.size();i++)cout<<a[i].first<<" "<<a[i].second<<endl;
}

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)) != 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;	cin >> n;
}