Candies and MilestonesProblem code: MILESTN |
All submissions for this problem are available.
|
Little Pratya loves collecting candies and she also likes playing games :). Today Pratya is travelling in bus and she has a bag full of N candies. She looks outside the window and notices milestones with numbers written on them. She decides to play a game. She wants to select some initial non-zero number of candies from the bag and call it her collection of candies. When she sees a milestone on the way, she will add or remove some candies from her collection as following. Let the previous milestone number be a1 and the current milestone number be a2. |
|
a2 > a1 : Pratya adds ( a2 - a1 ) candies to her collection (thus, her bag of candies loses them). If the bag doesn't contain at least ( a2 - a1 ) candies, she starts crying.
a2 < a1 : Pratya removes ( a1 - a2 ) candies from her collection (thus, her bag of candies gains them). If her collection doesn't contain more than ( a1 - a2 ) candies, she starts crying.
a1 = a2 : Pratya is bored to see the same number again and so she eats one of the candies from her collection.
Moreover, Pratya always wants her candy collection to have at least one candy, otherwise she starts crying. Given the numbers written on all the milestones in order, find the minimum number of candies Pratya should select in the beginning so that she doesn't have to cry at all. If it is not possible , print -1. Note that no change to the collection happens when she sees the first milestone.
Input
The first line contain the number of test cases T. Each test case has two lines. The first line specifies N and M. N is the number of candies in the bag and M is the total number of milestones. Second line contains M integers, the numbers written on the milestones, in the order Pratya sees them.
^ is used for power.
T <= 150
1 <= N <= 10^7
2 <= M <= 10^4
Each milestone number will be between [-10^6,10^6].
Output
For each test case output the minimum number of candies that should be selected from the bag or -1 if its not possible
Example
Input: 3 10 5 1 5 1 -1 -2 2 5 1 5 1 -1 -2 100 3 1 2 3 Output: 4 -1 1
* There are multiple test sets, and the judge shows the sum of the time taken over all test sets of your submission, if Accepted.
| Author: | rosyish |
| Date Added: | 12-02-2011 |
| Time Limit: | 1.5 sec |
| Source Limit: | 50000 Bytes |
| Languages: | ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC |
Comments
SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:
HELP
Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:
- Accepted
Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark. - Time Limit Exceeded
Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach. - Wrong Answer
Your program compiled and ran succesfully but the output did not match the expected output. - Runtime Error
Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section. - Compilation Error
Your code was unable to compile. When you see this icon, click on it for more information.
If you are still having problems, see a sample solution here.


Fetching successful submissions
