Problem:


おはじき取り

一郎君と次郎君の兄弟は家でよくおはじき取りをして遊びます。おはじき取りは、一カ所に積まれた複数のおはじきを二人が交互にとっていくゲームです。一度に1〜4個のおはじきを好きな数だけ順に取り、相手に最後の1個を取らせた方が勝ちになります。二人はいつも 32 個のおはじきを使い、兄である一郎君の番からゲームを始めます。

これまでに何度も戦っている二人ですが、次郎君は兄の一郎君にどうしても勝つことができません。それもそのはず、一郎君はこのゲームの必勝法を知っているからです。一郎君は、残りのおはじきの数を n とすると、必ず (n - 1) % 5 個のおはじきを取ります。ここで x % y は、x  を y  で割った余りを示します。

一方、次郎君は、残りのおはじきの数にかかわらず、ゲームのはじめに各回で取るおはじきの数を数列として決めてしまうのです。例えば、次郎君が決めた数列が{ 3, 1, 4, 2 } であるならば、彼の取るおはじきの数は順に 3 -> 1 -> 4 -> 2 -> 3 -> 1 -> 4 -> … となります（取ると決めた数が、おはじきの残りの数以上になった場合は、残りのおはじき全てを取ります）。

なんど負けてもやり方を変えようとしない頑固な次郎君の将来が心配になったお母さんは、次郎君がいかなる数列を選んだとしても一郎君には勝てないということを示すために、ゲームをシュミレートするプログラムを書くことにしました。

次郎君の考えた数列 a を入力とし、一郎君と次郎君が順次おはじきを取った後の残りのおはじきの個数を出力するプログラムを作成してください。

Input

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

n
a1 a2 ... an

１行目に次郎君の決めた数列の長さ n (1 ≤ n ≤ 25)、２行目に数列の i 番目の要素 ai (1 ≤ ai ≤ 4) が空白区切りで与えられます。入力はすべて整数で与えられます。

データセットの数は 100 を超えません。

Output

データセットごとに、ゲームの各回でのおはじきが取られた直後のおはじきの数（整数）を出力します。

Sample Input

4
3 1 4 2
3
4 3 2
0

Output for the Sample Input

31
28
26
25
21
17
16
14
11
8
6
5
1
0
31
27
26
23
21
19
16
12
11
8
6
4
1
0



Sample input:
4
3 1 4 2
3
4 3 2
0


Sample output:
31
28
26
25
21
17
16
14
11
8
6
5
1
0
31
27
26
23
21
19
16
12
11
8
6
4
1
0

Submission status: Wrong Answer

Solution code:
from itertools import cycle

INITIALIZE = 32

while True:

    input_count = int(input())

    if input_count == 0:
        break

    get_list = [int(item) for item in input().split(" ")]

    ohajiki = INITIALIZE
    output = []

    for get_count in cycle(get_list):

        ohajiki -= (ohajiki - 1) % 5
        output.append(ohajiki)

        if get_count <= ohajiki:
            ohajiki -= get_count
            output.append(ohajiki)
        else:
            output.append(0)
            break

    output = [str(item) for item in output]
    print("\n".join(output))

Submission status: Wrong Answer

Solution code:
while True:
	n = int(raw_input())
	if n == 0: break
	s = 32
	a = map(int, raw_input().split())
	for i in range(n):
		s -= (s-1)%5
		print s
		s -= a[i]
		print s

Submission status: Accepted

Solution code:
while 1:
  i = input()
  if i==0: break
  x = map(int, raw_input().split())
  
  n = 32
  c = 0
  while n:
    n -= (n-1) % 5
    print n
    n -= min(x[c], n)
    c = (c+1) % i
    print n

Submission status: Accepted

