CF813A The Contest

Description

Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place! This contest consists of $ n $ problems, and Pasha solves $ i $ th problem in $ a_{i} $ time units (his solutions are always correct). At any moment of time he can be thinking about a solution to only one of the problems (that is, he cannot be solving two problems at the same time). The time Pasha spends to send his solutions is negligible. Pasha can send any number of solutions at the same moment. Unfortunately, there are too many participants, and the website is not always working. Pasha received the information that the website will be working only during $ m $ time periods, $ j $ th period is represented by its starting moment $ l_{j} $ and ending moment $ r_{j} $ . Of course, Pasha can send his solution only when the website is working. In other words, Pasha can send his solution at some moment $ T $ iff there exists a period $ x $ such that $ l_{x}

Input Format

The first line contains one integer $ n (1

Output Format

If Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted. Otherwise print "-1" (without brackets).

Explanation/Hint

In the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again. In the second example Pasha invents the solution only after the website stops working for the last time. In the third example Pasha sends the solution exactly at the end of the first period.