Pagini recente » Cod sursa (job #813291) | Cod sursa (job #1908797) | Cod sursa (job #1410246) | Cod sursa (job #3229353) | Cod sursa (job #1945624)
#include <iostream>
#include <fstream>
#define DIM 1000010
#include <algorithm>
using namespace std;
int n,m,a[DIM],MAX;
long long s[DIM],sol[DIM],sol2[DIM];
void read()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
}
void solve()
{
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)s[i]=a[i]+s[i-1];
MAX=1000007;
int k=1;
for(int i=1;i<=MAX;i++)
{
while(i>=a[k]&&k<=n)++k;
long long rez=1LL*s[k-1]+1LL*(n-k+1)*i;
sol[i]=max(sol[i-1],rez-rez%i);
}
int d;
for(int i=s[n]/a[n];i>=1;i--)
sol2[i]=max(sol2[i+1],s[n]-s[n]%i);
for(m=m;m>0;--m)
{
scanf("%d",&d);
if(d<=MAX)printf("%lld\n",sol[d]); else
printf("%lld\n",sol2[(s[n]/d)+1]);
}
}
int main()
{
freopen("caramizi.in","r",stdin);
freopen("caramizi.out","w",stdout);
read();
solve();
return 0;
}