赛前训练3.A

Valera wanted to prepare a Codesecrof round. He's already got one problem and he wants to set a time limit (TL) on it.

Valera has written n correct solutions. For each correct solution, he knows its running time (in seconds). Valera has also wrote m wrong solutions and for each wrong solution he knows its running time (in seconds).

Let's suppose that Valera will set v seconds TL in the problem. Then we can say that a solution passes the system testing if its running time is at most v seconds. We can also say that a solution passes the system testing with some "extra" time if for its running time, a seconds, an inequality 2a ≤ v holds.

As a result, Valera decided to set v seconds TL, that the following conditions are met:

  1. v is a positive integer;
  2. all correct solutions pass the system testing;
  3. at least one correct solution passes the system testing with some "extra" time;
  4. all wrong solutions do not pass the system testing;
  5. value v is minimum among all TLs, for which points 1, 2, 3, 4 hold.

Help Valera and find the most suitable TL or else state that such TL doesn't exist.

Input

The first line contains two integers n, m (1 ≤ n, m ≤ 100). The second line contains n space-separated positive integers a1, a2, ..., an (1 ≤ ai ≤ 100) — the running time of each of the n correct solutions in seconds. The third line contains m space-separated positive integers b1, b2, ..., bm (1 ≤ bi ≤ 100) — the running time of each of m wrong solutions in seconds.

Output

If there is a valid TL value, print it. Otherwise, print -1.

Sample Input

Input
3 6
4 5 2
8 9 6 10 7 11
Output
5
Input
3 1
3 4 5
6
Output

-1

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n,m,f,t,p,x,i;
    while(cin>>n>>m)
    {
        f=100;t=1;p=100;
        for(i=0;i<n;i++)
        {
            cin>>x;
            if(f>x) f=x;
            if(t<x) t=x;//f是正确方案最小的运行时间,t是正确方案的最大的运行时间
        }
        for(i=0;i<m;i++)
        {
            cin>>x;
            if(p>x) p=x;//p是错误方案最小的运行时间
        }
        if(p<=t)
        cout<<"-1"<<endl;
        else
        if(2*f>=p)
        cout<<"-1"<<endl;
        else
        {
            t=max(t,f*2);
           cout<<t<<endl;
        }
    }
}
题解:
一开始想错了,选的时间v(符合的条件中的最小的),不一定是正确方案运行的最大时间,因为还要保证正确方案运行时间里至少有一个运行时间的二倍小于等于v;
至少有一个,那么正确方案最小的运行时间一定要满足2a<=v;
p<f,输出-1
p<t,输出-1
可以合并小于最小的一定小于最大的
2*f>=p
输出-1
否则输出值


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值