Cheap Airlines
Time limit1sMemory limit128 MB
Choose at most k non-overlapping contiguous segments of the array to maximize the total sum of their elements.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Prefix sum
- Solved
- No attempts yet
Problem
Bajtazar is finally taking a long-awaited holiday, which he plans to spend soaking up the sun on the golden sands of the Bitocki Sea. Weighing his biorhythm, the weather forecast, and the cultural events of Bitocja, Bajtazar assigns to each of the holiday days a recreation coefficient: an integer that says how much fun he would have that day. Each coefficient is an integer and may be negative, which means that on that day Bajtazar would rather stay home and weed his garden.
Luckily, Bajtazar does not have to spend the whole holiday by the sea. His favorite cheap airline is running a promotion that lets him buy up to plane tickets at an unusually attractive price (each ticket covers one round trip to the Bitocki Sea and back).
Help Bajtazar plan the holiday so that the sum of the recreation coefficients over the days he spends by the sea is as large as possible, given that he may fly to the sea at most times. Assume the planes fly at night, so a single trip covers a block of consecutive days by the sea. In other words, the days spent by the sea form at most non-overlapping intervals of consecutive days, and taking no trip at all (a sum of ) is allowed.
Input
The first line contains two integers and (). The second line contains integers, each with absolute value at most , describing the recreation coefficients of the consecutive holiday days.
Output
Print a single integer: the sum of recreation coefficients in an optimal holiday plan.