This page is still under construction.

Parts of this page are still being built. What you see may change.

Cheap Airlines

Time limit1sMemory limit128 MB

Summary
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 nn 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 kk 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 kk 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 kk non-overlapping intervals of consecutive days, and taking no trip at all (a sum of 00) is allowed.

Input

The first line contains two integers nn and kk (1≤k≤n≤1,000,0001 \le k \le n \le 1{,}000{,}000). The second line contains nn integers, each with absolute value at most 10910^9, describing the recreation coefficients of the consecutive holiday days.

Output

Print a single integer: the sum of recreation coefficients in an optimal holiday plan.

Examples3

  1. Example 1

    Input
    5 2
    7 -3 4 -9 5
    
    Expected output
    13
    
  2. Example 2

    Input
    4 1
    1 2 3 4
    
    Expected output
    10
    
  3. Example 3

    Input
    3 2
    -1 -2 -3
    
    Expected output
    0