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<cstring>
#include<utility>
#include<algorithm>
#include<functional>
using namespace std;

#define MAX_N 300000

int game[MAX_N][10], x[MAX_N][10][3];
int score[MAX_N][10];
int sum[MAX_N], cust[MAX_N];
int n, cnt, sum2;

pair<int, int>bowling[MAX_N];

int main() {
	while (true) {
		//memset.
		memset(game, 0, sizeof(game));
		memset(x, 0, sizeof(x));
		memset(score, 0, sizeof(score));
		memset(sum, 0, sizeof(sum));
		memset(cust, 0, sizeof(cust));
		memset(bowling, 0, sizeof(bowling));

		cin >> n;
		if (n == 0) {
			break;
		}

		for (int i = 0; i < n; i++) {
			cin >> cust[i];

			for (int j = 0; j < 10; j++) {
				//cin.
				cnt = 0;
				while (true) {
					cin >> x[i][j][cnt];

					//1-9 Frame.
					if (j < 9) {
						if (x[i][j][cnt] == 10) {
							goto Exit;
						}
						if (cnt == 1) {
							goto Exit;
						}
					}

					//10 Frame.
					else {
						if (cnt == 1) {
							if (x[i][j][0] != 10 && x[i][j][0] + x[i][j][1] != 10) {
								goto Exit;
							}
						}
						else if (cnt == 2) {
							goto Exit;
						}
					}

					//End Throw.
					cnt++;
				}
			Exit:;
				game[i][j] = cnt + 1;
			}
		}
		
		//check.
		for (int i = 0; i < n; i++) {

			//sum.
			for (int j = 0; j < 10; j++) {
				sum2 = 0;
				for (int k = 0; k < game[i][j]; k++) {
					sum2 += x[i][j][k];
				}

				//1-9 Frame.
				if (j < 9) {
					//Normal.
					if (sum2 < 10) {
						score[i][j] = sum2;
					}

					//Spair.
					else if (game[i][j] == 2) {
						score[i][j] = 10 + x[i][j + 1][0];
					}

					//Strike!!
					else {
						if (game[i][j] == 1) {
							score[i][j] = 10 + x[i][j + 1][0] + x[i][j + 2][0];
						}
						else {
							score[i][j] = 10 + x[i][j + 1][0] + x[i][j + 1][1];
						}
					}
				}

				//10 Frame.
				else {
					score[i][j] = sum2;
				}
			}

			//Calc.
			for (int j = 0; j < 10; j++) {
				sum[i] += score[i][j];
			}
		}

		//pair-input.
		for (int i = 0; i < n; i++) {
			bowling[i] = make_pair(sum[i], cust[i]);
		}

		//sort.
		sort(bowling, bowling + n, greater<pair<int, int> >());

		//print.
		for (int i = 0; i < n; i++) {
			cout << bowling[i].second << ' ' << bowling[i].first << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int m;
    while(cin >> m, m) {
        vector<pair<int, int>> v;
        for (int i = 0; i < m; ++i) {
            int id;
            cin >> id;
            int ans = 0, one = 0, two = 0;
            for (int j = 0; j < 9; ++j) {
                int x;
                cin >> x;
                ans += x*(!!one+!!two+1);
                one = two; two = 0;
                if(x == 10) two++;
                else {
                    int y;
                    cin >> y;
                    ans += y*(!!one+!!two+1);
                    one = two; two = 0;
                    if(x+y == 10) one++;
                }
            }
            int x, y;
            cin >> x >> y;
            ans += x*(!!one+!!two+1);
            one = two; two = 0;
            ans += y*(!!one+!!two+1);
            one = two; two = 0;
            if(x == 10 || x+y == 10){
                int z;
                cin >> z;
                ans += z*(!!one+!!two+1);
            }
            v.emplace_back(ans, -id);
        }
        sort(v.begin(),v.end(), greater<>());
        for (auto &&k : v) {
            cout << -k.second << " " << k.first << "\n";
        }
    }
    return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <set>

using namespace std;

template<class T1, class T2>
T1 lexical_cast(T2 a){
  T1 tmp;
  stringstream ss;
  ss << a; ss >> tmp;
  return tmp;
}

int main(){
  string buff;
  int n;

  while(getline(cin, buff), n = lexical_cast<int>(buff)){
    set<pair<int, int>, greater<pair<int, int> > > ans;

    for(int i = 0; i < n; i++){
      stringstream ss;
      int id;
      int point = 0;
      int frame = 0;

      getline(cin, buff); ss << buff; ss >> id;
      
      vector<int> v((istream_iterator<int>(ss)),
		    istream_iterator<int>());

      enum state{
	NONE = 0,
	SPARE = 1,
	STRIKE = 2
      };

      state prev  = NONE;
      state pprev = NONE;
      
      int vv = 0;
      for(int f = 0; f < 10; f++){
	int now = v[vv++];

	point += now;
	if(prev  != NONE)   point += now;
	if(pprev == STRIKE) point += now;

	if(now == 10){
	  if(f == 9){
	    int now2 = v[vv++];
	    point += now2;
	    if(prev == STRIKE) point += now2;
	    if(now2 == 10)
	      point += v[vv++];
	  }

	  pprev = prev;
	  prev  = STRIKE;
	}else{
	  int now2 = v[vv++];
	  point += now2;

	  if(prev  == STRIKE) point += now2;

	  pprev = NONE;
	  prev  = NONE;

	  if(now + now2 == 10){
	    if(f == 9){
	      int now3 = v[vv++];
	      point += now3;
	    }
	    prev = SPARE;
	  }
	}
	// cout << "f=" << f+1 << ": " << point << " ";
	// cout << "prev=" << prev << " " << "pprev=" << pprev << endl;
      }

      ans.insert(make_pair(point, -id));
    }

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

  return 0;
}