Problem:


メリーゴーランド

遊園地にあるメリーゴーランドはご存じでしょう。大きな円盤の上に馬や馬車などの乗り物が固定されていて、円盤が回転すると同時に乗り物が上下に揺れる、定番の遊具です。ある遊園地のメリーゴーランドは、４人乗りの馬車が２台、２人乗りの車２台、１人乗りの馬が４台、計８台の乗り物が図１のような順序で備えられています。そして、遊園地においでのお客様は、図１に示す乗り場０〜７のどこかで待つようになっています。

この遊園地のメリーゴーランドは、かならず乗り物が乗り場にぴったりと合う位置に停止します。そして、０〜７のそれぞれで待っているお客さまは、目の前にとまった乗り物に乗ることになっています。急いで他の乗り場へ移動してそこから乗るということはできません。効率よく、お客さまにたのしんでいただくためには、メリーゴーランドの停止する位置をうまく調整して、乗れないお客さまをできるだけ少なくするようにしなければなりません。

乗り場０〜７で待っているお客さまの人数を読み込んで、どの位置にどの乗り物が来るように止めれば乗れないお客さまが最も少なくなるかを出力するプログラムを作成してください。

入力

入力は複数のデータセットからなります。各データセットは以下の形式で与えられます。

p0 p1 p2 p3 p4 p5 p6 p7

乗り場 0, 1, ..., 7 で待っているお客様の人数を表す整数 p0, p1,... , p7 ( 0 ≤ pi ≤ 10,000) が空白区切りで１行に与えられます。

出力

メリーゴーランドの乗り物の馬車を 4、車を 2、馬を 1 で表すこととします。乗り場 0, 1, ... , 7 にとめる乗り物を、それぞれ c0, c1,..., c7 とします。データセットごとに、c0, c1,..., c7 を空白区切りで 1 行に出力します。

なお、乗れなくなるお客さまが最小となるとめ方が複数ある場合は、c0c1c2c3c4c5c6c7 を 8 桁の整数 V とみなし、V が最小となるとめ方を選ぶものとします。

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

Sample Input

2 3 1 4 0 1 0 1 
4 2 3 2 2 2 1 1 

Output for the Sample Input

1 4 1 4 1 2 1 2 
4 1 4 1 2 1 2 1



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


Sample output:
1 4 1 4 1 2 1 2
4 1 4 1 2 1 2 1

Submission status: Wrong Answer

Solution code:
mgr = [4, 1, 4, 1, 2, 1, 2, 1]

while True:
    try:
        p = list(map(int, input().split(' ')))
        v = []
        for i in range(8):
            q = mgr[i:] + mgr[:i]
            sum_v = sum(p) - sum(p[j]-q[j] if p[j]-q[j] >= 0 else 0 for j in range(8))
            v.append(sum_v)
        if v.count(max(v)) > 1:
            mv = max(v)
            ans = []
            for i in range(8):
                if v[i] == mv:
                    ans.append(int("".join(map(str, mgr[i:] + mgr[:i]))))
            print(" ".join(list(str(min(ans)))))
        else:
            i = v.index(max(v))
            print(mgr[i:] + mgr[:i])
    except:
        break

Submission status: Wrong Answer

Solution code:
v=[4,1,4,1,2,1,2,1]
while True:
    try:
        x=raw_input()
        x=x[0:len(x)-1]
        p=map(int,x.split(" "))
        min=[]
        min_value=0
        
        for i in range(8):
            count=0
            for j in range(8):
                if p[(j)%8]-v[(j+i)%8]>=0:
                    count+=p[(j)%8]-v[(j+i)%8]
            if len(min)==0:
                min_value=count
                min.append(i)
            else:
                if count<min_value:
                    min_value=count
                    min=[]
                    min.append(i)
                elif count==min_value:
                    min.append(i)
        min1_value=41412122
        for i in range(len(min)):
            value=""
            for j in range(8):
                value+=str(v[(j+min[i])%8])
            if min1_value>int(value):
                min1_value=int(value)
                min1=min[i]
        solve=[]
        for i in range(8):
            solve.append(str(v[(i+min1)%8]))
        print " ".join(solve)
    except:
        break

Submission status: Wrong Answer

Solution code:
while 1:
    try:
        en=map(int,raw_input().split())
        p=[]
        for i in xrange(len(en)):
            p.append([en[i],i])
        p.sort()
        p.reverse()
        b1=4;b2=2;b4=2
        ans=[""]*(len(p))
        for i in xrange(len(p)):
            if b4>0:
                ans[p[i][1]]="4"
                b4-=1
            elif b2>0:
                ans[p[i][1]]="2"
                b2-=1
            else:
                ans[p[i][1]]="1"
                b1-=1
        print(" ".join(ans))
    except:
        break

