Problem p02578 (python)

Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    n = int(input())
    a = list(map(int, input().split()))
    idx_start = 0
    sum_a = 0
    ans = 0
    cnt = 0
    len_a = len(a)

    for i in range(1, len_a):
        if a[idx_start] < a[i]:
            ans += (cnt * a[idx_start]) - sum_a
            idx_start = i
            sum_a = 0
            cnt = 0
        else:
            sum_a += a[i]
            cnt += 1
            if i == len_a - 1:
                ans += (cnt * a[idx_start]) - sum_a
    print(ans)
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np

input = lambda: sys.stdin.readline().rstrip()

def solve(a):
    # a_max = max(a)
    # idx_max = a.index(a_max)
    idx_max = a.argmax()
    a_max = a[idx_max]
    return ((len(a) - 1 - idx_max) * a_max) - sum(a[idx_max + 1:]), idx_max

def solve2(a):
    idx_start = 0
    sum_a = 0
    ans = 0
    len_a = len(a)
    for i in range(1, len_a):
        if a[idx_start] < a[i]:
            # return ((i - 1) * a[0]) - sum_a, i
            ans += ((i - 1) * a[idx_start]) - sum_a
            idx_start = i
            sum_a = 0
        else:
            sum_a += a[i]
            if i == len_a - 1:
                ans += ((i - idx_start) * a[idx_start]) - sum_a
    return ans

def main():
    n = int(input())
    a = np.array(list(map(int, input().split())))
    ans = 0
    # while(1):
    #     k, idx_max = solve2(a)
    #     ans += k
    #     if idx_max == len(a):
    #         break
    #     a = a[idx_max:]
    ans = solve2(a)
    print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
input = lambda: sys.stdin.readline().rstrip()

def solve(a):
    # a_max = max(a)
    # idx_max = a.index(a_max)
    idx_max = a.argmax()
    a_max = a[idx_max]
    return ((len(a) - 1 - idx_max) * a_max) - sum(a[idx_max + 1:]), idx_max

def solve2(a):
    sum_a = 0
    len_a = len(a)
    for i in range(1, len_a):
        if a[0] < a[i]:
            return ((i - 1) * a[0]) - sum_a, i
        else:
            sum_a += a[i]
            if i == len_a - 1:
                return (i * a[0]) - sum_a, i + 1

def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    while(1):
        k, idx_max = solve2(a)
        ans += k
        if idx_max == len(a):
            break
        a = a[idx_max:]
    print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np

input = lambda: sys.stdin.readline().rstrip()

def solve(a):
    # a_max = max(a)
    # idx_max = a.index(a_max)
    idx_max = a.argmax()
    a_max = a[idx_max]
    return ((len(a) - 1 - idx_max) * a_max) - sum(a[idx_max + 1:]), idx_max

def solve2(a):
    idx_min = 0
    sum_a = 0
    len_a = len(a)
    for i in range(1, len_a):
        if a[0] < a[i]:
            return ((i - 1) * a[0]) - sum_a, i
        else:
            sum_a += a[i]
            if i == len_a - 1:
                return (i * a[0]) - sum_a, i + 1

def main():
    n = int(input())
    a = np.array(list(map(int, input().split())))
    ans = 0
    while(1):
        k, idx_max = solve2(a)
        ans += k
        if idx_max == len(a):
            break
        a = a[idx_max:]
    print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np

input = lambda: sys.stdin.readline().rstrip()

def solve(a):
    # a_max = max(a)
    # idx_max = a.index(a_max)
    idx_max = a.argmax()
    a_max = a[idx_max]
    return ((len(a) - 1 - idx_max) * a_max) - sum(a[idx_max + 1:]), idx_max

def solve2(a):
    idx_min = 0
    sum_a = 0
    for i in range(1, len(a)):
        if a[0] < a[i]:
            return ((i - 1) * a[0]) - sum_a, i
        else:
            sum_a += a[i]
            if i == len(a) - 1:
                return (i * a[0]) - sum_a, i + 1

def main():
    n = int(input())
    a = np.array(list(map(int, input().split())))
    ans = 0
    while(1):
        k, idx_max = solve(a)
        ans += k
        if idx_max == 0:
            break
        a = a[:idx_max]
    print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
input = lambda: sys.stdin.readline().rstrip()

def solve(a):
    a_max = max(a)
    idx_max = a.index(a_max)
    return ((len(a) - 1 - idx_max) * a_max) - sum(a[idx_max + 1:]), idx_max

def solve2(a):
    idx_min = 0
    sum_a = 0
    for i in range(1, len(a)):
        if a[0] < a[i]:
            return ((i - 1) * a[0]) - sum_a, i
        else:
            sum_a += a[i]
            if i == len(a) - 1:
                return (i * a[0]) - sum_a, i + 1

def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    while(1):
        k, idx_max = solve2(a)
        ans += k
        if idx_max == len(a):
            break
        a = a[idx_max:]
    print(ans)

if __name__ == '__main__':
    main()