Go: package main

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

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 <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

int dx[]={1, 0, -1, 0};
int dy[]={0, -1, 0, 1};

int main(){
    while(1){
        int m; cin>>m;
        if(m == 0) return 0;

        vector<P> id(m);
        vector<vector<int>> score(m);
        for(int i=0; i<m; i++){
            int x; cin>>x;
            id[i] = P(x, i);
            
            int turn = 1;
            int cnt = 0;
            int sum = 0;
            while(turn <= 10){
                int s; cin>>s;
                score[i].eb(s);
                sum += s;
                cnt++;

                if(turn == 10){
                    if(sum == 10 && cnt == 1){
                        int s1, s2; cin>>s1>>s2;
                        score[i].eb(s1);
                        score[i].eb(s2);
                    }
                    else{
                        int s1; cin>>s1;
                        if(s + s1 == 10){
                            int s2; cin>>s2;
                            score[i].eb(s1);
                            score[i].eb(s2);
                        }
                        else{
                            score[i].eb(s1);
                        }
                    }

                    break;
                }

                if(sum == 10 && cnt == 1){
                    cnt = 0;
                    turn++;
                    sum = 0;
                }
                else if(sum == 10 && cnt == 2){
                    cnt = 0;
                    turn++;
                    sum = 0;
                }
                else if(cnt == 2){
                    cnt = 0;
                    turn++;
                    sum = 0;
                }
            }
        }

        vector<P> fans(m);
        for(int i=0; i<m; i++){
            int ID = id[i].first;
            int turn = 1;
            int cnt = 0;
            int sum = 0;
            int ans = 0;
            for(int j=0; j<score[i].size(); j++){
                sum += score[i][j];
                ans += score[i][j];
                cnt++;

                if(turn == 10){
                    continue;
                }

                if(sum == 10 && cnt == 1){
                    ans += score[i][j+1];
                    ans += score[i][j+2];
                    turn++;
                    cnt = 0;
                    sum = 0;
                }
                else if(sum == 10 && cnt == 2){
                    ans += score[i][j+1];
                    turn++;
                    cnt = 0;
                    sum = 0;
                }
                else if(cnt == 2){
                    turn++;
                    cnt = 0;
                    sum = 0;
                }
            }

            fans[i] = P(ans, ID);
        }

        sort(fans.begin(), fans.end(), greater<P>());

        for(int i=0; i<m; i++){
            cout << fans[i].second << " " << fans[i].first << endl;
        }
    }
}

C++: //17
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>

using namespace std;

struct IS{
  int i,s;
  bool operator<(IS a)const{
    if(s!=a.s){
      return s>a.s;
    }else{
      return i<a.i;
    }
  }
};

int main(){
  for(int m;cin>>m,m;){
    IS sc[40];
    cin.ignore();
    for(int i=0;i<m;i++){
      string s;
      getline(cin,s);
      stringstream ss(s);
      int id;
      ss>>id;
      int a[21]={};
      for(int i=0;ss>>a[i];i++);
      int scr=0;
      int x=0;
      for(int f=1;;f++){
	if(f!=10){
	  if(a[x]==10){
	    scr+=10+a[x+1]+a[x+2];
	    x++;
	  }else if(a[x]+a[x+1]==10){
	    scr+=10+a[x+2];
	    x+=2;
	  }else{
	    scr+=a[x]+a[x+1];
	    x+=2;
	  }
	}else{
	  scr+=a[x]+a[x+1]+a[x+2];
	  break;
	}
      }
      IS cs={id,scr};
      sc[i]=cs;
    }
    sort(sc,sc+m);
    for(int i=0;i<m;i++){
      cout<<sc[i].i<<' '<<sc[i].s<<endl;
    }
  }
  return 0;
}

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

using namespace std;

int main(){

	int n, score[40], h[21], cnt, frame, p, pre, tmp, index[40];
	string sn[40];
	bool flg;
	
	while(cin >> n){
		if(n == 0) break;
		
		fill(score, score+40, 0);
		for(int i=0; i<40; i++){
			index[i] = i;
		}

		for(int i=0; i<n; i++){
			fill(h, h+21, 1);
			cnt = 0;
			frame = 1;
			flg = false;
			pre = 0;	
		
			cin >> sn[i];
			while(frame<10){
				cin >> p;
				if(flg == false){
					if(p != 10){
						pre = p;
						flg = true;
					}
					else {
						h[cnt+1]++;
						h[cnt+2]++;
						frame++;
					}
				}
				else {
					if(p + pre == 10) h[cnt+1]++;
					flg = false;
					frame++;
				}
				score[i] += p * h[cnt];
				cnt++;
			}
			for(int j=0; j<3; j++){
				cin >> p;
				score[i] += p * h[cnt];
				cnt++;
				if(j == 0) pre = p;
				else if(j == 1 && p + pre < 10) break;
			}
		}
		
		for(int i=n-1; i>0; i--){
			for(int j=0; j<i; j++){
				if(score[index[j]] > score[index[j+1]]){
					tmp = index[j];
					index[j] = index[j+1];
					index[j+1] = tmp;
				}
			}
		}
		
		for(int i=n-1; i>=0; i--){
			cout << sn[index[i]] << " " << score[index[i]] << endl;
		}
		
	}

	return 0;
	
}