Result: Accepted
Time: 4916ms
Memory: 3440kB
#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long int
const int inf=0x3f3f3f3f;
using namespace std;
LL a[100100];
LL b0[100100],b1[100100];
int main ()
{
int n,s;
scanf("%d%d",&n,&s);
memset(a,inf,sizeof(a));
memset(b0,inf,sizeof(b0));
LL sum2=0,sum=0;
for (int i = 1; i <=n ; i++)
{
scanf("%lld",&a[i]);
if(a[i]<s)sum++;
}
sort(a+1,a+1+n);
int tot=0;
for (int i = 1; i <= n; )
{
int we=upper_bound(a+1,a+1+n,a[i])-a;
b0[++tot]=a[i];
b1[tot]=we-i;
i=we;
}
for (int j = 1; j <= tot; j++)
{
int sum1=b1[j];
for(int k=2;k<=s;k++)
{
int wo=upper_bound(b0+1,b0+1+tot,k*b0[j])-b0;
if(b0[wo-1]==k*b0[j])
{
sum1+=b1[wo-1];
}
}
if(sum1>sum2)
{
sum2=sum1;
}
}
for(int j=1;j<=7;j++)
{
int sum1=0;
for(int i=1;i<=s;i++)
{
int qa=upper_bound(b0+1,b0+1+tot,j*i)-b0;
if(b0[qa-1]==i*j)
{
sum1+=b1[qa-1];
}
}
if(sum1>sum2)
{
sum2=sum1;
}
}
printf("%lld\n",max(sum2,sum));
}