CF memsql Start[c]UP 2.0 B

B. Distributed Join

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Piegirl was asked to implement two table join operation for distributed database system, minimizing the network traffic.

Suppose she wants to join two tables, A and B. Each of them has certain number of rows which are distributed on different number of partitions. Table A is distributed on the first cluster consisting of m partitions. Partition with index i has ai rows from A. Similarly, second cluster containing table B has n partitions, i-th one having bi rows from B.

In one network operation she can copy one row from any partition to any other partition. At the end, for each row from A and each row from B there should be a partition that has both rows. Determine the minimal number of network operations to achieve this.

Input

First line contains two integer numbers, m and n (1 ≤ m, n ≤ 105). Second line contains description of the first cluster with m space separated integers, ai (1 ≤ ai ≤ 109). Similarly, third line describes second cluster with n space separated integers, bi (1 ≤ bi ≤ 109).

Output

Print one integer — minimal number of copy operations.

Sample test(s)

input

2 2 
2 6 
3 100

output

11

input

2 3 
10 10 
1 1 1

output

6

Note

In the first example it makes sense to move all the rows to the second partition of the second cluster which is achieved in 2 + 6 + 3 = 11operations

In the second example Piegirl can copy each row from B to the both partitions of the first cluster which needs 2·3 = 6 copy operations.

简单贪心,题意不是很好理解。。

大致题意:

A有m个分支,每个分支有ai组,B有n个分支,每个分支有bi组,要将A与B合并(A的每个分支都包含B的全部,或B的每个分支都包含A的全部),使得总花费最小。

贪心策略:将a数组升序排列,b数组升序排列,求出a的和ma,b的和mb。一共就两种情况,要么将A合并到B,要要么将B合并到A,取两者的小值即可。如果是将B合并到A中,我们就将a数组遍历一下,a[i]>=mb就保留a[i],将mb合并到a[i]中,花费为mb,否则,将a[i]合并到A的其他组中,花费为a[i],注意一点如果是a的最后一项必须保留,哪怕是比mb小,也要保留下来,将mb合并进来,因为是将B合并到A,A中应该至少保留一项。

 #include<cstdio>

 #include<iostream>

 #include<cmath>

 #include<stdlib.h>

 #include<vector>

 #include<cstring>

 #include<map>

 #include<algorithm>

 #include<string.h>

 #define M(a,b) memset(a,b,sizeof(a))

 #define INF 0x3f3f3f3f

 using namespace std;

 int n,m;

 int a[],b[];

 int main()

 {

    while(scanf("%d%d",&n,&m)==)

    {

        for(int i = ;i<n;i++)

            scanf("%d",&a[i]);

        for(int i = ;i<m;i++)

            scanf("%d",&b[i]);

        sort(a,a+n);

        sort(b,b+m);

        long long sum = ;

        long long ans = ;

        if(a[n-]<b[m-])

         {

             for(int i = ;i<n;i++)

               sum+=a[i];

             for(int i = ;i<m-;i++)

             {

                 if(sum>b[i])

                 ans+=b[i];

                 else

                 ans+=sum;

             }

             ans+=sum;

         }

        else

        {

             for(int i = ;i<m;i++)

                 sum+=b[i];

             for(int i = ;i<n-;i++)

             {

                 if(sum>a[i])

                 ans+=a[i];

                 else

                 ans+=sum;

             }

             ans+=sum;

        }

         printf("%I64d\n",ans);

    }

    return ;

 }

CF memsql Start[c]UP 2.0 B的更多相关文章

  1. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

  2. 【CF MEMSQL 3.0 A. Declined Finalists】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  3. 【CF MEMSQL 3.0 E. Desk Disorder】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. 【CF MEMSQL 3.0 D. Third Month Insanity】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  5. 【CF MEMSQL 3.0 C. Pie Rules】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  6. 【CF MEMSQL 3.0 B. Lazy Security Guard】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  7. MemSQL Start[c]UP 2.0 - Round 1(无聊练手B题)

    http://codeforces.com/contest/452/problem/B   B. 4-point polyline time limit per test 2 seconds memo ...

  8. MemSQL Start[c]UP 2.0 - Round 2 - Online Round

    搞到凌晨4点一个没出,要gg了. A. Golden System http://codeforces.com/contest/458/problem/A #include<cstdio> ...

  9. MemSQL Start[c]UP 2.0 - Round 1

    A. Eevee http://codeforces.com/contest/452/problem/A 字符串水题 #include<cstdio> #include<cstrin ...

随机推荐

  1. android 学习中的一些问题记录 主要是概念问题

    一些问题记录 应用程序 res 目录常见的目录有哪些,分别放置什么类型的资源? animator/ 和anim/ 放的都是定义动画的XML文件,两个地方的动画类型不同. color/ XML文件:定义 ...

  2. WPF文章资源库

        MUHAMMAD SHUJAAT SIDDIQI

  3. java设计模式之状态模式

    状态模式 允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的类. 状态模式UML图 上下文环境(Context):它定义了客户程序需要的接口并维护一个具体状态角色的实例,将与状态相关 ...

  4. Serial Port Programming on Linux(转载)

    This is a tutorial on how to program the Serial Ports on your Linux box.Serial Ports are nice little ...

  5. hdu-5988 Coding Contest(费用流)

    题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Ot ...

  6. UVALive 4870 Roller Coaster --01背包

    题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F ,     D -= K 问在D小于等于一定限度的时 ...

  7. 原生态ajax

    用户名是否被注册过? 创建出注册信息: <h1>注册信息</h1> <input type="text" name="txtName&quo ...

  8. C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 区域管理功能增强(电子商务方向)

    由于公司是面向全国服务的.全国各地都有分公司,需要管理到覆盖全国的各种业务,各种业务系统信息系统的数据都需要规范化. 公司开展网络订单功能,在全国范围内实现网络下单.提高工作效率,提高各公司之间的数据 ...

  9. ssh远程执行目标机器上的命令

    一句话: ssh -t -p 端口号 用户名@远程机器IP '远程机器上的命令完整路径' 例如: ssh -t -p 22 yangjunming@dev '/opt/app/deploy.sh' 注 ...

  10. IIS部署SSL,.crt .key 的证书,怎么部署到IIS,记录一下,以免忘记。

    SSL连接作用不说,百度很多.因为最近想考虑重构一些功能,在登录这块有打算弄成HTTPS的,然后百度了,弄成了,就记录一下,以便以后万一部署的时候忘记掉. 做实验的时候,拿的我个人申请的已经备案的域名 ...