Earn or Unlock (Codeforces 1855D)
Tags: DP, bitmasks

Andrea is playing the game Tanto Cuore.  He has a deck of $$$n$$$ cards with values $$$a_1, \ldots, a_n$$$ from top to bottom. Each card can be either locked or unlocked. Initially, only the topmost card is unlocked. The game proceeds in turns. In each turn, Andrea chooses an unlocked card in the deck — the value written on the card is $$$v$$$ — and performs exactly one of the following two operations:   Unlock the first $$$v$$$ locked cards in the deck from the top. If there are less than $$$v$$$ locked cards in the deck, then unlock all the locked cards.  Earn $$$v$$$ victory points.  In both cases, after performing the operation, he removes the card from the deck.The game ends when all the cards remaining in the deck are locked, or there are no more cards in the deck.What is the maximum number of victory points Andrea can earn?

Input: The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of cards in the deck.The second line contains $$$n$$$ integers $$$a_1, \ldots, a_n$$$ ($$$0 \leq a_1, \ldots, a_n \leq n$$$) — the values of the cards in the deck.

Output: Output a single integer — the maximum number of victory points Andrea can earn.

Note: In the first sample test, the deck starts as [unlocked, locked]. Then, Andrea uses the first card to unlock the second card. Then he uses the second card to earn $$$2$$$ victory points.In the second sample test, Andrea can use the first card to unlock the second and the third card. Then he uses the second and the third card to earn $$$4+5=9$$$ victory points.In the third sample test, Andrea can't unlock any cards or gain any victory points with the first card.

Sample Inputs:
2
1 2

5
2 4 5 0 1

4
0 4 4 4

Sample Outputs:
2

9

0

Solution (Python):
from sys import stdin
input = lambda : stdin.readline().rstrip('\r\n')
n = int(input())
a = list(map(int, input().split()))
dp = 1
mask = (1 << 2 * n) - 1
for (i, x) in enumerate(a):
    dp |= dp >> i << x + i
    dp &= mask
ans = pre = 0
dp = bin(dp)[2:][::-1]
for (i, x) in enumerate(dp):
    if i < n:
        pre += a[i]
    if x == '1':
        ans = max(ans, pre - i)
print(ans)

Solution (Python):
from sys import stdin
input = lambda : stdin.readline().rstrip('\r\n')
n = int(input())
a = list(map(int, input().split()))
dp = 1
mask = (1 << 2 * n) - 1
for (i, x) in enumerate(a):
    dp |= dp >> i << x + i
    dp &= mask
ans = pre = 0
dp = bin(dp)[2:][::-1]
for (i, x) in enumerate(dp):
    if i < n:
        pre += a[i]
    if x == '1':
        ans = max(ans, pre - i)
print(ans)

Solution (Python):
from sys import stdin
input = lambda : stdin.readline().rstrip('\r\n')
n = int(input())
a = list(map(int, input().split()))
dp = 1
mask = (1 << 2 * n) - 1
for (i, x) in enumerate(a):
    dp |= dp >> i << x + i
    dp &= mask
ans = pre = 0
dp = bin(dp)[2:][::-1]
for (i, x) in enumerate(dp):
    if i < n:
        pre += a[i]
    if x == '1':
        ans = max(ans, pre - i)
print(ans)