Pick cards from both ends
Cards with values lie in a row. Take exactly K cards, each time from the left end or the right end, with at most R cards taken from the right. Find the largest possible sum of the cards you take.
Reported in: Infosys (SP / DSE · 22 Nov 2026) (memory-based)
Input
First line: N K R. Second line: N card values.
Output
The largest sum.
Constraints
1 ≤ K ≤ N ≤ 10^5; 0 ≤ R ≤ K; −10^4 ≤ values ≤ 10^4.
Example 1
Input:
6 3 1
1 2 3 4 5 6
Output:
9Example 2
Input:
5 2 0
9 1 1 1 8
Output:
10Example 3
Input:
4 4 2
-1 5 -2 3
Output:
5Hint: If r cards come from the right, the other K − r come from the left. Try every r from 0 to R and use prefix sums to get each total in O(1).