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: Accepted

Solution code:
import sys
p=['41412121','41212141','21414121','21214141','14141212','14121214','12141412','12121414']
g=lambda x: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:
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 comp(min_horse, horse):
        min_horse = 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: Accepted

Solution code:
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_horse = 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: Accepted

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 _ in range(8):
      #乗車可能人数
      num = sum([min(plst[j], horse[j]) for j in range(8)])
      
      #人数が最大値と等しく、並びの数値が小さければ更新
      if num == max_num and max_horse > horse:
        max_horse = 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: Accepted

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={}
    for i in range(8):
        e=int("".join(map(str,t[i:]+t[:i])))
        s=sum([j if j<i else i for (i,j) in zip(t[i:]+t[:i],l)])
        if s not in q:
            q[s]=[e]
        else:
            q[s].extend([e])
    print(" ".join([i for i in str(sorted(q[max(q.keys())])[0])]))

Submission status: Accepted

Solution code:
# AOJ 0082 Flying Jenny
# Python3 2018.6.21 bal4u


h = [4,1,4,1,2,1,2,1,4,1,4,1,2,1,2,1]
d = [41412121, 14121214, 41212141, 12121414, 21214141, 12141412, 21414121, 14141212]

while 1:
	try: p = list(map(int, input().split()))
	except: break
	max, id = 0, 3
	for i in range(8):
		c = 0
		for j in range(8):
			if p[j] >= h[i+j]: c += h[i+j]
			else: c += p[j]
		if max < c: max, id = c, i
		elif max == c and d[id] > d[i]: id = i
	print(*list(str(d[id])))

Submission status: Accepted

Solution code:
import sys
raw=[4,1,2,1,2,1,4,1]
for line in sys.stdin.readlines():
    p=map(int,line.strip().split())
    ma,ans=1<<30,str(1<<30)
    for i in xrange(8):
        t=raw[i:]+raw[:i]
        temp=sum(map(lambda a,b:a-b if a>b else 0,p,t))
        if (temp<ma) or (temp==ma and int(ans)>int("".join(map(str,t)))):
            ma=temp
            ans="".join(map(str,t))
    print " ".join(ans)

Submission status: Accepted

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

Submission status: Accepted

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

NUM = 8
VALUE = (12121414, 21214141, 12141412, 21414121, 14141212, 41412121,
         14121214, 41212141)
RIDE = ((1, 2, 1, 2, 1, 4, 1, 4), (2, 1, 2, 1, 4, 1, 4, 1),
        (1, 2, 1, 4, 1, 4, 1, 2), (2, 1, 4, 1, 4, 1, 2, 1),
        (1, 4, 1, 4, 1, 2, 1, 2), (4, 1, 4, 1, 2, 1, 2, 1),
        (1, 4, 1, 2, 1, 2, 1, 4), (4, 1, 2, 1, 2, 1, 4, 1))

for line in stdin:
    gast = [int(s) for s in line.split()]
    remain = sum(gast)
    value = max(VALUE)
    for i in range(NUM):
        r = sum(g-n if g-n > 0 else 0 for g, n in zip(gast, RIDE[i]))
        if remain > r or (remain == r and value > VALUE[i]):
            remain = r
            index = i
            value = VALUE[i]
    print(*RIDE[index])

Submission status: Accepted

Solution code:
import sys

d = [[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 s in sys.stdin:
    c = map(int, s.split())
    r = 0
    min = 0x7fffffff
    for i in range(8):
        rr = 0
        for j in range(8):
            if c[j] > d[i][j]:
                rr += c[j] - d[i][j]
        if rr < min:
            min = rr
            r = i
    print ' '.join(map(str, d[r]))
    a = 1

Submission status: Accepted

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
				mxcart = "".join(map(str, cart[sp:]+cart[:sp]))
			elif sm == mx:
				acart = "".join(map(str, cart[sp:]+cart[:sp]))
				if int(mxcart) > int(acart):
					mxcart = acart
		print " ".join(map(str, mxcart))
	except:
		break

Submission status: Accepted

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, mxc))
	except:
		break

