Problem p03496 (python)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))

M = max(A)
Mi = A.index(M)
m = min(A)
mi = A.index(m)

cnt = 0
ans = []

if M > 0 and m < 0:
    if abs(m) <= abs(M):
        for i in range(N):
            if Mi == i:
                continue
            x = Mi
            y = i
            A[y] += A[x]
            ans.append((x, y))
            cnt += 1
    else:
        for i in range(N):
            if mi == i:
                continue
            x = mi
            y = i
            A[y] += A[x]
            ans.append((x, y))
            cnt += 1

M = max(A)
m = min(A)

if m >= 0:
    for i in range(N-1):
        x = i
        y = i+1
        A[y] += A[x]
        ans.append((x, y))
        cnt += 1
else:
    for i in range(N-1, 0, -1):
        x = i
        y = i-1
        A[y] += A[x]
        ans.append((x, y))
        cnt += 1

print(cnt)
for x, y in ans:
    print(x+1, y+1)

Buggy submission (verdict: Runtime Error):
inf = float('inf')

N = int(input())
A = list(map(int,input().split()))

# 累積min/max
cmin = [0] * (N+1)
cmax = [0] * (N+1)
resmin = inf
resmax = -inf
for i, a in enumerate(A):
    resmin = min(resmin, a)
    resmax = max(resmax, a)
    cmin[i+1] = resmin
    cmax[i+1] = resmax

# 正負の境界を全探索 -[0,k) +[k, N)
for k in range(N+1):
    ans = []
    cnt = 0
    m = cmin[k]
    M = cmax[N] - cmax[k]

    if m < 0:
        for i, a in enumerate(A):
            res = max(-(a // m), 0)
            cnt += res
            if cnt > 2*N:
                break
            for _ in range(res):
                x = A.index(m)
                y = i
                A[y] += A[x]
                ans.append((x, y))
        
        for i in range(k-1, 0, -1):
            ans.append((i, i-1))
            A[i-1] += A[i]
            cnt += 1

    if M > 0:
        for i, a in enumerate(A):
            res = max(-(a // M), 0)
            cnt += res
            if cnt > 2*N:
                break
            for _ in range(res):
                x = A.index(M)
                y = i
                A[y] += A[x]
                ans.append((x, y))

        for i in range(k, N-1):
            ans.append((i, i+1))
            A[i+1] += A[i]
            cnt += 1
    
    flag = all(A[i] <= A[i+1] for i in range(N-1))

    if cnt < 2*N and flag:
        print(cnt)
        for x, y in ans:
            print(x+1, y+1)
        exit()

Buggy submission (verdict: Runtime Error):
inf = float('inf')

N = int(input())
A = list(map(int,input().split()))

# 累積min/max
cmin = [0] * (N+1)
cmax = [0] * (N+1)
resmin = inf
resmax = -inf
for i, a in enumerate(A):
    resmin = min(resmin, a)
    resmax = max(resmax, a)
    cmin[i+1] = resmin
    cmax[i+1] = resmax

# 正負の境界を全探索 -[0,k) +[k, N)
for k in range(N+1):
    ans = []
    cnt = 0
    m = cmin[k]
    M = cmax[N] - cmax[k]

    if m < 0:
        for i, a in enumerate(A):
            res = max(-(a // m), 0)
            cnt += res
            if cnt > 2*N:
                break
            for _ in range(res):
                x = A.index(m)
                y = i
                A[y] += A[x]
                ans.append((x, y))
        
        for i in range(k-1, 0, -1):
            ans.append((i, i-1))
            A[i-1] += A[i]
            cnt += 1

    if M > 0:
        for i, a in enumerate(A):
            res = max(-(a // M), 0)
            cnt += res
            if cnt > 2*N:
                break
            for _ in range(res):
                x = A.index(M)
                y = i
                A[y] += A[x]
                ans.append((x, y))

        for i in range(k, N-1):
            ans.append((i, i+1))
            A[i+1] += A[i]
            cnt += 1
    
    flag = all(A[i] <= A[i+1] for i in range(N-1))

    if cnt < 2*N and flag:
        print(cnt)
        for x, y in ans:
            print(x, y)
        exit()

Buggy submission (verdict: Runtime Error):
inf = float('inf')

N = int(input())
A = list(map(int,input().split()))

# 累積min/max
cmin = [0] * (N+1)
cmax = [0] * (N+1)
resmin = inf
resmax = -inf
for i, a in enumerate(A):
    resmin = min(resmin, a)
    resmax = max(resmax, a)
    cmin[i+1] = resmin
    cmax[i+1] = resmax

# 正負の境界を全探索 -[0,k) +[k, N)
for k in range(N):
    ans = []
    cnt = 0
    m = cmin[k]
    M = cmax[N] - cmax[k]
    flag = True

    if m > 0:
        for i, a in enumerate(A):
            res = max(-(a // m), 0)
            cnt += res
            if cnt > 2*N:
                flag = False
                break
            for _ in range(res):
                x = A.index(m)
                y = i
                ans.append((x, y))

    if not flag:
        continue

    if M > 0:
        for i, a in enumerate(A):
            res = max(-(a // M), 0)
            cnt += res
            if cnt > 2*N:
                flag = False
                break
            for _ in range(res):
                x = A.index(M)
                y = i
                ans.append((x, y))
    
    if not flag:
        continue

    for i in range(k-1, 0, -1):
        ans.append((i, i-1))
        cnt += 1
    
    for i in range(k, N-1):
        ans.append((i, i+1))
        cnt += 1
    
    if cnt < 2*N:
        print(cnt)
        for x, y in ans:
            print(x, y)
        exit()

Buggy submission (verdict: Runtime Error):
inf = float('inf')

N = int(input())
A = list(map(int,input().split()))

# 累積min/max
cmin = [0] * (N+1)
cmax = [0] * (N+1)
resmin = inf
resmax = -inf
for i, a in enumerate(A):
    resmin = min(resmin, a)
    resmax = max(resmax, a)
    cmin[i+1] = resmin
    cmax[i+1] = resmax

# 正負の境界を全探索 -[0,k) +[k, N)
for k in range(N):
    ans = []
    cnt = 0
    m = cmin[k]
    M = cmax[N] - cmax[k]
    flag = True

    if m > 0:
        for i, a in enumerate(A):
            res = max(-(a // m), 0)
            cnt += res
            if cnt > 2*N:
                flag = False
                break
            for _ in range(res):
                x = A.index(m)
                y = i
                ans.append((x, y))

    if not flag:
        continue

    if M > 0:
        for i, a in enumerate(A):
            res = max(-(a // M), 0)
            cnt += res
            if cnt > 2*N:
                flag = False
                break
            for _ in range(res):
                x = A.index(M)
                y = i
                ans.append((x, y))
    
    if not flag:
        continue

    for i in range(k-1, 0, -1):
        ans.append((i, i-1))
        cnt += 1
    
    for i in range(k, N-1):
        ans.append((i, i+1))
        cnt += 1
    
    if cnt < 2*N:
        print(cnt)
        print(*ans, sep="\n")
        exit()