cswork / OA打开练习工作台

IBMOA #51

Maximize Resource Allocation

估计中等可在线判题

考点(AI 标注)双指针滑窗哈希计数

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.

输入格式

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 one integer, the answer.

样例 1

输入:

6 3
1 2 3 7 3 5

输出:

15

样例 2

输入:

2 2
-5 -2

输出:

-7

样例 3

输入:

2 2
1 1

输出:

-1

样例说明

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