Buggy python submission (verdict: Time Limit Exceeded):
N,X=map(int,input().split())
above=[0]*N
now=0
data=[0]*N
for i in range(N):
    b,l,u=map(int,input().split())
    above[i]=(i,b*l+(X-b)*u)
    data[i]=(b,l,u)
    now-=l*b
above.sort(key=lambda x:x[1],reverse=True)
used=set()
ans=0
idx=0
for i in range(N):
    if now+above[i][1]<0:
        now+=above[i][1]
        used|={above[i][0]}
        ans+=X
        idx=i+1
    else:
        break
base=ans
ans+=X
now*=-1
for i in range(N):
    b,l,u=data[i]
    if i in used:
        now0=now+b*l+(X-b)*u
        M=base-X
    else:
        now0=now
        M=base
    for i in range(idx,N):
        if now0+above[i][1]<0:
            now+=above[i][1]
    if now0<=b*l:
        ans=min(ans,M+(now0+l-1)//l)
    elif now0<=b*l+(X-b)*u:
        ans=min(ans,M+b+(now0-b*l+u-1)//u)
print(ans)

Fixed submission:
N,X=map(int,input().split())
data=[0]*N
A=[0]*N
zero=0
for i in range(N):
    b,l,u=map(int,input().split())
    zero-=l*b
    data[i]=(b,l,u)
    A[i]=(i,(X-b)*u+b*l)
A.sort(key=lambda x:x[1],reverse=True)
S=[0]*N
idx=[0]*N
for i,a in enumerate(A):
    S[i]=S[i-1]+a[1]
    idx[a[0]]=i
def check(k):
    q=k//X
    r=k-q*X
    for i in range(N):
        b,l,u=data[i]
        if q==0:
            now=zero
        elif idx[i]<q:
            now=zero+S[q]-A[idx[i]][1]
        else:
            now=zero+S[q-1]
        if r<=b:
            now+=r*l
        else:
            now+=(r-b)*u+b*l
        if now>=0:
            return True
    return False
Mincan=N*X
Maxcant=-1
c=(1,2)
while True:
    if Maxcant+1==Mincan:
        ans=Mincan
        break
    x=N*X*c[0]//c[1]
    if check(x):
        if Mincan>x:
            Mincan=x
            c=(c[0]*2-1,c[1]*2)
    else:
        if Maxcant<x:
            Maxcant=x
            c=(c[0]*2+1,c[1]*2)
print(ans)

Buggy python submission (verdict: Runtime Error):
N,X=map(int,input().split())
above=[0]*N
under=[0]*N
now=0
data=[0]*N
for i in range(N):
    b,l,u=map(int,input().split())
    above[i]=(b*l+(X-b)*u)
    data[i]=(i,b,l,u)
    under[i]=(i,l*b)
    
    now-=l*b
above.sort(key=lambda x:x[1],reverse=True)
idx=0
used=set()
ans=0
while above[idx][1]+now<0:
    now+=above[idx][1]
    used|={above[idx][0]}
    idx+=1
    ans+=X
M=float('inf')
now*=-1
ansM=i
nowM=now
for i in range(N):
    if i in used:
        continue
    b,l,u=data[i]
    M0=(now+l-1)//l
    if M0>b:
        now0=now-l*b
        M0=b+(now0+u-1)//u
        if M0<=X and M0<M:
            M=M0
            ansM=i
            nowM=now0-(M0-b)*u
    elif M0<M:
        M=M0
        ansM=i
        nowM=now-M0*l
ans+=M
nowM*=-1
check=[data[i] for i in used|{ansM}]
check.sort(key=lambda x:x[2])
idx=0
while idx<len(check):
    if nowM-check[idx][2]>=0:
        nowM-=check[idx][2]
        ans-=1
    else:
        idx+=1
print(ans)

Fixed submission:
N,X=map(int,input().split())
data=[0]*N
A=[0]*N
zero=0
for i in range(N):
    b,l,u=map(int,input().split())
    zero-=l*b
    data[i]=(b,l,u)
    A[i]=(i,(X-b)*u+b*l)
A.sort(key=lambda x:x[1],reverse=True)
S=[0]*N
idx=[0]*N
for i,a in enumerate(A):
    S[i]=S[i-1]+a[1]
    idx[a[0]]=i
def check(k):
    q=k//X
    r=k-q*X
    for i in range(N):
        b,l,u=data[i]
        if q==0:
            now=zero
        elif idx[i]<q:
            now=zero+S[q]-A[idx[i]][1]
        else:
            now=zero+S[q-1]
        if r<=b:
            now+=r*l
        else:
            now+=(r-b)*u+b*l
        if now>=0:
            return True
    return False
Mincan=N*X
Maxcant=-1
c=(1,2)
while True:
    if Maxcant+1==Mincan:
        ans=Mincan
        break
    x=N*X*c[0]//c[1]
    if check(x):
        if Mincan>x:
            Mincan=x
            c=(c[0]*2-1,c[1]*2)
    else:
        if Maxcant<x:
            Maxcant=x
            c=(c[0]*2+1,c[1]*2)
print(ans)