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. 2015 Multi-University Training Contest 3 hdu 5316 Magician

    Magician Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  2. JAVA关于byte数组与String转换的问题

    1 public class ToString{ public static void main(String[] args){ String aa = "hellow"; byt ...

  3. [MST] Restore the Model Tree State using Hot Module Reloading when Model Definitions Change

    n this lesson, we will set up Hot Module Reloading(HMR), making it possible to load new definitions ...

  4. java9新特性-21-java的动态编译器

    1. 官方Feature 243: Java-Level JVM Compiler Interface 295: Ahead-of-Time Compilation 2. 产生背景 Oracle 一直 ...

  5. 针对发起alter tablespace test begin backup 断电情况的处理

    SQL> select tablespace_name from dba_tablespaces; TABLESPACE_NAME ------------------------------ ...

  6. Android 自定义viewpager 三张图片在同一屏幕轮播的效果

    github:https://github.com/nickeyCode/RoundImageViewPager 说实话不知道怎么描述这个效果,在网页上见得跟多,公司要求做这个效果得时候不知道怎么用文 ...

  7. 使用物化视图解决GoldenGate不能使用中文表名问题

    源端: conn sh/sh create table "学生" ("学号" number primary key,"姓名" varchar ...

  8. http://www.open-open.com/lib/tag/ReactiveCocoa

    http://www.open-open.com/lib/tag/ReactiveCocoa

  9. 手把手教你进行R语言的安装及安装过程中相关问题解决方案

    这篇文章旨在为R语言的新手铺砖引路,行文相对基础,希望对在R语言安装上有问题的小伙伴提供帮助和指引.一.什么是 R 语言R 编程语言被广泛应用在统计科学和商业领域. 在各种编程语言排名中 R 语言的排 ...

  10. 与 JSON 相关的一些操作 (项目中用到一部分,后续逐渐完善)

    1.JSON 打印 console.log(JSON.stringify(data, null, 4)); 2.JSON 格式判断 var isjson = typeof(obj) == " ...