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:
n, W = map(int, input().split())
dp = [[[0] * 301 for _ in range(n + 1)] for _ in range(n + 1)]
li = [list(map(int, input().split())) for _ in range(n)]
p = li[0][0]
for i, x in enumerate(li):
    w, v = x
    w -= p
    for j in range(i + 1):
        for k in range(300):
            if k >= w:
                if dp[i][j][k - w] + v > dp[i][j][k]:
                    dp[i + 1][j + 1][k] = dp[i][j][k - w] + v
                else:
                    dp[i + 1][j][k] = dp[i][j][k]
            else:
                dp[i + 1][j][k] = dp[i][j][k]
ans = 0
for i in range(n + 1):
    for k in range(301):
        if i * p + k <= W:
            ans = max(dp[n][i][k], ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,W = map(int,input().split())
WV = [list(map(int,input().split())) for _ in range(N)]

Wdic = {}
for w in WV:
  if w[0] not in Wdic:
    Wdic[w[0]] = [w]
  else:
    Wdic[w[0]].append(w)

Wcount = [[0 for _ in range(2)] for _ in range(4)]
idx = 0
for k in Wdic.keys():
  Wcount[idx] = [k,len(Wdic[k])]
  idx += 1
#print(Wcount)

Wsum = {}
for k,v in Wdic.items():
  v.sort(key=lambda x:x[1], reverse = True)
  Wsum[k] = [v[0][1]]
  for i in range(1,len(v)):
    Wsum[k].append(Wsum[k][-1] + v[i][1])
#print(Wsum)

def check(i,j,k,l):
  ret = 0
  w = 0
  if i > 0 and Wcount[0][0] > 0:
    ret += Wsum[Wcount[0][0]][i-1]
    w += Wcount[0][0]*i
  if j > 0 and Wcount[1][0] > 0:
    ret += Wsum[Wcount[1][0]][j-1]
    w += Wcount[1][0]*j
  if k > 0 and Wcount[2][0] > 0:
    ret += Wsum[Wcount[2][0]][k-1]
    w += Wcount[2][0]*j
  if l > 0 and Wcount[3][0] > 0:
    ret += Wsum[Wcount[3][0]][l-1]
    w += Wcount[3][0]*l
  if w > W:
    ret = 0
  return ret

value = 0
for i in range(Wcount[0][1]+1):
  for j in range(Wcount[1][1]+1):
    for k in range(Wcount[2][1]+1):
      for l in range(Wcount[3][1]+1):
        value = max(value,check(i,j,k,l))
print(value)

Submission status: Wrong Answer

Solution code:
#D問題
N,W = map(int,input().split())
WV = [list(map(int,input().split())) for i in range(N)]


w1 = WV[0][0]
WV.sort()

vlist = [[] for i in range(4)]
for i in range(N):
    w,v = WV[i][0],WV[i][1]
    d = w-w1
    vlist[d].append(v)
    
vrui = [[0] for i in range(4)]
for i in range(4):
    vlist[i].sort(reverse=True)
    for v in vlist[i]:
        vrui[i].append(vrui[i][-1]+v)
        
#print(vlist)
#print(vrui)
MAX = 0
for i in range(min(N+1,len(vlist[0])+1)):
    for j in range(min(N-i+1,len(vlist[1])+1)):
        for k in range(min(N-i-j+1,len(vlist[2])+1)):
            l = min(N-(i+j+k)+1,len(vlist[3]))
            #print(w1*i+(w1+1)*j+(w1+2)*k+(w1+3)*l)
            #print(vrui[0][i],vrui[1][j],vrui[2][k],vrui[3][l])
            if (w1*i+(w1+1)*j+(w1+2)*k+(w1+3)*l) <= W:
                MAX = max(MAX,(vrui[0][i]+vrui[1][j]+vrui[2][k]+vrui[3][l]))

print(MAX)

Submission status: Wrong Answer

Solution code:
#D問題
N,W = map(int,input().split())
WV = [list(map(int,input().split())) for i in range(N)]


w1 = WV[0][0]
WV.sort()

vlist = [[] for i in range(4)]
for i in range(N):
    w,v = WV[i][0],WV[i][1]
    d = w-w1
    vlist[d].append(v)
    
vrui = [[0] for i in range(4)]
for i in range(4):
    vlist[i].sort(reverse=True)
    for v in vlist[i]:
        vrui[i].append(vrui[i][-1]+v)
        
#print(vlist)
#print(vrui)
MAX = 0
for i in range(min(N+1,len(vlist[0])+1)):
    for j in range(min(N-i+1,len(vlist[1])+1)):
        for k in range(min(N-i-j+1,len(vlist[2])+1)):
            l = min(N-(i+j+k),len(vlist[3]))
            #print(w1*i+(w1+1)*j+(w1+2)*k+(w1+3)*l)
            #print(vrui[0][i],vrui[1][j],vrui[2][k],vrui[3][l])
            if (w1*i+(w1+1)*j+(w1+2)*k+(w1+3)*l) <= W:
                MAX = max(MAX,(vrui[0][i]+vrui[1][j]+vrui[2][k]+vrui[3][l]))

print(MAX)

Submission status: Wrong Answer

Solution code:
N, W = map(int, input().split())
items = [tuple(map(int, input().split())) for i in range(N)]

dp = [[10**15 for sum_v in range(10**5+100)] for i in range(N+100)]

for i in range(N):
    for sum_v in range(10**5+50):

        dp[0][0] = 0

        # i-1番目の品物を選ぶ場合
        if sum_v-items[i][1] >= 0:
            dp[i+1][sum_v] = min(dp[i+1][sum_v],
                                 dp[i][sum_v-items[i][1]]+items[i][0])

        # i-1番目の品物を選ばない場合
        dp[i+1][sum_v] = min(dp[i+1][sum_v], dp[i][sum_v])

ans = 0
for i in range(10**5+50):
    if dp[N][i] <= W:
        ans = i
print(ans)

Submission status: Wrong Answer

Solution code:
# ARC073D - Simple Knapsack (ABC060D)
from itertools import accumulate


def main():
    N, W, *A = map(int, open(0).read().split())
    V, x = [[] for _ in range(4)], A[0]  # x: w1 (W1 ≤ wi ≤ w1 + 3)
    for i in range(0, 2 * N, 2):
        w, v = A[i : i + 2]
        V[w - x] += [v]
    for i in range(4):
        V[i] = tuple(accumulate([0] + sorted(V[i], reverse=1)))
    L = [len(v) for v in V]
    print(V, L)
    ans = 0
    for i in range(L[0]):
        for j in range(L[1]):
            for k in range(L[2]):
                for l in range(L[3]):
                    w = i * x + j * (x + 1) + k * (x + 2) + l * (x + 3)
                    if w > W:
                        break
                    v = V[0][i] + V[1][j] + V[2][k] + V[3][l]
                    ans = max(ans, v)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n, w = map(int, input().split())
a = []
for _ in range(n):
    w_in, v_in = map(int, input().split())
    a.append([w_in, v_in])

dp = [[0 for _ in range(w + 1)] for _ in range(n + 1)]
for j in range(w + 1):
    for i in range(n):
        w_in, v_in = a[i]
        if j + w_in <= w:
            dp[i + 1][j + w_in] = max(dp[i + 1][j + w_in], dp[i][j] + v_in)
            if i + 2 <= n:
                dp[i + 2][j + w_in] = max(dp[i + 2][j + w_in], dp[i + 1][j + w_in])
print(max([max(d) for d in dp]))

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

from itertools import accumulate

sys.setrecursionlimit(10 ** 7)

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())


