zoukankan      html  css  js  c++  java
  • CF w1d1 C. The Party and Sweets

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

    More formally, let ai,j be the number of sweets which the i-th boy give to the j-th girl. Then bi is equal exactly to the minimum among values ai,1,ai,2,…,ai,m and gj is equal exactly to the maximum among values b1,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,j for all (i,j) such that 1≤i≤n and 1≤j≤m. You are given the numbers b1,…,bn and g1,…,gm, determine this number.

    Input

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

    Output

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

    Examples
    input
    3 2
    1 2 1
    3 4
    output
    12

    input
    2 2
    0 1
    1 0
    output
    -1

    input
    2 3
    1 0
    1 1 2
    output
    4

    #include<iostream>
    #include<algorithm>
    #define ll long long
    using namespace std;
    ll n,m,b[100005],g[100005],tmp,tot;
    ll cmp(ll x,ll y)
    {
    	return x>y;
    }
    int main()
    {
    	cin>>n>>m;
    	for(int i=1;i<=n;i++){
    		cin>>b[i];
    		tot+=b[i]*m; 
    	}
    	for(int i=1;i<=m;i++)cin>>g[i];
    	sort(b+1,b+1+n,cmp);
    	sort(g+1,g+1+m,cmp);//大到小排序 
    	if(b[1]>g[m]){
    		cout<<-1;
    		return 0;
    	}
    	if(b[1]==g[m]){//男生中能发最多的等于女生中收的最少的 
    		for(int i=1;i<=m;i++)tot+=g[i]-b[1];//只能由他把剩下的补上 
    	}
    	if(b[1]<g[m]){
    		tot+=g[m]-b[2];
    		for(int i=1;i<m;i++)tot+=g[i]-b[1];
    	} 
    	cout<<tot;
    	return 0;
    }
    
  • 相关阅读:
    遍历数组的常用方法
    ios 提审被拒4.3,更换账号提审处理
    js 判断手机有没有网络
    js网页拉起支付宝支付
    uni-app常用 HTML5+APP 设置
    uni-app 无痛刷新 token 方法
    uni-app通过判断接口403跳转登录页面的问题
    APICloud项目纪要
    Vue递归组件实现层层嵌套显示数据
    Git恢复删除的分支
  • 原文地址:https://www.cnblogs.com/LiangYC1021/p/12657189.html
Copyright © 2011-2022 走看看