This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#define N 500000
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
long long gcd(long long a, long long b) {
return b == 0 ? a : gcd(b, a % b);
}
void sort(long long *aa, int l, int r) {
while (l < r) {
int i = l, j = l, k = r;
long long a = aa[l + rand_() % (r - l)], tmp;
while (j < k)
if (aa[j] == a)
j++;
else if (aa[j] < a) {
tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
i++, j++;
} else {
k--;
tmp = aa[j], aa[j] = aa[k], aa[k] = tmp;
}
sort(aa, l, i);
l = k;
}
}
int main() {
static long long aa[N], dd[N], ans[N + 1];
int n, i, j, k;
long long sum, sum_, d, x;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%lld", &aa[i]);
sort(aa, 0, n);
for (i = 0; i < n; i++)
dd[i] = aa[i];
sum = 0;
for (i = 0; i < n; i++)
sum += aa[i];
ans[n] = sum;
d = aa[0];
for (i = 1; i < n; i++)
if (aa[i] % d == 0) {
sum -= aa[i];
ans[n - i] = max(ans[n - i], sum);
} else {
x = INF;
for (j = i; j < n; j++) {
dd[j] = gcd(dd[j], d);
x = min(x, d + aa[j] - dd[j]);
}
sum_ = sum, k = n - i + 1;
for (j = i; j < n && aa[j] <= x; j++)
if (aa[j] == aa[j - 1]) {
sum_ -= aa[j], k--;
ans[k] = max(ans[k], sum_);
}
sum_ -= x, k--;
ans[k] = max(ans[k], sum_);
for ( ; j < n; j++)
if (aa[j] == aa[j - 1]) {
sum_ -= aa[j], k--;
ans[k] = max(ans[k], sum_);
}
sum -= d + aa[i] - dd[i], d = dd[i];
}
for (k = 1; k <= n; k++)
printf("%lld\n", ans[k]);
return 0;
}
Compilation message (stderr)
Main.c: In function 'main':
Main.c:44:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:46:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | scanf("%lld", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |