Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n=input()
r=[map(int,raw_input().split()) for _ in xrange(n)]
m=input()
pa=[map(int,raw_input().split()) for _ in xrange(m)]
for i in xrange(m):
    d=[1]*n
    q=deque([pa[i][0]])
    used=[False]*n
    used[pa[i][0]-1]=True
    while len(q)!=0:
        f=0
        p=q.popleft()
        p-=1
        if d[p]==pa[i][2]:continue
        for j in r[p][2:]:
            j-=1
            if pa[i][1]-1==j:
                f=1
                d[j]+=d[p]
                break
            if used[r[j][0]-1]:continue
            q.append(j+1)
            d[j]=d[p]+1
            used[j]=True
        if f==1:
            break
    if f==1:
        print(d[j])
    else:
        print("NA")

Fixed submission:
n=int(raw_input())
g=[[1000000]*n for _ in xrange(n)]
for i in xrange(n):
    g[i][i]=0
for i in xrange(n):
    r=map(int,raw_input().split())
    for j in r[2:]:
        g[r[0]-1][j-1]=1
for k in xrange(n):
    for i in xrange(n):
        for j in xrange(n):
            g[i][j]=min(g[i][j],g[i][k]+g[k][j])
m=int(raw_input())
for i in xrange(m):
    s,d,v=map(int,raw_input().split())
    s-=1
    d-=1
    if g[s][d]<v:
        print(g[s][d]+1)
    else:
        print('NA')

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n=int(raw_input())
r=[map(int,raw_input().split()) for _ in xrange(n)]
m=int(raw_input())
pa=[map(int,raw_input().split()) for _ in xrange(m)]
for i in xrange(m):
    d=[1]*n
    q=deque([pa[i][0]])
    used=[False]*n
    used[pa[i][0]-1]=True
    while len(q)!=0:
        f=0
        p=q.popleft()
        p-=1
        if d[p]==pa[i][2]:break
        for j in r[p][2:]:
            j-=1
            if pa[i][1]-1==j:
                f=1
                d[j]+=d[p]
                break
            if used[r[j][0]-1]:continue
            q.append(j+1)
            d[j]=d[p]+1
            used[j]=True
        if f==1:
            break
    if f==1:
        print(d[j])
    else:
        print("NA")

Fixed submission:
n=int(raw_input())
g=[[1000000]*n for _ in xrange(n)]
for i in xrange(n):
    g[i][i]=0
for i in xrange(n):
    r=map(int,raw_input().split())
    for j in r[2:]:
        g[r[0]-1][j-1]=1
for k in xrange(n):
    for i in xrange(n):
        for j in xrange(n):
            g[i][j]=min(g[i][j],g[i][k]+g[k][j])
m=int(raw_input())
for i in xrange(m):
    s,d,v=map(int,raw_input().split())
    s-=1
    d-=1
    if g[s][d]<v:
        print(g[s][d]+1)
    else:
        print('NA')
Buggy python submission (verdict: Time Limit Exceeded):
while 1:
    x,y,z=map(int,raw_input().split())
    if x==0 and y==0 and z==0:break
    v=map(int,raw_input().split())
    event=[[] for _ in xrange(y+1)]
    maxm=0
    for i in xrange(z):
        n,e,a=map(int,raw_input().split())
        event[n].append(e)
        event[n].append(a)
        if e==2:
            maxm+=a

    dp=[[0]*(5005) for _ in xrange(y+1)]
    dp[0][0]=1
    x=float(x)
    for i in xrange(y):
        for k in xrange(maxm+1):
            for j in xrange(int(x)):
                if i+v[j]>=y:
                    dp[y][k]+=dp[i][k]/x
                    continue

                if event[i+v[j]]==[]:
                    dp[i+v[j]][k]+=dp[i][k]/x

                elif event[i+v[j]][0]==1:
                    skip=event[i+v[j]][1]
                    dp[min(y,i+skip)][k]+=dp[i][k]/x

                elif event[i+v[j]][0]==2:
                    m=event[i+v[j]][1]
                    dp[i+v[j]][k+m]+=dp[i][k]/x

                else:
                    m=event[i+v[j]][1]
                    dp[i+v[j]][max(0,k-m)]+=dp[i][k]/x

    ans=0.0
    for i in xrange(maxm+1):
        ans+=dp[y][i]*i
    print(int(ans))

