Problem:


Score : 400 points

Problem Statement

You have N items and a bag of strength W.
The i-th item has a weight of w_i and a value of v_i.

You will select some of the items and put them in the bag.
Here, the total weight of the selected items needs to be at most W.

Your objective is to maximize the total value of the selected items.

Constraints

1 ≤ N ≤ 100

1 ≤ W ≤ 10^9

1 ≤ w_i ≤ 10^9

For each i = 2,3,...,N, w_1 ≤ w_i ≤ w_1 + 3.

1 ≤ v_i ≤ 10^7

W, each w_i and v_i are integers.

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible total value of the selected items.

Sample Input 1

4 6
2 1
3 4
4 10
3 4

Sample Output 1

11

The first and third items should be selected.

Sample Input 2

4 6
2 1
3 7
4 10
3 6

Sample Output 2

13

The second and fourth items should be selected.

Sample Input 3

4 10
1 100
1 100
1 100
1 100

Sample Output 3

400

You can take everything.

Sample Input 4

4 1
10 100
10 100
10 100
10 100

Sample Output 4

0

You can take nothing.



Sample input:
4 6
2 1
3 4
4 10
3 4


Sample output:
11

Submission status: Wrong Answer

Solution code:
def func(n, W, w):  # ナップザック問題の亜種
    _max = 0
    weights = [[0] for _ in range(4)]
    for i in range(n):
        weights[w[i][0]-w[0][0]].append(w[i][1])
    for i in range(len(weights)):
        weights[i].sort()
        for j in range(1, len(weights[i])):
            weights[i][j] += weights[i][j-1]
    # print(weights)
    for i in range(len(weights[0])):
        # print("i:{}".format(i))
        for j in range(len(weights[1])):
            # print("j:{}".format(j))
            for k in range(len(weights[2])):
                for l in range(len(weights[3])):
                    # 「i+j+k+l < n」つ選ぶ
                    if w[0][0]*i + (w[0][0]+1)*j + (w[0][0]+2)*k+(w[0][0]+3)*l <= W:
                        # print("i:{} j:{} k:{} l:{} score:{}".format(i, j, k, l, weights[0][i]+weights[1][
                        # j]+weights[2][k]+weights[3][l]))
                        _max = max(_max, weights[0][i]+weights[1][j]+weights[2][k]+weights[3][l])
    return _max


n, W = [int(i) for i in input().split()]
w = [[0, 0] for _ in range(n)]
for i in range(n):
    w[i] = [int(i) for i in input().split()]
print(func(n, W, w))
"""
# print("print(\" vs \" + str(func({}, {}, {})))".format(n, W, w))
print("11 vs " + str(func(4, 6, [[2, 1], [3, 4], [4, 10], [3, 4]])))
print("13 vs " + str(func(4, 6, [[2, 1], [3, 7], [4, 10], [3, 6]])))
print("400 vs " + str(func(4, 10, [[1, 100], [1, 100], [1, 100], [1, 100]])))
print("0 vs " + str(func(4, 1, [[10, 100], [10, 100], [10, 100], [10, 100]])))
"""

Submission status: Wrong Answer

Solution code:
def func(n, W, w):  # ナップザック問題の亜種
    _max = 0
    weights = [[0] for _ in range(4)]
    for i in range(n):
        weights[w[i][0]-w[0][0]].append(w[i][1])
    for i in range(len(weights)):
        weights[i].sort()
        for j in range(1, len(weights[i])):
            weights[i][j] += weights[i][j-1]
    # print(weights)
    for i in range(min(n+1, len(weights[0]))):
        # print("i:{}".format(i))
        for j in range(len(weights[1])):
            # print("j:{}".format(j))
            for k in range(len(weights[2])):
                for l in range(len(weights[3])):
                    # 「i+j+k+l < n」つ選ぶ
                    if w[0][0]*i + (w[0][0]+1)*j + (w[0][0]+2)*k+(w[0][0]+3)*l <= W:
                        # print("i:{} j:{} k:{} l:{} score:{}".format(i, j, k, l, weights[0][i]+weights[1][
                        # j]+weights[2][k]+weights[3][l]))
                        _max = max(_max, weights[0][i]+weights[1][j]+weights[2][k]+weights[3][l])
    return _max


n, W = [int(i) for i in input().split()]
w = [[0, 0] for _ in range(n)]
for i in range(n):
    w[i] = [int(i) for i in input().split()]
print(func(n, W, w))
"""
# print("print(\" vs \" + str(func({}, {}, {})))".format(n, W, w))
print("11 vs " + str(func(4, 6, [[2, 1], [3, 4], [4, 10], [3, 4]])))
print("13 vs " + str(func(4, 6, [[2, 1], [3, 7], [4, 10], [3, 6]])))
print("400 vs " + str(func(4, 10, [[1, 100], [1, 100], [1, 100], [1, 100]])))
print("0 vs " + str(func(4, 1, [[10, 100], [10, 100], [10, 100], [10, 100]])))
"""

