Problem p03147 (python)

Fixed submission:
N = int(input())
array = input().split(' ')
harray = []
for z in array:
    z = int(z)
    harray.append(z)
counter = 0
while harray != [0]*N:
    if 0 not in harray:
        a = min(harray)
        for i in range(len(harray)):
            harray[i] -= a
        counter += a
    else:
        zeroarray = [j for j, x in enumerate(harray) if x == 0]
        if len(zeroarray) == 1:
            if zeroarray[0] == 0:
                a = min(harray[1:])
                if len(harray) == 2:
                    harray[1] -= a
                else:
                    for y in range(1,N):
                        harray[y] -= a
                counter += a
            elif zeroarray[0] == N-1:
                a = min(harray[:N-1])
                for y in range(N-1):
                    harray[y] -= a
                counter += a
            else:
                a = min(harray[:zeroarray[0]])
                for y in range(zeroarray[0]):
                    harray[y] -= a
                counter += a
                a = min(harray[zeroarray[0]+1:])
                if zeroarray[0]+1 != N:
                    for y in range(zeroarray[0]+1,N):
                            harray[y] -= a
                else:
                    harray[N-1] -= a
                counter += a
        else:
            if zeroarray[0] != 0:
                    a = min(harray[:zeroarray[0]])
                    for y in range(zeroarray[0]):
                        harray[y] -= a
                    counter += a
            if zeroarray[-1] != N-1:
                    a = min(harray[zeroarray[-1]+1:])
                    if zeroarray[-1]+1 != N:
                        for y in range(zeroarray[-1]+1,N):
                            harray[y] -= a
                    else:
                        harray[N-1] -= a
                    counter += a
            for k in range(len(zeroarray)-1):
                
                if zeroarray[k+1] - zeroarray[k] == 1:
                    continue
                else:
                    a = min(harray[zeroarray[k]+1:zeroarray[k+1]])
                    for y in range(zeroarray[k]+1,zeroarray[k+1]):
                        harray[y] -= a
                    counter += a
                

print(counter)

Buggy submission (verdict: Runtime Error):
N = int(input())
array = input().split(' ')
harray = []
for z in array:
    z = int(z)
    harray.append(z)
counter = 0
while harray != [0]*N:
    if 0 not in harray:
        a = min(harray)
        for i in range(len(harray)):
            harray[i] -= a
        counter += a
    else:
        zeroarray = [j for j, x in enumerate(harray) if x == 0]
        if len(zeroarray) == 1:
            if zeroarray[0] == 0:
                a = min(harray[1:])
                for y in range(1,N+1):
                    harray[y] -= a
                counter += a
            elif zeroarray[0] == N-1:
                a = min(harray[:N-2])
                for y in range(N-1):
                    harray[y] -= a
                counter += a
            else:
                a = min(harray[:zeroarray[0]])
                for y in range(zeroarray[0]):
                    harray[y] -= a
                counter += a
                a = min(harray[zeroarray[0]+1:])
                for y in range(zeroarray[0]+1,N):
                    harray[y] -= a
                counter += a
        else:
            for k in range(len(zeroarray)-1):
                if zeroarray[0] != 0:
                    a = min(harray[:zeroarray[0]])
                    for y in range(zeroarray[0]):
                        harray[y] -= a
                    counter += a
                if zeroarray[k+1] - zeroarray[k] == 1:
                    continue
                else:
                    a = min(harray[zeroarray[k]+1:zeroarray[k+1]])
                    for y in range(zeroarray[k]+1,zeroarray[k+1]):
                        harray[y] -= a
                    counter += a
                if zeroarray[-1] != N-1:
                    a = min(harray[zeroarray[-1]+1:])
                    for y in range(zeroarray[-1]+1,N):
                        harray[y] -= a
                    counter += a
        

print(counter)