//By Abdurazzacov Yerzhan
// email address : erjan1996-96@mail.ru
// email address : erzhan96@mail.ru
// email address : erjan1996@gmail.ru
//telephone number : 8-702-818-66-12
 /* Allah Saktasin */
//C++ headers
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<cctype>
#include<cassert>
#include<climits>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<clocale>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cwchar>
#include<cwctype>

//containers
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<deque>
#include<set>
#include<complex>
#include<string>
#include<stack>
#include<bitset>
#include<istream>
#include<valarray>

//IOs
#include<iostream>
#include<sstream>
#include<iomanip>
#include<fstream>
#include<exception>
#include<ios>
#include<iosfwd>
#include<ostream>
#include<iterator>
#include<stdexcept>
#include<streambuf>

//algorithm & miscellaneous
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<limits>
#include<locale>
#include<memory>
#include<new>

/* ****************************************************************** */
// \_/\_/\_/\_/\_/\_/\_/---Frequently used macros---\_/\_/\_/\_/\_/\_/\_/
/* ****************************************************************** */

#define MOD 1000000007
#define INF 1000000000

/* ****************************************************************** */
// \_/\_/\_/\_/\_/\_/\_/\_/\_/---Shorthands---\_/\_/\_/\_/\_/\_/\_/\_/\_/
/* ****************************************************************** */

using namespace std;

typedef vector < int > vi;
typedef vector < vi > vvi;
typedef pair < int, int > pii;
typedef long long ll;
typedef set < int >::iterator sii;
typedef deque < int > di;

#define F first
#define S second

#define s( x ) scanf ( "%d" , &x )
#define sl( x ) scanf ( "%lld" , &x )
#define ss( x ) scanf ( "%s" , &x )

#define p(x) printf("%d",x)
#define pl(x) printf("%lld",x)
#define ps(x) printf("%s",x)

#define pn(x) printf("%d\n",x)
#define pln(x) printf("%lld\n",x)
#define psn(x) printf("%s\n",x)

#define X length()
#define mp make_pair
#define pub(x) push_back(x)
#define puf(x) push_front(x)
#define pob() pop_back()
#define pof() pop_front()
#define forn(i, n) for(ll i = 1; i <= (ll)(n); i++)
#define ford(i, n) for(ll i = (ll)(n) ; i >= 1; i--)
#define fore(i, a, n) for(ll i = (ll)(a); i < (ll)(n); i++)

ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}
ll lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}

ll n , m , a[ 110 ] , b[ 110 ] , sum = 0 , c = 1 , col = 0;
string s[ 110 ];
bool u[ 110 ];
map < string , bool > Q;
pair < ll, string > W[ 110 ];

  int main ()
{

  #ifndef ONLINE_JUDGE
    freopen ( "input.txt" , "rt" , stdin );
   freopen ( "output.txt" , "wt" , stdout );
  #endif
  memset ( u , false, sizeof ( u ) );
  s ( n ) , s ( m );
  forn ( i , n )
  {
    s ( a[ i ] );
  }
  sort ( a + 1 , a + n + 1 );

  forn ( i , m )
  {
    cin >> s[ i ];
  }
  forn ( i , n )
  {
    col = 0;
    forn ( j , n )
    {
        if ( s[ i ] == s[ j ] ) col ++;
    }
    b[ i ] = col;
  }
  forn ( i , m )
  {
    if ( Q[ s[ i ] ] == false ) Q[ s[ i ] ] = true ,  W[ c ].S = s[ i ] , W[ c ].F = b[ i ] , c ++;
  }
  c --;
  sort ( W + 1 , W + c + 1 );
  reverse ( W + 1 , W + c + 1 );
  // reverse ( a + 1 , a + n + 1 );
  forn ( i , c )
  {
    sum += a[ i ] * W[ i ].F;
  }
  // cout << sum << ' ';
  // sum = 0;
  // forn ( i , n )
  // {
    // cout << a[ i ] << ' ';
  // }
  // cout << endl;
  // forn ( i , c )
  // {
    // cout << W[ i ].F << ' ' << W[ i ].S << endl;
  // }
  cout << sum << ' ';
  // reverse ( W + 1 , W + c + 1 );
  reverse ( a + 1 , a + n + 1 );
  sum = 0;
  forn ( i , c )
  {
    sum += a[ i ] * W[ i ].F;
  }
  // forn ( i , n )
  // {
    // cout << a[ i ] << ' ';
  // }
  // ps ( " ********************* " );
  // ps ( "\n" );
  // forn ( i , c )
  // {
    // cout << W[ i ].F << ' ' << W[ i ].S << endl;
  // }
  cout << sum << endl;

  // cout << sum;

fclose ( stdin );
fclose ( stdout );
}


//By Abdurazzacov Yerzhan
// email address : erjan1996-96@mail.ru
// email address : erzhan96@mail.ru
// email address : erjan1996@gmail.ru
//telephone number : 8-702-818-66-12
 /* Allah Saktasin */
//C++ headers
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<cctype>
#include<cassert>
#include<climits>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<clocale>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cwchar>
#include<cwctype>

//containers
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<deque>
#include<set>
#include<complex>
#include<string>
#include<stack>
#include<bitset>
#include<istream>
#include<valarray>

//IOs
#include<iostream>
#include<sstream>
#include<iomanip>
#include<fstream>
#include<exception>
#include<ios>
#include<iosfwd>
#include<ostream>
#include<iterator>
#include<stdexcept>
#include<streambuf>

//algorithm & miscellaneous
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<limits>
#include<locale>
#include<memory>
#include<new>

/* ****************************************************************** */
// \_/\_/\_/\_/\_/\_/\_/---Frequently used macros---\_/\_/\_/\_/\_/\_/\_/
/* ****************************************************************** */

#define MOD 1000000007
#define INF 1000000000

/* ****************************************************************** */
// \_/\_/\_/\_/\_/\_/\_/\_/\_/---Shorthands---\_/\_/\_/\_/\_/\_/\_/\_/\_/
/* ****************************************************************** */

using namespace std;

typedef vector < int > vi;
typedef vector < vi > vvi;
typedef pair < int, int > pii;
typedef long long ll;
typedef set < int >::iterator sii;
typedef deque < int > di;

#define F first
#define S second

#define s( x ) scanf ( "%d" , &x )
#define sl( x ) scanf ( "%lld" , &x )
#define ss( x ) scanf ( "%s" , &x )

#define p(x) printf("%d",x)
#define pl(x) printf("%lld",x)
#define ps(x) printf("%s",x)

#define pn(x) printf("%d\n",x)
#define pln(x) printf("%lld\n",x)
#define psn(x) printf("%s\n",x)

#define X length()
#define mp make_pair
#define pub(x) push_back(x)
#define puf(x) push_front(x)
#define pob() pop_back()
#define pof() pop_front()
#define forn(i, n) for(ll i = 1; i <= (ll)(n); i++)
#define ford(i, n) for(ll i = (ll)(n) ; i >= 1; i--)
#define fore(i, a, n) for(ll i = (ll)(a); i < (ll)(n); i++)

ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}
ll lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}

ll n , m , a[ 110 ] , b[ 110 ] , sum = 0 , c = 1 , col = 0 , ans = 0;
string s[ 110 ] , k[ 110 ];
bool u[ 110 ];
map < string , ll > Q;
pair < ll, string > W[ 110 ];

  int main ()
{

  #ifndef ONLINE_JUDGE
    freopen ( "input.txt" , "rt" , stdin );
   freopen ( "output.txt" , "wt" , stdout );
  #endif
  memset ( u , false, sizeof ( u ) );
  s ( n ) , s ( m );
  forn ( i , n )
  {
  	s ( a[ i ] );
  }
  sort ( a + 1 , a + n + 1 );

  forn ( i , m )
  {
  	cin >> s[ i ];
  	Q[ s[ i ] ]++;
  }
  forn ( i , m )
  {
  	if ( u[ i ] == true ) continue;
  	forn ( j , m )
  	{
  		if ( i == j ) continue;
  		if ( s[ i ] == s[ j ] && u[ j ] == false )
  		{
  			u[ j ] = true;
  		}
  	}
  	k[ c ] = s[ i ] , c ++;
  }
  col = 1;
  forn ( i , c )
  {
  	if ( !u[ i ] )
  	{
  		b[ col ] = Q[ s[ i ] ] , col ++;
  	}
  }
  sort ( b + 1 , b + col + 1 );
  reverse ( b + 1 , b + col + 1 );
  forn ( i , col )
  {
  	ans += b[ i ] * a[ i ];
  }
  reverse ( a + 1 , a + n + 1 );
  cout << ans << ' ';
  ans = 0;
  ford ( i , col )
  {
  	ans += b[ i ] * a[ i ];
  }
  cout << ans;

fclose ( stdin );
fclose ( stdout );
}


//By Abdurazzacov Yerzhan
// email address : erjan1996-96@mail.ru
// email address : erzhan96@mail.ru
// email address : erjan1996@gmail.ru
//telephone number : 8-702-818-66-12
 /* Allah Saktasin */
//C++ headers
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<cctype>
#include<cassert>
#include<climits>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<clocale>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cwchar>
#include<cwctype>

//containers
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<deque>
#include<set>
#include<complex>
#include<string>
#include<stack>
#include<bitset>
#include<istream>
#include<valarray>

//IOs
#include<iostream>
#include<sstream>
#include<iomanip>
#include<fstream>
#include<exception>
#include<ios>
#include<iosfwd>
#include<ostream>
#include<iterator>
#include<stdexcept>
#include<streambuf>

//algorithm & miscellaneous
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<limits>
#include<locale>
#include<memory>
#include<new>

/* ****************************************************************** */
// \_/\_/\_/\_/\_/\_/\_/---Frequently used macros---\_/\_/\_/\_/\_/\_/\_/
/* ****************************************************************** */

#define MOD 1000000007
#define INF 1000000000

/* ****************************************************************** */
// \_/\_/\_/\_/\_/\_/\_/\_/\_/---Shorthands---\_/\_/\_/\_/\_/\_/\_/\_/\_/
/* ****************************************************************** */

using namespace std;

typedef vector < int > vi;
typedef vector < vi > vvi;
typedef pair < int, int > pii;
typedef long long ll;
typedef set < int >::iterator sii;
typedef deque < int > di;

#define F first
#define S second

#define s( x ) scanf ( "%d" , &x )
#define sl( x ) scanf ( "%lld" , &x )
#define ss( x ) scanf ( "%s" , &x )

#define p(x) printf("%d",x)
#define pl(x) printf("%lld",x)
#define ps(x) printf("%s",x)

#define pn(x) printf("%d\n",x)
#define pln(x) printf("%lld\n",x)
#define psn(x) printf("%s\n",x)

#define X length()
#define mp make_pair
#define pub(x) push_back(x)
#define puf(x) push_front(x)
#define pob() pop_back()
#define pof() pop_front()
#define forn(i, n) for(ll i = 1; i <= (ll)(n); i++)
#define ford(i, n) for(ll i = (ll)(n) ; i >= 1; i--)
#define fore(i, a, n) for(ll i = (ll)(a); i < (ll)(n); i++)

ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}
ll lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}

ll n , m , a[ 110 ] , b[ 110 ] , sum = 0;
string s[ 110 ];

  int main ()
{

  #ifndef ONLINE_JUDGE
    freopen ( "input.txt" , "rt" , stdin );
   freopen ( "output.txt" , "wt" , stdout );
  #endif
  s ( n ) , s ( m );
  forn ( i , n )
  {
    s ( a[ i ] );
  }
  forn ( i , m )
  {
    cin >> s[ i ];
  }
  forn ( i , m )
  {
    ll col = 0;
    forn ( j , m )
    {
        // if ( i == j ) continue;
        if ( s[ i ] == s[ j ] ) col ++;
    }
    b[ i ] = col;
  }
  sort ( a + 1 , a + n + 1 );
  sort ( b + 1 , b + m + 1 );
  forn ( i , m )
    sum += a[ i ] * b[ i ];
  cout << sum << ' ';
  sum = 0;
  reverse ( a + 1 , a + n + 1 );
  reverse ( b + 1 , b + m + 1 );
  forn ( i , m )
  {
    sum += a[ i ] * b[ i ];
  }
  cout << sum;

fclose ( stdin );
fclose ( stdout );
}


//By Abdurazzacov Yerzhan
// email address : erjan1996-96@mail.ru
// email address : erzhan96@mail.ru
// email address : erjan1996@gmail.ru
//telephone number : 8-702-818-66-12
 /* Allah Saktasin */
//C++ headers
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<cctype>
#include<cassert>
#include<climits>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<clocale>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cwchar>
#include<cwctype>

//containers
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<deque>
#include<set>
#include<complex>
#include<string>
#include<stack>
#include<bitset>
#include<istream>
#include<valarray>

//IOs
#include<iostream>
#include<sstream>
#include<iomanip>
#include<fstream>
#include<exception>
#include<ios>
#include<iosfwd>
#include<ostream>
#include<iterator>
#include<stdexcept>
#include<streambuf>