Submission status: Wrong Answer

Solution code:
def func(n, W, w):  # ナップザック問題の亜種
    _max = 0
    weights = [[0] for _ in range(4)]
    for i in range(n):
        weights[w[i][0]-w[0][0]].append(w[i][1])
    for i in range(len(weights)):
        weights[i].sort()
        for j in range(1, len(weights[i])):
            weights[i][j] += weights[i][j-1]
    # print(weights)
    for i in range(min(n+1, len(weights[0]))):
        # print("i:{}".format(i))
        for j in range(min(n+1-i, len(weights[1]))):
            # print("j:{}".format(j))
            for k in range(min(n+1-i-j, len(weights[2]))):
                for l in range(min(n+1-i-j-k, len(weights[3]))):
                    # 「i+j+k+l < n」つ選ぶ
                    if w[0][0]*i + (w[0][0]+1)*j + (w[0][0]+2)*k+(w[0][0]+3)*l <= W:
                        # print("i:{} j:{} k:{} l:{} score:{}".format(i, j, k, l, weights[0][i]+weights[1][
                        # j]+weights[2][k]+weights[3][l]))
                        _max = max(_max, weights[0][i]+weights[1][j]+weights[2][k]+weights[3][l])
    return _max


n, W = [int(i) for i in input().split()]
w = [[0, 0] for _ in range(n)]
for i in range(n):
    w[i] = [int(i) for i in input().split()]
print(func(n, W, w))
"""
# print("print(\" vs \" + str(func({}, {}, {})))".format(n, W, w))
print("11 vs " + str(func(4, 6, [[2, 1], [3, 4], [4, 10], [3, 4]])))
print("13 vs " + str(func(4, 6, [[2, 1], [3, 7], [4, 10], [3, 6]])))
print("400 vs " + str(func(4, 10, [[1, 100], [1, 100], [1, 100], [1, 100]])))
print("0 vs " + str(func(4, 1, [[10, 100], [10, 100], [10, 100], [10, 100]])))
"""

Submission status: Wrong Answer

Solution code:
def func(n, W, w):
    # ナップザック問題
    # 残り荷物 iつで,残り容量jの場合の最大価値
    dp = [[0 for _ in range(W+1)] for _ in range(n+1)]
    for i in range(1, len(dp)):
        for j in range(len(dp[0])):
            if j-w[-i][0] >= 0:
                dp[i][j] = max(dp[i-1][j-w[-i][0]] + w[-i][1], dp[i-1][j])
            else:
                dp[i][j] = dp[i-1][j]
    # for i in range(len(dp)):
        # print(dp[i])
    return dp[n][W]
n, W = [int(i) for i in input().split()]
w = [[0, 0] for _ in range(n)]
for i in range(n):
    w[i] = [int(i) for i in input().split()]
"""
# print("print(\" vs \" + str(func({}, {}, {})))".format(n, W, w))
print("11 vs " + str(func(4, 6, [[2, 1], [3, 4], [4, 10], [3, 4]])))
print("13 vs " + str(func(4, 6, [[2, 1], [3, 7], [4, 10], [3, 6]])))
print("400 vs " + str(func(4, 10, [[1, 100], [1, 100], [1, 100], [1, 100]])))
print("0 vs " + str(func(4, 1, [[10, 100], [10, 100], [10, 100], [10, 100]])))
"""

Submission status: Wrong Answer

Solution code:
N,W=map(int,input().split())
weight=[0]*N
value=[0]*N
for i in range(N):
    value[i],weight[i]=map(int,input().split())
dp=[[0 for i in range(W+1)] for j in range(N+1)]
for i in range(W+1):
    dp[0][i]=0

for i in range(N):
    for w in range(W+1):
        if weight[i]<=w:
            dp[i+1][w]=max(dp[i][w-weight[i]]+value[i], dp[i][w])
        else:
            dp[i+1][w]=dp[i][w]
print(dp[N][W])

Submission status: Wrong Answer

Solution code:
n,w=map(int,input().split())
d={}
p=0
ans=[0,0] 
u=[[] for i in range(4)]
for i in range(n):
    a,b=map(int,input().split())
    if a in d: d[a].append(b)
    else:d[a]=[0,b]
    p=max(p,a)
for i in range(4):
    if p-i in d: d[p-i].sort()
    else: d[p-i]=[0]

for x in range(4):
    if len(d[p-x])*(p-x)+ans[0]<=w:
        ans[0]+=len(d[p-x])*(p-x)
        ans[1]+=sum(d[p-x])
        u[x]=[o for o in d[p-x]]
        d[p-x]=[0]
    else:
        for i in range(-1,-1-len(d[p-x]),-1):
            if p+ans[0]-x<=w:
                ans[0]+=p-x
                ans[1]+=d[p-x][-1]
                u[x].append(d[p-x].pop())
            else:
                break
