Buying Feed, II
InterviewTime limit1sMemory limit128 MB
Pick up to K pounds of feed from stores along a line, paying each store's price plus transport cost of distance carried, and minimize the total.
- Level
Medium5 of 10
- Topics
- Greedy, Sorting, Math, Implementation
- Solved
- No attempts yet
Problem
Farmer John (FJ) needs to travel to town to buy pounds of feed (). Driving miles with pounds of feed in his truck costs cents.
The county feed lot has stores (, conveniently numbered through ) that sell feed. Every store lies on a segment of the axis whose length is (). Store is at location () on the number line and can sell up to pounds () of feed at a price of cents () per pound. Remarkably, a single point on the axis may host more than one store.
FJ starts at location on the number line and can drive only in the positive direction, ultimately arriving at location carrying at least pounds of feed. Along the way he may stop at any store and buy any amount of feed up to that store's limit.
What is the minimum amount FJ must pay to buy and transport the pounds of feed? A solution is guaranteed to exist.
Input
- Line : Three space-separated integers , , and .
- Lines to : Line contains three space-separated integers , , and describing store .
Output
- Line : A single integer, the minimum cost for FJ to buy and transport the feed.