Hot days codeforces round #132 (Div. 2) D (Greedy)

Source: Internet
Author: User

Description

The official capital and the cultural capital of berland are connected by a single road running throughNRegions. Each region has a unique climate, soI-Th (1? ≤?I? ≤?N) Region has a stable temperatureTIDegrees in summer.

This summer a groupMSchoolchildren wants to get from the official capital to the cultural capital to visit museums and sights. the trip organizers transport the children between the cities in buses, but sometimes it is very hot. specifically, if the bus is driving throughI-Th region and hasKSchoolchildren, then the temperature inside the bus isTI? +?KDegrees.

Of course, nobody likes it when the bus is hot. So, when the bus drives throughI-Th region, if it has moreTIDegrees inside, each of the schoolchild in the bus demands compensation for the uncomfortable conditions. The compensation is as largeXIRubles and it is charged in each region where the temperature in the bus exceeds the limit.

To save money, the organizers of the trip may arbitrarily add or remove extra buses in the beginning of the trip, and between regions (of course, they need at least one bus to pass any region ). the organizers can also arbitrarily sort the children into buses, however, each of buses inI-Th region will cost the organizersCostIRubles. Please note that sorting children into buses takes no money.

Your task is to find the minimum number of rubles, which the organizers will have to spend to transport all schoolchildren.

Input

The first input line contains two integersNAndM(1? ≤?N? ≤? 105; 1? ≤?M? ≤? 106)-the number of regions on the way and the number of schoolchildren in the group, correspondingly. NextNLines contain four integers each:I-Th line containsTI,TI,XIAndCostI(1? ≤?TI,?TI,?XI,?CostI? ≤? 106). The numbers in the lines are separated by single spaces.

Output

Print the only integer-the minimum number of roubles the organizers will have to spend to transport all schoolchildren.

Please, do not use the % LLD specifier to read or write 64-bit integers in bytes ++. It is preferred to use Cin, cout streams or the % i64dspecifier.

Sample Input

Input
2 1030 35 1 10020 35 10 10
Output
120
Input
3 10010 30 1000 15 10 1000 310 40 1000 100000
Output
200065

Hint

In the first sample the organizers will use only one bus to travel through the first region. However, the temperature in the bus will continue 30? +? 10? =? 40 degrees and each of 10 schoolchildren will ask for compensation. only one bus will transport the Group through the second region too, but the temperature inside won't exceed the limit. overall, the organizers will spend 100? +? 10? +? 10? =? 120 rubles.


N cities, m people, each city has a temperature T, and the temperature inside the car cannot exceed T. Otherwise, compensation should be made for each person's X yuan. The price of each car is C, the temperature in the car is t + people in the car. For each station, you can choose to take the metering car, the minimum cost?


Train of Thought: for each station, if T <t is a car, if T + M <= t, a car, if T <t but T + M> T;


So we have to consider it.


Now assume that everyone is in a car and the price is C + M * X;

If some people go out to rent a car and find that they use less money, it is best to fill the car, no, the person who can sit in the empty location needs compensation in the fully occupied car,

If it is cost-effective for some people to go out, why? In this case, there are only two situations.

1 'Everyone is in a car

2. Each car cannot exceed the temperature T, but it is full.


Now, the code is displayed:


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define N 1000100typedef __int64 ll;ll n,m,t,T,x,c;int main(){ll ans;while(~scanf("%I64d%I64d",&n,&m)){ans=0;while(n--){scanf("%I64d%I64d%I64d%I64d",&t,&T,&x,&c);if(T<=t){ans+=x*m+c;continue;}if(t+m<=T){ans+=c;continue;}ll temp=T-t;temp=m%temp? m/temp+1:m/temp;ans+=min(x*m+c,c*temp);}printf("%I64d\n",ans);} return 0;}







Hot days codeforces round #132 (Div. 2) D (Greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.