Cod sursa(job #1990415)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 11 iunie 2017 19:37:34
Problema Caramizi Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <fstream>
#include <algorithm>

using namespace std;

ofstream fout ("caramizi.out");

class InputReader {
	public:
        InputReader() {}
        InputReader(const char *name) {
            fin = fopen(name, "r");
			buffpos = Size - 1;
        }
        inline InputReader &operator >>(int &n) {
			char ch = nextpos();
            while(ch < '0' || ch > '9') {
                ch = nextpos();
            }

            n = 0;
            while('0' <= ch && ch <= '9') {
                n = n * 10 + ch - '0';
                ch = nextpos();
            }
            return *this;
        }
    private:
        FILE *fin;
        static const int Size = 1 << 17;
        char buff[Size];
        int buffpos;

        inline char nextpos() {
            ++ buffpos;
            if(buffpos == Size) {
                fread(buff, Size, 1, fin);
                buffpos = 0;
            }
			return buff[ buffpos ];
        }
} fin ("caramizi.in");

typedef long long i64;

const int nmax = 2e5;
const int cmax = 1e6;

int v[nmax + 1];
i64 sol[cmax + 1], solh[nmax + 5];

int main() {
    int n, m;
    fin >> n >> m;

    for (int i = 1; i <= n; ++ i) {
        fin >> v[ i ];
    }
    sort(v + 1, v + n + 1);

    i64 sum = 0;
    int j = 0;
    for (int i = 1; i <= cmax; ++ i) {
        while (j + 1 <= n && v[j + 1] <= i) {
            sum += v[ ++ j ];
        }

        sol[ i ] = max(sol[i - 1], 1LL * (n - j) * i + sum / i * i);
    }

    for (int i = sum / v[ n ]; i >= sum / cmax && i > 0; -- i) {
        solh[ i ] = max(solh[i + 1], sum / i * i);
    }

    for (int i = 1; i <= m; ++ i) {
        int x;
        fin >> x;

        if (x <= cmax) {
            fout << sol[ x ] << "\n";
        } else {
            int k = sum / x;
            if (sum % x) ++ k;
            fout << solh[ k ] << "\n";
        }
    }

    fout.close();
    return 0;
}