Submission status: Wrong Answer

Solution code:
c = [4,1,4,1,2,1,2,1,4,1,4,1,2,1,2,1]
while True:
    try:
        p = list(map(int, input().split()))
        p.append(p)
        c_tmp = 41412121
        s_min = 100000
        for i in range(8):
            s = 0
            for j in range(8):
                p_rest = p[j] - c[i + j] if p[j] - c[i + j] > 0 else 0
                s += p_rest
            if s == s_min and c_tmp > int("".join(map(str, c[i:i + 8]))):
                c_tmp = int("".join(map(str, c[i:i + 8])))
                ans = i
            elif s < s_min:
                s_min = s
                ans = i
        print(*c[ans:ans + 8])
    except:
        break

Submission status: Wrong Answer

Solution code:
c = [4,1,4,1,2,1,2,1,4,1,4,1,2,1,2,1]
while True:
    try:
        p = list(map(int, input().split()))
        p.append(p)
        c_tmp = 41412121
        s_min = 1000000
        for i in range(8):
            s = 0
            for j in range(8):
                p_rest = p[j] - c[i + j] if p[j] - c[i + j] > 0 else 0
                s += p_rest
            if s == s_min and c_tmp > int("".join(map(str, c[i:i + 8]))):
                c_tmp = int("".join(map(str, c[i:i + 8])))
                ans = i
            elif s < s_min:
                s_min = s
                ans = i
        print(*c[ans:ans + 8])
    except:
        break

Submission status: Wrong Answer

Solution code:
c = [4,1,4,1,2,1,2,1,4,1,4,1,2,1,2,1]
while True:
    try:
        p = list(map(int, input().split()))
        c_tmp = 41412121
        s_min = 100000
        for i in range(8):
            s = 0
            for j in range(8):
                p_rest = p[j] - c[i + j] if p[j] - c[i + j] > 0 else 0
                s += p_rest
            if s == s_min and c_tmp > int("".join(map(str, c[i:i + 8]))):
                c_tmp = int("".join(map(str, c[i:i + 8])))
                ans = i
            elif s < s_min:
                s_min = s
                ans = i
        print(*c[ans:ans + 8])
    except:
        break

Submission status: Wrong Answer

Solution code:
import sys
for line in sys.stdin:
    p = list(map(int, line.split()))
    car = [4, 1, 4, 1, 2, 1, 2, 1, 4, 1, 4, 1, 2, 1, 2, 1, 4, 1, 4, 1, 2, 1, 2, 1]

    ans_d = 10**9
    ans = -1
    ans_s = ''
    for i in range(8):
        now_d = 0
        now_s = ''
        for j in range(8):
            now_d += abs(p[j] - car[i+j])
        if ans_d > now_d:
            ans_d = now_d
            ans = car[i:i+8]
        elif ans_d == now_d:
            for k in ans:
                ans_s += str(j)
            for k in car[i+j:i+j+8]:
                now_s += str(j)
            if ans_s > now_s:
                ans_d = now_d
                ans = car[i:i+8]
    t = ''
    for i in ans:
        t += str(i) + ' '
    print(t[:-1])

Submission status: Wrong Answer

Solution code:
import sys
for line in sys.stdin:
    p = list(map(int, line.split()))
    car = [4, 1, 4, 1, 2, 1, 2, 1, 4, 1, 4, 1, 2, 1, 2, 1, 4, 1, 4, 1, 2, 1, 2, 1]

    ans_d = 10**9
    ans = -1
    ans_s = ''
    for i in range(8):
        now_d = 0
        now_s = ''
        for j in range(8):
            now_d += abs(p[j] - car[i+j])
        if ans_d > now_d:
            ans_d = now_d
            ans = car[i:i+8]
        elif ans_d == now_d:
            for k in ans:
                ans_s += str(j)
            for k in car[i:i+8]:
                now_s += str(j)
            if ans_s > now_s:
                ans_d = now_d
                ans = car[i:i+8]
    t = ''
    for i in ans:
        t += str(i) + ' '
    print(t[:-1])

Submission status: Wrong Answer

