Problem p02603 (python)

Fixed submission:

def main():
    _ = int(input())
    A = list(int(c) for c in input().split(' '))

    tmp = []
    for i in range(len(A)-1):
        if A[i] != A[i+1]:
            tmp.append(A[i])
    if len(tmp) == 0:
        return 1000
    if A[-1] != tmp[-1]:
        tmp.append(A[-1])
    A = tmp
    #print(A)

    money = 1000
    if len(A) < 2:
        return 1000

    if len(A) == 2:
        if A[0] < A[1]:
            kabu = money // A[0]
            money -= kabu*A[0]
            money += kabu*A[1]
            return money
        else:
            return 1000

    l = []
    kabu = 0
    if A[1] > A[0]:
        ascending = True
        kabu = money // A[0]
        money -= kabu*A[0]
    else:
        ascending = False
    for i in range(len(A)-1):
        #print(A[i],kabu,money)
        if ascending:  #上昇中
            if A[i] > A[i+1]:
                #下降に転じたので売り
                if kabu > 0:
                    money += kabu*A[i]
                    kabu = 0
                    ascending = False
                else:
                    pass
            else:
                continue
        else:  #下降中
            if A[i] < A[i+1]:
                #上昇したので買い
                assert kabu == 0
                kabu = money // A[i]
                money -= kabu*A[i]
                ascending = True
            else:
                continue
    if kabu > 0:
        money += kabu*A[-1]
        kabu = 0
    return money
if __name__ == "__main__":
    ret = main()
    print(ret)

Buggy submission (verdict: Runtime Error):

def main():
    _ = int(input())
    A = list(int(c) for c in input().split(' '))

    if len(A) == 2 and A[0] == A[1]:
        return 1000

    tmp = []
    for i in range(len(A)-1):
        if A[i] != A[i+1]:
            tmp.append(A[i])
    if A[-1] != tmp[-1]:
        tmp.append(A[-1])
    A = tmp
    #print(A)

    money = 1000
    if len(A) < 2:
        return 1000

    if len(A) == 2:
        if A[0] < A[1]:
            kabu = money // A[0]
            money -= kabu*A[0]
            money += kabu*A[1]
            return money
        else:
            return 1000

    l = []
    kabu = 0
    if A[1] > A[0]:
        ascending = True
        kabu = money // A[0]
        money -= kabu*A[0]
    else:
        ascending = False
    for i in range(len(A)-1):
        #print(A[i],kabu,money)
        if ascending:  #上昇中
            if A[i] > A[i+1]:
                #下降に転じたので売り
                if kabu > 0:
                    money += kabu*A[i]
                    kabu = 0
                    ascending = False
                else:
                    pass
            else:
                continue
        else:  #下降中
            if A[i] < A[i+1]:
                #上昇したので買い
                assert kabu == 0
                kabu = money // A[i]
                money -= kabu*A[i]
                ascending = True
            else:
                continue
    if kabu > 0:
        money += kabu*A[-1]
        kabu = 0
    return money
if __name__ == "__main__":
    ret = main()
    print(ret)

Buggy submission (verdict: Runtime Error):

def main():
    _ = int(input())
    A = list(int(c) for c in input().split(' '))

    
    tmp = []
    for i in range(len(A)-1):
        if A[i] != A[i+1]:
            tmp.append(A[i])
    if A[-1] != tmp[-1]:
        tmp.append(A[-1])
    A = tmp
    #print(A)

    money = 1000
    if len(A) < 2:
        return 1000

    if len(A) == 2:
        if A[0] < A[1]:
            kabu = money // A[0]
            money -= kabu*A[0]
            money += kabu*A[1]
            return money
        else:
            return 1000

    l = []
    kabu = 0
    if A[1] > A[0]:
        ascending = True
        kabu = money // A[0]
        money -= kabu*A[0]
    else:
        ascending = False
    for i in range(len(A)-1):
        #print(A[i],kabu,money)
        if ascending:  #上昇中
            if A[i] > A[i+1]:
                #下降に転じたので売り
                if kabu > 0:
                    money += kabu*A[i]
                    kabu = 0
                    ascending = False
                else:
                    pass
            else:
                continue
        else:  #下降中
            if A[i] < A[i+1]:
                #上昇したので買い
                assert kabu == 0
                kabu = money // A[i]
                money -= kabu*A[i]
                ascending = True
            else:
                continue
    if kabu > 0:
        money += kabu*A[-1]
        kabu = 0
    return money
if __name__ == "__main__":
    ret = main()
    print(ret)