Problem:


ボウリング

クラスのレクリエーションとしてボウリングを行うことになりました。参加者ごとの投球情報を入力とし、得点の高い順に成績情報を出力するプログラムを作成してください。なお、同点の場合は学籍番号の若い順に出力してください。ただし参加者は 3 名以上 40 名以下とし、1人当たり1ゲームずつ投球しているものとします。

ボウリングとは、プレイヤーに対して頂点を向けて正三角形に並べられた、10 本のピンをめがけてボールを転がし、ピンを倒すスポーツです。 1 回目の投球ですべて転倒した場合をストライクと言い、その投球のみで次のフレームに進みます。ストライクでない場合は、残ったピンをそのままにして 2 回目の投球を行います。2 回目の投球ですべて転倒した場合をスペアと言います。2 回目の投球終了後、次のフレームに進みます。

1 ゲームは 10 のフレームから構成され、第 1 から第 9 の各フレームは 2 回投球できます。各フレームの開始時点では、10 本のピンがすべて立った状態で用意されます。第 10 フレームは、ストライクもしくはスペアが出た場合には計 3 回の投球を、それ以外の場合は 2 回の投球を行い、ゲーム終了となります。

スコア例１

スコア例２（最高得点300点の場合）

スコア計算の方法

 各フレームにおいてスペア、ストライクがない場合は、 2 回の投球で倒したピンの本数がそのフレームの得点となります。（スコア例１の第 4 フレームと第 8 フレーム）

スペアを出した場合、倒した本数である 10 点に加え、次の投球で倒したピンの本数がこのフレームの得点に加算されます。（スコア例１の第 1 フレームと第 2 フレームの関係など）スコア例１の第 1 フレームでは第 2 フレームの 1 投で倒した 10 本（点）を加えた 20 点が得点となります。第 3 フレームも同様の計算方法です。

ストライクを出した場合、倒した本数である 10 点に加え、続く 2 回の投球で倒したピンの本数が加算されます。（スコア例１の第 2 フレームと第 3 フレームの関係など）もちろん続く 2 投中にストライクの場合があります。（スコア例１の第 5 フレームと第 6、7 フレームの関係など）

第 10 フレームのみ、スペア、ストライクを出した場合、3 投して倒したピンの総数が第 10 フレームの得点として加算されます。

各フレームの得点の合計が 1 ゲームの得点となり、最高得点は 300 点となります。

Input

複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。
各データセットは以下の形式で与えられます。

m
score1
score2
:
scorem

1行目に参加者数 m （3 ≤ m ≤ 40）、続くm 行にi 人目の参加者情報 scorei が与えられます。各参加者情報は１行ずつ次の形式で与えられます。

id s1 s2 ... sn

先頭に学籍番号id (0 ≤ id ≤ 9999)、続いて第 j 投の転倒ピン数 sj (0 ≤ sj ≤ 10) が与えられます。総投球数 n は、12 以上 21 以下であり、得点計算に必要なピン数が過不足なく与えられるものとします。

Output

入力データセットごとに、学籍番号と得点を、得点の高い順（同点の場合は学籍番号の若い順）に出力します。学籍番号と得点を１つの空白で区切って１行に出力してください。

Sample Input

3
1010 6 3 10 7 1 0 7 9 1 10 6 2 4 3 9 1 9 0
1200 5 3 9 1 7 1 0 0 8 1 10 10 4 3 9 1 8 2 9
1101 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
4
3321 8 2 10 9 1 7 0 10 10 10 0 8 10 10 10 10
3332 5 0 10 9 1 4 1 9 0 10 10 7 1 5 2 8 1
3335 10 10 10 10 10 10 10 10 10 10 10 10
3340 8 2 7 3 6 4 8 2 8 2 9 1 7 3 6 4 8 2 9 1 7
0

Output for the Sample Input

1200 127
1010 123
1101 60
3335 300
3321 200
3340 175
3332 122



Sample input:
3
1010 6 3 10 7 1 0 7 9 1 10 6 2 4 3 9 1 9 0
1200 5 3 9 1 7 1 0 0 8 1 10 10 4 3 9 1 8 2 9
1101 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
4
3321 8 2 10 9 1 7 0 10 10 10 0 8 10 10 10 10
3332 5 0 10 9 1 4 1 9 0 10 10 7 1 5 2 8 1
3335 10 10 10 10 10 10 10 10 10 10 10 10
3340 8 2 7 3 6 4 8 2 8 2 9 1 7 3 6 4 8 2 9 1 7
0


