Cutting Trees
Time limit2sMemory limit512 MB
Cut at most M trees per evening with distinct machines to exactly D_i meters, and find the minimum total height after T days.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, Sorting
- Solved
- No attempts yet
Problem
Subin grows N trees in the garden. Tree i is meters tall right now and grows meters every morning.
Subin owns M cutting machines. Machine i picks one tree whose height is greater than meters and cuts that tree down to exactly meters. A tree whose height is at most meters cannot be cut by machine i.
Every evening Subin picks some trees and cuts them. Picking no tree at all is allowed. Two conditions must hold.
- Each tree is cut at most once on a given day.
- Each machine can be used at most once on a given day.
So the trees cut on the same evening are paired with distinct machines, one machine per tree.
A day consists of the morning growth followed by the evening cutting. Write a program that finds the smallest possible sum of the tree heights after T days.
Input
The first line contains the number of trees N and the number of machines M, separated by a space.
The second line contains separated by spaces.
The third line contains separated by spaces.
The fourth line contains separated by spaces.
The fifth line contains T.
Output
Print the smallest possible sum of the tree heights after T days, on one line.
Limits
Hint
Suppose there are 2 trees. Tree 1 is 4 meters tall and grows 7 meters a day, tree 2 is 7 meters tall and grows 1 meter a day. There is a single machine with , and T is 1.
After the first morning, tree 1 is meters tall and tree 2 is meters tall. Cutting tree 1 down to 7 meters in the evening leaves a height sum of 15, and no smaller sum is reachable.