/*
   ~~~ BISMILLAH HIR RAHMANIR RAHIM ~~~
   Problem Name:
       Platform:
     Algorithom:
  Author's Name: RC Tushar

                 শুনেছি তোমার ভীষণ রাগ ?
                              সে কি রাগ , নাকি
                                     মগজ কোণে পচন ধরা
                 পুরনো কোডের বাগ ?

 ~~~     Never Lost Hope     ~~~
*/
#include<bits/stdc++.h>
#define pi 2*acos(0.0)
#define PS system("pause")
#define siter(n,T1)  for(set<T1>::iterator it=n.begin();it!=n.end();it++)
#define miter(n,T1,T2)  for(map<T1,T2>::iterator it=n.begin();it!=n.end();it++)
#define meter(n,T1,T2)  for(map<T1,T2>::iterator it=n.end();it!=n.begin();it--)
#define FOR(s,e,inc) for(int i=s;i<=e;i+=inc)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define Sfor(n) for(int i=1;i<=n;i++)
#define inf (1<<30)
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define sz(x) ((int)x.size())
#define eps 1e-9
#define gcd(x,y) __gcd(x,y)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define on(x,w)  x=x|(1<<w)
#define check(x,w) (x&(1<<w))==(1<<w)?true:false
#define all(x) (x).begin(),(x).end()
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define pf printf
#define ll long long int
#define ull unsigned long long int
#define MOD 1000000007
#define sqr(x) (( (x)* (x))%MOD)
#define cube(x)   ( (sqr(x)*(x))%MOD)
#define bit_cnt(x)   __builtin_popcount(x)
#define INT(c)  ((int)((c) - '0'))
#define CHAR(i) ((char)(i + int('0')))
#define maxall(v) *max_element(all(v))
#define minall(v) *min_element(all(v))
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define btz(a)   __builtin_ctz(a)
#define Mems(p,n) memset(p, n, sizeof(p))
#define makeint(n,s)  istringstream(s)>>n
#define BOUNDARY(i,j,Row,Col) ((i >= 0 && i < Row) && (j >= 0 && j < Col))
#define ubound(d,val)  int (upper_bound (d.begin(), d.end(), val) - d.begin())
#define lbound(d,val)  int (lower_bound (d.begin(), d.end(), val) - d.begin())
#define M 100003
using namespace std;

//int dx[]= {1,0,-1,0};
//int dy[]= {0,1,0,-1};                        //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};     //8 direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};   //Knight Direction
//int dx[]={2,1,-1,-2,-1,1};int dy[]={0,1,1,0,-1,-1};              //Hexagonal Direction
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);

typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ll,bool> pib;
typedef pair<double,int> pdi;
typedef pair<int,char> pic;
typedef pair< int, pii> ipii;

//FAST_INPUT
template<class T>
inline bool fs(T &x)
{
    int c=getchar();
    int sgn=1;
    while(~c&&c<'0'||c>'9')
    {
        if(c=='-')sgn=-1;
        c=getchar();
    }
    for(x=0; ~c&&'0'<=c&&c<='9'; c=getchar())
        x=x*10+c-'0';
    x*=sgn;
    return ~c;
}

map<ll ,ll> ara;
ll mara[M+5];
int main()
{
    ll k,n,x;
    fs(n);
    fs(k);

    loop(i,0,n)
    {
        fs(x);
        mara[i]=x;
        ara[x]=1;
    }
    sort(mara,mara+n);

    for(int i=n-1;i>=0;i--)
    {
        if(!ara[mara[i]*k])
            ara[mara[i]]=2;
        else
            {
                ara[mara[i]]=0;
                break;
            }
    }

    for(int i=0;i<n;i++)
    {
        if(ara[mara[i]])
        {
            if(ara[mara[i]*k]==1)
            ara[mara[i]*k]=0;
            if(ara[mara[i]*k]==2)
            {
                ara[mara[i]]=0;
                break;
            }
        }
    }

    int sum=0;

    for(int i=0;i<n;i++)
    {
        if(ara[mara[i]]!=0)
        sum++;
    }

    cout<<sum<<endl;
    return 0;
}


/*
   ~~~ BISMILLAH HIR RAHMANIR RAHIM ~~~
   Problem Name:
       Platform:
     Algorithom:
  Author's Name: RC Tushar

                 শুনেছি তোমার ভীষণ রাগ ?
                              সে কি রাগ , নাকি
                                     মগজ কোণে পচন ধরা
                 পুরনো কোডের বাগ ?

 ~~~     Never Lost Hope     ~~~
*/
#include<bits/stdc++.h>
#define pi 2*acos(0.0)
#define PS system("pause")
#define siter(n,T1)  for(set<T1>::iterator it=n.begin();it!=n.end();it++)
#define miter(n,T1,T2)  for(map<T1,T2>::iterator it=n.begin();it!=n.end();it++)
#define meter(n,T1,T2)  for(map<T1,T2>::iterator it=n.end();it!=n.begin();it--)
#define FOR(s,e,inc) for(int i=s;i<=e;i+=inc)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define Sfor(n) for(int i=1;i<=n;i++)
#define inf (1<<30)
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define sz(x) ((int)x.size())
#define eps 1e-9
#define gcd(x,y) __gcd(x,y)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define on(x,w)  x=x|(1<<w)
#define check(x,w) (x&(1<<w))==(1<<w)?true:false
#define all(x) (x).begin(),(x).end()
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define pf printf
#define ll long long int
#define ull unsigned long long int
#define MOD 1000000007
#define sqr(x) (( (x)* (x))%MOD)
#define cube(x)   ( (sqr(x)*(x))%MOD)
#define bit_cnt(x)   __builtin_popcount(x)
#define INT(c)  ((int)((c) - '0'))
#define CHAR(i) ((char)(i + int('0')))
#define maxall(v) *max_element(all(v))
#define minall(v) *min_element(all(v))
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define btz(a)   __builtin_ctz(a)
#define Mems(p,n) memset(p, n, sizeof(p))
#define makeint(n,s)  istringstream(s)>>n
#define BOUNDARY(i,j,Row,Col) ((i >= 0 && i < Row) && (j >= 0 && j < Col))
#define ubound(d,val)  int (upper_bound (d.begin(), d.end(), val) - d.begin())
#define lbound(d,val)  int (lower_bound (d.begin(), d.end(), val) - d.begin())
#define M 100003
using namespace std;

int dx[]= {1,0,-1,0};
int dy[]= {0,1,0,-1};                        //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};     //8 direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};   //Knight Direction
//int dx[]={2,1,-1,-2,-1,1};int dy[]={0,1,1,0,-1,-1};              //Hexagonal Direction
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);

typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ll,bool> pib;
typedef pair<double,int> pdi;
typedef pair<int,char> pic;
typedef pair< int, pii> ipii;

//FAST_INPUT
template<class T>
inline bool fs(T &x)
{
    int c=getchar();
    int sgn=1;
    while(~c&&c<'0'||c>'9')
    {
        if(c=='-')sgn=-1;
        c=getchar();
    }
    for(x=0; ~c&&'0'<=c&&c<='9'; c=getchar())
        x=x*10+c-'0';
    x*=sgn;
    return ~c;
}

map<ll ,ll> ara;
ll mara[M+5];
int main()
{
    ll k,n,x;
    fs(n);
    fs(k);

    loop(i,0,n)
    {
        fs(x);
        mara[i]=x;
        ara[x]=true;
    }
    sort(mara,mara+n);
    loop(i,0,n)
    if(ara[mara[i]])
    ara[mara[i]*k]=0;

    int sum=0;
    miter(ara,ll,ll)
    sum+=it->S;

    if(k==1)
        cout<<n<<endl;
    else
    cout<<sum<<endl;
    return 0;
}


