#P1877. Guitarist's Volume Optimization

    ID: 15160 Type: Default 1000ms 256MiB

Guitarist's Volume Optimization

Guitarist's Volume Optimization

A guitarist preparing for a performance wants to change his guitar's volume before each song. He has prepared a list of volume adjustments, and for each song, he can either increase or decrease the current volume by the specified adjustment. The volume is represented by an integer and must always be in the range \(0 \leq volume \leq maxLevel\).

You are given an initial volume beginLevel, the maximum volume maxLevel, and a sequence of n adjustments \(c_1, c_2, \dots, c_n\). For the i-th song, the guitarist changes the volume by \(c_i\) (either increasing or decreasing). Your task is to compute the maximum possible volume for the final song after applying all adjustments. If at some point no valid volume can be achieved, output -1.

inputFormat

The first line of input contains three integers: beginLevel, maxLevel, and n, where beginLevel is the initial volume, maxLevel is the maximum allowed volume, and n is the number of volume adjustments. The second line contains n space-separated integers \(c_1, c_2, \dots, c_n\), each representing the volume change before each song.

outputFormat

Output a single integer: the maximum achievable volume for the final song after applying all adjustments. If it is impossible to achieve any valid volume, output -1.

sample

5 10 3
5 3 7
10