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 ;
}

最新文章

  1. (Spring4 json入门)Spring4+SpringMVC+页面数据发送与接收(json格式)
  2. SQL 表 和字符串 互转 (行列互转)
  3. Windows Azure - App Services
  4. 《head first java 》读书笔记(四)
  5. C++的常量折叠(二)
  6. hdu 3652 B-number(数字dp)
  7. Net Framework中的提供的常用委托类型
  8. SignalR的坑爹细节
  9. sqlserver低版本生成插入脚本
  10. 子进程 已安装 post-installation 脚本 返回错误状态 1,dpkg: 处理软件包 python-crypto (--configure)时出错: 该软件包正处于非常不稳定的状态;
  11. jquery发起get/post请求_或_获取html页面数据
  12. SQL SERVER 查询与整理索引碎片
  13. eclipse 部署项目
  14. zookeeper windows伪集群搭建
  15. linux 学习笔记 查看端口
  16. 《Redis 持久化》
  17. lnmp 基础设置
  18. java pojo类
  19. [转][MVC]更新 dll 后版本不匹配的问题
  20. java反射中的动态代理机制(有实例)

热门文章

  1. 洛谷P5087 数学
  2. [React] Optimistic UI update in React using setState()
  3. JBOSS部署项目之后,无法通过IP地址訪问,仅仅能通过localhost或者127.0.0.1訪问
  4. 10gR2 rac怎样重跑root.sh ?
  5. hdu_5154 拓扑
  6. hdoj--2282--Chocolate(最小费用)
  7. 安卓第一课:android studio 的环境搭建与真机运行以及遇到的问题
  8. POJ 2528 线段树
  9. 函数响应式编程(FRP)从入门到”放弃”——基础概念篇
  10. BZOJ3744 Gty的妹子序列(分块+树状数组)