Sample output:
1200 127
1010 123
1101 60
3335 300
3321 200
3340 175
3332 122

Submission status: Wrong Answer

Solution code:
# from sys import exit

while(True):
    N = int(input())
    if N == 0:
        exit()
    score = [[] for _ in range(N)]
    ids = [0 for _ in range(N)]
    for i in range(N):
        score[i] = [int(n) for n in input().split()]
        ids[i] = score[i][0]
        cnt = 0 # フレームのための
        frame = 0 # 10フレームの処理
        sum = 0
        ans = 0
        # ストライクなら二回分、スペアなら一回分
        double = 0
        triple = 0
        for s in score[i][1:]:
            if triple and frame != 10:
                ans += 3*s
                # if i == 1:print(3*s, frame)
                triple -= 1
                double -= 1
            elif double and frame != 10:
                ans += 2*s
                # if i == 1:print(2*s, frame)
                double -= 1
            else:
                # if i == 1:print(s, frame)
                ans += s
            sum += s
            cnt += 1

            # ストライク
            if cnt == 1 and s == 10:
                cnt = 0
                sum = 0
                if double:
                    triple += 1
                    double = 2
                else:
                    double += 2
                frame+=1
            # スペア
            elif cnt == 2 and sum == 10:
                if double:
                    triple += 1
                else:
                    double += 1
            # フレームのリセット
            if (cnt == 2):
                # print("AAA" + str(sum), cnt)
                cnt = 0
                sum = 0
                frame+=1
        score[i] = (ids[i], ans)
    score = sorted(score, key=lambda x: x[0])
    score = sorted(score, key=lambda x: -x[1])
    for s in score:
        print(s[0], s[1])

Submission status: Wrong Answer

Solution code:
# from sys import exit

while(True):
    N = int(input())
    if N == 0:
        exit()
    score = [[] for _ in range(N)]
    ids = [0 for _ in range(N)]
    for i in range(N):
        score[i] = [int(n) for n in input().split()]
        ids[i] = score[i][0]
        cnt = 0 # フレームのための
        frame = 0 # 10フレームの処理
        sum = 0
        ans = 0
        # ストライクなら二回分、スペアなら一回分
        double = 0
        triple = 0
        for s in score[i][1:]:
            if triple and frame != 10:
                ans += 3*s
                # if i == 1:print(3*s, frame)
                triple -= 1
                double -= 1
            elif double and frame != 10:
                ans += 2*s
                # if i == 1:print(2*s, frame)
                double -= 1
            else:
                # if i == 1:print(s, frame)
                ans += s
            sum += s
            cnt += 1

            # ストライク
            if cnt == 1 and s == 10:
                cnt = 0
                sum = 0
                if double:
                    triple += 1
                    double = 2
                else:
                    double += 2
                frame+=1
            # スペア
            elif cnt == 2 and sum == 10:
                if double:
                    triple += 1
                else:
                    double += 1
            # フレームのリセット
            if (cnt == 2):
                # print("AAA" + str(sum), cnt)
                cnt = 0
                sum = 0
                frame+=1
        score[i] = (ids[i], ans)
    score = sorted(score, key=lambda x: x[0])
    score = sorted(score, key=lambda x: -x[1])
    n = len(score)
    for s in score:
        if n:
            print(s[0], s[1], end=" \n")
        else:
            print(s[0], s[1])

Submission status: Wrong Answer

Solution code:
def score(x):
  sc = 0
  j = 0
  for _ in [0]*10:
    a = 2
    b = 3
    if x[j]==10: a=1
    elif x[j]+x[j+1]!=10: b=2
    sc += sum(x[j:j+b])
    j += a
  return sc

while 1:
  n = input()
  if n==0: break
  A = []
  for _ in [0]*n:
    x = map(int,raw_input().split())
    A.append([x[0], score(x[1:])])
  for a, b in sorted(A, key=lambda x: -x[1]):
    print a,b

Submission status: Wrong Answer

Solution code:
def score(x):
  sc = 0
  j = 0
  for _ in [0]*10:
    a = 2
    b = 3
    if x[j]==10: a=1
    elif x[j]+x[j+1]!=10: b=2
    sc += sum(x[j:j+b])
    j += a
  return sc

while 1:
  n = input()
  if n==0: break
  A = []
  for _ in [0]*n:
    x = map(int,raw_input().split())
    A.append([x[0], score(x[1:])])
  for a, b in sorted(A, key=lambda x: {-x[1], x[0]}):
    print a,b

Submission status: Wrong Answer