Solution code:
import sys
for line in sys.stdin:
    p = list(map(int, line.split()))
    car = [4, 1, 4, 1, 2, 1, 2, 1, 4, 1, 4, 1, 2, 1, 2, 1]

    ans_d = 10**9
    ans = -1
    ans_s = ''
    for i in range(8):
        now_d = 0
        now_s = ''
        for j in range(8):
            now_d += abs(p[j] - car[i+j])
        if ans_d > now_d:
            ans_d = now_d
            ans = car[i:i+8]
        elif ans_d == now_d:
            for k in ans:
                ans_s += str(k)
            for k in car[i:i+8]:
                now_s += str(k)
            if ans_s > now_s:
                ans_d = now_d
                ans = car[i:i+8]
    t = ''
    for i in ans:
        t += str(i) + ' '
    print(t[:-1])

Submission status: Wrong Answer

Solution code:
import sys
p=['41412121','41212141','21414121','21214141','14141212','14121214','12141412','12121414']
for e in sys.stdin:
 d={}
 for f in p:d[sum(int(s)-int(t)for s,t in zip(e.split(),f)if s>t)]=f
 print(*list(d[min(d)]))

Submission status: Wrong Answer

Solution code:
while True:
  try:
    plst = list(map(int, input().split()))
    
    horse = [4, 1, 4, 1, 2, 1, 2, 1]
    max_num = 0
    max_horse = horse[:]
    
    for i in range(8):
      #乗車可能人数
      num = sum([min(horse[j], plst[j]) for j in range(8)])
      
      #人数が最大値と等しく、並びの数値が小さければ更新
      if num == max_num and max_horse > horse:
        max_hose = horse[:]
      
      #人数が最大値より大きければ更新
      elif num > max_num:
        max_num = num
        max_horse = horse[:]
      
      #馬を回転
      horse.append(horse.pop(0))

    print(" ".join(map(str, max_horse)))

  except EOFError:
    break

Submission status: Wrong Answer

Solution code:
while True:
  try:
    plst = list(map(int, input().split()))
    
    horse = [4, 1, 4, 1, 2, 1, 2, 1]
    max_num = -1
    max_horse = horse[:]
    
    for i in range(8):
      #乗車可能人数
      num = sum([min(horse[j], plst[j]) for j in range(8)])
      
      #人数が最大値と等しく、並びの数値が小さければ更新
      if num == max_num and max_horse > horse:
        max_hose = horse[:]
      
      #人数が最大値より大きければ更新
      elif num > max_num:
        max_num = num
        max_horse = horse[:]
      
      #馬を回転
      horse.append(horse.pop(0))

    print(" ".join(map(str, max_horse)))

  except EOFError:
    break

Submission status: Wrong Answer

Solution code:
def low(lst1, lst2):
  for v1, v2 in zip(lst1, lst2):
    if v1 > v2:
      return False
    if v1 < v2:
      return True

while True:
  try:
    plst = list(map(int, input().split()))
    
    horse = [4, 1, 4, 1, 2, 1, 2, 1]
    max_num = -1
    max_horse = horse[:]
    
    for i in range(8):
      #乗車可能人数
      num = sum([min(horse[j], plst[j]) for j in range(8)])
      
      #人数が最大値と等しく、並びの数値が小さければ更新
      if num == max_num and low(max_horse, horse):
        max_hose = horse[:]
      
      #人数が最大値より大きければ更新
      elif num > max_num:
        max_num = num
        max_horse = horse[:]
      
      #馬を回転
      horse.append(horse.pop(0))

    print(" ".join(map(str, max_horse)))

  except EOFError:
    break

Submission status: Wrong Answer

Solution code:
def low(lst1, lst2):
  for v1, v2 in zip(lst1, lst2):
    if v1 > v2:
      return False
    if v1 < v2:
      return True

while True:
  try:
    plst = list(map(int, input().split()))
    
    horse = [4, 1, 4, 1, 2, 1, 2, 1]
    min_num = 100000
    min_horse = horse[:]
    
    for i in range(8):
      #乗車不能人数
      num = sum([max(0, plst[j] - horse[j]) for j in range(8)])
      
      #人数が最小値と等しく、並びの数値が小さければ更新
      if num == min_num and low(min_horse, horse):
        max_hose = horse[:]
      
      #人数が最小値より小さければ更新
      elif num < min_num:
        min_num = num
        min_horse = horse[:]
      
      #馬を回転
      horse.append(horse.pop(0))

    print(" ".join(map(str, min_horse)))

  except EOFError:
    break

Submission status: Wrong Answer

Solution code:
def low(lst1, lst2):
  for v1, v2 in zip(lst1, lst2):
    if v1 < v2:
      return False
    if v1 > v2:
      return True

