#P6064. Maximizing Sleep Utility
Maximizing Sleep Utility
Maximizing Sleep Utility
Betsy the cow has her day divided into segments () in a cyclic manner, and she must sleep during exactly segments (). Each segment has an associated utility value (). However, when she sleeps, only the segments after the first one in each contiguous sleep period produce utility, because the first segment of each sleep period (the moment she falls asleep) does not count. With the help of an alarm clock, Betsy can start sleeping at any segment boundary, but she must wake up at another boundary. Note that the time is cyclic; for example, if she starts sleeping at segment and continues into segment , then segment 's utility will be counted (and segment ’s utility will be lost as it is the start of that sleep period).
In other words, Betsy will select exactly segments to sleep, which will form one or more contiguous intervals on a circle. For each contiguous block of sleep of length , the utility gained is the sum of utilities of all segments except the first one. Betsy wants to maximize the total sleep utility. It turns out that the optimal solution is to choose one contiguous block of segments (taken cyclically) so that only a single segment (the first one) is penalized.
Formally, if Betsy chooses a contiguous block of segments, say (in cyclic order), the total utility is given by:
[
\text{Utility} = \left(\sum_{i=0}^{B-1} x_i\right) - x_0.
]
Your task is to compute the maximum possible total sleep utility Betsy can achieve by selecting the optimal contiguous block of segments from the circular list.
inputFormat
The input consists of two lines:
N B U1 U2 ... UN
Here, the first line contains two integers N and B as described above, and the second line contains N integers representing the utility values of the time segments.
outputFormat
Output a single integer, the maximum total sleep utility that can be achieved.
sample
3 2
5 1 5
5