C. Fruits

Problem - C - Codeforces

The spring is coming and it means that a lot of fruits appear on the counters. One sunny day little boy Valera decided to go shopping. He made a list of m fruits he wanted to buy. If Valera want to buy more than one fruit of some kind, he includes it into the list several times.

When he came to the fruit stall of Ashot, he saw that the seller hadn't distributed price tags to the goods, but put all price tags on the counter. Later Ashot will attach every price tag to some kind of fruits, and Valera will be able to count the total price of all fruits from his list. But Valera wants to know now what can be the smallest total price (in case of the most «lucky» for him distribution of price tags) and the largest total price (in case of the most «unlucky» for him distribution of price tags).

Input

The first line of the input contains two integer number n and m (1 ≤ n, m ≤ 100) — the number of price tags (which is equal to the number of different kinds of fruits that Ashot sells) and the number of items in Valera's list. The second line contains n space-separated positive integer numbers. Each of them doesn't exceed 100 and stands for the price of one fruit of some kind. The following m lines contain names of the fruits from the list. Each name is a non-empty string of small Latin letters which length doesn't exceed 32. It is guaranteed that the number of distinct fruits from the list is less of equal to n. Also it is known that the seller has in stock all fruits that Valera wants to buy.

Output

Print two numbers a and b (a ≤ b) — the minimum and the maximum possible sum which Valera may need to buy all fruits from his list.

Examples

input

Copy

5 3
4 2 1 10 5
apple
orange
mango

output

Copy

7 19

input

Copy

6 5
3 5 1 6 8 1
peach
grapefruit
banana
orange
orange

output

Copy

11 30

题意:给出n个数和m个字符串

字符串可以重复,而且重复的字符串视为一种字符串,同种字符串所代表的价值相同

求把n个数分配给m种字符串得到的值的和最小价值和最大价值分别是多少(一个数只能分配一次)

1<=n,m<=100

// LUOGU_RID: 90251021
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<deque>
#include<cmath>
#include<string.h>
using namespace std;
// ctrl+shift+C 注释
//ctrl+shift+x 取消
#define int long long
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef pair<int,int> PII;
const int N=2e5+10;
const ll M=1e18+10;
const int mod=1e9+7;
int a[N],sum[N];
priority_queue<int,vector<int>,greater<int> >pq;//小到大
priority_queue<int,vector<int>,greater<int> >pp;
set<int>se;
map<string,int>mp;
queue<int>qu;
vector<int>v;
deque<int>de;
int n,m;
string s[N];
void solve()
{
    cin>>n>>m;
    for(int i=0;i<n;i++)cin>>a[i];
    sort(a,a+n);
    for(int i=0;i<m;i++)
    {
        cin>>s[i];
        mp[s[i]]++;
    }
    for(int i=0;i<m;i++)
    {
        if(mp[s[i]]!=-1)
        {
            pq.push(mp[s[i]]);
            pp.push(mp[s[i]]);
            mp[s[i]]=-1;
        }
    }
    //min
    int minn=0;
    for(int i=mp.size()-1;i>=0;i--)一定是最大的和最少的一对,所以求数组的最后几个数
    {
        minn+=a[i]*pq.top();
        pq.pop();
    }
    //max
    int maxx=0;
    for(int i=n-mp.size();i<=n-1;i++)//一定是最大的和最多的一对,所以求数组的最后几个数
    {
         maxx+=a[i]*pp.top();
        pp.pop();
    }
    cout<<minn<<" "<<maxx<<endl;
}
signed main()
{
   int t=1;
   //cin>>t;
   while(t--)
   {
       solve();
   }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值