Pagini recente » Cod sursa (job #2024835) | Cod sursa (job #1686307) | Cod sursa (job #2596170) | Cod sursa (job #739753) | Cod sursa (job #252530)
Cod sursa(job #252530)
Utilizator |
Liviu Ciortea efer |
Data |
4 februarie 2009 16:03:34 |
Problema |
Caramizi |
Scor |
Ascuns |
Compilator |
cpp |
Status |
done |
Runda |
|
Marime |
1.3 kb |
#include <cstdio>
#include <cassert>
#include <algorithm>
using namespace std;
const int MAX_N = 200001;
const int MAX_M = 200001;
const int MAX_H = 1000001;
const int MAX_L = 2000000001;
typedef long long LL;
int N, M, C[MAX_N], L[MAX_M];
LL bst[MAX_H], res[MAX_M];
int bst_idx[MAX_H];
void read_data() {
FILE* fin = fopen("caramizi.in", "rt");
fscanf(fin, "%d %d", &N, &M);
assert(1 <= N && N < MAX_N);
assert(1 <= M && M < MAX_M);
for (int i = 0; i < N; ++i) {
fscanf(fin, "%d", &C[i]);
assert(1 <= C[i] && C[i] < MAX_H);
}
for (int i = 0; i < M; ++i) {
fscanf(fin, "%d", &L[i]);
assert(1 <= L[i] && L[i] < MAX_L);
}
fclose(fin);
}
void solve() {
sort(C, C+N);
LL sum = 0;
int idx = 0, n = 1;
bst_idx[0] = 0;
for (; n < MAX_H; ++n) {
bst[n] = (LL)n * (sum/n + (N - idx));
if (bst[n] > bst[n-1]) bst_idx[n] = n;
else {
bst[n] = bst[n-1];
bst_idx[n] = bst_idx[n-1];
}
while (idx < N && C[idx] == n) {
sum += C[idx++];
}
}
for (int i = 0; i < M; ++i) {
res[i] = bst[L[i]];
}
}
void write_results() {
FILE* fout = fopen("caramizi.out", "wt");
for (int i = 0; i < M; ++i) {
fprintf(fout, "%lld\n", res[i]);
}
fclose(fout);
}
int main() {
read_data();
solve();
write_results();
return 0;
}