A Question of Ingestion
InterviewTime limit2sMemory limit512 MB
Given n hourly courses with calorie counts, maximize total calories eaten when the per-hour limit starts at m, shrinks to two thirds while eating, and resets after two skipped hours.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Greedy, Math, Implementation
- Solved
- No attempts yet
Problem
Stan is a graduate student, so the question on his mind is where the next meal comes from. The corporate sponsors of his research team invited him to a barbecue with several courses, and each course lasts exactly one hour.
Stan's appetite over consecutive hours follows a fixed rule. In the first hour he can eat up to calories. For every hour he keeps eating, that limit drops to two thirds of the previous hour's limit, truncating any fraction of a calorie. If he stops eating for one hour, then in the next hour he can eat at the same limit he had before he stopped. If he stops for two hours in a row, then in the hour after that his limit is again and the decrease starts over.
Take . Eating for five hours in a row gives hourly limits 900, 600, 400, 266, 177. Skipping the third hour gives 900, 600, 0, 600, 400. Skipping both the third and the fourth hour gives 900, 600, 0, 0, 900.
In each hour he eats, Stan consumes the smaller of that hour's limit and the calories of the course served in that hour. Eating a course that is smaller than the limit still counts as eating, so the limit drops all the same. Which hours are worth skipping therefore depends on the menu.
Given the number of courses and the calories of each course, find the largest total number of calories Stan can eat.
Input
The first line contains two positive integers and separated by a space, the number of courses and the calorie limit for the first hour, with and .
The second line contains positive integers, the calories of each course in the order they are served.
Output
Print the maximum number of calories Stan can eat.