#P8392. Maximizing the Number of Items with Exact Weight Sum

    ID: 21569 Type: Default 1000ms 256MiB

Maximizing the Number of Items with Exact Weight Sum

Maximizing the Number of Items with Exact Weight Sum

You are given 2m+1 types of items. The weights of the items are \( -m, -m+1, \ldots, m-1, m \). For each weight \( i \), there are \( a_i \) items available.

Your task is to select some items such that the sum of their weights is exactly \( l \). Among all selections achieving the sum \( l \), you are asked to pick one with the maximum possible number of items. If there is no way to obtain a total weight of \( l \), output -1.

inputFormat

The first line contains two integers \( m \) and \( l \).

The second line contains \( 2m+1 \) integers \( a_{-m}, a_{-m+1}, \ldots, a_{m-1}, a_m \), where \( a_i \) denotes the number of items with weight \( i \). The items are listed in increasing order of weight.

outputFormat

Output a single integer representing the maximum number of items you can take such that the sum of the weights is exactly \( l \). If no such selection exists, output -1.

sample

1 1
1 1 1
2