Submission status: Accepted

Solution code:
R=range(8)
A=[4,1,4,1,2,1,2,1]
A+=A
B="41412121"
B=map(int,[B[i:]+B[:i] for i in R])

import sys
for s in sys.stdin:
  x=map(int,s[:-1].split())
  m=sum(x)
  for i in R:
    s=sum([max(0,x[j]-A[i+j]) for j in R])
    if s<m: m,p=s,i
    elif m==s and B[i]<B[p]: p=i
  for i in R: print A[p+i],
  print

Submission status: Accepted

Solution code:
R=range(8)
A=[4,1,4,1,2,1,2,1]
A+=A
B="41412121"
B=map(int,[B[i:]+B[:i] for i in R])

import sys
for s in sys.stdin:
  x=map(int,s[:-1].split())
  m=sum(x)
  for i in R:
    s=sum([max(0,x[j]-A[i+j]) for j in R])
    if s<m: m,p=s,B[i]
    elif m==s and B[i]<p: p=B[i]
  print " ".join(list(str(p)))

Submission status: Accepted

Solution code:
R = range(8)
A = [4,1,4,1,2,1,2,1]
A += A
B = "41412121"
B = map(int, [B[i:]+B[:i] for i in R])

import sys
for s in sys.stdin:
  x = map(int, s[:-1].split())
  m = sum(x)
  for i in R:
    s = sum([max(0, x[j] - A[i+j]) for j in R])
    if s<m: m,p = s,B[i]
    elif m==s and B[i]<p: p=B[i]
  print " ".join(list(str(p)))

Submission status: Accepted

Solution code:
R = range(8)
A = [4,1,4,1,2,1,2,1]
A += A
B = "41412121"
B = map(int, [B[i:]+B[:i] for i in R])

import sys
for s in sys.stdin:
  x = map(int, s[:-1].split())
  m = sum(x)
  for i in R:
    s = sum([max(0, x[j] - A[i+j]) for j in R])
    if s<m: m,p = s,B[i]
    elif m==s and B[i]<p: p=B[i]
  a= " ".join(list(str(p)))
  print a

Submission status: Accepted

Solution code:
R=range(8)
A=[4,1,4,1,2,1,2,1]
A+=A
B="41412121"
B=map(int,[B[i:]+B[:i] for i in R])

import sys
for s in sys.stdin:
  x=map(int,s[:-1].split())
  m=sum(x)
  for i in R:
    s=sum([max(0,x[j]-A[i+j]) for j in R])
    if s<m:
      m=s
      p=B[i]
    elif m==s and B[i]<p: p=B[i]
  print " ".join(list(str(p)))

Submission status: Accepted

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 = 999999999
		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:
