Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define gc c=getchar()
- #define r(x) read(x)
- #define ll long long
- template<typename T>
- inline void read(T&x){
- x=0;T k=1;char gc;
- while(!isdigit(c)){if(c=='-')k=-1;gc;}
- while(isdigit(c)){x=x*10+c-'0';gc;}x*=k;
- }
- struct Person{
- int a,b,c,d;
- };
- inline bool cmp(const Person &a,const Person &b){
- return a.d<b.d;
- }
- vector<Person>A,B;
- int main(){
- // freopen(".in","r",stdin);
- // freopen(".out","w",stdout);
- int n,s;r(n),r(s);
- ll cnt1=0;
- ll cnt2=0;
- ll ans=0;
- for(int i=0;i<n;++i){
- int a,b,c;r(c),r(a),r(b);
- if(a>b){
- A.push_back(Person{a,b,c,a-b});
- cnt1+=c;
- ans+=(ll)a*c;
- }
- else {
- B.push_back(Person{a,b,c,b-a});
- cnt2+=c;
- ans+=(ll)b*c;
- }
- }
- cnt1%=s;
- cnt2%=s;
- if(cnt1+cnt2<=s){
- sort(A.begin(),A.end(),cmp);
- sort(B.begin(),B.end(),cmp);
- ll cost1=0;
- ll cost2=0;
- for(int i=0;i<A.size()&&cnt1>0;++i){
- cost1+=A[i].d*min(cnt1,(ll)A[i].c);
- cnt1-=A[i].c;
- }
- for(int i=0;i<B.size()&&cnt2>0;++i){
- cost2+=B[i].d*min(cnt2,(ll)B[i].c);
- cnt2-=B[i].c;
- }
- ans-=min(cost1,cost2);
- }
- printf("%lld\n",ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement