Buggy python submission (verdict: Runtime Error):
import copy
N, W = [int(i) for i in input().split(' ')]
dic = {}

for i in range(N):
    new_w, new_v = [int(i) for i in input().split(' ')]
    if new_w in dic:
        dic[new_w].append(new_v)
    else:
        dic[new_w] = [new_v]
for key in dic.keys():
    dic[new_w].sort(reverse=True)
w = list(dic.keys())

def rec(i, j, _dic):
    if w[i]>j:
        return 0
    cand = []
    if len(_dic[w[i]])>=1:
        new_dic = copy.deepcopy(_dic)
        val = new_dic[w[i]].pop(0)
        cand.append(val+rec(i, j-w[i], new_dic))
    if i+1<len(w):
        cand.append(rec(i+1,j, _dic))
    return max(cand)

print(rec(0, W, dic))

Fixed submission:
import copy
N, W = [int(i) for i in input().split(' ')]
dic = {}
dic_num = {}
dic4calc  = {}

for i in range(N):
    new_w, new_v = [int(i) for i in input().split(' ')]
    if new_w in dic:
        dic[new_w].append(new_v)
    else:
        dic[new_w] = [new_v]
        
w = sorted(list(dic.keys()))
for wi in w:
    dic[wi].sort(reverse=True)
    dic_num[wi] = len(dic[wi])
    dic4calc[wi] = 0

cond = []
def rec(i, rest, dic4calc):
    if rest<w[i]:
        cond.append(dic4calc)
        return

    if i+1<len(w):
        new_dic4calc = copy.deepcopy(dic4calc)
        rec(i+1, rest, new_dic4calc)
    if not dic_num[w[i]]-dic4calc[w[i]]==0:
        new_dic4calc = copy.deepcopy(dic4calc)
        new_dic4calc[w[i]] += 1
        rec(i, rest-w[i], new_dic4calc)
    if i+1>=len(w):
        cond.append(dic4calc)
        return

rec(0, W, dic4calc)
ans = 0
for c in cond:
    total = 0
    for wi in w:
        total += sum(dic[wi][:c[wi]])
    if ans < total:
        ans = total
        
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, W = [int(i) for i in input().split(' ')]
w = []
v = []
for i in range(N):
    new_w, new_v = [int(i) for i in input().split(' ')]
    w.append(new_w)
    v.append(new_v)
    
dp = [[-1 for j in range(W+1)] for i in range(N)]
for j in range(W+1):
    if w[0]<=j:
        dp[0][j] = v[0]
    else:
        dp[0][j] = 0

def rec(i, j):
    if not dp[i][j] == -1:
        return dp[i][j]
    if w[i]<=j:
        dp[i][j] = max(rec(i-1, j), rec(i-1, j-w[i])+v[i])
        #print(i, j, rec(i-1, j), rec(i-1, j-w[i])+v[i])
        return dp[i][j]
    else:
        dp[i][j] = rec(i-1, j)
        return dp[i][j]

print(rec(N-1,W))

Fixed submission:
import copy
N, W = [int(i) for i in input().split(' ')]
dic = {}
dic_num = {}
dic4calc  = {}

for i in range(N):
    new_w, new_v = [int(i) for i in input().split(' ')]
    if new_w in dic:
        dic[new_w].append(new_v)
    else:
        dic[new_w] = [new_v]
        
w = sorted(list(dic.keys()))
for wi in w:
    dic[wi].sort(reverse=True)
    dic_num[wi] = len(dic[wi])
    dic4calc[wi] = 0

cond = []
def rec(i, rest, dic4calc):
    if rest<w[i]:
        cond.append(dic4calc)
        return

    if i+1<len(w):
        new_dic4calc = copy.deepcopy(dic4calc)
        rec(i+1, rest, new_dic4calc)
    if not dic_num[w[i]]-dic4calc[w[i]]==0:
        new_dic4calc = copy.deepcopy(dic4calc)
        new_dic4calc[w[i]] += 1
        rec(i, rest-w[i], new_dic4calc)
    if i+1>=len(w):
        cond.append(dic4calc)
        return

rec(0, W, dic4calc)
ans = 0
for c in cond:
    total = 0
    for wi in w:
        total += sum(dic[wi][:c[wi]])
    if ans < total:
        ans = total
        
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, W = [int(i) for i in input().split(' ')]
w = []
v = []
for i in range(N):
    new_w, new_v = [int(i) for i in input().split(' ')]
    w.append(new_w)
    v.append(new_v)
    
dp = [[0 for j in range(W+1)] for i in range(N)]
for j in range(W+1):
    if w[0]<=j:
        dp[0][j] = v[0]
    
for i in range(1,N):
    for j in range(W+1):
        if w[i]<=j:
            dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]] + v[i])
        else:
            dp[i][j] = dp[i-1][j]
print(dp[N-1][W])

Fixed submission:
import copy
N, W = [int(i) for i in input().split(' ')]
dic = {}
dic_num = {}
dic4calc  = {}

for i in range(N):
    new_w, new_v = [int(i) for i in input().split(' ')]
    if new_w in dic:
        dic[new_w].append(new_v)
    else:
        dic[new_w] = [new_v]
        
w = sorted(list(dic.keys()))
for wi in w:
    dic[wi].sort(reverse=True)
    dic_num[wi] = len(dic[wi])
    dic4calc[wi] = 0

cond = []
def rec(i, rest, dic4calc):
    if rest<w[i]:
        cond.append(dic4calc)
        return

    if i+1<len(w):
        new_dic4calc = copy.deepcopy(dic4calc)
        rec(i+1, rest, new_dic4calc)
    if not dic_num[w[i]]-dic4calc[w[i]]==0:
        new_dic4calc = copy.deepcopy(dic4calc)
        new_dic4calc[w[i]] += 1
        rec(i, rest-w[i], new_dic4calc)
    if i+1>=len(w):
        cond.append(dic4calc)
        return

rec(0, W, dic4calc)
ans = 0
for c in cond:
    total = 0
    for wi in w:
        total += sum(dic[wi][:c[wi]])
    if ans < total:
        ans = total
        
print(ans)