Buggy python submission (verdict: Runtime Error):
def greedy(N,L,ices):
    counts = 0
    while True:
        for i in range(len(ices)):
            if 0 < i < N-1:
                if ices[i] > ices[i-1] and ices[i] > ices[i+1]:
                    ices[i] += 1
                    if ices[i] >= L:
                        ices[i] = 0
            elif i == 0:
                if ices[i] > ices[1]:
                    ices[i] += 1
                    if ices[i] >= L:
                        ices[i] = 0
            elif i == N-1:
                if ices[i] > ices[N-2]:
                    ices[i] += 1
                    if ices[i] >= L:
                        ices[i] = 0
    if ices.count(0) == N:
        print(counts)
        return
    counts += 1


N,L = input().rstrip().split()
ices = []
while True:
    ice = input().rstrip()
    ices.append(ice)
    if len(ices) == N:
        break

greedy(N,L,ices)

Fixed submission:
def orderN(N,L,ices):
    upPeak = L - ices[0]
    downPeak = L - ices[0]
    peaks = []
    for i in range(len(ices)):
        if i < N-1:
            if ices[i] < ices[i+1]:
                peaks.append(downPeak)
                downPeak = L - ices[i+1]
                upPeak += L - ices[i+1]
            elif ices[i] > ices[i+1]:
                peaks.append(upPeak)
                upPeak = L - ices[i+1]
                downPeak += L - ices[i+1]
        elif i == N-1:
            peaks.append(upPeak)
            peaks.append(downPeak)
    print(max(peaks))

N,L = map(int,input().strip().split())
ices = [int(input().strip()) for _ in range(N)]


orderN(N,L,ices)

Buggy python submission (verdict: Runtime Error):
def greedy(N,L,ices):
    counts = 1
    while True:
        # print(ices)
        for i in range(len(ices)):
            if 0 < i < N-1:
                if ices[i] > ices[i-1] and ices[i] > ices[i+1]:
                    ices[i] += 1
                    if ices[i] >= L:
                        ices[i] = 0
            elif i == 0:
                if ices[i] > ices[1]:
                    ices[i] += 1
                    if ices[i] >= L:
                        ices[i] = 0
            elif i == N-1:
                if ices[i] > ices[N-2]:
                    ices[i] += 1
                    if ices[i] >= L:
                        ices[i] = 0

        if ices.count(0) == N:
            print(counts)
            return
        counts += 1


N,L = input().rstrip().split()
ices = []
while True:
    ice = input().rstrip()
    ices.append(ice)
    if len(ices) == N:
        break

# N = 4
# L = 6
# ices = [4,2,3,5]

greedy(N,L,ices)

Fixed submission:
def orderN(N,L,ices):
    upPeak = L - ices[0]
    downPeak = L - ices[0]
    peaks = []
    for i in range(len(ices)):
        if i < N-1:
            if ices[i] < ices[i+1]:
                peaks.append(downPeak)
                downPeak = L - ices[i+1]
                upPeak += L - ices[i+1]
            elif ices[i] > ices[i+1]:
                peaks.append(upPeak)
                upPeak = L - ices[i+1]
                downPeak += L - ices[i+1]
        elif i == N-1:
            peaks.append(upPeak)
            peaks.append(downPeak)
    print(max(peaks))

N,L = map(int,input().strip().split())
ices = [int(input().strip()) for _ in range(N)]


orderN(N,L,ices)

Buggy python submission (verdict: Runtime Error):
import copy

def greedy(N,L,ices):
    counts = 1
    while True:
        # print(ices)
        copied = copy.deepcopy(ices)
        for i in range(len(ices)):
            if copied[i] != 0:
                if 0 < i < N-1:
                    if copied[i] >= L:
                        ices[i] = 0
                        break
                    if copied[i] > copied[i-1] and copied[i] > copied[i+1]:
                        ices[i] += 1
                        if ices[i] >= L:
                            ices[i] = 0
                elif i == 0:
                    if copied[i] >= L:
                        ices[i] = 0
                        break
                    if copied[i] > copied[1]:
                        ices[i] += 1
                        if ices[i] >= L:
                            ices[i] = 0
                elif i == N-1:
                    if ices[i] >= L:
                        ices[i] = 0
                        break
                    if copied[i] > copied[N-2]:
                        ices[i] += 1
                        if ices[i] >= L:
                            ices[i] = 0
        copied = ices


        if ices.count(0) == N:
            print(counts)
            return
        counts += 1