Solution code:
def f1():
    frame = 1
    total = 0
    i = 0
    while frame < 10:
        if score[i] == 10:
            total += (score[i] + score[i+1] + score[i+2])
            i += 1
        elif score[i] + score[i+1] == 10:
            total += (score[i] + score[i+1] + score[i+2])
            i += 2
        else:
            total += (score[i] + score[i+1])
            i += 2
        frame += 1
    else:
        total += sum(score[i:])
        res[sn] = total

def f2():
    for k, v in sorted(res.items(), key=lambda x: x[1], reverse=True):
        print -k, v

while True:
    n = input()
    if n == 0: break
    res = {}
    for i in range(n):
        L = map(int, raw_input().split())
        sn = -L[0]
        score = L[1:]
        f1()
    else:
        f2()

Submission status: Wrong Answer

Solution code:
def f1():
    frame = 1
    total = 0
    i = 0
    while frame < 10:
        if score[i] == 10:
            total += (score[i] + score[i+1] + score[i+2])
            i += 1
        elif score[i] + score[i+1] == 10:
            total += (score[i] + score[i+1] + score[i+2])
            i += 2
        else:
            total += (score[i] + score[i+1])
            i += 2
        frame += 1
    else:
        total += sum(score[i:])
        res[sn] = total

def f2():
    for k, v in sorted(res.items(), key=lambda x: (x[1], x[0]), reverse=True):
        print -k, v

while True:
    n = input()
    if n == 0: break
    res = {}
    for i in range(n):
        L = map(int, raw_input().split())
        sn = -L[0]
        score = L[1:]
        f1()
    else:
        f2()

Submission status: Wrong Answer

Solution code:
def f1():
    frame = 1
    total = 0
    i = 0
    while frame < 10:
        if score[i] == 10:
            total += (score[i] + score[i+1] + score[i+2])
            i += 1
        elif score[i] + score[i+1] == 10:
            total += (score[i] + score[i+1] + score[i+2])
            i += 2
        else:
            total += (score[i] + score[i+1])
            i += 2
        frame += 1
    else:
        total += sum(score[i:])
        res[sn] = total

def f2():
    for k, v in sorted(res.items(), key=lambda x:(x[1], x[0]), reverse=True):
        print -k, v

while True:
    n = input()
    if n == 0: break
    res = {}
    for i in range(n):
        L = map(int, raw_input().split())
        sn = -L[0]
        score = L[1:]
        f1()
    else:
        f2()

Submission status: Wrong Answer

Solution code:
def f1():
    frame = 1
    total = 0
    i = 0
    while frame < 10:
        if score[i] == 10:
            total += (score[i] + score[i+1] + score[i+2])
            i += 1
        elif score[i] + score[i+1] == 10:
            total += (score[i] + score[i+1] + score[i+2])
            i += 2
        else:
            total += (score[i] + score[i+1])
            i += 2
        frame += 1
    else:
        total += sum(score[i:])
        res[sn] = total

def f2():
    for k, v in sorted(res.items(), key=lambda x:(x[1], x[0]), reverse=True):
        print abs(k), v

while True:
    n = input()
    if n == 0: break
    res = {}
    for i in range(n):
        L = map(int, raw_input().split())
        sn = -L[0]
        score = L[1:]
        f1()
    else:
        f2()

Submission status: Wrong Answer

Solution code:
def get_point(info):
  info.reverse()
  sum_acc = 0
  NORMAL, SPARE, STRIKE, DOUBLE = 0, 1, 2, 3
  flag = NORMAL
  game_num = 0
  while info:
    if game_num != 9:
      down_num1 = info.pop()
      if down_num1 != 10:
        down_num2 = info.pop()
        if flag == SPARE:
          sum_acc += down_num1 * 2 + down_num2
        elif flag == STRIKE:
          sum_acc += down_num1 * 2 + down_num2 * 2
        elif flag == DOUBLE:
          sum_acc += down_num1 * 3 + down_num2 * 2
        else:
          sum_acc += down_num1 + down_num2
        
        if down_num1 + down_num2 == 10:
          flag = SPARE
        else:
          flag = NORMAL
      
      else:
        if flag in (SPARE, STRIKE):
          sum_acc += down_num1 * 2
        elif flag == DOUBLE:
          sum_acc += down_num1 * 3
        else:
          sum_acc += down_num1
        if flag in (STRIKE, DOUBLE):
          flag = DOUBLE
        else:
          flag = STRIKE
      game_num += 1

    else:
      down_num1, down_num2 = info.pop(), info.pop()
      if flag == SPARE:
        sum_acc += down_num1 * 2 + down_num2
      elif flag == STRIKE:
        sum_acc += down_num1 * 2 + down_num2 * 2
      elif flag == DOUBLE:
        sum_acc += down_num1 * 3 + down_num2 * 2
      else:
        sum_acc += down_num1 + down_num2
      if info:
        sum_acc += info.pop()
  
  return sum_acc

