Shubham's Dilemma

2.9

18 votes
Problem

Shubham is a guy who has been into the jungles all his life. He loves to be at the top of the trees and enjoy seeing the scenic beauty from the top. One day Shubham's girlfriend(assume he has one :p) visited him in the jungles. Now since she can't climb trees as good as him she asked him to bring a ladder and help her reach the top.

Now Shubham doesn't want to disappoint his gf so he went to Karan to ask for help. Karan is a shrewd guy. He gave him some numbers and told him use these numbers and you will reach the top. But Karan doesn't tell him how high can he reach. Also since he is shrewd, there has to be a catch.

He tells Shubham that he has to use all the numbers given to him in the order they have been provided. Also with each number he can either go down or go up that many steps on the ladder.

Your task now is to help Shubham help his gf reach the top (He might reward you with some valuable points if you do it correctly). Remember Shubham can't leave the ladder at any point of time except for when he hasn't yet started. If a case arises when he can't make a move output "-1" (without quotes).

Input: The first line contains 2 space separated integers n(1<=n<=10000) and m(1<=m<=100); length of the ladder and the count of the numbers Karan gives respectively; The second line contains m non-negative space separated integers(Lie between 1 and 500 both inclusive); numbers given by Karan

Output: A single line of output giving the maximum possible height on the ladder which his gf can reach or "-1" if no way possible. Note: You start at the 0th step of the ladder i.e. you aint at the ladder right now. It is not necessary that he reaches the top.

Time Limit: 1
Memory Limit: 256
Source Limit:
Editor Image

?