A11039 | The Contest
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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}<=T<=r_{x}$ .
Pasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems.
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}<=T<=r_{x}$ .
Pasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems.
输入格式
The first line contains one integer $n (1<=n<=1000)$ — the number of problems. The second line contains $n$ integers $a_{i} (1<=a_{i}<=10^{5})$ — the time Pasha needs to solve $i$ th problem.
The third line contains one integer $m (0<=m<=1000)$ — the number of periods of time when the website is working. Next $m$ lines represent these periods. $j$ th line contains two numbers $l_{j}$ and $r_{j} (1<=l_{j}<r_{j}<=10^{5})$ — the starting and the ending moment of $j$ th period.
It is guaranteed that the periods are not intersecting and are given in chronological order, so for every $j>1$ the condition $l_{j}>r_{j-1}$ is met.
The third line contains one integer $m (0<=m<=1000)$ — the number of periods of time when the website is working. Next $m$ lines represent these periods. $j$ th line contains two numbers $l_{j}$ and $r_{j} (1<=l_{j}<r_{j}<=10^{5})$ — the starting and the ending moment of $j$ th period.
It is guaranteed that the periods are not intersecting and are given in chronological order, so for every $j>1$ the condition $l_{j}>r_{j-1}$ is met.
输出格式
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).
Otherwise print "-1" (without brackets).
输入输出样例
输入 #1
2 3 4 2 1 4 7 9
输出 #1
7
输入 #2
1 5 1 1 4
输出 #2
-1
输入 #3
1 5 1 1 5
输出 #3
5
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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted