Ordering Pizza(贪心)

来源:互联网 发布:import java 编辑:程序博客网 时间:2024/06/04 22:37
Ordering Pizza
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

It's another Start[c]up finals, and that means there is pizza to order for the onsite contestants. There are only 2 types of pizza (obviously not, but let's just pretend for the sake of the problem), and all pizzas contain exactlyS slices.

It is known that the i-th contestant will eatsi slices of pizza, and gainai happiness for each slice of type 1 pizza they eat, andbi happiness for each slice of type 2 pizza they eat. We can order any number of type 1 and type 2 pizzas, but we want to buy the minimum possible number of pizzas for all of the contestants to be able to eat their required number of slices. Given that restriction, what is the maximum possible total happiness that can be achieved?

Input

The first line of input will contain integers N andS (1 ≤ N ≤ 105, 1 ≤ S ≤ 105), the number of contestants and the number of slices per pizza, respectively.N lines follow.

The i-th such line contains integers si, ai, and bi (1 ≤ si ≤ 105, 1 ≤ ai ≤ 105, 1 ≤ bi ≤ 105), the number of slices the i-th contestant will eat, the happiness they will gain from each type 1 slice they eat, and the happiness they will gain from each type 2 slice they eat, respectively.

Output

Print the maximum total happiness that can be achieved.

Examples
Input
3 123 5 74 6 75 9 5
Output
84
Input
6 107 4 75 8 812 5 86 11 63 3 75 9 6
Output
314
Note

In the first example, you only need to buy one pizza. If you buy a type 1 pizza, the total happiness will be3·5 + 4·6 + 5·9 = 84, and if you buy a type 2 pizza, the total happiness will be3·7 + 4·7 + 5·5 = 74.

题目大意:

懒得说了;

题目思路:
我们很容易想到如果某一个参赛者a>b就选第一个,否则就选第二个,但是这样连样例都过不去,可能很多人就会放弃这个想法,我也是,其实就是题写的不够多,这题就是顺着这个思路想下去,我们可以发现对于该选第一个的部分,能刚好整除s的部分那肯定是选第一个的,该选第二个的同理,该考虑的是余下的部分,如果余下的部分大于s,的话自然还需要两个,这样刚好可以分给两种选择一人一个,如果小于等于s,那证明只能再买一个,那说明两种选择中必须更改一种选择,这样的话,我们就去按照替换后价值损失最小排序,两种选择中选出一种最小的就行

ac代码:

#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<sstream>#include<map>#include<vector>#define LL long long#define INF 0x3f3f3f3f#define LLINF 0x3f3f3f3f3f3f3fusing namespace std;vector<pair<LL,LL> >V1,V2;int n,s;int main(){    while(~scanf("%d%d",&n,&s))    {        V1.clear();        V2.clear();        LL sum1 = 0;        LL sum2 = 0;        LL ans = 0;        for(int i = 0;i<n;i++){            LL a,b,c;            scanf("%lld%lld%lld",&c,&a,&b);            if(a>b){                sum1 += c;                ans += c*a;                V1.push_back(make_pair(a-b,c));            }            else{                sum2 += c;                ans += c*b;                V2.push_back(make_pair(b-a,c));            }        }        sum1 %= s;        sum2 %= s;        if(sum1+sum2>s){            cout<<ans<<endl;            continue;        }        else{            sort(V1.begin(),V1.end());            sort(V2.begin(),V2.end());            LL tmp1 = 0;            LL tmp2  = 0;            int len = V1.size();            for(int i = 0;i<len;i++){                tmp1 += min(sum1,V1[i].second)*V1[i].first;                sum1 -= min(sum1,V1[i].second);            }            len = V2.size();            for(int i = 0;i<len;i++){                tmp2 += min(sum2,V2[i].second)*V2[i].first;                sum2 -= min(sum2,V2[i].second);            }            cout<<ans-min(tmp1,tmp2)<<endl;        }    }}