while True:
  m = int(input())
  if m == 0:
    break
  score = [list(map(int, input().split())) for _ in range(m)]
  score_mp = [(info[0], get_point(info[1:])) for info in score]
  score_mp.sort(key=lambda x:-x[1])
  for pair in score_mp:
    print(*pair)

Submission status: Wrong Answer

Solution code:
# AOJ 0152 Bowling
# Python3 2018.6.20 bal4u

while True:
	n = int(input())
	if n == 0: break
	tbl = {}
	for _ in range(n):
		p = list(map(int, input().split()))
		id = p[0]
		del p[0]
		sum = k = 0
		for f in range(9):
			s = p[k]
			if s == 10:
				sum += 10+p[k+1]+p[k+2]
				k += 1
			else:
				s += p[k+1]
				sum += s
				k += 2
				if s == 10: sum += p[k]
		sum += p[k] + p[k+1]
		if p[k] == 10 or p[k]+p[k+1] == 10: sum += p[k+2]
		tbl[id] = sum
	ans = sorted(tbl.items(), key=lambda x:(x[1],-x[0]), reverse=True)
	for i in ans: print(*i)

Submission status: Wrong Answer

Solution code:
# AOJ 0152 Bowling
# Python3 2018.6.20 bal4u

while True:
	n = int(input())
	if n == 0: break
	tbl = {}
	for _ in range(n):
		p = list(map(int, input().split()))
		id = p[0]
		del p[0]
		sum = k = 0
		for f in range(9):
			s = p[k]
			if s == 10:
				sum += 10+p[k+1]+p[k+2]
				k += 1
			else:
				s += p[k+1]
				sum += s
				k += 2
				if s == 10: sum += p[k]
		sum += p[k] + p[k+1]
		if p[k] == 10 or p[k]+p[k+1] == 10: sum += p[k+2]
		tbl[id] = sum
	ans = sorted(tbl.items(), key=lambda x:(x[1],-x[0]), reverse=True)
	for i in ans: print(*i)

Submission status: Wrong Answer

Solution code:
# AOJ 0152 Bowling
# Python3 2018.6.20 bal4u

while True:
	n = int(input())
	if n == 0: break
	tbl = {}
	for _ in range(n):
		p = list(map(int, input().split()))
		id = p[0]
		del p[0]
		sum = k = 0
		for f in range(1, 11):
			if f == 10:
				sum += p[k] + p[k+1]
				if p[k] == 10 or p[k]+p[k+1] == 10: sum += p[k+2]
				break
			s = p[k]
			if s == 10:
				sum += 10+p[k+1]+p[k+2]
				k += 1
			else:
				s += p[k+1]
				sum += s
				k += 2
				if s == 10: sum += p[k]
		tbl[id] = sum
	ans = sorted(tbl.items(), key=lambda x:(x[1],-x[0]), reverse=True)
	for i in ans: print(*i)

Submission status: Wrong Answer

Solution code:
# AOJ 0152 Bowling
# Python3 2018.6.20 bal4u

while True:
	n = int(input())
	if n == 0: break
	tbl = {}
	for _ in range(n):
		p = list(map(int, input().split()))
		id = p[0]
		del p[0]
		sum = k = 0
		for f in range(9):
			if p[k] == 10:
				sum += p[k]+p[k+1]+p[k+2]
				k += 1
			else:
				sum += p[k]+p[k+1]
				if p[k]+p[k+1] == 10: sum += p[k+2]
				k += 2
		sum += p[k] + p[k+1]
		if p[k] == 10 or p[k]+p[k+1] == 10: sum += p[k+2]
		tbl[id] = sum
	ans = sorted(tbl.items(), key=lambda x:(x[1],-x[0]), reverse=True)
	for i in ans: print(*i)

Submission status: Wrong Answer

Solution code:
# AOJ 0152 Bowling
# Python3 2018.6.21 bal4u