Fixed submission:
def solve(x,y,maxm):
    for i in xrange(y):
        for j in xrange(maxm+1):
            if dp[i][j]==0:continue
            for k in xrange(int(x)):

                if event[min(y,i+v[k])][0]==0:
                    dp[min(y,i+v[k])][j]+=dp[i][j]/x

                elif event[i+v[k]][0]==1:
                    skip=event[i+v[k]][1]
                    dp[min(y,i+v[k]+skip)][j]+=dp[i][j]/x

                elif event[i+v[k]][0]==2:
                    m=event[i+v[k]][1]
                    dp[i+v[k]][j+m]+=dp[i][j]/x

                else:
                    m=event[i+v[k]][1]
                    dp[i+v[k]][max(0,j-m)]+=dp[i][j]/x
    return dp

while 1:
    x,y,z=map(int,raw_input().split())
    if x==0 and y==0 and z==0:break
    v=map(int,raw_input().split())
    event=[[0]*2 for _ in xrange(y+1)]
    maxm=0
    for i in xrange(z):
        n,e,a=map(int,raw_input().split())
        event[n][0]=e
        event[n][1]=a
        if e==2:
            maxm+=a

    dp=[[0]*(5005) for _ in xrange(y+1)]
    dp[0][0]=1
    x=float(x)
    dp=solve(x,y,maxm)
    ans=0.0
    for i in xrange(maxm+1):
        ans+=dp[y][i]*i
    print(int(ans))
Buggy python submission (verdict: Runtime Error):
a=[0]*10000
b=[0]*10000
ct=0
a_scores=[]
b_scores=[]
while True:
    a_score=0
    b_score=0
    n=input()
    if n==0:
        break
    else :
        for i in xrange(n):
            a[ct+i],b[ct+i]=map(int,raw_input().split())
            if a[ct+i]>b[ct+i]:
                a_score+=a[ct+i]+b[ct+i]
            elif a[ct+i]==b[ct+i]:
                a_score+=a[ct+i]
                b_score+=b[ct+i]
            else:
                b_score+=b[ct+i]+a[ct+i]
        a_scores.append(a_score)
        b_scores.append(b_score)
        ct+=n
for i in xrange(len(a_scores)):
    print a_scores[i],b_scores[i]
    
        
        

Fixed submission:
while 1:
    n=input()
    if n==0:break
    ascore=0
    bscore=0
    for i in xrange(n):
        a,b=map(int,raw_input().split())
        if a>b:
            ascore+=a+b
        elif a==b:
            ascore+=a
            bscore+=b
        else:
            bscore+=a+b
    print ascore,bscore

Buggy python submission (verdict: Runtime Error):
a=[0]*10000
b=[0]*10000
ct=0
a_scores=[]
b_scores=[]
while True:
    a_score=0
    b_score=0
    n=input()
    if n==0:
        break
    else :
        for i in xrange(n):
            a[ct+i],b[ct+i]=map(int,raw_input().split())
            if a[ct+i]>b[ct+i]:
                a_score+=a[ct+i]+b[ct+i]
            elif a[ct+i]==b[ct+i]:
                a_score+=a[ct+i]
                b_score+=b[ct+i]
            else:
                b_score+=b[ct+i]+a[ct+i]
        a_scores.append(a_score)
        b_scores.append(b_score)
        ct+=n
for i in xrange(len(a_scores)):
    print a_scores[i],b_scores[i]
    

Fixed submission:
while 1:
    n=input()
    if n==0:break
    ascore=0
    bscore=0
    for i in xrange(n):
        a,b=map(int,raw_input().split())
        if a>b:
            ascore+=a+b
        elif a==b:
            ascore+=a
            bscore+=b
        else:
            bscore+=a+b
    print ascore,bscore

Buggy python submission (verdict: Runtime Error):
a=[0]*10000
b=[0]*10000
ct=0
a_scores=[]
b_scores=[]
while True:
    a_score=0
    b_score=0
    n=input()
    if n==0:
        break
    else :
        for i in xrange(n):
            a[ct+i],b[ct+i]=map(int,raw_input().split())
            if a[ct+i]>b[ct+i]:
                a_score+=a[ct+i]+b[ct+i]
            elif a[ct+i]==b[ct+i]:
                a_score+=a[ct+i]
                b_score+=b[ct+i]
            else:
                b_score+=b[ct+i]+a[ct+i]
        print a_score,b_score
        ct+=n

