Optimal Milking
Time limit1sMemory limit128 MB
Each day one machine value changes, then choose nonadjacent machines with maximum total output and add it to the overall sum.
- Level
Medium7 of 10
- Topics
- Segment tree, Dynamic programming
- Solved
- No attempts yet
Problem
Farmer John's new barn holds milking machines standing in a row, numbered through from the left.
Machine extracts units of milk per day. The machines sit so close together that if machine runs on a given day, neither of its neighbors can run that day. A machine at either end has only one neighbor. Farmer John may choose a different set of machines to run on each day.
Farmer John wants the largest total amount of milk he can extract over days. At the start of each day he has time to service one machine, and that machine's daily output changes from that day onward. Given the list of daily changes, report the maximum total milk over the days. The answer can exceed a 32-bit integer.
, , and .
Input
The first line contains and .
Each of the next lines gives one initial value, with line holding .
Each of the following lines contains two integers and . Line means Farmer John sets to at the start of day .
Output
Print the maximum total amount of milk Farmer John can produce over the days.
Hint
Suppose five machines start with outputs 1, 2, 3, 4, 5 and machine 5 drops to 2 on the morning of day one.
Day one is best at , and reaches 6 as well. If machine 2 then rises to 7, day two is , and if machine 1 rises to 10, day three is .