Cod sursa(job #919381)

Utilizator apopeid13Apopeid Alejandro apopeid13 Data 19 martie 2013 17:01:53
Problema Caramizi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <algorithm>
#include <fstream>
#include <cstdlib>
 
using namespace std;
  
ifstream fin("caramizi.in");
ofstream fout("caramizi.out");
  
typedef long long i64;
const int nmax= 200000, xmax= 1000000;
  
int v[nmax+2];
i64 lo[xmax+1], hi[nmax+2];
  
char *buffer;
 
int read_int(){
    while (*buffer<'0'|| *buffer>'9'){
        ++buffer;
    }
    int x= 0;
    while (*buffer>='0'&& *buffer<='9'){
        x= 10*x+*buffer-'0';
        ++buffer;
    }
    return x;
}
 
int main()
{
    fin.seekg(0, ios::end);
    int fs= fin.tellg();
    fin.seekg(0, ios::beg);
    buffer= (char*)malloc(fs);
    fin.getline(buffer, fs, 0);
 
    int n, nq;
    n= read_int(); nq= read_int();
    for (int i= 1; i<=n; ++i){
        v[i]= read_int();
    }
    sort(v+1, v+n+1);
  
    for (int i= 1; i<v[1]; ++i){
        lo[i]= n*i;
    }
    i64 s= 0;
    v[n+1]= xmax+1;
    for (int i= 1; i<=n; ++i){
        s+= v[i];
        for (int j= v[i]; j<= v[i+1]; ++j){
            lo[j]= max(lo[j-1], s-s%j+((i64)n-i)*j);
        }
    }
    for (int i= s/v[n]; i>0; --i){
        hi[i]= max(hi[i+1], s-s%i); 
    }
  
    for (int cq= 1; cq<=nq; ++cq){
        int x;
        x= read_int();
        if (x<=xmax){
            fout<<lo[x]<<"\n";
        }else{
            fout<<hi[s/x+1]<<"\n";
        }
    }
  
    return 0;
}