Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

type entry struct {
	id, score int
}

type lessFunc func(p1, p2 *entry) bool

type multiSorter struct {
	entries []entry
	less    []lessFunc
}

func (ms *multiSorter) sort(entries []entry) {
	ms.entries = entries
	sort.Sort(ms)
}

func OrderedBy(less ...lessFunc) *multiSorter {
	return &multiSorter{
		less: less,
	}
}

func (ms *multiSorter) Len() int {
	return len(ms.entries)
}

func (ms *multiSorter) Swap(i, j int) {
	ms.entries[i], ms.entries[j] = ms.entries[j], ms.entries[i]
}

func (ms *multiSorter) Less(i, j int) bool {
	p, q := &ms.entries[i], &ms.entries[j]
	var k int
	for k = 0; k < len(ms.less)-1; k++ {
		less := ms.less[k]
		switch {
		case less(p, q):
			return true
		case less(q, p):
			return false
		}
	}
	return ms.less[k](p, q)
}

func main() {
	for scanner.Scan() {
		m, _ := strconv.Atoi(scanner.Text())
		if m == 0 {
			break
		}
		results := make([]entry, 0)
		for i := 0; i < m; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			data := make([]int, len(buf))
			for i := range data {
				data[i], _ = strconv.Atoi(buf[i])
			}
			id, score := score(data)
			results = append(results, entry{id, score})
		}
		score_axis := func(e1, e2 *entry) bool {
			return e1.score > e2.score
		}
		id_axis := func(e1, e2 *entry) bool {
			return e1.id < e2.id
		}
		OrderedBy(score_axis, id_axis).sort(results)
		// log.Println(results)
		for _, e := range results {
			fmt.Printf("%d %d\n", e.id, e.score)
		}
	}
}

func score(input []int) (int, int) {
	id := input[0]
	data := input[1:]
	frame := 1
	scores := make([]int, 11)
	for frame <= 10 {
		if data[0] == 10 {
			scores[frame] = data[0] + data[1] + data[2]
			data = data[1:]
			frame += 1
		} else if data[0]+data[1] == 10 {
			scores[frame] = data[0] + data[1] + data[2]
			data = data[2:]
			frame += 1
		} else {
			scores[frame] = data[0] + data[1]
			data = data[2:]
			frame += 1
		}
	}
	sum := 0
	for i, v := range scores {
		if i != 0 {
			sum += v
		}
	}
	return id, sum
}

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

int s[10][5];