//algorithm & miscellaneous
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<limits>
#include<locale>
#include<memory>
#include<new>

/* ****************************************************************** */
// \_/\_/\_/\_/\_/\_/\_/---Frequently used macros---\_/\_/\_/\_/\_/\_/\_/
/* ****************************************************************** */

#define MOD 1000000007
#define INF 1000000000

/* ****************************************************************** */
// \_/\_/\_/\_/\_/\_/\_/\_/\_/---Shorthands---\_/\_/\_/\_/\_/\_/\_/\_/\_/
/* ****************************************************************** */

using namespace std;

typedef vector < int > vi;
typedef vector < vi > vvi;
typedef pair < int, int > pii;
typedef long long ll;
typedef set < int >::iterator sii;
typedef deque < int > di;

#define F first
#define S second

#define s( x ) scanf ( "%d" , &x )
#define sl( x ) scanf ( "%lld" , &x )
#define ss( x ) scanf ( "%s" , &x )

#define p(x) printf("%d",x)
#define pl(x) printf("%lld",x)
#define ps(x) printf("%s",x)

#define pn(x) printf("%d\n",x)
#define pln(x) printf("%lld\n",x)
#define psn(x) printf("%s\n",x)

#define X length()
#define mp make_pair
#define pub(x) push_back(x)
#define puf(x) push_front(x)
#define pob() pop_back()
#define pof() pop_front()
#define forn(i, n) for(ll i = 1; i <= (ll)(n); i++)
#define ford(i, n) for(ll i = (ll)(n) ; i >= 1; i--)
#define fore(i, a, n) for(ll i = (ll)(a); i < (ll)(n); i++)

ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}
ll lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}

ll n , m , a[ 102 ] , b[ 102 ] , c = 1 , ans = 0;
string k[ 102 ];
bool u[ 102 ];

  int main ()
{

  #ifndef ONLINE_JUDGE
    freopen ( "input.txt" , "rt" , stdin );
   freopen ( "output.txt" , "wt" , stdout );
  #endif

  memset ( u , false , sizeof ( u ) );
  s ( n ) , s ( m );
  forn ( i , n )
  {
    s ( a[ i ] );
  }
  sort ( a + 1 , a + n + 1 );
  forn ( i , m )
  {
    cin >> k[ i ];
  }
  forn ( i , m )
  {
    ll ans = 0;
    forn ( j , m )
    {
        if ( k[ i ] == k[ j ] && u[ j ] == false )
        {
            ans ++;
            u[ j ] = true;
        }
    }
    if ( ans != 0 ) b[ c ] = ans , c ++;
  }
  c --;
  sort ( b + 1 , b + c + 1 );
  reverse ( b + 1 , b + c + 1 );
  forn ( i , c )
  {
    ans += b[ i ] * a[ i ];
  }
  cout << ans << ' ';
  ans = 0;
  reverse ( a + 1 , a + n + 1 );
  // reverse ( b + 1 , b + c + 1 );
  forn ( i , c )
  {
    ans += b[ i ] * a[ i ];
  }
  cout << ans;

fclose ( stdin );
fclose ( stdout );
}


#include<bits/stdc++.h>
using namespace std;
map<string,int> gr;
int a[100005];
int main()
{
int n,m;

scanf("%d%d",&n,&m);
int i,x;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
int c[105];
int in = 0;
char b[555];
for(i=0;i<m;i++)
{
scanf("%s",&b);
gr[b]++;
}
map<string,int>::iterator I;
for(I=gr.begin();I!=gr.end();I++)
c[in++] = I->second;

sort(c,c+in);
reverse(c,c+in);
int mi,ma;
mi = ma = 0;

for(i=0;i<in;i++)
mi+=c[i] * a[i];

reverse(a,a+n);

for(i=0;i<in;i++)
ma+=c[i] * a[i];

printf("%d %d\n",mi,ma);

return 0;
}


#include<bits/stdc++.h>
using namespace std;
map<string,int> ss;
int a[100005];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int i,x;
for(i=0;i<n;i++)
scanf("%d",&ai]);
sort(a,a+n);
int c[105];
int in = 0;
char b[555];
for(i=0;i<m;i++)
{
scanf("%s",&b);
ss[b]++;
}
map<string,int>::iterator I;
for(I=gr.begin();I!=gr.end();I++)
c[in++] = I->second;
sort(c,c+in);
reverse(c,c+in);
int mi,ma;
mi = ma = 0;
for(i=0;i<in;i++)
mi+=c[i] * a[i];
reverse(a,a+n);
for(i=0;i<in;i++)
ma+=c[i] * a[i];

printf("%d %d\n",mi,ma);

return 0;
}


#include<bits/stdc++.h>
using namespace std;
map<string,int> ss;
int a[100005];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int i,x;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
int c[105];
int in = 0;
char b[555];
for(i=0;i<m;i++)
{
scanf("%s",&b);
ss[b]++;
}
map<string,int>::iterator I;
for(I=gr.begin();I!=gr.end();I++)
c[in++] = I->second;
sort(c,c+in);
reverse(c,c+in);
int mi,ma;
mi = ma = 0;
for(i=0;i<in;i++)
mi+=c[i] * a[i];
reverse(a,a+n);
for(i=0;i<in;i++)
ma+=c[i] * a[i];

printf("%d %d\n",mi,ma);

return 0;
}


#include <bits/stdc++.h>

using namespace std;

int m,n,a,b,g[105],mi,ma,d=0;
string q[105];
int main()
{
    cin >> n >> m;
    int x=m;
    for(int i=0; i<n; i++) cin >> g[i];
    for(int i=0; i<m; i++) cin >> q[i];
    for(int i=0; i<m; i++){
        for(int j=i+1; j<x; j++){
            if(q[j]==q[i]) {m--;d++;}
        }
    }
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            if(g[j]<g[i]) swap(g[j],g[i]);
        }
    }
    for(int i=0;i<m;i++){
        mi+=g[i];
    }
    mi=mi+(d*g[0]);
    for(int j=n-1,k=1;k<=m;j--,k++){
        ma+=g[j];
    }
    ma=ma+(d*g[n-1]);
    cout << mi << ' ' << ma;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int m,n,a,b,g[105],mi,ma,d=0;
string q[105];
int main()
{
    cin >> n >> m;
    int x=m;
    for(int i=0; i<n; i++) cin >> g[i];
    for(int i=0; i<m; i++) cin >> q[i];
    for(int i=0; i<m; i++){
        for(int j=i+1; j<x; j++){
            if(q[j]==q[i]) {m--;d++;}
        }
    }
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            if(g[j]<g[i]) swap(g[j],g[i]);
        }
    }
    for(int i=0;i<m;i++){
        mi+=g[i];
    }
    mi=mi+(d*g[0]);
    for(int j=n-1;j>=m-1-d;j--){
        ma+=g[j];
    }
    ma=ma+(d*g[n-1]);
    cout << mi << ' ' << ma;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int m,n,a,b,g[105],mi,ma;
string q[105];
int main()
{
    cin >> n >> m;
    for(int i=0; i<n; i++) cin >> g[i];
    for(int i=0; i<m; i++) cin >> q[i];
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            if(g[j]<g[i]) swap(g[j],g[i]);
        }
    }
    for(int i=0;i<m;i++){
        mi+=g[i];
    }
    for(int j=n-1;j>=m-1;j--){
        ma+=g[j];
    }
    cout << mi << ' ' << ma;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int m,n,a,b,g[105],mi,ma,d[105];
string q[105];
int main()
{
    memset(d,0,sizeof(d));
    d[0]=1;
    cin >> n >> m;
    int x=m;
    for(int i=0; i<n; i++) cin >> g[i];
    for(int i=0; i<m; i++) cin >> q[i];
    for(int i=0; i<m; i++){
        for(int j=i+1; j<m; j++){
            if(q[j]<q[i]) swap(q[j],q[i]);
        }
    }

    int cnt=0;
    for(int i=1; i<m; i++){

            if(q[i]==q[i-1]) {d[cnt]++;}
            else {d[++cnt]=1;}

    }
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            if(g[j]<g[i]) swap(g[j],g[i]);
        }
    }
    //Tinh min
    for(int i=0; i<m; i++){
        for(int j=i+1; j<m; j++){
            if(d[j]<d[i]) swap(d[j],d[i]);
        }
    }
    for(int i=0,l=0;l<=cnt;i++,l++){
        mi+=d[l]*g[i];
    }
    //Tinh max
    for(int i=0; i<m; i++){
        for(int j=i+1; j<m; j++){
            if(d[j]>d[i]) swap(d[j],d[i]);
        }
    }
    for(int j=n-1,k=0;k<=cnt;j--,k++){
        ma+=d[k]*g[j];
    }

    cout << mi << ' ' << ma;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int m,n,a,b,g[105],mi,ma,d=0;
string q[105];
int main()
{
    cin >> n >> m;
    int x=m;
    for(int i=0; i<n; i++) cin >> g[i];
    for(int i=0; i<m; i++) cin >> q[i];
    for(int i=0; i<m; i++){
        for(int j=i+1; j<x; j++){
            if(q[j]==q[i]) {m--;d++;}
        }
    }
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            if(g[j]<g[i]) swap(g[j],g[i]);
        }
    }
    for(int i=0,l=1;l<=m;i++,l++){
        mi+=g[i];
    }
    mi=mi+(d*g[0]);
    for(int j=n-1,k=1;k<=m;j--,k++){
        ma+=g[j];
    }
    ma=ma+(d*g[n-1]);
    cout << mi << ' ' << ma;
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <complex>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cassert>
using namespace std;

#define SZ(X) ((int)(X.size()))
#define PB(X) push_back(X)
#define two(X) (1<<(X))
#define MP make_pair
#define FILL(a, b) memset(a, b, sizeof(a))
#define ALL(X) (X).begin(), (X).end()
#define IT iterator
#define COUNT(x) (__builtin_popcount(x))
#define fab(i,a,b) for(int i=(a);i<=(b);++i)
#define fba(i,b,a) for(int i=(b);i>=(a);--i)
#define fec(i,a)   for(typeof(a.end())::i=a.begin();i!=a.end();++i)

typedef long long LL;
typedef vector < int > VI;
typedef vector < double > VD;
typedef pair< int, int > PII;
typedef set < int > SI;
const double pi = acos(-1.0);

map<string, int> sum;
VI a, b;
int n, m, res1(0), res2(0);

