/*
思路:
先计算完成所有任务的总时间,然后找到大于等于总时间的最近的可提交的时间,就是找到一个临界的可提交区间,右端点大于等于总时间,比较左端点与总时间,取较大值
BTW:
这题真是...,之前一直找不到为什么会WA,找了很久很久,都忍不住想退场就查数据,看看到底是那个数据那么坑,是不是数据很大很特殊,可为什么改为long long型还是WA,一番纠结...后来才发现,我代码的一处细节写错了,有一句代码是这样的:
else if (t[i].ifbefore(sumdo))
结果被我写成了 else if (t[i].ifbefore(n))
怪不得怪不得怪不得...现在觉得有毒的分明是我自己......
以及,以后一定要好好看题...多看几次,这题我多写了些不必要的代码,包括int型的降序排列的mycmp代码,以及struct的比较...结果,前者根本用不到,因为和做题的时长没有关系,这本来就不是贪心类的问题,而后面那个函数,如果有好好读题,看到那句,"It is guaranteed that the periods are not intersecting and are given in chronological order, so for every j >1 the condition lj >r(j-1) is met.",根本不用写那个函数...
我居然...硬生生把一道如此简单的题,做到这么多次WA...唉,现在十分后悔,当时为什么不重新敲一次呢...找不出bug的时候,重敲一次也好啊!
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int a[N];
struct time
{
int l, r;
int ifisin(int n)
{
if (n >= l && n <= r) return 1;
return 0;
}
int ifbefore(int n)
{
return (n < l)?1:0;
}
/* bool operator < (const time& b) const
{
return r < b.r ;
}*/
}t[N];
/*bool mycmp(int a, int b)
{
return a > b?1:0;
}*/
int main()
{
int n, m;
while (cin >> n)
{
int sum = 0, sumdo = 0, cnt = 0, done = 0;
for (int i = 0; i < n; i++)
cin >> a[i], sumdo += a[i];
cin >> m;
for (int i = 0; i < m; i++)
cin >> t[i].l >> t[i].r;
if (sumdo > t[m-1].r)
{
cout << -1 << endl; continue;
}
// sort(a,a+n,mycmp);
// cout << "test: " << endl; for (int i = 0; i < n; i++) cout << a[i] << endl;
// sort(t,t+m);
// cout << "test: " << endl; for (int i = 0; i < m; i++) cout << t[i].l << " " << t[i].r << endl;
int flag = 1;
for (int i = 0; i < m; i++)
{
if (t[i].ifisin(sumdo))
{
flag = 0;
cout << sumdo << endl;
break;
}
else if (t[i].ifbefore(sumdo))
{
flag = 0;
cout << t[i].l << endl;
break;
}
}
if (flag) cout << -1 << endl;
}
return 0;
}
/*
后来发现别人的思路比我简单太多了,于是又重新改一次自己的代码
思路来自:http://blog.csdn.net/m0_37729344/article/details/72897785
It is guaranteed that the periods are not intersecting and are given in chronological order
这句话很重要,意味着可以代码很精简
而且仔细理解题意以后,发现,很多数据都是根本没必要记录下来的,只要累加,或者只要判断是否在区间内即可
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int main()
{
int n, m, sum = 0, l, r, x;
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> x;
sum += x;
}
cin >> m;
for (int i = 0; i < m; i++)
{
cin >> l >> r;
if (r >= sum)
{
cout << max(l, sum) << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}