#P2616. [USACO10JAN] Buying Feed, II S

[USACO10JAN] Buying Feed, II S

Description

Farmer John needs to travel to town to pick up K(1K100)K (1 \le K \le 100) pounds of feed. Driving D miles with KK pounds of feed in his truck costs DKD*K cents.

The county feed lot has N(1N100)N (1 \le N \le 100) stores (conveniently numbered 1...N1...N) that sell feed. Each store is located on a segment of the X axis whose length is E(1E350E (1 \le E \le 350). Store ii is at location Xi(0<Xi<E)X_i (0 < X_i < E) on the number line and can sell FJ as much as Fi(1Fi100)F_i (1 \le F_i \le 100) pounds of feed at a cost of Ci(1Ci1,000,000)C_i (1 \le C_i \le 1,000,000) cents per pound. Amazingly, a given point on the XX axis might have more than one store.

FJ starts at location 00 on this number line and can drive only in the positive direction, ultimately arriving at location EE, with at least KK pounds of feed. He can stop at any of the feed stores along the way and buy any amount of feed up to the the store's limit.

What is the minimum amount FJ has to pay to buy and transport the KK pounds of feed? FJ knows there is a solution.

2 5 3
3 1 2
4 1 2
1 1 1
7

Hint

Consider a sample where FJ needs two pounds of feed from three stores (locations: 11, 33, and 44) on a number line whose range is 0...50...5:

0   1   2   3   4   5 
+---|---+---|---|---+ 
    1       1   1      Available pounds of feed 
    1       2   2      Cents per pound 

It is best for FJ to buy one pound of feed from both the second and third stores. He must pay two cents to buy each pound of feed for a total cost of 44. When FJ travels from 33 to 44 he is moving 11 unit of length and he has 11 pound of feed so he must pay 11=11*1 = 1 cents.

When FJ travels from 44 to 55 he is moving one unit and he has 22 pounds of feed so he must pay 12=21*2 = 2 cents.

The total cost is 4+1+2=74+1+2 = 7 cents.