for i in range(3):
    while len(u[i]):
        if u[i][-1]<d[p-i-1][-1]:
            ans[0]-=1
            ans[1]+=d[p-i-1][-1]
            u[i].pop()
            u[i+1].append(d[p-i-1].pop())
        elif (p-i-2 in d) and u[i][-1]<d[p-i-2][-1]:
            ans[0]-=2
            ans[1]+=d[p-i-2][-1]
            u[i].pop()
            u[i+2].append(d[p-i-2].pop())
        elif (p-i-3 in d) and u[i][-1]<d[p-i-3][-1]:
            ans[0]-=3
            ans[1]+=d[p-i-3][-1]
            u[i].pop()
            u[i+3].append(d[p-i-3].pop())
        elif (p-i-2 in d) and d[p-i-2][-1]>0 and w>=ans[0]+(p-i-1)+(p-i-2) and u[i][-1]<d[p-i-1][-1]+d[p-i-2][-1]:
            ans[0]-=p-i
            ans[0]+=(p-i-1)+(p-i-2)
            ans[1]+=d[p-i-1][-1]
            ans[1]+=d[p-i-2][-1]
            u[i].pop()
            u[i+1].append(d[p-i-1].pop())
            u[i+2].append(d[p-i-2].pop())
        elif (p-i-2 in d) and len(d[p-i-2])>1 and d[p-i-2][-2]>0 and w>=ans[0]+((p-i-2)*2) and u[i][-1]<d[p-i-2][-1]+d[p-i-2][-2]:
            ans[0]-=p-i
            ans[0]+=(p-i-2)*2
            ans[1]+=d[p-i-2][-1]
            ans[1]+=d[p-i-2][-2]
            u[i].pop()
            u[i+2].append(d[p-i-2].pop())
            u[i+2].append(d[p-i-2].pop())
        elif (p-i-3 in d) and d[p-i-3][-1]>0 and w>=ans[0]+(p-i-1)+(p-i-3) and u[i][-1]<d[p-i-1][-1]+d[p-i-3][-1]:
            ans[0]-=p-i
            ans[0]+=(p-i-1)+(p-i-3)
            ans[1]+=d[p-i-1][-1]
            ans[1]+=d[p-i-3][-1]
            u[i].pop()
            u[i+1].append(d[p-i-1].pop())
            u[i+3].append(d[p-i-3].pop())
        else:
            break
print(ans[1])

Submission status: Wrong Answer

Solution code:
n,w=map(int,input().split())
d={}
p=0
ans=[0,0] 
u=[[] for i in range(4)]
for i in range(n):
    a,b=map(int,input().split())
    if a in d: d[a].append(b)
    else:d[a]=[0,b]
    p=max(p,a)
for i in range(4):
    if p-i in d: d[p-i].sort()
    else: d[p-i]=[0]

for x in range(4):
    if len(d[p-x])*(p-x)+ans[0]<=w:
        ans[0]+=len(d[p-x])*(p-x)
        ans[1]+=sum(d[p-x])
        u[x]=[o for o in d[p-x]]
        d[p-x]=[0]
    else:
        for i in range(-1,-1-len(d[p-x]),-1):
            if p+ans[0]-x<=w:
                ans[0]+=p-x
                ans[1]+=d[p-x][-1]
                u[x].append(d[p-x].pop())
            else:
                break
for i in range(3):
    while len(u[i]):
        if u[i][-1]<d[p-i-1][-1]:
            ans[0]-=1
            ans[1]+=d[p-i-1][-1]
            u[i].pop()
            u[i+1].append(d[p-i-1].pop())
        elif (p-i-2 in d) and u[i][-1]<d[p-i-2][-1]:
            ans[0]-=2
            ans[1]+=d[p-i-2][-1]
            u[i].pop()
            u[i+2].append(d[p-i-2].pop())
        elif (p-i-3 in d) and u[i][-1]<d[p-i-3][-1]:
            ans[0]-=3
            ans[1]+=d[p-i-3][-1]
            u[i].pop()
            u[i+3].append(d[p-i-3].pop())
        elif (p-i-2 in d) and d[p-i-2][-1]>0 and w>=ans[0]+(p-i-1)+(p-i-2) and u[i][-1]<d[p-i-1][-1]+d[p-i-2][-1]:
            ans[0]-=p-i
            ans[0]+=(p-i-1)+(p-i-2)
            ans[1]+=d[p-i-1][-1]
            ans[1]+=d[p-i-2][-1]
            u[i].pop()
            u[i+1].append(d[p-i-1].pop())
            u[i+2].append(d[p-i-2].pop())
        elif (p-i-2 in d) and len(d[p-i-2])>1 and d[p-i-2][-2]>0 and w>=ans[0]+((p-i-2)*2) and u[i][-1]<d[p-i-2][-1]+d[p-i-2][-2]:
            ans[0]-=p-i
            ans[0]+=(p-i-2)*2
            ans[1]+=d[p-i-2][-1]
            ans[1]+=d[p-i-2][-2]
            u[i].pop()
            u[i+2].append(d[p-i-2].pop())
            u[i+2].append(d[p-i-2].pop())
        else:
            break