Fixed submission:
while 1:
    n=input()
    if n==0:break
    ascore=0
    bscore=0
    for i in xrange(n):
        a,b=map(int,raw_input().split())
        if a>b:
            ascore+=a+b
        elif a==b:
            ascore+=a
            bscore+=b
        else:
            bscore+=a+b
    print ascore,bscore
Buggy python submission (verdict: Time Limit Exceeded):
def dfs(s,com):
    if s==0:
        for i in xrange(len(com)-1):
            if com[i]<com[i+1]:return
        ans.append(com)
    for i in xrange(s,0,-1):
        if s>=i:
            com+=str(i)
            dfs(s-i,com)
            com=com[0:len(com)-1]
    return ans

while 1:
    n=int(raw_input())
    if n==0:break
    ans=[]
    ans=dfs(n,"")
    for i in xrange(len(ans)):
        print(' '.join(ans[i]))

Fixed submission:
def dfs(s,com):
    if s==0:
        ans.append(" ".join(map(str,com)))
        return
    for i in xrange(s,0,-1):
        if len(com)>0 and com[-1]<i:continue
        com.append(i)
        dfs(s-i,com)
        del com[-1]
    return ans

while 1:
    n=int(raw_input())
    if n==0:break
    ans=[]
    com=[]
    ans=dfs(n,com)
    for i in xrange(len(ans)):
        print(ans[i])
Buggy python submission (verdict: Time Limit Exceeded):
while 1:
    m=input()
    if m==0:break
    st=[map(int,raw_input().split()) for _ in xrange(m)]
    n=input()
    pi=[map(int,raw_input().split()) for _ in xrange(n)]
    d=[[] for _ in xrange(m)]
    for i in xrange(m):
        for j in xrange(m):
            if i==j:continue
            d[i].append([st[j][0]-st[i][0],st[j][1]-st[i][1]])
    f=0
    for i in xrange(n):
        if f==1:break
        mo=[]
        f=0
        for j in xrange(n):
            mo.append([pi[j][0]-pi[i][0],pi[j][1]-pi[i][1]])
        for k in xrange(m):
            cnt=0
            for l in d[k]:
                if not l in mo:continue
                else:cnt+=1
                if cnt==len(d[k]):
                    print pi[i][0]-st[k][0],pi[i][1]-st[k][1]
                    f=1
                    break
            if f==1:break

Fixed submission:
while 1:
    m=int(raw_input())
    if m==0:break
    st=[map(int,raw_input().split()) for _ in xrange(m)]
    n=int(raw_input())
    pi=[map(int,raw_input().split()) for _ in xrange(n)]
    d=[[st[i][0]-st[0][0],st[i][1]-st[0][1]] for i in xrange(1,m)]
    for i in xrange(n):
        mo=[[pi[j][0]-pi[i][0],pi[j][1]-pi[i][1]] for j in xrange(n)]
        cnt=0
        cnt1=0
        for k in xrange(m-1):
            if not d[k] in mo:break
            else:cnt+=1
        if cnt==m-1:
            print pi[i][0]-st[0][0],pi[i][1]-st[0][1]
            break

Buggy python submission (verdict: Time Limit Exceeded):
while 1:
    m=int(raw_input())
    if m==0:break
    st=[map(int,raw_input().split()) for _ in xrange(m)]
    n=int(raw_input())
    pi=[map(int,raw_input().split()) for _ in xrange(n)]
    d=[[st[i][0]-st[0][0],st[i][1]-st[0][1]] for i in xrange(1,m)]
    for i in xrange(n):
        mo=[[pi[j][0]-pi[i][0],pi[j][1]-pi[i][1]] for j in xrange(n)]
        cnt=0
        for k in xrange(m-1):
            if not d[k] in mo:continue
            else:cnt+=1
        if cnt==m-1:
            print pi[i][0]-st[0][0],pi[i][1]-st[0][1]
            break

