/*
测试:
输入:
n m
价格1 价格2...价格n-1 价格n
商品1
商品2
...
商品m
输出:
最低价格和 最高价格和
测试用例:
input:
5 3
4 2 1 10 5
apple
orange
lemo
5 4
1 2 3 4 5
apple
orange
orange
apple
output:
7 19
6 18
*/
#include<iostream>
#include<stdio.h>#include<map>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
typedef pair<string, int> PAIR;
bool cmp_by_value(const PAIR& lhs, const PAIR& rhs) {
return lhs.second > rhs.second;
}
void Bubble_Sort(int *num, int n)
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; i + j < n - 1; j++)
{
if (num[j] > num[j + 1])
{
int temp = num[j];
num[j] = num[j + 1];
num[j + 1] = temp;
}
}
}
return;
}
int main()
{
int n;
int m;
int sum_min = 0;
int sum_max = 0;
int num[1000];
vector<pair<int,int>>out;
string str;
while (scanf("%d %d", &n, &m) != -1)//结束条件:输入Ctrl+z两次
//while (cin>>n>>m) 不能连续输入
{
for (int i = 0; i < n; i++)
scanf("%d",&num[i]);
//cin >> num[i];
Bubble_Sort(num, n);
map<string, int>s;
for (int j = 0; j < m; j++)
{
cin >> str;
if (s.find(str) != s.end())
s[str] = s[str] + 1;
else
s[str] = 1;
}
vector<PAIR> shangpin(s.begin(), s.end());
sort(shangpin.begin(), shangpin.end(), cmp_by_value);
int i = 0;
for (map<string, int>::iterator it = s.begin(); it != s.end(); it++)
{
sum_min = sum_min + (*it).second*num[i];
sum_max = sum_max + (*it).second*num[n - i - 1];
i++;
}
out.push_back(pair<int,int>(sum_min,sum_max));
sum_min = 0;
sum_max = 0;
}
for (vector<pair<int, int>>::iterator it = out.begin(); it != out.end();it++)
cout << (*it).first<<" "<<(*it).second<<endl;
//system("pause");
return 0;
}