print(ans[1])

Submission status: Wrong Answer

Solution code:
n,w=map(int,input().split())
d={}
p=0
ans=[0,0]
u=[[] for i in range(4)]
for i in range(n):
    a,b=map(int,input().split())
    if a in d: d[a].append(b)
    else:d[a]=[0,b]
    p=max(p,a)
for i in range(4):
    if p-i in d: d[p-i].sort()
    else: d[p-i]=[0]

for x in range(4):
    if len(d[p-x])*(p-x)+ans[0]<=w:
        ans[0]+=len(d[p-x])*(p-x)
        ans[1]+=sum(d[p-x])
        u[x]=[o for o in d[p-x]]
        d[p-x]=[0]
    else:
        for i in range(-1,-1-len(d[p-x]),-1):
            if p+ans[0]-x<=w:
                ans[0]+=p-x
                ans[1]+=d[p-x][-1]
                u[x].append(d[p-x].pop())
            else:
                break
for i in range(3):
    while len(u[i]):
        if u[i][-1]<d[p-i-1][-1]:
            ans[0]-=1
            ans[1]+=d[p-i-1][-1]
            u[i].pop()
            u[i+1].append(d[p-i-1].pop())
        elif (p-i-2 in d) and u[i][-1]<d[p-i-2][-1]:
            ans[0]-=2
            ans[1]+=d[p-i-2][-1]
            u[i].pop()
            u[i+2].append(d[p-i-2].pop())
        elif (p-i-2 in d) and d[p-i-2][-1]>0 and w>=ans[0]+(p-i-1)+(p-i-2) and u[i][-1]<d[p-i-1][-1]+d[p-i-2][-1]:
            ans[0]-=p-i
            ans[0]+=(p-i-1)+(p-i-2)
            ans[1]+=d[p-i-1][-1]
            ans[1]+=d[p-i-2][-1]
            u[i].pop()
            u[i+1].append(d[p-i-1].pop())
            u[i+2].append(d[p-i-2].pop())
        elif (p-i-2 in d) and len(d[p-i-2])>1 and d[p-i-2][-2]>0 and w>=ans[0]+((p-i-2)*2) and u[i][-1]<d[p-i-2][-1]+d[p-i-2][-2]:
            ans[0]-=p-i
            ans[0]+=(p-i-2)*2
            ans[1]+=d[p-i-2][-1]
            ans[1]+=d[p-i-2][-2]
            u[i].pop()
            u[i+2].append(d[p-i-2].pop())
            u[i+2].append(d[p-i-2].pop())
        else:
            break
print(ans[1])

Submission status: Wrong Answer

Solution code:
n,w=map(int,input().split())
d={}
p=0
ans=[0,0]
u=[[] for i in range(4)]
for i in range(n):
    a,b=map(int,input().split())
    if a in d: d[a].append(b)
    else:d[a]=[0,b]
    p=max(p,a)
for i in range(4):
    if p-i in d: d[p-i].sort()
    else: d[p-i]=[0]

for x in range(4):
    if len(d[p-x])*(p-x)+ans[0]<=w:
        ans[0]+=len(d[p-x])*(p-x)
        ans[1]+=sum(d[p-x])
        u[x]=[o for o in d[p-x]]
        d[p-x]=[0]
    else:
        for i in range(-1,-1-len(d[p-x]),-1):
            if p+ans[0]-x<=w:
                ans[0]+=p-x
                ans[1]+=d[p-x][-1]
                u[x].append(d[p-x].pop())
            else:
                break
#u[-1].sort(reverse=True)
for i in range(3):
    while len(u[i]):
        if u[i][-1]<d[p-i-1][-1]:
            ans[0]-=1
            ans[1]+=d[p-i-1][-1]
            u[i].pop()
            u[i+1].append(d[p-i-1].pop())
        elif (p-i-2 in d) and d[p-i-2][-1]>0 and w>=ans[0]+(p-i-1)+(p-i-2) and u[i][-1]<d[p-i-1][-1]+d[p-i-2][-1]:
            ans[0]-=p-i
            ans[0]+=(p-i-1)+(p-i-2)
            ans[1]+=d[p-i-1][-1]
            ans[1]+=d[p-i-2][-1]
            u[i].pop()
            u[i+1].append(d[p-i-1].pop())
            u[i+2].append(d[p-i-2].pop())
        else:
            break
print(ans[1])

Submission status: Wrong Answer

Solution code:
n,w=map(int,input().split())
d={}
p=0
ans=[0,0]
u=[[] for i in range(4)]
for i in range(n):
    a,b=map(int,input().split())
    if a in d: d[a].append(b)
    else:d[a]=[0,b]
    p=max(p,a)
