This page is still under construction.

Parts of this page are still being built. What you see may change.

Grading

Time limit2sMemory limit128 MB

Summary
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 NN questions. Question ii has point value SiS_i for i=1,2,…,Ni = 1, 2, \dots, N. Each question's score and the total score of the sheet are determined by the following rules.

  1. A wrong question scores 00.
  2. A correct question is scored as follows.
    • If question 11 is correct, its score is S1S_1.
    • For a correct question ii (2≤i≤N2 \le i \le N): if question i−1i-1 is also correct, its score is SiS_i plus the score of question i−1i-1; if question i−1i-1 is wrong, its score is SiS_i.
  3. 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.

Question123456789
Points327268252

Table 1

Suppose that on some answer sheet, whether questions 1 through 9 are correct (○) or wrong (×) is as in Table 2.

Question123456789
Correct?○×○○○××○×

Table 2

Then the per-question scores are as in Table 3, so the total score of the sheet is 39.

Question123456789
Score3079150050

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 7373.

Given the point values and a natural number KK, write a program that finds the smallest integer MM that is at least KK and cannot appear as the total score of the answer sheet.

Input

The first line contains the number of questions NN (1≤N≤1501 \le N \le 150). The second line contains the point values of the NN questions in order from question 1, separated by spaces; each value is an integer between 11 and 100100. The third line contains a natural number KK (1≤K≤2,000,000,0001 \le K \le 2{,}000{,}000{,}000).

Output

Print on the first line the smallest integer MM (M≥KM \ge K) that cannot appear as the total score of the answer sheet.

Examples1

  1. Example 1

    Input
    9
    3 2 7 2 6 8 2 5 2
    72
    
    Expected output
    73