while True:
	n = int(input())
	if n == 0: break
	tbl = {}
	for _ in range(n):
		p = list(map(int, input().split()))
		id = int(p.pop(0))
		sum = 0
		for f in range(10-1):
			if p[0] == 10:
				sum += p[0]+p[1]+p[2]
			else:
				sum += p[0]+p[1]
				if p[0]+p[1] == 10: sum += p[2]
				p.pop(0)
			p.pop(0)
		sum += p[0]+p[1]
		if p[0] == 10 or p[0]+p[1] == 10: sum += p[2] #10番フレーム		
		tbl[id] = sum
	for i in sorted(tbl.items(), key=lambda x:(x[1],-x[0]), reverse=True): print(*i)

Submission status: Wrong Answer

Solution code:
# AOJ 0152 Bowling
# Python3 2018.6.21 bal4u

while True:
	n = int(input())
	if n == 0: break
	tbl = {}
	for _ in range(n):
		p = list(map(int, input().split()))
		id, s = p.pop(0), 0
		for f in range(9):
			if p[0] == 10: s += p[0]+p[1]+p[2]
			else:
				s += p[0]+p[1]
				if p[0]+p[1] == 10: s += p[2]
				p.pop(0)
			p.pop(0)
		s += p[0]+p[1]  #10番フレーム
		if p[0] == 10 or p[0]+p[1] == 10: s += p[2]
		tbl[id] = s
	for i in sorted(tbl.items(), key=lambda x:(-x[1],x[0])): print(*i)

Submission status: Wrong Answer

Solution code:
def calcScore(L):
    score=0
    j,frame=0,1
    for i in range(len(L)):
        if frame==10:
            if L[i]==10:
                score+=sum(L[i:i+3])
                break
            elif L[i]+L[i+1]==10:
                score+=sum(L[i:i+3])
                break
            else:
                score+=sum(L[i:])
                break
        if L[i]==10:
            score+=sum(L[i:i+3])
            frame+=1
        elif j==1 and L[i]+L[i-1]==10:
            score+=sum(L[i:i+2])
            j=0
            frame+=1
        else:
            score+=L[i]
            j+=1
        if j==2:
            j=0
            frame+=1
    return score

while True:
    m=input()
    if m==0:break
    L=[map(int,raw_input().split()) for i in range(m)]
    res=[(-calcScore(i[1:]),i[0])for i in L]
    res.sort()
    for i,j in res:
        print j,-i

Submission status: Wrong Answer

Solution code:
def calcScore(L):
    score=0
    j,frame=0,1
    for i in range(len(L)):
        if frame==10:
            score+=sum(L[i:i+3])
            break
        if L[i]==10:
            score+=sum(L[i:i+3])
            frame+=1
        elif j==1 and L[i]+L[i-1]==10:
            score+=sum(L[i:i+2])
            j=0
            frame+=1
        else:
            score+=L[i]
            j+=1
        if j==2:
            j=0
            frame+=1
    return score

while True:
    m=input()
    if m==0:break
    L=[map(int,raw_input().split()) for i in range(m)]
    res=[(-calcScore(i[1:]),i[0])for i in L]
    res.sort()
    for i,j in res:
        print j,-i

Submission status: Wrong Answer

Solution code:
def calcScore(L):
    frame=1
    j=0
    score=[0]*10
    for i in range(len(L)):
        if frame==10:
            score[frame-1]=sum(L[i:])
            return sum(score)
        if L[i]==10:
            score[frame-1]=sum(L[i:i+3])
            frame+=1
        elif j==1 and L[i]+L[i-1]==10:
            score[frame-1]+=sum(L[i:i+2])
            frame+=1
            j=0
        else:
            score[frame-1]+=L[i]
            j+=1
        if j==2:
            j=0
            frame+=1

while True:
    m=input()
    if m==0:break
    L=[map(int,raw_input().split()) for i in range(m)]
    res=[(-calcScore(i[1:]),i[0])for i in L]
    res.sort()
    for i,j in res:
        print j,-i

Submission status: Wrong Answer

Solution code:
def f(L):
    i = 1
    ret = 0
    for _ in xrange(9):
        if L[i] == 10:
            ret += 10 + L[i+1] + L[i+2]
            i += 1
        elif L[i] + L[i+1] == 10:
            ret += 10 + L[i+2]
            i += 2
        else:
            ret += L[i] + L[i+1]
            i += 2
    return L[0],ret+sum(L[i:])

while True:
    n = input()
    if n == 0:
        break
    A = sorted([f(map(int,raw_input().split())) for _ in xrange(n)],
               key = lambda (i,x):-x)
    for i,x in A:
        print i,x

Submission status: Wrong Answer