for i in range(4):
    if p-i in d: d[p-i].sort()
    else: d[p-i]=[0]

for x in range(4):
    if len(d[p-x])*(p-x)+ans[0]<=w:
        ans[0]+=len(d[p-x])*(p-x)
        ans[1]+=sum(d[p-x])
        u[x]=[o for o in d[p-x]]
        d[p-x]=[0]
    else:
        for i in range(-1,-1-len(d[p-x]),-1):
            if p+ans[0]-x<=w:
                ans[0]+=p-x
                ans[1]+=d[p-x][-1]
                u[x].append(d[p-x].pop())
            else:
                break
#u[-1].sort(reverse=True)
while len(u[0]):
    if u[0][-1]<d[p-1][-1]:
        ans[0]-=1
        ans[1]+=d[p-1][-1]
        u[0].pop()
        u[0].append(d[p-1].pop())
    else:
        break
print(ans[1])

Submission status: Wrong Answer

Solution code:
n,w=map(int,input().split())
d={}
p=0
ans=[0,0]
u=[[] for i in range(4)]
for i in range(n):
    a,b=map(int,input().split())
    if a in d: d[a].append(b)
    else:d[a]=[0,b]
    p=max(p,a)
for i in range(4):
    if p-i in d: d[p-i].sort()
    else: d[p-i]=[0]

for x in range(4):
    if len(d[p-x])*(p-x)+ans[0]<=w:
        ans[0]+=len(d[p-x])*(p-x)
        ans[1]+=sum(d[p-x])
        u[x]=[o for o in d[p-x]]
        d[p-x]=[0]
    else:
        for i in range(-1,-1-len(d[p-x]),-1):
            if p+ans[0]-x<=w:
                ans[0]+=p-x
                ans[1]+=d[p-x][-1]
                u[x].append(d[p-x].pop())
            else:
                break

while len(u[0]):
    if u[0][-1]<d[p-1][-1]:
        ans[0]-=1
        ans[1]+=d[p-1][-1]
        u[-1].append(d[p-1][-1])
    else:
        break
print(ans[1])

Submission status: Wrong Answer

Solution code:
n,w=map(int,input().split())
d={}
p=0
ans=[0,0]
u=[[] for i in range(4)]
for i in range(n):
    a,b=map(int,input().split())
    if a in d: d[a].append(b)
    else:d[a]=[b]
    p=max(p,a)
for i in range(4):
    if p-i in d: d[p-i].sort()
    else: d[p-i]=[0]

for x in range(4):
    if len(d[p-x])*(p-x)+ans[0]<=w:
        ans[0]+=len(d[p-x])*(p-x)
        ans[1]+=sum(d[p-x])
    else:
        for i in range(-1,-1-len(d[p-x]),-1):
            if p+ans[0]-x<=w:
                ans[0]+=p-x
                ans[1]+=d[p-x][i]
                u[-x].append(d[p-x][i])
            else:
                break
print(ans[1])

Submission status: Wrong Answer

Solution code:
n,w=map(int,input().split())
d={}
p=0
ans=[0,0]
u=[[] for i in range(4)]
for i in range(n):
    a,b=map(int,input().split())
    if a in d: d[a].append(b)
    else:d[a]=[b]
    p=max(p,a)
for i in range(4):
    if p-i in d: d[p-i].sort()
    else: d[p-i]=[0]
if len(d[p])*p<=w:
    ans[0]+=len(d[p])*p
    ans[1]+=sum(d[p])
else:
    for i in range(-1,-1-len(d[p]),-1):
        if p+ans[0]<=w:
            ans[0]+=p
            ans[1]+=d[p][i]
            u[0].append(d[p][i])
        else:
            break
if len(d[p-1])*(p-1)+ans[0]<=w:
    ans[0]+=len(d[p-1])*(p-1)
    ans[1]+=sum(d[p-1])
else:
    for i in range(-1,-1-len(d[p-1]),-1):
        if p+ans[0]-1<=w:
            ans[0]+=p-1
            ans[1]+=d[p-1][i]
            u[1].append(d[p-1][i])
        else:
            break
if len(d[p-2])*(p-2)+ans[0]<=w:
    ans[0]+=len(d[p-2])*(p-2)
    ans[1]+=sum(d[p-2])
else:
    for i in range(-1,-1-len(d[p-2]),-1):
        if p+ans[0]-2<=w:
            ans[0]+=p-2
            ans[1]+=d[p-2][i]
            u[2].append(d[p-2][i])
        else:
            break
if len(d[p-3])*(p-3)+ans[0]<=w:
    ans[0]+=len(d[p-3])*(p-3)
    ans[1]+=sum(d[p-3])
else:
    for i in range(-1,-1-len(d[p-3]),-1):
        if p+ans[0]-3<=w:
            ans[0]+=p-3
            ans[1]+=d[p-3][i]
            u[3].append(d[p-3][i])
        else:
            break
