Cod sursa(job #1807079)

Utilizator silkMarin Dragos silk Data 15 noiembrie 2016 23:44:22
Problema Caramizi Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <cstdio>
#include <algorithm>
#define VMax 1000000
#define NMax 200000
#define ll long long
#define MAX(a,b)((a)>(b)?(a):(b))
using namespace std;

ll best[VMax+1];
ll good[NMax+1];
int v[NMax+1];
int N;

void Precalc()
{
    int i,j;
    ll S,temp;
    for(S = j = 0, i = 1; i <= VMax; ++i)
    {
        while( v[j] <= i && j <= N ) S += v[j++];
        temp = 1LL * ( N-j+1 ) * i;

        best[i] = ( S + temp ) - ( S + temp ) % i;
        best[i] = MAX( best[i-1], best[i] );
    }

    for(i = NMax; i >= 1; --i)
    {
        good[i] = i * ( S/i );
        good[i] = MAX( good[i-1], good[i] );
    }
}

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

    int i,x,M;
    ll S;

    scanf("%d %d",&N,&M);
    for(i = 1; i <= N; ++i) scanf("%d",&v[i]);
    sort(v+1,v+N+1);

    Precalc();

    for(S = 0, i = 1; i <= N; ++i) S += v[i];
    for(i = 1; i <= M; ++i)
    {
        scanf("%d",&x);
        if( x <= VMax ) printf("%lld\n",best[x]);
        else printf("%lld\n", MAX( best[VMax], MAX( good[S/x+1], S - S % x ) ) );
    }




return 0;
}