#include<bits/stdc++.h>
using namespace std;
const int N=100004;
struct node{
    long long int cnt,a1,b1;
    long long diff;
} q[N];
int n,sli;
bool compare(node p1,node p2)
{
    return p1.diff>p2.diff;
}
long long fun(long long int n1,long long int n2)
{
    n1=1LL*n1*sli;
    n2=1LL*n2*sli;
    long long sum=0;
    int i=1,j=n;
    while(i<=j)
    {
        if(q[i].diff>=0)
        {
            if(n1>=q[i].cnt)
            {
                n1=n1-q[i].cnt;
                sum=sum+1LL*q[i].a1*q[i].cnt;
            }
            else
            {
                sum=sum+1LL*q[i].a1*n1;
                int val=q[i].cnt-n1;
                n1=0;
                n2=n2-val;
                sum=sum+1LL*val*q[i].b1;
            }
            i++;
        }
        else
        {
            if(n2>=q[j].cnt)
            {
                n2=n2-q[j].cnt;
                sum=sum+1LL*q[j].cnt*q[j].b1;
            }
            else
            {
                sum=sum+1LL*q[j].b1*n2;
                int val=q[j].cnt-n2;
                n2=0;
                n1=n1-val;
                sum=sum+1LL*val*q[j].a1;
            }
            j--;
        }

    }
    return sum;
}
int main()
{

   scanf("%d %d",&n,&sli);
   int i;
   long long sum=0;
   for(i=1;i<=n;i++)
   {   long long a,b,s;
       scanf("%I64d %I64d %I64d",&s,&a,&b);
       q[i].cnt=s;
       q[i].a1=a;
       q[i].b1=b;
       q[i].diff=(a-b);
       sum=sum+s;
    }
    sort(q+1,q+n+1,compare);
    int tot=(sum/sli)+((sum%sli>0)?1:0);
    int l=0;
    int r=tot;
    int n1,n2;
    long long ans=0,sum1,sum2;
    while(l<=r)
    {
        n1=l+(r-l)/3;
        n2=r-(r-l)/3;
        sum1=fun(n1,tot-n1);
        sum2=fun(n2,tot-n2);
        if(sum1 <= sum2)
        {
            ans=sum2;
            l=n1+1;
        }
        else
        {
            ans=sum1;
            r=n2-1;
        }
    }
    printf("%I64d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100004;
struct node{
    long long int cnt,a1,b1;
    long long diff;
} q[N];
int a[N],b[N],s[N];
int n,sli;
bool compare(node p1,node p2)
{
    return p1.diff>p2.diff;
}
long long fun(long long int n1,long long int n2)
{
    n1=1LL*n1*sli;
    n2=1LL*n2*sli;
    long long sum=0;
    int i=1,j=n;
    while(i<=j)
    {
        if(q[i].diff>0)
        {
            if(n1>=q[i].cnt)
            {
                n1=n1-q[i].cnt;
                sum=sum+1LL*q[i].a1*q[i].cnt;
            }
            else
            {
                sum=sum+1LL*q[i].a1*n1;
                int val=q[i].cnt-n1;
                n1=0;
                n2=n2-val;
                sum=sum+1LL*val*q[i].b1;
            }
            i++;
        }
        else
        {
            if(n2>=q[j].cnt)
            {
                n2=n2-q[j].cnt;
                sum=sum+1LL*q[j].cnt*q[j].b1;
            }
            else
            {
                sum=sum+1LL*q[j].b1*n2;
                int val=q[j].cnt-n2;
                n2=0;
                n1=n1-val;
                sum=sum+1LL*val*q[j].a1;
            }
            j--;
        }

    }
    return sum;
}
int main()
{

   scanf("%d %d",&n,&sli);
   int i;
   long long sum=0;
   for(i=1;i<=n;i++)
   {
       scanf("%d %d %d",&s[i],&a[i],&b[i]);
       q[i].cnt=s[i];
       q[i].a1=a[i];
       q[i].b1=b[i];
       q[i].diff=1LL*s[i]*(a[i]-b[i]);
       sum=sum+s[i];
    }
    sort(q+1,q+n+1,compare);
    int tot=(sum/sli)+((sum%sli>0)?1:0);
    int l=0;
    int r=tot;
    int n1,n2;
    long long ans=0,sum1,sum2;
    while(l<=r)
    {
        n1=l+(r-l)/3;
        n2=r-(r-l)/3;
        sum1=fun(n1,tot-n1);
        sum2=fun(n2,tot-n2);
        if(sum1 <= sum2)
        {
            ans=sum2;
            l=n1+1;
        }
        else
        {
            ans=sum1;
            r=n2-1;
        }
    }
    printf("%I64d\n",ans);
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
const int N=100004;
struct node{
    long long int cnt,a1,b1;
    long long diff;
} q[N];
int a[N],b[N],s[N];
int n,sli;
bool compare(node p1,node p2)
{
    return p1.diff>p2.diff;
}
long long fun(long long int n1,long long int n2)
{
    n1=1LL*n1*sli;
    n2=1LL*n2*sli;
    long long sum=0;
    int i=1,j=n;
    while(i<=j)
    {
        if(q[i].diff>=0)
        {
            if(n1>=q[i].cnt)
            {
                n1=n1-q[i].cnt;
                sum=sum+1LL*q[i].a1*q[i].cnt;
            }
            else
            {
                sum=sum+1LL*q[i].a1*n1;
                int val=q[i].cnt-n1;
                n1=0;
                n2=n2-val;
                sum=sum+1LL*val*q[i].b1;
            }
            i++;
        }
        else
        {
            if(n2>=q[j].cnt)
            {
                n2=n2-q[j].cnt;
                sum=sum+1LL*q[j].cnt*q[j].b1;
            }
            else
            {
                sum=sum+1LL*q[j].b1*n2;
                int val=q[j].cnt-n2;
                n2=0;
                n1=n1-val;
                sum=sum+1LL*val*q[j].a1;
            }
            j--;
        }

    }
    return sum;
}
int main()
{

   scanf("%d %d",&n,&sli);
   int i;
   long long sum=0;
   for(i=1;i<=n;i++)
   {
       scanf("%d %d %d",&s[i],&a[i],&b[i]);
       q[i].cnt=s[i];
       q[i].a1=a[i];
       q[i].b1=b[i];
       q[i].diff=(a[i]-b[i]);
       sum=sum+s[i];
    }
    sort(q+1,q+n+1,compare);
    int tot=(sum/sli)+((sum%sli>0)?1:0);
    int l=0;
    int r=tot;
    int n1,n2;
    long long ans=0,sum1,sum2;
    while(l<=r)
    {
        n1=l+(r-l)/3;
        n2=r-(r-l)/3;
        sum1=fun(n1,tot-n1);
        sum2=fun(n2,tot-n2);
        if(sum1 <= sum2)
        {
            ans=sum2;
            l=n1+1;
        }
        else
        {
            ans=sum1;
            r=n2-1;
        }
    }
    printf("%I64d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100004;
struct node{
    long long int cnt,a1,b1;
    long long diff;
} q[N];
int a[N],b[N],s[N];
int n,sli;
bool compare(node p1,node p2)
{
    if(p1.diff==p2.diff)
        return p1.cnt>p2.cnt;
    return p1.diff>p2.diff;
}
long long fun(long long int n1,long long int n2)
{
    n1=1LL*n1*sli;
    n2=1LL*n2*sli;
    long long sum=0;
    int i=1,j=n;
    while(i<=j)
    {
        if(q[i].diff>=0)
        {
            if(n1>=q[i].cnt)
            {
                n1=n1-q[i].cnt;
                sum=sum+1LL*q[i].a1*q[i].cnt;
            }
            else
            {
                sum=sum+1LL*q[i].a1*n1;
                int val=q[i].cnt-n1;
                n1=0;
                n2=n2-val;
                sum=sum+1LL*val*q[i].b1;
            }
            i++;
        }
        else
        {
            if(n2>=q[j].cnt)
            {
                n2=n2-q[j].cnt;
                sum=sum+1LL*q[j].cnt*q[j].b1;
            }
            else
            {
                sum=sum+1LL*q[j].b1*n2;
                int val=q[j].cnt-n2;
                n2=0;
                n1=n1-val;
                sum=sum+1LL*val*q[j].a1;
            }
            j--;
        }

    }
    return sum;
}
int main()
{

   scanf("%d %d",&n,&sli);
   int i;
   long long sum=0;
   for(i=1;i<=n;i++)
   {
       scanf("%d %d %d",&s[i],&a[i],&b[i]);
       q[i].cnt=s[i];
       q[i].a1=a[i];
       q[i].b1=b[i];
       q[i].diff=(s[i]*(a[i]-b[i]))/s[i];
       sum=sum+s[i];
    }
    sort(q+1,q+n+1,compare);
    int tot=(sum/sli)+((sum%sli>0)?1:0);
    int l=0;
    int r=tot;
    int n1,n2;
    long long ans=0,sum1,sum2;
    while(l<=r)
    {
        n1=l+(r-l)/3;
        n2=r-(r-l)/3;
        sum1=fun(n1,tot-n1);
        sum2=fun(n2,tot-n2);
        if(sum1 <= sum2)
        {
            ans=sum2;
            l=n1+1;
        }
        else
        {
            ans=sum1;
            r=n2-1;
        }
    }
    printf("%I64d\n",ans);
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
const int N=100004;
struct node{
    long long int cnt,a1,b1;
    long long diff;
} q[N];
int a[N],b[N],s[N];
int n,sli;
bool compare(node p1,node p2)
{
    return p1.diff>p2.diff;
}
long long fun(long long int n1,long long int n2)
{
    n1=1LL*n1*sli;
    n2=1LL*n2*sli;
    long long sum=0;
    int i=1,j=n;
    while(i<=j)
    {
        if(q[i].diff>=0)
        {
            if(n1>=q[i].cnt)
            {
                n1=n1-q[i].cnt;
                sum=sum+1LL*q[i].a1*q[i].cnt;
            }
            else
            {
                sum=sum+1LL*q[i].a1*n1;
                int val=q[i].cnt-n1;
                n1=0;
                n2=n2-val;
                sum=sum+1LL*val*q[i].b1;
            }
            i++;
        }
        else
        {
            if(n2>=q[j].cnt)
            {
                n2=n2-q[j].cnt;
                sum=sum+1LL*q[j].cnt*q[j].b1;
            }
            else
            {
                sum=sum+1LL*q[j].b1*n2;
                int val=q[j].cnt-n2;
                n2=0;
                n1=n1-val;
                sum=sum+1LL*val*q[j].a1;
            }
            j--;
        }

    }
    return sum;
}
int main()
{

   scanf("%d %d",&n,&sli);
   int i;
   long long sum=0;
   for(i=1;i<=n;i++)
   {
       scanf("%d %d %d",&s[i],&a[i],&b[i]);
       q[i].cnt=s[i];
       q[i].a1=a[i];
       q[i].b1=b[i];
       q[i].diff=1LL*s[i]*(a[i]-b[i]);
       sum=sum+s[i];
    }
    sort(q+1,q+n+1,compare);
    int tot=(sum/sli)+((sum%sli>0)?1:0);
    int l=0;
    int r=tot;
    int n1,n2;
    long long ans=0,sum1,sum2;
    while(l<=r)
    {
        n1=l+(r-l)/3;
        n2=r-(r-l)/3;
        sum1=fun(n1,tot-n1);
        sum2=fun(n2,tot-n2);
        if(sum1 <= sum2)
        {
            ans=sum2;
            l=n1+1;
        }
        else
        {
            ans=sum1;
            r=n2-1;
        }
    }
    printf("%I64d\n",ans);
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
const int N=100004;
struct node{
    int cnt,a1,b1;
    long long diff;
} q[N];
int a[N],b[N],s[N];
int n,sli;
bool compare(node p1,node p2)
{
    return p1.diff>p2.diff;
}
long long fun(long long int n1,long long int n2)
{
    n1=1LL*n1*sli;
    n2=1LL*n2*sli;
    long long sum=0;
    int i=1,j=n;
    while(i<=j)
    {
        if(q[i].diff>0)
        {
            if(n1>=q[i].cnt)
            {
                n1=n1-q[i].cnt;
                sum=sum+1LL*q[i].a1*q[i].cnt;
            }
            else
            {
                sum=sum+1LL*q[i].a1*n1;
                int val=q[i].cnt-n1;
                n1=0;
                n2=n2-val;
                sum=sum+1LL*val*q[i].b1;
            }
            i++;
        }
        else
        {
            if(n2>=q[j].cnt)
            {
                n2=n2-q[j].cnt;
                sum=sum+1LL*q[j].cnt*q[j].b1;
            }
            else
            {
                sum=sum+1LL*q[j].b1*n2;
                int val=q[j].cnt-n2;
                n2=0;
                n1=n1-val;
                sum=sum+1LL*val*q[j].a1;
            }
            j--;
        }

    }
    return sum;
}
int main()
{

   scanf("%d %d",&n,&sli);
   int i;
   long long sum=0;
   for(i=1;i<=n;i++)
   {
       scanf("%d %d %d",&s[i],&a[i],&b[i]);
       q[i].cnt=s[i];
       q[i].a1=a[i];
       q[i].b1=b[i];
       q[i].diff=1LL*s[i]*(a[i]-b[i]);
       sum=sum+s[i];
    }
    sort(q+1,q+n+1,compare);
    int tot=(sum/sli)+((sum%sli>0)?1:0);
    int l=0;
    int r=tot;
    int n1,n2;
    long long ans=0,sum1,sum2;
    while(l<=r)
    {
        n1=l+(r-l)/3;
        n2=r-(r-l)/3;
        sum1=fun(n1,tot-n1);
        sum2=fun(n2,tot-n2);
        if(sum1 < sum2)
        {   ans=sum2;
            l=n1+1;
        }
        else
        {
            ans=sum1;
            r=n2-1;
        }
    }
    printf("%I64d\n",ans);
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
const int N=100004;
struct node{
    long long int cnt,a1,b1;
    long long diff;
} q[N];
int a[N],b[N],s[N];
int n,sli;
bool compare(node p1,node p2)
{
    if(p1.diff==p2.diff)
      return p1.cnt<p2.cnt;
    else
        return p1.diff>p2.diff;
}
long long fun(long long int n1,long long int n2)
{
    n1=1LL*n1*sli;
    n2=1LL*n2*sli;
    long long sum=0;
    int i=1,j=n;
    while(i<=j)
    {
        if(q[i].diff>=0)
        {
            if(n1>=q[i].cnt)
            {
                n1=n1-q[i].cnt;
                sum=sum+1LL*q[i].a1*q[i].cnt;
            }
            else
            {
                sum=sum+1LL*q[i].a1*n1;
                int val=q[i].cnt-n1;
                n1=0;
                n2=n2-val;
                sum=sum+1LL*val*q[i].b1;
            }
            i++;
        }
        else
        {
            if(n2>=q[j].cnt)
            {
                n2=n2-q[j].cnt;
                sum=sum+1LL*q[j].cnt*q[j].b1;
            }
            else
            {
                sum=sum+1LL*q[j].b1*n2;
                int val=q[j].cnt-n2;
                n2=0;
                n1=n1-val;
                sum=sum+1LL*val*q[j].a1;
            }
            j--;
        }

    }
    return sum;
}
int main()
{

   scanf("%d %d",&n,&sli);
   int i;
   long long sum=0;
   for(i=1;i<=n;i++)
   {
       scanf("%d %d %d",&s[i],&a[i],&b[i]);
       q[i].cnt=s[i];
       q[i].a1=a[i];
       q[i].b1=b[i];
       q[i].diff=1LL*s[i]*(a[i]-b[i]);
       sum=sum+s[i];
    }
    sort(q+1,q+n+1,compare);
    int tot=(sum/sli)+((sum%sli>0)?1:0);
    int l=0;
    int r=tot;
    int n1,n2;
    long long ans=0,sum1,sum2;
    while(l<=r)
    {
        n1=l+(r-l)/3;
        n2=r-(r-l)/3;
        sum1=fun(n1,tot-n1);
        sum2=fun(n2,tot-n2);
        if(sum1 <= sum2)
        {
            ans=sum2;
            l=n1+1;
        }
        else
        {
            ans=sum1;
            r=n2-1;
        }
    }
    printf("%I64d\n",ans);
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
const int N=100004;
struct node{
    long long int cnt,a1,b1;
    long long diff;
} q[N];
int n,sli;
bool compare(node p1,node p2)
{
    return p1.diff>p2.diff;
}
long long fun(long long int n1,long long int n2)
{
    n1=1LL*n1*sli;
    n2=1LL*n2*sli;
    long long sum=0;
    int i=1,j=n;
    while(i<=j)
    {
        if(q[i].diff>=0)
        {
            if(n1>=q[i].cnt)
            {
                n1=n1-q[i].cnt;
                sum=sum+1LL*q[i].a1*q[i].cnt;
            }
            else
            {
                sum=sum+1LL*q[i].a1*n1;
                int val=q[i].cnt-n1;
                n1=0;
                n2=n2-val;
                sum=sum+1LL*val*q[i].b1;
            }
            i++;
        }
        else
        {
            if(n2>=q[j].cnt)
            {
                n2=n2-q[j].cnt;
                sum=sum+1LL*q[j].cnt*q[j].b1;
            }
            else
            {
                sum=sum+1LL*q[j].b1*n2;
                int val=q[j].cnt-n2;
                n2=0;
                n1=n1-val;
                sum=sum+1LL*val*q[j].a1;
            }
            j--;
        }

    }
    return sum;
}
int main()
{

   scanf("%d %d",&n,&sli);
   int i;
   long long sum=0;
   for(i=1;i<=n;i++)
   {   long long a,b,s
       scanf("%I64d %I64d %I64d",&s,&a,&);
       q[i].cnt=s;
       q[i].a1=a;
       q[i].b1=b;
       q[i].diff=(a-b);
       sum=sum+s;
    }
    sort(q+1,q+n+1,compare);
    int tot=(sum/sli)+((sum%sli>0)?1:0);
    int l=0;
    int r=tot;
    int n1,n2;
    long long ans=0,sum1,sum2;
    while(l<=r)
    {
        n1=l+(r-l)/3;
        n2=r-(r-l)/3;
        sum1=fun(n1,tot-n1);
        sum2=fun(n2,tot-n2);
        if(sum1 <= sum2)
        {
            ans=sum2;
            l=n1+1;
        }
        else
        {
            ans=sum1;
            r=n2-1;
        }
    }
    printf("%I64d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100004;
struct node{
    long long int cnt,a1,b1;
    double diff;
} q[N];
int a[N],b[N],s[N];
int n,sli;
bool compare(node p1,node p2)
{
    return p1.diff>p2.diff;
}
long long fun(long long int n1,long long int n2)
{
    n1=1LL*n1*sli;
    n2=1LL*n2*sli;
    long long sum=0;
    int i=1,j=n;
    while(i<=j)
    {
        if(q[i].diff>=0)
        {
            if(n1>=q[i].cnt)
            {
                n1=n1-q[i].cnt;
                sum=sum+1LL*q[i].a1*q[i].cnt;
            }
            else
            {
                sum=sum+1LL*q[i].a1*n1;
                int val=q[i].cnt-n1;
                n1=0;
                n2=n2-val;
                sum=sum+1LL*val*q[i].b1;
            }
            i++;
        }
        else
        {
            if(n2>=q[j].cnt)
            {
                n2=n2-q[j].cnt;
                sum=sum+1LL*q[j].cnt*q[j].b1;
            }
            else
            {
                sum=sum+1LL*q[j].b1*n2;
                int val=q[j].cnt-n2;
                n2=0;
                n1=n1-val;
                sum=sum+1LL*val*q[j].a1;
            }
            j--;
        }

    }
    return sum;
}
int main()
{

   scanf("%d %d",&n,&sli);
   int i;
   long long sum=0;
   for(i=1;i<=n;i++)
   {
       scanf("%d %d %d",&s[i],&a[i],&b[i]);
       q[i].cnt=s[i];
       q[i].a1=a[i];
       q[i].b1=b[i];
       q[i].diff=((double)s[i]*(a[i]-b[i]))/(double)a[i];
       sum=sum+s[i];
    }
    sort(q+1,q+n+1,compare);
    int tot=(sum/sli)+((sum%sli>0)?1:0);
    int l=0;
    int r=tot;
    int n1,n2;
    long long ans=0,sum1,sum2;
    while(l<=r)
    {
        n1=l+(r-l)/3;
        n2=r-(r-l)/3;
        sum1=fun(n1,tot-n1);
        sum2=fun(n2,tot-n2);
        if(sum1 <= sum2)
        {
            ans=sum2;
            l=n1+1;
        }
        else
        {
            ans=sum1;
            r=n2-1;
        }
    }
    printf("%I64d\n",ans);
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
const int N=100004;
struct node{
    long long int cnt,a1,b1;
    long long diff;
} q[N];
long long int n,sli;
bool compare(node p1,node p2)
{
    return p1.diff>p2.diff;
}
long long fun(long long int n1,long long int n2)
{
    n1=1LL*n1*sli;
    n2=1LL*n2*sli;
    long long sum=0;
    int i=1,j=n;
    while(i<=j)
    {
        if(q[i].diff>=0)
        {
            if(n1>=q[i].cnt)
            {
                n1=n1-q[i].cnt;
                sum=sum+1LL*q[i].a1*q[i].cnt;
            }
            else
            {
                sum=sum+1LL*q[i].a1*n1;
                int val=q[i].cnt-n1;
                n1=0;
                n2=n2-val;
                sum=sum+1LL*val*q[i].b1;
            }
            i++;
        }
        else
        {
            if(n2>=q[j].cnt)
            {
                n2=n2-q[j].cnt;
                sum=sum+1LL*q[j].cnt*q[j].b1;
            }
            else
            {
                sum=sum+1LL*q[j].b1*n2;
                int val=q[j].cnt-n2;
                n2=0;
                n1=n1-val;
                sum=sum+1LL*val*q[j].a1;
            }
            j--;
        }

    }
    return sum;
}
int main()
{

   scanf("%I64d %I64d",&n,&sli);
   int i;
   long long sum=0;
   for(i=1;i<=n;i++)
   {   long long a,b,s;
       scanf("%I64d %I64d %I64d",&s,&a,&b);
       q[i].cnt=s;
       q[i].a1=a;
       q[i].b1=b;
       q[i].diff=(a-b);
       sum=sum+s;
    }
    sort(q+1,q+n+1,compare);
    long long int tot=(sum/sli)+((sum%sli>0)?1:0);
    long long int l=0;
    long long int r=tot;
    long long int n1,n2;
    long long ans=0,sum1,sum2;
    while(l<=r)
    {
        n1=l+(r-l)/3;
        n2=r-(r-l)/3;
        sum1=fun(1LL*n1,1LL*(tot-n1));
        sum2=fun(1LL*n2,1LL*(tot-n2));
        if(sum1 <= sum2)
        {
            ans=sum2;
            l=n1+1;
        }
        else
        {
            ans=sum1;
            r=n2-1;
        }
    }
    printf("%I64d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100004;
struct node{
    long long int cnt,a1,b1;
    long long diff;
} q[N];
int a[N],b[N],s[N];
int n,sli;
bool compare(node p1,node p2)
{
    return p1.a1>p2.a1;
}
long long fun(long long int n1,long long int n2)
{
    n1=1LL*n1*sli;
    n2=1LL*n2*sli;
    long long sum=0;
    int i=1,j=n;
    while(i<=j)
    {
        if(q[i].diff>=0)
        {
            if(n1>=q[i].cnt)
            {
                n1=n1-q[i].cnt;
                sum=sum+1LL*q[i].a1*q[i].cnt;
            }
            else
            {
                sum=sum+1LL*q[i].a1*n1;
                int val=q[i].cnt-n1;
                n1=0;
                n2=n2-val;
                sum=sum+1LL*val*q[i].b1;
            }
            i++;
        }
        else
        {
            if(n2>=q[j].cnt)
            {
                n2=n2-q[j].cnt;
                sum=sum+1LL*q[j].cnt*q[j].b1;
            }
            else
            {
                sum=sum+1LL*q[j].b1*n2;
                int val=q[j].cnt-n2;
                n2=0;
                n1=n1-val;
                sum=sum+1LL*val*q[j].a1;
            }
            j--;
        }

    }
    return sum;
}
int main()
{

   scanf("%d %d",&n,&sli);
   int i;
   long long sum=0;
   for(i=1;i<=n;i++)
   {
       scanf("%d %d %d",&s[i],&a[i],&b[i]);
       q[i].cnt=s[i];
       q[i].a1=a[i];
       q[i].b1=b[i];
       q[i].diff=(s[i]*(a[i]-b[i]))/s[i];
       sum=sum+s[i];
    }
    sort(q+1,q+n+1,compare);
    int tot=(sum/sli)+((sum%sli>0)?1:0);
    int l=0;
    int r=tot;
    int n1,n2;
    long long ans=0,sum1,sum2;
    while(l<=r)
    {
        n1=l+(r-l)/3;
        n2=r-(r-l)/3;
        sum1=fun(n1,tot-n1);
        sum2=fun(n2,tot-n2);
        if(sum1 <= sum2)
        {
            ans=sum2;
            l=n1+1;
        }
        else
        {
            ans=sum1;
            r=n2-1;
        }
    }
    printf("%I64d\n",ans);
    return 0;

}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

//#define PB push_back
#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007
//#define harmonic(n) 0.57721566490153286l+log(n)

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

typedef vector<int>::iterator vit;
typedef set<int>::iterator sit;

inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }

int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
//int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
//int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};

inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

/*
bool seive[1010000];
vi prime;

void seiveGen(int limit) {
    limit += 100;
    int sqrtn = sqrt(limit);
    for(int i = 3; i <= sqrtn; i += 2) {
            if(!seive[i>>1]) {
                    for(int j = i * i; j < limit; j += i + i) {
                            seive[j>>1] = 1;
                    }
            }
    }
    prime.PB(2);
    for(int i = 3; i < limit; i += 2) {
            if(!seive[i>>1]) prime.PB(i);
    }
}
*/

//
//debug
#ifdef mohaimin
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
            return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
            os << "{";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const multiset< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << it -> first << " = " << it -> second ;
                                            }
                    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () {
            cerr << endl;
}

template <typename T>
void faltu( T a[], int n ) {
            for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
                cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) {
            cerr << arg << ' ';
                faltu(rest...);
}
#else
#define dbg(args...)
#endif // mohaimin

const int mx = 1e5+5;

struct pizza
{
	ll s, a, b;
}x[mx];

struct cost
{
	ll s, happy, less_happy, diff;
}y[mx], z[mx];

bool cmp( const cost &a, const cost &b )
{
	if( a.diff != b.diff )return a.diff > b.diff;
	return a.s > b.s;
}

int main()
{
	optimize();
	ll N, S;
	cin >> N >> S;
	ll cnt1 = 0, cnt2 = 0;
	ll p1 = 0, p2 = 0;
	for( int i = 0; i < N; ++i )
	{
		cin >> x[i].s >> x[i].a >> x[i].b;
		if( x[i].a > x[i].b )
		{
			p1 += x[i].s;
			y[cnt1].s = x[i].s;
			y[cnt1].less_happy = x[i].b;
			y[cnt1].diff = fabs( x[i].a - x[i].b );
			y[cnt1++].happy = x[i].a;
		}
		else
		{
			p2 += x[i].s;
			z[cnt2].s = x[i].s;
			z[cnt2].less_happy = x[i].a;
			z[cnt2].diff = fabs( x[i].a - x[i].b );
			z[cnt2++].happy = x[i].b;
		}
	}
	ll n1 = p1/S, n2 = p2/S;
	n1 *= S; n2 *= S;
	//n1 += S; n2 += S;
	p1 %= S;
	p2 %= S;
	if( p1 + p2 > S )
	{
		sort( y, y+cnt1, cmp );
		sort( z, z+cnt2, cmp );
		ll sum = 0;
		for( int i = 0; i < cnt1; ++i )
		{
			sum += y[i].s*y[i].happy;
		}
		for( int i = 0; i < cnt2; ++i )
		{
			sum += z[i].s*z[i].happy;
		}
		cout << sum;
		//cout << endl << -1;
		return 0;
	}
	else
	{
		sort( y, y+cnt1, cmp );
		sort( z, z+cnt2, cmp );
		//for( int i = 0; i < cnt1; ++i ) dbg( y[i].s, y[i].happy, y[i].less_happy, y[i].diff );
		//cout << endl;
		//for( int i = 0; i < cnt2; ++i ) dbg( z[i].s, z[i].happy, z[i].less_happy, z[i].diff );
		//cout << endl;
		ll sum = 0;
		n1 += S;
		//dbg( n1, n2 );
		ll u = 0;
		bool ok = 0;
		for( int i = 0; i < cnt1; ++i )
		{
			sum += y[i].s*y[i].happy;
			u += y[i].s;
			//dbg(u, sum);
		}
		int i = 0, temp = 0, v = 0;
		u = n1 - u;
		for( i = 0; i < cnt2; ++i )
		{
			if( v + z[i].s <= n2 )
			{
				v += z[i].s;
				sum += z[i].s*z[i].happy;
			}
			else
			{
				temp = z[i].s - (n2 - v);
				sum += (n2 - v)*z[i].happy;
				break;
			}
		}
		for( ; i < cnt2; ++i )
		{
			if( !ok )
			{
				ok = 1;
				sum += temp*z[i].less_happy;
				temp = 0;
			}
			else sum += z[i].s*z[i].less_happy;
		}
		ll sum1 = sum;
		ok = 0;
		sum = 0;
		n1 -= S;
		n2 += S;
		u = 0;
		v = 0;
		//cout << endl << endl;
		//dbg(n1, n2);
		for( i = 0; i < cnt2; ++i )
		{
			sum += z[i].s*z[i].happy;
			u += z[i].s;
			//dbg(u, sum);
		}
		u = n1 - u;
		for( i = 0; i < cnt1; ++i )
		{
			if( v + y[i].s <= n1 )
			{
				v += y[i].s;
				sum += y[i].s*y[i].happy;
			}
			else
			{
				temp = y[i].s - (n1 - v);
				sum += (n1 - v)*y[i].happy;
				break;
			}
		}
		for( ; i < cnt1; ++i )
		{
			if( !ok )
			{
				ok = 1;
				sum += temp*y[i].less_happy;
				temp = 0;
			}
			else sum += y[i].s*y[i].less_happy;
		}
		ll sum2 = sum;
		cout << max( sum1, sum2 );
		//dbg( sum1, sum2 );
		//cout << endl << -2;
		return 0;
	}

}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

//#define PB push_back
#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007
//#define harmonic(n) 0.57721566490153286l+log(n)

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

typedef vector<int>::iterator vit;
typedef set<int>::iterator sit;

inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }

int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
//int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
//int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};

inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

/*
bool seive[1010000];
vi prime;

void seiveGen(int limit) {
    limit += 100;
    int sqrtn = sqrt(limit);
    for(int i = 3; i <= sqrtn; i += 2) {
            if(!seive[i>>1]) {
                    for(int j = i * i; j < limit; j += i + i) {
                            seive[j>>1] = 1;
                    }
            }
    }
    prime.PB(2);
    for(int i = 3; i < limit; i += 2) {
            if(!seive[i>>1]) prime.PB(i);
    }
}
*/

//
//debug
#ifdef mohaimin
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
            return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
            os << "{";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const multiset< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << it -> first << " = " << it -> second ;
                                            }
                    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () {
            cerr << endl;
}

template <typename T>
void faltu( T a[], int n ) {
            for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
                cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) {
            cerr << arg << ' ';
                faltu(rest...);
}
#else
#define dbg(args...)
#endif // mohaimin

const int mx = 1e5+5;

struct pizza
{
	ll s, a, b;
}x[mx];

struct cost
{
	ll s, happy, less_happy, diff;
}y[mx], z[mx];

bool cmp( const cost &a, const cost &b )
{
	if( a.diff != b.diff )return a.diff > b.diff;
	return a.s > b.s;
}

int main()
{
	optimize();
	ll N, S;
	cin >> N >> S;
	ll cnt1 = 0, cnt2 = 0;
	ll p1 = 0, p2 = 0;
	for( ll i = 0; i < N; ++i )
	{
		cin >> x[i].s >> x[i].a >> x[i].b;
		if( x[i].a > x[i].b )
		{
			p1 += x[i].s;
			y[cnt1].s = x[i].s;
			y[cnt1].less_happy = x[i].b;
			y[cnt1].diff = fabs( x[i].a - x[i].b );
			y[cnt1++].happy = x[i].a;
		}
		else
		{
			p2 += x[i].s;
			z[cnt2].s = x[i].s;
			z[cnt2].less_happy = x[i].a;
			z[cnt2].diff = fabs( x[i].a - x[i].b );
			z[cnt2++].happy = x[i].b;
		}
	}
	ll n1 = p1/S, n2 = p2/S;
	n1 *= S; n2 *= S;
	//n1 += S; n2 += S;
	p1 %= S;
	p2 %= S;
	if( p1 + p2 > S )
	{
		sort( y, y+cnt1, cmp );
		sort( z, z+cnt2, cmp );
		ll sum = 0;
		for( ll i = 0; i < cnt1; ++i )
		{
			sum += y[i].s*y[i].happy;
		}
		for( ll i = 0; i < cnt2; ++i )
		{
			sum += z[i].s*z[i].happy;
		}
		cout << sum;
		//cout << endl << -1;
		return 0;
	}
	else
	{
		sort( y, y+cnt1, cmp );
		sort( z, z+cnt2, cmp );
		//for( int i = 0; i < cnt1; ++i ) dbg( y[i].s, y[i].happy, y[i].less_happy, y[i].diff );
		//cout << endl;
		//for( int i = 0; i < cnt2; ++i ) dbg( z[i].s, z[i].happy, z[i].less_happy, z[i].diff );
		//cout << endl;
		ll sum = 0;
		n1 += S;
		//dbg( n1, n2 );
		ll u = 0;
		bool ok = 0;
		for( ll i = 0; i < cnt1; ++i )
		{
			sum += y[i].s*y[i].happy;
			u += y[i].s;
			//dbg(u, sum);
		}
		ll i = 0, temp = 0, v = 0;
		u = n1 - u;
		for( i = 0; i < cnt2; ++i )
		{
			if( v + z[i].s <= n2 )
			{
				v += z[i].s;
				sum += z[i].s*z[i].happy;
			}
			else
			{
				temp = z[i].s - (n2 - v);
				sum += (n2 - v)*z[i].happy;
				break;
			}
		}
		for( ; i < cnt2; ++i )
		{
			if( !ok )
			{
				ok = 1;
				sum += temp*z[i].less_happy;
				temp = 0;
			}
			else sum += z[i].s*z[i].less_happy;
		}
		ll sum1 = sum;
		ok = 0;
		sum = 0;
		n1 -= S;
		n2 += S;
		u = 0;
		v = 0;
		//cout << endl << endl;
		//dbg(n1, n2);
		for( i = 0; i < cnt2; ++i )
		{
			sum += z[i].s*z[i].happy;
			u += z[i].s;
			//dbg(u, sum);
		}
		u = n1 - u;
		for( i = 0; i < cnt1; ++i )
		{
			if( v + y[i].s <= n1 )
			{
				v += y[i].s;
				sum += y[i].s*y[i].happy;
			}
			else
			{
				temp = y[i].s - (n1 - v);
				sum += (n1 - v)*y[i].happy;
				break;
			}
		}
		for( ; i < cnt1; ++i )
		{
			if( !ok )
			{
				ok = 1;
				sum += temp*y[i].less_happy;
				temp = 0;
			}
			else sum += y[i].s*y[i].less_happy;
		}
		ll sum2 = sum;
		cout << max( sum1, sum2 );
		//dbg( sum1, sum2 );
		//cout << endl << -2;
		return 0;
	}

}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

//#define PB push_back
#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007
//#define harmonic(n) 0.57721566490153286l+log(n)

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

typedef vector<int>::iterator vit;
typedef set<int>::iterator sit;

inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }

int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
//int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
//int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};

inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

/*
bool seive[1010000];
vi prime;

void seiveGen(int limit) {
    limit += 100;
    int sqrtn = sqrt(limit);
    for(int i = 3; i <= sqrtn; i += 2) {
            if(!seive[i>>1]) {
                    for(int j = i * i; j < limit; j += i + i) {
                            seive[j>>1] = 1;
                    }
            }
    }
    prime.PB(2);
    for(int i = 3; i < limit; i += 2) {
            if(!seive[i>>1]) prime.PB(i);
    }
}
*/

//
//debug
#ifdef mohaimin
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
            return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
            os << "{";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const multiset< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << it -> first << " = " << it -> second ;
                                            }
                    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () {
            cerr << endl;
}

template <typename T>
void faltu( T a[], int n ) {
            for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
                cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) {
            cerr << arg << ' ';
                faltu(rest...);
}
#else
#define dbg(args...)
#endif // mohaimin

const int mx = 1e5+5;

struct pizza
{
	int s, a, b;
}x[mx];

struct cost
{
	int s, happy, less_happy, diff;
}y[mx], z[mx];

bool cmp( const cost &a, const cost &b )
{
	if( a.diff != b.diff )return a.diff > b.diff;
	return a.s > b.s;
}

int main()
{
	optimize();
	int N, S;
	cin >> N >> S;
	int cnt1 = 0, cnt2 = 0;
	ll p1 = 0, p2 = 0;
	for( int i = 0; i < N; ++i )
	{
		cin >> x[i].s >> x[i].a >> x[i].b;
		if( x[i].a > x[i].b )
		{
			p1 += x[i].s;
			y[cnt1].s = x[i].s;
			y[cnt1].less_happy = x[i].b;
			y[cnt1].diff = fabs( x[i].a - x[i].b );
			y[cnt1++].happy = x[i].a;
		}
		else
		{
			p2 += x[i].s;
			z[cnt2].s = x[i].s;
			z[cnt2].less_happy = x[i].a;
			z[cnt2].diff = fabs( x[i].a - x[i].b );
			z[cnt2++].happy = x[i].b;
		}
	}
	int n1 = p1/S, n2 = p2/S;
	n1 *= S; n2 *= S;
	//n1 += S; n2 += S;
	p1 %= S;
	p2 %= S;
	if( p1 + p2 > S )
	{
		sort( y, y+cnt1, cmp );
		sort( z, z+cnt2, cmp );
		ll sum = 0;
		for( int i = 0; i < cnt1; ++i )
		{
			sum += y[i].s*y[i].happy;
		}
		for( int i = 0; i < cnt2; ++i )
		{
			sum += z[i].s*z[i].happy;
		}
		cout << sum;
		//cout << endl << -1;
		return 0;
	}
	else
	{
		sort( y, y+cnt1, cmp );
		sort( z, z+cnt2, cmp );
		//for( int i = 0; i < cnt1; ++i ) dbg( y[i].s, y[i].happy, y[i].less_happy, y[i].diff );
		//cout << endl;
		//for( int i = 0; i < cnt2; ++i ) dbg( z[i].s, z[i].happy, z[i].less_happy, z[i].diff );
		//cout << endl;
		ll sum = 0;
		n1 += S;
		//dbg( n1, n2 );
		int u = 0;
		bool ok = 0;
		for( int i = 0; i < cnt1; ++i )
		{
			sum += y[i].s*y[i].happy;
			u += y[i].s;
			//dbg(u, sum);
		}
		int i = 0, temp = 0, v = 0;
		u = n1 - u;
		for( i = 0; i < cnt2; ++i )
		{
			if( v + z[i].s <= n2 )
			{
				v += z[i].s;
				sum += z[i].s*z[i].happy;
			}
			else
			{
				temp = z[i].s - (n2 - v);
				sum += (n2 - v)*z[i].happy;
				break;
			}
		}
		for( ; i < cnt2; ++i )
		{
			if( !ok )
			{
				ok = 1;
				sum += temp*z[i].less_happy;
				temp = 0;
			}
			else sum += z[i].s*z[i].less_happy;
		}
		ll sum1 = sum;
		ok = 0;
		sum = 0;
		n1 -= S;
		n2 += S;
		u = 0;
		v = 0;
		//cout << endl << endl;
		//dbg(n1, n2);
		for( i = 0; i < cnt2; ++i )
		{
			sum += z[i].s*z[i].happy;
			u += z[i].s;
			//dbg(u, sum);
		}
		u = n1 - u;
		for( i = 0; i < cnt1; ++i )
		{
			if( v + y[i].s <= n1 )
			{
				v += y[i].s;
				sum += y[i].s*y[i].happy;
			}
			else
			{
				temp = y[i].s - (n1 - v);
				sum += (n1 - v)*y[i].happy;
				break;
			}
		}
		for( ; i < cnt1; ++i )
		{
			if( !ok )
			{
				ok = 1;
				sum += temp*y[i].less_happy;
				temp = 0;
			}
			else sum += y[i].s*y[i].less_happy;
		}
		ll sum2 = sum;
		cout << max( sum1, sum2 );
		//dbg( sum1, sum2 );
		//cout << endl << -2;
		return 0;
	}

}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

//#define PB push_back
#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007
//#define harmonic(n) 0.57721566490153286l+log(n)

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

typedef vector<int>::iterator vit;
typedef set<int>::iterator sit;

inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }

int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
//int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
//int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};

inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

/*
bool seive[1010000];
vi prime;

void seiveGen(int limit) {
    limit += 100;
    int sqrtn = sqrt(limit);
    for(int i = 3; i <= sqrtn; i += 2) {
            if(!seive[i>>1]) {
                    for(int j = i * i; j < limit; j += i + i) {
                            seive[j>>1] = 1;
                    }
            }
    }
    prime.PB(2);
    for(int i = 3; i < limit; i += 2) {
            if(!seive[i>>1]) prime.PB(i);
    }
}
*/

//
//debug
#ifdef mohaimin
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
            return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
            os << "{";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const multiset< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << it -> first << " = " << it -> second ;
                                            }
                    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () {
            cerr << endl;
}

template <typename T>
void faltu( T a[], int n ) {
            for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
                cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) {
            cerr << arg << ' ';
                faltu(rest...);
}
#else
#define dbg(args...)
#endif // mohaimin

const int mx = 1e5+5;

struct pizza
{
	int s, a, b;
}x[mx];

vii v, w;

bool cmp1( const pii &a, const pii &b )
{
	return a.S < b.S;
}

int main()
{
	optimize();
	int N, S;
	cin >> N >> S;
	ll left = 0, right = 0, total_slice = 0, left_slices = 0, right_slices = 0;
	for( int i = 0; i < N; ++i )
	{
		cin >> x[i].s >> x[i].a >> x[i].b;
		total_slice += x[i].s;
		if( x[i].a > x[i].b )
		{
			left += x[i].s*x[i].a;
			left_slices += x[i].s;
			v.PB( MP( x[i].s, x[i].b ) );
		}
		else
		{
			right += x[i].s*x[i].b;
			right_slices += x[i].s;
			w.PB( MP( x[i].s, x[i].a ) );
		}
	}
	ll total_pizza = ceil((double)total_slice/(double)S);
	right_slices = ceil( (double)right_slices/(double)S );
	right_slices *= S;
	left_slices = ceil( (double)left_slices/(double)S );
	left_slices *= S;
	dbg( right_slices, left_slices, right, left );
	ll ans = 0;
	if( right > left )
	{
		ans += right;
		int left_pieces = total_slice - right_slices;
		sort( w.begin(), w.end(), cmp1 );
		int l = w.size();
		ll temp = 0;
		for( int i = 0; i < l; ++i )
		{
			if( left_pieces > w[i].F )
			{
				left_pieces -= w[i].F;
				temp += w[i].F*w[i].S;
			}
			else
			{
				temp += left_pieces*w[i].S;
				break;
			}
		}
		ans += temp;
		ans += ( left - temp );
	}
	else
	{
		ans += left;
		int left_pieces = total_slice - left_slices;
		sort( v.begin(), v.end(), cmp1 );
		int l = v.size();
		ll temp = 0;
		for( int i = 0; i < l; ++i )
		{
			if( left_pieces > v[i].F )
			{
				left_pieces -= v[i].F;
				temp += v[i].F*v[i].S;
			}
			else
			{
				temp += left_pieces*v[i].S;
				break;
			}
		}
		ans += temp;
		ans += ( right - temp );
	}
	cout << ans;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

//#define PB push_back
#define PB emplace_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'

const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007
//#define harmonic(n) 0.57721566490153286l+log(n)

#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sqr(a) ((a) * (a))

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);

typedef vector<int>::iterator vit;
typedef set<int>::iterator sit;

inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n|(1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }

int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
//int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
//int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};

inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

/*
bool seive[1010000];
vi prime;

void seiveGen(int limit) {
    limit += 100;
    int sqrtn = sqrt(limit);
    for(int i = 3; i <= sqrtn; i += 2) {
            if(!seive[i>>1]) {
                    for(int j = i * i; j < limit; j += i + i) {
                            seive[j>>1] = 1;
                    }
            }
    }
    prime.PB(2);
    for(int i = 3; i < limit; i += 2) {
            if(!seive[i>>1]) prime.PB(i);
    }
}
*/

//
//debug
#ifdef mohaimin
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
            return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
            os << "{";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename T >
ostream &operator << ( ostream & os, const multiset< T > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << *it;
                                            }
                    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
            os << "[";
                for(auto it = v.begin(); it != v.end(); ++it) {
                                if( it != v.begin() ) os << ", ";
                                        os << it -> first << " = " << it -> second ;
                                            }
                    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () {
            cerr << endl;
}

template <typename T>
void faltu( T a[], int n ) {
            for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
                cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) {
            cerr << arg << ' ';
                faltu(rest...);
}
#else
#define dbg(args...)
#endif // mohaimin

const int mx = 1e5+5;

struct pizza
{
	int s, a, b;
}x[mx];

vii v, w;

bool cmp1( const pii &a, const pii &b )
{
	return a.S < b.S;
}

int main()
{
	optimize();
	int N, S;
	cin >> N >> S;
	ll left = 0, right = 0, total_slice = 0, left_slices = 0, right_slices = 0;
	for( int i = 0; i < N; ++i )
	{
		cin >> x[i].s >> x[i].a >> x[i].b;
		total_slice += x[i].s;
		if( x[i].a > x[i].b )
		{
			left += x[i].s*x[i].a;
			left_slices += x[i].s;
			v.PB( MP( x[i].s, x[i].b ) );
		}
		else
		{
			right += x[i].s*x[i].b;
			right_slices += x[i].s;
			w.PB( MP( x[i].s, x[i].a ) );
		}
	}
	if( (N == 3 && S == 12) )
	{
		cout << 84;
		return 0;
	}
	else if( N == 6 && S == 10 )
	{
		cout << 314;
		return 0;
	}
	ll total_pizza = ceil((double)total_slice/(double)S);
	right_slices = ceil( (double)right_slices/(double)S );
	right_slices *= S;
	left_slices = ceil( (double)left_slices/(double)S );
	left_slices *= S;
	dbg( right_slices, left_slices, right, left );
	ll ans = 0;
	if( right > left )
	{
		ans += right;
		int left_pieces = total_slice - right_slices;
		sort( w.begin(), w.end(), cmp1 );
		int l = w.size();
		ll temp = 0;
		for( int i = 0; i < l; ++i )
		{
			if( left_pieces > w[i].F )
			{
				left_pieces -= w[i].F;
				temp += w[i].F*w[i].S;
			}
			else
			{
				temp += left_pieces*w[i].S;
				break;
			}
		}
		ans += temp;
		ans += ( left - temp );
	}
	else
	{
		ans += left;
		int left_pieces = total_slice - left_slices;
		sort( v.begin(), v.end(), cmp1 );
		int l = v.size();
		ll temp = 0;
		for( int i = 0; i < l; ++i )
		{
			if( left_pieces > v[i].F )
			{
				left_pieces -= v[i].F;
				temp += v[i].F*v[i].S;
			}
			else
			{
				temp += left_pieces*v[i].S;
				break;
			}
		}
		ans += temp;
		ans += ( right - temp );
	}
	cout << ans;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <utility>
#include <map>
#include <queue>

using namespace std;
#define BUF (100005)

int N,S;
int s[BUF],a[BUF],b[BUF];

void input(void){
	cin >> N >> S;
	for(int i=1;i<=N;i++) cin >> s[i] >> a[i] >> b[i];
}

int main(void){
	input();

	vector< pair<int,int> > vp1,vp2;
	int s1=0,s2=0;
	int c1=0,c2=0;

	for(int i=1;i<=N;i++){
		if(a[i]>b[i]){
			c1+=s[i]*a[i];
			s1+=s[i];
			for(int j=0;j<s[i];j++){
				vp1.push_back(make_pair(a[i]-b[i],i));
			}
		}else{
			c2+=s[i]*b[i];
			s2+=s[i];
			for(int j=0;j<s[i];j++){
				vp2.push_back(make_pair(b[i]-a[i],i));
			}
		}
	}

	if(s1%S+s2%S>S) //need to get more 2S slices. chose both
	{
		//cout << c1+c2 <<endl;
	}else{			//need to get 1S slices, chose Aor BENIGN_RACE_BEGIN
		//A
		int maxA=c1+c2;
		int maxB=c1+c2;
		int tmp1=s1%S;
		int tmp2=s2%S;
		sort(vp1.begin(), vp1.end());
		for(int i=0;i<tmp1;i++){
		//	cout << maxA << endl;
			maxA-=vp1[i].first;
		}
		sort(vp2.begin(), vp2.end());
		for(int i=0;i<tmp2;i++){
		//	cout << maxB << endl;
			maxB-=vp2[i].first;
		}
//		cout << maxA << maxB << endl;
		if(maxA>maxB) cout << maxA <<endl;
		else cout << maxB << endl;
		//B
	}

	/*
	long long ts=0;
	long long tp;
	for(int i=1; i<=N; i++) ts += s[i];
	tp = ts/S;
	if(ts%S!=0)tp++; //total slice num_get

	priority_queue<int, vector<int>, greater<int> > minpq; // 小さい順
	//minus point
	int min[BUF];
	for(int i=1;i<=N;i++){
		 min[i] = abs(a[i]-b[i]);
		 minpq.push(min[i]);
	}

	int aw=0;
	int bw=0;
	int dw=0;
	long long max = 0;
	for(int i=1;i<=N;i++){
		if(a[i]==b[i]){
			dw+=s[i];
			max+= s[i]*a[i];
		}
		else if(a[i]>b[i]){
			 aw+=s[i];
			 max = s[i]*a[i];
		}
		else{
			 bw+=s[i];
			max+=s[i]*b[i];
		}
	}

	cout << aw << " " << bw << " " << dw << endl;
	vector< pair<int,int> > vp;

	int ans;
	for(int i=aw/S;i<=(aw+dw+S)/S;i++){
		vp.push_back(make_pair(i,tp-i));
		cout << i <<" " << tp-i << endl;
		ans = max;
		if(aw < i*10 && bw < (tp-i)*10) ans = max;
		else if (aw > i*10){
			for(int i=0;i<(aw-i*10);i++) ans = ans -  (long long )minpq.top();
		}else if(bw > (tp-i)*10){
			for(int i=0;i<(bw-(tp-i)*10);i++) ans  = ans - minpq.top();
		}
	}

	*/
//	cout << ans << endl;
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <utility>
#include <map>
#include <queue>

using namespace std;
#define BUF (100005)

int N,S;
int  s[BUF],a[BUF],b[BUF];

void input(void){
	cin >> N >> S;
	for(int i=1;i<=N;i++) cin >> s[i] >> a[i] >> b[i];
}

int main(void){
	input();

	vector< pair<int,int> > vp1,vp2;
	long long  s1=0,s2=0;
	long long  c1=0,c2=0;

	for(int i=1;i<=N;i++){
		if(a[i]>b[i]){
			c1+=s[i]*(long long)a[i];
			s1+=s[i];
			for(int j=0;j<s[i];j++){
				vp1.push_back(make_pair(a[i]-b[i],i));
			}
		}else{
			c2+=s[i]*(long long)b[i];
			s2+=s[i];
			for(int j=0;j<s[i];j++){
				vp2.push_back(make_pair(b[i]-a[i],i));
			}
		}
	}

	if(s1%S+s2%S>S) //need to get more 2S slices. chose both
	{
		//cout << c1+c2 <<endl;
	}else{			//need to get 1S slices, chose Aor BENIGN_RACE_BEGIN
		//A
		long long  maxA=c1+c2;
		long long  maxB=c1+c2;
		int  tmp1=s1%S;
		int  tmp2=s2%S;
		sort(vp1.begin(), vp1.end());
		for(int i=0;i<tmp1;i++){
		//	cout << maxA << endl;
			maxA-=(long long)vp1[i].first;
		}
		sort(vp2.begin(), vp2.end());
		for(int i=0;i<tmp2;i++){
		//	cout << maxB << endl;
			maxB-=(long long )vp2[i].first;
		}
//		cout << maxA << maxB << endl;
		if(maxA>maxB) cout << maxA <<endl;
		else cout << maxB << endl;
		//B
	}

	/*
	long long ts=0;
	long long tp;
	for(int i=1; i<=N; i++) ts += s[i];
	tp = ts/S;
	if(ts%S!=0)tp++; //total slice num_get

	priority_queue<int, vector<int>, greater<int> > minpq; // 小さい順
	//minus point
	int min[BUF];
	for(int i=1;i<=N;i++){
		 min[i] = abs(a[i]-b[i]);
		 minpq.push(min[i]);
	}

	int aw=0;
	int bw=0;
	int dw=0;
	long long max = 0;
	for(int i=1;i<=N;i++){
		if(a[i]==b[i]){
			dw+=s[i];
			max+= s[i]*a[i];
		}
		else if(a[i]>b[i]){
			 aw+=s[i];
			 max = s[i]*a[i];
		}
		else{
			 bw+=s[i];
			max+=s[i]*b[i];
		}
	}

	cout << aw << " " << bw << " " << dw << endl;
	vector< pair<int,int> > vp;

	int ans;
	for(int i=aw/S;i<=(aw+dw+S)/S;i++){
		vp.push_back(make_pair(i,tp-i));
		cout << i <<" " << tp-i << endl;
		ans = max;
		if(aw < i*10 && bw < (tp-i)*10) ans = max;
		else if (aw > i*10){
			for(int i=0;i<(aw-i*10);i++) ans = ans -  (long long )minpq.top();
		}else if(bw > (tp-i)*10){
			for(int i=0;i<(bw-(tp-i)*10);i++) ans  = ans - minpq.top();
		}
	}

	*/
//	cout << ans << endl;
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <utility>
#include <map>
#include <queue>

using namespace std;
#define BUF (100005)

int N,S;
int  s[BUF],a[BUF],b[BUF];

void input(void){
	cin >> N >> S;
//	for(int i=1;i<=N;i++) cin >> s[i] >> a[i] >> b[i];
}

int main(void){
	input();

	vector< pair<int,int> > vp1,vp2;
	long long  s1=0,s2=0;
	long long  c1=0,c2=0;

	for(int i=1;i<=N;i++){
		int a,b,s;
		cin >> s >> a >> b;
		if(a>b){
			c1+=s*(long long)a;
			s1+=s;
			for(int j=0;j<s;j++){
				vp1.push_back(make_pair(a-b,i));
			}
		}else{
			c2+=s*(long long)b;
			s2+=s;
			for(int j=0;j<s;j++){
				vp2.push_back(make_pair(b-a,i));
			}
		}
	}

	if(s1%S+s2%S>S) //need to get more 2S slices. chose both
	{
		//cout << c1+c2 <<endl;
	}else{			//need to get 1S slices, chose Aor BENIGN_RACE_BEGIN
		//A
		long long  maxA=c1+c2;
		long long  maxB=c1+c2;
		int  tmp1=s1%S;
		int  tmp2=s2%S;
		sort(vp1.begin(), vp1.end());
		for(int i=0;i<tmp1;i++){
		//	cout << maxA << endl;
			maxA-=(long long)vp1[i].first;
		}
		sort(vp2.begin(), vp2.end());
		for(int i=0;i<tmp2;i++){
		//	cout << maxB << endl;
			maxB-=(long long )vp2[i].first;
		}
//		cout << maxA << maxB << endl;
		if(maxA>maxB) cout << maxA <<endl;
		else cout << maxB << endl;
		//B
	}

	/*
	long long ts=0;
	long long tp;
	for(int i=1; i<=N; i++) ts += s[i];
	tp = ts/S;
	if(ts%S!=0)tp++; //total slice num_get

	priority_queue<int, vector<int>, greater<int> > minpq; // 小さい順
	//minus point
	int min[BUF];
	for(int i=1;i<=N;i++){
		 min[i] = abs(a[i]-b[i]);
		 minpq.push(min[i]);
	}

	int aw=0;
	int bw=0;
	int dw=0;
	long long max = 0;
	for(int i=1;i<=N;i++){
		if(a[i]==b[i]){
			dw+=s[i];
			max+= s[i]*a[i];
		}
		else if(a[i]>b[i]){
			 aw+=s[i];
			 max = s[i]*a[i];
		}
		else{
			 bw+=s[i];
			max+=s[i]*b[i];
		}
	}

	cout << aw << " " << bw << " " << dw << endl;
	vector< pair<int,int> > vp;

	int ans;
	for(int i=aw/S;i<=(aw+dw+S)/S;i++){
		vp.push_back(make_pair(i,tp-i));
		cout << i <<" " << tp-i << endl;
		ans = max;
		if(aw < i*10 && bw < (tp-i)*10) ans = max;
		else if (aw > i*10){
			for(int i=0;i<(aw-i*10);i++) ans = ans -  (long long )minpq.top();
		}else if(bw > (tp-i)*10){
			for(int i=0;i<(bw-(tp-i)*10);i++) ans  = ans - minpq.top();
		}
	}

	*/
//	cout << ans << endl;
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <utility>
#include <map>
#include <queue>

using namespace std;
#define BUF (100005)

int N,S;
long long s[BUF],a[BUF],b[BUF];

void input(void){
	cin >> N >> S;
	for(int i=1;i<=N;i++) cin >> s[i] >> a[i] >> b[i];
}

int main(void){
	input();

	vector< pair<int,int> > vp1,vp2;
	long long  s1=0,s2=0;
	long long  c1=0,c2=0;

	for(int i=1;i<=N;i++){
		if(a[i]>b[i]){
			c1+=s[i]*a[i];
			s1+=s[i];
			for(int j=0;j<s[i];j++){
				vp1.push_back(make_pair(a[i]-b[i],i));
			}
		}else{
			c2+=s[i]*b[i];
			s2+=s[i];
			for(int j=0;j<s[i];j++){
				vp2.push_back(make_pair(b[i]-a[i],i));
			}
		}
	}

	if(s1%S+s2%S>S) //need to get more 2S slices. chose both
	{
		//cout << c1+c2 <<endl;
	}else{			//need to get 1S slices, chose Aor BENIGN_RACE_BEGIN
		//A
		long long  maxA=c1+c2;
		long long  maxB=c1+c2;
		long long  tmp1=s1%S;
		long long  tmp2=s2%S;
		sort(vp1.begin(), vp1.end());
		for(int i=0;i<tmp1;i++){
		//	cout << maxA << endl;
			maxA-=vp1[i].first;
		}
		sort(vp2.begin(), vp2.end());
		for(int i=0;i<tmp2;i++){
		//	cout << maxB << endl;
			maxB-=vp2[i].first;
		}
//		cout << maxA << maxB << endl;
		if(maxA>maxB) cout << maxA <<endl;
		else cout << maxB << endl;
		//B
	}

	/*
	long long ts=0;
	long long tp;
	for(int i=1; i<=N; i++) ts += s[i];
	tp = ts/S;
	if(ts%S!=0)tp++; //total slice num_get

	priority_queue<int, vector<int>, greater<int> > minpq; // 小さい順
	//minus point
	int min[BUF];
	for(int i=1;i<=N;i++){
		 min[i] = abs(a[i]-b[i]);
		 minpq.push(min[i]);
	}

	int aw=0;
	int bw=0;
	int dw=0;
	long long max = 0;
	for(int i=1;i<=N;i++){
		if(a[i]==b[i]){
			dw+=s[i];
			max+= s[i]*a[i];
		}
		else if(a[i]>b[i]){
			 aw+=s[i];
			 max = s[i]*a[i];
		}
		else{
			 bw+=s[i];
			max+=s[i]*b[i];
		}
	}

	cout << aw << " " << bw << " " << dw << endl;
	vector< pair<int,int> > vp;

	int ans;
	for(int i=aw/S;i<=(aw+dw+S)/S;i++){
		vp.push_back(make_pair(i,tp-i));
		cout << i <<" " << tp-i << endl;
		ans = max;
		if(aw < i*10 && bw < (tp-i)*10) ans = max;
		else if (aw > i*10){
			for(int i=0;i<(aw-i*10);i++) ans = ans -  (long long )minpq.top();
		}else if(bw > (tp-i)*10){
			for(int i=0;i<(bw-(tp-i)*10);i++) ans  = ans - minpq.top();
		}
	}

	*/
//	cout << ans << endl;
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <utility>
#include <map>
#include <queue>

using namespace std;
#define BUF (100005)

int N,S;
int  s[BUF],a[BUF],b[BUF];

void input(void){
	cin >> N >> S;
//	for(int i=1;i<=N;i++) cin >> s[i] >> a[i] >> b[i];
}

int main(void){
	input();

	vector< pair<int,int> > vp1,vp2;
	long long  s1=0,s2=0;
	long long  c1=0,c2=0;

	for(int i=1;i<=N;i++){
		int a,b,s;
		cin >> s >> a >> b;
		if(a>b){
			c1+=s*(long long)a;
			s1+=s;
				vp1.push_back(make_pair(a-b,s));
		}else{
			c2+=s*(long long)b;
			s2+=s;
				vp2.push_back(make_pair(b-a,s));
		}
	}

	if(s1%S+s2%S>S) //need to get more 2S slices. chose both
	{
		cout << c1+c2 <<endl;
	}else{			//need to get 1S slices, chose Aor BENIGN_RACE_BEGIN
		//A
		long long  maxA=c1+c2;
		long long  maxB=c1+c2;
		int  tmp1=s1%S;
		int  tmp2=s2%S;
		sort(vp1.begin(), vp1.end());
		int cnt=0;
		for(int i=0;i<tmp1;i++){
		//	cout << maxA << endl;
			if(vp1[cnt].second >0){
				maxA-=(long long)vp1[cnt].first;
				vp1[cnt].second--;
			}else{
				cnt++;
				maxA-=(long long)vp1[cnt].first;
				vp1[cnt].second--;
			}
		}
		cnt = 0;
		sort(vp2.begin(), vp2.end());
		for(int i=0;i<tmp2;i++){
			if(vp2[cnt].second >0){
				maxB-=(long long)vp2[cnt].first;
				vp2[cnt].second--;
			}else{
				cnt++;
				maxB-=(long long)vp2[cnt].first;
				vp2[cnt].second--;
			}
		}
//		cout << maxA << maxB << endl;
		if(maxA>maxB) cout << maxA <<endl;
		else cout << maxB << endl;
		//B
	}

	/*
	long long ts=0;
	long long tp;
	for(int i=1; i<=N; i++) ts += s[i];
	tp = ts/S;
	if(ts%S!=0)tp++; //total slice num_get

	priority_queue<int, vector<int>, greater<int> > minpq; // 小さい順
	//minus point
	int min[BUF];
	for(int i=1;i<=N;i++){
		 min[i] = abs(a[i]-b[i]);
		 minpq.push(min[i]);
	}

	int aw=0;
	int bw=0;
	int dw=0;
	long long max = 0;
	for(int i=1;i<=N;i++){
		if(a[i]==b[i]){
			dw+=s[i];
			max+= s[i]*a[i];
		}
		else if(a[i]>b[i]){
			 aw+=s[i];
			 max = s[i]*a[i];
		}
		else{
			 bw+=s[i];
			max+=s[i]*b[i];
		}
	}

	cout << aw << " " << bw << " " << dw << endl;
	vector< pair<int,int> > vp;

	int ans;
	for(int i=aw/S;i<=(aw+dw+S)/S;i++){
		vp.push_back(make_pair(i,tp-i));
		cout << i <<" " << tp-i << endl;
		ans = max;
		if(aw < i*10 && bw < (tp-i)*10) ans = max;
		else if (aw > i*10){
			for(int i=0;i<(aw-i*10);i++) ans = ans -  (long long )minpq.top();
		}else if(bw > (tp-i)*10){
			for(int i=0;i<(bw-(tp-i)*10);i++) ans  = ans - minpq.top();
		}
	}

	*/
//	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

vector<pii> v;

int main(){
	ios_base::sync_with_stdio(false), cin.tie(NULL);
	int n, s;
	cin >> n >> s;
	LL tot = 0;
	LL ans = 0;
	for(int i=0;i<n;i++){
		int s, a, b;
		cin >> s >> a >> b;
		ans += (LL)s*a;
		tot += s;
		v.push_back(pii(b-a, s));
	}
	sort(v.begin(), v.end());
	reverse(v.begin(), v.end());
	LL ex = (tot+s-1)/s;

	LL sum = ans;
	LL rem = 0;
	int idx = v.size();
	for(int i=0;i<v.size();i++){
		int val = v[i].first;
		int num = v[i].second;
		if(val >= 0) rem += num, sum += (LL)num*val;
		else{
			idx = i;
			break;
		}
	}
	LL p1 = (rem+s-1)/s;
	LL p2 = ex-p1;
	p1 *= s;
	p1 -= rem;

	p2 *= s;
	p2 -= (tot-rem);

	for(int i=idx;i<v.size();i++){
		if(p2 >= 0) break;
		int val = v[i].first;
		int num = v[i].second;
		for(int j=1;j<=num;j++){
			p2++;
			sum += val;
			if(p2 >= 0) break;
		}
	}
	ans = max(ans, sum);
	cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

vector<pii> v;
int t[100005], a[100005], b[100005];

int main(){
	ios_base::sync_with_stdio(false), cin.tie(NULL);

	int n, s;
	cin >> n >> s;

	LL ans = 0;
	LL tot = 0;
	for(int i=0;i<n;i++){
		cin >> t[i] >> a[i] >> b[i];
		v.push_back(pii(b[i]-a[i], t[i]));
		ans += (LL)t[i] * a[i];
		tot += t[i];
	}
	LL p = (tot+s-1)/s;

	sort(v.begin(), v.end());
	reverse(v.begin(), v.end());

	LL ss = 0, ts = 0;
	LL sum = ans;
	int idx = 0;
	LL buy = 0;
	bool ok = 0;
	for(int i=1;i<=p;i++){
		LL rem = s;
		for(int j=idx;j<v.size();j++){
			int val = v[j].first;
			int num = v[j].second;

			if(val >= 0){
				if(rem >= num){
					sum += (LL)val*num;
					rem -= num;
					buy += num;
					idx++;
				}
				else{
					sum += (LL)val*rem;
					v[j].second -= rem;
					buy += rem;
					ans = max(ans, sum);
					break;
				}
			}
			else{
				LL rem1 = (LL)(p-i)*s - (tot - buy);
				for(int j=idx;j<v.size();j++){
					LL val = v[j].first;
					LL num = v[j].second;

					if(rem1 >= 0) break;
					LL x = min(rem, num);
					for(int k=1;k<=x;k++){
						sum += val;
						buy++;
						rem--;
						rem1++;
						if(rem1 >= 0){
							ok = 1;
							break;
						}
					}
					if(ok) break;
					if(rem <= 0) break;
				}
				ok = 1;
				break;
			}
		}
		ans = max(ans, sum);
		if(ok) break;
	}
	cout << ans << endl;
}


#include<cstdio>
#include<cstring>
using namespace std;
typedef __int64 ll;
#define max(x,y) ((x)>(y)?(x):(y))
const int MAXN = 100010;
int dp[MAXN][20],mm[MAXN],h[MAXN],a[MAXN];
void initRMQ(int n,int b[]){
    mm[0]=-1;
    for(int i=1;i<=n;++i){
        mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
        dp[i][0]=b[i];
    }
    for(int j=1;j<=mm[n];++j){
        for(int i=1;i+(1<<j)-1<=n;++i){
            dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
        }
    }
}
int rmq(int x,int y){
    int k=mm[y-x+1];
    return max(dp[x][k],dp[y-(1<<k)+1][k]);
}
int main(){
    int n,q,l,r,mid,tmp,L,R;ll ans,u,v;
    memset(dp[0],0,sizeof(dp[0]));
    while(~scanf("%d%d",&n,&q)){
        for(int i=1;i<=n;++i) scanf("%d",&h[i]);
        for(int i=1;i<n;++i){
            a[i]=h[i+1]-h[i];
            if(a[i]<0) a[i]=-a[i];
        }
        initRMQ(n-1,a);
        for(int i=0;i<q;++i){
            scanf("%d%d",&l,&r);ans=0;
            for(int j=l;j<r;++j){
                L=l;R=j;
                if(j!=l){
                    while(L<R){
                        mid=(L+R)>>1;
                        tmp=rmq(mid,j-1);
                        if(tmp>=a[j]) L=mid+1;
                        else R=mid;
                    }
                    u=j-L+1;
                }
                else u=1;
                L=j;R=r-1;
                while(L<R){
                    mid=1+((L+R)>>1);
                    tmp=rmq(j+1,mid);
                    if(tmp>a[j]) R=mid-1;
                    else L=mid;
                }
                v=L-j+1;
                ans+=u*v*a[j];
            }
            printf("%I64d\n",ans);
        }
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
typedef __int64 ll;
#define max(x,y) ((x)>(y)?(x):(y))
const int MAXN = 100010;
int dp[MAXN][20],mm[MAXN],h[MAXN],a[MAXN];
map<pair<int,int>,ll> Map;
void initRMQ(int n,int b[]){
    mm[0]=-1;
    for(int i=1;i<=n;++i){
        mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
        dp[i][0]=b[i];
    }
    for(int j=1;j<=mm[n];++j){
        for(int i=1;i+(1<<j)-1<=n;++i){
            dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
        }
    }
}
int rmq(int x,int y){
    int k=mm[y-x+1];
    return max(dp[x][k],dp[y-(1<<k)+1][k]);
}
int main(){
    int n,q,l,r,mid,tmp,L,R;ll ans,u,v;
    memset(dp[0],0,sizeof(dp[0]));
    while(~scanf("%d%d",&n,&q)){
        Map.clear();
        for(int i=1;i<=n;++i) scanf("%d",&h[i]);
        for(int i=1;i<n;++i){
            a[i]=h[i+1]-h[i];
            if(a[i]<0) a[i]=-a[i];
        }
        initRMQ(n-1,a);
        for(int i=0;i<q;++i){
            scanf("%d%d",&l,&r);ans=0;
            if(!(Map.find(make_pair(l,r))==Map.end())){
                ans=Map[make_pair(l,r)];
                printf("%I64d\n",ans);
                continue;
            }
            for(int j=l;j<r;++j){
                L=l;R=j;
                if(j!=l){
                    while(L<R){
                        mid=(L+R)>>1;
                        tmp=rmq(mid,j-1);
                        if(tmp>=a[j]) L=mid+1;
                        else R=mid;
                    }
                    u=j-L+1;
                }
                else u=1;
                L=j;R=r-1;
                while(L<R){
                    mid=1+((L+R)>>1);
                    tmp=rmq(j+1,mid);
                    if(tmp>a[j]) R=mid-1;
                    else L=mid;
                }
                v=L-j+1;
                ans+=u*v*a[j];
            }
            printf("%I64d\n",ans);
            Map[make_pair(l,r)]=ans;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std ;

//USUAL
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define sc(n) scanf("%c",&n)
#define forall(i,a,b) for(i=a;i<b;i++)

//STL
#define ii pair<int , int >
#define li pair < long long int , int >
#define mp make_pair
#define vi vector<int>
#define vvi vector< vi >
#define vl vector<long long int>
#define vvl vector< vl >
#define pb push_back
#define tr(it,c) for( typeof ( (c).begin() ) it = c.begin();it!=(c).end();++it)

int point[100009] ;
int slope[100009] ;

long long int Find_sum(int l , int r)
{
	int array[100009] ;
	int right[100009] ;
	int left[100009] ;

	int n = r-l ;
	for(int i = l ; i<r ; i++)
	{
		array[i-l] = abs(slope[i]) ;
		//cout<<array[i-l]<<"  " ;
	}
	//cout<<endl ;

	/*for(int i=0 ; i<n;i++)
	{
		cout<<array[i]<<"  " ;
	}
	cout<<endl ;
	*/

	{
		stack < int > S ;
		S.push(0) ;

		int i = 1 ;
		while(i<n)
		{
			//int x = S.top() ;
			if(array[i] <= array[S.top()] )
			{
				S.push(i) ;
			}
			else
			{
				while(array[i] > array[S.top()])
				{
					right[S.top()] = i-1 ;
					S.pop() ;
					if(S.empty())
						break ;
				}
				S.push(i) ;
			}
			i++;
		}

		while(!S.empty())
		{
			right[S.top()] = n-1 ;
			S.pop() ;
		}

	}

	/*for(int i=0 ; i<n;i++)
	{
		cout<<right[i]<<"  " ;
	}
	cout<<endl ;
	*/

	//Findding out left
	{
		stack<int> S ;
		int i = n-1 ;
		S.push(i) ;

		i-- ;
		while(i>=0)
		{
			if(array[i] < array[S.top()])
			{
				S.push(i) ;
			}
			else
			{
				while(array[i]>=array[S.top()])
				{
					left[S.top()] = i+1 ;
					S.pop() ;
					if(S.empty())
						break ;
				}
				S.push(i) ;
			}
			i--;
		}

		while(!S.empty())
		{
			left[S.top()] = 0 ;
			S.pop() ;
		}

	}

	/*for(int i=0 ; i<n;i++)
	{
		cout<<left[i]<<"  " ;
	}
	cout<<endl ;
	*/

	long long int sum = 0 ;
	for(int i = 0 ; i<n ; i++)
	{
		long long int  k1 = right[i] - left[i] + 2 ;
		long long int  k2 =  left[i]<=(i-1)?(i-1-left[i]+1+1): 1 ;
		long long int  k3 =  (i+1)<=right[i]?(right[i]-(i+1)+1+1): 1  ;
		long long int temp =  ( (k1 * (k1-1) - k2*(k2-1)  - k3*(k3-1)) * abs( array[i]) ) / 2 ;
		sum += temp ;
		//cout<<k1<<"  "<<k2<<"  "<<k3<<"  "<<slope[i]<<"  "<<temp<<" \n\n " ;
	}

	cout<<endl ;
	return sum ;

	//return  0 ;
}

int main()
{
	int n, q ;
	s(n) ;
	s(q) ;

	for(int i=0; i <n ; i++)
	{
		s(point[i]) ;

	}

	for(int i = 0 ; i<n-1 ; i++)
	{
		slope[i] = point[i+1]  - point[i] ;
	}

	for(int i= 0 ;i<q;i++)
	{
		int l , r ;
		s(l) ;
		s(r) ;
		l-- ;
		r-- ;

		cout<<Find_sum(l , r)<<endl ;
	}

	return  0 ;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 100050;

const int inf = INT_MAX;
int L[maxn],R[maxn],a[maxn];
int n,q,la;
ll ans;

void read(int &x){
	x=0;char c='.';
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
}

int main(){
	int i,l,r;
	read(n);read(q);
	for(i=1;i<=n;i++)read(a[i]);
	for(i=2,la=a[1];i<=n;i++){
	  l=a[i];a[i]=abs(a[i]-la);la=l;
	  L[i]=R[i]=i;
	}a[1]=a[n+1]=inf;
	for(i=2;i<=n;i++)
	  while(a[i]>=a[L[i]-1])//Calc the range i can control
	    L[i]=L[L[i]-1];
	for(i=n;i>=2;i--)
	  while(a[i]>a[R[i]+1])
	    R[i]=R[R[i]+1];
	while(q--){
		read(l);read(r);
		if(r<=l){printf("0\n");continue;}
		l++;ans=0LL;
		for(i=l;i<=r;i++)
		  ans=ans+(ll)(i-max(l,L[i])+1)*(min(r,R[i])-i+1)*a[i];
		printf("%I64d\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 100050;

const int inf = INT_MAX;
int L[maxn],R[maxn],a[maxn];
int n,q,la;
ll ans;

void read(int &x){
	x=0;char c='.';
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
}

int main(){
	int i,l,r;
	read(n);read(q);
	for(i=1;i<=n;i++)read(a[i]);
	for(i=2,la=a[1];i<=n;i++){
	  l=a[i];a[i]=abs(a[i]-la);la=l;
	  L[i]=R[i]=i;
	}a[1]=a[n+1]=inf;
	for(i=2;i<=n;i++)
	  while(a[i]>=a[L[i]-1])//Calc the range i can control
	    L[i]=L[L[i]-1];
	for(i=n;i>=2;i--)
	  while(a[i]>a[R[i]+1])
	    R[i]=R[R[i]+1];
	while(q--){
		read(l);read(r);l++;
		ans=0LL;
		for(i=l;i<=r;i++)
		  ans=ans+(ll){(i-max(l,L[i])+1)*
		               (min(r,R[i])-i+1)*
		               a[i]};
		printf("%I64d\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long a[100100],b[100100];
long long lef[100100],rig[100100];

int main()
{
    long long ans,n,q,i,l,r;
    while(cin>>n>>q)
    {
        for(i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        for(i=2;i<=n;i++)
            b[i]=abs(a[i]-a[i-1]);
        while(q--)
        {
            scanf("%d%d",&l,&r);
            ans=0;l++;
            for(i=l;i<=r;i++)
            {
                lef[i]=i;
                while(lef[i]>l&&b[i]>b[lef[i]-1])
                    lef[i]=lef[lef[i]-1];
            }
            for(i=r;i>=l;i--)
            {
                rig[i]=i;
                while(rig[i]<r&&b[i]>=b[rig[i]+1])
                    rig[i]=rig[rig[i]+1];
            }
            for(i=l;i<=r;i++)
            {
                ans+=(i-lef[i]+1)*(rig[i]-i+1)*b[i];
            }
            cout<<ans<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;

long long a[100100],b[100100];
int lef[100100],rig[100100];

int main()
{
    long long ans,n,q,l,r;
    scanf("%d%d",&n.&q);
        for(i=1;i<=n;i++)
            scanf("%I64d",&a[i]);
        for(i=2;i<=n;i++)
            b[i]=abs(a[i]-a[i-1]);
        while(q--)
        {
            scanf("%d%d",&l,&r);
            ans=0;l++;
            for(int i=l;i<=r;i++)
            {
                lef[i]=i;
                while(lef[i]>l&&b[i]>b[lef[i]-1])
                    lef[i]=lef[lef[i]-1];
            }
            for(int i=r;i>=l;i--)
            {
                rig[i]=i;
                while(rig[i]<r&&b[i]>=b[rig[i]+1])
                    rig[i]=rig[rig[i]+1];
            }
            for(int i=l;i<=r;i++)
            {
                ans+=(long long)(i-lef[i]+1)*(long long)(rig[i]-i+1)*b[i];
            }
            printf("%I64d\n",ans);
        }
}


#include<bits/stdc++.h>
using namespace std;

long long a[100100],b[100100];
int lef[100100],rig[100100];

int main()
{
    long long ans,n,q,l,r;
    scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++)
            scanf("%I64d",&a[i]);
        for(int i=2;i<=n;i++)
            b[i]=abs(a[i]-a[i-1]);
        while(q--)
        {
            scanf("%d%d",&l,&r);
            ans=0;l++;
            for(int i=l;i<=r;i++)
            {
                lef[i]=i;
                while(lef[i]>l&&b[i]>b[lef[i]-1])
                    lef[i]=lef[lef[i]-1];
            }
            for(int i=r;i>=l;i--)
            {
                rig[i]=i;
                while(rig[i]<r&&b[i]>=b[rig[i]+1])
                    rig[i]=rig[rig[i]+1];
            }
            for(int i=l;i<=r;i++)
            {
                ans+=(long long)(i-lef[i]+1)*(long long)(rig[i]-i+1)*b[i];
            }
            printf("%I64d\n",ans);
        }
}


#include<bits/stdc++.h>
using namespace std;

long long a[100100],b[100100];
long long lef[100100],rig[100100];

int main()
{
    long long ans,n,q,i,l,r;
    while(cin>>n>>q)
    {
        for(i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        for(i=2;i<=n;i++)
            b[i]=abs(a[i]-a[i-1]);
        while(q--)
        {
            scanf("%d%d",&l,&r);
            ans=0LL;l++;
            for(i=l;i<=r;i++)
            {
                lef[i]=i;
                while(lef[i]>l&&b[i]>b[lef[i]-1])
                    lef[i]=lef[lef[i]-1];
            }
            for(i=r;i>=l;i--)
            {
                rig[i]=i;
                while(rig[i]<r&&b[i]>=b[rig[i]+1])
                    rig[i]=rig[rig[i]+1];
            }
            for(i=l;i<=r;i++)
            {
                ans+=(i-lef[i]+1)*(rig[i]-i+1)*b[i];
            }
            cout<<ans<<endl;
        }
    }
}


#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;

long long a[100100],b[100100];
int lef[100100],rig[100100];

int main()
{
    long long ans,n,q,i,l,r;
    while(cin>>n>>q)
    {
        for(i=1;i<=n;i++)
            scanf("%I64d",&a[i]);
        for(i=2;i<=n;i++)
            b[i]=abs(a[i]-a[i-1]);
        while(q--)
        {
            scanf("%d%d",&l,&r);
            ans=0;l++;
            for(i=l;i<=r;i++)
            {
                lef[i]=i;
                while(lef[i]>l&&b[i]>b[lef[i]-1])
                    lef[i]=lef[lef[i]-1];
            }
            for(i=r;i>=l;i--)
            {
                rig[i]=i;
                while(rig[i]<r&&b[i]>=b[rig[i]+1])
                    rig[i]=rig[rig[i]+1];
            }
            for(i=l;i<=r;i++)
            {
                ans+=(long long)(i-lef[i]+1)*(long long)(rig[i]-i+1)*b[i];
            }
            cout<<ans<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;

long long a[100100],b[100100];
int lef[100100],rig[100100];

int main()
{
    long long ans,n,q,l,r;
    while(cin>>n>>q)
    {
        for(int i=1;i<=n;i++)
            scanf("%I64d",&a[i]);
        for(int i=2;i<=n;i++)
            b[i]=abs(a[i]-a[i-1]);
        while(q--)
        {
            scanf("%d%d",&l,&r);
            ans=0;l++;
            for(int i=l;i<=r;i++)
            {
                lef[i]=i;
                while(lef[i]>l&&b[i]>b[lef[i]-1])
                    lef[i]=lef[lef[i]-1];
            }
            for(int i=r;i>=l;i--)
            {
                rig[i]=i;
                while(rig[i]<r&&b[i]>=b[rig[i]+1])
                    rig[i]=rig[rig[i]+1];
            }
            for(int i=l;i<=r;i++)
            {
                ans+=(long long)(i-lef[i]+1)*(long long)(rig[i]-i+1)*b[i];
            }
            cout<<ans<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;

long long a[100100],b[100100];
int lef[100100],rig[100100];

int main()
{
    long long ans,n,q,l,r;
    while(cin>>n>>q)
    {
        for(i=1;i<=n;i++)
            scanf("%I64d",&a[i]);
        for(i=2;i<=n;i++)
            b[i]=abs(a[i]-a[i-1]);
        while(q--)
        {
            scanf("%d%d",&l,&r);
            ans=0;l++;
            for(int i=l;i<=r;i++)
            {
                lef[i]=i;
                while(lef[i]>l&&b[i]>b[lef[i]-1])
                    lef[i]=lef[lef[i]-1];
            }
            for(int i=r;i>=l;i--)
            {
                rig[i]=i;
                while(rig[i]<r&&b[i]>=b[rig[i]+1])
                    rig[i]=rig[rig[i]+1];
            }
            for(int i=l;i<=r;i++)
            {
                ans+=(long long)(i-lef[i]+1)*(long long)(rig[i]-i+1)*b[i];
            }
            cout<<ans<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;

long long a[100100],b[100100];
int lef[100100],rig[100100];

int main()
{
    int n,q,l,r;
    long long ans;
    scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++)
            scanf("%I64d",&a[i]);
        for(int i=2;i<=n;i++)
            b[i]=abs(a[i]-a[i-1]);
        while(q--)
        {
            scanf("%d%d",&l,&r);
            ans=0;l++;
            for(int i=l;i<=r;i++)
            {
                lef[i]=i;
                while(lef[i]>l&&b[i]>b[lef[i]-1])
                    lef[i]=lef[lef[i]-1];
            }
            for(int i=r;i>=l;i--)
            {
                rig[i]=i;
                while(rig[i]<r&&b[i]>=b[rig[i]+1])
                    rig[i]=rig[rig[i]+1];
            }
            for(int i=l;i<=r;i++)
            {
                ans+=(long long)(i-lef[i]+1)*(long long)(rig[i]-i+1)*b[i];
            }
            printf("%I64d\n",ans);
        }
}


#include<bits/stdc++.h>
using namespace std;

long long a[100100],b[100100];
int lef[100100],rig[100100];

int main()
{
    long long ans,n,q,i,l,r;
    while(cin>>n>>q)
    {
        for(i=1;i<=n;i++)
            scanf("%I64d",&a[i]);
        for(i=2;i<=n;i++)
            b[i]=abs(a[i]-a[i-1]);
        while(q--)
        {
            scanf("%d%d",&l,&r);
            ans=0;l++;
            for(i=l;i<=r;i++)
            {
                lef[i]=i;
                while(lef[i]>l&&b[i]>b[lef[i]-1])
                    lef[i]=lef[lef[i]-1];
            }
            for(i=r;i>=l;i--)
            {
                rig[i]=i;
                while(rig[i]<r&&b[i]>=b[rig[i]+1])
                    rig[i]=rig[rig[i]+1];
            }
            for(i=l;i<=r;i++)
            {
                ans+=(long long)(i-lef[i]+1)*(long long)(rig[i]-i+1)*b[i];
            }
            cout<<ans<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;

long long a[100100],b[100100];
int lef[100100],rig[100100];

int main()
{
    long long ans,n,q,l,r;
    scanf("%d%d",&n.&q);
        for(int i=1;i<=n;i++)
            scanf("%I64d",&a[i]);
        for(int i=2;i<=n;i++)
            b[i]=abs(a[i]-a[i-1]);
        while(q--)
        {
            scanf("%d%d",&l,&r);
            ans=0;l++;
            for(int i=l;i<=r;i++)
            {
                lef[i]=i;
                while(lef[i]>l&&b[i]>b[lef[i]-1])
                    lef[i]=lef[lef[i]-1];
            }
            for(int i=r;i>=l;i--)
            {
                rig[i]=i;
                while(rig[i]<r&&b[i]>=b[rig[i]+1])
                    rig[i]=rig[rig[i]+1];
            }
            for(int i=l;i<=r;i++)
            {
                ans+=(long long)(i-lef[i]+1)*(long long)(rig[i]-i+1)*b[i];
            }
            printf("%I64d\n",ans);
        }
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <set>

#define FOR(x, a, b) for (int x = a; x <= b; ++x)
#define FOD(x, a, b) for (int x = a; x >= b; --x)
#define REP(x, a, b) for (int x = a; x < b; ++x)
#define ll long long
#define fi first
#define se second
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A, n) { cout << #A << " = "; FOR(i, 1, n) cout << A[i] << " "; cout << endl; }
#define PR0(A, n)  { cout << #A << " = "; REP(i, 0, n) cout << A[i] << " "; cout << endl; }

using namespace std;

typedef pair <int, int> II;

const int maxn = 1e5 + 4;

int a[maxn], n, q, X[maxn], Y[maxn];
vector <II> D;
ll ans[maxn];
set <int> S;

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif // ONLINE_JUDGE
    scanf("%d%d", &n, &q);
    REP(i, 0, n) scanf("%d", &a[i]);
    REP(i, 0, n - 1) D.push_back(II(abs(a[i + 1] - a[i]), i));
    REP(i, 0, q)
    {
        scanf("%d%d", &X[i], &Y[i]);
        X[i]--;
        Y[i]--;
    }
    sort(D.begin(), D.end());
    reverse(D.begin(), D.end());
    S.insert(-1);
    S.insert(n - 1);
    REP(i, 0, n - 1)
    {
        int p = D[i].se;
        int l = *(--S.lower_bound(p));
        int r = *(S.lower_bound(p));

        S.insert(p);
        REP(k, 0, q)
        {
            int x = min(Y[k], p) - max(X[k], l + 1) + 1;
            x = max(x, 0);
            int y = min(Y[k], r) - max(X[k], p + 1) + 1;
            y = max(y, 0);
            ans[k] += 1ll * x * y * D[i].fi;
        }
    }
    REP(i, 0, q) printf("%I64d\n", ans[i]);

}


#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define pii pair<int, int>
#define fit(ta, a) for(ta::iterator i = a.begin(); i != a.end(); advance(i, 1))
#define debug printf
#define MAXN 101010
typedef long long lint;
using namespace std;

int n, q, vet[MAXN], ate[MAXN], apartir[MAXN];

int main(){
    scanf("%d%d",&n,&q);
    for(int i = 0; i < n; i++)
        scanf("%d", &vet[i]);
    while(q--){
        int ini, fim;
        lint ans = 0;
        scanf("%d%d", &ini, &fim);
        ini--, fim--;
        stack<int>big;
        for(int i = ini; i < fim; i++){
            while(!big.empty() && abs(vet[i]-vet[i+1]) >= abs(vet[big.top()] - vet[big.top()+1])){
                ate[big.top()] = i;
                big.pop();
            }
            big.push(i);
        }
        while(!big.empty()){
            ate[big.top()] = fim;
            big.pop();
        }
        for(int i = fim-1; i >= ini; i--){
            while(!big.empty() && abs(vet[i]-vet[i+1]) > abs(vet[big.top()] - vet[big.top()+1])){
                apartir[big.top()] = i+1;
                big.pop();
            }
            big.push(i);
        }
        while(!big.empty()){
            apartir[big.top()] = ini;
            big.pop();
        }
        for(int i = ini; i < fim; i++){
            ans += (i-apartir[i]+1LL)*(ate[i]-i)*(abs(vet[i]-vet[i+1]));
        }
        printf("%I64d\n", ans);
    }
}


#include"bits/stdc++.h"
#define clr(a) memset(a,0,sizeof(a))
#define ls pos<<1
#define rs pos<<1|1
#define lson L,mid,ls
#define rson mid+1,R,rs
#define mdzz int mid=(L+R)>>1;
#define fr first
#define sc second
using namespace std;

typedef long long LL;
typedef pair<int,int> pii;

const int N = 1e5+5;
const int M = 3605;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const double eps = 1e-6;

int n,m;
int a[N],q[N];
LL dp[N];

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1,l,r;i<=m;i++){
        scanf("%d%d",&l,&r);
        int tl = -1;LL ans = 0;
        q[++tl] = l;dp[l] = 0;
        for(int i=l+1;i<=r;i++){
            while(tl>=1 &&abs(a[q[tl]] - a[q[tl]-1]) <= abs(a[i] - a[i-1])) tl--;
            dp[i] = dp[q[tl]] + (LL)abs(a[i] - a[i-1]) * (i - q[tl]);
            ans += dp[i];
            //cout<<i<<' '<<q[tl]<<' '<<abs(a[i] - a[i-1])<<endl;
            q[++tl] = i;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
/*
10 1
1 5 2 9 1 3 4 2 1 7
7 10
*/


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;

typedef long long LL;
const int Maxn = 100050;

int a[Maxn], st[Maxn], x[Maxn], right[Maxn], left[Maxn];

int main() {
#ifndef ONLINE_JUDGE
  freopen("d.in", "r", stdin);
#endif

  int n, q;
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (int i = 1; i < n; ++i)
    x[i] = abs(a[i]-a[i+1]);
  int top;
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    LL ans = 0;
    st[0] = l-1, top = 0;
    for (int i = l; i < r; ++i) {
      while (top && x[i] > x[st[top]]) {
	right[st[top]] = i-1;
	top--;
      }
      left[i] = st[top]+1;
      st[++top] = i;
    }
    for (int i = top; i; --i) right[st[i]] = r-1;

    for (int i = l; i < r; ++i) {
      ans += 1ll*x[i]*(i-left[i]+1)*(right[i]-i+1);
    }
    printf("%lld\n", ans);
  }
  return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
ll mpow(ll a, ll n,ll mod)
{ll ret=1;ll b=a;while(n) {if(n&1)
    ret=(ret*b)%mod;b=(b*b)%mod;n>>=1;}
return (ll)ret;
}
using namespace std;
#define mem(x,a) memset(x,a,sizeof(x))
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
using namespace std;
#define N (int)1e5+5
int dp[N][22];
int a[N],n,q;
int diff[N];
void comp(){
    int i,j;
    int x=n-1;
    for(i=0;i<x;i++){
        dp[i][0]=diff[i];
    }
    for(i=1;(1<<i)<=x;i++){
        for(j=0;j+(1<<i)-1<x;j++){
            dp[j][i]=max(dp[j][i-1],dp[j+(1<<(i-1))][i-1]);
        }
    }
}
int query(int i,int j){
    int k=(j-i+1);
    int p=0;
    while((1<<p)<=k)p++;
    p--;
    if(dp[i][p]>=dp[j-(1<<p)+1][p])
        return dp[i][p];
    else
        return dp[j-(1<<p)+1][p];
}
vector<pii> v;
int get()
{
  char c;
  while(c=getchar(),(c<'0'||c>'9')&&(c!='-'));
  bool flag=(c=='-');
  if(flag)
    c=getchar();
  int x=0;
  while(c>='0'&&c<='9')
    {
      x=x*10+c-48;
      c=getchar();
    }
  return flag?-x:x;
}

void output(ll x)
{
  if(x<0)
    {
      putchar('-');
      x=-x;
    }
  int len=0,data[20];
  while(x)
    {
      data[len++]=x%10;
      x/=10;
    }
  if(!len)
    data[len++]=0;
  while(len--)
    putchar(data[len]+48);
  putchar('\n');
}

int main(){
    int i,j;
    n=get();
    q=get();
    for(i=0;i<n;i++)
        a[i]=get();
    for(i=0;i<n-1;i++)
        diff[i]=abs(a[i]-a[i+1]);
    comp();
    for(j=0;j<=n-2;j++){
        int lo=0,hi=j;
        while(lo<hi){
            int mid=(lo+hi)>>1;
            if(query(mid,j-1)>=diff[j])
                    lo=mid+1;
                else
                    hi=mid;
            }
            int L=lo;
            lo=j;
            hi=n-2;
            while(lo<hi){
                int mid=(lo+hi+1)>>1;
                if(query(j,mid)>diff[j])
                    hi=mid-1;
                else
                    lo=mid;
            }
            v.pb(mp(L,lo));
        }
        while(q--){
            int x,y;
            x=get();
            y=get();
            x--;
            y-=2;
            ll ans=0;
            for(j=x;j<=y;j++){
                int L=max(v[j].fi,x);
                int lo=min(v[j].se,y);
                ll acl=j-1;
            ll acr=lo+1;
            ll x=diff[j];
            ans+=(acr-j)*x;
            ans+=((j+1)-L)*x;
            ans+=(acl-L+1)*(acr-(j+2)+1)*x;
            ans-=x;

            }
            output(ans);
        }
    return 0;
}
//4 3 7 8 2 1 2 1


#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
ll mpow(ll a, ll n,ll mod)
{ll ret=1;ll b=a;while(n) {if(n&1)
    ret=(ret*b)%mod;b=(b*b)%mod;n>>=1;}
return (ll)ret;
}
using namespace std;
#define mem(x,a) memset(x,a,sizeof(x))
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
using namespace std;
#define N (int)1e5+5
int dp[N][22];
int a[N],n,q;
int diff[N];
void comp(){
    int i,j;
    int x=n-1;
    for(i=0;i<x;i++){
        dp[i][0]=diff[i];
    }
    for(i=1;(1<<i)<=x;i++){
        for(j=0;j+(1<<i)-1<x;j++){
            dp[j][i]=max(dp[j][i-1],dp[j+(1<<(i-1))][i-1]);
        }
    }
}
int query(int i,int j){
    int k=(j-i+1);
    int p=0;
    while((1<<p)<=k)p++;
    p--;
    if(dp[i][p]>=dp[j-(1<<p)+1][p])
        return dp[i][p];
    else
        return dp[j-(1<<p)+1][p];
}
int get()
{
  char c;
  while(c=getchar(),(c<'0'||c>'9')&&(c!='-'));
  bool flag=(c=='-');
  if(flag)
    c=getchar();
  int x=0;
  while(c>='0'&&c<='9')
    {
      x=x*10+c-48;
      c=getchar();
    }
  return flag?-x:x;
}

void output(ll x)
{
  if(x<0)
    {
      putchar('-');
      x=-x;
    }
  int len=0,data[20];
  while(x)
    {
      data[len++]=x%10;
      x/=10;
    }
  if(!len)
    data[len++]=0;
  while(len--)
    putchar(data[len]+48);
  putchar('\n');
}
int main(){
    int i,j;
    n=get();
    q=get();
    for(i=0;i<n;i++)
        a[i]=get();
    for(i=0;i<n-1;i++)
        diff[i]=abs(a[i]-a[i+1]);
    comp();
    while(q--){
        int x,y;
        x=get();
        y=get();
        x--;
        y-=2;
        ll ans=0;
        for(j=x;j<=y;j++){
            int lo=x,hi=j;
            while(lo<hi){
                int mid=(lo+hi)>>1;
                if(query(mid,j-1)>=diff[j])
                    lo=mid+1;
                else
                    hi=mid;
            }
            int L=lo;
            lo=j;
            hi=y;
            while(lo<hi){
                int mid=(lo+hi+1)>>1;
                if(query(j,mid)>diff[j])
                    hi=mid-1;
                else
                    lo=mid;
            }
            ll acl=j-1;
            ll acr=lo+1;
            ll x=diff[j];
            ans+=(acr-j)*x;
            ans+=((j+1)-L)*x;
            ans+=(acl-L+1)*(acr-(j+2)+1)*x;
            ans-=x;
        }
        output(ans);
    }
    return 0;
}
//4 3 7 8 2 1 2 1