# N,L = map(int,input().strip().split())
# ices = []
# while True:
#     ice = int(input().strip())
#     ices.append(ice)
#     if len(ices) == N:
#         break

# N = 4
# L = 6
# ices = [4,2,3,5]

# N = 6
# L = 10
# ices = [3,4,1,9,5,1]

with open("aoj.txt") as infile:
    N,L = map(int,infile.readline().strip().split())
    ices = []
    while True:
        ice = int(infile.readline())
        ices.append(ice)
        if len(ices) == N:
            break

greedy(N,L,ices)

Fixed submission:
def orderN(N,L,ices):
    upPeak = L - ices[0]
    downPeak = L - ices[0]
    peaks = []
    for i in range(len(ices)):
        if i < N-1:
            if ices[i] < ices[i+1]:
                peaks.append(downPeak)
                downPeak = L - ices[i+1]
                upPeak += L - ices[i+1]
            elif ices[i] > ices[i+1]:
                peaks.append(upPeak)
                upPeak = L - ices[i+1]
                downPeak += L - ices[i+1]
        elif i == N-1:
            peaks.append(upPeak)
            peaks.append(downPeak)
    print(max(peaks))

N,L = map(int,input().strip().split())
ices = [int(input().strip()) for _ in range(N)]


orderN(N,L,ices)

Buggy python submission (verdict: Runtime Error):
import copy

def greedy(N,L,ices):
    counts = 1
    while True:
        # print(ices)
        copied = copy.deepcopy(ices)
        for i in range(len(ices)):
            if copied[i] != 0:
                if 0 < i < N-1:
                    if copied[i] >= L:
                        ices[i] = 0
                        break
                    if copied[i] > copied[i-1] and copied[i] > copied[i+1]:
                        ices[i] += 1
                        if ices[i] >= L:
                            ices[i] = 0
                elif i == 0:
                    if copied[i] >= L:
                        ices[i] = 0
                        break
                    if copied[i] > copied[1]:
                        ices[i] += 1
                        if ices[i] >= L:
                            ices[i] = 0
                elif i == N-1:
                    if ices[i] >= L:
                        ices[i] = 0
                        break
                    if copied[i] > copied[N-2]:
                        ices[i] += 1
                        if ices[i] >= L:
                            ices[i] = 0
        copied = ices


        if ices.count(0) == N:
            print(counts)
            return
        counts += 1


N,L = map(int,input().strip().split())
ices = []
while True:
    ice = int(input().strip())
    ices.append(ice)
    if len(ices) == N:
        break

# N = 4
# L = 6
# ices = [4,2,3,5]

# N = 6
# L = 10
# ices = [3,4,1,9,5,1]

# with open("aoj.txt") as infile:
#     N,L = map(int,infile.readline().strip().split())
#     ices = []
#     while True:
#         ice = int(infile.readline())
#         ices.append(ice)
#         if len(ices) == N:
#             break

greedy(N,L,ices)

Fixed submission:
def orderN(N,L,ices):
    upPeak = L - ices[0]
    downPeak = L - ices[0]
    peaks = []
    for i in range(len(ices)):
        if i < N-1:
            if ices[i] < ices[i+1]:
                peaks.append(downPeak)
                downPeak = L - ices[i+1]
                upPeak += L - ices[i+1]
            elif ices[i] > ices[i+1]:
                peaks.append(upPeak)
                upPeak = L - ices[i+1]
                downPeak += L - ices[i+1]
        elif i == N-1:
            peaks.append(upPeak)
            peaks.append(downPeak)
    print(max(peaks))

N,L = map(int,input().strip().split())
ices = [int(input().strip()) for _ in range(N)]


orderN(N,L,ices)