Cod sursa(job #2087615)

Utilizator stefdascalescuStefan Dascalescu stefdascalescu Data 13 decembrie 2017 21:23:49
Problema Caramizi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.98 kb
#include<bits/stdc++.h>
using namespace std;
class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n) {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
}f("caramizi.in");
ofstream g("caramizi.out");
int n,m;
int v[200002];
long long s,max1[1000002],ct,cp[1000002];
int main()
{
    f>>n>>m;
    for(int i=1;i<=n;++i)
        f>>v[i];
    sort(v+1,v+n+1);
    ct=0;
    for(int i=1;i<=v[n];++i)
    {
        max1[i]=max1[i-1];
        while(i>v[ct+1])
            ++ct,s+=v[ct];
        max1[i]=max(max1[i],1ll*(n-ct)*i+s-s%i);
    }
    while(ct+1<=n)
        s+=v[ct+1],++ct;
    for(long long i=v[n]+1;i<=1000000;++i)
        max1[i]=max(max1[i-1],s-s%i);
    for(int i=s/v[n];i>=1;--i)
        cp[i]=max(cp[i+1],s-s%i);
    for(int i = 1;i<=m;++i){
        int x;
        f>>x;
        if(x<=1000000)
            g<<max1[x]<<'\n';
        else
            g<<cp[s/x+1]<<'\n';
    }
    return 0;
}