Solution code:
#Bowling
def solve(S):
    ret, i = 0, 0
    for g in xrange(10):
        if S[i] == 10:
            ret += sum(S[i:i + 3])
            i += 1
        elif sum(S[i:i + 2]) == 10:
            ret += sum(S[i:i + 3])
            i += 2
        else:
            ret += sum(S[i:i + 2])
            i += 2
    return ret

while True:
    m = input()
    if m == 0:
        break
    ans = []
    for _ in xrange(m):
        data = map(int, raw_input().split())
        n, s = data[0], data[1:]
        ans += [(solve(s), n)]
    print "\n".join(map(lambda x:"{1} {0}".format(*x), sorted(ans)[::-1]))

Submission status: Wrong Answer

Solution code:
while 1:
	n=input()
	if n==0:break
	games=[map(int,raw_input().split()) for i in range(n)]
	ls=[]
	for game in games:
		score,flame,throw=0,1,1
		while throw<len(game):
			if flame<10:
				if game[throw]==10:
					score+=sum(game[throw:throw+3])
					throw+=1
				elif game[throw]+game[throw+1]==10:
					score+=sum(game[throw:throw+3])
					throw+=2
				else:
					score+=game[throw]+game[throw+1]
					throw+=2
				flame+=1
			else:
				score+=game[throw]
				throw+=1
		ls.append([game[0],score])
	for k,s in sorted(ls,key=lambda x:x[1],reverse=True):
		print k,s

Submission status: Accepted

Solution code:
def score(x):
  sc = 0
  th = 1
  for _ in [0]*10:
    a = 2
    b = 3
    if x[th]==10: a=1
    elif x[th]+x[th+1]!=10: b=2
    sc += sum(x[th:th+b])
    th += a
  return x[0],sc

while 1:
  n = input()
  if n==0: break
  A = [score(map(int,raw_input().split())) for _ in [0]*n]
  for a, b in sorted(A, key=lambda x: (-x[1], x[0])):
    print a,b

Submission status: Accepted

Solution code:
def score(x):
  sc = 0
  th = 1
  for _ in [0]*10:
    a = 2
    b = 3
    if x[th]==10: a=1
    elif x[th]+x[th+1]!=10: b=2
    sc += sum(x[th:th+b])
    th += a
  return x[0], sc

while 1:
  n = input()
  if n==0: break
  A = sorted([score(map(int, raw_input().split())) for _ in [0]*n])
  for a, b in sorted(A, key=lambda x: -x[1]): print a,b

Submission status: Accepted

Solution code:
def score(x):
  sc = 0
  th = 1
  for _ in [0]*10:
    a = x[th]+x[th+1]
    if x[th]==10:
      sc += 10+x[th+1]+x[th+2]
      th -= 1
    elif a==10: sc += 10+x[th+2]
    else: sc += a
    th += 2
  return x[0], sc

while 1:
  n = input()
  if n==0: break
  A = sorted([score(map(int, raw_input().split())) for _ in [0]*n], key=lambda x: (-x[1], x[0]))
  for a, b in A: print a, b

Submission status: Accepted

Solution code:
def f1():
    frame = 1
    total = 0
    i = 0
    while frame < 10:
        if score[i] == 10:
            total += (score[i] + score[i+1] + score[i+2])
            i += 1
        elif score[i] + score[i+1] == 10:
            total += (score[i] + score[i+1] + score[i+2])
            i += 2
        else:
            total += (score[i] + score[i+1])
            i += 2
        frame += 1
    else:
        total += sum(score[i:])
        res.append([sn,total])

def f2():
    for k, v in sorted(sorted(res), key=lambda x:x[1], reverse=True):
        print k, v

while True:
    n = input()
    if n == 0: break
    res = []
    for i in range(n):
        L = map(int, raw_input().split())
        sn = L[0]
        score = L[1:]
        f1()
    else:
        f2()

Submission status: Accepted

Solution code:
def score(x):
    s=0;t=1
    for _ in[0]*10:
        a=2;b=3
        if x[t]==10:a=1
        elif x[t]+x[t+1]!=10:b=2
        s+=sum(x[t:t+b]);t+=a
    return x[0],s

while 1:
    n=int(input())
    if n==0:break
    A=sorted([score(list(map(int,input().split()))) for _ in [0]*n])
    for a,b in sorted(A,key=lambda x:-x[1]):print(a,b)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0152