while True:
  try:
    plst = list(map(int, input().split()))
    
    horse = [4, 1, 4, 1, 2, 1, 2, 1]
    min_num = 100000
    min_horse = horse[:]
    
    for _ in range(8):
      #乗車不能人数
      num = sum([max(0, plst[j] - horse[j]) for j in range(8)])
      
      #人数が最小値と等しく、並びの数値が小さければ更新
      if num == min_num and low(min_horse, horse):
        max_hose = horse[:]
      
      #人数が最小値より小さければ更新
      elif num < min_num:
        min_num = num
        min_horse = horse[:]
      
      #馬を回転
      horse.append(horse.pop(0))

    print(" ".join(map(str, min_horse)))

  except EOFError:
    break

Submission status: Wrong Answer

Solution code:
def comp(lst1, lst2):
  for v1, v2 in zip(lst1, lst2):
    if v1 < v2:
      return False
    if v1 > v2:
      return True

  return False

while True:
  try:
    plst = list(map(int, input().split()))
    
    horse = [4, 1, 4, 1, 2, 1, 2, 1]
    min_num = 100000
    min_horse = horse[:]
    
    for _ in range(8):
      #乗車不能人数
      num = sum([max(0, plst[j] - horse[j]) for j in range(8)])
      
      #人数が最小値と等しく、並びの数値が小さければ更新
      if num == min_num and min_horse > horse:
        min_hose = horse[:]
      
      #人数が最小値より小さければ更新
      elif num < min_num:
        min_num = num
        min_horse = horse[:]
      
      #馬を回転
      horse.append(horse.pop(0))

    print(" ".join(map(str, min_horse)))

  except EOFError:
    break

Submission status: Wrong Answer

Solution code:
import sys
t=[1,4,1,4,1,2,1,2]
for i in sys.stdin:
    l=map(int,i[:-1].split())
    q=[sum([j if j<i else i for (i,j) in zip(t[i:]+t[:i],l)]) for i in range(8)]
    idx=q.index(max(q))
    print(" ".join(map(str,t[idx:]+t[:idx])))

Submission status: Wrong Answer

Solution code:
import sys
t=[1,4,1,4,1,2,1,2]
for i in sys.stdin:
    l=list(map(int,i[:-1].split()))
    q=[sum([j if j<i else i for (i,j) in zip(t[i:]+t[:i],l)]) for i in range(8)]
    idx=q.index(max(q))
    print(" ".join(map(str,t[idx:]+t[:idx])))

Submission status: Wrong Answer

Solution code:
merry=[4,1,4,1,2,1,2,1]
while True:
    try:
        gst=map(int,raw_input().split())
    except EOFError:
        break
    m=10000
    ans="9"*8
    for i in range(len(merry)):
        temp=merry[i:]+merry[:i]
        s=sum([max(gst[i]-temp[i],0) for i in range(len(merry))])
        if s<m or (s==m and ans>"".join(map(str,temp))):
            m=s
            ans="".join(map(str,temp))
    print " ".join(ans)

Submission status: Wrong Answer

Solution code:
merry=[4,1,4,1,2,1,2,1]
while True:
    try:
        gst=map(int,raw_input().split())
    except EOFError:
        break
    m,ans=10000,"9"*8
    for i in range(len(merry)):
        temp=merry[i:]+merry[:i]
        s=sum([max(gst[i]-temp[i],0) for i in range(len(merry))])
        if s<m or (s==m and int(ans)>int("".join(map(str,temp)))):
            m=s
            ans="".join(map(str,temp))
    print " ".join(ans)

Submission status: Wrong Answer

Solution code:
merry=[4,1,4,1,2,1,2,1]
while True:
    try:
        gst=map(int,raw_input().split())
    except EOFError:
        break
    m,ans=10000,"9"*8
    for i in range(len(merry)):
        temp=merry[i:]+merry[:i]
        s=sum([gst[i]-temp[i] for i in range(len(merry)) if gst[i]>temp[i]])
        if s<m or (s==m and int(ans)>int("".join(map(str,temp)))):
            m=s
            ans="".join(map(str,temp))
    print " ".join(ans)

Submission status: Wrong Answer

Solution code:
merry=[4,1,4,1,2,1,2,1]
while True:
    try:
        gst=map(int,raw_input().split())
    except EOFError:
        break
    m,ans=10000,"9"*10
    for i in range(len(merry)):
        temp=merry[i:]+merry[:i]
        s=sum([gst[i]-temp[i] for i in range(len(merry)) if gst[i]>temp[i]])
        if s<m or (s==m and int(ans)>int("".join(map(str,temp)))):
            m=s
            ans="".join(map(str,temp))
    print " ".join(ans)

Submission status: Wrong Answer

