import sys
input = sys.stdin.read
data = input().split()
idx = 0
n = int(data[idx])
idx += 1
T = int(data[idx])
idx += 1
type1 = []
type2 = []
for i in range(n):
s = int(data[idx])
v = int(data[idx + 1])
p = int(data[idx + 2])
idx += 3
if p == 1:
type1.append((s, v))
else:
type2.append((s, v))
# 對 type1 按 rate v/s 降序排序 (使用 cross multiply 避免浮點)
type1.sort(key=lambda x: -x[1] * x[0] if x[0] > 0 else 0, reverse=True) # 錯誤,改用 tuple
# 正確排序
def rate_key(girl):
s, v = girl
if s == 0:
return 0
return v * 10000000000 // s # 粗略,但為了穩定用 cross
# 更好的方式: 使用 functools
from functools import cmp_to_key
def cmp(a, b):
# return positive if a better than b (higher rate)
sa, va = a
sb, vb = b
if sa == 0 or sb == 0:
return 0
return va * sb - vb * sa # 正的表示 a 更好
type1.sort(key=cmp_to_key(lambda a,b: cmp(b,a))) # 降序
# 現在 type1 是 rate 降序
# 預計算 type1 的 prefix
prefix_s = [0]
prefix_v = [0]
for s, v in type1:
prefix_s.append(prefix_s[-1] + s)
prefix_v.append(prefix_v[-1] + v)
total_s1 = prefix_s[-1]
total_v1 = prefix_v[-1]
# 計算給定剩餘時間 r 時,type1 能得到的最大好感度 (含四捨五入)
def get_type1_value(r):
if r <= 0:
return 0
if r >= total_s1:
return total_v1 # 已經滿了,無法再多
# 找到前 k 個 full + fraction
# 使用 loop 因為 n<=1e4
curr_time = 0
curr_val = 0
for i in range(len(type1)):
s, v = type1[i]
if curr_time + s <= r:
curr_time += s
curr_val += v
else:
# fraction
remain = r - curr_time
if s > 0:
full = (remain * v) // s
frac = (remain * v) % s
extra = 1 if 2 * frac >= s else 0
curr_val += full + extra
break
return curr_val
# 現在處理 type2
sum_s2 = sum(s for s,_ in type2)
sum_v2 = sum(v for _,v in type2)
if T >= total_s1 + sum_s2:
# 可以拿全部
print(total_v1 + sum_v2)
else:
# 需要 DP
# 為了避免太大,我們限制 DP 容量為 min(T, sum_s2 + 1)
dp_cap = min(T, sum_s2) + 1
dp = [0] * dp_cap
for s, v in type2:
if s > dp_cap - 1:
continue
for j in range(dp_cap - 1, s - 1, -1):
dp[j] = max(dp[j], dp[j - s] + v)
max_ans = 0
# 枚舉 type2 使用的時間 k
for k in range(dp_cap):
v2 = dp[k]
r = T - k
v1 = get_type1_value(r)
max_ans = max(max_ans, v2 + v1)
# 只用 type1
max_ans = max(max_ans, get_type1_value(T))
print(max_ans)