n, w = li()
value = {i: [0] for i in range(4)}

for i in range(n):
    wei, val = li()

    if i == 0:
        w0 = wei

    value[wei - w0].append(val)

for i in range(4):
    value[i].sort()
    value[i] = list(accumulate(value[i]))


ans = 0

for i in range(len(value[0])):
    for j in range(len(value[1])):
        for k in range(len(value[2])):
            rest = w - i*w0 - j*(w0+1) - k*(w0+2)
            x = rest // (w0+3)

            if x < 0:
                continue
            else:
                ans = max(ans, value[0][i] + value[1][j] + value[2][k] + value[3][min(len(value[3])-1, x)])

print(ans)

Submission status: Wrong Answer

Solution code:
N,W=map(int,input().split())
wv=[]
for i in range(N):
    a,b=map(int,input().split())
    wv.append((a,b))
if wv[0][0]<300:
    dp=[[0]*(30000+1) for i in range(N+1)]
    for i in range(1,N+1):
        wi,vi=wv[i-1]
        for j in range(1,30000+1):
            dp[i][j]=dp[i-1][j]
            if j-wi<0:
                continue
            dp[i][j]=max(dp[i-1][j-wi]+vi,dp[i][j])
    ans=dp[N][W]
    print(ans)
    quit()
dp=[[0]*(301*101) for i in range(N+1)]
d=wv[0][0]
def change(n):
    return 301*(n//d)+n%d
if change(W)>301*101:
    quit()
for i in range(1,N+1):
    wi,vi=wv[i-1]
    z=301+(wi-d)
    for j in range(1,301*101):
        dp[i][j]=dp[i-1][j]
        if j-z<0:
            continue

        dp[i][j]=max(dp[i-1][j-z]+vi,dp[i][j])
ans=dp[N][change(W)]
print(ans)

Submission status: Wrong Answer

Solution code:
N,W=map(int,input().split())
wv=[]
for i in range(N):
    a,b=map(int,input().split())
    wv.append((a,b))
if wv[0][0]<300:
    dp=[[0]*(30000+1) for i in range(N+1)]
    for i in range(1,N+1):
        wi,vi=wv[i-1]
        for j in range(1,30000+1):
            dp[i][j]=dp[i-1][j]
            if j-wi<0:
                continue
            dp[i][j]=max(dp[i-1][j-wi]+vi,dp[i][j])
    ans=dp[N][W]
    print(ans)
    quit()
dp=[[0]*(301*100+1) for i in range(N+1)]
d=wv[0][0]
def change(n):
    return 301*(n//d)+n%d
if change(W)>301*100:
    quit()
for i in range(1,N+1):
    wi,vi=wv[i-1]
    z=301+(wi-d)
    for j in range(1,301*100+1):
        dp[i][j]=dp[i-1][j]
        if j-z<0:
            continue

        dp[i][j]=max(dp[i-1][j-z]+vi,dp[i][j])
ans=dp[N][change(W)]
print(ans)

Submission status: Wrong Answer

Solution code:
N,W=map(int,input().split())
wv=[]
for i in range(N):
    a,b=map(int,input().split())
    wv.append((a,b))
if wv[0][0]<300:
    dp=[[0]*(30000+1) for i in range(N+1)]
    for i in range(1,N+1):
        wi,vi=wv[i-1]
        for j in range(1,30000+1):
            dp[i][j]=dp[i-1][j]
            if j-wi<0:
                continue
            dp[i][j]=max(dp[i-1][j-wi]+vi,dp[i][j])
    ans=dp[N][W]
    print(ans)
    quit()
dp=[[0]*(301*100+1) for i in range(N+1)]
d=wv[0][0]
def change(n):
    if 301*(n//d)+n%d>301*100+1:
        print(1)
        quit()
    return 301*(n//d)+n%d
for i in range(1,N+1):
    wi,vi=wv[i-1]
    z=301+(wi-d)
    for j in range(1,301*100+1):
        dp[i][j]=dp[i-1][j]
        if j-z<0:
            continue

        dp[i][j]=max(dp[i-1][j-z]+vi,dp[i][j])
ans=dp[N][change(W)]
print(ans)

Submission status: Wrong Answer

Solution code:
n,w = list(map(int,input().split()))
lis = []
for i in range(n):
  lis.append(list(map(int,input().split())))

w_id = w - lis[0][0]

for i in range(n):
  lis[i][0] -= lis[0][0]
  
if w_id < 0:
  print(0)
  exit()
  
dp = [[0 for _ in range(w_id+1)] for _ in range(n)]


for i in range(w_id+1):
  if i >= lis[0][0]:
    dp[0][i] = lis[0][1] 

for k in range(1,n):
  for i in range(w_id+1):
    if i < lis[k][0]:
      dp[k][i] = dp[k-1][i]
    else:
      dp[k][i] = max(dp[k-1][i],dp[k-1][i-lis[k][0]]+lis[k][1])
print(dp[n-1][w_id])

Submission status: Wrong Answer

Solution code:
n,w = list(map(int,input().split()))
lis = []
for i in range(n):
  lis.append(list(map(int,input().split())))

w_id = w - lis[0][0]

for i in range(n):
  lis[i][0] -= lis[0][0]
  
if w_id < 0:
  print(0)
  exit()
  
print(w_id)
dp = [[0 for _ in range(w_id+1)] for _ in range(n)]


for i in range(w_id+1):
  if i >= lis[0][0]:
    dp[0][i] = lis[0][1] 

for k in range(1,n):
  for i in range(w_id+1):
    if i < lis[k][0]:
      dp[k][i] = dp[k-1][i]
    else:
      dp[k][i] = max(dp[k-1][i],dp[k-1][i-lis[k][0]]+lis[k][1])
print(dp)
print(dp[n-1][w_id])

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
import math
from collections import deque
from collections import defaultdict
from itertools import permutations
import heapq
import bisect
from collections import Counter
from itertools import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = 10**10
from functools import reduce
ans = 0
n,weight = map(int,input().split())
W,V = [0],[]
w1,v1 = map(int,input().split())
V.append(v1)
for i in range(n-1):
    w,v = map(int,input().split())
    W.append(w-w1)
    V.append(v)
dp=[[[-inf for i in range(n*3)] for j in range(n+1)] for l in range(n+1)]
for w in range(3*n):
    dp[0][0][w] = 0
for i in range(n):
    for j in range(i+1):
        for w in range(n*3):
            if  w >= W[i]:
                for k in range(i):
                    dp[i+1][j+1][w] = max(dp[k][j][w-W[i]]+V[i],dp[k][j+1][w],dp[i+1][j+1][w])
            else:
                dp[i+1][j][w] = dp[i][j][w]
for i in range(n+1):
    for j in range(3*n):
        if i*w1+j <= weight:
            ans = max(dp[n][i][j],ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,W=map(int,input().split())
from collections import*
d=defaultdict(int)
d[0]=0
for i in range(N):
    w,v=map(int,input().split())
    for k,b in d.copy().items():
        if k+w<=W:
            d[k+w]=max(b,d[k]+v)
print(max(d.values()))

Submission status: Wrong Answer

Solution code:
N, W = map(int, input().split())
A = [[int(i) for i in input().split()] for i in range(N)]

def dfs(i, j):
    if i == N:
        return 0
    if A[i][0] > j:
        return 0
    else:
        ans  = max(dfs(i + 1, j), A[i][1] + dfs(i + 1, j - A[i][0]))
    return ans
        
print(dfs(0, W))

Submission status: Wrong Answer

Solution code:
N, W = map(int, input().split())
vw = []
for i in range(N):
    w, v = map(int, input().split())
    vw.append((v, w))
w1 = vw[0][1]
#print(w1, vw)
vw.sort()
#print(vw)
ans = 0
for n in range(1, N + 1):
    if n * (w1 + 3) <= W:
        ans = max(ans, sum([x[0] for x in vw[-n:]]))
        #print(n, ans)
        continue
    if n * w1 > W:
        break
    W2 = W - n * (w1 - 1)
    dp = [[(0, 0) for _ in range(W2 + 1)] for _ in range(N + 1)]
    for i in range(N):
        for j in range(W2 + 1):
            w = vw[i][1] - w1 + 1
            if j < w or dp[i][j - w][1] == n:
                dp[i + 1][j] = dp[i][j]
            else:
                v1 = dp[i][j][0]
                v2 = dp[i][j - w][0] + vw[i][0]
                if v1 >= v2:
                    dp[i + 1][j] = dp[i][j]
                else:
                    dp[i + 1][j] = (dp[i][j - w][0] + vw[i][0], dp[i][j - w][1] + 1)
    ans = max(ans, dp[N][W2][0])
    #print(n, ans, dp)
print(ans)

Submission status: Wrong Answer

Solution code:
n,w = map(int, input().split())
w4 = [[] for i in range(4)]
w1 = None
for i in range(n):
    ws, vs = map(int,input().split())
    if i == 0:
        w1 = ws
    ws -= w1
    w4[ws].append(vs)

for i in range(4):
    w4[i].sort(reverse=True)
    tmp = [0]
    for j in w4[i]:
        tmp.append(tmp[-1]+j)
    w4[i] = tmp
ans = 0
for i in range(len(w4[0])):
    for j in range(len(w4[1])):
        for k in range(len(w4[2])):
            for l in range(len(w4[3])):
                if w >= w1*(i+j+k+l) + j + 2*k + 3*l:
                    print(i,j,k,l)
                    ans = max(ans, w4[0][i]+w4[1][j]+w4[2][k]+w4[3][l])
print(ans)

Submission status: Wrong Answer

Solution code:
N,W=map(int,input().split())
wv=[ tuple([int(j) for j in input().split()]) for _ in range(N) ]
w1=wv[0][0]
w2=w1+1
w3=w1+2 
w4=w1+3

wv=sorted(wv,key=lambda x:x[1],reverse=True)
wv=sorted(wv)
wvv = list(map(lambda x:x[1], wv))

# print(wv)
inf = float("inf")
ans=0
from collections import defaultdict
counter = defaultdict(int)

for i in range(N):
    w,v = wv[i]
    counter[w] += 1

from bisect import bisect_left as bll

for e1 in range(0, counter[w1]+1):
  for e2 in range(0, counter[w2]+1):
    for e3 in range(0,counter[w3]+1):
      for e4 in range(0, counter[w4]+1):
          tmp=0

          if w1*e1 + w2*e2 + w3*e3 + w4*e4 > W:
              continue

          i1=bll(wv, (w1,-inf))
          i2=bll(wv, (w2,-inf))
          i3=bll(wv, (w3,-inf))
          i4=bll(wv, (w4,-inf))

          tmp= tmp \
             +sum(wvv[i1:i1+e1])  \
             +sum(wvv[i2:i2+e2])  \
             +sum(wvv[i3:i3+e3])  \
             +sum(wvv[i4:i4+e4])

          ans=max(ans,tmp)
      
print(ans)

Submission status: Wrong Answer

Solution code:
N,W=map(int,input().split())
wv=[ tuple([int(j) for j in input().split()]) for _ in range(N) ]
w1=wv[0][0]
w2=w1+1
w3=w2+1 
w4=w3+1
wv=sorted(wv,key=lambda x:x[1],reverse=True)
wv=sorted(wv)
wvv = list(map(lambda x:x[1], wv))

inf = float("inf")
ans=0
from collections import defaultdict
counter = defaultdict(int)

for i in range(N):
    w,v = wv[i]
    counter[w] += 1

from bisect import bisect_left as bll

for e1 in range(0, counter[w1]+1):
  for e2 in range(0, counter[w2]+1):
    for e3 in range(0,counter[w3]+1):
      for e4 in range(0, counter[w4]+1):
          tmp=0

          if w1*e1 + w2*e2 + w3*e3 + w4*e4 > W:
              continue

          i1=bll(wv, (w1,-inf))
          i2=bll(wv, (w2,-inf))
          i3=bll(wv, (w3,-inf))
          i4=bll(wv, (w4,-inf))

          tmp+=sum(wvv[i1:i1+e1])  \
             +sum( wvv[i2:i2+e2])  \
             +sum(wvv[i3:i3+e3])  \
            +sum(wvv [i4:i4+e4])

          ans=max(ans,tmp)
      
print(ans)

Submission status: Wrong Answer

Solution code:
N,W=map(int,input().split())
wv=[ tuple([int(j) for j in input().split()]) for _ in range(N) ]
w0=wv[0][0]
w1=w0+1
w2=w1+1 
w3=w2+1
wv=sorted(wv,key=lambda x:x[1],reverse=True)
wv=sorted(wv)
wvv = list(map(lambda x:x[1], wv))
ww = list(map(lambda x:x[0], wv))

# print(wv)

inf = float("inf")
ans=0
from collections import defaultdict
counter = defaultdict(int)

for i in range(N):
    w,v = wv[i]
    counter[w] += 1

from bisect import bisect_left as bll
for e1 in range(0, counter[w0]+1):
  l2=W-w0*e1
  for e2 in range(0, counter[w1]+1):
    l3=l2-(w0+1)*e2
    for e3 in range(0,counter[w2]+1):
      l4=l3- (w0+2)*e3
      for e4 in range(0, counter[w3]+1):
          tmp=0

          if w0*e1 + w1*e2 + w2*e3 + w3*e4 > W:
              continue
          # print("e1: {}, e2: {}, e3: {}, e4: {}".format(e1, e2, e3, e4))

          i1=bll(wv, (w0,-inf))
          i2=bll(wv, (w1,-inf))
          i3=bll(wv, (w2,-inf))
          i4=bll(wv, (w3,-inf))
          # print("i1: {}, i2: {}, i3: {}, i4: {}".format(i1, i2, i3, i4))
          tmp+=sum(wvv[i1:i1+e1])  \
             +sum( wvv[i2:i2+e2])  \
             +sum(wvv[i3:i3+e3])  \
            +sum(wvv [i4:i4+e4])
          ans=max(ans,tmp)
      
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
import heapq
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, W = map(int, input().split())
first_item = list(map(int, input().split()))
basew = first_item[0]
w0 = []
w1 = []
w2 = []
w3 = []
heapq.heappush(w0, first_item[1])
for _ in range(N-1):
    w, v = map(int, input().split())
    w -= basew
    if w == 0:
        heapq.heappush(w0, v)
    elif w == 1:
        heapq.heappush(w1, v)
    elif w == 2:
        heapq.heappush(w2, v)
    elif w == 3:
        heapq.heappush(w3, v)

w0_acc = [0]+list(accumulate(reversed(w0)))
w1_acc = [0]+list(accumulate(reversed(w1)))
w2_acc = [0]+list(accumulate(reversed(w2)))
w3_acc = [0]+list(accumulate(reversed(w3)))
v = 0
for i0 in range(len(w0)):
    for i1 in range(len(w1)):
        for i2 in range(len(w2)):
            for i3 in range(len(w3)):
                if basew*(i0+i1+i2+i3)+3*i3 + 2*i2 + i1 > W:
                    break
                else:
                    v = max(v, w0_acc[i0]+w1_acc[i1]+w2_acc[i2]+w3_acc[i3])
print(v)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
import heapq
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, W = map(int, input().split())
first_item = list(map(int, input().split()))
basew = first_item[0]
w0 = []
w1 = []
w2 = []
w3 = []
heapq.heappush(w0, first_item[1])
for _ in range(N-1):
    w, v = map(int, input().split())
    w -= basew
    if w == 0:
        heapq.heappush(w0, v)
    elif w == 1:
        heapq.heappush(w1, v)
    elif w == 2:
        heapq.heappush(w2, v)
    else:
        heapq.heappush(w3, v)

w0_acc = [0]+list(accumulate(reversed(w0)))
w1_acc = [0]+list(accumulate(reversed(w1)))
w2_acc = [0]+list(accumulate(reversed(w2)))
w3_acc = [0]+list(accumulate(reversed(w3)))
v = 0
for i0 in range(N+1):
    if i0 > len(w0):
        break
    for i1 in range(N+1):
        if i1 > len(w1):
            break
        for i2 in range(N+1):
            if i2 > len(w2):
                break
            for i3 in range(N+1):
                #print("i->", i0, i1, i2, i3)
                if i3 > len(w3):
                    break
                if basew*(i0+i1+i2+i3)+3*i3 + 2*i2 + i1 > W:
                    # print("over_weight")
                    break
                else:
                    v = max(v, w0_acc[i0]+w1_acc[i1]+w2_acc[i2]+w3_acc[i3])
print(v)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
import heapq
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, W = map(int, input().split())
first_item = list(map(int, input().split()))
basew = first_item[0]
w0 = []
w1 = []
w2 = []
w3 = []
heapq.heappush(w0, first_item[1])
for _ in range(N-1):
    w, v = map(int, input().split())
    w -= basew
    if w == 0:
        heapq.heappush(w0, v)
    elif w == 1:
        heapq.heappush(w1, v)
    elif w == 2:
        heapq.heappush(w2, v)
    else:
        heapq.heappush(w3, v)

w0_acc = [0]+list(accumulate(reversed(w0)))
w1_acc = [0]+list(accumulate(reversed(w1)))
w2_acc = [0]+list(accumulate(reversed(w2)))
w3_acc = [0]+list(accumulate(reversed(w3)))
v = 0
for i0 in range(N+1):
    if i0 > len(w0):
        break
    for i1 in range(N-i0+1):
        if i1 > len(w1):
            break
        for i2 in range(N-i0-i1+1):
            if i2 > len(w2):
                break
            r = i1+i2+i0
            for i3 in range(N-r+1):
                if i3 > len(w3):
                    break
                if basew*(r+i3)+3*i3 + 2*i2 + i1 > W:
                    break
                else:
                    v = max(v, w0_acc[i0]+w1_acc[i1]+w2_acc[i2]+w3_acc[i3])
print(v)

Submission status: Wrong Answer

Solution code:
N, W = (int(i) for i in input().split())
w = [0]*N
v = [0]*N
for i in range(N):
  w[i], v[i] = (int(j) for j in input().split())
w0 = w[0]
wh = [a - w0 for a in w]
dp = [[[-1]*(N+1) for i in range(3*N+1)] for j in range(N+1)]
dp[0][0][0] = 0
for i in range(N):
  for j in range(3*N+1):
    for k in range(N+1):
      if dp[i][j][k] != -1:
        dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
      if j - wh[i] >=0 and k-1 >=0 and dp[i][j-wh[i]][k-1] != -1:
        dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j-wh[i]][k-1]+v[i])
res = 0
for k in range(N+1):
  if w0*k <= W <= (w0+3)*k:
    Wk = W - w0*k
    ans = 0
    for j in range(Wk+1):
      ans = max(dp[N][j][k], ans)
    res = max(res, ans)
print(res)

Submission status: Wrong Answer

Solution code:
N, W = (int(i) for i in input().split())
w = [0]*N
v = [0]*N
for i in range(N):
  w[i], v[i] = (int(j) for j in input().split())
w0 = w[0]
wh = [a - w0 for a in w]
dp = [[[0]*(N+1) for i in range(3*N+1)] for j in range(N+1)]
for i in range(N):
  for j in range(3*N+1):
    for k in range(N+1):
      dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
      if j - wh[i] >=0 and k-1 >=0:
        dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j-wh[i]][k-1]+v[i])
res = 0
for k in range(N+1):
  if w0*k <= W <= (w0+3)*k:
    Wk = W - w0*k
    ans = 0
    for j in range(Wk+1):
      ans = max(dp[N][j][k], ans)
    res = max(res, ans)
print(res)

Submission status: Wrong Answer

Solution code:
N, W = (int(i) for i in input().split())
w = [0]*N
v = [0]*N
for i in range(N):
  w[i], v[i] = (int(j) for j in input().split())
w0 = w[0]
wh = [a - w0 for a in w]
dp = [[[0]*(N+1) for i in range(3*N+1)] for j in range(N+1)]
for i in range(N):
  for j in range(3*N+1):
    for k in range(N+1):
      dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k])
      if j - wh[i] >=0 and k-1 >=0:
        dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j-wh[i]][k-1]+v[i])