print(ans[1])

Submission status: Wrong Answer

Solution code:
from functools import lru_cache

N, W = map(int, input().split())

ws = []
vs = []

for _ in range(N):
    v, w = input().split()
    vs.append(int(v))
    ws.append(int(w))

# i番目以降の品物から重さの和がj以下なるように選んだときの、
# 取りうる価値の総和の最大値を返す関数
@lru_cache(maxsize=None)
def rec(i, j):
    if i == N:
        return 0
    # 残りの容量が足りず品物iを入れられないので、入れないパターンだけ処理
    # i + 1以降の品物のみを使ったときの最大値をそのままこの場合の最大値にする
    elif j < ws[i]:
        return rec(i+1, j)
    # 品物iを入れるか入れないか選べるので、両方試して価値の和が大きい方を選ぶ
    else:
        return max(rec(i+1, j), rec(i+1, j-ws[i])+vs[i])

print(rec(0, W))

Submission status: Wrong Answer

Solution code:
from functools import lru_cache

N, W = map(int, input().split())

ws = []
vs = []

for _ in range(N):
    v, w = input().split()
    vs.append(int(v))
    ws.append(int(w))

# i番目以降の品物から重さの和がj以下なるように選んだときの、
# 取りうる価値の総和の最大値を返す関数
@lru_cache(maxsize=1000)
def rec(i, j):
    if i == N:
        return 0
    # 残りの容量が足りず品物iを入れられないので、入れないパターンだけ処理
    # i + 1以降の品物のみを使ったときの最大値をそのままこの場合の最大値にする
    elif j < ws[i]:
        return rec(i+1, j)
    # 品物iを入れるか入れないか選べるので、両方試して価値の和が大きい方を選ぶ
    else:
        return max(rec(i+1, j), rec(i+1, j-ws[i])+vs[i])

print(rec(0, W))

Submission status: Wrong Answer

Solution code:
N, W = map(int, input().split())
w = []
v = []
for i in range(N):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)
w_adjust = []
for i in range(N):
    w_adjust.append(w[i]-w[0])

a = w[0]
dp = [[[0 for i in range(N+1)] for j in range(N*3+10)] for k in range(N+1)]
#i個目までの物のうちk個選んで重さの和がjになる中での最大価値
for i in range(N):
    for j in range(3*N):
        for k in range(N):
            if j + w_adjust[i] > W - a*(k+1):
                dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
            else:
                dp[i+1][j+w_adjust[i]][k+1] = max(dp[i+1][j+w_adjust[i]][k+1], dp[i][j][k] + v[i])
                dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
ans = [0]
for i in range(1, N+1):
    if W - a*i >= 0 and W - a*i < N*3 + 3:
        ans.append(dp[N][W-a*i][i])
        print(N)
        print(W-a*1)
        print(i)

if W - a*N > N*3:
    things = W//w[0]
    if things > N:
        ans.append(sum(v))

elif len(ans) == 1:
    things = W//w[0]
    v.sort()
    v.reverse()
    ans_1 = 0
    for i in range(things):
        ans_1 += v[i]
    ans.append(ans_1)

print(max(ans))

Submission status: Wrong Answer

Solution code:
N, W = map(int, input().split())
w = []
v = []
for i in range(N):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)
w_adjust = []
for i in range(N):
    w_adjust.append(w[i]-w[0])

a = w[0]
dp = [[[0 for i in range(N+1)] for j in range(N*3+10)] for k in range(N+1)]
#i個目までの物のうちk個選んで重さの和がjになる中での最大価値
for i in range(N):
    for j in range(3*N):
        for k in range(N):
            if j + w_adjust[i] > W - a*(k+1):
                dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
            else:
                dp[i+1][j+w_adjust[i]][k+1] = max(dp[i+1][j+w_adjust[i]][k+1], dp[i][j][k] + v[i])
                dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
ans = [0]
for i in range(1, N+1):
    if W - a*i >= 0 and W - a*i < N*3 + 3:
        ans.append(dp[N][W-a*i][i])
        print(N)
        print(W-a*1)
        print(i)

if W - a*N > N*3:
    things = W//w[0]
    if things > N:
        ans.append(sum(v))

elif len(ans) == 1:
    things = W//w[0]
    v.sort()
    v.reverse()
    ans_1 = 0
    for i in range(things):
        ans_1 += v[i]
    ans.append(ans_1)
print(ans)
print(max(ans))

Submission status: Wrong Answer

Solution code:
N, W = map(int, input().split())
w = []
v = []
for i in range(N):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)
w_adjust = []
for i in range(N):
    w_adjust.append(w[i]-w[0])

a = w[0]
dp = [[[0 for i in range(N+1)] for j in range(N*3+10)] for k in range(N+1)]
#i個目までの物のうちk個選んで重さの和がjになる中での最大価値
for i in range(N):
    for j in range(3*N):
        for k in range(N):
            if j + w_adjust[i] > W - a*(k+1):
                dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
            else:
                dp[i+1][j+w_adjust[i]][k+1] = max(dp[i+1][j+w_adjust[i]][k+1], dp[i][j][k] + v[i])
                dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