Fixed submission:
while 1:
    m=int(raw_input())
    if m==0:break
    st=[map(int,raw_input().split()) for _ in xrange(m)]
    n=int(raw_input())
    pi=[map(int,raw_input().split()) for _ in xrange(n)]
    d=[[st[i][0]-st[0][0],st[i][1]-st[0][1]] for i in xrange(1,m)]
    for i in xrange(n):
        mo=[[pi[j][0]-pi[i][0],pi[j][1]-pi[i][1]] for j in xrange(n)]
        cnt=0
        cnt1=0
        for k in xrange(m-1):
            if not d[k] in mo:break
            else:cnt+=1
        if cnt==m-1:
            print pi[i][0]-st[0][0],pi[i][1]-st[0][1]
            break
Buggy python submission (verdict: Time Limit Exceeded):
import copy

def culc(k):
    cnt_0=0
    cnt_1=0
    for i in xrange(r):
        if osenbei1[i][k]==1:
            cnt_1+=1
        else:
            cnt_0+=1
    return max(cnt_0,cnt_1)

def rev(j):
    for i in xrange(c):
        if osenbei1[j][i]==1:
            osenbei1[j][i]=0
        else:
            osenbei1[j][i]=1
while 1:
    r,c=map(int,raw_input().split())
    if r==c==0:break
    osenbei=[map(int,raw_input().split()) for _ in xrange(r)]
    r_cnt=[[0]*2 for _  in xrange(r)]
    c_cnt=[[0]*2 for _  in xrange(c)]
    dp=[[0]*(c+1) for _ in xrange(1<<r)]
    for i in xrange(1<<r):
        osenbei1=copy.deepcopy(osenbei)
        for j in xrange(r):
            if i&(1<<j):
                rev(j)
        for k in xrange(c):
            dp[i][k+1]=dp[i][k]+culc(k)
    ans=-1
    for i in xrange(1<<r):
        ans=max(ans,max(dp[i]))
    print(ans)

Fixed submission:
while 1:
    r,c=map(int,raw_input().split())
    if r==c==0:break
    osenbei=[map(int,raw_input().split()) for _ in xrange(r)]
    r_osenbei=[None]*c
    for i in xrange(c):
        st=""
        for j in xrange(r):
            st+=str(osenbei[j][i])
        r_osenbei[i]=int(st,2)
    ans=-1
    for i in xrange(1<<r):
        cnt=0
        for j in xrange(c):
            n=bin(i^r_osenbei[j]).count('1')
            cnt+=max(n,r-n)
        ans=max(ans,cnt)
    print(ans)
Buggy python submission (verdict: Runtime Error):
n,l=map(int,raw_input().split())
a=[0]+[int(raw_input()) for _ in xrange(n)]+[0]
cnt=0
while a!=[0]*(n+2):
    t_id=[]
    max_t=-1
    for i in xrange(1,n+1):
        if a[i-1]<a[i] and a[i]>a[i+1]:
            t_id.append(i)
            if max_t<a[i]:
                max_t=a[i]
    cnt+=l-max_t
    for i in t_id:
        a[i]+=l-max_t
        a[i]=a[i]%l
print(cnt)

Fixed submission:
import heapq

n,l=map(int,raw_input().split())
a=[[0,0]]+[[0,i] for i in xrange(n)]+[[0,n+1]]
for i in xrange(n):
    en=int(raw_input())
    a[i]=[-en,i]
pq=[]
for i in xrange(n+2):
    heapq.heappush(pq,a[i])
ti=[0]*(n+2)
while 1:
    p,i=heapq.heappop(pq)
    p=-p
    if p==0:break
    ti[i]+=l-p
    a[i][0]=0
    if a[i-1][0]!=0:
        ti[i-1]=max(ti[i-1],ti[i])
    if a[i+1][0]!=0:
        ti[i+1]=max(ti[i+1],ti[i])
print(max(ti))

Buggy python submission (verdict: Runtime Error):
n,l=map(int,raw_input().split())
a=[0]+[int(raw_input()) for _ in xrange(n)]+[0]
r=[0]*(n+2)
for i in xrange(1,n+1):
    if a[i-1]<a[i] and a[i]>a[i+1]:
        r[i]=0
    elif a[i-1]<a[i] and a[i]<a[i+1]:
        r[i]=1
    elif a[i-1]>a[i] and a[i]>a[i+1]:
        r[i]=1
    else:
        r[i]=2