/*
   ~~~ BISMILLAH HIR RAHMANIR RAHIM ~~~
   Problem Name:
       Platform:
     Algorithom:
  Author's Name: RC Tushar

                 শুনেছি তোমার ভীষণ রাগ ?
                              সে কি রাগ , নাকি
                                     মগজ কোণে পচন ধরা
                 পুরনো কোডের বাগ ?

 ~~~     Never Lost Hope     ~~~
*/
#include<bits/stdc++.h>
#define pi 2*acos(0.0)
#define PS system("pause")
#define siter(n,T1)  for(set<T1>::iterator it=n.begin();it!=n.end();it++)
#define miter(n,T1,T2)  for(map<T1,T2>::iterator it=n.begin();it!=n.end();it++)
#define meter(n,T1,T2)  for(map<T1,T2>::iterator it=n.end();it!=n.begin();it--)
#define FOR(s,e,inc) for(int i=s;i<=e;i+=inc)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define Sfor(n) for(int i=1;i<=n;i++)
#define inf (1<<30)
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define sz(x) ((int)x.size())
#define eps 1e-9
#define gcd(x,y) __gcd(x,y)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define on(x,w)  x=x|(1<<w)
#define check(x,w) (x&(1<<w))==(1<<w)?true:false
#define all(x) (x).begin(),(x).end()
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define pf printf
#define ll long long int
#define ull unsigned long long int
#define MOD 1000000007
#define sqr(x) (( (x)* (x))%MOD)
#define cube(x)   ( (sqr(x)*(x))%MOD)
#define bit_cnt(x)   __builtin_popcount(x)
#define INT(c)  ((int)((c) - '0'))
#define CHAR(i) ((char)(i + int('0')))
#define maxall(v) *max_element(all(v))
#define minall(v) *min_element(all(v))
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define btz(a)   __builtin_ctz(a)
#define Mems(p,n) memset(p, n, sizeof(p))
#define makeint(n,s)  istringstream(s)>>n
#define BOUNDARY(i,j,Row,Col) ((i >= 0 && i < Row) && (j >= 0 && j < Col))
#define ubound(d,val)  int (upper_bound (d.begin(), d.end(), val) - d.begin())
#define lbound(d,val)  int (lower_bound (d.begin(), d.end(), val) - d.begin())
#define M 100003
using namespace std;

int dx[]= {1,0,-1,0};
int dy[]= {0,1,0,-1};                        //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};     //8 direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};   //Knight Direction
//int dx[]={2,1,-1,-2,-1,1};int dy[]={0,1,1,0,-1,-1};              //Hexagonal Direction
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);

typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ll,bool> pib;
typedef pair<double,int> pdi;
typedef pair<int,char> pic;
typedef pair< int, pii> ipii;

//FAST_INPUT
template<class T>
inline bool fs(T &x)
{
    int c=getchar();
    int sgn=1;
    while(~c&&c<'0'||c>'9')
    {
        if(c=='-')sgn=-1;
        c=getchar();
    }
    for(x=0; ~c&&'0'<=c&&c<='9'; c=getchar())
        x=x*10+c-'0';
    x*=sgn;
    return ~c;
}

map<ll ,ll> ara;
ll mara[M+5];
int main()
{
    ll k,n,x;
    fs(n);
    fs(k);

    loop(i,0,n)
    {
        fs(x);
        mara[i]=x;
        ara[x]=true;
    }
    sort(mara,mara+n);
    loop(i,0,n)
    if(ara[mara[i]])
    ara[mara[i]*k]=0;

    int sum=0;
    miter(ara,ll,ll)
    sum+=it->S;

    cout<<sum<<endl;
    return 0;
}


/*
   ~~~ BISMILLAH HIR RAHMANIR RAHIM ~~~
   Problem Name:
       Platform:
     Algorithom:
  Author's Name: RC Tushar

                 শুনেছি তোমার ভীষণ রাগ ?
                              সে কি রাগ , নাকি
                                     মগজ কোণে পচন ধরা
                 পুরনো কোডের বাগ ?

 ~~~     Never Lost Hope     ~~~
*/
#include<bits/stdc++.h>
#define pi 2*acos(0.0)
#define PS system("pause")
#define siter(n,T1)  for(set<T1>::iterator it=n.begin();it!=n.end();it++)
#define miter(n,T1,T2)  for(map<T1,T2>::iterator it=n.begin();it!=n.end();it++)
#define meter(n,T1,T2)  for(map<T1,T2>::iterator it=n.end();it!=n.begin();it--)
#define FOR(s,e,inc) for(int i=s;i<=e;i+=inc)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define Sfor(n) for(int i=1;i<=n;i++)
#define inf (1<<30)
#define pb push_back
#define ppb pop_back
#define F first
#define S second
#define sz(x) ((int)x.size())
#define eps 1e-9
#define gcd(x,y) __gcd(x,y)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define on(x,w)  x=x|(1<<w)
#define check(x,w) (x&(1<<w))==(1<<w)?true:false
#define all(x) (x).begin(),(x).end()
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define pf printf
#define ll long long int
#define ull unsigned long long int
#define MOD 1000000007
#define sqr(x) (( (x)* (x))%MOD)
#define cube(x)   ( (sqr(x)*(x))%MOD)
#define bit_cnt(x)   __builtin_popcount(x)
#define INT(c)  ((int)((c) - '0'))
#define CHAR(i) ((char)(i + int('0')))
#define maxall(v) *max_element(all(v))
#define minall(v) *min_element(all(v))
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define btz(a)   __builtin_ctz(a)
#define Mems(p,n) memset(p, n, sizeof(p))
#define makeint(n,s)  istringstream(s)>>n
#define BOUNDARY(i,j,Row,Col) ((i >= 0 && i < Row) && (j >= 0 && j < Col))
#define ubound(d,val)  int (upper_bound (d.begin(), d.end(), val) - d.begin())
#define lbound(d,val)  int (lower_bound (d.begin(), d.end(), val) - d.begin())
#define M 100003
using namespace std;

//int dx[]= {1,0,-1,0};
//int dy[]= {0,1,0,-1};                        //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};     //8 direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};   //Knight Direction
//int dx[]={2,1,-1,-2,-1,1};int dy[]={0,1,1,0,-1,-1};              //Hexagonal Direction
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);

typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ll,bool> pib;
typedef pair<double,int> pdi;
typedef pair<int,char> pic;
typedef pair< int, pii> ipii;

//FAST_INPUT
template<class T>
inline bool fs(T &x)
{
    int c=getchar();
    int sgn=1;
    while(~c&&c<'0'||c>'9')
    {
        if(c=='-')sgn=-1;
        c=getchar();
    }
    for(x=0; ~c&&'0'<=c&&c<='9'; c=getchar())
        x=x*10+c-'0';
    x*=sgn;
    return ~c;
}

map<ll ,ll> ara;
ll mara[M+5];
int main()
{
    ll k,n,x;
    fs(n);
    fs(k);

    loop(i,0,n)
    {
        fs(x);
        mara[i]=x;
        ara[x]=1;
    }
    sort(mara,mara+n);

    for(int i=n-1;i>=0;i--)
    {
        if(!ara[mara[i]*k])
            ara[mara[i]]=2;
        else
            break;
    }

    for(int i=0;i<n;i++)
    {
        if(ara[mara[i]]==1)
        {
            if(ara[mara[i]*k]==1)
            ara[mara[i]*k]=0;
            if(ara[mara[i]*k]==2)
                ara[mara[i]]=0;
        }
    }

    int sum=0;

    for(int i=0;i<n;i++)
    {
        if(ara[mara[i]]!=0)
        sum++;
    }

    if(k==1)
        cout<<n<<endl;
    else
    cout<<sum<<endl;
    return 0;
}


#include <algorithm>
#include <stdio.h>
#include <unordered_map>

const int MAX = (int)1e5;
int a[MAX];
int n, k;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  std::sort(a, a + n);
  std::unordered_map<int, bool> s;
  for (int i = 0; i < n; ++i) {
    if (a[i] % k == 0 && s.count(a[i] / k)) {
      continue;
    }
    s[a[i]] = true;
  }
  printf("%lu\n", s.size());
  return 0;
}


#include <algorithm>
#include <stdio.h>
#include <unordered_map>

const int MAX = (int)1e5;
int a[MAX];
int n, k;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  std::sort(a, a + n);
  std::unordered_map<int, bool> s;
  for (int i = 0; i < n; ++i) {
    if (a[i] % k == 0 && s.find(a[i] / k) != s.end()) {
      continue;
    }
    s[a[i]] = true;
  }
  printf("%lu\n", s.size());
  return 0;
}


#include <algorithm>
#include <stdio.h>
#include <set>

const int MAX = (int)1e5;
int a[MAX];
int n, k;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  std::sort(a, a + n);
  std::set<int> s;
  for (int i = 0; i < n; ++i) {
    if (a[i] % k == 0 && s.count(a[i] / k) == 0) {
      continue;
    }
    s.insert(a[i]);
  }
  printf("%lu\n", s.size());
  return 0;
}


#include <algorithm>
#include <stdio.h>
#include <set>

const int MAX = (int)1e5;
int a[MAX];
int n, k;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  std::sort(a, a + n);
  std::set<int> s;
  for (int i = 0; i < n; ++i) {
    if (a[i] % k == 0 && s.count(a[i] / k)) {
      continue;
    }
    s.insert(a[i]);
  }
  printf("%lu\n", s.size());
  return 0;
}


#include <algorithm>
#include <stdio.h>
#include <set>

const int MAX = (int)1e5;
int a[MAX];
int n, k;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  std::sort(a, a + n);
  std::set<int> s;
  for (int i = 0; i < n; ++i) {
    if (a[i] % k == 0 && s.count(a[i] / k) == 0) {
      s.insert(a[i]);
    }
  }
  printf("%lu\n", s.size());
  return 0;
}