res = 0
for k in range(N+1):
  if w0*k <= W <= (w0+3)*k:
    print(k, w0, W)
    Wk = W - w0*k
    ans = 0
    for j in range(Wk+1):
      ans = max(dp[N][j][k], ans)
    res = max(res, ans)
print(res)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
n,ww = map(int,raw_input().split())
dic = defaultdict(list)
m = 10**20
for i in range(n):
    w,v = map(int,raw_input().split())
    m = min(w,m)
    dic[w].append(v)
mxv = 0
for i in dic:
    dic[i].sort(reverse = True)
for i in range(len(dic[m+2])+1):
    for j in range(len(dic[m+1])+1):
        for k in range(len(dic[m])+1):
            tw = i*(m+2)+j*(m+1)+k*(m)
            if tw > ww:
                continue
            else:
                val = sum(dic[m+2][:i])+sum(dic[m+1][:j])+sum(dic[m][:k])
                mxv = max(mxv,val)
print mxv

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy

from math import *

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, W] =  [long(input_raw[0]), long(input_raw[1])]
    v = []
    item = {}
    v_sum = {}
    for i in range(N):
        input_raw = read_func().strip().split()
        [wi, vi] =  [long(input_raw[0]), long(input_raw[1])]
        if wi not in item:
            item[wi] = [vi]
        else:
            item[wi].append(vi)

    for i in range(4 - len(item)):
        item[long(-(i + 1))] = [0L]

    w_list = []
    for wi in item:
        item[wi].sort(reverse = True)
        w_list.append(wi)
        v_sum[wi] = [0L for i in range(len(item[wi]) + 1)]
        v_sum[wi][1] = item[wi][0]
        for i in range(1, len(item[wi])):
            v_sum[wi][i + 1] = v_sum[wi][i] + item[wi][i]

    max_value = 0L
    for i in range(len(item[w_list[0]]) + 1):
        for j in range(len(item[w_list[1]]) + 1):
            for k in range(len(item[w_list[2]]) + 1):
                for l in range(len(item[w_list[3]]) + 1):
                    if W - (i * max(0L, w_list[0]) + j * max(0L, w_list[1]) + k * max(0L, w_list[2]) + l * max(0L, w_list[3])) >= 0:
                        value = v_sum[w_list[0]][i] + v_sum[w_list[1]][j] + v_sum[w_list[2]][k] + v_sum[w_list[3]][k]
                        if max_value < value:
                            max_value = value
    print max_value

