URAL 1788 On the Benefits of Umbrellas

来源:互联网 发布:淘宝卖家衣服轮播图 编辑:程序博客网 时间:2024/05/18 20:36

1788. On the Benefits of Umbrellas

Time limit: 1.0 second
Memory limit: 64 MB
A group of school leavers had their graduation party at an aquapark. They had a great time, but when they were leaving the aquapark they were surprised by a suddenly cold weather and a heavy rain, which made it quite a problem to get to the trolleybus stop.
It turned out that all the boys in the company had their umbrellas and all the girls were without umbrellas. Of course, each boy, being a real gentleman, volunteered to accompany one of the girls to the trolleybus stop under his umbrella.
If the ith girl gets wet under the rain, she'll get upset by gi units. If no girl accepts an invitation from the jth boy, he'll get upset by bj · k units, where k is the number of luckier boys who will accompany girls under their umbrellas. The girls who will go under umbrellas and the accompanying boys will not get upset at all.
Help the boys and girls keep their holiday mood as unspoiled as possible. Determine how they should proceed to make the total upset minimal.

Input

The first line contains the number of girls n and boys m in the group (1 ≤ nm ≤ 100). The second line contains the girls' upsets g1, …, gn separated with a space. The third line contains the boys' upset coefficients b1, …, bm separated with a space. The numbers in the second and third lines are positive integers not exceeding 1000.

Output

Output the minimal possible total upset.

Sample

inputoutput
2 41 10010 8 6 4
19
Problem Author: Sofia Tekhazheva, prepared by Denis Dublennykh 
Problem Source: Ural Regional School Programming Contest 2010


这是一道贪心题,注意贪心的方法即可;


#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int MAXN=110;bool cmp(const int &a,const int &b){    return a>b;}int G[MAXN],B[MAXN];int Gsum[MAXN],Bsum[MAXN];int main(){int n,m;scanf("%d%d",&n,&m);    for(int i=0;i<n;i++){        scanf("%d",&G[i]);    }    for(int i=0;i<m;i++){        scanf("%d",&B[i]);    }    memset(Gsum,0,sizeof(Gsum));    memset(Bsum,0,sizeof(Bsum));    sort(G,G+n,cmp);    sort(B,B+m,cmp);    for(int i=n-1;i>=0;--i) Gsum[i]=Gsum[i+1]+G[i];    for(int i=m-1;i>=0;--i) Bsum[i]=Bsum[i+1]+B[i];    int ans=0x3f3f3f3f;    int sum;    for(int i=0;i<=min(m,n);++i)    {        sum=Gsum[i]+Bsum[i]*i;        ans=min(ans,sum);    }cout<<ans<<endl;return 0;}


0 0
原创粉丝点击