#include <algorithm>
#include <stdio.h>
#include <unordered_map>

const int MAX = (int)1e5;
int a[MAX];
int n, k;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  std::sort(a, a + n);
  std::unordered_map<int, bool> s;
  for (int i = 0; i < n; ++i) {
    if (a[i] % k == 0 && s[a[i] / k]) {
      continue;
    }
    s[a[i]] = true;
  }
  printf("%lu\n", s.size());
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int A[(int)1e6];
int main(){
int n,k;cin>>n>>k;
for(int i=0;i<n;i++)
    cin>>A[i];
map<int,int>M;
sort(A,A+n);
int count=0;
for(int i=0;i<n;i++){
    int lo=0,hi=n-1;
    while(lo<hi){
        int mid=(lo)+(hi-lo)/2;
        if(A[mid]>=(A[i]*k))
            hi=mid;
        else
            lo=mid+1;
    }
    count+=(A[i]*k==A[lo]);
}
cout<<count<<endl;
return 0;}


#include<bits/stdc++.h>
using namespace std;
int A[(int)1e6]
int main(){
int n,k;cin>>n>>k;
for(int i=0;i<n;i++)
    cin>>A[i];
map<int,int>M;
int count=0;
for(int i=0;i<n;i++){
    if(A[i]%k||(A[i]%k==0&&!M[A[i]/k])){
        count++;
        M[A[i]]=1;
    }
}
cout<<count<<endl;
return 0;}


#include<bits/stdc++.h>
using namespace std;
int A[(int)1e6];
int main(){
int n,k;cin>>n>>k;
for(int i=0;i<n;i++)
    cin>>A[i];
map<int,int>M;
sort(A,A+n);
int count=0;
for(int i=0;i<n;i++){
    if(A[i]%k){
        count++;
        M[A[i]]=1;
    }
    else{
        if(!M[A[i]/k]){
            M[A[i]]=1;
            count++;
        }
    }
}
cout<<count<<endl;
return 0;}


#include<bits/stdc++.h>
using namespace std;
long long n,k;
long long A[(int)1e5];
bool check(int limit){
    if(limit==1)
        return 1;
    int count=0;
    int i=0;
    map<int,bool>Map;
    while(i<n){
        if(A[i]%k){
            count++;
            Map[A[i]]++;
        }
        else{
            if(!Map[A[i]/k]){
                Map[A[i]]=1;
                count++;
            }
        }
        i++;
    }
return count>=limit;}
int main(){
    cin>>n>>k;
for(int i=0;i<n;i++){
    cin>>A[i];
}
sort(A,A+n);
int lo=1,hi=n;
while(lo<hi){
    int mid=lo+(hi-lo)/2;
    if(!check(mid))
        hi=mid;
    else
        lo=mid+1;
}
cout<<lo-1<<endl;
return 0;}


#include<bits/stdc++.h>
using namespace std;
int A[(int)1e6];
int main(){
int n,k;cin>>n>>k;
for(int i=0;i<n;i++)
    cin>>A[i];
map<int,int>M;
int count=0;
for(int i=0;i<n;i++){
    if(A[i]%k||(A[i]%k==0&&!M[A[i]/k])){
        count++;
        M[A[i]]=1;
    }
}
cout<<count<<endl;
return 0;}


#include<bits/stdc++.h>
using namespace std;
long long n,k;
long long A[(int)1e5];
bool check(int limit){
    if(limit==1)
        return 1;
    int count=0;
    int i=0;
    map<int,bool>Map;
    while(i<n){
        if(A[i]%k){
            count++;
            Map[A[i]]++;
        }
        else{
            if(!Map[A[i]/k]){
                Map[A[i]]=1;
                count++;
            }
        }
        i++;
    }
return count>=limit;}
int main(){
    cin>>n>>k;
for(int i=0;i<n;i++){
    cin>>A[i];
}
sort(A,A+n);
int lo=1,hi=n+1;
while(lo<hi){
    int mid=lo+(hi-lo)/2;
    if(!check(mid))
        hi=mid;
    else
        lo=mid+1;
}
cout<<lo-1<<endl;
return 0;}


#include<bits/stdc++.h>
using namespace std;
int A[(int)1e6];
int main(){
int n,k;cin>>n>>k;
for(int i=0;i<n;i++)
    cin>>A[i];
map<int,int>M;
int count=0;
for(int i=0;i<n;i++){
    if(A[i]%k){
        count++;
        M[A[i]]=1;
    }
    else{
        if(!M[A[i]/k]){
            M[A[i]]=1;
            count++;
        }
    }
}
cout<<count<<endl;
return 0;}


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

int main(){
	long int n,k,a;
	set<int> freeset;
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin >> a;
		if(!freeset.count(a*k) && !freeset.count(a/k))
			freeset.insert(a);
	}
	cout << freeset.size();
	cin >>n;
}


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

int main(){
	long int n,k;
	set<int> freeset;
	cin>>n>>k;
	vector<int> as(n);
	for(int i=0;i<n;i++){
		cin >> as[i];
	}
	sort(as.begin(),as.end());
	for(int i=n-1;i>=0;i--){
		if(!freeset.count(as[i]*k))
			freeset.insert(as[i]);
	}

	cout << freeset.size();
	cin >>n;
}


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

int main(){
	long int n,k;
	set<long int> freeset;
	cin>>n>>k;
	vector<long int> as(n);
	for(int i=0;i<n;i++){
		cin >> as[i];
	}
	sort(as.begin(),as.end());
	for(int i=n-1;i>=0;i--){
		if(!freeset.count(as[i]*k) && !freeset.count(as[i]/k))
			freeset.insert(as[i]);
	}

	cout << freeset.size();
}


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

int main(){
	long int n,k;
	set<long int> freeset;
	cin>>n>>k;
	vector<long int> as(n);
	for(int i=0;i<n;i++){
		cin >> as[i];
	}
	sort(as.begin(),as.end());
	for(int i=n-1;i>=0;i--){
		if(!freeset.count(as[i]*k))
			freeset.insert(as[i]);
	}

	cout << freeset.size();
	cin >>n;
}


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

int main(){
	long int n,k;
	set<long int> freeset;
	cin>>n>>k;
	vector<int> as(n);
	for(int i=0;i<n;i++){
		cin >> as[i];
	}
	sort(as.begin(),as.end());
	for(int i=n-1;i>=0;i--){
		if(!freeset.count(as[i]*k) && !freeset.count(as[i]/k))
			freeset.insert(as[i]);
	}

	cout << freeset.size();
}


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

int main(){
	long int n,k;
	set<long int> freeset;
	cin>>n>>k;
	vector<long int> as(n);
	for(int i=0;i<n;i++){
		cin >> as[i];
	}
	sort(as.begin(),as.end());
	for(int i=0;i<n;i++){
		if(as[i]%k!=0) freeset.insert(as[i]);
		else{
		if(!freeset.count(as[i]/k))	freeset.insert(as[i]);
	}
	}

	cout << freeset.size();
	cin >>n;
}


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

int main(){
	long int n,k;
	set<int> freeset;
	cin>>n>>k;
	vector<int> as(n);
	for(int i=0;i<n;i++){
		cin >> as[i];
	}
	sort(as.begin(),as.end());
	for(int i=n-1;i>=0;i--){
		if(!freeset.count(as[i]*k) && !freeset.count(as[i]/k))
			freeset.insert(as[i]);
	}

	cout << freeset.size();
}


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

int n, k;
vector<int> numbers;
vector<int> s;
int maximo = 0;

void backtracking(int len, int current)
{
    int currentNumber = numbers[current];
    if (len > maximo)
    {
        maximo = len;
    }
    if (current < n)
    {
        int t = 0;
        if ((currentNumber % k) == 0) {
            for (int i = 0; i < len; i++)
            {
                int compare = s[i];
                if (compare > currentNumber/k) {
                    break;
                }
                if (currentNumber == compare * k)
                {
                    t++;
                    break;
                }
            }
        }
        if (t == 0)
        {
            s.push_back(numbers[current]);
            backtracking(len + 1, current + 1);
            s.pop_back();
        }
        backtracking(len, current + 1);
    }
}

int main()
{
    cin >> n >> k;

    int x;

    for(int i = 0; i < n; i++)
    {
        cin >> x;
        numbers.push_back(x);
    }
    sort(numbers.begin(), numbers.end());

    backtracking(0, 0);
    cout << maximo;
}


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

int n, k;
vector<int> numbers;
vector<int> s;
int maximo = 0;