"""
import sys
from sys import stdin
input = stdin.readline


def bowling_score(data):
    name_id = data[0]
    data = data[1:]

    frame = 1
    scores = [0] * 11

    while frame <= 10:
        if data[0] == 10:
            scores[frame] = data[0] + data[1] + data[2]
            data = data[1:]
            frame += 1
        elif 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
    return name_id, sum(scores[1:])


def main(args):
    while True:
        m = int(input())
        if m == 0:
            break
        results = []
        for _ in range(m):
            data = [int(x) for x in input().split()]
            name_id, score = bowling_score(data)
            results.append([-score, name_id])

        results.sort()
        for s, i in results:
            print(i, -s)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
FIRST = 1
SECOND = 2

while True:

    score_count = int(input())

    if score_count == 0:
        break

    score_list = []

    for _ in range(score_count):

        total_score = 0
        score_data = [int(item) for item in input().split(" ")]

        last_score = 0
        frame_count = 1
        ball_state = FIRST

        for index, score in enumerate(score_data[1:], 1):

            if frame_count == 10:
                total_score += sum(score_data[index:])
                break

            if last_score + score == 10:
                if ball_state == FIRST:
                    # ストライク
                    total_score += score + score_data[index + 1] + score_data[index + 2]
                else:
                    # スペア
                    total_score += score + score_data[index + 1]
                    ball_state = FIRST
                    last_score = 0

                frame_count += 1

            else:
                # 通常
                total_score += score

                if ball_state == FIRST:
                    ball_state = SECOND
                    last_score = score
                else:
                    ball_state = FIRST
                    last_score = 0
                    frame_count += 1

        score_list.append([score_data[0], total_score])

    score_list.sort(key=lambda x: (-x[1], x[0]))
    output = [str(id) + " " + str(score) for id, score in score_list]
    print("\n".join(output))

Submission status: Accepted

Solution code:
def get_point(info):
  info.reverse()
  acc = 0
  NORMAL, SPARE, STRIKE, DOUBLE = 0, 1, 2, 3
  flag = NORMAL
  game_num = 0
  while info:
    if game_num != 9:
      down_num1 = info.pop()
      if down_num1 != 10:
        down_num2 = info.pop()
        if flag == SPARE:
          acc += down_num1 * 2 + down_num2
        elif flag == STRIKE:
          acc += down_num1 * 2 + down_num2 * 2
        elif flag == DOUBLE:
          acc += down_num1 * 3 + down_num2 * 2
        else:
          acc += down_num1 + down_num2
        
        if down_num1 + down_num2 == 10:
          flag = SPARE
        else:
          flag = NORMAL
      
      else:
        if flag in (SPARE, STRIKE):
          acc += down_num1 * 2
        elif flag == DOUBLE:
          acc += down_num1 * 3
        else:
          acc += down_num1
        if flag in (STRIKE, DOUBLE):
          flag = DOUBLE
        else:
          flag = STRIKE
      game_num += 1

    else:
      down_num1, down_num2 = info.pop(), info.pop()
      if flag == SPARE:
        acc += down_num1 * 2 + down_num2
      elif flag == STRIKE:
        acc += down_num1 * 2 + down_num2 * 2
      elif flag == DOUBLE:
        acc += down_num1 * 3 + down_num2 * 2
      else:
        acc += down_num1 + down_num2
      if info:
        acc += info.pop()
  
  return acc

while True:
  m = int(input())
  if m == 0:
    break
  score = [list(map(int, input().split())) for _ in range(m)]
  score_mp = [(info[0], get_point(info[1:])) for info in score]
  score_mp.sort()
  score_mp.sort(key=lambda x:-x[1])
  for pair in score_mp:
    print(*pair)

Submission status: Accepted

Solution code:
def calcScore(L):
    j,score=0,0
    for i in range(10):
        if L[j]==10:
            score+=sum(L[j:j+3])
            j+=1
        elif L[j]+L[j+1]==10:
            score+=10+L[j+2]
            j+=2
        else:
            score+=L[j]+L[j+1]
            j+=2
    return score

while True:
    m=input()
    if m==0:break
    L=[map(int,raw_input().split()) for i in range(m)]
    res=[(-calcScore(i[1:]),i[0])for i in L]
    res.sort()
    for i,j in res:
        print j,-i

Submission status: Accepted

Solution code:
#Bowling
def solve(S):
    ret, i = 0, 0
    for g in xrange(10):
        if S[i] == 10:
            ret += sum(S[i:i + 3])
            i += 1
        elif sum(S[i:i + 2]) == 10:
            ret += sum(S[i:i + 3])
            i += 2
        else:
            ret += sum(S[i:i + 2])
            i += 2
    return ret

while True:
    m = input()
    if m == 0:
        break
    ans = []
    for _ in xrange(m):
        data = map(int, raw_input().split())
        n, s = data[0], data[1:]
        ans += [(solve(s), n)]
    print "\n".join(map(lambda x:"{1} {0}".format(*x), sorted(ans, key = lambda (a, b): (-a, b))))

Submission status: Accepted

Solution code:
while 1:
	n=input()
	if n==0:break
	games=[map(int,raw_input().split()) for i in range(n)]
	ls=[]
	for game in games:
		score,flame,thrw=0,1,1
		while thrw<len(game):
			if flame<10:
				if game[thrw]==10:
					score+=sum(game[thrw:thrw+3])
					thrw+=1
				elif game[thrw]+game[thrw+1]==10:
					score+=sum(game[thrw:thrw+3])
					thrw+=2
				else:
					score+=game[thrw]+game[thrw+1]
					thrw+=2
				flame+=1
			else:
				score+=game[thrw]
				thrw+=1
		ls.append([game[0],score])
	for k,s in sorted(sorted(ls),key=lambda x:x[1],reverse=True):
		print k,s

Submission status: Accepted

Solution code:
while 1:
	n=input()
	if n==0:break
	games=[map(int,raw_input().split()) for i in range(n)]
	ls=[]
	for game in games:
		score,thrw=0,1
		for flame in range(10):
			if game[thrw]==10:
				score+=sum(game[thrw:thrw+3])
				thrw+=1
			elif game[thrw]+game[thrw+1]==10:
				score+=10+game[thrw+2]
				thrw+=2
			else:
				score+=game[thrw]+game[thrw+1]
				thrw+=2
		else:
			ls.append([game[0],score])
	for k,s in sorted(sorted(ls),key=lambda x:x[1],reverse=True):
		print k,s

Submission status: Accepted

Solution code:
while 1:
	n=input()
	if n==0:break
	ls=[0]*n
	for i in range(n):
		game=map(int,raw_input().split())
		score,thrw=0,1
		for flame in range(10):
			if game[thrw]==10:
				score+=sum(game[thrw:thrw+3])
				thrw+=1
			elif game[thrw]+game[thrw+1]==10:
				score+=10+game[thrw+2]
				thrw+=2
			else:
				score+=game[thrw]+game[thrw+1]
				thrw+=2
		else:
			ls[i]=[game[0],score]
	for k,s in sorted(sorted(ls),key=lambda x:x[1],reverse=True):
		print k,s

Submission status: Accepted

Solution code:
def bowling(game):
	game=map(int,game.split())
	score,thrw=0,1
	for flame in range(10):
		if game[thrw]==10:
			score+=sum(game[thrw:thrw+3])
			thrw+=1
		elif game[thrw]+game[thrw+1]==10:
			score+=10+game[thrw+2]
			thrw+=2
		else:
			score+=game[thrw]+game[thrw+1]
			thrw+=2
	return game[0],score
	
while 1:
	n=input()
	if n==0:break
	ls=[bowling(raw_input()) for i in range(n)]
	for k,s in sorted(sorted(ls),key=lambda x:x[1],reverse=True):
		print k,s

Submission status: Accepted

Solution code:
def bowling(game):
	game=map(int,game.split())
	score,thrw=0,1
	for flame in range(10):
		if game[thrw]==10:
			score+=sum(game[thrw:thrw+3])
			thrw+=1
		elif game[thrw]+game[thrw+1]==10:
			score+=10+game[thrw+2]
			thrw+=2
		else:
			score+=sum(game[thrw:thrw+2])
			thrw+=2
	return game[0],score
	
while 1:
	n=input()
	if n==0:break
	ls=[bowling(raw_input()) for i in range(n)]
	for k,s in sorted(sorted(ls),key=lambda x:x[1],reverse=1):
		print k,s

Submission status: Accepted

Solution code:
while 1:
    M = int(input())
    if M == 0:
        break
    S = []
    for i in range(M):
        t, *score = map(int, input().split())
        frame = rd = cum = 0
        L = len(score)
        res = sum(score)
        for i in range(L):
            s = score[i]
            if rd == 0:
                cum = s
                if s == 10:
                    res += score[i+1] + score[i+2]
                    frame += 1
                else:
                    rd = 1
            else:
                cum += s
                if cum == 10:
                    res += score[i+1]
                frame += 1
                rd = 0
            if frame == 9:
                break
        S.append((-res, t))
    S.sort()
    for x, y in S:
        print(y, -x)