ans = [0]
for i in range(1, N+1):
    try:
        ans.append(dp[N][W-a*i][i])
    except:
        pass

if W - a*N > N*3:
    things = W//w[0]
    if things > N:
        ans.append(sum(v))

elif len(ans) == 1:
    things = W//w[0]
    v.sort()
    v.reverse()
    ans_1 = 0
    for i in range(things):
        ans_1 += v[i]
    ans.append(ans_1)

print(max(ans))

Submission status: Wrong Answer

Solution code:
N, W = map(int, input().split())
w = []
v = []
for i in range(N):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)
w_adjust = []
for i in range(N):
    w_adjust.append(w[i]-w[0])

a = w[0]
dp = [[[0 for i in range(N+1)] for j in range(N*3+10)] for k in range(N+1)]
#i個目までの物のうちk個選んで重さの和がjになる中での最大価値
for i in range(N):
    for j in range(3*N):
        for k in range(N):
            if j + w_adjust[i] > W - a*(k+1):
                dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
            else:
                dp[i+1][j+w_adjust[i]][k+1] = max(dp[i+1][j+w_adjust[i]][k+1], dp[i][j][k] + v[i])
                dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
ans = [0]
for i in range(1, N+1):
    try:
        ans.append(dp[N][W-a*i][i])
    except:
        pass

if W - a*N > N*3:
    things = W//w[0]
    if things > N:
        ans.append(sum(v))
elif len(ans) == 1:
    things = W//w[0]
    v.sort()
    v.reverse()
    ans_1 = 0
    for i in range(things):
        ans_1 += v[things]
    ans.append(ans_1)

print(max(ans))

Submission status: Wrong Answer

Solution code:
N, W = map(int, input().split())
w = []
v = []
for i in range(N):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)
w_adjust = []
for i in range(N):
    w_adjust.append(w[i]-w[0])

a = w[0]
dp = [[[0 for i in range(N+1)] for j in range(N*3+10)] for k in range(N+1)]
#i個目までの物のうちk個選んで重さの和がjになる中での最大価値
for i in range(N):
    for j in range(3*N):
        for k in range(N):
            if j + w_adjust[i] > W - a*(k+1):
                dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
            else:
                dp[i+1][j+w_adjust[i]][k+1] = max(dp[i+1][j+w_adjust[i]][k+1], dp[i][j][k] + v[i])
                dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
ans = [0]
for i in range(1, N+1):
    try:
        ans.append(dp[N][W-a*i][i])
    except:
        pass

if W - a*N > N*3:
    things = W//w[0]
    if things > N:
        ans.append(sum(v))
elif len(ans) == 1:
    things = W//w[0]
    v.sort()
    v.reverse()
    ans_1 = 0
    for i in range(things):
        ans_1 += v[things]
    ans.append(ans_1)

print(ans)
print(a)
print(max(ans))

Submission status: Wrong Answer

Solution code:
N, W = map(int, input().split())
w = []
v = []
for i in range(N):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)
w_adjust = []
for i in range(N):
    w_adjust.append(w[i]-w[0])

a = w[0]
dp = [[[0 for i in range(N+1)] for j in range(N*3+10)] for k in range(N+1)]
#i個目までの物のうちk個選んで重さの和がjになる中での最大価値
for i in range(N):
    for j in range(3*N):
        for k in range(N):
            if j + w_adjust[i] > W - a*(k+1):
                dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
            else:
                dp[i+1][j+w_adjust[i]][k+1] = max(dp[i+1][j+w_adjust[i]][k+1], dp[i][j][k] + v[i])
                dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
ans = [0]
for i in range(N+1):
    if W - a*i >= 0 and W - a*i <= N*3+4:
        ans.append(dp[N][W-a*i][i])

print(max(ans))

Submission status: Wrong Answer

Solution code:
N, W = map(int, input().split())
w = []
v = []
for i in range(N):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)
w_adjust = []
for i in range(N):
    w_adjust.append(w[i]-w[0])

a = w[0]
dp = [[[0 for i in range(N+1)] for j in range(N*3+10)] for k in range(N+1)]
#i個目までの物のうちk個選んで重さの和がjになる中での最大価値
for i in range(N):
    for j in range(3*N):
        for k in range(N):
            dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
            dp[i+1][j+w_adjust[i]][k+1] = max(dp[i+1][j+w_adjust[i]][k+1], dp[i][j][k] + v[i])

ans = [0]
for i in range(N+1):
    if W - a*i >= 0:
        if W - a*i <= (i*3):
            ans.append(dp[N][W-a*i][i])

print(max(ans))

Submission status: Wrong Answer

