B. Ordering Pizza

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 exactly S slices.

It is known that the i-th contestant will eat si slices of pizza, and gain ai happiness for each slice of type 1 pizza they eat, and bi 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 and S (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 12
3 5 7
4 6 7
5 9 5
Output
84
Input
6 10
7 4 7
5 8 8
12 5 8
6 11 6
3 3 7
5 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 be 3·5 + 4·6 + 5·9 = 84, and if you buy a type 2 pizza, the total happiness will be 3·7 + 4·7 + 5·5 = 74.

在买的披萨数量最少的前提下幸福度最大。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <cstdlib>
#include <windows.h>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 1044266558
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll n,s,m,a,b,cnta=,cntb=;
ll ans=,pos=,inf=;
vector<pair<ll,ll> >v[];
int main()
{
scanf("%lld%lld",&n,&s);
for(int i=;i<n;i++)
{
scanf("%lld%lld%lld",&m,&a,&b);
if(a>=b) ans+=m*a,cnta=(cnta+m)%s,v[].push_back(make_pair(a-b,m));
else ans+=m*b,cntb=(cntb+m)%s,v[].push_back(make_pair(b-a,m));
}
sort(v[].begin(),v[].end());
sort(v[].begin(),v[].end());
if(cnta+cntb>s) return *printf("%lld\n",ans);
for(int i=;i<v[].size();i++)
{
pos+=min(v[][i].second,cnta)*v[][i].first;
cnta-=min(cnta,v[][i].second);
}
for(int i=;i<v[].size();i++)
{
inf+=min(v[][i].second,cntb)*v[][i].first;
cntb-=min(cntb,v[][i].second);
}
printf("%lld\n",ans-min(inf,pos));
return ;
}

cf 865 B. Ordering Pizza的更多相关文章

  1. Codeforce 867 C. Ordering Pizza (思维题)

    C. Ordering Pizza It's another Start[c]up finals, and that means there is pizza to order for the ons ...

  2. C - Ordering Pizza CodeForces - 867C 贪心 经典

    C - Ordering Pizza CodeForces - 867C C - Ordering Pizza 这个是最难的,一个贪心,很经典,但是我不会,早训结束看了题解才知道怎么贪心的. 这个是先 ...

  3. Codeforces Round #437 C. Ordering Pizza

    题意: n个人吃披萨,总共有两种披萨,每种披萨都是有S块,给出每个人要吃的块数,吃第一种披萨能获得的happy值,吃第二种披萨能获得的happy值,问你,在购买的披萨数最少的情况下能获得的最大的总的h ...

  4. 【Codeforces Round #437 (Div. 2) C】 Ordering Pizza

    [链接]h在这里写链接 [题意]     给你参赛者的数量以及一个整数S表示每块披萨的片数.     每个参数者有3个参数,si,ai,bi;     表示第i个参赛者它要吃的披萨的片数,以及吃一片第 ...

  5. Codeforces Round #437 (Div. 2)[A、B、C、E]

    Codeforces Round #437 (Div. 2) codeforces 867 A. Between the Offices(水) 题意:已知白天所在地(晚上可能坐飞机飞往异地),问是否从 ...

  6. CF泛做

    CF Rd478 Div2 A Aramic script 题意:给定几个字符串,去重后,求种类 思路:直接map乱搞 #include<bits/stdc++.h> using name ...

  7. Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2)

    Problem A Between the Offices 水题,水一水. #include<bits/stdc++.h> using namespace std; int n; ]; i ...

  8. Write-up-Bob_v1.0.1

    关于 下载地址:点我 哔哩哔哩视频:哔哩哔哩 信息收集 网卡:vmnet8:IP在192.168.131.1/24 ➜ ~ ip a show dev vmnet8 5: vmnet8: <BR ...

  9. UVA 565 565 Pizza Anyone? (深搜 +位运算)

      Pizza Anyone?  You are responsible for ordering a large pizza for you and your friends. Each of th ...

随机推荐

  1. 华夏60 战斗机(最短路dijkstra)

    华夏60 战斗机(最短路dijkstra) 华夏60 超音速战斗机是当今世界上机动性能最先进的战斗机.战斗过程中的一个关键问题是如何在最短的时间内使飞机从当前的飞行高度和速度爬升/俯冲到指定的高度并达 ...

  2. Yocto tips (15): Yocto中的包管理器

    使用包管理器 在local.conf中使能就可以: 然后编译后就会有rpm包了: 配置文件server 能够使用ngix和apache.可是我们也能够仅仅用使用python: python -m Si ...

  3. 10、匿名内部类、枚举类、日期、Math、Random、String、equals、StringBuffer、包装类、对象数组、克隆,标准输出3

    1对象的克隆(clone) 单纯的同类的两个对象a0 a00,a0=a00只是栈指向同一个堆,而不是开辟两个新堆,修改其中一个,另一个也会受牵连. 需要重写Clone()方法,并且实现Cloneabl ...

  4. Android清单文件具体解释(六) ---- &lt;activity&gt;节点的属性

    1.android:allowTaskReparenting android:allowTaskReparenting是一个任务调整属性,它表明当这个任务又一次被送到前台时,该应用程序所定义的Acti ...

  5. bzoj1009: [HNOI2008]GT考试(kmp+矩阵乘法)

    1009: [HNOI2008]GT考试 题目:传送门 题解: 看这第一眼是不是瞬间想起组合数学??? 没错...这样想你就GG了! 其实这是一道稍有隐藏的矩阵乘法,好题! 首先我们可以简化一下题意: ...

  6. python 3.x 学习笔记4(函数)

    1.编程方式分:面向对象.面向过程.函数式编程 2.区分面向对象---->类---->class面向过程---->过程---->def函数式编程---->函数----&g ...

  7. Redis缓存Mysql模拟用户登录Java实现实例

    https://blog.csdn.net/suneclipse/article/details/50920396

  8. iOS 集成Protobuf,转换proto文件

    原文地址:http://blog.csdn.net/hyq4412/article/details/54891038 附加Homebrew安装地址:https://brew.sh/index_zh-c ...

  9. BZOJ 4373算术天才⑨与等差数列(线段树)

    题意:给你一个长度为n的序列,有m个操作,写一个程序支持以下两个操作: 1. 修改一个值 2. 给出三个数l,r,k, 询问:如果把区间[l,r]的数从小到大排序,能否形成公差为k的等差数列. n,m ...

  10. yes---重复输出指定的字符串

    yes命令在命令行中输出指定的字符串,直到yes进程被杀死.不带任何参数输入yes命令默认的字符串就是y. 语法 yes(参数) 参数 字符串:指定要重复打印的字符串. 实例 [root@localh ...