##    W_ = long(N * 4 + 1)
##    dp = [[0L for j in range((W_) + 1)] for i in range(N + 1)]
##
##    for i in range(N):
##        for j in range(W_ + 1):
##
##            k = ((j - 1)%4 +  ((j - 1)/4 +1) * w[0]) - w[i]
##            #print k, j, w[i]
##            if k  < 0 or j == 0:
##                dp[i + 1][j] = dp[i][j]
##            else:
##                if k >= w[0]:
##                    l = k / w[0] + 3 * (k / w[0] - 1)
##                else:
##                    l = 0
##                print i ,w[i], k, l
##                dp[i + 1][j] = max(dp[i][j], dp[i][l] + v[i])
##
##    maximum = 0
##    for i in range(1, W_ + 1):
##        if maximum < dp[N][i] and (i - 1)%4 +  ((i - 1)/4 +1) * w[0] <= W:
##            maximum = dp[N][i]
##
##    print maximum


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy

from math import *

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, W] =  [long(input_raw[0]), long(input_raw[1])]
    v = []
    item = {}
    v_sum = {}
    for i in range(N):
        input_raw = read_func().strip().split()
        [wi, vi] =  [long(input_raw[0]), long(input_raw[1])]
        if wi not in item:
            item[wi] = [vi]
        else:
            item[wi].append(vi)

    for i in range(4 - len(item)):
        item[long(-(i + 1))] = [0L]

    w_list = []
    for wi in item:
        item[wi].sort(reverse = True)
        w_list.append(wi)
        v_sum[wi] = [0L for i in range(len(item[wi]) + 1)]
        v_sum[wi][1] = item[wi][0]
        for i in range(1, len(item[wi])):
            v_sum[wi][i + 1] = v_sum[wi][i] + item[wi][i]

    max_value = 0L
    for i in range(len(item[w_list[0]]) + 1):
        for j in range(len(item[w_list[1]]) + 1):
            for k in range(len(item[w_list[2]]) + 1):
                for l in range(len(item[w_list[3]]) + 1):
                    if W - (i * max(0, w_list[0]) + j * max(0L, w_list[1]) + k * max(0L, w_list[2]) + l * max(0L, w_list[3])) >= 0:
                        value = v_sum[w_list[0]][i] + v_sum[w_list[1]][j] + v_sum[w_list[2]][k] + v_sum[w_list[3]][k]
                        if max_value < value:
                            max_value = value
    print max_value

##    W_ = long(N * 4 + 1)
##    dp = [[0L for j in range((W_) + 1)] for i in range(N + 1)]
##
##    for i in range(N):
##        for j in range(W_ + 1):
##
##            k = ((j - 1)%4 +  ((j - 1)/4 +1) * w[0]) - w[i]
##            #print k, j, w[i]
##            if k  < 0 or j == 0:
##                dp[i + 1][j] = dp[i][j]
##            else:
##                if k >= w[0]:
##                    l = k / w[0] + 3 * (k / w[0] - 1)
##                else:
##                    l = 0
##                print i ,w[i], k, l
##                dp[i + 1][j] = max(dp[i][j], dp[i][l] + v[i])
##
##    maximum = 0
##    for i in range(1, W_ + 1):
##        if maximum < dp[N][i] and (i - 1)%4 +  ((i - 1)/4 +1) * w[0] <= W:
##            maximum = dp[N][i]
##
##    print maximum


if __name__ == '__main__':
    main()