Grading
Time limit2sMemory limit128 MB
Given point values and a threshold K, find the smallest integer at least K that cannot be any total score over all correct/wrong answer patterns.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Number theory, Math, Greedy
- Solved
- No attempts yet
Problem
You are grading an answer sheet with questions. Question has point value for . Each question's score and the total score of the sheet are determined by the following rules.
- A wrong question scores .
- A correct question is scored as follows.
- If question is correct, its score is .
- For a correct question (): if question is also correct, its score is plus the score of question ; if question is wrong, its score is .
- The total score of the sheet is the sum of all per-question scores.
In other words, within a run of consecutively correct questions the point values accumulate from the start of the run.
For example, suppose an exam has 9 questions whose point values are given in Table 1.
Table 1
Suppose that on some answer sheet, whether questions 1 through 9 are correct (○) or wrong (×) is as in Table 2.
Table 2
Then the per-question scores are as in Table 3, so the total score of the sheet is 39.
Table 3
However, some integers can never appear as the total score. For example, when the point values are those of Table 1, no combination of correct and wrong answers can make the total equal .
Given the point values and a natural number , write a program that finds the smallest integer that is at least and cannot appear as the total score of the answer sheet.
Input
The first line contains the number of questions (). The second line contains the point values of the questions in order from question 1, separated by spaces; each value is an integer between and . The third line contains a natural number ().
Output
Print on the first line the smallest integer () that cannot appear as the total score of the answer sheet.