Buggy python submission (verdict: Runtime Error):

import sys
import itertools

class Treasure:
    def __init__(self, value, weight):
        self.value = value
        self.weight = weight


def solv(treasures, w_limit):
    comb = t_combinations(treasures)
    ts = filter(lambda x: t_sum_weight(x) <= w_limit, comb)
    max_value = max(map(lambda x: t_sum_value(x), ts))
    ts2 = filter(lambda x: t_sum_value(x) == max_value, ts)
    if len(ts2) == 1:
        return (ts2[0].value, ts2[0].weight)
    else:
        rt = rightest(ts2)
        return (t_sum_value(rt), t_sum_weight(rt))


def t_combinations(t):
    l = len(t)
    r = []
    for x in range(1, l+1):
        for y in itertools.combinations(t, x):
            r.append(y)
    return r


def t_sum_weight(t):
    return sum(map(lambda x: x.weight, t))


def t_sum_value(t):
    return sum(map(lambda x: x.value, t))


def rightest(ts):
    return reduce(lambda a, b: take_right(a, b), ts)


def take_right(a, b):
    if t_sum_weight(a) <= t_sum_weight(b):
        return a
    else:
        return b


case = 0

while True:
    weight_limit = int(sys.stdin.readline())
    case += 1
    if weight_limit == 0:
        exit()
    n = int(sys.stdin.readline())
    treasures = []
    for i in range(n):
        v, w = map(int, sys.stdin.readline().split(','))
        treasures.append(Treasure(v, w))
    value, weight = solv(treasures, weight_limit)
    print 'Case ' + str(case) + ':'
    print value
    print weight

Fixed submission:

import sys
import itertools


class Treasure:
    def __init__(self, value, weight):
        self.value = value
        self.weight = weight



def solv(treasures, w_limit):
    dp = []
    dp.append(0)
    for n in range(w_limit):
        dp.append(-1)
    for i in treasures:
        for j in range(w_limit - i.weight, -1, -1):
            if dp[j] >= 0:
                dp[j + i.weight] = max(dp[j + i.weight], dp[j] + i.value)
    p = 0
    q = 0
    for i in range(w_limit + 1):
        if dp[i] > p:
            p = dp[i]
            q = i
    return (p, q)






case = 0

while True:
    weight_limit = int(sys.stdin.readline())
    case += 1
    if weight_limit == 0:
        exit()
    n = int(sys.stdin.readline())
    treasures = []
    for i in range(n):
        v, w = map(int, sys.stdin.readline().split(','))
        treasures.append(Treasure(v, w))
    value, weight = solv(treasures, weight_limit)
    print 'Case ' + str(case) + ':'
    print value
    print weight

Buggy python submission (verdict: Runtime Error):

import sys
import itertools


class Treasure:
    def __init__(self, value, weight):
        self.value = value
        self.weight = weight



def solv(treasures, w_limit):
    comb = t_combinations(treasures)
    ts = filter(lambda x: t_sum_weight(x) <= w_limit, comb)
    max_value = max(map(lambda x: t_sum_value(x), ts))
    ts2 = filter(lambda x: t_sum_value(x) == max_value, ts)
    if len(ts2) == 1:
        return (ts2[0].value, ts2[0].weight)
    else:
        rt = rightest(ts2)
        return (t_sum_value(rt), t_sum_weight(rt))


def t_combinations(t):
    l = len(t)
    r = []
    for x in range(1, l+1):
        for y in itertools.combinations(t, x):
            r.append(y)
    return r


def t_sum_weight(ts):
    return sum(map(lambda x: x.weight, ts))


def t_sum_value(ts):
    return sum(map(lambda x: x.value, ts))


def rightest(tss):
    return reduce(lambda a, b: take_right(a, b), tss)


def take_right(a, b):
    if t_sum_weight(a) <= t_sum_weight(b):
        return a
    else:
        return b


case = 0

while True:
    weight_limit = int(sys.stdin.readline())
    case += 1
    if weight_limit == 0:
        exit()
    n = int(sys.stdin.readline())
    treasures = []
    for i in range(n):
        v, w = map(int, sys.stdin.readline().split(','))
        treasures.append(Treasure(v, w))
    value, weight = solv(treasures, weight_limit)
    print 'Case ' + str(case) + ':'
    print value
    print weight

Fixed submission:

import sys
import itertools


class Treasure:
    def __init__(self, value, weight):
        self.value = value
        self.weight = weight



def solv(treasures, w_limit):
    dp = []
    dp.append(0)
    for n in range(w_limit):
        dp.append(-1)
    for i in treasures:
        for j in range(w_limit - i.weight, -1, -1):
            if dp[j] >= 0:
                dp[j + i.weight] = max(dp[j + i.weight], dp[j] + i.value)
    p = 0
    q = 0
    for i in range(w_limit + 1):
        if dp[i] > p:
            p = dp[i]
            q = i
    return (p, q)






case = 0

while True:
    weight_limit = int(sys.stdin.readline())
    case += 1
    if weight_limit == 0:
        exit()
    n = int(sys.stdin.readline())
    treasures = []
    for i in range(n):
        v, w = map(int, sys.stdin.readline().split(','))
        treasures.append(Treasure(v, w))
    value, weight = solv(treasures, weight_limit)
    print 'Case ' + str(case) + ':'
    print value
    print weight