Solution code:
merry=[4,1,2,1,2,1,4,1]
while True:
    try:
        gst=map(int,raw_input().split())
    except EOFError:
        break
    m,ans=10000,"9"*10
    for i in range(len(merry)):
        temp=merry[i:]+merry[:i]
        s=sum([gst[i]-temp[i] for i in range(len(merry)) if gst[i]>temp[i]])
        if s<m or (s==m and int(ans)>int("".join(map(str,temp)))):
            m=s
            ans="".join(map(str,temp))
    print " ".join(ans)

Submission status: Wrong Answer

Solution code:
merry=[4,1,4,1,2,1,2,1]
while True:
    try:
        gst=map(int,raw_input().split())
    except EOFError:
        break
    m,ans=10000,"9"*10
    for i in range(len(merry)):
        temp=merry[i:]+merry[:i]
        s=sum(map(lambda a,b:a-b if a>b else 0,gst,temp))
        if s<m or (s==m and int(ans)>int("".join(map(str,temp)))):
            m=s
            ans="".join(map(str,temp))
    print " ".join(ans)

Submission status: Wrong Answer

Solution code:
import sys
##device                                                                        
d = [1,4,1,4,1,2,1,2]

"""                                                                             
dd = []                                                                         
for i in range(8):                                                              
    dd = d[i:] + d[-8:i-8]                                                      
    print dd                                                                    
"""
min = 10000
r = -1

for i in range(2):
    g = map(int,raw_input().split())
    for j in range(8):
        dd = d[j:] + d[-8:j-8]
        t = []
        for k in range(8):
            if dd[k] - g[k] > 0:
                t.append(dd[k] - g[k])
            else:
                t.append(g[k] - dd[k])
        if sum(t) < min:
            min = sum(t)
            r = j
    re = d[r:] + d[-8:r-8]
    print re

Submission status: Wrong Answer

Solution code:
import sys
##device                                                                        
d = [1,4,1,4,1,2,1,2]

"""                                                                             
dd = []                                                                         
for i in range(8):                                                              
    dd = d[i:] + d[-8:i-8]                                                      
    print dd                                                                    
"""
min = 10000
r = -1

for i in range(2):
    g = map(int,raw_input().split())
    for j in range(8):
        dd = d[j:] + d[-8:j-8]
        t = []
        for k in range(8):
            if dd[k] - g[k] > 0:
                t.append(dd[k] - g[k])
            else:
                t.append(0)
        if sum(t) < min:
            min = sum(t)
            r = j
    re = d[r:] + d[-8:r-8]
    print re

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (division, absolute_import, print_function,
                        unicode_literals)
from sys import stdin

NUM_RIDE = 8


def enum_ride(n, ride=(4, 1, 4, 1, 2, 1, 2, 1)):
    for i in range(n, n+NUM_RIDE):
        yield ride[i % NUM_RIDE]

for line in stdin:
    gast = [int(s) for s in line.split()]
    minimum = sum(gast)
    for i in range(NUM_RIDE):
        gen = (g-n if g-n > 0 else 0 for g, n in zip(gast, enum_ride(i)))
        remain = sum(gen)
        if minimum > remain:
            minimum = remain
            idx = i
    print(*enum_ride(idx))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (division, absolute_import, print_function,
                        unicode_literals)
from sys import stdin

NUM_RIDE = 8


def enum_ride(n, ride=(1, 2, 1, 2, 1, 4, 1, 4)):
    for i in range(n, n+NUM_RIDE):
        yield ride[i % NUM_RIDE]

for line in stdin:
    gast = [int(s) for s in line.split()]
    minimum = sum(gast)
    for i in range(NUM_RIDE):
        gen = (g-n if g-n > 0 else 0 for g, n in zip(gast, enum_ride(i)))
        remain = sum(gen)
        if minimum > remain:
            minimum = remain
            idx = i
    print(*enum_ride(idx))

Submission status: Wrong Answer

Solution code:
cart = [4,1,4,1,2,1,2,1]
while True:
	try:
		que = map(int, raw_input().split())
		mx,mxcart = 0, "99999999"
		for sp in range(8):
			sm = 0
			for num in range(8):
				if cart[(sp+num)%8] <= que[num]:
					sm += cart[(sp+num)%8]
				else:
					sm += que[num]
			if sm >= mx:
				mx = sm
				acart = "".join(map(str, cart[sp:]+cart[:sp]))
				if int(mxcart) > int(acart):
					mxcart = acart
		print " ".join(map(str, mxcart))
	except:
		break

Submission status: Wrong Answer