void backtracking(int len, int current)
{
    int currentNumber = numbers[current];
    if (len > maximo)
    {
        maximo = len;
    }
    if (current < n)
    {
        int t = 0;
        if ((currentNumber % k) == 0) {
            for (int i = 0; i < len; i++)
            {
                int compare = s[i];
                if (compare > currentNumber/k) {
                    break;
                }
                if (currentNumber == compare * k)
                {
                    backtracking(len, current + 1);
                    t++;
                    break;
                }
            }
        }
        if (t == 0)
        {
            s.push_back(currentNumber);
            backtracking(len + 1, current + 1);
            s.pop_back();
        }
    }
}

int main()
{
    cin >> n >> k;

    int x;

    for(int i = 0; i < n; i++)
    {
        cin >> x;
        numbers.push_back(x);
    }
    sort(numbers.begin(), numbers.end());

    backtracking(0, 0);
    cout << maximo;
}


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

int n, k;
vector<int> numbers;
vector<int> s;
int maximo = 0;

void backtracking(int len, int current)
{
    int currentNumber = numbers[current];
    if (len > maximo)
    {
        maximo = len;
    }
    if (current < n)
    {
        int t = 0;
        for (int i = 0; i < len; i++)
        {
            if (currentNumber == s[i] * k)
            {
                t++;
                break;
            }
        }
        if (t == 0)
        {
            s.push_back(numbers[current]);
            backtracking(len + 1, current + 1);
            s.pop_back();
        }
        backtracking(len, current + 1);
    }
}

int main()
{
    cin >> n >> k;

    int x;

    for(int i = 0; i < n; i++)
    {
        cin >> x;
        numbers.push_back(x);
    }
    sort(numbers.begin(), numbers.end());

    backtracking(0, 0);
    cout << maximo;
}


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

int n, k;
vector<int> numbers;
vector<int> s;
int maximo = 0;

void backtracking(int len, int current)
{
    if (len > maximo)
    {
        maximo = len;
    }
    if (current < n)
    {
        int currentNumber = numbers[current];
        int t = 0;
        if ((currentNumber % k) == 0) {
            for (int i = 0; i < len; i++)
            {
                int compare = s[i];
                if (compare > currentNumber/k) {
                    break;
                }
                if (currentNumber == compare * k)
                {
                    backtracking(len, current + 1);
                    t++;
                    break;
                }
            }
        }
        if (t == 0)
        {
            s.push_back(currentNumber);
            backtracking(len + 1, current + 1);
            s.pop_back();
        }
    }
}

int main()
{
    cin >> n >> k;

    int x;

    for(int i = 0; i < n; i++)
    {
        cin >> x;
        numbers.push_back(x);
    }
    sort(numbers.begin(), numbers.end());

    backtracking(0, 0);
    cout << maximo;
}


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

int n, k;
vector<int> numbers;
vector<int> s;
int maximo = 0;

void backtracking(int len, int current)
{
    if (len > maximo)
    {
        maximo = len;
    }
    if (current < n)
    {
        int currentNumber = numbers[current];
        int t = 0;
        if ((currentNumber % k) == 0) {
            for (int i = 0; i < len; i++)
            {
                int compare = s[i];
                if (compare > currentNumber/k) {
                    break;
                }
                if (currentNumber == compare * k)
                {
                    backtracking(len, current + 1);
                    t++;
                    break;
                }
            }
        }
        if (t == 0)
        {
            s.push_back(currentNumber);
            backtracking(len + 1, current + 1);
            s.pop_back();
        }
    }
}

int main()
{
    // Estas dos lineallas recomendaron en la ayudantía
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> k;

    int x;

    for(int i = 0; i < n; i++)
    {
        cin >> x;
        numbers.push_back(x);
    }
    sort(numbers.begin(), numbers.end());

    backtracking(0, 0);
    cout << maximo;
}


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

int n, k;
vector<int> numbers;
vector<int> s;
int maximo = 0;

void backtracking(int len, int current)
{
    if (current < n) {
        int currentNumber = numbers[current];
        if ((currentNumber % k) == 0) {
            //https://stackoverflow.com/questions/571394/how-to-find-out-if-an-item-is-present-in-a-stdvector
            if (find(s.begin(), s.end(), (currentNumber / k)) != s.end()) {
                    backtracking(len, current + 1);
                    return;
            }
        }
        s.push_back(currentNumber);
        backtracking(len + 1, current + 1);
        s.pop_back();
    } else if (len > maximo) {
        maximo = len;
    }
}

int main() {
    // Estas dos lineas las recomendaron en la ayudantía
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> k;

    int x;

    for(int i = 0; i < n; i++)
    {
        cin >> x;
        numbers.push_back(x);
    }

    backtracking(0, 0);
    cout << maximo;
}


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

int n, k;
vector<int> numbers;
vector<int> s;
int maximo = 0;

void backtracking(int len, int current)
{
    int currentNumber = numbers[current];
    if (len > maximo)
    {
        maximo = len;
    }
    if (current < n)
    {
        int t = 0;
        if ((currentNumber % k) == 0) {
            for (int i = 0; i < len; i++)
            {
                int compare = s[i];
                if (compare > currentNumber/k) {
                    break;
                }
                if (currentNumber == compare * k)
                {
                    backtracking(len, current + 1);
                    t++;
                    break;
                }
            }
        }
        if (t == 0)
        {
            s.push_back(numbers[current]);
            backtracking(len + 1, current + 1);
            s.pop_back();
        }
    }
}

int main()
{
    cin >> n >> k;

    int x;

    for(int i = 0; i < n; i++)
    {
        cin >> x;
        numbers.push_back(x);
    }
    sort(numbers.begin(), numbers.end());

    backtracking(0, 0);
    cout << maximo;
}


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

int n, k;
vector<int> numbers;
vector<int> s;
int maximo = 0;

void backtracking(int len, int current)
{
    if (current < n)
    {
        int currentNumber = numbers[current];
        if ((currentNumber % k) == 0) {
            for (int i = 0; i < len; i++)
            {
                int compare = s[i];
                if (compare > (currentNumber / k)) {
                    break;
                }
                if (currentNumber == compare * k)
                {
                    backtracking(len, current + 1);
                    return;
                }
            }
        }
        s.push_back(currentNumber);
        backtracking(len + 1, current + 1);
        s.pop_back();
    } else if (len > maximo) {
        maximo = len;
    }
}

int main()
{
    // Estas dos lineallas recomendaron en la ayudantía
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> k;

    int x;

    for(int i = 0; i < n; i++)
    {
        cin >> x;
        numbers.push_back(x);
    }
    sort(numbers.begin(), numbers.end());

    backtracking(0, 0);
    cout << maximo;
}


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

int n, k;
vector<int> numbers;
vector<int> s;
int maximo = 0;

void backtracking(int len, int current)
{
    if (len > maximo)
    {
        maximo = len;
    }
    if (current < n)
    {
        int currentNumber = numbers[current];
        if ((currentNumber % k) == 0) {
            for (int i = 0; i < len; i++)
            {
                int compare = s[i];
                if (compare > currentNumber/k) {
                    break;
                }
                if (currentNumber == compare * k)
                {
                    backtracking(len, current + 1);
                    return;
                }
            }
        }
        s.push_back(currentNumber);
        backtracking(len + 1, current + 1);
        s.pop_back();
    }
}

int main()
{
    // Estas dos lineallas recomendaron en la ayudantía
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> k;

    int x;

    for(int i = 0; i < n; i++)
    {
        cin >> x;
        numbers.push_back(x);
    }
    sort(numbers.begin(), numbers.end());

    backtracking(0, 0);
    cout << maximo;
}


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

int n, k;
vector<int> numbers;
vector<int> s;
int maximo = 0;

void backtracking(int len, int current)
{
    if (current < n) {
        int currentNumber = numbers[current];
        if ((currentNumber % k) == 0) {
            //https://stackoverflow.com/questions/571394/how-to-find-out-if-an-item-is-present-in-a-stdvector
            if (find(s.begin(), s.end(), (currentNumber / k)) != s.end()) {
                    backtracking(len, current + 1);
                    return;
            }
        }
        s.push_back(currentNumber);
        backtracking(len + 1, current + 1);
        s.pop_back();
    } else if (len > maximo) {
        maximo = len;
    }
}

int main() {
    // Estas dos lineas las recomendaron en la ayudantía
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> k;

    int x;

    for(int i = 0; i < n; i++)
    {
        cin >> x;
        numbers.push_back(x);
    }
    sort(numbers.begin(), numbers.end());

    backtracking(0, 0);
    cout << maximo;
}


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

int n, k;
vector<int> numbers;
vector<int> s;
int maximo = 0;