time=[0]*(n+2)
while r!=[-1]*(n+2):
    for i in xrange(1,n+1):
        if r[i]==0:
            r[i]=-1
            time[i]+=l-a[i]
            time[i-1]=max(time[i-1],time[i])
            time[i+1]=max(time[i+1],time[i])
            if r[i-1]!=-1:r[i-1]-=1
            if r[i+1]!=-1:r[i+1]-=1
print(max(time))

Fixed submission:
import heapq

n,l=map(int,raw_input().split())
a=[[0,0]]+[[0,i] for i in xrange(n)]+[[0,n+1]]
for i in xrange(n):
    en=int(raw_input())
    a[i]=[-en,i]
pq=[]
for i in xrange(n+2):
    heapq.heappush(pq,a[i])
ti=[0]*(n+2)
while 1:
    p,i=heapq.heappop(pq)
    p=-p
    if p==0:break
    ti[i]+=l-p
    a[i][0]=0
    if a[i-1][0]!=0:
        ti[i-1]=max(ti[i-1],ti[i])
    if a[i+1][0]!=0:
        ti[i+1]=max(ti[i+1],ti[i])
print(max(ti))

Buggy python submission (verdict: Runtime Error):
n,l=map(int,raw_input().split())
a=[0]+[int(raw_input()) for _ in xrange(n)]+[0]
ti=[0]*(n+2)
while a!=[0]*(n+2):
    for i in xrange(1,n+1):
        if a[i-1]<a[i] and a[i]>a[i+1]:
            ti[i]+=l-a[i]
            a[i]=0
            if a[i-1]!=0:
                ti[i-1]=max(ti[i-1],ti[i])
            if a[i+1]!=0:
                ti[i+1]=max(ti[i+1],ti[i])
print(max(ti))

Fixed submission:
import heapq

n,l=map(int,raw_input().split())
a=[[0,0]]+[[0,i] for i in xrange(n)]+[[0,n+1]]
for i in xrange(n):
    en=int(raw_input())
    a[i]=[-en,i]
pq=[]
for i in xrange(n+2):
    heapq.heappush(pq,a[i])
ti=[0]*(n+2)
while 1:
    p,i=heapq.heappop(pq)
    p=-p
    if p==0:break
    ti[i]+=l-p
    a[i][0]=0
    if a[i-1][0]!=0:
        ti[i-1]=max(ti[i-1],ti[i])
    if a[i+1][0]!=0:
        ti[i+1]=max(ti[i+1],ti[i])
print(max(ti))
Buggy python submission (verdict: Runtime Error):
n,k=map(int,raw_input().split())
g=[[] for _ in xrange(10)]
for i in xrange(n):
    v,j=map(int,raw_input().split())
    g[j-1].append(v)
for i in xrange(10):
    g[i].sort()
    g[i].reverse()

books=[[0]*(k+1) for _ in xrange(10)]
for i in xrange(10):
    for j in xrange(1,len(g[i])+1):
        books[i][j]=books[i][j-1]+g[i][j-1]+2*(j-1)

dp=[[0]*(k+1) for _ in xrange(11)]
for i in xrange(10):
    for j in xrange(1,k+1):
        for l in xrange(j+1):
            dp[i+1][j]=max(dp[i+1][j],dp[i][j-l]+books[i][l])

print(dp[10][k])

Fixed submission:
n,k=map(int,raw_input().split())
g=[[] for _ in xrange(10)]
for i in xrange(n):
    v,j=map(int,raw_input().split())
    g[j-1].append(v)
for i in xrange(10):
    g[i].sort()
    g[i].reverse()

books=[[0]*2005 for _ in xrange(10)]
for i in xrange(10):
    for j in xrange(1,len(g[i])+1):
        books[i][j]=books[i][j-1]+g[i][j-1]+2*(j-1)

dp=[[0]*(k+1) for _ in xrange(11)]
for i in xrange(10):
    for j in xrange(1,k+1):
        for l in xrange(j+1):
            dp[i+1][j]=max(dp[i+1][j],dp[i][j-l]+books[i][l])

print(dp[10][k])
Buggy python submission (verdict: Runtime Error):
from collections import deque
import heapq

