values and an integer block. Pick a contiguous non-empty segment whose length is a multiple of block (that is, block, 2·block, 3·block and so on) and return the largest possible sum of its elements.
Elements can be negative, so the answer can be negative too.values=[3, -1, 4, -6, 2], block=2 → 3
Only even-length segments count. [3, -1, 4, -6] sums to 0 and [3, -1] to 2, while [-1, 4] gives 3, the best.
values=[-7, -2, -9], block=3 → -18
The length must be a multiple of 3, so the only choice is the whole array: -7 - 2 - 9 = -18.
values=[5, 1, -3, 8], block=1 → 11
With block = 1 any segment works; the whole array is best, with sum 11.
values=[3, -1, 4, -6, 2], block=2
3
Run checks the first 3 tests in your browser. Submit checks all 129 tests on the server, hidden ones included.