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 <map>
#include <string>
#include <iostream>
#include <vector>

using namespace std;


struct Comparator
{
        bool operator()( const string& l, const string& r ) const
        {
                if ( l.size() == r.size() )
                {
                        return l < r;
                }
                return l.size() < r.size();
        }
};
int main()
{
        int n; cin >> n;
        map<string, int, Comparator> m;

        for ( int i = 0; i < n; ++i )
        {
                string k;
                int v;
                cin >> k >> v;
                if ( m.count( k ) > 0 )
                {
                        m[ k ] += v;
                } else
                {
                        m[ k ] = v;
                }
        }

        for ( auto it = m.begin(); it != m.end(); ++it )
        {
                std::cout << it->first << " " << it->second << std::endl;
        }
}

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

int main()
{
    int n;

    cin >> n;

    string s[n];
    int p[n];

    for( int i = 0; i < n; i++ )
        cin >> s[i] >> p[i];

    for( int i = 0; i < n - 1; i++ )                //同じ製品をまとめる
        for( int j = i + 1; j < n; j++ )
            if( s[i] == s[j] )
            {
                s[j] = "";
                p[i] += p[j];
                p[j] = 0;
            }

    for( int i = 0; i < n - 1; i++ )                //文字数順に並べる。
        for( int j = i + 1; j < n; j++ )
            if( s[i].size() > s[j].size() )
            {
                string t1 = s[i];
                s[i] = s[j];
                s[j] = t1;
                
                int t2 = p[i];
                p[i] = p[j];
                p[j] = t2;
            }

    for( int i = 0; i < n - 1; i++ )                //辞書順に並べる。
        for( int j = i + 1; j < n; j++ )
            if( s[i].size() == s[j].size() && s[i] > s[j] )
            {
                string t1 = s[i];
                s[i] = s[j];
                s[j] = t1;
                
                int t2 = p[i];
                p[i] = p[j];
                p[j] = t2;
            }

    for( int i = 0; i < n; i++ )
        if( s[i] != "" )
            cout << s[i] << " " << p[i] << endl;

    return 0;
}

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

typedef struct{
	string prod ;
	int num ;
}Prod_st;

int N;
Prod_st tmp;
vector<Prod_st> VC ;
map<string,int> MP ;

bool operator<( const Prod_st& a , const Prod_st& b ){
	return (a.prod.size()==b.prod.size())?(a.prod<b.prod):(a.prod.size()<b.prod.size());
}
bool operator>( const Prod_st& a , const Prod_st& b ){
	return (a.prod.size()==b.prod.size())?(a.prod>b.prod):(a.prod.size()>b.prod.size());
}

int main(){
	
	cin >> N;
	while( N-- ){
		cin >> tmp.prod >> tmp.num ;
		if( MP[tmp.prod]==0 ){
			VC.push_back( tmp ) ;
			MP[tmp.prod] = VC.size();
		}else VC[MP[tmp.prod]-1].num += tmp.num ;
	}
	
	sort( VC.begin() , VC.end() ) ;
	for( int i=0 ; i<VC.size() ; i++ ){
		cout << VC[i].prod << " " << VC[i].num << 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);
}

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

using namespace std;

char s[10];

int main()
{
	map<string,int> m;
	int n;
	scanf("%d",&n);
	for(int i = 0; i < n; i++)
	{
		int w;
		scanf("%s %d\n",s,&w);
		if(m.end() != m.find(s))
		{
			m[s] += w;
		}
		else
		{
			m[s] = w;
		}
	}
	map<string,int>::iterator it = m.begin();
	while(it != m.end())
	{
		printf("%s %d\n",(*it).first,(*it).second);
		it++;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define FF first.first
#define FS first.second
#define  F first
#define  S second
using namespace std;
typedef pair<int,string>P;
typedef pair<P,int>PP;
typedef pair<string,int>PPP;
int main(void){
	int n;
	PP p[10001];
	PPP pp[10001];
	int  i;
	cin>>n;
	for(i=0;i<n;i++){
		cin>>p[i].FS>>p[i].S;
		p[i].FF=p[i].FS.size();
		pp[i].S=0;
	}
	sort(p,p+n);
	
	int j=0;
	for(i=0;i<n;i++){
		pp[j].F=p[i].FS;
		pp[j].S+=p[i].S;
		if(p[i].FS!=p[i+1].FS){
			j++;
		}
	}
	for(i=0;i<j;i++){
		cout<<pp[i].F<<" "<<pp[i].S<<endl;
	}
	return 0;
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\b.txt"

class Compare {
public:
	bool operator()(const string&l, const string&r) {
		if (l.size() == r.size()) {
			for (int i = 0; i < l.size(); ++i) {
				if (l[i] == r[i])continue;
				else return l[i] < r[i];
			}
			return false;
		}
		else {
			return l.size()< r.size();
		}
	}
};	//aa?????????????????¶

int main() {
	int N; cin >> N;
	map<string, int,Compare>mp;
	for (int i = 0; i < N; ++i) {
		string st; int n; cin >> st >> n;
		mp[st] += n;
	}
	for (auto m : mp) {
		cout << m.first << " " << m.second << endl;
	}

	return 0;
}

C++: #include<stdio.h>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>     
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <iomanip>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define ld long double
#define rep(a,t) for(int a=0;a<t;a++) 
#define forever while(true)
#define Sort(a) sort(a.begin(),a.end())
#define Reverse(a) reverse(a.begin(),a.end())
#define pb push_back
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	map<string, int>a;
	int n;
	cin >> n;
	vector<string>find_;
	while (n--) {
		string p;
		int q;
		cin >> p >> q;
		a[p] += q;
		for (int i = 0; i < find_.size(); i++) {
			if (find_[i] == p) { goto G; }
		}
		find_.pb(p);
	G: {}
	}
	Sort(find_);
	for (int i = 0; i < find_.size(); i++) {
		cout << find_[i] << " " << a[find_[i]] << endl;
	}
	return 0;
}