Problem p02610 (python)

Fixed submission:
import sys, io, os, re
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from pprint import pprint
from math import sin, cos, pi, radians, sqrt, floor, ceil
from copy import copy, deepcopy
from collections import deque
from fractions import gcd
from functools import reduce
from itertools import groupby
from heapq import heapify, heappush, heappop

# sys.setrecursionlimit(5000)

n1 = lambda: int(sys.stdin.readline().strip())
nn = lambda: list(map(int, sys.stdin.readline().strip().split()))
f1 = lambda: float(sys.stdin.readline().strip())
fn = lambda: list(map(float, sys.stdin.readline().strip().split()))
s1 = lambda: sys.stdin.readline().strip()
sn = lambda: list(sys.stdin.readline().strip().split())
nl = lambda n: [n1() for _ in range(n)]
fl = lambda n: [f1() for _ in range(n)]
sl = lambda n: [s1() for _ in range(n)]
nm = lambda n: [nn() for _ in range(n)]
fm = lambda n: [fn() for _ in range(n)]
sm = lambda n: [sn() for _ in range(n)]

def array1(n, d=0): return [d] * n
def array2(n, m, d=0): return [[d] * m for x in range(n)]
def array3(n, m, l, d=0): return [[[d] * l for y in xrange(m)] for x in xrange(n)]
def linc(A, d=1): return map(lambda x: x + d, A)
def ldec(A, d=1): return map(lambda x: x - d, A)


def solve(N, t):
    d1 = array2(N, 0)
    d2 = array2(N, 0)

    t  = [(x[0]-1, x[1], x[2]) for x in t]
    t1 = [(x[0], x[1] - x[2]) for x in t if x[1] >= x[2]]
    t2 = [(N - 2 - x[0], x[2] - x[1]) for x in t if x[1] < x[2] and N - 2 - x[0] >= 0]

    ans = sum([min(x[1], x[2]) for x in t])
    
    for k, dt in t1:
        d1[k].append(dt)

    S = []

    for i in range(N):
        for dt in d1[i]:
            insort(S, dt)

        if len(S) > i:
            S = S[len(S)-i-1:]

    #print("d1: ", d1, " S: ", S)

    ans += sum(S)

    for k, dt in t2:
        d2[k].append(dt)

    S = []
    for i in range(N):
        for dt in d2[i]:
            insort(S, dt)

        if len(S) > i:        
            S = S[len(S)-i-1:]

    #print("d2: ", d2, " S: ", S)
    ans += sum(S)
    print(ans)

T = n1()

for _ in range(T):
    N = n1()
    t = nm(N)
    solve(N, t)

Buggy submission (verdict: Runtime Error):
import sys, io, os, re
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from pprint import pprint
from math import sin, cos, pi, radians, sqrt, floor, ceil
from copy import copy, deepcopy
from collections import deque
from fractions import gcd
from functools import reduce
from itertools import groupby
from heapq import heapify, heappush, heappop
from Queue import PriorityQueue

# sys.setrecursionlimit(5000)

n1 = lambda: int(sys.stdin.readline().strip())
nn = lambda: list(map(int, sys.stdin.readline().strip().split()))
f1 = lambda: float(sys.stdin.readline().strip())
fn = lambda: list(map(float, sys.stdin.readline().strip().split()))
s1 = lambda: sys.stdin.readline().strip()
sn = lambda: list(sys.stdin.readline().strip().split())
nl = lambda n: [n1() for _ in range(n)]
fl = lambda n: [f1() for _ in range(n)]
sl = lambda n: [s1() for _ in range(n)]
nm = lambda n: [nn() for _ in range(n)]
fm = lambda n: [fn() for _ in range(n)]
sm = lambda n: [sn() for _ in range(n)]

def array1(n, d=0): return [d] * n
def array2(n, m, d=0): return [[d] * m for x in range(n)]
def array3(n, m, l, d=0): return [[[d] * l for y in xrange(m)] for x in xrange(n)]
def linc(A, d=1): return map(lambda x: x + d, A)
def ldec(A, d=1): return map(lambda x: x - d, A)


def solve(N, t):
    d1 = array2(N, 0)
    d2 = array2(N, 0)

    t  = [(x[0]-1, x[1], x[2]) for x in t]
    t1 = [(x[0], x[1] - x[2]) for x in t if x[1] >= x[2]]
    t2 = [(N - 2 - x[0], x[2] - x[1]) for x in t if x[1] < x[2] and N - 2 - x[0] >= 0]

    ans = sum([min(x[1], x[2]) for x in t])
    
    for k, dt in t1:
        d1[k].append(dt)

    S = []

    for i in range(N):
        for dt in d1[i]:
            insort(S, dt)

        if len(S) > i:
            S = S[len(S)-i-1:]

    #print("d1: ", d1, " S: ", S)

    ans += sum(S)

    for k, dt in t2:
        d2[k].append(dt)

    S = []
    for i in range(N):
        for dt in d2[i]:
            insort(S, dt)

        if len(S) > i:        
            S = S[len(S)-i-1:]

    #print("d2: ", d2, " S: ", S)
    ans += sum(S)
    print(ans)

T = n1()

for _ in range(T):
    N = n1()
    t = nm(N)
    solve(N, t)