Solution code:
while True:
    N = input()
    if N == 0: break
    p = map(int, raw_input().split())
    n = 32
    c = 1
    i = 0
    while n:
        if c % 2 != 0:
            s = (n - 1) % 5
        else:
            if p[i] <= n:
                s = p[i]
            else:
                s = n
            if i == N-1:
                i = 0
            else:
                i += 1
        n -= s
        print n
        c += 1

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    Num = list(map(int,input().split()))
    noko = 32
    jun = 0
    iti = 0
    while noko > 0:
        if jun == 0:
            noko -= (noko - 1) % 5
        else:
            noko -= Num[iti % len(Num)]
            iti += 1
        if noko < 0:
            noko = 0
        print(noko)
        jun = (jun + 1) % 2

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0: break
    a = list(map(int, input().split()))
    i, o = 0, 32
    while o > 0:
        o = o - (o-1)%5
        print(o)
        o = 0 if a[i] > o else o - a[i]
        print(o)
        i = 0 if i == n-1 else i+1

Submission status: Accepted

Solution code:
while 1:
    n=int(input())
    if n==0: break
    a=list(map(int,input().split()))
    i,s=0,32
    while s>0:
        s-=(s-1)%5
        print(s)
        s-=a[i%n]
        i+=1
        print(max(0,s))

Submission status: Accepted

Solution code:
from itertools import cycle
while 1:
    n = input()
    if not n:
        break
    A = map(int, raw_input().split())

    rest = 32
    for a in cycle(A):
        rest -= (rest + 4) % 5
        print rest
        rest -= a
        print max(rest, 0)
        if rest <= 0:
            break

Submission status: Accepted

Solution code:
# Aizu Problem 00164: Ohajiki Game
#
import sys, math, os, bisect

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


while True:
    n = int(input())
    if n == 0:
        break
    A  = [int(_) for _ in input().split()]
    oha = 32
    i = 0
    while oha != 0:
        oha -= (oha - 1) % 5
        print(oha)
        oha = max(oha - A[i % n], 0)
        print(oha)
        i += 1

Submission status: Accepted

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

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


def taro(n):
    return (n - 1) % 5


def main(args):
    while True:
        n = int(input())
        if n == 0:
            break

        jiro = cycle([int(x) for x in input().split(' ')])

        ohajiki = 32
        while ohajiki > 0:
            ohajiki -= taro(ohajiki)
            print(ohajiki)
            ohajiki -= jiro.__next__()
            print(max(0, ohajiki))


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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#import pdb;pdb.set_trace()
import sys
if False:
    fh=open('vol1_0164.txt','r')
else:
    fh=sys.stdin
j=0
a=[]

def ichi(n):
    n -= (n-1) % 5
    return(n)

def ji(n):
    global j    
    if n < a[j]:
        n = 0
    else:
        n -= a[j]
    j = (j+1) % len(a)
    #print('j=',j,' len(a)=',len(a))
    return(n)

while True:
    m = int(fh.readline())
    if m==0:
        break
    a = list(map(int, fh.readline().strip().split()))
    j=0
    n=32
    while True:
        n = ichi(n)
        print(n)
        if n==0:
            #print('ichiro lose')
            break
        n = ji(n)
        print(n)
        if n==0:
            #print('jiro lose')
            break

Submission status: Accepted

Solution code:
from itertools import cycle

INITIALIZE = 32

while True:

    input_count = int(input())

    if input_count == 0:
        break

    get_list = [int(item) for item in input().split(" ")]

    ohajiki = INITIALIZE
    output = []

    for get_count in cycle(get_list):

        ohajiki -= (ohajiki - 1) % 5
        output.append(ohajiki)

        if get_count < ohajiki:
            ohajiki -= get_count
            output.append(ohajiki)
        else:
            output.append(0)
            break

    output = [str(item) for item in output]
    print("\n".join(output))

Submission status: Accepted

Solution code:
from itertools import cycle

INITIALIZE = 32

while True:

    input_count = int(input())

    if input_count == 0:
        break

    get_list = [int(item) for item in input().split(" ")]

    ohajiki = INITIALIZE
    output = []

    for get_count in cycle(get_list):

        ohajiki -= (ohajiki - 1) % 5
        print(ohajiki)

        if get_count < ohajiki:
            ohajiki -= get_count
            print(ohajiki)
        else:
            print(0)
            break

Submission status: Accepted

Solution code:
from itertools import cycle

INITIALIZE = 32

