/* C program for Merge Sort */
#include<stdlib.h>
#include<stdio.h>

// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(long long int arr[], long long int l, long long int m, long long int r)
{
	long long int i, j, k;
	long long int n1 = m - l + 1;
	long long int n2 =  r - m;

	/* create temp arrays */
	long long int L[n1], R[n2];

	/* Copy data to temp arrays L[] and R[] */
	for (i = 0; i < n1; i++)
		L[i] = arr[l + i];
	for (j = 0; j < n2; j++)
		R[j] = arr[m + 1+ j];

	/* Merge the temp arrays back into arr[l..r]*/
	i = 0; // Initial index of first subarray
	j = 0; // Initial index of second subarray
	k = l; // Initial index of merged subarray
	while (i < n1 && j < n2)
	{
		if (L[i] <= R[j])
		{
			arr[k] = L[i];
			i++;
		}
		else
		{
			arr[k] = R[j];
			j++;
		}
		k++;
	}

	/* Copy the remaining elements of L[], if there
	   are any */
	while (i < n1)
	{
		arr[k] = L[i];
		i++;
		k++;
	}

	/* Copy the remaining elements of R[], if there
	   are any */
	while (j < n2)
	{
		arr[k] = R[j];
		j++;
		k++;
	}
}

/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
void mergeSort(long long int arr[], long long int l, long long int r)
{
	if (l < r)
	{
		// Same as (l+r)/2, but avoids overflow for
		// large l and h
		int m = l+(r-l)/2;

		// Sort first and second halves
		mergeSort(arr, l, m);
		mergeSort(arr, m+1, r);

		merge(arr, l, m, r);
	}
}

int main()
{
	long long int n,k;
	scanf("%lld %lld",&n,&k);
	long long int a[n],sum=0,coun=0;
	for(long long int i=0;i<n;i++)
	{
		scanf("%lld",&a[i]);
	}
	mergeSort(a,0,n-1);
	if(n<k)
	{
		for(long long int i=0;i<1000000;i++)
		{
			if(a[i]!=0)
			{
				printf("%lld\n",a[i]);
				sum+=a[i];
				a[i+1]=a[i+1]-sum;
				count++;
			}

			if(count==n)
				break;
		}
	}
	if(k<n)
	{
		for(long long int i=0;i<k;i++)
		{
			if(a[i]!=0)
			{
				printf("%lld\n",a[i]);
				sum+=a[i];
				a[i+1]=a[i+1]-sum;
				count++;
			}

			if(count==k)
				break;
		}
	}

	if(k>n)
	{
		for(long long int i=0;i<k-n;i++)
			printf("0\n");
	}
}


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e5;

int n, k;
int a[maxn + 5];
int sum;
bool flag;
int cnt;

int cmp(int x, int y){
	return x < y;
}