lst = [[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:
        inputlst = list(map(int, input().split()))
        
        m = 70000
        ans = 0
        for num, i in enumerate(lst):
            n = 0
            for j, k in zip(inputlst,i):
                if j > k:
                    n = n + (j - k)

            if m > n:
                m = n
                ans = num

        print(*lst[ans])

    except EOFError:
        break

Submission status: Accepted

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

def round():
    item = table.pop(0)
    table.append(item)
    
def getRideNum(participants):
    rideNum = 0
    for i in range(0,8):
        n = table[i] - participants[i]
        if n >= 0:
            n = participants[i]
        else:
            n = table[i]
        rideNum += n
    return rideNum
    
def tableToNum(t):
    num = 0
    for i in range(7,-1,-1):
        n = t[i] * pow(10,7 - i)
        num += n
    return num

while(1):
    try:
        participants = list(int(x) for x in input().split())
        maxRideNum = 0
        maxTable = []

        for i in range(0,8):
            rideNum = getRideNum(participants)
            if rideNum > maxRideNum:
                maxRideNum = rideNum
                maxTable = table.copy()
                
            elif rideNum == maxRideNum:
                if tableToNum(table) < tableToNum(maxTable):
                    maxTable = table.copy()
            round()

        for i in range(0,len(maxTable)):
            if i != 0:
                print(" ",end="")
            print(maxTable[i],end="")
        print()
        
    except EOFError:
        break

Submission status: Accepted

Solution code:
from copy import copy

p = []
while(1):
    try:
        p = [int(i) for i in input().split()]
        min_ = 10**10
        point = 0
        arrmin = []
        arr = [4,1,4,1,2,1,2,1]
        for _ in range(8):
            temp = copy(arr[0])
            repl = copy(arr[1:8])
            arr[0:7] = repl
            arr[7] = temp
            point = sum([j-i if i < j else 0 for i,j in zip(arr,p)])
            if point == min_:
                arrmin.append(copy(arr))
                min_ = point
            elif point < min_:
                arrmin = [copy(arr)]
                min_ = point
        arrmin = ["".join([str(i) for i in j]) for j in arrmin]
        amin = min([int(i) for i in arrmin])
        out = list(str(amin))
        print(" ".join(out))
    except EOFError:
        break

Submission status: Runtime Error

Solution code:
v=[4,1,4,1,2,1,2,1]
while True:
    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)

Submission status: Runtime Error

Solution code:
v=[4,1,4,1,2,1,2,1]
while True:
    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)

Submission status: Runtime Error

Solution code:
# Aizu Problem 0082: Flying Jenny
#
import sys, math, os

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


def flying_jenny(p):
    a = [1, 2, 1, 2, 1, 4, 1, 4]
    min_dist = 100000
    ans = []
    for j in range(8):
        dist = sum([max(0, p[k] - a[(k+j)%8]) for k in range(8)])
        if dist < min_dist:
            ans = [a[j:] + a[:j]]
            min_dist = dist
        elif dist == min_dist:
            ans.append([a[j:] + a[:j]])
    ans = sorted(ans)[0]
    print(' '.join([str(a) for a in ans]))
    

for line in sys.stdin:
    p = [int(_) for _ in line.split()]
    flying_jenny(p)

Submission status: Runtime Error

Solution code:
merry=[4,1,4,1,2,1,2,1]
while True:
    try:
        guest=map(int,raw_input().split())
    except EOFError:
        break
    m=10000
    for i in range(len(merry)):
        temp=merry[i:]+merry[:i]
        s=sum([guest[i]-temp[i] for i in range(len(merry)) if guest[i]-temp[i]>\
=0])
        m=min(m,s)
    P=set([])
    for i in range(len(merry)):
        temp=merry[i:]+merry[:i]
        s=sum([guest[i]-temp[i] for i in range(len(merry)) if guest[i]-temp[i]>\
=0])
        if s==m:
            P.add(int("".join(map(str,temp))))
    print " ".join(list(str(min(P))))

Submission status: Runtime Error

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=set()
    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:
            m=s
            ans.clear()
            ans.add("".join(map(str,temp)))
        if s==m:
            ans.add("".join(map(str,temp)))
    print " ".join(min(ans))

Submission status: Runtime Error

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=set()
    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:
            ans.add("".join(map(str,temp)))
        elif s<m:
            m=s
            ans.clear()
            ans.add("".join(map(str,temp)))
    print " ".join(min(ans))

Submission status: Runtime Error

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
				mxcart = "".join(map(str, cart[sp:]+cart[:sp]))
			elif sm = mx:
				acart = "".join(map(str, cart[sp:]+cart[:sp]))
				if int(mxcart) > int(acart):
					mxcart = acart
		print " ".join(map(str, mxcart))
	except:
		break

Submission status: Runtime Error

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
				mxcart = "".join(map(str, cart[sp:]+cart[:sp]))
			elif sm == mx:
				acart = "".join(map(str, cart[sp:]+cart[:sp]))
				if int(mxcart) > int(acart):
					mxcart = acart
		print " ".join(map(str, mxcart))
	except:
		break

Submission status: Runtime Error

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:
	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])

Submission status: Runtime Error

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:
	if not line: continue
	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])