Solution code:
c = [4,1,4,1,2,1,2,1]
while True:
	try:
		que = map(int, raw_input().split())
		mx,mxc = 0, "99999999"
		for sp in range(8):
			sm = 0
			for num in range(8):
				sm += c[(sp+num)%8] if c[(sp+num)%8] <= que[num] else que[num]
			if sm > mx:
				mx = sm
				mxc = "".join(map(str, c[sp:]+c[:sp]))
			elif sm == mx:
				ac = "".join(map(str, c[sp:]+c[:sp]))
				if int(mxc) > int(ac):
					mxc = ac
		print " ".join(map(str, mxcart))
	except:
		break

Submission status: Wrong Answer

Solution code:
import sys
patterns = [[1, 2, 1, 2, 1, 4, 1, 4],
 [1, 2, 1, 4, 1, 4, 1, 2],
 [1, 4, 1, 4, 1, 2, 1, 2],
 [2, 1, 2, 1, 4, 1, 4, 1],
 [2, 1, 4, 1, 4, 1, 2, 1],
 [4, 1, 2, 1, 2, 1, 4, 1],
 [4, 1, 4, 1, 2, 1, 2, 1],
]

for line in sys.stdin:
	try:
		p = [int(x) for x in line.split()]
		m = sum(p)
		pt = None
		for ptn in patterns:
			rem = sum(max(0, v - x) for v, x in zip(ptn, p))
			if rem < m:
				m = rem
				pt = ptn
		print " ".join([str(x) for x in pt])
	except:
		break

Submission status: Wrong Answer

Solution code:
import sys
patterns = [[1, 2, 1, 2, 1, 4, 1, 4],
			[1, 2, 1, 4, 1, 4, 1, 2],
			[1, 4, 1, 2, 1, 2, 1, 4],
			[1, 4, 1, 4, 1, 2, 1, 2],
			[2, 1, 2, 1, 4, 1, 4, 1],
			[2, 1, 4, 1, 4, 1, 2, 1],
			[4, 1, 2, 1, 2, 1, 4, 1],
			[4, 1, 4, 1, 2, 1, 2, 1],
]

for line in sys.stdin:
	try:
		p = [int(x) for x in line.split()]
		m = sum(p)
		pt = None
		for ptn in patterns:
			rem = sum(max(0, v - x) for v, x in zip(ptn, p))
			if rem < m:
				m = rem
				pt = ptn
		print " ".join([str(x) for x in pt])
	except:
		break

Submission status: Wrong Answer

Solution code:
import sys
patterns = [
	[1, 2, 1, 2, 1, 4, 1, 4],
	[1, 2, 1, 4, 1, 4, 1, 2],
	[1, 4, 1, 2, 1, 2, 1, 4],
	[1, 4, 1, 4, 1, 2, 1, 2],
	[2, 1, 2, 1, 4, 1, 4, 1],
	[2, 1, 4, 1, 4, 1, 2, 1],
	[4, 1, 2, 1, 2, 1, 4, 1],
	[4, 1, 4, 1, 2, 1, 2, 1],
]

for line in sys.stdin:
	try:
		p = [int(x) for x in line.split()]
		m = sum(p)
		for ptn in patterns:
			rem = sum(max(0, v - x) for v, x in zip(ptn, p))
			if rem < m:
				m = rem
				pt = ptn
		print " ".join([str(x) for x in pt])
	except:
		break

Submission status: Wrong Answer

Solution code:
import sys
patterns = [
	[1, 2, 1, 2, 1, 4, 1, 4],
	[1, 2, 1, 4, 1, 4, 1, 2],
	[1, 4, 1, 2, 1, 2, 1, 4],
	[1, 4, 1, 4, 1, 2, 1, 2],
	[2, 1, 2, 1, 4, 1, 4, 1],
	[2, 1, 4, 1, 4, 1, 2, 1],
	[4, 1, 2, 1, 2, 1, 4, 1],
	[4, 1, 4, 1, 2, 1, 2, 1],
]

while True:
	try:
		p = [int(x) for x in raw_input().split()]
		m = sum(p)
		for ptn in patterns:
			rem = sum(max(0, v - x) for v, x in zip(ptn, p))
			if rem < m:
				m = rem
				pt = ptn
		print " ".join([str(x) for x in pt])
	except:
		break

Submission status: Accepted

Solution code:
import sys
m = [4,1,4,1,2,1,2,1]

for l in sys.stdin:
    l = list(map(int, l.rstrip().split()))
    a = []
    for i in range(0, 8):
        _m = [m[j%8] for j in range(i, i+8)]
        n = 0
        for n1, n2 in zip(l, _m):
            n += max(0, n1-n2)
        a.append((n, "".join(map(str, _m))))
    print(" ".join(sorted(a)[0][1]))

Submission status: Accepted