P operator+(P a, int d) {
	rep(i, d) {
		if (~s[a.first][a.second + 1])a.second++;
		else {
			a.first++; a.second = 0;
		}
	}
	return a;
}
int main() {
	int m;
	while (scanf("%d", &m), m) {
		vector<P>v;
		rep(i, m) {
			memset(s, -1, sizeof(s));
			int id; scanf("%d", &id);
			int f = 0; bool b = 0;
			while (f < 9) {
				scanf("%d", &s[f][b]);
				if (s[f][b] > 10)abort();
				if (s[f][b] == 10)f++;
				else {
					if (b)f++;
					b = !b;
				}
			}
			scanf("%d%d", &s[9][0], &s[9][1]);
			if (s[9][0] + s[9][1] >= 10)scanf("%d", &s[9][2]);
			P p(0, 0);
			while (p.first < 9) {
				if (s[p.first][p.second] == 10) {
					P a = p + 1, b = p + 2;
					s[p.first][p.second] += s[a.first][a.second] + s[b.first][b.second];
				}
				if (p.second&& s[p.first][p.second - 1] + s[p.first][p.second] == 10) {
					P a = p + 1;
					s[p.first][p.second] += s[a.first][a.second];
				}
				p = p + 1;
			}
			int cnt = 0;
			rep(i, 10)rep(j, 3) {
				if (~s[i][j])cnt += s[i][j];
			}
			v.push_back(P(cnt, id));
		}
		sort(v.begin(), v.end(), [](P a, P b) {
			if (a.first != b.first)return a.first > b.first;
			return a.second < b.second;
		});
		for (auto p : v) {
			printf("%d %d\n", p.second, p.first);
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
#include <queue>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

class Compare
{
	public:
		bool operator() (const pair<int,int> p,const pair<int,int> p2)
		{
			if(p.first == p2.first) return p.second < p2.second;
			else return p.first > p2.first;
		}
};

int main()
{
	int n;
	while(cin >> n && n)
	{
		vector<pair<int,int> > v;
		rep(i,n)
		{
			
			vector<pair<int,int> > temp;
			vector<string> data;
			int num;
			cin >> num;
		
			int cnt = 0;
			while(cnt != 10)
			{
				int a,b,last;
				cin >> a;

				if(a == 10)
				{
					temp.push_back(make_pair(a,0));
					cnt++;
					data.push_back("st");

					if(cnt == 10)
					{
						int t;
						cin >> t;

						if(t == 10)
						{
							int t2;
							cin >> t2;
							temp.push_back(make_pair(t,0));
							data.push_back("st");

							temp.push_back(make_pair(t2,0));
							data.push_back("st");
							
						}
						else
						{
							int t2;
							cin >> t2;
							temp.push_back(make_pair(t,t2));
							if(t+t2 == 10) data.push_back("sp");
							else data.push_back("no");
						}
					}
				}
				else
				{
					cin >> b;
					temp.push_back(make_pair(a,b));
					cnt++;

					if(a + b == 10)
					{
						data.push_back("sp");
						
						if(cnt == 10)
						{
							int t;
							cin >> t;
							temp.push_back(make_pair(t,0));
							
							if(t == 10) data.push_back("st");
							else data.push_back("no");
						}
					}
					else
					{
						data.push_back("no");
					}
				}
			}
			
			int ans = 0;
			rep(i,10)
			{
				if(data[i] == "no")
				{
					ans += temp[i].first + temp[i].second;
				}
				else if(data[i] == "sp")
				{
					ans += temp[i].first + temp[i].second + temp[i+1].first;
				}
				else if(data[i] == "st")
				{
					ans += temp[i].first + temp[i].second + temp[i+1].first + temp[i+1].second;
					if(data[i+1] == "st") ans += temp[i+2].first;
				}
			}	
			v.push_back(make_pair(ans,num));
		}

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

		rep(i,v.size())
		{
			cout << v[i].second << " " << v[i].first << endl;
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(void)
{
	int i,j,k,m,score[41][21],id[41],x,cnt,yoso[41]={0},sum[41]={0},flg[41][21],p,check[41]={0};
	
	while(cin>>m&&m) {
		for(i=0;i<m;i++) yoso[i]=sum[i]=check[i]=0;
		for(i=0;i<m;i++) {
			cnt=21;
			cin>>id[i];
			x=p=0;
			for(j=0;j<21;j++) flg[i][j]=-1;
			while(cnt>0) {
				cin>>score[i][x];
				if(cnt<=3) {
					if(score[i][x]==10) cnt--,flg[i][x]=2;
					else if(cnt==1&&score[i][x]+score[i][x-1]==10) flg[i][x]=flg[i][x-1]=1;
					else cnt-=2,flg[i][x]=0;
				}	
				else {
					if(score[i][x]==10) cnt-=2,flg[i][x]=2;
					else if(p==1&&score[i][x]+score[i][x-1]==10) flg[i][x]=flg[i][x-1]=1,p=0,cnt--;
					else if(p==1) cnt--,flg[i][x]=0,p=0;
					else cnt--,flg[i][x]=0,p=1;
				}
				x++;
			}
			yoso[i]=x;
		}
		int a;
		for(i=0;i<m;i++) {
			a=0;
			for(j=0;j<yoso[i];j++) {
				if(a!=9) {
					if(flg[i][j]!=2) j++,a++;
					else a++;
				}
				else check[i]++;
			}
		}
		for(i=0;i<m;i++) {
			for(j=0;j<yoso[i];j++) {
				if(j<yoso[i]-check[i]) {
					if(flg[i][j]==0) sum[i]+=score[i][j]+score[i][j+1],j++;
					else if(flg[i][j]==1) {
						sum[i]+=score[i][j]+score[i][j+1]+score[i][j+2];
						j++;
					}
					else sum[i]+=score[i][j]+score[i][j+1]+score[i][j+2];
				}
				else {
					if(check[i]==3) sum[i]+=score[i][j]+score[i][j+1]+score[i][j+2];
					else sum[i]+=score[i][j]+score[i][j+1];
					break;
				}
			}
		}
		for(i=0;i<m-1;i++) {
			for(j=i+1;j<m;j++) {
				if(sum[i]<sum[j]) swap(sum[i],sum[j]),swap(id[i],id[j]);
			}
		}
		for(i=0;i<m;i++) cout<<id[i]<<" "<<sum[i]<<endl;
	}
}