Pagini recente » Cod sursa (job #14280) | Cod sursa (job #879343) | Cod sursa (job #2064109) | Cod sursa (job #1567916) | Cod sursa (job #332954)
Cod sursa(job #332954)
#include <stdio.h>
#include <algorithm>
using namespace std;
#define NMAX 200010
#define LL long long
int N, M;
int a[NMAX];
LL h[NMAX];
LL nc[NMAX];
int main()
{
int i, j;
freopen("caramizi.in", "r", stdin);
freopen("caramizi.out", "w", stdout);
scanf("%d %d", &N, &M);
for (i = 1; i <= N; i++) scanf("%d", &a[i]);
sort(a + 1, a + N + 1);
LL sa = 0, sc = 0;
j = 0;
for (i = 1; i <= N; i++) {
while (j < N && (LL) a[j + 1] * (j + 1 - i) - sc <= sa) {
j++;
sc += a[j];
}
h[i] = a[j] + (sa - ((LL) a[j] * (j - i + 1) - sc)) / (j - i + 1);
nc[i] = (LL) a[j] * h[i] + sa;
sa += a[i];
sc -= a[i];
}
for (i = 1; i <= N; i++) nc[i] = max(nc[i - 1], (LL) h[i] * (N - i + 1));
int stepmax, val;
for (stepmax = 1; stepmax <= N; stepmax <<= 1);
for (i = 1; i <= M; i++) {
scanf("%d", &val);
int x = 0;
for (int step = stepmax; step; step >>= 1)
if (x + step <= N && h[x + step] < val)
x += step;
printf("%lld\n", max(nc[x], (LL) val * (N - x)));
}
return 0;
}