Solution code:
N, W = map(int, input().split())
w = []
v = []
for i in range(N):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)
w_adjust = []
for i in range(N):
    w_adjust.append(w[i]-w[0])

a = w[0]
dp = [[[0 for i in range(N+1)] for j in range(N*3+10)] for k in range(N+1)]
#i個目までの物のうちk個選んで重さの和がjになる中での最大価値
for i in range(N):
    for j in range(3*N):
        for k in range(N):
            dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
            dp[i+1][j+w_adjust[i]][k+1] = max(dp[i+1][j+w_adjust[i]][k+1], dp[i][j][k] + v[i])

ans = [0]
for i in range(N+1):
    if W - a*i >= 0:
        if W - a*i <= (N*3 + 5):
            ans.append(dp[N][W-a*i][i])

print(max(ans))

Submission status: Wrong Answer

Solution code:
n,W=map(int,input().split())
wv=[list(map(int,input().split())) for _ in range(n)]
x=[[] for _ in range(4)]
w0=wv[0][0]
for w,v in wv:
    x[w-w0].append(v)
for i in range(4):
    x[i].sort()
y=[[0] for i in range(4)]
for i in range(4):
    if len(x[i])>0:
        y[i].append(x[i][0])
for i in range(4):
    for j in range(1,len(x[i])):
        y[i].append(y[i][-1]+x[i][j])
res=[]
for a in range(len(x[0])+1):
    for b in range(len(x[1])+1):
        for c in range(len(x[2])+1):
            for d in range(len(x[3])+1):
                if w0*a+(w0+1)*b+(w0+2)*c+(w0+3)*d<=W:
                    res.append(y[0][a]+y[1][b]+\
                              y[2][c]+y[3][d])
print(max(res))

Submission status: Wrong Answer

Solution code:
N, W = map(int, input().split())
data_list = []
i = 0
while i < N:
    w, v = map(int, input().split())
    data_list.append([w,v,v/w])
    i += 1
data_list.sort(key=lambda x: int(x[2]), reverse=True)
total_w = 0
total_v = 0
for i in data_list:
    if i[0] <= W:
        if i[0] + total_w <= W:
            total_w += i[0]
            total_v += i[1]
print(total_v)

Submission status: Wrong Answer

Solution code:
# ABC 060 D
N, W = map(int,input().split())
L = [list(map(int,input().split())) for k in range(N)]

wmin = L[0][0]
M = [[],[],[],[]]
for load in L:
    M[load[0]-wmin].append(load[1])

M0 = sorted(M[0][::-1])
M1 = sorted(M[1][::-1])
M2 = sorted(M[2][::-1])
M3 = sorted(M[3][::-1])

ans = 0
for p in range(len(M0)+1):
    for q in range(len(M1)+1):
        for r in range(len(M2)+1):
            for s in range(len(M3)+1):
                weight = p*wmin + q*(wmin+1) + r*(wmin+2) + s*(wmin+3)
                if weight <= W:
                    ans = max(ans,sum(M0[:p])+sum(M1[:q])+sum(M2[:r])+sum(M3[:s]))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from itertools import accumulate

n,w = li()
wv = []
for _ in range(n):
    wv.append(tuple(li()))
    
w0 = wv[0][0]
dic = {w0+i: [] for i in range(4)}
for k,v in wv:
    dic[k].append(v)
    
for wi in range(w0,w0+4):
    dic[wi] = [0] + sorted(dic[wi])
    dic[wi] = list(accumulate(dic[wi]))
    
ans = 0
for idx1, w1 in enumerate(dic[w0]):
    
    for idx2, w2 in enumerate(dic[w0+1]):
        
        for idx3, w3 in enumerate(dic[w0+2]):
            
            for idx4, w4 in enumerate(dic[w0+3]):
                
                if w0*idx1 + (w0+1)*idx2 + (w0+2)*idx3 + (w0+3)*idx4 > w:
                    continue
                
                ans = max(ans, w1+w2+w3+w4)
        
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
N, W = map( int, input().split())
o, k = map( int, input().split())
A = [k]
B, C, D = [], [], []
for _ in range(N-1):
    w, v = map( int, input().split())
    if w == o:
        A.append(v)
    elif w == o+1:
        B.append(v)
    elif w == o+2:
        C.append(v)
    else:
        D.append(v)
A.sort()
B.sort()
C.sort()
D.sort()
A = [0] + list(accumulate(A))
B = [0] + list(accumulate(B))
C = [0] + list(accumulate(C))
D = [0] + list(accumulate(D))
# print(A)
# print(B)
# print(C)
# print(D)
ans = 0
for i in range( len(A)):
    for j in range( len(B)):
        for k in range( len(C)):
            for l in range( len(D)):
                if i*o + j*(o+1) + k*(o+2) + l*(o+3) <= W:
#                    print('{} {} {} {}'.format(i,j,k,l))
                    ans = max( ans, A[i]+B[j]+C[k]+D[l])
print(ans)