Language: C++

#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define mp make_pair
#define fi first
#define se second
#define pii pair<ll,ll>
#define pb push_back
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
ll psum[100005]={0};
ll l[100005];
ll u[100005];
ll b[100005];
ll num[100005];
ll d1;
ll n,x;
ll p(ll k)
{
	ll f1=k/x;
	ll p1=k-x*f1;
	ll t=0;
	for(ll i=0;i<n;i++)
	{
		if(num[i]>f1-1)
		{
			if(psum[f1]+min(l[i]*b[i],p1*l[i])+max(0LL,(p1-b[i])*u[i])+d1>=0)
			return 1;
		}
		else
		{
			if(psum[f1+1]-(l[i]*b[i]+(x-b[i])*u[i])+min(l[i]*b[i],p1*l[i])+max(0LL,(p1-b[i])*u[i])+d1>=0)
			return 1;
		}
	}
	return 0;
		
}
ll binsearch(ll low,ll high)
{
	if(low==high)
	{
		if(p(low)==1)
		return low;
		else return -1;
	}
	ll mid=(low+high)/2;
	if(p(mid)==1)
	return binsearch(low,mid);
	else
	return binsearch(mid+1,high);
}
int main()
{
	io;
	//ll n,x;
	cin>>n>>x;
	//ll b[n],l[n],u[n];
	//ll d1=0;
	ll t=0;
	vector<pii> val;
	for(ll i=0;i<n;i++)
	{
		cin>>b[i]>>l[i]>>u[i];
		d1-=l[i]*b[i];
		val.pb(mp(l[i]*b[i]+(x-b[i])*u[i],i));
		t+=b[i];
	}
	sort(val.rbegin(),val.rend());
	//ll psum[n+1];
	psum[0]=0;
	for(ll i=0;i<n;i++)
	{
		psum[i+1]=psum[i]+val[i].fi;
		num[val[i].se]=i;
	}
	ll ans=binsearch(0,t);
	cout<<ans;
}
	#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define mp make_pair
#define fi first
#define se second
#define pii pair<ll,ll>
#define pb push_back
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
ll psum[100005]={0};
ll l[100005];
ll u[100005];
ll b[100005];
ll num[100005];
ll d1;
ll n,x;
ll p(ll k)
{
	ll f1=k/x;
	ll p1=k-x*f1;
	ll t=0;
	for(ll i=0;i<n;i++)
	{
		if(num[i]>f1-1)
		{
			if(psum[f1]+min(l[i]*b[i],p1*l[i])+max(0LL,(p1-b[i])*u[i])+d1>=0)
			return 1;
		}
		else
		{
			if(psum[f1+1]-(l[i]*b[i]+(x-b[i])*u[i])+min(l[i]*b[i],p1*l[i])+max(0LL,(p1-b[i])*u[i])+d1>=0)
			return 1;
		}
	}
	return 0;
		
}
ll binsearch(ll low,ll high)
{
	if(low==high)
	{
		if(p(low)==1)
		return low;
		else return -1;
	}
	ll mid=(low+high)/2;
	if(p(mid)==1)
	return binsearch(low,mid);
	else
	return binsearch(mid+1,high);
}
int main()
{
	io;
	//ll n,x;
	cin>>n>>x;
	//ll b[n],l[n],u[n];
	//ll d1=0;
	ll t=0;
	vector<pii> val;
	for(ll i=0;i<n;i++)
	{
		cin>>b[i]>>l[i]>>u[i];
		d1-=l[i]*b[i];
		val.pb(mp(l[i]*b[i]+(x-b[i])*u[i],i));
		t+=b[i];
	}
	sort(val.rbegin(),val.rend());
	//ll psum[n+1];
	psum[0]=0;
	for(ll i=0;i<n;i++)
	{
		psum[i+1]=psum[i]+val[i].fi;
		num[val[i].se]=i;
	}
	ll ans=binsearch(0,t);
	cout<<ans;
}
	

Compiler output:
a.cc:88:4: error: redefinition of 'long long int psum [100005]'
   88 | ll psum[100005]={0};
      |    ^~~~
a.cc:10:4: note: 'long long int psum [100005]' previously defined here
   10 | ll psum[100005]={0};
      |    ^~~~
a.cc:89:4: error: redefinition of 'long long int l [100005]'
   89 | ll l[100005];
      |    ^
a.cc:11:4: note: 'long long int l [100005]' previously declared here
   11 | ll l[100005];
      |    ^
a.cc:90:4: error: redefinition of 'long long int u [100005]'
   90 | ll u[100005];
      |    ^
a.cc:12:4: note: 'long long int u [100005]' previously declared here
   12 | ll u[100005];
      |    ^
a.cc:91:4: error: redefinition of 'long long int b [100005]'
   91 | ll b[100005];
      |    ^
a.cc:13:4: note: 'long long int b [100005]' previously declared here
   13 | ll b[100005];
      |    ^
a.cc:92:4: error: redefinition of 'long long int num [100005]'
   92 | ll num[100005];
      |    ^~~
a.cc:14:4: note: 'long long int num [100005]' previously declared here
   14 | ll num[100005];
      |    ^~~
a.cc:93:4: error: redefinition of 'long long int d1'
   93 | ll d1;
      |    ^~
a.cc:15:4: note: 'long long int d1' previously declared here
   15 | ll d1;
      |    ^~
a.cc:94:4: error: redefinition of 'long long int n'
   94 | ll n,x;
      |    ^
a.cc:16:4: note: 'long long int n' previously declared here
   16 | ll n,x;
      |    ^
a.cc:94:6: error: redefinition of 'long long int x'
   94 | ll n,x;
      |      ^
a.cc:16:6: note: 'long long int x' previously declared here
   16 | ll n,x;
      |      ^
a.cc:95:4: error: redefinition of 'long long int p(long long int)'
   95 | ll p(ll k)
      |    ^
a.cc:17:4: note: 'long long int p(long long int)' previously defined here
   17 | ll p(ll k)
      |    ^
a.cc:116:4: error: redefinition of 'long long int binsearch(long long int, long long int)'
  116 | ll binsearch(ll low,ll high)
      |    ^~~~~~~~~
a.cc:38:4: note: 'long long int binsearch(long long int, long long int)' previously defined here
   38 | ll binsearch(ll low,ll high)
      |    ^~~~~~~~~
a.cc:130:5: error: redefinition of 'int main()'
  130 | int main()
      |     ^~~~
a.cc:52:5: note: 'int main()' previously defined here
   52 | int main()
      |     ^~~~