values and an integer max_hop. Pick a non-empty set of elements so that any two neighboring picks (in array order) have indices at most max_hop apart. In other words, if indices i < j are picked and nothing between them is, then j - i ≤ max_hop.
Return the largest possible sum of the picked elements.values=[4, -7, 3, -1, -2, 6], max_hop=2 → 12
Take 4, 3, -1 and 6: the picked indices 0, 2, 3, 5 are never more than 2 apart. The sum is 12.
values=[-5, -2, -8], max_hop=3 → -2
Every number is negative and an empty pick is not allowed, so take just -2.
values=[9, -20, -20, 9], max_hop=2 → 9
values=[4, -7, 3, -1, -2, 6], max_hop=2
12
Run checks the first 3 tests in your browser. Submit checks all 117 tests on the server, hidden ones included.