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 <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>

using namespace std;

int main()
{
	int n,num[50],score[50]={0},tmp;
	int s_max,ans_num;
	vector<int> pin;

	while(cin>>n){
		if(n==0) break;
		for(int i=0;i<n;i++){
			cin>>num[i];
			score[i]=0; pin.clear();
			for(int j=0;j<9;j++){
				cin>>tmp; score[i]+=tmp; pin.push_back(tmp);
				if(tmp==10) continue;
				cin>>tmp; score[i]+=tmp; pin.push_back(tmp);
			}

			cin>>tmp; score[i]+=tmp; pin.push_back(tmp);
			cin>>tmp; score[i]+=tmp; pin.push_back(tmp);
			if(pin[pin.size()-2]==10 || pin[pin.size()-1]+pin[pin.size()-2]==10){
				cin>>tmp; score[i]+=tmp; pin.push_back(tmp);
			}


			for(int j=0,k=0;j<9;j++){
				if(pin[k]==10){
					score[i]+=pin[k+1]+pin[k+2]; k++;
				}else if(pin[k]+pin[k+1]==10){
					score[i]+=pin[k+2]; k+=2;
				}else{k+=2;}
			}

		}
		
		for(int i=0;i<n;i++){
			s_max=-1;
			for(int j=0;j<n;j++){
				if(score[j]>s_max){ s_max=score[j]; ans_num=j;}
				else if(score[j]==s_max && num[j]<ans_num){ s_max=score[j]; ans_num=j;}

			}
			cout<<num[ans_num]<<" "<<score[ans_num]<<endl;
			score[ans_num]=0;
		}
	}
	
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

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

static const double eps = 10e-6;

class mycmp{
public:
	bool operator() (const P p1,const P p2){
		if(p1.second == p2.second) return p1.first < p2.first;
		else return p1.second > p2.second;
	}
};

vector<int> split(string delim,string str){
	vector<int> res;
	str += delim;
	for(int i=0;i<str.size();i++){
		for(int j=1;i+j<=str.size();j++){
			if(str.substr(i,j).find(delim) != string::npos){
				int num = atoi(str.substr(i,j-1).c_str());
				if(str.substr(i,j-1).size() >0 ) res.push_back(num);
				i+=j-1;
				break;
			}
		}
	}

	return res;
}

int main(){

	string str;
	while(getline(cin,str)){
		int n = atoi(str.c_str());
		if(n==0) break;

		vector<P> board;
		for(int t=0;t<n;t++){
			
			getline(cin,str);
			vector<int> data = split(" ",str);

			int num = data[0];
			int score = 0;
			for(int i=1,j=1;i<data.size();){
				if(j==10){
					if(data[i] == 10){
						score += data[i] + data[i+1] + data[i+2];
						i+=3;
					}

					else if(i+2< data.size() && data[i] + data[i+1] == 10){
						score += data[i] + data[i+1] + data[i+2];
						i+=3;
					}

					else if(i+1< data.size() && data[i] + data[i+1] < 10){
						score += data[i] + data[i+1];
						i+=2;
					}
				}

				else if(data[i] == 10) {
					score += 10;
					if(i+1 < data.size()) score += data[i+1];
					if(i+2 < data.size()) score += data[i+2];
					j++;
					i++;
				}
				else if(i+1< data.size() && data[i] + data[i+1] == 10){
					score += 10;
					if(i+1 < data.size()) score += data[i+2];
					j++;
					i+=2;
				}
				else if(i+1< data.size() && data[i] + data[i+1] < 10){
					score += data[i] + data[i+1];
					j++;
					i+=2;
				}

			}
			board.push_back(P(num,score));
		}

		sort(board.begin(),board.end(),mycmp());

		for(int i=0;i<board.size();i++){
			printf("%d %d\n",board[i].first,board[i].second);
		}
	}
}

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

typedef struct
{
  int id, score;
} Student;

class GreaterScore
{
public:
  bool operator() (const Student& lhs, const Student& rhs) const
  {
    if (lhs.score == rhs.score)
      return lhs.id < rhs.id;
    return lhs.score > rhs.score;
  }
};

int main()
{
  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    vector<Student> s(n);
    for (int i = 0; i < n; ++i) {
      cin >> s[i].id;

      vector<int> p;
      for (int j = 0; j < 10; ++j) {
	int p1;
	cin >> p1;
	p.push_back(p1);
	if (p1 == 10 && j != 9)
	  continue;

	int p2;
	cin >> p2;
	p.push_back(p2);

	if (p1+p2 >= 10 && j == 9) {
	  int p3;
	  cin >> p3;
	  p.push_back(p3);
	}
      }

      int idx = 0;
      bool first = true;
      vector<int> frame(10, 0);
      for (unsigned int j = 0; j < p.size(); ++j) {
	if (idx == 9) {
	  frame[idx] += p[j];
	  continue;
	} else if (first) {
	  frame[idx] += p[j];
	  if (frame[idx] == 10)
	    frame[idx++] += p[j+1] + p[j+2];
	  else
	    first = false;
	} else {
	  frame[idx] += p[j];
	  if (frame[idx] == 10)
	    frame[idx] += p[j+1];
	  ++idx;
	  first = true;
	}
      }
      s[i].score = accumulate(frame.begin(), frame.end(), 0);
    }

    sort(s.begin(), s.end(), GreaterScore());
    for (int i = 0; i < n; ++i)
      cout << s[i].id << " " << s[i].score << endl;
  }
  return 0;
}