Solution code:
G = [4, 1, 4, 1, 2, 1, 2, 1]
N = 8
ans = []
for line in open(0).readlines():
    *P, = map(int, line.split())
    Q = None; c = -1
    for i in range(N):
        R = G[i:] + G[:i]
        d = 0
        for j in range(N):
            d += min(R[j], P[j])
        if c < d:
            c = d
            Q = R
        elif c == d and R < Q:
            Q = R
    print(*Q)

Submission status: Accepted

Solution code:
mgr = [4, 1, 4, 1, 2, 1, 2, 1]

while True:
    try:
        p = list(map(int, input().split(' ')))
        v = []
        for i in range(8):
            q = mgr[i:] + mgr[:i]
            sum_v = sum(p) - sum(p[j]-q[j] if p[j]-q[j] >= 0 else 0 for j in range(8))
            v.append(sum_v)
        if v.count(max(v)) > 1:
            mv = max(v)
            ans = []
            for i in range(8):
                if v[i] == mv:
                    ans.append(int("".join(map(str, mgr[i:] + mgr[:i]))))
            print(" ".join(list(str(min(ans)))))
        else:
            i = v.index(max(v))
            print(" ".join(map(str, mgr[i:] + mgr[:i])))
    except:
        break

Submission status: Accepted

Solution code:
import sys

for s in sys.stdin:
    row = [1,2,1,2,1,4,1,4]
    d = map(int,s.split())
    mini = 10000000
    n = len(row)
    m_row = [4]*n
    for i in xrange(n):
        su = [d[j]-row[(i+j)%n] if d[j] > row[(i+j)%n] else 0 for j in xrange(n)  ]
        su = sum(su)
        if mini == su:
            m_row = min(m_row,[ row[(i+j)%n] for j in xrange(n) ])
        elif mini > su:
            mini = su
            m_row =[ row[(i+j)%n] for j in xrange(n) ]
    for m in m_row:
        print m,
    print

Submission status: Accepted

Solution code:
import sys
f = sys.stdin

def get_v(c):
    v = 0
    for ci in c:
        v *= 10
        v += ci
    return v

c = (1,4,1,4,1,2,1,2)

for line in f:
    p = list(map(int, line.split()))
    min_waiting = None
    min_c = None
    for i in range(len(c)):
        ci = c[i:] +c[:i]
        waiting = sum(max(0, p[i] - ci[i]) for i in range(len(c)))
        if min_waiting == None or min_waiting > waiting:
            min_waiting = waiting
            min_c = ci
        elif min_waiting == waiting and get_v(min_c) > get_v(ci):
            min_c = ci
    print(*min_c)

Submission status: Accepted

Solution code:
v=[4,1,4,1,2,1,2,1]
while True:
    try:
        p=map(int,raw_input().split(" "))
        min=[]
        min_value=0
        
        for i in range(8):
            count=0
            for j in range(8):
                if p[(j)%8]-v[(j+i)%8]>=0:
                    count+=p[(j)%8]-v[(j+i)%8]
            if len(min)==0:
                min_value=count
                min.append(i)
            else:
                if count<min_value:
                    min_value=count
                    min=[]
                    min.append(i)
                elif count==min_value:
                    min.append(i)
        min1_value=41412122
        for i in range(len(min)):
            value=""
            for j in range(8):
                value+=str(v[(j+min[i])%8])
            if min1_value>int(value):
                min1_value=int(value)
                min1=min[i]
        solve=[]
        for i in range(8):
            solve.append(str(v[(i+min1)%8]))
        print " ".join(solve)
    except:
        break

Submission status: Accepted

Solution code:
m=[4,1,4,1,2,1,2,1]
l=[]
while 1:
    try:
        c=map(int,raw_input().split())
        cnt=[0]*8
        for i in range(8):
            chk=[m[(i+x)%8] for x in range(8)]
            l.append(chk)
            for j in range(8):
                if c[j]>chk[j]:
                    cnt[i]+=c[j]-chk[j]
        ans=[]
        for i in range(8):
            if cnt[i]==min(cnt):
                ans.append(int(''.join(map(str,l[i]))))
        ans.sort()
        for i in str(ans[0]):
            print i,
        print
    except:
        break

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
import os
import math

for s in sys.stdin:
    P = list(map(int, s.split()))

    C = [1, 4, 1, 4, 1, 2, 1, 2]
    enter_num_max_list = []
    enter_num_max = 0

    for i in range(8):
        # rotate
        D = C[i:] + C[:i]

        enter_num = 0
        for j in range(8):
            p = P[j]
            capacity = D[j]
            if capacity >= p:
                enter_num += p
            else:
                enter_num += capacity

        if enter_num > enter_num_max:
            enter_num_max = enter_num
            enter_num_max_list = [D]
        elif enter_num == enter_num_max:
            enter_num_max_list.append(D)

    enter_num_max_list.sort()
    print(*enter_num_max_list[0])

