Firefighters and grannies (2)

The firefighters of a distant country want to protect the grannies inside nn schools. All the schools are in a row on a street, numbered in order from 1 to nn. At each school jj there are iji_j grannies. The firefighters can form gg groups, and each group can only go to a single school. If a group goes to school jj, it protects all the grannies there. In addition, it also indirectly protects half the grannies in school j1j-1, assuming that it exists and that it is not already fully protected by another group; and similarly with school j+1j+1.

What is the maximum number of grannies that can be protected?

Input

Input consists of several cases, each one with gg and nn, followed by the iji_j’s. You can assume 1gn30001 \le g \le n \le 3000, and that all the iji_j’s are even natural numbers between 2 and 10510^5.

Output

For every case, print how many grannies can be protected.

Hint

The expected solution for this problem is a dynamic programming code with two mutual recurrences and cost O(gn)O(g \cdot n).

Problem information

Author: Unknown
Translator: Salvador Roura

Generation: 2026-01-25T11:26:56.374Z

© Jutge.org, 2006–2026.
https://jutge.org