Folding the Card Strip

hard games dp prefix-sums sliding-window queue 1000 ms · 256 MB

Folding the Card Strip

Alice and Bob found a long paper strip divided into n cards, each printed with an integer (some of them negative). They play a folding game: starting from the left end, they take turns folding the next few cards onto the growing folded stack, and whoever makes a fold scores the total value of everything in the stack at that moment. The game is over once the whole strip has been folded.

Formally, the cards are a_1, a_2, …, a_n. The state of the game is a number c, the count of cards already folded; initially c = 0. Let m = n − c be the number of cards still unfolded. A move chooses an integer k and sets c to c + k, where

  • if m ≥ L, then L ≤ k ≤ min(R, m);
  • if m < L, then k = m (the last few cards are folded all at once).

The player making the move scores a_1 + a_2 + … + a_c, using the new value of c. The players alternate, Alice moves first, and the game ends when c = n.

Both players play optimally: each of them wants to maximize their own total score minus the other player's total score. Find Alice's total score minus Bob's total score.

Input

The first line contains three integers n, L and R (1 ≤ n ≤ 2·10^5, 1 ≤ L ≤ R ≤ n).

The second line contains n integers a_1, a_2, …, a_n (−10^9 ≤ a_i ≤ 10^9).

Output

Print a single integer: Alice's total score minus Bob's total score when both play optimally.

Notes

In the first example L = 1 and R = 2. Alice folds two cards and scores 3 + (−1) = 2. Bob folds one more card and scores 3 − 1 + 4 = 6. Alice folds the last two cards and scores the whole strip, 10. The difference is 2 + 10 − 6 = 6, and no strategy of Alice's does better against Bob's best replies.

In the second example Alice must fold two or three cards. If she folds two, she scores −3 and Bob folds the remaining two for 5. If she folds three, she scores −2, only one card is left, which is fewer than L = 2, so Bob folds it and scores 5. The second option is better for Alice, giving −2 − 5 = −7.

The answer may not fit in a 32-bit integer.

Example 1

Input

5 1 2
3 -1 4 -1 5

Output

6

Example 2

Input

4 2 3
2 -5 1 7

Output

-7

Hints · reveal one at a time if you're stuck

Hint 1

A fold that leaves c cards folded always scores the sum of the first c cards, so prefix sums describe every score. Define the value of a position as the score difference the player to move can secure from it. How does that value depend only on how many cards are already folded?

Hint 2

Work from the end of the strip backwards. From a position with i cards folded, the mover picks a new fold point j within the allowed range, gains the prefix sum at j, and then the opponent is the mover at j. Handle the forced final move when fewer than L cards remain.

Hint 3

As i decreases, the range of allowed fold points slides one step left, so a naive scan of each range is too slow when R − L is large. A monotonic deque gives each window's maximum in O(n) overall. Remember that prefix sums can exceed 32 bits.

report an issue Prefer your own editor? sooks prep 16 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.