void backtracking(int len, int current)
{
    if (current < n)
    {
        int currentNumber = numbers[current];
        if ((currentNumber % k) == 0) {
            for (int i = 0; i < len; i++)
            {
                int compare = s[i];
                if (compare > (currentNumber / k)) {
                    break;
                }
                if (currentNumber == compare * k)
                {
                    //backtracking(len, current + 1);
                    return;
                }
            }
        }
        s.push_back(currentNumber);
        backtracking(len + 1, current + 1);
        s.pop_back();
    } else if (len > maximo) {
        maximo = len;
    }
}

int main()
{
    // Estas dos lineallas recomendaron en la ayudantía
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> k;

    int x;

    for(int i = 0; i < n; i++)
    {
        cin >> x;
        numbers.push_back(x);
    }
    sort(numbers.begin(), numbers.end());

    backtracking(0, 0);
    cout << maximo;
}


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

int n, k;
int numbers[100000] {0};
int s[100000] {0};
int maximo = 0;

void backtracking(int len, int current)
{
    if (len > maximo)
    {
        maximo = len;
    }
    if (current < n)
    {
        int t = 0;
        for (int i = 0; i < len; i++)
        {
            if (numbers[current] * k == s[i] or numbers[current] == s[i] * k)
            {
                backtracking(len, current + 1);
                t++;
                break;
            }
        }
        if (t == 0)
        {
            s[len] = numbers[current];
            backtracking(len + 1, current + 1);
            s[len] = 0;
        }
    }
}

int main()
{
    cin >> n >> k;

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

    backtracking(0, 0);
    cout << maximo;
}


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

int n, k;
vector<int> numbers;
vector<int> s;
int maximo = 0;

void backtracking(int len, int current)
{
    int currentNumber = numbers[current];
    if (len > maximo)
    {
        maximo = len;
    }
    if (current < n)
    {
        int t = 0;
        if ((currentNumber % k) == 0) {
            for (int i = 0; i < len; i++)
            {
                int compare = s[i];
                if (compare > n/k) {
                    break;
                }
                if (currentNumber == compare * k)
                {
                    t++;
                    break;
                }
            }
        }
        if (t == 0)
        {
            s.push_back(numbers[current]);
            backtracking(len + 1, current + 1);
            s.pop_back();
        }
        backtracking(len, current + 1);
    }
}

int main()
{
    cin >> n >> k;

    int x;

    for(int i = 0; i < n; i++)
    {
        cin >> x;
        numbers.push_back(x);
    }
    sort(numbers.begin(), numbers.end());

    backtracking(0, 0);
    cout << maximo;
}


#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
map<long long,long long> fix;
long long n,k,ans;
int main(){
    cin>>n>>k;
    long long a[n];
    for(int i=0;i<n;i++){
      cin>>a[i];
    }
    sort(a,a+n);
    for(int i=0;i<n;i++){
      if(fix[a[i]]!=1){
        ans++;
        fix[a[i]*k]=1;
        }
    }
    cout<<ans;
}


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

long long a,b,c[1000000],d,e;
map <long long,long long> mp;
int main(){
  cin>>a>>b;
  for(int i = 0; i < a; i++){
    cin>>c[i];
  }
  sort(c,c+a);
  for(int i = 0; i < a; i++){
    if(mp[c[i]]!=1){
      e++;
      mp[c[i]*b]=1;
    }
  }
  cout<<e;
}


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

long long a,b,c[1000000],d,e;
map <int> mp;
int main(){
  cin>>a>>b;
  for(int i = 0; i < a; i++){
    cin>>c[i];
  }
  for(int i = 0; i < a; i++){
    if(mp[c[i]]!=1){
      e++;
      mp[c[i]*b]=1;
    }
  }
  cout<<e;
}


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

long long a,b,c[1000000],d,e;
map <long,long> mp;
int main(){
  cin>>a>>b;
  for(int i = 0; i < a; i++){
    cin>>c[i];
  }
  for(int i = 0; i < a; i++){
    if(mp[c[i]]!=1){
      e++;
      mp[c[i]*b]=1;
    }
  }
  cout<<e;
}


#include <stdio.h>
#include<string.h>
void bubb(int*,int);
int main(void) {
    int m,n,i,sum=0,su1=0;
    scanf("%d %d",&m,&n);
    int a[m],j,p=0,x=n;
    int count[x];
    char b[n][123];
    for(i=0;i<m;i++)
    {scanf("%d ",&a[i]);
      }
    for(i=0;i<n;i++)
    {scanf("%s ",b[i]);
    count[i]=1;}
    bubb(a,m);
    for(i=0;i<n;i++)
    for(j=0;j<n;j++)
    if(strcmp(b[i],b[j])==0&&i<j)
    {count[i]++;
     p++;
    }

    bubb(count,n);

    for(i=0;i<n-p;i++)
    sum=sum+a[i]*(count[i]);
    for(i=m-1,j=0;i>=m-n+p;i--,j++)
    su1=su1+a[i]*(count[j]);
    printf("%d %d",su1,sum);
	return 0;
}
void bubb(int*a,int m)
{
	int i,j,temp;
	for(i=0;i<m-1;i++)
	for(j=0;j<m-1;j++)
	{
		if(*(a+j)<*(a+j+1))
		{
			temp=*(a+j);
			*(a+j)=*(a+j+1);
			*(a+j+1)=temp;
	}
	}
}


#include <stdio.h>
#include<string.h>
void bubb(int*,int);
int main(void) {
    int m,n,i,sum=0,su1=0;
    scanf("%d %d",&m,&n);
    int a[m],j,p=0,x=n,c=0;
    int count[x],o[n];
    char b[n][123];
    for(i=0;i<m;i++)
    {scanf("%d ",&a[i]);
      }
    for(i=0;i<n;i++)
    {scanf("%s ",b[i]);
    count[i]=1;
    o[i]=c++;
    }
    bubb(a,m);
    for(i=0;i<n;i++)
    for(j=0;j<n;j++)
    if(strcmp(b[i],b[j])==0&&i<j)
    {count[i]++;
     p++;
     b[j][0]=o[j];
     o[j]+=p;
    }

     bubb(count,n);

    for(i=0;i<n-p;i++)
    sum=sum+a[i]*(count[i]);
    for(i=m-1,j=0;i>=m-n+p;i--,j++)
    su1=su1+a[i]*(count[j]);
   printf("%d %d",su1,sum);
	return 0;
}
void bubb(int*a,int m)
{
	int i,j,temp;
	for(i=0;i<m-1;i++)
	for(j=0;j<m-1;j++)
	{
		if(*(a+j)<*(a+j+1))
		{
			temp=*(a+j);
			*(a+j)=*(a+j+1);
			*(a+j+1)=temp;
	}
	}
}


#include <stdio.h>
int mystrcmp(char *s, char *q)
{
    int i = 0;
    while (s[i] == q[i] && s[i] != '\0') i++;
    if(s[i] == '\0' && q[i] == '\0') return 1;
    else return 0;
}
void mystrcpy(char *s, char *q)
{
    int i = 0;
    while (q[i] != '\0')
    { s[i] = q[i]; i++;}
    s[i] = '\0';
}

int p[101], n, m, k = 0;
struct fruit {char name[40]; int rep;} f[101], tmp;
char cf[40];
void sortlist(struct fruit x[101], int k)
{
    for (int i=1; i<k; i++)
        for (int j =1; j<k; j++)
            if (x[j].rep < x[j+1].rep)
            { tmp = x[j]; x[j] = x[j+1]; x[j+1] = tmp; }
}
void sortprices(int x[101], int k)
{   int tmp;
    for (int i=1; i<k; i++)
        for (int j =1; j<k; j++)
            if (x[j] > x[j+1])
            { tmp = x[j]; x[j] = x[j+1]; x[j+1] = tmp; }
}

