N,V=map(int,input().split())
goods=[]
for i in range(N):
ni,vi,si=map(int,input().split())
if si ==0:
goods.append([ni,vi,si])
elif si==-1:
goods.append([ni,vi,1])
else:
k=1
while si-k>0:
goods.append([nik,vik,k])
si-=k
k=2
if si>0:
goods.append([nisi,visi,si])
dp=[0](V+1)
for i in range(len(goods)):
if goods[i][2]==0:
for j in range(goods[i][0],V+1):
dp[j]=max(dp[j],dp[j-goods[i][0]]+goods[i][1])
else:
for j in range(V,goods[i][0]-1,-1):
dp[j]=max(dp[j],dp[j-goods[i][0]]+goods[i][1])
print(dp[-1])