Buying Feed
Time limit2sMemory limit128 MB
Buy at least K pounds of feed from stores along a 1D route, paying purchase cost plus K^2 cents per mile for the load carried, and minimize the total.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Sorting, Implementation
- Solved
- No attempts yet
Problem
Farmer John (FJ) must travel to town to pick up () pounds of feed. Driving mile while carrying pounds of feed costs cents; driving miles with the same load costs cents.
FJ can buy feed from any of () stores, numbered . All stores sit on a segment of the X axis of length () miles. Store is at location () and sells feed at () cents per pound, up to () pounds. More than one store may share the same location.
FJ starts at location and can drive only in the positive direction, finishing at location carrying at least pounds of feed. Along the way he may stop at any store and buy any amount up to that store's limit.
What is the minimum total amount FJ must pay to buy and transport pounds of feed? It is guaranteed that the stores together can supply enough feed.
For example, suppose FJ needs pounds and there is one store at each of the locations , , and on a number line spanning :
0 1 2 3 4 5 X
+---|---+---|---|---+
1 1 1
1 2 2
Under each store, the first row of numbers is how many pounds it can sell and the second row is its price in cents per pound. So the store at location sells pound for cent, and the stores at locations and each sell pound for cents.
The cheapest plan is to buy pound from the store at location and pound from the store at location . The feed costs cents. Driving from to costs nothing because FJ carries no feed. Driving from to moves pound over mile, costing cent; driving from to moves pounds over mile, costing cents. The total is cents.
Input
- Line : three space-separated integers , , and .
- Lines to : line contains three space-separated integers , , and .
Output
- A single integer: the minimum total cost for FJ to buy and transport the feed.