i is worth values[i]. In one move you take one jar, either the leftmost or the rightmost of those still on the shelf. You must make exactly count moves.
Return the largest total value of the jars you take.values=[3, 9, 1, 1, 2, 8], count=3 → 20
Take the two leftmost jars (3 and 9) and the rightmost one (8): 20. Three from either end alone gives only 13 or 11.
values=[5, 1, 5], count=1 → 5
A single jar must come from an end, and both ends are worth 5.
values=[2, 7, 4], count=3 → 13
len(values) ≤ 10⁵values[i] ≤ 10⁴count ≤ len(values)values=[3, 9, 1, 1, 2, 8], count=3
20
Run checks the first 3 tests in your browser. Submit checks all 79 tests on the server, hidden ones included.