Cod sursa(job #1758879)

Utilizator fanache99Constantin-Buliga Stefan fanache99 Data 18 septembrie 2016 00:19:03
Problema Caramizi Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

ifstream cin("caramizi.in");
ofstream cout("caramizi.out");

const int MAXN = 200000;
const int MAXVAL = 1000000;

long long v[1 + MAXN + 1];
long long dp[1 + MAXVAL], height[1 + MAXVAL];

int main() {
    long long n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> v[i];
    sort(v + 1, v + n + 1);
    v[0] = 1;
    v[n + 1] = MAXVAL + 1;
    long long sum = 0;
    for (int i = 0; i <= n; i++) {
        if (i)
            sum += v[i];
        for (int j = v[i]; j < v[i + 1]; j++)
            dp[j] = max(sum - sum % j + (n - i) * j, dp[j - 1]);
    }
    for (int i = sum / v[n]; i; i--)
        height[i] = max(sum - sum % i, height[i + 1]);
    for (int i = 1; i <= m; i++) {
        long long limit;
        cin >> limit;
        if (limit < MAXVAL)
            cout << dp[limit] << "\n";
        else
            cout << height[sum / limit + 1] << "\n";
    }
    return 0;
}