def dijkstra(s,g,m):
    color=[0]*n
    dis=[float('inf')]*n
    dis[s]=0
    heapq.heappush(pq,[0,s])
    while len(pq)!=0:
        t,u=heapq.heappop(pq)
        color[u]=2
        if dis[u]<t:
            continue
        for v in g[u]:
            if color[v]!=2:
                if dis[u]+m[u][v]<dis[v]:
                    dis[v]=dis[u]+m[u][v]
                    color[v]=1
                    heapq.heappush(pq,[dis[v],v])
    return dis

n,k=map(int,raw_input().split())
cr=[map(int,raw_input().split()) for _ in xrange(n)]
g=[[]*n for _ in xrange(n)]
m=[[0]*n for _ in xrange(n)]
for i in xrange(k):
    a,b=map(int,raw_input().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)
    m[a-1][b-1]=1
    m[b-1][a-1]=1

pq=[]
d=[[]*n for _ in xrange(n)]
for i in xrange(n):
    d[i]=dijkstra(i,g,m)

cost=[[float('inf')]*n for _ in xrange(n)]
newg=[[]*n for _ in xrange(n)]
for i in xrange(n):
    for j in xrange(n):
        if d[i][j]<=cr[i][1]:
            cost[i][j]=cr[i][0]
            newg[i].append(j)

pq=[]
dis=dijkstra(0,newg,cost)
print(dis[n-1])

Fixed submission:
from collections import deque
import heapq

def dijkstra(s,g,m):
    color=[0]*n
    dis=[float('inf')]*n
    dis[s]=0
    heapq.heappush(pq,[0,s])
    while len(pq)!=0:
        t,u=heapq.heappop(pq)
        color[u]=2
        if dis[u]<t:
            continue
        for v in g[u]:
            if color[v]!=2:
                if dis[u]+m[u][v]<dis[v]:
                    dis[v]=dis[u]+m[u][v]
                    color[v]=1
                    heapq.heappush(pq,[dis[v],v])
    return dis

n,k=map(int,raw_input().split())
cr=[map(int,raw_input().split()) for _ in xrange(n)]
g=[[]*n for _ in xrange(n)]
for i in xrange(k):
    a,b=map(int,raw_input().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)

d=[[0]*n for _ in xrange(n)]
cost=[[float('inf')]*n for _ in xrange(n)]
newg=[[]*n for _ in xrange(n)]
for i in xrange(n):
    q=deque([i])
    visited=[0]*n
    visited[i]=1
    while len(q)!=0:
        p=q.popleft()
        for j in g[p]:
            if visited[j]==0:
                visited[j]=1
                d[i][j]=d[i][p]+1
                if d[i][j]<=cr[i][1]:
                    cost[i][j]=cr[i][0]
                    newg[i].append(j)
                    q.append(j)

pq=[]
dis=dijkstra(0,newg,cost)
print(dis[n-1])
Buggy python submission (verdict: Runtime Error):
n,m=map(int,raw_input().split())
p=map(int,raw_input().split())
abc=[map(int,raw_input().split()) for _ in xrange(n-1)]
cnt=[0]*(n-1)
for i in xrange(m-1):
    p[i]-=1
    p[i+1]-=1
    if p[i]<p[i+1]:
        for j in xrange(p[i],p[i+1]):
            cnt[j]+=1
    else:
        for j in xrange(p[i],p[i+1],-1):
            cnt[j-1]+=1
    p[i]+=1
    p[i+1]+=1

ans=0
for i in xrange(n-1):
    ans+=min(abc[i][0]*cnt[i],abc[i][1]*cnt[i]+abc[i][2])
print(ans)

Fixed submission:
n,m=map(int,raw_input().split())
p=map(int,raw_input().split())
abc=[map(int,raw_input().split()) for _ in xrange(n-1)]
cnt=[0]*n

for i in xrange(m-1):
    p[i]-=1
    p[i+1]-=1
    if p[i]<p[i+1]:
        cnt[p[i]]+=1
        cnt[p[i+1]]-=1
    else:
        cnt[p[i]]-=1
        cnt[p[i+1]]+=1
    p[i]+=1
    p[i+1]+=1
    
for i in xrange(n-1):
    cnt[i+1]+=cnt[i]
    
