A. The Party and Sweets

69 篇文章 0 订阅
12 篇文章 1 订阅

A. The Party and Sweets

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

nn boys and mm girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from 11 to nn and all girls are numbered with integers from 11 to mm. For all 1≤i≤n1≤i≤n the minimal number of sweets, which ii-th boy presented to some girl is equal to bibi and for all 1≤j≤m1≤j≤m the maximal number of sweets, which jj-th girl received from some boy is equal to gjgj.

More formally, let ai,jai,j be the number of sweets which the ii-th boy give to the jj-th girl. Then bibi is equal exactly to the minimum among values ai,1,ai,2,…,ai,mai,1,ai,2,…,ai,m and gjgj is equal exactly to the maximum among values b1,j,b2,j,…,bn,jb1,j,b2,j,…,bn,j.

You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of ai,jai,j for all (i,j)(i,j) such that 1≤i≤n1≤i≤n and 1≤j≤m1≤j≤m. You are given the numbers b1,…,bnb1,…,bn and g1,…,gmg1,…,gm, determine this number.

Input

The first line contains two integers nn and mm, separated with space — the number of boys and girls, respectively (2≤n,m≤1000002≤n,m≤100000). The second line contains nn integers b1,…,bnb1,…,bn, separated by spaces — bibi is equal to the minimal number of sweets, which ii-th boy presented to some girl (0≤bi≤1080≤bi≤108). The third line contains mm integers g1,…,gmg1,…,gm, separated by spaces — gjgj is equal to the maximal number of sweets, which jj-th girl received from some boy (0≤gj≤1080≤gj≤108).

Output

If the described situation is impossible, print −1−1. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.

Examples

input

Copy

3 2
1 2 1
3 4

output

Copy

12

input

Copy

2 2
0 1
1 0

output

Copy

-1

input

Copy

2 3
1 0
1 1 2

output

Copy

4

Note

In the first test, the minimal total number of sweets, which boys could have presented is equal to 1212. This can be possible, for example, if the first boy presented 11 and 44 sweets, the second boy presented 33 and 22 sweets and the third boy presented 11 and 11 sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 1212.

In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.

In the third test, the minimal total number of sweets, which boys could have presented is equal to 44. This can be possible, for example, if the first boy presented 11, 11, 22 sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to 44.

=========================================================================

每个男生都要给每个一个,并且最小是b[i],那我们可以先把给每个女生的都算成b[i]

这样就满足了基本条件,女生这边的条件是最大值,首先a的最小值,要是小于b的最大值,那么就一定不可以了,因为b中这个最大值难免分配给每一个女生,会超过一个女生的最小值的。

然后就考虑b[n]<=a[1]的情况,我们可以在b[n]的基础上,添加一些值来达到ai的效果,也就是a[i]-b[n],但值得注意的是,一旦我们a[1]不等于b[n],我们这样做就把b[n]的值修改成了a[1],也就是说b[n]是至少出现一次的,这种情况我们还必须让b[n]出现一次,还必须让分配在a[1]上的b等于a1,易知b[n-1]已经分配了m个了,少一个也没事,我们把分配在a1上的b[n-1]变成a1即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int a[100000+10],b[100000+10];

int main()
{
    ll sum=0;

    int n,m;

    cin>>n>>m;

    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }


    for(int j=1;j<=m;j++)
    {
        cin>>b[j];
    }


    sort(a+1,a+1+n);
    sort(b+1,b+1+m);

    if(b[1]<a[n])
    {
        cout<<-1;
        return 0;
    }
    else
    {

        for(int i=1;i<n;i++)
        {
            sum+=a[i];
        }

        sum*=m;


        for(int i=1;i<=m;i++)
        {
            sum+=b[i];

        }


        if(a[n]!=b[1])
        {
            sum+=a[n];

            sum-=a[n-1];
        }

        cout<<sum<<endl;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值