while True:

    input_count = int(input())

    if input_count == 0:
        break

    get_list = [int(item) for item in input().split(" ")]

    ohajiki = INITIALIZE

    for get_count in cycle(get_list):

        ohajiki -= (ohajiki - 1) % 5
        print(ohajiki)

        if get_count < ohajiki:
            ohajiki -= get_count
            print(ohajiki)
        else:
            print(0)
            break

Submission status: Accepted

Solution code:
# AOJ 0164 Ohajiki Game
# Python3 2018.6.18 bal4u

while True:
	n = int(input())
	if n == 0: break
	a = list(map(int, input().split()))
	print(31)
	i, k = 0, 31
	while k > 1:
		k -= a[i]
		print(k)
		k = k - (k-1) % 5;
		print(k)
		i += 1
		if i >= n: i = 0
	print(0)

Submission status: Accepted

Solution code:
while True:
    n=input()
    if n==0:break
    A=map(int,raw_input().split())
    i,num=0,32
    while num>0:
        num-=(num-1)%5
        print num
        num-=A[i%n]
        if num>0:
            print num
        else:
            print 0
        i+=1

Submission status: Accepted

Solution code:
from itertools import cycle
L = range(31,0,-5)
while True:
    n = input()
    if n == 0:
        break
    print "".join("{}\n{}\n".format(a,max(0,a-b))
                  for a,b in zip(L,cycle(map(int,raw_input().split())))),

Submission status: Accepted

Solution code:
while True:
	n = int(raw_input())
	if n == 0: break
	s = 32
	a = map(int, raw_input().split())
	i = 0
	while s > 0:
		s -= (s-1)%5
		print s
		s -= a[i%n] if s > a[i%n] else s
		print s
		i += 1

Submission status: Accepted

Solution code:
import heapq
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop
import copy
from test.support import _MemoryWatchdog

BIG_NUM = 2000000000
HUGE_NUM = 99999999999999999
MOD = 1000000007
EPS = 0.000000001
sys.setrecursionlimit(100000)


NUM = 32

while True:
    N = int(input())
    if N == 0:
        break

    table = list(map(int,input().split()))

    num_card = NUM
    tern = 0

    while num_card > 0:
        sente = (num_card-1)%5
        num_card -= sente
        print("%d"%(num_card))

        gote = table[tern%(len(table))]
        num_card = max(0,num_card-gote)
        print("%d"%(num_card))
        tern += 1

Submission status: Accepted

Solution code:
def ICH(n):
  x=(n-1)%5
  return x
while True:
  n = int(input())
  if n==0:
    break
  J=list(map(int, input().split()))
  i, num=0,31
  print(num)
  while num>1:
    num-=J[i]
    print(num)
    num=num-ICH(num)
    print(num)
    i+=1
    if i>=n:
      i=0
  print(0)

Submission status: Accepted

Solution code:
while True:
  n = int(input())
  if n == 0:
    break
  alst = list(map(int, input().split())) * 16
  turn = 0
  rest = 32
  while True:
    if turn % 2 == 0:
      rest -= (rest - 1) % 5
    else:
      rest -= alst[turn // 2]
      rest = max(rest, 0)
    print(rest)
    turn += 1
    if rest == 0:
      break

Submission status: Accepted

Solution code:
while 1:
    n = int(input())
    if n == 0:
        break
    alist = list(map(int, input().split()))

    ohajiki = 32
    cnt = 0
    while ohajiki != 0:
        taro = (ohajiki - 1) % 5
        ohajiki -= taro
        print(ohajiki)

        jiro = alist[cnt % n]
        ohajiki -= jiro
        if ohajiki < 0:
            ohajiki = 0
        print(ohajiki)
        cnt += 1

Submission status: Accepted

Solution code:
while True:

    n = int(input())
    if n == 0:
        break

    a = [int(i) for i in input().split()]

    stone = 32
    k = 0
    while True:
        stone = stone - (stone-1)%5
        if stone > 0:
            print(stone)
        else:
            print(0)
            break
        stone = stone - a[k]
        if stone > 0:
            print(stone)
        else:
            print(0)
            break
        k = (k + 1) % n