Thank you for your response. I changed my code to use a dictionary instead of a list, but here's my code anyway. I added a few comments for clarification.
[bbone=text,88]import sys
def func(v,t,P,S):
P.append(t[v])
try:
t[(v[0]+1,v[1])]
for i in range(2):
S=func((v[0]+1, v[1]+i),t,P,S)
P.pop()
return S
except KeyError:
return max(sum(P),S)
if name == 'main':
N=int(sys.stdin.readline())
#Each iteration of the for loop deal with a single triangle
for i in range(1,N+1):
n=int(sys.stdin.readline()) #Number of lines in the triangle
t={}
for layer in range(n):
l=sys.stdin.readline()
l=l.split()
for i in range(len(l)):
#Add each line to a dict containing the triangle values. The keys are tuples (i,k),
#where i is the line and k is the "column"
t[(layer,i)]=int(l[i])
P=[]
S=func((0,0),t,P,0) #Call recursive function at the root node (0,0)
sys.stdout.write(str(S)+'\n')
exit()[/bbone]