Submission status: Accepted

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

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


def calc_remaining(data, carriges):
    # ??????????????£????????°?????????
    total = 0
    for d, c in zip(data, carriges):
        if d > c:
            total += d - c
    return total


def solve(data):
    carriges = deque([1, 4, 1, 4, 1, 2, 1, 2])

    min_remaining = float('inf')
    min_carrige_value = float('inf')

    for i in range(8):
        remaining = calc_remaining(data, carriges)
        if remaining < min_remaining:
            min_remaining = remaining
            min_carrige_value = int(''.join(map(str, carriges)))
        elif remaining == min_remaining:
            carrige_value = int(''.join(map(str, carriges)))
            if carrige_value < min_carrige_value:
                min_carrige_value = carrige_value
        carriges.rotate(1)

    return min_remaining, min_carrige_value


def main(args):
    for line in sys.stdin:
        # data = [2, 3, 1, 4, 0, 1, 0, 1]
        # data = [4, 2, 3, 2, 2, 2, 1, 1]
        data = [int(x) for x in line.split()]
        _, result = solve(data)
        print(' '.join(map(str, list(str(result)))))



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

Submission status: Accepted

Solution code:
c = [4,1,4,1,2,1,2,1,4,1,4,1,2,1,2,1]
while True:
    try:
        p = list(map(int, input().split()))
        c_tmp = 41412121
        s_min = 100000
        for i in range(8):
            s = 0
            for j in range(8):
                p_rest = p[j] - c[i + j] if p[j] - c[i + j] > 0 else 0
                s += p_rest
            if s == s_min and c_tmp > int("".join(map(str, c[i:i + 8]))):
                c_tmp = int("".join(map(str, c[i:i + 8])))
                ans = i
            elif s < s_min:
                s_min = s
                c_tmp = int("".join(map(str, c[i:i + 8])))
                ans = i
        
        print(*c[ans:ans + 8])
    except:
        break

Submission status: Accepted

Solution code:
def get_input():
    while True:
        try:
            yield ''.join(input())
        except EOFError:
            break

N = list(get_input())


for l in range(len(N)):
    c = [4, 1, 4, 1, 2, 1, 2, 1]
    p = [int(i) for i in N[l].split()]
    for i in range(len(p)):
        if p[i] > 9:
            p[i] = 9

    V = 99999999
    score = 99999
    for d in range(8):
        s = 0
        for i in range(8):
            s += max(p[i] - c[(i+d)%8], 0)

        v = 0
        for i in range(8):
            v *= 10
            v += c[(i+d)%8]

        if s < score:
            score = s
            V = v
        elif s == score:
            if v < V:
                V = v

    ans = str(V)
    for i in range(7):
        print(ans[i], end=" ")
    print(ans[7])

Submission status: Accepted

Solution code:
import sys
for line in sys.stdin:
    p = list(map(int, line.split()))
    car = [4, 1, 4, 1, 2, 1, 2, 1, 4, 1, 4, 1, 2, 1, 2, 1]
    ans_d = 10**9
    ans = -1
    for i in range(8):
        now_d = 0
        for j in range(8):
            now_d += abs(p[j] - car[i+j])
        if ans_d > now_d:
            ans_d = now_d
            ans = car[i:i+8]
        elif ans_d == now_d:
            ans_s, now_s = '', ''
            for k in ans:
                ans_s += str(k)
            for k in car[i:i+8]:
                now_s += str(k)
            if int(ans_s) > int(now_s):
                ans_d = now_d
                ans = car[i:i+8]
    t = ''
    for i in ans:
        t += str(i) + ' '
    print(t[:-1])

Submission status: Accepted

Solution code:
import sys
p=['41412121','41212141','21414121','21214141','14141212','14121214','12141412','12121414']
g=lambda x:list(map(int,x))
for e in sys.stdin:
 d={}
 for f in p:d[sum(s-t for s,t in zip(g(e.split()),g(f))if s>t)]=f
 print(*list(d[min(d)]))

Submission status: Accepted

Solution code:
import sys
p=['41412121','41212141','21414121','21214141','14141212','14121214','12141412','12121414']
for e in sys.stdin:
 d={}
 for f in p:d[sum(s-t for s,t in zip(map(int,e.split()),map(int,f))if s>t)]=f
 print(*list(d[min(d)]))