int main(){
	scanf("%d %d", &n, &k);
	for(int i = 1; i <= n; i++){
		scanf(" %d", &a[i]);
	}
	sort(a + 1, a + 1 + n, cmp);
	for(int i = 1; cnt < k; i++){
		if(a[i] - sum != 0){
			printf("%d\n", a[i] - sum);
			cnt++;
			sum = a[i];
		}
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e5;

int n, k;
int a[maxn + 5];
int sum;
bool flag;
int cnt;

int cmp(int x, int y){
	return x < y;
}

int main(){
	scanf("%d %d", &n, &k);
	for(int i = 1; i <= n; i++){
		scanf(" %d", &a[i]);
	}
	sort(a + 1, a + 1 + n, cmp);
	for(int i = 1; cnt < k; i++){
		if(i > n) {
			flag = 1;
			break;
		}
		if(a[i] - sum != 0){
			printf("%d\n", a[i] - sum);
			cnt++;
			sum = a[i];
		}
	}
	if(flag == 1) {
		for(int i = cnt; i < k; i++){
			printf("0\n");
		}
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<cstring>
#include<string>
using namespace std;

typedef long long ll;
const int maxn = 1e6;
const int inf = 0x3f3f3f3f;

int n,k;
int a[100005];

int main(){
	scanf("%d %d", &n, &k);
	for(int i = 1; i <= n; i++){
		scanf("%d", &a[i]);
	}
	sort(a + 1, a + 1 + n);
	int pos = 0;
	for(int i = 1; i <= k; i++){
		if(a[n] != 0 && a[i] != 0){
			printf("%d\n", a[i]);
			for(int j = i + 1; j <= n; j++){
				a[j] = a[j] - a[i];
			}
		}
		else printf("0\n");
	}
	return 0;
}

/*
1 2 3
1
0 1 2
1
0 0 1
*/


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int inf = 0x3f3f3f3f;
const int maxn = 1e6;
typedef long long ll;

int n, k;
int a[maxn];

int main(){
	scanf("%d %d", &n, &k);
	for(int i = 1; i <= n; i ++){
		scanf("%d", &a[i]);
	}
	sort(a + 1, a + 1 + n);
	for(int i = 1; i <= k; i ++){
		if(a[i] == 0) printf("0\n");
		else {
			printf("%d\n", a[i] - i + 1);
		}
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e5;

int n, k;
int a[maxn + 5];
int sum;
bool flag;
int cnt;

int cmp(int x, int y){
	return x < y;
}

int main(){
	scanf("%d %d", &n, &k);
	for(int i = 1; i <= n; i++){
		scanf(" %d", &a[i]);
	}
	sort(a + 1, a + 1 + n, cmp);
	for(int i = 1; cnt < k; i++){
		if(a[i] - sum == 0 && i == n){
			flag = 1;
			break;
		}
		if(i == n + 1){
			flag = 1;
			break;
		}
		if(a[i] - sum != 0){
			printf("%d\n", a[i] - sum);
			cnt++;
			sum = a[i];
		}
	}
	if(flag){
		for(int i = n + 1; i <= k; i++) printf("0\n");
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<cstring>
#include<string>
using namespace std;

typedef long long ll;
const int maxn = 1e6;
const int inf = 0x3f3f3f3f;

int n,k;
int a[100005];

int main(){
	scanf("%d %d", &n, &k);
	for(int i = 1; i <= n; i++){
		scanf("%d", &a[i]);
	}
	sort(a + 1, a + 1 + n);
	int pos = 0;
	for(int i = 1; i <= k; i++){
		if(a[n] != 0){
			if(a[i] == 0) {
				k++;
				continue;
			}
			printf("%d\n", a[i]);
			for(int j = i + 1; j <= n; j++){
				a[j] = a[j] - a[i];
			}
		}
		else printf("0\n");
	}
	return 0;
}

/*
1 2 3
1
0 1 2
1
0 0 1
*/


#include <bits/stdc++.h>
using namespace std;

int main ()
{
	int A;
	int B;
	long long num[100000];
	int size;
	int operation;
	long long subs = 0;

	cin >> size >> operation;

	for (A = 0; A < size; A++)
	{
		cin >> num[A];
	}
	sort(num, num+size);
	cout << num[0] << endl;
	subs+= num[0];

	for (A = 1, B = 1; A < size; A++)
	{
		while (num[A] - subs <= 0)
		{
			A++;
		}

		if (A < size) {
			while (num[A] - subs > 0)
			{
				num[A]-= subs;
				cout << num[A] << endl;
				subs+= num[A];
				B++;
			}
		}
		else {
			while (num[size-1] - subs > 0)
			{
				num[size-1]-= subs;
				cout << num[size-1] << endl;
				subs+= num[size-1];
				B++;
			}

			while (B < operation)
			{
				cout << 0 << endl;
				B++;
			}
			break;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;

int main ()
{
	int A;
	int B;
	long long num[100000];
	int size;
	int operation;
	long long subs = 0;

	cin >> size >> operation;

	for (A = 0; A < size; A++)
	{
		cin >> num[A];
	}
	sort(num, num+size);
	cout << num[0] << endl;
	subs+= num[0];

	for (A = 1, B = 1; A < operation; A++)
	{
		while (num[A] - subs <= 0)
		{
			A++;
		}

		if (A < size) {
			while (num[A] - subs > 0)
			{
				cout << num[A] - subs << endl;
				subs+= num[A] - subs;
				B++;
			}
		}
		else {
			while (num[size-1] - subs > 0)
			{
				cout << num[size-1] - subs << endl;
				subs+= num[size-1] - subs;
				B++;
			}

			while (B < operation)
			{
				cout << 0 << endl;
				B++;
			}
			break;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;

int main ()
{
	int A;
	int B;
	long long num[100000];
	int size;
	int operation;
	long long subs = 0;

	cin >> size >> operation;

	for (A = 0; A < size; A++)
	{
		cin >> num[A];
	}
	sort(num, num+size);
	cout << num[0] << endl;
	subs+= num[0];

	for (A = 1, B = 1; A < max(size, operation); A++)
	{
		while (num[A] - subs <= 0)
		{
			A++;
		}

		if (A < size) {
			while (num[A] - subs > 0)
			{
				cout << num[A] - subs << endl;
				subs+= num[A] - subs;
				B++;
			}
		}
		else {
			while (num[size-1] - subs > 0)
			{
				cout << num[size-1] - subs << endl;
				subs+= num[size-1] - subs;
				B++;
			}

			while (B < operation)
			{
				cout << 0 << endl;
				B++;
			}
			break;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;

int main ()
{
	int A;
	int B;
	long long num[100000];
	int size;
	int operation;
	long long subs = 0;

	cin >> size >> operation;

	for (A = 0; A < size; A++)
	{
		cin >> num[A];
	}
	sort(num, num+size);
	cout << num[0] << endl;
	subs+= num[0];

	for (A = 1; A < operation; A++)
	{
		B = A;
		while (num[A] - subs <= 0)
		{
			A++;
		}

		if (A < size) {
			while (num[A] - subs > 0)
			{
				cout << num[A] - subs << endl;
				subs+= num[A] - subs;
			}
		}
		else {
			while (B < operation)
			{
				cout << 0 << endl;
				B++;
			}
			break;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;

int main ()
{
	int A;
	long long num[100000];
	int size;
	int operation;
	long long subs = 0;

	cin >> size >> operation;

	for (A = 0; A < size; A++)
	{
		cin >> num[A];
	}
	sort(num, num+size);
	cout << num[0] << endl;
	subs+= num[0];

	for (A = 1; A < min(size, operation); A++)
	{
		while (num[A] - subs <= 0)
		{
			A++;
		}
		if (A < size) cout << num[A] - subs << endl;
		subs+= num[A] - subs;
	}

	while (A < operation)
	{
		cout << 0 << endl;
		A++;
	}
}


#include <bits/stdc++.h>
using namespace std;

int main ()
{
	int A;
	int B;
	long long num[100000];
	int size;
	int operation;
	long long subs = 0;

	cin >> size >> operation;

	for (A = 0; A < size; A++)
	{
		cin >> num[A];
	}
	sort(num, num+size);
	cout << num[0] << endl;
	subs+= num[0];

	for (A = 1, B = 1; A < operation; A++)
	{
		while (num[A] - subs <= 0)
		{
			A++;
		}

		if (A < size) {
			while (num[A] - subs > 0)
			{
				cout << num[A] - subs << endl;
				subs+= num[A] - subs;
				B++;
			}
		}
		else {
			while (B < operation)
			{
				cout << 0 << endl;
				B++;
			}
			break;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;

int main ()
{
	int A;
	long long num[100000];
	int size;
	int operation;
	long long subs = 0;

	cin >> size >> operation;

	for (A = 0; A < size; A++)
	{
		cin >> num[A];
	}
	sort(num, num+size);
	cout << num[0] << endl;
	subs+= num[0];

	for (A = 1; A < operation; A++)
	{
		if (A >= size) cout << 0 << endl;
		while (num[A] - subs <= 0)
		{
			A++;
		}
		if (A < size) cout << num[A] - subs << endl;
		else cout << 0 << endl;
		subs+= num[A] - subs;
	}

}


#include <bits/stdc++.h>
using namespace std;

int main ()
{
	int A = 0;
	int B = 0;
	long long num[100000];
	int size;
	int operation;
	long long subs = 0;

	cin >> size >> operation;

	for (A = 0; A < size; A++)
	{
		cin >> num[A];
	}
	sort(num, num+size);

	for (A = 0, B = 0; A < max(size, operation); A++)
	{
		if (num[A] - subs <= 0) continue;

		if (A < size) {
			while (num[A] - subs > 0 && B < operation)
			{
				num[A]-= subs;
				cout << num[A] << endl;
				subs+= num[A];
				B++;
			}

			if (B > operation) break;
		}
		else break;
	}

	while (B < operation)
	{
		cout << 0 << endl;
		B++;
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <random>
#include <climits>
#include <iomanip>
#include <queue>
#include <bitset>
#include <cctype>
#include <deque>

using namespace std;

#define forn(i,n ) for (int i = 0; i<int(n); i++)

typedef vector<long > vi;
typedef vector<vector<int>> vvi;
typedef long long ll;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int n = 0;
	int k = 0;

	int min = 0;
	int delta = 0;

	vi x;
	vi t;

	cin >> n >> k;
	x.assign(n, 0);

	forn(i, n)
	{
		cin >> x[i];
	}

	sort(x.begin(), x.end());
	cout << x[0] << endl;
	delta = x[0];
	x[0] = 0;
	k--;

	int i = 1;

	if (k > n)
	{
		n--;
		while (n > 0)
		{
			x[i] = x[i] - delta;
			if (x[i] != 0)
			{
				cout << x[i] << endl;
				delta += x[i];
				k--;
			}
			n--;
			i++;
		}
	}
	else
	{
		while (k > 0)
		{
			x[i] = x[i] - delta;
			if (x[i] != 0)
			{
				cout << x[i] << endl;
				delta += x[i];
				k--;
			}
			i++;
		}
	}

	if (k > n)
		for (int i = 0; i < k - n; i++)
			cout << 0 << endl;

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <random>
#include <climits>
#include <iomanip>
#include <queue>
#include <bitset>
#include <cctype>
#include <deque>

using namespace std;

#define forn(i,n ) for (int i = 0; i<int(n); i++)

typedef vector<long long> vi;
typedef vector<vector<int>> vvi;
typedef long long ll;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int n = 0;
	int k = 0;

	int min = 0;
	int delta = 0;

	vi x;
	vi t;

	cin >> n >> k;
	x.assign(n, 0);

	forn(i, n)
	{
		cin >> x[i];
	}

	sort(x.begin(), x.end());
	cout << x[0] << endl;
	delta = x[0];
	x[0] = 0;
	k--;

	int i = 1;

	if (k >= n - 1)
	{
		for (int i = 1; i < n; i++)
		{
			x[i] = x[i] - delta;
			if (x[i] != 0)
			{
				cout << x[i] << endl;
				delta += x[i];
				k--;
			}
		}
		for (int i = 0; i < k; i++)
		{
			cout << 0 << endl;
		}
	}
	else
	{
		while (k > 0)
		{
			x[i] = x[i] - delta;
			if (x[i] != 0)
			{
				cout << x[i] << endl;
				delta += x[i];
				k--;
			}
			i++;
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <random>
#include <climits>
#include <iomanip>
#include <queue>
#include <bitset>
#include <cctype>
#include <deque>

using namespace std;

#define forn(i,n ) for (int i = 0; i<int(n); i++)

typedef vector<long long> vi;
typedef vector<vector<int>> vvi;
typedef long long ll;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int n = 0;
	int k = 0;

	int min = 0;
	int delta = 0;

	vi x;
	vi t;

	cin >> n >> k;
	x.assign(n, 0);

	forn(i, n)
	{
		cin >> x[i];
	}

	sort(x.begin(), x.end());
	cout << x[0] << endl;
	delta = x[0];
	x[0] = 0;
	k--;

	int i = 1;

	if (k > n - 1)
	{
		for (int i = 1; i < n; i++)
		{
			x[i] = x[i] - delta;
			if (x[i] != 0)
			{
				cout << x[i] << endl;
				delta += x[i];
				k--;
			}
		}
		for (int i = 0; i < k; i++)
		{
			cout << 0 << endl;
		}
	}
	else
	{
		while (k > 0)
		{
			x[i] = x[i] - delta;
			if (x[i] != 0)
			{
				cout << x[i] << endl;
				delta += x[i];
				k--;
			}
			i++;
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <random>
#include <climits>
#include <iomanip>
#include <queue>
#include <bitset>
#include <cctype>
#include <deque>

using namespace std;

#define forn(i,n ) for (int i = 0; i<int(n); i++)

typedef vector<long long> vi;
typedef vector<vector<int>> vvi;
typedef long long ll;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int n = 0;
	int k = 0;

	int min = 0;
	int delta = 0;

	vi x;
	vi t;

	cin >> n >> k;
	x.assign(n, 0);

	forn(i, n)
	{
		cin >> x[i];
	}

	sort(x.begin(), x.end());
	cout << x[0] << endl;
	delta = x[0];
	x[0] = 0;
	k--;

	int i = 1;

	if (k > n)
	{
		n--;
		while (n > 0)
		{
			x[i] = x[i] - delta;
			if (x[i] != 0)
			{
				cout << x[i] << endl;
				delta += x[i];
				k--;
			}
			n--;
			i++;
		}
	}
	else
	{
		while (k > 0)
		{
			x[i] = x[i] - delta;
			if (x[i] != 0)
			{
				cout << x[i] << endl;
				delta += x[i];
				k--;
			}
			i++;
		}
	}

	if (k > n)
		for (int i = 0; i < k - n; i++)
			cout << 0 << endl;

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <random>
#include <climits>
#include <iomanip>
#include <queue>
#include <bitset>
#include <cctype>
#include <deque>

using namespace std;

#define forn(i,n ) for (int i = 0; i<int(n); i++)

typedef vector<long long> vi;
typedef vector<vector<int>> vvi;
typedef long long ll;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int n = 0;
	int k = 0;

	int min = 0;
	int delta = 0;

	vi x;
	vi t;

	cin >> n >> k;
	x.assign(n, 0);

	forn(i, n)
	{
		cin >> x[i];
	}

	sort(x.begin(), x.end());
	cout << x[0] << endl;
	delta = x[0];
	x[0] = 0;
	k--;
	n--;

	int i = 1;

	if (k > n)
	{
		n--;
		while (n > 0)
		{
			x[i] = x[i] - delta;
			if (x[i] != 0)
			{
				cout << x[i] << endl;
				delta += x[i];
				k--;
			}
			n--;
			i++;
		}
		k = k - n;
		for (int i = 0; i < k; i++)
		{
			cout << 0 << endl;
			k--;
		}
	}
	else
	{
		while (k > 0)
		{
			x[i] = x[i] - delta;
			if (x[i] != 0)
			{
				cout << x[i] << endl;
				delta += x[i];
				k--;
			}
			i++;
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <random>
#include <climits>
#include <iomanip>
#include <queue>
#include <bitset>
#include <cctype>
#include <deque>

using namespace std;

#define forn(i,n ) for (int i = 0; i<int(n); i++)

typedef vector<long long> vi;
typedef vector<vector<int>> vvi;
typedef long long ll;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int n = 0;
	int k = 0;

	int min = 0;
	int delta = 0;

	vi x;
	vi t;

	cin >> n >> k;
	x.assign(n, 0);

	forn(i, n)
	{
		cin >> x[i];
	}

	sort(x.begin(), x.end());
	cout << x[0] << endl;
	delta = x[0];
	x[0] = 0;
	k--;
	n--;

	int i = 1;

	if (k > n)
	{
		n--;
		while (n > 0)
		{
			x[i] = x[i] - delta;
			if (x[i] != 0)
			{
				cout << x[i] << endl;
				delta += x[i];
				k--;
			}
			n--;
			i++;
		}
		k = k - n;
		for (int i = 0; i < k; i++)
		{
			cout << 0 << endl;
		}
	}
	else
	{
		while (k > 0)
		{
			x[i] = x[i] - delta;
			if (x[i] != 0)
			{
				cout << x[i] << endl;
				delta += x[i];
				k--;
			}
			i++;
		}
	}
}


#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main(){
    int n,k,mn,f,arr[100005];
    scanf("%d%d",&n,&k);
    mn = 1e9 + 9;
    for(int i = 0;i < n;i++){
        scanf("%d",&arr[i]);
    }
    sort(arr,arr + n);
    for(int i = 0;i < k;i++){
        if(arr[n - 1] == 0){
            printf("0\n");
        }
        else{
            f = 0;
            for(int j = 0;j < n;j++){
//                cout << j << endl;
                if(f == 0 && arr[j] != 0){
                    printf("%d\n",arr[j]);
                    mn = arr[j];
                    f = 1;
                }
                if(arr[j] > 0)
                    arr[j] -= mn;
//                cout << "@@" << arr[j] << "##" << mn << endl;
            }
        }
//        for(int j = 0;j < n;j++){
//            printf("%d ",arr[j]);
//        }
//        printf("\n");
    }
    return 0;
}


#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main(){
    int n,k,mx,sum,f,j,arr[100005];
    scanf("%d%d",&n,&k);
    for(int i = 0;i < n;i++){
        scanf("%d",&arr[i]);
    }
    sort(arr,arr + n);
    sum = 0,mx = 1e9 + 7;
    for(int i = 0;i < k;i++){
//        if(arr[n - 1] - sum <= 0){
//            printf("0\n");
//        }
//        else{
        j = i;
        while(j < n && arr[j] - sum <= 0){
                j++;
        }
        if(j >= n){
            printf("0\n");
        }
        else{
            printf("%d\n",arr[j] - sum);
            sum += (arr[j] - sum);
        }
//        }
    }
    return 0;
}


#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main(){
    int n,k,mx,sum,f,j,arr[100005];
    scanf("%d%d",&n,&k);
    for(int i = 0;i < n;i++){
        scanf("%d",&arr[i]);
    }
    sort(arr,arr + n);
    sum = 0,mx = 1e9 + 7;
    for(int i = 0;i < k;i++){
        if(arr[n - 1] - sum <= 0){
            printf("0\n");
        }
        else{
            j = i;
            while(arr[j] - sum <= 0){
                j++;
            }
            printf("%d\n",arr[j] - sum);
            mx = min(mx,arr[j] - sum);
            sum += mx;
        }
    }
    return 0;
}


#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main(){
    int n,k,mx,sum,f,j,arr[100005];
    scanf("%d%d",&n,&k);
    for(int i = 0;i < n;i++){
        scanf("%d",&arr[i]);
    }
    sort(arr,arr + n);
    sum = 0,mx = 1e9 + 7;
    for(int i = 0;i < k;i++){
        if(arr[n - 1] - sum <= 0){
            printf("0\n");
        }
        else{
            j = i;
            while(j < n && arr[j] - sum <= 0){
                j++;
            }
            if(j >= n){
                printf("0\n");
            }
            else{
                printf("%d\n",arr[j] - sum);
                sum += (arr[j] - sum);
            }
        }
    }
    return 0;
}


#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main(){
    int n,k,mx,f,arr[100005];
    scanf("%d%d",&n,&k);
    for(int i = 0;i < n;i++){
        scanf("%d",&arr[i]);
    }
    sort(arr,arr + n);
    for(int i = 0;i < k;i++){
        if(arr[n - 1] == 0){
            printf("0\n");
            continue;
        }
        f = 0;
        for(int j = 0;j < n;j++){
            if(f == 0 && arr[j] != 0){
                f = 1;
                mx = arr[j];
                printf("%d\n",arr[j]);
            }
            if(arr[j] - mx >= 0){
                arr[j] -= mx;
            }
            else{
                arr[j] = 0;
            }
        }
    }
    return 0;
}


#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main(){
    int n,k,sum,f,j,arr[100005];
    scanf("%d%d",&n,&k);
    for(int i = 0;i < n;i++){
        scanf("%d",&arr[i]);
    }
    sort(arr,arr + n);
    sum = 0;
    for(int i = 0;i < k;i++){
        for(j = 0;j < n;j++){
            if(arr[j] - sum > 0){
                arr[j] -= sum;
                printf("%d\n",arr[j]);
                sum += arr[j];
                break;
            }
        }
        if(j >= n){
            printf("0\n");
        }
    }
    return 0;
}


#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

int main()
{
    int n, k;
    deque<int64_t> vec;
    int64_t a;
    cin >> n >> k;
    for (int i = 0; i < n; ++i){
        cin >> a;
        if (a > 0){
            vec.push_back(a);
        }
    }
    sort(vec.begin(), vec.end());
//    for (auto a:vec){
//        cout << a <<" ";
//    }
//    cout << endl;
    int64_t min = 1000000001;
    for (int i = 0; i < k; ++i){
//        for (int j = 0; j < vec.size(); ++j){
            bool flag = false;
            int n = vec.size();
            for(int j = 0; j < n && !flag; ++j){
                if (vec.front() < min && vec.front() > 0){
                min = vec.front();
               // cout << min << "!" << endl;
                flag = true;
               // j = vec.size();
                } else if (vec.front() == 0 || vec.front() < 0 ){
                vec.pop_front();
               // cout << vec.front() << "front" << endl;
                }
        }
        if (min !=  1000000001){
            cout << min << endl;
        } else cout << 0 << endl;
        for (int j = 0; j < vec.size(); ++j){
            vec[j] -= min;
           // ++it;
        }
//        for (auto a:vec){
//            cout << a <<" ";
//        }
//        cout << endl;

        min = 1000000001;
    }
    return 0;
}


#include <iostream>
#include <queue>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
    int n, k;
    deque<int64_t> vec;
    int64_t a;
    cin >> n >> k;
    for (int i = 0; i < n; ++i){
        cin >> a;
        if (a > 0){
            vec.push_back(a);
        }
    }
    sort(vec.begin(), vec.end());
//    for (auto a:vec){
//        cout << a <<" ";
//    }
//    cout << endl;
    int64_t min = 1000000001;
    for (int i = 0; i < k; ++i){
//        for (int j = 0; j < vec.size(); ++j){
         //   bool flag = false;
          //  int n = vec.size();
            if (vec.size() > 0){
                min = vec.front();
            }
//            for(int j = 0; j < n && !flag; ++j){
//                if (vec.front() == 0 || vec.front() < 0 ){
//                vec.pop_front();
//                } else if (vec.front() < min ){
//                min = vec.front();
//                flag = true;
//                }
        if (min !=  1000000001){
            cout << min << endl;
        } else cout << 0 << endl;
        while(vec.front() - min <= 0 && vec.size() != 0){
            vec.pop_front();
        }
        for (int j = 0; j < vec.size(); ++j){
            vec[j] -= min;
        }
//        int m = vec.size();
//        int x = 3;
//        for (int j = 0; j < m; ++j){
//            if (vec.front() - min < 0 || vec.front() - min == 0 ){
//                vec.pop_front();
//           } else {
//               vec[x] -= min;
//               ++x;
//           }
//        }
//        for (auto a:vec){
//            cout << a <<" ^";
//        }
//        cout << endl;

        min = 1000000001;
    }
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n, k;
    vector<int64_t> vec;
    int64_t a;
    cin >> n >> k;
    for (int i = 0; i < n; ++i){
        cin >> a;
        if (a > 0){
            vec.push_back(a);
        }
    }
    int64_t min = 1000000001;
    for (int i = 0; i < k; ++i){
        for (auto it:vec){
            if (it < min && it > 0){
                min = it;
            }
        }
        if (min !=  1000000001){
            cout << min << endl;
        } else cout << 0 << endl;
        for (int j = 0; j < vec.size(); ++j){
            vec[j] -= min;
           // ++it;
        }
        min = 1000000001;
    }
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n, k;
    vector<int> vec;
    int a;
    cin >> n >> k;
    for (int i = 0; i < n; ++i){
        cin >> a;
        if (a > 0){
            vec.push_back(a);
        }
    }
    int64_t min = 1000000001;
    for (int i = 0; i < k; ++i){
        for (auto it:vec){
            if (it < min && it > 0){
                min = it;
            }
        }
        if (min !=  1000000001){
            cout << min << endl;
        } else cout << 0 << endl;
        for (int j = 0; j < vec.size(); ++j){
            vec[j] -= min;
           // ++it;
        }
        min = 1000000001;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int n, k;
    vector<int64_t> vec;
    int64_t a;
    cin >> n >> k;
    for (int i = 0; i < n; ++i){
        cin >> a;
        if (a > 0){
            vec.push_back(a);
        }
    }
    sort(vec.begin(), vec.end());
    int64_t min = 1000000001;
    for (int i = 0; i < k; ++i){
        for (int j = 0; j < vec.size(); ++j){
            if (vec[j] < min && vec[j] > 0){
                min = vec[j];
                j = vec.size();
            }
        }
        if (min !=  1000000001){
            cout << min << endl;
        } else cout << 0 << endl;
        for (int j = 0; j < vec.size(); ++j){
            vec[j] -= min;
           // ++it;
        }
        min = 1000000001;
    }
    return 0;
}


#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

int main()
{
    int n, k;
    deque<int64_t> vec;
    int64_t a;
    cin >> n >> k;
    for (int i = 0; i < n; ++i){
        cin >> a;
        if (a > 0){
            vec.push_back(a);
        }
    }
    sort(vec.begin(), vec.end());
//    for (auto a:vec){
//        cout << a <<" ";
//    }
//    cout << endl;
    int64_t min = 1000000001;
    for (int i = 0; i < k; ++i){
//        for (int j = 0; j < vec.size(); ++j){
            bool flag = false;
            int n = vec.size();
            for(int j = 0; j < n && !flag; ++j){
                if (vec.front() == 0 || vec.front() < 0 ){
                vec.pop_front();
                } else if (vec.front() < min ){
                min = vec.front();
                flag = true;
                }
        }
        if (min !=  1000000001){
            cout << min << endl;
        } else cout << 0 << endl;
        for (int j = 0; j < vec.size(); ++j){
            vec[j] -= min;
           // ++it;
        }
//        for (auto a:vec){
//            cout << a <<" ";
//        }
//        cout << endl;

        min = 1000000001;
    }
    return 0;
}


#include <iostream>
#include <queue>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
    int n, k;
    deque<int64_t> vec;
    int64_t a;
    cin >> n >> k;
    for (int i = 0; i < n; ++i){
        cin >> a;
        if (a > 0){
            vec.push_back(a);
        }
    }
    sort(vec.begin(), vec.end());
//    for (auto a:vec){
//        cout << a <<" ";
//    }
//    cout << endl;
    int64_t min = 1000000001;
    for (int i = 0; i < k; ++i){

        if (i < n){

            if (vec.size() > 0){
                min = vec.front();
            }

        if (min !=  1000000001){
            cout << min << endl;
        } else cout << "0" << endl;
        while(vec.front() - min <= 0 && vec.size() != 0){
            vec.pop_front();
        }
        for (int j = 0; j < vec.size(); ++j){
            vec[j] -= min;
        }

//        for (auto a:vec){
//            cout << a <<" ^";
//        }
//        cout << endl;

        min = 1000000001;
        } else cout << "0" << endl;
    }
    return 0;
}


#include <iostream>
#include <queue>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
    int n, k;
    deque<int64_t> vec;
    int64_t a;
    cin >> n >> k;
    for (int i = 0; i < n; ++i){
        cin >> a;
        if (a > 0){
            vec.push_back(a);
        }
    }
    sort(vec.begin(), vec.end());
//    for (auto a:vec){
//        cout << a <<" ";
//    }
//    cout << endl;
    int64_t min;
  int64_t sum = 0;
    for (int i = 0; i < k; ++i){

        if (i < n){

            if (vec.size() > 0){
                min = vec.front()- sum;
            } else min = 0;
            cout << min << endl;
//        if (min !=  1000000001){
//            cout << min << endl;
//        } else cout << "0" << endl;
        sum += min;
        while(vec.front() - sum <= 0 && vec.size() != 0){
            vec.pop_front();
        }

//        for (int j = 0; j < vec.size(); ++j){
//            vec[j] -= min;
//        }

//        for (auto a:vec){
//            cout << a <<" ^";
//        }
//        cout << endl;

    //    min = 1000000001;
        } else cout << "0" << endl;
    }
    return 0;
}


#include<stdio.h>
int a[100005],b[100005];
void merging(int low,int mid,int high){
	int l1,l2,i;
	for(l1=low,l2=mid+1,i=low;l1<=mid && l2<=high;i++){
		if(a[l1]<=a[l2])
			b[i]=a[l1++];
		else
			b[i]=a[l2++];

	}
	while(l1<=mid)
		b[i++]=a[l1++];
	while(l2<=high)
		b[i++]=a[l2++];
	for(i=low;i<=high;i++)
		a[i]=b[i];
}
void sort(int low,int high){
	int mid;
	if(low<high){
		mid=(low+high)/2;
		sort(low,mid);
		sort(mid+1,high);
		merging(low,mid,high);
	}
	else
		return;
}
int main(void){
	int n,k,i,j=0,p,l,r;
	scanf("%d %d",&n,&k);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	sort(0,n-1);
	for(p=0;p<k;p++){
		while(a[j]==0 && j<n)
                  j++;

		l=a[j];
		printf("%d\n",a[j]);

		for(r=j;r<n;r++){
                 a[r]=a[r]-l;
		//printf("%d",a[])
		}
		j++;
	}

	return 0;
}


#include<stdio.h>
long long int a[100005],b[100005],c[100005];
void merging(long long int low,long long int mid,long long int high){
	long long int l1,l2,i;
	for(l1=low,l2=mid+1,i=low;l1<=mid && l2<=high;i++){
		if(a[l1]<=a[l2])
			b[i]=a[l1++];
		else
			b[i]=a[l2++];

	}
	while(l1<=mid)
		b[i++]=a[l1++];
	while(l2<=high)
		b[i++]=a[l2++];
	for(i=low;i<=high;i++)
		a[i]=b[i];
}
void sort(long long int low,long long int high){
	long long int mid;
	if(low<high){
		mid=(low+high)/2;
		sort(low,mid);
		sort(mid+1,high);
		merging(low,mid,high);
	}
	else
		return;
}
int main(void){
	long long int n,k,i,j=0,p,l,r;
	scanf("%lld %lld",&n,&k);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	sort(0,n-1);

	for(p=0;p<k;p++){
		while(a[j]==0 && j<n)
                  j++;

		l=a[j];
		printf("%lld\n",a[j]);

		for(r=j;r<n;r++){
                 a[r]=a[r]-l;
		//printf("%d",a[])
		}
		j++;
	}

	return 0;
}


#include<stdio.h>
int a[100005],b[100005];
void merging(int low,int mid,int high){
	int l1,l2,i;
	for(l1=low,l2=mid+1,i=low;l1<=mid && l2<=high;i++){
		if(a[l1]<=a[l2])
			b[i]=a[l1++];
		else
			b[i]=a[l2++];

	}
	while(l1<=mid)
		b[i++]=a[l1++];
	while(l2<=high)
		b[i++]=a[l2++];
	for(i=low;i<=high;i++)
		a[i]=b[i];
}
void sort(int low,int high){
	int mid;
	if(low<high){
		mid=(low+high)/2;
		sort(low,mid);
		sort(mid+1,high);
		merging(low,mid,high);
	}
	else
		return;
}
int main(void){
	int n,k,i,j=0,p,l,r;
	scanf("%d %d",&n,&k);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	sort(0,n-1);
	for(p=0;p<k;p++){
		l=a[j];
		while(a[j]==0 && j<n)
                  j++;
		printf("%d\n",a[j]);

		for(r=j;r<n;r++){
                 a[r]=a[r]-l;
		//printf("%d",a[])
		}
		j++;
	}

	return 0;
}


#include<stdio.h>
int a[100005],b[100005],c[100005];
void merging(int low,int mid,int high){
	int l1,l2,i;
	for(l1=low,l2=mid+1,i=low;l1<=mid && l2<=high;i++){
		if(a[l1]<=a[l2])
			b[i]=a[l1++];
		else
			b[i]=a[l2++];

	}
	while(l1<=mid)
		b[i++]=a[l1++];
	while(l2<=high)
		b[i++]=a[l2++];
	for(i=low;i<=high;i++)
		a[i]=b[i];
}
void sort(int low,int high){
	int mid;
	if(low<high){
		mid=(low+high)/2;
		sort(low,mid);
		sort(mid+1,high);
		merging(low,mid,high);
	}
	else
		return;
}
int main(void){
	int n,k,i,j=0,p,l,r;
	scanf("%d %d",&n,&k);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	sort(0,n-1);

	for(p=0;p<k;p++){
		while(a[j]==0 && j<n)
                  j++;

		l=a[j];
		printf("%d\n",a[j]);

		for(r=j;r<n;r++){
                 a[r]=a[r]-l;
		//printf("%d",a[])
		}
		j++;
	}

	return 0;
}


#include<stdio.h>
long long int a[100005],b[100005],c[100005];
void merging(long long int low,long long int mid,long long int high){
	long long int l1,l2,i;
	for(l1=low,l2=mid+1,i=low;l1<=mid && l2<=high;i++){
		if(a[l1]<=a[l2])
			b[i]=a[l1++];
		else
			b[i]=a[l2++];

	}
	while(l1<=mid)
		b[i++]=a[l1++];
	while(l2<=high)
		b[i++]=a[l2++];
	for(i=low;i<=high;i++)
		a[i]=b[i];
}
void sort(long long int low,long long int high){
	long long int mid;
	if(low<high){
		mid=(low+high)/2;
		sort(low,mid);
		sort(mid+1,high);
		merging(low,mid,high);
	}
	else
		return;
}
int main(void){
	long long int n,k,i,j=0,p,l,r,len=1,temp;
	scanf("%lld %lld",&n,&k);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	sort(0,n-1);
	//for(i=0;i<n;i++)
	//	printf("%lld",a[i]);
	temp=a[0];
c[0]=a[0];
	for(i=1;i<n;i++){
        if(a[i]!=temp){
	c[len]=a[i];
	len++;
	temp=a[i];}

	}
	//for(p=0;p<len;p++)
         //printf("%lld\n",c[p]);

	for(p=0;p<k;p++){
		//while(a[j]==0 && j<n)
                  //j++;

		l=a[j];
		printf("%lld\n",c[j]);

		for(r=j;r<len;r++){
                 c[r]=c[r]-l;
		//printf("%d",a[])
		}
		j++;
	}

	return 0;
}


#include<stdio.h>
long long int a[100005],b[100005],c[100005];
void merging(long long int low,long long int mid,long long int high){
	long long int l1,l2,i;
	for(l1=low,l2=mid+1,i=low;l1<=mid && l2<=high;i++){
		if(a[l1]<=a[l2])
			b[i]=a[l1++];
		else
			b[i]=a[l2++];

	}
	while(l1<=mid)
		b[i++]=a[l1++];
	while(l2<=high)
		b[i++]=a[l2++];
	for(i=low;i<=high;i++)
		a[i]=b[i];
}
void sort(long long int low,long long int high){
	long long int mid;
	if(low<high){
		mid=(low+high)/2;
		sort(low,mid);
		sort(mid+1,high);
		merging(low,mid,high);
	}
	else
		return;
}
int main(void){
	long long int n,k,i,j=0,p,l,r,len=1,temp;
	scanf("%lld %lld",&n,&k);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	sort(0,n-1);
	//for(i=0;i<n;i++)
	//	printf("%lld",a[i]);
	temp=a[0];
c[0]=a[0];
	for(i=1;i<n;i++){
        if(a[i]!=temp){
	c[len]=a[i];
	len++;
	temp=a[i];}

	}
	//for(p=0;p<len;p++)
        // printf("%lld\n",c[p]);

	for(p=0;p<k;p++){
		//while(a[j]==0 && j<n)
                  //j++;

		l=c[j];
		printf("%lld\n",c[j]);

		for(r=j;r<len;r++){
                 c[r]=c[r]-l;
		//printf("%d",a[])
		}
		j++;
	}

	return 0;
}


#include<stdio.h>
long long int a[100005],b[100005],c[100005];
void merging(long long int low,long long int mid,long long int high){
	long long int l1,l2,i;
	for(l1=low,l2=mid+1,i=low;l1<=mid && l2<=high;i++){
		if(a[l1]<=a[l2])
			b[i]=a[l1++];
		else
			b[i]=a[l2++];

	}
	while(l1<=mid)
		b[i++]=a[l1++];
	while(l2<=high)
		b[i++]=a[l2++];
	for(i=low;i<=high;i++)
		a[i]=b[i];
}
void sort(long long int low,long long int high){
	long long int mid;
	if(low<high){
		mid=(low+high)/2;
		sort(low,mid);
		sort(mid+1,high);
		merging(low,mid,high);
	}
	else
		return;
}
int main(void){
	long long int n,k,i,j=0,p,l,r,len=1,temp,temp1;
	scanf("%lld %lld",&n,&k);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	sort(0,n-1);
	//for(i=0;i<n;i++)
	//	printf("%lld",a[i]);
	temp=a[0];
	c[0]=a[0];
	for(i=1;i<n;i++){
		if(a[i]!=temp){
			c[len]=a[i];
			len++;
			temp=a[i];}

	}
//	printf("%lldlen",len);
//	for(p=0;p<len;p++)
//		printf("%lld\n",c[p]);
	temp=0;
	for(p=0;p<k;p++){
		if(p<len){
			printf("%lld\n",c[p]);
			temp+=c[p];
			c[p+1]-=temp;

		}
		else
			printf("0\n");
		j++;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    set <int> a;
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,k;
    cin>>n;
    cin>>k;
    for(int i=0; i<n; i++)
    {
        int y;
        cin>>y;
        if(y!=0) a.insert(y);
    }
    int del = 0;
    for(int i=0; i<k; i++)
        {
            if(a.size()==0) cout<<0<<endl;
            else
            {
                int bg = *a.begin();
                int print = bg - del;
                cout<<print<<endl;
                del+=print;
                a.erase(a.begin());
            }
        }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
void QS(int a[],int l, int r)
{
    int giua=a[(l+r)/2];
    int i=l, j=r;
    while(i<=j)
    {
        while(a[i]<giua)
            i++;
        while(a[j]>giua)
            j--;
        if(i<=j)
        {
            swap(a[i],a[j]);
            i++;
            j--;
        }
    }
    if(l<j)
        QS(a,l,j);
    if(i<r)
        QS(a,i,r);
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,m;
    cin>>n>>m;
    int a[n];
    for(int i=0; i<n; i++)
        cin>>a[i];
    QS(a,0,n-1);
    int dem=1;
    int j=0;
    unsigned long long t=0;
    while(dem<=m)
    {
        if(a[j]==0)
        {
             a[j+1]-=t;
            j++;
        }
        else
        {
            dem++;
            cout<<a[j]<<" ";
            t+=a[j];
            a[j+1]-=t;
                j++;
        }
        if(j==n) break;
    }
    while (dem<=m)
    {
        cout<<0<<" ";
        dem++;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,k;
    cin>>n;
    cin>>k;
    int *a = new int [n];
    for(int i=0; i<n; i++)
        cin>>a[i];
    for(int i=0; i<k; i++)
    {
        int f = 0;
        for(int y=0; y<n; y++)
            if(a[y]!=0)
                f=1;
        if(f==0)
            cout<<0<<endl;
        else
        {
            int x = 0;
            while(a[x]==0)
                x++;
            int minn = a[x];
            for(int j=x+1; j<n; j++)
                if(a[j]<minn&&a[j]!=0)
                    minn = a[j];
            cout<<minn<<endl;
            for(int j=x; j<n; j++)
            {
                if(a[j]!=0)
                    a[j]-=minn;
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
void qSort(int *a, int n, int left, int right)
{
    int i = left;
    int j = right;
    int mid = a[(i+j)/2];
    while(i<=j)
    {
        while(a[i]<mid) i++;
        while(a[j]>mid) j--;
        if(i<=j)
        {
            swap(a[i], a[j]);
            i++;
            j--;
        }
        if(i<right) qSort(a, n, i, right);
        if(left<j)  qSort(a, n, left, j );
    }
}
void finish(int *a, int n, int &x, int &zero)
{
    int minn = a[x];
    cout<<minn;
    for(int i=x; i<n; i++)
    {
        a[i]-=minn;
        if(a[i]==0)
        {
            x = i+1;
            zero++;
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,k;
    cin>>n;
    cin>>k;
    int *a = new int [n];
    int zero = 0;
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
        if(a[i]==0) zero++;
    }
    qSort(a, n, 0, n-1);
    int x = 0;
    while(a[x]==0) x++;
    for(int i=0; i<k; i++)
        {
            if(zero==n) cout<<0;
            else
            {
                finish(a, n, x, zero);
            }
            cout<<endl;
        }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
int finish(int *a, int n, int &start, int &zero)
{
    int minn = a[start];
    for(int j=start+1; j<n; j++)
        if(a[j]!=0) if(a[j]<minn) minn = a[j];
    for(int j=start; j<n; j++)
    {
        if(a[j]!=0) a[j]-=minn;
        if(a[j]==0) zero++;
        if(a[start]==0&&j-start==1) start=j;
    }
    return minn;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,k;
    cin>>n;
    cin>>k;
    int *a = new int [n];
    int zero = 0;
    int x = 0;
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
        if(a[i]==0) zero++;
    }
    for(int i=0; i<k; i++)
    {
        if(zero==n) cout<<0;
        else cout<<finish(a, n, x, zero);
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
void qSort(int *a, int n, int left, int right)
{
    int i = left;
    int j = right;
    int mid = a[(i+j)/2];
    while(i<=j)
    {
        while(a[i]<mid) i++;
        while(a[j]>mid) j--;
        if(i<=j)
        {
            swap(a[i], a[j]);
            i++;
            j--;
        }
        if(i<right) qSort(a, n, i, right);
        if(left<j)  qSort(a, n, left, j );
    }
}
void finish(int *a, int n, int &x)
{
    cout<<a[x];
    int minn = a[x];
    for(int i=x; i<n; i++)
    {
        a[i]-=minn;
        if(a[i]==0) x = i+1;
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,k;
    cin>>n;
    cin>>k;
    int *a = new int [n];
    int zero = 0;
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
        if(a[i]==0) zero++;
    }
    qSort(a, n, 0, n-1);
    int x = 0;
    while(a[x]==0) x++;
    for(int i=0; i<k; i++)
        {
            if(zero==n) cout<<0;
            else
            {
                finish(a, n, x);
            }
            cout<<endl;
        }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
void qSort(int *a, int n, int left, int right)
{
    int i = left;
    int j = right;
    int mid = a[(i+j)/2];
    while(i<=j)
    {
        while(a[i]<mid) i++;
        while(a[j]>mid) j--;
        if(i<=j)
        {
            swap(a[i], a[j]);
            i++;
            j--;
        }
        if(i<right) qSort(a, n, i, right);
        if(left<j)  qSort(a, n, left, j );
    }
}
void finish(int *a, int n, int &x)
{
    int minn = a[x];
    cout<<minn<<endl;
    for(int i=x; i<n; i++)
    {
        a[i]-=minn;
        if(a[i]==0) x++;
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,k;
    cin>>n;
    cin>>k;
    int *a = new int [n];
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
    }
    qSort(a, n, 0, n-1);
    int x = 0;
    while(a[x]==0) x++;
    for(int i=0; i<k; i++)
        {
            if(x==n) cout<<0<<endl;
            else
            {
                finish(a, n, x);
            }
        }
    return 0;
}


#include <iostream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;

int main()
{
	int n, k;  cin >> n >> k;
	int* arr = new int[n];
	for (int i = 0; i < n; i++)
		cin >> arr[i];
	sort(arr, arr + n);
	int cur = 1;
	k--;
	cout << arr[0] << endl;
	while (k) {
		if (cur >= n)
		{
			cout << 0 << endl;
			k--;
				continue;
		}
		if (arr[cur] != arr[cur - 1])
		{
			cout << arr[cur] - arr[cur - 1] << endl;

			k--;
		}
		cur++;
	}

}


#include<iostream>
#include<algorithm>
using namespace std;

int a[100005];

int main()
{
	int n, k;
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}

	sort(a + 1, a + 1 + n);

	for (int i = 1, cnt = 1; cnt <= k;)
	{
		if (i > n)
		{
			cout << 0 << endl;
			i++;
			cnt++;
		}
		if (i <= 100000 && a[i] - a[i - 1])
		{
			cout << a[i] - a[i - 1] << endl;
			i++;
			cnt++;
		}
		else
			i++;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;

int a[100005];

int main()
{
	int n, k;
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}

	sort(a + 1, a + 1 + n);

	for (int i = 1, cnt = 1; cnt <= k;)
	{
		if (i > n)
		{
			cout << 0 << endl;
			i++;
			cnt++;
		}
		if (a[i] - a[i - 1])
		{
			cout << a[i] - a[i - 1] << endl;
			i++;
			cnt++;
		}
		else
			i++;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <numeric>
#include <string>
#include <functional>
#include <stack>
#include <chrono>
#include <ctime>
#include <cstdio>
#include<set>
#include<algorithm>
#include<cassert>
#include<iomanip>
#define fori(n) for(int i=0;i<n;i++)
#define forj(n) for (int j=0;j<n;j++)
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k;
    cin >> n >> k;
    vector<int>x(n);
    fori(n)cin >> x[i];
    sort(x.begin(), x.end());
    bool c = true;
    int temp = 0;
    int pos = 0;
    fori(k) {
        c = true;
        temp = 0;
        for (int j = pos; j < n; j++) {
            if (x[j] > 0 && c) {
                cout << x[j]<<"\n";
                temp = x[j];
                c = false;
            }
            x[j] -= temp;
            if (x[j] == 0) pos++;
        }
        if (c) {
            cout << 0 << " \n";
        }
    }
    return 0;

}


#include <iostream>
#include <vector>
#include <numeric>
#include <string>
#include <functional>
#include <stack>
#include <chrono>
#include <ctime>
#include <cstdio>
#include<set>
#include<algorithm>
#include<cassert>
#include<iomanip>
#define fori(n) for(int i=0;i<n;i++)
#define forj(n) for (int j=0;j<n;j++)
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k;
    cin >> n >> k;
    vector<int>x(n);
    fori(n)cin >> x[i];
    sort(x.begin(), x.end());
    bool c = true;
    int temp = 0;
    fori(k) {
        c = true;
        temp = 0;
        forj(n) {
            if (x[j] > 0 && c) {
                cout << x[j]<<"\n";
                temp = x[j];
                c = false;
            }
            x[j] -= temp;
        }
        if (c) {
            cout << 0 << " \n";
        }
    }
    return 0;

}


#include <iostream>
#include <vector>
#include <numeric>
#include <string>
#include <functional>
#include <stack>
#include <chrono>
#include <ctime>
#include <cstdio>
#include<set>
#include<algorithm>
#include<cassert>
#include<iomanip>
#define fori(n) for(int i=0;i<n;i++)
#define forj(n) for (int j=0;j<n;j++)
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k;
    cin >> n >> k;
    set<int>x;
    x.insert(0);
    fori(n) {
        int a;
        cin >> a;
        x.insert(a);
    }
    bool c = true;
    int temp = 0;
    int pos = 0;
    auto it = x.begin();
    fori(k) {
        if (next(it) == x.end()) {
            cout << "0\n";
        }
        else
        {
            cout << *next(it) - *it<<"\n";
            it = next(it);
        }

    }
    return 0;

}


#include <iostream>
#include <vector>
#include <numeric>
#include <string>
#include <functional>
#include <stack>
#include <chrono>
#include <ctime>
#include <cstdio>
#include<set>
#include<algorithm>
#include<cassert>
#include<iomanip>
#define fori(n) for(int i=0;i<n;i++)
#define forj(n) for (int j=0;j<n;j++)
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k;
    cin >> n >> k;
    set<int>x;
    fori(n) {
        int a;
        cin >> a;
        x.insert(a);
    }
    bool c = true;
    int temp = 0;
    int pos = 0;
    auto it = x.begin();
    fori(k) {
        if (next(it) == x.end()) {
            cout << "0\n";
        }
        else
        {
            cout << *next(it) - *it<<"\n";
            it = next(it);
        }

    }
    return 0;

}


//#include "C:\Users\Mohammad\Desktop\97c39f35dab0c45a5d924dd9e50c445f-47ecad34033f986b0972cdbf4636e22f838a1313/stdc++.h"
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;
//
//void getMin(int* arr, int l , int &max,int &min) {
//
//	max = -1;
//	min = 1000000000;
//	for (int j = 0; j < l; j++)
//	{
//		if (min > arr[j] && arr[j] != 0) {
//			min = arr[j];
//		}
//		if (max < arr[j]) {
//			max = arr[j];
//		}
//	}
//}
int main() {

	int n, k;
	cin >> n >> k;
	int *arr=new int[n];

	cin >> arr[0];

	for (int i = 1; i < n; i++)
	{
		cin >> arr[i];
	}

	sort(arr, arr + n);
	int count = 0;
	int min = arr[count];
	int rest = -1;

	for (int i = 0; i < k; i++)
	{

		cout << min << endl;

		for (int j = 0; j < n; j++)
		{
			if (arr[j] > 0) {
				arr[j] -= min;
			}
		}
		A:
		count++;
		if (count < n) {
			if (arr[count] != 0) {
				min = arr[count];
			}
			else {
				goto A;
			}
		}

		if (count == n) {
			rest = k-i;
			break;
		}
	}
	for (int i = 0; i < rest-1; i++)
	{
		cout << 0 << endl;
	}

	return 0;
}


//#include "C:\Users\Mohammad\Desktop\97c39f35dab0c45a5d924dd9e50c445f-47ecad34033f986b0972cdbf4636e22f838a1313/stdc++.h"
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;

int main() {

	int n, k;
	cin >> n >> k;
	int *arr=new int[n];

	cin >> arr[0];
	int min = arr[0];
	for (int i = 1; i < n; i++)
	{
		cin >> arr[i];
	}
	bool x=true;
	int rest = -1;
	int max = 0;
	for (int i = 0; i < k; i++)
	{
		x = true;
		for (int j = 0; j < n; j++)
		{
			if (min > arr[j] && arr[j]!=0) {
				min = arr[j];
			}
			if (max < arr[j]) {
				max = arr[j];
			}
		}
		if (min != 0)
		cout << min << endl;

		for (int j = 0; j < n; j++)
		{
			if (arr[j] != 0) {
				arr[j] -= min;
			}
		}

		for (int j = 0; j < n; j++)
		{
			if (arr[j] != 0)
				x = false;

		}
		if (x)
		{
			rest = i + 1;
			break;
		}
		min = max;
	}
	if (x)
	{
		for (int i = 0; i < k - rest; i++)
		{
			cout << 0 << endl;
		}
	}

	return 0;
}


//#include "C:\Users\Mohammad\Desktop\97c39f35dab0c45a5d924dd9e50c445f-47ecad34033f986b0972cdbf4636e22f838a1313/stdc++.h"
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;

void getMin(int* arr, int l , int &max,int &min) {

	max = -1;
	min = 1000000000;
	for (int j = 0; j < l; j++)
	{
		if (min > arr[j] && arr[j] != 0) {
			min = arr[j];
		}
		if (max < arr[j]) {
			max = arr[j];
		}
	}
}
int main() {

	int n, k;
	cin >> n >> k;
	int *arr=new int[n];

	cin >> arr[0];

	for (int i = 1; i < n; i++)
	{
		cin >> arr[i];
	}

	bool x = true;
	int rest = -1;
	int max;
	int min;

	for (int i = 0; i < k; i++)
	{
		x = true;

		getMin(arr, n, max, min);

		cout << min << endl;

		for (int j = 0; j < n; j++)
		{
			if (arr[j] > 0) {
				arr[j] -= min;
			}
		}

		for (int j = 0; j < n; j++)
		{
			if (arr[j] != 0)
			{
				x = false;
				min = max;
				continue;
			}
		}
		if (x)
		{
			rest = i + 1;
			break;
		}
	}

	if (x)
	{
		for (int i = 0; i < k - rest; i++)
		{
			cout << 0 << endl;
		}
	}

	return 0;
}


//#include "C:\Users\Mohammad\Desktop\97c39f35dab0c45a5d924dd9e50c445f-47ecad34033f986b0972cdbf4636e22f838a1313/stdc++.h"
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;
//
//void getMin(int* arr, int l , int &max,int &min) {
//
//	max = -1;
//	min = 1000000000;
//	for (int j = 0; j < l; j++)
//	{
//		if (min > arr[j] && arr[j] != 0) {
//			min = arr[j];
//		}
//		if (max < arr[j]) {
//			max = arr[j];
//		}
//	}
//}
int main() {

	int n, k;
	cin >> n >> k;
	int *arr=new int[n];

	cin >> arr[0];

	for (int i = 1; i < n; i++)
	{
		cin >> arr[i];
	}

	sort(arr, arr + n);

	int temp = 0;
	int count = 0;

	for (int i = 0; i < k; i++)
	{

		while (count != n - 1 && temp == arr[count])
			count++;

		cout << arr[count] - temp << endl;
		temp = arr[count];
	}

	return 0;
}


//#include "C:\Users\Mohammad\Desktop\97c39f35dab0c45a5d924dd9e50c445f-47ecad34033f986b0972cdbf4636e22f838a1313/stdc++.h"
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;

int main() {

	int n, k;
	cin >> n >> k;
	int *arr=new int[n];

	cin >> arr[0];
	int min = arr[0];
	for (int i = 1; i < n; i++)
	{
		cin >> arr[i];
	}
	bool x=true;
	int rest = -1;
	for (int i = 0; i < k; i++)
	{
		x = true;
		for (int j = 0; j < n; j++)
		{
			if (min > arr[j] && arr[j]!=0) {
				min = arr[j];
			}
		}
		if (min != 0)
		cout << min << endl;

		for (int j = 0; j < n; j++)
		{
			if (arr[j] != 0) {
				arr[j] -= min;
			}
		}

		for (int j = 0; j < n; j++)
		{
			if (arr[j] != 0)
				x = false;
		}
		if (x)
		{
			rest = i+1;
			break;
		}
	}
	if (x)
	{
		for (int i = 0; i < k - rest; i++)
		{
			cout << 0 << endl;
		}
	}

	return 0;
}


#include<bits/stdc++.h>
#include <iostream>
#include <numeric>
#define ll long long
#define ull unsigned long long
#define mpa make_pair
#define pb push_back
#define ff first
#define ss second
#define boost ios_base::sync_with_stdio(0)
#define ss second
#define forp(i,a,b) for(ll i=a;i<=b;i++)
#define rep(i,n)    for(ll i=0;i<n;++i)
#define ren(i,n)    for(ll i=n-1;i>=0;i--)
#define forn(i,a,b) for(ll i=a;i>=b;i--)
#define fre     freopen("input.txt","r",stdin),freopen("output.txt","w",stdout)
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define scan(x) scanf("%lld",&x)
using namespace std;
ll modpow(ll a,ll n,ll temp){ll res=1,y=a;while(n>0){if(n&1)res=(res*y)%temp;y=(y*y)%temp;n/=2;}return res%temp;}
//ll a[100005],n,dp[1000005]={0},b[1000005],c[100005],dp1[1000005],bit[100005];
//ll vis[100005];
//vector<ll> adj[100005];
#define pi 3.1415926536
#define sz(x) x.size()
ll ison(ll mask,ll pos){
	return (mask&(1<<pos));
}
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
ll cbit(ll n){ll k=0;while(n) n&=(n-1),k++;return k;}
ll nbit(ll n){ll k=0;while(n) n/=2,k++;return k;}
//ll b1[1001][1001],b2[1001][1001],g1[1001][1001],g2[1001][1001],w[1001][1001];
//bool a1[101][101],a2[102][101],a3[103][101];
ll mod=1e9+7;
int sgn(ll x) {
  return x < 0 ? -1 : !!x;
}
//pair<ll,ll> a[1000005];
/*void upd(ll ind,ll val){
    if(ind==0) return;
    while(ind<=n){
        bit[ind]+=val;
        ind+=(ind&-ind);
    }
}
ll qu(ll ind){
  ll k=0; while(ind>0){
    k+=bit[ind];
    ind-=(ind&-ind);
  }
    return k;
}*/

//ll tree[500005];

//vector<ll,pair<ll,ll> > b;
//vector<ll> adj[100005];

/*Tarjan incomplette
void dfs(ll s){
	vis[s]=1;
	for(ll i=0;i<adj[s].size();i++) if(!vis[adj[s][i]]) dfs(adj[s][i]);
    ofvs.pb(s);
}
void dfs2(ll s){
	vis[s]=1;
	if(wt[s]<optc){optn=0,optc=wt[s];}
	if(optc==wt[s]) optn++;
	for(ll i=0;i<rdj[s].size();i++) if(!vis[rdj[s][i]]) dfs2(rdj[s][i]);
}
*/
typedef pair<ll,pair<ll,ll> >ver;
//string s;
/*void build(ll node,ll ss,ll se)
{
	if(ss==se){
	tree[node]=a[ss];
	if(s[ss]=='{') o[node]++;
	else c[node]++;
    }
	else
	{
		int mid=(ss+se)/2;
		build(2*node,ss,mid);
		build(2*node+1,mid+1,se);
		ll t=min(o[2*node],c[2*node+1]);
		o[node]=o[2*node]+o[2*node+1]-t;
		c[node]=c[node*2]+c[node*2+1]-t;
		tree[node]=tree[node*2]+tree[node*2+1]+t;
	}
}
/*void upd(ll node,ll ss,ll se,ll idx,ll val)
{
	if(ss==se)
	a[idx]=val,tree[node]=a[idx];
	else
	{
		ll mid=(ss+se)/2;
		if(ss<=idx && idx<=mid)
		upd(2*node,ss,mid,idx,val);
		else
		upd(2*node+1,mid+1,se,idx,val);
	    tree[node]=tree[node*2]+tree[node*2+1];
	}
}
ver query(int node,int ss,int se,int l,int r)
{
	if(r<ss || se<l)
	return mpa(0,mpa(0,0));
	if(l<=ss && se<=r)
	return mpa(tree[node],mpa(o[node],c[node]));
	ll mid=(ss+se)/2;
	ver p1=query(2*node,ss,mid,l,r);
	ver p2=query(2*node+1,mid+1,se,l,r);
    ll t=min(p1.ss.ff,p2.ss.ss);
	o[node]=p1.ss.ff+p2.ss.ff-t;
	c[node]=p1.ss.ss+p2.ss.ss-t;
	return mpa(p1.ff+p2.ff+t,mpa(o[node],c[node]));
}
/*ll pow(ll n){
	ll t=1,i=1;
	while(i<=n) t*=10;
	return t;
}*/
//stack<ll> help;
//ll dp[1ll<<11];

//ll csum[100005],b[100005],help[100005];
//vector<ll> adj[1005];
//vector<ll> ans;
/*ll kmp[1000005];
ll zee[1000005];
void kmpm(string p){
	ll i,j,k=0,l;
	kmp[0]=0;
	j=0;
	for(i=1;i<p.size();i++){
		while(k && p[i]!=p[k]) k=kmp[k-1];
		if(p[i]==p[k]) k++;
		kmp[i]=k;
	}
}
ll isp(ll n){
	ll f=1,i;
	for(i=2;i<=sqrt(n);i++) if(!(n%i)) return 0;
	return 1;
}*/
/*void zee(string s){}*/
//map<ll,ll> help;
/*ll chk(ll val,ll n,ll m){
	ll t=0,i;
	val--;
	for(i=1;i<=n;i++) t+=min(m,val/i);
	return t;
}*/

/*ll size[100005u];
void init(ll n)
{for(int i=1;i<=n;i++) a[i]=i;}
ll root(ll x)
{
	while(a[x]!=x)
	{
		a[x]=a[a[x]];
		x=a[x];
	}
	return x;
}
ll u1(ll x,ll y)
{
	int p=root(x);
	int q=root(y);
	a[p]=a[q];
}
*/
/*ll tree[100005];
void update(ll idx, ll val){
	while(idx < 100005){
		tree[idx] = max(tree[idx], val);
		idx += (idx & (-idx));
	}
}
ll query(ll idx){
	ll ret = 0;
	while(idx > 0){
		ret = max(ret, tree[idx]);
		idx -= (idx & (-idx));
	}
	return ret;
}*/
//pair<char ,pair<ll,ll> >p[5005];[10004]
ll a[100005];
vector<ll> cp;
map<string,ll> help;
int main(){
   ll i,j,k,l,m,n;
   cin>>n>>m;
   rep(i,n) cin>>a[i];
   while(m--){
   	string b;
   	cin>>b;
   	help[b]++;
   }
   map<string,ll>::iterator it;
   for(it=help.begin();it!=help.end();it++) cp.pb(it->second);
   sort(a,a+n);
   sort(all(cp));
   reverse(all(cp));
   ll ans=0;
   j=0;
   for(i=0;i<cp.size();i++) ans+=cp[i]*a[j],j++;
   cout<<ans;
   ans=0;
   j=n-1;
   for(i=0;i<cp.size();i++) ans+=cp[i]*a[j],j--;
   cout<<" "<<ans;

}


#include<bits/stdc++.h>
#include <iostream>
#include <numeric>
#define ll long long
#define ull unsigned long long
#define mpa make_pair
#define pb push_back
#define ff first
#define ss second
#define boost ios_base::sync_with_stdio(0)
#define ss second
#define forp(i,a,b) for(ll i=a;i<=b;i++)
#define rep(i,n)    for(ll i=0;i<n;++i)
#define ren(i,n)    for(ll i=n-1;i>=0;i--)
#define forn(i,a,b) for(ll i=a;i>=b;i--)
#define fre     freopen("input.txt","r",stdin),freopen("output.txt","w",stdout)
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define scan(x) scanf("%lld",&x)
using namespace std;
ll modpow(ll a,ll n,ll temp){ll res=1,y=a;while(n>0){if(n&1)res=(res*y)%temp;y=(y*y)%temp;n/=2;}return res%temp;}
//ll a[100005],n,dp[1000005]={0},b[1000005],c[100005],dp1[1000005],bit[100005];
//ll vis[100005];
//vector<ll> adj[100005];
#define pi 3.1415926536
#define sz(x) x.size()
ll ison(ll mask,ll pos){
	return (mask&(1<<pos));
}
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
ll cbit(ll n){ll k=0;while(n) n&=(n-1),k++;return k;}
ll nbit(ll n){ll k=0;while(n) n/=2,k++;return k;}
//ll b1[1001][1001],b2[1001][1001],g1[1001][1001],g2[1001][1001],w[1001][1001];
//bool a1[101][101],a2[102][101],a3[103][101];
ll mod=1e9+7;
int sgn(ll x) {
  return x < 0 ? -1 : !!x;
}
//pair<ll,ll> a[1000005];
/*void upd(ll ind,ll val){
    if(ind==0) return;
    while(ind<=n){
        bit[ind]+=val;
        ind+=(ind&-ind);
    }
}
ll qu(ll ind){
  ll k=0; while(ind>0){
    k+=bit[ind];
    ind-=(ind&-ind);
  }
    return k;
}*/

//ll tree[500005];

//vector<ll,pair<ll,ll> > b;
//vector<ll> adj[100005];

/*Tarjan incomplette
void dfs(ll s){
	vis[s]=1;
	for(ll i=0;i<adj[s].size();i++) if(!vis[adj[s][i]]) dfs(adj[s][i]);
    ofvs.pb(s);
}
void dfs2(ll s){
	vis[s]=1;
	if(wt[s]<optc){optn=0,optc=wt[s];}
	if(optc==wt[s]) optn++;
	for(ll i=0;i<rdj[s].size();i++) if(!vis[rdj[s][i]]) dfs2(rdj[s][i]);
}
*/
typedef pair<ll,pair<ll,ll> >ver;
//string s;
/*void build(ll node,ll ss,ll se)
{
	if(ss==se){
	tree[node]=a[ss];
	if(s[ss]=='{') o[node]++;
	else c[node]++;
    }
	else
	{
		int mid=(ss+se)/2;
		build(2*node,ss,mid);
		build(2*node+1,mid+1,se);
		ll t=min(o[2*node],c[2*node+1]);
		o[node]=o[2*node]+o[2*node+1]-t;
		c[node]=c[node*2]+c[node*2+1]-t;
		tree[node]=tree[node*2]+tree[node*2+1]+t;
	}
}
/*void upd(ll node,ll ss,ll se,ll idx,ll val)
{
	if(ss==se)
	a[idx]=val,tree[node]=a[idx];
	else
	{
		ll mid=(ss+se)/2;
		if(ss<=idx && idx<=mid)
		upd(2*node,ss,mid,idx,val);
		else
		upd(2*node+1,mid+1,se,idx,val);
	    tree[node]=tree[node*2]+tree[node*2+1];
	}
}
ver query(int node,int ss,int se,int l,int r)
{
	if(r<ss || se<l)
	return mpa(0,mpa(0,0));
	if(l<=ss && se<=r)
	return mpa(tree[node],mpa(o[node],c[node]));
	ll mid=(ss+se)/2;
	ver p1=query(2*node,ss,mid,l,r);
	ver p2=query(2*node+1,mid+1,se,l,r);
    ll t=min(p1.ss.ff,p2.ss.ss);
	o[node]=p1.ss.ff+p2.ss.ff-t;
	c[node]=p1.ss.ss+p2.ss.ss-t;
	return mpa(p1.ff+p2.ff+t,mpa(o[node],c[node]));
}
/*ll pow(ll n){
	ll t=1,i=1;
	while(i<=n) t*=10;
	return t;
}*/
//stack<ll> help;
//ll dp[1ll<<11];

//ll csum[100005],b[100005],help[100005];
//vector<ll> adj[1005];
//vector<ll> ans;
/*ll kmp[1000005];
ll zee[1000005];
void kmpm(string p){
	ll i,j,k=0,l;
	kmp[0]=0;
	j=0;
	for(i=1;i<p.size();i++){
		while(k && p[i]!=p[k]) k=kmp[k-1];
		if(p[i]==p[k]) k++;
		kmp[i]=k;
	}
}
ll isp(ll n){
	ll f=1,i;
	for(i=2;i<=sqrt(n);i++) if(!(n%i)) return 0;
	return 1;
}*/
/*void zee(string s){}*/
//map<ll,ll> help;
/*ll chk(ll val,ll n,ll m){
	ll t=0,i;
	val--;
	for(i=1;i<=n;i++) t+=min(m,val/i);
	return t;
}*/

/*ll size[100005u];
void init(ll n)
{for(int i=1;i<=n;i++) a[i]=i;}
ll root(ll x)
{
	while(a[x]!=x)
	{
		a[x]=a[a[x]];
		x=a[x];
	}
	return x;
}
ll u1(ll x,ll y)
{
	int p=root(x);
	int q=root(y);
	a[p]=a[q];
}
*/
/*ll tree[100005];
void update(ll idx, ll val){
	while(idx < 100005){
		tree[idx] = max(tree[idx], val);
		idx += (idx & (-idx));
	}
}
ll query(ll idx){
	ll ret = 0;
	while(idx > 0){
		ret = max(ret, tree[idx]);
		idx -= (idx & (-idx));
	}
	return ret;
}*/
//pair<char ,pair<ll,ll> >p[5005];[10004]
ll a[100005];
vector<ll> cp;
map<string,ll> help;
int main(){
   ll i,j,k,l,m,n;
   cin>>n>>m;
   rep(i,n) cin>>a[i];
   while(m--){
   	string b;
   	cin>>b;
   	help[b]++;
   }
   map<string,ll>::iterator it;
   for(it=help.begin();it!=help.end();it++) cp.pb(it->second);
   sort(a,a+n);
   sort(all(cp));
   ll ans=0,temp=0;
   j=0;
   for(i=0;i<cp.size();i++) ans+=cp[i]*a[j],j++;
   for(i=0;i<cp.size();i++) temp+=cp[i]*a[n-j+1],j++;
   cout<<min(ans,temp);
   ans=0;
   j=n-1;
   for(i=cp.size()-1;i>=0;i--) ans+=cp[i]*a[j],j--;
   cout<<" "<<max(ans,temp);

}


#include<bits/stdc++.h>
#include <iostream>
#include <numeric>
#define ll long long
#define ull unsigned long long
#define mpa make_pair
#define pb push_back
#define ff first
#define ss second
#define boost ios_base::sync_with_stdio(0)
#define ss second
#define forp(i,a,b) for(ll i=a;i<=b;i++)
#define rep(i,n)    for(ll i=0;i<n;++i)
#define ren(i,n)    for(ll i=n-1;i>=0;i--)
#define forn(i,a,b) for(ll i=a;i>=b;i--)
#define fre     freopen("input.txt","r",stdin),freopen("output.txt","w",stdout)
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define scan(x) scanf("%lld",&x)
using namespace std;
ll modpow(ll a,ll n,ll temp){ll res=1,y=a;while(n>0){if(n&1)res=(res*y)%temp;y=(y*y)%temp;n/=2;}return res%temp;}
//ll a[100005],n,dp[1000005]={0},b[1000005],c[100005],dp1[1000005],bit[100005];
//ll vis[100005];
//vector<ll> adj[100005];
#define pi 3.1415926536
#define sz(x) x.size()
ll ison(ll mask,ll pos){
	return (mask&(1<<pos));
}
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
ll cbit(ll n){ll k=0;while(n) n&=(n-1),k++;return k;}
ll nbit(ll n){ll k=0;while(n) n/=2,k++;return k;}
//ll b1[1001][1001],b2[1001][1001],g1[1001][1001],g2[1001][1001],w[1001][1001];
//bool a1[101][101],a2[102][101],a3[103][101];
ll mod=1e9+7;
int sgn(ll x) {
  return x < 0 ? -1 : !!x;
}
//pair<ll,ll> a[1000005];
/*void upd(ll ind,ll val){
    if(ind==0) return;
    while(ind<=n){
        bit[ind]+=val;
        ind+=(ind&-ind);
    }
}
ll qu(ll ind){
  ll k=0; while(ind>0){
    k+=bit[ind];
    ind-=(ind&-ind);
  }
    return k;
}*/

//ll tree[500005];

//vector<ll,pair<ll,ll> > b;
//vector<ll> adj[100005];

/*Tarjan incomplette
void dfs(ll s){
	vis[s]=1;
	for(ll i=0;i<adj[s].size();i++) if(!vis[adj[s][i]]) dfs(adj[s][i]);
    ofvs.pb(s);
}
void dfs2(ll s){
	vis[s]=1;
	if(wt[s]<optc){optn=0,optc=wt[s];}
	if(optc==wt[s]) optn++;
	for(ll i=0;i<rdj[s].size();i++) if(!vis[rdj[s][i]]) dfs2(rdj[s][i]);
}
*/
typedef pair<ll,pair<ll,ll> >ver;
//string s;
/*void build(ll node,ll ss,ll se)
{
	if(ss==se){
	tree[node]=a[ss];
	if(s[ss]=='{') o[node]++;
	else c[node]++;
    }
	else
	{
		int mid=(ss+se)/2;
		build(2*node,ss,mid);
		build(2*node+1,mid+1,se);
		ll t=min(o[2*node],c[2*node+1]);
		o[node]=o[2*node]+o[2*node+1]-t;
		c[node]=c[node*2]+c[node*2+1]-t;
		tree[node]=tree[node*2]+tree[node*2+1]+t;
	}
}
/*void upd(ll node,ll ss,ll se,ll idx,ll val)
{
	if(ss==se)
	a[idx]=val,tree[node]=a[idx];
	else
	{
		ll mid=(ss+se)/2;
		if(ss<=idx && idx<=mid)
		upd(2*node,ss,mid,idx,val);
		else
		upd(2*node+1,mid+1,se,idx,val);
	    tree[node]=tree[node*2]+tree[node*2+1];
	}
}
ver query(int node,int ss,int se,int l,int r)
{
	if(r<ss || se<l)
	return mpa(0,mpa(0,0));
	if(l<=ss && se<=r)
	return mpa(tree[node],mpa(o[node],c[node]));
	ll mid=(ss+se)/2;
	ver p1=query(2*node,ss,mid,l,r);
	ver p2=query(2*node+1,mid+1,se,l,r);
    ll t=min(p1.ss.ff,p2.ss.ss);
	o[node]=p1.ss.ff+p2.ss.ff-t;
	c[node]=p1.ss.ss+p2.ss.ss-t;
	return mpa(p1.ff+p2.ff+t,mpa(o[node],c[node]));
}
/*ll pow(ll n){
	ll t=1,i=1;
	while(i<=n) t*=10;
	return t;
}*/
//stack<ll> help;
//ll dp[1ll<<11];

//ll csum[100005],b[100005],help[100005];
//vector<ll> adj[1005];
//vector<ll> ans;
/*ll kmp[1000005];
ll zee[1000005];
void kmpm(string p){
	ll i,j,k=0,l;
	kmp[0]=0;
	j=0;
	for(i=1;i<p.size();i++){
		while(k && p[i]!=p[k]) k=kmp[k-1];
		if(p[i]==p[k]) k++;
		kmp[i]=k;
	}
}
ll isp(ll n){
	ll f=1,i;
	for(i=2;i<=sqrt(n);i++) if(!(n%i)) return 0;
	return 1;
}*/
/*void zee(string s){}*/
//map<ll,ll> help;
/*ll chk(ll val,ll n,ll m){
	ll t=0,i;
	val--;
	for(i=1;i<=n;i++) t+=min(m,val/i);
	return t;
}*/

/*ll size[100005u];
void init(ll n)
{for(int i=1;i<=n;i++) a[i]=i;}
ll root(ll x)
{
	while(a[x]!=x)
	{
		a[x]=a[a[x]];
		x=a[x];
	}
	return x;
}
ll u1(ll x,ll y)
{
	int p=root(x);
	int q=root(y);
	a[p]=a[q];
}
*/
/*ll tree[100005];
void update(ll idx, ll val){
	while(idx < 100005){
		tree[idx] = max(tree[idx], val);
		idx += (idx & (-idx));
	}
}
ll query(ll idx){
	ll ret = 0;
	while(idx > 0){
		ret = max(ret, tree[idx]);
		idx -= (idx & (-idx));
	}
	return ret;
}*/
//pair<char ,pair<ll,ll> >p[5005];[10004]
ll a[100005];
vector<ll> cp;
map<string,ll> help;
int main(){
   ll i,j,k,l,m,n;
   cin>>n>>m;
   rep(i,n) cin>>a[i];
   while(m--){
   	string b;
   	cin>>b;
   	help[b]++;
   }
   map<string,ll>::iterator it;
   for(it=help.begin();it!=help.end();it++) cp.pb(it->second);
   sort(a,a+n);
   sort(all(cp));
   ll ans=0,temp=0;
   j=0;
   for(i=0;i<cp.size();i++) ans+=cp[i]*a[j],j++;
   for(i=0;i<cp.size();i++) temp+=cp[i]*a[n-j+1],j++;
   cout<<min(ans,temp);
   ans=0;
   j=n-1;
   for(i=cp.size()-1;i>=0;i--) ans+=cp[i]*a[j],j--;
   cout<<" "<<max(ans,temp)5;

}


#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
#define N 110
int a[N],d[N];
char b[N],map[N][N];
int main()
{
	int i,j,n,m,top=0,sum1=0,sum2=0,flag;
	scanf("%d%d",&n,&m);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	sort(a,a+n);
	while(m--)
	{
		flag=0;
		scanf("%s",b);
		if(top==0)
		{
			strcpy(map[top],b);
			d[top]=1;
			top++;
		}
		else
		{
			for(i=0;i<top;i++)
			{
				if(strcmp(map[i],b)==0)
				{
					d[i]++;
					flag=1;
					break;
				}

			}
			if(!flag)
			{
				strcpy(map[top],b);
				d[top]++;
				top++;
				flag=0;
			}
		}

	}
	sort(d,d+top);
	j=top-1;
	for(i=0;i<top;i++,j--)
		sum1+=d[j]*a[i];
	j=top-1;
	for(i=n-1;j>=0;i--,j--)
		sum2+=d[j]*a[i];
	printf("%d %d\n",sum1,sum2);
}/*2017-07-20 21:16:00.167*/


#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string>
#include<map>
#include<iostream>
using namespace std;

const int maxn=1e9+10;
struct str
{
    string s;
} b[40];
int a[100],c[40];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0; i<n; i++)
        scanf("%d",&a[i]);
    sort(a,a+n);

    map<string,int>M;
    for(int i=0; i<m; i++)
    {
        cin>>b[i].s;
        M[b[i].s]++;
    }
    for(int i=0; i<M.size(); i++)
    {
        c[i]=M[b[i].s];
    }
    sort(c,c+M.size());
    int min=0,max=0;
    for(int k=M.size()-1,i=0; k>=0; k--,i++)
        min+=(a[i]*c[k]);
    for(int k=n-1,j=M.size()-1; j>=0; k--,j--)
        max+=(a[k]*c[j]);
    printf("%d %d\n",min,max);
}
/*2017-07-20 20:27:48.865*/


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int n,m,arr[105];

struct node
{
    char str[105];
    int  num;
}f[105];
int Size;
char tmp[105];

bool cmp(node a,node b)
{
    return a.num>b.num;
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        Size=0;
        memset(f,0,sizeof(f));
        for(int i=0;i<n;i++)
        {
            scanf("%d",&arr[i]);
        }
        for(int i=0;i<m;i++)
        {
            scanf("%s",tmp);
            int flag=0;
            for(int j=0;j<Size;j++)
            {
                if(!strcmp(f[j].str,tmp))
                {
                    f[j].num++;
                    flag=1;
                    break;
                }
            }
            if(flag==0)
            {
                strcpy(f[Size].str,tmp);
                f[Size++].num=1;
            }
        }
        sort(arr,arr+n);
        sort(f,f+Size,cmp);

        int Max=0,Min=0;
        int pos=0;
        for(int i=0;i<Size;i++)
        {
            Min+=arr[pos++]*f[i].num;
        }
        pos=n-1;
        for(int i=0;i<Size;i++)
        {
            Max+=arr[pos--]*f[i].num;
        }
        printf("%d %d\n",Min,Max);

    }

    return 0;
}/*2017-07-20 20:23:49.27*/


#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<map>
using namespace std;
map<string,int> M;
int price[105];
int num[105],nn=0;
char s[105];
int n,m;
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%d",&price[i]);
    }
    sort(price,price+n);

    for(int i=0;i<m;i++){
        scanf("%s",s);
        M[s] ++;
    }
    for(map<string,int>::iterator it=M.begin();it!=M.end();it++)
    {
        num[nn++] = it->second;
        //printf("%d\n",num[nn-1]);
    }
    sort(num,num+nn);
    int ans1=0,ans2=0;
    for(int i=0;i<nn;i++){
        ans1+=num[nn-i-1]*price[i];
        ans2+=num[nn-i-1]*price[n-i-1];
    }
    printf("%d %d\n",ans1,ans2);
}
/*2017-07-20 19:30:24.835*/


#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<set>
#include<queue>
#include<vector>
#include<vector>
#include<map>
using namespace std;
map<string,int> s;
map<string,int>::iterator it;
int time[100];
int v[100];
int main()
{
    int n,a,b,i;
    string str;
    while(~scanf("%d %d",&a,&b))
    {
        s.clear();
        for(i=0;i<a;i++)
            scanf("%d",&v[i]);
        sort(v,v+a);
        for(i=0;i<b;i++)
        {
            cin>>str;
            s[str]++;
        }
        int j=0;
        for(it=s.begin();it!=s.end();it++)
        {
            int t=it->second;
            time[j++]=t;

        }
        //printf("%d\n",j);
        sort(time,time+j);
        int l=0,r=a-1,smin=0,smax=0;
        for(i=j-1;i>=0;i--)
        {
            smin+=(v[l++]*time[i]);
            smax+=(v[r--]*time[i]);
        }
        printf("%d %d\n",smin,smax);

    }
    return 0;
}/*2017-07-29 22:09:01.87*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std;

struct node
{
    char name[40];
    int t;
} per[105];

int main()
{
    int a[105], n, m, i, k, num[105], mini, maxn;
    bool p[105];

    scanf("%d%d", &n, &m);

    memset(p, 0, sizeof(p));
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    sort(a, a + n);

    for (i = 0; i < 105; i++)
        per[i].t = 0;

    int top = 0;
    for (i = 0; i < m; i++, top++)
    {
        scanf("%s", &per[top].name);
        per[top].t++;
        for (k = 0; k < i; k++)
        {
            if (strcmp(per[i].name, per[k].name) == 0)
            {
                top--;
                per[k].t++;
                break;
            }
        }
    }
    m = top;//要买的水果的数量
    for (i = 0; i < m; i++)
        num[i] = per[i].t;
    sort(num, num + m);

//    for (i = 0; i < n; i++)
//        printf("%d ", a[i]);
//    printf("\n");
//    for (i = 0; i < m; i++)
//        printf("%d ", num[i]);
//    printf("\n");

    mini = 0, maxn = 0;
    for (i = 0, k = m - 1; i < n && k >= 0; i++, k--)//i-n-a, k-m-num
    {
        mini += a[i] * num[k];
    }
    for (i = n - 1, k = m - 1; i >= 0 && k >= 0; i--, k--)
    {
        maxn += a[i] * num[k];
    }

    printf("%d %d\n", mini, maxn);
}
/*2017-07-20 20:04:17.334*/


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int a[105],b[105];
char str[105][40];
int main()
{
    int n,m,i,sum=0,x=0,y=0,j,k,t,s,w;
    scanf("%d%d",&n,&m);
    for(i=0; i<n; i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    for(i=0; i<m; i++)
    {
        int flag=0;
        scanf("%s",str[i]);
        for(k=0; k<=i; k++)
        {
            if(!strcmp(str[i],str[k]))
            {
                b[k]++;
                flag=1;
                break;
            }
            else
            {
                continue;
            }
        }
        if(flag==0)
        {
            sum++;
        }
    }
        sort(b,b+m);
    for(i=0,j=m-1; i<m,j>=0; i++,j--)
    {
        if(b[j]!=0)
        {
            x+=b[j]*a[i];
        }
        else
            break;
    }
    for(i=n-1,j=m-1; i>=0,j>=0; i--,j--)
    {
        y+=(b[j]*a[i]);
    }
    printf("%d %d\n",x,y);
    return 0;
}
/*2017-07-21 09:51:02.861*/


#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string>
#include<map>
#include<iostream>
using namespace std;

map<string,int>::iterator p;
const int maxn=1e9+10;
struct str
{
    string s;
} b[40];
int a[100],c[40];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0; i<n; i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    map<string,int>M;
    for(int i=0; i<m; i++)
    {
        cin>>b[i].s;
        M[b[i].s]++;
    }
    int top=0;
    for(p=M.begin();p!=M.end();p++)
        c[top++]=p->second;

    sort(c,c+M.size());
    int min=0,max=0;
    for(int k=M.size()-1,i=0; k>=0; k--,i++)
        min+=(a[i]*c[k]);
    for(int k=n-1,j=M.size()-1; j>=0; k--,j--)
        max+=(a[k]*c[j]),printf("%d %d\n",a[k],c[j]);
    printf("%d %d\n",min,max);
    return 0;
}
/*2017-07-20 20:55:44.086*/


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    char s1[20],s2[20],temp[20],ans[20];
    int l;
    gets(s1);
    gets(s2);
    l=strlen(s1);
    sort(s1,s1+l);
    memset(temp,0,sizeof(temp));
    int j=0,len=0,flag=1;
    for(int i=0; s1[i]; i++)
    {
        if(s1[i]=='0')
            temp[j++]=s1[i];
        else
        {
            ans[len++]=s1[i];
            if(flag)
            {
                temp[j]='\0';
                for(int k=0; temp[k]; k++)
                    ans[len++]=temp[k];
                flag=0;
            }
        }
    }
    if(flag)
    {
        for(int k=0; temp[k]; k++)
            ans[len++]=temp[k];
    }
    ans[len]='\0';
    //puts(ans);
    if(strcmp(s2,ans)==0)
        puts("OK");
    else
        puts("WRONG_ANSWER");
    return 0;
}
/*2017-07-20 19:55:54.624*/


#include<algorithm>
#include<cstdio>
#include<map>
#include<string>
#include<iostream>
#include<vector>
using namespace std;

map<string,int> my_map;
map<string,int> :: iterator it;
vector<int> Vec;

int cmp(int a,int b)
{
	return a > b;
}

int main()
{
    int n,m,i,Max,Min;
    int a[110];
	string s;

	while(cin >> n >> m)
	{
		my_map.clear();
		Vec.clear();
		Max = Min = 0;

		for(i = 0; i < n;i ++)
		{
			cin >> a[i];
		}

		sort(a,a + n);

		while(m --)
		{
			cin >> s;

			my_map[s] ++;
		}

		for(i = 0,it = my_map.begin();it != my_map.end();i++,it ++)
		{
			Vec.push_back(it -> second);
		}

		sort(Vec.begin(),Vec.begin()+Vec.size(),cmp);

		for(i = 0;i < Vec.size();i ++)
		{
			Min += a[i] * Vec[i];
			Max += a[n - 1 - i] * Vec[i];
		}

		cout<<Min<<" "<<Max<<endl;
	}

	return 0;
}
/*2017-07-31 15:05:44.338*/


#include<stdio.h>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
map<string,int>a;
int p[200],numm[200];
char s[200][200];
int main()
{
    int n,m,i,num,num2,min=0,max=0;
    scanf("%d%d",&n,&m);
    for(i=0; i<n; i++)
        scanf("%d",&p[i]);
    sort(p,p+n);
    num=0;
    for(i=0; i<m; i++)
    {
        scanf("%s",s[i]);
        if(a.find(s[i])==a.end())
            num++;
        a[s[i]]++;
    }
    num2=0;
    for(i=0;i<m;i++)
    {
        if(a[s[i]])
        {
            numm[num2++]=a[s[i]];
            a[s[i]]=0;
        }
    }
    sort(numm,numm+num);
    for(i=0;i<num;i++)
    {
        min+=numm[num-1-i]*p[i];
        max+=numm[num-1-i]*p[n-i-1];
    }
    printf("%d %d\n",min,max);

}
/*2017-07-20 20:26:47.016*/


    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<fstream>
    #include<string>
    #include<vector>
    #include<queue>
    #include<map>
    #include<algorithm>
    #include<set>
    #include<sstream>
    #include<stack>
    #define maxim 100
    #define LLD long long int
    #define LLU long long unsigned
    #define pi acos(-1.0)
    #define inf (1<<29)
    #define CLR(a) memset(a,0,sizeof(a))
    #define SET(a) memset(a,-1,sizeof(a))
    #define pb push_back
    #define all(a) a.begin(),a.end()
    #define eps 1e-9
    #define rep(i,init,n) for(i=init;i<n;i++)
    using namespace std;

    template< class T > T sq(T n) { return n*n; }
    template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
    template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
    template< class T > bool inside(T a, T b, T c) { return a<=b && b<=c; }
    template< class T > void setmax(T &a, T b) { if(a < b) a = b; }
    template< class T > void setmin(T &a, T b) { if(b < a) a = b; }
    template<class T> T power(T N,T P){  return (P==1)?  N: N*power(N,P-1); }

    int main()
    {
        int n,m,MIN,MAX,i,j,x;
        vector<int>cnt;
        vector<int>pri;
        map<string,int>name;
        map<string,int>::iterator it;
        string frt;
        while(scanf("%d%d",&n,&m)==2)
        {
            cnt.clear();
            pri.clear();
            name.clear();
            rep(i,0,n)
            {
                cin>>x;
                pri.pb(x);
            }
            rep(i,0,m)
            {
                cin>>frt;
                name[frt]++;
            }

            for(it=name.begin();it!=name.end();it++)
                cnt.pb(it->second);

            sort(all(cnt));
            sort(all(pri));

            MIN=MAX=0;
            j=0;
            for(i=cnt.size()-1;i>=0;i--)
                MIN+=cnt[i]*pri[j++];

            j=pri.size()-1;

            for(i=cnt.size()-1;i>=0;i--)
                MAX+=cnt[i]*pri[j--];
            printf("%d %d\n",MIN,MAX);
        }
        return 0;
    }


	#include<cstdio>
	#include<cstring>
	#include<cstdlib>
	#include<cctype>
	#include<cmath>
	#include<iostream>
	#include<fstream>
	#include<string>
	#include<vector>
	#include<queue>
	#include<map>
	#include<algorithm>
	#include<set>
	#include<sstream>
	#include<stack>
	#define maxim 100
	#define LLD long long int
	#define LLU long long unsigned
	#define pi acos(-1.0)
	#define inf (1<<29)
	#define CLR(a) memset(a,0,sizeof(a))
	#define SET(a) memset(a,-1,sizeof(a))
	#define pb push_back
	#define all(a) a.begin(),a.end()
	#define eps 1e-9
	#define rep(i,init,n) for(i=init;i<n;i++)
	using namespace std;

	template< class T > T sq(T n) { return n*n; }
	template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
	template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
	template< class T > bool inside(T a, T b, T c) { return a<=b && b<=c; }
	template< class T > void setmax(T &a, T b) { if(a < b) a = b; }
	template< class T > void setmin(T &a, T b) { if(b < a) a = b; }
	template<class T> T power(T N,T P){  return (P==1)?  N: N*power(N,P-1); }

	int main()
	{
		LLD n,m,MIN,MAX,i,j,x;
		vector<int>cnt;
		vector<int>pri;
		map<string,int>name;
		map<string,int>::iterator it;
		string frt;
		while(scanf("%lld%lld",&n,&m)==2)
		{
			cnt.clear();
			pri.clear();
			name.clear();
			rep(i,0,n)
			{
				cin>>x;
				pri.pb(x);
			}
			rep(i,0,m)
			{
				cin>>frt;
				name[frt]++;
			}

			for(it=name.begin();it!=name.end();it++)
				cnt.pb(it->second);

			sort(all(cnt));
			sort(all(pri));

			MIN=MAX=0;
			j=0;
			for(i=cnt.size()-1;i>=0;i--)
				MIN+=cnt[i]*pri[j++];

			j=pri.size()-1;

			for(i=cnt.size()-1;i>=0;i--)
				MAX+=cnt[i]*pri[j--];
			printf("%lld %lld\n",MIN,MAX);
		}
		return 0;
	}


	#include<cstdio>
	#include<cstring>
	#include<cstdlib>
	#include<cctype>
	#include<cmath>
	#include<iostream>
	#include<fstream>
	#include<string>
	#include<vector>
	#include<queue>
	#include<map>
	#include<algorithm>
	#include<set>
	#include<sstream>
	#include<stack>
	#define maxim 100
	#define LLD long long int
	#define LLU long long unsigned
	#define pi acos(-1.0)
	#define inf (1<<29)
	#define CLR(a) memset(a,0,sizeof(a))
	#define SET(a) memset(a,-1,sizeof(a))
	#define pb push_back
	#define all(a) a.begin(),a.end()
	#define eps 1e-9
	#define rep(i,init,n) for(i=init;i<n;i++)
	using namespace std;

	template< class T > T sq(T n) { return n*n; }
	template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
	template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
	template< class T > bool inside(T a, T b, T c) { return a<=b && b<=c; }
	template< class T > void setmax(T &a, T b) { if(a < b) a = b; }
	template< class T > void setmin(T &a, T b) { if(b < a) a = b; }
	template<class T> T power(T N,T P){  return (P==1)?  N: N*power(N,P-1); }

	int main()
	{
		LLD n,m,MIN,MAX,i,j,x;
		vector<int>cnt;
		vector<int>pri;
		map<string,int>name;
		map<string,int>::iterator it;
		string frt;
		while(scanf("%lld%lld",&n,&m)==2)
		{
			rep(i,0,n)
			{
				cin>>x;
				pri.pb(x);
			}
			rep(i,0,m)
			{
				cin>>frt;
				name[frt]++;
			}

			for(it=name.begin();it!=name.end();it++)
				cnt.pb(it->second);

			sort(all(cnt));
			sort(all(pri));

			MIN=MAX=0;
			j=0;
			for(i=cnt.size()-1;i>=0;i--)
				MIN+=cnt[i]*pri[j++];

			j=pri.size()-1;

			for(i=cnt.size()-1;i>=0;i--)
				MAX+=cnt[i]*pri[j--];
			printf("%lld %lld\n",MIN,MAX);
		}
		return 0;
	}


#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

const int MAXN = 100 + 10;
int arr[MAXN], t[MAXN];
vector <string> v;
map <string, int> mp;

int main()
{
	int n, m;
	cin>> n >> m;

	for(int i = 0; i < n; i ++)
		cin>> arr[i];
	sort(arr, arr + n);

	for(int i = 0; i < m; i ++)
	{
		string s;
		cin>> s;
		bool ch = false;

		for(int j = 0; j < v.size(); j ++)
			if(v[j] == s)
			{
				ch = true;
				break;
			}

		if(!ch)
			v.push_back(s);

		mp[s] ++;
	}

	for(int i = 0; i < v.size(); i ++)
		t[i] = mp[v[i]];
	sort(t, t + v.size());

	long long mn = 0, mx = 0;
	int j = v.size() - 1;
	for(int i = 0; i < v.size(); i ++)
	{
//		cout<< t[j] << " " << arr[i] << endl;
		mn += t[j] * arr[i];
		j --;
	}
	j = v.size() - 1;
	for(int i = n - 1; i >= 0; i --)
	{
//		cout<< t[j] << " " << arr[i] << endl;
		mx += t[j] * arr[i];
		j --;
	}
	cout<< mn << " " << mx << endl;

}


#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

const int MAXN = 100 + 10;
int arr[MAXN], t[MAXN];
vector <string> v;
map <string, int> mp;

int main()
{
    int n, m;
    cin>> n >> m;

    for(int i = 0; i < n; i ++)
        cin>> arr[i];
    sort(arr, arr + n);

    for(int i = 0; i < m; i ++)
    {
        string s;
        cin>> s;
        bool ch = false;

        for(int j = 0; j < v.size(); j ++)
            if(v[j] == s)
            {
                ch = true;
                break;
            }

        if(!ch)
            v.push_back(s);

        mp[s] ++;
    }

    for(int i = 0; i < v.size(); i ++)
        t[i] = mp[v[i]];
    sort(t, t + v.size());

    long long mn = 0, mx = 0;
    int j = v.size() - 1;
    for(int i = 0; i < v.size(); i ++)
    {
//      cout<< t[j] << " " << arr[i] << endl;
        mn += t[j] * arr[i];
        j --;
    }
    j = n - 1;
    for(int i = v.size() - 1; i >= 0; i --)
    {
//      cout<< t[j] << " " << arr[i] << endl;
        mx += t[i] * arr[j];
        j --;
    }
    cout<< mn << " " << mx << endl;

}


#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

const int MAXN = 100 + 10;
int arr[MAXN], t[MAXN];
vector <string> v;
map <string, int> mp;

int main()
{
	int n, m;
	cin>> n >> m;

	for(int i = 0; i < n; i ++)
		cin>> arr[i];
	sort(arr, arr + n);

	for(int i = 0; i < m; i ++)
	{
		string s;
		cin>> s;
		bool ch = false;

		for(int j = 0; j < v.size(); j ++)
			if(v[j] == s)
			{
				ch = true;
				break;
			}

		if(!ch)
			v.push_back(s);

		mp[s] ++;
	}

	for(int i = 0; i < v.size(); i ++)
		t[i] = mp[v[i]];
	sort(t, t + v.size());

	int mn = 0, mx = 0, j = v.size() - 1;
	for(int i = 0; i < v.size(); i ++)
	{
//		cout<< t[j] << " " << arr[i] << endl;
		mn += t[j] * arr[i];
		j --;
	}
	j = v.size() - 1;
	for(int i = n - 1; i >= 0; i --)
	{
//		cout<< t[j] << " " << arr[i] << endl;
		mx += t[j] * arr[i];
		j --;
	}
	cout<< mn << " " << mx << endl;

}


#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std ;
vector< pair<string,int> >  f ;
bool cmp ( pair<string,int>&a , pair<string,int>&b)
{
    return a.second > b.second ;
}
int myfind( string x ){
    for ( int i=0 ; i<f.size() ; i++){
        if ( x == f[i].first)
            return i ;
    }
    return -1 ;
}
int main () {
    vector<int> p(100) ;
    int n , m  ;
    cin >> n >> m ;
    for ( int i =0 ; i < n ; i++)
        cin >> p[i] ;
    sort ( p.begin() , p.begin() + n ) ;
    for ( int i = 0 ; i < m ; i++){
        string x ;
        cin >> x  ;
        if ( myfind(x) != -1 )
            f[myfind(x)].second++ ;
        else {
            f.push_back(make_pair(x,1)) ;
        }
    }
    sort ( f.begin(),f.begin()+m , cmp) ;
    int min = 0 ;
    for ( int i=0 , j=0 ; i<m ; i++ , j++)
        min += f[i].second * p[j] ;
    sort ( p.begin() , p.begin()+n , greater<int>() ) ;
    int max=0 ;
    for ( int i=0 , j=0 ; i<m ; i++ , j++)
        max+= f[i].second * p[j] ;

    cout << min <<" " <<max << endl ;
return 0 ;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

struct Fruit
{
    char name[35];
    int num;
}a[100];
int k = 0;

bool judge(char *p)     //判断是否已存在
{
    for(int i = 0; i < k; i++)
        if(strcmp(p,a[i].name) == 0)
        {
            a[i].num++;
            return true;
        }
    return false;
}

int main()
{
    int n, m;
    int price[100];
    int num[100];
    int the_max = 0, the_min = 0;
    char name[35];

    //输入数据
    scanf("%d%d", &n, &m);
    for(int i = 0; i < n; i++)
        scanf("%d", &price[i]);
    for(int i = 0; i < m; i++)
    {
        scanf("%s", name);
        if(judge(name) == false)
        {
            strcpy(a[k].name,name);
            a[k++].num = 1;
        }
    }

    //处理数据
    sort(price, price + n);
    for(int i = 0; i < k; i++)
        num[i] = a[i].num;
    sort(num, num + k);
    for(int i = 0; i < k; i++)
    {
        the_max += price[n - i - 1] * num[k - i - 1];
        the_min += price[i] * num[k - i - 1];
    }
    printf("%d %d", the_min, the_max);

    return 0;
}


import java.util.*;
public class Main {

    public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
    int a=s.nextInt();
    int b=s.nextInt();
    int in[]=new int[a];
    int in1[]=new int[b];
    String str[]=new String[b];
    for(int i=0;i<a;i++){
        in[i]=s.nextInt();
    }
  Arrays.sort(in);
     s.nextLine();
     for(int i=0;i<b;i++){
         in1[i]=1;
         str[i]=s.nextLine();
     }

     for(int i=0;i<b;i++){
         if(in1[i]>=0){
         for(int j=i+1;j<b;j++){
             if(str[i].equals(str[j])){
                 in1[i]++;
                 in1[j]=-1;
                 System.out.println(in1[i]);
             }
         }
         }
     }
     int min=0,max=0;
  Arrays.sort(in1);
     for(int i=0;i<b;i++){
         if(in1[i]>0) min=min+in[i]*in1[b-i-1] ;
     }
     for(int i=0;i<b;i++){
         if(in1[i]>0) max=max+in[a-i-1]*in1[i] ;
     }
     System.out.print(min+" ");
     System.out.println(max);
    }

}


/*
 * F. Fruits.cpp
 *
 *  Created on: Aug 29, 2015
 *      Author: Osama Hatem
 */

#include <bits/stdtr1c++.h>
#include <ext/numeric>

using namespace std;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
//	freopen("out.out", "w", stdout);
#endif

	int n, m, pr[105];
	map<string, int> cnt;
	vector<int> cnts;
	string fruit;
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		cin >> pr[i];
	while (m--) {
		cin >> fruit;
		cnt[fruit]++;
	}
	for (map<string, int>::iterator it = cnt.begin(); it != cnt.end(); it++)
		cnts.push_back(it->second);
	int mini = 0, maxi = 0;
	sort(pr, pr + n);
	sort(cnts.begin(), cnts.end(), greater<int>());
	for (int i = 0; i < (int) cnts.size(); i++)
		mini += pr[i] * cnts[i], maxi += pr[n - i - 1] * cnts[i];
	cout << mini << " " << maxi << endl;
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
map<string,int>s;
map<string,int>::iterator itr;
int a[105],cnt[105];
int main()
{
    ios::sync_with_stdio(false);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>a[i];
    sort(a+1,a+n+1);
    string str;
    for(int i=1;i<=m;i++)
    {
        cin>>str;
        s[str]++;
    }
    int tot=0;
    for(itr=s.begin();itr!=s.end();itr++)
    {
        cnt[++tot]=(*itr).second;
    }
    sort(cnt+1,cnt+tot+1,greater<int>());
    int ans1=0,ans2=0;
    for(int i=1;i<=tot;i++)
    {
        ans1+=cnt[i]*a[i];
        ans2+=cnt[i]*a[n+1-i];
    }
    cout<<ans1<<" "<<ans2<<endl;
    return 0;
}


#include <map>
#include <new>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <cstring>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < (n); i++)
#define re return
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define fill(x,y) memset(x,y,sizeof(x))
#define forn(i,n) for(int i=0;i<(n);++i)
#define lng long long
#define ABS(a) ((a)<0?-(a):(a))
#define inf 2000000000
#define eps 1e-8
#define EQ(a,b) abs((a)-(b))<eps
#define PI 3.1415926535897932384626433832795
#define ulng unsigned lng
#define FOR(i,a,b) for (int i(a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define SORT(v) sort((v).begin(),(v).end())
int gcd(int a,int b)
{
    return a?gcd(b%a,a):b;
}
bool cmp(char i, char j)
{
    return i>=j;
}
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n, m, a[200], k=0, minn=0, maxx=0, b[110];
    string ss[110], s1[110];
    map<string, int> p;
    cin >> n >> m;
    for (int i=0; i<n; i++) cin >> a[i];
    sort(a, a+n);
    for (int i=0; i<m; i++){string s; cin >> s; p[s]=0; s1[i]=s; s.clear();}
    for (int i=0; i<m; i++){if (!p[s1[i]]){ss[k]=s1[i]; k++;}p[s1[i]]++;}
    for (int i=0; i<k; i++)
        b[i]=p[ss[i]];
    sort(b, b+k, cmp);
    //for (int i=0; i<k; i++) cout << b[i] << endl;
    for (int i=0; i<k; i++)
    {
        minn+=b[i]*a[i];
        maxx+=b[i]*a[n-i-1];
    }
    cout << minn << " " << maxx << endl;
    //system("pause");
    //cout << fixed;
    //cout << setprecision (5) << f << endl;
    return 0;
}


#include <map>
#include <new>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <cstring>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < (n); i++)
#define re return
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define fill(x,y) memset(x,y,sizeof(x))
#define forn(i,n) for(int i=0;i<(n);++i)
#define lng long long
#define ABS(a) ((a)<0?-(a):(a))
#define inf 2000000000
#define eps 1e-8
#define EQ(a,b) abs((a)-(b))<eps
#define PI 3.1415926535897932384626433832795
#define ulng unsigned lng
#define FOR(i,a,b) for (int i(a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define SORT(v) sort((v).begin(),(v).end())
int gcd(int a,int b)
{
    return a?gcd(b%a,a):b;
}
bool cmp(char i, char j)
{
    return i>=j;
}
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n, m, a[200], k=0, minn=0, maxx=0, b[110];
    string ss[110];
    map<string, int> p;
    cin >> n >> m;
    for (int i=0; i<n; i++) cin >> a[i];
    sort(a, a+n);
    for (int i=0; i<m; i++){string s; cin >> s; if (!p[s]){ss[k]=s; k++;}p[s]++; s.clear();}
    for (int i=0; i<k; i++)
        b[i]=p[ss[i]];
    sort(b, b+k, cmp);
    //for (int i=0; i<k; i++) cout << b[i] << endl;
    for (int i=0; i<k; i++)
    {
        minn+=b[i]*a[i];
        maxx+=b[i]*a[n-i-1];
    }
    cout << minn << " " << maxx << endl;
    //system("pause");
    //cout << fixed;
    //cout << setprecision (5) << f << endl;
    return 0;
}


#include <map>
#include <new>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <cstring>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < (n); i++)
#define re return
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define fill(x,y) memset(x,y,sizeof(x))
#define forn(i,n) for(int i=0;i<(n);++i)
#define lng long long
#define ABS(a) ((a)<0?-(a):(a))
#define inf 2000000000
#define eps 1e-8
#define EQ(a,b) abs((a)-(b))<eps
#define PI 3.1415926535897932384626433832795
#define ulng unsigned lng
#define FOR(i,a,b) for (int i(a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define SORT(v) sort((v).begin(),(v).end())
int gcd(int a,int b)
{
    return a?gcd(b%a,a):b;
}
bool cmp(char i, char j)
{
    return i>=j;
}
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n, m, a[200], k=0, minn=0, maxx=0, b[110];
    string ss[110];
    map<string, int> p;
    cin >> n >> m;
    for (int i=0; i<n; i++) cin >> a[i];
    sort(a, a+n);
    for (int i=0; i<m; i++){string s; cin >> s; if (!p[s]){ss[k]=s; k++;}p[s]++; s.clear();}
    for (int i=0; i<k; i++)
        b[i]=p[ss[i]];
    sort(b, b+k, cmp);
    //for (int i=0; i<k; i++) cout << b[i] << endl;
    for (int i=0; i<k; i++)
    {
        minn+=b[i]*a[i];
        maxx+=b[i]*a[n-i-1];
    }
    cout << minn << " " << maxx << endl;
    system("pause");
    //cout << fixed;
    //cout << setprecision (5) << f << endl;
    return 0;
}