int main() {
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%d" , &p[i]);
    for (int i =1; i <=m; i++)
    {
        scanf("%s", cf);
        int flag = 0;
        for (int j = 1; j <= k; j++) if (mystrcmp(f[j].name, cf) == 1) flag = j;
        if (flag == 0) {k++; mystrcpy(f[k].name, cf); f[k].rep = 1;}
        else f[flag].rep++;
    }
    sortlist(f, k);
    sortprices (p,n);
/*    for (int i = 1; i <= n; i++) printf("%d ", p[i]);
    printf("\n");
    for (int i = 1; i <= k; i++)
        printf("%s %d\n", f[i].name, f[i].rep);*/
    int s = 0;
    for (int i = 1; i <= k; i++) s = s + p[i]*f[i].rep;
    int sm = 0;
    for (int i = 1; i <= k; i++) sm = sm + p[n - i + 1] * f[ i ].rep;
    printf("%d %d", s, sm);
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

int N, M;
int price[106];
int wanna[106];
char word[106][66];

//	统计相同名字的物品有多少个
//	如果是完全相同的字符串，调用 strcmp 会返回 0
//	重复的字符串通过标记为 '\0' 去除
void count_word()
{
	for(int m = 1; m <= M; ++m) {
		if(word[m][0] == 0)
			continue;

		wanna[m] = 1;
		for(int k = m + 1; k <= M; ++k)
			if(strcmp(word[m], word[k]) == 0)
			{
				++wanna[m];
				word[k][0] = 0;
			}
	}
}

//	这个冒泡把数组从大到小排序起来
void sort_arr(int arr[], int num)
{
	for(int j = num; j > 1; --j)
		for(int k = 2; k <= j; ++k)
			if(arr[k - 1] < arr[k]) {
				int tmp = arr[k];
				arr[k] = arr[k - 1];
				arr[k - 1] = tmp;
			}
}

// 把数组翻转，如果原来是从小到大则变成从大到小，反之同理
void reverse(int arr[], int num)
{
	for(int k = 1; k <= num / 2; ++k)
	{
		int tmp = arr[k];
		arr[k] = arr[num + 1 - k];
		arr[num + 1 - k] = tmp;
	}
}

int main()
{
	//	输入数据
	scanf("%d%d", &N, &M);
	for(int n = 1; n <= N; ++n)
		scanf("%d", &price[n]);
	for(int m = 1; m <= M; ++m)
		scanf("%s", word[m]);

	//BEGIN
	//	先数一下pmz想要的东西有多少种，每种几个，记录在wanna中
	count_word();
	//	对wanna数组排序，0会因为最小被排序到后面
	sort_arr(wanna, M);
	//	数一下pmz想要的东西有多少种，用 M 表示（原来的 M 已经没用了
	for(int m = 1; m <= M; ++m)
		if(wanna[m + 1] == 0)
		{
			M = m;
			break;
		}
	//	对价钱也进行排序
	sort_arr(price, N);

	int answer_MAX = 0, answer_MIN = 0;

	//	最坏的情况，越想买的东西越贵
	for(int m = 1; m <= M; ++m)
		answer_MAX += price[m] * wanna[m];

	//	最好的情况，想买的东西都很便宜
	reverse(price, N);
	for(int m = 1; m <= M; ++m)
		answer_MIN += price[m] * wanna[m];
	//END

	printf("%d %d\n", answer_MIN, answer_MAX);
	return 0;
}


using System;
using System.Linq;
using System.Collections.Generic;
using System.Numerics;

class Program
{
    public static void Main(string[] args)
    {
        //MulticaseTest();
        int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
        int n = input[0], m = input[1];
        List<int> prices = Console.ReadLine().Split().Select(int.Parse).ToList();
        Dictionary<string, int> Fruits = new Dictionary<string, int>();

        for (int i = 0; i < m; i++)
        {
            string r = Console.ReadLine();
            if (!Fruits.ContainsKey(r))
            {
                Fruits.Add(r, 1);
                continue;
            }
            Fruits[r]++;
        }
        Fruits = Fruits.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
        int lowestPrice = 0;
        int highestPrice = 0;
        List<int> Prices1 = prices.OrderBy(x => x).ToList();
        List<int> Prices2 = prices.OrderByDescending(x => x).ToList();
        foreach (var fruit in Fruits)
        {
            lowestPrice += fruit.Value * Prices1[0];
            highestPrice += fruit.Value * Prices2[0];
            Prices1.RemoveAt(0);
            Prices2.RemoveAt(0);
        }
        Console.WriteLine(lowestPrice + " " + highestPrice);
    }
    public static void MulticaseTest()
    {
        //4
        //4 5 2 3
        int t = int.Parse(Console.ReadLine());
        List<string> o = new List<string>(t);
        for (int i = 0; i < t; i++)
        {
            int n = int.Parse(Console.ReadLine());
            //List<int> input = Console.ReadLine().Split().Select(int.Parse).ToList();

        }
        o.ForEach(Console.WriteLine);
    }
}


using System;
using System.Collections.Generic;
namespace CodeForces12
{
    class Program
    {
        static void Main(string[] args)
        {
            string input1 = Console.ReadLine();
            string input2 = Console.ReadLine();

            string[] tmp = input1.Split(' ');
            int n = int.Parse(tmp[0]);
            int m = int.Parse(tmp[1]);

            string[] Str = input2.Split(' ');
            int[] sorted = new int[Str.Length];
            for (int i = 0; i < Str.Length; i++)
            {
                sorted[i] = int.Parse(Str[i]);
            }
            Array.Sort(sorted);

            Dictionary<string, int> dic = new Dictionary<string, int>();
            for (int i = 0; i < m; i++)
            {
                string inp = Console.ReadLine();
                if (dic.ContainsKey(inp))
                    dic[inp]++;
                else
                    dic.Add(inp, 1);
            }
            List<KeyValuePair<string, int>> myList = new List<KeyValuePair<string, int>>(dic);
            myList.Sort((firstPair, nextPair) =>
            {
                return firstPair.Value.CompareTo(nextPair.Value) * -1;
            }
            );

            int min = 0, max = 0;
            for (int i = 0; i < myList.Count; i++)
            {
                min += myList[i].Value * sorted[i];
                max += myList[i].Value * sorted[sorted.Length - i - 1];
            }
            Console.WriteLine(min + " " + max);
        }
    }
}


using System;
using System.Collections.Generic;

class Solve
{

    public void solve()
    {
        string[] In = Console.ReadLine().Split(new char[] { ' ' });
        int n = int.Parse(In[0]);
        int m = int.Parse(In[1]);

        In = Console.ReadLine().Split(new char[] { ' ' });
        List<int> Cost = new List<int>();
        for (int i = 0; i < n; ++i)
            Cost.Add(int.Parse(In[i]));
        Cost.Sort();

        List<string> Name = new List<string>();
        for (int i = 0; i < m; ++i)
            Name.Add(Console.ReadLine());
        Name.Sort();

        List<int> Cnt = new List<int>();
        Cnt.Add(1);
        for (int i = 1; i < m; ++i)
            if (Name[i] == Name[i - 1])
                ++Cnt[Cnt.Count - 1];
            else
                Cnt.Add(1);
        Cnt.Sort();

        int a = 0;
        for (int i = Cnt.Count - 1, k = 0; i >= 0; --i, ++k)
            a += Cnt[i] * Cost[k];

        int b = 0;
        for (int i = Cnt.Count - 1, k = n - 1; i >= 0; --i, --k)
            b += Cnt[i] * Cost[k];

        Console.WriteLine("{0} {1}", a, b);
    }
}

class Program
{
    static void Main()
    {
        Solve Task = new Solve();
        Task.solve();
      //  Console.ReadKey();
    }
}


/*******************************************************************************
* Author: Nirushuu
*******************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.IO;
using System.Globalization;
using System.Numerics;

/*******************************************************************************
* IO from Kattio.cs from open.kattis.com/help/csharp                          */
public class NoMoreTokensException : Exception
{
}

public class Tokenizer
{
    string[] tokens = new string[0];
    private int pos;
    StreamReader reader;

    public Tokenizer(Stream inStream)
    {
        var bs = new BufferedStream(inStream);
        reader = new StreamReader(bs);
    }

    public Tokenizer() : this(Console.OpenStandardInput())
    {
        // Nothing more to do
    }

    private string PeekNext()
    {
        if (pos < 0)
            // pos < 0 indicates that there are no more tokens
            return null;
        if (pos < tokens.Length)
        {
            if (tokens[pos].Length == 0)
            {
                ++pos;
                return PeekNext();
            }
            return tokens[pos];
        }
        string line = reader.ReadLine();
        if (line == null)
        {
            // There is no more data to read
            pos = -1;
            return null;
        }
        // Split the line that was read on white space characters
        tokens = line.Split(null);
        pos = 0;
        return PeekNext();
    }

    public bool HasNext()
    {
        return (PeekNext() != null);
    }

    public string Next()
    {
        string next = PeekNext();
        if (next == null)
            throw new NoMoreTokensException();
        ++pos;
        return next;
    }
}

public class Scanner : Tokenizer
{

    public int NextInt()
    {
        return int.Parse(Next());
    }

    public long NextLong()
    {
        return long.Parse(Next());
    }

    public float NextFloat()
    {
        return float.Parse(Next());
    }

    public double NextDouble()
    {
        return double.Parse(Next());
    }
}

public class BufferedStdoutWriter : StreamWriter
{
    public BufferedStdoutWriter() : base(new BufferedStream(Console.OpenStandardOutput()))
    {
    }
}
/******************************************************************************/

/*******************************************************************************
* DisjointSet datastructure                                                   */
public struct DisjointSet {
    public int[] parent;
    public int[] rank;
    public DisjointSet(int n) {
        parent = new int[n];
        for (int i = 0; i < n; ++i) {
            parent[i] = i;
        }
        rank = new int[n];
    }

    public int Find(int i) {
        int idx = i;
        var compress = new List<int>();
        while (idx != parent[idx]) {
            compress.Add(idx);
            idx = parent[idx];
        }

        foreach (var e in compress) {   // path compress
            parent[e] = idx;
        }
        return idx;
    }

    public void Union(int a, int b) {
        int aPar = this.Find(a);
        int bPar = this.Find(b);

        if (rank[aPar] > rank[bPar]) {  // by rank
            parent[bPar] = aPar;
        } else {
            parent[aPar] = bPar;
        }
        if (rank[aPar] == rank[bPar]) {
            ++rank[bPar];
        }
    }
}

class MaxHeap {
    List<int> data;

    public MaxHeap() {
        data = new List<int>();
    }

    public void Add(int a) {
        data.Add(a);
        int idx = data.Count - 1;
        int parent = (int) Math.Floor((double) (idx - 1) / 2);
        while (idx != 0 && (data[idx] > data[parent])) {
            int tmp = data[parent];
            data[parent] = data[idx];
            data[idx] = tmp;

            idx = parent;
            parent = (int) Math.Floor((double) (idx - 1) / 2);
        }
    }

    public int Pop() {
        int res = data[0];
        data[0] = data[data.Count - 1];
        data.RemoveAt(data.Count - 1);

        int idx = 0;
        while (true) {
            int child1 = (2 * idx) + 1;
            int child2 = (2 * idx) + 2;

            if (child1 >= data.Count) { // no children
                break;
            } else if (child2 >= data.Count) {  // one child
                if (data[idx] < data[child1]) {
                    int tmp = data[idx];
                    data[idx] = data[child1];
                    data[child1] = tmp;
                }
                break;
            } else {    // two children
                int maxChild = data[child1] > data[child2] ? child1 : child2;
                if (data[idx] < data[maxChild]) {
                    int tmp = data[idx];
                    data[idx] = data[maxChild];
                    data[maxChild] = tmp;
                    idx = maxChild;
                    continue;
                } else {    // no swap
                    break;
                }
            }
        }

        return res;
    }

}

class Dequeue {
    public Dictionary<int, int> data = new Dictionary<int, int>();
    int firstIdx;
    public int Count;
    public Dequeue() {
        data = new Dictionary<int, int>();
        firstIdx = 1;
        Count = 0;
    }

    public void PushFront(int a) {
        data[firstIdx - 1] = a;
        ++Count;
        --firstIdx;
    }

    public void PushBack(int a) {
        data[firstIdx + Count] = a;
        ++Count;
    }

    public int PopFront() {
        --Count;
        ++firstIdx;
        return data[firstIdx - 1];
    }

    public int PopBack() {
        --Count;
        return data[firstIdx + Count];
    }

    public int Get(int n) {
        return data[firstIdx + n];
    }
}
/******************************************************************************/

class MainClass {

    // Debug functions
    static void debug<T>(IEnumerable<T> a, string s = "") {
        Console.WriteLine($"name: {s}");
        int i = 0;
        foreach (var e in a) {
            Console.WriteLine($"value {i}: {e}");
            ++i;
        }
    }
    static void dbg<T>(T a, string s = "") {
        Console.WriteLine($"name: {s} value: {a}");
    }
    ////////////////////

    static int gcd(int a, int b) {
        while (b != 0) {
            int t = b;
            b = a % b;
            a = t;
        }
        return a;
    }

    static bool between(int a, int b, int c) {
        return (a >= b && a <= c);
    }

    static int findIn(string s, string a) {
        for (int i = 0; i < s.Length - (a.Length - 1); ++i) {
            bool found = true;
            for (int j = 0; j < a.Length; ++j) {
                if (s[i + j] != a[j]) {
                    found = false;
                    break;
                }
            }

            if (found) {
                return i;
            }
        }

        return -1;
    }

    static double dist(double a, double b, double c, double d) {
        return Math.Sqrt(Math.Pow((a - c), 2) + Math.Pow((b - d), 2));
    }

    public static void Main() {

        var sc = new Scanner();
        var wr = new BufferedStdoutWriter();
        var dot = new NumberFormatInfo();
        dot.NumberDecimalSeparator = ".";

        var n = sc.NextInt();
        var m = sc.NextInt();

        var prices = new List<int>(n);
        for (int i = 0; i < n; ++i) {
            prices.Add(sc.NextInt());
        }
        prices.Sort();

        var fruits = new Dictionary<string, int>();
        for (int i = 0; i < m; ++i) {
            string s = sc.Next();
            if (fruits.ContainsKey(s)) {
                ++fruits[s];
            } else {
                fruits[s] = 1;
            }
        }

        var fruitCnt = fruits.Values.ToList();
        fruitCnt.Sort((x, y) => (-x).CompareTo(-y));
        // fruitCnt.Sort();

        int min = 0;
        int max = 0;

        for (int i = 0; i < fruitCnt.Count; ++i) {
            min += prices[i] * fruitCnt[i];
            max += prices[prices.Count - i - 1] * fruitCnt[i];
        }

        // foreach (var e in fruitCnt) {
        //     wr.Write(e);
        // }
        wr.Write($"{min} {max}");

        wr.Flush();

    }
}


using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.IO;

class Program
{

    void solve()
    {

        int n, m;
        n = nextInt();
        m = nextInt();
        int[] price = new int[n];
        for (int i = 0; i < n; i++)
            price[i] = nextInt();
        Dictionary<string, int> d = new Dictionary<string, int>();
        for (int i = 0; i < m; i++)
        {
            string p = nextString();
            if (!d.ContainsKey(p))
                d[p] = 0;
            d[p]++;
        }
        int[] num = new int[n];
        int ind = 0;
        foreach (KeyValuePair<string, int> x in d)
            num[ind++] = x.Value;
        Array.Sort(num);
        Array.Sort(price);
        int min = 0;
        int max = 0;
        for (int i = 0; i < num.Length; i++)
        {
            min += num[n - 1 - i] * price[i];
            max += price[n - 1 - i] * num[n - 1 - i];
        }
        Console.WriteLine(min + " " + max);

    }

    //

    string[] inputLine = new string[0];
    int inputInd = 0;
    string nextLine()
    {
        return Console.ReadLine();
    }
    void readInput()
    {
        if (inputInd != inputLine.Length)
            throw new Exception();
        inputInd = 0;
        inputLine = Console.ReadLine().Split();

    }
    int nextInt()
    {
        if (inputInd == inputLine.Length)
            readInput();
        return int.Parse(inputLine[inputInd++]);
    }
    long nextLong()
    {
        if (inputInd == inputLine.Length)
            readInput();
        return long.Parse(inputLine[inputInd++]);
    }
    double nextDouble()
    {
        if (inputInd == inputLine.Length)
            readInput();
        return double.Parse(inputLine[inputInd++]);
    }
    string nextString()
    {
        if (inputInd == inputLine.Length)
            readInput();
        return inputLine[inputInd++];
    }
    static void Main(string[] args)
    {
        new Program().solve();
    }
}


using System;
using System.Collections.Generic;

class fruits
{
    public static void Main()
    {
        Dictionary<string, int> shopList = new Dictionary<string,int>();

        Console.ReadLine();

        int[] prices = Array.ConvertAll(Console.ReadLine().Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries), element => Convert.ToInt32(element));

        string s;
        while((s = Console.ReadLine()) != null)
        {
            if(!shopList.ContainsKey(s))
                shopList[s] = 1;
            else
                shopList[s] ++;
        }

        int[] reqQuant = new int[shopList.Values.Count];
        shopList.Values.CopyTo(reqQuant, 0);

        Array.Sort(reqQuant);
        Array.Reverse(reqQuant);

        Array.Sort(prices);

        int minPrice = 0, maxPrice = 0;

        for(int i = 0; i < reqQuant.Length; i ++)
        {
            minPrice += reqQuant[i] * prices[i];
            maxPrice += reqQuant[i] * prices[prices.Length - i - 1];
        }

        Console.WriteLine("{0} {1}", minPrice, maxPrice);
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine()),
                m = int.Parse(Console.ReadLine());
            var costs = new List<int>(n);
            for (int i = 0; i < n; ++i)
            {
                costs.Add(int.Parse(Console.ReadLine()));
            }
            var stats = new Dictionary<string, int>();
            for (int i = 0; i < m; ++i)
            {
                var fruit = Console.ReadLine();
                if (stats.Count(p => p.Key == fruit) > 0) ++stats[fruit];
                else stats.Add(fruit, 1);
            }
            var count = new List<int>();
            foreach (var key in stats.Keys)
            {
                count.Add(stats[key]);
            }
            costs.Sort(); count.Sort();
            int minSum = 0, maxSum = 0, c = count.Count;
            for (int i = 0; i < c; ++i)
            {
                minSum += count[i] * costs[c - 1 - i];
                maxSum += count[i] * costs[n - c + i];
            }
            Console.WriteLine($"{minSum} {maxSum}");
            //Console.ReadKey();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var nums = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            int n = nums[0], m = nums[1];
            var costs = Console.ReadLine().Split(' ').Take(n).Select(int.Parse).ToList();
            //for (int i = 0; i < n; i++)
            //{
            //    costs.Add(int.Parse(Console.ReadLine()));
            //}
            var stats = new Dictionary<string, int>();
            for (int i = 0; i < m; i++)
            {
                var fruit = Console.ReadLine();
                if (stats.Count(p => p.Key == fruit) > 0) ++stats[fruit];
                else stats.Add(fruit, 1);
            }
            var count = new List<int>();
            foreach (var val in stats.Values)
            {
                count.Add(val);
            }
            costs.Sort(); count.Sort();
            int minSum = 0, maxSum = 0, c = count.Count;
            for (int i = 0; i < c; i++)
            {
                minSum += count[i] * costs[c - 1 - i];
                maxSum += count[i] * costs[n - c + i];
            }
            Console.WriteLine($"{minSum} {maxSum}");
            //Console.ReadKey();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Contest12.Q3
{
    class Program
    {
        static void Main(string[] args)
        {
            var line1 = Console.ReadLine().Split(' ').Select(i => Convert.ToInt32(i)).ToArray();
            var n = line1[0];
            var m = line1[1];

            var costs = Console.ReadLine().Split(' ').Select(i => Convert.ToInt32(i)).ToArray();

            var d = new Dictionary<string, int>();
            for (int i = 0; i < m; i++)
            {
                var fruit = Console.ReadLine();
                if (d.ContainsKey(fruit))
                {
                    d[fruit]++;
                }
                else
                {
                    d.Add(fruit,1);
                }
            }

            var fc = d.Values.ToArray();
            fc = fc.OrderByDescending(i => i).ToArray();

            costs = costs.OrderByDescending(c => c).ToArray();
            int max = 0;
            for (int i = 0; i < fc.Length; i++)
            {
                max += costs[i] * fc[i];
            }

            costs = costs.OrderBy(c => c).ToArray();
            int min = 0;
            for (int i = 0; i < fc.Length; i++)
            {
                min += costs[i] * fc[i];
            }

            Console.WriteLine("{0} {1}", min, max);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace code_22
{
    class Program
    {
        static void Main(string[] args)
        {

            string s =Console.ReadLine();
            int k = int.Parse(s.Substring(0,s.IndexOf(' ')));
            int n = int.Parse(s.Substring(s.IndexOf(' ')+1));
            int[] mas;
            SortedDictionary<string,int >d = new SortedDictionary<string,int>();
            int min=0, max=0;
            s = Console.ReadLine();
            mas = (from v in s.Split(' ') let q = int.Parse(v) select q).ToArray();
            int n2 = 0;
            for (int i = 0; i < n; i++)
            {
                s = Console.ReadLine();
                if (d.ContainsKey(s))
                    d[s]++;
                else
                {
                    n2++;
                    d[s] = 1;
                }
            }
            n = n2;
            int [] m = new int[n];
            int j = 0;
            foreach (var v in d)
            {
                m[j] = v.Value;
                j++;

            }
            Array.Sort(m);
            int[] m2 = new int[n];
            int[] m3 = new int[k];

            m2=m.Reverse().ToArray();
            Array.Sort(mas);
            for (int i = 0; i < n; i++)
                min += m2[i] * mas[i];
            m3=mas.Reverse().ToArray();
            for (int i = 0; i < n; i++)
                max += m2[i] * m3[i];
            Console.WriteLine(min+" "+max);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CodeForces
{
    class Tokenizer
    {
        TextReader reader;
        TextWriter writer;
        string[] tokens;
        int cur;

        public Tokenizer(TextReader reader)
        {
            this.reader = reader;
        }

        public string NextLine()
        {
            return reader.ReadLine();
        }

        public string NextString()
        {
            while (tokens == null || cur >= tokens.Length)
            {
                tokens = NextLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                cur = 0;
            }
            return tokens[cur++];
        }

        public int NextInt()
        {
            return int.Parse(NextString());
        }

        public long NextLong()
        {
            return long.Parse(NextString());
        }

        public double NextDouble()
        {
            return long.Parse(NextString());
        }
    }

    class Program
    {
        Tokenizer t;

        public void Solve()
        {
            //TextReader reader = File.OpenText(@"../../in.txt");
            //t = new Tokenizer(reader);
            t = new Tokenizer(Console.In);
            int m, n;
            n = t.NextInt();
            m = t.NextInt();

            int[] prices = new int[n];

            for (int i = 0; i < n; i++)
                prices[i] = t.NextInt();

            Array.Sort(prices);

            Dictionary<string, int> d = new Dictionary<string, int>();
            for (int i = 0; i < m; i++)
            {
                string f = t.NextString();
                if (d.ContainsKey(f))
                    d[f]++;
                else
                    d.Add(f, 1);
            }

            int[] req = d.Values.ToArray<int>();
            Array.Sort(req);
            Array.Reverse(req);

            int low = 0;
            for (int i = 0; i < req.Length; i++)
                low += req[i] * prices[i];

            Array.Reverse(prices);
            int high = 0;
            for (int i = 0; i < req.Length; i++)
                high += req[i] * prices[i];

            Console.WriteLine(low + " " + high);
        }

        static void Main(string[] args)
        {
            new Program().Solve();
            Console.ReadLine();
        }

    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CodeForces
{
    class Tokenizer
    {
        TextReader reader;
        TextWriter writer;
        string[] tokens;
        int cur;

        public Tokenizer(TextReader reader)
        {
            this.reader = reader;
        }

        public string NextLine()
        {
            return reader.ReadLine();
        }

        public string NextString()
        {
            while (tokens == null || cur >= tokens.Length)
            {
                tokens = NextLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                cur = 0;
            }
            return tokens[cur++];
        }

        public int NextInt()
        {
            return int.Parse(NextString());
        }

        public long NextLong()
        {
            return long.Parse(NextString());
        }

        public double NextDouble()
        {
            return long.Parse(NextString());
        }
    }

    class Program
    {
        Tokenizer t;

        public void Solve()
        {
            TextReader reader = File.OpenText(@"../../in.txt");
            t = new Tokenizer(reader);
            //t = new Tokenizer(Console.In);
            int m, n;
            n = t.NextInt();
            m = t.NextInt();

            int[] prices = new int[n];

            for (int i = 0; i < n; i++)
                prices[i] = t.NextInt();

            Array.Sort(prices);

            Dictionary<string, int> d = new Dictionary<string, int>();
            for (int i = 0; i < m; i++)
            {
                string f = t.NextString();
                if (d.ContainsKey(f))
                    d[f]++;
                else
                    d.Add(f, 1);
            }

            int[] req = d.Values.ToArray<int>();
            Array.Sort(req);
            Array.Reverse(req);

            int low = 0;
            for (int i = 0; i < req.Length; i++)
                low += req[i] * prices[i];

            Array.Reverse(prices);
            int high = 0;
            for (int i = 0; i < req.Length; i++)
                high += req[i] * prices[i];

            Console.WriteLine(low + " " + high);
        }

        static void Main(string[] args)
        {
            new Program().Solve();
            Console.ReadLine();
        }

    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace CodeForces
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();

            int[] a = p.GetFruits();
            Console.WriteLine("{0} {1}", a[0], a[1]);
            //Console.ReadLine();
        }

        public int[] GetFruits()
        {
            int i = 0;
            int n = 0;
            int m = 0;
            int min = 0;
            int max = 0;
            int[] price = null;
            int[] count = null;
            string[] items = null;
            string temp = Console.ReadLine();
            Dictionary<string, int> pair = new Dictionary<string, int>();

            n = Convert.ToInt32(temp.Split(' ')[0]);
            m = Convert.ToInt32(temp.Split(' ')[1]);

            price = new int[n];
            items = Console.ReadLine().Split(' ');
            for (i = 0; i < n; i++)
            {
                price[i] = Convert.ToInt32(items[i]);
            }
            Array.Sort(price);

            for (i = 0; i < m; i++)
            {
                temp = Console.ReadLine();
                if (pair.ContainsKey(temp))
                {
                    pair[temp]++;
                }
                else
                {
                    pair.Add(temp, 1);
                }
            }

            i = 0;
            count = new int[pair.Count];
            foreach (KeyValuePair<string, int> kvp in pair)
            {
                count[i++] = kvp.Value;
            }
            Array.Sort(count);

            for (i = 0; i < pair.Count; i++)
            {
                min += count[count.Length - i - 1] * price[i];
                max += count[count.Length - i - 1] * price[price.Length - i - 1];
            }

            return new int[] { min, max };
        }
    }
}