int main()
{
	//freopen("a.in", "r", stdin);
	//freopen("a.out", "w", stdout);

	cin>> n>> m;
	fab(i, 1, n)
	{
		int x;
		cin>> x;
		a.PB(x);
	}
	sort(ALL(a));
	sum.clear();
	fab(i, 1, m)
	{
		string op;
		cin>> op;
		sum[op]++;
	}
	fec(i, sum)
	b.PB(i->second);
	sort(ALL(b));
	fba(i, SZ(b)-1, 0)
	res1 += b[i] * a[SZ(b)-1-i];
	fba(i, SZ(b)-1, 0)
	res2 += b[i] * a[SZ(a)-1-(SZ(b)-1-i)];
	printf("%d %d\n", res1, res2);

	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <complex>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cassert>
using namespace std;

#define SZ(X) ((int)(X.size()))
#define PB(X) push_back(X)
#define two(X) (1<<(X))
#define MP make_pair
#define FILL(a, b) memset(a, b, sizeof(a))
#define ALL(X) (X).begin(), (X).end()
#define IT iterator
#define COUNT(x) (__builtin_popcount(x))
#define fab(i,a,b) for(int i=(a);i<=(b);++i)
#define fba(i,b,a) for(int i=(b);i>=(a);--i)
#define fec(i,a)   for(typeof(a.end())i=a.begin();i!=a.end();++i)

typedef long long LL;
typedef vector < int > VI;
typedef vector < double > VD;
typedef pair< int, int > PII;
typedef set < int > SI;
const double pi = acos(-1.0);

map<string, int> sum;
VI a, b;
int n, m, res1(0), res2(0);

int main()
{
	//freopen("a.in", "r", stdin);
	//freopen("a.out", "w", stdout);

	cin>> n>> m;
	fab(i, 1, n)
	{
		int x;
		cin>> x;
		a.PB(x);
	}
	sort(ALL(a));
	sum.clear();
	fab(i, 1, m)
	{
		string op;
		cin>> op;
		sum[op]++;
	}
	fec(i, sum)
	b.PB(i->second);
	sort(ALL(b));
	fba(i, SZ(b)-1, 0)
	res1 += b[i] * a[SZ(b)-1-i];
	fba(i, SZ(b)-1, 0)
	res2 += b[i] * a[SZ(a)-1-(SZ(b)-1-i)];
	printf("%d %d\n", res1, res2);

	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <complex>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cassert>
using namespace std;

#define SZ(X) ((int)(X.size()))
#define PB(X) push_back(X)
#define two(X) (1<<(X))
#define MP make_pair
#define FILL(a, b) memset(a, b, sizeof(a))
#define ALL(X) (X).begin(), (X).end()
#define IT iterator
#define COUNT(x) (__builtin_popcount(x))
#define fab(i,a,b) for(int i=(a);i<=(b);++i)
#define fba(i,b,a) for(int i=(b);i>=(a);--i)
#define fec(i,a)   for(map<string, int>::iterator i=a.begin();i!=a.end();++i)

typedef long long LL;
typedef vector < int > VI;
typedef vector < double > VD;
typedef pair< int, int > PII;
typedef set < int > SI;
const double pi = acos(-1.0);

map<string, int> sum;
VI a, b;
int n, m, res1(0), res2(0);

int main()
{
	//freopen("a.in", "r", stdin);
	//freopen("a.out", "w", stdout);

	cin>> n>> m;
	fab(i, 1, n)
	{
		int x;
		cin>> x;
		a.PB(x);
	}
	sort(ALL(a));
	sum.clear();
	fab(i, 1, m)
	{
		string op;
		cin>> op;
		sum[op]++;
	}
	fec(i, sum)
	b.PB(i->second);
	sort(ALL(b));
	fba(i, SZ(b)-1, 0)
	res1 += b[i] * a[SZ(b)-1-i];
	fba(i, SZ(b)-1, 0)
	res2 += b[i] * a[SZ(a)-1-(SZ(b)-1-i)];
	printf("%d %d\n", res1, res2);

	return 0;
}


#include "stdio.h"
#include "string.h"
#include "stdlib.h"
int compare(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int compare2(const void *a,const void *b)
{
    return *(int *)b-*(int *)a;
}
int main()
{
    int *price,*nums,i,j,h,k,l=0,min=0,max=0;
    char **names,temp[80];
    scanf("%d%d",&i,&j);
    price=(int *)calloc(i,sizeof(int));
    nums=(int *)calloc(j,sizeof(int));
    names=(char **)calloc(j,sizeof(char*));
    for(h=0;h<i;h++)
        scanf("%d",&price[h]);
    qsort(price,i,sizeof(int),compare);
    getchar();
    for(h=0;h<j;h++)
    {
        scanf("%s",temp);
        for(k=0;k<l;k++)
            if(strcmp(temp,names[k])==0)
                break;
        if(k==l)
        {
            names[l]=(char*)calloc(strlen(temp)+1,sizeof(char));
            strcpy(names[l],temp);
            l++;
        }
        nums[k]++;
    }
    qsort(nums,l,sizeof(int),compare2);
    for(k=0;k<l;k++)
    {
        min+=nums[k]*price[k];
        max+=nums[k]*price[i-1-k];
    }
    printf("%d %d",min,max);
    return 0;
}


#include "stdio.h"
#include "string.h"
#include "stdlib.h"
int compare(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int compare2(const void *a,const void *b)
{
    return *(int *)b-*(int *)a;
}
int main()
{
    int *price,*nums,i,j,h,k,l=0,min=0,max=0;
    char **names,temp[80];
    scanf("%d%d",&i,&j);
    price=(int *)calloc(i,sizeof(int));
    nums=(int *)calloc(j,sizeof(int));
    names=(char **)calloc(j,sizeof(char*));
    for(h=0;h<i;h++)
        scanf("%d",&price[h]);
    qsort(price,i,sizeof(int),compare);
    getchar();
    for(h=0;h<j;h++)
    {
        scanf("%s",temp);
        for(k=0;k<h;k++)
            if(strcmp(temp,names[k])==0)
                break;
        if(k==h)
        {
            names[h]=(char*)calloc(strlen(temp)+1,sizeof(char));
            strcpy(names[h],temp);
        }
        nums[k]++;
        if(l<k)
            l=k;
    }
    qsort(nums,l+1,sizeof(int),compare2);
    for(k=0;k<=l;k++)
    {
        min+=nums[k]*price[k];
        max+=nums[k]*price[i-1-k];
    }
    printf("%d %d",min,max);
    return 0;
}


#include "stdio.h"
#include "string.h"
#include "stdlib.h"
int compare(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}
int compare2(const void *a,const void *b)
{
    return *(int *)b-*(int *)a;
}
int main()
{
    int *price,*nums,i,j,h,k,l=0,min=0,max=0;
    char **names,temp[50];
    scanf("%d%d",&i,&j);
    price=(int *)calloc(i,sizeof(int));
    nums=(int *)calloc(j,sizeof(int));
    names=(char **)calloc(j,sizeof(char*));
    for(h=0;h<i;h++)
        scanf("%d",&price[h]);
    qsort(price,i,sizeof(int),compare);
    getchar();
    for(h=0;h<j;h++)
    {
        scanf("%s",temp);
        for(k=0;k<h;k++)
            if(strcmp(temp,names[k])==0)
                break;
        if(k==h)
        {
            names[h]=(char*)calloc(strlen(temp)+1,sizeof(char));
            strcpy(names[h],temp);
        }
        nums[k]++;
        if(l<k)
            l=k;
    }
    qsort(nums,l+1,sizeof(int),compare2);
    for(k=0;k<=l;k++)
    {
        min+=nums[k]*price[k];
        max+=nums[k]*price[i-1-k];
    }
    printf("%d %d",min,max);
    return 0;
}


#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int a,b;
    cin>>a;
    cin>>b;

    vector<int>arr;
    int c;

    for(int i=0; i<a; i++)
    {
        cin>>c;
        arr.push_back(c);
    }

    map<string,int>store;

    string fruit;

    for(int i=0; i<b; i++)
    {
        cin>>fruit;

        if (store.count(fruit)>0)
        {
            store[fruit]++;
        }
        else
        {
            store[fruit]=1;
        }
    }

    sort(arr.begin(),arr.end());

    vector<int>::reverse_iterator it;

    map<string, int>::iterator sit;

    long int min=0;
    long int max=0;

    vector<int>kep;

    int k=0;
    int m=a-1;

    for(sit=store.begin(); sit!=store.end(); sit++)
    {
        kep.push_back(sit->second);
    }

    sort(kep.begin(),kep.end());

    for(it=kep.rbegin(); it!=kep.rend(); it++)
    {
        min+=(*it *arr[k]);
        max+=(*it *arr[m]);
        k++;
        m--;
    }

    cout<<min<<" "<<max<<endl;

}


#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int a,b;
    cin>>a;
    cin>>b;

    vector<int>arr;
    int c;

    for(int i=0; i<a; i++)
    {
        cin>>c;
        arr.push_back(c);
    }

    map<string,int>store;

    string fruit;

    for(int i=0; i<b; i++)
    {
        cin>>fruit;

        if (store.count(fruit)>0)
        {
            store[fruit]++;
        }
        else
        {
            store[fruit]=1;
        }
    }

    sort(arr.begin(),arr.end());

    vector<int>::iterator it;

    map<string, int>::iterator sit;

    long int min=0;
    long int max=0;

    int k=0;
    int m=a-1;

    for(sit=store.begin(); sit!=store.end(); sit++)
    {
        min+=(sit->second*arr[k]);
        max+=(sit->second*arr[m]);
        k++;
        m--;
    }

    cout<<min<<" "<<max<<endl;

}


#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int a,b;
    cin>>a;
    cin>>b;

    vector<int>arr;
    int c;

    for(int i=0; i<a; i++)
    {
        cin>>c;
        arr.push_back(c);
    }

    map<string,int>store;

    string fruit;

    for(int i=0; i<b; i++)
    {
        cin>>fruit;

        if (store.count(fruit)>0)
        {
            store[fruit]++;
        }
        else
        {
            store[fruit]=1;
        }
    }

    sort(arr.begin(),arr.end());

    vector<int>::iterator it;

    map<string, int>::iterator sit;

    long int min=0;
    long int max=0;

    int k=0;
    int m=a-1;

    for(sit=store.begin(); sit!=store.end(); sit++)
    {
        min+=(sit->second*arr[k]);
        max+=(sit->second*arr[m]);
        k++;
        m--;
    }

    cout<<max<<" "<<max<<endl;

}


#include <stdio.h>
#include <map>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
map<string,int>mp;
vector <string> a;
int c[1000];
int f[1000];
int main()
{
    int m,n,i,s;
    while(cin>>n>>m)
    {
      for(i=0;i<n;i++)
      cin>>c[i];
      sort(&c[0],&c[n]);
      for(i=0;i<m;i++)
      {
          string s;
          cin>>s;
          if(!mp[s])
          {
              a.push_back(s);
          }
          mp[s]++;
      }
      s=a.size();
      for(i=0;i<s;i++)
         f[i]=mp[a[i]];

      sort(&f[0],&f[s]);
      //for(i=0;i<s;i++)
      //printf("%d ",f[i]);
       int mn=0;

       for( i = 0; i < s; i++)
        {
                mn += f[s - 1 - i] * c[i];
        }

        int mx = 0;
        for(int i = 0; i < s; ++i)
        {
                mx += f[s - 1 - i] * c[n - 1 - i];
        }
        printf("%d %d\n",mn,mx);

       a.clear();
       mp.clear();
       memset(f,0,sizeof(f));
    }
    return 0;
}


#include <stdio.h>
#include <map>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <vector>
using namespace std;
map<string,int>mp;
vector <string> a;
int c[1000];
int f[1000];
int main()
{
    int m,n,i,s;
    while(cin>>n>>m)
    {
      for(i=0;i<n;i++)
      cin>>c[i];
      sort(&c[0],&c[n]);
      for(i=0;i<m;i++)
      {
          string s;
          cin>>s;
          if(!mp[s])
          {
              a.push_back(s);
          }
          mp[s]++;
      }
      s=a.size();
      for(i=0;i<s;i++)
         f[i]=mp[a[i]];

      sort(&f[0],&f[s]);
      //for(i=0;i<s;i++)
      //printf("%d ",f[i]);
       int mn=0;

       for( i = 0; i < s; i++)
        {
                mn += f[s - 1 - i] * c[i];
        }

        int mx = 0;
        for(int i = 0; i < s; ++i)
        {
                mx += f[s - 1 - i] * c[n - 1 - i];
        }
        printf("%d %d\n",mn,mx);

       a.clear();
       mp.clear();
       memset(f,0,sizeof(f));
    }
    return 0;
}


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

int main()
{
    //freopen("input.in", "r", stdin);
    int m, n;
    cin >> n >> m;
    vector<int> p(n);
    for(int i = 0; i < p.size(); i++)
        cin >> p[i];
    map<string, int> fruits;
    for(int i = 0; i < m; i++)
    {
        string s;
        cin >> s;
        fruits[s]++;
    }
    sort(p.begin(), p.end());
    vector<int> f;
    map<string, int>::iterator it;
    for(it = fruits.begin(); it != fruits.end(); it++)
        f.push_back(it->second);
    sort(f.rbegin(), f.rend());
    int minp = 0;
    int idx = 0;
    for(int i = 0; i < f.size(); i++)
    {
        minp += f[i] * p[idx];
        idx++;
    }
    reverse(p.begin(), p.end());
    int maxp = 0;
    idx = 0;
    for(int i = 0 ; i < f.size(); i++)
    {
        maxp += f[i] * p[idx];
        idx++;
    }
    cout << minp << " " << maxp << endl;

    return 0;
}


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

int main()
{
	freopen("input.in", "r", stdin);
	int m, n;
	cin >> n >> m;
	vector<int> p(n);
	for(int i = 0; i < p.size(); i++)
		cin >> p[i];
	map<string, int> fruits;
	for(int i = 0; i < m; i++)
	{
		string s;
		cin >> s;
		fruits[s]++;
	}
	sort(p.begin(), p.end());
	vector<int> f;
	map<string, int>::iterator it;
	for(it = fruits.begin(); it != fruits.end(); it++)
		f.push_back(it->second);
	sort(f.rbegin(), f.rend());
	int minp = 0;
	int idx = 0;
	for(int i = 0; i < f.size(); i++)
	{
		minp += f[i] * p[idx];
		idx++;
	}
	reverse(p.begin(), p.end());
	int maxp = 0;
	idx = 0;
	for(int i = 0 ; i < f.size(); i++)
	{
		maxp += f[i] * p[idx];
		idx++;
	}
	cout << minp << " " << maxp << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n,m,a[10005],g=0,d[105];
string s[105];

void sx ( int n )
{
    for ( int i = 0 ; i < n -1 ; i ++)
        for ( int j = i +1 ; j < n ; j ++)
            if ( a[i] > a[j] ) swap(a[i],a[j]);
}

void sx3(int g)
{
    for ( int i = 0 ; i < g ; i ++)
        for ( int j = i + 1 ; j < g ; j ++)
            if ( d[i] < d[j] ) swap(d[i],d[j]);
}

void sx2(int m)
{
    for ( int i = 0 ; i < m ; i ++)
        for ( int j = i +1 ; j < m ; j ++)
            if ( s[i] > s[j]) swap(s[i],s[j]);
}

int main()
{
    cin >> n >> m;
    for ( int i = 0 ; i < n ; i ++) cin >> a[i];
    for ( int j = 0 ; j < m ; j ++) cin >> s[j];
    sx2(m);
    sx(n);
    for ( int i = 0 ; i < 106 ; i ++) d[i]=1;
    int h=0;
    while ( h < m)
    {
        if ( s[h] == s[h+1] )
        {
            d[g]++;
        }
        else g++;
        h++;
    }
    sx3(g);
    long long sum1=0,sum2=0;
    for ( int  i = 0 ; i < g ; i ++)
    {
        for ( int j = 0 ; j < d[i] ; j ++)
        {
            sum1+=a[i];
            sum2+=a[n-i-1];
        }
    }
    cout << sum1<<" "<<sum2;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n,m,a[10005];

void sx ( int n )
{
    for ( int i = 0 ; i < n -1 ; i ++)
        for ( int j = i +1 ; j < n ; j ++)
            if ( a[i] > a[j] ) swap(a[i],a[j]);
}

int main()
{

    cin >> n >> m;
    string s[m+1];
    int d=0;
    for ( int i = 0 ; i < n ; i ++) cin >> a[i];
    for ( int j = 0 ; j < m ; j ++) cin >> s[j];
    for ( int i = 0 ; i < m ; i ++)
        for ( int j = i + 1 ; j < n ; j ++ )
            if ( s[i]==s[j] ) d++;
 //   for ( int i = 1 ; i <= d ; i ++) a[i-1]*=d;
    int l=m-d;
    sx(n);
    int f=0;
    long long sum1=0,sum2=0;
    int c=0,v=n-1;
    while ( (f++) < l)
    {
        sum1+=a[c++];
        sum2+=a[v--];
    }
    f=0;
    c=0,v=n-1;
    while(f++ < d)
    {
        sum1+=a[c++];
        sum2+=a[v--];
    }
    cout << sum1<<" "<<sum2;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n,m,a[10005],g=0,d[105];
string s[105];

void sx ( int n )
{
    for ( int i = 0 ; i < n -1 ; i ++)
        for ( int j = i +1 ; j < n ; j ++)
            if ( a[i] > a[j] ) swap(a[i],a[j]);
}

void sx3(int g)
{
    for ( int i = 0 ; i < g ; i ++)
        for ( int j = i + 1 ; j < g ; j ++)
            if ( d[i] < d[j] ) swap(d[i],d[j]);
}

void sx2(int m)
{
    for ( int i = 0 ; i < m ; i ++)
        for ( int j = i +1 ; j < m ; j ++)
            if ( s[i] > s[j]) swap(s[i],s[j]);
}

int main()
{
    cin >> n >> m;
    for ( int i = 0 ; i < n ; i ++) cin >> a[i];
    for ( int j = 0 ; j < m ; j ++) cin >> s[j];
    sx2(m);
    sx(n);
    for ( int i = 0 ; i < 106 ; i ++) d[i]=1;
    int h=0;
    while ( h < m)
    {
        while ( s[h] == s[h+1] )
        {
            d[g]++;
            h++;
        }
        g++;
        h++;
    }
    sx3(g);
//    for ( int i = 0 ; i < n ; i ++) cout << a[i] << " ";
//    cout <<endl;
//    for ( int i = 0 ; i < g ; i ++) cout << d[i]<<" ";
//    cout << endl;
//    for ( int i = 0 ; i < m ; i ++) cout <<s[i]<<" ";
        long long sum1=0,sum2=0;
    for ( int  i = 0 ; i < g ; i ++)
    {
        for ( int j = 0 ; j < d[i] ; j ++)
        {
            sum1+=a[i];
            sum2+=a[n-i-1];
        }
    }
    cout << sum1<<" "<<sum2;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n,m,a[10005],g=0,d[105];
string s[105];

void sx ( int n )
{
    for ( int i = 0 ; i < n -1 ; i ++)
        for ( int j = i +1 ; j < n ; j ++)
            if ( a[i] > a[j] ) swap(a[i],a[j]);
}

void sx3(int g)
{
    for ( int i = 0 ; i < g ; i ++)
        for ( int j = i + 1 ; j < g ; j ++)
            if ( d[i] < d[j] ) swap(d[i],d[j]);
}

void sx2(int m)
{
    for ( int i = 0 ; i < m ; i ++)
        for ( int j = i +1 ; j < m ; j ++)
            if ( s[i] > s[j]) swap(s[i],s[j]);
}

int main()
{
    cin >> n >> m;
    for ( int i = 0 ; i < n ; i ++) cin >> a[i];
    for ( int j = 0 ; j < m ; j ++) cin >> s[j];
    sx2(m);
    sx(n);
    for ( int i = 0 ; i < 106 ; i ++) d[i]=1;
    int h=0;
    while ( h < m)
    {
        while ( s[h] == s[h+1] )
        {
            d[g]++;
            h++;
        }
        else g++;
        h++;
    }
    sx3(g);
    long long sum1=0,sum2=0;
    for ( int  i = 0 ; i < g ; i ++)
    {
        for ( int j = 0 ; j < d[i] ; j ++)
        {
            sum1+=a[i];
            sum2+=a[n-i-1];
        }
    }
    cout << sum1<<" "<<sum2;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n,m,a[10005],g=0,d[105];
string s[105];

void sx ( int n )
{
    for ( int i = 0 ; i < n -1 ; i ++)
        for ( int j = i +1 ; j < n ; j ++)
            if ( a[i] > a[j] ) swap(a[i],a[j]);
}

void sx3(int g)
{
    for ( int i = 0 ; i < g -1 ; i ++)
        for ( int j = i + 1 ; j < g ; j ++)
            if ( d[i] < d[j] ) swap(d[i],d[j]);
}

void sx2(int m)
{
    for ( int i = 0 ; i < m ; i ++)
        for ( int j = i +1 ; j < m ; j ++)
            if ( s[i] > s[j]) swap(s[i],s[j]);
}

int main()
{
    cin >> n >> m;
    for ( int i = 0 ; i < n ; i ++) cin >> a[i];
    for ( int j = 0 ; j < m ; j ++) cin >> s[j];
    sx2(m);
    sx(n);
    for ( int i = 0 ; i < 105 ; i ++) d[i]=1;
    int h=0;
    while ( h < m)
    {
        if ( s[h] == s[h+1] )
        {
            d[g]++;
        }
        else g++;
        h++;
    }
    sx3(g);
    long long sum1=0,sum2=0;
    for ( int  i = 0 ; i < g ; i ++)
    {
        for ( int j = 0 ; j < d[i] ; j ++)
        {
            sum1+=a[i];
            sum2+=a[n-i-1];
        }
    }
    cout << sum1<<" "<<sum2;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n,m,a[10005],g=0,d[105];
string s[105];

void sx ( int n )
{
    for ( int i = 0 ; i < n -1 ; i ++)
        for ( int j = i +1 ; j < n ; j ++)
            if ( a[i] > a[j] ) swap(a[i],a[j]);
}

void sx3(int g)
{
    for ( int i = 0 ; i < g ; i ++)
        for ( int j = i + 1 ; j < g ; j ++)
            if ( d[i] < d[j] ) swap(d[i],d[j]);
}

void sx2(int m)
{
    for ( int i = 0 ; i < m ; i ++)
        for ( int j = i +1 ; j < m ; j ++)
            if ( s[i] > s[j]) swap(s[i],s[j]);
}

int main()
{
    cin >> n >> m;
    for ( int i = 0 ; i < n ; i ++) cin >> a[i];
    for ( int j = 0 ; j < m ; j ++) cin >> s[j];
    sx2(m);
    sx(n);
    for ( int i = 0 ; i < 106 ; i ++) d[i]=1;
    int h=0;
    while ( h < m)
    {
        while ( s[h] == s[h+1] )
        {
            d[g]++;
            h++;
        }
        g++;
        h++;
    }
    sx3(g);
//    for ( int i = 0 ; i < n ; i ++) cout << a[i] << " ";
//    cout <<endl;
//    for ( int i = 0 ; i < g ; i ++) cout << d[i]<<" ";
//    cout << endl;
//    for ( int i = 0 ; i < m ; i ++) cout <<s[i]<<" ";
    long long sum1=0,sum2=0;
    for ( int  i = 0 ; i < g ; i ++)
    {
        for ( int j = 0 ; j < d[i] ; j ++)
        {
            sum1+=a[i];
            sum2+=a[n-i-1];
        }
    }
    cout << sum1<<" "<<sum2;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n,m,a[10005],g=0,d[105];
string s[105];

void sx ( int n )
{
    for ( int i = 0 ; i < n -1 ; i ++)
        for ( int j = i +1 ; j < n ; j ++)
            if ( a[i] > a[j] ) swap(a[i],a[j]);
}

void sx3(int g)
{
    for ( int i = 0 ; i < g ; i ++)
        for ( int j = i + 1 ; j < g ; j ++)
            if ( d[i] < d[j] ) swap(d[i],d[j]);
}

void sx2(int m)
{
    for ( int i = 0 ; i < m ; i ++)
        for ( int j = i +1 ; j < m ; j ++)
            if ( s[i] > s[j]) swap(s[i],s[j]);
}

int main()
{
    cin >> n >> m;
    for ( int i = 0 ; i < n ; i ++) cin >> a[i];
    for ( int j = 0 ; j < m ; j ++) cin >> s[j];
    sx2(m);
    sx(n);
    for ( int i = 0 ; i < 106 ; i ++) d[i]=1;
    int h=0;
    while ( h < m)
    {
        if ( s[h] == s[h+1] )
        {
            d[g]++;
            h++;
        }
        else g++;
        h++;
    }
    sx3(g);
    long long sum1=0,sum2=0;
    for ( int  i = 0 ; i < g ; i ++)
    {
        for ( int j = 0 ; j < d[i] ; j ++)
        {
            sum1+=a[i];
            sum2+=a[n-i-1];
        }
    }
    cout << sum1<<" "<<sum2;
    return 0;
}


#include <iostream>

using namespace std;

int main(){
    int n, m;
    cin >> n >> m;
    int* p = new int[n];
    for (int i = 0; i < n; i++){
        cin >> p[i];
    }
    sort(p,p+n);
    string* f = new string[m];
    for (int i = 0; i < m; i++){
        cin >> f[i];
    }
    sort(f,f+m);
    int u = 1;
    int* t = new int [n];
    int c = 1;
    for (int i = 1; i < m; i++){
        if (f[i].compare(f[i-1]) != 0){
           t[u-1] = c;
           u++;
           c=1;
        } else {
          c++;
        }
    }
    t[u-1] = c;
    sort(t,t+u);

    int min = 0, max = 0;
    for (int i = 0; i < u; i++){
        min += p[i] * t[u-i-1];
    }
    for (int i = n-u; i < n; i++){
        max += p[i] * t[i-n+u];
    }
    cout << min << " " << max << endl;
    cin >> min;
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

int main(){
    int n, m;
    cin >> n >> m;
    int* p = new int[n];
    for (int i = 0; i < n; i++){
        cin >> p[i];
    }
    sort(p,p+n);
    string* f = new string[m];
    for (int i = 0; i < m; i++){
        cin >> f[i];
    }
    sort(f,f+m);
    int u = 1;
    int* t = new int [n];
    int c = 1;
    for (int i = 1; i < m; i++){
        if (f[i].compare(f[i-1]) != 0){
           t[u-1] = c;
           u++;
           c=1;
        } else {
          c++;
        }
    }
    t[u-1] = c;
    sort(t,t+u);

    int min = 0, max = 0;
    for (int i = 0; i < u; i++){
        min += p[i] * t[u-i-1];
    }
    for (int i = n-u; i < n; i++){
        max += p[i] * t[i-n+u];
    }
    cout << min << " " << max << endl;
    cin >> min;
    return 0;
}


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

int a[1010],c[1010],bl,amax,amin;
char b[1010],str[1010][1010];

int xd(int k){
	int t=strlen(b);
	if(t!=strlen(str[k])) return 0;
	for(int i=0;i<t;i++)
		if(str[k][i]!=b[i]) return 0;
	return 1;
}

int main(){
	int n,m; cin>>n>>m;
	for(int i=0;i<n;i++)
		cin>>a[i];
	sort(a,a+n);
	for(int i=0;i<m;i++){
		cin>>b;
		int ok=0;
		for(int j=0;j<bl;j++)
			if(xd(bl-1)) { ok=1,c[j]++; break; }
		if(!ok) { strcpy(str[bl],b); c[bl++]++; }
	}
// 	2
	sort(c,c+bl);
	for(int i=0;i<bl;i++) amin+=a[i]*c[bl-i-1],amax+=a[n-i-1]*c[bl-i-1];
	cout<<amin<<" "<<amax<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n, m, v[110], minv = 0, maxv = 0;
struct fruit{
    string name;
    int num;
}arr[110];
bool com(fruit a, fruit b) {
    return a.num < b.num;
}
bool comn(int a, int b) {
    return a < b;
}
string s;
int top;
int main() {
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        cin >> v[i];
    }
    sort(v, v + n, comn);
    bool ad;
    for (int i = 0; i < m; ++i) {
        cin >> s;
        ad = false;
        for (int j = 0; j < top; ++j) {
            if (arr[j].name == s) {
                ++arr[j].num;
                ad = true;
                break;
            }
        }
        if (!ad) {
            arr[top].name = s;
            arr[top].num = 1;
            ++top;
        }
    }
    sort(arr, arr + top, com);
    for (int i = 0; i < top; ++i) {
        minv += v[i] * arr[top - i - 1].num;
        maxv += v[n - i - 1] * arr[top - i - 1].num;
    }
    cout << minv << " " << maxv << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n, m, v[110], minv = 0, maxv = 0;
struct fruit{
    string name;
    int num;
}arr[110];
bool com(fruit a, fruit b) {
    return a.num < b.num;
}
bool comn(int a, int b) {
    return a < b;
}
string s;
int top;
int main()
{
    cin>>n>>m;
    for (int i = 0; i < n; ++i)
	{
        cin>>v[i];
    }
    sort(v,v+n,comn);
    bool ad;
    for (int i=0;i<m;++i)
	{
        cin >> s;
        ad = false;
        for (int j = 0; j < top; ++j)
		{
            if (arr[j].name == s)
		    {
                ++arr[j].num;
                ad = true;
                break;
            }
        }
        if (!ad)
		{
            arr[top].name = s;
            arr[top].num = 1;
            ++top;
        }
    }
    sort(arr,arr+top,com);
    for (int i = 0; i < top; ++i)
	{
        minv += v[i] * arr[top - i - 1].num;
        maxv += v[n - i - 1] * arr[top - i - 1].num;
    }
    cout << minv << " " << maxv << endl;
    return 0;
}


///IN THE NAME OF MY CREATOR...................
#include<bits/stdc++.h>
using namespace std;
int main()
{
     int n,m;
     cin>>n>>m;
     int a[n];
     for(int i=0;i<n;i++)
     {
         cin>>a[i];

     }
     sort(a,a+n);

     map<string,int>p;

      string s;
      string ss[1000];
        int b[1000];

      for(int i=0;i<m;i++)
      {
          cin>>s;
          ss[i]=(string)s;
          p[s]++;

      }
      sort(ss,ss+m);
      int j=0;
  for(int i=0;i<m;i+=p[ss[i]])
  {
       if(p[ss[i]])
       {
           b[j]=p[ss[i]];
           j++;
       }
  }
  sort(b,b+j);
  int k=j;
  /*
  for(int i=0;i<j;i++)
  {
       cout<<b[i]<<" ";
  }

  printf("\n");
*/

int sum1=0,sum2=0;
 for(int i=0;i<n&&j!=0;i++)
 {

       sum1+=(a[i]*b[j-1]);
       j--;

 }

for(int i=n-1;i>=0&&k!=0;i--)
{
       sum2+=(a[i]*b[k-1]);
       k--;
}

cout<<sum1<<" "<<sum2<<endl;
return 0;

}


///IN THE NAME OF MY CREATOR...................
#include<bits/stdc++.h>
using namespace std;
int main()
{
     int n,m;
     cin>>n>>m;
     int a[n];
     map<string,int>p;
     for(int i=0;i<n;i++)
     {
         cin>>a[i];
     }
     sort(a,a+n);
      string s;
      string ss[1000];
       bool f[10000];
      memset(f,false,sizeof(f));
      for(int i=0;i<m;i++)
      {
          cin>>s;
          ss[i]=(string)s;
          p[s]++;
          if(p[s]>1)
          {
                f[s[0]]=true;
          }

      }
sort(ss,ss+m);

      int j=0;
      int jj=1;
      int sum1=0;
      int sum2=0;
      for(int i=0;i<m;)
      {

            string pp=ss[i];
           if(f[pp[0]]==true){
             // cout<<j<<endl;
              int k=p[pp];
            while(p[pp])
            {

                sum1+=a[j];
                sum2+=a[n-jj];
                p[pp]--;

            }
            i+=k;
            j++;
            jj++;

           }
           else{
               i++;
           }

      }

    int k=j;
    int kk=jj;
     for(int i=0;i<m;i++)
     {
           string p=ss[i];
             if(f[p[0]]!=true)
             {
                 //cout<<p<<" "<<a[k]<<endl;
                   sum1+=a[k];
                   sum2+=a[n-jj];
                   jj++;
                   k++;
             }
     }
    cout<<sum1<<" "<<sum2<<endl;

return 0;

}


///IN THE NAME OF MY CREATOR...................
#include<bits/stdc++.h>
using namespace std;
int main()
{
     int n,m;
     cin>>n>>m;
     int a[n];
     map<string,int>p;
     for(int i=0;i<n;i++)
     {
         cin>>a[i];
     }
     sort(a,a+n);
      string s;
      string ss[1000];
       bool f[10000];
      memset(f,false,sizeof(f));
      for(int i=0;i<m;i++)
      {
          cin>>s;
          ss[i]=(string)s;
          p[s]++;
          if(p[s]>1)
          {
                f[s[0]]=true;
          }

      }

      int j=0;
      int jj=1;
      int sum1=0;
      int sum2=0;
      for(int i=0;i<m;)
      {

            string pp=ss[i];
           if(f[pp[0]]==true){
             // cout<<j<<endl;
              int k=p[pp];
            while(p[pp])
            {

                sum1+=a[j];
                sum2+=a[n-jj];
                p[pp]--;

            }
            i+=k;
            j++;
            jj++;

           }
           else{
               i++;
           }

      }

    int k=j;
    int kk=jj;
     for(int i=0;i<m;i++)
     {
           string p=ss[i];
             if(f[p[0]]!=true)
             {
                 //cout<<p<<" "<<a[k]<<endl;
                   sum1+=a[k];
                   sum2+=a[n-jj];
                   jj++;
                   k++;
             }
     }
    cout<<sum1<<" "<<sum2<<endl;

return 0;

}


/* 2013-08-10 13:53:52.055544 */#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <ctime>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cassert>
#include <bitset>

using namespace std;

int n;
int a[100010];

int main() {
	while (scanf("%d",&n) == 1)
	{
		for (int i = 0; i < n; ++i)
		{
			scanf("%d",&a[i]);
		}
		sort(a, a + n);
		int tot = unique(a, a + n) - a;
		bool flag = false;
		for (int i = 0; i < tot - 1; ++i)
			if (a[i] * 2 > a[i + 1])
			{
				flag = true;
				printf("YES\n");
				break;
			}
		if (!flag) printf("NO\n");
	}
}


/* 2013-08-10 13:56:43.109052 */#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <ctime>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cassert>
#include <bitset>

using namespace std;

int n, m;
int a[200], b[200];
string st[200];

void work()
{
	for (int i = 0; i < n; ++i) scanf("%d",&a[i]);
	sort(a, a + n);

	for (int i = 0; i < m; ++i)
	{
		cin >> st[i];
	}

	sort(st, st + m);

	int cnt = 0;
	b[0] = 0;
	for (int i = 0; i < m; ++i)
	{
		if (i == 0 || st[i] != st[i - 1])
		{
			++cnt;
			b[cnt] = 1;
		} else ++b[cnt];
	}
	sort(b + 1, b + cnt + 1);
	int Min = 0, Max = 0;
	for (int i = 0; i < cnt; ++i) Min += a[i] * b[cnt - i];
	for (int i = n - 1; i >= n - cnt; --i) Max += a[i] * b[cnt - (n - i - 1)];
	printf("%d %d\n",Min, Max);
}

int main() {
	while (scanf("%d%d",&n,&m) == 2)
	{
		work();
	}
}


/* 2013-08-10 12:34:17.564084 */#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <ctime>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cassert>
#include <bitset>

using namespace std;

map<string, int> M;
int a[100],b[100];
string st;

int main() {
	M.clear();
	int n,m;
	cin>>n>>m;
	for (int i=0; i<n; ++i) cin>>a[i];
	sort(a,a+n);
	for (int i=0; i<m; ++i) {
		cin>>st;
		map<string,int>::iterator iter;
		iter=M.find(st);
		if (iter!=M.end()) {
			int x=iter -> second;
			M.erase(iter);
			M.insert(make_pair(st,x+1));
		} else {
			M.insert(make_pair(st,1));
		}
	}
	m=0;
	while (!M.empty()) {
		b[m++]=M.begin() ->second;
		M.erase(M.begin());
	}
	sort(b,b+m);
	int ans=0;
	for (int i=0; i<m; ++i) {
		ans+=a[i]*b[m-i-1];
	}
	printf("%d ",ans);
	ans=0;
	for (int i=0; i<m; ++i) {
		ans+=a[n-i-1]*b[m-i-1];
	}
	printf("%d\n",ans);
	return 0;
}


/* 2013-08-10 10:50:41.199512 */#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <ctime>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cassert>
#include <bitset>
using namespace std;

const int MaxN = 110;
map<string, int> h;
map<string, int>::iterator iter;
int A[MaxN], P[MaxN];

int main() {
	int N, M;
	cin>>N>>M;
	for (int i = 0; i < N; i ++)
		cin>>P[i];
	sort(P, P + N);
	h.clear();
	for (int i = 0; i < M; i ++){
		string S;
		cin>>S;
		h[S] ++;
	}
	int Ans1 = 0, Ans2 = 0, p1 = 0, p2 = N - 1;
	int tot = 0;
	for (iter = h.begin(); iter !=  h.end(); iter ++)
		A[tot ++] = (*iter).second;
	sort(A, A + tot);
	for (int i = tot - 1; i >= 0; i --){
		Ans1 += P[p1] * A[i];
		Ans2 += P[p2] * A[i];
		p1 ++; p2 --;
	}
	cout<<Ans1<<" "<<Ans2<<endl;
	return 0;
}


#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <map>

using namespace std;

int main()
{
	int n, m;
	int in;
	string f;
	cin >> n >> m;
	vector<int> v(n);
	map<string, int> ma;
	for(int i = 0; i < n; i++)
	{
		cin >> in;
		v[i] = in;
	}
	for(int i = 0; i < m; i++)
	{
		cin >> f;
		ma[f]++;
	}
	vector<int> count(m);
	map<string, int>::iterator it;
	int i;
	for(i = 0, it = ma.begin(); it != ma.end(); it++, i++)
		count[i] = it->second;

	sort(v.begin(), v.end());
	sort(count.begin(), count.end());
	int min = 0, max = 0;
	vector<int>::iterator v_it = v.begin();
	vector<int>::reverse_iterator r_it1 = count.rbegin(), r_it2 = v.rbegin();
	for( i = 0; i < (int)ma.size(); i++, r_it1++, v_it++)
		min += (*r_it1 * *v_it);
	for( i = 0, r_it1 = count.rbegin(); i < (int) ma.size(); i++, r_it1++, r_it2++)
		max += (*r_it1 * *r_it2);

	cout << min << " "  << max << endl;

}


#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <map>

using namespace std;

int main()
{
	int n, m;
	int in;
	string f;
	cin >> n >> m;
	vector<int> v(n);
	map<string, int> ma;
	for(int i = 0; i < n; i++)
	{
		cin >> in;
		v[i] = in;
	}
	for(int i = 0; i < m; i++)
	{
		cin >> f;
		ma[f]++;
	}
	vector<int> count(m);
	map<string, int>::iterator it;
	int i;
	for(i = 0, it = ma.begin(); it != ma.end(); it++, i++)
		count[i] = it->second;

	sort(v.begin(), v.end());
	sort(count.begin(), count.end());
	int min = 0, max = 0;
	vector<int>::iterator v_it = count.end();
	v_it--;
	for(i = 0; i < m; i++, v_it--)
		min += (*v_it * v[i]);
	v_it = count.end();
	v_it--;
	for(i = n-1; i >= n - m; i--, v_it--)
	{
		max += (*v_it * v[i]);
		cout << *v_it << " " << v[i] << endl;
	}

	cout << min << " "  << max << endl;

}


#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <map>

using namespace std;

int main()
{
	int n, m;
	int in;
	string f;
	cin >> n >> m;
	vector<int> v(n);
	map<string, int> ma;
	for(int i = 0; i < n; i++)
	{
		cin >> in;
		v[i] = in;
	}
	for(int i = 0; i < m; i++)
	{
		cin >> f;
		ma[f]++;
	}
	vector<int> count(m);
	map<string, int>::iterator it;
	int i;
	for(i = 0, it = ma.begin(); it != ma.end(); it++, i++)
		count[i] = it->second;

	sort(v.begin(), v.end());
	sort(count.begin(), count.end());
	int min = 0, max = 0;
	vector<int>::iterator v_it = count.end();
	v_it--;
	for(i = 0; i < m; i++, v_it--)
		min += (*v_it * v[i]);
	v_it = count.end();
	v_it--;
	for(i = n-1; i >= n - m; i--, v_it--)
		max += (*v_it * v[i]);

	cout << min << " "  << max << endl;

}


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

int main()
{
    map < string , int> ma;
    int n,m,i,j,k=0,mini=0,maxi=0,l;
    string str[105];
    int a[105];
    int price[105];
    cin >> n >> m;
    for(i=0;i<n;i++){
        scanf(" %d",&price[i]);
    }
    sort(price,price+n);
    for(i=0;i<m;i++){
        cin >> str[i];
        ma[str[i]]++;
        if(ma[str[i]]==1){
            a[k++]=1;
        }
        else{
            for(j=0;j<k;j++){
                if(str[i]==str[j]){
                    a[j]++;
                    break;
                }
            }
        }
    }
    sort(a,a+k);

  /*  for(i=0;i<n;i++){
        printf("%d\n",price[i]);
    }
    for(i=0;i<k;i++){
        printf("%d ",a[i]);
    } */
    j=0;
    l=n-1;
    for(i=k-1;i>=0;i--){
        mini=mini+(a[i]*price[j]);
        maxi=maxi+(a[i]*price[l]);
        j++;
        l--;
    }
    printf("%d %d\n",mini,maxi);

    return 0;
}


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

int main()
{
    map <string,int > ma;
    int n,m,i,j,k=0,mini=0,maxi=0,l;
    string str[105];
    string newstr[105];
    int a[105];
    int price[105];
    cin >> n >> m;
    for(i=0;i<n;i++){
        scanf(" %d",&price[i]);
    }
    sort(price,price+n);
    for(i=0;i<m;i++){
        cin >> str[i];
        ma[str[i]]++;
   //     cout << ma[str[i]] << endl;
        if(ma[str[i]]==1){
            newstr[k++]=str[i];
            a[k]=1;
        }
        else{
            for(j=0;j<k;j++){
                if(str[i]==newstr[j]){
                    a[j]=ma[str[i]];
               //     printf("a[j]=%d\nj=%d\n",a[j],j);
                    break;
                }
            }
        }
    }
    sort(a,a+k);

 /*   for(i=0;i<n;i++){
        printf("%d\n",price[i]);
    }
    for(i=0;i<k;i++){
        printf("%d ",a[i]);
    }
    cout << endl; */
    j=0;
    l=n-1;
    for(i=k-1;i>=0;i--){
        mini=mini+(a[i]*price[j]);
        maxi=maxi+(a[i]*price[l]);
        j++;
        l--;
    }
    printf("%d %d\n",mini,maxi);

    return 0;
}


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

int main()
{
    map <string,int > ma;
    int n,m,i,j,k=0,mini=0,maxi=0,l;
    string str[105];
    string newstr[105];
    int a[105];
    int price[105];
    cin >> n >> m;
    for(i=0;i<n;i++){
        scanf(" %d",&price[i]);
    }
    sort(price,price+n);
    for(i=0;i<m;i++){
        cin >> str[i];
        ma[str[i]]++;
     //   cout << ma[str[i]] << endl;
        if(ma[str[i]]==1){
            newstr[k]=str[i];
            a[k++]=1;
        }
        else{
            for(j=0;j<k;j++){
                if(str[i]==newstr[j]){
                    a[j]=ma[str[i]];
        //            printf("a[j]=%d\nj=%d\n",a[j],j);
                    break;
                }
            }
        }
    }
    sort(a,a+k);

  /*  for(i=0;i<n;i++){
        printf("%d\n",price[i]);
    }
    for(i=0;i<k;i++){
        printf("%d ",a[i]);
    }
    cout << endl; */
    j=0;
    l=n-1;
    for(i=k-1;i>=0;i--){
        mini=mini+(a[i]*price[j]);
        maxi=maxi+(a[i]*price[l]);
        j++;
        l--;
    }
    printf("%d %d\n",mini,maxi);

    return 0;
}


#include <bits/stdc++.h>
#define N 110
using namespace std;
int price[N];
map<string,int> v;
int s[N];
char tmp[N];
int n,m,no,ctoi;
int main()
{
    scanf("%d %d",&n,&m);
    for (int i=0;i<n;i++)
        scanf("%d",&price[i]);
    for (int i=0;i<N;i++)
        s[i] = 0;
    no = 1;
    for (int i=0;i<m;i++)
    {
        scanf("%s",tmp);
        ctoi = v[tmp];
        if (ctoi==0)
        {
            v[tmp] = no;
            ctoi = no;
            no++;
        }
        s[ctoi]++;
    }
    sort(s+1,s+no);
    sort(price,price+n);
    int max_price=0,min_price=0;
    for (int i=1;i<no;i++)
    {
        max_price += price[n-i]*s[no-i];
        min_price += price[i-1]*s[no-i];
    }
    printf("%d %d\n",min_price,max_price);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<vector>
#include<queue>
#include<algorithm>
#include<string>
#include<sstream>
#include<cctype>
#include<cmath>
#include<iomanip>
#include<map>
#include<stack>
#include<set>
#include<functional>
#define in(x) x=read()
#define qr read()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int read()
{
    char ch=' ';
    int f=1,x=0;
    while(!isdigit(ch)){ch=getchar();if(ch=='-')f*=-1;}
    while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return f*x;
}
int t[19260820]={0};
int ha(string s)
{
	int ans=0;
	for(int i=0;i<s.length();i++)
	{
		ans*=27;
		ans+=s[i]-'a';
		ans%=19260817;
	}
	return ans;
}
int a[1010],y[1010],now=0;
string s[1010];
int main()
{
    int n=qr,m=qr;
    for(int i=1;i<=n;i++)in(a[i]);
    for(int i=1;i<=m;i++)getline(cin,s[i]);
    for(int i=1;i<=m;i++)
    {
    	int g=ha(s[i]);
    	t[g]++;
    	if(t[g]==1)y[++now]=g;
	}
	sort(a+1,a+1+n);
//	sort(y+1,y+1+now);
	ll ans1=0,ans2=0;
	for(int i=1;i<=now;i++)
	{
		ans1+=a[i];
		ans2+=a[n-i+1];
	}
	cout<<ans1<<" "<<ans2;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,num1[101],num2[101],cnt=1,Max[101]={0};
string s[101],s1[101];
map<string,int>M;
int comp(const int &a,const int &b)
{
	return a>b;
}
int main()
{
	M.clear();
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>num1[i];num2[i]=num1[i];
	}
	for(int i=1;i<=m;i++)
	{
		cin>>s[i];
		M[s[i]]++;
		if(M[s[i]]==1)  s1[cnt]=s[i],cnt++;
	}
	for(int i=1;i<cnt;i++)  Max[i]=M[s1[i]];
	sort(num1+1,num1+n+1,comp);
	sort(num2+1,num2+n+1);
	sort(Max+1,Max+cnt,comp);
	int ans=0;
	for(int i=1;i<cnt;i++)  ans+=Max[i]*num2[i];
	cout<<ans<<' ';
	ans=0;
	for(int i=1;i<cnt;i++)  ans+=Max[i]*num1[i];
	cout<<ans;
	return 0;
}


#include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;
int main(){
    int n,m;
    cin>>n>>m;
    int price[n];
    vector<pair<int,string> >fruit;
    for(int i=0;i<n;++i){
         cin>>price[i];
    }
    for(int i=0;i<m;++i){
         string s;
         cin>>s;
         bool f =false;
         for(int j=0;j<fruit.size();++j){
              if(fruit[j].second==s){
                   fruit[j].first++;
                   f=true;
              }
              }
              if(!f)
                  {
                      fruit.push_back(make_pair(1,s));
                  }

         }
         sort(price,price+n);
         sort(fruit.begin(),fruit.end());
       //  reverse(fruit.begin(),fruit.end());
      /*   for(int i=0;i<n;++i){
             cout<<price[i]<<" ";
         }
         cout<<endl;
         for(int i=0;i<fruit.size();++i)
             cout<<fruit[i].first<<" ";
         cout<<endl;*/
         int maxprice=0;
         for(int i=0;i<fruit.size();++i){
              maxprice+=fruit[i].first*price[fruit.size()-1-i];
              //cout<<fruit[i].first<<" "<<price[i]<<endl;
         }
         reverse(price,price+n);
         int minprice=0;
         for(int i=0;i<fruit.size();++i){
              minprice+=fruit[i].first*price[fruit.size()-1-i];
         }
         cout<<maxprice<<" "<<minprice;
         return 0;
}


#include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;
int main(){
    int n,m;
    cin>>n>>m;
    int price[n];
    vector<pair<int,string> >fruit;
    for(int i=0;i<n;++i){
         cin>>price[i];
    }
    for(int i=0;i<m;++i){
         string s;
         cin>>s;
         bool f =false;
         for(int j=0;j<fruit.size();++j){
              if(fruit[j].second==s){
                   fruit[j].first++;
                   f=true;
              }
              }
              if(!f)
                  {
                      fruit.push_back(make_pair(1,s));
                  }

         }
         sort(price,price+n);
         reverse(fruit.begin(),fruit.end());
       /*  for(int i=0;i<n;++i){
             cout<<price[i]<<" ";
         }
         cout<<endl;
         for(int i=0;i<fruit.size();++i)
             cout<<fruit[i].first<<" ";
         cout<<endl;*/
         int maxprice=0;
         for(int i=0;i<fruit.size();++i){
              maxprice+=fruit[i].first*price[fruit.size()-1-i];
            //  cout<<fruit[i].first<<" "<<price[i]<<endl;
         }
         reverse(price,price+n);
         int minprice=0;
         for(int i=0;i<fruit.size();++i){
              minprice+=fruit[i].first*price[fruit.size()-1-i];
         }
         cout<<maxprice<<" "<<minprice;
         return 0;
}


#include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;
int main(){
    int n,m;
    cin>>n>>m;
    int price[n];
    vector<pair<int,string> >fruit;
    for(int i=0;i<n;++i){
         cin>>price[i];
    }
    for(int i=0;i<m;++i){
         string s;
         cin>>s;
         bool f =false;
         for(int j=0;j<fruit.size();++j){
              if(fruit[j].second==s){
                   fruit[j].first++;
                   f=true;
              }
              }
              if(!f)
                  {
                      fruit.push_back(make_pair(1,s));
                  }

         }
         sort(price,price+n);
         reverse(fruit.begin(),fruit.end());
         /*for(int i=0;i<n;++i){
             cout<<price[i]<<" ";
         }
         cout<<endl;
         for(int i=0;i<fruit.size();++i)
             cout<<fruit[i].first<<" ";
         cout<<endl;*/
         int maxprice=0;
         for(int i=0;i<fruit.size();++i){
              maxprice+=fruit[i].first*price[i];
         }
         reverse(price,price+n);
         int minprice=0;
         for(int i=0;i<fruit.size();++i){
              minprice+=fruit[i].first*price[i];
         }
         cout<<maxprice<<" "<<minprice;
         return 0;
}


#include <iostream>
#include <string.h>
#include <algorithm>
#include <deque>
#include <set>
#include <map>
using namespace std;
map <string,int> p;
int max_ele(set <string> f)
{
	set <string> :: iterator it=f.begin();
	int maxi=0;
	string fruit=*it;
	for (it=f.begin();it!=f.end();it++)
	{
		if (p[*it]>maxi)
		{
			maxi=p[*it];
			fruit=*it;
		}
	}
	p[fruit]=0;
	return maxi;
}
int main()
{
    int n,m,i,a,ans1=0,ans2=0;
    cin >> n >> m;
    deque <int> p1(n),p2(n);
    set <string> f;
    string s;
    for (i=0;i<n;i++)
    cin >> p1[i];
    p2=p1;
    sort(p1.begin(),p1.end());
    sort (p2.begin(),p2.end());
    reverse(p2.begin(),p2.end());
    for (i=0;i<m;i++)
    {
    	cin >> s;
    	f.insert(s);
    	p[s]++;
	}
	while (a)
	{
		a=max_ele(f);
		ans2+=a*p1.back();
		ans1+=a*p2.back();
		p1.pop_back();
		p2.pop_back();
	}
	cout << ans1 << " " << ans2;
}


for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    print(a.sort())
    print(b.sort())


def check(arrF,arrS):
    sum = []
    for p in range(arrSize):
        sum.append(arrS[p] + arrF[p])

    d = {}
    for t in sum:
        if t in d:
            return True
        else:
            d[t] = 1
    return False

test = int(input())
for i in range(test):
    arrSize = int(input())

    arrF = list(map(int, input().split(" ")))
    arrS = list(map(int, input().split(" ")))

    if(check(arrF,arrS)):
        arrF.sort()
    elif(check(arrF,arrS)):
        arrS.sort()

    for k in arrF:
        print(k,end =" ")
    print()
    for k in arrS:
        print(k,end =" ")
    print()


test = int(input())
sum = []
for i in range(test):
    arrSize = int(input())

    arrF = list(map(int, input().split(" ")))
    arrS = list(map(int, input().split(" ")))

    for p in range(arrSize):
        sum.append(arrS[p] + arrF[p])

    d = {}
    find = False
    index = []

    for h in range(arrSize):
        if sum[h] in d:
            find = True
            index.append(h)
        else:
            d[sum[h]] = 1

    if find:
        arrF.sort()

    for p in range(arrSize):
        sum.append(arrS[p] + arrF[p])

    d = {}
    find = False
    index = []

    for h in range(arrSize):
        if sum[h] in d:
            find = True
            index.append(h)
        else:
            d[sum[h]] = 1

    if find:
        arrS.sort()

    for i in arrF:
        print(i, end=" ")
    print()
    for i in arrS:
        print(i, end=" ")
    print()


test = int(input())

for i in range(test):
    arrSize = int(input())

    arrF = list(map(int,input().split(" ")))
    arrS = list(map(int, input().split(" ")))

    arrF.sort()
    arrS.sort(reverse=True)

    for i in arrF:
        print(i,end=" ")
    print("\n")
    for i in arrS:
        print(i,end=" ")


test = int(input())
sum = []
for i in range(test):
    arrSize = int(input())

    arrF = list(map(int, input().split(" ")))
    arrS = list(map(int, input().split(" ")))

    for p in range(arrSize):
        sum.append(arrS[p] + arrF[p])

    d = {}
    find = False
    index = []

    for h in range(arrSize):
        if sum[h] in d:
            find = True
            index.append(h)
        else:
            d[sum[h]] = 1

    if find:
        arrF.sort()

    for i in arrF:
        print(i, end=" ")
    print()
    for i in arrS:
        print(i, end=" ")
    print()


def check(arrF,arrS):
    sum = []
    for p in range(arrSize):
        sum.append(arrS[p] + arrF[p])

    d = {}
    for t in sum:
        if t in d:
            return True
        else:
            d[t] = 1
    return False

test = int(input())
for i in range(test):
    arrSize = int(input())

    arrF = list(map(int, input().split(" ")))
    arrS = list(map(int, input().split(" ")))

    if(check(arrF,arrS)):
        arrF.sort()
    elif(check(arrF,arrS)):
        arrS.sort()
    elif(check(arrF,arrS)):
        arrS.sort(reverse=True)
    elif(check(arrF,arrS)):
        arrF.sort(reverse=True)

    for k in arrF:
        print(k,end =" ")
    print()
    for k in arrS:
        print(k,end =" ")
    print()


test = int(input())

for i in range(test):
    arrSize = int(input())

    arrF = list(map(int,input().split(" ")))
    arrS = list(map(int, input().split(" ")))

    sum = arrF + arrS

    d = {}

    index = []
    find = False
    for i in sum:
        if i in d:
            index.append(i)
            find = True
        else:
            d[i]=1

    if find:
        arrS.sort()
        arrF.sort()
    for i in arrF:
        print(i,end = " ")
    print()
    for i in arrS:
        print(i,end = " ")
    print()


test = int(input())
sum = []
for i in range(test):
    arrSize = int(input())

    arrF = list(map(int, input().split(" ")))
    arrS = list(map(int, input().split(" ")))

    for p in range(arrSize):
        sum.append(arrS[p] + arrF[p])

    d = {}
    find = False
    index = []

    for h in range(arrSize):
        if sum[h] in d:
            find = True
            index.append(h)
        else:
            d[sum[h]] = 1

    if find:
        for k in index:

            if k+1 < arrSize:
                indexA = k
                indexB = k + 1
            else:
                indexA = k
                indexB = 0

            temp = arrF[indexA]
            arrF[indexA] = arrF[indexB]
            arrF[indexB] = temp

    for i in arrF:
        print(i, end=" ")
    print()
    for i in arrS:
        print(i, end=" ")
    print()


test = int(input())
for i in range(test):
    arrSize = int(input())

    arrF = list(map(int, input().split(" ")))
    arrS = list(map(int, input().split(" ")))

    arrF.sort()
    arrS.sort()

    for k in arrF:
        print(k,end =" ")
    print()
    for k in arrS:
        print(k,end =" ")
    print()


test = int(input())

for i in range(test):
    arrSize = int(input())

    arrF = list(map(int,input().split(" ")))
    arrS = list(map(int, input().split(" ")))

    sum = arrF + arrS

    d = {}

    index = []
    find = False
    for i in sum:
        if i in d:
            index.append(i)
            find = True
        else:
            d[i]=1

    if find:
        arrS.sort()
    for i in arrF:
        print(i,end = " ")
    print()
    for i in arrS:
        print(i,end = " ")


test = int(input())

for i in range(test):
    arrSize = int(input())

    arrF = list(map(int,input().split(" ")))
    arrS = list(map(int, input().split(" ")))

    sum = arrF + arrS

    d = {}

    index = []
    find = False
    for i in sum:
        if i in d:
            index.append(i)
            find = True
        else:
            d[i]=1

    if find:
        arrS.sort()
    for i in arrF:
        print(i,end = " ")
    print()
    for i in arrS:
        print(i,end = " ")
    print()


test = int(input())

for i in range(test):
    arrSize = int(input())

    arrF = list(map(int,input().split(" ")))
    arrS = list(map(int, input().split(" ")))

    arrF.sort()
    arrS.sort(reverse=True)

    for i in arrF:
        print(i,end=" ")
    print()
    for i in arrS:
        print(i,end=" ")


for _ in range(int(input())):
    x=int(input())
    s=list(map(int,input().split()))
    z=sorted(list(map(int,input().split())))
    s.sort()
    s.reverse()
    print(*s)
    print(*z)


for _ in range(int(input())):
    x=int(input())
    s=list(map(int,input().split()))
    z=sorted(list(map(int,input().split())))
    s.sort()
    print(*s)
    print(*z)


for _ in range(int(input())):
    x=int(input())
    a=sorted(list(map(int,input().split())))
    b=sorted(list(map(int,input().split())))
    print(*a);print(*b)


#akash mandal: jalpaiguri government engineering college
#never give up: take a look on special cases: test cases

def ii(): return int(input())
def mii(): return map(int,input().split())
def lmii(): return list(mii())

for _ in range(ii()):
    x=ii()
    a=sorted(lmii())
    b=sorted(lmii())
    print(*a);print(*b)


### A. Kuroni and the Gifts
for _ in range(int(input())):
    n=int(input())
    a=[int(x) for x in input().split()]
    b=[int(y) for y in input().split()]
    a=set(a)
    b=set(b)
    print(*a)
    print(*b)


### A. Kuroni and the Gifts
for _ in range(int(input())):
    n=int(input())
    a=[int(x) for x in input().split()]
    b=[int(y) for y in input().split()]
    a=sorted(a)
    b=sorted(b)
    print(*a)
    print(*b)


t = int(input())

def solve(n,a,b):
	P = [[0] * n for _ in range(n)]
	C = []
	for i in range(n):
		for j in range(n):
			P[i][j] = a[i]+b[j]

	x = []
	y = []

	used = []
	flga = [0] * n
	flgb = [0] * n
	for i in range(n):
		cond = 0
		for j in range(n):
			if P[i][j] not in used:
				if flga[i] == 0 and flgb[j] == 0:
					x.append(a[i])
					y.append(b[j])
					flga[i] = 1
					flgb[j] = 1
					cond = 1
					break
		if cond:
			continue

	return [x,y]

ans = []
for _ in range(t):
	tn = int(input())
	ta = list(input().split())
	tb = list(input().split())

	ans.append(solve(tn,ta,tb))

for lx,ly in ans:
	print(*lx)
	print(*ly)


t = int(input())
ans = []
for _ in range(t):
	tn = int(input())
	ta = list(map(int, input().split()))
	tb = list(map(int, input().split()))

	ta.sort()
	tb.sort()

	ans.append([ta,tb])

for lx,ly in ans:
	print(*lx)
	print(*ly)


n,m=map(int,input().split())
a=list(map(int,input().split()))
sum=1
for i in range(n-1):
	for j in range(i+1,n):
		sum=sum*abs(a[i]-a[j])
print(sum%m)


for  _ in range(int(input())):
	n=int(input())
	a=list(map(int,input().split()))
	b=list(map(int,input().split()))
	a.sort()
	b.sort()
	print(*a)
	print(*b)


t = int(input())
for _ in range(t):
    n = int(input())
    l1 = list(map(int,input().split()))
    l2 = list(map(int,input().split()))
    l1.sort()
    l2.sort()
    print(l1)
    print(l2)


t = int(input())
for _ in range(t):
    n = int(input())
    l1 = list(map(int,input().split()))
    l2 = list(map(int,input().split()))
    l1.sort()
    l2.sort()
    print(*l1)
    print(*l2)


def merge(array,start,pointer,end):
	n1 = pointer - start + 1
	n2 = end-pointer
	left = array[start:pointer+1]
	right = array[pointer+1:end+1]
	i = j = 0
	k = start

	while i < n1 and j < n2 :
		if left[i] <= right[j]:
			array[k] = left[i]
			i += 1
		else:
			array[k] = right[j]
			j += 1
		k+=1

	while i < n1:
		array[k] = left[i]
		i+=1
		k+=1

	while j < n2:
		array[k] = right[j]
		j+=1
		k+=1

def merge_sort(array):
	merge_sort2(array,0,len(array)-1)

def merge_sort2(array,start,end):
	if start < end:
		m = (start+end)//2
		merge_sort2(array,start,m)
		merge_sort2(array,m+1,end)
		merge(array,start,m,end)

def findnums(arr1,arr2):
	suma = []
	j = newsum = 0

	while j < len(arr1):
		newsum = arr1[j] + arr2[j]
		if newsum not in suma:
			suma.append(newsum)
			j+=1
		else:
			for i in range(j+1,len(arr1)):
				newsum = arr1[j] + arr2[i]
				if newsum not in suma:
					suma.append(newsum)
					arr2[i],arr2[j] = arr2[j],arr2[i]
					break
				newsum = arr1[i] + arr2[j]
				if newsum not in suma:
					suma.append(newsum)
					arr1[i],arr1[j] = arr1[j],arr1[i]
					j+=1
					break
	return arr1,arr2

n = int(input())
outputs = []
for _ in range(n):
	r = int(input())
	a = list(map(int,input().split()))
	b = list(map(int,input().split()))

	merge_sort(a)
	merge_sort(b)

	r1,r2 = findnums(a,b)
	outputs.append(r1)
	outputs.append(r2)

for z in outputs:
	print(*z)


def insertion_sort(array):
	for j in range(1,len(array)):
		key = array[j]
		i = j - 1
		while i >= 0 and array[i] > key:
			array[i+1] = array[i]
			i -= 1
		array[i+1] = key

def findnums(sarr1,sarr2):
	r = len(sarr1) - 1
	new_sum = 0
	sums = []
	for i in range(r):
		for j in reversed(range(i,r)):
			new_sum = sarr1[i] + sarr2[j]
			if new_sum not in sums:
				sums.append(new_sum)
				new_sum = 0
				sarr2[j],sarr2[i] = sarr2[i],sarr2[j]
	return sarr1,sarr2

n = int(input())
outputs = []
for _ in range(n):
	r = int(input())
	a = list(map(int,input().split()))
	b = list(map(int,input().split()))
	insertion_sort(a)
	insertion_sort(b)
	r1,r2 = findnums(a,b)
	outputs.append(r1)
	outputs.append(r2)

for z in outputs:
	print(*z)


def findnums(sarr1,sarr2):
	r = len(sarr1) - 1
	new_sum = 0
	sums = []
	for i in range(r):
		new_sum = sarr1[i]
		for j in reversed(range(i,r)):
			new_sum += sarr2[j]
			if new_sum not in sums:
				sums.append(new_sum)
				new_sum = 0
				sarr2[j],sarr2[i] = sarr2[i],sarr2[j]
				break
	return sarr1,sarr2

def findnums2(arr1,arr2):
	r = len(arr1) - 1
	i = new_sum = 0
	j = r
	sums = []
	while i < r:

		if j < i:
			i += 1
			j = r

		new_sum = arr1[i] + arr2[j]

		if new_sum not in sums:
			sums.append(new_sum)
			new_sum = 0
			arr2[j],arr2[i] = arr2[i], arr2[j]
			i+=1
			j = r + 1
		j-=1
	return arr1,arr2

n = int(input())
outputs = []
for _ in range(n):
	r = int(input())
	a = list(map(int,input().split()))
	b = list(map(int,input().split()))
	r1,r2 = findnums(a,b)
	outputs.append(r1)
	outputs.append(r2)

for z in outputs:
	print(*z)


def findnums(sarr1,sarr2):
	r = len(sarr1) - 1
	new_sum = 0
	sums = []
	for i in range(r):
		for j in reversed(range(i,r)):
			new_sum = sarr1[i] + sarr2[j]
			if new_sum not in sums:
				sums.append(new_sum)
				new_sum = 0
				sarr2[j],sarr2[i] = sarr2[i],sarr2[j]
	return sarr1,sarr2

def findnums2(arr1,arr2):
	r = len(arr1) - 1
	i = new_sum = 0
	j = r
	sums = []
	while i < r:

		if j < i:
			i += 1
			j = r

		new_sum = arr1[i] + arr2[j]

		if new_sum not in sums:
			sums.append(new_sum)
			new_sum = 0
			arr2[j],arr2[i] = arr2[i], arr2[j]
		j-=1
	return arr1,arr2

n = int(input())
outputs = []
for _ in range(n):
	r = int(input())
	a = list(map(int,input().split()))
	b = list(map(int,input().split()))
	r1,r2 = findnums2(a,b)
	outputs.append(r1)
	outputs.append(r2)

for z in outputs:
	print(*z)


def merge(array,start,pointer,end):
	n1 = pointer - start + 1
	n2 = end-pointer
	left = array[start:pointer+1]
	right = array[pointer+1:end+1]
	i = j = 0
	k = start

	while i < n1 and j < n2 :
		if left[i] <= right[j]:
			array[k] = left[i]
			i += 1
		else:
			array[k] = right[j]
			j += 1
		k+=1

	while i < n1:
		array[k] = left[i]
		i+=1
		k+=1

	while j < n2:
		array[k] = right[j]
		j+=1
		k+=1

def merge_sort(array):
	merge_sort2(array,0,len(array)-1)

def merge_sort2(array,start,end):
	if start < end:
		m = (start+end)//2
		merge_sort2(array,start,m)
		merge_sort2(array,m+1,end)
		merge(array,start,m,end)

def findnums(arr1,arr2):
	suma = []
	j = newsum = 0

	while j < len(arr1):
		newsum = arr1[j] + arr2[j]
		if newsum not in suma:
			suma.append(newsum)
			j+=1
		else:
			for i in range(j+1,len(arr1)):
				newsum = arr1[j] + arr2[i]
				if newsum not in suma:
					suma.append(newsum)
					arr2[i],arr2[j] = arr2[j],arr2[i]
					break
				newsum = arr1[i] + arr2[j]
				if newsum not in suma:
					suma.append(newsum)
					arr1[i],arr1[j] = arr1[j],arr1[i]
					j+=1
					break
	return arr1,arr2

n = int(input())
outputs = []
for _ in range(n):
	r = int(input())
	a = list(map(int,input().split()))
	b = list(map(int,input().split()))

	merge_sort(a)
	merge_sort(b)

	print(*a)
	print(*b)


value = int(input())

def function(x):
	a = x
	b = 1
	while True:
		if a == 1:
			return -1

		if a*b > x:
			if a%b == 0:
				return a,b
			else:
				a-=1
		else:
			b+=1

result = function(value)
if type(result) is int:
	print(result)
else:
	print(*result)

'''
def findnums2(arr1,arr2):
	r = len(arr1) - 1
	i = new_sum = 0
	j = r
	sums = []
	while True:
		if i > r:
			break
		new_sum = sarr1[i]+sarr2[j]
		if new_sum not in sums:
			sums.append(new_sum)
			new_sum = 0
			sarr2[j],sarr2[i] = sarr2[i], sarr2[j]
			j+=1

		new_sum = 0
		new_sum = sarr1[j] + sarr2[i]
'''


def insertion_sort(array):
	for j in range(1,len(array)):
		key = array[j]
		i = j - 1
		while i >= 0 and array[i] > key:
			array[i+1] = array[i]
			i -= 1
		array[i+1] = key

def findnums(sarr1,sarr2):
	r = len(sarr1) - 1
	new_sum = 0
	sums = []
	for i in range(r):
		for j in reversed(range(i,r)):
			new_sum = sarr1[i] + sarr2[j]
			if new_sum not in sums:
				sums.append(new_sum)
				new_sum = 0
				sarr2[j],sarr2[i] = sarr2[i],sarr2[j]
	return sarr1,sarr2

n = int(input())
outputs = []
for _ in range(n):
	r = int(input())
	a = list(map(int,input().split()))
	b = list(map(int,input().split()))
	r1,r2 = findnums(a,b)
	outputs.append(r1)
	outputs.append(r2)

for z in outputs:
	print(*z)


def findnums(sarr1,sarr2):
	r = len(sarr1) - 1
	new_sum = 0
	sums = []
	for i in range(r):
		for j in reversed(range(i,r)):
			new_sum = sarr1[i] + sarr2[j]
			if new_sum not in sums:
				sums.append(new_sum)
				new_sum = 0
				sarr2[j],sarr2[i] = sarr2[i],sarr2[j]
	return sarr1,sarr2

def findnums2(arr1,arr2):
	r = len(arr1) - 1
	i = new_sum = 0
	j = r
	sums = []
	while i < r:

		if j < i:
			i += 1
			j = r

		new_sum = arr1[i] + arr2[j]

		if new_sum not in sums:
			sums.append(new_sum)
			new_sum = 0
			arr2[j],arr2[i] = arr2[i], arr2[j]
			i+=1
			j = r + 1

		j-=1
	return arr1,arr2

n = int(input())
outputs = []
for _ in range(n):
	r = int(input())
	a = list(map(int,input().split()))
	b = list(map(int,input().split()))
	r1,r2 = findnums2(a,b)
	outputs.append(r1)
	outputs.append(r2)

for z in outputs:
	print(*z)


def findnums(sarr1,sarr2):
	r = len(sarr1) - 1
	new_sum = 0
	sums = []
	for i in range(r):
		for j in reversed(range(i,r)):
			new_sum = sarr1[i] + sarr2[j]
			if new_sum not in sums:
				sums.append(new_sum)
				new_sum = 0
				sarr2[j],sarr2[i] = sarr2[i],sarr2[j]
	return sarr1,sarr2

def findnums2(arr1,arr2):
	r = len(arr1) - 1
	i = new_sum = 0
	j = r
	sums = []
	while i < r:

		if j < i:
			i += 1
			j = r

		new_sum = arr1[i] + arr2[j]

		if new_sum not in sums:
			sums.append(new_sum)
			new_sum = 0
			arr2[j],arr2[i] = arr2[i], arr2[j]
			i+=1
			j = r
		j-=1
	return arr1,arr2

n = int(input())
outputs = []
for _ in range(n):
	r = int(input())
	a = list(map(int,input().split()))
	b = list(map(int,input().split()))
	r1,r2 = findnums2(a,b)
	outputs.append(r1)
	outputs.append(r2)

for z in outputs:
	print(*z)


def findnums(sarr1,sarr2):
	r = len(sarr1) - 1
	new_sum = 0
	sums = []
	for i in range(r):
		for j in reversed(range(i,r)):
			new_sum = sarr1[i] + sarr2[j]
			if new_sum not in sums:
				sums.append(new_sum)
				new_sum = 0
				sarr2[j],sarr2[i] = sarr2[i],sarr2[j]
	return sarr1,sarr2

def findnums2(arr1,arr2):
	r = len(arr1) - 1
	i = new_sum = 0
	j = r
	sums = []
	while i < r:

		if j < i:
			i += 1
			j = r

		new_sum = arr1[i] + arr2[j]

		if new_sum not in sums:
			sums.append(new_sum)
			new_sum = 0
			arr2[j],arr2[i] = arr2[i], arr2[j]
			i+=1

		j-=1
	return arr1,arr2

n = int(input())
outputs = []
for _ in range(n):
	r = int(input())
	a = list(map(int,input().split()))
	b = list(map(int,input().split()))
	r1,r2 = findnums2(a,b)
	outputs.append(r1)
	outputs.append(r2)

for z in outputs:
	print(*z)


a=int(input())

y=[]
for i in range(a):
  x=[]
  b=int(input())
  c=list(map(int, input().split()))
  d=list(map(int, input().split()))
  for i in range(b):
    x.append(c[i]+d[i])

  if set(x)==b:
    print(*c)
    print(*d)
  else:
    print(*sorted(c))
    print(*d)


a=int(input())

y=[]
for i in range(a):
  x=[]
  b=int(input())
  c=list(map(int, input().split()))
  d=list(map(int, input().split()))
  for i in range(b):
    x.append(c[i]+d[i])

  if len(set(x))==b:
    print(*c)
    print(*d)
  else:
    print(*sorted(c))
    print(*d)


a=int(input())

y=[]
for i in range(a):
  x=[]
  b=int(input())
  c=sorted(list(map(int, input().split())))
  d=sorted(list(map(int, input().split())))
  print(*c)
  print(*d)


import os
import sys
from io import BytesIO, IOBase

def main():
    pass

# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

def test():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    print(*sorted(a))
    print(*sorted(b))

t = int(input())
for _ in range(t):
    test()


import os
import sys
from io import BytesIO, IOBase

def main():
    pass

# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

def test():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    print(*a)
    print(*b)

t = int(input())
for _ in range(t):
    test()


import os
import sys
from io import BytesIO, IOBase

def main():
    pass

# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

def test():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    for i in range(n):
        s = a[i] + b[i]
        for j in range(1, n):
            if a[j] + b[j] == s:
                a[j], a[i] = a[i], a[j]

    print(*a)
    print(*b)

t = int(input())
for _ in range(t):
    test()


import os
import sys
from io import BytesIO, IOBase

def main():
    pass

# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

def test():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    for i in range(n):
        s = a[i] + b[i]
        for j in range(i + 1, n):
            if a[j] + b[j] == s:
                # if a[i] == b[j]:
                #     a[j], b[i] = b[i], a[j]
                # else:
                a[j], a[i] = a[i], a[j]
                break

    print(*a)
    print(*b)

t = int(input())
for _ in range(t):
    test()


import os
import sys
from io import BytesIO, IOBase

def main():
    pass

# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

def test():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    for i in range(n):
        s = a[i] + b[i]
        for j in range(i + 1, n):
            if a[j] + b[j] == s:
                # if a[i] == b[j]:
                #     a[j], b[i] = b[i], a[j]
                # else:
                a[j], a[i] = a[i], a[j]

    print(*a)
    print(*b)

t = int(input())
for _ in range(t):
    test()


for _ in range(int(input())):
    input()
    data1 = sorted(map(int, input().split()))
    data2 = sorted(map(int, input().split()))
    result = [str(v1 + v2) for v1, v2 in zip(data1, data2)]
    print(" ".join(result))


for _ in range(int(input())):
    input()
    for _ in range(2):
        print(" ".join(map(str, sorted(map(int, input().split())))))


n=int(input())
while (n):
    nr=int(input())
    a=input().split()
    b=input().split()
    a.sort()
    b.sort()
    for x in a:
        print (x,end=' ')
    print()
    for x in b:
        print(x,end=' ')
    n-=1


n=int(input())
while(n):
    nr=int(input())
    a=input().split()
    b=input().split()
    for i in range(len(a)):
        a[i]=int(a[i])
        b[i]=int(b[i])
    a.sort()
    b.sort()
    for i in range(len(a)):
        print(a[i],end=' ')
    print()
    for i in range(nr):
        print(b[i],end=' ')
    n-=1


T = int(input())
for _ in range(T):
	n = int(input())
	A = sorted(list(map(int, input().split())))
	B = sorted(list(map(int, input().split())))

	print(*A)
	print(*B)


T = int(input())
for _ in range(T):
	n = int(input())
	A = sorted(list(map(int, input().split())))
	B = sorted(list(map(int, input().split())))

	print('{}\n{}'.format(*A, *B))


T = int(input())
for _ in range(T):
	n = int(input())
	A = sorted(list(map(int, input().split())))
	B = sorted(list(map(int, input().split())))

	print(' '.join([str(x) for x in A]))
	print(' '.join([str(x) for x in B]))


t=int(input())
for _ in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    a.sort()
    b.sort()
    for i in a:
        print(i,end=" ")
    print()
    for i in b:
        print(i,end=" ")
    print()


t=int(input())
for _ in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    a.sort()
    b.sort()
    print(a)
    print(b)


for _ in range(int(input())):
    n=int(input())
    l1=list(map(int,input().split()))
    l2=list(map(int,input().split()))
    l1.sort()
    l2.sort()
    for i in range(n):
          print(l1[i],end=' ')
    print(end='\n')
    for i in range(n):
        print(l2[i],end=' ')
    print(end='\n')


for _ in range(int(input())):
    n=int(input())
    l1=list(map(int,input().split()))
    l2=list(map(int,input().split()))
    l1.sort()
    l2.sort()
    for i in range(n):
        print(l1[i]+l2[i],end=" ")
    print(end='\n')


for _ in range(int(input())):
    n=int(input())
    lst1=[int(i) for i in input().split()]
    lst2=[int(i) for i in input().split()]
    lst1.sort()
    lst2.sort()
    for i in range(0,n):
        print(lst1[i]+lst2[i],end=' ')
    print()


for _ in range(int(input())):
    n=int(input())
    lst1=[int(i) for i in input().split()]
    lst2=[int(i) for i in input().split()]
    lst1.sort()
    lst2.sort()
    for i in range(0,n):
        print(lst1[i],end=' ')
    print()
    for i in range(0,n):
        print(lst2[i],end=' ')
    print()