An Amazon fulfillment center has
The reward for processing order
The manager wants to process exactly
Normally, no two selected orders can be adjacent. However, the manager has one special override.
The override allows at most one adjacent selected pair in the final selection.
Your task is to find the maximum total reward possible by selecting exactly
If it is impossible to select exactly
The first line contains two integers KaTeX can only parse string typed expression and KaTeX can only parse string typed expression — the number of orders and the number of orders to select.
The second line contains KaTeX can only parse string typed expression integers KaTeX can only parse string typed expression — the reward of each order.
Print a single integer — the maximum total reward possible. If it is impossible to select exactly KaTeX can only parse string typed expression valid orders, print KaTeX can only parse string typed expression.
Input
6 3
5 100 100 1 1 50Output250ExplanationSelect orders KaTeX can only parse string typed expression, KaTeX can only parse string typed expression, and KaTeX can only parse string typed expression. Orders KaTeX can only parse string typed expression and KaTeX can only parse string typed expression form one adjacent selected pair, which is allowed by the override. The total reward is KaTeX can only parse string typed expression.
Example 2:
Input
5 3
10 1 10 1 10Output30ExplanationSelect orders KaTeX can only parse string typed expression, KaTeX can only parse string typed expression, and KaTeX can only parse string typed expression. There are no adjacent selected pairs, so the override is not needed. The total reward is KaTeX can only parse string typed expression.
Example 3:
Input
3 3
4 5 6Output-1ExplanationSelecting all three orders creates two adjacent selected pairs: KaTeX can only parse string typed expression with KaTeX can only parse string typed expression, and KaTeX can only parse string typed expression with KaTeX can only parse string typed expression. Since only one adjacent pair is allowed, it is impossible.