Problem p03496 (python)

Fixed submission:
def a(xs):
    k = 0
    m = 0
    ops = []
    for i, x in enumerate(xs):
        if x > m:
            k = i
            m = x
    for i, x in enumerate(xs):
        if xs[i] < 0:
            xs[i] += m
            ops.append((k, i))
    for i in range(len(xs) - 1):
        if xs[i] > xs[i + 1]:
            xs[i + 1] += xs[i]
            ops.append((i, i + 1))
    return ops

def b(xs):
    k = 0
    m = 0
    ops = []
    for i, x in enumerate(xs):
        if x < m:
            k = i
            m = x
    for i, x in enumerate(xs):
        if xs[i] > 0:
            xs[i] += m
            ops.append((k, i))
    for i in range(len(xs) - 2, -1, -1):
        if xs[i] > xs[i + 1]:
            xs[i] += xs[i + 1]
            ops.append((i + 1, i))
    return ops
            
n = input()
xs = map(int, raw_input().split())
m = min(xs)
M = max(xs)
if abs(M) >= abs(m):
    ops = a(xs)
else:
    ops = b(xs)
print len(ops)
for a, b in ops:
    print a + 1, b + 1

Buggy submission (verdict: Runtime Error):
n = input()
xs = map(int, raw_input().split())
ops = []
for i in range(n - 1):
    xs[i] += xs[i + 1]
    ops.append((i, i + 1))
xs[n - 1] += xs[n - 2]
ops.append((n - 1, n - 2)
for i in range(n - 1):
    xs[i] += xs[i + 1]
    ops.append((i, i + 1))
xs[n - 1] += xs[n - 2]
ops.append((n - 1, n - 2)
print len(ops)
for a, b in ops:
    print a + 1, b + 1

Buggy submission (verdict: Runtime Error):
import math

def a(xs, m):
    k = xs.index(m) + 1
    ops = []
    a = xs[0];
    for i in range(1, len(xs)):
        b = xs[i]
        if a > b:
            d = a - b
            n = int(math.ceil(d / float(m)))
            d2 = m - b
            n2 = int(math.ceil(d2 / float(m)))
            if n2 > 0 and n2 < n:
                b += n2 * m
                ops.append((k, i + 1, n2))
                k = i + 2
                m = b
                d = b - a
                n = int(math.ceil(d / float(m)))
            b += n * m
            ops.append((k, i + 1, n))
        a = b
    return ops

def b(xs, m):
    k = xs.index(m) + 1
    ops = []
    a = xs[-1]
    for i in range(len(xs) - 2, -1, -1):
        b = xs[i]
        if a < b:
            d = a - b
            n = int(math.ceil(d / float(m)))
            d2 = m - b
            n2 = int(math.ceil(d2 / float(m)))
            if n2 > 0 and n2 < n:
                b += n2 * m
                ops.append(k, i, n2)
                k = i + 1
                m = b
                d = a - b
                n = int(math.ceil(d / float(m)))
            b += n * m
            ops.append((k, i, n))
        a = b
    return ops

n = input()
xs = map(int, raw_input().split())
M = max(xs)
m = min(xs)
if M > 0 and m >= 0:
    ops = a(xs, M)
    c = len(ops)
elif M <= 0 and m < 0:
    ops = b(xs, m)
    c = len(ops)
else:
    ops1 = a(xs, M)
    ops2 = b(xs, m)
    c1 = 0
    for _, _, n in ops1: c1 += n
    c2 = 0
    for _, _, n in ops2: c2 += n
    if c1 <= c2:
        ops = ops1
        c = c1
    else:
        ops = ops2
        c = c2
print c
for a, b, n in ops:
    for _ in range(n):
        print a, b

Buggy submission (verdict: Runtime Error):
import math

def a(xs, m):
    k = xs.index(m) + 1
    ops = []
    a = xs[0];
    for i in range(1, len(xs)):
        b = xs[i]
        if a > b:
            d = a - b
            n = int(math.ceil(d / float(m)))
            d2 = m - b
            n2 = int(math.ceil(d2 / float(m)))
            if n2 > 0 and n2 < n:
                b += n2 * m
                ops.append((k, i + 1, n2))
                k = i + 2
                m = b
                d = b - a
                n = int(math.ceil(d / float(m)))
            b += n * m
            ops.append((k, i + 1, n))
        a = b
    return ops

def b(xs, m):
    k = xs.index(m) + 1
    ops = []
    a = xs[-1]
    for i in range(len(xs) - 2, -1, -1):
        b = xs[i]
        if a < b:
            d = a - b
            n = int(math.ceil(d / float(m)))
            d2 = m - b
            n2 = int(math.ceil(d2 / float(m)))
            if n2 > 0 and n2 < n:
                b += n2 * m
                ops.append(k, i, n2)
                k = i + 1
                m = b
                d = a - b
                n = int(math.ceil(d / float(m)))
            b += n * m
            ops.append((k, i, n))
        a = b
    return ops

n = input()
xs = map(int, raw_input().split())
M = max(xs)
m = min(xs)
if M > 0 and m >= 0:
    ops = a(xs, M)
elif M <= 0 and m < 0:
    ops = b(xs, m)
else:
    ops1 = a(xs, M)
    ops2 = b(xs, m)
    if len(ops1) <= len(ops2):
        ops = ops1
    else:
        ops = ops2
c = 0
for _, _, n in ops: c += n
print c
for a, b, n in ops:
    for _ in range(n):
        print a, b