Hot Days (Codeforces 215D)
Tags: Greedy, greedy

The official capital and the cultural capital of Berland are connected by a single road running through n regions. Each region has a unique climate, so the i-th (1 ≤ i ≤ n) region has a stable temperature of ti degrees in summer.This summer a group of m schoolchildren 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 through the i-th region and has k schoolchildren, then the temperature inside the bus is ti + k degrees.Of course, nobody likes it when the bus is hot. So, when the bus drives through the i-th region, if it has more than Ti degrees inside, each of the schoolchild in the bus demands compensation for the uncomfortable conditions. The compensation is as large as xi rubles 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 in the i-th region will cost the organizers costi rubles. 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 integers n and m (1 ≤ n ≤ 105; 1 ≤ m ≤ 106) — the number of regions on the way and the number of schoolchildren in the group, correspondingly. Next n lines contain four integers each: the i-th line contains ti, Ti, xi and costi (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 С++. It is preferred to use cin, cout streams or the %I64d specifier.

Note: In the first sample the organizers will use only one bus to travel through the first region. However, the temperature in the bus will equal 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.

Sample Inputs:
2 10
30 35 1 100
20 35 10 10

3 100
10 30 1000 1
5 10 1000 3
10 40 1000 100000

Sample Outputs:
120

200065

Solution (Python):
import sys
(n, m) = map(int, sys.stdin.readline().split())
ans = 0
for _ in range(n):
    (ti, Ti, xi, ci) = map(int, sys.stdin.readline().split())
    cost = xi * m * (ti + m > Ti) + ci
    if ti < Ti:
        cost2 = ci * ((m - 1) // (Ti - ti) + 1)
        cost = min(cost, cost2)
    ans += cost
print(ans)

Solution (Python):
import sys
import math
info = [int(x) for x in sys.stdin.readline().split()]
(n_stations, n_children) = (info[0], info[1])
min_sum = 0
for i in range(n_stations):
    values = [int(x) for x in sys.stdin.readline().split()]
    (initial_temp, temp_limit, cost_demanded, bus_price) = (values[0], values[1], values[2], values[3])
    if temp_limit - initial_temp > 0:
        n_buses_no_penalty = int(math.ceil(n_children / (temp_limit - initial_temp)))
        if n_buses_no_penalty * bus_price <= bus_price + n_children * cost_demanded:
            min_sum += n_buses_no_penalty * bus_price
        else:
            min_sum += bus_price + n_children * cost_demanded
    else:
        min_sum += bus_price + n_children * cost_demanded
print(min_sum)

Solution (Python):
import math
(n, m) = [int(x) for x in input().split()]
num = 0
for i in range(n):
    (t, T, x, cost) = [int(x) for x in input().split()]
    if T >= t + m:
        num += cost
    elif T > t:
        k = T - t
        num += min(x * m + cost, cost * math.ceil(m / k))
    else:
        num += x * m + cost
print(num)