ans=0
for i in xrange(n-1):
    ans+=min(abc[i][0]*cnt[i],abc[i][1]*cnt[i]+abc[i][2])
    
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def isPrime(x):
    if x==2:
        return True
    if x<2 or x%2==0:
        return False
    i=3
    while i*2<=x:
        if x%i==0:
            return False
        i+=2
    return  True

n=input()
ct=0
for i in xrange(n):
    if isPrime(input()):
        ct+=1
print(ct)

Fixed submission:
def isPrime(x):
    if x==2:
        return True
    if x<2 or x%2==0:
        return False
    if pow(2,x-1,x)==1:
        return True
    else:
        return False

n=input()
ct=0
for i in xrange(n):
    if isPrime(input()):
        ct+=1
print(ct)
Buggy python submission (verdict: Runtime Error):
def insert(x,a):
    a.insert(0,x)
    return a
def delete(x,a):
    a.remove(x)
    return a
def deletefirst(a):
    del a[0]
    return a
def deletelast(a):
    del a[-1]
    return a
n=input()
a=[]
for i in xrange(n):
    s=raw_input().split()
    if s[0]=="insert":
        insert(s[1],a)
    elif s[0]=="delete":
        delete(s[1],a)
    elif s[0]=="deleteFirst":
        deletefirst(a)
    else:
        deletelast(a)
print(" ".join(a))

Fixed submission:
from collections import deque
def insert(x,a):
    a.appendleft(x)
def delete(x,a):
    if x in a:
        a.remove(x)
    else:
        return
def deletefirst(a):
    a.popleft()
def deletelast(a):
    a.pop()
n=input()
a=deque()
for i in xrange(n):
    s=raw_input().split()
    if s[0]=="insert":
        insert(s[1],a)
    elif s[0]=="delete":
        delete(s[1],a)
    elif s[0]=="deleteFirst":
        deletefirst(a)
    else:
        deletelast(a)
print(" ".join(a))

Buggy python submission (verdict: Time Limit Exceeded):
def insert(x,a):
    a.insert(0,x)
    return a
def delete(x,a):
    if x in a:
        a.remove(x)
    else:
        return
    return a
def deletefirst(a):
    del a[0]
    return a
def deletelast(a):
    del a[-1]
    return a
n=input()
a=[]
for i in xrange(n):
    s=raw_input().split()
    if s[0]=="insert":
        insert(s[1],a)
    elif s[0]=="delete":
        delete(s[1],a)
    elif s[0]=="deleteFirst":
        deletefirst(a)
    else:
        deletelast(a)
print(" ".join(a))

Fixed submission:
from collections import deque
def insert(x,a):
    a.appendleft(x)
def delete(x,a):
    if x in a:
        a.remove(x)
    else:
        return
def deletefirst(a):
    a.popleft()
def deletelast(a):
    a.pop()
n=input()
a=deque()
for i in xrange(n):
    s=raw_input().split()
    if s[0]=="insert":
        insert(s[1],a)
    elif s[0]=="delete":
        delete(s[1],a)
    elif s[0]=="deleteFirst":
        deletefirst(a)
    else:
        deletelast(a)
print(" ".join(a))

Buggy python submission (verdict: Time Limit Exceeded):
def insert(x,a):
    a.insert(0,x)
def delete(x,a):
    if x in a:
        a.remove(x)
    else:
        return
def deletefirst(a):
    del a[0]
def deletelast(a):
    del a[-1]
n=input()
a=[]
for i in xrange(n):
    s=raw_input().split()
    if s[0]=="insert":
        insert(s[1],a)
    elif s[0]=="delete":
        delete(s[1],a)
    elif s[0]=="deleteFirst":
        deletefirst(a)
    else:
        deletelast(a)
print(" ".join(a))

Fixed submission:
from collections import deque
def insert(x,a):
    a.appendleft(x)
def delete(x,a):
    if x in a:
        a.remove(x)
    else:
        return
def deletefirst(a):
    a.popleft()
def deletelast(a):
    a.pop()
n=input()
a=deque()
for i in xrange(n):
    s=raw_input().split()
    if s[0]=="insert":
        insert(s[1],a)
    elif s[0]=="delete":
        delete(s[1],a)
    elif s[0]=="deleteFirst":
        deletefirst(a)
    else:
        deletelast(a)
print(" ".join(a))