Buggy python submission (verdict: Runtime Error):
num=1
maxw=input()
while maxw<>0:
    N=input()
    ws,ps=[],[]
    for i in range(N):
        tmp=input()
        ws.append(tmp[1])
        ps.append(tmp[0])
    dp=[[0]*(maxw+1) for i in range(len(ws)+1)]
    ret=0
    for i in range(len(ws)):
        for j in range(maxw+1):
            if (j+ws[i])<=maxw:
                dp[i+1][j+ws[i]]=max(dp[i+1][j+ws[i]],dp[i][j]+ps[i])
                ret=max(dp[i+1][j+ws[i]],ret)
    print "Case %d:"%num
    print ret
    print dp[len(ws)].index(ret)
    num+=1
    maxw=input()

Fixed submission:
num=1
while True:
    maxw=input()
    if maxw==0:break
    print "Case %d:"%num
    dp,w,v=[0]*1001,[0]*1001,[0]*1001
    n=input()
    for i in xrange(n):
        v[i],w[i]=map(int,raw_input().strip().split(","))
    for i in xrange(n):
        for j in xrange(maxw,-1,-1):
            if j+w[i]<=maxw:
                dp[j+w[i]]=max(dp[j+w[i]],dp[j]+v[i])
    print max(dp)
    print dp.index(max(dp))
    num+=1

Buggy python submission (verdict: Runtime Error):
num=1
while True:
    maxw=input()
    if maxw==0:break
    N=input()
    ws,ps=[0]*N,[0]*N
    for i in xrange(N):
        ps[i],ws[i]=map(int,raw_input().strip().split(","))
    dp=[[0]*(maxw+1) for i in range(len(ws)+1)]
    ret=0
    for i in range(len(ws)):
        for j in range(maxw+1):
            if (j+ws[i])<=maxw:
                dp[i+1][j+ws[i]]=max(dp[i+1][j+ws[i]],dp[i][j]+ps[i])
                ret=max(dp[i+1][j+ws[i]],ret)
    print "Case %d:"%num
    print ret
    print dp[len(ws)].index(ret)
    num+=1

Fixed submission:
num=1
while True:
    maxw=input()
    if maxw==0:break
    print "Case %d:"%num
    dp,w,v=[0]*1001,[0]*1001,[0]*1001
    n=input()
    for i in xrange(n):
        v[i],w[i]=map(int,raw_input().strip().split(","))
    for i in xrange(n):
        for j in xrange(maxw,-1,-1):
            if j+w[i]<=maxw:
                dp[j+w[i]]=max(dp[j+w[i]],dp[j]+v[i])
    print max(dp)
    print dp.index(max(dp))
    num+=1

Buggy python submission (verdict: Runtime Error):
num=1
while True:
    maxw=input()
    if maxw==0:break
    N=input()
    ws,ps=[0]*N,[0]*N
    for i in xrange(N):
        ps[i],ws[i]=map(int,raw_input().strip().split(","))
    dp=[[0]*(maxw+1) for i in range(len(ws)+1)]
    ret=0
    for i in range(len(ws)):
        for j in range(maxw+1):
            if (j+ws[i])<=maxw:
                dp[i+1][j+ws[i]]=max(dp[i+1][j+ws[i]],dp[i][j]+ps[i])
                ret=max(dp[i+1][j+ws[i]],ret)
    print "Case {}:".format(num)
    print ret
    print dp[len(ws)].index(ret)
    num+=1

Fixed submission:
num=1
while True:
    maxw=input()
    if maxw==0:break
    print "Case %d:"%num
    dp,w,v=[0]*1001,[0]*1001,[0]*1001
    n=input()
    for i in xrange(n):
        v[i],w[i]=map(int,raw_input().strip().split(","))
    for i in xrange(n):
        for j in xrange(maxw,-1,-1):
            if j+w[i]<=maxw:
                dp[j+w[i]]=max(dp[j+w[i]],dp[j]+v[i])
    print max(dp)
    print dp.index(max(dp))
    num+=1

Buggy python submission (verdict: Runtime Error):
num=1
while True:
    maxw=input()
    if maxw==0:break
    N=input()
    ws,ps=[0]*N,[0]*N
    for i in range(N):
        ps[i],ws[i]=map(int,raw_input().strip().split(","))
    #dp=[[0]*(maxw+1) for i in range(N+1)]
    ret=0
    for i in range(N):
        for j in range(maxw+1):
            if (j+ws[i])<=maxw:
                dp[i+1][j+ws[i]]=max(dp[i+1][j+ws[i]],dp[i][j]+ps[i])
                ret=max(dp[i+1][j+ws[i]],ret)
    print "Case %d:"%num
    print ret
    print dp[len(ws)].index(ret)
    num+=1

Fixed submission:
num=1
while True:
    maxw=input()
    if maxw==0:break
    print "Case %d:"%num
    dp,w,v=[0]*1001,[0]*1001,[0]*1001
    n=input()
    for i in xrange(n):
        v[i],w[i]=map(int,raw_input().strip().split(","))
    for i in xrange(n):
        for j in xrange(maxw,-1,-1):
            if j+w[i]<=maxw:
                dp[j+w[i]]=max(dp[j+w[i]],dp[j]+v[i])
    print max(dp)
    print dp.index(max(dp))
    num+=1