Stimulant
InterviewTime limit10sMemory limit512 MB
Given daily study and gaming hours and K stimulants that double the effect from their day on, find the maximum total skill with at most one stimulant per day.
- Level
Medium5 of 10
- Topics
- Prefix sum, Sorting, Greedy
- Solved
- No attempts yet
Problem
Junhyuk takes special training to become an expert in algorithms. During the training period, on some days he only studies, and on other days he only plays games.
Junhyuk's training lasts days. Each hour of studying during the training period raises his skill by 1, and each hour of gaming lowers it by 1. If he studies for 5 hours on day 1 and plays games for 3 hours on day 2, the total increase in skill is 2. To maximize the efficiency of his studying, Junhyuk orders stimulants with a special effect. When he takes a stimulant, he becomes awakened from that day on. Every hour of studying and gaming from that day, including that day, has double effect.
The effects of stimulants stack. For example, if he takes a stimulant on day 1 and another on day 2, then on day 3, one hour of studying raises his skill by 4. Conversely, one hour of gaming on day 3 lowers his skill by 4.
Junhyuk takes at most one stimulant per day, and he does not have to take all stimulants he ordered. Find the maximum total skill gained from studying by the last day, when he takes the stimulants appropriately.
Input
The first line contains and ().
The second line contains integers separated by spaces. On day , if he studies for hours, the value is . If he plays games for hours, the value is . A day in Junhyuk's training is 1,000,000 hours, not 24. ()
Output
Print the maximum skill Junhyuk can gain by taking stimulants appropriately, taken modulo (). If the maximum is negative, print it as is, without taking the modulo.
It can be proven that the maximum is always greater than .