cswork / OAOpen practice workspace

IBMOA #51

Maximize Resource Allocation

Estimated MediumJudge-ready

Topics (AI-labelled)Two PointersHashing

Choose a contiguous subarray of exactly k items in the array whose elements are pairwise distinct, maximizing the sum of its elements. Return -1 if there is no valid subarray. A valid maximum sum must be returned as its true value even if it is less than -1.

Input

The first line contains n k, and the second line contains the array. On this site 1≤n,k≤200000 (k>n is allowed), −10^9≤element≤10^9.

Output

Output one integer, the answer.

Example 1

Input:

6 3
1 2 3 7 3 5

Output:

15

Example 2

Input:

2 2
-5 -2

Output:

-7

Example 3

Input:

2 2
1 1

Output:

-1

Example notes

The examples give 15, -7 and -1 respectively.

Editorial

Loading solution…