Buggy python submission (verdict: Time Limit Exceeded):

import sys
import itertools


class Treasure:
    def __init__(self, value, weight):
        self.value = value
        self.weight = weight



def solv(treasures, w_limit):
    tvw = (0, 0)
    for ts in t_combinations(treasures):
        w = t_sum_weight(ts)
        if w <= w_limit:
            v = t_sum_value(ts)
            if tvw[0] < v:
                 tvw = (v, w)
            elif tvw[0] == v:
                 if w < tvw[1]:
                     tvw = (v, w)
    return tvw


def t_combinations(t):
    l = len(t)
    for x in range(1, l+1):
        for y in itertools.combinations(t, x):
            yield y


def t_sum_weight(ts):
    return sum(map(lambda x: x.weight, ts))


def t_sum_value(ts):
    return sum(map(lambda x: x.value, ts))


def rightest(tss):
    return reduce(lambda a, b: take_right(a, b), tss)


def take_right(a, b):
    if t_sum_weight(a) <= t_sum_weight(b):
        return a
    else:
        return b


case = 0

while True:
    weight_limit = int(sys.stdin.readline())
    case += 1
    if weight_limit == 0:
        exit()
    n = int(sys.stdin.readline())
    treasures = []
    for i in range(n):
        v, w = map(int, sys.stdin.readline().split(','))
        treasures.append(Treasure(v, w))
    value, weight = solv(treasures, weight_limit)
    print 'Case ' + str(case) + ':'
    print value
    print weight

Fixed submission:

import sys
import itertools


class Treasure:
    def __init__(self, value, weight):
        self.value = value
        self.weight = weight



def solv(treasures, w_limit):
    dp = []
    dp.append(0)
    for n in range(w_limit):
        dp.append(-1)
    for i in treasures:
        for j in range(w_limit - i.weight, -1, -1):
            if dp[j] >= 0:
                dp[j + i.weight] = max(dp[j + i.weight], dp[j] + i.value)
    p = 0
    q = 0
    for i in range(w_limit + 1):
        if dp[i] > p:
            p = dp[i]
            q = i
    return (p, q)






case = 0

while True:
    weight_limit = int(sys.stdin.readline())
    case += 1
    if weight_limit == 0:
        exit()
    n = int(sys.stdin.readline())
    treasures = []
    for i in range(n):
        v, w = map(int, sys.stdin.readline().split(','))
        treasures.append(Treasure(v, w))
    value, weight = solv(treasures, weight_limit)
    print 'Case ' + str(case) + ':'
    print value
    print weight

Buggy python submission (verdict: Time Limit Exceeded):

import sys
import itertools


class Treasure:
    def __init__(self, value, weight):
        self.value = value
        self.weight = weight



def solv(treasures, w_limit):
    treasures.sort(lambda a, b: cmp(b.value, a.value))
    tvw = (0, 0)
    for ts in t_combinations(treasures):
        w = t_sum_weight(ts)
        if w <= w_limit:
            v = t_sum_value(ts)
            if tvw[0] < v:
                 tvw = (v, w)
            elif tvw[0] == v:
                 if w < tvw[1]:
                     tvw = (v, w)
    return tvw


def t_combinations(t):
    l = len(t)
    for x in range(l, 0, -1):
        for y in itertools.combinations(t, x):
            yield y


def t_sum_weight(ts):
    return sum(map(lambda x: x.weight, ts))


def t_sum_value(ts):
    return sum(map(lambda x: x.value, ts))


def rightest(tss):
    return reduce(lambda a, b: take_right(a, b), tss)


def take_right(a, b):
    if t_sum_weight(a) <= t_sum_weight(b):
        return a
    else:
        return b


case = 0

while True:
    weight_limit = int(sys.stdin.readline())
    case += 1
    if weight_limit == 0:
        exit()
    n = int(sys.stdin.readline())
    treasures = []
    for i in range(n):
        v, w = map(int, sys.stdin.readline().split(','))
        treasures.append(Treasure(v, w))
    value, weight = solv(treasures, weight_limit)
    print 'Case ' + str(case) + ':'
    print value
    print weight

Fixed submission:

import sys
import itertools


class Treasure:
    def __init__(self, value, weight):
        self.value = value
        self.weight = weight



def solv(treasures, w_limit):
    dp = []
    dp.append(0)
    for n in range(w_limit):
        dp.append(-1)
    for i in treasures:
        for j in range(w_limit - i.weight, -1, -1):
            if dp[j] >= 0:
                dp[j + i.weight] = max(dp[j + i.weight], dp[j] + i.value)
    p = 0
    q = 0
    for i in range(w_limit + 1):
        if dp[i] > p:
            p = dp[i]
            q = i
    return (p, q)






case = 0

while True:
    weight_limit = int(sys.stdin.readline())
    case += 1
    if weight_limit == 0:
        exit()
    n = int(sys.stdin.readline())
    treasures = []
    for i in range(n):
        v, w = map(int, sys.stdin.readline().split(','))
        treasures.append(Treasure(v, w))
    value, weight = solv(treasures, weight_limit)
    print 'Case ' + str(case) + ':'
    print value
    print weight