#include<bits/stdc++.h>
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define int long long
using namespace std;
typedef double DB;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<PII> VPII;
//head
const int N=1e5+5;
int sum1[N],sum2[N],a[N],b[N],cnt1,cnt2,n,m,d;
signed main() {
	scanf("%lld%lld%lld",&n,&d,&m);
	for(int i=1;i<=n;i++) {
		int x;
		scanf("%lld",&x);
		if(x<=m) a[++cnt1]=x;
		else b[++cnt2]=x;
	}
	sort(a+1,a+1+cnt1,greater<int>());
	sort(b+1,b+1+cnt2,greater<int>());
	for(int i=1;i<=cnt1;i++) sum1[i]=sum1[i-1]+a[i];
	for(int i=1;i<=cnt2;i++) sum2[i]=sum2[i-1]+b[i];
	int res=sum1[cnt1];
	for(int i=1;i<=cnt2;i++) {
		int t=n-(d+1)*i+d;
		if(t<0) break;
		res=max(res,sum1[min(t,cnt1)]+sum2[i]);
	}
	printf("%lld\n",res);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <stdlib.h>
#include <stack>
#include <queue>
#define ri register int
#define int long long

inline int read() {
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	return x*f;
}

const int N=1e5+5;
int a[N],n,m,d;
int b[N],cnt1=0,c[N],cnt2=0,p=1;
int ans=0;

bool cmp(int x,int y){
	return x>y;
}

signed main(void){
	n=read(),d=read(),m=read();
	for(ri i=1;i<=n;i++){
		a[i]=read();
		if(a[i]>m)b[++cnt1]=a[i];
		else c[++cnt2]=a[i];
	}
	std::sort(b+1,b+cnt1+1,cmp);
	std::sort(c+1,c+cnt2+1);
	for(ri i=1;i<cnt1;i++){
		ans+=b[i],p+=d;
		if(p>=n){
			printf("%d\n",ans);exit(0);
		}
	}
	for(ri i=p;i<=cnt2;i++)ans+=c[i];
	printf("%lld\n",ans+b[cnt1]);
    return 0;
}


#include <bits/stdc++.h>
#define For(i,a,b) for ( int i=(a);i<=(b);i++ )
#define Dow(i,b,a) for ( int i=(b);i>=(a);i-- )
#define GO(i,x) for ( int i=head[x];i;i=e[i].nex )
#define mem(x,s) memset(x,s,sizeof(x))
#define cpy(x,s) memcpy(x,s,sizeof(x))
#define YES return puts("YES"),0
#define NO return puts("NO"),0
#define GG return puts("-1"),0
#define pb push_back
#define int long long
using namespace std;

struct IO
{
	#define MAXSIZE (1 << 20)
	#define isdigit(x) (x >= '0' && x <= '9')
  	char buf[MAXSIZE],*p1,*p2;
  	char pbuf[MAXSIZE],*pp;
  	IO():p1(buf),p2(buf),pp(pbuf){}
  	inline char gc() {
    	return getchar();
    	if (p1==p2) p2=(p1=buf)+fread(buf,1,MAXSIZE,stdin);
    	return p1==p2?' ':*p1++;
  	}
	inline bool blank(char ch) {return ch==' '||ch =='\n'||ch == '\r'||ch == '\t';}
  	template <class T>
  	inline void read(T &x)
	{
	    register double tmp=1;register bool sign=0; x=0;
	    register char ch=gc();
	    for (;!isdigit(ch);ch=gc()) if(ch=='-') sign=1;
	    for (;isdigit(ch);ch=gc()) x=x*10+(ch-'0');
	    if (ch=='.') for (ch=gc();isdigit(ch);ch=gc()) tmp/=10.0,x+=tmp*(ch-'0');
	    if (sign) x=-x;
  	}
  	inline void read(char *s)
	{
    	register char ch=gc();
		for (;blank(ch);ch=gc());
		for (;!blank(ch);ch=gc()) *s++=ch;
    	*s=0;
  	}
  	inline void read(char &c) {for(c=gc();blank(c);c=gc());}
  	template<class t> inline void write(t x){
		if(x<0) putchar('-'),write(-x);
		else{if(x>9) write(x/10);putchar('0'+x%10);}
	}
} io;

const int mod=1e9+7;
const int mo=998244353;
const int N=2e5+5;

int n,m,d,a[N],b[N],c[N],ans;

signed main()
{
	io.read(n),io.read(d),io.read(m);
	For(i,1,n) io.read(a[i]);
	For(i,1,n)
	{
		if(a[i]>m) c[++c[0]]=a[i];
		else b[++b[0]]=a[i];
	}
	sort(b+1,b+b[0]+1);
	reverse(b+1,b+b[0]+1);
	sort(c+1,c+c[0]+1);
	reverse(c+1,c+c[0]+1);
	For(i,2,n) b[i]+=b[i-1],c[i]+=c[i-1];
	b[0]=c[0]=0;
	For(i,0,n)
	{
		int yzc=b[i]+c[(n-i)/(d+1)+(bool)((n-i)%(d+1))];
		ans=max(ans,yzc);
	}
	io.write(ans);
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define Pii pair<ll,ll>
#define Pip pair<ll,Pii>
#define fi first
#define se second
#define MAXN 10005
#define MAXT 1005
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair

using namespace std;

ll n,d,m,cnt=0,cnt1=0,cnt2=0,sum=0;
vector<ll> a,a1,b,b1;

int main()
{

    //ifstream cin("MaxFun.inp");
    //ofstream cout("MaxFun.out");
    cin >> n >> d >> m;
    a.pb(-1);
    a1.pb(INF);
    b.pb(0);
    b1.pb(0);
    for(ll i=1; i<=n; i++){
        ll x;
        cin >> x;
        a.pb(x);
        sum+=x;
        b.pb(0);
        if(x>m) cnt1++;
        else{
            a1.pb(x);
            b1.pb(0);
            cnt2++;
        }
    }

    sort(a.begin(),a.end());
    sort(a1.begin(),a1.end(),greater<ll>());
    for(int i=1; i<=n; i++) b[i]=b[i-1]+a[i];
    for(int i=1; i<=cnt2; i++) b1[i]=b1[i-1]+a1[i];
    for(int i=1; i<=cnt1; i++){
        ll j=n-(i-1)*(d+1)-1;
        if(j<0) break;
        j=min(j,cnt2);
        ll s=(b[n]-b[n-i])+(b1[j]);
        sum=max(sum,s);
    }
    cout << sum;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define Pii pair<ll,ll>
#define Pip pair<ll,Pii>
#define fi first
#define se second
#define MAXN 10005
#define MAXT 1005
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair

using namespace std;

ll n,d,m,cnt=0,cnt1=0,cnt2=0,sum=0;
vector<ll> a,b;

int main()
{

    //ifstream cin("MaxFun.inp");
    //ofstream cout("MaxFun.out");
    cin >> n >> d >> m;
    a.pb(-1);
    b.pb(0);
    for(ll i=1; i<=n; i++){
        ll x;
        cin >> x;
        a.pb(x);
        b.pb(0);
        if(x>m) cnt1++;
        else{
            cnt2++;
            sum+=x;
        }
    }

    sort(a.begin(),a.end());
    for(int i=1; i<=n; i++) b[i]=b[i-1]+a[i];
    cnt=min(cnt1-1,(cnt2/d));
    int l=1,r=l+d-1;
    int i,j;
    for(i=n,j=cnt; i>=1,j>0; i--,j--){
        if(r>cnt2){
            break;
        }
        int s=b[r]-b[l-1];
        if(s<=a[i]){
            sum-=s;
            sum+=a[i];
            l=r+1; r=l+d-1;
            cnt1--;
        }
        else break;
    }
    if(cnt1>0){
        int c=cnt1/(d+1) + 1;
        while(c--){
            sum+=a[i];
            i--;
        }
    }
    cout << sum;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define Pii pair<ll,ll>
#define Pip pair<ll,Pii>
#define fi first
#define se second
#define MAXN 10005
#define MAXT 1005
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair

using namespace std;

ll n,d,m,cnt=0,cnt1=0,cnt2=0,sum=0;
vector<ll> a,a1,b,b1;

int main()
{

    //ifstream cin("MaxFun.inp");
    //ofstream cout("MaxFun.out");
    cin >> n >> d >> m;
    a.pb(-1);
    a1.pb(INF);
    b.pb(0);
    b1.pb(0);
    for(ll i=1; i<=n; i++){
        ll x;
        cin >> x;
        a.pb(x);
        b.pb(0);
        if(x>m) cnt1++;
        else{
            a1.pb(x);
            b1.pb(0);
            cnt2++;
            sum+=x;
        }
    }

    sort(a.begin(),a.end());
    sort(a1.begin(),a1.end(),greater<ll>());
    for(int i=1; i<=n; i++) b[i]=b[i-1]+a[i];
    for(int i=1; i<=cnt2; i++) b1[i]=b1[i-1]+a1[i];
    for(int i=1; i<=cnt1; i++){
        ll j=n-(i-1)*(d+1)-1;
        if(j<0) break;
        j=min(j,cnt2);
        ll s=(b[n]-b[n-i])+(b1[j]);
        sum=max(sum,s);
    }
    cout << sum;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define Pii pair<ll,ll>
#define Pip pair<ll,Pii>
#define fi first
#define se second
#define MAXN 10005
#define MAXT 1005
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair

using namespace std;

ll n,d,m,cnt=0,cnt1=0,cnt2=0,sum=0;
vector<ll> a,b;

int main()
{

    //ifstream cin("MaxFun.inp");
    //ofstream cout("MaxFun.out");
    cin >> n >> d >> m;
    a.pb(-1);
    b.pb(0);
    for(ll i=1; i<=n; i++){
        ll x;
        cin >> x;
        a.pb(x);
        b.pb(0);
        if(x>m) cnt1++;
        else cnt2++;
    }

    sort(a.begin(),a.end());
    for(int i=1; i<=n; i++) b[i]=b[i-1]+a[i];
    int l=1, r=l+d-1, i;
    sum=a[n];
    for(i=n-1; i>0; i--){
        if(a[i]<=m) break;
        int s=b[r]-b[l-1];
        if(a[i]>s){
            sum+=a[i];
            l=r+1; r=l+d-1;
        }
        else break;
    }
    int j,lim=min(cnt2,n-1);
    for(j=l; j<=lim; j++) sum+=a[j];
    if(l<j) l=j;
    if(i>=l){
        ll c=(i-l+1)/(d+1);
        while(c--){
            sum+=a[i];
            i--;
        }
    }
    cout << sum;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define Pii pair<ll,ll>
#define Pip pair<ll,Pii>
#define fi first
#define se second
#define MAXN 10005
#define MAXT 1005
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair

using namespace std;

int n,d,m,cnt,sum=0;
vector<int> a;

int main()
{

    //ifstream cin("MaxFun.inp");
    //ofstream cout("MaxFun.out");
    cin >> n >> d >> m;
    a.pb(-1);
    for(int i=1; i<=n; i++){
        int x;
        cin >> x;
        a.pb(x);
        if(x>m) cnt++;
        sum+=x;
    }
    sort(a.begin(),a.end());
    int lim=min(n,(cnt-1)*d);
    for(int i=1; i<=lim; i++) sum-=a[i];
    cout << sum;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define Pii pair<ll,ll>
#define Pip pair<ll,Pii>
#define fi first
#define se second
#define MAXN 10005
#define MAXT 1005
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair

using namespace std;

ll n,d,m,cnt,sum=0;
vector<ll> a,b;

int main()
{

    //ifstream cin("MaxFun.inp");
    //ofstream cout("MaxFun.out");
    cin >> n >> d >> m;
    a.pb(-1);
    b.pb(0);
    for(ll i=1; i<=n; i++){
        ll x;
        cin >> x;
        a.pb(x);
        b.pb(0);
        if(x>m) cnt++;
        else sum+=x;
    }
    sort(a.begin(),a.end());
    for(int i=1; i<=n; i++) b[i]=b[i-1]+a[i];
    int l=1,r=l+d-1;
    int i,j;
    for(i=n,j=cnt-1; i>=1,j>0; i--,j--){
        if(r>=i){
            sum+=a[i];
            break;
        }
        int s=b[r]-b[l-1];
        if(s<=a[i]){
            sum-=s;
            sum+=a[i];
            l=r+1; r=l+d-1;
        }

    }
    if(r<i){
        sum+=a[i];
    }
    cout << sum;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define Pii pair<ll,ll>
#define Pip pair<ll,Pii>
#define fi first
#define se second
#define MAXN 10005
#define MAXT 1005
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair

using namespace std;

ll n,d,m,cnt,sum=0;
vector<ll> a;

int main()
{

    //ifstream cin("MaxFun.inp");
    //ofstream cout("MaxFun.out");
    cin >> n >> d >> m;
    a.pb(-1);
    for(ll i=1; i<=n; i++){
        ll x;
        cin >> x;
        a.pb(x);
        if(x>m) cnt++;
        sum+=x;
    }
    sort(a.begin(),a.end());
    cnt--;
    for(ll i=1; i<=n; i++){

        sum-=a[i];
        if(i%d==0) cnt--;
        if(cnt==0) break;
    }
    cout << sum;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define Pii pair<ll,ll>
#define Pip pair<ll,Pii>
#define fi first
#define se second
#define MAXN 10005
#define MAXT 1005
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair

using namespace std;

ll n,d,m,cnt=0,cnt1=0,cnt2=0,sum=0,D[MAXN];
vector<ll> a,b;

int main()
{

    //ifstream cin("MaxFun.inp");
    //ofstream cout("MaxFun.out");
    cin >> n >> d >> m;
    a.pb(-1);
    b.pb(0);
    for(ll i=1; i<=n; i++){
        ll x;
        cin >> x;
        a.pb(x);
        b.pb(0);
        D[i]=0;
        if(x>m) cnt1++;
        else cnt2++;
    }

    sort(a.begin(),a.end());
    for(int i=1; i<=n; i++) b[i]=b[i-1]+a[i];
    int l=1, r=l+d-1, i;
    sum=a[n];
    for(i=n-1; i>0; i--){
        if(a[i]<=m) break;
        int s=b[r]-b[l-1];
        if(a[i]>s){
            sum+=a[i];
            l=r+1; r=l+d-1;
        }
        else break;
    }
    int j;
    for(j=l; j<=cnt2; j++) sum+=a[j];
    if(l<j) l=j;
    int c=(i-l+1)/(d+1);
    double c1=(i-l+1)/(d+1), c2=c;
    if(c1>c2) c++;
    while(c--){
        sum+=a[i];
        i--;
    }
    cout << sum;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define Pii pair<ll,ll>
#define Pip pair<ll,Pii>
#define fi first
#define se second
#define MAXN 10005
#define MAXT 1005
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair

using namespace std;

ll n,d,m,cnt,sum=0;
vector<ll> a,b;

int main()
{

    //ifstream cin("MaxFun.inp");
    //ofstream cout("MaxFun.out");
    cin >> n >> d >> m;
    a.pb(-1);
    b.pb(0);
    for(ll i=1; i<=n; i++){
        ll x;
        cin >> x;
        a.pb(x);
        b.pb(0);
        if(x>m) cnt++;
        else sum+=x;
    }
    sort(a.begin(),a.end());
    for(int i=1; i<=n; i++) b[i]=b[i-1]+a[i];
    int lim=n-cnt;
    int l=1,r=l+d-1;
    int i,j;
    for(i=n,j=cnt-1; i>=1,j>0; i--,j--){
        if(r>lim){
            break;
        }
        int s=b[r]-b[l-1];
        if(s<=a[i]){
            sum-=s;
            sum+=a[i];
            l=r+1; r=l+d-1;
        }

    }
    if(j==0){

            sum+=a[i];

    }
    else{
        int c=(i-lim)/(d+1) + 1;
        while(c--){
            sum+=a[i];
            i--;
        }
    }
    cout << sum;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define Pii pair<ll,ll>
#define Pip pair<ll,Pii>
#define fi first
#define se second
#define MAXN 10005
#define MAXT 1005
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair

using namespace std;

ll n,d,m,cnt,sum=0;
vector<ll> a;

int main()
{

    //ifstream cin("MaxFun.inp");
    //ofstream cout("MaxFun.out");
    cin >> n >> d >> m;
    a.pb(-1);
    for(ll i=1; i<=n; i++){
        ll x;
        cin >> x;
        a.pb(x);
        if(x>m) cnt++;
        sum+=x;
    }
    sort(a.begin(),a.end());
    ll lim=min(n,(cnt-1)*d);
    for(ll i=1; i<=lim; i++) sum-=a[i];
    cout << sum;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define Pii pair<ll,ll>
#define Pip pair<ll,Pii>
#define fi first
#define se second
#define MAXN 10005
#define MAXT 1005
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair

using namespace std;

ll n,d,m,cnt=0,cnt1=0,cnt2=0,sum=0;
vector<ll> a,a1,b,b1;

int main()
{

    //ifstream cin("MaxFun.inp");
    //ofstream cout("MaxFun.out");
    cin >> n >> d >> m;
    a.pb(-1);
    a1.pb(INF);
    b.pb(0);
    b1.pb(0);
    for(ll i=1; i<=n; i++){
        ll x;
        cin >> x;
        a.pb(x);
        b.pb(0);
        if(x>m) cnt1++;
        else{
            a1.pb(x);
            b1.pb(0);
            cnt2++;
        }
    }

    sort(a.begin(),a.end());
    sort(a1.begin(),a1.end(),greater<ll>());
    for(int i=1; i<=n; i++) b[i]=b[i-1]+a[i];
    for(int i=1; i<=cnt2; i++) b1[i]=b1[i-1]+a1[i];
    for(int i=1; i<=cnt1; i++){
        ll j=n-(i-1)*(d+1)-1;
        if(j<0) break;
        j=min(j,cnt2);
        ll s=(b[n]-b[n-i])+(b1[j]);
        sum=max(sum,s);
    }
    cout << sum;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define Pii pair<ll,ll>
#define Pip pair<ll,Pii>
#define fi first
#define se second
#define MAXN 10005
#define MAXT 1005
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair

using namespace std;

ll n,d,m,cnt,sum=0;
vector<ll> a,b;

int main()
{

    //ifstream cin("MaxFun.inp");
    //ofstream cout("MaxFun.out");
    cin >> n >> d >> m;
    a.pb(-1);
    b.pb(0);
    for(ll i=1; i<=n; i++){
        ll x;
        cin >> x;
        a.pb(x);
        b.pb(0);
        if(x>m) cnt++;
        else sum+=x;
    }
    sort(a.begin(),a.end());
    for(int i=1; i<=n; i++) b[i]=b[i-1]+a[i];
    int lim=n-cnt;
    int l=1,r=l+d-1;
    int i,j;
    for(i=n,j=cnt-1; i>=1,j>0; i--,j--){
        if(r>lim){
            break;
        }
        int s=b[r]-b[l-1];
        if(s<=a[i]){
            sum-=s;
            sum+=a[i];
            l=r+1; r=l+d-1;
        }

    }
    if(j==0){

            sum+=a[i];

    }
    else if(j>0){
        int c=(i-lim)/(d+1) + 1;
        while(c--){
            sum+=a[i];
            i--;
        }
    }
    cout << sum;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define Pii pair<ll,ll>
#define Pip pair<ll,Pii>
#define fi first
#define se second
#define MAXN 10005
#define MAXT 1005
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair

using namespace std;

ll n,d,m,cnt=0,cnt1=0,cnt2=0,sum=0;
vector<ll> a,b;

int main()
{

    //ifstream cin("MaxFun.inp");
    //ofstream cout("MaxFun.out");
    cin >> n >> d >> m;
    a.pb(-1);
    b.pb(0);
    for(ll i=1; i<=n; i++){
        ll x;
        cin >> x;
        a.pb(x);
        b.pb(0);
        if(x>m) cnt1++;
        else cnt2++;
    }

    sort(a.begin(),a.end());
    for(int i=1; i<=n; i++) b[i]=b[i-1]+a[i];
    int l=1, r=l+d-1, i;
    sum=a[n];
    for(i=n-1; i>0; i--){
        if(a[i]<=m) break;
        int s=b[r]-b[l-1];
        if(a[i]>s){
            sum+=a[i];
            l=r+1; r=l+d-1;
        }
        else break;
    }
    int j;
    for(j=l; j<=cnt2; j++) sum+=a[j];
    if(l<j) l=j;
    ll c=(i-l+1)/(d+1);
    while(c--){
        sum+=a[i];
        i--;
    }
    cout << sum;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define Pii pair<ll,ll>
#define Pip pair<ll,Pii>
#define fi first
#define se second
#define MAXN 10005
#define MAXT 1005
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair

using namespace std;

ll n,d,m,cnt=0,cnt1=0,cnt2=0,sum=0;
vector<ll> a,b;

int main()
{

    //ifstream cin("MaxFun.inp");
    //ofstream cout("MaxFun.out");
    cin >> n >> d >> m;
    a.pb(-1);
    b.pb(0);
    for(ll i=1; i<=n; i++){
        ll x;
        cin >> x;
        a.pb(x);
        b.pb(0);
        if(x>m) cnt1++;
        else cnt2++;
    }

    sort(a.begin(),a.end());
    for(int i=1; i<=n; i++) b[i]=b[i-1]+a[i];
    int l=1, r=l+d-1, i;
    sum=a[n];
    for(i=n-1; i>0; i--){
        if(a[i]<=m) break;
        int s=b[r]-b[l-1];
        if(a[i]>s){
            sum+=a[i];
            l=r+1; r=l+d-1;
        }
        else break;
    }
    int j;
    for(j=l; j<=cnt2; j++) sum+=a[j];
    if(l<j) l=j;
    ll c=(i-l+1)/(d+1);
    double c1=(i-l+1)/(d+1), c2=c;
    if(c1>c2) c++;
    while(c--){
        sum+=a[i];
        i--;
    }
    cout << sum;
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5+5;
int n,d,m;
int a[N];
int q1[N],r1,sumq1[N];
int q2[N],r2,sumq2[N];
int ans;
inline bool cmp(int a,int b)
{
	return a>b;
}
signed main()
{
	scanf("%lld%lld%lld",&n,&d,&m);
	for(int i=1;i<=n;++i)
	{
		scanf("%lld",&a[i]);
		if(a[i]>m)
		{
			q1[++r1]=a[i];
		}
		else
		{
			q2[++r2]=a[i];
		}
	}
	sort(q1+1,q1+r1+1,cmp);
	sort(q2+1,q2+r2+1,cmp);
	for(int i=1;i<=r1;++i) sumq1[i]=sumq1[i-1]+q1[i];
	for(int i=1;i<=r2;++i) sumq2[i]=sumq2[i-1]+q2[i];
	for(int i=r2+1;i<=200000;++i) sumq2[i]=sumq2[r2];
	for(int i=0;i<=r1&&(i-1)*(d+1)+1<=n;++i)
	{
		int anss=0;
		anss=sumq1[i];
		int em=n-((i-1)*(d+1)+1);
		anss+=sumq2[em];
		ans=max(ans,anss);
	}
	printf("%lld",ans);
}


#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
int n,d,m;
int a[N];
int q1[N],r1,sumq1[N];
int q2[N],r2,sumq2[N];
int ans;
inline bool cmp(int a,int b)
{
	return a>b;
}
int main()
{
	scanf("%d%d%d",&n,&d,&m);
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&a[i]);
		if(a[i]>m)
		{
			q1[++r1]=a[i];
		}
		else
		{
			q2[++r2]=a[i];
		}
	}
	sort(q1+1,q1+r1+1,cmp);
	sort(q2+1,q2+r2+1,cmp);
	for(int i=1;i<=r1;++i) sumq1[i]=sumq1[i-1]+q1[i];
	for(int i=1;i<=r2;++i) sumq2[i]=sumq2[i-1]+q2[i];
	for(int i=r2+1;i<=200000;++i) sumq2[i]=sumq2[r2];
	for(int i=0;i<=r1&&(i-1)*(d+1)+1<=n;++i)
	{
		int anss=0;
		anss=sumq1[i];
		int em=n-((i-1)*(d+1)+1);
		anss+=sumq2[em];
		ans=max(ans,anss);
	}
	printf("%d",ans);
}


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;

const ll N=100010;
ll n,m,d,a[N],ans,sum[N],now;

inline ll read(){
    ll x=0,tmp=1;
    char ch=getchar();
    while(!isdigit(ch)){
        if(ch=='-') tmp=-1;
        ch=getchar();
    }
    while(isdigit(ch)){
        x=(x<<3)+(x<<1)+(ch^48);
        ch=getchar();
    }
    return tmp*x;
}

int main(){
    n=read(); d=read(); m=read();
    for(ll i=1; i<=n; i++) a[i]=read();
    sort(a+1,a+1+n);
    for(now=1; now<=n; now++){
        if(a[now]>m) break;
    }
    for(ll i=1; i<=n; i++) sum[i]=sum[i-1]+a[i];
    if(now==n+1) return printf("%lld\n",sum[n])&0;
    for(ll i=n; i>=now; i--){
        ll used=n-i+1;
        if(n-((used-1)*(d+1)+1)<=now-1) ans=max(ans,sum[n]-sum[i-1]+sum[now-1]-sum[(used-1)*d]);
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;

const ll N=100010;
ll n,m,d,ans,small[N],big[N],sums[N],sumb[N],cnts,cntb;

inline ll read(){
    ll x=0,tmp=1;
    char ch=getchar();
    while(!isdigit(ch)){
        if(ch=='-') tmp=-1;
        ch=getchar();
    }
    while(isdigit(ch)){
        x=(x<<3)+(x<<1)+(ch^48);
        ch=getchar();
    }
    return tmp*x;
}

inline bool cmp(ll x,ll y){
    return x>y;
}

int main(){
    n=read(); d=read(); m=read();
    for(ll i=1; i<=n; i++){
        ll x=read();
        if(x<=m) small[++cnts]=x;
        else big[++cntb]=x;
    }
    sort(small+1,small+1+cnts,cmp); sort(big+1,big+1+cntb,cmp);
    for(ll i=1; i<=cnts; i++) sums[i]=sums[i-1]+small[i];
    for(ll i=1; i<=cntb; i++) sumb[i]=sumb[i-1]+big[i];
    for(ll i=0; i<=cnts; i++) ans=max(ans,sums[i]+sumb[min(cntb,(ll)ceil((n-i)*1.0/(d+1)))]);
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;

const ll N=100010;
ll n,m,d,ans,small[N],big[N],sums[N],sumb[N],cnts,cntb;

inline ll read(){
    ll x=0,tmp=1;
    char ch=getchar();
    while(!isdigit(ch)){
        if(ch=='-') tmp=-1;
        ch=getchar();
    }
    while(isdigit(ch)){
        x=(x<<3)+(x<<1)+(ch^48);
        ch=getchar();
    }
    return tmp*x;
}

inline bool cmp(ll x,ll y){
    return x>y;
}

int main(){
    n=read(); d=read(); m=read();
    for(ll i=1; i<=n; i++){
        ll x=read();
        if(x<=m) small[++cnts]=x;
        else big[++cntb]=x;
    }
    sort(small+1,small+1+cnts,cmp); sort(big+1,big+1+cntb,cmp);
    for(ll i=1; i<=cnts; i++) sums[i]=sums[i-1]+small[i];
    for(ll i=1; i<=cntb; i++) sumb[i]=sumb[i-1]+big[i];
    for(ll i=1; i<=cnts; i++) ans=max(ans,sums[i]+sumb[min(cntb,(ll)ceil((n-i)*1.0/(d+1)))]);
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;

const ll N=100010;
ll n,m,d,a[N],ans,sum[N],now;

inline ll read(){
    ll x=0,tmp=1;
    char ch=getchar();
    while(!isdigit(ch)){
        if(ch=='-') tmp=-1;
        ch=getchar();
    }
    while(isdigit(ch)){
        x=(x<<3)+(x<<1)+(ch^48);
        ch=getchar();
    }
    return tmp*x;
}

int main(){
    n=read(); d=read(); m=read();
    for(ll i=1; i<=n; i++) a[i]=read();
    sort(a+1,a+1+n);
    for(now=1; now<=n; now++){
        if(a[now]>m) break;
    }
    for(ll i=1; i<=n; i++) sum[i]=sum[i-1]+a[i];
    if(now==n+1) return printf("%lld\n",sum[n])&0;
    for(ll i=n; i>=now; i--){
        ll used=n-i+1;
        if((used-1)*d<=now-1) ans=max(ans,sum[n]-sum[i-1]+sum[now-1]-sum[(used-1)*d]);
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename has_less>
using ordered_set =
tree<has_less,
    null_type,
    less<has_less>,
    rb_tree_tag,
    tree_order_statistics_node_update>;

typedef long double ld;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define pb push_back
#define F first
#define S second
#define ALL(v) v.begin(),v.end()
#define fast_cin() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define lc sidx * 2
#define rc lc + 1
#define smid ((i + j) >> 1)

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const int mod = 1e9+7;
const ll inf = 5e18;

int main() {
    fast_cin();
    int n, d, m; cin >> n >> d >> m;
    vector <ll> small, large;
    for(int i= 1; i <= n; i++) {
        int x; cin >> x;
        if(x > m) {
            large.pb(x);
        } else {
            small.pb(x);
        }
    }
    sort(ALL(large));
    reverse(ALL(large));

    sort(ALL(small));
    reverse(ALL(small));

    if(large.empty()) {
        ll sum = 0;
        for(auto x: small) {
            sum += x;
        }
        cout << sum << endl;
    } else {
        int si = small.size(), li = 0;
        ll ans = 0, mans;
        for(auto x: small) {
            ans += x;
        }
        mans = ans;
        int nl;
        for(int i = si + 1; i <= n; i += d + 1) {
            ans += large[li++];
            nl = i;
        }
        nl += d + 1;
        --si;
        trace(ans);
        while(si >= 1) {
            ans -= small[si--];
            nl--;
            if(nl == n) {
                ans += large[li++];
                nl += d + 1;
            }
            mans = max(mans, ans);
        }
        cout << mans << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;

bool seen[N];

long long solve(int i, int d, long long dp[], int a[], int n, int m) {
	if(i > n) {
		return 0ll;
	}
	if(seen[i]) {
		return dp[i];
	}
	seen[i] = true;
	long long &candidate = dp[i];
	if(a[i] <= m) {
		return solve(i + 1, d, dp, a, n, m) + a[i];
	}
	candidate = solve(i + 1, d, dp, a, n, m);
	candidate = max(candidate, solve(i + d + 1, d, dp, a, n, m) + a[i]);
	return candidate;
}

int main() {
	int n, d, m; cin >> n >> d >> m;
	int a[n + 1];
	vector <int> nv, big;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		if(a[i] <= m) {
			nv.push_back(a[i]);
		} else {
			big.push_back(a[i]);
		}
	}
	sort(big.begin(), big.end());
	int b[n + 1];
	memset(b, -1, sizeof(b));
	for(int i = 1; i <= n and !big.empty(); i += d + 1) {
		b[i] = big.back();
		big.pop_back();
	}
	for(int x: big) {
		nv.push_back(x);
	}
	sort(nv.begin(), nv.end());
	int nidx = 0;
	for(int i = 1; i <= n; i++) {
		if(b[i] == -1) {
			b[i] = nv[nidx++];
		}
	}
	reverse(b + 1, b + n + 1);
	for(int i = 1; i <= n; i++) {
		cerr << b[i] << ' ';
	}
	cerr << endl;
	// cerr << "HERE" << endl;
	long long dp[n + 1];
	memset(dp, 0, sizeof(dp));
	cout << solve(1, d, dp, b, n, m) << endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename has_less>
using ordered_set =
tree<has_less,
    null_type,
    less<has_less>,
    rb_tree_tag,
    tree_order_statistics_node_update>;

typedef long double ld;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define pb push_back
#define F first
#define S second
#define ALL(v) v.begin(),v.end()
#define fast_cin() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define lc sidx * 2
#define rc lc + 1
#define smid ((i + j) >> 1)

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const int mod = 1e9+7;
const ll inf = 5e18;

int main() {
    fast_cin();
    int n, d, m; cin >> n >> d >> m;
    vector <ll> small, large;
    for(int i= 1; i <= n; i++) {
        int x; cin >> x;
        if(x > m) {
            large.pb(x);
        } else {
            small.pb(x);
        }
    }
    sort(ALL(large));
    reverse(ALL(large));

    sort(ALL(small));
    reverse(ALL(small));

    if(large.empty()) {
        ll sum = 0;
        for(auto x: small) {
            sum += x;
        }
        cout << sum << endl;
    } else {
        int si = small.size(), li = 0;
        ll ans = 0, mans;
        for(auto x: small) {
            ans += x;
        }
        mans = ans;
        int nl;
        for(int i = si + 1; i <= n; i += d + 1) {
            ans += large[li++];
            nl = i;
        }
        nl += d + 1;
        --si;
        trace(ans);
        while(si >= 0) {
            ans -= small[si--];
            nl--;
            if(nl == n and li < large.size()) {
                ans += large[li++];
                nl += d + 1;
            }
            mans = max(mans, ans);
        }
        cout << mans << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;

bool seen[N];

long long solve(int i, int d, long long dp[], int a[], int n, int m) {
	if(i > n) {
		return 0ll;
	}
	if(seen[i]) {
		return dp[i];
	}
	seen[i] = true;
	long long &candidate = dp[i];
	if(a[i] <= m) {
		return solve(i + 1, d, dp, a, n, m) + a[i];
	}
	candidate = solve(i + 1, d, dp, a, n, m);
	candidate = max(candidate, solve(i + d + 1, d, dp, a, n, m) + a[i]);
	return candidate;
}

int main() {
	int n, d, m; cin >> n >> d >> m;
	int a[n + 1];
	vector <int> nv, big;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		if(a[i] <= m) {
			nv.push_back(a[i]);
		} else {
			big.push_back(a[i]);
		}
	}
	sort(big.begin(), big.end());
	int b[n + 1];
	memset(b, -1, sizeof(b));
	for(int i = 1; i <= n and !big.empty(); i += d + 1) {
		b[i] = big.back();
		big.pop_back();
	}
	for(int x: big) {
		nv.push_back(x);
	}
	sort(nv.begin(), nv.end());
	int nidx = 0;
	for(int i = 1; i <= n; i++) {
		if(b[i] == -1) {
			if(nidx < nv.size()) {
				b[i] = nv[nidx++];
			} else {
				b[i] = big.back();
				big.pop_back();
			}
		}
	}
	reverse(b + 1, b + n + 1);
	for(int i = 1; i <= n; i++) {
		cerr << b[i] << ' ';
	}
	cerr << endl;
	// cerr << "HERE" << endl;
	long long dp[n + 1];
	memset(dp, 0, sizeof(dp));
	cout << solve(1, d, dp, b, n, m) << endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename has_less>
using ordered_set =
tree<has_less,
	null_type,
	less<has_less>,
	rb_tree_tag,
	tree_order_statistics_node_update>;

typedef long double ld;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define pb push_back
#define F first
#define S second
#define ALL(v) v.begin(),v.end()
#define fast_cin() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define lc sidx * 2
#define rc lc + 1
#define smid ((i + j) >> 1)

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const int mod = 1e9+7;
const ll inf = 5e18;

int main() {
	fast_cin();
	int n, d, m; cin >> n >> d >> m;
	vector <ll> small, large;
	for(int i= 1; i <= n; i++) {
		int x; cin >> x;
		if(x > m) {
			large.pb(x);
		} else {
			small.pb(x);
		}
	}
	sort(ALL(large));
	reverse(ALL(large));

	sort(ALL(small));
	reverse(ALL(small));

	if(large.empty()) {
		ll sum = 0;
		for(auto x: small) {
			sum += x;
		}
		cout << sum << endl;
	} else {
		int si = small.size(), li = 0;
		ll ans = 0, mans;
		for(auto x: small) {
			ans += x;
		}
		int nl;
		for(int i = si + 1; i <= n; i += d + 1) {
			ans += large[li++];
			nl = i;
		}
		mans = ans;
		nl += d + 1;
		--si;
		trace(ans);
		while(si >= 0) {
			ans -= small[si--];
			nl--;
			if(nl == n and li < large.size()) {
				ans += large[li++];
				nl += d + 1;
			}
			mans = max(mans, ans);
		}
		cout << mans << endl;
	}
}


// Problem: CF1394A Boboniu Chats with Du
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1394A
// Memory Limit: 250 MB
// Time Limit: 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define int long long
using namespace std;
template<typename T>inline void read(T &FF){
	T RR=1;FF=0;char CH=getchar();
	for(;!isdigit(CH);CH=getchar())if(CH=='-')RR=-1;
	for(;isdigit(CH);CH=getchar())FF=(FF<<1)+(FF<<3)+(CH^48);
	FF*=RR;
}
int a[100010],q1[100010],q2[100010],sz1,sz2;
int ans,s1[100010],s2[100010];
bool cmp(int x,int y){
	return x>y;
}
signed main(){
	int n,d,m;read(n);read(d);read(m);
	for(int i=1;i<=n;i++){
		read(a[i]);
		if(a[i]>m)q1[++sz1]=a[i];
		else q2[++sz2]=a[i];
	}
	sort(q1+1,q1+sz1+1,cmp);sort(q2+1,q2+sz2+1,cmp);
	for(int i=1;i<=n;i++)s1[i]=s1[i-1]+q1[i];
	for(int i=1;i<=n;i++)s2[i]=s2[i-1]+q2[i];
	for(int i=0;max(0ll,(i-1)*(d+1)+1)<=n&&i<=sz1;i++)ans=max(ans,s1[i]+s2[n-max(0ll,((i-1)*(d+1)+1))]);
	cout<<ans;
	return 0;
}


// Problem: CF1394A Boboniu Chats with Du
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1394A
// Memory Limit: 250 MB
// Time Limit: 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define int __int128
using namespace std;
template<typename T>inline void read(T &FF){
	T RR=1;FF=0;char CH=getchar();
	for(;!isdigit(CH);CH=getchar())if(CH=='-')RR=-1;
	for(;isdigit(CH);CH=getchar())FF=(FF<<1)+(FF<<3)+(CH^48);
	FF*=RR;
}
template<typename T>inline void write(T FF){
	if(FF<0){FF*=-1,putchar('-');}
	if(FF>9)write(FF/10);
	putchar(FF%10+48);
}
int a[100010],q1[100010],q2[100010],sz1,sz2;
int ans,s1[100010],s2[100010];
bool cmp(int x,int y){
	return x>y;
}
signed main(){
	int n,d,m;read(n);read(d);read(m);
	for(int i=1;i<=n;i++){
		read(a[i]);
		if(a[i]>m)q1[++sz1]=a[i];
		else q2[++sz2]=a[i];
	}
	sort(q1+1,q1+sz1+1,cmp);sort(q2+1,q2+sz2+1,cmp);
	for(int i=1;i<=n;i++)s1[i]=s1[i-1]+q1[i];
	for(int i=1;i<=m;i++)s2[i]=s2[i-1]+q2[i];
	for(int i=1;(i-1)*(d+1)+1<=n&&i<=sz1;i++)ans=max(ans,s1[i]+s2[n-((i-1)*(d+1)+1)]);
	write(ans);
	return 0;
}


// Problem: CF1394A Boboniu Chats with Du
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1394A
// Memory Limit: 250 MB
// Time Limit: 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define int long long
using namespace std;
template<typename T>inline void read(T &FF){
	T RR=1;FF=0;char CH=getchar();
	for(;!isdigit(CH);CH=getchar())if(CH=='-')RR=-1;
	for(;isdigit(CH);CH=getchar())FF=(FF<<1)+(FF<<3)+(CH^48);
	FF*=RR;
}
int a[100010],q1[100010],q2[100010],sz1,sz2;
int ans,s1[100010],s2[100010];
bool cmp(int x,int y){
	return x>y;
}
signed main(){
	int n,d,m;read(n);read(d);read(m);
	for(int i=1;i<=n;i++){
		read(a[i]);
		if(a[i]>m)q1[++sz1]=a[i];
		else q2[++sz2]=a[i];
	}
	sort(q1+1,q1+sz1+1,cmp);sort(q2+1,q2+sz2+1,cmp);
	for(int i=1;i<=n;i++)s1[i]=s1[i-1]+q1[i];
	for(int i=1;i<=n;i++)s2[i]=s2[i-1]+q2[i];
	for(int i=1;(i-1)*(d+1)+1<=n&&i<=sz1;i++)ans=max(ans,s1[i]+s2[n-((i-1)*(d+1)+1)]);
	cout<<ans;
	return 0;
}


// Problem: CF1394A Boboniu Chats with Du
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1394A
// Memory Limit: 250 MB
// Time Limit: 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<typename T>inline void read(T &FF){
	T RR=1;FF=0;char CH=getchar();
	for(;!isdigit(CH);CH=getchar())if(CH=='-')RR=-1;
	for(;isdigit(CH);CH=getchar())FF=(FF<<1)+(FF<<3)+(CH^48);
	FF*=RR;
}
int a[100010],q1[100010],q2[100010],sz1,sz2;
long long ans,s1[100010],s2[100010];
bool cmp(int x,int y){
	return x>y;
}
int main(){
	int n,d,m;read(n);read(d);read(m);
	for(int i=1;i<=n;i++){
		read(a[i]);
		if(a[i]>m)q1[++sz1]=a[i];
		else q2[++sz2]=a[i];
	}
	sort(q1+1,q1+sz1+1,cmp);sort(q2+1,q2+sz2+1,cmp);
	for(int i=1;i<=n;i++)s1[i]=s1[i-1]+q1[i];
	for(int i=1;i<=m;i++)s2[i]=s2[i-1]+q2[i];
	for(int i=1;(i-1)*(d+1)+1<=n&&i<=sz1;i++)ans=max(ans,s1[i]+s2[n-((i-1)*(d+1)+1)]);
	cout<<ans;
	return 0;
}


// Problem: CF1394A Boboniu Chats with Du
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1394A
// Memory Limit: 250 MB
// Time Limit: 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<typename T>inline void read(T &FF){
	T RR=1;FF=0;char CH=getchar();
	for(;!isdigit(CH);CH=getchar())if(CH=='-')RR=-1;
	for(;isdigit(CH);CH=getchar())FF=(FF<<1)+(FF<<3)+(CH^48);
	FF*=RR;
}
int a[100010],q1[100010],q2[100010],sz1,sz2;
long long ans,s1[100010],s2[100010];
bool cmp(int x,int y){
	return x>y;
}
int main(){
	int n,d,m;read(n);read(d);read(m);
	for(int i=1;i<=n;i++){
		read(a[i]);
		if(a[i]>m)q1[++sz1]=a[i];
		else q2[++sz2]=a[i];
	}
	sort(q1+1,q1+sz1+1,cmp);sort(q2+1,q2+sz2+1,cmp);
	for(int i=1;i<=sz1;i++)s1[i]=s1[i-1]+q1[i];
	for(int i=1;i<=sz2;i++)s2[i]=s2[i-1]+q2[i];
	for(int i=1;(i-1)*(d+1)+1<=n&&i<=sz1;i++)ans=max(ans,s1[i]+s2[n-((i-1)*(d+1)+1)]);
	cout<<ans;
	return 0;
}


// Problem: CF1394A Boboniu Chats with Du
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1394A
// Memory Limit: 250 MB
// Time Limit: 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define int long long
using namespace std;
template<typename T>inline void read(T &FF){
	T RR=1;FF=0;char CH=getchar();
	for(;!isdigit(CH);CH=getchar())if(CH=='-')RR=-1;
	for(;isdigit(CH);CH=getchar())FF=(FF<<1)+(FF<<3)+(CH^48);
	FF*=RR;
}
int a[100010],q1[100010],q2[100010],sz1,sz2;
int ans,s1[100010],s2[100010];
bool cmp(int x,int y){
	return x>y;
}
signed main(){
	int n,d,m;read(n);read(d);read(m);
	for(int i=1;i<=n;i++){
		read(a[i]);
		if(a[i]>m)q1[++sz1]=a[i];
		else q2[++sz2]=a[i];
	}
	sort(q1+1,q1+sz1+1,cmp);sort(q2+1,q2+sz2+1,cmp);
	for(int i=1;i<=n;i++)s1[i]=s1[i-1]+q1[i];
	for(int i=1;i<=m;i++)s2[i]=s2[i-1]+q2[i];
	for(int i=1;(i-1)*(d+1)+1<=n&&i<=sz1;i++)ans=max(ans,s1[i]+s2[n-((i-1)*(d+1)+1)]);
	cout<<ans;
	return 0;
}


// Problem: CF1394A Boboniu Chats with Du
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1394A
// Memory Limit: 250 MB
// Time Limit: 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<typename T>inline void read(T &FF){
	T RR=1;FF=0;char CH=getchar();
	for(;!isdigit(CH);CH=getchar())if(CH=='-')RR=-1;
	for(;isdigit(CH);CH=getchar())FF=(FF<<1)+(FF<<3)+(CH^48);
	FF*=RR;
}
long long a[100010],q1[100010],q2[100010],sz1,sz2;
long long ans,s1[100010],s2[100010];
bool cmp(int x,int y){
	return x>y;
}
int main(){
	int n,d,m;read(n);read(d);read(m);
	for(int i=1;i<=n;i++){
		read(a[i]);
		if(a[i]>m)q1[++sz1]=a[i];
		else q2[++sz2]=a[i];
	}
	sort(q1+1,q1+sz1+1,cmp);sort(q2+1,q2+sz2+1,cmp);
	for(int i=1;i<=n;i++)s1[i]=s1[i-1]+q1[i];
	for(int i=1;i<=m;i++)s2[i]=s2[i-1]+q2[i];
	for(int i=1;(i-1)*(d+1)+1<=n&&i<=sz1;i++)ans=max(ans,s1[i]+s2[n-((i-1)*(d+1)+1)]);
	cout<<ans;
	return 0;
}


// Problem: CF1394A Boboniu Chats with Du
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1394A
// Memory Limit: 250 MB
// Time Limit: 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
template<typename T>inline void read(T &FF){
	T RR=1;FF=0;char CH=getchar();
	for(;!isdigit(CH);CH=getchar())if(CH=='-')RR=-1;
	for(;isdigit(CH);CH=getchar())FF=(FF<<1)+(FF<<3)+(CH^48);
	FF*=RR;
}
template<typename T>inline void write(T FF){
	if(FF<0){FF*=-1,putchar('-');}
	if(FF>9)write(FF/10);
	putchar(FF%10+48);
}
__int128 a[100010],q1[100010],q2[100010],sz1,sz2,ans,s1[100010],s2[100010];
bool cmp(int x,int y){
	return x>y;
}
signed main(){
	int n,d,m;read(n);read(d);read(m);
	for(int i=1;i<=n;i++){
		read(a[i]);
		if(a[i]>m)q1[++sz1]=a[i];
		else q2[++sz2]=a[i];
	}
	sort(q1+1,q1+sz1+1,cmp);sort(q2+1,q2+sz2+1,cmp);
	for(int i=1;i<=n;i++)s1[i]=s1[i-1]+q1[i];
	for(int i=1;i<=m;i++)s2[i]=s2[i-1]+q2[i];
	for(int i=1;(i-1)*(d+1)+1<=n&&i<=sz1;i++)ans=max(ans,s1[i]+s2[n-((i-1)*(d+1)+1)]);
	write(ans);
	return 0;
}


// Problem: CF1394A Boboniu Chats with Du
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1394A
// Memory Limit: 250 MB
// Time Limit: 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define int long long
using namespace std;
template<typename T>inline void read(T &FF){
	T RR=1;FF=0;char CH=getchar();
	for(;!isdigit(CH);CH=getchar())if(CH=='-')RR=-1;
	for(;isdigit(CH);CH=getchar())FF=(FF<<1)+(FF<<3)+(CH^48);
	FF*=RR;
}
int a[100010],q1[100010],q2[100010],sz1,sz2;
int ans,s1[100010],s2[100010];
bool cmp(int x,int y){
	return x>y;
}
signed main(){
	int n,d,m;read(n);read(d);read(m);
	for(int i=1;i<=n;i++){
		read(a[i]);
		if(a[i]>m)q1[++sz1]=a[i];
		else q2[++sz2]=a[i];
	}
	sort(q1+1,q1+sz1+1,cmp);sort(q2+1,q2+sz2+1,cmp);
	for(int i=1;i<=n;i++)s1[i]=s1[i-1]+q1[i];
	for(int i=1;i<=n;i++)s2[i]=s2[i-1]+q2[i];
	for(int i=0;(i-1)*(d+1)+1<=n&&i<=sz1;i++)ans=max(ans,s1[i]+s2[n-((i-1)*(d+1)+1)]);
	cout<<ans;
	return 0;
}


#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;

int main() {
    ll i,j,k,n,m,d,p,q,ans;
    cin >> n >> d >> m;
    ll a[n],b[n];
    p = q = 0;
    for (i=0; i<n; i++) {
        cin >> k;
        if (k > m) a[p++] = k;
        else b[q++] = k;
    }
    sort(a,a+p,greater<ll>());
    sort(b,b+q,greater<ll>());
    for (i=1; i<p; i++) a[i] += a[i-1];
    for (i=1; i<q; i++) b[i] += b[i-1];
    ans = q?b[q-1]:0;
    for (i=1; i<=p; i++) {
        //cout << ans << endl;
        if ((i-1)*(d+1)+1 <= p) ans = max(ans,a[i-1]+(q?b[q-1]:0));
        else {
            if (q-((i-1)*(d+1)+1-p) == 0) {
                ans = max(ans,a[i-1]);
                break;
            }
            else if (q-((i-1)*(d+1)+1-p) < 0) break;
            ans = max(ans,a[i-1]+b[q-((i-1)*(d+1)+1-p)-1]);
        }
    }
    cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;

int main() {
    ll i,j,k,n,m,d,p,q,ans;
    cin >> n >> d >> m;
    ll a[n],b[n];
    p = q = 0;
    for (i=0; i<n; i++) {
        cin >> k;
        if (k > m) a[p++] = k;
        else b[q++] = k;
    }
    sort(a,a+p,greater<ll>());
    sort(b,b+q,greater<ll>());
    for (i=1; i<p; i++) a[i] += a[i-1];
    for (i=1; i<q; i++) b[i] += b[i-1];
    ans = b[q-1];
    for (i=1; i<=p; i++) {
        //cout << ans << endl;
        if ((i-1)*(d+1)+1 <= p) ans = max(ans,a[i-1]+b[q-1]);
        else {
            if (q-((i-1)*(d+1)+1-p) <= 0) break;
            ans = max(ans,a[i-1]+b[q-((i-1)*(d+1)+1-p)-1]);
        }
    }
    cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;

int main() {
    ll i,j,k,n,m,d,p,q;
    ll ans = 0;
    cin >> n >> d >> m;
    ll a[n],b[n];
    p = q = 0;
    for (i=0; i<n; i++) {
        cin >> k;
        if (k > m) a[p++] = k;
        else b[q++] = k;
    }
    sort(a,a+p,greater<ll>());
    sort(b,b+q,greater<ll>());
    for (i=1; i<p; i++) a[i] += a[i-1];
    for (i=1; i<q; i++) b[i] += b[i-1];
    for (i=1; i<=p; i++) {
        //cout << ans << endl;
        if ((i-1)*(d+1)+1 <= p) ans = max(ans,a[i-1]+b[q-1]);
        else {
            if (q-((i-1)*(d+1)+1-p) <= 0) break;
            ans = max(ans,a[i-1]+b[q-((i-1)*(d+1)+1-p)-1]);
        }
    }
    cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;

int main() {
    ll i,j,k,n,m,d,p,q,ans;
    cin >> n >> d >> m;
    ll a[n],b[n];
    p = q = 0;
    for (i=0; i<n; i++) {
        cin >> k;
        if (k > m) a[p++] = k;
        else b[q++] = k;
    }
    sort(a,a+p,greater<ll>());
    sort(b,b+q,greater<ll>());
    for (i=1; i<p; i++) a[i] += a[i-1];
    for (i=1; i<q; i++) b[i] += b[i-1];
    ans = b[q-1];
    for (i=1; i<=p; i++) {
        //cout << ans << endl;
        if ((i-1)*(d+1)+1 <= p) ans = max(ans,a[i-1]+b[q-1]);
        else {
            if (q-((i-1)*(d+1)+1-p) == 0) {
                ans = max(ans,a[i-1]);
                break;
            }
            else if (q-((i-1)*(d+1)+1-p) < 0) break;
            ans = max(ans,a[i-1]+b[q-((i-1)*(d+1)+1-p)-1]);
        }
    }
    cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;

int main() {
    ll i,j,k,n,m,d,p,q,ans;
    cin >> n >> d >> m;
    ll a[n],b[n];
    p = q = 0;
    for (i=0; i<n; i++) {
        cin >> k;
        if (k > m) a[p++] = k;
        else b[q++] = k;
    }
    sort(a,a+p,greater<ll>());
    sort(b,b+q,greater<ll>());
    for (i=1; i<p; i++) a[i] += a[i-1];
    for (i=1; i<q; i++) b[i] += b[i-1];
    ans = b[q-1];
    for (i=1; i<=p; i++) {
        //cout << ans << endl;
        if ((i-1)*(d+1)+1 <= p) ans = max(ans,a[i-1]+b[q-1]);
        else {
            if (q-((i-1)*(d+1)+1-p) <= 0) {
                ans = max(ans,a[i-1]);
                break;
            }
            ans = max(ans,a[i-1]+b[q-((i-1)*(d+1)+1-p)-1]);
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

#define MAXN 100005

long long prefixS[MAXN][2];
int main() {
#ifdef orange
    freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, d, m;
	cin >> n >> d >> m;
	vector<int> small;
	vector<int> big;
	long long total = 0;
	for (int i = 0; i < n; i++) {
		int temp;
		cin >> temp;
		if (temp <= m) {
			small.push_back(temp);
		} else {
			big.push_back(temp);
		}
		total += temp;
	}
	sort(small.begin(), small.end());
	sort(big.begin(), big.end());
	prefixS[0][0] = 0;
	prefixS[0][1] = 0;
	for (int i = 0; i < (int) small.size(); i++) {
		prefixS[i + 1][0] = prefixS[i][0] + small[i];
	}
	for (int i = 0; i < (int) big.size(); i++) {
		prefixS[i + 1][1] = prefixS[i][1] + big[i];
	}
	long long ans = 0;
	for (int gaps = 0; gaps <= (int) big.size(); gaps++) {
	//	cout << big.size() - gaps << "\n";
		if (min(gaps * d, n - gaps) >= (int) big.size() - gaps) {
			int minNo = max((gaps - 1) * d, (int) big.size() - gaps);
			long long subtract = prefixS[big.size() - gaps][1];
			subtract += prefixS[minNo - (big.size() - gaps)][0];
			ans = max(ans, total - subtract);
		}
	}
	cout << ans << "\n";
}


#include <bits/stdc++.h>

using namespace std;

#define MAXN 100005

long long prefixS[MAXN][2];
int main() {
#ifdef orange
    freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	long long n, d, m;
	cin >> n >> d >> m;
	vector<long long> small;
	vector<long long> big;
	long long total = 0;
	for (int i = 0; i < n; i++) {
		int temp;
		cin >> temp;
		if (temp <= m) {
			small.push_back(temp);
		} else {
			big.push_back(temp);
		}
		total += temp;
	}
	sort(small.begin(), small.end());
	sort(big.begin(), big.end());
	prefixS[0][0] = 0;
	prefixS[0][1] = 0;
	for (int i = 0; i < (int) small.size(); i++) {
		prefixS[i + 1][0] = prefixS[i][0] + small[i];
	}
	for (int i = 0; i < (int) big.size(); i++) {
		prefixS[i + 1][1] = prefixS[i][1] + big[i];
	}
	long long ans = 0;
	for (int gaps = 0; gaps <= (int) big.size(); gaps++) {
		//	cout << big.size() - gaps << "\n";
		if (min(gaps * d, n - gaps) >= (int) big.size() - gaps) {
			long long minNo = max((gaps - 1) * d,
					(long long) big.size() - gaps);
			long long bigA = big.size() - gaps;
			if (minNo - (big.size() - gaps) > small.size()) {
				bigA += minNo - (big.size() - gaps) - small.size();
			}
			if (bigA > big.size())
				continue;
			long long subtract = prefixS[bigA][1];
			//	assert(minNo - (bigA) <= small.size());
			subtract += prefixS[minNo - (bigA)][0];
			ans = max(ans, total - subtract);
		}
	}
	cout << ans << "\n";
}


#include <bits/stdc++.h>

using namespace std;

#define MAXN 100005

long long prefixS[MAXN][2];
int main() {
#ifdef orange
    freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, d, m;
	cin >> n >> d >> m;
	vector<int> small;
	vector<int> big;
	long long total = 0;
	for (int i = 0; i < n; i++) {
		int temp;
		cin >> temp;
		if (temp <= m) {
			small.push_back(temp);
		} else {
			big.push_back(temp);
		}
		total += temp;
	}
	sort(small.begin(), small.end());
	sort(big.begin(), big.end());
	prefixS[0][0] = 0;
	prefixS[0][1] = 0;
	for (int i = 0; i < (int) small.size(); i++) {
		prefixS[i + 1][0] = prefixS[i][0] + small[i];
	}
	for (int i = 0; i < (int) big.size(); i++) {
		prefixS[i + 1][1] = prefixS[i][1] + big[i];
	}
	long long ans = 0;
	for (int gaps = 0; gaps <= (int) big.size(); gaps++) {
		//	cout << big.size() - gaps << "\n";
		if (min(gaps * d, n - gaps) >= (int) big.size() - gaps) {
			int minNo = max((gaps - 1) * d, (int) big.size() - gaps);
			int bigA = big.size() - gaps;
			if (minNo - (big.size() - gaps) > small.size()) {
				bigA += minNo - (big.size() - gaps) - small.size();
			}
			long long subtract = prefixS[bigA][1];
			assert(minNo - (bigA) <= small.size());
			subtract += prefixS[minNo - (bigA)][0];
			ans = max(ans, total - subtract);
		}
	}
	cout << ans << "\n";
}


#include <bits/stdc++.h>

using namespace std;

#define MAXN 100005

long long prefixS[MAXN][2];
int main() {
#ifdef orange
    freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, d, m;
	cin >> n >> d >> m;
	vector<int> small;
	vector<int> big;
	long long total = 0;
	for (int i = 0; i < n; i++) {
		int temp;
		cin >> temp;
		if (temp <= m) {
			small.push_back(temp);
		} else {
			big.push_back(temp);
		}
		total += temp;
	}
	sort(small.begin(), small.end());
	sort(big.begin(), big.end());
	prefixS[0][0] = 0;
	prefixS[0][1] = 0;
	for (int i = 0; i < (int) small.size(); i++) {
		prefixS[i + 1][0] = prefixS[i][0] + small[i];
	}
	for (int i = 0; i < (int) big.size(); i++) {
		prefixS[i + 1][1] = prefixS[i][1] + big[i];
	}
	long long ans = 0;
	for (int gaps = 0; gaps <= (int) big.size(); gaps++) {
		//	cout << big.size() - gaps << "\n";
		if (min(gaps * d, n - gaps) >= (int) big.size() - gaps) {
			int minNo = max((gaps - 1) * d, (int) big.size() - gaps);
			int bigA = big.size() - gaps;
			if (minNo - (big.size() - gaps) > small.size()) {
				bigA += minNo - (big.size() - gaps) - small.size();
			}
			if (bigA > big.size())
				continue;
			long long subtract = prefixS[bigA][1];
			//	assert(minNo - (bigA) <= small.size());
			subtract += prefixS[minNo - (bigA)][0];
			ans = max(ans, total - subtract);
		}
	}
	cout << ans << "\n";
}


#include <bits/stdc++.h>

using namespace std;

#define MAXN 100005

long long prefixS[MAXN][2];
int main() {
#ifdef orange
    freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, d, m;
	cin >> n >> d >> m;
	vector<int> small;
	vector<int> big;
	long long total = 0;
	for (int i = 0; i < n; i++) {
		int temp;
		cin >> temp;
		if (temp <= m) {
			small.push_back(temp);
		} else {
			big.push_back(temp);
		}
		total += temp;
	}
	sort(small.begin(), small.end());
	sort(big.begin(), big.end());
	prefixS[0][0] = 0;
	prefixS[0][1] = 0;
	for (int i = 0; i < (int) small.size(); i++) {
		prefixS[i + 1][0] = prefixS[i][0] + small[i];
	}
	for (int i = 0; i < (int) big.size(); i++) {
		prefixS[i + 1][1] = prefixS[i][1] + big[i];
	}
	long long ans = 0;
	for (int gaps = 0; gaps <= (int) big.size(); gaps++) {
		//	cout << big.size() - gaps << "\n";
		if (min(gaps * d, n - gaps) >= (int) big.size() - gaps) {
			int minNo = max((gaps - 1) * d, (int) big.size() - gaps);
			int bigA = big.size() - gaps;
			if (minNo - (big.size() - gaps) > small.size()) {
				bigA += minNo - (big.size() - gaps) - small.size();
			}
			long long subtract = prefixS[bigA][1];
		//	assert(minNo - (bigA) <= small.size());
			subtract += prefixS[minNo - (bigA)][0];
			ans = max(ans, total - subtract);
		}
	}
	cout << ans << "\n";
}


#include <bits/stdc++.h>

using namespace std;

#define MAXN 100005

long long prefixS[MAXN][2];
int main() {
#ifdef orange
    freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	long long n, d, m;
	cin >> n >> d >> m;
	vector<int> small;
	vector<int> big;
	long long total = 0;
	for (int i = 0; i < n; i++) {
		int temp;
		cin >> temp;
		if (temp <= m) {
			small.push_back(temp);
		} else {
			big.push_back(temp);
		}
		total += temp;
	}
	sort(small.begin(), small.end());
	sort(big.begin(), big.end());
	prefixS[0][0] = 0;
	prefixS[0][1] = 0;
	for (int i = 0; i < (int) small.size(); i++) {
		prefixS[i + 1][0] = prefixS[i][0] + small[i];
	}
	for (int i = 0; i < (int) big.size(); i++) {
		prefixS[i + 1][1] = prefixS[i][1] + big[i];
	}
	long long ans = 0;
	for (int gaps = 0; gaps <= (int) big.size(); gaps++) {
		//	cout << big.size() - gaps << "\n";
		if (min(gaps * d, n - gaps) >= (int) big.size() - gaps) {
			long long minNo = max((gaps - 1) * d,
					(long long) big.size() - gaps);
			long long bigA = big.size() - gaps;
			if (minNo - (big.size() - gaps) > small.size()) {
				bigA += minNo - (big.size() - gaps) - small.size();
			}
			if (bigA > big.size())
				continue;
			long long subtract = prefixS[bigA][1];
			//	assert(minNo - (bigA) <= small.size());
			subtract += prefixS[minNo - (bigA)][0];
			ans = max(ans, total - subtract);
		}
	}
	cout << ans << "\n";
}


#include <bits/stdc++.h>

using namespace std;

#define MAXN 100005

long long prefixS[MAXN][2];
int main() {
#ifdef orange
    freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	long long n, d, m;
	cin >> n >> d >> m;
	vector<long long> small;
	vector<long long> big;
	long long total = 0;
	for (int i = 0; i < n; i++) {
		long long temp;
		cin >> temp;
		if (temp <= m) {
			small.push_back(temp);
		} else {
			big.push_back(temp);
		}
		total += temp;
	}
	sort(small.begin(), small.end());
	sort(big.begin(), big.end());
	prefixS[0][0] = 0;
	prefixS[0][1] = 0;
	for (int i = 0; i < (int) small.size(); i++) {
		prefixS[i + 1][0] = prefixS[i][0] + small[i];
	}
	for (int i = 0; i < (int) big.size(); i++) {
		prefixS[i + 1][1] = prefixS[i][1] + big[i];
	}
	long long ans = 0;
	for (int gaps = 0; gaps <= (int) big.size(); gaps++) {
		//	cout << big.size() - gaps << "\n";
		if (min(gaps * d, n - gaps) >= (int) big.size() - gaps) {
			long long minNo = max((gaps - 1) * d,
					(long long) big.size() - gaps);
			long long bigA = big.size() - gaps;
			if (minNo - (big.size() - gaps) > small.size()) {
				bigA += minNo - (big.size() - gaps) - small.size();
			}
			if (bigA > big.size())
				continue;
			long long subtract = prefixS[bigA][1];
			//	assert(minNo - (bigA) <= small.size());
			subtract += prefixS[minNo - (bigA)][0];
			ans = max(ans, total - subtract);
		}
	}
	cout << ans << "\n";
}


#include <bits/stdc++.h>

using namespace std;

#define MAXN 100005

long long prefixS[MAXN][2];
int main() {
#ifdef orange
    freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	long long n, d, m;
	cin >> n >> d >> m;
	vector<long long> small;
	vector<long long> big;
	long long total = 0;
	for (int i = 0; i < n; i++) {
		int temp;
		cin >> temp;
		if (temp <= m) {
			small.push_back(temp);
		} else {
			big.push_back(temp);
		}
		total += temp;
	}
	sort(small.begin(), small.end());
	sort(big.begin(), big.end());
	prefixS[0][0] = 0;
	prefixS[0][1] = 0;
	for (int i = 0; i < (int) small.size(); i++) {
		prefixS[i + 1][0] = prefixS[i][0] + small[i];
	}
	for (int i = 0; i < (int) big.size(); i++) {
		prefixS[i + 1][1] = prefixS[i][1] + big[i];
	}
	long long ans = 0;
	for (int gaps = 0; gaps <= (int) big.size(); gaps++) {
		//	cout << big.size() - gaps << "\n";
		if (min(gaps * d, n - gaps) >= (int) big.size() - gaps) {
			long long minNo = max((gaps - 1) * d,
					(long long) big.size() - gaps);
			int bigA = big.size() - gaps;
			if (minNo - (big.size() - gaps) > small.size()) {
				bigA += minNo - (big.size() - gaps) - small.size();
			}
			if (bigA > big.size())
				continue;
			long long subtract = prefixS[bigA][1];
			//	assert(minNo - (bigA) <= small.size());
			subtract += prefixS[minNo - (bigA)][0];
			ans = max(ans, total - subtract);
		}
	}
	cout << ans << "\n";
}


#include <bits/stdc++.h>

using namespace std;

#define MAXN 100005

long long prefixS[MAXN][2];
int main() {
#ifdef orange
    freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	long long n, d, m;
	cin >> n >> d >> m;
	vector<int> small;
	vector<int> big;
	long long total = 0;
	for (int i = 0; i < n; i++) {
		int temp;
		cin >> temp;
		if (temp <= m) {
			small.push_back(temp);
		} else {
			big.push_back(temp);
		}
		total += temp;
	}
	sort(small.begin(), small.end());
	sort(big.begin(), big.end());
	prefixS[0][0] = 0;
	prefixS[0][1] = 0;
	for (int i = 0; i < (int) small.size(); i++) {
		prefixS[i + 1][0] = prefixS[i][0] + small[i];
	}
	for (int i = 0; i < (int) big.size(); i++) {
		prefixS[i + 1][1] = prefixS[i][1] + big[i];
	}
	long long ans = 0;
	for (int gaps = 0; gaps <= (int) big.size(); gaps++) {
		//	cout << big.size() - gaps << "\n";
		if (gaps * d >= (int) big.size() - gaps) {
			long long minNo = max((gaps - 1) * d,
					(long long) big.size() - gaps);
			long long bigA = big.size() - gaps;
			if (minNo - (big.size() - gaps) > small.size()) {
				bigA += minNo - (big.size() - gaps) - small.size();
			}
			if (bigA > big.size())
				continue;
			long long subtract = prefixS[bigA][1];
			//	assert(minNo - (bigA) <= small.size());
			subtract += prefixS[minNo - (bigA)][0];
			ans = max(ans, total - subtract);
		}
	}
	cout << ans << "\n";
}


#include <bits/stdc++.h>

using namespace std;

#define MAXN 100005

long long prefixS[MAXN][2];
int main() {
#ifdef orange
    freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, d, m;
	cin >> n >> d >> m;
	vector<int> small;
	vector<int> big;
	long long total = 0;
	for (int i = 0; i < n; i++) {
		int temp;
		cin >> temp;
		if (temp <= m) {
			small.push_back(temp);
		} else {
			big.push_back(temp);
		}
		total += temp;
	}
	sort(small.begin(), small.end());
	sort(big.begin(), big.end());
	prefixS[0][0] = 0;
	prefixS[0][1] = 0;
	for (int i = 0; i < (int) small.size(); i++) {
		prefixS[i + 1][0] = prefixS[i][0] + small[i];
	}
	for (int i = 0; i < (int) big.size(); i++) {
		prefixS[i + 1][1] = prefixS[i][1] + big[i];
	}
	long long ans = 0;
	for (int gaps = 0; gaps <= (int) big.size(); gaps++) {
		//	cout << big.size() - gaps << "\n";
		if (min(gaps * d, n - gaps) >= (int) big.size() - gaps) {
			int minNo = max((gaps - 1) * d, (int) big.size() - gaps);
			long long subtract = prefixS[big.size() - gaps][1];
			assert(minNo - (big.size() - gaps) <= small.size());
			subtract += prefixS[minNo - (big.size() - gaps)][0];
			ans = max(ans, total - subtract);
		}
	}
	cout << ans << "\n";
}


/*
 * @沉着，冷静！: 噗，这你都信！
 * @LastEditors: HANGNAG
 * @LastEditTime: 2020-08-13 21:39:17
 * @FilePath: \ACM_vscode\codeforces\cf.cpp
 */
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define emplace_back push_back
#define pb push_back
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
const double eps = 1e-6;
const LL inf = 0x3f3f3f3f;
const LL N = 2e6 + 10;
LL a[N], sum[N];
LL n, m;
int main()
{
    LL n, d, m;
    LL num = 0;
    scanf("%lld%lld%lld", &n, &d, &m);
    for (LL i = 1; i <= n; i++)
    {
        scanf("%lld", &a[i]);
        if (a[i] <= m)
        {
            num++;
        }
    }
    sort(a + 1, a + 1 + n);
    for (int i = 1; i <= n;i++)
    {
        sum[i] = sum[i - 1] + a[i];
    }
     LL ans = 0;
    LL u = n - num;
    d++;
    for (int i = 0; i <= u;i++){
        if((i-1)*d<(n-1)&&i*d>=u)
        {
            LL we = n - max(u,(i-1)*d+1);
            ans = max(ans, sum[n] - sum[n - i] + sum[num] - sum[num - we]);
        }
    }
    printf("%lld\n", ans);

    return 0;
}


/*
 * @沉着，冷静！: 噗，这你都信！
 * @LastEditors: HANGNAG
 * @LastEditTime: 2020-08-13 21:41:20
 * @FilePath: \ACM_vscode\codeforces\cf.cpp
 */
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define emplace_back push_back
#define pb push_back
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
const double eps = 1e-6;
const LL inf = 0x3f3f3f3f;
const LL N = 2e6 + 10;
LL a[N], sum[N];
LL n, m;
int main()
{
    LL n, d, m;
    LL num = 0;
    scanf("%lld%lld%lld", &n, &d, &m);
    for (LL i = 1; i <= n; i++)
    {
        scanf("%lld", &a[i]);
        if (a[i] <= m)
        {
            num++;
        }
    }
    sort(a + 1, a + 1 + n);
    for (int i = 1; i <= n;i++)
    {
        sum[i] = sum[i - 1] + a[i];
    }
     LL ans = 0;
    LL u = n - num;
    d++;
    for (int i = 0; i <= u;i++){
        if((i-1)*d<=(n-1)&&i*d>=u)
        {
            LL we = n - max(u,(i-1)*d+1);
            ans = max(ans, sum[n] - sum[n - i] + sum[num] - sum[num - we]);
        }
    }
    printf("%lld\n", ans);

    return 0;
}


/*
 * @沉着，冷静！: 噗，这你都信！
 * @LastEditors: HANGNAG
 * @LastEditTime: 2020-08-13 21:00:39
 * @FilePath: \ACM_vscode\codeforces\cf.cpp
 */
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define emplace_back push_back
#define pb push_back
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
const double eps = 1e-6;
const LL inf = 0x3f3f3f3f;
const LL N = 2e6 + 10;
LL a[N], sum[N];
LL n, m;
int main()
{
    LL n, d, m;
    LL num = 0;
    scanf("%lld%lld%lld", &n, &d, &m);
    for (LL i = 1; i <= n; i++)
    {
        scanf("%lld", &a[i]);
        if (a[i] <= m)
        {
            num++;
        }
    }
    sort(a + 1, a + 1 + n);
    LL ans = a[n];
    for (LL i = 1; i <= n; i++)
    {
        sum[i] = sum[i - 1] + a[i];
    }
    ans += sum[num];
    if (num == n)
    {
        printf("%lld", ans - a[n]);
    }
    else
    {
         LL q = ans;
        for (LL i = n - 1; i > num; i--)
        {

            if ((n - i) * d > (i - num - 1))
            {
                if((n-i)*d-(i-num-1)<=n)
                ans = max(ans, q- sum[(n - i) * d - (i - num - 1)] + sum[n - 1] - sum[i - 1]);
                //printf("%lld %lld %lld\n",ans,sum[(n - i) * d - (i - num - 1)],sum[n - 1] - sum[i - 1]);
            }
            else
            {
                ans = ans + sum[n - 1] - sum[i - 1];
            }
        }
         printf("%lld\n", ans);

    }

    return 0;
}


/*
 * @沉着，冷静！: 噗，这你都信！
 * @LastEditors: HANGNAG
 * @LastEditTime: 2020-08-13 20:56:29
 * @FilePath: \ACM_vscode\codeforces\cf.cpp
 */
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define emplace_back push_back
#define pb push_back
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
const double eps = 1e-6;
const LL inf = 0x3f3f3f3f;
const LL N = 2e5 + 10;
LL a[N], sum[N];
LL n, m;
int main()
{
    LL n, d, m;
    LL num = 0;
    scanf("%lld%lld%lld", &n, &d, &m);
    for (LL i = 1; i <= n; i++)
    {
        scanf("%lld", &a[i]);
        if (a[i] <= m)
        {
            num++;
        }
    }
    sort(a + 1, a + 1 + n);
    LL ans = a[n];
    for (LL i = 1; i <= n; i++)
    {
        sum[i] = sum[i - 1] + a[i];
    }
    ans += sum[num];
    if (num == n)
    {
        printf("%lld", ans - a[n]);
    }
    else
    {
         LL q = ans;
        for (LL i = n - 1; i > num; i--)
        {

            if ((n - i) * d > (i - num - 1))
            {
                ans = max(ans, q- sum[(n - i) * d - (i - num - 1)] + sum[n - 1] - sum[i - 1]);
                //printf("%lld %lld %lld\n",ans,sum[(n - i) * d - (i - num - 1)],sum[n - 1] - sum[i - 1]);
            }
            else
            {
                ans = ans + sum[n - 1] - sum[i - 1];
            }
        }
         printf("%lld\n", ans);

    }

    return 0;
}


/*
 * @沉着，冷静！: 噗，这你都信！
 * @LastEditors: HANGNAG
 * @LastEditTime: 2020-08-13 21:15:20
 * @FilePath: \ACM_vscode\codeforces\cf.cpp
 */
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define emplace_back push_back
#define pb push_back
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
const double eps = 1e-6;
const LL inf = 0x3f3f3f3f;
const LL N = 2e6 + 10;
LL a[N], sum[N];
LL n, m;
int main()
{
    LL n, d, m;
    LL num = 0;
    scanf("%lld%lld%lld", &n, &d, &m);
    for (LL i = 1; i <= n; i++)
    {
        scanf("%lld", &a[i]);
        if (a[i] <= m)
        {
            num++;
        }
    }
    sort(a + 1, a + 1 + n);
    LL ans = a[n];
    for (LL i = 1; i <= n; i++)
    {
        sum[i] = sum[i - 1] + a[i];
    }
    ans += sum[num];
    if (num == n)
    {
        printf("%lld", ans - a[n]);
    }
    else
    {
         LL q = ans;
        for (LL i = n - 1; i > num; i--)
        {

            if ((n - i) * d > (i - num - 1))
            {
                if((n-i)*d-(i-num-1)<=num)
                ans = max(ans, q- sum[(n - i) * d - (i - num - 1)] + sum[n - 1] - sum[i - 1]);
                //printf("%lld %lld %lld\n",ans,sum[(n - i) * d - (i - num - 1)],sum[n - 1] - sum[i - 1]);
            }
            else
            {
                ans = ans + sum[n - 1] - sum[i - 1];
            }
        }
         printf("%lld\n", ans);

    }

    return 0;
}


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

const int maxn = 1e5 + 7;

int n, d, m, l, s;
int large[maxn], small[maxn];

int main() {

    cin >> n >> d >> m;
    int input;
    for(int i=0 ; i<n ; ++i){
        cin >> input;
        if(input > m) large[l++] = input;
        else small[s++] = input;
    }

    sort(large, large+l, greater<int>());
    for(int i=1 ; i<l ; ++i) large[i] += large[i-1];
    sort(small, small+s, greater<int>());
    for(int i=1 ; i<s ; ++i) small[i] += small[i-1];
    for(int i=s ; i<n ; ++i) small[i] = small[i-1];

    int ans = small[s-1];
    cout << ans << endl;
    for(int threshold=1 ; threshold<=l ; ++threshold){
        int num_of_small_value = n - (threshold - 1) * (d + 1) - 1;
        if(num_of_small_value < 0)
            continue;
        ans = max(ans, large[threshold-1] + small[num_of_small_value-1]);
    }
    cout << ans << "\n";

    return 0;
}


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

const int maxn = 1e5 + 7;

int n, d, m, l, s;
long long large[maxn], small[maxn];

int main() {

    cin >> n >> d >> m;
    long long input;
    for(int i=0 ; i<n ; ++i){
        cin >> input;
        if(input > m) large[l++] = input;
        else small[s++] = input;
    }

    if(l == 0) {
        long long ans = 0;
        for(int i=0 ; i<s ; ++i)
            ans += small[i];
        cout << ans << "\n";
        return 0;
    }

    sort(large, large+l, greater<long long>());
    for(int i=1 ; i<l ; ++i) large[i] += large[i-1];
    sort(small, small+s, greater<long long>());
    for(int i=1 ; i<s ; ++i) small[i] += small[i-1];
    for(int i=s ; i<n ; ++i) small[i] = small[i-1];

    long long ans = 0;
    for(int threshold=1 ; threshold<=l ; ++threshold){
        int num_of_small_value = n - (threshold - 1) * (d + 1) - 1;
        if(num_of_small_value <= 0)
            continue;
        ans = max(ans, large[threshold-1] + small[num_of_small_value-1]);
    }
    cout << ans << "\n";

    return 0;
}


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

const int maxn = 1e5 + 7;

int n, d, m, l, s;
long long large[maxn], small[maxn];

int main() {

    cin >> n >> d >> m;
    long long input;
    for(int i=0 ; i<n ; ++i){
        cin >> input;
        if(input > m) large[l++] = input;
        else small[s++] = input;
    }

    if(l == 0) {
        long long ans = 0;
        for(int i=0 ; i<s ; ++i)
            ans += small[i];
        cout << ans << "\n";
        return 0;
    }

    sort(large, large+l, greater<long long>());
    for(int i=1 ; i<l ; ++i) large[i] += large[i-1];
    sort(small, small+s, greater<long long>());
    for(int i=1 ; i<s ; ++i) small[i] += small[i-1];
    for(int i=s ; i<n ; ++i) small[i] = small[i-1];

    long long ans = 0;
    for(int threshold=(l + d) / (1 + d) ; threshold<=l ; ++threshold){
        int num_of_small_value = n - (threshold - 1) * (d + 1) - 1;
        if(num_of_small_value <= 0)
            continue;
        ans = max(ans, large[threshold-1] + small[num_of_small_value-1]);
    }
    cout << ans << "\n";

    return 0;
}


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

const int maxn = 1e5 + 7;

int n, d, m, l, s;
int large[maxn], small[maxn];

int main() {

    cin >> n >> d >> m;
    int input;
    for(int i=0 ; i<n ; ++i){
        cin >> input;
        if(input > m) large[l++] = input;
        else small[s++] = input;
    }

    if(l == 0) {
        int ans = 0;
        for(int i=0 ; i<s ; ++i)
            ans += small[i];
        cout << ans << "\n";
        return 0;
    }

    sort(large, large+l, greater<int>());
    for(int i=1 ; i<l ; ++i) large[i] += large[i-1];
    sort(small, small+s, greater<int>());
    for(int i=1 ; i<s ; ++i) small[i] += small[i-1];

    int ans = 0;
    for(int threshold=1 ; threshold<=l ; ++threshold){
        int num_of_small_value = n - (threshold - 1) * (d + 1) - 1;
        if(num_of_small_value < 0)
            continue;
        ans = max(ans, large[threshold-1] + small[num_of_small_value-1]);
    }
    cout << ans << "\n";

    return 0;
}


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

const int maxn = 1e5 + 7;

int n, d, m, l, s;
long long large[maxn], small[maxn];

int main() {

    cin >> n >> d >> m;
    long long input;
    for(int i=0 ; i<n ; ++i){
        cin >> input;
        if(input > m) large[l++] = input;
        else small[s++] = input;
    }

    if(l == 0) {
        long long ans = 0;
        for(int i=0 ; i<s ; ++i)
            ans += small[i];
        cout << ans << "\n";
        return 0;
    }

    sort(large, large+l, greater<long long>());
    for(int i=1 ; i<l ; ++i) large[i] += large[i-1];
    sort(small, small+s, greater<long long>());
    for(int i=1 ; i<s ; ++i) small[i] += small[i-1];
    for(int i=s ; i<n ; ++i) small[i] = small[i-1];

    long long ans = 0;
    for(int threshold=1 ; threshold<=l ; ++threshold){
        int num_of_small_value = n - (threshold - 1) * (d + 1) - 1;
        if(num_of_small_value < 0)
            continue;
        ans = max(ans, large[threshold-1] + small[num_of_small_value-1]);
    }
    cout << ans << "\n";

    return 0;
}


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

const int maxn = 1e5 + 7;

int n, d, m, l, s;
int large[maxn], small[maxn];

int main() {

    cin >> n >> d >> m;
    int input;
    for(int i=0 ; i<n ; ++i){
        cin >> input;
        if(input > m) large[l++] = input;
        else small[s++] = input;
    }

    sort(large, large+l, greater<int>());
    for(int i=1 ; i<l ; ++i) large[i] += large[i-1];
    sort(small, small+s, greater<int>());
    for(int i=1 ; i<s ; ++i) small[i] += small[i-1];
    for(int i=s ; i<n ; ++i) small[i] = small[i-1];

    int ans = small[s-1];
    for(int threshold=1 ; threshold<=l ; ++threshold){
        int num_of_small_value = n - (threshold - 1) * (d + 1) - 1;
        if(num_of_small_value < 0)
            continue;
        ans = max(ans, large[threshold-1] + small[num_of_small_value-1]);
    }
    cout << ans << "\n";

    return 0;
}


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

const int maxn = 1e5 + 7;

int n, d, m, l, s;
int large[maxn], small[maxn];

int main() {

    cin >> n >> d >> m;
    int input;
    for(int i=0 ; i<n ; ++i){
        cin >> input;
        if(input > m) large[l++] = input;
        else small[s++] = input;
    }

    if(l == 0) {
        int ans = 0;
        for(int i=0 ; i<s ; ++i)
            ans += small[i];
        cout << ans << "\n";
        return 0;
    }

    sort(large, large+l, greater<int>());
    for(int i=1 ; i<l ; ++i) large[i] += large[i-1];
    sort(small, small+s, greater<int>());
    for(int i=1 ; i<s ; ++i) small[i] += small[i-1];

    int ans = small[s-1];
    for(int threshold=1 ; threshold<=l ; ++threshold){
        int num_of_small_value = n - (threshold - 1) * (d + 1) - 1;
        if(num_of_small_value < 0)
            continue;
        ans = max(ans, large[threshold-1] + small[num_of_small_value-1]);
    }
    cout << ans << "\n";

    return 0;
}


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

const int maxn = 1e5 + 7;

int n, d, m, l, s;
long long large[maxn], small[maxn];

int main() {

    cin >> n >> d >> m;
    long long input;
    for(int i=0 ; i<n ; ++i){
        cin >> input;
        if(input > m) large[l++] = input;
        else small[s++] = input;
    }

    if(l == 0) {
        long long ans = 0;
        for(int i=0 ; i<s ; ++i)
            ans += small[i];
        cout << ans << "\n";
        return 0;
    }

    sort(large, large+l, greater<long long>());
    for(int i=1 ; i<l ; ++i) large[i] += large[i-1];
    sort(small, small+s, greater<long long>());
    for(int i=1 ; i<s ; ++i) small[i] += small[i-1];
    for(int i=s ; i<n ; ++i) small[i] = small[i-1];

    long long ans = 0;
    for(int threshold=1 ; threshold<=l ; ++threshold){
        long long  num_of_small_value = n - 1ll * (threshold - 1) * (d + 1) - 1;
        if(num_of_small_value <= 0)
            continue;
        ans = max(ans, large[threshold-1] + small[num_of_small_value-1]);
    }
    cout << ans << "\n";

    return 0;
}


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

const int maxn = 1e5 + 7;

int n, d, m, l, s;
long long large[maxn], small[maxn];

int main() {

    cin >> n >> d >> m;
    long long input;
    for(int i=0 ; i<n ; ++i){
        cin >> input;
        if(input > m) large[l++] = input;
        else small[s++] = input;
    }

    if(l == 0) {
        long long ans = 0;
        for(int i=0 ; i<s ; ++i)
            ans += small[i];
        cout << ans << "\n";
        return 0;
    }

    sort(large, large+l, greater<long long>());
    for(int i=1 ; i<l ; ++i) large[i] += large[i-1];
    sort(small, small+s, greater<long long>());
    for(int i=1 ; i<s ; ++i) small[i] += small[i-1];

    long long ans = 0;
    for(int threshold=1 ; threshold<=l ; ++threshold){
        int num_of_small_value = n - (threshold - 1) * (d + 1) - 1;
        if(num_of_small_value < 0)
            continue;
        ans = max(ans, large[threshold-1] + small[num_of_small_value-1]);
    }
    cout << ans << "\n";

    return 0;
}


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

#define REP(i,n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define ll long long int
#define INF 1000000000
#define endl '\n'
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
ll n , k;
cin>>n>>k;
deque<ll> q;
unordered_set <ll,custom_hash>s;int a;
for(ll i = 0 ; i< n; i++){
	cin>>a;
	if(s.find(a) != s.end())continue;
	else
	{
		if(i<k)
		{
		q.push_front(a);
		s.insert(a);
	    }
		else  if(q.size() == k)
		{
	    s.erase(q.back());
	    s.insert(a);
	    q.pop_back();
	    q.push_front(a);
	    }
	    else
	    q.push_front(a),
		s.insert(a);
	}
}
cout<<q.size()<<endl;
for(auto it:q){
	cout<<it<<" ";
}
return 0;

}


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

#define REP(i,n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define ll long long int
#define INF 1000000000
#define endl '\n'
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
ll n , k;
cin>>n>>k;
deque<ll> q;
unordered_set <ll,custom_hash>s;int a;
for(ll i = 0 ; i< n; i++){
	cin>>a;
	if(s.find(a) != s.end())continue;
	else
	{
		if(i<k)
		{
		q.push_front(a);
		s.insert(a);
	   }
		else
		{
	    s.erase(q.back());
	    s.insert(a);
	    q.pop_back();
	    q.push_front(a);
	    }
	}
}
cout<<q.size()<<endl;
for(auto it:q){
	cout<<it<<" ";
}
return 0;

}


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

#define REP(i,n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define ll long long int
#define INF 1000000000
#define endl '\n'
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
int main(){
ll n , k;
cin>>n>>k;
deque<ll> q;
unordered_set <ll>s;int a;
for(ll i = 0 ; i< n; i++){
	cin>>a;
	if(s.find(a) != s.end())continue;
	else
	{
		if(i<k)
		q.push_front(a),
		s.insert(a);
		else
		{
	    s.erase(q.back());
	    s.insert(a);
	    q.pop_back();
	    q.push_front(a);
	    }
	}
}
cout<<q.size()<<endl;
for(auto it:q){
	cout<<it<<" ";
}
return 0;

}


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

#define REP(i,n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define ll long long int
#define INF 1000000000
#define endl '\n'
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
ll n , k;
cin>>n>>k;
deque<ll> q;
unordered_set <ll,custom_hash>s;int a;
for(ll i = 0 ; i< n; i++){
	cin>>a;
	if(s.find(a) != s.end())continue;
		if(q.size() == k)
		{
			s.erase(q.back());
			q.pop_back();
		}
		q.push_front(a);
		s.insert(a);
}
cout<<q.size()<<endl;
for(auto it:q){
	cout<<it<<" ";
}
return 0;

}


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

#define REP(i,n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define ll long long int
#define INF 1000000000
#define endl '\n'
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
ll n , k;
cin>>n>>k;
deque<ll> q;
unordered_set <ll>s;int a;
for(ll i = 0 ; i< n; i++){
	cin>>a;
	if(s.find(a) != s.end())continue;
		if(q.size() == k)
		{
			s.erase(q.back());
			q.pop_back();
		}
		q.push_front(a);
		s.insert(a);
}
cout<<q.size()<<endl;
for(auto it:q){
	cout<<it<<" ";
}
return 0;

}


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

#define REP(i,n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define ll long long int
#define INF 1000000000
#define endl '\n'
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
ll n , k;
cin>>n>>k;
deque<ll> q;
unordered_set <ll,custom_hash>s;int a;
for(ll i = 0 ; i< n; i++){
	cin>>a;
	if(s.find(a) != s.end())continue;
	else
	{
		if(i<k)
		{
		q.push_front(a);
		s.insert(a);
	    }
		else  if(q.size() == k)
		{
	    s.erase(q.back());
	    s.insert(a);
	    q.pop_back();
	    q.push_front(a);
	    }
	    q.push_front(a);
		s.insert(a);
	}
}
cout<<q.size()<<endl;
for(auto it:q){
	cout<<it<<" ";
}
return 0;

}


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

#define REP(i,n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define ll long long int
#define INF 1000000000
#define endl '\n'
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
int main(){
ll n , k;
cin>>n>>k;
deque<ll> q;
unordered_set <ll>s;int a;
for(ll i = 0 ; i< n; i++){
	cin>>a;
	if(s.find(a) != s.end())continue;
	else
	{
		if(q.size() == k)
		{
			q.pop_back();
			s.erase(q.back());
		}
		q.push_front(a);
		s.insert(a);
	}
}
cout<<q.size()<<endl;
for(auto it:q){
	cout<<it<<" ";
}
return 0;

}


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

#define REP(i,n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define ll long long int
#define INF 1000000000
#define endl '\n'
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
int main(){
ll n , k;
cin>>n>>k;
deque<ll> q;
unordered_set <ll>s;int a;
for(ll i = 0 ; i< n; i++){
	cin>>a;
	if(s.find(a) != s.end())continue;
		if(q.size() == k)
		{
			s.erase(q.back());
			q.pop_back();
		}
		q.push_front(a);
		s.insert(a);
}
cout<<q.size()<<endl;
for(auto it:q){
	cout<<it<<" ";
}
return 0;

}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD 1000000007
#define eps 1e-8
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
//ifstream cinn("input.txt");ofstream coutt("output.txt");

signed main(){
    FASTER;
    map<int,bool> mp;
    int n,k;cin>>n>>k;
    vi v(n);cin>>v;
    deque<int> dq;

    for(int i=0;i<k;i++){
        if(!mp[v[i]]){
            dq.push_front(v[i]);
            mp[v[i]]=1;
        }
    }

    for(int i=k;i<n;i++){
        if(!mp[v[i]]){
            mp[v[i]]=1;
            dq.push_front(v[i]);
            if(dq.size()==k){
                mp[dq.back()]=0;
                dq.pop_back();
            }
        }
    }
    cout<<dq.size()<<"\n";
    for(int i:dq){
        cout<<i<<" ";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD 1000000007
#define eps 1e-8
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
//ifstream cinn("input.txt");ofstream coutt("output.txt");

signed main(){
    FASTER;
    map<int,bool> mp;
    int n,k;cin>>n>>k;
    vi v(n);cin>>v;
    deque<int> dq;

    for(int i=0;i<k;i++){
        if(!mp[v[i]]){
            dq.push_front(v[i]);
            mp[v[i]]=1;
        }
    }

    for(int i=k;i<n;i++){
        if(!mp[v[i]]){
            mp[v[i]]=1;
            dq.push_front(v[i]);
            if(dq.size()>k){
                mp[dq.back()]=0;
                dq.pop_back();
            }
        }
    }
    cout<<dq.size()<<"\n";
    for(int i:dq){
        cout<<i<<" ";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD 1000000007
#define eps 1e-8
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
//ifstream cinn("input.txt");ofstream coutt("output.txt");

signed main(){
    FASTER;
    map<int,bool> mp;
    int n,k;cin>>n>>k;
    vi v(n);cin>>v;
    deque<int> dq;

    for(int i=0;i<k;i++){
        if(!mp[v[i]]){
            dq.push_front(v[i]);
            mp[v[i]]=1;
        }
    }

    for(int i=k;i<n;i++){
        if(!mp[v[i]]){
            if(dq.size()==k){
                mp[dq.back()]=0;
                dq.pop_back();
            }
            mp[v[i]]=1;
            dq.push_front(v[i]);
        }
    }
    cout<<dq.size()<<"\n";
    for(int i:dq){
        cout<<i<<" ";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD 1000000007
#define eps 1e-8
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
//ifstream cinn("input.txt");ofstream coutt("output.txt");

signed main(){
    FASTER;
    map<int,bool> mp;
    int n,k;cin>>n>>k;
    vi v(n);cin>>v;
    deque<int> dq;

    for(int i=0;i<k;i++){
        if(!mp[v[i]]){
            dq.push_front(v[i]);
            mp[v[i]]=1;
        }
    }

    for(int i=k;i<n;i++){
        if(!mp[v[i]]){
            mp[v[i]]=1;
            dq.push_front(v[i]);
            if(dq.size()>k){
                mp.erase(dq.back());
                dq.pop_back();
            }
        }
    }
    cout<<dq.size()<<"\n";
    for(int i:dq){
        cout<<i<<" ";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD 1000000007
#define eps 1e-8
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
//ifstream cinn("input.txt");ofstream coutt("output.txt");

signed main(){
    FASTER;
    map<int,bool> mp;
    int n,k;cin>>n>>k;
    vi v(n);cin>>v;
    deque<int> dq;

    for(int i=0;i<k && i<n;i++){
        if(!mp[v[i]]){
            dq.push_front(v[i]);
            mp[v[i]]=1;
        }
    }

    for(int i=k;i<n;i++){
        if(!mp[v[i]]){
            mp[v[i]]=1;
            dq.push_front(v[i]);
            if(dq.size()>k){
                mp[dq.back()]=0;
                dq.pop_back();
            }
        }
    }
    cout<<dq.size()<<"\n";
    for(int i:dq){
        cout<<i<<" ";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD 1000000007
#define eps 1e-8
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
//ifstream cinn("input.txt");ofstream coutt("output.txt");

signed main(){
    FASTER;
    unordered_map<int,int> mp;
    int n,k;cin>>n>>k;
    vi v(n);cin>>v;
    deque<int> dq;

    for(int i=0;i<k;i++){
        if(!mp[v[i]]){
            dq.push_front(v[i]);
            mp[v[i]]=1;
        }
    }

    for(int i=0;i<n;i++){
        if(!mp[v[i]]){
            mp[v[i]]=1;
            dq.push_front(v[i]);
            mp[dq.back()]=0;
            dq.pop_back();
        }
    }
    for(int i:dq){
        cout<<i<<" ";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD 1000000007
#define eps 1e-8
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
//ifstream cinn("input.txt");ofstream coutt("output.txt");

signed main(){
    FASTER;
    unordered_map<int,int> mp;
    int n,k;cin>>n>>k;
    vi v(n);cin>>v;
    deque<int> dq;

    for(int i=0;i<k;i++){
        if(!mp[v[i]]){
            dq.push_front(v[i]);
            mp[v[i]]=1;
        }
    }

    for(int i=k;i<n;i++){
        if(!mp[v[i]]){
            mp[v[i]]=1;
            dq.push_front(v[i]);
            mp[dq.back()]=0;
            dq.pop_back();
        }
    }
    cout<<dq.size()<<"\n";
    for(int i:dq){
        cout<<i<<" ";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD 1000000007
#define eps 1e-8
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
//ifstream cinn("input.txt");ofstream coutt("output.txt");

signed main(){
    FASTER;
    unordered_map<int,int> mp;
    int n,k;cin>>n>>k;
    vi v(n);cin>>v;
    deque<int> dq;

    for(int i=0;i<k;i++){
        if(!mp[v[i]]){
            dq.push_front(v[i]);
            mp[v[i]]=1;
        }
    }

    for(int i=k;i<n;i++){
        if(!mp[v[i]]){
            mp[v[i]]=1;
            dq.push_front(v[i]);
            if(dq.size()>k){
                mp[dq.back()]=0;
                dq.pop_back();
            }
        }
    }
    cout<<dq.size()<<"\n";
    for(int i:dq){
        cout<<i<<" ";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD 1000000007
#define eps 1e-8
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
//ifstream cinn("input.txt");ofstream coutt("output.txt");

signed main(){
    FASTER;
    unordered_map<int,int> mp;
    int n,k;cin>>n>>k;
    vi v(n);cin>>v;
    deque<int> dq;

    for(int i=0;i<k;i++){
        if(!mp[v[i]]){
            dq.push_front(v[i]);
            mp[v[i]]=1;
        }
    }

    for(int i=k;i<n;i++){
        if(!mp[v[i]]){
            mp[v[i]]=1;
            dq.push_front(v[i]);
            mp[dq.back()]=0;
            dq.pop_back();
        }
    }
    for(int i:dq){
        cout<<i<<" ";
    }
}


#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD 1000000007
#define eps 1e-8
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
//ifstream cinn("input.txt");ofstream coutt("output.txt");

signed main(){
    FASTER;
    unordered_map<int,bool> mp;
    int n,k;cin>>n>>k;
    vi v(n);cin>>v;
    deque<int> dq;

    for(int i=0;i<k;i++){
        if(!mp[v[i]]){
            dq.push_front(v[i]);
            mp[v[i]]=1;
        }
    }

    for(int i=k;i<n;i++){
        if(!mp[v[i]]){
            mp[v[i]]=1;
            dq.push_front(v[i]);
            if(dq.size()>k){
                mp.erase(dq.back());
                dq.pop_back();
            }
        }
    }
    cout<<dq.size()<<"\n";
    for(int i:dq){
        cout<<i<<" ";
    }
}